blob: 216c137d96ea909524a60c16cbe61ece9b77405e [file] [log] [blame]
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001/*
2 * Disk Array driver for HP Smart Array SAS controllers
Don Brace94c7bc32016-02-23 15:16:46 -06003 * Copyright 2016 Microsemi Corporation
Don Brace1358f6d2015-07-18 11:12:38 -05004 * Copyright 2014-2015 PMC-Sierra, Inc.
5 * Copyright 2000,2009-2015 Hewlett-Packard Development Company, L.P.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more details.
15 *
Don Brace94c7bc32016-02-23 15:16:46 -060016 * Questions/Comments/Bugfixes to esc.storagedev@microsemi.com
Stephen M. Cameronedd16362009-12-08 14:09:11 -080017 *
18 */
19
20#include <linux/module.h>
21#include <linux/interrupt.h>
22#include <linux/types.h>
23#include <linux/pci.h>
Matthew Garrette5a44df2011-11-11 11:14:23 -050024#include <linux/pci-aspm.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080025#include <linux/kernel.h>
26#include <linux/slab.h>
27#include <linux/delay.h>
28#include <linux/fs.h>
29#include <linux/timer.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080030#include <linux/init.h>
31#include <linux/spinlock.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080032#include <linux/compat.h>
33#include <linux/blktrace_api.h>
34#include <linux/uaccess.h>
35#include <linux/io.h>
36#include <linux/dma-mapping.h>
37#include <linux/completion.h>
38#include <linux/moduleparam.h>
39#include <scsi/scsi.h>
40#include <scsi/scsi_cmnd.h>
41#include <scsi/scsi_device.h>
42#include <scsi/scsi_host.h>
Stephen M. Cameron667e23d2010-02-25 14:02:51 -060043#include <scsi/scsi_tcq.h>
Stephen Cameron9437ac42015-04-23 09:32:16 -050044#include <scsi/scsi_eh.h>
Kevin Barnettd04e62b2015-11-04 15:52:34 -060045#include <scsi/scsi_transport_sas.h>
Webb Scales73153fe2015-04-23 09:35:04 -050046#include <scsi/scsi_dbg.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080047#include <linux/cciss_ioctl.h>
48#include <linux/string.h>
49#include <linux/bitmap.h>
Arun Sharma600634972011-07-26 16:09:06 -070050#include <linux/atomic.h>
Stephen M. Camerona0c12412011-10-26 16:22:04 -050051#include <linux/jiffies.h>
Don Brace42a91642014-11-14 17:26:27 -060052#include <linux/percpu-defs.h>
Stephen M. Cameron094963d2014-05-29 10:53:18 -050053#include <linux/percpu.h>
Don Brace2b08b3e2015-01-23 16:41:09 -060054#include <asm/unaligned.h>
Stephen M. Cameron283b4a92014-02-18 13:55:33 -060055#include <asm/div64.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080056#include "hpsa_cmd.h"
57#include "hpsa.h"
58
Don Braceec2c3aa2015-11-04 15:52:40 -060059/*
60 * HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.'
61 * with an optional trailing '-' followed by a byte value (0-255).
62 */
Don Braceff54aee2016-04-27 17:14:26 -050063#define HPSA_DRIVER_VERSION "3.4.16-0"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080064#define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -060065#define HPSA "hpsa"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080066
Robert Elliott007e7aa2015-01-23 16:44:56 -060067/* How long to wait for CISS doorbell communication */
68#define CLEAR_EVENT_WAIT_INTERVAL 20 /* ms for each msleep() call */
69#define MODE_CHANGE_WAIT_INTERVAL 10 /* ms for each msleep() call */
70#define MAX_CLEAR_EVENT_WAIT 30000 /* times 20 ms = 600 s */
71#define MAX_MODE_CHANGE_WAIT 2000 /* times 10 ms = 20 s */
Stephen M. Cameronedd16362009-12-08 14:09:11 -080072#define MAX_IOCTL_CONFIG_WAIT 1000
73
74/*define how many times we will try a command because of bus resets */
75#define MAX_CMD_RETRIES 3
76
77/* Embedded module documentation macros - see modules.h */
78MODULE_AUTHOR("Hewlett-Packard Company");
79MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
80 HPSA_DRIVER_VERSION);
81MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
82MODULE_VERSION(HPSA_DRIVER_VERSION);
83MODULE_LICENSE("GPL");
84
85static int hpsa_allow_any;
86module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
87MODULE_PARM_DESC(hpsa_allow_any,
88 "Allow hpsa driver to access unknown HP Smart Array hardware");
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -060089static int hpsa_simple_mode;
90module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
91MODULE_PARM_DESC(hpsa_simple_mode,
92 "Use 'simple mode' rather than 'performant mode'");
Stephen M. Cameronedd16362009-12-08 14:09:11 -080093
94/* define the PCI info for the cards we can control */
95static const struct pci_device_id hpsa_pci_device_id[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -080096 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
97 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
98 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
99 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
100 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
Mike Miller163dbcd2013-09-04 15:11:10 -0500101 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
102 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
Mike Millerf8b01eb2010-02-04 08:42:45 -0600103 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
scameron@beardog.cce.hp.com9143a962011-03-07 10:44:16 -0600104 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350},
105 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351},
106 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352},
107 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353},
108 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354},
109 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355},
110 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356},
Mike Millerfe0c9612012-09-20 16:05:18 -0500111 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1921},
112 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1922},
113 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1923},
114 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1924},
Mike Millerfe0c9612012-09-20 16:05:18 -0500115 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1926},
116 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1928},
Mike Miller97b9f532013-09-04 15:05:55 -0500117 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1929},
118 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BD},
119 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BE},
120 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BF},
121 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C0},
122 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C1},
123 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C2},
124 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C3},
125 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C4},
126 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C5},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500127 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C6},
Mike Miller97b9f532013-09-04 15:05:55 -0500128 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C7},
129 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C8},
130 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C9},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500131 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CA},
132 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CB},
133 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CC},
134 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CD},
135 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CE},
Don Bracefdfa4b62015-04-23 09:35:27 -0500136 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0580},
Don Bracecbb47dc2015-07-18 11:12:54 -0500137 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0581},
138 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0582},
139 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0583},
140 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0584},
141 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0585},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600142 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
143 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
144 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
145 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
146 {PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
Mike Miller7c03b872010-12-01 11:16:07 -0600147 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
Stephen M. Cameron6798cc02010-06-16 13:51:20 -0500148 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800149 {0,}
150};
151
152MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
153
154/* board_id = Subsystem Device ID & Vendor ID
155 * product = Marketing Name for the board
156 * access = Address of the struct of function pointers
157 */
158static struct board_type products[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800159 {0x3241103C, "Smart Array P212", &SA5_access},
160 {0x3243103C, "Smart Array P410", &SA5_access},
161 {0x3245103C, "Smart Array P410i", &SA5_access},
162 {0x3247103C, "Smart Array P411", &SA5_access},
163 {0x3249103C, "Smart Array P812", &SA5_access},
Mike Miller163dbcd2013-09-04 15:11:10 -0500164 {0x324A103C, "Smart Array P712m", &SA5_access},
165 {0x324B103C, "Smart Array P711m", &SA5_access},
Stephen M. Cameron7d2cce52014-11-14 17:26:38 -0600166 {0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
Mike Millerfe0c9612012-09-20 16:05:18 -0500167 {0x3350103C, "Smart Array P222", &SA5_access},
168 {0x3351103C, "Smart Array P420", &SA5_access},
169 {0x3352103C, "Smart Array P421", &SA5_access},
170 {0x3353103C, "Smart Array P822", &SA5_access},
171 {0x3354103C, "Smart Array P420i", &SA5_access},
172 {0x3355103C, "Smart Array P220i", &SA5_access},
173 {0x3356103C, "Smart Array P721m", &SA5_access},
Mike Miller1fd6c8e2013-09-04 15:08:29 -0500174 {0x1921103C, "Smart Array P830i", &SA5_access},
175 {0x1922103C, "Smart Array P430", &SA5_access},
176 {0x1923103C, "Smart Array P431", &SA5_access},
177 {0x1924103C, "Smart Array P830", &SA5_access},
178 {0x1926103C, "Smart Array P731m", &SA5_access},
179 {0x1928103C, "Smart Array P230i", &SA5_access},
180 {0x1929103C, "Smart Array P530", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600181 {0x21BD103C, "Smart Array P244br", &SA5_access},
182 {0x21BE103C, "Smart Array P741m", &SA5_access},
183 {0x21BF103C, "Smart HBA H240ar", &SA5_access},
184 {0x21C0103C, "Smart Array P440ar", &SA5_access},
Don Bracec8ae0ab2015-01-23 16:45:12 -0600185 {0x21C1103C, "Smart Array P840ar", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600186 {0x21C2103C, "Smart Array P440", &SA5_access},
187 {0x21C3103C, "Smart Array P441", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500188 {0x21C4103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600189 {0x21C5103C, "Smart Array P841", &SA5_access},
190 {0x21C6103C, "Smart HBA H244br", &SA5_access},
191 {0x21C7103C, "Smart HBA H240", &SA5_access},
192 {0x21C8103C, "Smart HBA H241", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500193 {0x21C9103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600194 {0x21CA103C, "Smart Array P246br", &SA5_access},
195 {0x21CB103C, "Smart Array P840", &SA5_access},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500196 {0x21CC103C, "Smart Array", &SA5_access},
197 {0x21CD103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600198 {0x21CE103C, "Smart HBA", &SA5_access},
Don Bracefdfa4b62015-04-23 09:35:27 -0500199 {0x05809005, "SmartHBA-SA", &SA5_access},
Don Bracecbb47dc2015-07-18 11:12:54 -0500200 {0x05819005, "SmartHBA-SA 8i", &SA5_access},
201 {0x05829005, "SmartHBA-SA 8i8e", &SA5_access},
202 {0x05839005, "SmartHBA-SA 8e", &SA5_access},
203 {0x05849005, "SmartHBA-SA 16i", &SA5_access},
204 {0x05859005, "SmartHBA-SA 4i4e", &SA5_access},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600205 {0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
206 {0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
207 {0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
208 {0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
209 {0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800210 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
211};
212
Kevin Barnettd04e62b2015-11-04 15:52:34 -0600213static struct scsi_transport_template *hpsa_sas_transport_template;
214static int hpsa_add_sas_host(struct ctlr_info *h);
215static void hpsa_delete_sas_host(struct ctlr_info *h);
216static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
217 struct hpsa_scsi_dev_t *device);
218static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device);
219static struct hpsa_scsi_dev_t
220 *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
221 struct sas_rphy *rphy);
222
Webb Scalesa58e7e52015-04-23 09:34:16 -0500223#define SCSI_CMD_BUSY ((struct scsi_cmnd *)&hpsa_cmd_busy)
224static const struct scsi_cmnd hpsa_cmd_busy;
225#define SCSI_CMD_IDLE ((struct scsi_cmnd *)&hpsa_cmd_idle)
226static const struct scsi_cmnd hpsa_cmd_idle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800227static int number_of_controllers;
228
Stephen M. Cameron10f66012010-06-16 13:51:50 -0500229static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
230static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
Don Brace42a91642014-11-14 17:26:27 -0600231static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800232
233#ifdef CONFIG_COMPAT
Don Brace42a91642014-11-14 17:26:27 -0600234static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
235 void __user *arg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800236#endif
237
238static void cmd_free(struct ctlr_info *h, struct CommandList *c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800239static struct CommandList *cmd_alloc(struct ctlr_info *h);
Webb Scales73153fe2015-04-23 09:35:04 -0500240static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c);
241static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
242 struct scsi_cmnd *scmd);
Stephen M. Camerona2dac132013-02-20 11:24:41 -0600243static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600244 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800245 int cmd_type);
Robert Elliott2c143342015-01-23 16:42:48 -0600246static void hpsa_free_cmd_pool(struct ctlr_info *h);
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600247#define VPD_PAGE (1 << 8)
Don Braceb48d9802015-11-04 15:50:13 -0600248#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800249
Jeff Garzikf2812332010-11-16 02:10:29 -0500250static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Stephen M. Camerona08a8472010-02-04 08:43:16 -0600251static void hpsa_scan_start(struct Scsi_Host *);
252static int hpsa_scan_finished(struct Scsi_Host *sh,
253 unsigned long elapsed_time);
Don Brace7c0a0222015-01-23 16:41:30 -0600254static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800255
256static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500257static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800258static int hpsa_slave_alloc(struct scsi_device *sdev);
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500259static int hpsa_slave_configure(struct scsi_device *sdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800260static void hpsa_slave_destroy(struct scsi_device *sdev);
261
Don Brace8aa60682015-11-04 15:50:01 -0600262static void hpsa_update_scsi_devices(struct ctlr_info *h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800263static int check_for_unit_attention(struct ctlr_info *h,
264 struct CommandList *c);
265static void check_ioctl_unit_attention(struct ctlr_info *h,
266 struct CommandList *c);
Don Brace303932f2010-02-04 08:42:40 -0600267/* performant mode helper functions */
268static void calc_bucket_map(int *bucket, int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -0600269 int nsgs, int min_blocks, u32 *bucket_map);
Robert Elliott105a3db2015-04-23 09:33:48 -0500270static void hpsa_free_performant_mode(struct ctlr_info *h);
271static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
Matt Gates254f7962012-05-01 11:43:06 -0500272static inline u32 next_command(struct ctlr_info *h, u8 q);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800273static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
274 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
275 u64 *cfg_offset);
276static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
277 unsigned long *memory_bar);
278static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
Don Bracebfd75462016-11-15 14:45:32 -0600279static int wait_for_device_to_become_ready(struct ctlr_info *h,
280 unsigned char lunaddr[],
281 int reply_queue);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800282static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
283 int wait_for_ready);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500284static inline void finish_cmd(struct CommandList *c);
Robert Elliottc706a792015-01-23 16:45:01 -0600285static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -0600286#define BOARD_NOT_READY 0
287#define BOARD_READY 1
Stephen M. Cameron23100dd2014-02-18 13:57:37 -0600288static void hpsa_drain_accel_commands(struct ctlr_info *h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -0600289static void hpsa_flush_cache(struct ctlr_info *h);
Scott Teelc3497752014-02-18 13:56:34 -0600290static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
291 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -0600292 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
Don Brace080ef1c2015-01-23 16:43:25 -0600293static void hpsa_command_resubmit_worker(struct work_struct *work);
Webb Scales25163bd2015-04-23 09:32:00 -0500294static u32 lockup_detected(struct ctlr_info *h);
295static int detect_controller_lockup(struct ctlr_info *h);
Scott Teelc2adae42015-11-04 15:52:16 -0600296static void hpsa_disable_rld_caching(struct ctlr_info *h);
Kevin Barnettd04e62b2015-11-04 15:52:34 -0600297static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
298 struct ReportExtendedLUNdata *buf, int bufsize);
Scott Teel83832782016-09-09 16:30:29 -0500299static bool hpsa_vpd_page_supported(struct ctlr_info *h,
300 unsigned char scsi3addr[], u8 page);
Scott Teel34592252015-11-04 15:52:09 -0600301static int hpsa_luns_changed(struct ctlr_info *h);
Don Braceba74fdc2016-04-27 17:14:17 -0500302static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
303 struct hpsa_scsi_dev_t *dev,
304 unsigned char *scsi3addr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800305
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800306static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
307{
308 unsigned long *priv = shost_priv(sdev->host);
309 return (struct ctlr_info *) *priv;
310}
311
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600312static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
313{
314 unsigned long *priv = shost_priv(sh);
315 return (struct ctlr_info *) *priv;
316}
317
Webb Scalesa58e7e52015-04-23 09:34:16 -0500318static inline bool hpsa_is_cmd_idle(struct CommandList *c)
319{
320 return c->scsi_cmd == SCSI_CMD_IDLE;
321}
322
Webb Scalesd604f532015-04-23 09:35:22 -0500323static inline bool hpsa_is_pending_event(struct CommandList *c)
324{
325 return c->abort_pending || c->reset_pending;
326}
327
Stephen Cameron9437ac42015-04-23 09:32:16 -0500328/* extract sense key, asc, and ascq from sense data. -1 means invalid. */
329static void decode_sense_data(const u8 *sense_data, int sense_data_len,
330 u8 *sense_key, u8 *asc, u8 *ascq)
331{
332 struct scsi_sense_hdr sshdr;
333 bool rc;
334
335 *sense_key = -1;
336 *asc = -1;
337 *ascq = -1;
338
339 if (sense_data_len < 1)
340 return;
341
342 rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
343 if (rc) {
344 *sense_key = sshdr.sense_key;
345 *asc = sshdr.asc;
346 *ascq = sshdr.ascq;
347 }
348}
349
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800350static int check_for_unit_attention(struct ctlr_info *h,
351 struct CommandList *c)
352{
Stephen Cameron9437ac42015-04-23 09:32:16 -0500353 u8 sense_key, asc, ascq;
354 int sense_len;
355
356 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
357 sense_len = sizeof(c->err_info->SenseInfo);
358 else
359 sense_len = c->err_info->SenseLen;
360
361 decode_sense_data(c->err_info->SenseInfo, sense_len,
362 &sense_key, &asc, &ascq);
Don Brace81c27552015-07-18 11:12:28 -0500363 if (sense_key != UNIT_ATTENTION || asc == 0xff)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800364 return 0;
365
Stephen Cameron9437ac42015-04-23 09:32:16 -0500366 switch (asc) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800367 case STATE_CHANGED:
Stephen Cameron9437ac42015-04-23 09:32:16 -0500368 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500369 "%s: a state change detected, command retried\n",
370 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800371 break;
372 case LUN_FAILED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600373 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500374 "%s: LUN failure detected\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800375 break;
376 case REPORT_LUNS_CHANGED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600377 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500378 "%s: report LUN data changed\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800379 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600380 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
381 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800382 */
383 break;
384 case POWER_OR_RESET:
Robert Elliott2946e822015-04-23 09:35:09 -0500385 dev_warn(&h->pdev->dev,
386 "%s: a power on or device reset detected\n",
387 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800388 break;
389 case UNIT_ATTENTION_CLEARED:
Robert Elliott2946e822015-04-23 09:35:09 -0500390 dev_warn(&h->pdev->dev,
391 "%s: unit attention cleared by another initiator\n",
392 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800393 break;
394 default:
Robert Elliott2946e822015-04-23 09:35:09 -0500395 dev_warn(&h->pdev->dev,
396 "%s: unknown unit attention detected\n",
397 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800398 break;
399 }
400 return 1;
401}
402
Matt Bondurant852af202012-05-01 11:42:35 -0500403static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
404{
405 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
406 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
407 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
408 return 0;
409 dev_warn(&h->pdev->dev, HPSA "device busy");
410 return 1;
411}
412
Stephen Camerone985c582015-04-23 09:32:22 -0500413static u32 lockup_detected(struct ctlr_info *h);
414static ssize_t host_show_lockup_detected(struct device *dev,
415 struct device_attribute *attr, char *buf)
416{
417 int ld;
418 struct ctlr_info *h;
419 struct Scsi_Host *shost = class_to_shost(dev);
420
421 h = shost_to_hba(shost);
422 ld = lockup_detected(h);
423
424 return sprintf(buf, "ld=%d\n", ld);
425}
426
Scott Teelda0697b2014-02-18 13:57:00 -0600427static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
428 struct device_attribute *attr,
429 const char *buf, size_t count)
430{
431 int status, len;
432 struct ctlr_info *h;
433 struct Scsi_Host *shost = class_to_shost(dev);
434 char tmpbuf[10];
435
436 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
437 return -EACCES;
438 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
439 strncpy(tmpbuf, buf, len);
440 tmpbuf[len] = '\0';
441 if (sscanf(tmpbuf, "%d", &status) != 1)
442 return -EINVAL;
443 h = shost_to_hba(shost);
444 h->acciopath_status = !!status;
445 dev_warn(&h->pdev->dev,
446 "hpsa: HP SSD Smart Path %s via sysfs update.\n",
447 h->acciopath_status ? "enabled" : "disabled");
448 return count;
449}
450
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600451static ssize_t host_store_raid_offload_debug(struct device *dev,
452 struct device_attribute *attr,
453 const char *buf, size_t count)
454{
455 int debug_level, len;
456 struct ctlr_info *h;
457 struct Scsi_Host *shost = class_to_shost(dev);
458 char tmpbuf[10];
459
460 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
461 return -EACCES;
462 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
463 strncpy(tmpbuf, buf, len);
464 tmpbuf[len] = '\0';
465 if (sscanf(tmpbuf, "%d", &debug_level) != 1)
466 return -EINVAL;
467 if (debug_level < 0)
468 debug_level = 0;
469 h = shost_to_hba(shost);
470 h->raid_offload_debug = debug_level;
471 dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
472 h->raid_offload_debug);
473 return count;
474}
475
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800476static ssize_t host_store_rescan(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
479{
480 struct ctlr_info *h;
481 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600482 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600483 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800484 return count;
485}
486
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500487static ssize_t host_show_firmware_revision(struct device *dev,
488 struct device_attribute *attr, char *buf)
489{
490 struct ctlr_info *h;
491 struct Scsi_Host *shost = class_to_shost(dev);
492 unsigned char *fwrev;
493
494 h = shost_to_hba(shost);
495 if (!h->hba_inquiry_data)
496 return 0;
497 fwrev = &h->hba_inquiry_data[32];
498 return snprintf(buf, 20, "%c%c%c%c\n",
499 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
500}
501
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600502static ssize_t host_show_commands_outstanding(struct device *dev,
503 struct device_attribute *attr, char *buf)
504{
505 struct Scsi_Host *shost = class_to_shost(dev);
506 struct ctlr_info *h = shost_to_hba(shost);
507
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600508 return snprintf(buf, 20, "%d\n",
509 atomic_read(&h->commands_outstanding));
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600510}
511
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600512static ssize_t host_show_transport_mode(struct device *dev,
513 struct device_attribute *attr, char *buf)
514{
515 struct ctlr_info *h;
516 struct Scsi_Host *shost = class_to_shost(dev);
517
518 h = shost_to_hba(shost);
519 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600520 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600521 "performant" : "simple");
522}
523
Scott Teelda0697b2014-02-18 13:57:00 -0600524static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
525 struct device_attribute *attr, char *buf)
526{
527 struct ctlr_info *h;
528 struct Scsi_Host *shost = class_to_shost(dev);
529
530 h = shost_to_hba(shost);
531 return snprintf(buf, 30, "HP SSD Smart Path %s\n",
532 (h->acciopath_status == 1) ? "enabled" : "disabled");
533}
534
Stephen M. Cameron46380782011-05-03 15:00:01 -0500535/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600536static u32 unresettable_controller[] = {
537 0x324a103C, /* Smart Array P712m */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500538 0x324b103C, /* Smart Array P711m */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600539 0x3223103C, /* Smart Array P800 */
540 0x3234103C, /* Smart Array P400 */
541 0x3235103C, /* Smart Array P400i */
542 0x3211103C, /* Smart Array E200i */
543 0x3212103C, /* Smart Array E200 */
544 0x3213103C, /* Smart Array E200i */
545 0x3214103C, /* Smart Array E200i */
546 0x3215103C, /* Smart Array E200i */
547 0x3237103C, /* Smart Array E500 */
548 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100549 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600550 0x409C0E11, /* Smart Array 6400 */
551 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100552 0x40700E11, /* Smart Array 5300 */
553 0x40820E11, /* Smart Array 532 */
554 0x40830E11, /* Smart Array 5312 */
555 0x409A0E11, /* Smart Array 641 */
556 0x409B0E11, /* Smart Array 642 */
557 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600558};
559
Stephen M. Cameron46380782011-05-03 15:00:01 -0500560/* List of controllers which cannot even be soft reset */
561static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100562 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100563 0x40700E11, /* Smart Array 5300 */
564 0x40820E11, /* Smart Array 532 */
565 0x40830E11, /* Smart Array 5312 */
566 0x409A0E11, /* Smart Array 641 */
567 0x409B0E11, /* Smart Array 642 */
568 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500569 /* Exclude 640x boards. These are two pci devices in one slot
570 * which share a battery backed cache module. One controls the
571 * cache, the other accesses the cache through the one that controls
572 * it. If we reset the one controlling the cache, the other will
573 * likely not be happy. Just forbid resetting this conjoined mess.
574 * The 640x isn't really supported by hpsa anyway.
575 */
576 0x409C0E11, /* Smart Array 6400 */
577 0x409D0E11, /* Smart Array 6400 EM */
578};
579
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500580static u32 needs_abort_tags_swizzled[] = {
581 0x323D103C, /* Smart Array P700m */
582 0x324a103C, /* Smart Array P712m */
583 0x324b103C, /* SmartArray P711m */
584};
585
586static int board_id_in_array(u32 a[], int nelems, u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600587{
588 int i;
589
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500590 for (i = 0; i < nelems; i++)
591 if (a[i] == board_id)
592 return 1;
593 return 0;
594}
595
596static int ctlr_is_hard_resettable(u32 board_id)
597{
598 return !board_id_in_array(unresettable_controller,
599 ARRAY_SIZE(unresettable_controller), board_id);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600600}
601
Stephen M. Cameron46380782011-05-03 15:00:01 -0500602static int ctlr_is_soft_resettable(u32 board_id)
603{
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500604 return !board_id_in_array(soft_unresettable_controller,
605 ARRAY_SIZE(soft_unresettable_controller), board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500606}
607
608static int ctlr_is_resettable(u32 board_id)
609{
610 return ctlr_is_hard_resettable(board_id) ||
611 ctlr_is_soft_resettable(board_id);
612}
613
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500614static int ctlr_needs_abort_tags_swizzled(u32 board_id)
615{
616 return board_id_in_array(needs_abort_tags_swizzled,
617 ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
618}
619
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600620static ssize_t host_show_resettable(struct device *dev,
621 struct device_attribute *attr, char *buf)
622{
623 struct ctlr_info *h;
624 struct Scsi_Host *shost = class_to_shost(dev);
625
626 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500627 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600628}
629
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800630static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
631{
632 return (scsi3addr[3] & 0xC0) == 0x40;
633}
634
Robert Elliottf2ef0ce2015-01-23 16:41:35 -0600635static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
Don Brace7c59a0d2015-11-04 15:52:22 -0600636 "1(+0)ADM", "UNKNOWN", "PHYS DRV"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800637};
Scott Teel6b80b182014-02-18 13:56:55 -0600638#define HPSA_RAID_0 0
639#define HPSA_RAID_4 1
640#define HPSA_RAID_1 2 /* also used for RAID 10 */
641#define HPSA_RAID_5 3 /* also used for RAID 50 */
642#define HPSA_RAID_51 4
643#define HPSA_RAID_6 5 /* also used for RAID 60 */
644#define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
Don Brace7c59a0d2015-11-04 15:52:22 -0600645#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 2)
646#define PHYSICAL_DRIVE (ARRAY_SIZE(raid_label) - 1)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800647
Kevin Barnettf3f01732015-11-04 15:51:33 -0600648static inline bool is_logical_device(struct hpsa_scsi_dev_t *device)
649{
650 return !device->physical_device;
651}
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800652
653static ssize_t raid_level_show(struct device *dev,
654 struct device_attribute *attr, char *buf)
655{
656 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600657 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800658 struct ctlr_info *h;
659 struct scsi_device *sdev;
660 struct hpsa_scsi_dev_t *hdev;
661 unsigned long flags;
662
663 sdev = to_scsi_device(dev);
664 h = sdev_to_hba(sdev);
665 spin_lock_irqsave(&h->lock, flags);
666 hdev = sdev->hostdata;
667 if (!hdev) {
668 spin_unlock_irqrestore(&h->lock, flags);
669 return -ENODEV;
670 }
671
672 /* Is this even a logical drive? */
Kevin Barnettf3f01732015-11-04 15:51:33 -0600673 if (!is_logical_device(hdev)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800674 spin_unlock_irqrestore(&h->lock, flags);
675 l = snprintf(buf, PAGE_SIZE, "N/A\n");
676 return l;
677 }
678
679 rlevel = hdev->raid_level;
680 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600681 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800682 rlevel = RAID_UNKNOWN;
683 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
684 return l;
685}
686
687static ssize_t lunid_show(struct device *dev,
688 struct device_attribute *attr, char *buf)
689{
690 struct ctlr_info *h;
691 struct scsi_device *sdev;
692 struct hpsa_scsi_dev_t *hdev;
693 unsigned long flags;
694 unsigned char lunid[8];
695
696 sdev = to_scsi_device(dev);
697 h = sdev_to_hba(sdev);
698 spin_lock_irqsave(&h->lock, flags);
699 hdev = sdev->hostdata;
700 if (!hdev) {
701 spin_unlock_irqrestore(&h->lock, flags);
702 return -ENODEV;
703 }
704 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
705 spin_unlock_irqrestore(&h->lock, flags);
706 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
707 lunid[0], lunid[1], lunid[2], lunid[3],
708 lunid[4], lunid[5], lunid[6], lunid[7]);
709}
710
711static ssize_t unique_id_show(struct device *dev,
712 struct device_attribute *attr, char *buf)
713{
714 struct ctlr_info *h;
715 struct scsi_device *sdev;
716 struct hpsa_scsi_dev_t *hdev;
717 unsigned long flags;
718 unsigned char sn[16];
719
720 sdev = to_scsi_device(dev);
721 h = sdev_to_hba(sdev);
722 spin_lock_irqsave(&h->lock, flags);
723 hdev = sdev->hostdata;
724 if (!hdev) {
725 spin_unlock_irqrestore(&h->lock, flags);
726 return -ENODEV;
727 }
728 memcpy(sn, hdev->device_id, sizeof(sn));
729 spin_unlock_irqrestore(&h->lock, flags);
730 return snprintf(buf, 16 * 2 + 2,
731 "%02X%02X%02X%02X%02X%02X%02X%02X"
732 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
733 sn[0], sn[1], sn[2], sn[3],
734 sn[4], sn[5], sn[6], sn[7],
735 sn[8], sn[9], sn[10], sn[11],
736 sn[12], sn[13], sn[14], sn[15]);
737}
738
Joseph T Handzikded1be42016-04-27 17:13:33 -0500739static ssize_t sas_address_show(struct device *dev,
740 struct device_attribute *attr, char *buf)
741{
742 struct ctlr_info *h;
743 struct scsi_device *sdev;
744 struct hpsa_scsi_dev_t *hdev;
745 unsigned long flags;
746 u64 sas_address;
747
748 sdev = to_scsi_device(dev);
749 h = sdev_to_hba(sdev);
750 spin_lock_irqsave(&h->lock, flags);
751 hdev = sdev->hostdata;
752 if (!hdev || is_logical_device(hdev) || !hdev->expose_device) {
753 spin_unlock_irqrestore(&h->lock, flags);
754 return -ENODEV;
755 }
756 sas_address = hdev->sas_address;
757 spin_unlock_irqrestore(&h->lock, flags);
758
759 return snprintf(buf, PAGE_SIZE, "0x%016llx\n", sas_address);
760}
761
Scott Teelc1988682014-02-18 13:55:54 -0600762static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
763 struct device_attribute *attr, char *buf)
764{
765 struct ctlr_info *h;
766 struct scsi_device *sdev;
767 struct hpsa_scsi_dev_t *hdev;
768 unsigned long flags;
769 int offload_enabled;
770
771 sdev = to_scsi_device(dev);
772 h = sdev_to_hba(sdev);
773 spin_lock_irqsave(&h->lock, flags);
774 hdev = sdev->hostdata;
775 if (!hdev) {
776 spin_unlock_irqrestore(&h->lock, flags);
777 return -ENODEV;
778 }
779 offload_enabled = hdev->offload_enabled;
780 spin_unlock_irqrestore(&h->lock, flags);
781 return snprintf(buf, 20, "%d\n", offload_enabled);
782}
783
Joe Handzik8270b862015-07-18 11:12:43 -0500784#define MAX_PATHS 8
Joe Handzik8270b862015-07-18 11:12:43 -0500785static ssize_t path_info_show(struct device *dev,
786 struct device_attribute *attr, char *buf)
787{
788 struct ctlr_info *h;
789 struct scsi_device *sdev;
790 struct hpsa_scsi_dev_t *hdev;
791 unsigned long flags;
792 int i;
793 int output_len = 0;
794 u8 box;
795 u8 bay;
796 u8 path_map_index = 0;
797 char *active;
798 unsigned char phys_connector[2];
Joe Handzik8270b862015-07-18 11:12:43 -0500799
Joe Handzik8270b862015-07-18 11:12:43 -0500800 sdev = to_scsi_device(dev);
801 h = sdev_to_hba(sdev);
802 spin_lock_irqsave(&h->devlock, flags);
803 hdev = sdev->hostdata;
804 if (!hdev) {
805 spin_unlock_irqrestore(&h->devlock, flags);
806 return -ENODEV;
807 }
808
809 bay = hdev->bay;
810 for (i = 0; i < MAX_PATHS; i++) {
811 path_map_index = 1<<i;
812 if (i == hdev->active_path_index)
813 active = "Active";
814 else if (hdev->path_map & path_map_index)
815 active = "Inactive";
816 else
817 continue;
818
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600819 output_len += scnprintf(buf + output_len,
820 PAGE_SIZE - output_len,
821 "[%d:%d:%d:%d] %20.20s ",
Joe Handzik8270b862015-07-18 11:12:43 -0500822 h->scsi_host->host_no,
823 hdev->bus, hdev->target, hdev->lun,
824 scsi_device_type(hdev->devtype));
825
Don Bracecca8f132015-12-22 10:36:48 -0600826 if (hdev->devtype == TYPE_RAID || is_logical_device(hdev)) {
Don Brace2708f292015-12-22 10:36:36 -0600827 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600828 PAGE_SIZE - output_len,
829 "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500830 continue;
831 }
832
833 box = hdev->box[i];
834 memcpy(&phys_connector, &hdev->phys_connector[i],
835 sizeof(phys_connector));
836 if (phys_connector[0] < '0')
837 phys_connector[0] = '0';
838 if (phys_connector[1] < '0')
839 phys_connector[1] = '0';
Don Bracecca8f132015-12-22 10:36:48 -0600840 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600841 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500842 "PORT: %.2s ",
843 phys_connector);
Don Braceaf15ed32016-02-23 15:16:15 -0600844 if ((hdev->devtype == TYPE_DISK || hdev->devtype == TYPE_ZBC) &&
845 hdev->expose_device) {
Joe Handzik8270b862015-07-18 11:12:43 -0500846 if (box == 0 || box == 0xFF) {
Don Brace2708f292015-12-22 10:36:36 -0600847 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600848 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500849 "BAY: %hhu %s\n",
850 bay, active);
851 } else {
Don Brace2708f292015-12-22 10:36:36 -0600852 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600853 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500854 "BOX: %hhu BAY: %hhu %s\n",
855 box, bay, active);
856 }
857 } else if (box != 0 && box != 0xFF) {
Don Brace2708f292015-12-22 10:36:36 -0600858 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600859 PAGE_SIZE - output_len, "BOX: %hhu %s\n",
Joe Handzik8270b862015-07-18 11:12:43 -0500860 box, active);
861 } else
Don Brace2708f292015-12-22 10:36:36 -0600862 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600863 PAGE_SIZE - output_len, "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500864 }
865
866 spin_unlock_irqrestore(&h->devlock, flags);
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600867 return output_len;
Joe Handzik8270b862015-07-18 11:12:43 -0500868}
869
Hannes Reinecke16961202016-11-18 08:32:49 +0100870static ssize_t host_show_ctlr_num(struct device *dev,
871 struct device_attribute *attr, char *buf)
872{
873 struct ctlr_info *h;
874 struct Scsi_Host *shost = class_to_shost(dev);
875
876 h = shost_to_hba(shost);
877 return snprintf(buf, 20, "%d\n", h->ctlr);
878}
879
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600880static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
881static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
882static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
883static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Joseph T Handzikded1be42016-04-27 17:13:33 -0500884static DEVICE_ATTR(sas_address, S_IRUGO, sas_address_show, NULL);
Scott Teelc1988682014-02-18 13:55:54 -0600885static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
886 host_show_hp_ssd_smart_path_enabled, NULL);
Joe Handzik8270b862015-07-18 11:12:43 -0500887static DEVICE_ATTR(path_info, S_IRUGO, path_info_show, NULL);
Scott Teelda0697b2014-02-18 13:57:00 -0600888static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
889 host_show_hp_ssd_smart_path_status,
890 host_store_hp_ssd_smart_path_status);
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600891static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
892 host_store_raid_offload_debug);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600893static DEVICE_ATTR(firmware_revision, S_IRUGO,
894 host_show_firmware_revision, NULL);
895static DEVICE_ATTR(commands_outstanding, S_IRUGO,
896 host_show_commands_outstanding, NULL);
897static DEVICE_ATTR(transport_mode, S_IRUGO,
898 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600899static DEVICE_ATTR(resettable, S_IRUGO,
900 host_show_resettable, NULL);
Stephen Camerone985c582015-04-23 09:32:22 -0500901static DEVICE_ATTR(lockup_detected, S_IRUGO,
902 host_show_lockup_detected, NULL);
Hannes Reinecke16961202016-11-18 08:32:49 +0100903static DEVICE_ATTR(ctlr_num, S_IRUGO,
904 host_show_ctlr_num, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600905
906static struct device_attribute *hpsa_sdev_attrs[] = {
907 &dev_attr_raid_level,
908 &dev_attr_lunid,
909 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600910 &dev_attr_hp_ssd_smart_path_enabled,
Joe Handzik8270b862015-07-18 11:12:43 -0500911 &dev_attr_path_info,
Joseph T Handzikded1be42016-04-27 17:13:33 -0500912 &dev_attr_sas_address,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600913 NULL,
914};
915
916static struct device_attribute *hpsa_shost_attrs[] = {
917 &dev_attr_rescan,
918 &dev_attr_firmware_revision,
919 &dev_attr_commands_outstanding,
920 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600921 &dev_attr_resettable,
Scott Teelda0697b2014-02-18 13:57:00 -0600922 &dev_attr_hp_ssd_smart_path_status,
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600923 &dev_attr_raid_offload_debug,
Tomas Henzlfb53c432015-11-06 16:24:09 +0100924 &dev_attr_lockup_detected,
Hannes Reinecke16961202016-11-18 08:32:49 +0100925 &dev_attr_ctlr_num,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600926 NULL,
927};
928
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500929#define HPSA_NRESERVED_CMDS (HPSA_CMDS_RESERVED_FOR_ABORTS + \
930 HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
931
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600932static struct scsi_host_template hpsa_driver_template = {
933 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600934 .name = HPSA,
935 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600936 .queuecommand = hpsa_scsi_queue_command,
937 .scan_start = hpsa_scan_start,
938 .scan_finished = hpsa_scan_finished,
Don Brace7c0a0222015-01-23 16:41:30 -0600939 .change_queue_depth = hpsa_change_queue_depth,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600940 .this_id = -1,
941 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500942 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600943 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
944 .ioctl = hpsa_ioctl,
945 .slave_alloc = hpsa_slave_alloc,
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500946 .slave_configure = hpsa_slave_configure,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600947 .slave_destroy = hpsa_slave_destroy,
948#ifdef CONFIG_COMPAT
949 .compat_ioctl = hpsa_compat_ioctl,
950#endif
951 .sdev_attrs = hpsa_sdev_attrs,
952 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500953 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400954 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600955};
956
Matt Gates254f7962012-05-01 11:43:06 -0500957static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600958{
959 u32 a;
Stephen M. Cameron072b0512014-05-29 10:53:07 -0500960 struct reply_queue_buffer *rq = &h->reply_queue[q];
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600961
Matt Gatese1f7de02014-02-18 13:55:17 -0600962 if (h->transMethod & CFGTBL_Trans_io_accel1)
963 return h->access.command_completed(h, q);
964
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600965 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500966 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600967
Matt Gates254f7962012-05-01 11:43:06 -0500968 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
969 a = rq->head[rq->current_entry];
970 rq->current_entry++;
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600971 atomic_dec(&h->commands_outstanding);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600972 } else {
973 a = FIFO_EMPTY;
974 }
975 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500976 if (rq->current_entry == h->max_commands) {
977 rq->current_entry = 0;
978 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600979 }
980 return a;
981}
982
Scott Teelc3497752014-02-18 13:56:34 -0600983/*
984 * There are some special bits in the bus address of the
985 * command that we have to set for the controller to know
986 * how to process the command:
987 *
988 * Normal performant mode:
989 * bit 0: 1 means performant mode, 0 means simple mode.
990 * bits 1-3 = block fetch table entry
991 * bits 4-6 = command type (== 0)
992 *
993 * ioaccel1 mode:
994 * bit 0 = "performant mode" bit.
995 * bits 1-3 = block fetch table entry
996 * bits 4-6 = command type (== 110)
997 * (command type is needed because ioaccel1 mode
998 * commands are submitted through the same register as normal
999 * mode commands, so this is how the controller knows whether
1000 * the command is normal mode or ioaccel1 mode.)
1001 *
1002 * ioaccel2 mode:
1003 * bit 0 = "performant mode" bit.
1004 * bits 1-4 = block fetch table entry (note extra bit)
1005 * bits 4-6 = not needed, because ioaccel2 mode has
1006 * a separate special register for submitting commands.
1007 */
1008
Webb Scales25163bd2015-04-23 09:32:00 -05001009/*
1010 * set_performant_mode: Modify the tag for cciss performant
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001011 * set bit 0 for pull model, bits 3-1 for block fetch
1012 * register number
1013 */
Webb Scales25163bd2015-04-23 09:32:00 -05001014#define DEFAULT_REPLY_QUEUE (-1)
1015static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
1016 int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001017{
Matt Gates254f7962012-05-01 11:43:06 -05001018 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001019 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08001020 if (unlikely(!h->msix_vectors))
Webb Scales25163bd2015-04-23 09:32:00 -05001021 return;
1022 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
Matt Gates254f7962012-05-01 11:43:06 -05001023 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +02001024 raw_smp_processor_id() % h->nreply_queues;
Webb Scales25163bd2015-04-23 09:32:00 -05001025 else
1026 c->Header.ReplyQueue = reply_queue % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -05001027 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001028}
1029
Scott Teelc3497752014-02-18 13:56:34 -06001030static void set_ioaccel1_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05001031 struct CommandList *c,
1032 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -06001033{
1034 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
1035
Webb Scales25163bd2015-04-23 09:32:00 -05001036 /*
1037 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -06001038 * processor. This seems to give the best I/O throughput.
1039 */
Webb Scales25163bd2015-04-23 09:32:00 -05001040 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1041 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
1042 else
1043 cp->ReplyQueue = reply_queue % h->nreply_queues;
1044 /*
1045 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -06001046 * - performant mode bit (bit 0)
1047 * - pull count (bits 1-3)
1048 * - command type (bits 4-6)
1049 */
1050 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
1051 IOACCEL1_BUSADDR_CMDTYPE;
1052}
1053
Stephen Cameron8be986c2015-04-23 09:34:06 -05001054static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
1055 struct CommandList *c,
1056 int reply_queue)
1057{
1058 struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
1059 &h->ioaccel2_cmd_pool[c->cmdindex];
1060
1061 /* Tell the controller to post the reply to the queue for this
1062 * processor. This seems to give the best I/O throughput.
1063 */
1064 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1065 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1066 else
1067 cp->reply_queue = reply_queue % h->nreply_queues;
1068 /* Set the bits in the address sent down to include:
1069 * - performant mode bit not used in ioaccel mode 2
1070 * - pull count (bits 0-3)
1071 * - command type isn't needed for ioaccel2
1072 */
1073 c->busaddr |= h->ioaccel2_blockFetchTable[0];
1074}
1075
Scott Teelc3497752014-02-18 13:56:34 -06001076static void set_ioaccel2_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05001077 struct CommandList *c,
1078 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -06001079{
1080 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
1081
Webb Scales25163bd2015-04-23 09:32:00 -05001082 /*
1083 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -06001084 * processor. This seems to give the best I/O throughput.
1085 */
Webb Scales25163bd2015-04-23 09:32:00 -05001086 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1087 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1088 else
1089 cp->reply_queue = reply_queue % h->nreply_queues;
1090 /*
1091 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -06001092 * - performant mode bit not used in ioaccel mode 2
1093 * - pull count (bits 0-3)
1094 * - command type isn't needed for ioaccel2
1095 */
1096 c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
1097}
1098
Stephen M. Camerone85c5972012-05-01 11:43:42 -05001099static int is_firmware_flash_cmd(u8 *cdb)
1100{
1101 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
1102}
1103
1104/*
1105 * During firmware flash, the heartbeat register may not update as frequently
1106 * as it should. So we dial down lockup detection during firmware flash. and
1107 * dial it back up when firmware flash completes.
1108 */
1109#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
1110#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
1111static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
1112 struct CommandList *c)
1113{
1114 if (!is_firmware_flash_cmd(c->Request.CDB))
1115 return;
1116 atomic_inc(&h->firmware_flash_in_progress);
1117 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
1118}
1119
1120static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
1121 struct CommandList *c)
1122{
1123 if (is_firmware_flash_cmd(c->Request.CDB) &&
1124 atomic_dec_and_test(&h->firmware_flash_in_progress))
1125 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
1126}
1127
Webb Scales25163bd2015-04-23 09:32:00 -05001128static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
1129 struct CommandList *c, int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001130{
Stephen Cameronc05e8862015-01-23 16:44:40 -06001131 dial_down_lockup_detection_during_fw_flash(h, c);
1132 atomic_inc(&h->commands_outstanding);
Scott Teelc3497752014-02-18 13:56:34 -06001133 switch (c->cmd_type) {
1134 case CMD_IOACCEL1:
Webb Scales25163bd2015-04-23 09:32:00 -05001135 set_ioaccel1_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001136 writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
Scott Teelc3497752014-02-18 13:56:34 -06001137 break;
1138 case CMD_IOACCEL2:
Webb Scales25163bd2015-04-23 09:32:00 -05001139 set_ioaccel2_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001140 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
Scott Teelc3497752014-02-18 13:56:34 -06001141 break;
Stephen Cameron8be986c2015-04-23 09:34:06 -05001142 case IOACCEL2_TMF:
1143 set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
1144 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
1145 break;
Scott Teelc3497752014-02-18 13:56:34 -06001146 default:
Webb Scales25163bd2015-04-23 09:32:00 -05001147 set_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001148 h->access.submit_command(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06001149 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001150}
1151
Webb Scalesa58e7e52015-04-23 09:34:16 -05001152static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
Webb Scales25163bd2015-04-23 09:32:00 -05001153{
Webb Scalesd604f532015-04-23 09:35:22 -05001154 if (unlikely(hpsa_is_pending_event(c)))
Webb Scalesa58e7e52015-04-23 09:34:16 -05001155 return finish_cmd(c);
1156
Webb Scales25163bd2015-04-23 09:32:00 -05001157 __enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
1158}
1159
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001160static inline int is_hba_lunid(unsigned char scsi3addr[])
1161{
1162 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
1163}
1164
1165static inline int is_scsi_rev_5(struct ctlr_info *h)
1166{
1167 if (!h->hba_inquiry_data)
1168 return 0;
1169 if ((h->hba_inquiry_data[2] & 0x07) == 5)
1170 return 1;
1171 return 0;
1172}
1173
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001174static int hpsa_find_target_lun(struct ctlr_info *h,
1175 unsigned char scsi3addr[], int bus, int *target, int *lun)
1176{
1177 /* finds an unused bus, target, lun for a new physical device
1178 * assumes h->devlock is held
1179 */
1180 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -05001181 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001182
Akinobu Mita263d9402012-01-21 00:15:27 +09001183 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001184
1185 for (i = 0; i < h->ndevices; i++) {
1186 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +09001187 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001188 }
1189
Akinobu Mita263d9402012-01-21 00:15:27 +09001190 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
1191 if (i < HPSA_MAX_DEVICES) {
1192 /* *bus = 1; */
1193 *target = i;
1194 *lun = 0;
1195 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001196 }
1197 return !found;
1198}
1199
Don Brace1d33d852015-11-04 15:50:31 -06001200static void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
Webb Scales0d96ef52015-04-23 09:31:55 -05001201 struct hpsa_scsi_dev_t *dev, char *description)
1202{
Don Brace7c59a0d2015-11-04 15:52:22 -06001203#define LABEL_SIZE 25
1204 char label[LABEL_SIZE];
1205
Don Brace9975ec92015-11-04 15:50:25 -06001206 if (h == NULL || h->pdev == NULL || h->scsi_host == NULL)
1207 return;
1208
Don Brace7c59a0d2015-11-04 15:52:22 -06001209 switch (dev->devtype) {
1210 case TYPE_RAID:
1211 snprintf(label, LABEL_SIZE, "controller");
1212 break;
1213 case TYPE_ENCLOSURE:
1214 snprintf(label, LABEL_SIZE, "enclosure");
1215 break;
1216 case TYPE_DISK:
Don Braceaf15ed32016-02-23 15:16:15 -06001217 case TYPE_ZBC:
Don Brace7c59a0d2015-11-04 15:52:22 -06001218 if (dev->external)
1219 snprintf(label, LABEL_SIZE, "external");
1220 else if (!is_logical_dev_addr_mode(dev->scsi3addr))
1221 snprintf(label, LABEL_SIZE, "%s",
1222 raid_label[PHYSICAL_DRIVE]);
1223 else
1224 snprintf(label, LABEL_SIZE, "RAID-%s",
1225 dev->raid_level > RAID_UNKNOWN ? "?" :
1226 raid_label[dev->raid_level]);
1227 break;
1228 case TYPE_ROM:
1229 snprintf(label, LABEL_SIZE, "rom");
1230 break;
1231 case TYPE_TAPE:
1232 snprintf(label, LABEL_SIZE, "tape");
1233 break;
1234 case TYPE_MEDIUM_CHANGER:
1235 snprintf(label, LABEL_SIZE, "changer");
1236 break;
1237 default:
1238 snprintf(label, LABEL_SIZE, "UNKNOWN");
1239 break;
1240 }
1241
Webb Scales0d96ef52015-04-23 09:31:55 -05001242 dev_printk(level, &h->pdev->dev,
Don Brace7c59a0d2015-11-04 15:52:22 -06001243 "scsi %d:%d:%d:%d: %s %s %.8s %.16s %s SSDSmartPathCap%c En%c Exp=%d\n",
Webb Scales0d96ef52015-04-23 09:31:55 -05001244 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
1245 description,
1246 scsi_device_type(dev->devtype),
1247 dev->vendor,
1248 dev->model,
Don Brace7c59a0d2015-11-04 15:52:22 -06001249 label,
Webb Scales0d96ef52015-04-23 09:31:55 -05001250 dev->offload_config ? '+' : '-',
1251 dev->offload_enabled ? '+' : '-',
Kevin Barnett2a168202015-11-04 15:51:21 -06001252 dev->expose_device);
Webb Scales0d96ef52015-04-23 09:31:55 -05001253}
1254
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001255/* Add an entry into h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001256static int hpsa_scsi_add_entry(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001257 struct hpsa_scsi_dev_t *device,
1258 struct hpsa_scsi_dev_t *added[], int *nadded)
1259{
1260 /* assumes h->devlock is held */
1261 int n = h->ndevices;
1262 int i;
1263 unsigned char addr1[8], addr2[8];
1264 struct hpsa_scsi_dev_t *sd;
1265
Scott Teelcfe5bad2011-10-26 16:21:07 -05001266 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001267 dev_err(&h->pdev->dev, "too many devices, some will be "
1268 "inaccessible.\n");
1269 return -1;
1270 }
1271
1272 /* physical devices do not have lun or target assigned until now. */
1273 if (device->lun != -1)
1274 /* Logical device, lun is already assigned. */
1275 goto lun_assigned;
1276
1277 /* If this device a non-zero lun of a multi-lun device
1278 * byte 4 of the 8-byte LUN addr will contain the logical
Don Brace2b08b3e2015-01-23 16:41:09 -06001279 * unit no, zero otherwise.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001280 */
1281 if (device->scsi3addr[4] == 0) {
1282 /* This is not a non-zero lun of a multi-lun device */
1283 if (hpsa_find_target_lun(h, device->scsi3addr,
1284 device->bus, &device->target, &device->lun) != 0)
1285 return -1;
1286 goto lun_assigned;
1287 }
1288
1289 /* This is a non-zero lun of a multi-lun device.
1290 * Search through our list and find the device which
shane.seymour9a4178b2015-07-18 11:13:09 -05001291 * has the same 8 byte LUN address, excepting byte 4 and 5.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001292 * Assign the same bus and target for this new LUN.
1293 * Use the logical unit number from the firmware.
1294 */
1295 memcpy(addr1, device->scsi3addr, 8);
1296 addr1[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001297 addr1[5] = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001298 for (i = 0; i < n; i++) {
1299 sd = h->dev[i];
1300 memcpy(addr2, sd->scsi3addr, 8);
1301 addr2[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001302 addr2[5] = 0;
1303 /* differ only in byte 4 and 5? */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001304 if (memcmp(addr1, addr2, 8) == 0) {
1305 device->bus = sd->bus;
1306 device->target = sd->target;
1307 device->lun = device->scsi3addr[4];
1308 break;
1309 }
1310 }
1311 if (device->lun == -1) {
1312 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1313 " suspect firmware bug or unsupported hardware "
1314 "configuration.\n");
1315 return -1;
1316 }
1317
1318lun_assigned:
1319
1320 h->dev[n] = device;
1321 h->ndevices++;
1322 added[*nadded] = device;
1323 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001324 hpsa_show_dev_msg(KERN_INFO, h, device,
Kevin Barnett2a168202015-11-04 15:51:21 -06001325 device->expose_device ? "added" : "masked");
Robert Elliotta473d862015-04-23 09:32:54 -05001326 device->offload_to_be_enabled = device->offload_enabled;
1327 device->offload_enabled = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001328 return 0;
1329}
1330
Scott Teelbd9244f2012-01-19 14:01:30 -06001331/* Update an entry in h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001332static void hpsa_scsi_update_entry(struct ctlr_info *h,
Scott Teelbd9244f2012-01-19 14:01:30 -06001333 int entry, struct hpsa_scsi_dev_t *new_entry)
1334{
Robert Elliotta473d862015-04-23 09:32:54 -05001335 int offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001336 /* assumes h->devlock is held */
1337 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1338
1339 /* Raid level changed. */
1340 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001341
Don Brace03383732015-01-23 16:43:30 -06001342 /* Raid offload parameters changed. Careful about the ordering. */
1343 if (new_entry->offload_config && new_entry->offload_enabled) {
1344 /*
1345 * if drive is newly offload_enabled, we want to copy the
1346 * raid map data first. If previously offload_enabled and
1347 * offload_config were set, raid map data had better be
1348 * the same as it was before. if raid map data is changed
1349 * then it had better be the case that
1350 * h->dev[entry]->offload_enabled is currently 0.
1351 */
1352 h->dev[entry]->raid_map = new_entry->raid_map;
1353 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
Don Brace03383732015-01-23 16:43:30 -06001354 }
Joe Handzika3144e02015-04-23 09:32:59 -05001355 if (new_entry->hba_ioaccel_enabled) {
1356 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1357 wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1358 }
1359 h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001360 h->dev[entry]->offload_config = new_entry->offload_config;
Stephen M. Cameron9fb0de22014-02-18 13:56:50 -06001361 h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
Don Brace03383732015-01-23 16:43:30 -06001362 h->dev[entry]->queue_depth = new_entry->queue_depth;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001363
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001364 /*
1365 * We can turn off ioaccel offload now, but need to delay turning
1366 * it on until we can update h->dev[entry]->phys_disk[], but we
1367 * can't do that until all the devices are updated.
1368 */
1369 h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
1370 if (!new_entry->offload_enabled)
1371 h->dev[entry]->offload_enabled = 0;
1372
Robert Elliotta473d862015-04-23 09:32:54 -05001373 offload_enabled = h->dev[entry]->offload_enabled;
1374 h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
Webb Scales0d96ef52015-04-23 09:31:55 -05001375 hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
Robert Elliotta473d862015-04-23 09:32:54 -05001376 h->dev[entry]->offload_enabled = offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001377}
1378
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001379/* Replace an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001380static void hpsa_scsi_replace_entry(struct ctlr_info *h,
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001381 int entry, struct hpsa_scsi_dev_t *new_entry,
1382 struct hpsa_scsi_dev_t *added[], int *nadded,
1383 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1384{
1385 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -05001386 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001387 removed[*nremoved] = h->dev[entry];
1388 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -05001389
1390 /*
1391 * New physical devices won't have target/lun assigned yet
1392 * so we need to preserve the values in the slot we are replacing.
1393 */
1394 if (new_entry->target == -1) {
1395 new_entry->target = h->dev[entry]->target;
1396 new_entry->lun = h->dev[entry]->lun;
1397 }
1398
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001399 h->dev[entry] = new_entry;
1400 added[*nadded] = new_entry;
1401 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001402 hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
Robert Elliotta473d862015-04-23 09:32:54 -05001403 new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1404 new_entry->offload_enabled = 0;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001405}
1406
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001407/* Remove an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001408static void hpsa_scsi_remove_entry(struct ctlr_info *h, int entry,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001409 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1410{
1411 /* assumes h->devlock is held */
1412 int i;
1413 struct hpsa_scsi_dev_t *sd;
1414
Scott Teelcfe5bad2011-10-26 16:21:07 -05001415 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001416
1417 sd = h->dev[entry];
1418 removed[*nremoved] = h->dev[entry];
1419 (*nremoved)++;
1420
1421 for (i = entry; i < h->ndevices-1; i++)
1422 h->dev[i] = h->dev[i+1];
1423 h->ndevices--;
Webb Scales0d96ef52015-04-23 09:31:55 -05001424 hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001425}
1426
1427#define SCSI3ADDR_EQ(a, b) ( \
1428 (a)[7] == (b)[7] && \
1429 (a)[6] == (b)[6] && \
1430 (a)[5] == (b)[5] && \
1431 (a)[4] == (b)[4] && \
1432 (a)[3] == (b)[3] && \
1433 (a)[2] == (b)[2] && \
1434 (a)[1] == (b)[1] && \
1435 (a)[0] == (b)[0])
1436
1437static void fixup_botched_add(struct ctlr_info *h,
1438 struct hpsa_scsi_dev_t *added)
1439{
1440 /* called when scsi_add_device fails in order to re-adjust
1441 * h->dev[] to match the mid layer's view.
1442 */
1443 unsigned long flags;
1444 int i, j;
1445
1446 spin_lock_irqsave(&h->lock, flags);
1447 for (i = 0; i < h->ndevices; i++) {
1448 if (h->dev[i] == added) {
1449 for (j = i; j < h->ndevices-1; j++)
1450 h->dev[j] = h->dev[j+1];
1451 h->ndevices--;
1452 break;
1453 }
1454 }
1455 spin_unlock_irqrestore(&h->lock, flags);
1456 kfree(added);
1457}
1458
1459static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1460 struct hpsa_scsi_dev_t *dev2)
1461{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001462 /* we compare everything except lun and target as these
1463 * are not yet assigned. Compare parts likely
1464 * to differ first
1465 */
1466 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1467 sizeof(dev1->scsi3addr)) != 0)
1468 return 0;
1469 if (memcmp(dev1->device_id, dev2->device_id,
1470 sizeof(dev1->device_id)) != 0)
1471 return 0;
1472 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1473 return 0;
1474 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1475 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001476 if (dev1->devtype != dev2->devtype)
1477 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001478 if (dev1->bus != dev2->bus)
1479 return 0;
1480 return 1;
1481}
1482
Scott Teelbd9244f2012-01-19 14:01:30 -06001483static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1484 struct hpsa_scsi_dev_t *dev2)
1485{
1486 /* Device attributes that can change, but don't mean
1487 * that the device is a different device, nor that the OS
1488 * needs to be told anything about the change.
1489 */
1490 if (dev1->raid_level != dev2->raid_level)
1491 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001492 if (dev1->offload_config != dev2->offload_config)
1493 return 1;
1494 if (dev1->offload_enabled != dev2->offload_enabled)
1495 return 1;
Don Brace93849502015-07-18 11:12:49 -05001496 if (!is_logical_dev_addr_mode(dev1->scsi3addr))
1497 if (dev1->queue_depth != dev2->queue_depth)
1498 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -06001499 return 0;
1500}
1501
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001502/* Find needle in haystack. If exact match found, return DEVICE_SAME,
1503 * and return needle location in *index. If scsi3addr matches, but not
1504 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -06001505 * location in *index.
1506 * In the case of a minor device attribute change, such as RAID level, just
1507 * return DEVICE_UPDATED, along with the updated device's location in index.
1508 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001509 */
1510static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1511 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1512 int *index)
1513{
1514 int i;
1515#define DEVICE_NOT_FOUND 0
1516#define DEVICE_CHANGED 1
1517#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -06001518#define DEVICE_UPDATED 3
Don Brace1d33d852015-11-04 15:50:31 -06001519 if (needle == NULL)
1520 return DEVICE_NOT_FOUND;
1521
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001522 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -06001523 if (haystack[i] == NULL) /* previously removed. */
1524 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001525 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1526 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -06001527 if (device_is_the_same(needle, haystack[i])) {
1528 if (device_updated(needle, haystack[i]))
1529 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001530 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -06001531 } else {
Stephen M. Cameron98465902014-02-21 16:25:00 -06001532 /* Keep offline devices offline */
1533 if (needle->volume_offline)
1534 return DEVICE_NOT_FOUND;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001535 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -06001536 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001537 }
1538 }
1539 *index = -1;
1540 return DEVICE_NOT_FOUND;
1541}
1542
Stephen M. Cameron98465902014-02-21 16:25:00 -06001543static void hpsa_monitor_offline_device(struct ctlr_info *h,
1544 unsigned char scsi3addr[])
1545{
1546 struct offline_device_entry *device;
1547 unsigned long flags;
1548
1549 /* Check to see if device is already on the list */
1550 spin_lock_irqsave(&h->offline_device_lock, flags);
1551 list_for_each_entry(device, &h->offline_device_list, offline_list) {
1552 if (memcmp(device->scsi3addr, scsi3addr,
1553 sizeof(device->scsi3addr)) == 0) {
1554 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1555 return;
1556 }
1557 }
1558 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1559
1560 /* Device is not on the list, add it. */
1561 device = kmalloc(sizeof(*device), GFP_KERNEL);
1562 if (!device) {
1563 dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
1564 return;
1565 }
1566 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1567 spin_lock_irqsave(&h->offline_device_lock, flags);
1568 list_add_tail(&device->offline_list, &h->offline_device_list);
1569 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1570}
1571
1572/* Print a message explaining various offline volume states */
1573static void hpsa_show_volume_status(struct ctlr_info *h,
1574 struct hpsa_scsi_dev_t *sd)
1575{
1576 if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
1577 dev_info(&h->pdev->dev,
1578 "C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
1579 h->scsi_host->host_no,
1580 sd->bus, sd->target, sd->lun);
1581 switch (sd->volume_offline) {
1582 case HPSA_LV_OK:
1583 break;
1584 case HPSA_LV_UNDERGOING_ERASE:
1585 dev_info(&h->pdev->dev,
1586 "C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
1587 h->scsi_host->host_no,
1588 sd->bus, sd->target, sd->lun);
1589 break;
Scott Benesh5ca01202015-07-18 11:13:04 -05001590 case HPSA_LV_NOT_AVAILABLE:
1591 dev_info(&h->pdev->dev,
1592 "C%d:B%d:T%d:L%d Volume is waiting for transforming volume.\n",
1593 h->scsi_host->host_no,
1594 sd->bus, sd->target, sd->lun);
1595 break;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001596 case HPSA_LV_UNDERGOING_RPI:
1597 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001598 "C%d:B%d:T%d:L%d Volume is undergoing rapid parity init.\n",
Stephen M. Cameron98465902014-02-21 16:25:00 -06001599 h->scsi_host->host_no,
1600 sd->bus, sd->target, sd->lun);
1601 break;
1602 case HPSA_LV_PENDING_RPI:
1603 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001604 "C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
1605 h->scsi_host->host_no,
1606 sd->bus, sd->target, sd->lun);
Stephen M. Cameron98465902014-02-21 16:25:00 -06001607 break;
1608 case HPSA_LV_ENCRYPTED_NO_KEY:
1609 dev_info(&h->pdev->dev,
1610 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
1611 h->scsi_host->host_no,
1612 sd->bus, sd->target, sd->lun);
1613 break;
1614 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
1615 dev_info(&h->pdev->dev,
1616 "C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
1617 h->scsi_host->host_no,
1618 sd->bus, sd->target, sd->lun);
1619 break;
1620 case HPSA_LV_UNDERGOING_ENCRYPTION:
1621 dev_info(&h->pdev->dev,
1622 "C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
1623 h->scsi_host->host_no,
1624 sd->bus, sd->target, sd->lun);
1625 break;
1626 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
1627 dev_info(&h->pdev->dev,
1628 "C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
1629 h->scsi_host->host_no,
1630 sd->bus, sd->target, sd->lun);
1631 break;
1632 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1633 dev_info(&h->pdev->dev,
1634 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
1635 h->scsi_host->host_no,
1636 sd->bus, sd->target, sd->lun);
1637 break;
1638 case HPSA_LV_PENDING_ENCRYPTION:
1639 dev_info(&h->pdev->dev,
1640 "C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
1641 h->scsi_host->host_no,
1642 sd->bus, sd->target, sd->lun);
1643 break;
1644 case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
1645 dev_info(&h->pdev->dev,
1646 "C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
1647 h->scsi_host->host_no,
1648 sd->bus, sd->target, sd->lun);
1649 break;
1650 }
1651}
1652
Don Brace03383732015-01-23 16:43:30 -06001653/*
1654 * Figure the list of physical drive pointers for a logical drive with
1655 * raid offload configured.
1656 */
1657static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
1658 struct hpsa_scsi_dev_t *dev[], int ndevices,
1659 struct hpsa_scsi_dev_t *logical_drive)
1660{
1661 struct raid_map_data *map = &logical_drive->raid_map;
1662 struct raid_map_disk_data *dd = &map->data[0];
1663 int i, j;
1664 int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
1665 le16_to_cpu(map->metadata_disks_per_row);
1666 int nraid_map_entries = le16_to_cpu(map->row_cnt) *
1667 le16_to_cpu(map->layout_map_count) *
1668 total_disks_per_row;
1669 int nphys_disk = le16_to_cpu(map->layout_map_count) *
1670 total_disks_per_row;
1671 int qdepth;
1672
1673 if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
1674 nraid_map_entries = RAID_MAP_MAX_ENTRIES;
1675
Webb Scalesd604f532015-04-23 09:35:22 -05001676 logical_drive->nphysical_disks = nraid_map_entries;
1677
Don Brace03383732015-01-23 16:43:30 -06001678 qdepth = 0;
1679 for (i = 0; i < nraid_map_entries; i++) {
1680 logical_drive->phys_disk[i] = NULL;
1681 if (!logical_drive->offload_config)
1682 continue;
1683 for (j = 0; j < ndevices; j++) {
Don Brace1d33d852015-11-04 15:50:31 -06001684 if (dev[j] == NULL)
1685 continue;
Petros Koutoupisff615f02016-05-09 13:44:10 -05001686 if (dev[j]->devtype != TYPE_DISK &&
1687 dev[j]->devtype != TYPE_ZBC)
Don Braceaf15ed32016-02-23 15:16:15 -06001688 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001689 if (is_logical_device(dev[j]))
Don Brace03383732015-01-23 16:43:30 -06001690 continue;
1691 if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
1692 continue;
1693
1694 logical_drive->phys_disk[i] = dev[j];
1695 if (i < nphys_disk)
1696 qdepth = min(h->nr_cmds, qdepth +
1697 logical_drive->phys_disk[i]->queue_depth);
1698 break;
1699 }
1700
1701 /*
1702 * This can happen if a physical drive is removed and
1703 * the logical drive is degraded. In that case, the RAID
1704 * map data will refer to a physical disk which isn't actually
1705 * present. And in that case offload_enabled should already
1706 * be 0, but we'll turn it off here just in case
1707 */
1708 if (!logical_drive->phys_disk[i]) {
1709 logical_drive->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001710 logical_drive->offload_to_be_enabled = 0;
1711 logical_drive->queue_depth = 8;
Don Brace03383732015-01-23 16:43:30 -06001712 }
1713 }
1714 if (nraid_map_entries)
1715 /*
1716 * This is correct for reads, too high for full stripe writes,
1717 * way too high for partial stripe writes
1718 */
1719 logical_drive->queue_depth = qdepth;
1720 else
1721 logical_drive->queue_depth = h->nr_cmds;
1722}
1723
1724static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
1725 struct hpsa_scsi_dev_t *dev[], int ndevices)
1726{
1727 int i;
1728
1729 for (i = 0; i < ndevices; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001730 if (dev[i] == NULL)
1731 continue;
Petros Koutoupisff615f02016-05-09 13:44:10 -05001732 if (dev[i]->devtype != TYPE_DISK &&
1733 dev[i]->devtype != TYPE_ZBC)
Don Braceaf15ed32016-02-23 15:16:15 -06001734 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001735 if (!is_logical_device(dev[i]))
Don Brace03383732015-01-23 16:43:30 -06001736 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001737
1738 /*
1739 * If offload is currently enabled, the RAID map and
1740 * phys_disk[] assignment *better* not be changing
1741 * and since it isn't changing, we do not need to
1742 * update it.
1743 */
1744 if (dev[i]->offload_enabled)
1745 continue;
1746
Don Brace03383732015-01-23 16:43:30 -06001747 hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
1748 }
1749}
1750
Kevin Barnett096ccff2015-11-04 15:51:51 -06001751static int hpsa_add_device(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1752{
1753 int rc = 0;
1754
1755 if (!h->scsi_host)
1756 return 1;
1757
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001758 if (is_logical_device(device)) /* RAID */
1759 rc = scsi_add_device(h->scsi_host, device->bus,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001760 device->target, device->lun);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001761 else /* HBA */
1762 rc = hpsa_add_sas_device(h->sas_host, device);
1763
Kevin Barnett096ccff2015-11-04 15:51:51 -06001764 return rc;
1765}
1766
Don Braceba74fdc2016-04-27 17:14:17 -05001767static int hpsa_find_outstanding_commands_for_dev(struct ctlr_info *h,
1768 struct hpsa_scsi_dev_t *dev)
1769{
1770 int i;
1771 int count = 0;
1772
1773 for (i = 0; i < h->nr_cmds; i++) {
1774 struct CommandList *c = h->cmd_pool + i;
1775 int refcount = atomic_inc_return(&c->refcount);
1776
1777 if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev,
1778 dev->scsi3addr)) {
1779 unsigned long flags;
1780
1781 spin_lock_irqsave(&h->lock, flags); /* Implied MB */
1782 if (!hpsa_is_cmd_idle(c))
1783 ++count;
1784 spin_unlock_irqrestore(&h->lock, flags);
1785 }
1786
1787 cmd_free(h, c);
1788 }
1789
1790 return count;
1791}
1792
1793static void hpsa_wait_for_outstanding_commands_for_dev(struct ctlr_info *h,
1794 struct hpsa_scsi_dev_t *device)
1795{
1796 int cmds = 0;
1797 int waits = 0;
1798
1799 while (1) {
1800 cmds = hpsa_find_outstanding_commands_for_dev(h, device);
1801 if (cmds == 0)
1802 break;
1803 if (++waits > 20)
1804 break;
1805 dev_warn(&h->pdev->dev,
1806 "%s: removing device with %d outstanding commands!\n",
1807 __func__, cmds);
1808 msleep(1000);
1809 }
1810}
1811
Kevin Barnett096ccff2015-11-04 15:51:51 -06001812static void hpsa_remove_device(struct ctlr_info *h,
1813 struct hpsa_scsi_dev_t *device)
1814{
1815 struct scsi_device *sdev = NULL;
1816
1817 if (!h->scsi_host)
1818 return;
1819
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001820 if (is_logical_device(device)) { /* RAID */
1821 sdev = scsi_device_lookup(h->scsi_host, device->bus,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001822 device->target, device->lun);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001823 if (sdev) {
1824 scsi_remove_device(sdev);
1825 scsi_device_put(sdev);
1826 } else {
1827 /*
1828 * We don't expect to get here. Future commands
1829 * to this device will get a selection timeout as
1830 * if the device were gone.
1831 */
1832 hpsa_show_dev_msg(KERN_WARNING, h, device,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001833 "didn't find device for removal.");
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001834 }
Don Braceba74fdc2016-04-27 17:14:17 -05001835 } else { /* HBA */
1836
1837 device->removed = 1;
1838 hpsa_wait_for_outstanding_commands_for_dev(h, device);
1839
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001840 hpsa_remove_sas_device(device);
Don Braceba74fdc2016-04-27 17:14:17 -05001841 }
Kevin Barnett096ccff2015-11-04 15:51:51 -06001842}
1843
Don Brace8aa60682015-11-04 15:50:01 -06001844static void adjust_hpsa_scsi_table(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001845 struct hpsa_scsi_dev_t *sd[], int nsds)
1846{
1847 /* sd contains scsi3 addresses and devtypes, and inquiry
1848 * data. This function takes what's in sd to be the current
1849 * reality and updates h->dev[] to reflect that reality.
1850 */
1851 int i, entry, device_change, changes = 0;
1852 struct hpsa_scsi_dev_t *csd;
1853 unsigned long flags;
1854 struct hpsa_scsi_dev_t **added, **removed;
1855 int nadded, nremoved;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001856
Don Braceda03ded2015-11-04 15:50:56 -06001857 /*
1858 * A reset can cause a device status to change
1859 * re-schedule the scan to see what happened.
1860 */
1861 if (h->reset_in_progress) {
1862 h->drv_req_rescan = 1;
1863 return;
1864 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001865
Scott Teelcfe5bad2011-10-26 16:21:07 -05001866 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1867 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001868
1869 if (!added || !removed) {
1870 dev_warn(&h->pdev->dev, "out of memory in "
1871 "adjust_hpsa_scsi_table\n");
1872 goto free_and_out;
1873 }
1874
1875 spin_lock_irqsave(&h->devlock, flags);
1876
1877 /* find any devices in h->dev[] that are not in
1878 * sd[] and remove them from h->dev[], and for any
1879 * devices which have changed, remove the old device
1880 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001881 * If minor device attributes change, just update
1882 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001883 */
1884 i = 0;
1885 nremoved = 0;
1886 nadded = 0;
1887 while (i < h->ndevices) {
1888 csd = h->dev[i];
1889 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1890 if (device_change == DEVICE_NOT_FOUND) {
1891 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001892 hpsa_scsi_remove_entry(h, i, removed, &nremoved);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001893 continue; /* remove ^^^, hence i not incremented */
1894 } else if (device_change == DEVICE_CHANGED) {
1895 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001896 hpsa_scsi_replace_entry(h, i, sd[entry],
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001897 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001898 /* Set it to NULL to prevent it from being freed
1899 * at the bottom of hpsa_update_scsi_devices()
1900 */
1901 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001902 } else if (device_change == DEVICE_UPDATED) {
Don Brace8aa60682015-11-04 15:50:01 -06001903 hpsa_scsi_update_entry(h, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001904 }
1905 i++;
1906 }
1907
1908 /* Now, make sure every device listed in sd[] is also
1909 * listed in h->dev[], adding them if they aren't found
1910 */
1911
1912 for (i = 0; i < nsds; i++) {
1913 if (!sd[i]) /* if already added above. */
1914 continue;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001915
1916 /* Don't add devices which are NOT READY, FORMAT IN PROGRESS
1917 * as the SCSI mid-layer does not handle such devices well.
1918 * It relentlessly loops sending TUR at 3Hz, then READ(10)
1919 * at 160Hz, and prevents the system from coming up.
1920 */
1921 if (sd[i]->volume_offline) {
1922 hpsa_show_volume_status(h, sd[i]);
Webb Scales0d96ef52015-04-23 09:31:55 -05001923 hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
Stephen M. Cameron98465902014-02-21 16:25:00 -06001924 continue;
1925 }
1926
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001927 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1928 h->ndevices, &entry);
1929 if (device_change == DEVICE_NOT_FOUND) {
1930 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001931 if (hpsa_scsi_add_entry(h, sd[i], added, &nadded) != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001932 break;
1933 sd[i] = NULL; /* prevent from being freed later. */
1934 } else if (device_change == DEVICE_CHANGED) {
1935 /* should never happen... */
1936 changes++;
1937 dev_warn(&h->pdev->dev,
1938 "device unexpectedly changed.\n");
1939 /* but if it does happen, we just ignore that device */
1940 }
1941 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001942 hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
1943
1944 /* Now that h->dev[]->phys_disk[] is coherent, we can enable
1945 * any logical drives that need it enabled.
1946 */
Don Brace1d33d852015-11-04 15:50:31 -06001947 for (i = 0; i < h->ndevices; i++) {
1948 if (h->dev[i] == NULL)
1949 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001950 h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
Don Brace1d33d852015-11-04 15:50:31 -06001951 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001952
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001953 spin_unlock_irqrestore(&h->devlock, flags);
1954
Stephen M. Cameron98465902014-02-21 16:25:00 -06001955 /* Monitor devices which are in one of several NOT READY states to be
1956 * brought online later. This must be done without holding h->devlock,
1957 * so don't touch h->dev[]
1958 */
1959 for (i = 0; i < nsds; i++) {
1960 if (!sd[i]) /* if already added above. */
1961 continue;
1962 if (sd[i]->volume_offline)
1963 hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
1964 }
1965
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001966 /* Don't notify scsi mid layer of any changes the first time through
1967 * (or if there are no changes) scsi_scan_host will do it later the
1968 * first time through.
1969 */
Don Brace8aa60682015-11-04 15:50:01 -06001970 if (!changes)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001971 goto free_and_out;
1972
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001973 /* Notify scsi mid layer of any removed devices */
1974 for (i = 0; i < nremoved; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001975 if (removed[i] == NULL)
1976 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001977 if (removed[i]->expose_device)
1978 hpsa_remove_device(h, removed[i]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001979 kfree(removed[i]);
1980 removed[i] = NULL;
1981 }
1982
1983 /* Notify scsi mid layer of any added devices */
1984 for (i = 0; i < nadded; i++) {
Kevin Barnett096ccff2015-11-04 15:51:51 -06001985 int rc = 0;
1986
Don Brace1d33d852015-11-04 15:50:31 -06001987 if (added[i] == NULL)
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001988 continue;
Kevin Barnett2a168202015-11-04 15:51:21 -06001989 if (!(added[i]->expose_device))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001990 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001991 rc = hpsa_add_device(h, added[i]);
1992 if (!rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001993 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001994 dev_warn(&h->pdev->dev,
1995 "addition failed %d, device not added.", rc);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001996 /* now we have to remove it from h->dev,
1997 * since it didn't get added to scsi mid layer
1998 */
1999 fixup_botched_add(h, added[i]);
Don Brace853633e2015-11-04 15:50:37 -06002000 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002001 }
2002
2003free_and_out:
2004 kfree(added);
2005 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002006}
2007
2008/*
Joe Perches9e03aa22013-09-03 13:45:58 -07002009 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002010 * Assume's h->devlock is held.
2011 */
2012static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
2013 int bus, int target, int lun)
2014{
2015 int i;
2016 struct hpsa_scsi_dev_t *sd;
2017
2018 for (i = 0; i < h->ndevices; i++) {
2019 sd = h->dev[i];
2020 if (sd->bus == bus && sd->target == target && sd->lun == lun)
2021 return sd;
2022 }
2023 return NULL;
2024}
2025
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002026static int hpsa_slave_alloc(struct scsi_device *sdev)
2027{
2028 struct hpsa_scsi_dev_t *sd;
2029 unsigned long flags;
2030 struct ctlr_info *h;
2031
2032 h = sdev_to_hba(sdev);
2033 spin_lock_irqsave(&h->devlock, flags);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06002034 if (sdev_channel(sdev) == HPSA_PHYSICAL_DEVICE_BUS) {
2035 struct scsi_target *starget;
2036 struct sas_rphy *rphy;
2037
2038 starget = scsi_target(sdev);
2039 rphy = target_to_rphy(starget);
2040 sd = hpsa_find_device_by_sas_rphy(h, rphy);
2041 if (sd) {
2042 sd->target = sdev_id(sdev);
2043 sd->lun = sdev->lun;
2044 }
2045 } else
2046 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
2047 sdev_id(sdev), sdev->lun);
2048
2049 if (sd && sd->expose_device) {
Don Brace03383732015-01-23 16:43:30 -06002050 atomic_set(&sd->ioaccel_cmds_out, 0);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06002051 sdev->hostdata = sd;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002052 } else
2053 sdev->hostdata = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002054 spin_unlock_irqrestore(&h->devlock, flags);
2055 return 0;
2056}
2057
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002058/* configure scsi device based on internal per-device structure */
2059static int hpsa_slave_configure(struct scsi_device *sdev)
2060{
2061 struct hpsa_scsi_dev_t *sd;
2062 int queue_depth;
2063
2064 sd = sdev->hostdata;
Kevin Barnett2a168202015-11-04 15:51:21 -06002065 sdev->no_uld_attach = !sd || !sd->expose_device;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002066
2067 if (sd)
2068 queue_depth = sd->queue_depth != 0 ?
2069 sd->queue_depth : sdev->host->can_queue;
2070 else
2071 queue_depth = sdev->host->can_queue;
2072
2073 scsi_change_queue_depth(sdev, queue_depth);
2074
2075 return 0;
2076}
2077
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002078static void hpsa_slave_destroy(struct scsi_device *sdev)
2079{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06002080 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002081}
2082
Webb Scalesd9a729f2015-04-23 09:33:27 -05002083static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
2084{
2085 int i;
2086
2087 if (!h->ioaccel2_cmd_sg_list)
2088 return;
2089 for (i = 0; i < h->nr_cmds; i++) {
2090 kfree(h->ioaccel2_cmd_sg_list[i]);
2091 h->ioaccel2_cmd_sg_list[i] = NULL;
2092 }
2093 kfree(h->ioaccel2_cmd_sg_list);
2094 h->ioaccel2_cmd_sg_list = NULL;
2095}
2096
2097static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
2098{
2099 int i;
2100
2101 if (h->chainsize <= 0)
2102 return 0;
2103
2104 h->ioaccel2_cmd_sg_list =
2105 kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
2106 GFP_KERNEL);
2107 if (!h->ioaccel2_cmd_sg_list)
2108 return -ENOMEM;
2109 for (i = 0; i < h->nr_cmds; i++) {
2110 h->ioaccel2_cmd_sg_list[i] =
2111 kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
2112 h->maxsgentries, GFP_KERNEL);
2113 if (!h->ioaccel2_cmd_sg_list[i])
2114 goto clean;
2115 }
2116 return 0;
2117
2118clean:
2119 hpsa_free_ioaccel2_sg_chain_blocks(h);
2120 return -ENOMEM;
2121}
2122
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002123static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
2124{
2125 int i;
2126
2127 if (!h->cmd_sg_list)
2128 return;
2129 for (i = 0; i < h->nr_cmds; i++) {
2130 kfree(h->cmd_sg_list[i]);
2131 h->cmd_sg_list[i] = NULL;
2132 }
2133 kfree(h->cmd_sg_list);
2134 h->cmd_sg_list = NULL;
2135}
2136
Robert Elliott105a3db2015-04-23 09:33:48 -05002137static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002138{
2139 int i;
2140
2141 if (h->chainsize <= 0)
2142 return 0;
2143
2144 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
2145 GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002146 if (!h->cmd_sg_list) {
2147 dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002148 return -ENOMEM;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002149 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002150 for (i = 0; i < h->nr_cmds; i++) {
2151 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
2152 h->chainsize, GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002153 if (!h->cmd_sg_list[i]) {
2154 dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002155 goto clean;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002156 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002157 }
2158 return 0;
2159
2160clean:
2161 hpsa_free_sg_chain_blocks(h);
2162 return -ENOMEM;
2163}
2164
Webb Scalesd9a729f2015-04-23 09:33:27 -05002165static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
2166 struct io_accel2_cmd *cp, struct CommandList *c)
2167{
2168 struct ioaccel2_sg_element *chain_block;
2169 u64 temp64;
2170 u32 chain_size;
2171
2172 chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
Don Bracea736e9b2015-11-04 15:51:14 -06002173 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002174 temp64 = pci_map_single(h->pdev, chain_block, chain_size,
2175 PCI_DMA_TODEVICE);
2176 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2177 /* prevent subsequent unmapping */
2178 cp->sg->address = 0;
2179 return -1;
2180 }
2181 cp->sg->address = cpu_to_le64(temp64);
2182 return 0;
2183}
2184
2185static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
2186 struct io_accel2_cmd *cp)
2187{
2188 struct ioaccel2_sg_element *chain_sg;
2189 u64 temp64;
2190 u32 chain_size;
2191
2192 chain_sg = cp->sg;
2193 temp64 = le64_to_cpu(chain_sg->address);
Don Bracea736e9b2015-11-04 15:51:14 -06002194 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002195 pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
2196}
2197
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002198static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002199 struct CommandList *c)
2200{
2201 struct SGDescriptor *chain_sg, *chain_block;
2202 u64 temp64;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002203 u32 chain_len;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002204
2205 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
2206 chain_block = h->cmd_sg_list[c->cmdindex];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002207 chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
2208 chain_len = sizeof(*chain_sg) *
Don Brace2b08b3e2015-01-23 16:41:09 -06002209 (le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002210 chain_sg->Len = cpu_to_le32(chain_len);
2211 temp64 = pci_map_single(h->pdev, chain_block, chain_len,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002212 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002213 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2214 /* prevent subsequent unmapping */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002215 chain_sg->Addr = cpu_to_le64(0);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002216 return -1;
2217 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002218 chain_sg->Addr = cpu_to_le64(temp64);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002219 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002220}
2221
2222static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
2223 struct CommandList *c)
2224{
2225 struct SGDescriptor *chain_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002226
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002227 if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002228 return;
2229
2230 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002231 pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
2232 le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002233}
2234
Scott Teela09c1442014-02-18 13:57:21 -06002235
2236/* Decode the various types of errors on ioaccel2 path.
2237 * Return 1 for any error that should generate a RAID path retry.
2238 * Return 0 for errors that don't require a RAID path retry.
2239 */
2240static int handle_ioaccel_mode2_error(struct ctlr_info *h,
Scott Teelc3497752014-02-18 13:56:34 -06002241 struct CommandList *c,
2242 struct scsi_cmnd *cmd,
Don Braceba74fdc2016-04-27 17:14:17 -05002243 struct io_accel2_cmd *c2,
2244 struct hpsa_scsi_dev_t *dev)
Scott Teelc3497752014-02-18 13:56:34 -06002245{
2246 int data_len;
Scott Teela09c1442014-02-18 13:57:21 -06002247 int retry = 0;
Joe Handzikc40820d2015-04-23 09:33:32 -05002248 u32 ioaccel2_resid = 0;
Scott Teelc3497752014-02-18 13:56:34 -06002249
2250 switch (c2->error_data.serv_response) {
2251 case IOACCEL2_SERV_RESPONSE_COMPLETE:
2252 switch (c2->error_data.status) {
2253 case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
2254 break;
2255 case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002256 cmd->result |= SAM_STAT_CHECK_CONDITION;
Scott Teelc3497752014-02-18 13:56:34 -06002257 if (c2->error_data.data_present !=
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002258 IOACCEL2_SENSE_DATA_PRESENT) {
2259 memset(cmd->sense_buffer, 0,
2260 SCSI_SENSE_BUFFERSIZE);
Scott Teelc3497752014-02-18 13:56:34 -06002261 break;
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002262 }
Scott Teelc3497752014-02-18 13:56:34 -06002263 /* copy the sense data */
2264 data_len = c2->error_data.sense_data_len;
2265 if (data_len > SCSI_SENSE_BUFFERSIZE)
2266 data_len = SCSI_SENSE_BUFFERSIZE;
2267 if (data_len > sizeof(c2->error_data.sense_data_buff))
2268 data_len =
2269 sizeof(c2->error_data.sense_data_buff);
2270 memcpy(cmd->sense_buffer,
2271 c2->error_data.sense_data_buff, data_len);
Scott Teela09c1442014-02-18 13:57:21 -06002272 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002273 break;
2274 case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
Scott Teela09c1442014-02-18 13:57:21 -06002275 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002276 break;
2277 case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
Scott Teela09c1442014-02-18 13:57:21 -06002278 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002279 break;
2280 case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
Stephen Cameron4a8da222015-04-23 09:32:43 -05002281 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002282 break;
2283 case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
Scott Teela09c1442014-02-18 13:57:21 -06002284 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002285 break;
2286 default:
Scott Teela09c1442014-02-18 13:57:21 -06002287 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002288 break;
2289 }
2290 break;
2291 case IOACCEL2_SERV_RESPONSE_FAILURE:
Joe Handzikc40820d2015-04-23 09:33:32 -05002292 switch (c2->error_data.status) {
2293 case IOACCEL2_STATUS_SR_IO_ERROR:
2294 case IOACCEL2_STATUS_SR_IO_ABORTED:
2295 case IOACCEL2_STATUS_SR_OVERRUN:
2296 retry = 1;
2297 break;
2298 case IOACCEL2_STATUS_SR_UNDERRUN:
2299 cmd->result = (DID_OK << 16); /* host byte */
2300 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
2301 ioaccel2_resid = get_unaligned_le32(
2302 &c2->error_data.resid_cnt[0]);
2303 scsi_set_resid(cmd, ioaccel2_resid);
2304 break;
2305 case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
2306 case IOACCEL2_STATUS_SR_INVALID_DEVICE:
2307 case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
Don Braceba74fdc2016-04-27 17:14:17 -05002308 /*
2309 * Did an HBA disk disappear? We will eventually
2310 * get a state change event from the controller but
2311 * in the meantime, we need to tell the OS that the
2312 * HBA disk is no longer there and stop I/O
2313 * from going down. This allows the potential re-insert
2314 * of the disk to get the same device node.
2315 */
2316 if (dev->physical_device && dev->expose_device) {
2317 cmd->result = DID_NO_CONNECT << 16;
2318 dev->removed = 1;
2319 h->drv_req_rescan = 1;
2320 dev_warn(&h->pdev->dev,
2321 "%s: device is gone!\n", __func__);
2322 } else
2323 /*
2324 * Retry by sending down the RAID path.
2325 * We will get an event from ctlr to
2326 * trigger rescan regardless.
2327 */
2328 retry = 1;
Joe Handzikc40820d2015-04-23 09:33:32 -05002329 break;
2330 default:
2331 retry = 1;
Joe Handzikc40820d2015-04-23 09:33:32 -05002332 }
Scott Teelc3497752014-02-18 13:56:34 -06002333 break;
2334 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
2335 break;
2336 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
2337 break;
2338 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
Scott Teela09c1442014-02-18 13:57:21 -06002339 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002340 break;
2341 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
Scott Teelc3497752014-02-18 13:56:34 -06002342 break;
2343 default:
Scott Teela09c1442014-02-18 13:57:21 -06002344 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002345 break;
2346 }
Scott Teela09c1442014-02-18 13:57:21 -06002347
2348 return retry; /* retry on raid path? */
Scott Teelc3497752014-02-18 13:56:34 -06002349}
2350
Webb Scalesa58e7e52015-04-23 09:34:16 -05002351static void hpsa_cmd_resolve_events(struct ctlr_info *h,
2352 struct CommandList *c)
2353{
Webb Scalesd604f532015-04-23 09:35:22 -05002354 bool do_wake = false;
2355
Webb Scalesa58e7e52015-04-23 09:34:16 -05002356 /*
2357 * Prevent the following race in the abort handler:
2358 *
2359 * 1. LLD is requested to abort a SCSI command
2360 * 2. The SCSI command completes
2361 * 3. The struct CommandList associated with step 2 is made available
2362 * 4. New I/O request to LLD to another LUN re-uses struct CommandList
2363 * 5. Abort handler follows scsi_cmnd->host_scribble and
2364 * finds struct CommandList and tries to aborts it
2365 * Now we have aborted the wrong command.
2366 *
Webb Scalesd604f532015-04-23 09:35:22 -05002367 * Reset c->scsi_cmd here so that the abort or reset handler will know
2368 * this command has completed. Then, check to see if the handler is
Webb Scalesa58e7e52015-04-23 09:34:16 -05002369 * waiting for this command, and, if so, wake it.
2370 */
2371 c->scsi_cmd = SCSI_CMD_IDLE;
Webb Scalesd604f532015-04-23 09:35:22 -05002372 mb(); /* Declare command idle before checking for pending events. */
Webb Scalesa58e7e52015-04-23 09:34:16 -05002373 if (c->abort_pending) {
Webb Scalesd604f532015-04-23 09:35:22 -05002374 do_wake = true;
Webb Scalesa58e7e52015-04-23 09:34:16 -05002375 c->abort_pending = false;
Webb Scalesa58e7e52015-04-23 09:34:16 -05002376 }
Webb Scalesd604f532015-04-23 09:35:22 -05002377 if (c->reset_pending) {
2378 unsigned long flags;
2379 struct hpsa_scsi_dev_t *dev;
2380
2381 /*
2382 * There appears to be a reset pending; lock the lock and
2383 * reconfirm. If so, then decrement the count of outstanding
2384 * commands and wake the reset command if this is the last one.
2385 */
2386 spin_lock_irqsave(&h->lock, flags);
2387 dev = c->reset_pending; /* Re-fetch under the lock. */
2388 if (dev && atomic_dec_and_test(&dev->reset_cmds_out))
2389 do_wake = true;
2390 c->reset_pending = NULL;
2391 spin_unlock_irqrestore(&h->lock, flags);
2392 }
2393
2394 if (do_wake)
2395 wake_up_all(&h->event_sync_wait_queue);
Webb Scalesa58e7e52015-04-23 09:34:16 -05002396}
2397
Webb Scales73153fe2015-04-23 09:35:04 -05002398static void hpsa_cmd_resolve_and_free(struct ctlr_info *h,
2399 struct CommandList *c)
2400{
2401 hpsa_cmd_resolve_events(h, c);
2402 cmd_tagged_free(h, c);
2403}
2404
Webb Scales8a0ff922015-04-23 09:34:11 -05002405static void hpsa_cmd_free_and_done(struct ctlr_info *h,
2406 struct CommandList *c, struct scsi_cmnd *cmd)
2407{
Webb Scales73153fe2015-04-23 09:35:04 -05002408 hpsa_cmd_resolve_and_free(h, c);
Don Braced49c2072016-09-09 16:30:23 -05002409 if (cmd && cmd->scsi_done)
2410 cmd->scsi_done(cmd);
Webb Scales8a0ff922015-04-23 09:34:11 -05002411}
2412
2413static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
2414{
2415 INIT_WORK(&c->work, hpsa_command_resubmit_worker);
2416 queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
2417}
2418
Webb Scalesa58e7e52015-04-23 09:34:16 -05002419static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
2420{
2421 cmd->result = DID_ABORT << 16;
2422}
2423
2424static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
2425 struct scsi_cmnd *cmd)
2426{
2427 hpsa_set_scsi_cmd_aborted(cmd);
2428 dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2429 c->Request.CDB, c->err_info->ScsiStatus);
Webb Scales73153fe2015-04-23 09:35:04 -05002430 hpsa_cmd_resolve_and_free(h, c);
Webb Scalesa58e7e52015-04-23 09:34:16 -05002431}
2432
Scott Teelc3497752014-02-18 13:56:34 -06002433static void process_ioaccel2_completion(struct ctlr_info *h,
2434 struct CommandList *c, struct scsi_cmnd *cmd,
2435 struct hpsa_scsi_dev_t *dev)
2436{
2437 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2438
2439 /* check for good status */
2440 if (likely(c2->error_data.serv_response == 0 &&
Webb Scales8a0ff922015-04-23 09:34:11 -05002441 c2->error_data.status == 0))
2442 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002443
Webb Scales8a0ff922015-04-23 09:34:11 -05002444 /*
2445 * Any RAID offload error results in retry which will use
Scott Teelc3497752014-02-18 13:56:34 -06002446 * the normal I/O path so the controller can handle whatever's
2447 * wrong.
2448 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002449 if (is_logical_device(dev) &&
Scott Teelc3497752014-02-18 13:56:34 -06002450 c2->error_data.serv_response ==
2451 IOACCEL2_SERV_RESPONSE_FAILURE) {
Don Brace080ef1c2015-01-23 16:43:25 -06002452 if (c2->error_data.status ==
Don Brace064d1b12016-04-27 17:14:07 -05002453 IOACCEL2_STATUS_SR_IOACCEL_DISABLED) {
Don Brace080ef1c2015-01-23 16:43:25 -06002454 dev->offload_enabled = 0;
Don Brace064d1b12016-04-27 17:14:07 -05002455 dev->offload_to_be_enabled = 0;
2456 }
Webb Scales8a0ff922015-04-23 09:34:11 -05002457
2458 return hpsa_retry_cmd(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06002459 }
Don Brace080ef1c2015-01-23 16:43:25 -06002460
Don Braceba74fdc2016-04-27 17:14:17 -05002461 if (handle_ioaccel_mode2_error(h, c, cmd, c2, dev))
Webb Scales8a0ff922015-04-23 09:34:11 -05002462 return hpsa_retry_cmd(h, c);
Don Brace080ef1c2015-01-23 16:43:25 -06002463
Webb Scales8a0ff922015-04-23 09:34:11 -05002464 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002465}
2466
Stephen Cameron9437ac42015-04-23 09:32:16 -05002467/* Returns 0 on success, < 0 otherwise. */
2468static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
2469 struct CommandList *cp)
2470{
2471 u8 tmf_status = cp->err_info->ScsiStatus;
2472
2473 switch (tmf_status) {
2474 case CISS_TMF_COMPLETE:
2475 /*
2476 * CISS_TMF_COMPLETE never happens, instead,
2477 * ei->CommandStatus == 0 for this case.
2478 */
2479 case CISS_TMF_SUCCESS:
2480 return 0;
2481 case CISS_TMF_INVALID_FRAME:
2482 case CISS_TMF_NOT_SUPPORTED:
2483 case CISS_TMF_FAILED:
2484 case CISS_TMF_WRONG_LUN:
2485 case CISS_TMF_OVERLAPPED_TAG:
2486 break;
2487 default:
2488 dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
2489 tmf_status);
2490 break;
2491 }
2492 return -tmf_status;
2493}
2494
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05002495static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002496{
2497 struct scsi_cmnd *cmd;
2498 struct ctlr_info *h;
2499 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002500 struct hpsa_scsi_dev_t *dev;
Webb Scalesd9a729f2015-04-23 09:33:27 -05002501 struct io_accel2_cmd *c2;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002502
Stephen Cameron9437ac42015-04-23 09:32:16 -05002503 u8 sense_key;
2504 u8 asc; /* additional sense code */
2505 u8 ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05002506 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002507
2508 ei = cp->err_info;
Stephen Cameron7fa30302015-01-23 16:44:30 -06002509 cmd = cp->scsi_cmd;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002510 h = cp->h;
Don Braced49c2072016-09-09 16:30:23 -05002511
2512 if (!cmd->device) {
2513 cmd->result = DID_NO_CONNECT << 16;
2514 return hpsa_cmd_free_and_done(h, cp, cmd);
2515 }
2516
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002517 dev = cmd->device->hostdata;
Don Brace45e596c2016-09-09 16:30:42 -05002518 if (!dev) {
2519 cmd->result = DID_NO_CONNECT << 16;
2520 return hpsa_cmd_free_and_done(h, cp, cmd);
2521 }
Webb Scalesd9a729f2015-04-23 09:33:27 -05002522 c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002523
2524 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06002525 if ((cp->cmd_type == CMD_SCSI) &&
Don Brace2b08b3e2015-01-23 16:41:09 -06002526 (le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002527 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002528
Webb Scalesd9a729f2015-04-23 09:33:27 -05002529 if ((cp->cmd_type == CMD_IOACCEL2) &&
2530 (c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2531 hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2532
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002533 cmd->result = (DID_OK << 16); /* host byte */
2534 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06002535
Don Braced49c2072016-09-09 16:30:23 -05002536 if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1) {
2537 if (dev->physical_device && dev->expose_device &&
2538 dev->removed) {
2539 cmd->result = DID_NO_CONNECT << 16;
2540 return hpsa_cmd_free_and_done(h, cp, cmd);
2541 }
2542 if (likely(cp->phys_disk != NULL))
2543 atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
2544 }
Don Brace03383732015-01-23 16:43:30 -06002545
Webb Scales25163bd2015-04-23 09:32:00 -05002546 /*
2547 * We check for lockup status here as it may be set for
2548 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
2549 * fail_all_oustanding_cmds()
2550 */
2551 if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
2552 /* DID_NO_CONNECT will prevent a retry */
2553 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05002554 return hpsa_cmd_free_and_done(h, cp, cmd);
Webb Scales25163bd2015-04-23 09:32:00 -05002555 }
2556
Webb Scalesd604f532015-04-23 09:35:22 -05002557 if ((unlikely(hpsa_is_pending_event(cp)))) {
2558 if (cp->reset_pending)
Don Bracebfd75462016-11-15 14:45:32 -06002559 return hpsa_cmd_free_and_done(h, cp, cmd);
Webb Scalesd604f532015-04-23 09:35:22 -05002560 if (cp->abort_pending)
2561 return hpsa_cmd_abort_and_free(h, cp, cmd);
2562 }
2563
Scott Teelc3497752014-02-18 13:56:34 -06002564 if (cp->cmd_type == CMD_IOACCEL2)
2565 return process_ioaccel2_completion(h, cp, cmd, dev);
2566
Robert Elliott6aa4c362014-07-03 10:18:19 -05002567 scsi_set_resid(cmd, ei->ResidualCnt);
Webb Scales8a0ff922015-04-23 09:34:11 -05002568 if (ei->CommandStatus == 0)
2569 return hpsa_cmd_free_and_done(h, cp, cmd);
Robert Elliott6aa4c362014-07-03 10:18:19 -05002570
Matt Gatese1f7de02014-02-18 13:55:17 -06002571 /* For I/O accelerator commands, copy over some fields to the normal
2572 * CISS header used below for error handling.
2573 */
2574 if (cp->cmd_type == CMD_IOACCEL1) {
2575 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06002576 cp->Header.SGList = scsi_sg_count(cmd);
2577 cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
2578 cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
2579 IOACCEL1_IOFLAGS_CDBLEN_MASK;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002580 cp->Header.tag = c->tag;
Matt Gatese1f7de02014-02-18 13:55:17 -06002581 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2582 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002583
2584 /* Any RAID offload error results in retry which will use
2585 * the normal I/O path so the controller can handle whatever's
2586 * wrong.
2587 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002588 if (is_logical_device(dev)) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002589 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2590 dev->offload_enabled = 0;
Webb Scalesd604f532015-04-23 09:35:22 -05002591 return hpsa_retry_cmd(h, cp);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002592 }
Matt Gatese1f7de02014-02-18 13:55:17 -06002593 }
2594
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002595 /* an error has occurred */
2596 switch (ei->CommandStatus) {
2597
2598 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002599 cmd->result |= ei->ScsiStatus;
2600 /* copy the sense data */
2601 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
2602 sense_data_size = SCSI_SENSE_BUFFERSIZE;
2603 else
2604 sense_data_size = sizeof(ei->SenseInfo);
2605 if (ei->SenseLen < sense_data_size)
2606 sense_data_size = ei->SenseLen;
2607 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
2608 if (ei->ScsiStatus)
2609 decode_sense_data(ei->SenseInfo, sense_data_size,
2610 &sense_key, &asc, &ascq);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002611 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates1d3b3602010-02-04 08:43:00 -06002612 if (sense_key == ABORTED_COMMAND) {
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05002613 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06002614 break;
2615 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002616 break;
2617 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002618 /* Problem was not a check condition
2619 * Pass it up to the upper layers...
2620 */
2621 if (ei->ScsiStatus) {
2622 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2623 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2624 "Returning result: 0x%x\n",
2625 cp, ei->ScsiStatus,
2626 sense_key, asc, ascq,
2627 cmd->result);
2628 } else { /* scsi status is zero??? How??? */
2629 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2630 "Returning no connection.\n", cp),
2631
2632 /* Ordinarily, this case should never happen,
2633 * but there is a bug in some released firmware
2634 * revisions that allows it to happen if, for
2635 * example, a 4100 backplane loses power and
2636 * the tape drive is in it. We assume that
2637 * it's a fatal error of some kind because we
2638 * can't show that it wasn't. We will make it
2639 * look like selection timeout since that is
2640 * the most common reason for this to occur,
2641 * and it's severe enough.
2642 */
2643
2644 cmd->result = DID_NO_CONNECT << 16;
2645 }
2646 break;
2647
2648 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2649 break;
2650 case CMD_DATA_OVERRUN:
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002651 dev_warn(&h->pdev->dev,
2652 "CDB %16phN data overrun\n", cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002653 break;
2654 case CMD_INVALID: {
2655 /* print_bytes(cp, sizeof(*cp), 1, 0);
2656 print_cmd(cp); */
2657 /* We get CMD_INVALID if you address a non-existent device
2658 * instead of a selection timeout (no response). You will
2659 * see this if you yank out a drive, then try to access it.
2660 * This is kind of a shame because it means that any other
2661 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2662 * missing target. */
2663 cmd->result = DID_NO_CONNECT << 16;
2664 }
2665 break;
2666 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05002667 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002668 dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2669 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002670 break;
2671 case CMD_HARDWARE_ERR:
2672 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002673 dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2674 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002675 break;
2676 case CMD_CONNECTION_LOST:
2677 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002678 dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2679 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002680 break;
2681 case CMD_ABORTED:
Webb Scalesa58e7e52015-04-23 09:34:16 -05002682 /* Return now to avoid calling scsi_done(). */
2683 return hpsa_cmd_abort_and_free(h, cp, cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002684 case CMD_ABORT_FAILED:
2685 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002686 dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2687 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002688 break;
2689 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05002690 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002691 dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2692 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002693 break;
2694 case CMD_TIMEOUT:
2695 cmd->result = DID_TIME_OUT << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002696 dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2697 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002698 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002699 case CMD_UNABORTABLE:
2700 cmd->result = DID_ERROR << 16;
2701 dev_warn(&h->pdev->dev, "Command unabortable\n");
2702 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002703 case CMD_TMF_STATUS:
2704 if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
2705 cmd->result = DID_ERROR << 16;
2706 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002707 case CMD_IOACCEL_DISABLED:
2708 /* This only handles the direct pass-through case since RAID
2709 * offload is handled above. Just attempt a retry.
2710 */
2711 cmd->result = DID_SOFT_ERROR << 16;
2712 dev_warn(&h->pdev->dev,
2713 "cp %p had HP SSD Smart Path error\n", cp);
2714 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002715 default:
2716 cmd->result = DID_ERROR << 16;
2717 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2718 cp, ei->CommandStatus);
2719 }
Webb Scales8a0ff922015-04-23 09:34:11 -05002720
2721 return hpsa_cmd_free_and_done(h, cp, cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002722}
2723
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002724static void hpsa_pci_unmap(struct pci_dev *pdev,
2725 struct CommandList *c, int sg_used, int data_direction)
2726{
2727 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002728
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002729 for (i = 0; i < sg_used; i++)
2730 pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
2731 le32_to_cpu(c->SG[i].Len),
2732 data_direction);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002733}
2734
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002735static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002736 struct CommandList *cp,
2737 unsigned char *buf,
2738 size_t buflen,
2739 int data_direction)
2740{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002741 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002742
2743 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2744 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002745 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002746 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002747 }
2748
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002749 addr64 = pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06002750 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002751 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06002752 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002753 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002754 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06002755 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002756 cp->SG[0].Addr = cpu_to_le64(addr64);
2757 cp->SG[0].Len = cpu_to_le32(buflen);
2758 cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
2759 cp->Header.SGList = 1; /* no. SGs contig in this cmd */
2760 cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002761 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002762}
2763
Webb Scales25163bd2015-04-23 09:32:00 -05002764#define NO_TIMEOUT ((unsigned long) -1)
2765#define DEFAULT_TIMEOUT 30000 /* milliseconds */
2766static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
2767 struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002768{
2769 DECLARE_COMPLETION_ONSTACK(wait);
2770
2771 c->waiting = &wait;
Webb Scales25163bd2015-04-23 09:32:00 -05002772 __enqueue_cmd_and_start_io(h, c, reply_queue);
2773 if (timeout_msecs == NO_TIMEOUT) {
2774 /* TODO: get rid of this no-timeout thing */
2775 wait_for_completion_io(&wait);
2776 return IO_OK;
2777 }
2778 if (!wait_for_completion_io_timeout(&wait,
2779 msecs_to_jiffies(timeout_msecs))) {
2780 dev_warn(&h->pdev->dev, "Command timed out.\n");
2781 return -ETIMEDOUT;
2782 }
2783 return IO_OK;
2784}
2785
2786static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
2787 int reply_queue, unsigned long timeout_msecs)
2788{
2789 if (unlikely(lockup_detected(h))) {
2790 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
2791 return IO_OK;
2792 }
2793 return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002794}
2795
Stephen M. Cameron094963d2014-05-29 10:53:18 -05002796static u32 lockup_detected(struct ctlr_info *h)
2797{
2798 int cpu;
2799 u32 rc, *lockup_detected;
2800
2801 cpu = get_cpu();
2802 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2803 rc = *lockup_detected;
2804 put_cpu();
2805 return rc;
2806}
2807
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002808#define MAX_DRIVER_CMD_RETRIES 25
Webb Scales25163bd2015-04-23 09:32:00 -05002809static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
2810 struct CommandList *c, int data_direction, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002811{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002812 int backoff_time = 10, retry_count = 0;
Webb Scales25163bd2015-04-23 09:32:00 -05002813 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002814
2815 do {
Joe Perches7630abd2011-05-08 23:32:40 -07002816 memset(c->err_info, 0, sizeof(*c->err_info));
Webb Scales25163bd2015-04-23 09:32:00 -05002817 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
2818 timeout_msecs);
2819 if (rc)
2820 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002821 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002822 if (retry_count > 3) {
2823 msleep(backoff_time);
2824 if (backoff_time < 1000)
2825 backoff_time *= 2;
2826 }
Matt Bondurant852af202012-05-01 11:42:35 -05002827 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002828 check_for_busy(h, c)) &&
2829 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002830 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
Webb Scales25163bd2015-04-23 09:32:00 -05002831 if (retry_count > MAX_DRIVER_CMD_RETRIES)
2832 rc = -EIO;
2833 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002834}
2835
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002836static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2837 struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002838{
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002839 const u8 *cdb = c->Request.CDB;
2840 const u8 *lun = c->Header.LUN.LunAddrBytes;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002841
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002842 dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2843 " CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2844 txt, lun[0], lun[1], lun[2], lun[3],
2845 lun[4], lun[5], lun[6], lun[7],
2846 cdb[0], cdb[1], cdb[2], cdb[3],
2847 cdb[4], cdb[5], cdb[6], cdb[7],
2848 cdb[8], cdb[9], cdb[10], cdb[11],
2849 cdb[12], cdb[13], cdb[14], cdb[15]);
2850}
2851
2852static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2853 struct CommandList *cp)
2854{
2855 const struct ErrorInfo *ei = cp->err_info;
2856 struct device *d = &cp->h->pdev->dev;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002857 u8 sense_key, asc, ascq;
2858 int sense_len;
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002859
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002860 switch (ei->CommandStatus) {
2861 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002862 if (ei->SenseLen > sizeof(ei->SenseInfo))
2863 sense_len = sizeof(ei->SenseInfo);
2864 else
2865 sense_len = ei->SenseLen;
2866 decode_sense_data(ei->SenseInfo, sense_len,
2867 &sense_key, &asc, &ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002868 hpsa_print_cmd(h, "SCSI status", cp);
2869 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
Stephen Cameron9437ac42015-04-23 09:32:16 -05002870 dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
2871 sense_key, asc, ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002872 else
Stephen Cameron9437ac42015-04-23 09:32:16 -05002873 dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002874 if (ei->ScsiStatus == 0)
2875 dev_warn(d, "SCSI status is abnormally zero. "
2876 "(probably indicates selection timeout "
2877 "reported incorrectly due to a known "
2878 "firmware bug, circa July, 2001.)\n");
2879 break;
2880 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002881 break;
2882 case CMD_DATA_OVERRUN:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002883 hpsa_print_cmd(h, "overrun condition", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002884 break;
2885 case CMD_INVALID: {
2886 /* controller unfortunately reports SCSI passthru's
2887 * to non-existent targets as invalid commands.
2888 */
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002889 hpsa_print_cmd(h, "invalid command", cp);
2890 dev_warn(d, "probably means device no longer present\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002891 }
2892 break;
2893 case CMD_PROTOCOL_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002894 hpsa_print_cmd(h, "protocol error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002895 break;
2896 case CMD_HARDWARE_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002897 hpsa_print_cmd(h, "hardware error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002898 break;
2899 case CMD_CONNECTION_LOST:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002900 hpsa_print_cmd(h, "connection lost", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002901 break;
2902 case CMD_ABORTED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002903 hpsa_print_cmd(h, "aborted", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002904 break;
2905 case CMD_ABORT_FAILED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002906 hpsa_print_cmd(h, "abort failed", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002907 break;
2908 case CMD_UNSOLICITED_ABORT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002909 hpsa_print_cmd(h, "unsolicited abort", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002910 break;
2911 case CMD_TIMEOUT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002912 hpsa_print_cmd(h, "timed out", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002913 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002914 case CMD_UNABORTABLE:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002915 hpsa_print_cmd(h, "unabortable", cp);
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002916 break;
Webb Scales25163bd2015-04-23 09:32:00 -05002917 case CMD_CTLR_LOCKUP:
2918 hpsa_print_cmd(h, "controller lockup detected", cp);
2919 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002920 default:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002921 hpsa_print_cmd(h, "unknown status", cp);
2922 dev_warn(d, "Unknown command status %x\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002923 ei->CommandStatus);
2924 }
2925}
2926
2927static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002928 u16 page, unsigned char *buf,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002929 unsigned char bufsize)
2930{
2931 int rc = IO_OK;
2932 struct CommandList *c;
2933 struct ErrorInfo *ei;
2934
Stephen Cameron45fcb862015-01-23 16:43:04 -06002935 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002936
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002937 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2938 page, scsi3addr, TYPE_CMD)) {
2939 rc = -1;
2940 goto out;
2941 }
Webb Scales25163bd2015-04-23 09:32:00 -05002942 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05002943 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05002944 if (rc)
2945 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002946 ei = c->err_info;
2947 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002948 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002949 rc = -1;
2950 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002951out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002952 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002953 return rc;
2954}
2955
Scott Teelbf711ac2014-02-18 13:56:39 -06002956static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Webb Scales25163bd2015-04-23 09:32:00 -05002957 u8 reset_type, int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002958{
2959 int rc = IO_OK;
2960 struct CommandList *c;
2961 struct ErrorInfo *ei;
2962
Stephen Cameron45fcb862015-01-23 16:43:04 -06002963 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002964
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002965
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002966 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teel0b9b7b62015-11-04 15:51:02 -06002967 (void) fill_cmd(c, reset_type, h, NULL, 0, 0,
Scott Teelbf711ac2014-02-18 13:56:39 -06002968 scsi3addr, TYPE_MSG);
Don Bracec448ecf2016-04-27 17:13:51 -05002969 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05002970 if (rc) {
2971 dev_warn(&h->pdev->dev, "Failed to send reset command\n");
2972 goto out;
2973 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002974 /* no unmap needed here because no data xfer. */
2975
2976 ei = c->err_info;
2977 if (ei->CommandStatus != 0) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002978 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002979 rc = -1;
2980 }
Webb Scales25163bd2015-04-23 09:32:00 -05002981out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002982 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002983 return rc;
2984}
2985
Webb Scalesd604f532015-04-23 09:35:22 -05002986static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
2987 struct hpsa_scsi_dev_t *dev,
2988 unsigned char *scsi3addr)
2989{
2990 int i;
2991 bool match = false;
2992 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2993 struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
2994
2995 if (hpsa_is_cmd_idle(c))
2996 return false;
2997
2998 switch (c->cmd_type) {
2999 case CMD_SCSI:
3000 case CMD_IOCTL_PEND:
3001 match = !memcmp(scsi3addr, &c->Header.LUN.LunAddrBytes,
3002 sizeof(c->Header.LUN.LunAddrBytes));
3003 break;
3004
3005 case CMD_IOACCEL1:
3006 case CMD_IOACCEL2:
3007 if (c->phys_disk == dev) {
3008 /* HBA mode match */
3009 match = true;
3010 } else {
3011 /* Possible RAID mode -- check each phys dev. */
3012 /* FIXME: Do we need to take out a lock here? If
3013 * so, we could just call hpsa_get_pdisk_of_ioaccel2()
3014 * instead. */
3015 for (i = 0; i < dev->nphysical_disks && !match; i++) {
3016 /* FIXME: an alternate test might be
3017 *
3018 * match = dev->phys_disk[i]->ioaccel_handle
3019 * == c2->scsi_nexus; */
3020 match = dev->phys_disk[i] == c->phys_disk;
3021 }
3022 }
3023 break;
3024
3025 case IOACCEL2_TMF:
3026 for (i = 0; i < dev->nphysical_disks && !match; i++) {
3027 match = dev->phys_disk[i]->ioaccel_handle ==
3028 le32_to_cpu(ac->it_nexus);
3029 }
3030 break;
3031
3032 case 0: /* The command is in the middle of being initialized. */
3033 match = false;
3034 break;
3035
3036 default:
3037 dev_err(&h->pdev->dev, "unexpected cmd_type: %d\n",
3038 c->cmd_type);
3039 BUG();
3040 }
3041
3042 return match;
3043}
3044
3045static int hpsa_do_reset(struct ctlr_info *h, struct hpsa_scsi_dev_t *dev,
3046 unsigned char *scsi3addr, u8 reset_type, int reply_queue)
3047{
3048 int i;
3049 int rc = 0;
3050
3051 /* We can really only handle one reset at a time */
3052 if (mutex_lock_interruptible(&h->reset_mutex) == -EINTR) {
3053 dev_warn(&h->pdev->dev, "concurrent reset wait interrupted.\n");
3054 return -EINTR;
3055 }
3056
3057 BUG_ON(atomic_read(&dev->reset_cmds_out) != 0);
3058
3059 for (i = 0; i < h->nr_cmds; i++) {
3060 struct CommandList *c = h->cmd_pool + i;
3061 int refcount = atomic_inc_return(&c->refcount);
3062
3063 if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev, scsi3addr)) {
3064 unsigned long flags;
3065
3066 /*
3067 * Mark the target command as having a reset pending,
3068 * then lock a lock so that the command cannot complete
3069 * while we're considering it. If the command is not
3070 * idle then count it; otherwise revoke the event.
3071 */
3072 c->reset_pending = dev;
3073 spin_lock_irqsave(&h->lock, flags); /* Implied MB */
3074 if (!hpsa_is_cmd_idle(c))
3075 atomic_inc(&dev->reset_cmds_out);
3076 else
3077 c->reset_pending = NULL;
3078 spin_unlock_irqrestore(&h->lock, flags);
3079 }
3080
3081 cmd_free(h, c);
3082 }
3083
3084 rc = hpsa_send_reset(h, scsi3addr, reset_type, reply_queue);
3085 if (!rc)
3086 wait_event(h->event_sync_wait_queue,
3087 atomic_read(&dev->reset_cmds_out) == 0 ||
3088 lockup_detected(h));
3089
3090 if (unlikely(lockup_detected(h))) {
Don Brace77678d32015-07-18 11:12:22 -05003091 dev_warn(&h->pdev->dev,
3092 "Controller lockup detected during reset wait\n");
3093 rc = -ENODEV;
3094 }
Webb Scalesd604f532015-04-23 09:35:22 -05003095
3096 if (unlikely(rc))
3097 atomic_set(&dev->reset_cmds_out, 0);
Don Bracebfd75462016-11-15 14:45:32 -06003098 else
3099 wait_for_device_to_become_ready(h, scsi3addr, 0);
Webb Scalesd604f532015-04-23 09:35:22 -05003100
3101 mutex_unlock(&h->reset_mutex);
3102 return rc;
3103}
3104
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003105static void hpsa_get_raid_level(struct ctlr_info *h,
3106 unsigned char *scsi3addr, unsigned char *raid_level)
3107{
3108 int rc;
3109 unsigned char *buf;
3110
3111 *raid_level = RAID_UNKNOWN;
3112 buf = kzalloc(64, GFP_KERNEL);
3113 if (!buf)
3114 return;
Scott Teel83832782016-09-09 16:30:29 -05003115
3116 if (!hpsa_vpd_page_supported(h, scsi3addr,
3117 HPSA_VPD_LV_DEVICE_GEOMETRY))
3118 goto exit;
3119
3120 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE |
3121 HPSA_VPD_LV_DEVICE_GEOMETRY, buf, 64);
3122
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003123 if (rc == 0)
3124 *raid_level = buf[8];
3125 if (*raid_level > RAID_UNKNOWN)
3126 *raid_level = RAID_UNKNOWN;
Scott Teel83832782016-09-09 16:30:29 -05003127exit:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003128 kfree(buf);
3129 return;
3130}
3131
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003132#define HPSA_MAP_DEBUG
3133#ifdef HPSA_MAP_DEBUG
3134static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
3135 struct raid_map_data *map_buff)
3136{
3137 struct raid_map_disk_data *dd = &map_buff->data[0];
3138 int map, row, col;
3139 u16 map_cnt, row_cnt, disks_per_row;
3140
3141 if (rc != 0)
3142 return;
3143
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06003144 /* Show details only if debugging has been activated. */
3145 if (h->raid_offload_debug < 2)
3146 return;
3147
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003148 dev_info(&h->pdev->dev, "structure_size = %u\n",
3149 le32_to_cpu(map_buff->structure_size));
3150 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
3151 le32_to_cpu(map_buff->volume_blk_size));
3152 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
3153 le64_to_cpu(map_buff->volume_blk_cnt));
3154 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
3155 map_buff->phys_blk_shift);
3156 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
3157 map_buff->parity_rotation_shift);
3158 dev_info(&h->pdev->dev, "strip_size = %u\n",
3159 le16_to_cpu(map_buff->strip_size));
3160 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
3161 le64_to_cpu(map_buff->disk_starting_blk));
3162 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
3163 le64_to_cpu(map_buff->disk_blk_cnt));
3164 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
3165 le16_to_cpu(map_buff->data_disks_per_row));
3166 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
3167 le16_to_cpu(map_buff->metadata_disks_per_row));
3168 dev_info(&h->pdev->dev, "row_cnt = %u\n",
3169 le16_to_cpu(map_buff->row_cnt));
3170 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
3171 le16_to_cpu(map_buff->layout_map_count));
Don Brace2b08b3e2015-01-23 16:41:09 -06003172 dev_info(&h->pdev->dev, "flags = 0x%x\n",
Scott Teeldd0e19f2014-02-18 13:57:31 -06003173 le16_to_cpu(map_buff->flags));
Don Brace2b08b3e2015-01-23 16:41:09 -06003174 dev_info(&h->pdev->dev, "encrypytion = %s\n",
3175 le16_to_cpu(map_buff->flags) &
3176 RAID_MAP_FLAG_ENCRYPT_ON ? "ON" : "OFF");
Scott Teeldd0e19f2014-02-18 13:57:31 -06003177 dev_info(&h->pdev->dev, "dekindex = %u\n",
3178 le16_to_cpu(map_buff->dekindex));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003179 map_cnt = le16_to_cpu(map_buff->layout_map_count);
3180 for (map = 0; map < map_cnt; map++) {
3181 dev_info(&h->pdev->dev, "Map%u:\n", map);
3182 row_cnt = le16_to_cpu(map_buff->row_cnt);
3183 for (row = 0; row < row_cnt; row++) {
3184 dev_info(&h->pdev->dev, " Row%u:\n", row);
3185 disks_per_row =
3186 le16_to_cpu(map_buff->data_disks_per_row);
3187 for (col = 0; col < disks_per_row; col++, dd++)
3188 dev_info(&h->pdev->dev,
3189 " D%02u: h=0x%04x xor=%u,%u\n",
3190 col, dd->ioaccel_handle,
3191 dd->xor_mult[0], dd->xor_mult[1]);
3192 disks_per_row =
3193 le16_to_cpu(map_buff->metadata_disks_per_row);
3194 for (col = 0; col < disks_per_row; col++, dd++)
3195 dev_info(&h->pdev->dev,
3196 " M%02u: h=0x%04x xor=%u,%u\n",
3197 col, dd->ioaccel_handle,
3198 dd->xor_mult[0], dd->xor_mult[1]);
3199 }
3200 }
3201}
3202#else
3203static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
3204 __attribute__((unused)) int rc,
3205 __attribute__((unused)) struct raid_map_data *map_buff)
3206{
3207}
3208#endif
3209
3210static int hpsa_get_raid_map(struct ctlr_info *h,
3211 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3212{
3213 int rc = 0;
3214 struct CommandList *c;
3215 struct ErrorInfo *ei;
3216
Stephen Cameron45fcb862015-01-23 16:43:04 -06003217 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003218
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003219 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
3220 sizeof(this_device->raid_map), 0,
3221 scsi3addr, TYPE_CMD)) {
Robert Elliott2dd02d72015-04-23 09:33:43 -05003222 dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
3223 cmd_free(h, c);
3224 return -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003225 }
Webb Scales25163bd2015-04-23 09:32:00 -05003226 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003227 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003228 if (rc)
3229 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003230 ei = c->err_info;
3231 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003232 hpsa_scsi_interpret_error(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05003233 rc = -1;
3234 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003235 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06003236 cmd_free(h, c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003237
3238 /* @todo in the future, dynamically allocate RAID map memory */
3239 if (le32_to_cpu(this_device->raid_map.structure_size) >
3240 sizeof(this_device->raid_map)) {
3241 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
3242 rc = -1;
3243 }
3244 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
3245 return rc;
Webb Scales25163bd2015-04-23 09:32:00 -05003246out:
3247 cmd_free(h, c);
3248 return rc;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003249}
3250
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003251static int hpsa_bmic_sense_subsystem_information(struct ctlr_info *h,
3252 unsigned char scsi3addr[], u16 bmic_device_index,
3253 struct bmic_sense_subsystem_info *buf, size_t bufsize)
3254{
3255 int rc = IO_OK;
3256 struct CommandList *c;
3257 struct ErrorInfo *ei;
3258
3259 c = cmd_alloc(h);
3260
3261 rc = fill_cmd(c, BMIC_SENSE_SUBSYSTEM_INFORMATION, h, buf, bufsize,
3262 0, RAID_CTLR_LUNID, TYPE_CMD);
3263 if (rc)
3264 goto out;
3265
3266 c->Request.CDB[2] = bmic_device_index & 0xff;
3267 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
3268
3269 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003270 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003271 if (rc)
3272 goto out;
3273 ei = c->err_info;
3274 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3275 hpsa_scsi_interpret_error(h, c);
3276 rc = -1;
3277 }
3278out:
3279 cmd_free(h, c);
3280 return rc;
3281}
3282
Scott Teel66749d02015-11-04 15:51:57 -06003283static int hpsa_bmic_id_controller(struct ctlr_info *h,
3284 struct bmic_identify_controller *buf, size_t bufsize)
3285{
3286 int rc = IO_OK;
3287 struct CommandList *c;
3288 struct ErrorInfo *ei;
3289
3290 c = cmd_alloc(h);
3291
3292 rc = fill_cmd(c, BMIC_IDENTIFY_CONTROLLER, h, buf, bufsize,
3293 0, RAID_CTLR_LUNID, TYPE_CMD);
3294 if (rc)
3295 goto out;
3296
3297 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003298 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teel66749d02015-11-04 15:51:57 -06003299 if (rc)
3300 goto out;
3301 ei = c->err_info;
3302 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3303 hpsa_scsi_interpret_error(h, c);
3304 rc = -1;
3305 }
3306out:
3307 cmd_free(h, c);
3308 return rc;
3309}
3310
Don Brace03383732015-01-23 16:43:30 -06003311static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
3312 unsigned char scsi3addr[], u16 bmic_device_index,
3313 struct bmic_identify_physical_device *buf, size_t bufsize)
3314{
3315 int rc = IO_OK;
3316 struct CommandList *c;
3317 struct ErrorInfo *ei;
3318
3319 c = cmd_alloc(h);
3320 rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
3321 0, RAID_CTLR_LUNID, TYPE_CMD);
3322 if (rc)
3323 goto out;
3324
3325 c->Request.CDB[2] = bmic_device_index & 0xff;
3326 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
3327
Webb Scales25163bd2015-04-23 09:32:00 -05003328 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
Don Bracec448ecf2016-04-27 17:13:51 -05003329 DEFAULT_TIMEOUT);
Don Brace03383732015-01-23 16:43:30 -06003330 ei = c->err_info;
3331 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3332 hpsa_scsi_interpret_error(h, c);
3333 rc = -1;
3334 }
3335out:
3336 cmd_free(h, c);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003337
Don Brace03383732015-01-23 16:43:30 -06003338 return rc;
3339}
3340
Don Bracecca8f132015-12-22 10:36:48 -06003341/*
3342 * get enclosure information
3343 * struct ReportExtendedLUNdata *rlep - Used for BMIC drive number
3344 * struct hpsa_scsi_dev_t *encl_dev - device entry for enclosure
3345 * Uses id_physical_device to determine the box_index.
3346 */
3347static void hpsa_get_enclosure_info(struct ctlr_info *h,
3348 unsigned char *scsi3addr,
3349 struct ReportExtendedLUNdata *rlep, int rle_index,
3350 struct hpsa_scsi_dev_t *encl_dev)
3351{
3352 int rc = -1;
3353 struct CommandList *c = NULL;
3354 struct ErrorInfo *ei = NULL;
3355 struct bmic_sense_storage_box_params *bssbp = NULL;
3356 struct bmic_identify_physical_device *id_phys = NULL;
3357 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
3358 u16 bmic_device_index = 0;
3359
3360 bmic_device_index = GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]);
3361
Don Brace17a9e542016-02-23 15:16:09 -06003362 if (bmic_device_index == 0xFF00 || MASKED_DEVICE(&rle->lunid[0])) {
3363 rc = IO_OK;
Don Bracecca8f132015-12-22 10:36:48 -06003364 goto out;
Don Brace17a9e542016-02-23 15:16:09 -06003365 }
Don Bracecca8f132015-12-22 10:36:48 -06003366
3367 bssbp = kzalloc(sizeof(*bssbp), GFP_KERNEL);
3368 if (!bssbp)
3369 goto out;
3370
3371 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3372 if (!id_phys)
3373 goto out;
3374
3375 rc = hpsa_bmic_id_physical_device(h, scsi3addr, bmic_device_index,
3376 id_phys, sizeof(*id_phys));
3377 if (rc) {
3378 dev_warn(&h->pdev->dev, "%s: id_phys failed %d bdi[0x%x]\n",
3379 __func__, encl_dev->external, bmic_device_index);
3380 goto out;
3381 }
3382
3383 c = cmd_alloc(h);
3384
3385 rc = fill_cmd(c, BMIC_SENSE_STORAGE_BOX_PARAMS, h, bssbp,
3386 sizeof(*bssbp), 0, RAID_CTLR_LUNID, TYPE_CMD);
3387
3388 if (rc)
3389 goto out;
3390
3391 if (id_phys->phys_connector[1] == 'E')
3392 c->Request.CDB[5] = id_phys->box_index;
3393 else
3394 c->Request.CDB[5] = 0;
3395
3396 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
Don Bracec448ecf2016-04-27 17:13:51 -05003397 DEFAULT_TIMEOUT);
Don Bracecca8f132015-12-22 10:36:48 -06003398 if (rc)
3399 goto out;
3400
3401 ei = c->err_info;
3402 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3403 rc = -1;
3404 goto out;
3405 }
3406
3407 encl_dev->box[id_phys->active_path_number] = bssbp->phys_box_on_port;
3408 memcpy(&encl_dev->phys_connector[id_phys->active_path_number],
3409 bssbp->phys_connector, sizeof(bssbp->phys_connector));
3410
3411 rc = IO_OK;
3412out:
3413 kfree(bssbp);
3414 kfree(id_phys);
3415
3416 if (c)
3417 cmd_free(h, c);
3418
3419 if (rc != IO_OK)
3420 hpsa_show_dev_msg(KERN_INFO, h, encl_dev,
3421 "Error, could not get enclosure information\n");
3422}
3423
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003424static u64 hpsa_get_sas_address_from_report_physical(struct ctlr_info *h,
3425 unsigned char *scsi3addr)
3426{
3427 struct ReportExtendedLUNdata *physdev;
3428 u32 nphysicals;
3429 u64 sa = 0;
3430 int i;
3431
3432 physdev = kzalloc(sizeof(*physdev), GFP_KERNEL);
3433 if (!physdev)
3434 return 0;
3435
3436 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3437 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3438 kfree(physdev);
3439 return 0;
3440 }
3441 nphysicals = get_unaligned_be32(physdev->LUNListLength) / 24;
3442
3443 for (i = 0; i < nphysicals; i++)
3444 if (!memcmp(&physdev->LUN[i].lunid[0], scsi3addr, 8)) {
3445 sa = get_unaligned_be64(&physdev->LUN[i].wwid[0]);
3446 break;
3447 }
3448
3449 kfree(physdev);
3450
3451 return sa;
3452}
3453
3454static void hpsa_get_sas_address(struct ctlr_info *h, unsigned char *scsi3addr,
3455 struct hpsa_scsi_dev_t *dev)
3456{
3457 int rc;
3458 u64 sa = 0;
3459
3460 if (is_hba_lunid(scsi3addr)) {
3461 struct bmic_sense_subsystem_info *ssi;
3462
3463 ssi = kzalloc(sizeof(*ssi), GFP_KERNEL);
3464 if (ssi == NULL) {
3465 dev_warn(&h->pdev->dev,
3466 "%s: out of memory\n", __func__);
3467 return;
3468 }
3469
3470 rc = hpsa_bmic_sense_subsystem_information(h,
3471 scsi3addr, 0, ssi, sizeof(*ssi));
3472 if (rc == 0) {
3473 sa = get_unaligned_be64(ssi->primary_world_wide_id);
3474 h->sas_address = sa;
3475 }
3476
3477 kfree(ssi);
3478 } else
3479 sa = hpsa_get_sas_address_from_report_physical(h, scsi3addr);
3480
3481 dev->sas_address = sa;
3482}
3483
3484/* Get a device id from inquiry page 0x83 */
Scott Teel83832782016-09-09 16:30:29 -05003485static bool hpsa_vpd_page_supported(struct ctlr_info *h,
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003486 unsigned char scsi3addr[], u8 page)
3487{
3488 int rc;
3489 int i;
3490 int pages;
3491 unsigned char *buf, bufsize;
3492
3493 buf = kzalloc(256, GFP_KERNEL);
3494 if (!buf)
Scott Teel83832782016-09-09 16:30:29 -05003495 return false;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003496
3497 /* Get the size of the page list first */
3498 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3499 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3500 buf, HPSA_VPD_HEADER_SZ);
3501 if (rc != 0)
3502 goto exit_unsupported;
3503 pages = buf[3];
3504 if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
3505 bufsize = pages + HPSA_VPD_HEADER_SZ;
3506 else
3507 bufsize = 255;
3508
3509 /* Get the whole VPD page list */
3510 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3511 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3512 buf, bufsize);
3513 if (rc != 0)
3514 goto exit_unsupported;
3515
3516 pages = buf[3];
3517 for (i = 1; i <= pages; i++)
3518 if (buf[3 + i] == page)
3519 goto exit_supported;
3520exit_unsupported:
3521 kfree(buf);
Scott Teel83832782016-09-09 16:30:29 -05003522 return false;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003523exit_supported:
3524 kfree(buf);
Scott Teel83832782016-09-09 16:30:29 -05003525 return true;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003526}
3527
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003528static void hpsa_get_ioaccel_status(struct ctlr_info *h,
3529 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3530{
3531 int rc;
3532 unsigned char *buf;
3533 u8 ioaccel_status;
3534
3535 this_device->offload_config = 0;
3536 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003537 this_device->offload_to_be_enabled = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003538
3539 buf = kzalloc(64, GFP_KERNEL);
3540 if (!buf)
3541 return;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003542 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
3543 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003544 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003545 VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003546 if (rc != 0)
3547 goto out;
3548
3549#define IOACCEL_STATUS_BYTE 4
3550#define OFFLOAD_CONFIGURED_BIT 0x01
3551#define OFFLOAD_ENABLED_BIT 0x02
3552 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
3553 this_device->offload_config =
3554 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
3555 if (this_device->offload_config) {
3556 this_device->offload_enabled =
3557 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
3558 if (hpsa_get_raid_map(h, scsi3addr, this_device))
3559 this_device->offload_enabled = 0;
3560 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003561 this_device->offload_to_be_enabled = this_device->offload_enabled;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003562out:
3563 kfree(buf);
3564 return;
3565}
3566
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003567/* Get the device id from inquiry page 0x83 */
3568static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
Don Brace75d23d82015-11-04 15:51:39 -06003569 unsigned char *device_id, int index, int buflen)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003570{
3571 int rc;
3572 unsigned char *buf;
3573
Scott Teel83832782016-09-09 16:30:29 -05003574 /* Does controller have VPD for device id? */
3575 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_DEVICE_ID))
3576 return 1; /* not supported */
3577
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003578 buf = kzalloc(64, GFP_KERNEL);
3579 if (!buf)
Stephen M. Camerona84d7942014-05-29 10:54:20 -05003580 return -ENOMEM;
Scott Teel83832782016-09-09 16:30:29 -05003581
3582 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE |
3583 HPSA_VPD_LV_DEVICE_ID, buf, 64);
3584 if (rc == 0) {
3585 if (buflen > 16)
3586 buflen = 16;
3587 memcpy(device_id, &buf[8], buflen);
3588 }
Don Brace75d23d82015-11-04 15:51:39 -06003589
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003590 kfree(buf);
Don Brace75d23d82015-11-04 15:51:39 -06003591
Scott Teel83832782016-09-09 16:30:29 -05003592 return rc; /*0 - got id, otherwise, didn't */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003593}
3594
3595static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
Don Brace03383732015-01-23 16:43:30 -06003596 void *buf, int bufsize,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003597 int extended_response)
3598{
3599 int rc = IO_OK;
3600 struct CommandList *c;
3601 unsigned char scsi3addr[8];
3602 struct ErrorInfo *ei;
3603
Stephen Cameron45fcb862015-01-23 16:43:04 -06003604 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003605
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06003606 /* address the controller */
3607 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003608 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
3609 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
3610 rc = -1;
3611 goto out;
3612 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003613 if (extended_response)
3614 c->Request.CDB[1] = extended_response;
Webb Scales25163bd2015-04-23 09:32:00 -05003615 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003616 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003617 if (rc)
3618 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003619 ei = c->err_info;
3620 if (ei->CommandStatus != 0 &&
3621 ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003622 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003623 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003624 } else {
Don Brace03383732015-01-23 16:43:30 -06003625 struct ReportLUNdata *rld = buf;
3626
3627 if (rld->extended_response_flag != extended_response) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003628 dev_err(&h->pdev->dev,
3629 "report luns requested format %u, got %u\n",
3630 extended_response,
Don Brace03383732015-01-23 16:43:30 -06003631 rld->extended_response_flag);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003632 rc = -1;
3633 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003634 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003635out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06003636 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003637 return rc;
3638}
3639
3640static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06003641 struct ReportExtendedLUNdata *buf, int bufsize)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003642{
Don Brace03383732015-01-23 16:43:30 -06003643 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
3644 HPSA_REPORT_PHYS_EXTENDED);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003645}
3646
3647static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
3648 struct ReportLUNdata *buf, int bufsize)
3649{
3650 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
3651}
3652
3653static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
3654 int bus, int target, int lun)
3655{
3656 device->bus = bus;
3657 device->target = target;
3658 device->lun = lun;
3659}
3660
Stephen M. Cameron98465902014-02-21 16:25:00 -06003661/* Use VPD inquiry to get details of volume status */
3662static int hpsa_get_volume_status(struct ctlr_info *h,
3663 unsigned char scsi3addr[])
3664{
3665 int rc;
3666 int status;
3667 int size;
3668 unsigned char *buf;
3669
3670 buf = kzalloc(64, GFP_KERNEL);
3671 if (!buf)
3672 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3673
3674 /* Does controller have VPD for logical volume status? */
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003675 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
Stephen M. Cameron98465902014-02-21 16:25:00 -06003676 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003677
3678 /* Get the size of the VPD return buffer */
3679 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3680 buf, HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003681 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003682 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003683 size = buf[3];
3684
3685 /* Now get the whole VPD buffer */
3686 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3687 buf, size + HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003688 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003689 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003690 status = buf[4]; /* status byte */
3691
3692 kfree(buf);
3693 return status;
3694exit_failed:
3695 kfree(buf);
3696 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3697}
3698
3699/* Determine offline status of a volume.
3700 * Return either:
3701 * 0 (not offline)
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003702 * 0xff (offline for unknown reasons)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003703 * # (integer code indicating one of several NOT READY states
3704 * describing why a volume is to be kept offline)
3705 */
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003706static int hpsa_volume_offline(struct ctlr_info *h,
Stephen M. Cameron98465902014-02-21 16:25:00 -06003707 unsigned char scsi3addr[])
3708{
3709 struct CommandList *c;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003710 unsigned char *sense;
3711 u8 sense_key, asc, ascq;
3712 int sense_len;
Webb Scales25163bd2015-04-23 09:32:00 -05003713 int rc, ldstat = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003714 u16 cmd_status;
3715 u8 scsi_status;
3716#define ASC_LUN_NOT_READY 0x04
3717#define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
3718#define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
3719
3720 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003721
Stephen M. Cameron98465902014-02-21 16:25:00 -06003722 (void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
Don Bracec448ecf2016-04-27 17:13:51 -05003723 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
3724 DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003725 if (rc) {
3726 cmd_free(h, c);
3727 return 0;
3728 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06003729 sense = c->err_info->SenseInfo;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003730 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3731 sense_len = sizeof(c->err_info->SenseInfo);
3732 else
3733 sense_len = c->err_info->SenseLen;
3734 decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
Stephen M. Cameron98465902014-02-21 16:25:00 -06003735 cmd_status = c->err_info->CommandStatus;
3736 scsi_status = c->err_info->ScsiStatus;
3737 cmd_free(h, c);
3738 /* Is the volume 'not ready'? */
3739 if (cmd_status != CMD_TARGET_STATUS ||
3740 scsi_status != SAM_STAT_CHECK_CONDITION ||
3741 sense_key != NOT_READY ||
3742 asc != ASC_LUN_NOT_READY) {
3743 return 0;
3744 }
3745
3746 /* Determine the reason for not ready state */
3747 ldstat = hpsa_get_volume_status(h, scsi3addr);
3748
3749 /* Keep volume offline in certain cases: */
3750 switch (ldstat) {
3751 case HPSA_LV_UNDERGOING_ERASE:
Scott Benesh5ca01202015-07-18 11:13:04 -05003752 case HPSA_LV_NOT_AVAILABLE:
Stephen M. Cameron98465902014-02-21 16:25:00 -06003753 case HPSA_LV_UNDERGOING_RPI:
3754 case HPSA_LV_PENDING_RPI:
3755 case HPSA_LV_ENCRYPTED_NO_KEY:
3756 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
3757 case HPSA_LV_UNDERGOING_ENCRYPTION:
3758 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
3759 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
3760 return ldstat;
3761 case HPSA_VPD_LV_STATUS_UNSUPPORTED:
3762 /* If VPD status page isn't available,
3763 * use ASC/ASCQ to determine state
3764 */
3765 if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
3766 (ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
3767 return ldstat;
3768 break;
3769 default:
3770 break;
3771 }
3772 return 0;
3773}
3774
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003775/*
3776 * Find out if a logical device supports aborts by simply trying one.
3777 * Smart Array may claim not to support aborts on logical drives, but
3778 * if a MSA2000 * is connected, the drives on that will be presented
3779 * by the Smart Array as logical drives, and aborts may be sent to
3780 * those devices successfully. So the simplest way to find out is
3781 * to simply try an abort and see how the device responds.
3782 */
3783static int hpsa_device_supports_aborts(struct ctlr_info *h,
3784 unsigned char *scsi3addr)
3785{
3786 struct CommandList *c;
3787 struct ErrorInfo *ei;
3788 int rc = 0;
3789
3790 u64 tag = (u64) -1; /* bogus tag */
3791
3792 /* Assume that physical devices support aborts */
3793 if (!is_logical_dev_addr_mode(scsi3addr))
3794 return 1;
3795
3796 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003797
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003798 (void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
Don Bracec448ecf2016-04-27 17:13:51 -05003799 (void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
3800 DEFAULT_TIMEOUT);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003801 /* no unmap needed here because no data xfer. */
3802 ei = c->err_info;
3803 switch (ei->CommandStatus) {
3804 case CMD_INVALID:
3805 rc = 0;
3806 break;
3807 case CMD_UNABORTABLE:
3808 case CMD_ABORT_FAILED:
3809 rc = 1;
3810 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003811 case CMD_TMF_STATUS:
3812 rc = hpsa_evaluate_tmf_status(h, c);
3813 break;
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003814 default:
3815 rc = 0;
3816 break;
3817 }
3818 cmd_free(h, c);
3819 return rc;
3820}
3821
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003822static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003823 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
3824 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003825{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003826
3827#define OBDR_SIG_OFFSET 43
3828#define OBDR_TAPE_SIG "$DR-10"
3829#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
3830#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
3831
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003832 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003833 unsigned char *obdr_sig;
Don Brace683fc442015-11-04 15:50:44 -06003834 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003835
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003836 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Don Brace683fc442015-11-04 15:50:44 -06003837 if (!inq_buff) {
3838 rc = -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003839 goto bail_out;
Don Brace683fc442015-11-04 15:50:44 -06003840 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003841
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003842 /* Do an inquiry to the device to see what it is. */
3843 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3844 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3845 /* Inquiry failed (msg printed already) */
3846 dev_err(&h->pdev->dev,
3847 "hpsa_update_device_info: inquiry failed\n");
Don Brace683fc442015-11-04 15:50:44 -06003848 rc = -EIO;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003849 goto bail_out;
3850 }
3851
Don Brace4af61e42016-02-23 15:16:40 -06003852 scsi_sanitize_inquiry_string(&inq_buff[8], 8);
3853 scsi_sanitize_inquiry_string(&inq_buff[16], 16);
Don Brace75d23d82015-11-04 15:51:39 -06003854
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003855 this_device->devtype = (inq_buff[0] & 0x1f);
3856 memcpy(this_device->scsi3addr, scsi3addr, 8);
3857 memcpy(this_device->vendor, &inq_buff[8],
3858 sizeof(this_device->vendor));
3859 memcpy(this_device->model, &inq_buff[16],
3860 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003861 memset(this_device->device_id, 0,
3862 sizeof(this_device->device_id));
Scott Teel83832782016-09-09 16:30:29 -05003863 if (hpsa_get_device_id(h, scsi3addr, this_device->device_id, 8,
3864 sizeof(this_device->device_id)))
3865 dev_err(&h->pdev->dev,
3866 "hpsa%d: %s: can't get device id for host %d:C0:T%d:L%d\t%s\t%.16s\n",
3867 h->ctlr, __func__,
3868 h->scsi_host->host_no,
3869 this_device->target, this_device->lun,
3870 scsi_device_type(this_device->devtype),
3871 this_device->model);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003872
Don Braceaf15ed32016-02-23 15:16:15 -06003873 if ((this_device->devtype == TYPE_DISK ||
3874 this_device->devtype == TYPE_ZBC) &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003875 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003876 int volume_offline;
3877
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003878 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003879 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3880 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003881 volume_offline = hpsa_volume_offline(h, scsi3addr);
3882 if (volume_offline < 0 || volume_offline > 0xff)
3883 volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
3884 this_device->volume_offline = volume_offline & 0xff;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003885 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003886 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003887 this_device->offload_config = 0;
3888 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003889 this_device->offload_to_be_enabled = 0;
Joe Handzika3144e02015-04-23 09:32:59 -05003890 this_device->hba_ioaccel_enabled = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003891 this_device->volume_offline = 0;
Don Brace03383732015-01-23 16:43:30 -06003892 this_device->queue_depth = h->nr_cmds;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003893 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003894
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003895 if (is_OBDR_device) {
3896 /* See if this is a One-Button-Disaster-Recovery device
3897 * by looking for "$DR-10" at offset 43 in inquiry data.
3898 */
3899 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
3900 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
3901 strncmp(obdr_sig, OBDR_TAPE_SIG,
3902 OBDR_SIG_LEN) == 0);
3903 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003904 kfree(inq_buff);
3905 return 0;
3906
3907bail_out:
3908 kfree(inq_buff);
Don Brace683fc442015-11-04 15:50:44 -06003909 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003910}
3911
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003912static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
3913 struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
3914{
3915 unsigned long flags;
3916 int rc, entry;
3917 /*
3918 * See if this device supports aborts. If we already know
3919 * the device, we already know if it supports aborts, otherwise
3920 * we have to find out if it supports aborts by trying one.
3921 */
3922 spin_lock_irqsave(&h->devlock, flags);
3923 rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
3924 if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
3925 entry >= 0 && entry < h->ndevices) {
3926 dev->supports_aborts = h->dev[entry]->supports_aborts;
3927 spin_unlock_irqrestore(&h->devlock, flags);
3928 } else {
3929 spin_unlock_irqrestore(&h->devlock, flags);
3930 dev->supports_aborts =
3931 hpsa_device_supports_aborts(h, scsi3addr);
3932 if (dev->supports_aborts < 0)
3933 dev->supports_aborts = 0;
3934 }
3935}
3936
Kevin Barnettc7955052015-11-04 15:51:45 -06003937/*
3938 * Helper function to assign bus, target, lun mapping of devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003939 * Logical drive target and lun are assigned at this time, but
3940 * physical device lun and target assignment are deferred (assigned
3941 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
Kevin Barnettc7955052015-11-04 15:51:45 -06003942*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003943static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003944 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003945{
Kevin Barnettc7955052015-11-04 15:51:45 -06003946 u32 lunid = get_unaligned_le32(lunaddrbytes);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003947
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003948 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
3949 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003950 if (is_hba_lunid(lunaddrbytes))
Kevin Barnettc7955052015-11-04 15:51:45 -06003951 hpsa_set_bus_target_lun(device,
3952 HPSA_HBA_BUS, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003953 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003954 /* defer target, lun assignment for physical devices */
Kevin Barnettc7955052015-11-04 15:51:45 -06003955 hpsa_set_bus_target_lun(device,
3956 HPSA_PHYSICAL_DEVICE_BUS, -1, -1);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003957 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003958 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003959 /* It's a logical device */
Scott Teel66749d02015-11-04 15:51:57 -06003960 if (device->external) {
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003961 hpsa_set_bus_target_lun(device,
Kevin Barnettc7955052015-11-04 15:51:45 -06003962 HPSA_EXTERNAL_RAID_VOLUME_BUS, (lunid >> 16) & 0x3fff,
3963 lunid & 0x00ff);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003964 return;
3965 }
Kevin Barnettc7955052015-11-04 15:51:45 -06003966 hpsa_set_bus_target_lun(device, HPSA_RAID_VOLUME_BUS,
3967 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003968}
3969
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003970
3971/*
Scott Teel54b6e9e2014-02-18 13:56:45 -06003972 * Get address of physical disk used for an ioaccel2 mode command:
3973 * 1. Extract ioaccel2 handle from the command.
3974 * 2. Find a matching ioaccel2 handle from list of physical disks.
3975 * 3. Return:
3976 * 1 and set scsi3addr to address of matching physical
3977 * 0 if no matching physical disk was found.
3978 */
3979static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
3980 struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
3981{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003982 struct io_accel2_cmd *c2 =
3983 &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
3984 unsigned long flags;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003985 int i;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003986
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003987 spin_lock_irqsave(&h->devlock, flags);
3988 for (i = 0; i < h->ndevices; i++)
3989 if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
3990 memcpy(scsi3addr, h->dev[i]->scsi3addr,
3991 sizeof(h->dev[i]->scsi3addr));
3992 spin_unlock_irqrestore(&h->devlock, flags);
3993 return 1;
3994 }
3995 spin_unlock_irqrestore(&h->devlock, flags);
3996 return 0;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003997}
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003998
Scott Teel66749d02015-11-04 15:51:57 -06003999static int figure_external_status(struct ctlr_info *h, int raid_ctlr_position,
4000 int i, int nphysicals, int nlocal_logicals)
4001{
4002 /* In report logicals, local logicals are listed first,
4003 * then any externals.
4004 */
4005 int logicals_start = nphysicals + (raid_ctlr_position == 0);
4006
4007 if (i == raid_ctlr_position)
4008 return 0;
4009
4010 if (i < logicals_start)
4011 return 0;
4012
4013 /* i is in logicals range, but still within local logicals */
4014 if ((i - nphysicals - (raid_ctlr_position == 0)) < nlocal_logicals)
4015 return 0;
4016
4017 return 1; /* it's an external lun */
4018}
4019
Scott Teel54b6e9e2014-02-18 13:56:45 -06004020/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004021 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
4022 * logdev. The number of luns in physdev and logdev are returned in
4023 * *nphysicals and *nlogicals, respectively.
4024 * Returns 0 on success, -1 otherwise.
4025 */
4026static int hpsa_gather_lun_info(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06004027 struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004028 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004029{
Don Brace03383732015-01-23 16:43:30 -06004030 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004031 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
4032 return -1;
4033 }
Don Brace03383732015-01-23 16:43:30 -06004034 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004035 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
Don Brace03383732015-01-23 16:43:30 -06004036 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
4037 HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004038 *nphysicals = HPSA_MAX_PHYS_LUN;
4039 }
Don Brace03383732015-01-23 16:43:30 -06004040 if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004041 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
4042 return -1;
4043 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06004044 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004045 /* Reject Logicals in excess of our max capability. */
4046 if (*nlogicals > HPSA_MAX_LUN) {
4047 dev_warn(&h->pdev->dev,
4048 "maximum logical LUNs (%d) exceeded. "
4049 "%d LUNs ignored.\n", HPSA_MAX_LUN,
4050 *nlogicals - HPSA_MAX_LUN);
4051 *nlogicals = HPSA_MAX_LUN;
4052 }
4053 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
4054 dev_warn(&h->pdev->dev,
4055 "maximum logical + physical LUNs (%d) exceeded. "
4056 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
4057 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
4058 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
4059 }
4060 return 0;
4061}
4062
Don Brace42a91642014-11-14 17:26:27 -06004063static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
4064 int i, int nphysicals, int nlogicals,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06004065 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004066 struct ReportLUNdata *logdev_list)
4067{
4068 /* Helper function, figure out where the LUN ID info is coming from
4069 * given index i, lists of physical and logical devices, where in
4070 * the list the raid controller is supposed to appear (first or last)
4071 */
4072
4073 int logicals_start = nphysicals + (raid_ctlr_position == 0);
4074 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
4075
4076 if (i == raid_ctlr_position)
4077 return RAID_CTLR_LUNID;
4078
4079 if (i < logicals_start)
Stephen M. Camerond5b5d962014-05-29 10:53:34 -05004080 return &physdev_list->LUN[i -
4081 (raid_ctlr_position == 0)].lunid[0];
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004082
4083 if (i < last_device)
4084 return &logdev_list->LUN[i - nphysicals -
4085 (raid_ctlr_position == 0)][0];
4086 BUG();
4087 return NULL;
4088}
4089
Don Brace03383732015-01-23 16:43:30 -06004090/* get physical drive ioaccel handle and queue depth */
4091static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
4092 struct hpsa_scsi_dev_t *dev,
Don Bracef2039b02015-11-04 15:51:08 -06004093 struct ReportExtendedLUNdata *rlep, int rle_index,
Don Brace03383732015-01-23 16:43:30 -06004094 struct bmic_identify_physical_device *id_phys)
4095{
4096 int rc;
Scott Teel4b6e5592016-09-09 16:30:36 -05004097 struct ext_report_lun_entry *rle;
4098
4099 /*
4100 * external targets don't support BMIC
4101 */
4102 if (dev->external) {
4103 dev->queue_depth = 7;
4104 return;
4105 }
4106
4107 rle = &rlep->LUN[rle_index];
Don Brace03383732015-01-23 16:43:30 -06004108
4109 dev->ioaccel_handle = rle->ioaccel_handle;
Don Bracef2039b02015-11-04 15:51:08 -06004110 if ((rle->device_flags & 0x08) && dev->ioaccel_handle)
Joe Handzika3144e02015-04-23 09:32:59 -05004111 dev->hba_ioaccel_enabled = 1;
Don Brace03383732015-01-23 16:43:30 -06004112 memset(id_phys, 0, sizeof(*id_phys));
Don Bracef2039b02015-11-04 15:51:08 -06004113 rc = hpsa_bmic_id_physical_device(h, &rle->lunid[0],
4114 GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]), id_phys,
Don Brace03383732015-01-23 16:43:30 -06004115 sizeof(*id_phys));
4116 if (!rc)
4117 /* Reserve space for FW operations */
4118#define DRIVE_CMDS_RESERVED_FOR_FW 2
4119#define DRIVE_QUEUE_DEPTH 7
4120 dev->queue_depth =
4121 le16_to_cpu(id_phys->current_queue_depth_limit) -
4122 DRIVE_CMDS_RESERVED_FOR_FW;
4123 else
4124 dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
Don Brace03383732015-01-23 16:43:30 -06004125}
4126
Joe Handzik8270b862015-07-18 11:12:43 -05004127static void hpsa_get_path_info(struct hpsa_scsi_dev_t *this_device,
Don Bracef2039b02015-11-04 15:51:08 -06004128 struct ReportExtendedLUNdata *rlep, int rle_index,
Joe Handzik8270b862015-07-18 11:12:43 -05004129 struct bmic_identify_physical_device *id_phys)
4130{
Don Bracef2039b02015-11-04 15:51:08 -06004131 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
4132
4133 if ((rle->device_flags & 0x08) && this_device->ioaccel_handle)
Joe Handzik8270b862015-07-18 11:12:43 -05004134 this_device->hba_ioaccel_enabled = 1;
4135
4136 memcpy(&this_device->active_path_index,
4137 &id_phys->active_path_number,
4138 sizeof(this_device->active_path_index));
4139 memcpy(&this_device->path_map,
4140 &id_phys->redundant_path_present_map,
4141 sizeof(this_device->path_map));
4142 memcpy(&this_device->box,
4143 &id_phys->alternate_paths_phys_box_on_port,
4144 sizeof(this_device->box));
4145 memcpy(&this_device->phys_connector,
4146 &id_phys->alternate_paths_phys_connector,
4147 sizeof(this_device->phys_connector));
4148 memcpy(&this_device->bay,
4149 &id_phys->phys_bay_in_box,
4150 sizeof(this_device->bay));
4151}
4152
Scott Teel66749d02015-11-04 15:51:57 -06004153/* get number of local logical disks. */
4154static int hpsa_set_local_logical_count(struct ctlr_info *h,
4155 struct bmic_identify_controller *id_ctlr,
4156 u32 *nlocals)
4157{
4158 int rc;
4159
4160 if (!id_ctlr) {
4161 dev_warn(&h->pdev->dev, "%s: id_ctlr buffer is NULL.\n",
4162 __func__);
4163 return -ENOMEM;
4164 }
4165 memset(id_ctlr, 0, sizeof(*id_ctlr));
4166 rc = hpsa_bmic_id_controller(h, id_ctlr, sizeof(*id_ctlr));
4167 if (!rc)
4168 if (id_ctlr->configured_logical_drive_count < 256)
4169 *nlocals = id_ctlr->configured_logical_drive_count;
4170 else
4171 *nlocals = le16_to_cpu(
4172 id_ctlr->extended_logical_unit_count);
4173 else
4174 *nlocals = -1;
4175 return rc;
4176}
4177
Don Brace64ce60c2016-07-01 13:37:31 -05004178static bool hpsa_is_disk_spare(struct ctlr_info *h, u8 *lunaddrbytes)
4179{
4180 struct bmic_identify_physical_device *id_phys;
4181 bool is_spare = false;
4182 int rc;
4183
4184 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
4185 if (!id_phys)
4186 return false;
4187
4188 rc = hpsa_bmic_id_physical_device(h,
4189 lunaddrbytes,
4190 GET_BMIC_DRIVE_NUMBER(lunaddrbytes),
4191 id_phys, sizeof(*id_phys));
4192 if (rc == 0)
4193 is_spare = (id_phys->more_flags >> 6) & 0x01;
4194
4195 kfree(id_phys);
4196 return is_spare;
4197}
4198
4199#define RPL_DEV_FLAG_NON_DISK 0x1
4200#define RPL_DEV_FLAG_UNCONFIG_DISK_REPORTING_SUPPORTED 0x2
4201#define RPL_DEV_FLAG_UNCONFIG_DISK 0x4
4202
4203#define BMIC_DEVICE_TYPE_ENCLOSURE 6
4204
4205static bool hpsa_skip_device(struct ctlr_info *h, u8 *lunaddrbytes,
4206 struct ext_report_lun_entry *rle)
4207{
4208 u8 device_flags;
4209 u8 device_type;
4210
4211 if (!MASKED_DEVICE(lunaddrbytes))
4212 return false;
4213
4214 device_flags = rle->device_flags;
4215 device_type = rle->device_type;
4216
4217 if (device_flags & RPL_DEV_FLAG_NON_DISK) {
4218 if (device_type == BMIC_DEVICE_TYPE_ENCLOSURE)
4219 return false;
4220 return true;
4221 }
4222
4223 if (!(device_flags & RPL_DEV_FLAG_UNCONFIG_DISK_REPORTING_SUPPORTED))
4224 return false;
4225
4226 if (device_flags & RPL_DEV_FLAG_UNCONFIG_DISK)
4227 return false;
4228
4229 /*
4230 * Spares may be spun down, we do not want to
4231 * do an Inquiry to a RAID set spare drive as
4232 * that would have them spun up, that is a
4233 * performance hit because I/O to the RAID device
4234 * stops while the spin up occurs which can take
4235 * over 50 seconds.
4236 */
4237 if (hpsa_is_disk_spare(h, lunaddrbytes))
4238 return true;
4239
4240 return false;
4241}
Scott Teel66749d02015-11-04 15:51:57 -06004242
Don Brace8aa60682015-11-04 15:50:01 -06004243static void hpsa_update_scsi_devices(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004244{
4245 /* the idea here is we could get notified
4246 * that some devices have changed, so we do a report
4247 * physical luns and report logical luns cmd, and adjust
4248 * our list of devices accordingly.
4249 *
4250 * The scsi3addr's of devices won't change so long as the
4251 * adapter is not reset. That means we can rescan and
4252 * tell which devices we already know about, vs. new
4253 * devices, vs. disappearing devices.
4254 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06004255 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004256 struct ReportLUNdata *logdev_list = NULL;
Don Brace03383732015-01-23 16:43:30 -06004257 struct bmic_identify_physical_device *id_phys = NULL;
Scott Teel66749d02015-11-04 15:51:57 -06004258 struct bmic_identify_controller *id_ctlr = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004259 u32 nphysicals = 0;
4260 u32 nlogicals = 0;
Scott Teel66749d02015-11-04 15:51:57 -06004261 u32 nlocal_logicals = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004262 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004263 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
4264 int ncurrent = 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06004265 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004266 int raid_ctlr_position;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004267 bool physical_device;
Scott Teelaca4a522012-01-19 14:01:19 -06004268 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004269
Scott Teelcfe5bad2011-10-26 16:21:07 -05004270 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameron92084712014-11-14 17:26:54 -06004271 physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
4272 logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004273 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
Don Brace03383732015-01-23 16:43:30 -06004274 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
Scott Teel66749d02015-11-04 15:51:57 -06004275 id_ctlr = kzalloc(sizeof(*id_ctlr), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004276
Don Brace03383732015-01-23 16:43:30 -06004277 if (!currentsd || !physdev_list || !logdev_list ||
Scott Teel66749d02015-11-04 15:51:57 -06004278 !tmpdevice || !id_phys || !id_ctlr) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004279 dev_err(&h->pdev->dev, "out of memory\n");
4280 goto out;
4281 }
4282 memset(lunzerobits, 0, sizeof(lunzerobits));
4283
Don Brace853633e2015-11-04 15:50:37 -06004284 h->drv_req_rescan = 0; /* cancel scheduled rescan - we're doing it. */
4285
Don Brace03383732015-01-23 16:43:30 -06004286 if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
Don Brace853633e2015-11-04 15:50:37 -06004287 logdev_list, &nlogicals)) {
4288 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004289 goto out;
Don Brace853633e2015-11-04 15:50:37 -06004290 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004291
Scott Teel66749d02015-11-04 15:51:57 -06004292 /* Set number of local logicals (non PTRAID) */
4293 if (hpsa_set_local_logical_count(h, id_ctlr, &nlocal_logicals)) {
4294 dev_warn(&h->pdev->dev,
4295 "%s: Can't determine number of local logical devices.\n",
4296 __func__);
4297 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004298
Scott Teelaca4a522012-01-19 14:01:19 -06004299 /* We might see up to the maximum number of logical and physical disks
4300 * plus external target devices, and a device for the local RAID
4301 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004302 */
Scott Teelaca4a522012-01-19 14:01:19 -06004303 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004304
4305 /* Allocate the per device structures */
4306 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05004307 if (i >= HPSA_MAX_DEVICES) {
4308 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
4309 " %d devices ignored.\n", HPSA_MAX_DEVICES,
4310 ndevs_to_allocate - HPSA_MAX_DEVICES);
4311 break;
4312 }
4313
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004314 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
4315 if (!currentsd[i]) {
4316 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
4317 __FILE__, __LINE__);
Don Brace853633e2015-11-04 15:50:37 -06004318 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004319 goto out;
4320 }
4321 ndev_allocated++;
4322 }
4323
Stephen M. Cameron86452912014-05-29 10:53:49 -05004324 if (is_scsi_rev_5(h))
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004325 raid_ctlr_position = 0;
4326 else
4327 raid_ctlr_position = nphysicals + nlogicals;
4328
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004329 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06004330 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004331 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004332 u8 *lunaddrbytes, is_OBDR = 0;
Don Brace683fc442015-11-04 15:50:44 -06004333 int rc = 0;
Don Bracef2039b02015-11-04 15:51:08 -06004334 int phys_dev_index = i - (raid_ctlr_position == 0);
Don Brace64ce60c2016-07-01 13:37:31 -05004335 bool skip_device = false;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004336
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004337 physical_device = i < nphysicals + (raid_ctlr_position == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004338
4339 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004340 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
4341 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004342
Don Brace86cf7132016-09-09 16:30:17 -05004343 /* Determine if this is a lun from an external target array */
4344 tmpdevice->external =
4345 figure_external_status(h, raid_ctlr_position, i,
4346 nphysicals, nlocal_logicals);
4347
Don Brace64ce60c2016-07-01 13:37:31 -05004348 /*
4349 * Skip over some devices such as a spare.
4350 */
4351 if (!tmpdevice->external && physical_device) {
4352 skip_device = hpsa_skip_device(h, lunaddrbytes,
4353 &physdev_list->LUN[phys_dev_index]);
4354 if (skip_device)
4355 continue;
4356 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004357
4358 /* Get device type, vendor, model, device id */
Don Brace683fc442015-11-04 15:50:44 -06004359 rc = hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
4360 &is_OBDR);
4361 if (rc == -ENOMEM) {
4362 dev_warn(&h->pdev->dev,
4363 "Out of memory, rescan deferred.\n");
Don Brace853633e2015-11-04 15:50:37 -06004364 h->drv_req_rescan = 1;
Don Brace683fc442015-11-04 15:50:44 -06004365 goto out;
Don Brace853633e2015-11-04 15:50:37 -06004366 }
Don Brace683fc442015-11-04 15:50:44 -06004367 if (rc) {
4368 dev_warn(&h->pdev->dev,
4369 "Inquiry failed, skipping device.\n");
4370 continue;
4371 }
4372
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06004373 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05004374 hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004375 this_device = currentsd[ncurrent];
4376
Scott Teel34592252015-11-04 15:52:09 -06004377 /* Turn on discovery_polling if there are ext target devices.
4378 * Event-based change notification is unreliable for those.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004379 */
Scott Teel34592252015-11-04 15:52:09 -06004380 if (!h->discovery_polling) {
4381 if (tmpdevice->external) {
4382 h->discovery_polling = 1;
4383 dev_info(&h->pdev->dev,
4384 "External target, activate discovery polling.\n");
4385 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004386 }
4387
Scott Teel34592252015-11-04 15:52:09 -06004388
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004389 *this_device = *tmpdevice;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004390 this_device->physical_device = physical_device;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004391
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004392 /*
4393 * Expose all devices except for physical devices that
4394 * are masked.
4395 */
4396 if (MASKED_DEVICE(lunaddrbytes) && this_device->physical_device)
Kevin Barnett2a168202015-11-04 15:51:21 -06004397 this_device->expose_device = 0;
4398 else
4399 this_device->expose_device = 1;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004400
Kevin Barnettd04e62b2015-11-04 15:52:34 -06004401
4402 /*
4403 * Get the SAS address for physical devices that are exposed.
4404 */
4405 if (this_device->physical_device && this_device->expose_device)
4406 hpsa_get_sas_address(h, lunaddrbytes, this_device);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004407
4408 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004409 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004410 /* We don't *really* support actual CD-ROM devices,
4411 * just "One Button Disaster Recovery" tape drive
4412 * which temporarily pretends to be a CD-ROM drive.
4413 * So we check that the device is really an OBDR tape
4414 * device by checking for "$DR-10" in bytes 43-48 of
4415 * the inquiry data.
4416 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004417 if (is_OBDR)
4418 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004419 break;
4420 case TYPE_DISK:
Don Braceaf15ed32016-02-23 15:16:15 -06004421 case TYPE_ZBC:
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004422 if (this_device->physical_device) {
Kevin Barnettb9092b72015-07-18 11:12:59 -05004423 /* The disk is in HBA mode. */
4424 /* Never use RAID mapper in HBA mode. */
Stephen M. Cameron316b2212014-02-21 16:25:15 -06004425 this_device->offload_enabled = 0;
Kevin Barnettb9092b72015-07-18 11:12:59 -05004426 hpsa_get_ioaccel_drive_info(h, this_device,
Don Bracef2039b02015-11-04 15:51:08 -06004427 physdev_list, phys_dev_index, id_phys);
4428 hpsa_get_path_info(this_device,
4429 physdev_list, phys_dev_index, id_phys);
Kevin Barnettb9092b72015-07-18 11:12:59 -05004430 }
Joe Handzikecf418d12015-04-23 09:33:04 -05004431 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004432 break;
4433 case TYPE_TAPE:
4434 case TYPE_MEDIUM_CHANGER:
Don Bracecca8f132015-12-22 10:36:48 -06004435 ncurrent++;
4436 break;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004437 case TYPE_ENCLOSURE:
Don Brace17a9e542016-02-23 15:16:09 -06004438 if (!this_device->external)
4439 hpsa_get_enclosure_info(h, lunaddrbytes,
Don Bracecca8f132015-12-22 10:36:48 -06004440 physdev_list, phys_dev_index,
4441 this_device);
Kevin Barnettb9092b72015-07-18 11:12:59 -05004442 ncurrent++;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004443 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004444 case TYPE_RAID:
4445 /* Only present the Smartarray HBA as a RAID controller.
4446 * If it's a RAID controller other than the HBA itself
4447 * (an external RAID controller, MSA500 or similar)
4448 * don't present it.
4449 */
4450 if (!is_hba_lunid(lunaddrbytes))
4451 break;
4452 ncurrent++;
4453 break;
4454 default:
4455 break;
4456 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05004457 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004458 break;
4459 }
Kevin Barnettd04e62b2015-11-04 15:52:34 -06004460
4461 if (h->sas_host == NULL) {
4462 int rc = 0;
4463
4464 rc = hpsa_add_sas_host(h);
4465 if (rc) {
4466 dev_warn(&h->pdev->dev,
4467 "Could not add sas host %d\n", rc);
4468 goto out;
4469 }
4470 }
4471
Don Brace8aa60682015-11-04 15:50:01 -06004472 adjust_hpsa_scsi_table(h, currentsd, ncurrent);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004473out:
4474 kfree(tmpdevice);
4475 for (i = 0; i < ndev_allocated; i++)
4476 kfree(currentsd[i]);
4477 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004478 kfree(physdev_list);
4479 kfree(logdev_list);
Scott Teel66749d02015-11-04 15:51:57 -06004480 kfree(id_ctlr);
Don Brace03383732015-01-23 16:43:30 -06004481 kfree(id_phys);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004482}
4483
Webb Scalesec5cbf02015-01-23 16:44:45 -06004484static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
4485 struct scatterlist *sg)
4486{
4487 u64 addr64 = (u64) sg_dma_address(sg);
4488 unsigned int len = sg_dma_len(sg);
4489
4490 desc->Addr = cpu_to_le64(addr64);
4491 desc->Len = cpu_to_le32(len);
4492 desc->Ext = 0;
4493}
4494
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004495/*
4496 * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004497 * dma mapping and fills in the scatter gather entries of the
4498 * hpsa command, cp.
4499 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004500static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004501 struct CommandList *cp,
4502 struct scsi_cmnd *cmd)
4503{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004504 struct scatterlist *sg;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004505 int use_sg, i, sg_limit, chained, last_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004506 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004507
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004508 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004509
4510 use_sg = scsi_dma_map(cmd);
4511 if (use_sg < 0)
4512 return use_sg;
4513
4514 if (!use_sg)
4515 goto sglist_finished;
4516
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004517 /*
4518 * If the number of entries is greater than the max for a single list,
4519 * then we have a chained list; we will set up all but one entry in the
4520 * first list (the last entry is saved for link information);
4521 * otherwise, we don't have a chained list and we'll set up at each of
4522 * the entries in the one list.
4523 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004524 curr_sg = cp->SG;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004525 chained = use_sg > h->max_cmd_sg_entries;
4526 sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
4527 last_sg = scsi_sg_count(cmd) - 1;
4528 scsi_for_each_sg(cmd, sg, sg_limit, i) {
Webb Scalesec5cbf02015-01-23 16:44:45 -06004529 hpsa_set_sg_descriptor(curr_sg, sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004530 curr_sg++;
4531 }
Webb Scalesec5cbf02015-01-23 16:44:45 -06004532
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004533 if (chained) {
4534 /*
4535 * Continue with the chained list. Set curr_sg to the chained
4536 * list. Modify the limit to the total count less the entries
4537 * we've already set up. Resume the scan at the list entry
4538 * where the previous loop left off.
4539 */
4540 curr_sg = h->cmd_sg_list[cp->cmdindex];
4541 sg_limit = use_sg - sg_limit;
4542 for_each_sg(sg, sg, sg_limit, i) {
4543 hpsa_set_sg_descriptor(curr_sg, sg);
4544 curr_sg++;
4545 }
4546 }
4547
Webb Scalesec5cbf02015-01-23 16:44:45 -06004548 /* Back the pointer up to the last entry and mark it as "last". */
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004549 (curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004550
4551 if (use_sg + chained > h->maxSG)
4552 h->maxSG = use_sg + chained;
4553
4554 if (chained) {
4555 cp->Header.SGList = h->max_cmd_sg_entries;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004556 cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06004557 if (hpsa_map_sg_chain_block(h, cp)) {
4558 scsi_dma_unmap(cmd);
4559 return -1;
4560 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004561 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004562 }
4563
4564sglist_finished:
4565
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004566 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004567 cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004568 return 0;
4569}
4570
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004571#define IO_ACCEL_INELIGIBLE (1)
4572static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
4573{
4574 int is_write = 0;
4575 u32 block;
4576 u32 block_cnt;
4577
4578 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
4579 switch (cdb[0]) {
4580 case WRITE_6:
4581 case WRITE_12:
4582 is_write = 1;
4583 case READ_6:
4584 case READ_12:
4585 if (*cdb_len == 6) {
Mahesh Rajashekharaabbada72016-09-16 14:54:23 -05004586 block = (((cdb[1] & 0x1F) << 16) |
4587 (cdb[2] << 8) |
4588 cdb[3]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004589 block_cnt = cdb[4];
Don Bracec8a6c9a2015-11-04 15:50:50 -06004590 if (block_cnt == 0)
4591 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004592 } else {
4593 BUG_ON(*cdb_len != 12);
Don Bracec8a6c9a2015-11-04 15:50:50 -06004594 block = get_unaligned_be32(&cdb[2]);
4595 block_cnt = get_unaligned_be32(&cdb[6]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004596 }
4597 if (block_cnt > 0xffff)
4598 return IO_ACCEL_INELIGIBLE;
4599
4600 cdb[0] = is_write ? WRITE_10 : READ_10;
4601 cdb[1] = 0;
4602 cdb[2] = (u8) (block >> 24);
4603 cdb[3] = (u8) (block >> 16);
4604 cdb[4] = (u8) (block >> 8);
4605 cdb[5] = (u8) (block);
4606 cdb[6] = 0;
4607 cdb[7] = (u8) (block_cnt >> 8);
4608 cdb[8] = (u8) (block_cnt);
4609 cdb[9] = 0;
4610 *cdb_len = 10;
4611 break;
4612 }
4613 return 0;
4614}
4615
Scott Teelc3497752014-02-18 13:56:34 -06004616static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004617 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004618 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Matt Gatese1f7de02014-02-18 13:55:17 -06004619{
4620 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06004621 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
4622 unsigned int len;
4623 unsigned int total_len = 0;
4624 struct scatterlist *sg;
4625 u64 addr64;
4626 int use_sg, i;
4627 struct SGDescriptor *curr_sg;
4628 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
4629
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004630 /* TODO: implement chaining support */
Don Brace03383732015-01-23 16:43:30 -06004631 if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
4632 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004633 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004634 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004635
Matt Gatese1f7de02014-02-18 13:55:17 -06004636 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
4637
Don Brace03383732015-01-23 16:43:30 -06004638 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4639 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004640 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004641 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004642
Matt Gatese1f7de02014-02-18 13:55:17 -06004643 c->cmd_type = CMD_IOACCEL1;
4644
4645 /* Adjust the DMA address to point to the accelerated command buffer */
4646 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
4647 (c->cmdindex * sizeof(*cp));
4648 BUG_ON(c->busaddr & 0x0000007F);
4649
4650 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004651 if (use_sg < 0) {
4652 atomic_dec(&phys_disk->ioaccel_cmds_out);
Matt Gatese1f7de02014-02-18 13:55:17 -06004653 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004654 }
Matt Gatese1f7de02014-02-18 13:55:17 -06004655
4656 if (use_sg) {
4657 curr_sg = cp->SG;
4658 scsi_for_each_sg(cmd, sg, use_sg, i) {
4659 addr64 = (u64) sg_dma_address(sg);
4660 len = sg_dma_len(sg);
4661 total_len += len;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004662 curr_sg->Addr = cpu_to_le64(addr64);
4663 curr_sg->Len = cpu_to_le32(len);
4664 curr_sg->Ext = cpu_to_le32(0);
Matt Gatese1f7de02014-02-18 13:55:17 -06004665 curr_sg++;
4666 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004667 (--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
Matt Gatese1f7de02014-02-18 13:55:17 -06004668
4669 switch (cmd->sc_data_direction) {
4670 case DMA_TO_DEVICE:
4671 control |= IOACCEL1_CONTROL_DATA_OUT;
4672 break;
4673 case DMA_FROM_DEVICE:
4674 control |= IOACCEL1_CONTROL_DATA_IN;
4675 break;
4676 case DMA_NONE:
4677 control |= IOACCEL1_CONTROL_NODATAXFER;
4678 break;
4679 default:
4680 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4681 cmd->sc_data_direction);
4682 BUG();
4683 break;
4684 }
4685 } else {
4686 control |= IOACCEL1_CONTROL_NODATAXFER;
4687 }
4688
Scott Teelc3497752014-02-18 13:56:34 -06004689 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06004690 /* Fill out the command structure to submit */
Don Brace2b08b3e2015-01-23 16:41:09 -06004691 cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
4692 cp->transfer_len = cpu_to_le32(total_len);
4693 cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
4694 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
4695 cp->control = cpu_to_le32(control);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004696 memcpy(cp->CDB, cdb, cdb_len);
4697 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06004698 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004699 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06004700 return 0;
4701}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004702
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004703/*
4704 * Queue a command directly to a device behind the controller using the
4705 * I/O accelerator path.
4706 */
4707static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
4708 struct CommandList *c)
4709{
4710 struct scsi_cmnd *cmd = c->scsi_cmd;
4711 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4712
Don Brace45e596c2016-09-09 16:30:42 -05004713 if (!dev)
4714 return -1;
4715
Don Brace03383732015-01-23 16:43:30 -06004716 c->phys_disk = dev;
4717
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004718 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004719 cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004720}
4721
Scott Teeldd0e19f2014-02-18 13:57:31 -06004722/*
4723 * Set encryption parameters for the ioaccel2 request
4724 */
4725static void set_encrypt_ioaccel2(struct ctlr_info *h,
4726 struct CommandList *c, struct io_accel2_cmd *cp)
4727{
4728 struct scsi_cmnd *cmd = c->scsi_cmd;
4729 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4730 struct raid_map_data *map = &dev->raid_map;
4731 u64 first_block;
4732
Scott Teeldd0e19f2014-02-18 13:57:31 -06004733 /* Are we doing encryption on this device */
Don Brace2b08b3e2015-01-23 16:41:09 -06004734 if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
Scott Teeldd0e19f2014-02-18 13:57:31 -06004735 return;
4736 /* Set the data encryption key index. */
4737 cp->dekindex = map->dekindex;
4738
4739 /* Set the encryption enable flag, encoded into direction field. */
4740 cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
4741
4742 /* Set encryption tweak values based on logical block address
4743 * If block size is 512, tweak value is LBA.
4744 * For other block sizes, tweak is (LBA * block size)/ 512)
4745 */
4746 switch (cmd->cmnd[0]) {
4747 /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
Scott Teeldd0e19f2014-02-18 13:57:31 -06004748 case READ_6:
Mahesh Rajashekharaabbada72016-09-16 14:54:23 -05004749 case WRITE_6:
4750 first_block = (((cmd->cmnd[1] & 0x1F) << 16) |
4751 (cmd->cmnd[2] << 8) |
4752 cmd->cmnd[3]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004753 break;
4754 case WRITE_10:
4755 case READ_10:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004756 /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
4757 case WRITE_12:
4758 case READ_12:
Don Brace2b08b3e2015-01-23 16:41:09 -06004759 first_block = get_unaligned_be32(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004760 break;
4761 case WRITE_16:
4762 case READ_16:
Don Brace2b08b3e2015-01-23 16:41:09 -06004763 first_block = get_unaligned_be64(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004764 break;
4765 default:
4766 dev_err(&h->pdev->dev,
Don Brace2b08b3e2015-01-23 16:41:09 -06004767 "ERROR: %s: size (0x%x) not supported for encryption\n",
4768 __func__, cmd->cmnd[0]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004769 BUG();
4770 break;
4771 }
Don Brace2b08b3e2015-01-23 16:41:09 -06004772
4773 if (le32_to_cpu(map->volume_blk_size) != 512)
4774 first_block = first_block *
4775 le32_to_cpu(map->volume_blk_size)/512;
4776
4777 cp->tweak_lower = cpu_to_le32(first_block);
4778 cp->tweak_upper = cpu_to_le32(first_block >> 32);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004779}
4780
Scott Teelc3497752014-02-18 13:56:34 -06004781static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
4782 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004783 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004784{
4785 struct scsi_cmnd *cmd = c->scsi_cmd;
4786 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
4787 struct ioaccel2_sg_element *curr_sg;
4788 int use_sg, i;
4789 struct scatterlist *sg;
4790 u64 addr64;
4791 u32 len;
4792 u32 total_len = 0;
4793
Don Brace45e596c2016-09-09 16:30:42 -05004794 if (!cmd->device)
4795 return -1;
4796
4797 if (!cmd->device->hostdata)
4798 return -1;
4799
Webb Scalesd9a729f2015-04-23 09:33:27 -05004800 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Scott Teelc3497752014-02-18 13:56:34 -06004801
Don Brace03383732015-01-23 16:43:30 -06004802 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4803 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004804 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004805 }
4806
Scott Teelc3497752014-02-18 13:56:34 -06004807 c->cmd_type = CMD_IOACCEL2;
4808 /* Adjust the DMA address to point to the accelerated command buffer */
4809 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
4810 (c->cmdindex * sizeof(*cp));
4811 BUG_ON(c->busaddr & 0x0000007F);
4812
4813 memset(cp, 0, sizeof(*cp));
4814 cp->IU_type = IOACCEL2_IU_TYPE;
4815
4816 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004817 if (use_sg < 0) {
4818 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004819 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004820 }
Scott Teelc3497752014-02-18 13:56:34 -06004821
4822 if (use_sg) {
Scott Teelc3497752014-02-18 13:56:34 -06004823 curr_sg = cp->sg;
Webb Scalesd9a729f2015-04-23 09:33:27 -05004824 if (use_sg > h->ioaccel_maxsg) {
4825 addr64 = le64_to_cpu(
4826 h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
4827 curr_sg->address = cpu_to_le64(addr64);
4828 curr_sg->length = 0;
4829 curr_sg->reserved[0] = 0;
4830 curr_sg->reserved[1] = 0;
4831 curr_sg->reserved[2] = 0;
4832 curr_sg->chain_indicator = 0x80;
4833
4834 curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
4835 }
Scott Teelc3497752014-02-18 13:56:34 -06004836 scsi_for_each_sg(cmd, sg, use_sg, i) {
4837 addr64 = (u64) sg_dma_address(sg);
4838 len = sg_dma_len(sg);
4839 total_len += len;
4840 curr_sg->address = cpu_to_le64(addr64);
4841 curr_sg->length = cpu_to_le32(len);
4842 curr_sg->reserved[0] = 0;
4843 curr_sg->reserved[1] = 0;
4844 curr_sg->reserved[2] = 0;
4845 curr_sg->chain_indicator = 0;
4846 curr_sg++;
4847 }
4848
4849 switch (cmd->sc_data_direction) {
4850 case DMA_TO_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004851 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4852 cp->direction |= IOACCEL2_DIR_DATA_OUT;
Scott Teelc3497752014-02-18 13:56:34 -06004853 break;
4854 case DMA_FROM_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004855 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4856 cp->direction |= IOACCEL2_DIR_DATA_IN;
Scott Teelc3497752014-02-18 13:56:34 -06004857 break;
4858 case DMA_NONE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004859 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4860 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004861 break;
4862 default:
4863 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4864 cmd->sc_data_direction);
4865 BUG();
4866 break;
4867 }
4868 } else {
Scott Teeldd0e19f2014-02-18 13:57:31 -06004869 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4870 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004871 }
Scott Teeldd0e19f2014-02-18 13:57:31 -06004872
4873 /* Set encryption parameters, if necessary */
4874 set_encrypt_ioaccel2(h, c, cp);
4875
Don Brace2b08b3e2015-01-23 16:41:09 -06004876 cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
Don Bracef2405db2015-01-23 16:43:09 -06004877 cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
Scott Teelc3497752014-02-18 13:56:34 -06004878 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
Scott Teelc3497752014-02-18 13:56:34 -06004879
Scott Teelc3497752014-02-18 13:56:34 -06004880 cp->data_len = cpu_to_le32(total_len);
4881 cp->err_ptr = cpu_to_le64(c->busaddr +
4882 offsetof(struct io_accel2_cmd, error_data));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004883 cp->err_len = cpu_to_le32(sizeof(cp->error_data));
Scott Teelc3497752014-02-18 13:56:34 -06004884
Webb Scalesd9a729f2015-04-23 09:33:27 -05004885 /* fill in sg elements */
4886 if (use_sg > h->ioaccel_maxsg) {
4887 cp->sg_count = 1;
Don Bracea736e9b2015-11-04 15:51:14 -06004888 cp->sg[0].length = cpu_to_le32(use_sg * sizeof(cp->sg[0]));
Webb Scalesd9a729f2015-04-23 09:33:27 -05004889 if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
4890 atomic_dec(&phys_disk->ioaccel_cmds_out);
4891 scsi_dma_unmap(cmd);
4892 return -1;
4893 }
4894 } else
4895 cp->sg_count = (u8) use_sg;
4896
Scott Teelc3497752014-02-18 13:56:34 -06004897 enqueue_cmd_and_start_io(h, c);
4898 return 0;
4899}
4900
4901/*
4902 * Queue a command to the correct I/O accelerator path.
4903 */
4904static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
4905 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004906 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004907{
Don Brace45e596c2016-09-09 16:30:42 -05004908 if (!c->scsi_cmd->device)
4909 return -1;
4910
4911 if (!c->scsi_cmd->device->hostdata)
4912 return -1;
4913
Don Brace03383732015-01-23 16:43:30 -06004914 /* Try to honor the device's queue depth */
4915 if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
4916 phys_disk->queue_depth) {
4917 atomic_dec(&phys_disk->ioaccel_cmds_out);
4918 return IO_ACCEL_INELIGIBLE;
4919 }
Scott Teelc3497752014-02-18 13:56:34 -06004920 if (h->transMethod & CFGTBL_Trans_io_accel1)
4921 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004922 cdb, cdb_len, scsi3addr,
4923 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004924 else
4925 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004926 cdb, cdb_len, scsi3addr,
4927 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004928}
4929
Scott Teel6b80b182014-02-18 13:56:55 -06004930static void raid_map_helper(struct raid_map_data *map,
4931 int offload_to_mirror, u32 *map_index, u32 *current_group)
4932{
4933 if (offload_to_mirror == 0) {
4934 /* use physical disk in the first mirrored group. */
Don Brace2b08b3e2015-01-23 16:41:09 -06004935 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004936 return;
4937 }
4938 do {
4939 /* determine mirror group that *map_index indicates */
Don Brace2b08b3e2015-01-23 16:41:09 -06004940 *current_group = *map_index /
4941 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004942 if (offload_to_mirror == *current_group)
4943 continue;
Don Brace2b08b3e2015-01-23 16:41:09 -06004944 if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
Scott Teel6b80b182014-02-18 13:56:55 -06004945 /* select map index from next group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004946 *map_index += le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004947 (*current_group)++;
4948 } else {
4949 /* select map index from first group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004950 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004951 *current_group = 0;
4952 }
4953 } while (offload_to_mirror != *current_group);
4954}
4955
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004956/*
4957 * Attempt to perform offload RAID mapping for a logical volume I/O.
4958 */
4959static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4960 struct CommandList *c)
4961{
4962 struct scsi_cmnd *cmd = c->scsi_cmd;
4963 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4964 struct raid_map_data *map = &dev->raid_map;
4965 struct raid_map_disk_data *dd = &map->data[0];
4966 int is_write = 0;
4967 u32 map_index;
4968 u64 first_block, last_block;
4969 u32 block_cnt;
4970 u32 blocks_per_row;
4971 u64 first_row, last_row;
4972 u32 first_row_offset, last_row_offset;
4973 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06004974 u64 r0_first_row, r0_last_row;
4975 u32 r5or6_blocks_per_row;
4976 u64 r5or6_first_row, r5or6_last_row;
4977 u32 r5or6_first_row_offset, r5or6_last_row_offset;
4978 u32 r5or6_first_column, r5or6_last_column;
4979 u32 total_disks_per_row;
4980 u32 stripesize;
4981 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004982 u32 map_row;
4983 u32 disk_handle;
4984 u64 disk_block;
4985 u32 disk_block_cnt;
4986 u8 cdb[16];
4987 u8 cdb_len;
Don Brace2b08b3e2015-01-23 16:41:09 -06004988 u16 strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004989#if BITS_PER_LONG == 32
4990 u64 tmpdiv;
4991#endif
Scott Teel6b80b182014-02-18 13:56:55 -06004992 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004993
Don Brace45e596c2016-09-09 16:30:42 -05004994 if (!dev)
4995 return -1;
4996
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004997 /* check for valid opcode, get LBA and block count */
4998 switch (cmd->cmnd[0]) {
4999 case WRITE_6:
5000 is_write = 1;
5001 case READ_6:
Mahesh Rajashekharaabbada72016-09-16 14:54:23 -05005002 first_block = (((cmd->cmnd[1] & 0x1F) << 16) |
5003 (cmd->cmnd[2] << 8) |
5004 cmd->cmnd[3]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005005 block_cnt = cmd->cmnd[4];
Stephen M. Cameron3fa89a02014-07-03 10:18:14 -05005006 if (block_cnt == 0)
5007 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005008 break;
5009 case WRITE_10:
5010 is_write = 1;
5011 case READ_10:
5012 first_block =
5013 (((u64) cmd->cmnd[2]) << 24) |
5014 (((u64) cmd->cmnd[3]) << 16) |
5015 (((u64) cmd->cmnd[4]) << 8) |
5016 cmd->cmnd[5];
5017 block_cnt =
5018 (((u32) cmd->cmnd[7]) << 8) |
5019 cmd->cmnd[8];
5020 break;
5021 case WRITE_12:
5022 is_write = 1;
5023 case READ_12:
5024 first_block =
5025 (((u64) cmd->cmnd[2]) << 24) |
5026 (((u64) cmd->cmnd[3]) << 16) |
5027 (((u64) cmd->cmnd[4]) << 8) |
5028 cmd->cmnd[5];
5029 block_cnt =
5030 (((u32) cmd->cmnd[6]) << 24) |
5031 (((u32) cmd->cmnd[7]) << 16) |
5032 (((u32) cmd->cmnd[8]) << 8) |
5033 cmd->cmnd[9];
5034 break;
5035 case WRITE_16:
5036 is_write = 1;
5037 case READ_16:
5038 first_block =
5039 (((u64) cmd->cmnd[2]) << 56) |
5040 (((u64) cmd->cmnd[3]) << 48) |
5041 (((u64) cmd->cmnd[4]) << 40) |
5042 (((u64) cmd->cmnd[5]) << 32) |
5043 (((u64) cmd->cmnd[6]) << 24) |
5044 (((u64) cmd->cmnd[7]) << 16) |
5045 (((u64) cmd->cmnd[8]) << 8) |
5046 cmd->cmnd[9];
5047 block_cnt =
5048 (((u32) cmd->cmnd[10]) << 24) |
5049 (((u32) cmd->cmnd[11]) << 16) |
5050 (((u32) cmd->cmnd[12]) << 8) |
5051 cmd->cmnd[13];
5052 break;
5053 default:
5054 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
5055 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005056 last_block = first_block + block_cnt - 1;
5057
5058 /* check for write to non-RAID-0 */
5059 if (is_write && dev->raid_level != 0)
5060 return IO_ACCEL_INELIGIBLE;
5061
5062 /* check for invalid block or wraparound */
Don Brace2b08b3e2015-01-23 16:41:09 -06005063 if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
5064 last_block < first_block)
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005065 return IO_ACCEL_INELIGIBLE;
5066
5067 /* calculate stripe information for the request */
Don Brace2b08b3e2015-01-23 16:41:09 -06005068 blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
5069 le16_to_cpu(map->strip_size);
5070 strip_size = le16_to_cpu(map->strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005071#if BITS_PER_LONG == 32
5072 tmpdiv = first_block;
5073 (void) do_div(tmpdiv, blocks_per_row);
5074 first_row = tmpdiv;
5075 tmpdiv = last_block;
5076 (void) do_div(tmpdiv, blocks_per_row);
5077 last_row = tmpdiv;
5078 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
5079 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
5080 tmpdiv = first_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06005081 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005082 first_column = tmpdiv;
5083 tmpdiv = last_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06005084 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005085 last_column = tmpdiv;
5086#else
5087 first_row = first_block / blocks_per_row;
5088 last_row = last_block / blocks_per_row;
5089 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
5090 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
Don Brace2b08b3e2015-01-23 16:41:09 -06005091 first_column = first_row_offset / strip_size;
5092 last_column = last_row_offset / strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005093#endif
5094
5095 /* if this isn't a single row/column then give to the controller */
5096 if ((first_row != last_row) || (first_column != last_column))
5097 return IO_ACCEL_INELIGIBLE;
5098
5099 /* proceeding with driver mapping */
Don Brace2b08b3e2015-01-23 16:41:09 -06005100 total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
5101 le16_to_cpu(map->metadata_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005102 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06005103 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06005104 map_index = (map_row * total_disks_per_row) + first_column;
5105
5106 switch (dev->raid_level) {
5107 case HPSA_RAID_0:
5108 break; /* nothing special to do */
5109 case HPSA_RAID_1:
5110 /* Handles load balance across RAID 1 members.
5111 * (2-drive R1 and R10 with even # of drives.)
5112 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005113 */
Don Brace2b08b3e2015-01-23 16:41:09 -06005114 BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005115 if (dev->offload_to_mirror)
Don Brace2b08b3e2015-01-23 16:41:09 -06005116 map_index += le16_to_cpu(map->data_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005117 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06005118 break;
5119 case HPSA_RAID_ADM:
5120 /* Handles N-way mirrors (R1-ADM)
5121 * and R10 with # of drives divisible by 3.)
5122 */
Don Brace2b08b3e2015-01-23 16:41:09 -06005123 BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
Scott Teel6b80b182014-02-18 13:56:55 -06005124
5125 offload_to_mirror = dev->offload_to_mirror;
5126 raid_map_helper(map, offload_to_mirror,
5127 &map_index, &current_group);
5128 /* set mirror group to use next time */
5129 offload_to_mirror =
Don Brace2b08b3e2015-01-23 16:41:09 -06005130 (offload_to_mirror >=
5131 le16_to_cpu(map->layout_map_count) - 1)
Scott Teel6b80b182014-02-18 13:56:55 -06005132 ? 0 : offload_to_mirror + 1;
Scott Teel6b80b182014-02-18 13:56:55 -06005133 dev->offload_to_mirror = offload_to_mirror;
5134 /* Avoid direct use of dev->offload_to_mirror within this
5135 * function since multiple threads might simultaneously
5136 * increment it beyond the range of dev->layout_map_count -1.
5137 */
5138 break;
5139 case HPSA_RAID_5:
5140 case HPSA_RAID_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06005141 if (le16_to_cpu(map->layout_map_count) <= 1)
Scott Teel6b80b182014-02-18 13:56:55 -06005142 break;
5143
5144 /* Verify first and last block are in same RAID group */
5145 r5or6_blocks_per_row =
Don Brace2b08b3e2015-01-23 16:41:09 -06005146 le16_to_cpu(map->strip_size) *
5147 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06005148 BUG_ON(r5or6_blocks_per_row == 0);
Don Brace2b08b3e2015-01-23 16:41:09 -06005149 stripesize = r5or6_blocks_per_row *
5150 le16_to_cpu(map->layout_map_count);
Scott Teel6b80b182014-02-18 13:56:55 -06005151#if BITS_PER_LONG == 32
5152 tmpdiv = first_block;
5153 first_group = do_div(tmpdiv, stripesize);
5154 tmpdiv = first_group;
5155 (void) do_div(tmpdiv, r5or6_blocks_per_row);
5156 first_group = tmpdiv;
5157 tmpdiv = last_block;
5158 last_group = do_div(tmpdiv, stripesize);
5159 tmpdiv = last_group;
5160 (void) do_div(tmpdiv, r5or6_blocks_per_row);
5161 last_group = tmpdiv;
5162#else
5163 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
5164 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
Scott Teel6b80b182014-02-18 13:56:55 -06005165#endif
Stephen M. Cameron000ff7c2014-03-13 17:12:50 -05005166 if (first_group != last_group)
Scott Teel6b80b182014-02-18 13:56:55 -06005167 return IO_ACCEL_INELIGIBLE;
5168
5169 /* Verify request is in a single row of RAID 5/6 */
5170#if BITS_PER_LONG == 32
5171 tmpdiv = first_block;
5172 (void) do_div(tmpdiv, stripesize);
5173 first_row = r5or6_first_row = r0_first_row = tmpdiv;
5174 tmpdiv = last_block;
5175 (void) do_div(tmpdiv, stripesize);
5176 r5or6_last_row = r0_last_row = tmpdiv;
5177#else
5178 first_row = r5or6_first_row = r0_first_row =
5179 first_block / stripesize;
5180 r5or6_last_row = r0_last_row = last_block / stripesize;
5181#endif
5182 if (r5or6_first_row != r5or6_last_row)
5183 return IO_ACCEL_INELIGIBLE;
5184
5185
5186 /* Verify request is in a single column */
5187#if BITS_PER_LONG == 32
5188 tmpdiv = first_block;
5189 first_row_offset = do_div(tmpdiv, stripesize);
5190 tmpdiv = first_row_offset;
5191 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
5192 r5or6_first_row_offset = first_row_offset;
5193 tmpdiv = last_block;
5194 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
5195 tmpdiv = r5or6_last_row_offset;
5196 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
5197 tmpdiv = r5or6_first_row_offset;
5198 (void) do_div(tmpdiv, map->strip_size);
5199 first_column = r5or6_first_column = tmpdiv;
5200 tmpdiv = r5or6_last_row_offset;
5201 (void) do_div(tmpdiv, map->strip_size);
5202 r5or6_last_column = tmpdiv;
5203#else
5204 first_row_offset = r5or6_first_row_offset =
5205 (u32)((first_block % stripesize) %
5206 r5or6_blocks_per_row);
5207
5208 r5or6_last_row_offset =
5209 (u32)((last_block % stripesize) %
5210 r5or6_blocks_per_row);
5211
5212 first_column = r5or6_first_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06005213 r5or6_first_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06005214 r5or6_last_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06005215 r5or6_last_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06005216#endif
5217 if (r5or6_first_column != r5or6_last_column)
5218 return IO_ACCEL_INELIGIBLE;
5219
5220 /* Request is eligible */
5221 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06005222 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06005223
5224 map_index = (first_group *
Don Brace2b08b3e2015-01-23 16:41:09 -06005225 (le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
Scott Teel6b80b182014-02-18 13:56:55 -06005226 (map_row * total_disks_per_row) + first_column;
5227 break;
5228 default:
5229 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005230 }
Scott Teel6b80b182014-02-18 13:56:55 -06005231
Stephen Cameron07543e02015-01-23 16:44:14 -06005232 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
5233 return IO_ACCEL_INELIGIBLE;
5234
Don Brace03383732015-01-23 16:43:30 -06005235 c->phys_disk = dev->phys_disk[map_index];
Don Bracec3390df2016-02-23 15:16:34 -06005236 if (!c->phys_disk)
5237 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06005238
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005239 disk_handle = dd[map_index].ioaccel_handle;
Don Brace2b08b3e2015-01-23 16:41:09 -06005240 disk_block = le64_to_cpu(map->disk_starting_blk) +
5241 first_row * le16_to_cpu(map->strip_size) +
5242 (first_row_offset - first_column *
5243 le16_to_cpu(map->strip_size));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005244 disk_block_cnt = block_cnt;
5245
5246 /* handle differing logical/physical block sizes */
5247 if (map->phys_blk_shift) {
5248 disk_block <<= map->phys_blk_shift;
5249 disk_block_cnt <<= map->phys_blk_shift;
5250 }
5251 BUG_ON(disk_block_cnt > 0xffff);
5252
5253 /* build the new CDB for the physical disk I/O */
5254 if (disk_block > 0xffffffff) {
5255 cdb[0] = is_write ? WRITE_16 : READ_16;
5256 cdb[1] = 0;
5257 cdb[2] = (u8) (disk_block >> 56);
5258 cdb[3] = (u8) (disk_block >> 48);
5259 cdb[4] = (u8) (disk_block >> 40);
5260 cdb[5] = (u8) (disk_block >> 32);
5261 cdb[6] = (u8) (disk_block >> 24);
5262 cdb[7] = (u8) (disk_block >> 16);
5263 cdb[8] = (u8) (disk_block >> 8);
5264 cdb[9] = (u8) (disk_block);
5265 cdb[10] = (u8) (disk_block_cnt >> 24);
5266 cdb[11] = (u8) (disk_block_cnt >> 16);
5267 cdb[12] = (u8) (disk_block_cnt >> 8);
5268 cdb[13] = (u8) (disk_block_cnt);
5269 cdb[14] = 0;
5270 cdb[15] = 0;
5271 cdb_len = 16;
5272 } else {
5273 cdb[0] = is_write ? WRITE_10 : READ_10;
5274 cdb[1] = 0;
5275 cdb[2] = (u8) (disk_block >> 24);
5276 cdb[3] = (u8) (disk_block >> 16);
5277 cdb[4] = (u8) (disk_block >> 8);
5278 cdb[5] = (u8) (disk_block);
5279 cdb[6] = 0;
5280 cdb[7] = (u8) (disk_block_cnt >> 8);
5281 cdb[8] = (u8) (disk_block_cnt);
5282 cdb[9] = 0;
5283 cdb_len = 10;
5284 }
5285 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
Don Brace03383732015-01-23 16:43:30 -06005286 dev->scsi3addr,
5287 dev->phys_disk[map_index]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005288}
5289
Webb Scales25163bd2015-04-23 09:32:00 -05005290/*
5291 * Submit commands down the "normal" RAID stack path
5292 * All callers to hpsa_ciss_submit must check lockup_detected
5293 * beforehand, before (opt.) and after calling cmd_alloc
5294 */
Stephen Cameron574f05d2015-01-23 16:43:20 -06005295static int hpsa_ciss_submit(struct ctlr_info *h,
5296 struct CommandList *c, struct scsi_cmnd *cmd,
5297 unsigned char scsi3addr[])
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005298{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005299 cmd->host_scribble = (unsigned char *) c;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005300 c->cmd_type = CMD_SCSI;
5301 c->scsi_cmd = cmd;
5302 c->Header.ReplyQueue = 0; /* unused in simple mode */
5303 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Bracef2405db2015-01-23 16:43:09 -06005304 c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005305
5306 /* Fill in the request block... */
5307
5308 c->Request.Timeout = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005309 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
5310 c->Request.CDBLen = cmd->cmd_len;
5311 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005312 switch (cmd->sc_data_direction) {
5313 case DMA_TO_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005314 c->Request.type_attr_dir =
5315 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005316 break;
5317 case DMA_FROM_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005318 c->Request.type_attr_dir =
5319 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005320 break;
5321 case DMA_NONE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005322 c->Request.type_attr_dir =
5323 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005324 break;
5325 case DMA_BIDIRECTIONAL:
5326 /* This can happen if a buggy application does a scsi passthru
5327 * and sets both inlen and outlen to non-zero. ( see
5328 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
5329 */
5330
Stephen M. Camerona505b862014-11-14 17:27:04 -06005331 c->Request.type_attr_dir =
5332 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005333 /* This is technically wrong, and hpsa controllers should
5334 * reject it with CMD_INVALID, which is the most correct
5335 * response, but non-fibre backends appear to let it
5336 * slide by, and give the same results as if this field
5337 * were set correctly. Either way is acceptable for
5338 * our purposes here.
5339 */
5340
5341 break;
5342
5343 default:
5344 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
5345 cmd->sc_data_direction);
5346 BUG();
5347 break;
5348 }
5349
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005350 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Webb Scales73153fe2015-04-23 09:35:04 -05005351 hpsa_cmd_resolve_and_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005352 return SCSI_MLQUEUE_HOST_BUSY;
5353 }
5354 enqueue_cmd_and_start_io(h, c);
5355 /* the cmd'll come back via intr handler in complete_scsi_command() */
5356 return 0;
5357}
5358
Stephen Cameron360c73b2015-04-23 09:32:32 -05005359static void hpsa_cmd_init(struct ctlr_info *h, int index,
5360 struct CommandList *c)
5361{
5362 dma_addr_t cmd_dma_handle, err_dma_handle;
5363
5364 /* Zero out all of commandlist except the last field, refcount */
5365 memset(c, 0, offsetof(struct CommandList, refcount));
5366 c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
5367 cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
5368 c->err_info = h->errinfo_pool + index;
5369 memset(c->err_info, 0, sizeof(*c->err_info));
5370 err_dma_handle = h->errinfo_pool_dhandle
5371 + index * sizeof(*c->err_info);
5372 c->cmdindex = index;
5373 c->busaddr = (u32) cmd_dma_handle;
5374 c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
5375 c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
5376 c->h = h;
Webb Scalesa58e7e52015-04-23 09:34:16 -05005377 c->scsi_cmd = SCSI_CMD_IDLE;
Stephen Cameron360c73b2015-04-23 09:32:32 -05005378}
5379
5380static void hpsa_preinitialize_commands(struct ctlr_info *h)
5381{
5382 int i;
5383
5384 for (i = 0; i < h->nr_cmds; i++) {
5385 struct CommandList *c = h->cmd_pool + i;
5386
5387 hpsa_cmd_init(h, i, c);
5388 atomic_set(&c->refcount, 0);
5389 }
5390}
5391
5392static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
5393 struct CommandList *c)
5394{
5395 dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
5396
Webb Scales73153fe2015-04-23 09:35:04 -05005397 BUG_ON(c->cmdindex != index);
5398
Stephen Cameron360c73b2015-04-23 09:32:32 -05005399 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
5400 memset(c->err_info, 0, sizeof(*c->err_info));
5401 c->busaddr = (u32) cmd_dma_handle;
5402}
5403
Webb Scales592a0ad2015-04-23 09:32:48 -05005404static int hpsa_ioaccel_submit(struct ctlr_info *h,
5405 struct CommandList *c, struct scsi_cmnd *cmd,
5406 unsigned char *scsi3addr)
5407{
5408 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
5409 int rc = IO_ACCEL_INELIGIBLE;
5410
Don Brace45e596c2016-09-09 16:30:42 -05005411 if (!dev)
5412 return SCSI_MLQUEUE_HOST_BUSY;
5413
Webb Scales592a0ad2015-04-23 09:32:48 -05005414 cmd->host_scribble = (unsigned char *) c;
5415
5416 if (dev->offload_enabled) {
5417 hpsa_cmd_init(h, c->cmdindex, c);
5418 c->cmd_type = CMD_SCSI;
5419 c->scsi_cmd = cmd;
5420 rc = hpsa_scsi_ioaccel_raid_map(h, c);
5421 if (rc < 0) /* scsi_dma_map failed. */
5422 rc = SCSI_MLQUEUE_HOST_BUSY;
Joe Handzika3144e02015-04-23 09:32:59 -05005423 } else if (dev->hba_ioaccel_enabled) {
Webb Scales592a0ad2015-04-23 09:32:48 -05005424 hpsa_cmd_init(h, c->cmdindex, c);
5425 c->cmd_type = CMD_SCSI;
5426 c->scsi_cmd = cmd;
5427 rc = hpsa_scsi_ioaccel_direct_map(h, c);
5428 if (rc < 0) /* scsi_dma_map failed. */
5429 rc = SCSI_MLQUEUE_HOST_BUSY;
5430 }
5431 return rc;
5432}
5433
Don Brace080ef1c2015-01-23 16:43:25 -06005434static void hpsa_command_resubmit_worker(struct work_struct *work)
5435{
5436 struct scsi_cmnd *cmd;
5437 struct hpsa_scsi_dev_t *dev;
Webb Scales8a0ff922015-04-23 09:34:11 -05005438 struct CommandList *c = container_of(work, struct CommandList, work);
Don Brace080ef1c2015-01-23 16:43:25 -06005439
5440 cmd = c->scsi_cmd;
5441 dev = cmd->device->hostdata;
5442 if (!dev) {
5443 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05005444 return hpsa_cmd_free_and_done(c->h, c, cmd);
Don Brace080ef1c2015-01-23 16:43:25 -06005445 }
Webb Scalesd604f532015-04-23 09:35:22 -05005446 if (c->reset_pending)
5447 return hpsa_cmd_resolve_and_free(c->h, c);
Webb Scalesa58e7e52015-04-23 09:34:16 -05005448 if (c->abort_pending)
5449 return hpsa_cmd_abort_and_free(c->h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05005450 if (c->cmd_type == CMD_IOACCEL2) {
5451 struct ctlr_info *h = c->h;
5452 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5453 int rc;
5454
5455 if (c2->error_data.serv_response ==
5456 IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
5457 rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
5458 if (rc == 0)
5459 return;
5460 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
5461 /*
5462 * If we get here, it means dma mapping failed.
5463 * Try again via scsi mid layer, which will
5464 * then get SCSI_MLQUEUE_HOST_BUSY.
5465 */
5466 cmd->result = DID_IMM_RETRY << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05005467 return hpsa_cmd_free_and_done(h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05005468 }
5469 /* else, fall thru and resubmit down CISS path */
5470 }
5471 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05005472 hpsa_cmd_partial_init(c->h, c->cmdindex, c);
Don Brace080ef1c2015-01-23 16:43:25 -06005473 if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
5474 /*
5475 * If we get here, it means dma mapping failed. Try
5476 * again via scsi mid layer, which will then get
5477 * SCSI_MLQUEUE_HOST_BUSY.
Webb Scales592a0ad2015-04-23 09:32:48 -05005478 *
5479 * hpsa_ciss_submit will have already freed c
5480 * if it encountered a dma mapping failure.
Don Brace080ef1c2015-01-23 16:43:25 -06005481 */
5482 cmd->result = DID_IMM_RETRY << 16;
5483 cmd->scsi_done(cmd);
5484 }
5485}
5486
Stephen Cameron574f05d2015-01-23 16:43:20 -06005487/* Running in struct Scsi_Host->host_lock less mode */
5488static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
5489{
5490 struct ctlr_info *h;
5491 struct hpsa_scsi_dev_t *dev;
5492 unsigned char scsi3addr[8];
5493 struct CommandList *c;
5494 int rc = 0;
5495
5496 /* Get the ptr to our adapter structure out of cmd->host. */
5497 h = sdev_to_hba(cmd->device);
Webb Scales73153fe2015-04-23 09:35:04 -05005498
5499 BUG_ON(cmd->request->tag < 0);
5500
Stephen Cameron574f05d2015-01-23 16:43:20 -06005501 dev = cmd->device->hostdata;
5502 if (!dev) {
Hannes Reinecke1ccde702016-11-18 08:32:47 +01005503 cmd->result = DID_NO_CONNECT << 16;
Don Braceba74fdc2016-04-27 17:14:17 -05005504 cmd->scsi_done(cmd);
5505 return 0;
5506 }
5507
5508 if (dev->removed) {
Stephen Cameron574f05d2015-01-23 16:43:20 -06005509 cmd->result = DID_NO_CONNECT << 16;
5510 cmd->scsi_done(cmd);
5511 return 0;
5512 }
Webb Scales73153fe2015-04-23 09:35:04 -05005513
Stephen Cameron574f05d2015-01-23 16:43:20 -06005514 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
5515
5516 if (unlikely(lockup_detected(h))) {
Webb Scales25163bd2015-04-23 09:32:00 -05005517 cmd->result = DID_NO_CONNECT << 16;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005518 cmd->scsi_done(cmd);
5519 return 0;
5520 }
Webb Scales73153fe2015-04-23 09:35:04 -05005521 c = cmd_tagged_alloc(h, cmd);
Stephen Cameron574f05d2015-01-23 16:43:20 -06005522
Stephen Cameron407863c2015-01-23 16:44:19 -06005523 /*
5524 * Call alternate submit routine for I/O accelerated commands.
Stephen Cameron574f05d2015-01-23 16:43:20 -06005525 * Retries always go down the normal I/O path.
5526 */
5527 if (likely(cmd->retries == 0 &&
5528 cmd->request->cmd_type == REQ_TYPE_FS &&
5529 h->acciopath_status)) {
Webb Scales592a0ad2015-04-23 09:32:48 -05005530 rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
5531 if (rc == 0)
5532 return 0;
5533 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
Webb Scales73153fe2015-04-23 09:35:04 -05005534 hpsa_cmd_resolve_and_free(h, c);
Webb Scales592a0ad2015-04-23 09:32:48 -05005535 return SCSI_MLQUEUE_HOST_BUSY;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005536 }
5537 }
5538 return hpsa_ciss_submit(h, c, cmd, scsi3addr);
5539}
5540
Webb Scales8ebc9242015-01-23 16:44:50 -06005541static void hpsa_scan_complete(struct ctlr_info *h)
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005542{
5543 unsigned long flags;
5544
Webb Scales8ebc9242015-01-23 16:44:50 -06005545 spin_lock_irqsave(&h->scan_lock, flags);
5546 h->scan_finished = 1;
5547 wake_up_all(&h->scan_wait_queue);
5548 spin_unlock_irqrestore(&h->scan_lock, flags);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005549}
5550
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005551static void hpsa_scan_start(struct Scsi_Host *sh)
5552{
5553 struct ctlr_info *h = shost_to_hba(sh);
5554 unsigned long flags;
5555
Webb Scales8ebc9242015-01-23 16:44:50 -06005556 /*
5557 * Don't let rescans be initiated on a controller known to be locked
5558 * up. If the controller locks up *during* a rescan, that thread is
5559 * probably hosed, but at least we can prevent new rescan threads from
5560 * piling up on a locked up controller.
5561 */
5562 if (unlikely(lockup_detected(h)))
5563 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005564
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005565 /* wait until any scan already in progress is finished. */
5566 while (1) {
5567 spin_lock_irqsave(&h->scan_lock, flags);
5568 if (h->scan_finished)
5569 break;
5570 spin_unlock_irqrestore(&h->scan_lock, flags);
5571 wait_event(h->scan_wait_queue, h->scan_finished);
5572 /* Note: We don't need to worry about a race between this
5573 * thread and driver unload because the midlayer will
5574 * have incremented the reference count, so unload won't
5575 * happen if we're in here.
5576 */
5577 }
5578 h->scan_finished = 0; /* mark scan as in progress */
5579 spin_unlock_irqrestore(&h->scan_lock, flags);
5580
Webb Scales8ebc9242015-01-23 16:44:50 -06005581 if (unlikely(lockup_detected(h)))
5582 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005583
Don Bracebfd75462016-11-15 14:45:32 -06005584 /*
5585 * Do the scan after a reset completion
5586 */
5587 if (h->reset_in_progress) {
5588 h->drv_req_rescan = 1;
5589 return;
5590 }
5591
Don Brace8aa60682015-11-04 15:50:01 -06005592 hpsa_update_scsi_devices(h);
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005593
Webb Scales8ebc9242015-01-23 16:44:50 -06005594 hpsa_scan_complete(h);
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005595}
5596
Don Brace7c0a0222015-01-23 16:41:30 -06005597static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
5598{
Don Brace03383732015-01-23 16:43:30 -06005599 struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
5600
5601 if (!logical_drive)
5602 return -ENODEV;
Don Brace7c0a0222015-01-23 16:41:30 -06005603
5604 if (qdepth < 1)
5605 qdepth = 1;
Don Brace03383732015-01-23 16:43:30 -06005606 else if (qdepth > logical_drive->queue_depth)
5607 qdepth = logical_drive->queue_depth;
5608
5609 return scsi_change_queue_depth(sdev, qdepth);
Don Brace7c0a0222015-01-23 16:41:30 -06005610}
5611
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005612static int hpsa_scan_finished(struct Scsi_Host *sh,
5613 unsigned long elapsed_time)
5614{
5615 struct ctlr_info *h = shost_to_hba(sh);
5616 unsigned long flags;
5617 int finished;
5618
5619 spin_lock_irqsave(&h->scan_lock, flags);
5620 finished = h->scan_finished;
5621 spin_unlock_irqrestore(&h->scan_lock, flags);
5622 return finished;
5623}
5624
Robert Elliott2946e822015-04-23 09:35:09 -05005625static int hpsa_scsi_host_alloc(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005626{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005627 struct Scsi_Host *sh;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005628
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005629 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
Robert Elliott2946e822015-04-23 09:35:09 -05005630 if (sh == NULL) {
5631 dev_err(&h->pdev->dev, "scsi_host_alloc failed\n");
5632 return -ENOMEM;
5633 }
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005634
5635 sh->io_port = 0;
5636 sh->n_io_port = 0;
5637 sh->this_id = -1;
5638 sh->max_channel = 3;
5639 sh->max_cmd_len = MAX_COMMAND_SIZE;
5640 sh->max_lun = HPSA_MAX_LUN;
5641 sh->max_id = HPSA_MAX_LUN;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05005642 sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
Don Brace03383732015-01-23 16:43:30 -06005643 sh->cmd_per_lun = sh->can_queue;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005644 sh->sg_tablesize = h->maxsgentries;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06005645 sh->transportt = hpsa_sas_transport_template;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005646 sh->hostdata[0] = (unsigned long) h;
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08005647 sh->irq = pci_irq_vector(h->pdev, 0);
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005648 sh->unique_id = sh->irq;
Christoph Hellwig64d513ac2015-10-08 09:28:04 +01005649
Robert Elliott2946e822015-04-23 09:35:09 -05005650 h->scsi_host = sh;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005651 return 0;
Robert Elliott2946e822015-04-23 09:35:09 -05005652}
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005653
Robert Elliott2946e822015-04-23 09:35:09 -05005654static int hpsa_scsi_add_host(struct ctlr_info *h)
5655{
5656 int rv;
5657
5658 rv = scsi_add_host(h->scsi_host, &h->pdev->dev);
5659 if (rv) {
5660 dev_err(&h->pdev->dev, "scsi_add_host failed\n");
5661 return rv;
5662 }
5663 scsi_scan_host(h->scsi_host);
5664 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005665}
5666
Webb Scalesb69324f2015-04-23 09:34:22 -05005667/*
Webb Scales73153fe2015-04-23 09:35:04 -05005668 * The block layer has already gone to the trouble of picking out a unique,
5669 * small-integer tag for this request. We use an offset from that value as
5670 * an index to select our command block. (The offset allows us to reserve the
5671 * low-numbered entries for our own uses.)
5672 */
5673static int hpsa_get_cmd_index(struct scsi_cmnd *scmd)
5674{
5675 int idx = scmd->request->tag;
5676
5677 if (idx < 0)
5678 return idx;
5679
5680 /* Offset to leave space for internal cmds. */
5681 return idx += HPSA_NRESERVED_CMDS;
5682}
5683
5684/*
Webb Scalesb69324f2015-04-23 09:34:22 -05005685 * Send a TEST_UNIT_READY command to the specified LUN using the specified
5686 * reply queue; returns zero if the unit is ready, and non-zero otherwise.
5687 */
5688static int hpsa_send_test_unit_ready(struct ctlr_info *h,
5689 struct CommandList *c, unsigned char lunaddr[],
5690 int reply_queue)
5691{
5692 int rc;
5693
5694 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
5695 (void) fill_cmd(c, TEST_UNIT_READY, h,
5696 NULL, 0, 0, lunaddr, TYPE_CMD);
Don Bracec448ecf2016-04-27 17:13:51 -05005697 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Webb Scalesb69324f2015-04-23 09:34:22 -05005698 if (rc)
5699 return rc;
5700 /* no unmap needed here because no data xfer. */
5701
5702 /* Check if the unit is already ready. */
5703 if (c->err_info->CommandStatus == CMD_SUCCESS)
5704 return 0;
5705
5706 /*
5707 * The first command sent after reset will receive "unit attention" to
5708 * indicate that the LUN has been reset...this is actually what we're
5709 * looking for (but, success is good too).
5710 */
5711 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5712 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
5713 (c->err_info->SenseInfo[2] == NO_SENSE ||
5714 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
5715 return 0;
5716
5717 return 1;
5718}
5719
5720/*
5721 * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
5722 * returns zero when the unit is ready, and non-zero when giving up.
5723 */
5724static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
5725 struct CommandList *c,
5726 unsigned char lunaddr[], int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005727{
Tomas Henzl89193582014-02-21 16:25:05 -06005728 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005729 int count = 0;
5730 int waittime = 1; /* seconds */
Webb Scalesb69324f2015-04-23 09:34:22 -05005731
5732 /* Send test unit ready until device ready, or give up. */
5733 for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
5734
5735 /*
5736 * Wait for a bit. do this first, because if we send
5737 * the TUR right away, the reset will just abort it.
5738 */
5739 msleep(1000 * waittime);
5740
5741 rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
5742 if (!rc)
5743 break;
5744
5745 /* Increase wait time with each try, up to a point. */
5746 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
5747 waittime *= 2;
5748
5749 dev_warn(&h->pdev->dev,
5750 "waiting %d secs for device to become ready.\n",
5751 waittime);
5752 }
5753
5754 return rc;
5755}
5756
5757static int wait_for_device_to_become_ready(struct ctlr_info *h,
5758 unsigned char lunaddr[],
5759 int reply_queue)
5760{
5761 int first_queue;
5762 int last_queue;
5763 int rq;
5764 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005765 struct CommandList *c;
5766
Stephen Cameron45fcb862015-01-23 16:43:04 -06005767 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005768
Webb Scalesb69324f2015-04-23 09:34:22 -05005769 /*
5770 * If no specific reply queue was requested, then send the TUR
5771 * repeatedly, requesting a reply on each reply queue; otherwise execute
5772 * the loop exactly once using only the specified queue.
5773 */
5774 if (reply_queue == DEFAULT_REPLY_QUEUE) {
5775 first_queue = 0;
5776 last_queue = h->nreply_queues - 1;
5777 } else {
5778 first_queue = reply_queue;
5779 last_queue = reply_queue;
5780 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005781
Webb Scalesb69324f2015-04-23 09:34:22 -05005782 for (rq = first_queue; rq <= last_queue; rq++) {
5783 rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
Webb Scales25163bd2015-04-23 09:32:00 -05005784 if (rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005785 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005786 }
5787
5788 if (rc)
5789 dev_warn(&h->pdev->dev, "giving up on device.\n");
5790 else
5791 dev_warn(&h->pdev->dev, "device is ready.\n");
5792
Stephen Cameron45fcb862015-01-23 16:43:04 -06005793 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005794 return rc;
5795}
5796
5797/* Need at least one of these error handlers to keep ../scsi/hosts.c from
5798 * complaining. Doing a host- or bus-reset can't do anything good here.
5799 */
5800static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
5801{
5802 int rc;
5803 struct ctlr_info *h;
5804 struct hpsa_scsi_dev_t *dev;
Scott Teel0b9b7b62015-11-04 15:51:02 -06005805 u8 reset_type;
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005806 char msg[48];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005807
5808 /* find the controller to which the command to be aborted was sent */
5809 h = sdev_to_hba(scsicmd->device);
5810 if (h == NULL) /* paranoia */
5811 return FAILED;
Don Bracee3458932015-01-23 16:44:24 -06005812
5813 if (lockup_detected(h))
5814 return FAILED;
5815
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005816 dev = scsicmd->device->hostdata;
5817 if (!dev) {
Webb Scalesd604f532015-04-23 09:35:22 -05005818 dev_err(&h->pdev->dev, "%s: device lookup failed\n", __func__);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005819 return FAILED;
5820 }
Webb Scales25163bd2015-04-23 09:32:00 -05005821
5822 /* if controller locked up, we can guarantee command won't complete */
5823 if (lockup_detected(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005824 snprintf(msg, sizeof(msg),
5825 "cmd %d RESET FAILED, lockup detected",
5826 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005827 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005828 return FAILED;
5829 }
5830
5831 /* this reset request might be the result of a lockup; check */
5832 if (detect_controller_lockup(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005833 snprintf(msg, sizeof(msg),
5834 "cmd %d RESET FAILED, new lockup detected",
5835 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005836 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005837 return FAILED;
5838 }
5839
Webb Scalesd604f532015-04-23 09:35:22 -05005840 /* Do not attempt on controller */
5841 if (is_hba_lunid(dev->scsi3addr))
5842 return SUCCESS;
5843
Scott Teel0b9b7b62015-11-04 15:51:02 -06005844 if (is_logical_dev_addr_mode(dev->scsi3addr))
5845 reset_type = HPSA_DEVICE_RESET_MSG;
5846 else
5847 reset_type = HPSA_PHYS_TARGET_RESET;
5848
5849 sprintf(msg, "resetting %s",
5850 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ");
5851 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005852
Don Braceda03ded2015-11-04 15:50:56 -06005853 h->reset_in_progress = 1;
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06005854
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005855 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teel0b9b7b62015-11-04 15:51:02 -06005856 rc = hpsa_do_reset(h, dev, dev->scsi3addr, reset_type,
Webb Scalesd604f532015-04-23 09:35:22 -05005857 DEFAULT_REPLY_QUEUE);
Scott Teel0b9b7b62015-11-04 15:51:02 -06005858 sprintf(msg, "reset %s %s",
5859 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ",
5860 rc == 0 ? "completed successfully" : "failed");
Webb Scalesd604f532015-04-23 09:35:22 -05005861 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Don Braceda03ded2015-11-04 15:50:56 -06005862 h->reset_in_progress = 0;
Webb Scalesd604f532015-04-23 09:35:22 -05005863 return rc == 0 ? SUCCESS : FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005864}
5865
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005866static void swizzle_abort_tag(u8 *tag)
5867{
5868 u8 original_tag[8];
5869
5870 memcpy(original_tag, tag, 8);
5871 tag[0] = original_tag[3];
5872 tag[1] = original_tag[2];
5873 tag[2] = original_tag[1];
5874 tag[3] = original_tag[0];
5875 tag[4] = original_tag[7];
5876 tag[5] = original_tag[6];
5877 tag[6] = original_tag[5];
5878 tag[7] = original_tag[4];
5879}
5880
Scott Teel17eb87d2014-02-18 13:55:28 -06005881static void hpsa_get_tag(struct ctlr_info *h,
Don Brace2b08b3e2015-01-23 16:41:09 -06005882 struct CommandList *c, __le32 *taglower, __le32 *tagupper)
Scott Teel17eb87d2014-02-18 13:55:28 -06005883{
Don Brace2b08b3e2015-01-23 16:41:09 -06005884 u64 tag;
Scott Teel17eb87d2014-02-18 13:55:28 -06005885 if (c->cmd_type == CMD_IOACCEL1) {
5886 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
5887 &h->ioaccel_cmd_pool[c->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06005888 tag = le64_to_cpu(cm1->tag);
5889 *tagupper = cpu_to_le32(tag >> 32);
5890 *taglower = cpu_to_le32(tag);
Scott Teel54b6e9e2014-02-18 13:56:45 -06005891 return;
Scott Teel17eb87d2014-02-18 13:55:28 -06005892 }
Scott Teel54b6e9e2014-02-18 13:56:45 -06005893 if (c->cmd_type == CMD_IOACCEL2) {
5894 struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
5895 &h->ioaccel2_cmd_pool[c->cmdindex];
Scott Teeldd0e19f2014-02-18 13:57:31 -06005896 /* upper tag not used in ioaccel2 mode */
5897 memset(tagupper, 0, sizeof(*tagupper));
5898 *taglower = cm2->Tag;
Scott Teel54b6e9e2014-02-18 13:56:45 -06005899 return;
5900 }
Don Brace2b08b3e2015-01-23 16:41:09 -06005901 tag = le64_to_cpu(c->Header.tag);
5902 *tagupper = cpu_to_le32(tag >> 32);
5903 *taglower = cpu_to_le32(tag);
Scott Teel17eb87d2014-02-18 13:55:28 -06005904}
5905
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005906static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005907 struct CommandList *abort, int reply_queue)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005908{
5909 int rc = IO_OK;
5910 struct CommandList *c;
5911 struct ErrorInfo *ei;
Don Brace2b08b3e2015-01-23 16:41:09 -06005912 __le32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005913
Stephen Cameron45fcb862015-01-23 16:43:04 -06005914 c = cmd_alloc(h);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005915
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005916 /* fill_cmd can't fail here, no buffer to map */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005917 (void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005918 0, 0, scsi3addr, TYPE_MSG);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005919 if (h->needs_abort_tags_swizzled)
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005920 swizzle_abort_tag(&c->Request.CDB[4]);
Don Bracec448ecf2016-04-27 17:13:51 -05005921 (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Scott Teel17eb87d2014-02-18 13:55:28 -06005922 hpsa_get_tag(h, abort, &taglower, &tagupper);
Webb Scales25163bd2015-04-23 09:32:00 -05005923 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06005924 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005925 /* no unmap needed here because no data xfer. */
5926
5927 ei = c->err_info;
5928 switch (ei->CommandStatus) {
5929 case CMD_SUCCESS:
5930 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05005931 case CMD_TMF_STATUS:
5932 rc = hpsa_evaluate_tmf_status(h, c);
5933 break;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005934 case CMD_UNABORTABLE: /* Very common, don't make noise. */
5935 rc = -1;
5936 break;
5937 default:
5938 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06005939 __func__, tagupper, taglower);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06005940 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005941 rc = -1;
5942 break;
5943 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06005944 cmd_free(h, c);
Scott Teeldd0e19f2014-02-18 13:57:31 -06005945 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
5946 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005947 return rc;
5948}
5949
Stephen Cameron8be986c2015-04-23 09:34:06 -05005950static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
5951 struct CommandList *command_to_abort, int reply_queue)
5952{
5953 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5954 struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
5955 struct io_accel2_cmd *c2a =
5956 &h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
Webb Scalesa58e7e52015-04-23 09:34:16 -05005957 struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
Stephen Cameron8be986c2015-04-23 09:34:06 -05005958 struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
5959
Don Brace45e596c2016-09-09 16:30:42 -05005960 if (!dev)
5961 return;
5962
Stephen Cameron8be986c2015-04-23 09:34:06 -05005963 /*
5964 * We're overlaying struct hpsa_tmf_struct on top of something which
5965 * was allocated as a struct io_accel2_cmd, so we better be sure it
5966 * actually fits, and doesn't overrun the error info space.
5967 */
5968 BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
5969 sizeof(struct io_accel2_cmd));
5970 BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
5971 offsetof(struct hpsa_tmf_struct, error_len) +
5972 sizeof(ac->error_len));
5973
5974 c->cmd_type = IOACCEL2_TMF;
Webb Scalesa58e7e52015-04-23 09:34:16 -05005975 c->scsi_cmd = SCSI_CMD_BUSY;
5976
Stephen Cameron8be986c2015-04-23 09:34:06 -05005977 /* Adjust the DMA address to point to the accelerated command buffer */
5978 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
5979 (c->cmdindex * sizeof(struct io_accel2_cmd));
5980 BUG_ON(c->busaddr & 0x0000007F);
5981
5982 memset(ac, 0, sizeof(*c2)); /* yes this is correct */
5983 ac->iu_type = IOACCEL2_IU_TMF_TYPE;
5984 ac->reply_queue = reply_queue;
5985 ac->tmf = IOACCEL2_TMF_ABORT;
5986 ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
5987 memset(ac->lun_id, 0, sizeof(ac->lun_id));
5988 ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
5989 ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
5990 ac->error_ptr = cpu_to_le64(c->busaddr +
5991 offsetof(struct io_accel2_cmd, error_data));
5992 ac->error_len = cpu_to_le32(sizeof(c2->error_data));
5993}
5994
Scott Teel54b6e9e2014-02-18 13:56:45 -06005995/* ioaccel2 path firmware cannot handle abort task requests.
5996 * Change abort requests to physical target reset, and send to the
5997 * address of the physical disk used for the ioaccel 2 command.
5998 * Return 0 on success (IO_OK)
5999 * -1 on failure
6000 */
6001
6002static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05006003 unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
Scott Teel54b6e9e2014-02-18 13:56:45 -06006004{
6005 int rc = IO_OK;
6006 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
6007 struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
6008 unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
6009 unsigned char *psa = &phys_scsi3addr[0];
6010
6011 /* Get a pointer to the hpsa logical device. */
Stephen Cameron7fa30302015-01-23 16:44:30 -06006012 scmd = abort->scsi_cmd;
Scott Teel54b6e9e2014-02-18 13:56:45 -06006013 dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
6014 if (dev == NULL) {
6015 dev_warn(&h->pdev->dev,
6016 "Cannot abort: no device pointer for command.\n");
6017 return -1; /* not abortable */
6018 }
6019
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06006020 if (h->raid_offload_debug > 0)
6021 dev_info(&h->pdev->dev,
Webb Scales0d96ef52015-04-23 09:31:55 -05006022 "scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06006023 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
Webb Scales0d96ef52015-04-23 09:31:55 -05006024 "Reset as abort",
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06006025 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
6026 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
6027
Scott Teel54b6e9e2014-02-18 13:56:45 -06006028 if (!dev->offload_enabled) {
6029 dev_warn(&h->pdev->dev,
6030 "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
6031 return -1; /* not abortable */
6032 }
6033
6034 /* Incoming scsi3addr is logical addr. We need physical disk addr. */
6035 if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
6036 dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
6037 return -1; /* not abortable */
6038 }
6039
6040 /* send the reset */
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06006041 if (h->raid_offload_debug > 0)
6042 dev_info(&h->pdev->dev,
6043 "Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
6044 psa[0], psa[1], psa[2], psa[3],
6045 psa[4], psa[5], psa[6], psa[7]);
Don Braceb32ece02016-09-20 15:42:30 -05006046 rc = hpsa_do_reset(h, dev, psa, HPSA_PHYS_TARGET_RESET, reply_queue);
Scott Teel54b6e9e2014-02-18 13:56:45 -06006047 if (rc != 0) {
6048 dev_warn(&h->pdev->dev,
6049 "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
6050 psa[0], psa[1], psa[2], psa[3],
6051 psa[4], psa[5], psa[6], psa[7]);
6052 return rc; /* failed to reset */
6053 }
6054
6055 /* wait for device to recover */
Webb Scalesb69324f2015-04-23 09:34:22 -05006056 if (wait_for_device_to_become_ready(h, psa, reply_queue) != 0) {
Scott Teel54b6e9e2014-02-18 13:56:45 -06006057 dev_warn(&h->pdev->dev,
6058 "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
6059 psa[0], psa[1], psa[2], psa[3],
6060 psa[4], psa[5], psa[6], psa[7]);
6061 return -1; /* failed to recover */
6062 }
6063
6064 /* device recovered */
6065 dev_info(&h->pdev->dev,
6066 "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
6067 psa[0], psa[1], psa[2], psa[3],
6068 psa[4], psa[5], psa[6], psa[7]);
6069
6070 return rc; /* success */
6071}
6072
Stephen Cameron8be986c2015-04-23 09:34:06 -05006073static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
6074 struct CommandList *abort, int reply_queue)
6075{
6076 int rc = IO_OK;
6077 struct CommandList *c;
6078 __le32 taglower, tagupper;
6079 struct hpsa_scsi_dev_t *dev;
6080 struct io_accel2_cmd *c2;
6081
6082 dev = abort->scsi_cmd->device->hostdata;
Don Brace45e596c2016-09-09 16:30:42 -05006083 if (!dev)
6084 return -1;
6085
Stephen Cameron8be986c2015-04-23 09:34:06 -05006086 if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
6087 return -1;
6088
6089 c = cmd_alloc(h);
6090 setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
6091 c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
Don Bracec448ecf2016-04-27 17:13:51 -05006092 (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Stephen Cameron8be986c2015-04-23 09:34:06 -05006093 hpsa_get_tag(h, abort, &taglower, &tagupper);
6094 dev_dbg(&h->pdev->dev,
6095 "%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
6096 __func__, tagupper, taglower);
6097 /* no unmap needed here because no data xfer. */
6098
6099 dev_dbg(&h->pdev->dev,
6100 "%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
6101 __func__, tagupper, taglower, c2->error_data.serv_response);
6102 switch (c2->error_data.serv_response) {
6103 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
6104 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
6105 rc = 0;
6106 break;
6107 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
6108 case IOACCEL2_SERV_RESPONSE_FAILURE:
6109 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
6110 rc = -1;
6111 break;
6112 default:
6113 dev_warn(&h->pdev->dev,
6114 "%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
6115 __func__, tagupper, taglower,
6116 c2->error_data.serv_response);
6117 rc = -1;
6118 }
6119 cmd_free(h, c);
6120 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
6121 tagupper, taglower);
6122 return rc;
6123}
6124
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05006125static int hpsa_send_abort_both_ways(struct ctlr_info *h,
Don Brace39f3deb2016-02-23 15:16:28 -06006126 struct hpsa_scsi_dev_t *dev, struct CommandList *abort, int reply_queue)
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05006127{
Stephen Cameron8be986c2015-04-23 09:34:06 -05006128 /*
6129 * ioccelerator mode 2 commands should be aborted via the
Scott Teel54b6e9e2014-02-18 13:56:45 -06006130 * accelerated path, since RAID path is unaware of these commands,
Stephen Cameron8be986c2015-04-23 09:34:06 -05006131 * but not all underlying firmware can handle abort TMF.
6132 * Change abort to physical device reset when abort TMF is unsupported.
Scott Teel54b6e9e2014-02-18 13:56:45 -06006133 */
Stephen Cameron8be986c2015-04-23 09:34:06 -05006134 if (abort->cmd_type == CMD_IOACCEL2) {
Don Brace39f3deb2016-02-23 15:16:28 -06006135 if ((HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags) ||
6136 dev->physical_device)
Stephen Cameron8be986c2015-04-23 09:34:06 -05006137 return hpsa_send_abort_ioaccel2(h, abort,
6138 reply_queue);
6139 else
Don Brace39f3deb2016-02-23 15:16:28 -06006140 return hpsa_send_reset_as_abort_ioaccel2(h,
6141 dev->scsi3addr,
Webb Scales25163bd2015-04-23 09:32:00 -05006142 abort, reply_queue);
Stephen Cameron8be986c2015-04-23 09:34:06 -05006143 }
Don Brace39f3deb2016-02-23 15:16:28 -06006144 return hpsa_send_abort(h, dev->scsi3addr, abort, reply_queue);
Webb Scales25163bd2015-04-23 09:32:00 -05006145}
6146
6147/* Find out which reply queue a command was meant to return on */
6148static int hpsa_extract_reply_queue(struct ctlr_info *h,
6149 struct CommandList *c)
6150{
6151 if (c->cmd_type == CMD_IOACCEL2)
6152 return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
6153 return c->Header.ReplyQueue;
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05006154}
6155
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006156/*
6157 * Limit concurrency of abort commands to prevent
6158 * over-subscription of commands
6159 */
6160static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
6161{
6162#define ABORT_CMD_WAIT_MSECS 5000
6163 return !wait_event_timeout(h->abort_cmd_wait_queue,
6164 atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
6165 msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
6166}
6167
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006168/* Send an abort for the specified command.
6169 * If the device and controller support it,
6170 * send a task abort request.
6171 */
6172static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
6173{
6174
Webb Scalesa58e7e52015-04-23 09:34:16 -05006175 int rc;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006176 struct ctlr_info *h;
6177 struct hpsa_scsi_dev_t *dev;
6178 struct CommandList *abort; /* pointer to command to be aborted */
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006179 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
6180 char msg[256]; /* For debug messaging. */
6181 int ml = 0;
Don Brace2b08b3e2015-01-23 16:41:09 -06006182 __le32 tagupper, taglower;
Webb Scales25163bd2015-04-23 09:32:00 -05006183 int refcount, reply_queue;
6184
6185 if (sc == NULL)
6186 return FAILED;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006187
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006188 if (sc->device == NULL)
6189 return FAILED;
6190
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006191 /* Find the controller of the command to be aborted */
6192 h = sdev_to_hba(sc->device);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006193 if (h == NULL)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006194 return FAILED;
6195
Webb Scales25163bd2015-04-23 09:32:00 -05006196 /* Find the device of the command to be aborted */
6197 dev = sc->device->hostdata;
6198 if (!dev) {
6199 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
6200 msg);
Don Bracee3458932015-01-23 16:44:24 -06006201 return FAILED;
Webb Scales25163bd2015-04-23 09:32:00 -05006202 }
6203
6204 /* If controller locked up, we can guarantee command won't complete */
6205 if (lockup_detected(h)) {
6206 hpsa_show_dev_msg(KERN_WARNING, h, dev,
6207 "ABORT FAILED, lockup detected");
6208 return FAILED;
6209 }
6210
6211 /* This is a good time to check if controller lockup has occurred */
6212 if (detect_controller_lockup(h)) {
6213 hpsa_show_dev_msg(KERN_WARNING, h, dev,
6214 "ABORT FAILED, new lockup detected");
6215 return FAILED;
6216 }
Don Bracee3458932015-01-23 16:44:24 -06006217
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006218 /* Check that controller supports some kind of task abort */
6219 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
6220 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
6221 return FAILED;
6222
6223 memset(msg, 0, sizeof(msg));
Robert Elliott4b761552015-04-23 09:33:54 -05006224 ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006225 h->scsi_host->host_no, sc->device->channel,
Webb Scales0d96ef52015-04-23 09:31:55 -05006226 sc->device->id, sc->device->lun,
Robert Elliott4b761552015-04-23 09:33:54 -05006227 "Aborting command", sc);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006228
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006229 /* Get SCSI command to be aborted */
6230 abort = (struct CommandList *) sc->host_scribble;
6231 if (abort == NULL) {
Webb Scales281a7fd2015-01-23 16:43:35 -06006232 /* This can happen if the command already completed. */
6233 return SUCCESS;
6234 }
6235 refcount = atomic_inc_return(&abort->refcount);
6236 if (refcount == 1) { /* Command is done already. */
6237 cmd_free(h, abort);
6238 return SUCCESS;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006239 }
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006240
6241 /* Don't bother trying the abort if we know it won't work. */
6242 if (abort->cmd_type != CMD_IOACCEL2 &&
6243 abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
6244 cmd_free(h, abort);
6245 return FAILED;
6246 }
6247
Webb Scalesa58e7e52015-04-23 09:34:16 -05006248 /*
6249 * Check that we're aborting the right command.
6250 * It's possible the CommandList already completed and got re-used.
6251 */
6252 if (abort->scsi_cmd != sc) {
6253 cmd_free(h, abort);
6254 return SUCCESS;
6255 }
6256
6257 abort->abort_pending = true;
Scott Teel17eb87d2014-02-18 13:55:28 -06006258 hpsa_get_tag(h, abort, &taglower, &tagupper);
Webb Scales25163bd2015-04-23 09:32:00 -05006259 reply_queue = hpsa_extract_reply_queue(h, abort);
Scott Teel17eb87d2014-02-18 13:55:28 -06006260 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen Cameron7fa30302015-01-23 16:44:30 -06006261 as = abort->scsi_cmd;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006262 if (as != NULL)
Robert Elliott4b761552015-04-23 09:33:54 -05006263 ml += sprintf(msg+ml,
6264 "CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
6265 as->cmd_len, as->cmnd[0], as->cmnd[1],
6266 as->serial_number);
6267 dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
Webb Scales0d96ef52015-04-23 09:31:55 -05006268 hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
Robert Elliott4b761552015-04-23 09:33:54 -05006269
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006270 /*
6271 * Command is in flight, or possibly already completed
6272 * by the firmware (but not to the scsi mid layer) but we can't
6273 * distinguish which. Send the abort down.
6274 */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006275 if (wait_for_available_abort_cmd(h)) {
6276 dev_warn(&h->pdev->dev,
Robert Elliott4b761552015-04-23 09:33:54 -05006277 "%s FAILED, timeout waiting for an abort command to become available.\n",
6278 msg);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006279 cmd_free(h, abort);
6280 return FAILED;
6281 }
Don Brace39f3deb2016-02-23 15:16:28 -06006282 rc = hpsa_send_abort_both_ways(h, dev, abort, reply_queue);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006283 atomic_inc(&h->abort_cmds_available);
6284 wake_up_all(&h->abort_cmd_wait_queue);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006285 if (rc != 0) {
Robert Elliott4b761552015-04-23 09:33:54 -05006286 dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
Webb Scales0d96ef52015-04-23 09:31:55 -05006287 hpsa_show_dev_msg(KERN_WARNING, h, dev,
Robert Elliott4b761552015-04-23 09:33:54 -05006288 "FAILED to abort command");
Webb Scales281a7fd2015-01-23 16:43:35 -06006289 cmd_free(h, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006290 return FAILED;
6291 }
Robert Elliott4b761552015-04-23 09:33:54 -05006292 dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
Webb Scalesd604f532015-04-23 09:35:22 -05006293 wait_event(h->event_sync_wait_queue,
Webb Scalesa58e7e52015-04-23 09:34:16 -05006294 abort->scsi_cmd != sc || lockup_detected(h));
Webb Scales281a7fd2015-01-23 16:43:35 -06006295 cmd_free(h, abort);
Webb Scalesa58e7e52015-04-23 09:34:16 -05006296 return !lockup_detected(h) ? SUCCESS : FAILED;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006297}
6298
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006299/*
Webb Scales73153fe2015-04-23 09:35:04 -05006300 * For operations with an associated SCSI command, a command block is allocated
6301 * at init, and managed by cmd_tagged_alloc() and cmd_tagged_free() using the
6302 * block request tag as an index into a table of entries. cmd_tagged_free() is
6303 * the complement, although cmd_free() may be called instead.
6304 */
6305static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
6306 struct scsi_cmnd *scmd)
6307{
6308 int idx = hpsa_get_cmd_index(scmd);
6309 struct CommandList *c = h->cmd_pool + idx;
6310
6311 if (idx < HPSA_NRESERVED_CMDS || idx >= h->nr_cmds) {
6312 dev_err(&h->pdev->dev, "Bad block tag: %d not in [%d..%d]\n",
6313 idx, HPSA_NRESERVED_CMDS, h->nr_cmds - 1);
6314 /* The index value comes from the block layer, so if it's out of
6315 * bounds, it's probably not our bug.
6316 */
6317 BUG();
6318 }
6319
6320 atomic_inc(&c->refcount);
6321 if (unlikely(!hpsa_is_cmd_idle(c))) {
6322 /*
6323 * We expect that the SCSI layer will hand us a unique tag
6324 * value. Thus, there should never be a collision here between
6325 * two requests...because if the selected command isn't idle
6326 * then someone is going to be very disappointed.
6327 */
6328 dev_err(&h->pdev->dev,
6329 "tag collision (tag=%d) in cmd_tagged_alloc().\n",
6330 idx);
6331 if (c->scsi_cmd != NULL)
6332 scsi_print_command(c->scsi_cmd);
6333 scsi_print_command(scmd);
6334 }
6335
6336 hpsa_cmd_partial_init(h, idx, c);
6337 return c;
6338}
6339
6340static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c)
6341{
6342 /*
6343 * Release our reference to the block. We don't need to do anything
6344 * else to free it, because it is accessed by index. (There's no point
6345 * in checking the result of the decrement, since we cannot guarantee
6346 * that there isn't a concurrent abort which is also accessing it.)
6347 */
6348 (void)atomic_dec(&c->refcount);
6349}
6350
6351/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006352 * For operations that cannot sleep, a command block is allocated at init,
6353 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
6354 * which ones are free or in use. Lock must be held when calling this.
6355 * cmd_free() is the complement.
Robert Elliottbf43caf2015-04-23 09:33:38 -05006356 * This function never gives up and returns NULL. If it hangs,
6357 * another thread must call cmd_free() to free some tags.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006358 */
Webb Scales281a7fd2015-01-23 16:43:35 -06006359
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006360static struct CommandList *cmd_alloc(struct ctlr_info *h)
6361{
6362 struct CommandList *c;
Stephen Cameron360c73b2015-04-23 09:32:32 -05006363 int refcount, i;
Webb Scales73153fe2015-04-23 09:35:04 -05006364 int offset = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006365
Robert Elliott33811022015-01-23 16:43:41 -06006366 /*
6367 * There is some *extremely* small but non-zero chance that that
Stephen M. Cameron4c413122014-11-14 17:27:29 -06006368 * multiple threads could get in here, and one thread could
6369 * be scanning through the list of bits looking for a free
6370 * one, but the free ones are always behind him, and other
6371 * threads sneak in behind him and eat them before he can
6372 * get to them, so that while there is always a free one, a
6373 * very unlucky thread might be starved anyway, never able to
6374 * beat the other threads. In reality, this happens so
6375 * infrequently as to be indistinguishable from never.
Webb Scales73153fe2015-04-23 09:35:04 -05006376 *
6377 * Note that we start allocating commands before the SCSI host structure
6378 * is initialized. Since the search starts at bit zero, this
6379 * all works, since we have at least one command structure available;
6380 * however, it means that the structures with the low indexes have to be
6381 * reserved for driver-initiated requests, while requests from the block
6382 * layer will use the higher indexes.
Stephen M. Cameron4c413122014-11-14 17:27:29 -06006383 */
6384
Webb Scales281a7fd2015-01-23 16:43:35 -06006385 for (;;) {
Webb Scales73153fe2015-04-23 09:35:04 -05006386 i = find_next_zero_bit(h->cmd_pool_bits,
6387 HPSA_NRESERVED_CMDS,
6388 offset);
6389 if (unlikely(i >= HPSA_NRESERVED_CMDS)) {
Webb Scales281a7fd2015-01-23 16:43:35 -06006390 offset = 0;
6391 continue;
6392 }
6393 c = h->cmd_pool + i;
6394 refcount = atomic_inc_return(&c->refcount);
6395 if (unlikely(refcount > 1)) {
6396 cmd_free(h, c); /* already in use */
Webb Scales73153fe2015-04-23 09:35:04 -05006397 offset = (i + 1) % HPSA_NRESERVED_CMDS;
Webb Scales281a7fd2015-01-23 16:43:35 -06006398 continue;
6399 }
6400 set_bit(i & (BITS_PER_LONG - 1),
6401 h->cmd_pool_bits + (i / BITS_PER_LONG));
6402 break; /* it's ours now. */
6403 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05006404 hpsa_cmd_partial_init(h, i, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006405 return c;
6406}
6407
Webb Scales73153fe2015-04-23 09:35:04 -05006408/*
6409 * This is the complementary operation to cmd_alloc(). Note, however, in some
6410 * corner cases it may also be used to free blocks allocated by
6411 * cmd_tagged_alloc() in which case the ref-count decrement does the trick and
6412 * the clear-bit is harmless.
6413 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006414static void cmd_free(struct ctlr_info *h, struct CommandList *c)
6415{
Webb Scales281a7fd2015-01-23 16:43:35 -06006416 if (atomic_dec_and_test(&c->refcount)) {
6417 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006418
Webb Scales281a7fd2015-01-23 16:43:35 -06006419 i = c - h->cmd_pool;
6420 clear_bit(i & (BITS_PER_LONG - 1),
6421 h->cmd_pool_bits + (i / BITS_PER_LONG));
6422 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006423}
6424
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006425#ifdef CONFIG_COMPAT
6426
Don Brace42a91642014-11-14 17:26:27 -06006427static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
6428 void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006429{
6430 IOCTL32_Command_struct __user *arg32 =
6431 (IOCTL32_Command_struct __user *) arg;
6432 IOCTL_Command_struct arg64;
6433 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
6434 int err;
6435 u32 cp;
6436
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06006437 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006438 err = 0;
6439 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
6440 sizeof(arg64.LUN_info));
6441 err |= copy_from_user(&arg64.Request, &arg32->Request,
6442 sizeof(arg64.Request));
6443 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
6444 sizeof(arg64.error_info));
6445 err |= get_user(arg64.buf_size, &arg32->buf_size);
6446 err |= get_user(cp, &arg32->buf);
6447 arg64.buf = compat_ptr(cp);
6448 err |= copy_to_user(p, &arg64, sizeof(arg64));
6449
6450 if (err)
6451 return -EFAULT;
6452
Don Brace42a91642014-11-14 17:26:27 -06006453 err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006454 if (err)
6455 return err;
6456 err |= copy_in_user(&arg32->error_info, &p->error_info,
6457 sizeof(arg32->error_info));
6458 if (err)
6459 return -EFAULT;
6460 return err;
6461}
6462
6463static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
Don Brace42a91642014-11-14 17:26:27 -06006464 int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006465{
6466 BIG_IOCTL32_Command_struct __user *arg32 =
6467 (BIG_IOCTL32_Command_struct __user *) arg;
6468 BIG_IOCTL_Command_struct arg64;
6469 BIG_IOCTL_Command_struct __user *p =
6470 compat_alloc_user_space(sizeof(arg64));
6471 int err;
6472 u32 cp;
6473
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06006474 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006475 err = 0;
6476 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
6477 sizeof(arg64.LUN_info));
6478 err |= copy_from_user(&arg64.Request, &arg32->Request,
6479 sizeof(arg64.Request));
6480 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
6481 sizeof(arg64.error_info));
6482 err |= get_user(arg64.buf_size, &arg32->buf_size);
6483 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
6484 err |= get_user(cp, &arg32->buf);
6485 arg64.buf = compat_ptr(cp);
6486 err |= copy_to_user(p, &arg64, sizeof(arg64));
6487
6488 if (err)
6489 return -EFAULT;
6490
Don Brace42a91642014-11-14 17:26:27 -06006491 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006492 if (err)
6493 return err;
6494 err |= copy_in_user(&arg32->error_info, &p->error_info,
6495 sizeof(arg32->error_info));
6496 if (err)
6497 return -EFAULT;
6498 return err;
6499}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06006500
Don Brace42a91642014-11-14 17:26:27 -06006501static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06006502{
6503 switch (cmd) {
6504 case CCISS_GETPCIINFO:
6505 case CCISS_GETINTINFO:
6506 case CCISS_SETINTINFO:
6507 case CCISS_GETNODENAME:
6508 case CCISS_SETNODENAME:
6509 case CCISS_GETHEARTBEAT:
6510 case CCISS_GETBUSTYPES:
6511 case CCISS_GETFIRMVER:
6512 case CCISS_GETDRIVVER:
6513 case CCISS_REVALIDVOLS:
6514 case CCISS_DEREGDISK:
6515 case CCISS_REGNEWDISK:
6516 case CCISS_REGNEWD:
6517 case CCISS_RESCANDISK:
6518 case CCISS_GETLUNINFO:
6519 return hpsa_ioctl(dev, cmd, arg);
6520
6521 case CCISS_PASSTHRU32:
6522 return hpsa_ioctl32_passthru(dev, cmd, arg);
6523 case CCISS_BIG_PASSTHRU32:
6524 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
6525
6526 default:
6527 return -ENOIOCTLCMD;
6528 }
6529}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006530#endif
6531
6532static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
6533{
6534 struct hpsa_pci_info pciinfo;
6535
6536 if (!argp)
6537 return -EINVAL;
6538 pciinfo.domain = pci_domain_nr(h->pdev->bus);
6539 pciinfo.bus = h->pdev->bus->number;
6540 pciinfo.dev_fn = h->pdev->devfn;
6541 pciinfo.board_id = h->board_id;
6542 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
6543 return -EFAULT;
6544 return 0;
6545}
6546
6547static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
6548{
6549 DriverVer_type DriverVer;
6550 unsigned char vmaj, vmin, vsubmin;
6551 int rc;
6552
6553 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
6554 &vmaj, &vmin, &vsubmin);
6555 if (rc != 3) {
6556 dev_info(&h->pdev->dev, "driver version string '%s' "
6557 "unrecognized.", HPSA_DRIVER_VERSION);
6558 vmaj = 0;
6559 vmin = 0;
6560 vsubmin = 0;
6561 }
6562 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
6563 if (!argp)
6564 return -EINVAL;
6565 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
6566 return -EFAULT;
6567 return 0;
6568}
6569
6570static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6571{
6572 IOCTL_Command_struct iocommand;
6573 struct CommandList *c;
6574 char *buff = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006575 u64 temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006576 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006577
6578 if (!argp)
6579 return -EINVAL;
6580 if (!capable(CAP_SYS_RAWIO))
6581 return -EPERM;
6582 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
6583 return -EFAULT;
6584 if ((iocommand.buf_size < 1) &&
6585 (iocommand.Request.Type.Direction != XFER_NONE)) {
6586 return -EINVAL;
6587 }
6588 if (iocommand.buf_size > 0) {
6589 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
6590 if (buff == NULL)
Robert Elliott2dd02d72015-04-23 09:33:43 -05006591 return -ENOMEM;
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006592 if (iocommand.Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006593 /* Copy the data into the buffer we created */
6594 if (copy_from_user(buff, iocommand.buf,
6595 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006596 rc = -EFAULT;
6597 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006598 }
6599 } else {
6600 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006601 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006602 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006603 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006604
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006605 /* Fill in the command type */
6606 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006607 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006608 /* Fill in Command Header */
6609 c->Header.ReplyQueue = 0; /* unused in simple mode */
6610 if (iocommand.buf_size > 0) { /* buffer to fill */
6611 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006612 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006613 } else { /* no buffers to fill */
6614 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006615 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006616 }
6617 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006618
6619 /* Fill in Request block */
6620 memcpy(&c->Request, &iocommand.Request,
6621 sizeof(c->Request));
6622
6623 /* Fill in the scatter gather information */
6624 if (iocommand.buf_size > 0) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006625 temp64 = pci_map_single(h->pdev, buff,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006626 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006627 if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
6628 c->SG[0].Addr = cpu_to_le64(0);
6629 c->SG[0].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006630 rc = -ENOMEM;
6631 goto out;
6632 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006633 c->SG[0].Addr = cpu_to_le64(temp64);
6634 c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
6635 c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006636 }
Don Bracec448ecf2016-04-27 17:13:51 -05006637 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
Don Brace3fb134c2016-07-01 13:37:38 -05006638 NO_TIMEOUT);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05006639 if (iocommand.buf_size > 0)
6640 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006641 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006642 if (rc) {
6643 rc = -EIO;
6644 goto out;
6645 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006646
6647 /* Copy the error information out */
6648 memcpy(&iocommand.error_info, c->err_info,
6649 sizeof(iocommand.error_info));
6650 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006651 rc = -EFAULT;
6652 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006653 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006654 if ((iocommand.Request.Type.Direction & XFER_READ) &&
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006655 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006656 /* Copy the data out of the buffer we created */
6657 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006658 rc = -EFAULT;
6659 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006660 }
6661 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006662out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006663 cmd_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006664out_kfree:
6665 kfree(buff);
6666 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006667}
6668
6669static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6670{
6671 BIG_IOCTL_Command_struct *ioc;
6672 struct CommandList *c;
6673 unsigned char **buff = NULL;
6674 int *buff_size = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006675 u64 temp64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006676 BYTE sg_used = 0;
6677 int status = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06006678 u32 left;
6679 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006680 BYTE __user *data_ptr;
6681
6682 if (!argp)
6683 return -EINVAL;
6684 if (!capable(CAP_SYS_RAWIO))
6685 return -EPERM;
Javier Martinez Canillas19be606b2016-10-13 13:10:08 -03006686 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006687 if (!ioc) {
6688 status = -ENOMEM;
6689 goto cleanup1;
6690 }
6691 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
6692 status = -EFAULT;
6693 goto cleanup1;
6694 }
6695 if ((ioc->buf_size < 1) &&
6696 (ioc->Request.Type.Direction != XFER_NONE)) {
6697 status = -EINVAL;
6698 goto cleanup1;
6699 }
6700 /* Check kmalloc limits using all SGs */
6701 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
6702 status = -EINVAL;
6703 goto cleanup1;
6704 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006705 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006706 status = -EINVAL;
6707 goto cleanup1;
6708 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006709 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006710 if (!buff) {
6711 status = -ENOMEM;
6712 goto cleanup1;
6713 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006714 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006715 if (!buff_size) {
6716 status = -ENOMEM;
6717 goto cleanup1;
6718 }
6719 left = ioc->buf_size;
6720 data_ptr = ioc->buf;
6721 while (left) {
6722 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
6723 buff_size[sg_used] = sz;
6724 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
6725 if (buff[sg_used] == NULL) {
6726 status = -ENOMEM;
6727 goto cleanup1;
6728 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006729 if (ioc->Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006730 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
Stephen M. Cameron0758f4f2014-07-03 10:18:03 -05006731 status = -EFAULT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006732 goto cleanup1;
6733 }
6734 } else
6735 memset(buff[sg_used], 0, sz);
6736 left -= sz;
6737 data_ptr += sz;
6738 sg_used++;
6739 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006740 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006741
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006742 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006743 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006744 c->Header.ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006745 c->Header.SGList = (u8) sg_used;
6746 c->Header.SGTotal = cpu_to_le16(sg_used);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006747 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006748 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
6749 if (ioc->buf_size > 0) {
6750 int i;
6751 for (i = 0; i < sg_used; i++) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006752 temp64 = pci_map_single(h->pdev, buff[i],
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006753 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006754 if (dma_mapping_error(&h->pdev->dev,
6755 (dma_addr_t) temp64)) {
6756 c->SG[i].Addr = cpu_to_le64(0);
6757 c->SG[i].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006758 hpsa_pci_unmap(h->pdev, c, i,
6759 PCI_DMA_BIDIRECTIONAL);
6760 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006761 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006762 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006763 c->SG[i].Addr = cpu_to_le64(temp64);
6764 c->SG[i].Len = cpu_to_le32(buff_size[i]);
6765 c->SG[i].Ext = cpu_to_le32(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006766 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006767 c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006768 }
Don Bracec448ecf2016-04-27 17:13:51 -05006769 status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
Don Brace3fb134c2016-07-01 13:37:38 -05006770 NO_TIMEOUT);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006771 if (sg_used)
6772 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006773 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006774 if (status) {
6775 status = -EIO;
6776 goto cleanup0;
6777 }
6778
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006779 /* Copy the error information out */
6780 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
6781 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006782 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006783 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006784 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006785 if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006786 int i;
6787
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006788 /* Copy the data out of the buffer we created */
6789 BYTE __user *ptr = ioc->buf;
6790 for (i = 0; i < sg_used; i++) {
6791 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006792 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006793 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006794 }
6795 ptr += buff_size[i];
6796 }
6797 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006798 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006799cleanup0:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006800 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006801cleanup1:
6802 if (buff) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006803 int i;
6804
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006805 for (i = 0; i < sg_used; i++)
6806 kfree(buff[i]);
6807 kfree(buff);
6808 }
6809 kfree(buff_size);
6810 kfree(ioc);
6811 return status;
6812}
6813
6814static void check_ioctl_unit_attention(struct ctlr_info *h,
6815 struct CommandList *c)
6816{
6817 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
6818 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
6819 (void) check_for_unit_attention(h, c);
6820}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006821
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006822/*
6823 * ioctl
6824 */
Don Brace42a91642014-11-14 17:26:27 -06006825static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006826{
6827 struct ctlr_info *h;
6828 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006829 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006830
6831 h = sdev_to_hba(dev);
6832
6833 switch (cmd) {
6834 case CCISS_DEREGDISK:
6835 case CCISS_REGNEWDISK:
6836 case CCISS_REGNEWD:
Stephen M. Camerona08a8472010-02-04 08:43:16 -06006837 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006838 return 0;
6839 case CCISS_GETPCIINFO:
6840 return hpsa_getpciinfo_ioctl(h, argp);
6841 case CCISS_GETDRIVVER:
6842 return hpsa_getdrivver_ioctl(h, argp);
6843 case CCISS_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006844 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006845 return -EAGAIN;
6846 rc = hpsa_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006847 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006848 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006849 case CCISS_BIG_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006850 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006851 return -EAGAIN;
6852 rc = hpsa_big_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006853 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006854 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006855 default:
6856 return -ENOTTY;
6857 }
6858}
6859
Robert Elliottbf43caf2015-04-23 09:33:38 -05006860static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006861 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006862{
6863 struct CommandList *c;
6864
6865 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006866
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006867 /* fill_cmd can't fail here, no data buffer to map */
6868 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006869 RAID_CTLR_LUNID, TYPE_MSG);
6870 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
6871 c->waiting = NULL;
6872 enqueue_cmd_and_start_io(h, c);
6873 /* Don't wait for completion, the reset won't complete. Don't free
6874 * the command either. This is the last command we will send before
6875 * re-initializing everything, so it doesn't matter and won't leak.
6876 */
Robert Elliottbf43caf2015-04-23 09:33:38 -05006877 return;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006878}
6879
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006880static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006881 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006882 int cmd_type)
6883{
6884 int pci_dir = XFER_NONE;
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006885 u64 tag; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006886
6887 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006888 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006889 c->Header.ReplyQueue = 0;
6890 if (buff != NULL && size > 0) {
6891 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006892 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006893 } else {
6894 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006895 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006896 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006897 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
6898
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006899 if (cmd_type == TYPE_CMD) {
6900 switch (cmd) {
6901 case HPSA_INQUIRY:
6902 /* are we trying to read a vital product page */
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006903 if (page_code & VPD_PAGE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006904 c->Request.CDB[1] = 0x01;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006905 c->Request.CDB[2] = (page_code & 0xff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006906 }
6907 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006908 c->Request.type_attr_dir =
6909 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006910 c->Request.Timeout = 0;
6911 c->Request.CDB[0] = HPSA_INQUIRY;
6912 c->Request.CDB[4] = size & 0xFF;
6913 break;
6914 case HPSA_REPORT_LOG:
6915 case HPSA_REPORT_PHYS:
6916 /* Talking to controller so It's a physical command
6917 mode = 00 target = 0. Nothing to write.
6918 */
6919 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006920 c->Request.type_attr_dir =
6921 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006922 c->Request.Timeout = 0;
6923 c->Request.CDB[0] = cmd;
6924 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6925 c->Request.CDB[7] = (size >> 16) & 0xFF;
6926 c->Request.CDB[8] = (size >> 8) & 0xFF;
6927 c->Request.CDB[9] = size & 0xFF;
6928 break;
Scott Teelc2adae42015-11-04 15:52:16 -06006929 case BMIC_SENSE_DIAG_OPTIONS:
6930 c->Request.CDBLen = 16;
6931 c->Request.type_attr_dir =
6932 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6933 c->Request.Timeout = 0;
6934 /* Spec says this should be BMIC_WRITE */
6935 c->Request.CDB[0] = BMIC_READ;
6936 c->Request.CDB[6] = BMIC_SENSE_DIAG_OPTIONS;
6937 break;
6938 case BMIC_SET_DIAG_OPTIONS:
6939 c->Request.CDBLen = 16;
6940 c->Request.type_attr_dir =
6941 TYPE_ATTR_DIR(cmd_type,
6942 ATTR_SIMPLE, XFER_WRITE);
6943 c->Request.Timeout = 0;
6944 c->Request.CDB[0] = BMIC_WRITE;
6945 c->Request.CDB[6] = BMIC_SET_DIAG_OPTIONS;
6946 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006947 case HPSA_CACHE_FLUSH:
6948 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006949 c->Request.type_attr_dir =
6950 TYPE_ATTR_DIR(cmd_type,
6951 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006952 c->Request.Timeout = 0;
6953 c->Request.CDB[0] = BMIC_WRITE;
6954 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05006955 c->Request.CDB[7] = (size >> 8) & 0xFF;
6956 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006957 break;
6958 case TEST_UNIT_READY:
6959 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006960 c->Request.type_attr_dir =
6961 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006962 c->Request.Timeout = 0;
6963 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006964 case HPSA_GET_RAID_MAP:
6965 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006966 c->Request.type_attr_dir =
6967 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006968 c->Request.Timeout = 0;
6969 c->Request.CDB[0] = HPSA_CISS_READ;
6970 c->Request.CDB[1] = cmd;
6971 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6972 c->Request.CDB[7] = (size >> 16) & 0xFF;
6973 c->Request.CDB[8] = (size >> 8) & 0xFF;
6974 c->Request.CDB[9] = size & 0xFF;
6975 break;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006976 case BMIC_SENSE_CONTROLLER_PARAMETERS:
6977 c->Request.CDBLen = 10;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006978 c->Request.type_attr_dir =
6979 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006980 c->Request.Timeout = 0;
6981 c->Request.CDB[0] = BMIC_READ;
6982 c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
6983 c->Request.CDB[7] = (size >> 16) & 0xFF;
6984 c->Request.CDB[8] = (size >> 8) & 0xFF;
6985 break;
Don Brace03383732015-01-23 16:43:30 -06006986 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
6987 c->Request.CDBLen = 10;
6988 c->Request.type_attr_dir =
6989 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6990 c->Request.Timeout = 0;
6991 c->Request.CDB[0] = BMIC_READ;
6992 c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
6993 c->Request.CDB[7] = (size >> 16) & 0xFF;
6994 c->Request.CDB[8] = (size >> 8) & 0XFF;
6995 break;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06006996 case BMIC_SENSE_SUBSYSTEM_INFORMATION:
6997 c->Request.CDBLen = 10;
6998 c->Request.type_attr_dir =
6999 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
7000 c->Request.Timeout = 0;
7001 c->Request.CDB[0] = BMIC_READ;
7002 c->Request.CDB[6] = BMIC_SENSE_SUBSYSTEM_INFORMATION;
7003 c->Request.CDB[7] = (size >> 16) & 0xFF;
7004 c->Request.CDB[8] = (size >> 8) & 0XFF;
7005 break;
Don Bracecca8f132015-12-22 10:36:48 -06007006 case BMIC_SENSE_STORAGE_BOX_PARAMS:
7007 c->Request.CDBLen = 10;
7008 c->Request.type_attr_dir =
7009 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
7010 c->Request.Timeout = 0;
7011 c->Request.CDB[0] = BMIC_READ;
7012 c->Request.CDB[6] = BMIC_SENSE_STORAGE_BOX_PARAMS;
7013 c->Request.CDB[7] = (size >> 16) & 0xFF;
7014 c->Request.CDB[8] = (size >> 8) & 0XFF;
7015 break;
Scott Teel66749d02015-11-04 15:51:57 -06007016 case BMIC_IDENTIFY_CONTROLLER:
7017 c->Request.CDBLen = 10;
7018 c->Request.type_attr_dir =
7019 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
7020 c->Request.Timeout = 0;
7021 c->Request.CDB[0] = BMIC_READ;
7022 c->Request.CDB[1] = 0;
7023 c->Request.CDB[2] = 0;
7024 c->Request.CDB[3] = 0;
7025 c->Request.CDB[4] = 0;
7026 c->Request.CDB[5] = 0;
7027 c->Request.CDB[6] = BMIC_IDENTIFY_CONTROLLER;
7028 c->Request.CDB[7] = (size >> 16) & 0xFF;
7029 c->Request.CDB[8] = (size >> 8) & 0XFF;
7030 c->Request.CDB[9] = 0;
7031 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007032 default:
7033 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
7034 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06007035 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007036 }
7037 } else if (cmd_type == TYPE_MSG) {
7038 switch (cmd) {
7039
Scott Teel0b9b7b62015-11-04 15:51:02 -06007040 case HPSA_PHYS_TARGET_RESET:
7041 c->Request.CDBLen = 16;
7042 c->Request.type_attr_dir =
7043 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
7044 c->Request.Timeout = 0; /* Don't time out */
7045 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
7046 c->Request.CDB[0] = HPSA_RESET;
7047 c->Request.CDB[1] = HPSA_TARGET_RESET_TYPE;
7048 /* Physical target reset needs no control bytes 4-7*/
7049 c->Request.CDB[4] = 0x00;
7050 c->Request.CDB[5] = 0x00;
7051 c->Request.CDB[6] = 0x00;
7052 c->Request.CDB[7] = 0x00;
7053 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007054 case HPSA_DEVICE_RESET_MSG:
7055 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06007056 c->Request.type_attr_dir =
7057 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007058 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007059 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
7060 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05007061 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007062 /* If bytes 4-7 are zero, it means reset the */
7063 /* LunID device */
7064 c->Request.CDB[4] = 0x00;
7065 c->Request.CDB[5] = 0x00;
7066 c->Request.CDB[6] = 0x00;
7067 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05007068 break;
7069 case HPSA_ABORT_MSG:
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05007070 memcpy(&tag, buff, sizeof(tag));
Don Brace2b08b3e2015-01-23 16:41:09 -06007071 dev_dbg(&h->pdev->dev,
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05007072 "Abort Tag:0x%016llx using rqst Tag:0x%016llx",
7073 tag, c->Header.tag);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05007074 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06007075 c->Request.type_attr_dir =
7076 TYPE_ATTR_DIR(cmd_type,
7077 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05007078 c->Request.Timeout = 0; /* Don't time out */
7079 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
7080 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
7081 c->Request.CDB[2] = 0x00; /* reserved */
7082 c->Request.CDB[3] = 0x00; /* reserved */
7083 /* Tag to abort goes in CDB[4]-CDB[11] */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05007084 memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
Stephen M. Cameron75167d22012-05-01 11:42:51 -05007085 c->Request.CDB[12] = 0x00; /* reserved */
7086 c->Request.CDB[13] = 0x00; /* reserved */
7087 c->Request.CDB[14] = 0x00; /* reserved */
7088 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007089 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007090 default:
7091 dev_warn(&h->pdev->dev, "unknown message type %d\n",
7092 cmd);
7093 BUG();
7094 }
7095 } else {
7096 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
7097 BUG();
7098 }
7099
Stephen M. Camerona505b862014-11-14 17:27:04 -06007100 switch (GET_DIR(c->Request.type_attr_dir)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007101 case XFER_READ:
7102 pci_dir = PCI_DMA_FROMDEVICE;
7103 break;
7104 case XFER_WRITE:
7105 pci_dir = PCI_DMA_TODEVICE;
7106 break;
7107 case XFER_NONE:
7108 pci_dir = PCI_DMA_NONE;
7109 break;
7110 default:
7111 pci_dir = PCI_DMA_BIDIRECTIONAL;
7112 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06007113 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
7114 return -1;
7115 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007116}
7117
7118/*
7119 * Map (physical) PCI mem into (virtual) kernel space
7120 */
7121static void __iomem *remap_pci_mem(ulong base, ulong size)
7122{
7123 ulong page_base = ((ulong) base) & PAGE_MASK;
7124 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba342012-07-26 11:34:23 -05007125 void __iomem *page_remapped = ioremap_nocache(page_base,
7126 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007127
7128 return page_remapped ? (page_remapped + page_offs) : NULL;
7129}
7130
Matt Gates254f7962012-05-01 11:43:06 -05007131static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007132{
Matt Gates254f7962012-05-01 11:43:06 -05007133 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007134}
7135
Stephen M. Cameron900c5442010-02-04 08:42:35 -06007136static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007137{
7138 return h->access.intr_pending(h);
7139}
7140
7141static inline long interrupt_not_for_us(struct ctlr_info *h)
7142{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007143 return (h->access.intr_pending(h) == 0) ||
7144 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007145}
7146
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06007147static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
7148 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007149{
7150 if (unlikely(tag_index >= h->nr_cmds)) {
7151 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
7152 return 1;
7153 }
7154 return 0;
7155}
7156
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05007157static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007158{
Stephen M. Camerone85c5972012-05-01 11:43:42 -05007159 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06007160 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
7161 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05007162 complete_scsi_command(c);
Stephen Cameron8be986c2015-04-23 09:34:06 -05007163 else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007164 complete(c->waiting);
Stephen M. Camerona104c992010-02-04 08:42:24 -06007165}
7166
Don Brace303932f2010-02-04 08:42:40 -06007167/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05007168static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06007169 u32 raw_tag)
7170{
7171 u32 tag_index;
7172 struct CommandList *c;
7173
Don Bracef2405db2015-01-23 16:43:09 -06007174 tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05007175 if (!bad_tag(h, tag_index, raw_tag)) {
7176 c = h->cmd_pool + tag_index;
7177 finish_cmd(c);
7178 }
Don Brace303932f2010-02-04 08:42:40 -06007179}
7180
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007181/* Some controllers, like p400, will give us one interrupt
7182 * after a soft reset, even if we turned interrupts off.
7183 * Only need to check for this in the hpsa_xxx_discard_completions
7184 * functions.
7185 */
7186static int ignore_bogus_interrupt(struct ctlr_info *h)
7187{
7188 if (likely(!reset_devices))
7189 return 0;
7190
7191 if (likely(h->interrupts_enabled))
7192 return 0;
7193
7194 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
7195 "(known firmware bug.) Ignoring.\n");
7196
7197 return 1;
7198}
7199
Matt Gates254f7962012-05-01 11:43:06 -05007200/*
7201 * Convert &h->q[x] (passed to interrupt handlers) back to h.
7202 * Relies on (h-q[x] == x) being true for x such that
7203 * 0 <= x < MAX_REPLY_QUEUES.
7204 */
7205static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007206{
Matt Gates254f7962012-05-01 11:43:06 -05007207 return container_of((queue - *queue), struct ctlr_info, q[0]);
7208}
7209
7210static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
7211{
7212 struct ctlr_info *h = queue_to_hba(queue);
7213 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007214 u32 raw_tag;
7215
7216 if (ignore_bogus_interrupt(h))
7217 return IRQ_NONE;
7218
7219 if (interrupt_not_for_us(h))
7220 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007221 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007222 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05007223 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007224 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05007225 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007226 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007227 return IRQ_HANDLED;
7228}
7229
Matt Gates254f7962012-05-01 11:43:06 -05007230static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007231{
Matt Gates254f7962012-05-01 11:43:06 -05007232 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007233 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05007234 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007235
7236 if (ignore_bogus_interrupt(h))
7237 return IRQ_NONE;
7238
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007239 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05007240 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007241 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05007242 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007243 return IRQ_HANDLED;
7244}
7245
Matt Gates254f7962012-05-01 11:43:06 -05007246static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007247{
Matt Gates254f7962012-05-01 11:43:06 -05007248 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06007249 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05007250 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007251
7252 if (interrupt_not_for_us(h))
7253 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007254 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007255 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05007256 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007257 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06007258 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05007259 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007260 }
7261 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007262 return IRQ_HANDLED;
7263}
7264
Matt Gates254f7962012-05-01 11:43:06 -05007265static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007266{
Matt Gates254f7962012-05-01 11:43:06 -05007267 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007268 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05007269 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007270
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007271 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05007272 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06007273 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06007274 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05007275 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007276 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007277 return IRQ_HANDLED;
7278}
7279
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06007280/* Send a message CDB to the firmware. Careful, this only works
7281 * in simple mode, not performant mode due to the tag lookup.
7282 * We only ever use this immediately after a controller reset.
7283 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007284static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
7285 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007286{
7287 struct Command {
7288 struct CommandListHeader CommandHeader;
7289 struct RequestBlock Request;
7290 struct ErrDescriptor ErrorDescriptor;
7291 };
7292 struct Command *cmd;
7293 static const size_t cmd_sz = sizeof(*cmd) +
7294 sizeof(cmd->ErrorDescriptor);
7295 dma_addr_t paddr64;
Don Brace2b08b3e2015-01-23 16:41:09 -06007296 __le32 paddr32;
7297 u32 tag;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007298 void __iomem *vaddr;
7299 int i, err;
7300
7301 vaddr = pci_ioremap_bar(pdev, 0);
7302 if (vaddr == NULL)
7303 return -ENOMEM;
7304
7305 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
7306 * CCISS commands, so they must be allocated from the lower 4GiB of
7307 * memory.
7308 */
7309 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
7310 if (err) {
7311 iounmap(vaddr);
Robert Elliott1eaec8f2015-01-23 16:42:37 -06007312 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007313 }
7314
7315 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
7316 if (cmd == NULL) {
7317 iounmap(vaddr);
7318 return -ENOMEM;
7319 }
7320
7321 /* This must fit, because of the 32-bit consistent DMA mask. Also,
7322 * although there's no guarantee, we assume that the address is at
7323 * least 4-byte aligned (most likely, it's page-aligned).
7324 */
Don Brace2b08b3e2015-01-23 16:41:09 -06007325 paddr32 = cpu_to_le32(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007326
7327 cmd->CommandHeader.ReplyQueue = 0;
7328 cmd->CommandHeader.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007329 cmd->CommandHeader.SGTotal = cpu_to_le16(0);
Don Brace2b08b3e2015-01-23 16:41:09 -06007330 cmd->CommandHeader.tag = cpu_to_le64(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007331 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
7332
7333 cmd->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06007334 cmd->Request.type_attr_dir =
7335 TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007336 cmd->Request.Timeout = 0; /* Don't time out */
7337 cmd->Request.CDB[0] = opcode;
7338 cmd->Request.CDB[1] = type;
7339 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007340 cmd->ErrorDescriptor.Addr =
Don Brace2b08b3e2015-01-23 16:41:09 -06007341 cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007342 cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007343
Don Brace2b08b3e2015-01-23 16:41:09 -06007344 writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007345
7346 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
7347 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Don Brace2b08b3e2015-01-23 16:41:09 -06007348 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007349 break;
7350 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
7351 }
7352
7353 iounmap(vaddr);
7354
7355 /* we leak the DMA buffer here ... no choice since the controller could
7356 * still complete the command.
7357 */
7358 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
7359 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
7360 opcode, type);
7361 return -ETIMEDOUT;
7362 }
7363
7364 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
7365
7366 if (tag & HPSA_ERROR_BIT) {
7367 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
7368 opcode, type);
7369 return -EIO;
7370 }
7371
7372 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
7373 opcode, type);
7374 return 0;
7375}
7376
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007377#define hpsa_noop(p) hpsa_message(p, 3, 0)
7378
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007379static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Don Brace42a91642014-11-14 17:26:27 -06007380 void __iomem *vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007381{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007382
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007383 if (use_doorbell) {
7384 /* For everything after the P600, the PCI power state method
7385 * of resetting the controller doesn't work, so we have this
7386 * other way using the doorbell register.
7387 */
7388 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007389 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05007390
Justin Lindley00701a92014-05-29 10:52:47 -05007391 /* PMC hardware guys tell us we need a 10 second delay after
Stephen M. Cameron85009232013-09-23 13:33:36 -05007392 * doorbell reset and before any attempt to talk to the board
7393 * at all to ensure that this actually works and doesn't fall
7394 * over in some weird corner cases.
7395 */
Justin Lindley00701a92014-05-29 10:52:47 -05007396 msleep(10000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007397 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007398
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007399 /* Quoting from the Open CISS Specification: "The Power
7400 * Management Control/Status Register (CSR) controls the power
7401 * state of the device. The normal operating state is D0,
7402 * CSR=00h. The software off state is D3, CSR=03h. To reset
7403 * the controller, place the interface device in D3 then to D0,
7404 * this causes a secondary PCI reset which will reset the
7405 * controller." */
7406
Don Brace2662cab2015-01-23 16:41:25 -06007407 int rc = 0;
7408
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007409 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
Don Brace2662cab2015-01-23 16:41:25 -06007410
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007411 /* enter the D3hot power management state */
Don Brace2662cab2015-01-23 16:41:25 -06007412 rc = pci_set_power_state(pdev, PCI_D3hot);
7413 if (rc)
7414 return rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007415
7416 msleep(500);
7417
7418 /* enter the D0 power management state */
Don Brace2662cab2015-01-23 16:41:25 -06007419 rc = pci_set_power_state(pdev, PCI_D0);
7420 if (rc)
7421 return rc;
Mike Millerc4853ef2011-10-21 08:19:43 +02007422
7423 /*
7424 * The P600 requires a small delay when changing states.
7425 * Otherwise we may think the board did not reset and we bail.
7426 * This for kdump only and is particular to the P600.
7427 */
7428 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007429 }
7430 return 0;
7431}
7432
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007433static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007434{
7435 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06007436 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007437}
7438
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007439static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007440{
7441 char *driver_version;
7442 int i, size = sizeof(cfgtable->driver_version);
7443
7444 driver_version = kmalloc(size, GFP_KERNEL);
7445 if (!driver_version)
7446 return -ENOMEM;
7447
7448 init_driver_version(driver_version, size);
7449 for (i = 0; i < size; i++)
7450 writeb(driver_version[i], &cfgtable->driver_version[i]);
7451 kfree(driver_version);
7452 return 0;
7453}
7454
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007455static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
7456 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007457{
7458 int i;
7459
7460 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
7461 driver_ver[i] = readb(&cfgtable->driver_version[i]);
7462}
7463
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007464static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007465{
7466
7467 char *driver_ver, *old_driver_ver;
7468 int rc, size = sizeof(cfgtable->driver_version);
7469
7470 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
7471 if (!old_driver_ver)
7472 return -ENOMEM;
7473 driver_ver = old_driver_ver + size;
7474
7475 /* After a reset, the 32 bytes of "driver version" in the cfgtable
7476 * should have been changed, otherwise we know the reset failed.
7477 */
7478 init_driver_version(old_driver_ver, size);
7479 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
7480 rc = !memcmp(driver_ver, old_driver_ver, size);
7481 kfree(old_driver_ver);
7482 return rc;
7483}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007484/* This does a hard reset of the controller using PCI power management
7485 * states or the using the doorbell register.
7486 */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007487static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007488{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007489 u64 cfg_offset;
7490 u32 cfg_base_addr;
7491 u64 cfg_base_addr_index;
7492 void __iomem *vaddr;
7493 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007494 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007495 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007496 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007497 u32 use_doorbell;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007498 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007499
7500 /* For controllers as old as the P600, this is very nearly
7501 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007502 *
7503 * pci_save_state(pci_dev);
7504 * pci_set_power_state(pci_dev, PCI_D3hot);
7505 * pci_set_power_state(pci_dev, PCI_D0);
7506 * pci_restore_state(pci_dev);
7507 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007508 * For controllers newer than the P600, the pci power state
7509 * method of resetting doesn't work so we have another way
7510 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007511 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05007512
Robert Elliott60f923b2015-01-23 16:42:06 -06007513 if (!ctlr_is_resettable(board_id)) {
7514 dev_warn(&pdev->dev, "Controller not resettable\n");
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06007515 return -ENODEV;
7516 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05007517
7518 /* if controller is soft- but not hard resettable... */
7519 if (!ctlr_is_hard_resettable(board_id))
7520 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05007521
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007522 /* Save the PCI command register */
7523 pci_read_config_word(pdev, 4, &command_register);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007524 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007525
7526 /* find the first memory BAR, so we can find the cfg table */
7527 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
7528 if (rc)
7529 return rc;
7530 vaddr = remap_pci_mem(paddr, 0x250);
7531 if (!vaddr)
7532 return -ENOMEM;
7533
7534 /* find cfgtable in order to check if reset via doorbell is supported */
7535 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
7536 &cfg_base_addr_index, &cfg_offset);
7537 if (rc)
7538 goto unmap_vaddr;
7539 cfgtable = remap_pci_mem(pci_resource_start(pdev,
7540 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
7541 if (!cfgtable) {
7542 rc = -ENOMEM;
7543 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007544 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007545 rc = write_driver_ver_to_cfgtable(cfgtable);
7546 if (rc)
Tomas Henzl03741d92015-01-23 16:41:14 -06007547 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007548
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007549 /* If reset via doorbell register is supported, use that.
7550 * There are two such methods. Favor the newest method.
7551 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007552 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007553 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
7554 if (use_doorbell) {
7555 use_doorbell = DOORBELL_CTLR_RESET2;
7556 } else {
7557 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
7558 if (use_doorbell) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007559 dev_warn(&pdev->dev,
7560 "Soft reset not supported. Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007561 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007562 goto unmap_cfgtable;
7563 }
7564 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007565
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007566 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
7567 if (rc)
7568 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007569
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007570 pci_restore_state(pdev);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007571 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007572
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007573 /* Some devices (notably the HP Smart Array 5i Controller)
7574 need a little pause here */
7575 msleep(HPSA_POST_RESET_PAUSE_MSECS);
7576
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007577 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
7578 if (rc) {
7579 dev_warn(&pdev->dev,
Stephen Cameron050f7142015-01-23 16:42:22 -06007580 "Failed waiting for board to become ready after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007581 goto unmap_cfgtable;
7582 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007583
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007584 rc = controller_reset_failed(vaddr);
7585 if (rc < 0)
7586 goto unmap_cfgtable;
7587 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007588 dev_warn(&pdev->dev, "Unable to successfully reset "
7589 "controller. Will try soft reset.\n");
7590 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007591 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007592 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007593 }
7594
7595unmap_cfgtable:
7596 iounmap(cfgtable);
7597
7598unmap_vaddr:
7599 iounmap(vaddr);
7600 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007601}
7602
7603/*
7604 * We cannot read the structure directly, for portability we must use
7605 * the io functions.
7606 * This is for debug only.
7607 */
Don Brace42a91642014-11-14 17:26:27 -06007608static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007609{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007610#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007611 int i;
7612 char temp_name[17];
7613
7614 dev_info(dev, "Controller Configuration information\n");
7615 dev_info(dev, "------------------------------------\n");
7616 for (i = 0; i < 4; i++)
7617 temp_name[i] = readb(&(tb->Signature[i]));
7618 temp_name[4] = '\0';
7619 dev_info(dev, " Signature = %s\n", temp_name);
7620 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
7621 dev_info(dev, " Transport methods supported = 0x%x\n",
7622 readl(&(tb->TransportSupport)));
7623 dev_info(dev, " Transport methods active = 0x%x\n",
7624 readl(&(tb->TransportActive)));
7625 dev_info(dev, " Requested transport Method = 0x%x\n",
7626 readl(&(tb->HostWrite.TransportRequest)));
7627 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
7628 readl(&(tb->HostWrite.CoalIntDelay)));
7629 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
7630 readl(&(tb->HostWrite.CoalIntCount)));
Robert Elliott69d6e332015-01-23 16:41:56 -06007631 dev_info(dev, " Max outstanding commands = %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007632 readl(&(tb->CmdsOutMax)));
7633 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
7634 for (i = 0; i < 16; i++)
7635 temp_name[i] = readb(&(tb->ServerName[i]));
7636 temp_name[16] = '\0';
7637 dev_info(dev, " Server Name = %s\n", temp_name);
7638 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
7639 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007640#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007641}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007642
7643static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
7644{
7645 int i, offset, mem_type, bar_type;
7646
7647 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
7648 return 0;
7649 offset = 0;
7650 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
7651 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
7652 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
7653 offset += 4;
7654 else {
7655 mem_type = pci_resource_flags(pdev, i) &
7656 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
7657 switch (mem_type) {
7658 case PCI_BASE_ADDRESS_MEM_TYPE_32:
7659 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
7660 offset += 4; /* 32 bit */
7661 break;
7662 case PCI_BASE_ADDRESS_MEM_TYPE_64:
7663 offset += 8;
7664 break;
7665 default: /* reserved in PCI 2.2 */
7666 dev_warn(&pdev->dev,
7667 "base address is invalid\n");
7668 return -1;
7669 break;
7670 }
7671 }
7672 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
7673 return i + 1;
7674 }
7675 return -1;
7676}
7677
Robert Elliottcc64c812015-04-23 09:33:12 -05007678static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
7679{
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007680 pci_free_irq_vectors(h->pdev);
7681 h->msix_vectors = 0;
Robert Elliottcc64c812015-04-23 09:33:12 -05007682}
7683
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007684/* If MSI/MSI-X is supported by the kernel we will try to enable it on
Stephen Cameron050f7142015-01-23 16:42:22 -06007685 * controllers that are capable. If not, we use legacy INTx mode.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007686 */
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007687static int hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007688{
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007689 unsigned int flags = PCI_IRQ_LEGACY;
7690 int ret;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007691
7692 /* Some boards advertise MSI but don't really support it */
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007693 switch (h->board_id) {
7694 case 0x40700E11:
7695 case 0x40800E11:
7696 case 0x40820E11:
7697 case 0x40830E11:
7698 break;
7699 default:
7700 ret = pci_alloc_irq_vectors(h->pdev, 1, MAX_REPLY_QUEUES,
7701 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
7702 if (ret > 0) {
7703 h->msix_vectors = ret;
7704 return 0;
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007705 }
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007706
7707 flags |= PCI_IRQ_MSI;
7708 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007709 }
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007710
7711 ret = pci_alloc_irq_vectors(h->pdev, 1, 1, flags);
7712 if (ret < 0)
7713 return ret;
7714 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007715}
7716
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007717static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007718{
7719 int i;
7720 u32 subsystem_vendor_id, subsystem_device_id;
7721
7722 subsystem_vendor_id = pdev->subsystem_vendor;
7723 subsystem_device_id = pdev->subsystem_device;
7724 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
7725 subsystem_vendor_id;
7726
7727 for (i = 0; i < ARRAY_SIZE(products); i++)
7728 if (*board_id == products[i].board_id)
7729 return i;
7730
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05007731 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
7732 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
7733 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007734 dev_warn(&pdev->dev, "unrecognized board ID: "
7735 "0x%08x, ignoring.\n", *board_id);
7736 return -ENODEV;
7737 }
7738 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
7739}
7740
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007741static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
7742 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007743{
7744 int i;
7745
7746 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007747 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007748 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007749 *memory_bar = pci_resource_start(pdev, i);
7750 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007751 *memory_bar);
7752 return 0;
7753 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007754 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007755 return -ENODEV;
7756}
7757
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007758static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
7759 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007760{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007761 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007762 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007763 if (wait_for_ready)
7764 iterations = HPSA_BOARD_READY_ITERATIONS;
7765 else
7766 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007767
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007768 for (i = 0; i < iterations; i++) {
7769 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
7770 if (wait_for_ready) {
7771 if (scratchpad == HPSA_FIRMWARE_READY)
7772 return 0;
7773 } else {
7774 if (scratchpad != HPSA_FIRMWARE_READY)
7775 return 0;
7776 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007777 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
7778 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007779 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007780 return -ENODEV;
7781}
7782
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007783static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
7784 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
7785 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007786{
7787 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
7788 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
7789 *cfg_base_addr &= (u32) 0x0000ffff;
7790 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
7791 if (*cfg_base_addr_index == -1) {
7792 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
7793 return -ENODEV;
7794 }
7795 return 0;
7796}
7797
Robert Elliott195f2c62015-04-23 09:33:17 -05007798static void hpsa_free_cfgtables(struct ctlr_info *h)
7799{
Robert Elliott105a3db2015-04-23 09:33:48 -05007800 if (h->transtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007801 iounmap(h->transtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007802 h->transtable = NULL;
7803 }
7804 if (h->cfgtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007805 iounmap(h->cfgtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007806 h->cfgtable = NULL;
7807 }
Robert Elliott195f2c62015-04-23 09:33:17 -05007808}
7809
7810/* Find and map CISS config table and transfer table
7811+ * several items must be unmapped (freed) later
7812+ * */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007813static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007814{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06007815 u64 cfg_offset;
7816 u32 cfg_base_addr;
7817 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06007818 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007819 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007820
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007821 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7822 &cfg_base_addr_index, &cfg_offset);
7823 if (rc)
7824 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007825 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007826 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007827 if (!h->cfgtable) {
7828 dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007829 return -ENOMEM;
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007830 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007831 rc = write_driver_ver_to_cfgtable(h->cfgtable);
7832 if (rc)
7833 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007834 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007835 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007836 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
7837 cfg_base_addr_index)+cfg_offset+trans_offset,
7838 sizeof(*h->transtable));
Robert Elliott195f2c62015-04-23 09:33:17 -05007839 if (!h->transtable) {
7840 dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
7841 hpsa_free_cfgtables(h);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007842 return -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05007843 }
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007844 return 0;
7845}
7846
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007847static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007848{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007849#define MIN_MAX_COMMANDS 16
7850 BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
7851
7852 h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06007853
7854 /* Limit commands in memory limited kdump scenario. */
7855 if (reset_devices && h->max_commands > 32)
7856 h->max_commands = 32;
7857
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007858 if (h->max_commands < MIN_MAX_COMMANDS) {
7859 dev_warn(&h->pdev->dev,
7860 "Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
7861 h->max_commands,
7862 MIN_MAX_COMMANDS);
7863 h->max_commands = MIN_MAX_COMMANDS;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007864 }
7865}
7866
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007867/* If the controller reports that the total max sg entries is greater than 512,
7868 * then we know that chained SG blocks work. (Original smart arrays did not
7869 * support chained SG blocks and would return zero for max sg entries.)
7870 */
7871static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
7872{
7873 return h->maxsgentries > 512;
7874}
7875
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007876/* Interrogate the hardware for some limits:
7877 * max commands, max SG elements without chaining, and with chaining,
7878 * SG chain block size, etc.
7879 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007880static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007881{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007882 hpsa_get_max_perf_mode_cmds(h);
Stephen Cameron45fcb862015-01-23 16:43:04 -06007883 h->nr_cmds = h->max_commands;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007884 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007885 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007886 if (hpsa_supports_chained_sg_blocks(h)) {
7887 /* Limit in-command s/g elements to 32 save dma'able memory. */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007888 h->max_cmd_sg_entries = 32;
Webb Scales1a63ea62014-11-14 17:26:43 -06007889 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007890 h->maxsgentries--; /* save one for chain pointer */
7891 } else {
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007892 /*
7893 * Original smart arrays supported at most 31 s/g entries
7894 * embedded inline in the command (trying to use more
7895 * would lock up the controller)
7896 */
7897 h->max_cmd_sg_entries = 31;
Webb Scales1a63ea62014-11-14 17:26:43 -06007898 h->maxsgentries = 31; /* default to traditional values */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007899 h->chainsize = 0;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007900 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05007901
7902 /* Find out what task management functions are supported and cache */
7903 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06007904 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
7905 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
7906 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
7907 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen Cameron8be986c2015-04-23 09:34:06 -05007908 if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
7909 dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007910}
7911
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007912static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
7913{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09007914 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007915 dev_err(&h->pdev->dev, "not a valid CISS config table\n");
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007916 return false;
7917 }
7918 return true;
7919}
7920
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007921static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007922{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007923 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007924
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007925 driver_support = readl(&(h->cfgtable->driver_support));
Arnd Bergmann0b9e7b72014-06-26 15:44:52 +02007926 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
7927#ifdef CONFIG_X86
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007928 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007929#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06007930 driver_support |= ENABLE_UNIT_ATTN;
7931 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007932}
7933
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05007934/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
7935 * in a prefetch beyond physical memory.
7936 */
7937static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
7938{
7939 u32 dma_prefetch;
7940
7941 if (h->board_id != 0x3225103C)
7942 return;
7943 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
7944 dma_prefetch |= 0x8000;
7945 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
7946}
7947
Robert Elliottc706a792015-01-23 16:45:01 -06007948static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007949{
7950 int i;
7951 u32 doorbell_value;
7952 unsigned long flags;
7953 /* wait until the clear_event_notify bit 6 is cleared by controller. */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007954 for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007955 spin_lock_irqsave(&h->lock, flags);
7956 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7957 spin_unlock_irqrestore(&h->lock, flags);
7958 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
Robert Elliottc706a792015-01-23 16:45:01 -06007959 goto done;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007960 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007961 msleep(CLEAR_EVENT_WAIT_INTERVAL);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007962 }
Robert Elliottc706a792015-01-23 16:45:01 -06007963 return -ENODEV;
7964done:
7965 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007966}
7967
Robert Elliottc706a792015-01-23 16:45:01 -06007968static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007969{
7970 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007971 u32 doorbell_value;
7972 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007973
7974 /* under certain very rare conditions, this can take awhile.
7975 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
7976 * as we enter this code.)
7977 */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007978 for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
Webb Scales25163bd2015-04-23 09:32:00 -05007979 if (h->remove_in_progress)
7980 goto done;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007981 spin_lock_irqsave(&h->lock, flags);
7982 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7983 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06007984 if (!(doorbell_value & CFGTBL_ChangeReq))
Robert Elliottc706a792015-01-23 16:45:01 -06007985 goto done;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007986 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007987 msleep(MODE_CHANGE_WAIT_INTERVAL);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007988 }
Robert Elliottc706a792015-01-23 16:45:01 -06007989 return -ENODEV;
7990done:
7991 return 0;
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007992}
7993
Robert Elliottc706a792015-01-23 16:45:01 -06007994/* return -ENODEV or other reason on error, 0 on success */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007995static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007996{
7997 u32 trans_support;
7998
7999 trans_support = readl(&(h->cfgtable->TransportSupport));
8000 if (!(trans_support & SIMPLE_MODE))
8001 return -ENOTSUPP;
8002
8003 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06008004
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05008005 /* Update the field, and then ring the doorbell */
8006 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008007 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05008008 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06008009 if (hpsa_wait_for_mode_change_ack(h))
8010 goto error;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05008011 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06008012 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
8013 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06008014 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05008015 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06008016error:
Stephen Cameron050f7142015-01-23 16:42:22 -06008017 dev_err(&h->pdev->dev, "failed to enter simple mode\n");
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06008018 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05008019}
8020
Robert Elliott195f2c62015-04-23 09:33:17 -05008021/* free items allocated or mapped by hpsa_pci_init */
8022static void hpsa_free_pci_init(struct ctlr_info *h)
8023{
8024 hpsa_free_cfgtables(h); /* pci_init 4 */
8025 iounmap(h->vaddr); /* pci_init 3 */
Robert Elliott105a3db2015-04-23 09:33:48 -05008026 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05008027 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Robert Elliott943a7022015-04-23 09:34:32 -05008028 /*
8029 * call pci_disable_device before pci_release_regions per
8030 * Documentation/PCI/pci.txt
8031 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008032 pci_disable_device(h->pdev); /* pci_init 1 */
Robert Elliott943a7022015-04-23 09:34:32 -05008033 pci_release_regions(h->pdev); /* pci_init 2 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008034}
8035
8036/* several items must be freed later */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008037static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05008038{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05008039 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008040
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05008041 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
8042 if (prod_index < 0)
Robert Elliott60f923b2015-01-23 16:42:06 -06008043 return prod_index;
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05008044 h->product_name = products[prod_index].product_name;
8045 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008046
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05008047 h->needs_abort_tags_swizzled =
8048 ctlr_needs_abort_tags_swizzled(h->board_id);
8049
Matthew Garrette5a44df2011-11-11 11:14:23 -05008050 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
8051 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
8052
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008053 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008054 if (err) {
Robert Elliott195f2c62015-04-23 09:33:17 -05008055 dev_err(&h->pdev->dev, "failed to enable PCI device\n");
Robert Elliott943a7022015-04-23 09:34:32 -05008056 pci_disable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008057 return err;
8058 }
8059
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06008060 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008061 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008062 dev_err(&h->pdev->dev,
Robert Elliott195f2c62015-04-23 09:33:17 -05008063 "failed to obtain PCI resources\n");
Robert Elliott943a7022015-04-23 09:34:32 -05008064 pci_disable_device(h->pdev);
8065 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008066 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06008067
8068 pci_set_master(h->pdev);
8069
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008070 err = hpsa_interrupt_mode(h);
8071 if (err)
8072 goto clean1;
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05008073 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05008074 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05008075 goto clean2; /* intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008076 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05008077 if (!h->vaddr) {
Robert Elliott195f2c62015-04-23 09:33:17 -05008078 dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
Stephen M. Cameron204892e2010-05-27 15:13:22 -05008079 err = -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05008080 goto clean2; /* intmode+region, pci */
Stephen M. Cameron204892e2010-05-27 15:13:22 -05008081 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06008082 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05008083 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05008084 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameron77c44952010-05-27 15:13:17 -05008085 err = hpsa_find_cfgtables(h);
8086 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05008087 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05008088 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008089
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05008090 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008091 err = -ENODEV;
Robert Elliott195f2c62015-04-23 09:33:17 -05008092 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008093 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06008094 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05008095 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05008096 err = hpsa_enter_simple_mode(h);
8097 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05008098 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008099 return 0;
8100
Robert Elliott195f2c62015-04-23 09:33:17 -05008101clean4: /* cfgtables, vaddr, intmode+region, pci */
8102 hpsa_free_cfgtables(h);
8103clean3: /* vaddr, intmode+region, pci */
8104 iounmap(h->vaddr);
Robert Elliott105a3db2015-04-23 09:33:48 -05008105 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05008106clean2: /* intmode+region, pci */
8107 hpsa_disable_interrupt_mode(h);
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008108clean1:
Robert Elliott943a7022015-04-23 09:34:32 -05008109 /*
8110 * call pci_disable_device before pci_release_regions per
8111 * Documentation/PCI/pci.txt
8112 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008113 pci_disable_device(h->pdev);
Robert Elliott943a7022015-04-23 09:34:32 -05008114 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008115 return err;
8116}
8117
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008118static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06008119{
8120 int rc;
8121
8122#define HBA_INQUIRY_BYTE_COUNT 64
8123 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
8124 if (!h->hba_inquiry_data)
8125 return;
8126 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
8127 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
8128 if (rc != 0) {
8129 kfree(h->hba_inquiry_data);
8130 h->hba_inquiry_data = NULL;
8131 }
8132}
8133
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008134static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008135{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05008136 int rc, i;
Tomas Henzl3b747292015-01-23 16:41:20 -06008137 void __iomem *vaddr;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008138
8139 if (!reset_devices)
8140 return 0;
8141
Tomas Henzl132aa222014-08-14 16:12:39 +02008142 /* kdump kernel is loading, we don't know in which state is
8143 * the pci interface. The dev->enable_cnt is equal zero
8144 * so we call enable+disable, wait a while and switch it on.
8145 */
8146 rc = pci_enable_device(pdev);
8147 if (rc) {
8148 dev_warn(&pdev->dev, "Failed to enable PCI device\n");
8149 return -ENODEV;
8150 }
8151 pci_disable_device(pdev);
8152 msleep(260); /* a randomly chosen number */
8153 rc = pci_enable_device(pdev);
8154 if (rc) {
8155 dev_warn(&pdev->dev, "failed to enable device.\n");
8156 return -ENODEV;
8157 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06008158
Tomas Henzl859c75a2014-09-12 14:44:15 +02008159 pci_set_master(pdev);
Robert Elliott4fa604e2014-11-14 17:27:24 -06008160
Tomas Henzl3b747292015-01-23 16:41:20 -06008161 vaddr = pci_ioremap_bar(pdev, 0);
8162 if (vaddr == NULL) {
8163 rc = -ENOMEM;
8164 goto out_disable;
8165 }
8166 writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
8167 iounmap(vaddr);
8168
Stephen M. Cameron1df85522010-06-16 13:51:40 -05008169 /* Reset the controller with a PCI power-cycle or via doorbell */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008170 rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008171
Stephen M. Cameron1df85522010-06-16 13:51:40 -05008172 /* -ENOTSUPP here means we cannot reset the controller
8173 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05008174 * "performant mode". Or, it might be 640x, which can't reset
8175 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05008176 */
Robert Elliottadf1b3a2015-01-23 16:42:01 -06008177 if (rc)
Tomas Henzl132aa222014-08-14 16:12:39 +02008178 goto out_disable;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008179
8180 /* Now try to get the controller to respond to a no-op */
Robert Elliott1ba66c92015-01-23 16:42:11 -06008181 dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008182 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
8183 if (hpsa_noop(pdev) == 0)
8184 break;
8185 else
8186 dev_warn(&pdev->dev, "no-op failed%s\n",
8187 (i < 11 ? "; re-trying" : ""));
8188 }
Tomas Henzl132aa222014-08-14 16:12:39 +02008189
8190out_disable:
8191
8192 pci_disable_device(pdev);
8193 return rc;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008194}
8195
Robert Elliott1fb7c982015-04-23 09:33:22 -05008196static void hpsa_free_cmd_pool(struct ctlr_info *h)
8197{
8198 kfree(h->cmd_pool_bits);
Robert Elliott105a3db2015-04-23 09:33:48 -05008199 h->cmd_pool_bits = NULL;
8200 if (h->cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05008201 pci_free_consistent(h->pdev,
8202 h->nr_cmds * sizeof(struct CommandList),
8203 h->cmd_pool,
8204 h->cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05008205 h->cmd_pool = NULL;
8206 h->cmd_pool_dhandle = 0;
8207 }
8208 if (h->errinfo_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05008209 pci_free_consistent(h->pdev,
8210 h->nr_cmds * sizeof(struct ErrorInfo),
8211 h->errinfo_pool,
8212 h->errinfo_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05008213 h->errinfo_pool = NULL;
8214 h->errinfo_pool_dhandle = 0;
8215 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05008216}
8217
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008218static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008219{
8220 h->cmd_pool_bits = kzalloc(
8221 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
8222 sizeof(unsigned long), GFP_KERNEL);
8223 h->cmd_pool = pci_alloc_consistent(h->pdev,
8224 h->nr_cmds * sizeof(*h->cmd_pool),
8225 &(h->cmd_pool_dhandle));
8226 h->errinfo_pool = pci_alloc_consistent(h->pdev,
8227 h->nr_cmds * sizeof(*h->errinfo_pool),
8228 &(h->errinfo_pool_dhandle));
8229 if ((h->cmd_pool_bits == NULL)
8230 || (h->cmd_pool == NULL)
8231 || (h->errinfo_pool == NULL)) {
8232 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
Robert Elliott2c143342015-01-23 16:42:48 -06008233 goto clean_up;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008234 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05008235 hpsa_preinitialize_commands(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008236 return 0;
Robert Elliott2c143342015-01-23 16:42:48 -06008237clean_up:
8238 hpsa_free_cmd_pool(h);
8239 return -ENOMEM;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008240}
8241
Robert Elliottec501a12015-01-23 16:41:40 -06008242/* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
8243static void hpsa_free_irqs(struct ctlr_info *h)
8244{
8245 int i;
8246
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008247 if (!h->msix_vectors || h->intr_mode != PERF_MODE_INT) {
Robert Elliottec501a12015-01-23 16:41:40 -06008248 /* Single reply queue, only one irq to free */
Colin Ian King7dc62d92016-11-14 12:59:35 +00008249 free_irq(pci_irq_vector(h->pdev, 0), &h->q[h->intr_mode]);
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008250 h->q[h->intr_mode] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008251 return;
8252 }
8253
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008254 for (i = 0; i < h->msix_vectors; i++) {
8255 free_irq(pci_irq_vector(h->pdev, i), &h->q[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008256 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008257 }
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008258 for (; i < MAX_REPLY_QUEUES; i++)
8259 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008260}
8261
Robert Elliott9ee61792015-01-23 16:42:32 -06008262/* returns 0 on success; cleans up and returns -Enn on error */
8263static int hpsa_request_irqs(struct ctlr_info *h,
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008264 irqreturn_t (*msixhandler)(int, void *),
8265 irqreturn_t (*intxhandler)(int, void *))
8266{
Matt Gates254f7962012-05-01 11:43:06 -05008267 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008268
Matt Gates254f7962012-05-01 11:43:06 -05008269 /*
8270 * initialize h->q[x] = x so that interrupt handlers know which
8271 * queue to process.
8272 */
8273 for (i = 0; i < MAX_REPLY_QUEUES; i++)
8274 h->q[i] = (u8) i;
8275
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008276 if (h->intr_mode == PERF_MODE_INT && h->msix_vectors > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05008277 /* If performant mode and MSI-X, use multiple reply queues */
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008278 for (i = 0; i < h->msix_vectors; i++) {
Robert Elliott8b470042015-04-23 09:34:58 -05008279 sprintf(h->intrname[i], "%s-msix%d", h->devname, i);
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008280 rc = request_irq(pci_irq_vector(h->pdev, i), msixhandler,
Robert Elliott8b470042015-04-23 09:34:58 -05008281 0, h->intrname[i],
Matt Gates254f7962012-05-01 11:43:06 -05008282 &h->q[i]);
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008283 if (rc) {
8284 int j;
8285
8286 dev_err(&h->pdev->dev,
8287 "failed to get irq %d for %s\n",
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008288 pci_irq_vector(h->pdev, i), h->devname);
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008289 for (j = 0; j < i; j++) {
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008290 free_irq(pci_irq_vector(h->pdev, j), &h->q[j]);
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008291 h->q[j] = 0;
8292 }
8293 for (; j < MAX_REPLY_QUEUES; j++)
8294 h->q[j] = 0;
8295 return rc;
8296 }
8297 }
Matt Gates254f7962012-05-01 11:43:06 -05008298 } else {
8299 /* Use single reply pool */
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008300 if (h->msix_vectors > 0 || h->pdev->msi_enabled) {
8301 sprintf(h->intrname[0], "%s-msi%s", h->devname,
8302 h->msix_vectors ? "x" : "");
8303 rc = request_irq(pci_irq_vector(h->pdev, 0),
Robert Elliott8b470042015-04-23 09:34:58 -05008304 msixhandler, 0,
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008305 h->intrname[0],
Matt Gates254f7962012-05-01 11:43:06 -05008306 &h->q[h->intr_mode]);
8307 } else {
Robert Elliott8b470042015-04-23 09:34:58 -05008308 sprintf(h->intrname[h->intr_mode],
8309 "%s-intx", h->devname);
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008310 rc = request_irq(pci_irq_vector(h->pdev, 0),
Robert Elliott8b470042015-04-23 09:34:58 -05008311 intxhandler, IRQF_SHARED,
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008312 h->intrname[0],
Matt Gates254f7962012-05-01 11:43:06 -05008313 &h->q[h->intr_mode]);
8314 }
8315 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008316 if (rc) {
Robert Elliott195f2c62015-04-23 09:33:17 -05008317 dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08008318 pci_irq_vector(h->pdev, 0), h->devname);
Robert Elliott195f2c62015-04-23 09:33:17 -05008319 hpsa_free_irqs(h);
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008320 return -ENODEV;
8321 }
8322 return 0;
8323}
8324
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008325static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008326{
Robert Elliott39c53f52015-04-23 09:35:14 -05008327 int rc;
Robert Elliottbf43caf2015-04-23 09:33:38 -05008328 hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008329
8330 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008331 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY);
8332 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008333 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008334 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008335 }
8336
8337 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008338 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
8339 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008340 dev_warn(&h->pdev->dev, "Board failed to become ready "
8341 "after soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008342 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008343 }
8344
8345 return 0;
8346}
8347
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008348static void hpsa_free_reply_queues(struct ctlr_info *h)
8349{
8350 int i;
8351
8352 for (i = 0; i < h->nreply_queues; i++) {
8353 if (!h->reply_queue[i].head)
8354 continue;
Robert Elliott1fb7c982015-04-23 09:33:22 -05008355 pci_free_consistent(h->pdev,
8356 h->reply_queue_size,
8357 h->reply_queue[i].head,
8358 h->reply_queue[i].busaddr);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008359 h->reply_queue[i].head = NULL;
8360 h->reply_queue[i].busaddr = 0;
8361 }
Robert Elliott105a3db2015-04-23 09:33:48 -05008362 h->reply_queue_size = 0;
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008363}
8364
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05008365static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
8366{
Robert Elliott105a3db2015-04-23 09:33:48 -05008367 hpsa_free_performant_mode(h); /* init_one 7 */
8368 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
8369 hpsa_free_cmd_pool(h); /* init_one 5 */
8370 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliott2946e822015-04-23 09:35:09 -05008371 scsi_host_put(h->scsi_host); /* init_one 3 */
8372 h->scsi_host = NULL; /* init_one 3 */
8373 hpsa_free_pci_init(h); /* init_one 2_5 */
Robert Elliott9ecd9532015-04-23 09:34:43 -05008374 free_percpu(h->lockup_detected); /* init_one 2 */
8375 h->lockup_detected = NULL; /* init_one 2 */
8376 if (h->resubmit_wq) {
8377 destroy_workqueue(h->resubmit_wq); /* init_one 1 */
8378 h->resubmit_wq = NULL;
8379 }
8380 if (h->rescan_ctlr_wq) {
8381 destroy_workqueue(h->rescan_ctlr_wq);
8382 h->rescan_ctlr_wq = NULL;
8383 }
Robert Elliott105a3db2015-04-23 09:33:48 -05008384 kfree(h); /* init_one 1 */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008385}
8386
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008387/* Called when controller lockup detected. */
Don Bracef2405db2015-01-23 16:43:09 -06008388static void fail_all_outstanding_cmds(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008389{
Webb Scales281a7fd2015-01-23 16:43:35 -06008390 int i, refcount;
8391 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05008392 int failcount = 0;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008393
Don Brace080ef1c2015-01-23 16:43:25 -06008394 flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
Don Bracef2405db2015-01-23 16:43:09 -06008395 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06008396 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06008397 refcount = atomic_inc_return(&c->refcount);
8398 if (refcount > 1) {
Webb Scales25163bd2015-04-23 09:32:00 -05008399 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
Webb Scales281a7fd2015-01-23 16:43:35 -06008400 finish_cmd(c);
Stephen Cameron433b5f42015-04-23 09:32:11 -05008401 atomic_dec(&h->commands_outstanding);
Webb Scales25163bd2015-04-23 09:32:00 -05008402 failcount++;
Webb Scales281a7fd2015-01-23 16:43:35 -06008403 }
8404 cmd_free(h, c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008405 }
Webb Scales25163bd2015-04-23 09:32:00 -05008406 dev_warn(&h->pdev->dev,
8407 "failed %d commands in fail_all\n", failcount);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008408}
8409
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008410static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
8411{
Rusty Russellc8ed0012015-03-05 10:49:19 +10308412 int cpu;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008413
Rusty Russellc8ed0012015-03-05 10:49:19 +10308414 for_each_online_cpu(cpu) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008415 u32 *lockup_detected;
8416 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
8417 *lockup_detected = value;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008418 }
8419 wmb(); /* be sure the per-cpu variables are out to memory */
8420}
8421
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008422static void controller_lockup_detected(struct ctlr_info *h)
8423{
8424 unsigned long flags;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008425 u32 lockup_detected;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008426
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008427 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8428 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008429 lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
8430 if (!lockup_detected) {
8431 /* no heartbeat, but controller gave us a zero. */
8432 dev_warn(&h->pdev->dev,
Webb Scales25163bd2015-04-23 09:32:00 -05008433 "lockup detected after %d but scratchpad register is zero\n",
8434 h->heartbeat_sample_interval / HZ);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008435 lockup_detected = 0xffffffff;
8436 }
8437 set_lockup_detected_for_all_cpus(h, lockup_detected);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008438 spin_unlock_irqrestore(&h->lock, flags);
Webb Scales25163bd2015-04-23 09:32:00 -05008439 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
8440 lockup_detected, h->heartbeat_sample_interval / HZ);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008441 pci_disable_device(h->pdev);
Don Bracef2405db2015-01-23 16:43:09 -06008442 fail_all_outstanding_cmds(h);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008443}
8444
Webb Scales25163bd2015-04-23 09:32:00 -05008445static int detect_controller_lockup(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008446{
8447 u64 now;
8448 u32 heartbeat;
8449 unsigned long flags;
8450
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008451 now = get_jiffies_64();
8452 /* If we've received an interrupt recently, we're ok. */
8453 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05008454 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05008455 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008456
8457 /*
8458 * If we've already checked the heartbeat recently, we're ok.
8459 * This could happen if someone sends us a signal. We
8460 * otherwise don't care about signals in this thread.
8461 */
8462 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05008463 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05008464 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008465
8466 /* If heartbeat has not changed since we last looked, we're not ok. */
8467 spin_lock_irqsave(&h->lock, flags);
8468 heartbeat = readl(&h->cfgtable->HeartBeat);
8469 spin_unlock_irqrestore(&h->lock, flags);
8470 if (h->last_heartbeat == heartbeat) {
8471 controller_lockup_detected(h);
Webb Scales25163bd2015-04-23 09:32:00 -05008472 return true;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008473 }
8474
8475 /* We're ok. */
8476 h->last_heartbeat = heartbeat;
8477 h->last_heartbeat_timestamp = now;
Webb Scales25163bd2015-04-23 09:32:00 -05008478 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008479}
8480
Stephen M. Cameron98465902014-02-21 16:25:00 -06008481static void hpsa_ack_ctlr_events(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008482{
8483 int i;
8484 char *event_type;
8485
Stephen Camerone4aa3e62015-01-23 16:44:07 -06008486 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
8487 return;
8488
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008489 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06008490 if ((h->transMethod & (CFGTBL_Trans_io_accel1
8491 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008492 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
8493 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
8494
8495 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
8496 event_type = "state change";
8497 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
8498 event_type = "configuration change";
8499 /* Stop sending new RAID offload reqs via the IO accelerator */
8500 scsi_block_requests(h->scsi_host);
Don Brace5323ed72016-04-27 17:13:59 -05008501 for (i = 0; i < h->ndevices; i++) {
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008502 h->dev[i]->offload_enabled = 0;
Don Brace5323ed72016-04-27 17:13:59 -05008503 h->dev[i]->offload_to_be_enabled = 0;
8504 }
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06008505 hpsa_drain_accel_commands(h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008506 /* Set 'accelerator path config change' bit */
8507 dev_warn(&h->pdev->dev,
8508 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
8509 h->events, event_type);
8510 writel(h->events, &(h->cfgtable->clear_event_notify));
8511 /* Set the "clear event notify field update" bit 6 */
8512 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8513 /* Wait until ctlr clears 'clear event notify field', bit 6 */
8514 hpsa_wait_for_clear_event_notify_ack(h);
8515 scsi_unblock_requests(h->scsi_host);
8516 } else {
8517 /* Acknowledge controller notification events. */
8518 writel(h->events, &(h->cfgtable->clear_event_notify));
8519 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8520 hpsa_wait_for_clear_event_notify_ack(h);
8521#if 0
8522 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8523 hpsa_wait_for_mode_change_ack(h);
8524#endif
8525 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008526 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008527}
8528
8529/* Check a register on the controller to see if there are configuration
8530 * changes (added/changed/removed logical drives, etc.) which mean that
Scott Teele863d682014-02-18 13:57:05 -06008531 * we should rescan the controller for devices.
8532 * Also check flag for driver-initiated rescan.
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008533 */
Stephen M. Cameron98465902014-02-21 16:25:00 -06008534static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008535{
Don Brace853633e2015-11-04 15:50:37 -06008536 if (h->drv_req_rescan) {
8537 h->drv_req_rescan = 0;
8538 return 1;
8539 }
8540
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008541 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
Stephen M. Cameron98465902014-02-21 16:25:00 -06008542 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008543
8544 h->events = readl(&(h->cfgtable->event_notify));
Stephen M. Cameron98465902014-02-21 16:25:00 -06008545 return h->events & RESCAN_REQUIRED_EVENT_BITS;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008546}
8547
Stephen M. Cameron98465902014-02-21 16:25:00 -06008548/*
8549 * Check if any of the offline devices have become ready
8550 */
8551static int hpsa_offline_devices_ready(struct ctlr_info *h)
8552{
8553 unsigned long flags;
8554 struct offline_device_entry *d;
8555 struct list_head *this, *tmp;
8556
8557 spin_lock_irqsave(&h->offline_device_lock, flags);
8558 list_for_each_safe(this, tmp, &h->offline_device_list) {
8559 d = list_entry(this, struct offline_device_entry,
8560 offline_list);
8561 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008562 if (!hpsa_volume_offline(h, d->scsi3addr)) {
8563 spin_lock_irqsave(&h->offline_device_lock, flags);
8564 list_del(&d->offline_list);
8565 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008566 return 1;
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008567 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008568 spin_lock_irqsave(&h->offline_device_lock, flags);
8569 }
8570 spin_unlock_irqrestore(&h->offline_device_lock, flags);
8571 return 0;
8572}
8573
Scott Teel34592252015-11-04 15:52:09 -06008574static int hpsa_luns_changed(struct ctlr_info *h)
8575{
8576 int rc = 1; /* assume there are changes */
8577 struct ReportLUNdata *logdev = NULL;
8578
8579 /* if we can't find out if lun data has changed,
8580 * assume that it has.
8581 */
8582
8583 if (!h->lastlogicals)
8584 goto out;
8585
8586 logdev = kzalloc(sizeof(*logdev), GFP_KERNEL);
8587 if (!logdev) {
8588 dev_warn(&h->pdev->dev,
8589 "Out of memory, can't track lun changes.\n");
8590 goto out;
8591 }
8592 if (hpsa_scsi_do_report_luns(h, 1, logdev, sizeof(*logdev), 0)) {
8593 dev_warn(&h->pdev->dev,
8594 "report luns failed, can't track lun changes.\n");
8595 goto out;
8596 }
8597 if (memcmp(logdev, h->lastlogicals, sizeof(*logdev))) {
8598 dev_info(&h->pdev->dev,
8599 "Lun changes detected.\n");
8600 memcpy(h->lastlogicals, logdev, sizeof(*logdev));
8601 goto out;
8602 } else
8603 rc = 0; /* no changes detected. */
8604out:
8605 kfree(logdev);
8606 return rc;
8607}
8608
Don Brace6636e7f2015-01-23 16:45:17 -06008609static void hpsa_rescan_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008610{
8611 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008612 struct ctlr_info *h = container_of(to_delayed_work(work),
Don Brace6636e7f2015-01-23 16:45:17 -06008613 struct ctlr_info, rescan_ctlr_work);
8614
8615
8616 if (h->remove_in_progress)
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008617 return;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008618
Don Bracebfd75462016-11-15 14:45:32 -06008619 /*
8620 * Do the scan after the reset
8621 */
8622 if (h->reset_in_progress) {
8623 h->drv_req_rescan = 1;
8624 return;
8625 }
8626
Stephen M. Cameron98465902014-02-21 16:25:00 -06008627 if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
8628 scsi_host_get(h->scsi_host);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008629 hpsa_ack_ctlr_events(h);
8630 hpsa_scan_start(h->scsi_host);
8631 scsi_host_put(h->scsi_host);
Scott Teel34592252015-11-04 15:52:09 -06008632 } else if (h->discovery_polling) {
Scott Teelc2adae42015-11-04 15:52:16 -06008633 hpsa_disable_rld_caching(h);
Scott Teel34592252015-11-04 15:52:09 -06008634 if (hpsa_luns_changed(h)) {
8635 struct Scsi_Host *sh = NULL;
8636
8637 dev_info(&h->pdev->dev,
8638 "driver discovery polling rescan.\n");
8639 sh = scsi_host_get(h->scsi_host);
8640 if (sh != NULL) {
8641 hpsa_scan_start(sh);
8642 scsi_host_put(sh);
8643 }
8644 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008645 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008646 spin_lock_irqsave(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06008647 if (!h->remove_in_progress)
8648 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008649 h->heartbeat_sample_interval);
8650 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008651}
8652
Don Brace6636e7f2015-01-23 16:45:17 -06008653static void hpsa_monitor_ctlr_worker(struct work_struct *work)
8654{
8655 unsigned long flags;
8656 struct ctlr_info *h = container_of(to_delayed_work(work),
8657 struct ctlr_info, monitor_ctlr_work);
8658
8659 detect_controller_lockup(h);
8660 if (lockup_detected(h))
8661 return;
8662
8663 spin_lock_irqsave(&h->lock, flags);
8664 if (!h->remove_in_progress)
8665 schedule_delayed_work(&h->monitor_ctlr_work,
8666 h->heartbeat_sample_interval);
8667 spin_unlock_irqrestore(&h->lock, flags);
8668}
8669
8670static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
8671 char *name)
8672{
8673 struct workqueue_struct *wq = NULL;
Don Brace6636e7f2015-01-23 16:45:17 -06008674
Don Brace397ea9c2015-02-06 17:44:15 -06008675 wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
Don Brace6636e7f2015-01-23 16:45:17 -06008676 if (!wq)
8677 dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
8678
8679 return wq;
8680}
8681
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008682static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008683{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008684 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008685 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008686 int try_soft_reset = 0;
8687 unsigned long flags;
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008688 u32 board_id;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008689
8690 if (number_of_controllers == 0)
8691 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008692
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008693 rc = hpsa_lookup_board_id(pdev, &board_id);
8694 if (rc < 0) {
8695 dev_warn(&pdev->dev, "Board ID not found\n");
8696 return rc;
8697 }
8698
8699 rc = hpsa_init_reset_devices(pdev, board_id);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008700 if (rc) {
8701 if (rc != -ENOTSUPP)
8702 return rc;
8703 /* If the reset fails in a particular way (it has no way to do
8704 * a proper hard reset, so returns -ENOTSUPP) we can try to do
8705 * a soft reset once we get the controller configured up to the
8706 * point that it can accept a command.
8707 */
8708 try_soft_reset = 1;
8709 rc = 0;
8710 }
8711
8712reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008713
Don Brace303932f2010-02-04 08:42:40 -06008714 /* Command structures must be aligned on a 32-byte boundary because
8715 * the 5 lower bits of the address are used by the hardware. and by
8716 * the driver. See comments in hpsa.h for more info.
8717 */
Don Brace303932f2010-02-04 08:42:40 -06008718 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008719 h = kzalloc(sizeof(*h), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05008720 if (!h) {
8721 dev_err(&pdev->dev, "Failed to allocate controller head\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008722 return -ENOMEM;
Robert Elliott105a3db2015-04-23 09:33:48 -05008723 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008724
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008725 h->pdev = pdev;
Robert Elliott105a3db2015-04-23 09:33:48 -05008726
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06008727 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008728 INIT_LIST_HEAD(&h->offline_device_list);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008729 spin_lock_init(&h->lock);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008730 spin_lock_init(&h->offline_device_lock);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008731 spin_lock_init(&h->scan_lock);
Don Brace34f0c622015-01-23 16:43:46 -06008732 atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05008733 atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008734
8735 /* Allocate and clear per-cpu variable lockup_detected */
8736 h->lockup_detected = alloc_percpu(u32);
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008737 if (!h->lockup_detected) {
Robert Elliott105a3db2015-04-23 09:33:48 -05008738 dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008739 rc = -ENOMEM;
Robert Elliott2efa5922015-04-23 09:34:53 -05008740 goto clean1; /* aer/h */
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008741 }
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008742 set_lockup_detected_for_all_cpus(h, 0);
8743
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008744 rc = hpsa_pci_init(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05008745 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008746 goto clean2; /* lu, aer/h */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008747
Robert Elliott2946e822015-04-23 09:35:09 -05008748 /* relies on h-> settings made by hpsa_pci_init, including
8749 * interrupt_mode h->intr */
8750 rc = hpsa_scsi_host_alloc(h);
8751 if (rc)
8752 goto clean2_5; /* pci, lu, aer/h */
8753
8754 sprintf(h->devname, HPSA "%d", h->scsi_host->host_no);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008755 h->ctlr = number_of_controllers;
8756 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008757
8758 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008759 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
8760 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008761 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008762 } else {
8763 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8764 if (rc == 0) {
8765 dac = 0;
8766 } else {
8767 dev_err(&pdev->dev, "no suitable DMA available\n");
Robert Elliott2946e822015-04-23 09:35:09 -05008768 goto clean3; /* shost, pci, lu, aer/h */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008769 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008770 }
8771
8772 /* make sure the board interrupts are off */
8773 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05008774
Robert Elliott105a3db2015-04-23 09:33:48 -05008775 rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
8776 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008777 goto clean3; /* shost, pci, lu, aer/h */
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008778 rc = hpsa_alloc_cmd_pool(h);
Robert Elliott8947fd12015-01-23 16:42:54 -06008779 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008780 goto clean4; /* irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008781 rc = hpsa_alloc_sg_chain_blocks(h);
8782 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008783 goto clean5; /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Camerona08a8472010-02-04 08:43:16 -06008784 init_waitqueue_head(&h->scan_wait_queue);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05008785 init_waitqueue_head(&h->abort_cmd_wait_queue);
Webb Scalesd604f532015-04-23 09:35:22 -05008786 init_waitqueue_head(&h->event_sync_wait_queue);
8787 mutex_init(&h->reset_mutex);
Stephen M. Camerona08a8472010-02-04 08:43:16 -06008788 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008789
8790 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008791 h->ndevices = 0;
Robert Elliott2946e822015-04-23 09:35:09 -05008792
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008793 spin_lock_init(&h->devlock);
Robert Elliott105a3db2015-04-23 09:33:48 -05008794 rc = hpsa_put_ctlr_into_performant_mode(h);
8795 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008796 goto clean6; /* sg, cmd, irq, shost, pci, lu, aer/h */
8797
Robert Elliott2efa5922015-04-23 09:34:53 -05008798 /* create the resubmit workqueue */
8799 h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
8800 if (!h->rescan_ctlr_wq) {
8801 rc = -ENOMEM;
8802 goto clean7;
8803 }
8804
8805 h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
8806 if (!h->resubmit_wq) {
8807 rc = -ENOMEM;
8808 goto clean7; /* aer/h */
8809 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008810
Robert Elliott105a3db2015-04-23 09:33:48 -05008811 /*
8812 * At this point, the controller is ready to take commands.
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008813 * Now, if reset_devices and the hard reset didn't work, try
8814 * the soft reset and see if that works.
8815 */
8816 if (try_soft_reset) {
8817
8818 /* This is kind of gross. We may or may not get a completion
8819 * from the soft reset command, and if we do, then the value
8820 * from the fifo may or may not be valid. So, we wait 10 secs
8821 * after the reset throwing away any completions we get during
8822 * that time. Unregister the interrupt handler and register
8823 * fake ones to scoop up any residual completions.
8824 */
8825 spin_lock_irqsave(&h->lock, flags);
8826 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8827 spin_unlock_irqrestore(&h->lock, flags);
Robert Elliottec501a12015-01-23 16:41:40 -06008828 hpsa_free_irqs(h);
Robert Elliott9ee61792015-01-23 16:42:32 -06008829 rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008830 hpsa_intx_discard_completions);
8831 if (rc) {
Robert Elliott9ee61792015-01-23 16:42:32 -06008832 dev_warn(&h->pdev->dev,
8833 "Failed to request_irq after soft reset.\n");
Robert Elliottd4987572015-04-23 09:34:37 -05008834 /*
Robert Elliottb2ef4802015-04-23 09:34:48 -05008835 * cannot goto clean7 or free_irqs will be called
8836 * again. Instead, do its work
8837 */
8838 hpsa_free_performant_mode(h); /* clean7 */
8839 hpsa_free_sg_chain_blocks(h); /* clean6 */
8840 hpsa_free_cmd_pool(h); /* clean5 */
8841 /*
8842 * skip hpsa_free_irqs(h) clean4 since that
8843 * was just called before request_irqs failed
Robert Elliottd4987572015-04-23 09:34:37 -05008844 */
8845 goto clean3;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008846 }
8847
8848 rc = hpsa_kdump_soft_reset(h);
8849 if (rc)
8850 /* Neither hard nor soft reset worked, we're hosed. */
Don Brace7ef73232015-07-18 11:12:33 -05008851 goto clean7;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008852
8853 dev_info(&h->pdev->dev, "Board READY.\n");
8854 dev_info(&h->pdev->dev,
8855 "Waiting for stale completions to drain.\n");
8856 h->access.set_intr_mask(h, HPSA_INTR_ON);
8857 msleep(10000);
8858 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8859
8860 rc = controller_reset_failed(h->cfgtable);
8861 if (rc)
8862 dev_info(&h->pdev->dev,
8863 "Soft reset appears to have failed.\n");
8864
8865 /* since the controller's reset, we have to go back and re-init
8866 * everything. Easiest to just forget what we've done and do it
8867 * all over again.
8868 */
8869 hpsa_undo_allocations_after_kdump_soft_reset(h);
8870 try_soft_reset = 0;
8871 if (rc)
Robert Elliottb2ef4802015-04-23 09:34:48 -05008872 /* don't goto clean, we already unallocated */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008873 return -ENODEV;
8874
8875 goto reinit_after_soft_reset;
8876 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008877
Robert Elliott105a3db2015-04-23 09:33:48 -05008878 /* Enable Accelerated IO path at driver layer */
8879 h->acciopath_status = 1;
Scott Teel34592252015-11-04 15:52:09 -06008880 /* Disable discovery polling.*/
8881 h->discovery_polling = 0;
Scott Teelda0697b2014-02-18 13:57:00 -06008882
Scott Teele863d682014-02-18 13:57:05 -06008883
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008884 /* Turn the interrupts on so we can service requests */
8885 h->access.set_intr_mask(h, HPSA_INTR_ON);
8886
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06008887 hpsa_hba_inquiry(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008888
Scott Teel34592252015-11-04 15:52:09 -06008889 h->lastlogicals = kzalloc(sizeof(*(h->lastlogicals)), GFP_KERNEL);
8890 if (!h->lastlogicals)
8891 dev_info(&h->pdev->dev,
8892 "Can't track change to report lun data\n");
8893
Don Bracecf477232016-04-27 17:13:26 -05008894 /* hook into SCSI subsystem */
8895 rc = hpsa_scsi_add_host(h);
8896 if (rc)
8897 goto clean7; /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
8898
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008899 /* Monitor the controller for firmware lockups */
8900 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
8901 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
8902 schedule_delayed_work(&h->monitor_ctlr_work,
8903 h->heartbeat_sample_interval);
Don Brace6636e7f2015-01-23 16:45:17 -06008904 INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
8905 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
8906 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05008907 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008908
Robert Elliott2946e822015-04-23 09:35:09 -05008909clean7: /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008910 hpsa_free_performant_mode(h);
8911 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8912clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06008913 hpsa_free_sg_chain_blocks(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008914clean5: /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008915 hpsa_free_cmd_pool(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008916clean4: /* irq, shost, pci, lu, aer/h */
Robert Elliottec501a12015-01-23 16:41:40 -06008917 hpsa_free_irqs(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008918clean3: /* shost, pci, lu, aer/h */
8919 scsi_host_put(h->scsi_host);
8920 h->scsi_host = NULL;
8921clean2_5: /* pci, lu, aer/h */
Robert Elliott195f2c62015-04-23 09:33:17 -05008922 hpsa_free_pci_init(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008923clean2: /* lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008924 if (h->lockup_detected) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008925 free_percpu(h->lockup_detected);
Robert Elliott105a3db2015-04-23 09:33:48 -05008926 h->lockup_detected = NULL;
8927 }
8928clean1: /* wq/aer/h */
8929 if (h->resubmit_wq) {
8930 destroy_workqueue(h->resubmit_wq);
8931 h->resubmit_wq = NULL;
8932 }
8933 if (h->rescan_ctlr_wq) {
8934 destroy_workqueue(h->rescan_ctlr_wq);
8935 h->rescan_ctlr_wq = NULL;
8936 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008937 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008938 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008939}
8940
8941static void hpsa_flush_cache(struct ctlr_info *h)
8942{
8943 char *flush_buf;
8944 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05008945 int rc;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008946
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008947 if (unlikely(lockup_detected(h)))
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008948 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008949 flush_buf = kzalloc(4, GFP_KERNEL);
8950 if (!flush_buf)
8951 return;
8952
Stephen Cameron45fcb862015-01-23 16:43:04 -06008953 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05008954
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008955 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
8956 RAID_CTLR_LUNID, TYPE_CMD)) {
8957 goto out;
8958 }
Webb Scales25163bd2015-04-23 09:32:00 -05008959 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008960 PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05008961 if (rc)
8962 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008963 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008964out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008965 dev_warn(&h->pdev->dev,
8966 "error flushing cache on controller\n");
Stephen Cameron45fcb862015-01-23 16:43:04 -06008967 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008968 kfree(flush_buf);
8969}
8970
Scott Teelc2adae42015-11-04 15:52:16 -06008971/* Make controller gather fresh report lun data each time we
8972 * send down a report luns request
8973 */
8974static void hpsa_disable_rld_caching(struct ctlr_info *h)
8975{
8976 u32 *options;
8977 struct CommandList *c;
8978 int rc;
8979
8980 /* Don't bother trying to set diag options if locked up */
8981 if (unlikely(h->lockup_detected))
8982 return;
8983
8984 options = kzalloc(sizeof(*options), GFP_KERNEL);
8985 if (!options) {
8986 dev_err(&h->pdev->dev,
8987 "Error: failed to disable rld caching, during alloc.\n");
8988 return;
8989 }
8990
8991 c = cmd_alloc(h);
8992
8993 /* first, get the current diag options settings */
8994 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
8995 RAID_CTLR_LUNID, TYPE_CMD))
8996 goto errout;
8997
8998 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008999 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06009000 if ((rc != 0) || (c->err_info->CommandStatus != 0))
9001 goto errout;
9002
9003 /* Now, set the bit for disabling the RLD caching */
9004 *options |= HPSA_DIAG_OPTS_DISABLE_RLD_CACHING;
9005
9006 if (fill_cmd(c, BMIC_SET_DIAG_OPTIONS, h, options, 4, 0,
9007 RAID_CTLR_LUNID, TYPE_CMD))
9008 goto errout;
9009
9010 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05009011 PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06009012 if ((rc != 0) || (c->err_info->CommandStatus != 0))
9013 goto errout;
9014
9015 /* Now verify that it got set: */
9016 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
9017 RAID_CTLR_LUNID, TYPE_CMD))
9018 goto errout;
9019
9020 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05009021 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06009022 if ((rc != 0) || (c->err_info->CommandStatus != 0))
9023 goto errout;
9024
Dan Carpenterd8a080c2015-11-12 12:43:38 +03009025 if (*options & HPSA_DIAG_OPTS_DISABLE_RLD_CACHING)
Scott Teelc2adae42015-11-04 15:52:16 -06009026 goto out;
9027
9028errout:
9029 dev_err(&h->pdev->dev,
9030 "Error: failed to disable report lun data caching.\n");
9031out:
9032 cmd_free(h, c);
9033 kfree(options);
9034}
9035
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009036static void hpsa_shutdown(struct pci_dev *pdev)
9037{
9038 struct ctlr_info *h;
9039
9040 h = pci_get_drvdata(pdev);
9041 /* Turn board interrupts off and send the flush cache command
9042 * sendcmd will turn off interrupt, and send the flush...
9043 * To write all data in the battery backed cache to disks
9044 */
9045 hpsa_flush_cache(h);
9046 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Robert Elliott105a3db2015-04-23 09:33:48 -05009047 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliottcc64c812015-04-23 09:33:12 -05009048 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009049}
9050
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009051static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06009052{
9053 int i;
9054
Robert Elliott105a3db2015-04-23 09:33:48 -05009055 for (i = 0; i < h->ndevices; i++) {
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06009056 kfree(h->dev[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05009057 h->dev[i] = NULL;
9058 }
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06009059}
9060
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009061static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009062{
9063 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06009064 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009065
9066 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05009067 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009068 return;
9069 }
9070 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06009071
9072 /* Get rid of any controller monitoring work items */
9073 spin_lock_irqsave(&h->lock, flags);
9074 h->remove_in_progress = 1;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06009075 spin_unlock_irqrestore(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06009076 cancel_delayed_work_sync(&h->monitor_ctlr_work);
9077 cancel_delayed_work_sync(&h->rescan_ctlr_work);
9078 destroy_workqueue(h->rescan_ctlr_wq);
9079 destroy_workqueue(h->resubmit_wq);
Robert Elliottcc64c812015-04-23 09:33:12 -05009080
Don Brace2d041302015-07-18 11:13:15 -05009081 /*
9082 * Call before disabling interrupts.
9083 * scsi_remove_host can trigger I/O operations especially
9084 * when multipath is enabled. There can be SYNCHRONIZE CACHE
9085 * operations which cannot complete and will hang the system.
9086 */
9087 if (h->scsi_host)
9088 scsi_remove_host(h->scsi_host); /* init_one 8 */
Robert Elliott105a3db2015-04-23 09:33:48 -05009089 /* includes hpsa_free_irqs - init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05009090 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009091 hpsa_shutdown(pdev);
Robert Elliottcc64c812015-04-23 09:33:12 -05009092
Robert Elliott105a3db2015-04-23 09:33:48 -05009093 hpsa_free_device_info(h); /* scan */
9094
Robert Elliott2946e822015-04-23 09:35:09 -05009095 kfree(h->hba_inquiry_data); /* init_one 10 */
9096 h->hba_inquiry_data = NULL; /* init_one 10 */
Robert Elliott2946e822015-04-23 09:35:09 -05009097 hpsa_free_ioaccel2_sg_chain_blocks(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05009098 hpsa_free_performant_mode(h); /* init_one 7 */
9099 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
9100 hpsa_free_cmd_pool(h); /* init_one 5 */
Scott Teel34592252015-11-04 15:52:09 -06009101 kfree(h->lastlogicals);
Robert Elliott105a3db2015-04-23 09:33:48 -05009102
9103 /* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05009104
Robert Elliott2946e822015-04-23 09:35:09 -05009105 scsi_host_put(h->scsi_host); /* init_one 3 */
9106 h->scsi_host = NULL; /* init_one 3 */
9107
Robert Elliott195f2c62015-04-23 09:33:17 -05009108 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Robert Elliott2946e822015-04-23 09:35:09 -05009109 hpsa_free_pci_init(h); /* init_one 2.5 */
Robert Elliott195f2c62015-04-23 09:33:17 -05009110
Robert Elliott105a3db2015-04-23 09:33:48 -05009111 free_percpu(h->lockup_detected); /* init_one 2 */
9112 h->lockup_detected = NULL; /* init_one 2 */
9113 /* (void) pci_disable_pcie_error_reporting(pdev); */ /* init_one 1 */
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009114
9115 hpsa_delete_sas_host(h);
9116
Robert Elliott105a3db2015-04-23 09:33:48 -05009117 kfree(h); /* init_one 1 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009118}
9119
9120static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
9121 __attribute__((unused)) pm_message_t state)
9122{
9123 return -ENOSYS;
9124}
9125
9126static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
9127{
9128 return -ENOSYS;
9129}
9130
9131static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06009132 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009133 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009134 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009135 .id_table = hpsa_pci_device_id, /* id_table */
9136 .shutdown = hpsa_shutdown,
9137 .suspend = hpsa_suspend,
9138 .resume = hpsa_resume,
9139};
9140
Don Brace303932f2010-02-04 08:42:40 -06009141/* Fill in bucket_map[], given nsgs (the max number of
9142 * scatter gather elements supported) and bucket[],
9143 * which is an array of 8 integers. The bucket[] array
9144 * contains 8 different DMA transfer sizes (in 16
9145 * byte increments) which the controller uses to fetch
9146 * commands. This function fills in bucket_map[], which
9147 * maps a given number of scatter gather elements to one of
9148 * the 8 DMA transfer sizes. The point of it is to allow the
9149 * controller to only do as much DMA as needed to fetch the
9150 * command, with the DMA transfer size encoded in the lower
9151 * bits of the command address.
9152 */
9153static void calc_bucket_map(int bucket[], int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -06009154 int nsgs, int min_blocks, u32 *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06009155{
9156 int i, j, b, size;
9157
Don Brace303932f2010-02-04 08:42:40 -06009158 /* Note, bucket_map must have nsgs+1 entries. */
9159 for (i = 0; i <= nsgs; i++) {
9160 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06009161 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06009162 b = num_buckets; /* Assume the biggest bucket */
9163 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06009164 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06009165 if (bucket[j] >= size) {
9166 b = j;
9167 break;
9168 }
9169 }
9170 /* for a command with i SG entries, use bucket b. */
9171 bucket_map[i] = b;
9172 }
9173}
9174
Robert Elliott105a3db2015-04-23 09:33:48 -05009175/*
9176 * return -ENODEV on err, 0 on success (or no action)
9177 * allocates numerous items that must be freed later
9178 */
Robert Elliottc706a792015-01-23 16:45:01 -06009179static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06009180{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009181 int i;
9182 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06009183 unsigned long transMethod = CFGTBL_Trans_Performant |
9184 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009185 CFGTBL_Trans_enable_directed_msix |
9186 (trans_support & (CFGTBL_Trans_io_accel1 |
9187 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06009188 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05009189
9190 /* This is a bit complicated. There are 8 registers on
9191 * the controller which we write to to tell it 8 different
9192 * sizes of commands which there may be. It's a way of
9193 * reducing the DMA done to fetch each command. Encoded into
9194 * each command's tag are 3 bits which communicate to the controller
9195 * which of the eight sizes that command fits within. The size of
9196 * each command depends on how many scatter gather entries there are.
9197 * Each SG entry requires 16 bytes. The eight registers are programmed
9198 * with the number of 16-byte blocks a command of that size requires.
9199 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009200 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05009201 * blocks. Note, this only extends to the SG entries contained
9202 * within the command block, and does not extend to chained blocks
9203 * of SG elements. bft[] contains the eight values we write to
9204 * the registers. They are not evenly distributed, but have more
9205 * sizes for small commands, and fewer sizes for larger commands.
9206 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009207 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009208#define MIN_IOACCEL2_BFT_ENTRY 5
9209#define HPSA_IOACCEL2_HEADER_SZ 4
9210 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
9211 13, 14, 15, 16, 17, 18, 19,
9212 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
9213 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
9214 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
9215 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
9216 16 * MIN_IOACCEL2_BFT_ENTRY);
9217 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009218 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06009219 /* 5 = 1 s/g entry or 4k
9220 * 6 = 2 s/g entry or 8k
9221 * 8 = 4 s/g entry or 16k
9222 * 10 = 6 s/g entry or 24k
9223 */
Don Brace303932f2010-02-04 08:42:40 -06009224
Stephen M. Cameronb3a52e72014-05-29 10:53:23 -05009225 /* If the controller supports either ioaccel method then
9226 * we can also use the RAID stack submit path that does not
9227 * perform the superfluous readl() after each command submission.
9228 */
9229 if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
9230 access = SA5_performant_access_no_read;
9231
Don Brace303932f2010-02-04 08:42:40 -06009232 /* Controller spec: zero out this buffer. */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009233 for (i = 0; i < h->nreply_queues; i++)
9234 memset(h->reply_queue[i].head, 0, h->reply_queue_size);
Don Brace303932f2010-02-04 08:42:40 -06009235
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009236 bft[7] = SG_ENTRIES_IN_CMD + 4;
9237 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06009238 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06009239 for (i = 0; i < 8; i++)
9240 writel(bft[i], &h->transtable->BlockFetch[i]);
9241
9242 /* size of controller ring buffer */
9243 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05009244 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06009245 writel(0, &h->transtable->RepQCtrAddrLow32);
9246 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05009247
9248 for (i = 0; i < h->nreply_queues; i++) {
9249 writel(0, &h->transtable->RepQAddr[i].upper);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009250 writel(h->reply_queue[i].busaddr,
Matt Gates254f7962012-05-01 11:43:06 -05009251 &h->transtable->RepQAddr[i].lower);
9252 }
9253
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009254 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06009255 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
9256 /*
9257 * enable outbound interrupt coalescing in accelerator mode;
9258 */
9259 if (trans_support & CFGTBL_Trans_io_accel1) {
9260 access = SA5_ioaccel_mode1_access;
9261 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
9262 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Scott Teelc3497752014-02-18 13:56:34 -06009263 } else {
9264 if (trans_support & CFGTBL_Trans_io_accel2) {
9265 access = SA5_ioaccel_mode2_access;
9266 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
9267 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
9268 }
Matt Gatese1f7de02014-02-18 13:55:17 -06009269 }
Don Brace303932f2010-02-04 08:42:40 -06009270 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06009271 if (hpsa_wait_for_mode_change_ack(h)) {
9272 dev_err(&h->pdev->dev,
9273 "performant mode problem - doorbell timeout\n");
9274 return -ENODEV;
9275 }
Don Brace303932f2010-02-04 08:42:40 -06009276 register_value = readl(&(h->cfgtable->TransportActive));
9277 if (!(register_value & CFGTBL_Trans_Performant)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06009278 dev_err(&h->pdev->dev,
9279 "performant mode problem - transport not active\n");
Robert Elliottc706a792015-01-23 16:45:01 -06009280 return -ENODEV;
Don Brace303932f2010-02-04 08:42:40 -06009281 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06009282 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06009283 h->access = access;
9284 h->transMethod = transMethod;
9285
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009286 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
9287 (trans_support & CFGTBL_Trans_io_accel2)))
Robert Elliottc706a792015-01-23 16:45:01 -06009288 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06009289
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009290 if (trans_support & CFGTBL_Trans_io_accel1) {
9291 /* Set up I/O accelerator mode */
9292 for (i = 0; i < h->nreply_queues; i++) {
9293 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
9294 h->reply_queue[i].current_entry =
9295 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
9296 }
9297 bft[7] = h->ioaccel_maxsg + 8;
9298 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
9299 h->ioaccel1_blockFetchTable);
9300
9301 /* initialize all reply queue entries to unused */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009302 for (i = 0; i < h->nreply_queues; i++)
9303 memset(h->reply_queue[i].head,
9304 (u8) IOACCEL_MODE1_REPLY_UNUSED,
9305 h->reply_queue_size);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009306
9307 /* set all the constant fields in the accelerator command
9308 * frames once at init time to save CPU cycles later.
9309 */
9310 for (i = 0; i < h->nr_cmds; i++) {
9311 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
9312
9313 cp->function = IOACCEL1_FUNCTION_SCSIIO;
9314 cp->err_info = (u32) (h->errinfo_pool_dhandle +
9315 (i * sizeof(struct ErrorInfo)));
9316 cp->err_info_len = sizeof(struct ErrorInfo);
9317 cp->sgl_offset = IOACCEL1_SGLOFFSET;
Don Brace2b08b3e2015-01-23 16:41:09 -06009318 cp->host_context_flags =
9319 cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009320 cp->timeout_sec = 0;
9321 cp->ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009322 cp->tag =
Don Bracef2405db2015-01-23 16:43:09 -06009323 cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009324 cp->host_addr =
9325 cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009326 (i * sizeof(struct io_accel1_cmd)));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009327 }
9328 } else if (trans_support & CFGTBL_Trans_io_accel2) {
9329 u64 cfg_offset, cfg_base_addr_index;
9330 u32 bft2_offset, cfg_base_addr;
9331 int rc;
9332
9333 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
9334 &cfg_base_addr_index, &cfg_offset);
9335 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
9336 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
9337 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
9338 4, h->ioaccel2_blockFetchTable);
9339 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
9340 BUILD_BUG_ON(offsetof(struct CfgTable,
9341 io_accel_request_size_offset) != 0xb8);
9342 h->ioaccel2_bft2_regs =
9343 remap_pci_mem(pci_resource_start(h->pdev,
9344 cfg_base_addr_index) +
9345 cfg_offset + bft2_offset,
9346 ARRAY_SIZE(bft2) *
9347 sizeof(*h->ioaccel2_bft2_regs));
9348 for (i = 0; i < ARRAY_SIZE(bft2); i++)
9349 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06009350 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009351 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06009352 if (hpsa_wait_for_mode_change_ack(h)) {
9353 dev_err(&h->pdev->dev,
9354 "performant mode problem - enabling ioaccel mode\n");
9355 return -ENODEV;
9356 }
9357 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06009358}
9359
Robert Elliott1fb7c982015-04-23 09:33:22 -05009360/* Free ioaccel1 mode command blocks and block fetch table */
9361static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
9362{
Robert Elliott105a3db2015-04-23 09:33:48 -05009363 if (h->ioaccel_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05009364 pci_free_consistent(h->pdev,
9365 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
9366 h->ioaccel_cmd_pool,
9367 h->ioaccel_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05009368 h->ioaccel_cmd_pool = NULL;
9369 h->ioaccel_cmd_pool_dhandle = 0;
9370 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05009371 kfree(h->ioaccel1_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009372 h->ioaccel1_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05009373}
9374
Robert Elliottd37ffbe2015-04-23 09:32:27 -05009375/* Allocate ioaccel1 mode command blocks and block fetch table */
9376static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
Matt Gatese1f7de02014-02-18 13:55:17 -06009377{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06009378 h->ioaccel_maxsg =
9379 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
9380 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
9381 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
9382
Matt Gatese1f7de02014-02-18 13:55:17 -06009383 /* Command structures must be aligned on a 128-byte boundary
9384 * because the 7 lower bits of the address are used by the
9385 * hardware.
9386 */
Matt Gatese1f7de02014-02-18 13:55:17 -06009387 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
9388 IOACCEL1_COMMANDLIST_ALIGNMENT);
9389 h->ioaccel_cmd_pool =
9390 pci_alloc_consistent(h->pdev,
9391 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
9392 &(h->ioaccel_cmd_pool_dhandle));
9393
9394 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06009395 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06009396 sizeof(u32)), GFP_KERNEL);
9397
9398 if ((h->ioaccel_cmd_pool == NULL) ||
9399 (h->ioaccel1_blockFetchTable == NULL))
9400 goto clean_up;
9401
9402 memset(h->ioaccel_cmd_pool, 0,
9403 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
9404 return 0;
9405
9406clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05009407 hpsa_free_ioaccel1_cmd_and_bft(h);
Robert Elliott2dd02d72015-04-23 09:33:43 -05009408 return -ENOMEM;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009409}
9410
Robert Elliott1fb7c982015-04-23 09:33:22 -05009411/* Free ioaccel2 mode command blocks and block fetch table */
9412static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
9413{
Webb Scalesd9a729f2015-04-23 09:33:27 -05009414 hpsa_free_ioaccel2_sg_chain_blocks(h);
9415
Robert Elliott105a3db2015-04-23 09:33:48 -05009416 if (h->ioaccel2_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05009417 pci_free_consistent(h->pdev,
9418 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
9419 h->ioaccel2_cmd_pool,
9420 h->ioaccel2_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05009421 h->ioaccel2_cmd_pool = NULL;
9422 h->ioaccel2_cmd_pool_dhandle = 0;
9423 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05009424 kfree(h->ioaccel2_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009425 h->ioaccel2_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05009426}
9427
Robert Elliottd37ffbe2015-04-23 09:32:27 -05009428/* Allocate ioaccel2 mode command blocks and block fetch table */
9429static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009430{
Webb Scalesd9a729f2015-04-23 09:33:27 -05009431 int rc;
9432
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009433 /* Allocate ioaccel2 mode command blocks and block fetch table */
9434
9435 h->ioaccel_maxsg =
9436 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
9437 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
9438 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
9439
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009440 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
9441 IOACCEL2_COMMANDLIST_ALIGNMENT);
9442 h->ioaccel2_cmd_pool =
9443 pci_alloc_consistent(h->pdev,
9444 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
9445 &(h->ioaccel2_cmd_pool_dhandle));
9446
9447 h->ioaccel2_blockFetchTable =
9448 kmalloc(((h->ioaccel_maxsg + 1) *
9449 sizeof(u32)), GFP_KERNEL);
9450
9451 if ((h->ioaccel2_cmd_pool == NULL) ||
Webb Scalesd9a729f2015-04-23 09:33:27 -05009452 (h->ioaccel2_blockFetchTable == NULL)) {
9453 rc = -ENOMEM;
9454 goto clean_up;
9455 }
9456
9457 rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
9458 if (rc)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009459 goto clean_up;
9460
9461 memset(h->ioaccel2_cmd_pool, 0,
9462 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
9463 return 0;
9464
9465clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05009466 hpsa_free_ioaccel2_cmd_and_bft(h);
Webb Scalesd9a729f2015-04-23 09:33:27 -05009467 return rc;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009468}
9469
Robert Elliott105a3db2015-04-23 09:33:48 -05009470/* Free items allocated by hpsa_put_ctlr_into_performant_mode */
9471static void hpsa_free_performant_mode(struct ctlr_info *h)
9472{
9473 kfree(h->blockFetchTable);
9474 h->blockFetchTable = NULL;
9475 hpsa_free_reply_queues(h);
9476 hpsa_free_ioaccel1_cmd_and_bft(h);
9477 hpsa_free_ioaccel2_cmd_and_bft(h);
9478}
9479
9480/* return -ENODEV on error, 0 on success (or no action)
9481 * allocates numerous items that must be freed later
9482 */
9483static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009484{
9485 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06009486 unsigned long transMethod = CFGTBL_Trans_Performant |
9487 CFGTBL_Trans_use_short_tags;
Robert Elliott105a3db2015-04-23 09:33:48 -05009488 int i, rc;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009489
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06009490 if (hpsa_simple_mode)
Robert Elliott105a3db2015-04-23 09:33:48 -05009491 return 0;
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06009492
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05009493 trans_support = readl(&(h->cfgtable->TransportSupport));
9494 if (!(trans_support & PERFORMANT_MODE))
Robert Elliott105a3db2015-04-23 09:33:48 -05009495 return 0;
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05009496
Matt Gatese1f7de02014-02-18 13:55:17 -06009497 /* Check for I/O accelerator mode support */
9498 if (trans_support & CFGTBL_Trans_io_accel1) {
9499 transMethod |= CFGTBL_Trans_io_accel1 |
9500 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05009501 rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
9502 if (rc)
9503 return rc;
9504 } else if (trans_support & CFGTBL_Trans_io_accel2) {
9505 transMethod |= CFGTBL_Trans_io_accel2 |
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009506 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05009507 rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
9508 if (rc)
9509 return rc;
Matt Gatese1f7de02014-02-18 13:55:17 -06009510 }
9511
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08009512 h->nreply_queues = h->msix_vectors > 0 ? h->msix_vectors : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05009513 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009514 /* Performant mode ring buffer and supporting data structures */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009515 h->reply_queue_size = h->max_commands * sizeof(u64);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009516
Matt Gates254f7962012-05-01 11:43:06 -05009517 for (i = 0; i < h->nreply_queues; i++) {
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009518 h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
9519 h->reply_queue_size,
9520 &(h->reply_queue[i].busaddr));
Robert Elliott105a3db2015-04-23 09:33:48 -05009521 if (!h->reply_queue[i].head) {
9522 rc = -ENOMEM;
9523 goto clean1; /* rq, ioaccel */
9524 }
Matt Gates254f7962012-05-01 11:43:06 -05009525 h->reply_queue[i].size = h->max_commands;
9526 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
9527 h->reply_queue[i].current_entry = 0;
9528 }
9529
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009530 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009531 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009532 sizeof(u32)), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05009533 if (!h->blockFetchTable) {
9534 rc = -ENOMEM;
9535 goto clean1; /* rq, ioaccel */
9536 }
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009537
Robert Elliott105a3db2015-04-23 09:33:48 -05009538 rc = hpsa_enter_performant_mode(h, trans_support);
9539 if (rc)
9540 goto clean2; /* bft, rq, ioaccel */
9541 return 0;
Don Brace303932f2010-02-04 08:42:40 -06009542
Robert Elliott105a3db2015-04-23 09:33:48 -05009543clean2: /* bft, rq, ioaccel */
Don Brace303932f2010-02-04 08:42:40 -06009544 kfree(h->blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009545 h->blockFetchTable = NULL;
9546clean1: /* rq, ioaccel */
9547 hpsa_free_reply_queues(h);
9548 hpsa_free_ioaccel1_cmd_and_bft(h);
9549 hpsa_free_ioaccel2_cmd_and_bft(h);
9550 return rc;
Don Brace303932f2010-02-04 08:42:40 -06009551}
9552
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009553static int is_accelerated_cmd(struct CommandList *c)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009554{
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009555 return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
9556}
9557
9558static void hpsa_drain_accel_commands(struct ctlr_info *h)
9559{
9560 struct CommandList *c = NULL;
Don Bracef2405db2015-01-23 16:43:09 -06009561 int i, accel_cmds_out;
Webb Scales281a7fd2015-01-23 16:43:35 -06009562 int refcount;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009563
Don Bracef2405db2015-01-23 16:43:09 -06009564 do { /* wait for all outstanding ioaccel commands to drain out */
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009565 accel_cmds_out = 0;
Don Bracef2405db2015-01-23 16:43:09 -06009566 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06009567 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06009568 refcount = atomic_inc_return(&c->refcount);
9569 if (refcount > 1) /* Command is allocated */
9570 accel_cmds_out += is_accelerated_cmd(c);
9571 cmd_free(h, c);
Don Bracef2405db2015-01-23 16:43:09 -06009572 }
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009573 if (accel_cmds_out <= 0)
Webb Scales281a7fd2015-01-23 16:43:35 -06009574 break;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009575 msleep(100);
9576 } while (1);
9577}
9578
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009579static struct hpsa_sas_phy *hpsa_alloc_sas_phy(
9580 struct hpsa_sas_port *hpsa_sas_port)
9581{
9582 struct hpsa_sas_phy *hpsa_sas_phy;
9583 struct sas_phy *phy;
9584
9585 hpsa_sas_phy = kzalloc(sizeof(*hpsa_sas_phy), GFP_KERNEL);
9586 if (!hpsa_sas_phy)
9587 return NULL;
9588
9589 phy = sas_phy_alloc(hpsa_sas_port->parent_node->parent_dev,
9590 hpsa_sas_port->next_phy_index);
9591 if (!phy) {
9592 kfree(hpsa_sas_phy);
9593 return NULL;
9594 }
9595
9596 hpsa_sas_port->next_phy_index++;
9597 hpsa_sas_phy->phy = phy;
9598 hpsa_sas_phy->parent_port = hpsa_sas_port;
9599
9600 return hpsa_sas_phy;
9601}
9602
9603static void hpsa_free_sas_phy(struct hpsa_sas_phy *hpsa_sas_phy)
9604{
9605 struct sas_phy *phy = hpsa_sas_phy->phy;
9606
9607 sas_port_delete_phy(hpsa_sas_phy->parent_port->port, phy);
9608 sas_phy_free(phy);
9609 if (hpsa_sas_phy->added_to_port)
9610 list_del(&hpsa_sas_phy->phy_list_entry);
9611 kfree(hpsa_sas_phy);
9612}
9613
9614static int hpsa_sas_port_add_phy(struct hpsa_sas_phy *hpsa_sas_phy)
9615{
9616 int rc;
9617 struct hpsa_sas_port *hpsa_sas_port;
9618 struct sas_phy *phy;
9619 struct sas_identify *identify;
9620
9621 hpsa_sas_port = hpsa_sas_phy->parent_port;
9622 phy = hpsa_sas_phy->phy;
9623
9624 identify = &phy->identify;
9625 memset(identify, 0, sizeof(*identify));
9626 identify->sas_address = hpsa_sas_port->sas_address;
9627 identify->device_type = SAS_END_DEVICE;
9628 identify->initiator_port_protocols = SAS_PROTOCOL_STP;
9629 identify->target_port_protocols = SAS_PROTOCOL_STP;
9630 phy->minimum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
9631 phy->maximum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
9632 phy->minimum_linkrate = SAS_LINK_RATE_UNKNOWN;
9633 phy->maximum_linkrate = SAS_LINK_RATE_UNKNOWN;
9634 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
9635
9636 rc = sas_phy_add(hpsa_sas_phy->phy);
9637 if (rc)
9638 return rc;
9639
9640 sas_port_add_phy(hpsa_sas_port->port, hpsa_sas_phy->phy);
9641 list_add_tail(&hpsa_sas_phy->phy_list_entry,
9642 &hpsa_sas_port->phy_list_head);
9643 hpsa_sas_phy->added_to_port = true;
9644
9645 return 0;
9646}
9647
9648static int
9649 hpsa_sas_port_add_rphy(struct hpsa_sas_port *hpsa_sas_port,
9650 struct sas_rphy *rphy)
9651{
9652 struct sas_identify *identify;
9653
9654 identify = &rphy->identify;
9655 identify->sas_address = hpsa_sas_port->sas_address;
9656 identify->initiator_port_protocols = SAS_PROTOCOL_STP;
9657 identify->target_port_protocols = SAS_PROTOCOL_STP;
9658
9659 return sas_rphy_add(rphy);
9660}
9661
9662static struct hpsa_sas_port
9663 *hpsa_alloc_sas_port(struct hpsa_sas_node *hpsa_sas_node,
9664 u64 sas_address)
9665{
9666 int rc;
9667 struct hpsa_sas_port *hpsa_sas_port;
9668 struct sas_port *port;
9669
9670 hpsa_sas_port = kzalloc(sizeof(*hpsa_sas_port), GFP_KERNEL);
9671 if (!hpsa_sas_port)
9672 return NULL;
9673
9674 INIT_LIST_HEAD(&hpsa_sas_port->phy_list_head);
9675 hpsa_sas_port->parent_node = hpsa_sas_node;
9676
9677 port = sas_port_alloc_num(hpsa_sas_node->parent_dev);
9678 if (!port)
9679 goto free_hpsa_port;
9680
9681 rc = sas_port_add(port);
9682 if (rc)
9683 goto free_sas_port;
9684
9685 hpsa_sas_port->port = port;
9686 hpsa_sas_port->sas_address = sas_address;
9687 list_add_tail(&hpsa_sas_port->port_list_entry,
9688 &hpsa_sas_node->port_list_head);
9689
9690 return hpsa_sas_port;
9691
9692free_sas_port:
9693 sas_port_free(port);
9694free_hpsa_port:
9695 kfree(hpsa_sas_port);
9696
9697 return NULL;
9698}
9699
9700static void hpsa_free_sas_port(struct hpsa_sas_port *hpsa_sas_port)
9701{
9702 struct hpsa_sas_phy *hpsa_sas_phy;
9703 struct hpsa_sas_phy *next;
9704
9705 list_for_each_entry_safe(hpsa_sas_phy, next,
9706 &hpsa_sas_port->phy_list_head, phy_list_entry)
9707 hpsa_free_sas_phy(hpsa_sas_phy);
9708
9709 sas_port_delete(hpsa_sas_port->port);
9710 list_del(&hpsa_sas_port->port_list_entry);
9711 kfree(hpsa_sas_port);
9712}
9713
9714static struct hpsa_sas_node *hpsa_alloc_sas_node(struct device *parent_dev)
9715{
9716 struct hpsa_sas_node *hpsa_sas_node;
9717
9718 hpsa_sas_node = kzalloc(sizeof(*hpsa_sas_node), GFP_KERNEL);
9719 if (hpsa_sas_node) {
9720 hpsa_sas_node->parent_dev = parent_dev;
9721 INIT_LIST_HEAD(&hpsa_sas_node->port_list_head);
9722 }
9723
9724 return hpsa_sas_node;
9725}
9726
9727static void hpsa_free_sas_node(struct hpsa_sas_node *hpsa_sas_node)
9728{
9729 struct hpsa_sas_port *hpsa_sas_port;
9730 struct hpsa_sas_port *next;
9731
9732 if (!hpsa_sas_node)
9733 return;
9734
9735 list_for_each_entry_safe(hpsa_sas_port, next,
9736 &hpsa_sas_node->port_list_head, port_list_entry)
9737 hpsa_free_sas_port(hpsa_sas_port);
9738
9739 kfree(hpsa_sas_node);
9740}
9741
9742static struct hpsa_scsi_dev_t
9743 *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
9744 struct sas_rphy *rphy)
9745{
9746 int i;
9747 struct hpsa_scsi_dev_t *device;
9748
9749 for (i = 0; i < h->ndevices; i++) {
9750 device = h->dev[i];
9751 if (!device->sas_port)
9752 continue;
9753 if (device->sas_port->rphy == rphy)
9754 return device;
9755 }
9756
9757 return NULL;
9758}
9759
9760static int hpsa_add_sas_host(struct ctlr_info *h)
9761{
9762 int rc;
9763 struct device *parent_dev;
9764 struct hpsa_sas_node *hpsa_sas_node;
9765 struct hpsa_sas_port *hpsa_sas_port;
9766 struct hpsa_sas_phy *hpsa_sas_phy;
9767
9768 parent_dev = &h->scsi_host->shost_gendev;
9769
9770 hpsa_sas_node = hpsa_alloc_sas_node(parent_dev);
9771 if (!hpsa_sas_node)
9772 return -ENOMEM;
9773
9774 hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, h->sas_address);
9775 if (!hpsa_sas_port) {
9776 rc = -ENODEV;
9777 goto free_sas_node;
9778 }
9779
9780 hpsa_sas_phy = hpsa_alloc_sas_phy(hpsa_sas_port);
9781 if (!hpsa_sas_phy) {
9782 rc = -ENODEV;
9783 goto free_sas_port;
9784 }
9785
9786 rc = hpsa_sas_port_add_phy(hpsa_sas_phy);
9787 if (rc)
9788 goto free_sas_phy;
9789
9790 h->sas_host = hpsa_sas_node;
9791
9792 return 0;
9793
9794free_sas_phy:
9795 hpsa_free_sas_phy(hpsa_sas_phy);
9796free_sas_port:
9797 hpsa_free_sas_port(hpsa_sas_port);
9798free_sas_node:
9799 hpsa_free_sas_node(hpsa_sas_node);
9800
9801 return rc;
9802}
9803
9804static void hpsa_delete_sas_host(struct ctlr_info *h)
9805{
9806 hpsa_free_sas_node(h->sas_host);
9807}
9808
9809static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
9810 struct hpsa_scsi_dev_t *device)
9811{
9812 int rc;
9813 struct hpsa_sas_port *hpsa_sas_port;
9814 struct sas_rphy *rphy;
9815
9816 hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, device->sas_address);
9817 if (!hpsa_sas_port)
9818 return -ENOMEM;
9819
9820 rphy = sas_end_device_alloc(hpsa_sas_port->port);
9821 if (!rphy) {
9822 rc = -ENODEV;
9823 goto free_sas_port;
9824 }
9825
9826 hpsa_sas_port->rphy = rphy;
9827 device->sas_port = hpsa_sas_port;
9828
9829 rc = hpsa_sas_port_add_rphy(hpsa_sas_port, rphy);
9830 if (rc)
9831 goto free_sas_port;
9832
9833 return 0;
9834
9835free_sas_port:
9836 hpsa_free_sas_port(hpsa_sas_port);
9837 device->sas_port = NULL;
9838
9839 return rc;
9840}
9841
9842static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device)
9843{
9844 if (device->sas_port) {
9845 hpsa_free_sas_port(device->sas_port);
9846 device->sas_port = NULL;
9847 }
9848}
9849
9850static int
9851hpsa_sas_get_linkerrors(struct sas_phy *phy)
9852{
9853 return 0;
9854}
9855
9856static int
9857hpsa_sas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
9858{
Dan Carpenteraa105692016-04-14 12:37:44 +03009859 *identifier = 0;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009860 return 0;
9861}
9862
9863static int
9864hpsa_sas_get_bay_identifier(struct sas_rphy *rphy)
9865{
9866 return -ENXIO;
9867}
9868
9869static int
9870hpsa_sas_phy_reset(struct sas_phy *phy, int hard_reset)
9871{
9872 return 0;
9873}
9874
9875static int
9876hpsa_sas_phy_enable(struct sas_phy *phy, int enable)
9877{
9878 return 0;
9879}
9880
9881static int
9882hpsa_sas_phy_setup(struct sas_phy *phy)
9883{
9884 return 0;
9885}
9886
9887static void
9888hpsa_sas_phy_release(struct sas_phy *phy)
9889{
9890}
9891
9892static int
9893hpsa_sas_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
9894{
9895 return -EINVAL;
9896}
9897
9898/* SMP = Serial Management Protocol */
9899static int
9900hpsa_sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
9901struct request *req)
9902{
9903 return -EINVAL;
9904}
9905
9906static struct sas_function_template hpsa_sas_transport_functions = {
9907 .get_linkerrors = hpsa_sas_get_linkerrors,
9908 .get_enclosure_identifier = hpsa_sas_get_enclosure_identifier,
9909 .get_bay_identifier = hpsa_sas_get_bay_identifier,
9910 .phy_reset = hpsa_sas_phy_reset,
9911 .phy_enable = hpsa_sas_phy_enable,
9912 .phy_setup = hpsa_sas_phy_setup,
9913 .phy_release = hpsa_sas_phy_release,
9914 .set_phy_speed = hpsa_sas_phy_speed,
9915 .smp_handler = hpsa_sas_smp_handler,
9916};
9917
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009918/*
9919 * This is it. Register the PCI driver information for the cards we control
9920 * the OS will call our registered routines when it finds one of our cards.
9921 */
9922static int __init hpsa_init(void)
9923{
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009924 int rc;
9925
9926 hpsa_sas_transport_template =
9927 sas_attach_transport(&hpsa_sas_transport_functions);
9928 if (!hpsa_sas_transport_template)
9929 return -ENODEV;
9930
9931 rc = pci_register_driver(&hpsa_pci_driver);
9932
9933 if (rc)
9934 sas_release_transport(hpsa_sas_transport_template);
9935
9936 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009937}
9938
9939static void __exit hpsa_cleanup(void)
9940{
9941 pci_unregister_driver(&hpsa_pci_driver);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009942 sas_release_transport(hpsa_sas_transport_template);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009943}
9944
Matt Gatese1f7de02014-02-18 13:55:17 -06009945static void __attribute__((unused)) verify_offsets(void)
9946{
9947#define VERIFY_OFFSET(member, offset) \
Scott Teeldd0e19f2014-02-18 13:57:31 -06009948 BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
9949
9950 VERIFY_OFFSET(structure_size, 0);
9951 VERIFY_OFFSET(volume_blk_size, 4);
9952 VERIFY_OFFSET(volume_blk_cnt, 8);
9953 VERIFY_OFFSET(phys_blk_shift, 16);
9954 VERIFY_OFFSET(parity_rotation_shift, 17);
9955 VERIFY_OFFSET(strip_size, 18);
9956 VERIFY_OFFSET(disk_starting_blk, 20);
9957 VERIFY_OFFSET(disk_blk_cnt, 28);
9958 VERIFY_OFFSET(data_disks_per_row, 36);
9959 VERIFY_OFFSET(metadata_disks_per_row, 38);
9960 VERIFY_OFFSET(row_cnt, 40);
9961 VERIFY_OFFSET(layout_map_count, 42);
9962 VERIFY_OFFSET(flags, 44);
9963 VERIFY_OFFSET(dekindex, 46);
9964 /* VERIFY_OFFSET(reserved, 48 */
9965 VERIFY_OFFSET(data, 64);
9966
9967#undef VERIFY_OFFSET
9968
9969#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06009970 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
9971
9972 VERIFY_OFFSET(IU_type, 0);
9973 VERIFY_OFFSET(direction, 1);
9974 VERIFY_OFFSET(reply_queue, 2);
9975 /* VERIFY_OFFSET(reserved1, 3); */
9976 VERIFY_OFFSET(scsi_nexus, 4);
9977 VERIFY_OFFSET(Tag, 8);
9978 VERIFY_OFFSET(cdb, 16);
9979 VERIFY_OFFSET(cciss_lun, 32);
9980 VERIFY_OFFSET(data_len, 40);
9981 VERIFY_OFFSET(cmd_priority_task_attr, 44);
9982 VERIFY_OFFSET(sg_count, 45);
9983 /* VERIFY_OFFSET(reserved3 */
9984 VERIFY_OFFSET(err_ptr, 48);
9985 VERIFY_OFFSET(err_len, 56);
9986 /* VERIFY_OFFSET(reserved4 */
9987 VERIFY_OFFSET(sg, 64);
9988
9989#undef VERIFY_OFFSET
9990
9991#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06009992 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
9993
9994 VERIFY_OFFSET(dev_handle, 0x00);
9995 VERIFY_OFFSET(reserved1, 0x02);
9996 VERIFY_OFFSET(function, 0x03);
9997 VERIFY_OFFSET(reserved2, 0x04);
9998 VERIFY_OFFSET(err_info, 0x0C);
9999 VERIFY_OFFSET(reserved3, 0x10);
10000 VERIFY_OFFSET(err_info_len, 0x12);
10001 VERIFY_OFFSET(reserved4, 0x13);
10002 VERIFY_OFFSET(sgl_offset, 0x14);
10003 VERIFY_OFFSET(reserved5, 0x15);
10004 VERIFY_OFFSET(transfer_len, 0x1C);
10005 VERIFY_OFFSET(reserved6, 0x20);
10006 VERIFY_OFFSET(io_flags, 0x24);
10007 VERIFY_OFFSET(reserved7, 0x26);
10008 VERIFY_OFFSET(LUN, 0x34);
10009 VERIFY_OFFSET(control, 0x3C);
10010 VERIFY_OFFSET(CDB, 0x40);
10011 VERIFY_OFFSET(reserved8, 0x50);
10012 VERIFY_OFFSET(host_context_flags, 0x60);
10013 VERIFY_OFFSET(timeout_sec, 0x62);
10014 VERIFY_OFFSET(ReplyQueue, 0x64);
10015 VERIFY_OFFSET(reserved9, 0x65);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -060010016 VERIFY_OFFSET(tag, 0x68);
Matt Gatese1f7de02014-02-18 13:55:17 -060010017 VERIFY_OFFSET(host_addr, 0x70);
10018 VERIFY_OFFSET(CISS_LUN, 0x78);
10019 VERIFY_OFFSET(SG, 0x78 + 8);
10020#undef VERIFY_OFFSET
10021}
10022
Stephen M. Cameronedd16362009-12-08 14:09:11 -080010023module_init(hpsa_init);
10024module_exit(hpsa_cleanup);