blob: 82390ad8b1a23361d7bd4f0b09d9c97324599b3e [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. Camerona0c12412011-10-26 16:22:04 -050050#include <linux/jiffies.h>
Don Brace42a91642014-11-14 17:26:27 -060051#include <linux/percpu-defs.h>
Stephen M. Cameron094963d2014-05-29 10:53:18 -050052#include <linux/percpu.h>
Don Brace2b08b3e2015-01-23 16:41:09 -060053#include <asm/unaligned.h>
Stephen M. Cameron283b4a92014-02-18 13:55:33 -060054#include <asm/div64.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080055#include "hpsa_cmd.h"
56#include "hpsa.h"
57
58/* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
Stephen M. Cameron9a993302014-03-13 17:13:06 -050059#define HPSA_DRIVER_VERSION "3.4.4-1"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080060#define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -060061#define HPSA "hpsa"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080062
Robert Elliott007e7aa2015-01-23 16:44:56 -060063/* How long to wait for CISS doorbell communication */
64#define CLEAR_EVENT_WAIT_INTERVAL 20 /* ms for each msleep() call */
65#define MODE_CHANGE_WAIT_INTERVAL 10 /* ms for each msleep() call */
66#define MAX_CLEAR_EVENT_WAIT 30000 /* times 20 ms = 600 s */
67#define MAX_MODE_CHANGE_WAIT 2000 /* times 10 ms = 20 s */
Stephen M. Cameronedd16362009-12-08 14:09:11 -080068#define MAX_IOCTL_CONFIG_WAIT 1000
69
70/*define how many times we will try a command because of bus resets */
71#define MAX_CMD_RETRIES 3
72
73/* Embedded module documentation macros - see modules.h */
74MODULE_AUTHOR("Hewlett-Packard Company");
75MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
76 HPSA_DRIVER_VERSION);
77MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
78MODULE_VERSION(HPSA_DRIVER_VERSION);
79MODULE_LICENSE("GPL");
80
81static int hpsa_allow_any;
82module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
83MODULE_PARM_DESC(hpsa_allow_any,
84 "Allow hpsa driver to access unknown HP Smart Array hardware");
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -060085static int hpsa_simple_mode;
86module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
87MODULE_PARM_DESC(hpsa_simple_mode,
88 "Use 'simple mode' rather than 'performant mode'");
Stephen M. Cameronedd16362009-12-08 14:09:11 -080089
90/* define the PCI info for the cards we can control */
91static const struct pci_device_id hpsa_pci_device_id[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -080092 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
93 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
94 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
95 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
96 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
Mike Miller163dbcd2013-09-04 15:11:10 -050097 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
98 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
Mike Millerf8b01eb2010-02-04 08:42:45 -060099 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
scameron@beardog.cce.hp.com9143a962011-03-07 10:44:16 -0600100 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350},
101 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351},
102 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352},
103 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353},
104 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354},
105 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355},
106 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356},
Mike Millerfe0c9612012-09-20 16:05:18 -0500107 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1921},
108 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1922},
109 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1923},
110 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1924},
Mike Millerfe0c9612012-09-20 16:05:18 -0500111 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1926},
112 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1928},
Mike Miller97b9f532013-09-04 15:05:55 -0500113 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1929},
114 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BD},
115 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BE},
116 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BF},
117 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C0},
118 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C1},
119 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C2},
120 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C3},
121 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C4},
122 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C5},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500123 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C6},
Mike Miller97b9f532013-09-04 15:05:55 -0500124 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C7},
125 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C8},
126 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C9},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500127 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CA},
128 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CB},
129 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CC},
130 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CD},
131 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CE},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600132 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
133 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
134 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
135 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
136 {PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
Mike Miller7c03b872010-12-01 11:16:07 -0600137 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
Stephen M. Cameron6798cc02010-06-16 13:51:20 -0500138 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800139 {0,}
140};
141
142MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
143
144/* board_id = Subsystem Device ID & Vendor ID
145 * product = Marketing Name for the board
146 * access = Address of the struct of function pointers
147 */
148static struct board_type products[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800149 {0x3241103C, "Smart Array P212", &SA5_access},
150 {0x3243103C, "Smart Array P410", &SA5_access},
151 {0x3245103C, "Smart Array P410i", &SA5_access},
152 {0x3247103C, "Smart Array P411", &SA5_access},
153 {0x3249103C, "Smart Array P812", &SA5_access},
Mike Miller163dbcd2013-09-04 15:11:10 -0500154 {0x324A103C, "Smart Array P712m", &SA5_access},
155 {0x324B103C, "Smart Array P711m", &SA5_access},
Stephen M. Cameron7d2cce52014-11-14 17:26:38 -0600156 {0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
Mike Millerfe0c9612012-09-20 16:05:18 -0500157 {0x3350103C, "Smart Array P222", &SA5_access},
158 {0x3351103C, "Smart Array P420", &SA5_access},
159 {0x3352103C, "Smart Array P421", &SA5_access},
160 {0x3353103C, "Smart Array P822", &SA5_access},
161 {0x3354103C, "Smart Array P420i", &SA5_access},
162 {0x3355103C, "Smart Array P220i", &SA5_access},
163 {0x3356103C, "Smart Array P721m", &SA5_access},
Mike Miller1fd6c8e2013-09-04 15:08:29 -0500164 {0x1921103C, "Smart Array P830i", &SA5_access},
165 {0x1922103C, "Smart Array P430", &SA5_access},
166 {0x1923103C, "Smart Array P431", &SA5_access},
167 {0x1924103C, "Smart Array P830", &SA5_access},
168 {0x1926103C, "Smart Array P731m", &SA5_access},
169 {0x1928103C, "Smart Array P230i", &SA5_access},
170 {0x1929103C, "Smart Array P530", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600171 {0x21BD103C, "Smart Array P244br", &SA5_access},
172 {0x21BE103C, "Smart Array P741m", &SA5_access},
173 {0x21BF103C, "Smart HBA H240ar", &SA5_access},
174 {0x21C0103C, "Smart Array P440ar", &SA5_access},
Don Bracec8ae0ab2015-01-23 16:45:12 -0600175 {0x21C1103C, "Smart Array P840ar", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600176 {0x21C2103C, "Smart Array P440", &SA5_access},
177 {0x21C3103C, "Smart Array P441", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500178 {0x21C4103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600179 {0x21C5103C, "Smart Array P841", &SA5_access},
180 {0x21C6103C, "Smart HBA H244br", &SA5_access},
181 {0x21C7103C, "Smart HBA H240", &SA5_access},
182 {0x21C8103C, "Smart HBA H241", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500183 {0x21C9103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600184 {0x21CA103C, "Smart Array P246br", &SA5_access},
185 {0x21CB103C, "Smart Array P840", &SA5_access},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500186 {0x21CC103C, "Smart Array", &SA5_access},
187 {0x21CD103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600188 {0x21CE103C, "Smart HBA", &SA5_access},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600189 {0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
190 {0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
191 {0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
192 {0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
193 {0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800194 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
195};
196
197static int number_of_controllers;
198
Stephen M. Cameron10f66012010-06-16 13:51:50 -0500199static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
200static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
Don Brace42a91642014-11-14 17:26:27 -0600201static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800202
203#ifdef CONFIG_COMPAT
Don Brace42a91642014-11-14 17:26:27 -0600204static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
205 void __user *arg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800206#endif
207
208static void cmd_free(struct ctlr_info *h, struct CommandList *c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800209static struct CommandList *cmd_alloc(struct ctlr_info *h);
Stephen M. Camerona2dac132013-02-20 11:24:41 -0600210static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600211 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800212 int cmd_type);
Robert Elliott2c143342015-01-23 16:42:48 -0600213static void hpsa_free_cmd_pool(struct ctlr_info *h);
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600214#define VPD_PAGE (1 << 8)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800215
Jeff Garzikf2812332010-11-16 02:10:29 -0500216static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Stephen M. Camerona08a84712010-02-04 08:43:16 -0600217static void hpsa_scan_start(struct Scsi_Host *);
218static int hpsa_scan_finished(struct Scsi_Host *sh,
219 unsigned long elapsed_time);
Don Brace7c0a0222015-01-23 16:41:30 -0600220static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800221
222static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500223static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800224static int hpsa_slave_alloc(struct scsi_device *sdev);
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500225static int hpsa_slave_configure(struct scsi_device *sdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800226static void hpsa_slave_destroy(struct scsi_device *sdev);
227
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800228static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800229static int check_for_unit_attention(struct ctlr_info *h,
230 struct CommandList *c);
231static void check_ioctl_unit_attention(struct ctlr_info *h,
232 struct CommandList *c);
Don Brace303932f2010-02-04 08:42:40 -0600233/* performant mode helper functions */
234static void calc_bucket_map(int *bucket, int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -0600235 int nsgs, int min_blocks, u32 *bucket_map);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800236static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
Matt Gates254f7962012-05-01 11:43:06 -0500237static inline u32 next_command(struct ctlr_info *h, u8 q);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800238static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
239 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
240 u64 *cfg_offset);
241static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
242 unsigned long *memory_bar);
243static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
244static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
245 int wait_for_ready);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500246static inline void finish_cmd(struct CommandList *c);
Robert Elliottc706a792015-01-23 16:45:01 -0600247static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -0600248#define BOARD_NOT_READY 0
249#define BOARD_READY 1
Stephen M. Cameron23100dd2014-02-18 13:57:37 -0600250static void hpsa_drain_accel_commands(struct ctlr_info *h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -0600251static void hpsa_flush_cache(struct ctlr_info *h);
Scott Teelc3497752014-02-18 13:56:34 -0600252static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
253 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -0600254 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
Don Brace080ef1c2015-01-23 16:43:25 -0600255static void hpsa_command_resubmit_worker(struct work_struct *work);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800256
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800257static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
258{
259 unsigned long *priv = shost_priv(sdev->host);
260 return (struct ctlr_info *) *priv;
261}
262
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600263static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
264{
265 unsigned long *priv = shost_priv(sh);
266 return (struct ctlr_info *) *priv;
267}
268
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800269static int check_for_unit_attention(struct ctlr_info *h,
270 struct CommandList *c)
271{
272 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
273 return 0;
274
275 switch (c->err_info->SenseInfo[12]) {
276 case STATE_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600277 dev_warn(&h->pdev->dev, HPSA "%d: a state change "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800278 "detected, command retried\n", h->ctlr);
279 break;
280 case LUN_FAILED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600281 dev_warn(&h->pdev->dev,
282 HPSA "%d: LUN failure detected\n", h->ctlr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800283 break;
284 case REPORT_LUNS_CHANGED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600285 dev_warn(&h->pdev->dev,
286 HPSA "%d: report LUN data changed\n", h->ctlr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800287 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600288 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
289 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800290 */
291 break;
292 case POWER_OR_RESET:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600293 dev_warn(&h->pdev->dev, HPSA "%d: a power on "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800294 "or device reset detected\n", h->ctlr);
295 break;
296 case UNIT_ATTENTION_CLEARED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600297 dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800298 "cleared by another initiator\n", h->ctlr);
299 break;
300 default:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600301 dev_warn(&h->pdev->dev, HPSA "%d: unknown "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800302 "unit attention detected\n", h->ctlr);
303 break;
304 }
305 return 1;
306}
307
Matt Bondurant852af202012-05-01 11:42:35 -0500308static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
309{
310 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
311 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
312 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
313 return 0;
314 dev_warn(&h->pdev->dev, HPSA "device busy");
315 return 1;
316}
317
Scott Teelda0697b2014-02-18 13:57:00 -0600318static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
319 struct device_attribute *attr,
320 const char *buf, size_t count)
321{
322 int status, len;
323 struct ctlr_info *h;
324 struct Scsi_Host *shost = class_to_shost(dev);
325 char tmpbuf[10];
326
327 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
328 return -EACCES;
329 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
330 strncpy(tmpbuf, buf, len);
331 tmpbuf[len] = '\0';
332 if (sscanf(tmpbuf, "%d", &status) != 1)
333 return -EINVAL;
334 h = shost_to_hba(shost);
335 h->acciopath_status = !!status;
336 dev_warn(&h->pdev->dev,
337 "hpsa: HP SSD Smart Path %s via sysfs update.\n",
338 h->acciopath_status ? "enabled" : "disabled");
339 return count;
340}
341
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600342static ssize_t host_store_raid_offload_debug(struct device *dev,
343 struct device_attribute *attr,
344 const char *buf, size_t count)
345{
346 int debug_level, len;
347 struct ctlr_info *h;
348 struct Scsi_Host *shost = class_to_shost(dev);
349 char tmpbuf[10];
350
351 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
352 return -EACCES;
353 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
354 strncpy(tmpbuf, buf, len);
355 tmpbuf[len] = '\0';
356 if (sscanf(tmpbuf, "%d", &debug_level) != 1)
357 return -EINVAL;
358 if (debug_level < 0)
359 debug_level = 0;
360 h = shost_to_hba(shost);
361 h->raid_offload_debug = debug_level;
362 dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
363 h->raid_offload_debug);
364 return count;
365}
366
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800367static ssize_t host_store_rescan(struct device *dev,
368 struct device_attribute *attr,
369 const char *buf, size_t count)
370{
371 struct ctlr_info *h;
372 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600373 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600374 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800375 return count;
376}
377
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500378static ssize_t host_show_firmware_revision(struct device *dev,
379 struct device_attribute *attr, char *buf)
380{
381 struct ctlr_info *h;
382 struct Scsi_Host *shost = class_to_shost(dev);
383 unsigned char *fwrev;
384
385 h = shost_to_hba(shost);
386 if (!h->hba_inquiry_data)
387 return 0;
388 fwrev = &h->hba_inquiry_data[32];
389 return snprintf(buf, 20, "%c%c%c%c\n",
390 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
391}
392
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600393static ssize_t host_show_commands_outstanding(struct device *dev,
394 struct device_attribute *attr, char *buf)
395{
396 struct Scsi_Host *shost = class_to_shost(dev);
397 struct ctlr_info *h = shost_to_hba(shost);
398
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600399 return snprintf(buf, 20, "%d\n",
400 atomic_read(&h->commands_outstanding));
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600401}
402
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600403static ssize_t host_show_transport_mode(struct device *dev,
404 struct device_attribute *attr, char *buf)
405{
406 struct ctlr_info *h;
407 struct Scsi_Host *shost = class_to_shost(dev);
408
409 h = shost_to_hba(shost);
410 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600411 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600412 "performant" : "simple");
413}
414
Scott Teelda0697b2014-02-18 13:57:00 -0600415static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
416 struct device_attribute *attr, char *buf)
417{
418 struct ctlr_info *h;
419 struct Scsi_Host *shost = class_to_shost(dev);
420
421 h = shost_to_hba(shost);
422 return snprintf(buf, 30, "HP SSD Smart Path %s\n",
423 (h->acciopath_status == 1) ? "enabled" : "disabled");
424}
425
Stephen M. Cameron46380782011-05-03 15:00:01 -0500426/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600427static u32 unresettable_controller[] = {
428 0x324a103C, /* Smart Array P712m */
429 0x324b103C, /* SmartArray P711m */
430 0x3223103C, /* Smart Array P800 */
431 0x3234103C, /* Smart Array P400 */
432 0x3235103C, /* Smart Array P400i */
433 0x3211103C, /* Smart Array E200i */
434 0x3212103C, /* Smart Array E200 */
435 0x3213103C, /* Smart Array E200i */
436 0x3214103C, /* Smart Array E200i */
437 0x3215103C, /* Smart Array E200i */
438 0x3237103C, /* Smart Array E500 */
439 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100440 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600441 0x409C0E11, /* Smart Array 6400 */
442 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100443 0x40700E11, /* Smart Array 5300 */
444 0x40820E11, /* Smart Array 532 */
445 0x40830E11, /* Smart Array 5312 */
446 0x409A0E11, /* Smart Array 641 */
447 0x409B0E11, /* Smart Array 642 */
448 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600449};
450
Stephen M. Cameron46380782011-05-03 15:00:01 -0500451/* List of controllers which cannot even be soft reset */
452static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100453 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100454 0x40700E11, /* Smart Array 5300 */
455 0x40820E11, /* Smart Array 532 */
456 0x40830E11, /* Smart Array 5312 */
457 0x409A0E11, /* Smart Array 641 */
458 0x409B0E11, /* Smart Array 642 */
459 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500460 /* Exclude 640x boards. These are two pci devices in one slot
461 * which share a battery backed cache module. One controls the
462 * cache, the other accesses the cache through the one that controls
463 * it. If we reset the one controlling the cache, the other will
464 * likely not be happy. Just forbid resetting this conjoined mess.
465 * The 640x isn't really supported by hpsa anyway.
466 */
467 0x409C0E11, /* Smart Array 6400 */
468 0x409D0E11, /* Smart Array 6400 EM */
469};
470
471static int ctlr_is_hard_resettable(u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600472{
473 int i;
474
475 for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
Stephen M. Cameron46380782011-05-03 15:00:01 -0500476 if (unresettable_controller[i] == board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600477 return 0;
478 return 1;
479}
480
Stephen M. Cameron46380782011-05-03 15:00:01 -0500481static int ctlr_is_soft_resettable(u32 board_id)
482{
483 int i;
484
485 for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
486 if (soft_unresettable_controller[i] == board_id)
487 return 0;
488 return 1;
489}
490
491static int ctlr_is_resettable(u32 board_id)
492{
493 return ctlr_is_hard_resettable(board_id) ||
494 ctlr_is_soft_resettable(board_id);
495}
496
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600497static ssize_t host_show_resettable(struct device *dev,
498 struct device_attribute *attr, char *buf)
499{
500 struct ctlr_info *h;
501 struct Scsi_Host *shost = class_to_shost(dev);
502
503 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500504 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600505}
506
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800507static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
508{
509 return (scsi3addr[3] & 0xC0) == 0x40;
510}
511
Robert Elliottf2ef0ce2015-01-23 16:41:35 -0600512static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
513 "1(+0)ADM", "UNKNOWN"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800514};
Scott Teel6b80b182014-02-18 13:56:55 -0600515#define HPSA_RAID_0 0
516#define HPSA_RAID_4 1
517#define HPSA_RAID_1 2 /* also used for RAID 10 */
518#define HPSA_RAID_5 3 /* also used for RAID 50 */
519#define HPSA_RAID_51 4
520#define HPSA_RAID_6 5 /* also used for RAID 60 */
521#define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800522#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
523
524static ssize_t raid_level_show(struct device *dev,
525 struct device_attribute *attr, char *buf)
526{
527 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600528 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800529 struct ctlr_info *h;
530 struct scsi_device *sdev;
531 struct hpsa_scsi_dev_t *hdev;
532 unsigned long flags;
533
534 sdev = to_scsi_device(dev);
535 h = sdev_to_hba(sdev);
536 spin_lock_irqsave(&h->lock, flags);
537 hdev = sdev->hostdata;
538 if (!hdev) {
539 spin_unlock_irqrestore(&h->lock, flags);
540 return -ENODEV;
541 }
542
543 /* Is this even a logical drive? */
544 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
545 spin_unlock_irqrestore(&h->lock, flags);
546 l = snprintf(buf, PAGE_SIZE, "N/A\n");
547 return l;
548 }
549
550 rlevel = hdev->raid_level;
551 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600552 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800553 rlevel = RAID_UNKNOWN;
554 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
555 return l;
556}
557
558static ssize_t lunid_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
560{
561 struct ctlr_info *h;
562 struct scsi_device *sdev;
563 struct hpsa_scsi_dev_t *hdev;
564 unsigned long flags;
565 unsigned char lunid[8];
566
567 sdev = to_scsi_device(dev);
568 h = sdev_to_hba(sdev);
569 spin_lock_irqsave(&h->lock, flags);
570 hdev = sdev->hostdata;
571 if (!hdev) {
572 spin_unlock_irqrestore(&h->lock, flags);
573 return -ENODEV;
574 }
575 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
576 spin_unlock_irqrestore(&h->lock, flags);
577 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
578 lunid[0], lunid[1], lunid[2], lunid[3],
579 lunid[4], lunid[5], lunid[6], lunid[7]);
580}
581
582static ssize_t unique_id_show(struct device *dev,
583 struct device_attribute *attr, char *buf)
584{
585 struct ctlr_info *h;
586 struct scsi_device *sdev;
587 struct hpsa_scsi_dev_t *hdev;
588 unsigned long flags;
589 unsigned char sn[16];
590
591 sdev = to_scsi_device(dev);
592 h = sdev_to_hba(sdev);
593 spin_lock_irqsave(&h->lock, flags);
594 hdev = sdev->hostdata;
595 if (!hdev) {
596 spin_unlock_irqrestore(&h->lock, flags);
597 return -ENODEV;
598 }
599 memcpy(sn, hdev->device_id, sizeof(sn));
600 spin_unlock_irqrestore(&h->lock, flags);
601 return snprintf(buf, 16 * 2 + 2,
602 "%02X%02X%02X%02X%02X%02X%02X%02X"
603 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
604 sn[0], sn[1], sn[2], sn[3],
605 sn[4], sn[5], sn[6], sn[7],
606 sn[8], sn[9], sn[10], sn[11],
607 sn[12], sn[13], sn[14], sn[15]);
608}
609
Scott Teelc1988682014-02-18 13:55:54 -0600610static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
611 struct device_attribute *attr, char *buf)
612{
613 struct ctlr_info *h;
614 struct scsi_device *sdev;
615 struct hpsa_scsi_dev_t *hdev;
616 unsigned long flags;
617 int offload_enabled;
618
619 sdev = to_scsi_device(dev);
620 h = sdev_to_hba(sdev);
621 spin_lock_irqsave(&h->lock, flags);
622 hdev = sdev->hostdata;
623 if (!hdev) {
624 spin_unlock_irqrestore(&h->lock, flags);
625 return -ENODEV;
626 }
627 offload_enabled = hdev->offload_enabled;
628 spin_unlock_irqrestore(&h->lock, flags);
629 return snprintf(buf, 20, "%d\n", offload_enabled);
630}
631
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600632static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
633static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
634static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
635static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Scott Teelc1988682014-02-18 13:55:54 -0600636static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
637 host_show_hp_ssd_smart_path_enabled, NULL);
Scott Teelda0697b2014-02-18 13:57:00 -0600638static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
639 host_show_hp_ssd_smart_path_status,
640 host_store_hp_ssd_smart_path_status);
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600641static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
642 host_store_raid_offload_debug);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600643static DEVICE_ATTR(firmware_revision, S_IRUGO,
644 host_show_firmware_revision, NULL);
645static DEVICE_ATTR(commands_outstanding, S_IRUGO,
646 host_show_commands_outstanding, NULL);
647static DEVICE_ATTR(transport_mode, S_IRUGO,
648 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600649static DEVICE_ATTR(resettable, S_IRUGO,
650 host_show_resettable, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600651
652static struct device_attribute *hpsa_sdev_attrs[] = {
653 &dev_attr_raid_level,
654 &dev_attr_lunid,
655 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600656 &dev_attr_hp_ssd_smart_path_enabled,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600657 NULL,
658};
659
660static struct device_attribute *hpsa_shost_attrs[] = {
661 &dev_attr_rescan,
662 &dev_attr_firmware_revision,
663 &dev_attr_commands_outstanding,
664 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600665 &dev_attr_resettable,
Scott Teelda0697b2014-02-18 13:57:00 -0600666 &dev_attr_hp_ssd_smart_path_status,
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600667 &dev_attr_raid_offload_debug,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600668 NULL,
669};
670
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500671#define HPSA_NRESERVED_CMDS (HPSA_CMDS_RESERVED_FOR_ABORTS + \
672 HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
673
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600674static struct scsi_host_template hpsa_driver_template = {
675 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600676 .name = HPSA,
677 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600678 .queuecommand = hpsa_scsi_queue_command,
679 .scan_start = hpsa_scan_start,
680 .scan_finished = hpsa_scan_finished,
Don Brace7c0a0222015-01-23 16:41:30 -0600681 .change_queue_depth = hpsa_change_queue_depth,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600682 .this_id = -1,
683 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500684 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600685 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
686 .ioctl = hpsa_ioctl,
687 .slave_alloc = hpsa_slave_alloc,
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500688 .slave_configure = hpsa_slave_configure,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600689 .slave_destroy = hpsa_slave_destroy,
690#ifdef CONFIG_COMPAT
691 .compat_ioctl = hpsa_compat_ioctl,
692#endif
693 .sdev_attrs = hpsa_sdev_attrs,
694 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500695 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400696 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600697};
698
Matt Gates254f7962012-05-01 11:43:06 -0500699static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600700{
701 u32 a;
Stephen M. Cameron072b0512014-05-29 10:53:07 -0500702 struct reply_queue_buffer *rq = &h->reply_queue[q];
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600703
Matt Gatese1f7de02014-02-18 13:55:17 -0600704 if (h->transMethod & CFGTBL_Trans_io_accel1)
705 return h->access.command_completed(h, q);
706
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600707 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500708 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600709
Matt Gates254f7962012-05-01 11:43:06 -0500710 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
711 a = rq->head[rq->current_entry];
712 rq->current_entry++;
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600713 atomic_dec(&h->commands_outstanding);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600714 } else {
715 a = FIFO_EMPTY;
716 }
717 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500718 if (rq->current_entry == h->max_commands) {
719 rq->current_entry = 0;
720 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600721 }
722 return a;
723}
724
Scott Teelc3497752014-02-18 13:56:34 -0600725/*
726 * There are some special bits in the bus address of the
727 * command that we have to set for the controller to know
728 * how to process the command:
729 *
730 * Normal performant mode:
731 * bit 0: 1 means performant mode, 0 means simple mode.
732 * bits 1-3 = block fetch table entry
733 * bits 4-6 = command type (== 0)
734 *
735 * ioaccel1 mode:
736 * bit 0 = "performant mode" bit.
737 * bits 1-3 = block fetch table entry
738 * bits 4-6 = command type (== 110)
739 * (command type is needed because ioaccel1 mode
740 * commands are submitted through the same register as normal
741 * mode commands, so this is how the controller knows whether
742 * the command is normal mode or ioaccel1 mode.)
743 *
744 * ioaccel2 mode:
745 * bit 0 = "performant mode" bit.
746 * bits 1-4 = block fetch table entry (note extra bit)
747 * bits 4-6 = not needed, because ioaccel2 mode has
748 * a separate special register for submitting commands.
749 */
750
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600751/* set_performant_mode: Modify the tag for cciss performant
752 * set bit 0 for pull model, bits 3-1 for block fetch
753 * register number
754 */
755static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
756{
Matt Gates254f7962012-05-01 11:43:06 -0500757 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600758 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Hannes Reineckeeee0f032014-01-15 13:30:53 +0100759 if (likely(h->msix_vector > 0))
Matt Gates254f7962012-05-01 11:43:06 -0500760 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +0200761 raw_smp_processor_id() % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -0500762 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600763}
764
Scott Teelc3497752014-02-18 13:56:34 -0600765static void set_ioaccel1_performant_mode(struct ctlr_info *h,
766 struct CommandList *c)
767{
768 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
769
770 /* Tell the controller to post the reply to the queue for this
771 * processor. This seems to give the best I/O throughput.
772 */
773 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
774 /* Set the bits in the address sent down to include:
775 * - performant mode bit (bit 0)
776 * - pull count (bits 1-3)
777 * - command type (bits 4-6)
778 */
779 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
780 IOACCEL1_BUSADDR_CMDTYPE;
781}
782
783static void set_ioaccel2_performant_mode(struct ctlr_info *h,
784 struct CommandList *c)
785{
786 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
787
788 /* Tell the controller to post the reply to the queue for this
789 * processor. This seems to give the best I/O throughput.
790 */
791 cp->reply_queue = smp_processor_id() % h->nreply_queues;
792 /* Set the bits in the address sent down to include:
793 * - performant mode bit not used in ioaccel mode 2
794 * - pull count (bits 0-3)
795 * - command type isn't needed for ioaccel2
796 */
797 c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
798}
799
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500800static int is_firmware_flash_cmd(u8 *cdb)
801{
802 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
803}
804
805/*
806 * During firmware flash, the heartbeat register may not update as frequently
807 * as it should. So we dial down lockup detection during firmware flash. and
808 * dial it back up when firmware flash completes.
809 */
810#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
811#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
812static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
813 struct CommandList *c)
814{
815 if (!is_firmware_flash_cmd(c->Request.CDB))
816 return;
817 atomic_inc(&h->firmware_flash_in_progress);
818 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
819}
820
821static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
822 struct CommandList *c)
823{
824 if (is_firmware_flash_cmd(c->Request.CDB) &&
825 atomic_dec_and_test(&h->firmware_flash_in_progress))
826 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
827}
828
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600829static void enqueue_cmd_and_start_io(struct ctlr_info *h,
830 struct CommandList *c)
831{
Stephen Cameronc05e8862015-01-23 16:44:40 -0600832 dial_down_lockup_detection_during_fw_flash(h, c);
833 atomic_inc(&h->commands_outstanding);
Scott Teelc3497752014-02-18 13:56:34 -0600834 switch (c->cmd_type) {
835 case CMD_IOACCEL1:
836 set_ioaccel1_performant_mode(h, c);
Stephen Cameronc05e8862015-01-23 16:44:40 -0600837 writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
Scott Teelc3497752014-02-18 13:56:34 -0600838 break;
839 case CMD_IOACCEL2:
840 set_ioaccel2_performant_mode(h, c);
Stephen Cameronc05e8862015-01-23 16:44:40 -0600841 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
Scott Teelc3497752014-02-18 13:56:34 -0600842 break;
843 default:
844 set_performant_mode(h, c);
Stephen Cameronc05e8862015-01-23 16:44:40 -0600845 h->access.submit_command(h, c);
Scott Teelc3497752014-02-18 13:56:34 -0600846 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600847}
848
849static inline int is_hba_lunid(unsigned char scsi3addr[])
850{
851 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
852}
853
854static inline int is_scsi_rev_5(struct ctlr_info *h)
855{
856 if (!h->hba_inquiry_data)
857 return 0;
858 if ((h->hba_inquiry_data[2] & 0x07) == 5)
859 return 1;
860 return 0;
861}
862
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800863static int hpsa_find_target_lun(struct ctlr_info *h,
864 unsigned char scsi3addr[], int bus, int *target, int *lun)
865{
866 /* finds an unused bus, target, lun for a new physical device
867 * assumes h->devlock is held
868 */
869 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -0500870 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800871
Akinobu Mita263d9402012-01-21 00:15:27 +0900872 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800873
874 for (i = 0; i < h->ndevices; i++) {
875 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +0900876 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800877 }
878
Akinobu Mita263d9402012-01-21 00:15:27 +0900879 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
880 if (i < HPSA_MAX_DEVICES) {
881 /* *bus = 1; */
882 *target = i;
883 *lun = 0;
884 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800885 }
886 return !found;
887}
888
889/* Add an entry into h->dev[] array. */
890static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
891 struct hpsa_scsi_dev_t *device,
892 struct hpsa_scsi_dev_t *added[], int *nadded)
893{
894 /* assumes h->devlock is held */
895 int n = h->ndevices;
896 int i;
897 unsigned char addr1[8], addr2[8];
898 struct hpsa_scsi_dev_t *sd;
899
Scott Teelcfe5bad2011-10-26 16:21:07 -0500900 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800901 dev_err(&h->pdev->dev, "too many devices, some will be "
902 "inaccessible.\n");
903 return -1;
904 }
905
906 /* physical devices do not have lun or target assigned until now. */
907 if (device->lun != -1)
908 /* Logical device, lun is already assigned. */
909 goto lun_assigned;
910
911 /* If this device a non-zero lun of a multi-lun device
912 * byte 4 of the 8-byte LUN addr will contain the logical
Don Brace2b08b3e2015-01-23 16:41:09 -0600913 * unit no, zero otherwise.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800914 */
915 if (device->scsi3addr[4] == 0) {
916 /* This is not a non-zero lun of a multi-lun device */
917 if (hpsa_find_target_lun(h, device->scsi3addr,
918 device->bus, &device->target, &device->lun) != 0)
919 return -1;
920 goto lun_assigned;
921 }
922
923 /* This is a non-zero lun of a multi-lun device.
924 * Search through our list and find the device which
925 * has the same 8 byte LUN address, excepting byte 4.
926 * Assign the same bus and target for this new LUN.
927 * Use the logical unit number from the firmware.
928 */
929 memcpy(addr1, device->scsi3addr, 8);
930 addr1[4] = 0;
931 for (i = 0; i < n; i++) {
932 sd = h->dev[i];
933 memcpy(addr2, sd->scsi3addr, 8);
934 addr2[4] = 0;
935 /* differ only in byte 4? */
936 if (memcmp(addr1, addr2, 8) == 0) {
937 device->bus = sd->bus;
938 device->target = sd->target;
939 device->lun = device->scsi3addr[4];
940 break;
941 }
942 }
943 if (device->lun == -1) {
944 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
945 " suspect firmware bug or unsupported hardware "
946 "configuration.\n");
947 return -1;
948 }
949
950lun_assigned:
951
952 h->dev[n] = device;
953 h->ndevices++;
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500954 device->offload_to_be_enabled = device->offload_enabled;
955 device->offload_enabled = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800956 added[*nadded] = device;
957 (*nadded)++;
958
959 /* initially, (before registering with scsi layer) we don't
960 * know our hostno and we don't want to print anything first
961 * time anyway (the scsi layer's inquiries will show that info)
962 */
963 /* if (hostno != -1) */
964 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
965 scsi_device_type(device->devtype), hostno,
966 device->bus, device->target, device->lun);
967 return 0;
968}
969
Scott Teelbd9244f2012-01-19 14:01:30 -0600970/* Update an entry in h->dev[] array. */
971static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
972 int entry, struct hpsa_scsi_dev_t *new_entry)
973{
974 /* assumes h->devlock is held */
975 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
976
977 /* Raid level changed. */
978 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600979
Don Brace03383732015-01-23 16:43:30 -0600980 /* Raid offload parameters changed. Careful about the ordering. */
981 if (new_entry->offload_config && new_entry->offload_enabled) {
982 /*
983 * if drive is newly offload_enabled, we want to copy the
984 * raid map data first. If previously offload_enabled and
985 * offload_config were set, raid map data had better be
986 * the same as it was before. if raid map data is changed
987 * then it had better be the case that
988 * h->dev[entry]->offload_enabled is currently 0.
989 */
990 h->dev[entry]->raid_map = new_entry->raid_map;
991 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
Don Brace03383732015-01-23 16:43:30 -0600992 }
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600993 h->dev[entry]->offload_config = new_entry->offload_config;
Stephen M. Cameron9fb0de22014-02-18 13:56:50 -0600994 h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
Don Brace03383732015-01-23 16:43:30 -0600995 h->dev[entry]->queue_depth = new_entry->queue_depth;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600996
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500997 /*
998 * We can turn off ioaccel offload now, but need to delay turning
999 * it on until we can update h->dev[entry]->phys_disk[], but we
1000 * can't do that until all the devices are updated.
1001 */
1002 h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
1003 if (!new_entry->offload_enabled)
1004 h->dev[entry]->offload_enabled = 0;
1005
Scott Teelbd9244f2012-01-19 14:01:30 -06001006}
1007
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001008/* Replace an entry from h->dev[] array. */
1009static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
1010 int entry, struct hpsa_scsi_dev_t *new_entry,
1011 struct hpsa_scsi_dev_t *added[], int *nadded,
1012 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1013{
1014 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -05001015 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001016 removed[*nremoved] = h->dev[entry];
1017 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -05001018
1019 /*
1020 * New physical devices won't have target/lun assigned yet
1021 * so we need to preserve the values in the slot we are replacing.
1022 */
1023 if (new_entry->target == -1) {
1024 new_entry->target = h->dev[entry]->target;
1025 new_entry->lun = h->dev[entry]->lun;
1026 }
1027
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001028 new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1029 new_entry->offload_enabled = 0;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001030 h->dev[entry] = new_entry;
1031 added[*nadded] = new_entry;
1032 (*nadded)++;
1033 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
1034 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
1035 new_entry->target, new_entry->lun);
1036}
1037
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001038/* Remove an entry from h->dev[] array. */
1039static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1040 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1041{
1042 /* assumes h->devlock is held */
1043 int i;
1044 struct hpsa_scsi_dev_t *sd;
1045
Scott Teelcfe5bad2011-10-26 16:21:07 -05001046 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001047
1048 sd = h->dev[entry];
1049 removed[*nremoved] = h->dev[entry];
1050 (*nremoved)++;
1051
1052 for (i = entry; i < h->ndevices-1; i++)
1053 h->dev[i] = h->dev[i+1];
1054 h->ndevices--;
1055 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
1056 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
1057 sd->lun);
1058}
1059
1060#define SCSI3ADDR_EQ(a, b) ( \
1061 (a)[7] == (b)[7] && \
1062 (a)[6] == (b)[6] && \
1063 (a)[5] == (b)[5] && \
1064 (a)[4] == (b)[4] && \
1065 (a)[3] == (b)[3] && \
1066 (a)[2] == (b)[2] && \
1067 (a)[1] == (b)[1] && \
1068 (a)[0] == (b)[0])
1069
1070static void fixup_botched_add(struct ctlr_info *h,
1071 struct hpsa_scsi_dev_t *added)
1072{
1073 /* called when scsi_add_device fails in order to re-adjust
1074 * h->dev[] to match the mid layer's view.
1075 */
1076 unsigned long flags;
1077 int i, j;
1078
1079 spin_lock_irqsave(&h->lock, flags);
1080 for (i = 0; i < h->ndevices; i++) {
1081 if (h->dev[i] == added) {
1082 for (j = i; j < h->ndevices-1; j++)
1083 h->dev[j] = h->dev[j+1];
1084 h->ndevices--;
1085 break;
1086 }
1087 }
1088 spin_unlock_irqrestore(&h->lock, flags);
1089 kfree(added);
1090}
1091
1092static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1093 struct hpsa_scsi_dev_t *dev2)
1094{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001095 /* we compare everything except lun and target as these
1096 * are not yet assigned. Compare parts likely
1097 * to differ first
1098 */
1099 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1100 sizeof(dev1->scsi3addr)) != 0)
1101 return 0;
1102 if (memcmp(dev1->device_id, dev2->device_id,
1103 sizeof(dev1->device_id)) != 0)
1104 return 0;
1105 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1106 return 0;
1107 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1108 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001109 if (dev1->devtype != dev2->devtype)
1110 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001111 if (dev1->bus != dev2->bus)
1112 return 0;
1113 return 1;
1114}
1115
Scott Teelbd9244f2012-01-19 14:01:30 -06001116static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1117 struct hpsa_scsi_dev_t *dev2)
1118{
1119 /* Device attributes that can change, but don't mean
1120 * that the device is a different device, nor that the OS
1121 * needs to be told anything about the change.
1122 */
1123 if (dev1->raid_level != dev2->raid_level)
1124 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001125 if (dev1->offload_config != dev2->offload_config)
1126 return 1;
1127 if (dev1->offload_enabled != dev2->offload_enabled)
1128 return 1;
Don Brace03383732015-01-23 16:43:30 -06001129 if (dev1->queue_depth != dev2->queue_depth)
1130 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -06001131 return 0;
1132}
1133
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001134/* Find needle in haystack. If exact match found, return DEVICE_SAME,
1135 * and return needle location in *index. If scsi3addr matches, but not
1136 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -06001137 * location in *index.
1138 * In the case of a minor device attribute change, such as RAID level, just
1139 * return DEVICE_UPDATED, along with the updated device's location in index.
1140 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001141 */
1142static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1143 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1144 int *index)
1145{
1146 int i;
1147#define DEVICE_NOT_FOUND 0
1148#define DEVICE_CHANGED 1
1149#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -06001150#define DEVICE_UPDATED 3
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001151 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -06001152 if (haystack[i] == NULL) /* previously removed. */
1153 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001154 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1155 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -06001156 if (device_is_the_same(needle, haystack[i])) {
1157 if (device_updated(needle, haystack[i]))
1158 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001159 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -06001160 } else {
Stephen M. Cameron98465902014-02-21 16:25:00 -06001161 /* Keep offline devices offline */
1162 if (needle->volume_offline)
1163 return DEVICE_NOT_FOUND;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001164 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -06001165 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001166 }
1167 }
1168 *index = -1;
1169 return DEVICE_NOT_FOUND;
1170}
1171
Stephen M. Cameron98465902014-02-21 16:25:00 -06001172static void hpsa_monitor_offline_device(struct ctlr_info *h,
1173 unsigned char scsi3addr[])
1174{
1175 struct offline_device_entry *device;
1176 unsigned long flags;
1177
1178 /* Check to see if device is already on the list */
1179 spin_lock_irqsave(&h->offline_device_lock, flags);
1180 list_for_each_entry(device, &h->offline_device_list, offline_list) {
1181 if (memcmp(device->scsi3addr, scsi3addr,
1182 sizeof(device->scsi3addr)) == 0) {
1183 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1184 return;
1185 }
1186 }
1187 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1188
1189 /* Device is not on the list, add it. */
1190 device = kmalloc(sizeof(*device), GFP_KERNEL);
1191 if (!device) {
1192 dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
1193 return;
1194 }
1195 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1196 spin_lock_irqsave(&h->offline_device_lock, flags);
1197 list_add_tail(&device->offline_list, &h->offline_device_list);
1198 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1199}
1200
1201/* Print a message explaining various offline volume states */
1202static void hpsa_show_volume_status(struct ctlr_info *h,
1203 struct hpsa_scsi_dev_t *sd)
1204{
1205 if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
1206 dev_info(&h->pdev->dev,
1207 "C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
1208 h->scsi_host->host_no,
1209 sd->bus, sd->target, sd->lun);
1210 switch (sd->volume_offline) {
1211 case HPSA_LV_OK:
1212 break;
1213 case HPSA_LV_UNDERGOING_ERASE:
1214 dev_info(&h->pdev->dev,
1215 "C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
1216 h->scsi_host->host_no,
1217 sd->bus, sd->target, sd->lun);
1218 break;
1219 case HPSA_LV_UNDERGOING_RPI:
1220 dev_info(&h->pdev->dev,
1221 "C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
1222 h->scsi_host->host_no,
1223 sd->bus, sd->target, sd->lun);
1224 break;
1225 case HPSA_LV_PENDING_RPI:
1226 dev_info(&h->pdev->dev,
1227 "C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
1228 h->scsi_host->host_no,
1229 sd->bus, sd->target, sd->lun);
1230 break;
1231 case HPSA_LV_ENCRYPTED_NO_KEY:
1232 dev_info(&h->pdev->dev,
1233 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
1234 h->scsi_host->host_no,
1235 sd->bus, sd->target, sd->lun);
1236 break;
1237 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
1238 dev_info(&h->pdev->dev,
1239 "C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
1240 h->scsi_host->host_no,
1241 sd->bus, sd->target, sd->lun);
1242 break;
1243 case HPSA_LV_UNDERGOING_ENCRYPTION:
1244 dev_info(&h->pdev->dev,
1245 "C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
1246 h->scsi_host->host_no,
1247 sd->bus, sd->target, sd->lun);
1248 break;
1249 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
1250 dev_info(&h->pdev->dev,
1251 "C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
1252 h->scsi_host->host_no,
1253 sd->bus, sd->target, sd->lun);
1254 break;
1255 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1256 dev_info(&h->pdev->dev,
1257 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
1258 h->scsi_host->host_no,
1259 sd->bus, sd->target, sd->lun);
1260 break;
1261 case HPSA_LV_PENDING_ENCRYPTION:
1262 dev_info(&h->pdev->dev,
1263 "C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
1264 h->scsi_host->host_no,
1265 sd->bus, sd->target, sd->lun);
1266 break;
1267 case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
1268 dev_info(&h->pdev->dev,
1269 "C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
1270 h->scsi_host->host_no,
1271 sd->bus, sd->target, sd->lun);
1272 break;
1273 }
1274}
1275
Don Brace03383732015-01-23 16:43:30 -06001276/*
1277 * Figure the list of physical drive pointers for a logical drive with
1278 * raid offload configured.
1279 */
1280static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
1281 struct hpsa_scsi_dev_t *dev[], int ndevices,
1282 struct hpsa_scsi_dev_t *logical_drive)
1283{
1284 struct raid_map_data *map = &logical_drive->raid_map;
1285 struct raid_map_disk_data *dd = &map->data[0];
1286 int i, j;
1287 int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
1288 le16_to_cpu(map->metadata_disks_per_row);
1289 int nraid_map_entries = le16_to_cpu(map->row_cnt) *
1290 le16_to_cpu(map->layout_map_count) *
1291 total_disks_per_row;
1292 int nphys_disk = le16_to_cpu(map->layout_map_count) *
1293 total_disks_per_row;
1294 int qdepth;
1295
1296 if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
1297 nraid_map_entries = RAID_MAP_MAX_ENTRIES;
1298
1299 qdepth = 0;
1300 for (i = 0; i < nraid_map_entries; i++) {
1301 logical_drive->phys_disk[i] = NULL;
1302 if (!logical_drive->offload_config)
1303 continue;
1304 for (j = 0; j < ndevices; j++) {
1305 if (dev[j]->devtype != TYPE_DISK)
1306 continue;
1307 if (is_logical_dev_addr_mode(dev[j]->scsi3addr))
1308 continue;
1309 if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
1310 continue;
1311
1312 logical_drive->phys_disk[i] = dev[j];
1313 if (i < nphys_disk)
1314 qdepth = min(h->nr_cmds, qdepth +
1315 logical_drive->phys_disk[i]->queue_depth);
1316 break;
1317 }
1318
1319 /*
1320 * This can happen if a physical drive is removed and
1321 * the logical drive is degraded. In that case, the RAID
1322 * map data will refer to a physical disk which isn't actually
1323 * present. And in that case offload_enabled should already
1324 * be 0, but we'll turn it off here just in case
1325 */
1326 if (!logical_drive->phys_disk[i]) {
1327 logical_drive->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001328 logical_drive->offload_to_be_enabled = 0;
1329 logical_drive->queue_depth = 8;
Don Brace03383732015-01-23 16:43:30 -06001330 }
1331 }
1332 if (nraid_map_entries)
1333 /*
1334 * This is correct for reads, too high for full stripe writes,
1335 * way too high for partial stripe writes
1336 */
1337 logical_drive->queue_depth = qdepth;
1338 else
1339 logical_drive->queue_depth = h->nr_cmds;
1340}
1341
1342static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
1343 struct hpsa_scsi_dev_t *dev[], int ndevices)
1344{
1345 int i;
1346
1347 for (i = 0; i < ndevices; i++) {
1348 if (dev[i]->devtype != TYPE_DISK)
1349 continue;
1350 if (!is_logical_dev_addr_mode(dev[i]->scsi3addr))
1351 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001352
1353 /*
1354 * If offload is currently enabled, the RAID map and
1355 * phys_disk[] assignment *better* not be changing
1356 * and since it isn't changing, we do not need to
1357 * update it.
1358 */
1359 if (dev[i]->offload_enabled)
1360 continue;
1361
Don Brace03383732015-01-23 16:43:30 -06001362 hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
1363 }
1364}
1365
Stephen M. Cameron4967bd32010-02-04 08:41:49 -06001366static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001367 struct hpsa_scsi_dev_t *sd[], int nsds)
1368{
1369 /* sd contains scsi3 addresses and devtypes, and inquiry
1370 * data. This function takes what's in sd to be the current
1371 * reality and updates h->dev[] to reflect that reality.
1372 */
1373 int i, entry, device_change, changes = 0;
1374 struct hpsa_scsi_dev_t *csd;
1375 unsigned long flags;
1376 struct hpsa_scsi_dev_t **added, **removed;
1377 int nadded, nremoved;
1378 struct Scsi_Host *sh = NULL;
1379
Scott Teelcfe5bad2011-10-26 16:21:07 -05001380 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1381 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001382
1383 if (!added || !removed) {
1384 dev_warn(&h->pdev->dev, "out of memory in "
1385 "adjust_hpsa_scsi_table\n");
1386 goto free_and_out;
1387 }
1388
1389 spin_lock_irqsave(&h->devlock, flags);
1390
1391 /* find any devices in h->dev[] that are not in
1392 * sd[] and remove them from h->dev[], and for any
1393 * devices which have changed, remove the old device
1394 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001395 * If minor device attributes change, just update
1396 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001397 */
1398 i = 0;
1399 nremoved = 0;
1400 nadded = 0;
1401 while (i < h->ndevices) {
1402 csd = h->dev[i];
1403 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1404 if (device_change == DEVICE_NOT_FOUND) {
1405 changes++;
1406 hpsa_scsi_remove_entry(h, hostno, i,
1407 removed, &nremoved);
1408 continue; /* remove ^^^, hence i not incremented */
1409 } else if (device_change == DEVICE_CHANGED) {
1410 changes++;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001411 hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
1412 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001413 /* Set it to NULL to prevent it from being freed
1414 * at the bottom of hpsa_update_scsi_devices()
1415 */
1416 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001417 } else if (device_change == DEVICE_UPDATED) {
1418 hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001419 }
1420 i++;
1421 }
1422
1423 /* Now, make sure every device listed in sd[] is also
1424 * listed in h->dev[], adding them if they aren't found
1425 */
1426
1427 for (i = 0; i < nsds; i++) {
1428 if (!sd[i]) /* if already added above. */
1429 continue;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001430
1431 /* Don't add devices which are NOT READY, FORMAT IN PROGRESS
1432 * as the SCSI mid-layer does not handle such devices well.
1433 * It relentlessly loops sending TUR at 3Hz, then READ(10)
1434 * at 160Hz, and prevents the system from coming up.
1435 */
1436 if (sd[i]->volume_offline) {
1437 hpsa_show_volume_status(h, sd[i]);
1438 dev_info(&h->pdev->dev, "c%db%dt%dl%d: temporarily offline\n",
1439 h->scsi_host->host_no,
1440 sd[i]->bus, sd[i]->target, sd[i]->lun);
1441 continue;
1442 }
1443
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001444 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1445 h->ndevices, &entry);
1446 if (device_change == DEVICE_NOT_FOUND) {
1447 changes++;
1448 if (hpsa_scsi_add_entry(h, hostno, sd[i],
1449 added, &nadded) != 0)
1450 break;
1451 sd[i] = NULL; /* prevent from being freed later. */
1452 } else if (device_change == DEVICE_CHANGED) {
1453 /* should never happen... */
1454 changes++;
1455 dev_warn(&h->pdev->dev,
1456 "device unexpectedly changed.\n");
1457 /* but if it does happen, we just ignore that device */
1458 }
1459 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001460 hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
1461
1462 /* Now that h->dev[]->phys_disk[] is coherent, we can enable
1463 * any logical drives that need it enabled.
1464 */
1465 for (i = 0; i < h->ndevices; i++)
1466 h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
1467
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001468 spin_unlock_irqrestore(&h->devlock, flags);
1469
Stephen M. Cameron98465902014-02-21 16:25:00 -06001470 /* Monitor devices which are in one of several NOT READY states to be
1471 * brought online later. This must be done without holding h->devlock,
1472 * so don't touch h->dev[]
1473 */
1474 for (i = 0; i < nsds; i++) {
1475 if (!sd[i]) /* if already added above. */
1476 continue;
1477 if (sd[i]->volume_offline)
1478 hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
1479 }
1480
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001481 /* Don't notify scsi mid layer of any changes the first time through
1482 * (or if there are no changes) scsi_scan_host will do it later the
1483 * first time through.
1484 */
1485 if (hostno == -1 || !changes)
1486 goto free_and_out;
1487
1488 sh = h->scsi_host;
1489 /* Notify scsi mid layer of any removed devices */
1490 for (i = 0; i < nremoved; i++) {
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001491 if (removed[i]->expose_state & HPSA_SCSI_ADD) {
1492 struct scsi_device *sdev =
1493 scsi_device_lookup(sh, removed[i]->bus,
1494 removed[i]->target, removed[i]->lun);
1495 if (sdev != NULL) {
1496 scsi_remove_device(sdev);
1497 scsi_device_put(sdev);
1498 } else {
1499 /*
1500 * We don't expect to get here.
1501 * future cmds to this device will get selection
1502 * timeout as if the device was gone.
1503 */
1504 dev_warn(&h->pdev->dev,
1505 "didn't find c%db%dt%dl%d for removal.\n",
1506 hostno, removed[i]->bus,
1507 removed[i]->target, removed[i]->lun);
1508 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001509 }
1510 kfree(removed[i]);
1511 removed[i] = NULL;
1512 }
1513
1514 /* Notify scsi mid layer of any added devices */
1515 for (i = 0; i < nadded; i++) {
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001516 if (!(added[i]->expose_state & HPSA_SCSI_ADD))
1517 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001518 if (scsi_add_device(sh, added[i]->bus,
1519 added[i]->target, added[i]->lun) == 0)
1520 continue;
1521 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1522 "device not added.\n", hostno, added[i]->bus,
1523 added[i]->target, added[i]->lun);
1524 /* now we have to remove it from h->dev,
1525 * since it didn't get added to scsi mid layer
1526 */
1527 fixup_botched_add(h, added[i]);
1528 }
1529
1530free_and_out:
1531 kfree(added);
1532 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001533}
1534
1535/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001536 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001537 * Assume's h->devlock is held.
1538 */
1539static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1540 int bus, int target, int lun)
1541{
1542 int i;
1543 struct hpsa_scsi_dev_t *sd;
1544
1545 for (i = 0; i < h->ndevices; i++) {
1546 sd = h->dev[i];
1547 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1548 return sd;
1549 }
1550 return NULL;
1551}
1552
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001553static int hpsa_slave_alloc(struct scsi_device *sdev)
1554{
1555 struct hpsa_scsi_dev_t *sd;
1556 unsigned long flags;
1557 struct ctlr_info *h;
1558
1559 h = sdev_to_hba(sdev);
1560 spin_lock_irqsave(&h->devlock, flags);
1561 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1562 sdev_id(sdev), sdev->lun);
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001563 if (likely(sd)) {
Don Brace03383732015-01-23 16:43:30 -06001564 atomic_set(&sd->ioaccel_cmds_out, 0);
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001565 sdev->hostdata = (sd->expose_state & HPSA_SCSI_ADD) ? sd : NULL;
1566 } else
1567 sdev->hostdata = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001568 spin_unlock_irqrestore(&h->devlock, flags);
1569 return 0;
1570}
1571
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001572/* configure scsi device based on internal per-device structure */
1573static int hpsa_slave_configure(struct scsi_device *sdev)
1574{
1575 struct hpsa_scsi_dev_t *sd;
1576 int queue_depth;
1577
1578 sd = sdev->hostdata;
1579 sdev->no_uld_attach = !sd || !(sd->expose_state & HPSA_ULD_ATTACH);
1580
1581 if (sd)
1582 queue_depth = sd->queue_depth != 0 ?
1583 sd->queue_depth : sdev->host->can_queue;
1584 else
1585 queue_depth = sdev->host->can_queue;
1586
1587 scsi_change_queue_depth(sdev, queue_depth);
1588
1589 return 0;
1590}
1591
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001592static void hpsa_slave_destroy(struct scsi_device *sdev)
1593{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06001594 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001595}
1596
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001597static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
1598{
1599 int i;
1600
1601 if (!h->cmd_sg_list)
1602 return;
1603 for (i = 0; i < h->nr_cmds; i++) {
1604 kfree(h->cmd_sg_list[i]);
1605 h->cmd_sg_list[i] = NULL;
1606 }
1607 kfree(h->cmd_sg_list);
1608 h->cmd_sg_list = NULL;
1609}
1610
1611static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
1612{
1613 int i;
1614
1615 if (h->chainsize <= 0)
1616 return 0;
1617
1618 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
1619 GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06001620 if (!h->cmd_sg_list) {
1621 dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001622 return -ENOMEM;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06001623 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001624 for (i = 0; i < h->nr_cmds; i++) {
1625 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
1626 h->chainsize, GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06001627 if (!h->cmd_sg_list[i]) {
1628 dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001629 goto clean;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06001630 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001631 }
1632 return 0;
1633
1634clean:
1635 hpsa_free_sg_chain_blocks(h);
1636 return -ENOMEM;
1637}
1638
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001639static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001640 struct CommandList *c)
1641{
1642 struct SGDescriptor *chain_sg, *chain_block;
1643 u64 temp64;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06001644 u32 chain_len;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001645
1646 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1647 chain_block = h->cmd_sg_list[c->cmdindex];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06001648 chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
1649 chain_len = sizeof(*chain_sg) *
Don Brace2b08b3e2015-01-23 16:41:09 -06001650 (le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06001651 chain_sg->Len = cpu_to_le32(chain_len);
1652 temp64 = pci_map_single(h->pdev, chain_block, chain_len,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001653 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001654 if (dma_mapping_error(&h->pdev->dev, temp64)) {
1655 /* prevent subsequent unmapping */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06001656 chain_sg->Addr = cpu_to_le64(0);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001657 return -1;
1658 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06001659 chain_sg->Addr = cpu_to_le64(temp64);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001660 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001661}
1662
1663static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
1664 struct CommandList *c)
1665{
1666 struct SGDescriptor *chain_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001667
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06001668 if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001669 return;
1670
1671 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06001672 pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
1673 le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001674}
1675
Scott Teela09c1442014-02-18 13:57:21 -06001676
1677/* Decode the various types of errors on ioaccel2 path.
1678 * Return 1 for any error that should generate a RAID path retry.
1679 * Return 0 for errors that don't require a RAID path retry.
1680 */
1681static int handle_ioaccel_mode2_error(struct ctlr_info *h,
Scott Teelc3497752014-02-18 13:56:34 -06001682 struct CommandList *c,
1683 struct scsi_cmnd *cmd,
1684 struct io_accel2_cmd *c2)
1685{
1686 int data_len;
Scott Teela09c1442014-02-18 13:57:21 -06001687 int retry = 0;
Scott Teelc3497752014-02-18 13:56:34 -06001688
1689 switch (c2->error_data.serv_response) {
1690 case IOACCEL2_SERV_RESPONSE_COMPLETE:
1691 switch (c2->error_data.status) {
1692 case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1693 break;
1694 case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1695 dev_warn(&h->pdev->dev,
1696 "%s: task complete with check condition.\n",
1697 "HP SSD Smart Path");
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05001698 cmd->result |= SAM_STAT_CHECK_CONDITION;
Scott Teelc3497752014-02-18 13:56:34 -06001699 if (c2->error_data.data_present !=
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05001700 IOACCEL2_SENSE_DATA_PRESENT) {
1701 memset(cmd->sense_buffer, 0,
1702 SCSI_SENSE_BUFFERSIZE);
Scott Teelc3497752014-02-18 13:56:34 -06001703 break;
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05001704 }
Scott Teelc3497752014-02-18 13:56:34 -06001705 /* copy the sense data */
1706 data_len = c2->error_data.sense_data_len;
1707 if (data_len > SCSI_SENSE_BUFFERSIZE)
1708 data_len = SCSI_SENSE_BUFFERSIZE;
1709 if (data_len > sizeof(c2->error_data.sense_data_buff))
1710 data_len =
1711 sizeof(c2->error_data.sense_data_buff);
1712 memcpy(cmd->sense_buffer,
1713 c2->error_data.sense_data_buff, data_len);
Scott Teela09c1442014-02-18 13:57:21 -06001714 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001715 break;
1716 case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1717 dev_warn(&h->pdev->dev,
1718 "%s: task complete with BUSY status.\n",
1719 "HP SSD Smart Path");
Scott Teela09c1442014-02-18 13:57:21 -06001720 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001721 break;
1722 case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1723 dev_warn(&h->pdev->dev,
1724 "%s: task complete with reservation conflict.\n",
1725 "HP SSD Smart Path");
Scott Teela09c1442014-02-18 13:57:21 -06001726 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001727 break;
1728 case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
1729 /* Make scsi midlayer do unlimited retries */
1730 cmd->result = DID_IMM_RETRY << 16;
1731 break;
1732 case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1733 dev_warn(&h->pdev->dev,
1734 "%s: task complete with aborted status.\n",
1735 "HP SSD Smart Path");
Scott Teela09c1442014-02-18 13:57:21 -06001736 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001737 break;
1738 default:
1739 dev_warn(&h->pdev->dev,
1740 "%s: task complete with unrecognized status: 0x%02x\n",
1741 "HP SSD Smart Path", c2->error_data.status);
Scott Teela09c1442014-02-18 13:57:21 -06001742 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001743 break;
1744 }
1745 break;
1746 case IOACCEL2_SERV_RESPONSE_FAILURE:
1747 /* don't expect to get here. */
1748 dev_warn(&h->pdev->dev,
1749 "unexpected delivery or target failure, status = 0x%02x\n",
1750 c2->error_data.status);
Scott Teela09c1442014-02-18 13:57:21 -06001751 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001752 break;
1753 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1754 break;
1755 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1756 break;
1757 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1758 dev_warn(&h->pdev->dev, "task management function rejected.\n");
Scott Teela09c1442014-02-18 13:57:21 -06001759 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001760 break;
1761 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1762 dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1763 break;
1764 default:
1765 dev_warn(&h->pdev->dev,
1766 "%s: Unrecognized server response: 0x%02x\n",
Scott Teela09c1442014-02-18 13:57:21 -06001767 "HP SSD Smart Path",
1768 c2->error_data.serv_response);
1769 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001770 break;
1771 }
Scott Teela09c1442014-02-18 13:57:21 -06001772
1773 return retry; /* retry on raid path? */
Scott Teelc3497752014-02-18 13:56:34 -06001774}
1775
1776static void process_ioaccel2_completion(struct ctlr_info *h,
1777 struct CommandList *c, struct scsi_cmnd *cmd,
1778 struct hpsa_scsi_dev_t *dev)
1779{
1780 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
1781
1782 /* check for good status */
1783 if (likely(c2->error_data.serv_response == 0 &&
1784 c2->error_data.status == 0)) {
1785 cmd_free(h, c);
1786 cmd->scsi_done(cmd);
1787 return;
1788 }
1789
1790 /* Any RAID offload error results in retry which will use
1791 * the normal I/O path so the controller can handle whatever's
1792 * wrong.
1793 */
1794 if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1795 c2->error_data.serv_response ==
1796 IOACCEL2_SERV_RESPONSE_FAILURE) {
Don Brace080ef1c2015-01-23 16:43:25 -06001797 if (c2->error_data.status ==
1798 IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
1799 dev->offload_enabled = 0;
1800 goto retry_cmd;
Scott Teelc3497752014-02-18 13:56:34 -06001801 }
Don Brace080ef1c2015-01-23 16:43:25 -06001802
1803 if (handle_ioaccel_mode2_error(h, c, cmd, c2))
1804 goto retry_cmd;
1805
Scott Teelc3497752014-02-18 13:56:34 -06001806 cmd_free(h, c);
1807 cmd->scsi_done(cmd);
Don Brace080ef1c2015-01-23 16:43:25 -06001808 return;
1809
1810retry_cmd:
1811 INIT_WORK(&c->work, hpsa_command_resubmit_worker);
1812 queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
Scott Teelc3497752014-02-18 13:56:34 -06001813}
1814
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05001815static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001816{
1817 struct scsi_cmnd *cmd;
1818 struct ctlr_info *h;
1819 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001820 struct hpsa_scsi_dev_t *dev;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001821
1822 unsigned char sense_key;
1823 unsigned char asc; /* additional sense code */
1824 unsigned char ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001825 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001826
1827 ei = cp->err_info;
Stephen Cameron7fa30302015-01-23 16:44:30 -06001828 cmd = cp->scsi_cmd;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001829 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001830 dev = cmd->device->hostdata;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001831
1832 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06001833 if ((cp->cmd_type == CMD_SCSI) &&
Don Brace2b08b3e2015-01-23 16:41:09 -06001834 (le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001835 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001836
1837 cmd->result = (DID_OK << 16); /* host byte */
1838 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06001839
Don Brace03383732015-01-23 16:43:30 -06001840 if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
1841 atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
1842
Scott Teelc3497752014-02-18 13:56:34 -06001843 if (cp->cmd_type == CMD_IOACCEL2)
1844 return process_ioaccel2_completion(h, cp, cmd, dev);
1845
Stephen M. Cameron55126722010-02-25 14:03:01 -06001846 cmd->result |= ei->ScsiStatus;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001847
Robert Elliott6aa4c362014-07-03 10:18:19 -05001848 scsi_set_resid(cmd, ei->ResidualCnt);
1849 if (ei->CommandStatus == 0) {
Don Brace03383732015-01-23 16:43:30 -06001850 if (cp->cmd_type == CMD_IOACCEL1)
1851 atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
Robert Elliott6aa4c362014-07-03 10:18:19 -05001852 cmd_free(h, cp);
1853 cmd->scsi_done(cmd);
1854 return;
1855 }
1856
1857 /* copy the sense data */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001858 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1859 sense_data_size = SCSI_SENSE_BUFFERSIZE;
1860 else
1861 sense_data_size = sizeof(ei->SenseInfo);
1862 if (ei->SenseLen < sense_data_size)
1863 sense_data_size = ei->SenseLen;
1864
1865 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001866
Matt Gatese1f7de02014-02-18 13:55:17 -06001867 /* For I/O accelerator commands, copy over some fields to the normal
1868 * CISS header used below for error handling.
1869 */
1870 if (cp->cmd_type == CMD_IOACCEL1) {
1871 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06001872 cp->Header.SGList = scsi_sg_count(cmd);
1873 cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
1874 cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
1875 IOACCEL1_IOFLAGS_CDBLEN_MASK;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06001876 cp->Header.tag = c->tag;
Matt Gatese1f7de02014-02-18 13:55:17 -06001877 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1878 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001879
1880 /* Any RAID offload error results in retry which will use
1881 * the normal I/O path so the controller can handle whatever's
1882 * wrong.
1883 */
1884 if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1885 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1886 dev->offload_enabled = 0;
Don Brace080ef1c2015-01-23 16:43:25 -06001887 INIT_WORK(&cp->work, hpsa_command_resubmit_worker);
1888 queue_work_on(raw_smp_processor_id(),
1889 h->resubmit_wq, &cp->work);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001890 return;
1891 }
Matt Gatese1f7de02014-02-18 13:55:17 -06001892 }
1893
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001894 /* an error has occurred */
1895 switch (ei->CommandStatus) {
1896
1897 case CMD_TARGET_STATUS:
1898 if (ei->ScsiStatus) {
1899 /* Get sense key */
1900 sense_key = 0xf & ei->SenseInfo[2];
1901 /* Get additional sense code */
1902 asc = ei->SenseInfo[12];
1903 /* Get addition sense code qualifier */
1904 ascq = ei->SenseInfo[13];
1905 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001906 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates1d3b3602010-02-04 08:43:00 -06001907 if (sense_key == ABORTED_COMMAND) {
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05001908 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06001909 break;
1910 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001911 break;
1912 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001913 /* Problem was not a check condition
1914 * Pass it up to the upper layers...
1915 */
1916 if (ei->ScsiStatus) {
1917 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1918 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1919 "Returning result: 0x%x\n",
1920 cp, ei->ScsiStatus,
1921 sense_key, asc, ascq,
1922 cmd->result);
1923 } else { /* scsi status is zero??? How??? */
1924 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1925 "Returning no connection.\n", cp),
1926
1927 /* Ordinarily, this case should never happen,
1928 * but there is a bug in some released firmware
1929 * revisions that allows it to happen if, for
1930 * example, a 4100 backplane loses power and
1931 * the tape drive is in it. We assume that
1932 * it's a fatal error of some kind because we
1933 * can't show that it wasn't. We will make it
1934 * look like selection timeout since that is
1935 * the most common reason for this to occur,
1936 * and it's severe enough.
1937 */
1938
1939 cmd->result = DID_NO_CONNECT << 16;
1940 }
1941 break;
1942
1943 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1944 break;
1945 case CMD_DATA_OVERRUN:
Stephen Cameronf42e81e2015-01-23 16:44:35 -06001946 dev_warn(&h->pdev->dev,
1947 "CDB %16phN data overrun\n", cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001948 break;
1949 case CMD_INVALID: {
1950 /* print_bytes(cp, sizeof(*cp), 1, 0);
1951 print_cmd(cp); */
1952 /* We get CMD_INVALID if you address a non-existent device
1953 * instead of a selection timeout (no response). You will
1954 * see this if you yank out a drive, then try to access it.
1955 * This is kind of a shame because it means that any other
1956 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1957 * missing target. */
1958 cmd->result = DID_NO_CONNECT << 16;
1959 }
1960 break;
1961 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001962 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06001963 dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
1964 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001965 break;
1966 case CMD_HARDWARE_ERR:
1967 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06001968 dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
1969 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001970 break;
1971 case CMD_CONNECTION_LOST:
1972 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06001973 dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
1974 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001975 break;
1976 case CMD_ABORTED:
1977 cmd->result = DID_ABORT << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06001978 dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
1979 cp->Request.CDB, ei->ScsiStatus);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001980 break;
1981 case CMD_ABORT_FAILED:
1982 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06001983 dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
1984 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001985 break;
1986 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05001987 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
Stephen Cameronf42e81e2015-01-23 16:44:35 -06001988 dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
1989 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001990 break;
1991 case CMD_TIMEOUT:
1992 cmd->result = DID_TIME_OUT << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06001993 dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
1994 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001995 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001996 case CMD_UNABORTABLE:
1997 cmd->result = DID_ERROR << 16;
1998 dev_warn(&h->pdev->dev, "Command unabortable\n");
1999 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002000 case CMD_IOACCEL_DISABLED:
2001 /* This only handles the direct pass-through case since RAID
2002 * offload is handled above. Just attempt a retry.
2003 */
2004 cmd->result = DID_SOFT_ERROR << 16;
2005 dev_warn(&h->pdev->dev,
2006 "cp %p had HP SSD Smart Path error\n", cp);
2007 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002008 default:
2009 cmd->result = DID_ERROR << 16;
2010 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2011 cp, ei->CommandStatus);
2012 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002013 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02002014 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002015}
2016
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002017static void hpsa_pci_unmap(struct pci_dev *pdev,
2018 struct CommandList *c, int sg_used, int data_direction)
2019{
2020 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002021
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002022 for (i = 0; i < sg_used; i++)
2023 pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
2024 le32_to_cpu(c->SG[i].Len),
2025 data_direction);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002026}
2027
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002028static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002029 struct CommandList *cp,
2030 unsigned char *buf,
2031 size_t buflen,
2032 int data_direction)
2033{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002034 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002035
2036 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2037 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002038 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002039 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002040 }
2041
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002042 addr64 = pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06002043 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002044 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06002045 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002046 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002047 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06002048 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002049 cp->SG[0].Addr = cpu_to_le64(addr64);
2050 cp->SG[0].Len = cpu_to_le32(buflen);
2051 cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
2052 cp->Header.SGList = 1; /* no. SGs contig in this cmd */
2053 cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002054 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002055}
2056
2057static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
2058 struct CommandList *c)
2059{
2060 DECLARE_COMPLETION_ONSTACK(wait);
2061
2062 c->waiting = &wait;
2063 enqueue_cmd_and_start_io(h, c);
2064 wait_for_completion(&wait);
2065}
2066
Stephen M. Cameron094963d2014-05-29 10:53:18 -05002067static u32 lockup_detected(struct ctlr_info *h)
2068{
2069 int cpu;
2070 u32 rc, *lockup_detected;
2071
2072 cpu = get_cpu();
2073 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2074 rc = *lockup_detected;
2075 put_cpu();
2076 return rc;
2077}
2078
Stephen M. Camerona0c12412011-10-26 16:22:04 -05002079static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
2080 struct CommandList *c)
2081{
Stephen M. Camerona0c12412011-10-26 16:22:04 -05002082 /* If controller lockup detected, fake a hardware error. */
Stephen M. Cameron094963d2014-05-29 10:53:18 -05002083 if (unlikely(lockup_detected(h)))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05002084 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05002085 else
Stephen M. Camerona0c12412011-10-26 16:22:04 -05002086 hpsa_scsi_do_simple_cmd_core(h, c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05002087}
2088
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002089#define MAX_DRIVER_CMD_RETRIES 25
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002090static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
2091 struct CommandList *c, int data_direction)
2092{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002093 int backoff_time = 10, retry_count = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002094
2095 do {
Joe Perches7630abd2011-05-08 23:32:40 -07002096 memset(c->err_info, 0, sizeof(*c->err_info));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002097 hpsa_scsi_do_simple_cmd_core(h, c);
2098 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002099 if (retry_count > 3) {
2100 msleep(backoff_time);
2101 if (backoff_time < 1000)
2102 backoff_time *= 2;
2103 }
Matt Bondurant852af202012-05-01 11:42:35 -05002104 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002105 check_for_busy(h, c)) &&
2106 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002107 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
2108}
2109
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002110static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2111 struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002112{
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002113 const u8 *cdb = c->Request.CDB;
2114 const u8 *lun = c->Header.LUN.LunAddrBytes;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002115
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002116 dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2117 " CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2118 txt, lun[0], lun[1], lun[2], lun[3],
2119 lun[4], lun[5], lun[6], lun[7],
2120 cdb[0], cdb[1], cdb[2], cdb[3],
2121 cdb[4], cdb[5], cdb[6], cdb[7],
2122 cdb[8], cdb[9], cdb[10], cdb[11],
2123 cdb[12], cdb[13], cdb[14], cdb[15]);
2124}
2125
2126static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2127 struct CommandList *cp)
2128{
2129 const struct ErrorInfo *ei = cp->err_info;
2130 struct device *d = &cp->h->pdev->dev;
2131 const u8 *sd = ei->SenseInfo;
2132
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002133 switch (ei->CommandStatus) {
2134 case CMD_TARGET_STATUS:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002135 hpsa_print_cmd(h, "SCSI status", cp);
2136 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
2137 dev_warn(d, "SCSI Status = 02, Sense key = %02x, ASC = %02x, ASCQ = %02x\n",
2138 sd[2] & 0x0f, sd[12], sd[13]);
2139 else
2140 dev_warn(d, "SCSI Status = %02x\n", ei->ScsiStatus);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002141 if (ei->ScsiStatus == 0)
2142 dev_warn(d, "SCSI status is abnormally zero. "
2143 "(probably indicates selection timeout "
2144 "reported incorrectly due to a known "
2145 "firmware bug, circa July, 2001.)\n");
2146 break;
2147 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002148 break;
2149 case CMD_DATA_OVERRUN:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002150 hpsa_print_cmd(h, "overrun condition", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002151 break;
2152 case CMD_INVALID: {
2153 /* controller unfortunately reports SCSI passthru's
2154 * to non-existent targets as invalid commands.
2155 */
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002156 hpsa_print_cmd(h, "invalid command", cp);
2157 dev_warn(d, "probably means device no longer present\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002158 }
2159 break;
2160 case CMD_PROTOCOL_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002161 hpsa_print_cmd(h, "protocol error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002162 break;
2163 case CMD_HARDWARE_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002164 hpsa_print_cmd(h, "hardware error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002165 break;
2166 case CMD_CONNECTION_LOST:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002167 hpsa_print_cmd(h, "connection lost", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002168 break;
2169 case CMD_ABORTED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002170 hpsa_print_cmd(h, "aborted", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002171 break;
2172 case CMD_ABORT_FAILED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002173 hpsa_print_cmd(h, "abort failed", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002174 break;
2175 case CMD_UNSOLICITED_ABORT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002176 hpsa_print_cmd(h, "unsolicited abort", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002177 break;
2178 case CMD_TIMEOUT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002179 hpsa_print_cmd(h, "timed out", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002180 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002181 case CMD_UNABORTABLE:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002182 hpsa_print_cmd(h, "unabortable", cp);
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002183 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002184 default:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002185 hpsa_print_cmd(h, "unknown status", cp);
2186 dev_warn(d, "Unknown command status %x\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002187 ei->CommandStatus);
2188 }
2189}
2190
2191static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002192 u16 page, unsigned char *buf,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002193 unsigned char bufsize)
2194{
2195 int rc = IO_OK;
2196 struct CommandList *c;
2197 struct ErrorInfo *ei;
2198
Stephen Cameron45fcb862015-01-23 16:43:04 -06002199 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002200
Stephen Cameron574f05d2015-01-23 16:43:20 -06002201 if (c == NULL) {
Stephen Cameron45fcb862015-01-23 16:43:04 -06002202 dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06002203 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002204 }
2205
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002206 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2207 page, scsi3addr, TYPE_CMD)) {
2208 rc = -1;
2209 goto out;
2210 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002211 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2212 ei = c->err_info;
2213 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002214 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002215 rc = -1;
2216 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002217out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002218 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002219 return rc;
2220}
2221
Stephen M. Cameron316b2212014-02-21 16:25:15 -06002222static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
2223 unsigned char *scsi3addr, unsigned char page,
2224 struct bmic_controller_parameters *buf, size_t bufsize)
2225{
2226 int rc = IO_OK;
2227 struct CommandList *c;
2228 struct ErrorInfo *ei;
2229
Stephen Cameron45fcb862015-01-23 16:43:04 -06002230 c = cmd_alloc(h);
Stephen M. Cameron316b2212014-02-21 16:25:15 -06002231 if (c == NULL) { /* trouble... */
Stephen Cameron45fcb862015-01-23 16:43:04 -06002232 dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
Stephen M. Cameron316b2212014-02-21 16:25:15 -06002233 return -ENOMEM;
2234 }
2235
2236 if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
2237 page, scsi3addr, TYPE_CMD)) {
2238 rc = -1;
2239 goto out;
2240 }
2241 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2242 ei = c->err_info;
2243 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2244 hpsa_scsi_interpret_error(h, c);
2245 rc = -1;
2246 }
2247out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002248 cmd_free(h, c);
Stephen M. Cameron316b2212014-02-21 16:25:15 -06002249 return rc;
2250 }
2251
Scott Teelbf711ac2014-02-18 13:56:39 -06002252static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
2253 u8 reset_type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002254{
2255 int rc = IO_OK;
2256 struct CommandList *c;
2257 struct ErrorInfo *ei;
2258
Stephen Cameron45fcb862015-01-23 16:43:04 -06002259 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002260
2261 if (c == NULL) { /* trouble... */
Stephen Cameron45fcb862015-01-23 16:43:04 -06002262 dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
Stephen M. Camerone9ea04a2010-02-25 14:03:06 -06002263 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002264 }
2265
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002266 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teelbf711ac2014-02-18 13:56:39 -06002267 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2268 scsi3addr, TYPE_MSG);
2269 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002270 hpsa_scsi_do_simple_cmd_core(h, c);
2271 /* no unmap needed here because no data xfer. */
2272
2273 ei = c->err_info;
2274 if (ei->CommandStatus != 0) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002275 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002276 rc = -1;
2277 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06002278 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002279 return rc;
2280}
2281
2282static void hpsa_get_raid_level(struct ctlr_info *h,
2283 unsigned char *scsi3addr, unsigned char *raid_level)
2284{
2285 int rc;
2286 unsigned char *buf;
2287
2288 *raid_level = RAID_UNKNOWN;
2289 buf = kzalloc(64, GFP_KERNEL);
2290 if (!buf)
2291 return;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002292 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002293 if (rc == 0)
2294 *raid_level = buf[8];
2295 if (*raid_level > RAID_UNKNOWN)
2296 *raid_level = RAID_UNKNOWN;
2297 kfree(buf);
2298 return;
2299}
2300
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002301#define HPSA_MAP_DEBUG
2302#ifdef HPSA_MAP_DEBUG
2303static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2304 struct raid_map_data *map_buff)
2305{
2306 struct raid_map_disk_data *dd = &map_buff->data[0];
2307 int map, row, col;
2308 u16 map_cnt, row_cnt, disks_per_row;
2309
2310 if (rc != 0)
2311 return;
2312
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06002313 /* Show details only if debugging has been activated. */
2314 if (h->raid_offload_debug < 2)
2315 return;
2316
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002317 dev_info(&h->pdev->dev, "structure_size = %u\n",
2318 le32_to_cpu(map_buff->structure_size));
2319 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2320 le32_to_cpu(map_buff->volume_blk_size));
2321 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2322 le64_to_cpu(map_buff->volume_blk_cnt));
2323 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2324 map_buff->phys_blk_shift);
2325 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2326 map_buff->parity_rotation_shift);
2327 dev_info(&h->pdev->dev, "strip_size = %u\n",
2328 le16_to_cpu(map_buff->strip_size));
2329 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2330 le64_to_cpu(map_buff->disk_starting_blk));
2331 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2332 le64_to_cpu(map_buff->disk_blk_cnt));
2333 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2334 le16_to_cpu(map_buff->data_disks_per_row));
2335 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2336 le16_to_cpu(map_buff->metadata_disks_per_row));
2337 dev_info(&h->pdev->dev, "row_cnt = %u\n",
2338 le16_to_cpu(map_buff->row_cnt));
2339 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2340 le16_to_cpu(map_buff->layout_map_count));
Don Brace2b08b3e2015-01-23 16:41:09 -06002341 dev_info(&h->pdev->dev, "flags = 0x%x\n",
Scott Teeldd0e19f2014-02-18 13:57:31 -06002342 le16_to_cpu(map_buff->flags));
Don Brace2b08b3e2015-01-23 16:41:09 -06002343 dev_info(&h->pdev->dev, "encrypytion = %s\n",
2344 le16_to_cpu(map_buff->flags) &
2345 RAID_MAP_FLAG_ENCRYPT_ON ? "ON" : "OFF");
Scott Teeldd0e19f2014-02-18 13:57:31 -06002346 dev_info(&h->pdev->dev, "dekindex = %u\n",
2347 le16_to_cpu(map_buff->dekindex));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002348 map_cnt = le16_to_cpu(map_buff->layout_map_count);
2349 for (map = 0; map < map_cnt; map++) {
2350 dev_info(&h->pdev->dev, "Map%u:\n", map);
2351 row_cnt = le16_to_cpu(map_buff->row_cnt);
2352 for (row = 0; row < row_cnt; row++) {
2353 dev_info(&h->pdev->dev, " Row%u:\n", row);
2354 disks_per_row =
2355 le16_to_cpu(map_buff->data_disks_per_row);
2356 for (col = 0; col < disks_per_row; col++, dd++)
2357 dev_info(&h->pdev->dev,
2358 " D%02u: h=0x%04x xor=%u,%u\n",
2359 col, dd->ioaccel_handle,
2360 dd->xor_mult[0], dd->xor_mult[1]);
2361 disks_per_row =
2362 le16_to_cpu(map_buff->metadata_disks_per_row);
2363 for (col = 0; col < disks_per_row; col++, dd++)
2364 dev_info(&h->pdev->dev,
2365 " M%02u: h=0x%04x xor=%u,%u\n",
2366 col, dd->ioaccel_handle,
2367 dd->xor_mult[0], dd->xor_mult[1]);
2368 }
2369 }
2370}
2371#else
2372static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2373 __attribute__((unused)) int rc,
2374 __attribute__((unused)) struct raid_map_data *map_buff)
2375{
2376}
2377#endif
2378
2379static int hpsa_get_raid_map(struct ctlr_info *h,
2380 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2381{
2382 int rc = 0;
2383 struct CommandList *c;
2384 struct ErrorInfo *ei;
2385
Stephen Cameron45fcb862015-01-23 16:43:04 -06002386 c = cmd_alloc(h);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002387 if (c == NULL) {
Stephen Cameron45fcb862015-01-23 16:43:04 -06002388 dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002389 return -ENOMEM;
2390 }
2391 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2392 sizeof(this_device->raid_map), 0,
2393 scsi3addr, TYPE_CMD)) {
2394 dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
Stephen Cameron45fcb862015-01-23 16:43:04 -06002395 cmd_free(h, c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002396 return -ENOMEM;
2397 }
2398 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2399 ei = c->err_info;
2400 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002401 hpsa_scsi_interpret_error(h, c);
Stephen Cameron45fcb862015-01-23 16:43:04 -06002402 cmd_free(h, c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002403 return -1;
2404 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06002405 cmd_free(h, c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002406
2407 /* @todo in the future, dynamically allocate RAID map memory */
2408 if (le32_to_cpu(this_device->raid_map.structure_size) >
2409 sizeof(this_device->raid_map)) {
2410 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2411 rc = -1;
2412 }
2413 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2414 return rc;
2415}
2416
Don Brace03383732015-01-23 16:43:30 -06002417static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
2418 unsigned char scsi3addr[], u16 bmic_device_index,
2419 struct bmic_identify_physical_device *buf, size_t bufsize)
2420{
2421 int rc = IO_OK;
2422 struct CommandList *c;
2423 struct ErrorInfo *ei;
2424
2425 c = cmd_alloc(h);
2426 rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
2427 0, RAID_CTLR_LUNID, TYPE_CMD);
2428 if (rc)
2429 goto out;
2430
2431 c->Request.CDB[2] = bmic_device_index & 0xff;
2432 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
2433
2434 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2435 ei = c->err_info;
2436 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2437 hpsa_scsi_interpret_error(h, c);
2438 rc = -1;
2439 }
2440out:
2441 cmd_free(h, c);
2442 return rc;
2443}
2444
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06002445static int hpsa_vpd_page_supported(struct ctlr_info *h,
2446 unsigned char scsi3addr[], u8 page)
2447{
2448 int rc;
2449 int i;
2450 int pages;
2451 unsigned char *buf, bufsize;
2452
2453 buf = kzalloc(256, GFP_KERNEL);
2454 if (!buf)
2455 return 0;
2456
2457 /* Get the size of the page list first */
2458 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2459 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
2460 buf, HPSA_VPD_HEADER_SZ);
2461 if (rc != 0)
2462 goto exit_unsupported;
2463 pages = buf[3];
2464 if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
2465 bufsize = pages + HPSA_VPD_HEADER_SZ;
2466 else
2467 bufsize = 255;
2468
2469 /* Get the whole VPD page list */
2470 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2471 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
2472 buf, bufsize);
2473 if (rc != 0)
2474 goto exit_unsupported;
2475
2476 pages = buf[3];
2477 for (i = 1; i <= pages; i++)
2478 if (buf[3 + i] == page)
2479 goto exit_supported;
2480exit_unsupported:
2481 kfree(buf);
2482 return 0;
2483exit_supported:
2484 kfree(buf);
2485 return 1;
2486}
2487
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002488static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2489 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2490{
2491 int rc;
2492 unsigned char *buf;
2493 u8 ioaccel_status;
2494
2495 this_device->offload_config = 0;
2496 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002497 this_device->offload_to_be_enabled = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002498
2499 buf = kzalloc(64, GFP_KERNEL);
2500 if (!buf)
2501 return;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06002502 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
2503 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002504 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002505 VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002506 if (rc != 0)
2507 goto out;
2508
2509#define IOACCEL_STATUS_BYTE 4
2510#define OFFLOAD_CONFIGURED_BIT 0x01
2511#define OFFLOAD_ENABLED_BIT 0x02
2512 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2513 this_device->offload_config =
2514 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2515 if (this_device->offload_config) {
2516 this_device->offload_enabled =
2517 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2518 if (hpsa_get_raid_map(h, scsi3addr, this_device))
2519 this_device->offload_enabled = 0;
2520 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002521 this_device->offload_to_be_enabled = this_device->offload_enabled;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002522out:
2523 kfree(buf);
2524 return;
2525}
2526
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002527/* Get the device id from inquiry page 0x83 */
2528static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2529 unsigned char *device_id, int buflen)
2530{
2531 int rc;
2532 unsigned char *buf;
2533
2534 if (buflen > 16)
2535 buflen = 16;
2536 buf = kzalloc(64, GFP_KERNEL);
2537 if (!buf)
Stephen M. Camerona84d7942014-05-29 10:54:20 -05002538 return -ENOMEM;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002539 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002540 if (rc == 0)
2541 memcpy(device_id, &buf[8], buflen);
2542 kfree(buf);
2543 return rc != 0;
2544}
2545
2546static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
Don Brace03383732015-01-23 16:43:30 -06002547 void *buf, int bufsize,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002548 int extended_response)
2549{
2550 int rc = IO_OK;
2551 struct CommandList *c;
2552 unsigned char scsi3addr[8];
2553 struct ErrorInfo *ei;
2554
Stephen Cameron45fcb862015-01-23 16:43:04 -06002555 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002556 if (c == NULL) { /* trouble... */
Stephen Cameron45fcb862015-01-23 16:43:04 -06002557 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002558 return -1;
2559 }
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06002560 /* address the controller */
2561 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002562 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2563 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2564 rc = -1;
2565 goto out;
2566 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002567 if (extended_response)
2568 c->Request.CDB[1] = extended_response;
2569 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2570 ei = c->err_info;
2571 if (ei->CommandStatus != 0 &&
2572 ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002573 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002574 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002575 } else {
Don Brace03383732015-01-23 16:43:30 -06002576 struct ReportLUNdata *rld = buf;
2577
2578 if (rld->extended_response_flag != extended_response) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002579 dev_err(&h->pdev->dev,
2580 "report luns requested format %u, got %u\n",
2581 extended_response,
Don Brace03383732015-01-23 16:43:30 -06002582 rld->extended_response_flag);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002583 rc = -1;
2584 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002585 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002586out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002587 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002588 return rc;
2589}
2590
2591static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06002592 struct ReportExtendedLUNdata *buf, int bufsize)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002593{
Don Brace03383732015-01-23 16:43:30 -06002594 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
2595 HPSA_REPORT_PHYS_EXTENDED);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002596}
2597
2598static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2599 struct ReportLUNdata *buf, int bufsize)
2600{
2601 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2602}
2603
2604static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2605 int bus, int target, int lun)
2606{
2607 device->bus = bus;
2608 device->target = target;
2609 device->lun = lun;
2610}
2611
Stephen M. Cameron98465902014-02-21 16:25:00 -06002612/* Use VPD inquiry to get details of volume status */
2613static int hpsa_get_volume_status(struct ctlr_info *h,
2614 unsigned char scsi3addr[])
2615{
2616 int rc;
2617 int status;
2618 int size;
2619 unsigned char *buf;
2620
2621 buf = kzalloc(64, GFP_KERNEL);
2622 if (!buf)
2623 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
2624
2625 /* Does controller have VPD for logical volume status? */
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05002626 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
Stephen M. Cameron98465902014-02-21 16:25:00 -06002627 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06002628
2629 /* Get the size of the VPD return buffer */
2630 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
2631 buf, HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05002632 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06002633 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06002634 size = buf[3];
2635
2636 /* Now get the whole VPD buffer */
2637 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
2638 buf, size + HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05002639 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06002640 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06002641 status = buf[4]; /* status byte */
2642
2643 kfree(buf);
2644 return status;
2645exit_failed:
2646 kfree(buf);
2647 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
2648}
2649
2650/* Determine offline status of a volume.
2651 * Return either:
2652 * 0 (not offline)
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05002653 * 0xff (offline for unknown reasons)
Stephen M. Cameron98465902014-02-21 16:25:00 -06002654 * # (integer code indicating one of several NOT READY states
2655 * describing why a volume is to be kept offline)
2656 */
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05002657static int hpsa_volume_offline(struct ctlr_info *h,
Stephen M. Cameron98465902014-02-21 16:25:00 -06002658 unsigned char scsi3addr[])
2659{
2660 struct CommandList *c;
2661 unsigned char *sense, sense_key, asc, ascq;
2662 int ldstat = 0;
2663 u16 cmd_status;
2664 u8 scsi_status;
2665#define ASC_LUN_NOT_READY 0x04
2666#define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
2667#define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
2668
2669 c = cmd_alloc(h);
2670 if (!c)
2671 return 0;
2672 (void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
2673 hpsa_scsi_do_simple_cmd_core(h, c);
2674 sense = c->err_info->SenseInfo;
2675 sense_key = sense[2];
2676 asc = sense[12];
2677 ascq = sense[13];
2678 cmd_status = c->err_info->CommandStatus;
2679 scsi_status = c->err_info->ScsiStatus;
2680 cmd_free(h, c);
2681 /* Is the volume 'not ready'? */
2682 if (cmd_status != CMD_TARGET_STATUS ||
2683 scsi_status != SAM_STAT_CHECK_CONDITION ||
2684 sense_key != NOT_READY ||
2685 asc != ASC_LUN_NOT_READY) {
2686 return 0;
2687 }
2688
2689 /* Determine the reason for not ready state */
2690 ldstat = hpsa_get_volume_status(h, scsi3addr);
2691
2692 /* Keep volume offline in certain cases: */
2693 switch (ldstat) {
2694 case HPSA_LV_UNDERGOING_ERASE:
2695 case HPSA_LV_UNDERGOING_RPI:
2696 case HPSA_LV_PENDING_RPI:
2697 case HPSA_LV_ENCRYPTED_NO_KEY:
2698 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
2699 case HPSA_LV_UNDERGOING_ENCRYPTION:
2700 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
2701 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
2702 return ldstat;
2703 case HPSA_VPD_LV_STATUS_UNSUPPORTED:
2704 /* If VPD status page isn't available,
2705 * use ASC/ASCQ to determine state
2706 */
2707 if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
2708 (ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
2709 return ldstat;
2710 break;
2711 default:
2712 break;
2713 }
2714 return 0;
2715}
2716
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002717static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002718 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
2719 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002720{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002721
2722#define OBDR_SIG_OFFSET 43
2723#define OBDR_TAPE_SIG "$DR-10"
2724#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
2725#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
2726
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002727 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002728 unsigned char *obdr_sig;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002729
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002730 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002731 if (!inq_buff)
2732 goto bail_out;
2733
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002734 /* Do an inquiry to the device to see what it is. */
2735 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2736 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2737 /* Inquiry failed (msg printed already) */
2738 dev_err(&h->pdev->dev,
2739 "hpsa_update_device_info: inquiry failed\n");
2740 goto bail_out;
2741 }
2742
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002743 this_device->devtype = (inq_buff[0] & 0x1f);
2744 memcpy(this_device->scsi3addr, scsi3addr, 8);
2745 memcpy(this_device->vendor, &inq_buff[8],
2746 sizeof(this_device->vendor));
2747 memcpy(this_device->model, &inq_buff[16],
2748 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002749 memset(this_device->device_id, 0,
2750 sizeof(this_device->device_id));
2751 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2752 sizeof(this_device->device_id));
2753
2754 if (this_device->devtype == TYPE_DISK &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002755 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05002756 int volume_offline;
2757
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002758 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002759 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2760 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05002761 volume_offline = hpsa_volume_offline(h, scsi3addr);
2762 if (volume_offline < 0 || volume_offline > 0xff)
2763 volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
2764 this_device->volume_offline = volume_offline & 0xff;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002765 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002766 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002767 this_device->offload_config = 0;
2768 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002769 this_device->offload_to_be_enabled = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06002770 this_device->volume_offline = 0;
Don Brace03383732015-01-23 16:43:30 -06002771 this_device->queue_depth = h->nr_cmds;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002772 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002773
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002774 if (is_OBDR_device) {
2775 /* See if this is a One-Button-Disaster-Recovery device
2776 * by looking for "$DR-10" at offset 43 in inquiry data.
2777 */
2778 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
2779 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
2780 strncmp(obdr_sig, OBDR_TAPE_SIG,
2781 OBDR_SIG_LEN) == 0);
2782 }
2783
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002784 kfree(inq_buff);
2785 return 0;
2786
2787bail_out:
2788 kfree(inq_buff);
2789 return 1;
2790}
2791
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002792static unsigned char *ext_target_model[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002793 "MSA2012",
2794 "MSA2024",
2795 "MSA2312",
2796 "MSA2324",
Stephen M. Cameronfda38512011-05-03 15:00:07 -05002797 "P2000 G3 SAS",
Stephen M. Camerone06c8e52013-09-23 13:33:56 -05002798 "MSA 2040 SAS",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002799 NULL,
2800};
2801
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002802static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002803{
2804 int i;
2805
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002806 for (i = 0; ext_target_model[i]; i++)
2807 if (strncmp(device->model, ext_target_model[i],
2808 strlen(ext_target_model[i])) == 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002809 return 1;
2810 return 0;
2811}
2812
2813/* Helper function to assign bus, target, lun mapping of devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002814 * Puts non-external target logical volumes on bus 0, external target logical
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002815 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2816 * Logical drive target and lun are assigned at this time, but
2817 * physical device lun and target assignment are deferred (assigned
2818 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2819 */
2820static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002821 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002822{
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002823 u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002824
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002825 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
2826 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002827 if (is_hba_lunid(lunaddrbytes))
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002828 hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002829 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002830 /* defer target, lun assignment for physical devices */
2831 hpsa_set_bus_target_lun(device, 2, -1, -1);
2832 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002833 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002834 /* It's a logical device */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002835 if (is_ext_target(h, device)) {
2836 /* external target way, put logicals on bus 1
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002837 * and match target/lun numbers box
2838 * reports, other smart array, bus 0, target 0, match lunid
2839 */
2840 hpsa_set_bus_target_lun(device,
2841 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
2842 return;
2843 }
2844 hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002845}
2846
2847/*
2848 * If there is no lun 0 on a target, linux won't find any devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002849 * For the external targets (arrays), we have to manually detect the enclosure
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002850 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2851 * it for some reason. *tmpdevice is the target we're adding,
2852 * this_device is a pointer into the current element of currentsd[]
2853 * that we're building up in update_scsi_devices(), below.
2854 * lunzerobits is a bitmap that tracks which targets already have a
2855 * lun 0 assigned.
2856 * Returns 1 if an enclosure was added, 0 if not.
2857 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002858static int add_ext_target_dev(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002859 struct hpsa_scsi_dev_t *tmpdevice,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002860 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002861 unsigned long lunzerobits[], int *n_ext_target_devs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002862{
2863 unsigned char scsi3addr[8];
2864
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002865 if (test_bit(tmpdevice->target, lunzerobits))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002866 return 0; /* There is already a lun 0 on this target. */
2867
2868 if (!is_logical_dev_addr_mode(lunaddrbytes))
2869 return 0; /* It's the logical targets that may lack lun 0. */
2870
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002871 if (!is_ext_target(h, tmpdevice))
2872 return 0; /* Only external target devices have this problem. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002873
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002874 if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002875 return 0;
2876
Stephen M. Cameronc4f8a292011-01-07 10:55:43 -06002877 memset(scsi3addr, 0, 8);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002878 scsi3addr[3] = tmpdevice->target;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002879 if (is_hba_lunid(scsi3addr))
2880 return 0; /* Don't add the RAID controller here. */
2881
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002882 if (is_scsi_rev_5(h))
2883 return 0; /* p1210m doesn't need to do this. */
2884
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002885 if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
Scott Teelaca4a522012-01-19 14:01:19 -06002886 dev_warn(&h->pdev->dev, "Maximum number of external "
2887 "target devices exceeded. Check your hardware "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002888 "configuration.");
2889 return 0;
2890 }
2891
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002892 if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002893 return 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002894 (*n_ext_target_devs)++;
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002895 hpsa_set_bus_target_lun(this_device,
2896 tmpdevice->bus, tmpdevice->target, 0);
2897 set_bit(tmpdevice->target, lunzerobits);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002898 return 1;
2899}
2900
2901/*
Scott Teel54b6e9e2014-02-18 13:56:45 -06002902 * Get address of physical disk used for an ioaccel2 mode command:
2903 * 1. Extract ioaccel2 handle from the command.
2904 * 2. Find a matching ioaccel2 handle from list of physical disks.
2905 * 3. Return:
2906 * 1 and set scsi3addr to address of matching physical
2907 * 0 if no matching physical disk was found.
2908 */
2909static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
2910 struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
2911{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002912 struct io_accel2_cmd *c2 =
2913 &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
2914 unsigned long flags;
Scott Teel54b6e9e2014-02-18 13:56:45 -06002915 int i;
Scott Teel54b6e9e2014-02-18 13:56:45 -06002916
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002917 spin_lock_irqsave(&h->devlock, flags);
2918 for (i = 0; i < h->ndevices; i++)
2919 if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
2920 memcpy(scsi3addr, h->dev[i]->scsi3addr,
2921 sizeof(h->dev[i]->scsi3addr));
2922 spin_unlock_irqrestore(&h->devlock, flags);
2923 return 1;
2924 }
2925 spin_unlock_irqrestore(&h->devlock, flags);
2926 return 0;
Scott Teel54b6e9e2014-02-18 13:56:45 -06002927}
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002928
Scott Teel54b6e9e2014-02-18 13:56:45 -06002929/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002930 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
2931 * logdev. The number of luns in physdev and logdev are returned in
2932 * *nphysicals and *nlogicals, respectively.
2933 * Returns 0 on success, -1 otherwise.
2934 */
2935static int hpsa_gather_lun_info(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06002936 struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002937 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002938{
Don Brace03383732015-01-23 16:43:30 -06002939 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002940 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2941 return -1;
2942 }
Don Brace03383732015-01-23 16:43:30 -06002943 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002944 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
Don Brace03383732015-01-23 16:43:30 -06002945 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
2946 HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002947 *nphysicals = HPSA_MAX_PHYS_LUN;
2948 }
Don Brace03383732015-01-23 16:43:30 -06002949 if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002950 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2951 return -1;
2952 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06002953 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002954 /* Reject Logicals in excess of our max capability. */
2955 if (*nlogicals > HPSA_MAX_LUN) {
2956 dev_warn(&h->pdev->dev,
2957 "maximum logical LUNs (%d) exceeded. "
2958 "%d LUNs ignored.\n", HPSA_MAX_LUN,
2959 *nlogicals - HPSA_MAX_LUN);
2960 *nlogicals = HPSA_MAX_LUN;
2961 }
2962 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2963 dev_warn(&h->pdev->dev,
2964 "maximum logical + physical LUNs (%d) exceeded. "
2965 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2966 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2967 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2968 }
2969 return 0;
2970}
2971
Don Brace42a91642014-11-14 17:26:27 -06002972static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
2973 int i, int nphysicals, int nlogicals,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002974 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002975 struct ReportLUNdata *logdev_list)
2976{
2977 /* Helper function, figure out where the LUN ID info is coming from
2978 * given index i, lists of physical and logical devices, where in
2979 * the list the raid controller is supposed to appear (first or last)
2980 */
2981
2982 int logicals_start = nphysicals + (raid_ctlr_position == 0);
2983 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2984
2985 if (i == raid_ctlr_position)
2986 return RAID_CTLR_LUNID;
2987
2988 if (i < logicals_start)
Stephen M. Camerond5b5d962014-05-29 10:53:34 -05002989 return &physdev_list->LUN[i -
2990 (raid_ctlr_position == 0)].lunid[0];
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002991
2992 if (i < last_device)
2993 return &logdev_list->LUN[i - nphysicals -
2994 (raid_ctlr_position == 0)][0];
2995 BUG();
2996 return NULL;
2997}
2998
Stephen M. Cameron316b2212014-02-21 16:25:15 -06002999static int hpsa_hba_mode_enabled(struct ctlr_info *h)
3000{
3001 int rc;
Joe Handzik6e8e8082014-05-15 15:44:42 -05003002 int hba_mode_enabled;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06003003 struct bmic_controller_parameters *ctlr_params;
3004 ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
3005 GFP_KERNEL);
3006
3007 if (!ctlr_params)
Joe Handzik96444fb2014-05-15 15:44:47 -05003008 return -ENOMEM;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06003009 rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
3010 sizeof(struct bmic_controller_parameters));
Joe Handzik96444fb2014-05-15 15:44:47 -05003011 if (rc) {
Stephen M. Cameron316b2212014-02-21 16:25:15 -06003012 kfree(ctlr_params);
Joe Handzik96444fb2014-05-15 15:44:47 -05003013 return rc;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06003014 }
Joe Handzik6e8e8082014-05-15 15:44:42 -05003015
3016 hba_mode_enabled =
3017 ((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
3018 kfree(ctlr_params);
3019 return hba_mode_enabled;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06003020}
3021
Don Brace03383732015-01-23 16:43:30 -06003022/* get physical drive ioaccel handle and queue depth */
3023static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
3024 struct hpsa_scsi_dev_t *dev,
3025 u8 *lunaddrbytes,
3026 struct bmic_identify_physical_device *id_phys)
3027{
3028 int rc;
3029 struct ext_report_lun_entry *rle =
3030 (struct ext_report_lun_entry *) lunaddrbytes;
3031
3032 dev->ioaccel_handle = rle->ioaccel_handle;
3033 memset(id_phys, 0, sizeof(*id_phys));
3034 rc = hpsa_bmic_id_physical_device(h, lunaddrbytes,
3035 GET_BMIC_DRIVE_NUMBER(lunaddrbytes), id_phys,
3036 sizeof(*id_phys));
3037 if (!rc)
3038 /* Reserve space for FW operations */
3039#define DRIVE_CMDS_RESERVED_FOR_FW 2
3040#define DRIVE_QUEUE_DEPTH 7
3041 dev->queue_depth =
3042 le16_to_cpu(id_phys->current_queue_depth_limit) -
3043 DRIVE_CMDS_RESERVED_FOR_FW;
3044 else
3045 dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
3046 atomic_set(&dev->ioaccel_cmds_out, 0);
3047}
3048
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003049static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
3050{
3051 /* the idea here is we could get notified
3052 * that some devices have changed, so we do a report
3053 * physical luns and report logical luns cmd, and adjust
3054 * our list of devices accordingly.
3055 *
3056 * The scsi3addr's of devices won't change so long as the
3057 * adapter is not reset. That means we can rescan and
3058 * tell which devices we already know about, vs. new
3059 * devices, vs. disappearing devices.
3060 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06003061 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003062 struct ReportLUNdata *logdev_list = NULL;
Don Brace03383732015-01-23 16:43:30 -06003063 struct bmic_identify_physical_device *id_phys = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003064 u32 nphysicals = 0;
3065 u32 nlogicals = 0;
3066 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003067 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
3068 int ncurrent = 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06003069 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003070 int raid_ctlr_position;
Joe Handzik2bbf5c72014-05-21 11:16:01 -05003071 int rescan_hba_mode;
Scott Teelaca4a522012-01-19 14:01:19 -06003072 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003073
Scott Teelcfe5bad2011-10-26 16:21:07 -05003074 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameron92084712014-11-14 17:26:54 -06003075 physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
3076 logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003077 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
Don Brace03383732015-01-23 16:43:30 -06003078 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003079
Don Brace03383732015-01-23 16:43:30 -06003080 if (!currentsd || !physdev_list || !logdev_list ||
3081 !tmpdevice || !id_phys) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003082 dev_err(&h->pdev->dev, "out of memory\n");
3083 goto out;
3084 }
3085 memset(lunzerobits, 0, sizeof(lunzerobits));
3086
Stephen M. Cameron316b2212014-02-21 16:25:15 -06003087 rescan_hba_mode = hpsa_hba_mode_enabled(h);
Joe Handzik96444fb2014-05-15 15:44:47 -05003088 if (rescan_hba_mode < 0)
3089 goto out;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06003090
3091 if (!h->hba_mode_enabled && rescan_hba_mode)
3092 dev_warn(&h->pdev->dev, "HBA mode enabled\n");
3093 else if (h->hba_mode_enabled && !rescan_hba_mode)
3094 dev_warn(&h->pdev->dev, "HBA mode disabled\n");
3095
3096 h->hba_mode_enabled = rescan_hba_mode;
3097
Don Brace03383732015-01-23 16:43:30 -06003098 if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
3099 logdev_list, &nlogicals))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003100 goto out;
3101
Scott Teelaca4a522012-01-19 14:01:19 -06003102 /* We might see up to the maximum number of logical and physical disks
3103 * plus external target devices, and a device for the local RAID
3104 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003105 */
Scott Teelaca4a522012-01-19 14:01:19 -06003106 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003107
3108 /* Allocate the per device structures */
3109 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05003110 if (i >= HPSA_MAX_DEVICES) {
3111 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
3112 " %d devices ignored.\n", HPSA_MAX_DEVICES,
3113 ndevs_to_allocate - HPSA_MAX_DEVICES);
3114 break;
3115 }
3116
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003117 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
3118 if (!currentsd[i]) {
3119 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
3120 __FILE__, __LINE__);
3121 goto out;
3122 }
3123 ndev_allocated++;
3124 }
3125
Stephen M. Cameron86452912014-05-29 10:53:49 -05003126 if (is_scsi_rev_5(h))
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003127 raid_ctlr_position = 0;
3128 else
3129 raid_ctlr_position = nphysicals + nlogicals;
3130
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003131 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06003132 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003133 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003134 u8 *lunaddrbytes, is_OBDR = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003135
3136 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003137 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3138 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003139
3140 /* skip masked non-disk devices */
3141 if (MASKED_DEVICE(lunaddrbytes))
3142 if (i < nphysicals + (raid_ctlr_position == 0) &&
3143 NON_DISK_PHYS_DEV(lunaddrbytes))
3144 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003145
3146 /* Get device type, vendor, model, device id */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003147 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
3148 &is_OBDR))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003149 continue; /* skip it if we can't talk to it. */
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003150 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003151 this_device = currentsd[ncurrent];
3152
3153 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -06003154 * For external target devices, we have to insert a LUN 0 which
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003155 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3156 * is nonetheless an enclosure device there. We have to
3157 * present that otherwise linux won't find anything if
3158 * there is no lun 0.
3159 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06003160 if (add_ext_target_dev(h, tmpdevice, this_device,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003161 lunaddrbytes, lunzerobits,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06003162 &n_ext_target_devs)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003163 ncurrent++;
3164 this_device = currentsd[ncurrent];
3165 }
3166
3167 *this_device = *tmpdevice;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003168
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003169 /* do not expose masked devices */
3170 if (MASKED_DEVICE(lunaddrbytes) &&
3171 i < nphysicals + (raid_ctlr_position == 0)) {
3172 if (h->hba_mode_enabled)
3173 dev_warn(&h->pdev->dev,
3174 "Masked physical device detected\n");
3175 this_device->expose_state = HPSA_DO_NOT_EXPOSE;
3176 } else {
3177 this_device->expose_state =
3178 HPSA_SG_ATTACH | HPSA_ULD_ATTACH;
3179 }
3180
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003181 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003182 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003183 /* We don't *really* support actual CD-ROM devices,
3184 * just "One Button Disaster Recovery" tape drive
3185 * which temporarily pretends to be a CD-ROM drive.
3186 * So we check that the device is really an OBDR tape
3187 * device by checking for "$DR-10" in bytes 43-48 of
3188 * the inquiry data.
3189 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003190 if (is_OBDR)
3191 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003192 break;
3193 case TYPE_DISK:
Stephen M. Cameron316b2212014-02-21 16:25:15 -06003194 if (h->hba_mode_enabled) {
3195 /* never use raid mapper in HBA mode */
3196 this_device->offload_enabled = 0;
3197 ncurrent++;
3198 break;
3199 } else if (h->acciopath_status) {
3200 if (i >= nphysicals) {
3201 ncurrent++;
3202 break;
3203 }
3204 } else {
3205 if (i < nphysicals)
3206 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003207 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003208 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003209 }
Don Brace03383732015-01-23 16:43:30 -06003210 if (h->transMethod & CFGTBL_Trans_io_accel1 ||
3211 h->transMethod & CFGTBL_Trans_io_accel2) {
3212 hpsa_get_ioaccel_drive_info(h, this_device,
3213 lunaddrbytes, id_phys);
3214 atomic_set(&this_device->ioaccel_cmds_out, 0);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003215 ncurrent++;
3216 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003217 break;
3218 case TYPE_TAPE:
3219 case TYPE_MEDIUM_CHANGER:
3220 ncurrent++;
3221 break;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003222 case TYPE_ENCLOSURE:
3223 if (h->hba_mode_enabled)
3224 ncurrent++;
3225 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003226 case TYPE_RAID:
3227 /* Only present the Smartarray HBA as a RAID controller.
3228 * If it's a RAID controller other than the HBA itself
3229 * (an external RAID controller, MSA500 or similar)
3230 * don't present it.
3231 */
3232 if (!is_hba_lunid(lunaddrbytes))
3233 break;
3234 ncurrent++;
3235 break;
3236 default:
3237 break;
3238 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05003239 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003240 break;
3241 }
3242 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3243out:
3244 kfree(tmpdevice);
3245 for (i = 0; i < ndev_allocated; i++)
3246 kfree(currentsd[i]);
3247 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003248 kfree(physdev_list);
3249 kfree(logdev_list);
Don Brace03383732015-01-23 16:43:30 -06003250 kfree(id_phys);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003251}
3252
Webb Scalesec5cbf02015-01-23 16:44:45 -06003253static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
3254 struct scatterlist *sg)
3255{
3256 u64 addr64 = (u64) sg_dma_address(sg);
3257 unsigned int len = sg_dma_len(sg);
3258
3259 desc->Addr = cpu_to_le64(addr64);
3260 desc->Len = cpu_to_le32(len);
3261 desc->Ext = 0;
3262}
3263
Webb Scalesc7ee65b2015-01-23 16:42:17 -06003264/*
3265 * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003266 * dma mapping and fills in the scatter gather entries of the
3267 * hpsa command, cp.
3268 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003269static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003270 struct CommandList *cp,
3271 struct scsi_cmnd *cmd)
3272{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003273 struct scatterlist *sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003274 int use_sg, i, sg_index, chained;
3275 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003276
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003277 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003278
3279 use_sg = scsi_dma_map(cmd);
3280 if (use_sg < 0)
3281 return use_sg;
3282
3283 if (!use_sg)
3284 goto sglist_finished;
3285
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003286 curr_sg = cp->SG;
3287 chained = 0;
3288 sg_index = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003289 scsi_for_each_sg(cmd, sg, use_sg, i) {
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003290 if (i == h->max_cmd_sg_entries - 1 &&
3291 use_sg > h->max_cmd_sg_entries) {
3292 chained = 1;
3293 curr_sg = h->cmd_sg_list[cp->cmdindex];
3294 sg_index = 0;
3295 }
Webb Scalesec5cbf02015-01-23 16:44:45 -06003296 hpsa_set_sg_descriptor(curr_sg, sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003297 curr_sg++;
3298 }
Webb Scalesec5cbf02015-01-23 16:44:45 -06003299
3300 /* Back the pointer up to the last entry and mark it as "last". */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06003301 (--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003302
3303 if (use_sg + chained > h->maxSG)
3304 h->maxSG = use_sg + chained;
3305
3306 if (chained) {
3307 cp->Header.SGList = h->max_cmd_sg_entries;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06003308 cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06003309 if (hpsa_map_sg_chain_block(h, cp)) {
3310 scsi_dma_unmap(cmd);
3311 return -1;
3312 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003313 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003314 }
3315
3316sglist_finished:
3317
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003318 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06003319 cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003320 return 0;
3321}
3322
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003323#define IO_ACCEL_INELIGIBLE (1)
3324static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3325{
3326 int is_write = 0;
3327 u32 block;
3328 u32 block_cnt;
3329
3330 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
3331 switch (cdb[0]) {
3332 case WRITE_6:
3333 case WRITE_12:
3334 is_write = 1;
3335 case READ_6:
3336 case READ_12:
3337 if (*cdb_len == 6) {
3338 block = (((u32) cdb[2]) << 8) | cdb[3];
3339 block_cnt = cdb[4];
3340 } else {
3341 BUG_ON(*cdb_len != 12);
3342 block = (((u32) cdb[2]) << 24) |
3343 (((u32) cdb[3]) << 16) |
3344 (((u32) cdb[4]) << 8) |
3345 cdb[5];
3346 block_cnt =
3347 (((u32) cdb[6]) << 24) |
3348 (((u32) cdb[7]) << 16) |
3349 (((u32) cdb[8]) << 8) |
3350 cdb[9];
3351 }
3352 if (block_cnt > 0xffff)
3353 return IO_ACCEL_INELIGIBLE;
3354
3355 cdb[0] = is_write ? WRITE_10 : READ_10;
3356 cdb[1] = 0;
3357 cdb[2] = (u8) (block >> 24);
3358 cdb[3] = (u8) (block >> 16);
3359 cdb[4] = (u8) (block >> 8);
3360 cdb[5] = (u8) (block);
3361 cdb[6] = 0;
3362 cdb[7] = (u8) (block_cnt >> 8);
3363 cdb[8] = (u8) (block_cnt);
3364 cdb[9] = 0;
3365 *cdb_len = 10;
3366 break;
3367 }
3368 return 0;
3369}
3370
Scott Teelc3497752014-02-18 13:56:34 -06003371static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003372 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06003373 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Matt Gatese1f7de02014-02-18 13:55:17 -06003374{
3375 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06003376 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3377 unsigned int len;
3378 unsigned int total_len = 0;
3379 struct scatterlist *sg;
3380 u64 addr64;
3381 int use_sg, i;
3382 struct SGDescriptor *curr_sg;
3383 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3384
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003385 /* TODO: implement chaining support */
Don Brace03383732015-01-23 16:43:30 -06003386 if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
3387 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003388 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06003389 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003390
Matt Gatese1f7de02014-02-18 13:55:17 -06003391 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3392
Don Brace03383732015-01-23 16:43:30 -06003393 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
3394 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003395 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06003396 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003397
Matt Gatese1f7de02014-02-18 13:55:17 -06003398 c->cmd_type = CMD_IOACCEL1;
3399
3400 /* Adjust the DMA address to point to the accelerated command buffer */
3401 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3402 (c->cmdindex * sizeof(*cp));
3403 BUG_ON(c->busaddr & 0x0000007F);
3404
3405 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06003406 if (use_sg < 0) {
3407 atomic_dec(&phys_disk->ioaccel_cmds_out);
Matt Gatese1f7de02014-02-18 13:55:17 -06003408 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06003409 }
Matt Gatese1f7de02014-02-18 13:55:17 -06003410
3411 if (use_sg) {
3412 curr_sg = cp->SG;
3413 scsi_for_each_sg(cmd, sg, use_sg, i) {
3414 addr64 = (u64) sg_dma_address(sg);
3415 len = sg_dma_len(sg);
3416 total_len += len;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06003417 curr_sg->Addr = cpu_to_le64(addr64);
3418 curr_sg->Len = cpu_to_le32(len);
3419 curr_sg->Ext = cpu_to_le32(0);
Matt Gatese1f7de02014-02-18 13:55:17 -06003420 curr_sg++;
3421 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06003422 (--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
Matt Gatese1f7de02014-02-18 13:55:17 -06003423
3424 switch (cmd->sc_data_direction) {
3425 case DMA_TO_DEVICE:
3426 control |= IOACCEL1_CONTROL_DATA_OUT;
3427 break;
3428 case DMA_FROM_DEVICE:
3429 control |= IOACCEL1_CONTROL_DATA_IN;
3430 break;
3431 case DMA_NONE:
3432 control |= IOACCEL1_CONTROL_NODATAXFER;
3433 break;
3434 default:
3435 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3436 cmd->sc_data_direction);
3437 BUG();
3438 break;
3439 }
3440 } else {
3441 control |= IOACCEL1_CONTROL_NODATAXFER;
3442 }
3443
Scott Teelc3497752014-02-18 13:56:34 -06003444 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06003445 /* Fill out the command structure to submit */
Don Brace2b08b3e2015-01-23 16:41:09 -06003446 cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
3447 cp->transfer_len = cpu_to_le32(total_len);
3448 cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
3449 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
3450 cp->control = cpu_to_le32(control);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003451 memcpy(cp->CDB, cdb, cdb_len);
3452 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06003453 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003454 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06003455 return 0;
3456}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003457
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003458/*
3459 * Queue a command directly to a device behind the controller using the
3460 * I/O accelerator path.
3461 */
3462static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3463 struct CommandList *c)
3464{
3465 struct scsi_cmnd *cmd = c->scsi_cmd;
3466 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3467
Don Brace03383732015-01-23 16:43:30 -06003468 c->phys_disk = dev;
3469
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003470 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06003471 cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003472}
3473
Scott Teeldd0e19f2014-02-18 13:57:31 -06003474/*
3475 * Set encryption parameters for the ioaccel2 request
3476 */
3477static void set_encrypt_ioaccel2(struct ctlr_info *h,
3478 struct CommandList *c, struct io_accel2_cmd *cp)
3479{
3480 struct scsi_cmnd *cmd = c->scsi_cmd;
3481 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3482 struct raid_map_data *map = &dev->raid_map;
3483 u64 first_block;
3484
Scott Teeldd0e19f2014-02-18 13:57:31 -06003485 /* Are we doing encryption on this device */
Don Brace2b08b3e2015-01-23 16:41:09 -06003486 if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
Scott Teeldd0e19f2014-02-18 13:57:31 -06003487 return;
3488 /* Set the data encryption key index. */
3489 cp->dekindex = map->dekindex;
3490
3491 /* Set the encryption enable flag, encoded into direction field. */
3492 cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3493
3494 /* Set encryption tweak values based on logical block address
3495 * If block size is 512, tweak value is LBA.
3496 * For other block sizes, tweak is (LBA * block size)/ 512)
3497 */
3498 switch (cmd->cmnd[0]) {
3499 /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3500 case WRITE_6:
3501 case READ_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06003502 first_block = get_unaligned_be16(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06003503 break;
3504 case WRITE_10:
3505 case READ_10:
Scott Teeldd0e19f2014-02-18 13:57:31 -06003506 /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3507 case WRITE_12:
3508 case READ_12:
Don Brace2b08b3e2015-01-23 16:41:09 -06003509 first_block = get_unaligned_be32(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06003510 break;
3511 case WRITE_16:
3512 case READ_16:
Don Brace2b08b3e2015-01-23 16:41:09 -06003513 first_block = get_unaligned_be64(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06003514 break;
3515 default:
3516 dev_err(&h->pdev->dev,
Don Brace2b08b3e2015-01-23 16:41:09 -06003517 "ERROR: %s: size (0x%x) not supported for encryption\n",
3518 __func__, cmd->cmnd[0]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06003519 BUG();
3520 break;
3521 }
Don Brace2b08b3e2015-01-23 16:41:09 -06003522
3523 if (le32_to_cpu(map->volume_blk_size) != 512)
3524 first_block = first_block *
3525 le32_to_cpu(map->volume_blk_size)/512;
3526
3527 cp->tweak_lower = cpu_to_le32(first_block);
3528 cp->tweak_upper = cpu_to_le32(first_block >> 32);
Scott Teeldd0e19f2014-02-18 13:57:31 -06003529}
3530
Scott Teelc3497752014-02-18 13:56:34 -06003531static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3532 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06003533 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06003534{
3535 struct scsi_cmnd *cmd = c->scsi_cmd;
3536 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3537 struct ioaccel2_sg_element *curr_sg;
3538 int use_sg, i;
3539 struct scatterlist *sg;
3540 u64 addr64;
3541 u32 len;
3542 u32 total_len = 0;
3543
Don Brace03383732015-01-23 16:43:30 -06003544 if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
3545 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06003546 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06003547 }
Scott Teelc3497752014-02-18 13:56:34 -06003548
Don Brace03383732015-01-23 16:43:30 -06003549 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
3550 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06003551 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06003552 }
3553
Scott Teelc3497752014-02-18 13:56:34 -06003554 c->cmd_type = CMD_IOACCEL2;
3555 /* Adjust the DMA address to point to the accelerated command buffer */
3556 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3557 (c->cmdindex * sizeof(*cp));
3558 BUG_ON(c->busaddr & 0x0000007F);
3559
3560 memset(cp, 0, sizeof(*cp));
3561 cp->IU_type = IOACCEL2_IU_TYPE;
3562
3563 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06003564 if (use_sg < 0) {
3565 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06003566 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06003567 }
Scott Teelc3497752014-02-18 13:56:34 -06003568
3569 if (use_sg) {
3570 BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
3571 curr_sg = cp->sg;
3572 scsi_for_each_sg(cmd, sg, use_sg, i) {
3573 addr64 = (u64) sg_dma_address(sg);
3574 len = sg_dma_len(sg);
3575 total_len += len;
3576 curr_sg->address = cpu_to_le64(addr64);
3577 curr_sg->length = cpu_to_le32(len);
3578 curr_sg->reserved[0] = 0;
3579 curr_sg->reserved[1] = 0;
3580 curr_sg->reserved[2] = 0;
3581 curr_sg->chain_indicator = 0;
3582 curr_sg++;
3583 }
3584
3585 switch (cmd->sc_data_direction) {
3586 case DMA_TO_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06003587 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3588 cp->direction |= IOACCEL2_DIR_DATA_OUT;
Scott Teelc3497752014-02-18 13:56:34 -06003589 break;
3590 case DMA_FROM_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06003591 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3592 cp->direction |= IOACCEL2_DIR_DATA_IN;
Scott Teelc3497752014-02-18 13:56:34 -06003593 break;
3594 case DMA_NONE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06003595 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3596 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06003597 break;
3598 default:
3599 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3600 cmd->sc_data_direction);
3601 BUG();
3602 break;
3603 }
3604 } else {
Scott Teeldd0e19f2014-02-18 13:57:31 -06003605 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3606 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06003607 }
Scott Teeldd0e19f2014-02-18 13:57:31 -06003608
3609 /* Set encryption parameters, if necessary */
3610 set_encrypt_ioaccel2(h, c, cp);
3611
Don Brace2b08b3e2015-01-23 16:41:09 -06003612 cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
Don Bracef2405db2015-01-23 16:43:09 -06003613 cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
Scott Teelc3497752014-02-18 13:56:34 -06003614 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
Scott Teelc3497752014-02-18 13:56:34 -06003615
3616 /* fill in sg elements */
3617 cp->sg_count = (u8) use_sg;
3618
3619 cp->data_len = cpu_to_le32(total_len);
3620 cp->err_ptr = cpu_to_le64(c->busaddr +
3621 offsetof(struct io_accel2_cmd, error_data));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06003622 cp->err_len = cpu_to_le32(sizeof(cp->error_data));
Scott Teelc3497752014-02-18 13:56:34 -06003623
3624 enqueue_cmd_and_start_io(h, c);
3625 return 0;
3626}
3627
3628/*
3629 * Queue a command to the correct I/O accelerator path.
3630 */
3631static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3632 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06003633 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06003634{
Don Brace03383732015-01-23 16:43:30 -06003635 /* Try to honor the device's queue depth */
3636 if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
3637 phys_disk->queue_depth) {
3638 atomic_dec(&phys_disk->ioaccel_cmds_out);
3639 return IO_ACCEL_INELIGIBLE;
3640 }
Scott Teelc3497752014-02-18 13:56:34 -06003641 if (h->transMethod & CFGTBL_Trans_io_accel1)
3642 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06003643 cdb, cdb_len, scsi3addr,
3644 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06003645 else
3646 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06003647 cdb, cdb_len, scsi3addr,
3648 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06003649}
3650
Scott Teel6b80b182014-02-18 13:56:55 -06003651static void raid_map_helper(struct raid_map_data *map,
3652 int offload_to_mirror, u32 *map_index, u32 *current_group)
3653{
3654 if (offload_to_mirror == 0) {
3655 /* use physical disk in the first mirrored group. */
Don Brace2b08b3e2015-01-23 16:41:09 -06003656 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06003657 return;
3658 }
3659 do {
3660 /* determine mirror group that *map_index indicates */
Don Brace2b08b3e2015-01-23 16:41:09 -06003661 *current_group = *map_index /
3662 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06003663 if (offload_to_mirror == *current_group)
3664 continue;
Don Brace2b08b3e2015-01-23 16:41:09 -06003665 if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
Scott Teel6b80b182014-02-18 13:56:55 -06003666 /* select map index from next group */
Don Brace2b08b3e2015-01-23 16:41:09 -06003667 *map_index += le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06003668 (*current_group)++;
3669 } else {
3670 /* select map index from first group */
Don Brace2b08b3e2015-01-23 16:41:09 -06003671 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06003672 *current_group = 0;
3673 }
3674 } while (offload_to_mirror != *current_group);
3675}
3676
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003677/*
3678 * Attempt to perform offload RAID mapping for a logical volume I/O.
3679 */
3680static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3681 struct CommandList *c)
3682{
3683 struct scsi_cmnd *cmd = c->scsi_cmd;
3684 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3685 struct raid_map_data *map = &dev->raid_map;
3686 struct raid_map_disk_data *dd = &map->data[0];
3687 int is_write = 0;
3688 u32 map_index;
3689 u64 first_block, last_block;
3690 u32 block_cnt;
3691 u32 blocks_per_row;
3692 u64 first_row, last_row;
3693 u32 first_row_offset, last_row_offset;
3694 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06003695 u64 r0_first_row, r0_last_row;
3696 u32 r5or6_blocks_per_row;
3697 u64 r5or6_first_row, r5or6_last_row;
3698 u32 r5or6_first_row_offset, r5or6_last_row_offset;
3699 u32 r5or6_first_column, r5or6_last_column;
3700 u32 total_disks_per_row;
3701 u32 stripesize;
3702 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003703 u32 map_row;
3704 u32 disk_handle;
3705 u64 disk_block;
3706 u32 disk_block_cnt;
3707 u8 cdb[16];
3708 u8 cdb_len;
Don Brace2b08b3e2015-01-23 16:41:09 -06003709 u16 strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003710#if BITS_PER_LONG == 32
3711 u64 tmpdiv;
3712#endif
Scott Teel6b80b182014-02-18 13:56:55 -06003713 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003714
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003715 /* check for valid opcode, get LBA and block count */
3716 switch (cmd->cmnd[0]) {
3717 case WRITE_6:
3718 is_write = 1;
3719 case READ_6:
3720 first_block =
3721 (((u64) cmd->cmnd[2]) << 8) |
3722 cmd->cmnd[3];
3723 block_cnt = cmd->cmnd[4];
Stephen M. Cameron3fa89a02014-07-03 10:18:14 -05003724 if (block_cnt == 0)
3725 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003726 break;
3727 case WRITE_10:
3728 is_write = 1;
3729 case READ_10:
3730 first_block =
3731 (((u64) cmd->cmnd[2]) << 24) |
3732 (((u64) cmd->cmnd[3]) << 16) |
3733 (((u64) cmd->cmnd[4]) << 8) |
3734 cmd->cmnd[5];
3735 block_cnt =
3736 (((u32) cmd->cmnd[7]) << 8) |
3737 cmd->cmnd[8];
3738 break;
3739 case WRITE_12:
3740 is_write = 1;
3741 case READ_12:
3742 first_block =
3743 (((u64) cmd->cmnd[2]) << 24) |
3744 (((u64) cmd->cmnd[3]) << 16) |
3745 (((u64) cmd->cmnd[4]) << 8) |
3746 cmd->cmnd[5];
3747 block_cnt =
3748 (((u32) cmd->cmnd[6]) << 24) |
3749 (((u32) cmd->cmnd[7]) << 16) |
3750 (((u32) cmd->cmnd[8]) << 8) |
3751 cmd->cmnd[9];
3752 break;
3753 case WRITE_16:
3754 is_write = 1;
3755 case READ_16:
3756 first_block =
3757 (((u64) cmd->cmnd[2]) << 56) |
3758 (((u64) cmd->cmnd[3]) << 48) |
3759 (((u64) cmd->cmnd[4]) << 40) |
3760 (((u64) cmd->cmnd[5]) << 32) |
3761 (((u64) cmd->cmnd[6]) << 24) |
3762 (((u64) cmd->cmnd[7]) << 16) |
3763 (((u64) cmd->cmnd[8]) << 8) |
3764 cmd->cmnd[9];
3765 block_cnt =
3766 (((u32) cmd->cmnd[10]) << 24) |
3767 (((u32) cmd->cmnd[11]) << 16) |
3768 (((u32) cmd->cmnd[12]) << 8) |
3769 cmd->cmnd[13];
3770 break;
3771 default:
3772 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3773 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003774 last_block = first_block + block_cnt - 1;
3775
3776 /* check for write to non-RAID-0 */
3777 if (is_write && dev->raid_level != 0)
3778 return IO_ACCEL_INELIGIBLE;
3779
3780 /* check for invalid block or wraparound */
Don Brace2b08b3e2015-01-23 16:41:09 -06003781 if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
3782 last_block < first_block)
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003783 return IO_ACCEL_INELIGIBLE;
3784
3785 /* calculate stripe information for the request */
Don Brace2b08b3e2015-01-23 16:41:09 -06003786 blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
3787 le16_to_cpu(map->strip_size);
3788 strip_size = le16_to_cpu(map->strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003789#if BITS_PER_LONG == 32
3790 tmpdiv = first_block;
3791 (void) do_div(tmpdiv, blocks_per_row);
3792 first_row = tmpdiv;
3793 tmpdiv = last_block;
3794 (void) do_div(tmpdiv, blocks_per_row);
3795 last_row = tmpdiv;
3796 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3797 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3798 tmpdiv = first_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06003799 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003800 first_column = tmpdiv;
3801 tmpdiv = last_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06003802 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003803 last_column = tmpdiv;
3804#else
3805 first_row = first_block / blocks_per_row;
3806 last_row = last_block / blocks_per_row;
3807 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3808 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
Don Brace2b08b3e2015-01-23 16:41:09 -06003809 first_column = first_row_offset / strip_size;
3810 last_column = last_row_offset / strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003811#endif
3812
3813 /* if this isn't a single row/column then give to the controller */
3814 if ((first_row != last_row) || (first_column != last_column))
3815 return IO_ACCEL_INELIGIBLE;
3816
3817 /* proceeding with driver mapping */
Don Brace2b08b3e2015-01-23 16:41:09 -06003818 total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
3819 le16_to_cpu(map->metadata_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003820 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06003821 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06003822 map_index = (map_row * total_disks_per_row) + first_column;
3823
3824 switch (dev->raid_level) {
3825 case HPSA_RAID_0:
3826 break; /* nothing special to do */
3827 case HPSA_RAID_1:
3828 /* Handles load balance across RAID 1 members.
3829 * (2-drive R1 and R10 with even # of drives.)
3830 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003831 */
Don Brace2b08b3e2015-01-23 16:41:09 -06003832 BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003833 if (dev->offload_to_mirror)
Don Brace2b08b3e2015-01-23 16:41:09 -06003834 map_index += le16_to_cpu(map->data_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003835 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06003836 break;
3837 case HPSA_RAID_ADM:
3838 /* Handles N-way mirrors (R1-ADM)
3839 * and R10 with # of drives divisible by 3.)
3840 */
Don Brace2b08b3e2015-01-23 16:41:09 -06003841 BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
Scott Teel6b80b182014-02-18 13:56:55 -06003842
3843 offload_to_mirror = dev->offload_to_mirror;
3844 raid_map_helper(map, offload_to_mirror,
3845 &map_index, &current_group);
3846 /* set mirror group to use next time */
3847 offload_to_mirror =
Don Brace2b08b3e2015-01-23 16:41:09 -06003848 (offload_to_mirror >=
3849 le16_to_cpu(map->layout_map_count) - 1)
Scott Teel6b80b182014-02-18 13:56:55 -06003850 ? 0 : offload_to_mirror + 1;
Scott Teel6b80b182014-02-18 13:56:55 -06003851 dev->offload_to_mirror = offload_to_mirror;
3852 /* Avoid direct use of dev->offload_to_mirror within this
3853 * function since multiple threads might simultaneously
3854 * increment it beyond the range of dev->layout_map_count -1.
3855 */
3856 break;
3857 case HPSA_RAID_5:
3858 case HPSA_RAID_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06003859 if (le16_to_cpu(map->layout_map_count) <= 1)
Scott Teel6b80b182014-02-18 13:56:55 -06003860 break;
3861
3862 /* Verify first and last block are in same RAID group */
3863 r5or6_blocks_per_row =
Don Brace2b08b3e2015-01-23 16:41:09 -06003864 le16_to_cpu(map->strip_size) *
3865 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06003866 BUG_ON(r5or6_blocks_per_row == 0);
Don Brace2b08b3e2015-01-23 16:41:09 -06003867 stripesize = r5or6_blocks_per_row *
3868 le16_to_cpu(map->layout_map_count);
Scott Teel6b80b182014-02-18 13:56:55 -06003869#if BITS_PER_LONG == 32
3870 tmpdiv = first_block;
3871 first_group = do_div(tmpdiv, stripesize);
3872 tmpdiv = first_group;
3873 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3874 first_group = tmpdiv;
3875 tmpdiv = last_block;
3876 last_group = do_div(tmpdiv, stripesize);
3877 tmpdiv = last_group;
3878 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3879 last_group = tmpdiv;
3880#else
3881 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
3882 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
Scott Teel6b80b182014-02-18 13:56:55 -06003883#endif
Stephen M. Cameron000ff7c2014-03-13 17:12:50 -05003884 if (first_group != last_group)
Scott Teel6b80b182014-02-18 13:56:55 -06003885 return IO_ACCEL_INELIGIBLE;
3886
3887 /* Verify request is in a single row of RAID 5/6 */
3888#if BITS_PER_LONG == 32
3889 tmpdiv = first_block;
3890 (void) do_div(tmpdiv, stripesize);
3891 first_row = r5or6_first_row = r0_first_row = tmpdiv;
3892 tmpdiv = last_block;
3893 (void) do_div(tmpdiv, stripesize);
3894 r5or6_last_row = r0_last_row = tmpdiv;
3895#else
3896 first_row = r5or6_first_row = r0_first_row =
3897 first_block / stripesize;
3898 r5or6_last_row = r0_last_row = last_block / stripesize;
3899#endif
3900 if (r5or6_first_row != r5or6_last_row)
3901 return IO_ACCEL_INELIGIBLE;
3902
3903
3904 /* Verify request is in a single column */
3905#if BITS_PER_LONG == 32
3906 tmpdiv = first_block;
3907 first_row_offset = do_div(tmpdiv, stripesize);
3908 tmpdiv = first_row_offset;
3909 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
3910 r5or6_first_row_offset = first_row_offset;
3911 tmpdiv = last_block;
3912 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
3913 tmpdiv = r5or6_last_row_offset;
3914 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
3915 tmpdiv = r5or6_first_row_offset;
3916 (void) do_div(tmpdiv, map->strip_size);
3917 first_column = r5or6_first_column = tmpdiv;
3918 tmpdiv = r5or6_last_row_offset;
3919 (void) do_div(tmpdiv, map->strip_size);
3920 r5or6_last_column = tmpdiv;
3921#else
3922 first_row_offset = r5or6_first_row_offset =
3923 (u32)((first_block % stripesize) %
3924 r5or6_blocks_per_row);
3925
3926 r5or6_last_row_offset =
3927 (u32)((last_block % stripesize) %
3928 r5or6_blocks_per_row);
3929
3930 first_column = r5or6_first_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06003931 r5or6_first_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06003932 r5or6_last_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06003933 r5or6_last_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06003934#endif
3935 if (r5or6_first_column != r5or6_last_column)
3936 return IO_ACCEL_INELIGIBLE;
3937
3938 /* Request is eligible */
3939 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06003940 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06003941
3942 map_index = (first_group *
Don Brace2b08b3e2015-01-23 16:41:09 -06003943 (le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
Scott Teel6b80b182014-02-18 13:56:55 -06003944 (map_row * total_disks_per_row) + first_column;
3945 break;
3946 default:
3947 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003948 }
Scott Teel6b80b182014-02-18 13:56:55 -06003949
Stephen Cameron07543e02015-01-23 16:44:14 -06003950 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
3951 return IO_ACCEL_INELIGIBLE;
3952
Don Brace03383732015-01-23 16:43:30 -06003953 c->phys_disk = dev->phys_disk[map_index];
3954
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003955 disk_handle = dd[map_index].ioaccel_handle;
Don Brace2b08b3e2015-01-23 16:41:09 -06003956 disk_block = le64_to_cpu(map->disk_starting_blk) +
3957 first_row * le16_to_cpu(map->strip_size) +
3958 (first_row_offset - first_column *
3959 le16_to_cpu(map->strip_size));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003960 disk_block_cnt = block_cnt;
3961
3962 /* handle differing logical/physical block sizes */
3963 if (map->phys_blk_shift) {
3964 disk_block <<= map->phys_blk_shift;
3965 disk_block_cnt <<= map->phys_blk_shift;
3966 }
3967 BUG_ON(disk_block_cnt > 0xffff);
3968
3969 /* build the new CDB for the physical disk I/O */
3970 if (disk_block > 0xffffffff) {
3971 cdb[0] = is_write ? WRITE_16 : READ_16;
3972 cdb[1] = 0;
3973 cdb[2] = (u8) (disk_block >> 56);
3974 cdb[3] = (u8) (disk_block >> 48);
3975 cdb[4] = (u8) (disk_block >> 40);
3976 cdb[5] = (u8) (disk_block >> 32);
3977 cdb[6] = (u8) (disk_block >> 24);
3978 cdb[7] = (u8) (disk_block >> 16);
3979 cdb[8] = (u8) (disk_block >> 8);
3980 cdb[9] = (u8) (disk_block);
3981 cdb[10] = (u8) (disk_block_cnt >> 24);
3982 cdb[11] = (u8) (disk_block_cnt >> 16);
3983 cdb[12] = (u8) (disk_block_cnt >> 8);
3984 cdb[13] = (u8) (disk_block_cnt);
3985 cdb[14] = 0;
3986 cdb[15] = 0;
3987 cdb_len = 16;
3988 } else {
3989 cdb[0] = is_write ? WRITE_10 : READ_10;
3990 cdb[1] = 0;
3991 cdb[2] = (u8) (disk_block >> 24);
3992 cdb[3] = (u8) (disk_block >> 16);
3993 cdb[4] = (u8) (disk_block >> 8);
3994 cdb[5] = (u8) (disk_block);
3995 cdb[6] = 0;
3996 cdb[7] = (u8) (disk_block_cnt >> 8);
3997 cdb[8] = (u8) (disk_block_cnt);
3998 cdb[9] = 0;
3999 cdb_len = 10;
4000 }
4001 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004002 dev->scsi3addr,
4003 dev->phys_disk[map_index]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004004}
4005
Stephen Cameron574f05d2015-01-23 16:43:20 -06004006/* Submit commands down the "normal" RAID stack path */
4007static int hpsa_ciss_submit(struct ctlr_info *h,
4008 struct CommandList *c, struct scsi_cmnd *cmd,
4009 unsigned char scsi3addr[])
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004010{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004011 cmd->host_scribble = (unsigned char *) c;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004012 c->cmd_type = CMD_SCSI;
4013 c->scsi_cmd = cmd;
4014 c->Header.ReplyQueue = 0; /* unused in simple mode */
4015 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Bracef2405db2015-01-23 16:43:09 -06004016 c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004017
4018 /* Fill in the request block... */
4019
4020 c->Request.Timeout = 0;
4021 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4022 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4023 c->Request.CDBLen = cmd->cmd_len;
4024 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004025 switch (cmd->sc_data_direction) {
4026 case DMA_TO_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06004027 c->Request.type_attr_dir =
4028 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004029 break;
4030 case DMA_FROM_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06004031 c->Request.type_attr_dir =
4032 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004033 break;
4034 case DMA_NONE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06004035 c->Request.type_attr_dir =
4036 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004037 break;
4038 case DMA_BIDIRECTIONAL:
4039 /* This can happen if a buggy application does a scsi passthru
4040 * and sets both inlen and outlen to non-zero. ( see
4041 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4042 */
4043
Stephen M. Camerona505b862014-11-14 17:27:04 -06004044 c->Request.type_attr_dir =
4045 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004046 /* This is technically wrong, and hpsa controllers should
4047 * reject it with CMD_INVALID, which is the most correct
4048 * response, but non-fibre backends appear to let it
4049 * slide by, and give the same results as if this field
4050 * were set correctly. Either way is acceptable for
4051 * our purposes here.
4052 */
4053
4054 break;
4055
4056 default:
4057 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4058 cmd->sc_data_direction);
4059 BUG();
4060 break;
4061 }
4062
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004063 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004064 cmd_free(h, c);
4065 return SCSI_MLQUEUE_HOST_BUSY;
4066 }
4067 enqueue_cmd_and_start_io(h, c);
4068 /* the cmd'll come back via intr handler in complete_scsi_command() */
4069 return 0;
4070}
4071
Don Brace080ef1c2015-01-23 16:43:25 -06004072static void hpsa_command_resubmit_worker(struct work_struct *work)
4073{
4074 struct scsi_cmnd *cmd;
4075 struct hpsa_scsi_dev_t *dev;
4076 struct CommandList *c =
4077 container_of(work, struct CommandList, work);
4078
4079 cmd = c->scsi_cmd;
4080 dev = cmd->device->hostdata;
4081 if (!dev) {
4082 cmd->result = DID_NO_CONNECT << 16;
4083 cmd->scsi_done(cmd);
4084 return;
4085 }
4086 if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
4087 /*
4088 * If we get here, it means dma mapping failed. Try
4089 * again via scsi mid layer, which will then get
4090 * SCSI_MLQUEUE_HOST_BUSY.
4091 */
4092 cmd->result = DID_IMM_RETRY << 16;
4093 cmd->scsi_done(cmd);
4094 }
4095}
4096
Stephen Cameron574f05d2015-01-23 16:43:20 -06004097/* Running in struct Scsi_Host->host_lock less mode */
4098static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
4099{
4100 struct ctlr_info *h;
4101 struct hpsa_scsi_dev_t *dev;
4102 unsigned char scsi3addr[8];
4103 struct CommandList *c;
4104 int rc = 0;
4105
4106 /* Get the ptr to our adapter structure out of cmd->host. */
4107 h = sdev_to_hba(cmd->device);
4108 dev = cmd->device->hostdata;
4109 if (!dev) {
4110 cmd->result = DID_NO_CONNECT << 16;
4111 cmd->scsi_done(cmd);
4112 return 0;
4113 }
4114 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
4115
4116 if (unlikely(lockup_detected(h))) {
4117 cmd->result = DID_ERROR << 16;
4118 cmd->scsi_done(cmd);
4119 return 0;
4120 }
4121 c = cmd_alloc(h);
4122 if (c == NULL) { /* trouble... */
4123 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
4124 return SCSI_MLQUEUE_HOST_BUSY;
4125 }
Stephen Cameron407863c2015-01-23 16:44:19 -06004126 if (unlikely(lockup_detected(h))) {
4127 cmd->result = DID_ERROR << 16;
4128 cmd_free(h, c);
4129 cmd->scsi_done(cmd);
4130 return 0;
4131 }
Stephen Cameron574f05d2015-01-23 16:43:20 -06004132
Stephen Cameron407863c2015-01-23 16:44:19 -06004133 /*
4134 * Call alternate submit routine for I/O accelerated commands.
Stephen Cameron574f05d2015-01-23 16:43:20 -06004135 * Retries always go down the normal I/O path.
4136 */
4137 if (likely(cmd->retries == 0 &&
4138 cmd->request->cmd_type == REQ_TYPE_FS &&
4139 h->acciopath_status)) {
4140
4141 cmd->host_scribble = (unsigned char *) c;
4142 c->cmd_type = CMD_SCSI;
4143 c->scsi_cmd = cmd;
4144
4145 if (dev->offload_enabled) {
4146 rc = hpsa_scsi_ioaccel_raid_map(h, c);
4147 if (rc == 0)
4148 return 0; /* Sent on ioaccel path */
4149 if (rc < 0) { /* scsi_dma_map failed. */
4150 cmd_free(h, c);
4151 return SCSI_MLQUEUE_HOST_BUSY;
4152 }
4153 } else if (dev->ioaccel_handle) {
4154 rc = hpsa_scsi_ioaccel_direct_map(h, c);
4155 if (rc == 0)
4156 return 0; /* Sent on direct map path */
4157 if (rc < 0) { /* scsi_dma_map failed. */
4158 cmd_free(h, c);
4159 return SCSI_MLQUEUE_HOST_BUSY;
4160 }
4161 }
4162 }
4163 return hpsa_ciss_submit(h, c, cmd, scsi3addr);
4164}
4165
Webb Scales8ebc9242015-01-23 16:44:50 -06004166static void hpsa_scan_complete(struct ctlr_info *h)
Stephen M. Cameron5f389362014-02-18 13:55:48 -06004167{
4168 unsigned long flags;
4169
Webb Scales8ebc9242015-01-23 16:44:50 -06004170 spin_lock_irqsave(&h->scan_lock, flags);
4171 h->scan_finished = 1;
4172 wake_up_all(&h->scan_wait_queue);
4173 spin_unlock_irqrestore(&h->scan_lock, flags);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06004174}
4175
Stephen M. Camerona08a84712010-02-04 08:43:16 -06004176static void hpsa_scan_start(struct Scsi_Host *sh)
4177{
4178 struct ctlr_info *h = shost_to_hba(sh);
4179 unsigned long flags;
4180
Webb Scales8ebc9242015-01-23 16:44:50 -06004181 /*
4182 * Don't let rescans be initiated on a controller known to be locked
4183 * up. If the controller locks up *during* a rescan, that thread is
4184 * probably hosed, but at least we can prevent new rescan threads from
4185 * piling up on a locked up controller.
4186 */
4187 if (unlikely(lockup_detected(h)))
4188 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06004189
Stephen M. Camerona08a84712010-02-04 08:43:16 -06004190 /* wait until any scan already in progress is finished. */
4191 while (1) {
4192 spin_lock_irqsave(&h->scan_lock, flags);
4193 if (h->scan_finished)
4194 break;
4195 spin_unlock_irqrestore(&h->scan_lock, flags);
4196 wait_event(h->scan_wait_queue, h->scan_finished);
4197 /* Note: We don't need to worry about a race between this
4198 * thread and driver unload because the midlayer will
4199 * have incremented the reference count, so unload won't
4200 * happen if we're in here.
4201 */
4202 }
4203 h->scan_finished = 0; /* mark scan as in progress */
4204 spin_unlock_irqrestore(&h->scan_lock, flags);
4205
Webb Scales8ebc9242015-01-23 16:44:50 -06004206 if (unlikely(lockup_detected(h)))
4207 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06004208
Stephen M. Camerona08a84712010-02-04 08:43:16 -06004209 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4210
Webb Scales8ebc9242015-01-23 16:44:50 -06004211 hpsa_scan_complete(h);
Stephen M. Camerona08a84712010-02-04 08:43:16 -06004212}
4213
Don Brace7c0a0222015-01-23 16:41:30 -06004214static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
4215{
Don Brace03383732015-01-23 16:43:30 -06004216 struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
4217
4218 if (!logical_drive)
4219 return -ENODEV;
Don Brace7c0a0222015-01-23 16:41:30 -06004220
4221 if (qdepth < 1)
4222 qdepth = 1;
Don Brace03383732015-01-23 16:43:30 -06004223 else if (qdepth > logical_drive->queue_depth)
4224 qdepth = logical_drive->queue_depth;
4225
4226 return scsi_change_queue_depth(sdev, qdepth);
Don Brace7c0a0222015-01-23 16:41:30 -06004227}
4228
Stephen M. Camerona08a84712010-02-04 08:43:16 -06004229static int hpsa_scan_finished(struct Scsi_Host *sh,
4230 unsigned long elapsed_time)
4231{
4232 struct ctlr_info *h = shost_to_hba(sh);
4233 unsigned long flags;
4234 int finished;
4235
4236 spin_lock_irqsave(&h->scan_lock, flags);
4237 finished = h->scan_finished;
4238 spin_unlock_irqrestore(&h->scan_lock, flags);
4239 return finished;
4240}
4241
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004242static void hpsa_unregister_scsi(struct ctlr_info *h)
4243{
4244 /* we are being forcibly unloaded, and may not refuse. */
4245 scsi_remove_host(h->scsi_host);
4246 scsi_host_put(h->scsi_host);
4247 h->scsi_host = NULL;
4248}
4249
4250static int hpsa_register_scsi(struct ctlr_info *h)
4251{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06004252 struct Scsi_Host *sh;
4253 int error;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004254
Stephen M. Cameronb7056902012-01-19 14:00:53 -06004255 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4256 if (sh == NULL)
4257 goto fail;
4258
4259 sh->io_port = 0;
4260 sh->n_io_port = 0;
4261 sh->this_id = -1;
4262 sh->max_channel = 3;
4263 sh->max_cmd_len = MAX_COMMAND_SIZE;
4264 sh->max_lun = HPSA_MAX_LUN;
4265 sh->max_id = HPSA_MAX_LUN;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004266 sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
Don Brace03383732015-01-23 16:43:30 -06004267 sh->cmd_per_lun = sh->can_queue;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06004268 sh->sg_tablesize = h->maxsgentries;
4269 h->scsi_host = sh;
4270 sh->hostdata[0] = (unsigned long) h;
4271 sh->irq = h->intr[h->intr_mode];
4272 sh->unique_id = sh->irq;
4273 error = scsi_add_host(sh, &h->pdev->dev);
4274 if (error)
4275 goto fail_host_put;
4276 scsi_scan_host(sh);
4277 return 0;
4278
4279 fail_host_put:
4280 dev_err(&h->pdev->dev, "%s: scsi_add_host"
4281 " failed for controller %d\n", __func__, h->ctlr);
4282 scsi_host_put(sh);
4283 return error;
4284 fail:
4285 dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4286 " failed for controller %d\n", __func__, h->ctlr);
4287 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004288}
4289
4290static int wait_for_device_to_become_ready(struct ctlr_info *h,
4291 unsigned char lunaddr[])
4292{
Tomas Henzl89193582014-02-21 16:25:05 -06004293 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004294 int count = 0;
4295 int waittime = 1; /* seconds */
4296 struct CommandList *c;
4297
Stephen Cameron45fcb862015-01-23 16:43:04 -06004298 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004299 if (!c) {
4300 dev_warn(&h->pdev->dev, "out of memory in "
4301 "wait_for_device_to_become_ready.\n");
4302 return IO_ERROR;
4303 }
4304
4305 /* Send test unit ready until device ready, or give up. */
4306 while (count < HPSA_TUR_RETRY_LIMIT) {
4307
4308 /* Wait for a bit. do this first, because if we send
4309 * the TUR right away, the reset will just abort it.
4310 */
4311 msleep(1000 * waittime);
4312 count++;
Tomas Henzl89193582014-02-21 16:25:05 -06004313 rc = 0; /* Device ready. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004314
4315 /* Increase wait time with each try, up to a point. */
4316 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4317 waittime = waittime * 2;
4318
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004319 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4320 (void) fill_cmd(c, TEST_UNIT_READY, h,
4321 NULL, 0, 0, lunaddr, TYPE_CMD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004322 hpsa_scsi_do_simple_cmd_core(h, c);
4323 /* no unmap needed here because no data xfer. */
4324
4325 if (c->err_info->CommandStatus == CMD_SUCCESS)
4326 break;
4327
4328 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4329 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4330 (c->err_info->SenseInfo[2] == NO_SENSE ||
4331 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4332 break;
4333
4334 dev_warn(&h->pdev->dev, "waiting %d secs "
4335 "for device to become ready.\n", waittime);
4336 rc = 1; /* device not ready. */
4337 }
4338
4339 if (rc)
4340 dev_warn(&h->pdev->dev, "giving up on device.\n");
4341 else
4342 dev_warn(&h->pdev->dev, "device is ready.\n");
4343
Stephen Cameron45fcb862015-01-23 16:43:04 -06004344 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004345 return rc;
4346}
4347
4348/* Need at least one of these error handlers to keep ../scsi/hosts.c from
4349 * complaining. Doing a host- or bus-reset can't do anything good here.
4350 */
4351static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4352{
4353 int rc;
4354 struct ctlr_info *h;
4355 struct hpsa_scsi_dev_t *dev;
4356
4357 /* find the controller to which the command to be aborted was sent */
4358 h = sdev_to_hba(scsicmd->device);
4359 if (h == NULL) /* paranoia */
4360 return FAILED;
Don Bracee3458932015-01-23 16:44:24 -06004361
4362 if (lockup_detected(h))
4363 return FAILED;
4364
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004365 dev = scsicmd->device->hostdata;
4366 if (!dev) {
4367 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4368 "device lookup failed.\n");
4369 return FAILED;
4370 }
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06004371 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
4372 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004373 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teelbf711ac2014-02-18 13:56:39 -06004374 rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004375 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
4376 return SUCCESS;
4377
4378 dev_warn(&h->pdev->dev, "resetting device failed.\n");
4379 return FAILED;
4380}
4381
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004382static void swizzle_abort_tag(u8 *tag)
4383{
4384 u8 original_tag[8];
4385
4386 memcpy(original_tag, tag, 8);
4387 tag[0] = original_tag[3];
4388 tag[1] = original_tag[2];
4389 tag[2] = original_tag[1];
4390 tag[3] = original_tag[0];
4391 tag[4] = original_tag[7];
4392 tag[5] = original_tag[6];
4393 tag[6] = original_tag[5];
4394 tag[7] = original_tag[4];
4395}
4396
Scott Teel17eb87d2014-02-18 13:55:28 -06004397static void hpsa_get_tag(struct ctlr_info *h,
Don Brace2b08b3e2015-01-23 16:41:09 -06004398 struct CommandList *c, __le32 *taglower, __le32 *tagupper)
Scott Teel17eb87d2014-02-18 13:55:28 -06004399{
Don Brace2b08b3e2015-01-23 16:41:09 -06004400 u64 tag;
Scott Teel17eb87d2014-02-18 13:55:28 -06004401 if (c->cmd_type == CMD_IOACCEL1) {
4402 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
4403 &h->ioaccel_cmd_pool[c->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06004404 tag = le64_to_cpu(cm1->tag);
4405 *tagupper = cpu_to_le32(tag >> 32);
4406 *taglower = cpu_to_le32(tag);
Scott Teel54b6e9e2014-02-18 13:56:45 -06004407 return;
Scott Teel17eb87d2014-02-18 13:55:28 -06004408 }
Scott Teel54b6e9e2014-02-18 13:56:45 -06004409 if (c->cmd_type == CMD_IOACCEL2) {
4410 struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
4411 &h->ioaccel2_cmd_pool[c->cmdindex];
Scott Teeldd0e19f2014-02-18 13:57:31 -06004412 /* upper tag not used in ioaccel2 mode */
4413 memset(tagupper, 0, sizeof(*tagupper));
4414 *taglower = cm2->Tag;
Scott Teel54b6e9e2014-02-18 13:56:45 -06004415 return;
4416 }
Don Brace2b08b3e2015-01-23 16:41:09 -06004417 tag = le64_to_cpu(c->Header.tag);
4418 *tagupper = cpu_to_le32(tag >> 32);
4419 *taglower = cpu_to_le32(tag);
Scott Teel17eb87d2014-02-18 13:55:28 -06004420}
4421
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004422static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004423 struct CommandList *abort, int swizzle)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004424{
4425 int rc = IO_OK;
4426 struct CommandList *c;
4427 struct ErrorInfo *ei;
Don Brace2b08b3e2015-01-23 16:41:09 -06004428 __le32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004429
Stephen Cameron45fcb862015-01-23 16:43:04 -06004430 c = cmd_alloc(h);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004431 if (c == NULL) { /* trouble... */
Stephen Cameron45fcb862015-01-23 16:43:04 -06004432 dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004433 return -ENOMEM;
4434 }
4435
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004436 /* fill_cmd can't fail here, no buffer to map */
4437 (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
4438 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004439 if (swizzle)
4440 swizzle_abort_tag(&c->Request.CDB[4]);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004441 hpsa_scsi_do_simple_cmd_core(h, c);
Scott Teel17eb87d2014-02-18 13:55:28 -06004442 hpsa_get_tag(h, abort, &taglower, &tagupper);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004443 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06004444 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004445 /* no unmap needed here because no data xfer. */
4446
4447 ei = c->err_info;
4448 switch (ei->CommandStatus) {
4449 case CMD_SUCCESS:
4450 break;
4451 case CMD_UNABORTABLE: /* Very common, don't make noise. */
4452 rc = -1;
4453 break;
4454 default:
4455 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06004456 __func__, tagupper, taglower);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06004457 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004458 rc = -1;
4459 break;
4460 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06004461 cmd_free(h, c);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004462 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4463 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004464 return rc;
4465}
4466
Scott Teel54b6e9e2014-02-18 13:56:45 -06004467/* ioaccel2 path firmware cannot handle abort task requests.
4468 * Change abort requests to physical target reset, and send to the
4469 * address of the physical disk used for the ioaccel 2 command.
4470 * Return 0 on success (IO_OK)
4471 * -1 on failure
4472 */
4473
4474static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
4475 unsigned char *scsi3addr, struct CommandList *abort)
4476{
4477 int rc = IO_OK;
4478 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
4479 struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
4480 unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
4481 unsigned char *psa = &phys_scsi3addr[0];
4482
4483 /* Get a pointer to the hpsa logical device. */
Stephen Cameron7fa30302015-01-23 16:44:30 -06004484 scmd = abort->scsi_cmd;
Scott Teel54b6e9e2014-02-18 13:56:45 -06004485 dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
4486 if (dev == NULL) {
4487 dev_warn(&h->pdev->dev,
4488 "Cannot abort: no device pointer for command.\n");
4489 return -1; /* not abortable */
4490 }
4491
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06004492 if (h->raid_offload_debug > 0)
4493 dev_info(&h->pdev->dev,
4494 "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",
4495 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
4496 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
4497 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
4498
Scott Teel54b6e9e2014-02-18 13:56:45 -06004499 if (!dev->offload_enabled) {
4500 dev_warn(&h->pdev->dev,
4501 "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
4502 return -1; /* not abortable */
4503 }
4504
4505 /* Incoming scsi3addr is logical addr. We need physical disk addr. */
4506 if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
4507 dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
4508 return -1; /* not abortable */
4509 }
4510
4511 /* send the reset */
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06004512 if (h->raid_offload_debug > 0)
4513 dev_info(&h->pdev->dev,
4514 "Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4515 psa[0], psa[1], psa[2], psa[3],
4516 psa[4], psa[5], psa[6], psa[7]);
Scott Teel54b6e9e2014-02-18 13:56:45 -06004517 rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
4518 if (rc != 0) {
4519 dev_warn(&h->pdev->dev,
4520 "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4521 psa[0], psa[1], psa[2], psa[3],
4522 psa[4], psa[5], psa[6], psa[7]);
4523 return rc; /* failed to reset */
4524 }
4525
4526 /* wait for device to recover */
4527 if (wait_for_device_to_become_ready(h, psa) != 0) {
4528 dev_warn(&h->pdev->dev,
4529 "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4530 psa[0], psa[1], psa[2], psa[3],
4531 psa[4], psa[5], psa[6], psa[7]);
4532 return -1; /* failed to recover */
4533 }
4534
4535 /* device recovered */
4536 dev_info(&h->pdev->dev,
4537 "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4538 psa[0], psa[1], psa[2], psa[3],
4539 psa[4], psa[5], psa[6], psa[7]);
4540
4541 return rc; /* success */
4542}
4543
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004544/* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to
4545 * tell which kind we're dealing with, so we send the abort both ways. There
4546 * shouldn't be any collisions between swizzled and unswizzled tags due to the
4547 * way we construct our tags but we check anyway in case the assumptions which
4548 * make this true someday become false.
4549 */
4550static int hpsa_send_abort_both_ways(struct ctlr_info *h,
4551 unsigned char *scsi3addr, struct CommandList *abort)
4552{
Scott Teel54b6e9e2014-02-18 13:56:45 -06004553 /* ioccelerator mode 2 commands should be aborted via the
4554 * accelerated path, since RAID path is unaware of these commands,
4555 * but underlying firmware can't handle abort TMF.
4556 * Change abort to physical device reset.
4557 */
4558 if (abort->cmd_type == CMD_IOACCEL2)
4559 return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
4560
Don Bracef2405db2015-01-23 16:43:09 -06004561 return hpsa_send_abort(h, scsi3addr, abort, 0) &&
4562 hpsa_send_abort(h, scsi3addr, abort, 1);
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004563}
4564
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004565/* Send an abort for the specified command.
4566 * If the device and controller support it,
4567 * send a task abort request.
4568 */
4569static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
4570{
4571
4572 int i, rc;
4573 struct ctlr_info *h;
4574 struct hpsa_scsi_dev_t *dev;
4575 struct CommandList *abort; /* pointer to command to be aborted */
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004576 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
4577 char msg[256]; /* For debug messaging. */
4578 int ml = 0;
Don Brace2b08b3e2015-01-23 16:41:09 -06004579 __le32 tagupper, taglower;
Webb Scales281a7fd2015-01-23 16:43:35 -06004580 int refcount;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004581
4582 /* Find the controller of the command to be aborted */
4583 h = sdev_to_hba(sc->device);
4584 if (WARN(h == NULL,
4585 "ABORT REQUEST FAILED, Controller lookup failed.\n"))
4586 return FAILED;
4587
Don Bracee3458932015-01-23 16:44:24 -06004588 if (lockup_detected(h))
4589 return FAILED;
4590
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004591 /* Check that controller supports some kind of task abort */
4592 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
4593 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
4594 return FAILED;
4595
4596 memset(msg, 0, sizeof(msg));
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004597 ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%llu ",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004598 h->scsi_host->host_no, sc->device->channel,
4599 sc->device->id, sc->device->lun);
4600
4601 /* Find the device of the command to be aborted */
4602 dev = sc->device->hostdata;
4603 if (!dev) {
4604 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
4605 msg);
4606 return FAILED;
4607 }
4608
4609 /* Get SCSI command to be aborted */
4610 abort = (struct CommandList *) sc->host_scribble;
4611 if (abort == NULL) {
Webb Scales281a7fd2015-01-23 16:43:35 -06004612 /* This can happen if the command already completed. */
4613 return SUCCESS;
4614 }
4615 refcount = atomic_inc_return(&abort->refcount);
4616 if (refcount == 1) { /* Command is done already. */
4617 cmd_free(h, abort);
4618 return SUCCESS;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004619 }
Scott Teel17eb87d2014-02-18 13:55:28 -06004620 hpsa_get_tag(h, abort, &taglower, &tagupper);
4621 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen Cameron7fa30302015-01-23 16:44:30 -06004622 as = abort->scsi_cmd;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004623 if (as != NULL)
4624 ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
4625 as->cmnd[0], as->serial_number);
4626 dev_dbg(&h->pdev->dev, "%s\n", msg);
4627 dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
4628 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004629 /*
4630 * Command is in flight, or possibly already completed
4631 * by the firmware (but not to the scsi mid layer) but we can't
4632 * distinguish which. Send the abort down.
4633 */
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004634 rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004635 if (rc != 0) {
4636 dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
4637 dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
4638 h->scsi_host->host_no,
4639 dev->bus, dev->target, dev->lun);
Webb Scales281a7fd2015-01-23 16:43:35 -06004640 cmd_free(h, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004641 return FAILED;
4642 }
4643 dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
4644
4645 /* If the abort(s) above completed and actually aborted the
4646 * command, then the command to be aborted should already be
4647 * completed. If not, wait around a bit more to see if they
4648 * manage to complete normally.
4649 */
4650#define ABORT_COMPLETE_WAIT_SECS 30
4651 for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
Webb Scales281a7fd2015-01-23 16:43:35 -06004652 refcount = atomic_read(&abort->refcount);
4653 if (refcount < 2) {
4654 cmd_free(h, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004655 return SUCCESS;
Webb Scales281a7fd2015-01-23 16:43:35 -06004656 } else {
4657 msleep(100);
4658 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004659 }
4660 dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
4661 msg, ABORT_COMPLETE_WAIT_SECS);
Webb Scales281a7fd2015-01-23 16:43:35 -06004662 cmd_free(h, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004663 return FAILED;
4664}
4665
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004666/*
4667 * For operations that cannot sleep, a command block is allocated at init,
4668 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4669 * which ones are free or in use. Lock must be held when calling this.
4670 * cmd_free() is the complement.
4671 */
Webb Scales281a7fd2015-01-23 16:43:35 -06004672
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004673static struct CommandList *cmd_alloc(struct ctlr_info *h)
4674{
4675 struct CommandList *c;
4676 int i;
4677 union u64bit temp64;
4678 dma_addr_t cmd_dma_handle, err_dma_handle;
Webb Scales281a7fd2015-01-23 16:43:35 -06004679 int refcount;
Robert Elliott33811022015-01-23 16:43:41 -06004680 unsigned long offset;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004681
Robert Elliott33811022015-01-23 16:43:41 -06004682 /*
4683 * There is some *extremely* small but non-zero chance that that
Stephen M. Cameron4c413122014-11-14 17:27:29 -06004684 * multiple threads could get in here, and one thread could
4685 * be scanning through the list of bits looking for a free
4686 * one, but the free ones are always behind him, and other
4687 * threads sneak in behind him and eat them before he can
4688 * get to them, so that while there is always a free one, a
4689 * very unlucky thread might be starved anyway, never able to
4690 * beat the other threads. In reality, this happens so
4691 * infrequently as to be indistinguishable from never.
4692 */
4693
Robert Elliott33811022015-01-23 16:43:41 -06004694 offset = h->last_allocation; /* benignly racy */
Webb Scales281a7fd2015-01-23 16:43:35 -06004695 for (;;) {
4696 i = find_next_zero_bit(h->cmd_pool_bits, h->nr_cmds, offset);
4697 if (unlikely(i == h->nr_cmds)) {
4698 offset = 0;
4699 continue;
4700 }
4701 c = h->cmd_pool + i;
4702 refcount = atomic_inc_return(&c->refcount);
4703 if (unlikely(refcount > 1)) {
4704 cmd_free(h, c); /* already in use */
4705 offset = (i + 1) % h->nr_cmds;
4706 continue;
4707 }
4708 set_bit(i & (BITS_PER_LONG - 1),
4709 h->cmd_pool_bits + (i / BITS_PER_LONG));
4710 break; /* it's ours now. */
4711 }
Robert Elliott33811022015-01-23 16:43:41 -06004712 h->last_allocation = i; /* benignly racy */
Stephen M. Cameron4c413122014-11-14 17:27:29 -06004713
Webb Scales281a7fd2015-01-23 16:43:35 -06004714 /* Zero out all of commandlist except the last field, refcount */
4715 memset(c, 0, offsetof(struct CommandList, refcount));
4716 c->Header.tag = cpu_to_le64((u64) (i << DIRECT_LOOKUP_SHIFT));
Don Bracef2405db2015-01-23 16:43:09 -06004717 cmd_dma_handle = h->cmd_pool_dhandle + i * sizeof(*c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004718 c->err_info = h->errinfo_pool + i;
4719 memset(c->err_info, 0, sizeof(*c->err_info));
4720 err_dma_handle = h->errinfo_pool_dhandle
4721 + i * sizeof(*c->err_info);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004722
4723 c->cmdindex = i;
4724
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004725 c->busaddr = (u32) cmd_dma_handle;
4726 temp64.val = (u64) err_dma_handle;
Webb Scales281a7fd2015-01-23 16:43:35 -06004727 c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
4728 c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004729
4730 c->h = h;
4731 return c;
4732}
4733
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004734static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4735{
Webb Scales281a7fd2015-01-23 16:43:35 -06004736 if (atomic_dec_and_test(&c->refcount)) {
4737 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004738
Webb Scales281a7fd2015-01-23 16:43:35 -06004739 i = c - h->cmd_pool;
4740 clear_bit(i & (BITS_PER_LONG - 1),
4741 h->cmd_pool_bits + (i / BITS_PER_LONG));
4742 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004743}
4744
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004745#ifdef CONFIG_COMPAT
4746
Don Brace42a91642014-11-14 17:26:27 -06004747static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
4748 void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004749{
4750 IOCTL32_Command_struct __user *arg32 =
4751 (IOCTL32_Command_struct __user *) arg;
4752 IOCTL_Command_struct arg64;
4753 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4754 int err;
4755 u32 cp;
4756
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004757 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004758 err = 0;
4759 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4760 sizeof(arg64.LUN_info));
4761 err |= copy_from_user(&arg64.Request, &arg32->Request,
4762 sizeof(arg64.Request));
4763 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4764 sizeof(arg64.error_info));
4765 err |= get_user(arg64.buf_size, &arg32->buf_size);
4766 err |= get_user(cp, &arg32->buf);
4767 arg64.buf = compat_ptr(cp);
4768 err |= copy_to_user(p, &arg64, sizeof(arg64));
4769
4770 if (err)
4771 return -EFAULT;
4772
Don Brace42a91642014-11-14 17:26:27 -06004773 err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004774 if (err)
4775 return err;
4776 err |= copy_in_user(&arg32->error_info, &p->error_info,
4777 sizeof(arg32->error_info));
4778 if (err)
4779 return -EFAULT;
4780 return err;
4781}
4782
4783static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
Don Brace42a91642014-11-14 17:26:27 -06004784 int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004785{
4786 BIG_IOCTL32_Command_struct __user *arg32 =
4787 (BIG_IOCTL32_Command_struct __user *) arg;
4788 BIG_IOCTL_Command_struct arg64;
4789 BIG_IOCTL_Command_struct __user *p =
4790 compat_alloc_user_space(sizeof(arg64));
4791 int err;
4792 u32 cp;
4793
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004794 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004795 err = 0;
4796 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4797 sizeof(arg64.LUN_info));
4798 err |= copy_from_user(&arg64.Request, &arg32->Request,
4799 sizeof(arg64.Request));
4800 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4801 sizeof(arg64.error_info));
4802 err |= get_user(arg64.buf_size, &arg32->buf_size);
4803 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
4804 err |= get_user(cp, &arg32->buf);
4805 arg64.buf = compat_ptr(cp);
4806 err |= copy_to_user(p, &arg64, sizeof(arg64));
4807
4808 if (err)
4809 return -EFAULT;
4810
Don Brace42a91642014-11-14 17:26:27 -06004811 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004812 if (err)
4813 return err;
4814 err |= copy_in_user(&arg32->error_info, &p->error_info,
4815 sizeof(arg32->error_info));
4816 if (err)
4817 return -EFAULT;
4818 return err;
4819}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06004820
Don Brace42a91642014-11-14 17:26:27 -06004821static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06004822{
4823 switch (cmd) {
4824 case CCISS_GETPCIINFO:
4825 case CCISS_GETINTINFO:
4826 case CCISS_SETINTINFO:
4827 case CCISS_GETNODENAME:
4828 case CCISS_SETNODENAME:
4829 case CCISS_GETHEARTBEAT:
4830 case CCISS_GETBUSTYPES:
4831 case CCISS_GETFIRMVER:
4832 case CCISS_GETDRIVVER:
4833 case CCISS_REVALIDVOLS:
4834 case CCISS_DEREGDISK:
4835 case CCISS_REGNEWDISK:
4836 case CCISS_REGNEWD:
4837 case CCISS_RESCANDISK:
4838 case CCISS_GETLUNINFO:
4839 return hpsa_ioctl(dev, cmd, arg);
4840
4841 case CCISS_PASSTHRU32:
4842 return hpsa_ioctl32_passthru(dev, cmd, arg);
4843 case CCISS_BIG_PASSTHRU32:
4844 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
4845
4846 default:
4847 return -ENOIOCTLCMD;
4848 }
4849}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004850#endif
4851
4852static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4853{
4854 struct hpsa_pci_info pciinfo;
4855
4856 if (!argp)
4857 return -EINVAL;
4858 pciinfo.domain = pci_domain_nr(h->pdev->bus);
4859 pciinfo.bus = h->pdev->bus->number;
4860 pciinfo.dev_fn = h->pdev->devfn;
4861 pciinfo.board_id = h->board_id;
4862 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4863 return -EFAULT;
4864 return 0;
4865}
4866
4867static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4868{
4869 DriverVer_type DriverVer;
4870 unsigned char vmaj, vmin, vsubmin;
4871 int rc;
4872
4873 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4874 &vmaj, &vmin, &vsubmin);
4875 if (rc != 3) {
4876 dev_info(&h->pdev->dev, "driver version string '%s' "
4877 "unrecognized.", HPSA_DRIVER_VERSION);
4878 vmaj = 0;
4879 vmin = 0;
4880 vsubmin = 0;
4881 }
4882 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4883 if (!argp)
4884 return -EINVAL;
4885 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4886 return -EFAULT;
4887 return 0;
4888}
4889
4890static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4891{
4892 IOCTL_Command_struct iocommand;
4893 struct CommandList *c;
4894 char *buff = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004895 u64 temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004896 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004897
4898 if (!argp)
4899 return -EINVAL;
4900 if (!capable(CAP_SYS_RAWIO))
4901 return -EPERM;
4902 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4903 return -EFAULT;
4904 if ((iocommand.buf_size < 1) &&
4905 (iocommand.Request.Type.Direction != XFER_NONE)) {
4906 return -EINVAL;
4907 }
4908 if (iocommand.buf_size > 0) {
4909 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4910 if (buff == NULL)
4911 return -EFAULT;
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05004912 if (iocommand.Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004913 /* Copy the data into the buffer we created */
4914 if (copy_from_user(buff, iocommand.buf,
4915 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004916 rc = -EFAULT;
4917 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004918 }
4919 } else {
4920 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004921 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004922 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06004923 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004924 if (c == NULL) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004925 rc = -ENOMEM;
4926 goto out_kfree;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004927 }
4928 /* Fill in the command type */
4929 c->cmd_type = CMD_IOCTL_PEND;
4930 /* Fill in Command Header */
4931 c->Header.ReplyQueue = 0; /* unused in simple mode */
4932 if (iocommand.buf_size > 0) { /* buffer to fill */
4933 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004934 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004935 } else { /* no buffers to fill */
4936 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004937 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004938 }
4939 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004940
4941 /* Fill in Request block */
4942 memcpy(&c->Request, &iocommand.Request,
4943 sizeof(c->Request));
4944
4945 /* Fill in the scatter gather information */
4946 if (iocommand.buf_size > 0) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004947 temp64 = pci_map_single(h->pdev, buff,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004948 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004949 if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
4950 c->SG[0].Addr = cpu_to_le64(0);
4951 c->SG[0].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004952 rc = -ENOMEM;
4953 goto out;
4954 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004955 c->SG[0].Addr = cpu_to_le64(temp64);
4956 c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
4957 c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004958 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004959 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05004960 if (iocommand.buf_size > 0)
4961 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004962 check_ioctl_unit_attention(h, c);
4963
4964 /* Copy the error information out */
4965 memcpy(&iocommand.error_info, c->err_info,
4966 sizeof(iocommand.error_info));
4967 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004968 rc = -EFAULT;
4969 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004970 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05004971 if ((iocommand.Request.Type.Direction & XFER_READ) &&
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004972 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004973 /* Copy the data out of the buffer we created */
4974 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004975 rc = -EFAULT;
4976 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004977 }
4978 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004979out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06004980 cmd_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004981out_kfree:
4982 kfree(buff);
4983 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004984}
4985
4986static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4987{
4988 BIG_IOCTL_Command_struct *ioc;
4989 struct CommandList *c;
4990 unsigned char **buff = NULL;
4991 int *buff_size = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004992 u64 temp64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004993 BYTE sg_used = 0;
4994 int status = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004995 u32 left;
4996 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004997 BYTE __user *data_ptr;
4998
4999 if (!argp)
5000 return -EINVAL;
5001 if (!capable(CAP_SYS_RAWIO))
5002 return -EPERM;
5003 ioc = (BIG_IOCTL_Command_struct *)
5004 kmalloc(sizeof(*ioc), GFP_KERNEL);
5005 if (!ioc) {
5006 status = -ENOMEM;
5007 goto cleanup1;
5008 }
5009 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
5010 status = -EFAULT;
5011 goto cleanup1;
5012 }
5013 if ((ioc->buf_size < 1) &&
5014 (ioc->Request.Type.Direction != XFER_NONE)) {
5015 status = -EINVAL;
5016 goto cleanup1;
5017 }
5018 /* Check kmalloc limits using all SGs */
5019 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
5020 status = -EINVAL;
5021 goto cleanup1;
5022 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005023 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005024 status = -EINVAL;
5025 goto cleanup1;
5026 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005027 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005028 if (!buff) {
5029 status = -ENOMEM;
5030 goto cleanup1;
5031 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005032 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005033 if (!buff_size) {
5034 status = -ENOMEM;
5035 goto cleanup1;
5036 }
5037 left = ioc->buf_size;
5038 data_ptr = ioc->buf;
5039 while (left) {
5040 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
5041 buff_size[sg_used] = sz;
5042 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
5043 if (buff[sg_used] == NULL) {
5044 status = -ENOMEM;
5045 goto cleanup1;
5046 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05005047 if (ioc->Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005048 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
Stephen M. Cameron0758f4f2014-07-03 10:18:03 -05005049 status = -EFAULT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005050 goto cleanup1;
5051 }
5052 } else
5053 memset(buff[sg_used], 0, sz);
5054 left -= sz;
5055 data_ptr += sz;
5056 sg_used++;
5057 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06005058 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005059 if (c == NULL) {
5060 status = -ENOMEM;
5061 goto cleanup1;
5062 }
5063 c->cmd_type = CMD_IOCTL_PEND;
5064 c->Header.ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06005065 c->Header.SGList = (u8) sg_used;
5066 c->Header.SGTotal = cpu_to_le16(sg_used);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005067 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005068 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
5069 if (ioc->buf_size > 0) {
5070 int i;
5071 for (i = 0; i < sg_used; i++) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06005072 temp64 = pci_map_single(h->pdev, buff[i],
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005073 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06005074 if (dma_mapping_error(&h->pdev->dev,
5075 (dma_addr_t) temp64)) {
5076 c->SG[i].Addr = cpu_to_le64(0);
5077 c->SG[i].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06005078 hpsa_pci_unmap(h->pdev, c, i,
5079 PCI_DMA_BIDIRECTIONAL);
5080 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05005081 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06005082 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06005083 c->SG[i].Addr = cpu_to_le64(temp64);
5084 c->SG[i].Len = cpu_to_le32(buff_size[i]);
5085 c->SG[i].Ext = cpu_to_le32(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005086 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06005087 c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005088 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005089 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06005090 if (sg_used)
5091 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005092 check_ioctl_unit_attention(h, c);
5093 /* Copy the error information out */
5094 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5095 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005096 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05005097 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005098 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05005099 if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
Don Brace2b08b3e2015-01-23 16:41:09 -06005100 int i;
5101
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005102 /* Copy the data out of the buffer we created */
5103 BYTE __user *ptr = ioc->buf;
5104 for (i = 0; i < sg_used; i++) {
5105 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005106 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05005107 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005108 }
5109 ptr += buff_size[i];
5110 }
5111 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005112 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05005113cleanup0:
Stephen Cameron45fcb862015-01-23 16:43:04 -06005114 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005115cleanup1:
5116 if (buff) {
Don Brace2b08b3e2015-01-23 16:41:09 -06005117 int i;
5118
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005119 for (i = 0; i < sg_used; i++)
5120 kfree(buff[i]);
5121 kfree(buff);
5122 }
5123 kfree(buff_size);
5124 kfree(ioc);
5125 return status;
5126}
5127
5128static void check_ioctl_unit_attention(struct ctlr_info *h,
5129 struct CommandList *c)
5130{
5131 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5132 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5133 (void) check_for_unit_attention(h, c);
5134}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05005135
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005136/*
5137 * ioctl
5138 */
Don Brace42a91642014-11-14 17:26:27 -06005139static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005140{
5141 struct ctlr_info *h;
5142 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05005143 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005144
5145 h = sdev_to_hba(dev);
5146
5147 switch (cmd) {
5148 case CCISS_DEREGDISK:
5149 case CCISS_REGNEWDISK:
5150 case CCISS_REGNEWD:
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005151 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005152 return 0;
5153 case CCISS_GETPCIINFO:
5154 return hpsa_getpciinfo_ioctl(h, argp);
5155 case CCISS_GETDRIVVER:
5156 return hpsa_getdrivver_ioctl(h, argp);
5157 case CCISS_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06005158 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05005159 return -EAGAIN;
5160 rc = hpsa_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06005161 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05005162 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005163 case CCISS_BIG_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06005164 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05005165 return -EAGAIN;
5166 rc = hpsa_big_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06005167 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05005168 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005169 default:
5170 return -ENOTTY;
5171 }
5172}
5173
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005174static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
5175 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005176{
5177 struct CommandList *c;
5178
5179 c = cmd_alloc(h);
5180 if (!c)
5181 return -ENOMEM;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005182 /* fill_cmd can't fail here, no data buffer to map */
5183 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005184 RAID_CTLR_LUNID, TYPE_MSG);
5185 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
5186 c->waiting = NULL;
5187 enqueue_cmd_and_start_io(h, c);
5188 /* Don't wait for completion, the reset won't complete. Don't free
5189 * the command either. This is the last command we will send before
5190 * re-initializing everything, so it doesn't matter and won't leak.
5191 */
5192 return 0;
5193}
5194
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005195static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06005196 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005197 int cmd_type)
5198{
5199 int pci_dir = XFER_NONE;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005200 struct CommandList *a; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005201
5202 c->cmd_type = CMD_IOCTL_PEND;
5203 c->Header.ReplyQueue = 0;
5204 if (buff != NULL && size > 0) {
5205 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06005206 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005207 } else {
5208 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06005209 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005210 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005211 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
5212
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005213 if (cmd_type == TYPE_CMD) {
5214 switch (cmd) {
5215 case HPSA_INQUIRY:
5216 /* are we trying to read a vital product page */
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06005217 if (page_code & VPD_PAGE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005218 c->Request.CDB[1] = 0x01;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06005219 c->Request.CDB[2] = (page_code & 0xff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005220 }
5221 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06005222 c->Request.type_attr_dir =
5223 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005224 c->Request.Timeout = 0;
5225 c->Request.CDB[0] = HPSA_INQUIRY;
5226 c->Request.CDB[4] = size & 0xFF;
5227 break;
5228 case HPSA_REPORT_LOG:
5229 case HPSA_REPORT_PHYS:
5230 /* Talking to controller so It's a physical command
5231 mode = 00 target = 0. Nothing to write.
5232 */
5233 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06005234 c->Request.type_attr_dir =
5235 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005236 c->Request.Timeout = 0;
5237 c->Request.CDB[0] = cmd;
5238 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5239 c->Request.CDB[7] = (size >> 16) & 0xFF;
5240 c->Request.CDB[8] = (size >> 8) & 0xFF;
5241 c->Request.CDB[9] = size & 0xFF;
5242 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005243 case HPSA_CACHE_FLUSH:
5244 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06005245 c->Request.type_attr_dir =
5246 TYPE_ATTR_DIR(cmd_type,
5247 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005248 c->Request.Timeout = 0;
5249 c->Request.CDB[0] = BMIC_WRITE;
5250 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05005251 c->Request.CDB[7] = (size >> 8) & 0xFF;
5252 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005253 break;
5254 case TEST_UNIT_READY:
5255 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06005256 c->Request.type_attr_dir =
5257 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005258 c->Request.Timeout = 0;
5259 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005260 case HPSA_GET_RAID_MAP:
5261 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06005262 c->Request.type_attr_dir =
5263 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005264 c->Request.Timeout = 0;
5265 c->Request.CDB[0] = HPSA_CISS_READ;
5266 c->Request.CDB[1] = cmd;
5267 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5268 c->Request.CDB[7] = (size >> 16) & 0xFF;
5269 c->Request.CDB[8] = (size >> 8) & 0xFF;
5270 c->Request.CDB[9] = size & 0xFF;
5271 break;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06005272 case BMIC_SENSE_CONTROLLER_PARAMETERS:
5273 c->Request.CDBLen = 10;
Stephen M. Camerona505b862014-11-14 17:27:04 -06005274 c->Request.type_attr_dir =
5275 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron316b2212014-02-21 16:25:15 -06005276 c->Request.Timeout = 0;
5277 c->Request.CDB[0] = BMIC_READ;
5278 c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
5279 c->Request.CDB[7] = (size >> 16) & 0xFF;
5280 c->Request.CDB[8] = (size >> 8) & 0xFF;
5281 break;
Don Brace03383732015-01-23 16:43:30 -06005282 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
5283 c->Request.CDBLen = 10;
5284 c->Request.type_attr_dir =
5285 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5286 c->Request.Timeout = 0;
5287 c->Request.CDB[0] = BMIC_READ;
5288 c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
5289 c->Request.CDB[7] = (size >> 16) & 0xFF;
5290 c->Request.CDB[8] = (size >> 8) & 0XFF;
5291 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005292 default:
5293 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5294 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005295 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005296 }
5297 } else if (cmd_type == TYPE_MSG) {
5298 switch (cmd) {
5299
5300 case HPSA_DEVICE_RESET_MSG:
5301 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06005302 c->Request.type_attr_dir =
5303 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005304 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005305 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
5306 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05005307 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005308 /* If bytes 4-7 are zero, it means reset the */
5309 /* LunID device */
5310 c->Request.CDB[4] = 0x00;
5311 c->Request.CDB[5] = 0x00;
5312 c->Request.CDB[6] = 0x00;
5313 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005314 break;
5315 case HPSA_ABORT_MSG:
5316 a = buff; /* point to command to be aborted */
Don Brace2b08b3e2015-01-23 16:41:09 -06005317 dev_dbg(&h->pdev->dev,
5318 "Abort Tag:0x%016llx request Tag:0x%016llx",
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06005319 a->Header.tag, c->Header.tag);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005320 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06005321 c->Request.type_attr_dir =
5322 TYPE_ATTR_DIR(cmd_type,
5323 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005324 c->Request.Timeout = 0; /* Don't time out */
5325 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
5326 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
5327 c->Request.CDB[2] = 0x00; /* reserved */
5328 c->Request.CDB[3] = 0x00; /* reserved */
5329 /* Tag to abort goes in CDB[4]-CDB[11] */
Don Brace2b08b3e2015-01-23 16:41:09 -06005330 memcpy(&c->Request.CDB[4], &a->Header.tag,
5331 sizeof(a->Header.tag));
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005332 c->Request.CDB[12] = 0x00; /* reserved */
5333 c->Request.CDB[13] = 0x00; /* reserved */
5334 c->Request.CDB[14] = 0x00; /* reserved */
5335 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005336 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005337 default:
5338 dev_warn(&h->pdev->dev, "unknown message type %d\n",
5339 cmd);
5340 BUG();
5341 }
5342 } else {
5343 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5344 BUG();
5345 }
5346
Stephen M. Camerona505b862014-11-14 17:27:04 -06005347 switch (GET_DIR(c->Request.type_attr_dir)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005348 case XFER_READ:
5349 pci_dir = PCI_DMA_FROMDEVICE;
5350 break;
5351 case XFER_WRITE:
5352 pci_dir = PCI_DMA_TODEVICE;
5353 break;
5354 case XFER_NONE:
5355 pci_dir = PCI_DMA_NONE;
5356 break;
5357 default:
5358 pci_dir = PCI_DMA_BIDIRECTIONAL;
5359 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005360 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5361 return -1;
5362 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005363}
5364
5365/*
5366 * Map (physical) PCI mem into (virtual) kernel space
5367 */
5368static void __iomem *remap_pci_mem(ulong base, ulong size)
5369{
5370 ulong page_base = ((ulong) base) & PAGE_MASK;
5371 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba34c2012-07-26 11:34:23 -05005372 void __iomem *page_remapped = ioremap_nocache(page_base,
5373 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005374
5375 return page_remapped ? (page_remapped + page_offs) : NULL;
5376}
5377
Matt Gates254f7962012-05-01 11:43:06 -05005378static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005379{
Matt Gates254f7962012-05-01 11:43:06 -05005380 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005381}
5382
Stephen M. Cameron900c5442010-02-04 08:42:35 -06005383static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005384{
5385 return h->access.intr_pending(h);
5386}
5387
5388static inline long interrupt_not_for_us(struct ctlr_info *h)
5389{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005390 return (h->access.intr_pending(h) == 0) ||
5391 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005392}
5393
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06005394static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
5395 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005396{
5397 if (unlikely(tag_index >= h->nr_cmds)) {
5398 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
5399 return 1;
5400 }
5401 return 0;
5402}
5403
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05005404static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005405{
Stephen M. Camerone85c5972012-05-01 11:43:42 -05005406 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06005407 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
5408 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05005409 complete_scsi_command(c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005410 else if (c->cmd_type == CMD_IOCTL_PEND)
5411 complete(c->waiting);
Stephen M. Camerona104c992010-02-04 08:42:24 -06005412}
5413
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005414
5415static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
Stephen M. Camerona104c992010-02-04 08:42:24 -06005416{
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005417#define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
5418#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005419 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005420 return tag & ~HPSA_SIMPLE_ERROR_BITS;
5421 return tag & ~HPSA_PERF_ERROR_BITS;
Stephen M. Camerona104c992010-02-04 08:42:24 -06005422}
5423
Don Brace303932f2010-02-04 08:42:40 -06005424/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005425static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06005426 u32 raw_tag)
5427{
5428 u32 tag_index;
5429 struct CommandList *c;
5430
Don Bracef2405db2015-01-23 16:43:09 -06005431 tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005432 if (!bad_tag(h, tag_index, raw_tag)) {
5433 c = h->cmd_pool + tag_index;
5434 finish_cmd(c);
5435 }
Don Brace303932f2010-02-04 08:42:40 -06005436}
5437
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005438/* Some controllers, like p400, will give us one interrupt
5439 * after a soft reset, even if we turned interrupts off.
5440 * Only need to check for this in the hpsa_xxx_discard_completions
5441 * functions.
5442 */
5443static int ignore_bogus_interrupt(struct ctlr_info *h)
5444{
5445 if (likely(!reset_devices))
5446 return 0;
5447
5448 if (likely(h->interrupts_enabled))
5449 return 0;
5450
5451 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
5452 "(known firmware bug.) Ignoring.\n");
5453
5454 return 1;
5455}
5456
Matt Gates254f7962012-05-01 11:43:06 -05005457/*
5458 * Convert &h->q[x] (passed to interrupt handlers) back to h.
5459 * Relies on (h-q[x] == x) being true for x such that
5460 * 0 <= x < MAX_REPLY_QUEUES.
5461 */
5462static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005463{
Matt Gates254f7962012-05-01 11:43:06 -05005464 return container_of((queue - *queue), struct ctlr_info, q[0]);
5465}
5466
5467static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
5468{
5469 struct ctlr_info *h = queue_to_hba(queue);
5470 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005471 u32 raw_tag;
5472
5473 if (ignore_bogus_interrupt(h))
5474 return IRQ_NONE;
5475
5476 if (interrupt_not_for_us(h))
5477 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005478 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005479 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005480 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005481 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005482 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005483 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005484 return IRQ_HANDLED;
5485}
5486
Matt Gates254f7962012-05-01 11:43:06 -05005487static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005488{
Matt Gates254f7962012-05-01 11:43:06 -05005489 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005490 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005491 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005492
5493 if (ignore_bogus_interrupt(h))
5494 return IRQ_NONE;
5495
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005496 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005497 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005498 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005499 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005500 return IRQ_HANDLED;
5501}
5502
Matt Gates254f7962012-05-01 11:43:06 -05005503static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005504{
Matt Gates254f7962012-05-01 11:43:06 -05005505 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06005506 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005507 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005508
5509 if (interrupt_not_for_us(h))
5510 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005511 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005512 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005513 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005514 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06005515 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005516 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005517 }
5518 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005519 return IRQ_HANDLED;
5520}
5521
Matt Gates254f7962012-05-01 11:43:06 -05005522static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005523{
Matt Gates254f7962012-05-01 11:43:06 -05005524 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005525 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005526 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005527
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005528 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005529 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06005530 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06005531 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005532 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005533 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005534 return IRQ_HANDLED;
5535}
5536
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005537/* Send a message CDB to the firmware. Careful, this only works
5538 * in simple mode, not performant mode due to the tag lookup.
5539 * We only ever use this immediately after a controller reset.
5540 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005541static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5542 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005543{
5544 struct Command {
5545 struct CommandListHeader CommandHeader;
5546 struct RequestBlock Request;
5547 struct ErrDescriptor ErrorDescriptor;
5548 };
5549 struct Command *cmd;
5550 static const size_t cmd_sz = sizeof(*cmd) +
5551 sizeof(cmd->ErrorDescriptor);
5552 dma_addr_t paddr64;
Don Brace2b08b3e2015-01-23 16:41:09 -06005553 __le32 paddr32;
5554 u32 tag;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005555 void __iomem *vaddr;
5556 int i, err;
5557
5558 vaddr = pci_ioremap_bar(pdev, 0);
5559 if (vaddr == NULL)
5560 return -ENOMEM;
5561
5562 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
5563 * CCISS commands, so they must be allocated from the lower 4GiB of
5564 * memory.
5565 */
5566 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5567 if (err) {
5568 iounmap(vaddr);
Robert Elliott1eaec8f2015-01-23 16:42:37 -06005569 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005570 }
5571
5572 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5573 if (cmd == NULL) {
5574 iounmap(vaddr);
5575 return -ENOMEM;
5576 }
5577
5578 /* This must fit, because of the 32-bit consistent DMA mask. Also,
5579 * although there's no guarantee, we assume that the address is at
5580 * least 4-byte aligned (most likely, it's page-aligned).
5581 */
Don Brace2b08b3e2015-01-23 16:41:09 -06005582 paddr32 = cpu_to_le32(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005583
5584 cmd->CommandHeader.ReplyQueue = 0;
5585 cmd->CommandHeader.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06005586 cmd->CommandHeader.SGTotal = cpu_to_le16(0);
Don Brace2b08b3e2015-01-23 16:41:09 -06005587 cmd->CommandHeader.tag = cpu_to_le64(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005588 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5589
5590 cmd->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06005591 cmd->Request.type_attr_dir =
5592 TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005593 cmd->Request.Timeout = 0; /* Don't time out */
5594 cmd->Request.CDB[0] = opcode;
5595 cmd->Request.CDB[1] = type;
5596 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06005597 cmd->ErrorDescriptor.Addr =
Don Brace2b08b3e2015-01-23 16:41:09 -06005598 cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06005599 cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005600
Don Brace2b08b3e2015-01-23 16:41:09 -06005601 writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005602
5603 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5604 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Don Brace2b08b3e2015-01-23 16:41:09 -06005605 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005606 break;
5607 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5608 }
5609
5610 iounmap(vaddr);
5611
5612 /* we leak the DMA buffer here ... no choice since the controller could
5613 * still complete the command.
5614 */
5615 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5616 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5617 opcode, type);
5618 return -ETIMEDOUT;
5619 }
5620
5621 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5622
5623 if (tag & HPSA_ERROR_BIT) {
5624 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5625 opcode, type);
5626 return -EIO;
5627 }
5628
5629 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5630 opcode, type);
5631 return 0;
5632}
5633
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005634#define hpsa_noop(p) hpsa_message(p, 3, 0)
5635
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005636static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Don Brace42a91642014-11-14 17:26:27 -06005637 void __iomem *vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005638{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005639
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005640 if (use_doorbell) {
5641 /* For everything after the P600, the PCI power state method
5642 * of resetting the controller doesn't work, so we have this
5643 * other way using the doorbell register.
5644 */
5645 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005646 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05005647
Justin Lindley00701a92014-05-29 10:52:47 -05005648 /* PMC hardware guys tell us we need a 10 second delay after
Stephen M. Cameron85009232013-09-23 13:33:36 -05005649 * doorbell reset and before any attempt to talk to the board
5650 * at all to ensure that this actually works and doesn't fall
5651 * over in some weird corner cases.
5652 */
Justin Lindley00701a92014-05-29 10:52:47 -05005653 msleep(10000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005654 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005655
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005656 /* Quoting from the Open CISS Specification: "The Power
5657 * Management Control/Status Register (CSR) controls the power
5658 * state of the device. The normal operating state is D0,
5659 * CSR=00h. The software off state is D3, CSR=03h. To reset
5660 * the controller, place the interface device in D3 then to D0,
5661 * this causes a secondary PCI reset which will reset the
5662 * controller." */
5663
Don Brace2662cab2015-01-23 16:41:25 -06005664 int rc = 0;
5665
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005666 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
Don Brace2662cab2015-01-23 16:41:25 -06005667
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005668 /* enter the D3hot power management state */
Don Brace2662cab2015-01-23 16:41:25 -06005669 rc = pci_set_power_state(pdev, PCI_D3hot);
5670 if (rc)
5671 return rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005672
5673 msleep(500);
5674
5675 /* enter the D0 power management state */
Don Brace2662cab2015-01-23 16:41:25 -06005676 rc = pci_set_power_state(pdev, PCI_D0);
5677 if (rc)
5678 return rc;
Mike Millerc4853ef2011-10-21 08:19:43 +02005679
5680 /*
5681 * The P600 requires a small delay when changing states.
5682 * Otherwise we may think the board did not reset and we bail.
5683 * This for kdump only and is particular to the P600.
5684 */
5685 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005686 }
5687 return 0;
5688}
5689
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005690static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005691{
5692 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005693 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005694}
5695
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005696static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005697{
5698 char *driver_version;
5699 int i, size = sizeof(cfgtable->driver_version);
5700
5701 driver_version = kmalloc(size, GFP_KERNEL);
5702 if (!driver_version)
5703 return -ENOMEM;
5704
5705 init_driver_version(driver_version, size);
5706 for (i = 0; i < size; i++)
5707 writeb(driver_version[i], &cfgtable->driver_version[i]);
5708 kfree(driver_version);
5709 return 0;
5710}
5711
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005712static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
5713 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005714{
5715 int i;
5716
5717 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5718 driver_ver[i] = readb(&cfgtable->driver_version[i]);
5719}
5720
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005721static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005722{
5723
5724 char *driver_ver, *old_driver_ver;
5725 int rc, size = sizeof(cfgtable->driver_version);
5726
5727 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5728 if (!old_driver_ver)
5729 return -ENOMEM;
5730 driver_ver = old_driver_ver + size;
5731
5732 /* After a reset, the 32 bytes of "driver version" in the cfgtable
5733 * should have been changed, otherwise we know the reset failed.
5734 */
5735 init_driver_version(old_driver_ver, size);
5736 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5737 rc = !memcmp(driver_ver, old_driver_ver, size);
5738 kfree(old_driver_ver);
5739 return rc;
5740}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005741/* This does a hard reset of the controller using PCI power management
5742 * states or the using the doorbell register.
5743 */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02005744static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005745{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005746 u64 cfg_offset;
5747 u32 cfg_base_addr;
5748 u64 cfg_base_addr_index;
5749 void __iomem *vaddr;
5750 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005751 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005752 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005753 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005754 u32 use_doorbell;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005755 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005756
5757 /* For controllers as old as the P600, this is very nearly
5758 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005759 *
5760 * pci_save_state(pci_dev);
5761 * pci_set_power_state(pci_dev, PCI_D3hot);
5762 * pci_set_power_state(pci_dev, PCI_D0);
5763 * pci_restore_state(pci_dev);
5764 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005765 * For controllers newer than the P600, the pci power state
5766 * method of resetting doesn't work so we have another way
5767 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005768 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005769
Robert Elliott60f923b2015-01-23 16:42:06 -06005770 if (!ctlr_is_resettable(board_id)) {
5771 dev_warn(&pdev->dev, "Controller not resettable\n");
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06005772 return -ENODEV;
5773 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05005774
5775 /* if controller is soft- but not hard resettable... */
5776 if (!ctlr_is_hard_resettable(board_id))
5777 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005778
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005779 /* Save the PCI command register */
5780 pci_read_config_word(pdev, 4, &command_register);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005781 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005782
5783 /* find the first memory BAR, so we can find the cfg table */
5784 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
5785 if (rc)
5786 return rc;
5787 vaddr = remap_pci_mem(paddr, 0x250);
5788 if (!vaddr)
5789 return -ENOMEM;
5790
5791 /* find cfgtable in order to check if reset via doorbell is supported */
5792 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
5793 &cfg_base_addr_index, &cfg_offset);
5794 if (rc)
5795 goto unmap_vaddr;
5796 cfgtable = remap_pci_mem(pci_resource_start(pdev,
5797 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
5798 if (!cfgtable) {
5799 rc = -ENOMEM;
5800 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005801 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005802 rc = write_driver_ver_to_cfgtable(cfgtable);
5803 if (rc)
Tomas Henzl03741d92015-01-23 16:41:14 -06005804 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005805
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005806 /* If reset via doorbell register is supported, use that.
5807 * There are two such methods. Favor the newest method.
5808 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005809 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005810 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
5811 if (use_doorbell) {
5812 use_doorbell = DOORBELL_CTLR_RESET2;
5813 } else {
5814 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
5815 if (use_doorbell) {
Stephen Cameron050f7142015-01-23 16:42:22 -06005816 dev_warn(&pdev->dev,
5817 "Soft reset not supported. Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005818 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005819 goto unmap_cfgtable;
5820 }
5821 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005822
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005823 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
5824 if (rc)
5825 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005826
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005827 pci_restore_state(pdev);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005828 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005829
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005830 /* Some devices (notably the HP Smart Array 5i Controller)
5831 need a little pause here */
5832 msleep(HPSA_POST_RESET_PAUSE_MSECS);
5833
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005834 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
5835 if (rc) {
5836 dev_warn(&pdev->dev,
Stephen Cameron050f7142015-01-23 16:42:22 -06005837 "Failed waiting for board to become ready after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005838 goto unmap_cfgtable;
5839 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005840
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005841 rc = controller_reset_failed(vaddr);
5842 if (rc < 0)
5843 goto unmap_cfgtable;
5844 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005845 dev_warn(&pdev->dev, "Unable to successfully reset "
5846 "controller. Will try soft reset.\n");
5847 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005848 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005849 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005850 }
5851
5852unmap_cfgtable:
5853 iounmap(cfgtable);
5854
5855unmap_vaddr:
5856 iounmap(vaddr);
5857 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005858}
5859
5860/*
5861 * We cannot read the structure directly, for portability we must use
5862 * the io functions.
5863 * This is for debug only.
5864 */
Don Brace42a91642014-11-14 17:26:27 -06005865static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005866{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005867#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005868 int i;
5869 char temp_name[17];
5870
5871 dev_info(dev, "Controller Configuration information\n");
5872 dev_info(dev, "------------------------------------\n");
5873 for (i = 0; i < 4; i++)
5874 temp_name[i] = readb(&(tb->Signature[i]));
5875 temp_name[4] = '\0';
5876 dev_info(dev, " Signature = %s\n", temp_name);
5877 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
5878 dev_info(dev, " Transport methods supported = 0x%x\n",
5879 readl(&(tb->TransportSupport)));
5880 dev_info(dev, " Transport methods active = 0x%x\n",
5881 readl(&(tb->TransportActive)));
5882 dev_info(dev, " Requested transport Method = 0x%x\n",
5883 readl(&(tb->HostWrite.TransportRequest)));
5884 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
5885 readl(&(tb->HostWrite.CoalIntDelay)));
5886 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
5887 readl(&(tb->HostWrite.CoalIntCount)));
Robert Elliott69d6e332015-01-23 16:41:56 -06005888 dev_info(dev, " Max outstanding commands = %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005889 readl(&(tb->CmdsOutMax)));
5890 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
5891 for (i = 0; i < 16; i++)
5892 temp_name[i] = readb(&(tb->ServerName[i]));
5893 temp_name[16] = '\0';
5894 dev_info(dev, " Server Name = %s\n", temp_name);
5895 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
5896 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005897#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005898}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005899
5900static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
5901{
5902 int i, offset, mem_type, bar_type;
5903
5904 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
5905 return 0;
5906 offset = 0;
5907 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
5908 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
5909 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
5910 offset += 4;
5911 else {
5912 mem_type = pci_resource_flags(pdev, i) &
5913 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
5914 switch (mem_type) {
5915 case PCI_BASE_ADDRESS_MEM_TYPE_32:
5916 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
5917 offset += 4; /* 32 bit */
5918 break;
5919 case PCI_BASE_ADDRESS_MEM_TYPE_64:
5920 offset += 8;
5921 break;
5922 default: /* reserved in PCI 2.2 */
5923 dev_warn(&pdev->dev,
5924 "base address is invalid\n");
5925 return -1;
5926 break;
5927 }
5928 }
5929 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
5930 return i + 1;
5931 }
5932 return -1;
5933}
5934
5935/* If MSI/MSI-X is supported by the kernel we will try to enable it on
Stephen Cameron050f7142015-01-23 16:42:22 -06005936 * controllers that are capable. If not, we use legacy INTx mode.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005937 */
5938
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005939static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005940{
5941#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05005942 int err, i;
5943 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
5944
5945 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
5946 hpsa_msix_entries[i].vector = 0;
5947 hpsa_msix_entries[i].entry = i;
5948 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005949
5950 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005951 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
5952 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005953 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005954 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06005955 dev_info(&h->pdev->dev, "MSI-X capable controller\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005956 h->msix_vector = MAX_REPLY_QUEUES;
Stephen M. Cameronf89439b2014-05-29 10:53:02 -05005957 if (h->msix_vector > num_online_cpus())
5958 h->msix_vector = num_online_cpus();
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02005959 err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
5960 1, h->msix_vector);
5961 if (err < 0) {
5962 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
5963 h->msix_vector = 0;
5964 goto single_msi_mode;
5965 } else if (err < h->msix_vector) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005966 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005967 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005968 }
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02005969 h->msix_vector = err;
5970 for (i = 0; i < h->msix_vector; i++)
5971 h->intr[i] = hpsa_msix_entries[i].vector;
5972 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005973 }
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02005974single_msi_mode:
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005975 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06005976 dev_info(&h->pdev->dev, "MSI capable controller\n");
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005977 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005978 h->msi_vector = 1;
5979 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005980 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005981 }
5982default_int_mode:
5983#endif /* CONFIG_PCI_MSI */
5984 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005985 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005986}
5987
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005988static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005989{
5990 int i;
5991 u32 subsystem_vendor_id, subsystem_device_id;
5992
5993 subsystem_vendor_id = pdev->subsystem_vendor;
5994 subsystem_device_id = pdev->subsystem_device;
5995 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
5996 subsystem_vendor_id;
5997
5998 for (i = 0; i < ARRAY_SIZE(products); i++)
5999 if (*board_id == products[i].board_id)
6000 return i;
6001
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05006002 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
6003 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
6004 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05006005 dev_warn(&pdev->dev, "unrecognized board ID: "
6006 "0x%08x, ignoring.\n", *board_id);
6007 return -ENODEV;
6008 }
6009 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6010}
6011
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006012static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
6013 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05006014{
6015 int i;
6016
6017 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05006018 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05006019 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05006020 *memory_bar = pci_resource_start(pdev, i);
6021 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05006022 *memory_bar);
6023 return 0;
6024 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05006025 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05006026 return -ENODEV;
6027}
6028
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006029static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
6030 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05006031{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06006032 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05006033 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06006034 if (wait_for_ready)
6035 iterations = HPSA_BOARD_READY_ITERATIONS;
6036 else
6037 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05006038
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06006039 for (i = 0; i < iterations; i++) {
6040 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6041 if (wait_for_ready) {
6042 if (scratchpad == HPSA_FIRMWARE_READY)
6043 return 0;
6044 } else {
6045 if (scratchpad != HPSA_FIRMWARE_READY)
6046 return 0;
6047 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05006048 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
6049 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06006050 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05006051 return -ENODEV;
6052}
6053
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006054static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
6055 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6056 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05006057{
6058 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6059 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6060 *cfg_base_addr &= (u32) 0x0000ffff;
6061 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6062 if (*cfg_base_addr_index == -1) {
6063 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6064 return -ENODEV;
6065 }
6066 return 0;
6067}
6068
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006069static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006070{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06006071 u64 cfg_offset;
6072 u32 cfg_base_addr;
6073 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06006074 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05006075 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05006076
Stephen M. Camerona51fd472010-06-16 13:51:30 -05006077 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6078 &cfg_base_addr_index, &cfg_offset);
6079 if (rc)
6080 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05006081 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05006082 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Robert Elliottcd3c81c2015-01-23 16:42:27 -06006083 if (!h->cfgtable) {
6084 dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
Stephen M. Cameron77c44952010-05-27 15:13:17 -05006085 return -ENOMEM;
Robert Elliottcd3c81c2015-01-23 16:42:27 -06006086 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006087 rc = write_driver_ver_to_cfgtable(h->cfgtable);
6088 if (rc)
6089 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05006090 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05006091 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05006092 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
6093 cfg_base_addr_index)+cfg_offset+trans_offset,
6094 sizeof(*h->transtable));
6095 if (!h->transtable)
6096 return -ENOMEM;
6097 return 0;
6098}
6099
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006100static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05006101{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05006102#define MIN_MAX_COMMANDS 16
6103 BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
6104
6105 h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06006106
6107 /* Limit commands in memory limited kdump scenario. */
6108 if (reset_devices && h->max_commands > 32)
6109 h->max_commands = 32;
6110
Stephen Cameron41ce4c32015-04-23 09:31:47 -05006111 if (h->max_commands < MIN_MAX_COMMANDS) {
6112 dev_warn(&h->pdev->dev,
6113 "Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
6114 h->max_commands,
6115 MIN_MAX_COMMANDS);
6116 h->max_commands = MIN_MAX_COMMANDS;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05006117 }
6118}
6119
Webb Scalesc7ee65b2015-01-23 16:42:17 -06006120/* If the controller reports that the total max sg entries is greater than 512,
6121 * then we know that chained SG blocks work. (Original smart arrays did not
6122 * support chained SG blocks and would return zero for max sg entries.)
6123 */
6124static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
6125{
6126 return h->maxsgentries > 512;
6127}
6128
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006129/* Interrogate the hardware for some limits:
6130 * max commands, max SG elements without chaining, and with chaining,
6131 * SG chain block size, etc.
6132 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006133static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006134{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05006135 hpsa_get_max_perf_mode_cmds(h);
Stephen Cameron45fcb862015-01-23 16:43:04 -06006136 h->nr_cmds = h->max_commands;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006137 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006138 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Webb Scalesc7ee65b2015-01-23 16:42:17 -06006139 if (hpsa_supports_chained_sg_blocks(h)) {
6140 /* Limit in-command s/g elements to 32 save dma'able memory. */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006141 h->max_cmd_sg_entries = 32;
Webb Scales1a63ea62014-11-14 17:26:43 -06006142 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006143 h->maxsgentries--; /* save one for chain pointer */
6144 } else {
Webb Scalesc7ee65b2015-01-23 16:42:17 -06006145 /*
6146 * Original smart arrays supported at most 31 s/g entries
6147 * embedded inline in the command (trying to use more
6148 * would lock up the controller)
6149 */
6150 h->max_cmd_sg_entries = 31;
Webb Scales1a63ea62014-11-14 17:26:43 -06006151 h->maxsgentries = 31; /* default to traditional values */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06006152 h->chainsize = 0;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006153 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006154
6155 /* Find out what task management functions are supported and cache */
6156 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06006157 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
6158 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
6159 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
6160 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006161}
6162
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05006163static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
6164{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09006165 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06006166 dev_err(&h->pdev->dev, "not a valid CISS config table\n");
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05006167 return false;
6168 }
6169 return true;
6170}
6171
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006172static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006173{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006174 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006175
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006176 driver_support = readl(&(h->cfgtable->driver_support));
Arnd Bergmann0b9e7b72014-06-26 15:44:52 +02006177 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
6178#ifdef CONFIG_X86
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006179 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006180#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06006181 driver_support |= ENABLE_UNIT_ATTN;
6182 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006183}
6184
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05006185/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
6186 * in a prefetch beyond physical memory.
6187 */
6188static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
6189{
6190 u32 dma_prefetch;
6191
6192 if (h->board_id != 0x3225103C)
6193 return;
6194 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
6195 dma_prefetch |= 0x8000;
6196 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
6197}
6198
Robert Elliottc706a792015-01-23 16:45:01 -06006199static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006200{
6201 int i;
6202 u32 doorbell_value;
6203 unsigned long flags;
6204 /* wait until the clear_event_notify bit 6 is cleared by controller. */
Robert Elliott007e7aa2015-01-23 16:44:56 -06006205 for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006206 spin_lock_irqsave(&h->lock, flags);
6207 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
6208 spin_unlock_irqrestore(&h->lock, flags);
6209 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
Robert Elliottc706a792015-01-23 16:45:01 -06006210 goto done;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006211 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06006212 msleep(CLEAR_EVENT_WAIT_INTERVAL);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006213 }
Robert Elliottc706a792015-01-23 16:45:01 -06006214 return -ENODEV;
6215done:
6216 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006217}
6218
Robert Elliottc706a792015-01-23 16:45:01 -06006219static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006220{
6221 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006222 u32 doorbell_value;
6223 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006224
6225 /* under certain very rare conditions, this can take awhile.
6226 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6227 * as we enter this code.)
6228 */
Robert Elliott007e7aa2015-01-23 16:44:56 -06006229 for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006230 spin_lock_irqsave(&h->lock, flags);
6231 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
6232 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06006233 if (!(doorbell_value & CFGTBL_ChangeReq))
Robert Elliottc706a792015-01-23 16:45:01 -06006234 goto done;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006235 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06006236 msleep(MODE_CHANGE_WAIT_INTERVAL);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006237 }
Robert Elliottc706a792015-01-23 16:45:01 -06006238 return -ENODEV;
6239done:
6240 return 0;
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006241}
6242
Robert Elliottc706a792015-01-23 16:45:01 -06006243/* return -ENODEV or other reason on error, 0 on success */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006244static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006245{
6246 u32 trans_support;
6247
6248 trans_support = readl(&(h->cfgtable->TransportSupport));
6249 if (!(trans_support & SIMPLE_MODE))
6250 return -ENOTSUPP;
6251
6252 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006253
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006254 /* Update the field, and then ring the doorbell */
6255 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006256 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006257 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06006258 if (hpsa_wait_for_mode_change_ack(h))
6259 goto error;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006260 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006261 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6262 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06006263 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006264 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006265error:
Stephen Cameron050f7142015-01-23 16:42:22 -06006266 dev_err(&h->pdev->dev, "failed to enter simple mode\n");
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006267 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006268}
6269
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006270static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05006271{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006272 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006273
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05006274 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6275 if (prod_index < 0)
Robert Elliott60f923b2015-01-23 16:42:06 -06006276 return prod_index;
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05006277 h->product_name = products[prod_index].product_name;
6278 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006279
Matthew Garrette5a44df2011-11-11 11:14:23 -05006280 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6281 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6282
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006283 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006284 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006285 dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006286 return err;
6287 }
6288
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006289 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006290 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006291 dev_err(&h->pdev->dev,
6292 "cannot obtain PCI resources, aborting\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006293 return err;
6294 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06006295
6296 pci_set_master(h->pdev);
6297
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05006298 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05006299 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05006300 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006301 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006302 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05006303 if (!h->vaddr) {
6304 err = -ENOMEM;
6305 goto err_out_free_res;
6306 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06006307 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05006308 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006309 goto err_out_free_res;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05006310 err = hpsa_find_cfgtables(h);
6311 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006312 goto err_out_free_res;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006313 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006314
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05006315 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006316 err = -ENODEV;
6317 goto err_out_free_res;
6318 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006319 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05006320 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006321 err = hpsa_enter_simple_mode(h);
6322 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006323 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006324 return 0;
6325
6326err_out_free_res:
Stephen M. Cameron204892e2010-05-27 15:13:22 -05006327 if (h->transtable)
6328 iounmap(h->transtable);
6329 if (h->cfgtable)
6330 iounmap(h->cfgtable);
6331 if (h->vaddr)
6332 iounmap(h->vaddr);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05006333 pci_disable_device(h->pdev);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006334 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006335 return err;
6336}
6337
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006338static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006339{
6340 int rc;
6341
6342#define HBA_INQUIRY_BYTE_COUNT 64
6343 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6344 if (!h->hba_inquiry_data)
6345 return;
6346 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
6347 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
6348 if (rc != 0) {
6349 kfree(h->hba_inquiry_data);
6350 h->hba_inquiry_data = NULL;
6351 }
6352}
6353
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02006354static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006355{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006356 int rc, i;
Tomas Henzl3b747292015-01-23 16:41:20 -06006357 void __iomem *vaddr;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006358
6359 if (!reset_devices)
6360 return 0;
6361
Tomas Henzl132aa222014-08-14 16:12:39 +02006362 /* kdump kernel is loading, we don't know in which state is
6363 * the pci interface. The dev->enable_cnt is equal zero
6364 * so we call enable+disable, wait a while and switch it on.
6365 */
6366 rc = pci_enable_device(pdev);
6367 if (rc) {
6368 dev_warn(&pdev->dev, "Failed to enable PCI device\n");
6369 return -ENODEV;
6370 }
6371 pci_disable_device(pdev);
6372 msleep(260); /* a randomly chosen number */
6373 rc = pci_enable_device(pdev);
6374 if (rc) {
6375 dev_warn(&pdev->dev, "failed to enable device.\n");
6376 return -ENODEV;
6377 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06006378
Tomas Henzl859c75a2014-09-12 14:44:15 +02006379 pci_set_master(pdev);
Robert Elliott4fa604e2014-11-14 17:27:24 -06006380
Tomas Henzl3b747292015-01-23 16:41:20 -06006381 vaddr = pci_ioremap_bar(pdev, 0);
6382 if (vaddr == NULL) {
6383 rc = -ENOMEM;
6384 goto out_disable;
6385 }
6386 writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
6387 iounmap(vaddr);
6388
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006389 /* Reset the controller with a PCI power-cycle or via doorbell */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02006390 rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006391
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006392 /* -ENOTSUPP here means we cannot reset the controller
6393 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05006394 * "performant mode". Or, it might be 640x, which can't reset
6395 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006396 */
Robert Elliottadf1b3a2015-01-23 16:42:01 -06006397 if (rc)
Tomas Henzl132aa222014-08-14 16:12:39 +02006398 goto out_disable;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006399
6400 /* Now try to get the controller to respond to a no-op */
Robert Elliott1ba66c92015-01-23 16:42:11 -06006401 dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006402 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
6403 if (hpsa_noop(pdev) == 0)
6404 break;
6405 else
6406 dev_warn(&pdev->dev, "no-op failed%s\n",
6407 (i < 11 ? "; re-trying" : ""));
6408 }
Tomas Henzl132aa222014-08-14 16:12:39 +02006409
6410out_disable:
6411
6412 pci_disable_device(pdev);
6413 return rc;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006414}
6415
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006416static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006417{
6418 h->cmd_pool_bits = kzalloc(
6419 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
6420 sizeof(unsigned long), GFP_KERNEL);
6421 h->cmd_pool = pci_alloc_consistent(h->pdev,
6422 h->nr_cmds * sizeof(*h->cmd_pool),
6423 &(h->cmd_pool_dhandle));
6424 h->errinfo_pool = pci_alloc_consistent(h->pdev,
6425 h->nr_cmds * sizeof(*h->errinfo_pool),
6426 &(h->errinfo_pool_dhandle));
6427 if ((h->cmd_pool_bits == NULL)
6428 || (h->cmd_pool == NULL)
6429 || (h->errinfo_pool == NULL)) {
6430 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
Robert Elliott2c143342015-01-23 16:42:48 -06006431 goto clean_up;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006432 }
6433 return 0;
Robert Elliott2c143342015-01-23 16:42:48 -06006434clean_up:
6435 hpsa_free_cmd_pool(h);
6436 return -ENOMEM;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006437}
6438
6439static void hpsa_free_cmd_pool(struct ctlr_info *h)
6440{
6441 kfree(h->cmd_pool_bits);
6442 if (h->cmd_pool)
6443 pci_free_consistent(h->pdev,
6444 h->nr_cmds * sizeof(struct CommandList),
6445 h->cmd_pool, h->cmd_pool_dhandle);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006446 if (h->ioaccel2_cmd_pool)
6447 pci_free_consistent(h->pdev,
6448 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6449 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006450 if (h->errinfo_pool)
6451 pci_free_consistent(h->pdev,
6452 h->nr_cmds * sizeof(struct ErrorInfo),
6453 h->errinfo_pool,
6454 h->errinfo_pool_dhandle);
Matt Gatese1f7de02014-02-18 13:55:17 -06006455 if (h->ioaccel_cmd_pool)
6456 pci_free_consistent(h->pdev,
6457 h->nr_cmds * sizeof(struct io_accel1_cmd),
6458 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006459}
6460
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05006461static void hpsa_irq_affinity_hints(struct ctlr_info *h)
6462{
Fabian Frederickec429952015-01-23 16:41:46 -06006463 int i, cpu;
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05006464
6465 cpu = cpumask_first(cpu_online_mask);
6466 for (i = 0; i < h->msix_vector; i++) {
Fabian Frederickec429952015-01-23 16:41:46 -06006467 irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05006468 cpu = cpumask_next(cpu, cpu_online_mask);
6469 }
6470}
6471
Robert Elliottec501a12015-01-23 16:41:40 -06006472/* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
6473static void hpsa_free_irqs(struct ctlr_info *h)
6474{
6475 int i;
6476
6477 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6478 /* Single reply queue, only one irq to free */
6479 i = h->intr_mode;
6480 irq_set_affinity_hint(h->intr[i], NULL);
6481 free_irq(h->intr[i], &h->q[i]);
6482 return;
6483 }
6484
6485 for (i = 0; i < h->msix_vector; i++) {
6486 irq_set_affinity_hint(h->intr[i], NULL);
6487 free_irq(h->intr[i], &h->q[i]);
6488 }
Robert Elliotta4e17fc2015-01-23 16:41:51 -06006489 for (; i < MAX_REPLY_QUEUES; i++)
6490 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06006491}
6492
Robert Elliott9ee61792015-01-23 16:42:32 -06006493/* returns 0 on success; cleans up and returns -Enn on error */
6494static int hpsa_request_irqs(struct ctlr_info *h,
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006495 irqreturn_t (*msixhandler)(int, void *),
6496 irqreturn_t (*intxhandler)(int, void *))
6497{
Matt Gates254f7962012-05-01 11:43:06 -05006498 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006499
Matt Gates254f7962012-05-01 11:43:06 -05006500 /*
6501 * initialize h->q[x] = x so that interrupt handlers know which
6502 * queue to process.
6503 */
6504 for (i = 0; i < MAX_REPLY_QUEUES; i++)
6505 h->q[i] = (u8) i;
6506
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006507 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05006508 /* If performant mode and MSI-X, use multiple reply queues */
Robert Elliotta4e17fc2015-01-23 16:41:51 -06006509 for (i = 0; i < h->msix_vector; i++) {
Matt Gates254f7962012-05-01 11:43:06 -05006510 rc = request_irq(h->intr[i], msixhandler,
6511 0, h->devname,
6512 &h->q[i]);
Robert Elliotta4e17fc2015-01-23 16:41:51 -06006513 if (rc) {
6514 int j;
6515
6516 dev_err(&h->pdev->dev,
6517 "failed to get irq %d for %s\n",
6518 h->intr[i], h->devname);
6519 for (j = 0; j < i; j++) {
6520 free_irq(h->intr[j], &h->q[j]);
6521 h->q[j] = 0;
6522 }
6523 for (; j < MAX_REPLY_QUEUES; j++)
6524 h->q[j] = 0;
6525 return rc;
6526 }
6527 }
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05006528 hpsa_irq_affinity_hints(h);
Matt Gates254f7962012-05-01 11:43:06 -05006529 } else {
6530 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006531 if (h->msix_vector > 0 || h->msi_vector) {
Matt Gates254f7962012-05-01 11:43:06 -05006532 rc = request_irq(h->intr[h->intr_mode],
6533 msixhandler, 0, h->devname,
6534 &h->q[h->intr_mode]);
6535 } else {
6536 rc = request_irq(h->intr[h->intr_mode],
6537 intxhandler, IRQF_SHARED, h->devname,
6538 &h->q[h->intr_mode]);
6539 }
6540 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006541 if (rc) {
6542 dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
6543 h->intr[h->intr_mode], h->devname);
6544 return -ENODEV;
6545 }
6546 return 0;
6547}
6548
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006549static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006550{
6551 if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
6552 HPSA_RESET_TYPE_CONTROLLER)) {
6553 dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
6554 return -EIO;
6555 }
6556
6557 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
6558 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
6559 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
6560 return -1;
6561 }
6562
6563 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
6564 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
6565 dev_warn(&h->pdev->dev, "Board failed to become ready "
6566 "after soft reset.\n");
6567 return -1;
6568 }
6569
6570 return 0;
6571}
6572
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006573static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006574{
Robert Elliottec501a12015-01-23 16:41:40 -06006575 hpsa_free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006576#ifdef CONFIG_PCI_MSI
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006577 if (h->msix_vector) {
6578 if (h->pdev->msix_enabled)
6579 pci_disable_msix(h->pdev);
6580 } else if (h->msi_vector) {
6581 if (h->pdev->msi_enabled)
6582 pci_disable_msi(h->pdev);
6583 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006584#endif /* CONFIG_PCI_MSI */
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006585}
6586
Stephen M. Cameron072b0512014-05-29 10:53:07 -05006587static void hpsa_free_reply_queues(struct ctlr_info *h)
6588{
6589 int i;
6590
6591 for (i = 0; i < h->nreply_queues; i++) {
6592 if (!h->reply_queue[i].head)
6593 continue;
6594 pci_free_consistent(h->pdev, h->reply_queue_size,
6595 h->reply_queue[i].head, h->reply_queue[i].busaddr);
6596 h->reply_queue[i].head = NULL;
6597 h->reply_queue[i].busaddr = 0;
6598 }
6599}
6600
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006601static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
6602{
6603 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006604 hpsa_free_sg_chain_blocks(h);
6605 hpsa_free_cmd_pool(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06006606 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006607 kfree(h->blockFetchTable);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05006608 hpsa_free_reply_queues(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006609 if (h->vaddr)
6610 iounmap(h->vaddr);
6611 if (h->transtable)
6612 iounmap(h->transtable);
6613 if (h->cfgtable)
6614 iounmap(h->cfgtable);
Tomas Henzl132aa222014-08-14 16:12:39 +02006615 pci_disable_device(h->pdev);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006616 pci_release_regions(h->pdev);
6617 kfree(h);
6618}
6619
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006620/* Called when controller lockup detected. */
Don Bracef2405db2015-01-23 16:43:09 -06006621static void fail_all_outstanding_cmds(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006622{
Webb Scales281a7fd2015-01-23 16:43:35 -06006623 int i, refcount;
6624 struct CommandList *c;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006625
Don Brace080ef1c2015-01-23 16:43:25 -06006626 flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
Don Bracef2405db2015-01-23 16:43:09 -06006627 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06006628 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06006629 refcount = atomic_inc_return(&c->refcount);
6630 if (refcount > 1) {
6631 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
6632 finish_cmd(c);
6633 }
6634 cmd_free(h, c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006635 }
6636}
6637
Stephen M. Cameron094963d2014-05-29 10:53:18 -05006638static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
6639{
Rusty Russellc8ed0012015-03-05 10:49:19 +10306640 int cpu;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05006641
Rusty Russellc8ed0012015-03-05 10:49:19 +10306642 for_each_online_cpu(cpu) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05006643 u32 *lockup_detected;
6644 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
6645 *lockup_detected = value;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05006646 }
6647 wmb(); /* be sure the per-cpu variables are out to memory */
6648}
6649
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006650static void controller_lockup_detected(struct ctlr_info *h)
6651{
6652 unsigned long flags;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05006653 u32 lockup_detected;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006654
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006655 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6656 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05006657 lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6658 if (!lockup_detected) {
6659 /* no heartbeat, but controller gave us a zero. */
6660 dev_warn(&h->pdev->dev,
6661 "lockup detected but scratchpad register is zero\n");
6662 lockup_detected = 0xffffffff;
6663 }
6664 set_lockup_detected_for_all_cpus(h, lockup_detected);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006665 spin_unlock_irqrestore(&h->lock, flags);
6666 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
Stephen M. Cameron094963d2014-05-29 10:53:18 -05006667 lockup_detected);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006668 pci_disable_device(h->pdev);
Don Bracef2405db2015-01-23 16:43:09 -06006669 fail_all_outstanding_cmds(h);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006670}
6671
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006672static void detect_controller_lockup(struct ctlr_info *h)
6673{
6674 u64 now;
6675 u32 heartbeat;
6676 unsigned long flags;
6677
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006678 now = get_jiffies_64();
6679 /* If we've received an interrupt recently, we're ok. */
6680 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006681 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006682 return;
6683
6684 /*
6685 * If we've already checked the heartbeat recently, we're ok.
6686 * This could happen if someone sends us a signal. We
6687 * otherwise don't care about signals in this thread.
6688 */
6689 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006690 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006691 return;
6692
6693 /* If heartbeat has not changed since we last looked, we're not ok. */
6694 spin_lock_irqsave(&h->lock, flags);
6695 heartbeat = readl(&h->cfgtable->HeartBeat);
6696 spin_unlock_irqrestore(&h->lock, flags);
6697 if (h->last_heartbeat == heartbeat) {
6698 controller_lockup_detected(h);
6699 return;
6700 }
6701
6702 /* We're ok. */
6703 h->last_heartbeat = heartbeat;
6704 h->last_heartbeat_timestamp = now;
6705}
6706
Stephen M. Cameron98465902014-02-21 16:25:00 -06006707static void hpsa_ack_ctlr_events(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006708{
6709 int i;
6710 char *event_type;
6711
Stephen Camerone4aa3e62015-01-23 16:44:07 -06006712 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
6713 return;
6714
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006715 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06006716 if ((h->transMethod & (CFGTBL_Trans_io_accel1
6717 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006718 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
6719 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
6720
6721 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
6722 event_type = "state change";
6723 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
6724 event_type = "configuration change";
6725 /* Stop sending new RAID offload reqs via the IO accelerator */
6726 scsi_block_requests(h->scsi_host);
6727 for (i = 0; i < h->ndevices; i++)
6728 h->dev[i]->offload_enabled = 0;
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06006729 hpsa_drain_accel_commands(h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006730 /* Set 'accelerator path config change' bit */
6731 dev_warn(&h->pdev->dev,
6732 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
6733 h->events, event_type);
6734 writel(h->events, &(h->cfgtable->clear_event_notify));
6735 /* Set the "clear event notify field update" bit 6 */
6736 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6737 /* Wait until ctlr clears 'clear event notify field', bit 6 */
6738 hpsa_wait_for_clear_event_notify_ack(h);
6739 scsi_unblock_requests(h->scsi_host);
6740 } else {
6741 /* Acknowledge controller notification events. */
6742 writel(h->events, &(h->cfgtable->clear_event_notify));
6743 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6744 hpsa_wait_for_clear_event_notify_ack(h);
6745#if 0
6746 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6747 hpsa_wait_for_mode_change_ack(h);
6748#endif
6749 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06006750 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006751}
6752
6753/* Check a register on the controller to see if there are configuration
6754 * changes (added/changed/removed logical drives, etc.) which mean that
Scott Teele863d682014-02-18 13:57:05 -06006755 * we should rescan the controller for devices.
6756 * Also check flag for driver-initiated rescan.
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006757 */
Stephen M. Cameron98465902014-02-21 16:25:00 -06006758static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006759{
6760 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
Stephen M. Cameron98465902014-02-21 16:25:00 -06006761 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006762
6763 h->events = readl(&(h->cfgtable->event_notify));
Stephen M. Cameron98465902014-02-21 16:25:00 -06006764 return h->events & RESCAN_REQUIRED_EVENT_BITS;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006765}
6766
Stephen M. Cameron98465902014-02-21 16:25:00 -06006767/*
6768 * Check if any of the offline devices have become ready
6769 */
6770static int hpsa_offline_devices_ready(struct ctlr_info *h)
6771{
6772 unsigned long flags;
6773 struct offline_device_entry *d;
6774 struct list_head *this, *tmp;
6775
6776 spin_lock_irqsave(&h->offline_device_lock, flags);
6777 list_for_each_safe(this, tmp, &h->offline_device_list) {
6778 d = list_entry(this, struct offline_device_entry,
6779 offline_list);
6780 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Camerond1fea472014-07-03 10:17:58 -05006781 if (!hpsa_volume_offline(h, d->scsi3addr)) {
6782 spin_lock_irqsave(&h->offline_device_lock, flags);
6783 list_del(&d->offline_list);
6784 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Cameron98465902014-02-21 16:25:00 -06006785 return 1;
Stephen M. Camerond1fea472014-07-03 10:17:58 -05006786 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06006787 spin_lock_irqsave(&h->offline_device_lock, flags);
6788 }
6789 spin_unlock_irqrestore(&h->offline_device_lock, flags);
6790 return 0;
6791}
6792
Don Brace6636e7f2015-01-23 16:45:17 -06006793static void hpsa_rescan_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006794{
6795 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006796 struct ctlr_info *h = container_of(to_delayed_work(work),
Don Brace6636e7f2015-01-23 16:45:17 -06006797 struct ctlr_info, rescan_ctlr_work);
6798
6799
6800 if (h->remove_in_progress)
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006801 return;
Stephen M. Cameron98465902014-02-21 16:25:00 -06006802
6803 if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
6804 scsi_host_get(h->scsi_host);
Stephen M. Cameron98465902014-02-21 16:25:00 -06006805 hpsa_ack_ctlr_events(h);
6806 hpsa_scan_start(h->scsi_host);
6807 scsi_host_put(h->scsi_host);
6808 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006809 spin_lock_irqsave(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06006810 if (!h->remove_in_progress)
6811 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006812 h->heartbeat_sample_interval);
6813 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006814}
6815
Don Brace6636e7f2015-01-23 16:45:17 -06006816static void hpsa_monitor_ctlr_worker(struct work_struct *work)
6817{
6818 unsigned long flags;
6819 struct ctlr_info *h = container_of(to_delayed_work(work),
6820 struct ctlr_info, monitor_ctlr_work);
6821
6822 detect_controller_lockup(h);
6823 if (lockup_detected(h))
6824 return;
6825
6826 spin_lock_irqsave(&h->lock, flags);
6827 if (!h->remove_in_progress)
6828 schedule_delayed_work(&h->monitor_ctlr_work,
6829 h->heartbeat_sample_interval);
6830 spin_unlock_irqrestore(&h->lock, flags);
6831}
6832
6833static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
6834 char *name)
6835{
6836 struct workqueue_struct *wq = NULL;
Don Brace6636e7f2015-01-23 16:45:17 -06006837
Don Brace397ea9c2015-02-06 17:44:15 -06006838 wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
Don Brace6636e7f2015-01-23 16:45:17 -06006839 if (!wq)
6840 dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
6841
6842 return wq;
6843}
6844
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006845static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006846{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006847 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006848 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006849 int try_soft_reset = 0;
6850 unsigned long flags;
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02006851 u32 board_id;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006852
6853 if (number_of_controllers == 0)
6854 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006855
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02006856 rc = hpsa_lookup_board_id(pdev, &board_id);
6857 if (rc < 0) {
6858 dev_warn(&pdev->dev, "Board ID not found\n");
6859 return rc;
6860 }
6861
6862 rc = hpsa_init_reset_devices(pdev, board_id);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006863 if (rc) {
6864 if (rc != -ENOTSUPP)
6865 return rc;
6866 /* If the reset fails in a particular way (it has no way to do
6867 * a proper hard reset, so returns -ENOTSUPP) we can try to do
6868 * a soft reset once we get the controller configured up to the
6869 * point that it can accept a command.
6870 */
6871 try_soft_reset = 1;
6872 rc = 0;
6873 }
6874
6875reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006876
Don Brace303932f2010-02-04 08:42:40 -06006877 /* Command structures must be aligned on a 32-byte boundary because
6878 * the 5 lower bits of the address are used by the hardware. and by
6879 * the driver. See comments in hpsa.h for more info.
6880 */
Don Brace303932f2010-02-04 08:42:40 -06006881 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006882 h = kzalloc(sizeof(*h), GFP_KERNEL);
6883 if (!h)
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006884 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006885
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006886 h->pdev = pdev;
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006887 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron98465902014-02-21 16:25:00 -06006888 INIT_LIST_HEAD(&h->offline_device_list);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006889 spin_lock_init(&h->lock);
Stephen M. Cameron98465902014-02-21 16:25:00 -06006890 spin_lock_init(&h->offline_device_lock);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006891 spin_lock_init(&h->scan_lock);
Don Brace34f0c622015-01-23 16:43:46 -06006892 atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05006893
Don Brace6636e7f2015-01-23 16:45:17 -06006894 h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
6895 if (!h->rescan_ctlr_wq) {
Don Brace080ef1c2015-01-23 16:43:25 -06006896 rc = -ENOMEM;
6897 goto clean1;
6898 }
Don Brace6636e7f2015-01-23 16:45:17 -06006899
6900 h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
6901 if (!h->resubmit_wq) {
6902 rc = -ENOMEM;
6903 goto clean1;
6904 }
6905
Stephen M. Cameron094963d2014-05-29 10:53:18 -05006906 /* Allocate and clear per-cpu variable lockup_detected */
6907 h->lockup_detected = alloc_percpu(u32);
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05006908 if (!h->lockup_detected) {
6909 rc = -ENOMEM;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05006910 goto clean1;
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05006911 }
Stephen M. Cameron094963d2014-05-29 10:53:18 -05006912 set_lockup_detected_for_all_cpus(h, 0);
6913
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006914 rc = hpsa_pci_init(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006915 if (rc != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006916 goto clean1;
6917
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006918 sprintf(h->devname, HPSA "%d", number_of_controllers);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006919 h->ctlr = number_of_controllers;
6920 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006921
6922 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006923 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6924 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006925 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006926 } else {
6927 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6928 if (rc == 0) {
6929 dac = 0;
6930 } else {
6931 dev_err(&pdev->dev, "no suitable DMA available\n");
6932 goto clean1;
6933 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006934 }
6935
6936 /* make sure the board interrupts are off */
6937 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006938
Robert Elliott9ee61792015-01-23 16:42:32 -06006939 if (hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006940 goto clean2;
Don Brace303932f2010-02-04 08:42:40 -06006941 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6942 h->devname, pdev->device,
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006943 h->intr[h->intr_mode], dac ? "" : " not");
Robert Elliott8947fd12015-01-23 16:42:54 -06006944 rc = hpsa_allocate_cmd_pool(h);
6945 if (rc)
6946 goto clean2_and_free_irqs;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006947 if (hpsa_allocate_sg_chain_blocks(h))
6948 goto clean4;
Stephen M. Camerona08a84712010-02-04 08:43:16 -06006949 init_waitqueue_head(&h->scan_wait_queue);
6950 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006951
6952 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05006953 h->ndevices = 0;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006954 h->hba_mode_enabled = 0;
Stephen M. Cameron9a413382011-05-03 14:59:41 -05006955 h->scsi_host = NULL;
6956 spin_lock_init(&h->devlock);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006957 hpsa_put_ctlr_into_performant_mode(h);
6958
6959 /* At this point, the controller is ready to take commands.
6960 * Now, if reset_devices and the hard reset didn't work, try
6961 * the soft reset and see if that works.
6962 */
6963 if (try_soft_reset) {
6964
6965 /* This is kind of gross. We may or may not get a completion
6966 * from the soft reset command, and if we do, then the value
6967 * from the fifo may or may not be valid. So, we wait 10 secs
6968 * after the reset throwing away any completions we get during
6969 * that time. Unregister the interrupt handler and register
6970 * fake ones to scoop up any residual completions.
6971 */
6972 spin_lock_irqsave(&h->lock, flags);
6973 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6974 spin_unlock_irqrestore(&h->lock, flags);
Robert Elliottec501a12015-01-23 16:41:40 -06006975 hpsa_free_irqs(h);
Robert Elliott9ee61792015-01-23 16:42:32 -06006976 rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006977 hpsa_intx_discard_completions);
6978 if (rc) {
Robert Elliott9ee61792015-01-23 16:42:32 -06006979 dev_warn(&h->pdev->dev,
6980 "Failed to request_irq after soft reset.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006981 goto clean4;
6982 }
6983
6984 rc = hpsa_kdump_soft_reset(h);
6985 if (rc)
6986 /* Neither hard nor soft reset worked, we're hosed. */
6987 goto clean4;
6988
6989 dev_info(&h->pdev->dev, "Board READY.\n");
6990 dev_info(&h->pdev->dev,
6991 "Waiting for stale completions to drain.\n");
6992 h->access.set_intr_mask(h, HPSA_INTR_ON);
6993 msleep(10000);
6994 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6995
6996 rc = controller_reset_failed(h->cfgtable);
6997 if (rc)
6998 dev_info(&h->pdev->dev,
6999 "Soft reset appears to have failed.\n");
7000
7001 /* since the controller's reset, we have to go back and re-init
7002 * everything. Easiest to just forget what we've done and do it
7003 * all over again.
7004 */
7005 hpsa_undo_allocations_after_kdump_soft_reset(h);
7006 try_soft_reset = 0;
7007 if (rc)
7008 /* don't go to clean4, we already unallocated */
7009 return -ENODEV;
7010
7011 goto reinit_after_soft_reset;
7012 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007013
Stephen M. Cameron316b2212014-02-21 16:25:15 -06007014 /* Enable Accelerated IO path at driver layer */
7015 h->acciopath_status = 1;
Scott Teelda0697b2014-02-18 13:57:00 -06007016
Scott Teele863d682014-02-18 13:57:05 -06007017
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007018 /* Turn the interrupts on so we can service requests */
7019 h->access.set_intr_mask(h, HPSA_INTR_ON);
7020
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06007021 hpsa_hba_inquiry(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007022 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06007023
7024 /* Monitor the controller for firmware lockups */
7025 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
7026 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
7027 schedule_delayed_work(&h->monitor_ctlr_work,
7028 h->heartbeat_sample_interval);
Don Brace6636e7f2015-01-23 16:45:17 -06007029 INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
7030 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
7031 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05007032 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007033
7034clean4:
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06007035 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007036 hpsa_free_cmd_pool(h);
Robert Elliott8947fd12015-01-23 16:42:54 -06007037clean2_and_free_irqs:
Robert Elliottec501a12015-01-23 16:41:40 -06007038 hpsa_free_irqs(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007039clean2:
7040clean1:
Don Brace080ef1c2015-01-23 16:43:25 -06007041 if (h->resubmit_wq)
7042 destroy_workqueue(h->resubmit_wq);
Don Brace6636e7f2015-01-23 16:45:17 -06007043 if (h->rescan_ctlr_wq)
7044 destroy_workqueue(h->rescan_ctlr_wq);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007045 if (h->lockup_detected)
7046 free_percpu(h->lockup_detected);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007047 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06007048 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007049}
7050
7051static void hpsa_flush_cache(struct ctlr_info *h)
7052{
7053 char *flush_buf;
7054 struct CommandList *c;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05007055
7056 /* Don't bother trying to flush the cache if locked up */
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007057 if (unlikely(lockup_detected(h)))
Stephen M. Cameron702890e2013-09-23 13:33:30 -05007058 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007059 flush_buf = kzalloc(4, GFP_KERNEL);
7060 if (!flush_buf)
7061 return;
7062
Stephen Cameron45fcb862015-01-23 16:43:04 -06007063 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007064 if (!c) {
Stephen Cameron45fcb862015-01-23 16:43:04 -06007065 dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007066 goto out_of_memory;
7067 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06007068 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
7069 RAID_CTLR_LUNID, TYPE_CMD)) {
7070 goto out;
7071 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007072 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
7073 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06007074out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007075 dev_warn(&h->pdev->dev,
7076 "error flushing cache on controller\n");
Stephen Cameron45fcb862015-01-23 16:43:04 -06007077 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007078out_of_memory:
7079 kfree(flush_buf);
7080}
7081
7082static void hpsa_shutdown(struct pci_dev *pdev)
7083{
7084 struct ctlr_info *h;
7085
7086 h = pci_get_drvdata(pdev);
7087 /* Turn board interrupts off and send the flush cache command
7088 * sendcmd will turn off interrupt, and send the flush...
7089 * To write all data in the battery backed cache to disks
7090 */
7091 hpsa_flush_cache(h);
7092 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05007093 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007094}
7095
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007096static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06007097{
7098 int i;
7099
7100 for (i = 0; i < h->ndevices; i++)
7101 kfree(h->dev[i]);
7102}
7103
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007104static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007105{
7106 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06007107 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007108
7109 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007110 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007111 return;
7112 }
7113 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06007114
7115 /* Get rid of any controller monitoring work items */
7116 spin_lock_irqsave(&h->lock, flags);
7117 h->remove_in_progress = 1;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06007118 spin_unlock_irqrestore(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06007119 cancel_delayed_work_sync(&h->monitor_ctlr_work);
7120 cancel_delayed_work_sync(&h->rescan_ctlr_work);
7121 destroy_workqueue(h->rescan_ctlr_wq);
7122 destroy_workqueue(h->resubmit_wq);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007123 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
7124 hpsa_shutdown(pdev);
7125 iounmap(h->vaddr);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007126 iounmap(h->transtable);
7127 iounmap(h->cfgtable);
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06007128 hpsa_free_device_info(h);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06007129 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007130 pci_free_consistent(h->pdev,
7131 h->nr_cmds * sizeof(struct CommandList),
7132 h->cmd_pool, h->cmd_pool_dhandle);
7133 pci_free_consistent(h->pdev,
7134 h->nr_cmds * sizeof(struct ErrorInfo),
7135 h->errinfo_pool, h->errinfo_pool_dhandle);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007136 hpsa_free_reply_queues(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007137 kfree(h->cmd_pool_bits);
Don Brace303932f2010-02-04 08:42:40 -06007138 kfree(h->blockFetchTable);
Matt Gatese1f7de02014-02-18 13:55:17 -06007139 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06007140 kfree(h->ioaccel2_blockFetchTable);
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06007141 kfree(h->hba_inquiry_data);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05007142 pci_disable_device(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007143 pci_release_regions(pdev);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007144 free_percpu(h->lockup_detected);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007145 kfree(h);
7146}
7147
7148static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
7149 __attribute__((unused)) pm_message_t state)
7150{
7151 return -ENOSYS;
7152}
7153
7154static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
7155{
7156 return -ENOSYS;
7157}
7158
7159static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06007160 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007161 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007162 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007163 .id_table = hpsa_pci_device_id, /* id_table */
7164 .shutdown = hpsa_shutdown,
7165 .suspend = hpsa_suspend,
7166 .resume = hpsa_resume,
7167};
7168
Don Brace303932f2010-02-04 08:42:40 -06007169/* Fill in bucket_map[], given nsgs (the max number of
7170 * scatter gather elements supported) and bucket[],
7171 * which is an array of 8 integers. The bucket[] array
7172 * contains 8 different DMA transfer sizes (in 16
7173 * byte increments) which the controller uses to fetch
7174 * commands. This function fills in bucket_map[], which
7175 * maps a given number of scatter gather elements to one of
7176 * the 8 DMA transfer sizes. The point of it is to allow the
7177 * controller to only do as much DMA as needed to fetch the
7178 * command, with the DMA transfer size encoded in the lower
7179 * bits of the command address.
7180 */
7181static void calc_bucket_map(int bucket[], int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -06007182 int nsgs, int min_blocks, u32 *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06007183{
7184 int i, j, b, size;
7185
Don Brace303932f2010-02-04 08:42:40 -06007186 /* Note, bucket_map must have nsgs+1 entries. */
7187 for (i = 0; i <= nsgs; i++) {
7188 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06007189 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06007190 b = num_buckets; /* Assume the biggest bucket */
7191 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06007192 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06007193 if (bucket[j] >= size) {
7194 b = j;
7195 break;
7196 }
7197 }
7198 /* for a command with i SG entries, use bucket b. */
7199 bucket_map[i] = b;
7200 }
7201}
7202
Robert Elliottc706a792015-01-23 16:45:01 -06007203/* return -ENODEV or other reason on error, 0 on success */
7204static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06007205{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007206 int i;
7207 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06007208 unsigned long transMethod = CFGTBL_Trans_Performant |
7209 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007210 CFGTBL_Trans_enable_directed_msix |
7211 (trans_support & (CFGTBL_Trans_io_accel1 |
7212 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06007213 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05007214
7215 /* This is a bit complicated. There are 8 registers on
7216 * the controller which we write to to tell it 8 different
7217 * sizes of commands which there may be. It's a way of
7218 * reducing the DMA done to fetch each command. Encoded into
7219 * each command's tag are 3 bits which communicate to the controller
7220 * which of the eight sizes that command fits within. The size of
7221 * each command depends on how many scatter gather entries there are.
7222 * Each SG entry requires 16 bytes. The eight registers are programmed
7223 * with the number of 16-byte blocks a command of that size requires.
7224 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06007225 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05007226 * blocks. Note, this only extends to the SG entries contained
7227 * within the command block, and does not extend to chained blocks
7228 * of SG elements. bft[] contains the eight values we write to
7229 * the registers. They are not evenly distributed, but have more
7230 * sizes for small commands, and fewer sizes for larger commands.
7231 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06007232 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007233#define MIN_IOACCEL2_BFT_ENTRY 5
7234#define HPSA_IOACCEL2_HEADER_SZ 4
7235 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
7236 13, 14, 15, 16, 17, 18, 19,
7237 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
7238 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
7239 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
7240 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
7241 16 * MIN_IOACCEL2_BFT_ENTRY);
7242 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06007243 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06007244 /* 5 = 1 s/g entry or 4k
7245 * 6 = 2 s/g entry or 8k
7246 * 8 = 4 s/g entry or 16k
7247 * 10 = 6 s/g entry or 24k
7248 */
Don Brace303932f2010-02-04 08:42:40 -06007249
Stephen M. Cameronb3a52e72014-05-29 10:53:23 -05007250 /* If the controller supports either ioaccel method then
7251 * we can also use the RAID stack submit path that does not
7252 * perform the superfluous readl() after each command submission.
7253 */
7254 if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
7255 access = SA5_performant_access_no_read;
7256
Don Brace303932f2010-02-04 08:42:40 -06007257 /* Controller spec: zero out this buffer. */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007258 for (i = 0; i < h->nreply_queues; i++)
7259 memset(h->reply_queue[i].head, 0, h->reply_queue_size);
Don Brace303932f2010-02-04 08:42:40 -06007260
Stephen M. Camerond66ae082012-01-19 14:00:48 -06007261 bft[7] = SG_ENTRIES_IN_CMD + 4;
7262 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06007263 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06007264 for (i = 0; i < 8; i++)
7265 writel(bft[i], &h->transtable->BlockFetch[i]);
7266
7267 /* size of controller ring buffer */
7268 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05007269 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06007270 writel(0, &h->transtable->RepQCtrAddrLow32);
7271 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05007272
7273 for (i = 0; i < h->nreply_queues; i++) {
7274 writel(0, &h->transtable->RepQAddr[i].upper);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007275 writel(h->reply_queue[i].busaddr,
Matt Gates254f7962012-05-01 11:43:06 -05007276 &h->transtable->RepQAddr[i].lower);
7277 }
7278
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007279 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06007280 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
7281 /*
7282 * enable outbound interrupt coalescing in accelerator mode;
7283 */
7284 if (trans_support & CFGTBL_Trans_io_accel1) {
7285 access = SA5_ioaccel_mode1_access;
7286 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7287 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Scott Teelc3497752014-02-18 13:56:34 -06007288 } else {
7289 if (trans_support & CFGTBL_Trans_io_accel2) {
7290 access = SA5_ioaccel_mode2_access;
7291 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7292 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7293 }
Matt Gatese1f7de02014-02-18 13:55:17 -06007294 }
Don Brace303932f2010-02-04 08:42:40 -06007295 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06007296 if (hpsa_wait_for_mode_change_ack(h)) {
7297 dev_err(&h->pdev->dev,
7298 "performant mode problem - doorbell timeout\n");
7299 return -ENODEV;
7300 }
Don Brace303932f2010-02-04 08:42:40 -06007301 register_value = readl(&(h->cfgtable->TransportActive));
7302 if (!(register_value & CFGTBL_Trans_Performant)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007303 dev_err(&h->pdev->dev,
7304 "performant mode problem - transport not active\n");
Robert Elliottc706a792015-01-23 16:45:01 -06007305 return -ENODEV;
Don Brace303932f2010-02-04 08:42:40 -06007306 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06007307 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06007308 h->access = access;
7309 h->transMethod = transMethod;
7310
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007311 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
7312 (trans_support & CFGTBL_Trans_io_accel2)))
Robert Elliottc706a792015-01-23 16:45:01 -06007313 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06007314
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007315 if (trans_support & CFGTBL_Trans_io_accel1) {
7316 /* Set up I/O accelerator mode */
7317 for (i = 0; i < h->nreply_queues; i++) {
7318 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
7319 h->reply_queue[i].current_entry =
7320 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
7321 }
7322 bft[7] = h->ioaccel_maxsg + 8;
7323 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
7324 h->ioaccel1_blockFetchTable);
7325
7326 /* initialize all reply queue entries to unused */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007327 for (i = 0; i < h->nreply_queues; i++)
7328 memset(h->reply_queue[i].head,
7329 (u8) IOACCEL_MODE1_REPLY_UNUSED,
7330 h->reply_queue_size);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007331
7332 /* set all the constant fields in the accelerator command
7333 * frames once at init time to save CPU cycles later.
7334 */
7335 for (i = 0; i < h->nr_cmds; i++) {
7336 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
7337
7338 cp->function = IOACCEL1_FUNCTION_SCSIIO;
7339 cp->err_info = (u32) (h->errinfo_pool_dhandle +
7340 (i * sizeof(struct ErrorInfo)));
7341 cp->err_info_len = sizeof(struct ErrorInfo);
7342 cp->sgl_offset = IOACCEL1_SGLOFFSET;
Don Brace2b08b3e2015-01-23 16:41:09 -06007343 cp->host_context_flags =
7344 cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007345 cp->timeout_sec = 0;
7346 cp->ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007347 cp->tag =
Don Bracef2405db2015-01-23 16:43:09 -06007348 cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007349 cp->host_addr =
7350 cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007351 (i * sizeof(struct io_accel1_cmd)));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007352 }
7353 } else if (trans_support & CFGTBL_Trans_io_accel2) {
7354 u64 cfg_offset, cfg_base_addr_index;
7355 u32 bft2_offset, cfg_base_addr;
7356 int rc;
7357
7358 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7359 &cfg_base_addr_index, &cfg_offset);
7360 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
7361 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
7362 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
7363 4, h->ioaccel2_blockFetchTable);
7364 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
7365 BUILD_BUG_ON(offsetof(struct CfgTable,
7366 io_accel_request_size_offset) != 0xb8);
7367 h->ioaccel2_bft2_regs =
7368 remap_pci_mem(pci_resource_start(h->pdev,
7369 cfg_base_addr_index) +
7370 cfg_offset + bft2_offset,
7371 ARRAY_SIZE(bft2) *
7372 sizeof(*h->ioaccel2_bft2_regs));
7373 for (i = 0; i < ARRAY_SIZE(bft2); i++)
7374 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06007375 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007376 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06007377 if (hpsa_wait_for_mode_change_ack(h)) {
7378 dev_err(&h->pdev->dev,
7379 "performant mode problem - enabling ioaccel mode\n");
7380 return -ENODEV;
7381 }
7382 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06007383}
7384
7385static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
7386{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007387 h->ioaccel_maxsg =
7388 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7389 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
7390 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
7391
Matt Gatese1f7de02014-02-18 13:55:17 -06007392 /* Command structures must be aligned on a 128-byte boundary
7393 * because the 7 lower bits of the address are used by the
7394 * hardware.
7395 */
Matt Gatese1f7de02014-02-18 13:55:17 -06007396 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
7397 IOACCEL1_COMMANDLIST_ALIGNMENT);
7398 h->ioaccel_cmd_pool =
7399 pci_alloc_consistent(h->pdev,
7400 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7401 &(h->ioaccel_cmd_pool_dhandle));
7402
7403 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007404 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06007405 sizeof(u32)), GFP_KERNEL);
7406
7407 if ((h->ioaccel_cmd_pool == NULL) ||
7408 (h->ioaccel1_blockFetchTable == NULL))
7409 goto clean_up;
7410
7411 memset(h->ioaccel_cmd_pool, 0,
7412 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
7413 return 0;
7414
7415clean_up:
7416 if (h->ioaccel_cmd_pool)
7417 pci_free_consistent(h->pdev,
7418 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7419 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
7420 kfree(h->ioaccel1_blockFetchTable);
7421 return 1;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007422}
7423
Stephen M. Cameronaca90122014-02-18 13:56:14 -06007424static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
7425{
7426 /* Allocate ioaccel2 mode command blocks and block fetch table */
7427
7428 h->ioaccel_maxsg =
7429 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7430 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
7431 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
7432
Stephen M. Cameronaca90122014-02-18 13:56:14 -06007433 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
7434 IOACCEL2_COMMANDLIST_ALIGNMENT);
7435 h->ioaccel2_cmd_pool =
7436 pci_alloc_consistent(h->pdev,
7437 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7438 &(h->ioaccel2_cmd_pool_dhandle));
7439
7440 h->ioaccel2_blockFetchTable =
7441 kmalloc(((h->ioaccel_maxsg + 1) *
7442 sizeof(u32)), GFP_KERNEL);
7443
7444 if ((h->ioaccel2_cmd_pool == NULL) ||
7445 (h->ioaccel2_blockFetchTable == NULL))
7446 goto clean_up;
7447
7448 memset(h->ioaccel2_cmd_pool, 0,
7449 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
7450 return 0;
7451
7452clean_up:
7453 if (h->ioaccel2_cmd_pool)
7454 pci_free_consistent(h->pdev,
7455 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7456 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
7457 kfree(h->ioaccel2_blockFetchTable);
7458 return 1;
7459}
7460
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007461static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007462{
7463 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06007464 unsigned long transMethod = CFGTBL_Trans_Performant |
7465 CFGTBL_Trans_use_short_tags;
Matt Gates254f7962012-05-01 11:43:06 -05007466 int i;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007467
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06007468 if (hpsa_simple_mode)
7469 return;
7470
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05007471 trans_support = readl(&(h->cfgtable->TransportSupport));
7472 if (!(trans_support & PERFORMANT_MODE))
7473 return;
7474
Matt Gatese1f7de02014-02-18 13:55:17 -06007475 /* Check for I/O accelerator mode support */
7476 if (trans_support & CFGTBL_Trans_io_accel1) {
7477 transMethod |= CFGTBL_Trans_io_accel1 |
7478 CFGTBL_Trans_enable_directed_msix;
7479 if (hpsa_alloc_ioaccel_cmd_and_bft(h))
7480 goto clean_up;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06007481 } else {
7482 if (trans_support & CFGTBL_Trans_io_accel2) {
7483 transMethod |= CFGTBL_Trans_io_accel2 |
7484 CFGTBL_Trans_enable_directed_msix;
7485 if (ioaccel2_alloc_cmds_and_bft(h))
7486 goto clean_up;
7487 }
Matt Gatese1f7de02014-02-18 13:55:17 -06007488 }
7489
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007490 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007491 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007492 /* Performant mode ring buffer and supporting data structures */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007493 h->reply_queue_size = h->max_commands * sizeof(u64);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007494
Matt Gates254f7962012-05-01 11:43:06 -05007495 for (i = 0; i < h->nreply_queues; i++) {
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007496 h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
7497 h->reply_queue_size,
7498 &(h->reply_queue[i].busaddr));
7499 if (!h->reply_queue[i].head)
7500 goto clean_up;
Matt Gates254f7962012-05-01 11:43:06 -05007501 h->reply_queue[i].size = h->max_commands;
7502 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
7503 h->reply_queue[i].current_entry = 0;
7504 }
7505
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007506 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06007507 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007508 sizeof(u32)), GFP_KERNEL);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007509 if (!h->blockFetchTable)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007510 goto clean_up;
7511
Matt Gatese1f7de02014-02-18 13:55:17 -06007512 hpsa_enter_performant_mode(h, trans_support);
Don Brace303932f2010-02-04 08:42:40 -06007513 return;
7514
7515clean_up:
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007516 hpsa_free_reply_queues(h);
Don Brace303932f2010-02-04 08:42:40 -06007517 kfree(h->blockFetchTable);
7518}
7519
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007520static int is_accelerated_cmd(struct CommandList *c)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007521{
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007522 return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
7523}
7524
7525static void hpsa_drain_accel_commands(struct ctlr_info *h)
7526{
7527 struct CommandList *c = NULL;
Don Bracef2405db2015-01-23 16:43:09 -06007528 int i, accel_cmds_out;
Webb Scales281a7fd2015-01-23 16:43:35 -06007529 int refcount;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007530
Don Bracef2405db2015-01-23 16:43:09 -06007531 do { /* wait for all outstanding ioaccel commands to drain out */
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007532 accel_cmds_out = 0;
Don Bracef2405db2015-01-23 16:43:09 -06007533 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06007534 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06007535 refcount = atomic_inc_return(&c->refcount);
7536 if (refcount > 1) /* Command is allocated */
7537 accel_cmds_out += is_accelerated_cmd(c);
7538 cmd_free(h, c);
Don Bracef2405db2015-01-23 16:43:09 -06007539 }
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007540 if (accel_cmds_out <= 0)
Webb Scales281a7fd2015-01-23 16:43:35 -06007541 break;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007542 msleep(100);
7543 } while (1);
7544}
7545
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007546/*
7547 * This is it. Register the PCI driver information for the cards we control
7548 * the OS will call our registered routines when it finds one of our cards.
7549 */
7550static int __init hpsa_init(void)
7551{
Mike Miller31468402010-02-25 14:03:12 -06007552 return pci_register_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007553}
7554
7555static void __exit hpsa_cleanup(void)
7556{
7557 pci_unregister_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007558}
7559
Matt Gatese1f7de02014-02-18 13:55:17 -06007560static void __attribute__((unused)) verify_offsets(void)
7561{
7562#define VERIFY_OFFSET(member, offset) \
Scott Teeldd0e19f2014-02-18 13:57:31 -06007563 BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
7564
7565 VERIFY_OFFSET(structure_size, 0);
7566 VERIFY_OFFSET(volume_blk_size, 4);
7567 VERIFY_OFFSET(volume_blk_cnt, 8);
7568 VERIFY_OFFSET(phys_blk_shift, 16);
7569 VERIFY_OFFSET(parity_rotation_shift, 17);
7570 VERIFY_OFFSET(strip_size, 18);
7571 VERIFY_OFFSET(disk_starting_blk, 20);
7572 VERIFY_OFFSET(disk_blk_cnt, 28);
7573 VERIFY_OFFSET(data_disks_per_row, 36);
7574 VERIFY_OFFSET(metadata_disks_per_row, 38);
7575 VERIFY_OFFSET(row_cnt, 40);
7576 VERIFY_OFFSET(layout_map_count, 42);
7577 VERIFY_OFFSET(flags, 44);
7578 VERIFY_OFFSET(dekindex, 46);
7579 /* VERIFY_OFFSET(reserved, 48 */
7580 VERIFY_OFFSET(data, 64);
7581
7582#undef VERIFY_OFFSET
7583
7584#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06007585 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
7586
7587 VERIFY_OFFSET(IU_type, 0);
7588 VERIFY_OFFSET(direction, 1);
7589 VERIFY_OFFSET(reply_queue, 2);
7590 /* VERIFY_OFFSET(reserved1, 3); */
7591 VERIFY_OFFSET(scsi_nexus, 4);
7592 VERIFY_OFFSET(Tag, 8);
7593 VERIFY_OFFSET(cdb, 16);
7594 VERIFY_OFFSET(cciss_lun, 32);
7595 VERIFY_OFFSET(data_len, 40);
7596 VERIFY_OFFSET(cmd_priority_task_attr, 44);
7597 VERIFY_OFFSET(sg_count, 45);
7598 /* VERIFY_OFFSET(reserved3 */
7599 VERIFY_OFFSET(err_ptr, 48);
7600 VERIFY_OFFSET(err_len, 56);
7601 /* VERIFY_OFFSET(reserved4 */
7602 VERIFY_OFFSET(sg, 64);
7603
7604#undef VERIFY_OFFSET
7605
7606#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06007607 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
7608
7609 VERIFY_OFFSET(dev_handle, 0x00);
7610 VERIFY_OFFSET(reserved1, 0x02);
7611 VERIFY_OFFSET(function, 0x03);
7612 VERIFY_OFFSET(reserved2, 0x04);
7613 VERIFY_OFFSET(err_info, 0x0C);
7614 VERIFY_OFFSET(reserved3, 0x10);
7615 VERIFY_OFFSET(err_info_len, 0x12);
7616 VERIFY_OFFSET(reserved4, 0x13);
7617 VERIFY_OFFSET(sgl_offset, 0x14);
7618 VERIFY_OFFSET(reserved5, 0x15);
7619 VERIFY_OFFSET(transfer_len, 0x1C);
7620 VERIFY_OFFSET(reserved6, 0x20);
7621 VERIFY_OFFSET(io_flags, 0x24);
7622 VERIFY_OFFSET(reserved7, 0x26);
7623 VERIFY_OFFSET(LUN, 0x34);
7624 VERIFY_OFFSET(control, 0x3C);
7625 VERIFY_OFFSET(CDB, 0x40);
7626 VERIFY_OFFSET(reserved8, 0x50);
7627 VERIFY_OFFSET(host_context_flags, 0x60);
7628 VERIFY_OFFSET(timeout_sec, 0x62);
7629 VERIFY_OFFSET(ReplyQueue, 0x64);
7630 VERIFY_OFFSET(reserved9, 0x65);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007631 VERIFY_OFFSET(tag, 0x68);
Matt Gatese1f7de02014-02-18 13:55:17 -06007632 VERIFY_OFFSET(host_addr, 0x70);
7633 VERIFY_OFFSET(CISS_LUN, 0x78);
7634 VERIFY_OFFSET(SG, 0x78 + 8);
7635#undef VERIFY_OFFSET
7636}
7637
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007638module_init(hpsa_init);
7639module_exit(hpsa_cleanup);