blob: 97f50c71b7c9060fd5411444753e91c2d194e914 [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. Camerona08a84712010-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);
279static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
280 int wait_for_ready);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500281static inline void finish_cmd(struct CommandList *c);
Robert Elliottc706a792015-01-23 16:45:01 -0600282static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -0600283#define BOARD_NOT_READY 0
284#define BOARD_READY 1
Stephen M. Cameron23100dd2014-02-18 13:57:37 -0600285static void hpsa_drain_accel_commands(struct ctlr_info *h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -0600286static void hpsa_flush_cache(struct ctlr_info *h);
Scott Teelc3497752014-02-18 13:56:34 -0600287static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
288 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -0600289 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
Don Brace080ef1c2015-01-23 16:43:25 -0600290static void hpsa_command_resubmit_worker(struct work_struct *work);
Webb Scales25163bd2015-04-23 09:32:00 -0500291static u32 lockup_detected(struct ctlr_info *h);
292static int detect_controller_lockup(struct ctlr_info *h);
Scott Teelc2adae42015-11-04 15:52:16 -0600293static void hpsa_disable_rld_caching(struct ctlr_info *h);
Kevin Barnettd04e62b2015-11-04 15:52:34 -0600294static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
295 struct ReportExtendedLUNdata *buf, int bufsize);
Scott Teel34592252015-11-04 15:52:09 -0600296static int hpsa_luns_changed(struct ctlr_info *h);
Don Braceba74fdc2016-04-27 17:14:17 -0500297static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
298 struct hpsa_scsi_dev_t *dev,
299 unsigned char *scsi3addr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800300
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800301static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
302{
303 unsigned long *priv = shost_priv(sdev->host);
304 return (struct ctlr_info *) *priv;
305}
306
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600307static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
308{
309 unsigned long *priv = shost_priv(sh);
310 return (struct ctlr_info *) *priv;
311}
312
Webb Scalesa58e7e52015-04-23 09:34:16 -0500313static inline bool hpsa_is_cmd_idle(struct CommandList *c)
314{
315 return c->scsi_cmd == SCSI_CMD_IDLE;
316}
317
Webb Scalesd604f532015-04-23 09:35:22 -0500318static inline bool hpsa_is_pending_event(struct CommandList *c)
319{
320 return c->abort_pending || c->reset_pending;
321}
322
Stephen Cameron9437ac42015-04-23 09:32:16 -0500323/* extract sense key, asc, and ascq from sense data. -1 means invalid. */
324static void decode_sense_data(const u8 *sense_data, int sense_data_len,
325 u8 *sense_key, u8 *asc, u8 *ascq)
326{
327 struct scsi_sense_hdr sshdr;
328 bool rc;
329
330 *sense_key = -1;
331 *asc = -1;
332 *ascq = -1;
333
334 if (sense_data_len < 1)
335 return;
336
337 rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
338 if (rc) {
339 *sense_key = sshdr.sense_key;
340 *asc = sshdr.asc;
341 *ascq = sshdr.ascq;
342 }
343}
344
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800345static int check_for_unit_attention(struct ctlr_info *h,
346 struct CommandList *c)
347{
Stephen Cameron9437ac42015-04-23 09:32:16 -0500348 u8 sense_key, asc, ascq;
349 int sense_len;
350
351 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
352 sense_len = sizeof(c->err_info->SenseInfo);
353 else
354 sense_len = c->err_info->SenseLen;
355
356 decode_sense_data(c->err_info->SenseInfo, sense_len,
357 &sense_key, &asc, &ascq);
Don Brace81c27552015-07-18 11:12:28 -0500358 if (sense_key != UNIT_ATTENTION || asc == 0xff)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800359 return 0;
360
Stephen Cameron9437ac42015-04-23 09:32:16 -0500361 switch (asc) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800362 case STATE_CHANGED:
Stephen Cameron9437ac42015-04-23 09:32:16 -0500363 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500364 "%s: a state change detected, command retried\n",
365 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800366 break;
367 case LUN_FAILED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600368 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500369 "%s: LUN failure detected\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800370 break;
371 case REPORT_LUNS_CHANGED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600372 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500373 "%s: report LUN data changed\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800374 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600375 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
376 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800377 */
378 break;
379 case POWER_OR_RESET:
Robert Elliott2946e822015-04-23 09:35:09 -0500380 dev_warn(&h->pdev->dev,
381 "%s: a power on or device reset detected\n",
382 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800383 break;
384 case UNIT_ATTENTION_CLEARED:
Robert Elliott2946e822015-04-23 09:35:09 -0500385 dev_warn(&h->pdev->dev,
386 "%s: unit attention cleared by another initiator\n",
387 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800388 break;
389 default:
Robert Elliott2946e822015-04-23 09:35:09 -0500390 dev_warn(&h->pdev->dev,
391 "%s: unknown unit attention detected\n",
392 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800393 break;
394 }
395 return 1;
396}
397
Matt Bondurant852af202012-05-01 11:42:35 -0500398static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
399{
400 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
401 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
402 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
403 return 0;
404 dev_warn(&h->pdev->dev, HPSA "device busy");
405 return 1;
406}
407
Stephen Camerone985c582015-04-23 09:32:22 -0500408static u32 lockup_detected(struct ctlr_info *h);
409static ssize_t host_show_lockup_detected(struct device *dev,
410 struct device_attribute *attr, char *buf)
411{
412 int ld;
413 struct ctlr_info *h;
414 struct Scsi_Host *shost = class_to_shost(dev);
415
416 h = shost_to_hba(shost);
417 ld = lockup_detected(h);
418
419 return sprintf(buf, "ld=%d\n", ld);
420}
421
Scott Teelda0697b2014-02-18 13:57:00 -0600422static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
423 struct device_attribute *attr,
424 const char *buf, size_t count)
425{
426 int status, len;
427 struct ctlr_info *h;
428 struct Scsi_Host *shost = class_to_shost(dev);
429 char tmpbuf[10];
430
431 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
432 return -EACCES;
433 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
434 strncpy(tmpbuf, buf, len);
435 tmpbuf[len] = '\0';
436 if (sscanf(tmpbuf, "%d", &status) != 1)
437 return -EINVAL;
438 h = shost_to_hba(shost);
439 h->acciopath_status = !!status;
440 dev_warn(&h->pdev->dev,
441 "hpsa: HP SSD Smart Path %s via sysfs update.\n",
442 h->acciopath_status ? "enabled" : "disabled");
443 return count;
444}
445
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600446static ssize_t host_store_raid_offload_debug(struct device *dev,
447 struct device_attribute *attr,
448 const char *buf, size_t count)
449{
450 int debug_level, len;
451 struct ctlr_info *h;
452 struct Scsi_Host *shost = class_to_shost(dev);
453 char tmpbuf[10];
454
455 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
456 return -EACCES;
457 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
458 strncpy(tmpbuf, buf, len);
459 tmpbuf[len] = '\0';
460 if (sscanf(tmpbuf, "%d", &debug_level) != 1)
461 return -EINVAL;
462 if (debug_level < 0)
463 debug_level = 0;
464 h = shost_to_hba(shost);
465 h->raid_offload_debug = debug_level;
466 dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
467 h->raid_offload_debug);
468 return count;
469}
470
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800471static ssize_t host_store_rescan(struct device *dev,
472 struct device_attribute *attr,
473 const char *buf, size_t count)
474{
475 struct ctlr_info *h;
476 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600477 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600478 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800479 return count;
480}
481
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500482static ssize_t host_show_firmware_revision(struct device *dev,
483 struct device_attribute *attr, char *buf)
484{
485 struct ctlr_info *h;
486 struct Scsi_Host *shost = class_to_shost(dev);
487 unsigned char *fwrev;
488
489 h = shost_to_hba(shost);
490 if (!h->hba_inquiry_data)
491 return 0;
492 fwrev = &h->hba_inquiry_data[32];
493 return snprintf(buf, 20, "%c%c%c%c\n",
494 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
495}
496
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600497static ssize_t host_show_commands_outstanding(struct device *dev,
498 struct device_attribute *attr, char *buf)
499{
500 struct Scsi_Host *shost = class_to_shost(dev);
501 struct ctlr_info *h = shost_to_hba(shost);
502
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600503 return snprintf(buf, 20, "%d\n",
504 atomic_read(&h->commands_outstanding));
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600505}
506
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600507static ssize_t host_show_transport_mode(struct device *dev,
508 struct device_attribute *attr, char *buf)
509{
510 struct ctlr_info *h;
511 struct Scsi_Host *shost = class_to_shost(dev);
512
513 h = shost_to_hba(shost);
514 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600515 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600516 "performant" : "simple");
517}
518
Scott Teelda0697b2014-02-18 13:57:00 -0600519static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
520 struct device_attribute *attr, char *buf)
521{
522 struct ctlr_info *h;
523 struct Scsi_Host *shost = class_to_shost(dev);
524
525 h = shost_to_hba(shost);
526 return snprintf(buf, 30, "HP SSD Smart Path %s\n",
527 (h->acciopath_status == 1) ? "enabled" : "disabled");
528}
529
Stephen M. Cameron46380782011-05-03 15:00:01 -0500530/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600531static u32 unresettable_controller[] = {
532 0x324a103C, /* Smart Array P712m */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500533 0x324b103C, /* Smart Array P711m */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600534 0x3223103C, /* Smart Array P800 */
535 0x3234103C, /* Smart Array P400 */
536 0x3235103C, /* Smart Array P400i */
537 0x3211103C, /* Smart Array E200i */
538 0x3212103C, /* Smart Array E200 */
539 0x3213103C, /* Smart Array E200i */
540 0x3214103C, /* Smart Array E200i */
541 0x3215103C, /* Smart Array E200i */
542 0x3237103C, /* Smart Array E500 */
543 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100544 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600545 0x409C0E11, /* Smart Array 6400 */
546 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100547 0x40700E11, /* Smart Array 5300 */
548 0x40820E11, /* Smart Array 532 */
549 0x40830E11, /* Smart Array 5312 */
550 0x409A0E11, /* Smart Array 641 */
551 0x409B0E11, /* Smart Array 642 */
552 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600553};
554
Stephen M. Cameron46380782011-05-03 15:00:01 -0500555/* List of controllers which cannot even be soft reset */
556static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100557 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100558 0x40700E11, /* Smart Array 5300 */
559 0x40820E11, /* Smart Array 532 */
560 0x40830E11, /* Smart Array 5312 */
561 0x409A0E11, /* Smart Array 641 */
562 0x409B0E11, /* Smart Array 642 */
563 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500564 /* Exclude 640x boards. These are two pci devices in one slot
565 * which share a battery backed cache module. One controls the
566 * cache, the other accesses the cache through the one that controls
567 * it. If we reset the one controlling the cache, the other will
568 * likely not be happy. Just forbid resetting this conjoined mess.
569 * The 640x isn't really supported by hpsa anyway.
570 */
571 0x409C0E11, /* Smart Array 6400 */
572 0x409D0E11, /* Smart Array 6400 EM */
573};
574
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500575static u32 needs_abort_tags_swizzled[] = {
576 0x323D103C, /* Smart Array P700m */
577 0x324a103C, /* Smart Array P712m */
578 0x324b103C, /* SmartArray P711m */
579};
580
581static int board_id_in_array(u32 a[], int nelems, u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600582{
583 int i;
584
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500585 for (i = 0; i < nelems; i++)
586 if (a[i] == board_id)
587 return 1;
588 return 0;
589}
590
591static int ctlr_is_hard_resettable(u32 board_id)
592{
593 return !board_id_in_array(unresettable_controller,
594 ARRAY_SIZE(unresettable_controller), board_id);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600595}
596
Stephen M. Cameron46380782011-05-03 15:00:01 -0500597static int ctlr_is_soft_resettable(u32 board_id)
598{
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500599 return !board_id_in_array(soft_unresettable_controller,
600 ARRAY_SIZE(soft_unresettable_controller), board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500601}
602
603static int ctlr_is_resettable(u32 board_id)
604{
605 return ctlr_is_hard_resettable(board_id) ||
606 ctlr_is_soft_resettable(board_id);
607}
608
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500609static int ctlr_needs_abort_tags_swizzled(u32 board_id)
610{
611 return board_id_in_array(needs_abort_tags_swizzled,
612 ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
613}
614
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600615static ssize_t host_show_resettable(struct device *dev,
616 struct device_attribute *attr, char *buf)
617{
618 struct ctlr_info *h;
619 struct Scsi_Host *shost = class_to_shost(dev);
620
621 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500622 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600623}
624
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800625static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
626{
627 return (scsi3addr[3] & 0xC0) == 0x40;
628}
629
Robert Elliottf2ef0ce2015-01-23 16:41:35 -0600630static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
Don Brace7c59a0d2015-11-04 15:52:22 -0600631 "1(+0)ADM", "UNKNOWN", "PHYS DRV"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800632};
Scott Teel6b80b182014-02-18 13:56:55 -0600633#define HPSA_RAID_0 0
634#define HPSA_RAID_4 1
635#define HPSA_RAID_1 2 /* also used for RAID 10 */
636#define HPSA_RAID_5 3 /* also used for RAID 50 */
637#define HPSA_RAID_51 4
638#define HPSA_RAID_6 5 /* also used for RAID 60 */
639#define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
Don Brace7c59a0d2015-11-04 15:52:22 -0600640#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 2)
641#define PHYSICAL_DRIVE (ARRAY_SIZE(raid_label) - 1)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800642
Kevin Barnettf3f01732015-11-04 15:51:33 -0600643static inline bool is_logical_device(struct hpsa_scsi_dev_t *device)
644{
645 return !device->physical_device;
646}
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800647
648static ssize_t raid_level_show(struct device *dev,
649 struct device_attribute *attr, char *buf)
650{
651 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600652 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800653 struct ctlr_info *h;
654 struct scsi_device *sdev;
655 struct hpsa_scsi_dev_t *hdev;
656 unsigned long flags;
657
658 sdev = to_scsi_device(dev);
659 h = sdev_to_hba(sdev);
660 spin_lock_irqsave(&h->lock, flags);
661 hdev = sdev->hostdata;
662 if (!hdev) {
663 spin_unlock_irqrestore(&h->lock, flags);
664 return -ENODEV;
665 }
666
667 /* Is this even a logical drive? */
Kevin Barnettf3f01732015-11-04 15:51:33 -0600668 if (!is_logical_device(hdev)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800669 spin_unlock_irqrestore(&h->lock, flags);
670 l = snprintf(buf, PAGE_SIZE, "N/A\n");
671 return l;
672 }
673
674 rlevel = hdev->raid_level;
675 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600676 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800677 rlevel = RAID_UNKNOWN;
678 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
679 return l;
680}
681
682static ssize_t lunid_show(struct device *dev,
683 struct device_attribute *attr, char *buf)
684{
685 struct ctlr_info *h;
686 struct scsi_device *sdev;
687 struct hpsa_scsi_dev_t *hdev;
688 unsigned long flags;
689 unsigned char lunid[8];
690
691 sdev = to_scsi_device(dev);
692 h = sdev_to_hba(sdev);
693 spin_lock_irqsave(&h->lock, flags);
694 hdev = sdev->hostdata;
695 if (!hdev) {
696 spin_unlock_irqrestore(&h->lock, flags);
697 return -ENODEV;
698 }
699 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
700 spin_unlock_irqrestore(&h->lock, flags);
701 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
702 lunid[0], lunid[1], lunid[2], lunid[3],
703 lunid[4], lunid[5], lunid[6], lunid[7]);
704}
705
706static ssize_t unique_id_show(struct device *dev,
707 struct device_attribute *attr, char *buf)
708{
709 struct ctlr_info *h;
710 struct scsi_device *sdev;
711 struct hpsa_scsi_dev_t *hdev;
712 unsigned long flags;
713 unsigned char sn[16];
714
715 sdev = to_scsi_device(dev);
716 h = sdev_to_hba(sdev);
717 spin_lock_irqsave(&h->lock, flags);
718 hdev = sdev->hostdata;
719 if (!hdev) {
720 spin_unlock_irqrestore(&h->lock, flags);
721 return -ENODEV;
722 }
723 memcpy(sn, hdev->device_id, sizeof(sn));
724 spin_unlock_irqrestore(&h->lock, flags);
725 return snprintf(buf, 16 * 2 + 2,
726 "%02X%02X%02X%02X%02X%02X%02X%02X"
727 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
728 sn[0], sn[1], sn[2], sn[3],
729 sn[4], sn[5], sn[6], sn[7],
730 sn[8], sn[9], sn[10], sn[11],
731 sn[12], sn[13], sn[14], sn[15]);
732}
733
Joseph T Handzikded1be42016-04-27 17:13:33 -0500734static ssize_t sas_address_show(struct device *dev,
735 struct device_attribute *attr, char *buf)
736{
737 struct ctlr_info *h;
738 struct scsi_device *sdev;
739 struct hpsa_scsi_dev_t *hdev;
740 unsigned long flags;
741 u64 sas_address;
742
743 sdev = to_scsi_device(dev);
744 h = sdev_to_hba(sdev);
745 spin_lock_irqsave(&h->lock, flags);
746 hdev = sdev->hostdata;
747 if (!hdev || is_logical_device(hdev) || !hdev->expose_device) {
748 spin_unlock_irqrestore(&h->lock, flags);
749 return -ENODEV;
750 }
751 sas_address = hdev->sas_address;
752 spin_unlock_irqrestore(&h->lock, flags);
753
754 return snprintf(buf, PAGE_SIZE, "0x%016llx\n", sas_address);
755}
756
Scott Teelc1988682014-02-18 13:55:54 -0600757static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
758 struct device_attribute *attr, char *buf)
759{
760 struct ctlr_info *h;
761 struct scsi_device *sdev;
762 struct hpsa_scsi_dev_t *hdev;
763 unsigned long flags;
764 int offload_enabled;
765
766 sdev = to_scsi_device(dev);
767 h = sdev_to_hba(sdev);
768 spin_lock_irqsave(&h->lock, flags);
769 hdev = sdev->hostdata;
770 if (!hdev) {
771 spin_unlock_irqrestore(&h->lock, flags);
772 return -ENODEV;
773 }
774 offload_enabled = hdev->offload_enabled;
775 spin_unlock_irqrestore(&h->lock, flags);
776 return snprintf(buf, 20, "%d\n", offload_enabled);
777}
778
Joe Handzik8270b862015-07-18 11:12:43 -0500779#define MAX_PATHS 8
Joe Handzik8270b862015-07-18 11:12:43 -0500780static ssize_t path_info_show(struct device *dev,
781 struct device_attribute *attr, char *buf)
782{
783 struct ctlr_info *h;
784 struct scsi_device *sdev;
785 struct hpsa_scsi_dev_t *hdev;
786 unsigned long flags;
787 int i;
788 int output_len = 0;
789 u8 box;
790 u8 bay;
791 u8 path_map_index = 0;
792 char *active;
793 unsigned char phys_connector[2];
Joe Handzik8270b862015-07-18 11:12:43 -0500794
Joe Handzik8270b862015-07-18 11:12:43 -0500795 sdev = to_scsi_device(dev);
796 h = sdev_to_hba(sdev);
797 spin_lock_irqsave(&h->devlock, flags);
798 hdev = sdev->hostdata;
799 if (!hdev) {
800 spin_unlock_irqrestore(&h->devlock, flags);
801 return -ENODEV;
802 }
803
804 bay = hdev->bay;
805 for (i = 0; i < MAX_PATHS; i++) {
806 path_map_index = 1<<i;
807 if (i == hdev->active_path_index)
808 active = "Active";
809 else if (hdev->path_map & path_map_index)
810 active = "Inactive";
811 else
812 continue;
813
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600814 output_len += scnprintf(buf + output_len,
815 PAGE_SIZE - output_len,
816 "[%d:%d:%d:%d] %20.20s ",
Joe Handzik8270b862015-07-18 11:12:43 -0500817 h->scsi_host->host_no,
818 hdev->bus, hdev->target, hdev->lun,
819 scsi_device_type(hdev->devtype));
820
Don Bracecca8f132015-12-22 10:36:48 -0600821 if (hdev->devtype == TYPE_RAID || is_logical_device(hdev)) {
Don Brace2708f292015-12-22 10:36:36 -0600822 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600823 PAGE_SIZE - output_len,
824 "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500825 continue;
826 }
827
828 box = hdev->box[i];
829 memcpy(&phys_connector, &hdev->phys_connector[i],
830 sizeof(phys_connector));
831 if (phys_connector[0] < '0')
832 phys_connector[0] = '0';
833 if (phys_connector[1] < '0')
834 phys_connector[1] = '0';
Don Bracecca8f132015-12-22 10:36:48 -0600835 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600836 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500837 "PORT: %.2s ",
838 phys_connector);
Don Braceaf15ed32016-02-23 15:16:15 -0600839 if ((hdev->devtype == TYPE_DISK || hdev->devtype == TYPE_ZBC) &&
840 hdev->expose_device) {
Joe Handzik8270b862015-07-18 11:12:43 -0500841 if (box == 0 || box == 0xFF) {
Don Brace2708f292015-12-22 10:36:36 -0600842 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600843 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500844 "BAY: %hhu %s\n",
845 bay, active);
846 } else {
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 "BOX: %hhu BAY: %hhu %s\n",
850 box, bay, active);
851 }
852 } else if (box != 0 && box != 0xFF) {
Don Brace2708f292015-12-22 10:36:36 -0600853 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600854 PAGE_SIZE - output_len, "BOX: %hhu %s\n",
Joe Handzik8270b862015-07-18 11:12:43 -0500855 box, active);
856 } else
Don Brace2708f292015-12-22 10:36:36 -0600857 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600858 PAGE_SIZE - output_len, "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500859 }
860
861 spin_unlock_irqrestore(&h->devlock, flags);
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600862 return output_len;
Joe Handzik8270b862015-07-18 11:12:43 -0500863}
864
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600865static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
866static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
867static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
868static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Joseph T Handzikded1be42016-04-27 17:13:33 -0500869static DEVICE_ATTR(sas_address, S_IRUGO, sas_address_show, NULL);
Scott Teelc1988682014-02-18 13:55:54 -0600870static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
871 host_show_hp_ssd_smart_path_enabled, NULL);
Joe Handzik8270b862015-07-18 11:12:43 -0500872static DEVICE_ATTR(path_info, S_IRUGO, path_info_show, NULL);
Scott Teelda0697b2014-02-18 13:57:00 -0600873static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
874 host_show_hp_ssd_smart_path_status,
875 host_store_hp_ssd_smart_path_status);
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600876static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
877 host_store_raid_offload_debug);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600878static DEVICE_ATTR(firmware_revision, S_IRUGO,
879 host_show_firmware_revision, NULL);
880static DEVICE_ATTR(commands_outstanding, S_IRUGO,
881 host_show_commands_outstanding, NULL);
882static DEVICE_ATTR(transport_mode, S_IRUGO,
883 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600884static DEVICE_ATTR(resettable, S_IRUGO,
885 host_show_resettable, NULL);
Stephen Camerone985c582015-04-23 09:32:22 -0500886static DEVICE_ATTR(lockup_detected, S_IRUGO,
887 host_show_lockup_detected, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600888
889static struct device_attribute *hpsa_sdev_attrs[] = {
890 &dev_attr_raid_level,
891 &dev_attr_lunid,
892 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600893 &dev_attr_hp_ssd_smart_path_enabled,
Joe Handzik8270b862015-07-18 11:12:43 -0500894 &dev_attr_path_info,
Joseph T Handzikded1be42016-04-27 17:13:33 -0500895 &dev_attr_sas_address,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600896 NULL,
897};
898
899static struct device_attribute *hpsa_shost_attrs[] = {
900 &dev_attr_rescan,
901 &dev_attr_firmware_revision,
902 &dev_attr_commands_outstanding,
903 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600904 &dev_attr_resettable,
Scott Teelda0697b2014-02-18 13:57:00 -0600905 &dev_attr_hp_ssd_smart_path_status,
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600906 &dev_attr_raid_offload_debug,
Tomas Henzlfb53c432015-11-06 16:24:09 +0100907 &dev_attr_lockup_detected,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600908 NULL,
909};
910
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500911#define HPSA_NRESERVED_CMDS (HPSA_CMDS_RESERVED_FOR_ABORTS + \
912 HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
913
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600914static struct scsi_host_template hpsa_driver_template = {
915 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600916 .name = HPSA,
917 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600918 .queuecommand = hpsa_scsi_queue_command,
919 .scan_start = hpsa_scan_start,
920 .scan_finished = hpsa_scan_finished,
Don Brace7c0a0222015-01-23 16:41:30 -0600921 .change_queue_depth = hpsa_change_queue_depth,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600922 .this_id = -1,
923 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500924 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600925 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
926 .ioctl = hpsa_ioctl,
927 .slave_alloc = hpsa_slave_alloc,
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500928 .slave_configure = hpsa_slave_configure,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600929 .slave_destroy = hpsa_slave_destroy,
930#ifdef CONFIG_COMPAT
931 .compat_ioctl = hpsa_compat_ioctl,
932#endif
933 .sdev_attrs = hpsa_sdev_attrs,
934 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500935 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400936 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600937};
938
Matt Gates254f7962012-05-01 11:43:06 -0500939static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600940{
941 u32 a;
Stephen M. Cameron072b0512014-05-29 10:53:07 -0500942 struct reply_queue_buffer *rq = &h->reply_queue[q];
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600943
Matt Gatese1f7de02014-02-18 13:55:17 -0600944 if (h->transMethod & CFGTBL_Trans_io_accel1)
945 return h->access.command_completed(h, q);
946
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600947 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500948 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600949
Matt Gates254f7962012-05-01 11:43:06 -0500950 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
951 a = rq->head[rq->current_entry];
952 rq->current_entry++;
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600953 atomic_dec(&h->commands_outstanding);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600954 } else {
955 a = FIFO_EMPTY;
956 }
957 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500958 if (rq->current_entry == h->max_commands) {
959 rq->current_entry = 0;
960 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600961 }
962 return a;
963}
964
Scott Teelc3497752014-02-18 13:56:34 -0600965/*
966 * There are some special bits in the bus address of the
967 * command that we have to set for the controller to know
968 * how to process the command:
969 *
970 * Normal performant mode:
971 * bit 0: 1 means performant mode, 0 means simple mode.
972 * bits 1-3 = block fetch table entry
973 * bits 4-6 = command type (== 0)
974 *
975 * ioaccel1 mode:
976 * bit 0 = "performant mode" bit.
977 * bits 1-3 = block fetch table entry
978 * bits 4-6 = command type (== 110)
979 * (command type is needed because ioaccel1 mode
980 * commands are submitted through the same register as normal
981 * mode commands, so this is how the controller knows whether
982 * the command is normal mode or ioaccel1 mode.)
983 *
984 * ioaccel2 mode:
985 * bit 0 = "performant mode" bit.
986 * bits 1-4 = block fetch table entry (note extra bit)
987 * bits 4-6 = not needed, because ioaccel2 mode has
988 * a separate special register for submitting commands.
989 */
990
Webb Scales25163bd2015-04-23 09:32:00 -0500991/*
992 * set_performant_mode: Modify the tag for cciss performant
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600993 * set bit 0 for pull model, bits 3-1 for block fetch
994 * register number
995 */
Webb Scales25163bd2015-04-23 09:32:00 -0500996#define DEFAULT_REPLY_QUEUE (-1)
997static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
998 int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600999{
Matt Gates254f7962012-05-01 11:43:06 -05001000 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001001 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Webb Scales25163bd2015-04-23 09:32:00 -05001002 if (unlikely(!h->msix_vector))
1003 return;
1004 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
Matt Gates254f7962012-05-01 11:43:06 -05001005 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +02001006 raw_smp_processor_id() % h->nreply_queues;
Webb Scales25163bd2015-04-23 09:32:00 -05001007 else
1008 c->Header.ReplyQueue = reply_queue % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -05001009 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001010}
1011
Scott Teelc3497752014-02-18 13:56:34 -06001012static void set_ioaccel1_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05001013 struct CommandList *c,
1014 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -06001015{
1016 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
1017
Webb Scales25163bd2015-04-23 09:32:00 -05001018 /*
1019 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -06001020 * processor. This seems to give the best I/O throughput.
1021 */
Webb Scales25163bd2015-04-23 09:32:00 -05001022 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1023 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
1024 else
1025 cp->ReplyQueue = reply_queue % h->nreply_queues;
1026 /*
1027 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -06001028 * - performant mode bit (bit 0)
1029 * - pull count (bits 1-3)
1030 * - command type (bits 4-6)
1031 */
1032 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
1033 IOACCEL1_BUSADDR_CMDTYPE;
1034}
1035
Stephen Cameron8be986c2015-04-23 09:34:06 -05001036static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
1037 struct CommandList *c,
1038 int reply_queue)
1039{
1040 struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
1041 &h->ioaccel2_cmd_pool[c->cmdindex];
1042
1043 /* Tell the controller to post the reply to the queue for this
1044 * processor. This seems to give the best I/O throughput.
1045 */
1046 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1047 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1048 else
1049 cp->reply_queue = reply_queue % h->nreply_queues;
1050 /* Set the bits in the address sent down to include:
1051 * - performant mode bit not used in ioaccel mode 2
1052 * - pull count (bits 0-3)
1053 * - command type isn't needed for ioaccel2
1054 */
1055 c->busaddr |= h->ioaccel2_blockFetchTable[0];
1056}
1057
Scott Teelc3497752014-02-18 13:56:34 -06001058static void set_ioaccel2_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05001059 struct CommandList *c,
1060 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -06001061{
1062 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
1063
Webb Scales25163bd2015-04-23 09:32:00 -05001064 /*
1065 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -06001066 * processor. This seems to give the best I/O throughput.
1067 */
Webb Scales25163bd2015-04-23 09:32:00 -05001068 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1069 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1070 else
1071 cp->reply_queue = reply_queue % h->nreply_queues;
1072 /*
1073 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -06001074 * - performant mode bit not used in ioaccel mode 2
1075 * - pull count (bits 0-3)
1076 * - command type isn't needed for ioaccel2
1077 */
1078 c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
1079}
1080
Stephen M. Camerone85c5972012-05-01 11:43:42 -05001081static int is_firmware_flash_cmd(u8 *cdb)
1082{
1083 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
1084}
1085
1086/*
1087 * During firmware flash, the heartbeat register may not update as frequently
1088 * as it should. So we dial down lockup detection during firmware flash. and
1089 * dial it back up when firmware flash completes.
1090 */
1091#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
1092#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
1093static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
1094 struct CommandList *c)
1095{
1096 if (!is_firmware_flash_cmd(c->Request.CDB))
1097 return;
1098 atomic_inc(&h->firmware_flash_in_progress);
1099 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
1100}
1101
1102static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
1103 struct CommandList *c)
1104{
1105 if (is_firmware_flash_cmd(c->Request.CDB) &&
1106 atomic_dec_and_test(&h->firmware_flash_in_progress))
1107 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
1108}
1109
Webb Scales25163bd2015-04-23 09:32:00 -05001110static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
1111 struct CommandList *c, int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001112{
Stephen Cameronc05e8862015-01-23 16:44:40 -06001113 dial_down_lockup_detection_during_fw_flash(h, c);
1114 atomic_inc(&h->commands_outstanding);
Scott Teelc3497752014-02-18 13:56:34 -06001115 switch (c->cmd_type) {
1116 case CMD_IOACCEL1:
Webb Scales25163bd2015-04-23 09:32:00 -05001117 set_ioaccel1_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001118 writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
Scott Teelc3497752014-02-18 13:56:34 -06001119 break;
1120 case CMD_IOACCEL2:
Webb Scales25163bd2015-04-23 09:32:00 -05001121 set_ioaccel2_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001122 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
Scott Teelc3497752014-02-18 13:56:34 -06001123 break;
Stephen Cameron8be986c2015-04-23 09:34:06 -05001124 case IOACCEL2_TMF:
1125 set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
1126 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
1127 break;
Scott Teelc3497752014-02-18 13:56:34 -06001128 default:
Webb Scales25163bd2015-04-23 09:32:00 -05001129 set_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001130 h->access.submit_command(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06001131 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001132}
1133
Webb Scalesa58e7e52015-04-23 09:34:16 -05001134static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
Webb Scales25163bd2015-04-23 09:32:00 -05001135{
Webb Scalesd604f532015-04-23 09:35:22 -05001136 if (unlikely(hpsa_is_pending_event(c)))
Webb Scalesa58e7e52015-04-23 09:34:16 -05001137 return finish_cmd(c);
1138
Webb Scales25163bd2015-04-23 09:32:00 -05001139 __enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
1140}
1141
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001142static inline int is_hba_lunid(unsigned char scsi3addr[])
1143{
1144 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
1145}
1146
1147static inline int is_scsi_rev_5(struct ctlr_info *h)
1148{
1149 if (!h->hba_inquiry_data)
1150 return 0;
1151 if ((h->hba_inquiry_data[2] & 0x07) == 5)
1152 return 1;
1153 return 0;
1154}
1155
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001156static int hpsa_find_target_lun(struct ctlr_info *h,
1157 unsigned char scsi3addr[], int bus, int *target, int *lun)
1158{
1159 /* finds an unused bus, target, lun for a new physical device
1160 * assumes h->devlock is held
1161 */
1162 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -05001163 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001164
Akinobu Mita263d9402012-01-21 00:15:27 +09001165 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001166
1167 for (i = 0; i < h->ndevices; i++) {
1168 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +09001169 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001170 }
1171
Akinobu Mita263d9402012-01-21 00:15:27 +09001172 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
1173 if (i < HPSA_MAX_DEVICES) {
1174 /* *bus = 1; */
1175 *target = i;
1176 *lun = 0;
1177 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001178 }
1179 return !found;
1180}
1181
Don Brace1d33d852015-11-04 15:50:31 -06001182static void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
Webb Scales0d96ef52015-04-23 09:31:55 -05001183 struct hpsa_scsi_dev_t *dev, char *description)
1184{
Don Brace7c59a0d2015-11-04 15:52:22 -06001185#define LABEL_SIZE 25
1186 char label[LABEL_SIZE];
1187
Don Brace9975ec92015-11-04 15:50:25 -06001188 if (h == NULL || h->pdev == NULL || h->scsi_host == NULL)
1189 return;
1190
Don Brace7c59a0d2015-11-04 15:52:22 -06001191 switch (dev->devtype) {
1192 case TYPE_RAID:
1193 snprintf(label, LABEL_SIZE, "controller");
1194 break;
1195 case TYPE_ENCLOSURE:
1196 snprintf(label, LABEL_SIZE, "enclosure");
1197 break;
1198 case TYPE_DISK:
Don Braceaf15ed32016-02-23 15:16:15 -06001199 case TYPE_ZBC:
Don Brace7c59a0d2015-11-04 15:52:22 -06001200 if (dev->external)
1201 snprintf(label, LABEL_SIZE, "external");
1202 else if (!is_logical_dev_addr_mode(dev->scsi3addr))
1203 snprintf(label, LABEL_SIZE, "%s",
1204 raid_label[PHYSICAL_DRIVE]);
1205 else
1206 snprintf(label, LABEL_SIZE, "RAID-%s",
1207 dev->raid_level > RAID_UNKNOWN ? "?" :
1208 raid_label[dev->raid_level]);
1209 break;
1210 case TYPE_ROM:
1211 snprintf(label, LABEL_SIZE, "rom");
1212 break;
1213 case TYPE_TAPE:
1214 snprintf(label, LABEL_SIZE, "tape");
1215 break;
1216 case TYPE_MEDIUM_CHANGER:
1217 snprintf(label, LABEL_SIZE, "changer");
1218 break;
1219 default:
1220 snprintf(label, LABEL_SIZE, "UNKNOWN");
1221 break;
1222 }
1223
Webb Scales0d96ef52015-04-23 09:31:55 -05001224 dev_printk(level, &h->pdev->dev,
Don Brace7c59a0d2015-11-04 15:52:22 -06001225 "scsi %d:%d:%d:%d: %s %s %.8s %.16s %s SSDSmartPathCap%c En%c Exp=%d\n",
Webb Scales0d96ef52015-04-23 09:31:55 -05001226 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
1227 description,
1228 scsi_device_type(dev->devtype),
1229 dev->vendor,
1230 dev->model,
Don Brace7c59a0d2015-11-04 15:52:22 -06001231 label,
Webb Scales0d96ef52015-04-23 09:31:55 -05001232 dev->offload_config ? '+' : '-',
1233 dev->offload_enabled ? '+' : '-',
Kevin Barnett2a168202015-11-04 15:51:21 -06001234 dev->expose_device);
Webb Scales0d96ef52015-04-23 09:31:55 -05001235}
1236
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001237/* Add an entry into h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001238static int hpsa_scsi_add_entry(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001239 struct hpsa_scsi_dev_t *device,
1240 struct hpsa_scsi_dev_t *added[], int *nadded)
1241{
1242 /* assumes h->devlock is held */
1243 int n = h->ndevices;
1244 int i;
1245 unsigned char addr1[8], addr2[8];
1246 struct hpsa_scsi_dev_t *sd;
1247
Scott Teelcfe5bad2011-10-26 16:21:07 -05001248 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001249 dev_err(&h->pdev->dev, "too many devices, some will be "
1250 "inaccessible.\n");
1251 return -1;
1252 }
1253
1254 /* physical devices do not have lun or target assigned until now. */
1255 if (device->lun != -1)
1256 /* Logical device, lun is already assigned. */
1257 goto lun_assigned;
1258
1259 /* If this device a non-zero lun of a multi-lun device
1260 * byte 4 of the 8-byte LUN addr will contain the logical
Don Brace2b08b3e2015-01-23 16:41:09 -06001261 * unit no, zero otherwise.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001262 */
1263 if (device->scsi3addr[4] == 0) {
1264 /* This is not a non-zero lun of a multi-lun device */
1265 if (hpsa_find_target_lun(h, device->scsi3addr,
1266 device->bus, &device->target, &device->lun) != 0)
1267 return -1;
1268 goto lun_assigned;
1269 }
1270
1271 /* This is a non-zero lun of a multi-lun device.
1272 * Search through our list and find the device which
shane.seymour9a4178b2015-07-18 11:13:09 -05001273 * has the same 8 byte LUN address, excepting byte 4 and 5.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001274 * Assign the same bus and target for this new LUN.
1275 * Use the logical unit number from the firmware.
1276 */
1277 memcpy(addr1, device->scsi3addr, 8);
1278 addr1[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001279 addr1[5] = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001280 for (i = 0; i < n; i++) {
1281 sd = h->dev[i];
1282 memcpy(addr2, sd->scsi3addr, 8);
1283 addr2[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001284 addr2[5] = 0;
1285 /* differ only in byte 4 and 5? */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001286 if (memcmp(addr1, addr2, 8) == 0) {
1287 device->bus = sd->bus;
1288 device->target = sd->target;
1289 device->lun = device->scsi3addr[4];
1290 break;
1291 }
1292 }
1293 if (device->lun == -1) {
1294 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1295 " suspect firmware bug or unsupported hardware "
1296 "configuration.\n");
1297 return -1;
1298 }
1299
1300lun_assigned:
1301
1302 h->dev[n] = device;
1303 h->ndevices++;
1304 added[*nadded] = device;
1305 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001306 hpsa_show_dev_msg(KERN_INFO, h, device,
Kevin Barnett2a168202015-11-04 15:51:21 -06001307 device->expose_device ? "added" : "masked");
Robert Elliotta473d862015-04-23 09:32:54 -05001308 device->offload_to_be_enabled = device->offload_enabled;
1309 device->offload_enabled = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001310 return 0;
1311}
1312
Scott Teelbd9244f2012-01-19 14:01:30 -06001313/* Update an entry in h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001314static void hpsa_scsi_update_entry(struct ctlr_info *h,
Scott Teelbd9244f2012-01-19 14:01:30 -06001315 int entry, struct hpsa_scsi_dev_t *new_entry)
1316{
Robert Elliotta473d862015-04-23 09:32:54 -05001317 int offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001318 /* assumes h->devlock is held */
1319 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1320
1321 /* Raid level changed. */
1322 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001323
Don Brace03383732015-01-23 16:43:30 -06001324 /* Raid offload parameters changed. Careful about the ordering. */
1325 if (new_entry->offload_config && new_entry->offload_enabled) {
1326 /*
1327 * if drive is newly offload_enabled, we want to copy the
1328 * raid map data first. If previously offload_enabled and
1329 * offload_config were set, raid map data had better be
1330 * the same as it was before. if raid map data is changed
1331 * then it had better be the case that
1332 * h->dev[entry]->offload_enabled is currently 0.
1333 */
1334 h->dev[entry]->raid_map = new_entry->raid_map;
1335 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
Don Brace03383732015-01-23 16:43:30 -06001336 }
Joe Handzika3144e02015-04-23 09:32:59 -05001337 if (new_entry->hba_ioaccel_enabled) {
1338 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1339 wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1340 }
1341 h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001342 h->dev[entry]->offload_config = new_entry->offload_config;
Stephen M. Cameron9fb0de22014-02-18 13:56:50 -06001343 h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
Don Brace03383732015-01-23 16:43:30 -06001344 h->dev[entry]->queue_depth = new_entry->queue_depth;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001345
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001346 /*
1347 * We can turn off ioaccel offload now, but need to delay turning
1348 * it on until we can update h->dev[entry]->phys_disk[], but we
1349 * can't do that until all the devices are updated.
1350 */
1351 h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
1352 if (!new_entry->offload_enabled)
1353 h->dev[entry]->offload_enabled = 0;
1354
Robert Elliotta473d862015-04-23 09:32:54 -05001355 offload_enabled = h->dev[entry]->offload_enabled;
1356 h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
Webb Scales0d96ef52015-04-23 09:31:55 -05001357 hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
Robert Elliotta473d862015-04-23 09:32:54 -05001358 h->dev[entry]->offload_enabled = offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001359}
1360
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001361/* Replace an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001362static void hpsa_scsi_replace_entry(struct ctlr_info *h,
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001363 int entry, struct hpsa_scsi_dev_t *new_entry,
1364 struct hpsa_scsi_dev_t *added[], int *nadded,
1365 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1366{
1367 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -05001368 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001369 removed[*nremoved] = h->dev[entry];
1370 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -05001371
1372 /*
1373 * New physical devices won't have target/lun assigned yet
1374 * so we need to preserve the values in the slot we are replacing.
1375 */
1376 if (new_entry->target == -1) {
1377 new_entry->target = h->dev[entry]->target;
1378 new_entry->lun = h->dev[entry]->lun;
1379 }
1380
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001381 h->dev[entry] = new_entry;
1382 added[*nadded] = new_entry;
1383 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001384 hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
Robert Elliotta473d862015-04-23 09:32:54 -05001385 new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1386 new_entry->offload_enabled = 0;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001387}
1388
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001389/* Remove an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001390static void hpsa_scsi_remove_entry(struct ctlr_info *h, int entry,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001391 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1392{
1393 /* assumes h->devlock is held */
1394 int i;
1395 struct hpsa_scsi_dev_t *sd;
1396
Scott Teelcfe5bad2011-10-26 16:21:07 -05001397 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001398
1399 sd = h->dev[entry];
1400 removed[*nremoved] = h->dev[entry];
1401 (*nremoved)++;
1402
1403 for (i = entry; i < h->ndevices-1; i++)
1404 h->dev[i] = h->dev[i+1];
1405 h->ndevices--;
Webb Scales0d96ef52015-04-23 09:31:55 -05001406 hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001407}
1408
1409#define SCSI3ADDR_EQ(a, b) ( \
1410 (a)[7] == (b)[7] && \
1411 (a)[6] == (b)[6] && \
1412 (a)[5] == (b)[5] && \
1413 (a)[4] == (b)[4] && \
1414 (a)[3] == (b)[3] && \
1415 (a)[2] == (b)[2] && \
1416 (a)[1] == (b)[1] && \
1417 (a)[0] == (b)[0])
1418
1419static void fixup_botched_add(struct ctlr_info *h,
1420 struct hpsa_scsi_dev_t *added)
1421{
1422 /* called when scsi_add_device fails in order to re-adjust
1423 * h->dev[] to match the mid layer's view.
1424 */
1425 unsigned long flags;
1426 int i, j;
1427
1428 spin_lock_irqsave(&h->lock, flags);
1429 for (i = 0; i < h->ndevices; i++) {
1430 if (h->dev[i] == added) {
1431 for (j = i; j < h->ndevices-1; j++)
1432 h->dev[j] = h->dev[j+1];
1433 h->ndevices--;
1434 break;
1435 }
1436 }
1437 spin_unlock_irqrestore(&h->lock, flags);
1438 kfree(added);
1439}
1440
1441static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1442 struct hpsa_scsi_dev_t *dev2)
1443{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001444 /* we compare everything except lun and target as these
1445 * are not yet assigned. Compare parts likely
1446 * to differ first
1447 */
1448 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1449 sizeof(dev1->scsi3addr)) != 0)
1450 return 0;
1451 if (memcmp(dev1->device_id, dev2->device_id,
1452 sizeof(dev1->device_id)) != 0)
1453 return 0;
1454 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1455 return 0;
1456 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1457 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001458 if (dev1->devtype != dev2->devtype)
1459 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001460 if (dev1->bus != dev2->bus)
1461 return 0;
1462 return 1;
1463}
1464
Scott Teelbd9244f2012-01-19 14:01:30 -06001465static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1466 struct hpsa_scsi_dev_t *dev2)
1467{
1468 /* Device attributes that can change, but don't mean
1469 * that the device is a different device, nor that the OS
1470 * needs to be told anything about the change.
1471 */
1472 if (dev1->raid_level != dev2->raid_level)
1473 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001474 if (dev1->offload_config != dev2->offload_config)
1475 return 1;
1476 if (dev1->offload_enabled != dev2->offload_enabled)
1477 return 1;
Don Brace93849502015-07-18 11:12:49 -05001478 if (!is_logical_dev_addr_mode(dev1->scsi3addr))
1479 if (dev1->queue_depth != dev2->queue_depth)
1480 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -06001481 return 0;
1482}
1483
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001484/* Find needle in haystack. If exact match found, return DEVICE_SAME,
1485 * and return needle location in *index. If scsi3addr matches, but not
1486 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -06001487 * location in *index.
1488 * In the case of a minor device attribute change, such as RAID level, just
1489 * return DEVICE_UPDATED, along with the updated device's location in index.
1490 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001491 */
1492static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1493 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1494 int *index)
1495{
1496 int i;
1497#define DEVICE_NOT_FOUND 0
1498#define DEVICE_CHANGED 1
1499#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -06001500#define DEVICE_UPDATED 3
Don Brace1d33d852015-11-04 15:50:31 -06001501 if (needle == NULL)
1502 return DEVICE_NOT_FOUND;
1503
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001504 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -06001505 if (haystack[i] == NULL) /* previously removed. */
1506 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001507 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1508 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -06001509 if (device_is_the_same(needle, haystack[i])) {
1510 if (device_updated(needle, haystack[i]))
1511 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001512 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -06001513 } else {
Stephen M. Cameron98465902014-02-21 16:25:00 -06001514 /* Keep offline devices offline */
1515 if (needle->volume_offline)
1516 return DEVICE_NOT_FOUND;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001517 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -06001518 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001519 }
1520 }
1521 *index = -1;
1522 return DEVICE_NOT_FOUND;
1523}
1524
Stephen M. Cameron98465902014-02-21 16:25:00 -06001525static void hpsa_monitor_offline_device(struct ctlr_info *h,
1526 unsigned char scsi3addr[])
1527{
1528 struct offline_device_entry *device;
1529 unsigned long flags;
1530
1531 /* Check to see if device is already on the list */
1532 spin_lock_irqsave(&h->offline_device_lock, flags);
1533 list_for_each_entry(device, &h->offline_device_list, offline_list) {
1534 if (memcmp(device->scsi3addr, scsi3addr,
1535 sizeof(device->scsi3addr)) == 0) {
1536 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1537 return;
1538 }
1539 }
1540 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1541
1542 /* Device is not on the list, add it. */
1543 device = kmalloc(sizeof(*device), GFP_KERNEL);
1544 if (!device) {
1545 dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
1546 return;
1547 }
1548 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1549 spin_lock_irqsave(&h->offline_device_lock, flags);
1550 list_add_tail(&device->offline_list, &h->offline_device_list);
1551 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1552}
1553
1554/* Print a message explaining various offline volume states */
1555static void hpsa_show_volume_status(struct ctlr_info *h,
1556 struct hpsa_scsi_dev_t *sd)
1557{
1558 if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
1559 dev_info(&h->pdev->dev,
1560 "C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
1561 h->scsi_host->host_no,
1562 sd->bus, sd->target, sd->lun);
1563 switch (sd->volume_offline) {
1564 case HPSA_LV_OK:
1565 break;
1566 case HPSA_LV_UNDERGOING_ERASE:
1567 dev_info(&h->pdev->dev,
1568 "C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
1569 h->scsi_host->host_no,
1570 sd->bus, sd->target, sd->lun);
1571 break;
Scott Benesh5ca01202015-07-18 11:13:04 -05001572 case HPSA_LV_NOT_AVAILABLE:
1573 dev_info(&h->pdev->dev,
1574 "C%d:B%d:T%d:L%d Volume is waiting for transforming volume.\n",
1575 h->scsi_host->host_no,
1576 sd->bus, sd->target, sd->lun);
1577 break;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001578 case HPSA_LV_UNDERGOING_RPI:
1579 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001580 "C%d:B%d:T%d:L%d Volume is undergoing rapid parity init.\n",
Stephen M. Cameron98465902014-02-21 16:25:00 -06001581 h->scsi_host->host_no,
1582 sd->bus, sd->target, sd->lun);
1583 break;
1584 case HPSA_LV_PENDING_RPI:
1585 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001586 "C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
1587 h->scsi_host->host_no,
1588 sd->bus, sd->target, sd->lun);
Stephen M. Cameron98465902014-02-21 16:25:00 -06001589 break;
1590 case HPSA_LV_ENCRYPTED_NO_KEY:
1591 dev_info(&h->pdev->dev,
1592 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
1593 h->scsi_host->host_no,
1594 sd->bus, sd->target, sd->lun);
1595 break;
1596 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
1597 dev_info(&h->pdev->dev,
1598 "C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
1599 h->scsi_host->host_no,
1600 sd->bus, sd->target, sd->lun);
1601 break;
1602 case HPSA_LV_UNDERGOING_ENCRYPTION:
1603 dev_info(&h->pdev->dev,
1604 "C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
1605 h->scsi_host->host_no,
1606 sd->bus, sd->target, sd->lun);
1607 break;
1608 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
1609 dev_info(&h->pdev->dev,
1610 "C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
1611 h->scsi_host->host_no,
1612 sd->bus, sd->target, sd->lun);
1613 break;
1614 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1615 dev_info(&h->pdev->dev,
1616 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
1617 h->scsi_host->host_no,
1618 sd->bus, sd->target, sd->lun);
1619 break;
1620 case HPSA_LV_PENDING_ENCRYPTION:
1621 dev_info(&h->pdev->dev,
1622 "C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
1623 h->scsi_host->host_no,
1624 sd->bus, sd->target, sd->lun);
1625 break;
1626 case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
1627 dev_info(&h->pdev->dev,
1628 "C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
1629 h->scsi_host->host_no,
1630 sd->bus, sd->target, sd->lun);
1631 break;
1632 }
1633}
1634
Don Brace03383732015-01-23 16:43:30 -06001635/*
1636 * Figure the list of physical drive pointers for a logical drive with
1637 * raid offload configured.
1638 */
1639static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
1640 struct hpsa_scsi_dev_t *dev[], int ndevices,
1641 struct hpsa_scsi_dev_t *logical_drive)
1642{
1643 struct raid_map_data *map = &logical_drive->raid_map;
1644 struct raid_map_disk_data *dd = &map->data[0];
1645 int i, j;
1646 int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
1647 le16_to_cpu(map->metadata_disks_per_row);
1648 int nraid_map_entries = le16_to_cpu(map->row_cnt) *
1649 le16_to_cpu(map->layout_map_count) *
1650 total_disks_per_row;
1651 int nphys_disk = le16_to_cpu(map->layout_map_count) *
1652 total_disks_per_row;
1653 int qdepth;
1654
1655 if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
1656 nraid_map_entries = RAID_MAP_MAX_ENTRIES;
1657
Webb Scalesd604f532015-04-23 09:35:22 -05001658 logical_drive->nphysical_disks = nraid_map_entries;
1659
Don Brace03383732015-01-23 16:43:30 -06001660 qdepth = 0;
1661 for (i = 0; i < nraid_map_entries; i++) {
1662 logical_drive->phys_disk[i] = NULL;
1663 if (!logical_drive->offload_config)
1664 continue;
1665 for (j = 0; j < ndevices; j++) {
Don Brace1d33d852015-11-04 15:50:31 -06001666 if (dev[j] == NULL)
1667 continue;
Petros Koutoupisff615f02016-05-09 13:44:10 -05001668 if (dev[j]->devtype != TYPE_DISK &&
1669 dev[j]->devtype != TYPE_ZBC)
Don Braceaf15ed32016-02-23 15:16:15 -06001670 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001671 if (is_logical_device(dev[j]))
Don Brace03383732015-01-23 16:43:30 -06001672 continue;
1673 if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
1674 continue;
1675
1676 logical_drive->phys_disk[i] = dev[j];
1677 if (i < nphys_disk)
1678 qdepth = min(h->nr_cmds, qdepth +
1679 logical_drive->phys_disk[i]->queue_depth);
1680 break;
1681 }
1682
1683 /*
1684 * This can happen if a physical drive is removed and
1685 * the logical drive is degraded. In that case, the RAID
1686 * map data will refer to a physical disk which isn't actually
1687 * present. And in that case offload_enabled should already
1688 * be 0, but we'll turn it off here just in case
1689 */
1690 if (!logical_drive->phys_disk[i]) {
1691 logical_drive->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001692 logical_drive->offload_to_be_enabled = 0;
1693 logical_drive->queue_depth = 8;
Don Brace03383732015-01-23 16:43:30 -06001694 }
1695 }
1696 if (nraid_map_entries)
1697 /*
1698 * This is correct for reads, too high for full stripe writes,
1699 * way too high for partial stripe writes
1700 */
1701 logical_drive->queue_depth = qdepth;
1702 else
1703 logical_drive->queue_depth = h->nr_cmds;
1704}
1705
1706static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
1707 struct hpsa_scsi_dev_t *dev[], int ndevices)
1708{
1709 int i;
1710
1711 for (i = 0; i < ndevices; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001712 if (dev[i] == NULL)
1713 continue;
Petros Koutoupisff615f02016-05-09 13:44:10 -05001714 if (dev[i]->devtype != TYPE_DISK &&
1715 dev[i]->devtype != TYPE_ZBC)
Don Braceaf15ed32016-02-23 15:16:15 -06001716 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001717 if (!is_logical_device(dev[i]))
Don Brace03383732015-01-23 16:43:30 -06001718 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001719
1720 /*
1721 * If offload is currently enabled, the RAID map and
1722 * phys_disk[] assignment *better* not be changing
1723 * and since it isn't changing, we do not need to
1724 * update it.
1725 */
1726 if (dev[i]->offload_enabled)
1727 continue;
1728
Don Brace03383732015-01-23 16:43:30 -06001729 hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
1730 }
1731}
1732
Kevin Barnett096ccff2015-11-04 15:51:51 -06001733static int hpsa_add_device(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1734{
1735 int rc = 0;
1736
1737 if (!h->scsi_host)
1738 return 1;
1739
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001740 if (is_logical_device(device)) /* RAID */
1741 rc = scsi_add_device(h->scsi_host, device->bus,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001742 device->target, device->lun);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001743 else /* HBA */
1744 rc = hpsa_add_sas_device(h->sas_host, device);
1745
Kevin Barnett096ccff2015-11-04 15:51:51 -06001746 return rc;
1747}
1748
Don Braceba74fdc2016-04-27 17:14:17 -05001749static int hpsa_find_outstanding_commands_for_dev(struct ctlr_info *h,
1750 struct hpsa_scsi_dev_t *dev)
1751{
1752 int i;
1753 int count = 0;
1754
1755 for (i = 0; i < h->nr_cmds; i++) {
1756 struct CommandList *c = h->cmd_pool + i;
1757 int refcount = atomic_inc_return(&c->refcount);
1758
1759 if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev,
1760 dev->scsi3addr)) {
1761 unsigned long flags;
1762
1763 spin_lock_irqsave(&h->lock, flags); /* Implied MB */
1764 if (!hpsa_is_cmd_idle(c))
1765 ++count;
1766 spin_unlock_irqrestore(&h->lock, flags);
1767 }
1768
1769 cmd_free(h, c);
1770 }
1771
1772 return count;
1773}
1774
1775static void hpsa_wait_for_outstanding_commands_for_dev(struct ctlr_info *h,
1776 struct hpsa_scsi_dev_t *device)
1777{
1778 int cmds = 0;
1779 int waits = 0;
1780
1781 while (1) {
1782 cmds = hpsa_find_outstanding_commands_for_dev(h, device);
1783 if (cmds == 0)
1784 break;
1785 if (++waits > 20)
1786 break;
1787 dev_warn(&h->pdev->dev,
1788 "%s: removing device with %d outstanding commands!\n",
1789 __func__, cmds);
1790 msleep(1000);
1791 }
1792}
1793
Kevin Barnett096ccff2015-11-04 15:51:51 -06001794static void hpsa_remove_device(struct ctlr_info *h,
1795 struct hpsa_scsi_dev_t *device)
1796{
1797 struct scsi_device *sdev = NULL;
1798
1799 if (!h->scsi_host)
1800 return;
1801
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001802 if (is_logical_device(device)) { /* RAID */
1803 sdev = scsi_device_lookup(h->scsi_host, device->bus,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001804 device->target, device->lun);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001805 if (sdev) {
1806 scsi_remove_device(sdev);
1807 scsi_device_put(sdev);
1808 } else {
1809 /*
1810 * We don't expect to get here. Future commands
1811 * to this device will get a selection timeout as
1812 * if the device were gone.
1813 */
1814 hpsa_show_dev_msg(KERN_WARNING, h, device,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001815 "didn't find device for removal.");
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001816 }
Don Braceba74fdc2016-04-27 17:14:17 -05001817 } else { /* HBA */
1818
1819 device->removed = 1;
1820 hpsa_wait_for_outstanding_commands_for_dev(h, device);
1821
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001822 hpsa_remove_sas_device(device);
Don Braceba74fdc2016-04-27 17:14:17 -05001823 }
Kevin Barnett096ccff2015-11-04 15:51:51 -06001824}
1825
Don Brace8aa60682015-11-04 15:50:01 -06001826static void adjust_hpsa_scsi_table(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001827 struct hpsa_scsi_dev_t *sd[], int nsds)
1828{
1829 /* sd contains scsi3 addresses and devtypes, and inquiry
1830 * data. This function takes what's in sd to be the current
1831 * reality and updates h->dev[] to reflect that reality.
1832 */
1833 int i, entry, device_change, changes = 0;
1834 struct hpsa_scsi_dev_t *csd;
1835 unsigned long flags;
1836 struct hpsa_scsi_dev_t **added, **removed;
1837 int nadded, nremoved;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001838
Don Braceda03ded2015-11-04 15:50:56 -06001839 /*
1840 * A reset can cause a device status to change
1841 * re-schedule the scan to see what happened.
1842 */
1843 if (h->reset_in_progress) {
1844 h->drv_req_rescan = 1;
1845 return;
1846 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001847
Scott Teelcfe5bad2011-10-26 16:21:07 -05001848 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1849 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001850
1851 if (!added || !removed) {
1852 dev_warn(&h->pdev->dev, "out of memory in "
1853 "adjust_hpsa_scsi_table\n");
1854 goto free_and_out;
1855 }
1856
1857 spin_lock_irqsave(&h->devlock, flags);
1858
1859 /* find any devices in h->dev[] that are not in
1860 * sd[] and remove them from h->dev[], and for any
1861 * devices which have changed, remove the old device
1862 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001863 * If minor device attributes change, just update
1864 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001865 */
1866 i = 0;
1867 nremoved = 0;
1868 nadded = 0;
1869 while (i < h->ndevices) {
1870 csd = h->dev[i];
1871 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1872 if (device_change == DEVICE_NOT_FOUND) {
1873 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001874 hpsa_scsi_remove_entry(h, i, removed, &nremoved);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001875 continue; /* remove ^^^, hence i not incremented */
1876 } else if (device_change == DEVICE_CHANGED) {
1877 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001878 hpsa_scsi_replace_entry(h, i, sd[entry],
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001879 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001880 /* Set it to NULL to prevent it from being freed
1881 * at the bottom of hpsa_update_scsi_devices()
1882 */
1883 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001884 } else if (device_change == DEVICE_UPDATED) {
Don Brace8aa60682015-11-04 15:50:01 -06001885 hpsa_scsi_update_entry(h, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001886 }
1887 i++;
1888 }
1889
1890 /* Now, make sure every device listed in sd[] is also
1891 * listed in h->dev[], adding them if they aren't found
1892 */
1893
1894 for (i = 0; i < nsds; i++) {
1895 if (!sd[i]) /* if already added above. */
1896 continue;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001897
1898 /* Don't add devices which are NOT READY, FORMAT IN PROGRESS
1899 * as the SCSI mid-layer does not handle such devices well.
1900 * It relentlessly loops sending TUR at 3Hz, then READ(10)
1901 * at 160Hz, and prevents the system from coming up.
1902 */
1903 if (sd[i]->volume_offline) {
1904 hpsa_show_volume_status(h, sd[i]);
Webb Scales0d96ef52015-04-23 09:31:55 -05001905 hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
Stephen M. Cameron98465902014-02-21 16:25:00 -06001906 continue;
1907 }
1908
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001909 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1910 h->ndevices, &entry);
1911 if (device_change == DEVICE_NOT_FOUND) {
1912 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001913 if (hpsa_scsi_add_entry(h, sd[i], added, &nadded) != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001914 break;
1915 sd[i] = NULL; /* prevent from being freed later. */
1916 } else if (device_change == DEVICE_CHANGED) {
1917 /* should never happen... */
1918 changes++;
1919 dev_warn(&h->pdev->dev,
1920 "device unexpectedly changed.\n");
1921 /* but if it does happen, we just ignore that device */
1922 }
1923 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001924 hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
1925
1926 /* Now that h->dev[]->phys_disk[] is coherent, we can enable
1927 * any logical drives that need it enabled.
1928 */
Don Brace1d33d852015-11-04 15:50:31 -06001929 for (i = 0; i < h->ndevices; i++) {
1930 if (h->dev[i] == NULL)
1931 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001932 h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
Don Brace1d33d852015-11-04 15:50:31 -06001933 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001934
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001935 spin_unlock_irqrestore(&h->devlock, flags);
1936
Stephen M. Cameron98465902014-02-21 16:25:00 -06001937 /* Monitor devices which are in one of several NOT READY states to be
1938 * brought online later. This must be done without holding h->devlock,
1939 * so don't touch h->dev[]
1940 */
1941 for (i = 0; i < nsds; i++) {
1942 if (!sd[i]) /* if already added above. */
1943 continue;
1944 if (sd[i]->volume_offline)
1945 hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
1946 }
1947
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001948 /* Don't notify scsi mid layer of any changes the first time through
1949 * (or if there are no changes) scsi_scan_host will do it later the
1950 * first time through.
1951 */
Don Brace8aa60682015-11-04 15:50:01 -06001952 if (!changes)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001953 goto free_and_out;
1954
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001955 /* Notify scsi mid layer of any removed devices */
1956 for (i = 0; i < nremoved; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001957 if (removed[i] == NULL)
1958 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001959 if (removed[i]->expose_device)
1960 hpsa_remove_device(h, removed[i]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001961 kfree(removed[i]);
1962 removed[i] = NULL;
1963 }
1964
1965 /* Notify scsi mid layer of any added devices */
1966 for (i = 0; i < nadded; i++) {
Kevin Barnett096ccff2015-11-04 15:51:51 -06001967 int rc = 0;
1968
Don Brace1d33d852015-11-04 15:50:31 -06001969 if (added[i] == NULL)
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001970 continue;
Kevin Barnett2a168202015-11-04 15:51:21 -06001971 if (!(added[i]->expose_device))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001972 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001973 rc = hpsa_add_device(h, added[i]);
1974 if (!rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001975 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001976 dev_warn(&h->pdev->dev,
1977 "addition failed %d, device not added.", rc);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001978 /* now we have to remove it from h->dev,
1979 * since it didn't get added to scsi mid layer
1980 */
1981 fixup_botched_add(h, added[i]);
Don Brace853633e2015-11-04 15:50:37 -06001982 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001983 }
1984
1985free_and_out:
1986 kfree(added);
1987 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001988}
1989
1990/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001991 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001992 * Assume's h->devlock is held.
1993 */
1994static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1995 int bus, int target, int lun)
1996{
1997 int i;
1998 struct hpsa_scsi_dev_t *sd;
1999
2000 for (i = 0; i < h->ndevices; i++) {
2001 sd = h->dev[i];
2002 if (sd->bus == bus && sd->target == target && sd->lun == lun)
2003 return sd;
2004 }
2005 return NULL;
2006}
2007
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002008static int hpsa_slave_alloc(struct scsi_device *sdev)
2009{
2010 struct hpsa_scsi_dev_t *sd;
2011 unsigned long flags;
2012 struct ctlr_info *h;
2013
2014 h = sdev_to_hba(sdev);
2015 spin_lock_irqsave(&h->devlock, flags);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06002016 if (sdev_channel(sdev) == HPSA_PHYSICAL_DEVICE_BUS) {
2017 struct scsi_target *starget;
2018 struct sas_rphy *rphy;
2019
2020 starget = scsi_target(sdev);
2021 rphy = target_to_rphy(starget);
2022 sd = hpsa_find_device_by_sas_rphy(h, rphy);
2023 if (sd) {
2024 sd->target = sdev_id(sdev);
2025 sd->lun = sdev->lun;
2026 }
2027 } else
2028 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
2029 sdev_id(sdev), sdev->lun);
2030
2031 if (sd && sd->expose_device) {
Don Brace03383732015-01-23 16:43:30 -06002032 atomic_set(&sd->ioaccel_cmds_out, 0);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06002033 sdev->hostdata = sd;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002034 } else
2035 sdev->hostdata = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002036 spin_unlock_irqrestore(&h->devlock, flags);
2037 return 0;
2038}
2039
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002040/* configure scsi device based on internal per-device structure */
2041static int hpsa_slave_configure(struct scsi_device *sdev)
2042{
2043 struct hpsa_scsi_dev_t *sd;
2044 int queue_depth;
2045
2046 sd = sdev->hostdata;
Kevin Barnett2a168202015-11-04 15:51:21 -06002047 sdev->no_uld_attach = !sd || !sd->expose_device;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002048
2049 if (sd)
2050 queue_depth = sd->queue_depth != 0 ?
2051 sd->queue_depth : sdev->host->can_queue;
2052 else
2053 queue_depth = sdev->host->can_queue;
2054
2055 scsi_change_queue_depth(sdev, queue_depth);
2056
2057 return 0;
2058}
2059
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002060static void hpsa_slave_destroy(struct scsi_device *sdev)
2061{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06002062 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002063}
2064
Webb Scalesd9a729f2015-04-23 09:33:27 -05002065static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
2066{
2067 int i;
2068
2069 if (!h->ioaccel2_cmd_sg_list)
2070 return;
2071 for (i = 0; i < h->nr_cmds; i++) {
2072 kfree(h->ioaccel2_cmd_sg_list[i]);
2073 h->ioaccel2_cmd_sg_list[i] = NULL;
2074 }
2075 kfree(h->ioaccel2_cmd_sg_list);
2076 h->ioaccel2_cmd_sg_list = NULL;
2077}
2078
2079static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
2080{
2081 int i;
2082
2083 if (h->chainsize <= 0)
2084 return 0;
2085
2086 h->ioaccel2_cmd_sg_list =
2087 kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
2088 GFP_KERNEL);
2089 if (!h->ioaccel2_cmd_sg_list)
2090 return -ENOMEM;
2091 for (i = 0; i < h->nr_cmds; i++) {
2092 h->ioaccel2_cmd_sg_list[i] =
2093 kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
2094 h->maxsgentries, GFP_KERNEL);
2095 if (!h->ioaccel2_cmd_sg_list[i])
2096 goto clean;
2097 }
2098 return 0;
2099
2100clean:
2101 hpsa_free_ioaccel2_sg_chain_blocks(h);
2102 return -ENOMEM;
2103}
2104
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002105static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
2106{
2107 int i;
2108
2109 if (!h->cmd_sg_list)
2110 return;
2111 for (i = 0; i < h->nr_cmds; i++) {
2112 kfree(h->cmd_sg_list[i]);
2113 h->cmd_sg_list[i] = NULL;
2114 }
2115 kfree(h->cmd_sg_list);
2116 h->cmd_sg_list = NULL;
2117}
2118
Robert Elliott105a3db2015-04-23 09:33:48 -05002119static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002120{
2121 int i;
2122
2123 if (h->chainsize <= 0)
2124 return 0;
2125
2126 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
2127 GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002128 if (!h->cmd_sg_list) {
2129 dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002130 return -ENOMEM;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002131 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002132 for (i = 0; i < h->nr_cmds; i++) {
2133 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
2134 h->chainsize, GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002135 if (!h->cmd_sg_list[i]) {
2136 dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002137 goto clean;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002138 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002139 }
2140 return 0;
2141
2142clean:
2143 hpsa_free_sg_chain_blocks(h);
2144 return -ENOMEM;
2145}
2146
Webb Scalesd9a729f2015-04-23 09:33:27 -05002147static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
2148 struct io_accel2_cmd *cp, struct CommandList *c)
2149{
2150 struct ioaccel2_sg_element *chain_block;
2151 u64 temp64;
2152 u32 chain_size;
2153
2154 chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
Don Bracea736e9b2015-11-04 15:51:14 -06002155 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002156 temp64 = pci_map_single(h->pdev, chain_block, chain_size,
2157 PCI_DMA_TODEVICE);
2158 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2159 /* prevent subsequent unmapping */
2160 cp->sg->address = 0;
2161 return -1;
2162 }
2163 cp->sg->address = cpu_to_le64(temp64);
2164 return 0;
2165}
2166
2167static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
2168 struct io_accel2_cmd *cp)
2169{
2170 struct ioaccel2_sg_element *chain_sg;
2171 u64 temp64;
2172 u32 chain_size;
2173
2174 chain_sg = cp->sg;
2175 temp64 = le64_to_cpu(chain_sg->address);
Don Bracea736e9b2015-11-04 15:51:14 -06002176 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002177 pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
2178}
2179
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002180static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002181 struct CommandList *c)
2182{
2183 struct SGDescriptor *chain_sg, *chain_block;
2184 u64 temp64;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002185 u32 chain_len;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002186
2187 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
2188 chain_block = h->cmd_sg_list[c->cmdindex];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002189 chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
2190 chain_len = sizeof(*chain_sg) *
Don Brace2b08b3e2015-01-23 16:41:09 -06002191 (le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002192 chain_sg->Len = cpu_to_le32(chain_len);
2193 temp64 = pci_map_single(h->pdev, chain_block, chain_len,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002194 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002195 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2196 /* prevent subsequent unmapping */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002197 chain_sg->Addr = cpu_to_le64(0);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002198 return -1;
2199 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002200 chain_sg->Addr = cpu_to_le64(temp64);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002201 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002202}
2203
2204static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
2205 struct CommandList *c)
2206{
2207 struct SGDescriptor *chain_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002208
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002209 if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002210 return;
2211
2212 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002213 pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
2214 le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002215}
2216
Scott Teela09c1442014-02-18 13:57:21 -06002217
2218/* Decode the various types of errors on ioaccel2 path.
2219 * Return 1 for any error that should generate a RAID path retry.
2220 * Return 0 for errors that don't require a RAID path retry.
2221 */
2222static int handle_ioaccel_mode2_error(struct ctlr_info *h,
Scott Teelc3497752014-02-18 13:56:34 -06002223 struct CommandList *c,
2224 struct scsi_cmnd *cmd,
Don Braceba74fdc2016-04-27 17:14:17 -05002225 struct io_accel2_cmd *c2,
2226 struct hpsa_scsi_dev_t *dev)
Scott Teelc3497752014-02-18 13:56:34 -06002227{
2228 int data_len;
Scott Teela09c1442014-02-18 13:57:21 -06002229 int retry = 0;
Joe Handzikc40820d2015-04-23 09:33:32 -05002230 u32 ioaccel2_resid = 0;
Scott Teelc3497752014-02-18 13:56:34 -06002231
2232 switch (c2->error_data.serv_response) {
2233 case IOACCEL2_SERV_RESPONSE_COMPLETE:
2234 switch (c2->error_data.status) {
2235 case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
2236 break;
2237 case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002238 cmd->result |= SAM_STAT_CHECK_CONDITION;
Scott Teelc3497752014-02-18 13:56:34 -06002239 if (c2->error_data.data_present !=
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002240 IOACCEL2_SENSE_DATA_PRESENT) {
2241 memset(cmd->sense_buffer, 0,
2242 SCSI_SENSE_BUFFERSIZE);
Scott Teelc3497752014-02-18 13:56:34 -06002243 break;
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002244 }
Scott Teelc3497752014-02-18 13:56:34 -06002245 /* copy the sense data */
2246 data_len = c2->error_data.sense_data_len;
2247 if (data_len > SCSI_SENSE_BUFFERSIZE)
2248 data_len = SCSI_SENSE_BUFFERSIZE;
2249 if (data_len > sizeof(c2->error_data.sense_data_buff))
2250 data_len =
2251 sizeof(c2->error_data.sense_data_buff);
2252 memcpy(cmd->sense_buffer,
2253 c2->error_data.sense_data_buff, data_len);
Scott Teela09c1442014-02-18 13:57:21 -06002254 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002255 break;
2256 case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
Scott Teela09c1442014-02-18 13:57:21 -06002257 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002258 break;
2259 case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
Scott Teela09c1442014-02-18 13:57:21 -06002260 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002261 break;
2262 case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
Stephen Cameron4a8da222015-04-23 09:32:43 -05002263 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002264 break;
2265 case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
Scott Teela09c1442014-02-18 13:57:21 -06002266 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002267 break;
2268 default:
Scott Teela09c1442014-02-18 13:57:21 -06002269 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002270 break;
2271 }
2272 break;
2273 case IOACCEL2_SERV_RESPONSE_FAILURE:
Joe Handzikc40820d2015-04-23 09:33:32 -05002274 switch (c2->error_data.status) {
2275 case IOACCEL2_STATUS_SR_IO_ERROR:
2276 case IOACCEL2_STATUS_SR_IO_ABORTED:
2277 case IOACCEL2_STATUS_SR_OVERRUN:
2278 retry = 1;
2279 break;
2280 case IOACCEL2_STATUS_SR_UNDERRUN:
2281 cmd->result = (DID_OK << 16); /* host byte */
2282 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
2283 ioaccel2_resid = get_unaligned_le32(
2284 &c2->error_data.resid_cnt[0]);
2285 scsi_set_resid(cmd, ioaccel2_resid);
2286 break;
2287 case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
2288 case IOACCEL2_STATUS_SR_INVALID_DEVICE:
2289 case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
Don Braceba74fdc2016-04-27 17:14:17 -05002290 /*
2291 * Did an HBA disk disappear? We will eventually
2292 * get a state change event from the controller but
2293 * in the meantime, we need to tell the OS that the
2294 * HBA disk is no longer there and stop I/O
2295 * from going down. This allows the potential re-insert
2296 * of the disk to get the same device node.
2297 */
2298 if (dev->physical_device && dev->expose_device) {
2299 cmd->result = DID_NO_CONNECT << 16;
2300 dev->removed = 1;
2301 h->drv_req_rescan = 1;
2302 dev_warn(&h->pdev->dev,
2303 "%s: device is gone!\n", __func__);
2304 } else
2305 /*
2306 * Retry by sending down the RAID path.
2307 * We will get an event from ctlr to
2308 * trigger rescan regardless.
2309 */
2310 retry = 1;
Joe Handzikc40820d2015-04-23 09:33:32 -05002311 break;
2312 default:
2313 retry = 1;
Joe Handzikc40820d2015-04-23 09:33:32 -05002314 }
Scott Teelc3497752014-02-18 13:56:34 -06002315 break;
2316 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
2317 break;
2318 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
2319 break;
2320 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
Scott Teela09c1442014-02-18 13:57:21 -06002321 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002322 break;
2323 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
Scott Teelc3497752014-02-18 13:56:34 -06002324 break;
2325 default:
Scott Teela09c1442014-02-18 13:57:21 -06002326 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002327 break;
2328 }
Scott Teela09c1442014-02-18 13:57:21 -06002329
2330 return retry; /* retry on raid path? */
Scott Teelc3497752014-02-18 13:56:34 -06002331}
2332
Webb Scalesa58e7e52015-04-23 09:34:16 -05002333static void hpsa_cmd_resolve_events(struct ctlr_info *h,
2334 struct CommandList *c)
2335{
Webb Scalesd604f532015-04-23 09:35:22 -05002336 bool do_wake = false;
2337
Webb Scalesa58e7e52015-04-23 09:34:16 -05002338 /*
2339 * Prevent the following race in the abort handler:
2340 *
2341 * 1. LLD is requested to abort a SCSI command
2342 * 2. The SCSI command completes
2343 * 3. The struct CommandList associated with step 2 is made available
2344 * 4. New I/O request to LLD to another LUN re-uses struct CommandList
2345 * 5. Abort handler follows scsi_cmnd->host_scribble and
2346 * finds struct CommandList and tries to aborts it
2347 * Now we have aborted the wrong command.
2348 *
Webb Scalesd604f532015-04-23 09:35:22 -05002349 * Reset c->scsi_cmd here so that the abort or reset handler will know
2350 * this command has completed. Then, check to see if the handler is
Webb Scalesa58e7e52015-04-23 09:34:16 -05002351 * waiting for this command, and, if so, wake it.
2352 */
2353 c->scsi_cmd = SCSI_CMD_IDLE;
Webb Scalesd604f532015-04-23 09:35:22 -05002354 mb(); /* Declare command idle before checking for pending events. */
Webb Scalesa58e7e52015-04-23 09:34:16 -05002355 if (c->abort_pending) {
Webb Scalesd604f532015-04-23 09:35:22 -05002356 do_wake = true;
Webb Scalesa58e7e52015-04-23 09:34:16 -05002357 c->abort_pending = false;
Webb Scalesa58e7e52015-04-23 09:34:16 -05002358 }
Webb Scalesd604f532015-04-23 09:35:22 -05002359 if (c->reset_pending) {
2360 unsigned long flags;
2361 struct hpsa_scsi_dev_t *dev;
2362
2363 /*
2364 * There appears to be a reset pending; lock the lock and
2365 * reconfirm. If so, then decrement the count of outstanding
2366 * commands and wake the reset command if this is the last one.
2367 */
2368 spin_lock_irqsave(&h->lock, flags);
2369 dev = c->reset_pending; /* Re-fetch under the lock. */
2370 if (dev && atomic_dec_and_test(&dev->reset_cmds_out))
2371 do_wake = true;
2372 c->reset_pending = NULL;
2373 spin_unlock_irqrestore(&h->lock, flags);
2374 }
2375
2376 if (do_wake)
2377 wake_up_all(&h->event_sync_wait_queue);
Webb Scalesa58e7e52015-04-23 09:34:16 -05002378}
2379
Webb Scales73153fe2015-04-23 09:35:04 -05002380static void hpsa_cmd_resolve_and_free(struct ctlr_info *h,
2381 struct CommandList *c)
2382{
2383 hpsa_cmd_resolve_events(h, c);
2384 cmd_tagged_free(h, c);
2385}
2386
Webb Scales8a0ff922015-04-23 09:34:11 -05002387static void hpsa_cmd_free_and_done(struct ctlr_info *h,
2388 struct CommandList *c, struct scsi_cmnd *cmd)
2389{
Webb Scales73153fe2015-04-23 09:35:04 -05002390 hpsa_cmd_resolve_and_free(h, c);
Don Braced49c2072016-09-09 16:30:23 -05002391 if (cmd && cmd->scsi_done)
2392 cmd->scsi_done(cmd);
Webb Scales8a0ff922015-04-23 09:34:11 -05002393}
2394
2395static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
2396{
2397 INIT_WORK(&c->work, hpsa_command_resubmit_worker);
2398 queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
2399}
2400
Webb Scalesa58e7e52015-04-23 09:34:16 -05002401static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
2402{
2403 cmd->result = DID_ABORT << 16;
2404}
2405
2406static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
2407 struct scsi_cmnd *cmd)
2408{
2409 hpsa_set_scsi_cmd_aborted(cmd);
2410 dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2411 c->Request.CDB, c->err_info->ScsiStatus);
Webb Scales73153fe2015-04-23 09:35:04 -05002412 hpsa_cmd_resolve_and_free(h, c);
Webb Scalesa58e7e52015-04-23 09:34:16 -05002413}
2414
Scott Teelc3497752014-02-18 13:56:34 -06002415static void process_ioaccel2_completion(struct ctlr_info *h,
2416 struct CommandList *c, struct scsi_cmnd *cmd,
2417 struct hpsa_scsi_dev_t *dev)
2418{
2419 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2420
2421 /* check for good status */
2422 if (likely(c2->error_data.serv_response == 0 &&
Webb Scales8a0ff922015-04-23 09:34:11 -05002423 c2->error_data.status == 0))
2424 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002425
Webb Scales8a0ff922015-04-23 09:34:11 -05002426 /*
2427 * Any RAID offload error results in retry which will use
Scott Teelc3497752014-02-18 13:56:34 -06002428 * the normal I/O path so the controller can handle whatever's
2429 * wrong.
2430 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002431 if (is_logical_device(dev) &&
Scott Teelc3497752014-02-18 13:56:34 -06002432 c2->error_data.serv_response ==
2433 IOACCEL2_SERV_RESPONSE_FAILURE) {
Don Brace080ef1c2015-01-23 16:43:25 -06002434 if (c2->error_data.status ==
Don Brace064d1b12016-04-27 17:14:07 -05002435 IOACCEL2_STATUS_SR_IOACCEL_DISABLED) {
Don Brace080ef1c2015-01-23 16:43:25 -06002436 dev->offload_enabled = 0;
Don Brace064d1b12016-04-27 17:14:07 -05002437 dev->offload_to_be_enabled = 0;
2438 }
Webb Scales8a0ff922015-04-23 09:34:11 -05002439
2440 return hpsa_retry_cmd(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06002441 }
Don Brace080ef1c2015-01-23 16:43:25 -06002442
Don Braceba74fdc2016-04-27 17:14:17 -05002443 if (handle_ioaccel_mode2_error(h, c, cmd, c2, dev))
Webb Scales8a0ff922015-04-23 09:34:11 -05002444 return hpsa_retry_cmd(h, c);
Don Brace080ef1c2015-01-23 16:43:25 -06002445
Webb Scales8a0ff922015-04-23 09:34:11 -05002446 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002447}
2448
Stephen Cameron9437ac42015-04-23 09:32:16 -05002449/* Returns 0 on success, < 0 otherwise. */
2450static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
2451 struct CommandList *cp)
2452{
2453 u8 tmf_status = cp->err_info->ScsiStatus;
2454
2455 switch (tmf_status) {
2456 case CISS_TMF_COMPLETE:
2457 /*
2458 * CISS_TMF_COMPLETE never happens, instead,
2459 * ei->CommandStatus == 0 for this case.
2460 */
2461 case CISS_TMF_SUCCESS:
2462 return 0;
2463 case CISS_TMF_INVALID_FRAME:
2464 case CISS_TMF_NOT_SUPPORTED:
2465 case CISS_TMF_FAILED:
2466 case CISS_TMF_WRONG_LUN:
2467 case CISS_TMF_OVERLAPPED_TAG:
2468 break;
2469 default:
2470 dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
2471 tmf_status);
2472 break;
2473 }
2474 return -tmf_status;
2475}
2476
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05002477static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002478{
2479 struct scsi_cmnd *cmd;
2480 struct ctlr_info *h;
2481 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002482 struct hpsa_scsi_dev_t *dev;
Webb Scalesd9a729f2015-04-23 09:33:27 -05002483 struct io_accel2_cmd *c2;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002484
Stephen Cameron9437ac42015-04-23 09:32:16 -05002485 u8 sense_key;
2486 u8 asc; /* additional sense code */
2487 u8 ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05002488 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002489
2490 ei = cp->err_info;
Stephen Cameron7fa30302015-01-23 16:44:30 -06002491 cmd = cp->scsi_cmd;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002492 h = cp->h;
Don Braced49c2072016-09-09 16:30:23 -05002493
2494 if (!cmd->device) {
2495 cmd->result = DID_NO_CONNECT << 16;
2496 return hpsa_cmd_free_and_done(h, cp, cmd);
2497 }
2498
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002499 dev = cmd->device->hostdata;
Webb Scalesd9a729f2015-04-23 09:33:27 -05002500 c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002501
2502 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06002503 if ((cp->cmd_type == CMD_SCSI) &&
Don Brace2b08b3e2015-01-23 16:41:09 -06002504 (le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002505 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002506
Webb Scalesd9a729f2015-04-23 09:33:27 -05002507 if ((cp->cmd_type == CMD_IOACCEL2) &&
2508 (c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2509 hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2510
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002511 cmd->result = (DID_OK << 16); /* host byte */
2512 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06002513
Don Braced49c2072016-09-09 16:30:23 -05002514 if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1) {
2515 if (dev->physical_device && dev->expose_device &&
2516 dev->removed) {
2517 cmd->result = DID_NO_CONNECT << 16;
2518 return hpsa_cmd_free_and_done(h, cp, cmd);
2519 }
2520 if (likely(cp->phys_disk != NULL))
2521 atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
2522 }
Don Brace03383732015-01-23 16:43:30 -06002523
Webb Scales25163bd2015-04-23 09:32:00 -05002524 /*
2525 * We check for lockup status here as it may be set for
2526 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
2527 * fail_all_oustanding_cmds()
2528 */
2529 if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
2530 /* DID_NO_CONNECT will prevent a retry */
2531 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05002532 return hpsa_cmd_free_and_done(h, cp, cmd);
Webb Scales25163bd2015-04-23 09:32:00 -05002533 }
2534
Webb Scalesd604f532015-04-23 09:35:22 -05002535 if ((unlikely(hpsa_is_pending_event(cp)))) {
2536 if (cp->reset_pending)
2537 return hpsa_cmd_resolve_and_free(h, cp);
2538 if (cp->abort_pending)
2539 return hpsa_cmd_abort_and_free(h, cp, cmd);
2540 }
2541
Scott Teelc3497752014-02-18 13:56:34 -06002542 if (cp->cmd_type == CMD_IOACCEL2)
2543 return process_ioaccel2_completion(h, cp, cmd, dev);
2544
Robert Elliott6aa4c362014-07-03 10:18:19 -05002545 scsi_set_resid(cmd, ei->ResidualCnt);
Webb Scales8a0ff922015-04-23 09:34:11 -05002546 if (ei->CommandStatus == 0)
2547 return hpsa_cmd_free_and_done(h, cp, cmd);
Robert Elliott6aa4c362014-07-03 10:18:19 -05002548
Matt Gatese1f7de02014-02-18 13:55:17 -06002549 /* For I/O accelerator commands, copy over some fields to the normal
2550 * CISS header used below for error handling.
2551 */
2552 if (cp->cmd_type == CMD_IOACCEL1) {
2553 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06002554 cp->Header.SGList = scsi_sg_count(cmd);
2555 cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
2556 cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
2557 IOACCEL1_IOFLAGS_CDBLEN_MASK;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002558 cp->Header.tag = c->tag;
Matt Gatese1f7de02014-02-18 13:55:17 -06002559 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2560 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002561
2562 /* Any RAID offload error results in retry which will use
2563 * the normal I/O path so the controller can handle whatever's
2564 * wrong.
2565 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002566 if (is_logical_device(dev)) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002567 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2568 dev->offload_enabled = 0;
Webb Scalesd604f532015-04-23 09:35:22 -05002569 return hpsa_retry_cmd(h, cp);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002570 }
Matt Gatese1f7de02014-02-18 13:55:17 -06002571 }
2572
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002573 /* an error has occurred */
2574 switch (ei->CommandStatus) {
2575
2576 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002577 cmd->result |= ei->ScsiStatus;
2578 /* copy the sense data */
2579 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
2580 sense_data_size = SCSI_SENSE_BUFFERSIZE;
2581 else
2582 sense_data_size = sizeof(ei->SenseInfo);
2583 if (ei->SenseLen < sense_data_size)
2584 sense_data_size = ei->SenseLen;
2585 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
2586 if (ei->ScsiStatus)
2587 decode_sense_data(ei->SenseInfo, sense_data_size,
2588 &sense_key, &asc, &ascq);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002589 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates1d3b3602010-02-04 08:43:00 -06002590 if (sense_key == ABORTED_COMMAND) {
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05002591 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06002592 break;
2593 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002594 break;
2595 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002596 /* Problem was not a check condition
2597 * Pass it up to the upper layers...
2598 */
2599 if (ei->ScsiStatus) {
2600 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2601 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2602 "Returning result: 0x%x\n",
2603 cp, ei->ScsiStatus,
2604 sense_key, asc, ascq,
2605 cmd->result);
2606 } else { /* scsi status is zero??? How??? */
2607 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2608 "Returning no connection.\n", cp),
2609
2610 /* Ordinarily, this case should never happen,
2611 * but there is a bug in some released firmware
2612 * revisions that allows it to happen if, for
2613 * example, a 4100 backplane loses power and
2614 * the tape drive is in it. We assume that
2615 * it's a fatal error of some kind because we
2616 * can't show that it wasn't. We will make it
2617 * look like selection timeout since that is
2618 * the most common reason for this to occur,
2619 * and it's severe enough.
2620 */
2621
2622 cmd->result = DID_NO_CONNECT << 16;
2623 }
2624 break;
2625
2626 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2627 break;
2628 case CMD_DATA_OVERRUN:
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002629 dev_warn(&h->pdev->dev,
2630 "CDB %16phN data overrun\n", cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002631 break;
2632 case CMD_INVALID: {
2633 /* print_bytes(cp, sizeof(*cp), 1, 0);
2634 print_cmd(cp); */
2635 /* We get CMD_INVALID if you address a non-existent device
2636 * instead of a selection timeout (no response). You will
2637 * see this if you yank out a drive, then try to access it.
2638 * This is kind of a shame because it means that any other
2639 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2640 * missing target. */
2641 cmd->result = DID_NO_CONNECT << 16;
2642 }
2643 break;
2644 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05002645 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002646 dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2647 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002648 break;
2649 case CMD_HARDWARE_ERR:
2650 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002651 dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2652 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002653 break;
2654 case CMD_CONNECTION_LOST:
2655 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002656 dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2657 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002658 break;
2659 case CMD_ABORTED:
Webb Scalesa58e7e52015-04-23 09:34:16 -05002660 /* Return now to avoid calling scsi_done(). */
2661 return hpsa_cmd_abort_and_free(h, cp, cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002662 case CMD_ABORT_FAILED:
2663 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002664 dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2665 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002666 break;
2667 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05002668 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002669 dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2670 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002671 break;
2672 case CMD_TIMEOUT:
2673 cmd->result = DID_TIME_OUT << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002674 dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2675 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002676 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002677 case CMD_UNABORTABLE:
2678 cmd->result = DID_ERROR << 16;
2679 dev_warn(&h->pdev->dev, "Command unabortable\n");
2680 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002681 case CMD_TMF_STATUS:
2682 if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
2683 cmd->result = DID_ERROR << 16;
2684 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002685 case CMD_IOACCEL_DISABLED:
2686 /* This only handles the direct pass-through case since RAID
2687 * offload is handled above. Just attempt a retry.
2688 */
2689 cmd->result = DID_SOFT_ERROR << 16;
2690 dev_warn(&h->pdev->dev,
2691 "cp %p had HP SSD Smart Path error\n", cp);
2692 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002693 default:
2694 cmd->result = DID_ERROR << 16;
2695 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2696 cp, ei->CommandStatus);
2697 }
Webb Scales8a0ff922015-04-23 09:34:11 -05002698
2699 return hpsa_cmd_free_and_done(h, cp, cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002700}
2701
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002702static void hpsa_pci_unmap(struct pci_dev *pdev,
2703 struct CommandList *c, int sg_used, int data_direction)
2704{
2705 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002706
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002707 for (i = 0; i < sg_used; i++)
2708 pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
2709 le32_to_cpu(c->SG[i].Len),
2710 data_direction);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002711}
2712
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002713static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002714 struct CommandList *cp,
2715 unsigned char *buf,
2716 size_t buflen,
2717 int data_direction)
2718{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002719 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002720
2721 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2722 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002723 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002724 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002725 }
2726
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002727 addr64 = pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06002728 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002729 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06002730 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002731 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002732 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06002733 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002734 cp->SG[0].Addr = cpu_to_le64(addr64);
2735 cp->SG[0].Len = cpu_to_le32(buflen);
2736 cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
2737 cp->Header.SGList = 1; /* no. SGs contig in this cmd */
2738 cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002739 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002740}
2741
Webb Scales25163bd2015-04-23 09:32:00 -05002742#define NO_TIMEOUT ((unsigned long) -1)
2743#define DEFAULT_TIMEOUT 30000 /* milliseconds */
2744static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
2745 struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002746{
2747 DECLARE_COMPLETION_ONSTACK(wait);
2748
2749 c->waiting = &wait;
Webb Scales25163bd2015-04-23 09:32:00 -05002750 __enqueue_cmd_and_start_io(h, c, reply_queue);
2751 if (timeout_msecs == NO_TIMEOUT) {
2752 /* TODO: get rid of this no-timeout thing */
2753 wait_for_completion_io(&wait);
2754 return IO_OK;
2755 }
2756 if (!wait_for_completion_io_timeout(&wait,
2757 msecs_to_jiffies(timeout_msecs))) {
2758 dev_warn(&h->pdev->dev, "Command timed out.\n");
2759 return -ETIMEDOUT;
2760 }
2761 return IO_OK;
2762}
2763
2764static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
2765 int reply_queue, unsigned long timeout_msecs)
2766{
2767 if (unlikely(lockup_detected(h))) {
2768 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
2769 return IO_OK;
2770 }
2771 return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002772}
2773
Stephen M. Cameron094963d2014-05-29 10:53:18 -05002774static u32 lockup_detected(struct ctlr_info *h)
2775{
2776 int cpu;
2777 u32 rc, *lockup_detected;
2778
2779 cpu = get_cpu();
2780 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2781 rc = *lockup_detected;
2782 put_cpu();
2783 return rc;
2784}
2785
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002786#define MAX_DRIVER_CMD_RETRIES 25
Webb Scales25163bd2015-04-23 09:32:00 -05002787static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
2788 struct CommandList *c, int data_direction, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002789{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002790 int backoff_time = 10, retry_count = 0;
Webb Scales25163bd2015-04-23 09:32:00 -05002791 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002792
2793 do {
Joe Perches7630abd2011-05-08 23:32:40 -07002794 memset(c->err_info, 0, sizeof(*c->err_info));
Webb Scales25163bd2015-04-23 09:32:00 -05002795 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
2796 timeout_msecs);
2797 if (rc)
2798 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002799 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002800 if (retry_count > 3) {
2801 msleep(backoff_time);
2802 if (backoff_time < 1000)
2803 backoff_time *= 2;
2804 }
Matt Bondurant852af202012-05-01 11:42:35 -05002805 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002806 check_for_busy(h, c)) &&
2807 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002808 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
Webb Scales25163bd2015-04-23 09:32:00 -05002809 if (retry_count > MAX_DRIVER_CMD_RETRIES)
2810 rc = -EIO;
2811 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002812}
2813
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002814static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2815 struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002816{
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002817 const u8 *cdb = c->Request.CDB;
2818 const u8 *lun = c->Header.LUN.LunAddrBytes;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002819
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002820 dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2821 " CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2822 txt, lun[0], lun[1], lun[2], lun[3],
2823 lun[4], lun[5], lun[6], lun[7],
2824 cdb[0], cdb[1], cdb[2], cdb[3],
2825 cdb[4], cdb[5], cdb[6], cdb[7],
2826 cdb[8], cdb[9], cdb[10], cdb[11],
2827 cdb[12], cdb[13], cdb[14], cdb[15]);
2828}
2829
2830static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2831 struct CommandList *cp)
2832{
2833 const struct ErrorInfo *ei = cp->err_info;
2834 struct device *d = &cp->h->pdev->dev;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002835 u8 sense_key, asc, ascq;
2836 int sense_len;
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002837
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002838 switch (ei->CommandStatus) {
2839 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002840 if (ei->SenseLen > sizeof(ei->SenseInfo))
2841 sense_len = sizeof(ei->SenseInfo);
2842 else
2843 sense_len = ei->SenseLen;
2844 decode_sense_data(ei->SenseInfo, sense_len,
2845 &sense_key, &asc, &ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002846 hpsa_print_cmd(h, "SCSI status", cp);
2847 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
Stephen Cameron9437ac42015-04-23 09:32:16 -05002848 dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
2849 sense_key, asc, ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002850 else
Stephen Cameron9437ac42015-04-23 09:32:16 -05002851 dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002852 if (ei->ScsiStatus == 0)
2853 dev_warn(d, "SCSI status is abnormally zero. "
2854 "(probably indicates selection timeout "
2855 "reported incorrectly due to a known "
2856 "firmware bug, circa July, 2001.)\n");
2857 break;
2858 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002859 break;
2860 case CMD_DATA_OVERRUN:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002861 hpsa_print_cmd(h, "overrun condition", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002862 break;
2863 case CMD_INVALID: {
2864 /* controller unfortunately reports SCSI passthru's
2865 * to non-existent targets as invalid commands.
2866 */
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002867 hpsa_print_cmd(h, "invalid command", cp);
2868 dev_warn(d, "probably means device no longer present\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002869 }
2870 break;
2871 case CMD_PROTOCOL_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002872 hpsa_print_cmd(h, "protocol error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002873 break;
2874 case CMD_HARDWARE_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002875 hpsa_print_cmd(h, "hardware error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002876 break;
2877 case CMD_CONNECTION_LOST:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002878 hpsa_print_cmd(h, "connection lost", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002879 break;
2880 case CMD_ABORTED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002881 hpsa_print_cmd(h, "aborted", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002882 break;
2883 case CMD_ABORT_FAILED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002884 hpsa_print_cmd(h, "abort failed", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002885 break;
2886 case CMD_UNSOLICITED_ABORT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002887 hpsa_print_cmd(h, "unsolicited abort", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002888 break;
2889 case CMD_TIMEOUT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002890 hpsa_print_cmd(h, "timed out", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002891 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002892 case CMD_UNABORTABLE:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002893 hpsa_print_cmd(h, "unabortable", cp);
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002894 break;
Webb Scales25163bd2015-04-23 09:32:00 -05002895 case CMD_CTLR_LOCKUP:
2896 hpsa_print_cmd(h, "controller lockup detected", cp);
2897 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002898 default:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002899 hpsa_print_cmd(h, "unknown status", cp);
2900 dev_warn(d, "Unknown command status %x\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002901 ei->CommandStatus);
2902 }
2903}
2904
2905static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002906 u16 page, unsigned char *buf,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002907 unsigned char bufsize)
2908{
2909 int rc = IO_OK;
2910 struct CommandList *c;
2911 struct ErrorInfo *ei;
2912
Stephen Cameron45fcb862015-01-23 16:43:04 -06002913 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002914
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002915 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2916 page, scsi3addr, TYPE_CMD)) {
2917 rc = -1;
2918 goto out;
2919 }
Webb Scales25163bd2015-04-23 09:32:00 -05002920 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05002921 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05002922 if (rc)
2923 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002924 ei = c->err_info;
2925 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002926 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002927 rc = -1;
2928 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002929out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002930 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002931 return rc;
2932}
2933
Scott Teelbf711ac2014-02-18 13:56:39 -06002934static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Webb Scales25163bd2015-04-23 09:32:00 -05002935 u8 reset_type, int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002936{
2937 int rc = IO_OK;
2938 struct CommandList *c;
2939 struct ErrorInfo *ei;
2940
Stephen Cameron45fcb862015-01-23 16:43:04 -06002941 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002942
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002943
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002944 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teel0b9b7b62015-11-04 15:51:02 -06002945 (void) fill_cmd(c, reset_type, h, NULL, 0, 0,
Scott Teelbf711ac2014-02-18 13:56:39 -06002946 scsi3addr, TYPE_MSG);
Don Bracec448ecf2016-04-27 17:13:51 -05002947 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05002948 if (rc) {
2949 dev_warn(&h->pdev->dev, "Failed to send reset command\n");
2950 goto out;
2951 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002952 /* no unmap needed here because no data xfer. */
2953
2954 ei = c->err_info;
2955 if (ei->CommandStatus != 0) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002956 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002957 rc = -1;
2958 }
Webb Scales25163bd2015-04-23 09:32:00 -05002959out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002960 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002961 return rc;
2962}
2963
Webb Scalesd604f532015-04-23 09:35:22 -05002964static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
2965 struct hpsa_scsi_dev_t *dev,
2966 unsigned char *scsi3addr)
2967{
2968 int i;
2969 bool match = false;
2970 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2971 struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
2972
2973 if (hpsa_is_cmd_idle(c))
2974 return false;
2975
2976 switch (c->cmd_type) {
2977 case CMD_SCSI:
2978 case CMD_IOCTL_PEND:
2979 match = !memcmp(scsi3addr, &c->Header.LUN.LunAddrBytes,
2980 sizeof(c->Header.LUN.LunAddrBytes));
2981 break;
2982
2983 case CMD_IOACCEL1:
2984 case CMD_IOACCEL2:
2985 if (c->phys_disk == dev) {
2986 /* HBA mode match */
2987 match = true;
2988 } else {
2989 /* Possible RAID mode -- check each phys dev. */
2990 /* FIXME: Do we need to take out a lock here? If
2991 * so, we could just call hpsa_get_pdisk_of_ioaccel2()
2992 * instead. */
2993 for (i = 0; i < dev->nphysical_disks && !match; i++) {
2994 /* FIXME: an alternate test might be
2995 *
2996 * match = dev->phys_disk[i]->ioaccel_handle
2997 * == c2->scsi_nexus; */
2998 match = dev->phys_disk[i] == c->phys_disk;
2999 }
3000 }
3001 break;
3002
3003 case IOACCEL2_TMF:
3004 for (i = 0; i < dev->nphysical_disks && !match; i++) {
3005 match = dev->phys_disk[i]->ioaccel_handle ==
3006 le32_to_cpu(ac->it_nexus);
3007 }
3008 break;
3009
3010 case 0: /* The command is in the middle of being initialized. */
3011 match = false;
3012 break;
3013
3014 default:
3015 dev_err(&h->pdev->dev, "unexpected cmd_type: %d\n",
3016 c->cmd_type);
3017 BUG();
3018 }
3019
3020 return match;
3021}
3022
3023static int hpsa_do_reset(struct ctlr_info *h, struct hpsa_scsi_dev_t *dev,
3024 unsigned char *scsi3addr, u8 reset_type, int reply_queue)
3025{
3026 int i;
3027 int rc = 0;
3028
3029 /* We can really only handle one reset at a time */
3030 if (mutex_lock_interruptible(&h->reset_mutex) == -EINTR) {
3031 dev_warn(&h->pdev->dev, "concurrent reset wait interrupted.\n");
3032 return -EINTR;
3033 }
3034
3035 BUG_ON(atomic_read(&dev->reset_cmds_out) != 0);
3036
3037 for (i = 0; i < h->nr_cmds; i++) {
3038 struct CommandList *c = h->cmd_pool + i;
3039 int refcount = atomic_inc_return(&c->refcount);
3040
3041 if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev, scsi3addr)) {
3042 unsigned long flags;
3043
3044 /*
3045 * Mark the target command as having a reset pending,
3046 * then lock a lock so that the command cannot complete
3047 * while we're considering it. If the command is not
3048 * idle then count it; otherwise revoke the event.
3049 */
3050 c->reset_pending = dev;
3051 spin_lock_irqsave(&h->lock, flags); /* Implied MB */
3052 if (!hpsa_is_cmd_idle(c))
3053 atomic_inc(&dev->reset_cmds_out);
3054 else
3055 c->reset_pending = NULL;
3056 spin_unlock_irqrestore(&h->lock, flags);
3057 }
3058
3059 cmd_free(h, c);
3060 }
3061
3062 rc = hpsa_send_reset(h, scsi3addr, reset_type, reply_queue);
3063 if (!rc)
3064 wait_event(h->event_sync_wait_queue,
3065 atomic_read(&dev->reset_cmds_out) == 0 ||
3066 lockup_detected(h));
3067
3068 if (unlikely(lockup_detected(h))) {
Don Brace77678d32015-07-18 11:12:22 -05003069 dev_warn(&h->pdev->dev,
3070 "Controller lockup detected during reset wait\n");
3071 rc = -ENODEV;
3072 }
Webb Scalesd604f532015-04-23 09:35:22 -05003073
3074 if (unlikely(rc))
3075 atomic_set(&dev->reset_cmds_out, 0);
3076
3077 mutex_unlock(&h->reset_mutex);
3078 return rc;
3079}
3080
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003081static void hpsa_get_raid_level(struct ctlr_info *h,
3082 unsigned char *scsi3addr, unsigned char *raid_level)
3083{
3084 int rc;
3085 unsigned char *buf;
3086
3087 *raid_level = RAID_UNKNOWN;
3088 buf = kzalloc(64, GFP_KERNEL);
3089 if (!buf)
3090 return;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003091 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003092 if (rc == 0)
3093 *raid_level = buf[8];
3094 if (*raid_level > RAID_UNKNOWN)
3095 *raid_level = RAID_UNKNOWN;
3096 kfree(buf);
3097 return;
3098}
3099
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003100#define HPSA_MAP_DEBUG
3101#ifdef HPSA_MAP_DEBUG
3102static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
3103 struct raid_map_data *map_buff)
3104{
3105 struct raid_map_disk_data *dd = &map_buff->data[0];
3106 int map, row, col;
3107 u16 map_cnt, row_cnt, disks_per_row;
3108
3109 if (rc != 0)
3110 return;
3111
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06003112 /* Show details only if debugging has been activated. */
3113 if (h->raid_offload_debug < 2)
3114 return;
3115
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003116 dev_info(&h->pdev->dev, "structure_size = %u\n",
3117 le32_to_cpu(map_buff->structure_size));
3118 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
3119 le32_to_cpu(map_buff->volume_blk_size));
3120 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
3121 le64_to_cpu(map_buff->volume_blk_cnt));
3122 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
3123 map_buff->phys_blk_shift);
3124 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
3125 map_buff->parity_rotation_shift);
3126 dev_info(&h->pdev->dev, "strip_size = %u\n",
3127 le16_to_cpu(map_buff->strip_size));
3128 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
3129 le64_to_cpu(map_buff->disk_starting_blk));
3130 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
3131 le64_to_cpu(map_buff->disk_blk_cnt));
3132 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
3133 le16_to_cpu(map_buff->data_disks_per_row));
3134 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
3135 le16_to_cpu(map_buff->metadata_disks_per_row));
3136 dev_info(&h->pdev->dev, "row_cnt = %u\n",
3137 le16_to_cpu(map_buff->row_cnt));
3138 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
3139 le16_to_cpu(map_buff->layout_map_count));
Don Brace2b08b3e2015-01-23 16:41:09 -06003140 dev_info(&h->pdev->dev, "flags = 0x%x\n",
Scott Teeldd0e19f2014-02-18 13:57:31 -06003141 le16_to_cpu(map_buff->flags));
Don Brace2b08b3e2015-01-23 16:41:09 -06003142 dev_info(&h->pdev->dev, "encrypytion = %s\n",
3143 le16_to_cpu(map_buff->flags) &
3144 RAID_MAP_FLAG_ENCRYPT_ON ? "ON" : "OFF");
Scott Teeldd0e19f2014-02-18 13:57:31 -06003145 dev_info(&h->pdev->dev, "dekindex = %u\n",
3146 le16_to_cpu(map_buff->dekindex));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003147 map_cnt = le16_to_cpu(map_buff->layout_map_count);
3148 for (map = 0; map < map_cnt; map++) {
3149 dev_info(&h->pdev->dev, "Map%u:\n", map);
3150 row_cnt = le16_to_cpu(map_buff->row_cnt);
3151 for (row = 0; row < row_cnt; row++) {
3152 dev_info(&h->pdev->dev, " Row%u:\n", row);
3153 disks_per_row =
3154 le16_to_cpu(map_buff->data_disks_per_row);
3155 for (col = 0; col < disks_per_row; col++, dd++)
3156 dev_info(&h->pdev->dev,
3157 " D%02u: h=0x%04x xor=%u,%u\n",
3158 col, dd->ioaccel_handle,
3159 dd->xor_mult[0], dd->xor_mult[1]);
3160 disks_per_row =
3161 le16_to_cpu(map_buff->metadata_disks_per_row);
3162 for (col = 0; col < disks_per_row; col++, dd++)
3163 dev_info(&h->pdev->dev,
3164 " M%02u: h=0x%04x xor=%u,%u\n",
3165 col, dd->ioaccel_handle,
3166 dd->xor_mult[0], dd->xor_mult[1]);
3167 }
3168 }
3169}
3170#else
3171static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
3172 __attribute__((unused)) int rc,
3173 __attribute__((unused)) struct raid_map_data *map_buff)
3174{
3175}
3176#endif
3177
3178static int hpsa_get_raid_map(struct ctlr_info *h,
3179 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3180{
3181 int rc = 0;
3182 struct CommandList *c;
3183 struct ErrorInfo *ei;
3184
Stephen Cameron45fcb862015-01-23 16:43:04 -06003185 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003186
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003187 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
3188 sizeof(this_device->raid_map), 0,
3189 scsi3addr, TYPE_CMD)) {
Robert Elliott2dd02d72015-04-23 09:33:43 -05003190 dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
3191 cmd_free(h, c);
3192 return -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003193 }
Webb Scales25163bd2015-04-23 09:32:00 -05003194 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003195 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003196 if (rc)
3197 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003198 ei = c->err_info;
3199 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003200 hpsa_scsi_interpret_error(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05003201 rc = -1;
3202 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003203 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06003204 cmd_free(h, c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003205
3206 /* @todo in the future, dynamically allocate RAID map memory */
3207 if (le32_to_cpu(this_device->raid_map.structure_size) >
3208 sizeof(this_device->raid_map)) {
3209 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
3210 rc = -1;
3211 }
3212 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
3213 return rc;
Webb Scales25163bd2015-04-23 09:32:00 -05003214out:
3215 cmd_free(h, c);
3216 return rc;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003217}
3218
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003219static int hpsa_bmic_sense_subsystem_information(struct ctlr_info *h,
3220 unsigned char scsi3addr[], u16 bmic_device_index,
3221 struct bmic_sense_subsystem_info *buf, size_t bufsize)
3222{
3223 int rc = IO_OK;
3224 struct CommandList *c;
3225 struct ErrorInfo *ei;
3226
3227 c = cmd_alloc(h);
3228
3229 rc = fill_cmd(c, BMIC_SENSE_SUBSYSTEM_INFORMATION, h, buf, bufsize,
3230 0, RAID_CTLR_LUNID, TYPE_CMD);
3231 if (rc)
3232 goto out;
3233
3234 c->Request.CDB[2] = bmic_device_index & 0xff;
3235 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
3236
3237 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003238 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003239 if (rc)
3240 goto out;
3241 ei = c->err_info;
3242 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3243 hpsa_scsi_interpret_error(h, c);
3244 rc = -1;
3245 }
3246out:
3247 cmd_free(h, c);
3248 return rc;
3249}
3250
Scott Teel66749d02015-11-04 15:51:57 -06003251static int hpsa_bmic_id_controller(struct ctlr_info *h,
3252 struct bmic_identify_controller *buf, size_t bufsize)
3253{
3254 int rc = IO_OK;
3255 struct CommandList *c;
3256 struct ErrorInfo *ei;
3257
3258 c = cmd_alloc(h);
3259
3260 rc = fill_cmd(c, BMIC_IDENTIFY_CONTROLLER, h, buf, bufsize,
3261 0, RAID_CTLR_LUNID, TYPE_CMD);
3262 if (rc)
3263 goto out;
3264
3265 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003266 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teel66749d02015-11-04 15:51:57 -06003267 if (rc)
3268 goto out;
3269 ei = c->err_info;
3270 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3271 hpsa_scsi_interpret_error(h, c);
3272 rc = -1;
3273 }
3274out:
3275 cmd_free(h, c);
3276 return rc;
3277}
3278
Don Brace03383732015-01-23 16:43:30 -06003279static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
3280 unsigned char scsi3addr[], u16 bmic_device_index,
3281 struct bmic_identify_physical_device *buf, size_t bufsize)
3282{
3283 int rc = IO_OK;
3284 struct CommandList *c;
3285 struct ErrorInfo *ei;
3286
3287 c = cmd_alloc(h);
3288 rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
3289 0, RAID_CTLR_LUNID, TYPE_CMD);
3290 if (rc)
3291 goto out;
3292
3293 c->Request.CDB[2] = bmic_device_index & 0xff;
3294 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
3295
Webb Scales25163bd2015-04-23 09:32:00 -05003296 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
Don Bracec448ecf2016-04-27 17:13:51 -05003297 DEFAULT_TIMEOUT);
Don Brace03383732015-01-23 16:43:30 -06003298 ei = c->err_info;
3299 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3300 hpsa_scsi_interpret_error(h, c);
3301 rc = -1;
3302 }
3303out:
3304 cmd_free(h, c);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003305
Don Brace03383732015-01-23 16:43:30 -06003306 return rc;
3307}
3308
Don Bracecca8f132015-12-22 10:36:48 -06003309/*
3310 * get enclosure information
3311 * struct ReportExtendedLUNdata *rlep - Used for BMIC drive number
3312 * struct hpsa_scsi_dev_t *encl_dev - device entry for enclosure
3313 * Uses id_physical_device to determine the box_index.
3314 */
3315static void hpsa_get_enclosure_info(struct ctlr_info *h,
3316 unsigned char *scsi3addr,
3317 struct ReportExtendedLUNdata *rlep, int rle_index,
3318 struct hpsa_scsi_dev_t *encl_dev)
3319{
3320 int rc = -1;
3321 struct CommandList *c = NULL;
3322 struct ErrorInfo *ei = NULL;
3323 struct bmic_sense_storage_box_params *bssbp = NULL;
3324 struct bmic_identify_physical_device *id_phys = NULL;
3325 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
3326 u16 bmic_device_index = 0;
3327
3328 bmic_device_index = GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]);
3329
Don Brace17a9e542016-02-23 15:16:09 -06003330 if (bmic_device_index == 0xFF00 || MASKED_DEVICE(&rle->lunid[0])) {
3331 rc = IO_OK;
Don Bracecca8f132015-12-22 10:36:48 -06003332 goto out;
Don Brace17a9e542016-02-23 15:16:09 -06003333 }
Don Bracecca8f132015-12-22 10:36:48 -06003334
3335 bssbp = kzalloc(sizeof(*bssbp), GFP_KERNEL);
3336 if (!bssbp)
3337 goto out;
3338
3339 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3340 if (!id_phys)
3341 goto out;
3342
3343 rc = hpsa_bmic_id_physical_device(h, scsi3addr, bmic_device_index,
3344 id_phys, sizeof(*id_phys));
3345 if (rc) {
3346 dev_warn(&h->pdev->dev, "%s: id_phys failed %d bdi[0x%x]\n",
3347 __func__, encl_dev->external, bmic_device_index);
3348 goto out;
3349 }
3350
3351 c = cmd_alloc(h);
3352
3353 rc = fill_cmd(c, BMIC_SENSE_STORAGE_BOX_PARAMS, h, bssbp,
3354 sizeof(*bssbp), 0, RAID_CTLR_LUNID, TYPE_CMD);
3355
3356 if (rc)
3357 goto out;
3358
3359 if (id_phys->phys_connector[1] == 'E')
3360 c->Request.CDB[5] = id_phys->box_index;
3361 else
3362 c->Request.CDB[5] = 0;
3363
3364 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
Don Bracec448ecf2016-04-27 17:13:51 -05003365 DEFAULT_TIMEOUT);
Don Bracecca8f132015-12-22 10:36:48 -06003366 if (rc)
3367 goto out;
3368
3369 ei = c->err_info;
3370 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3371 rc = -1;
3372 goto out;
3373 }
3374
3375 encl_dev->box[id_phys->active_path_number] = bssbp->phys_box_on_port;
3376 memcpy(&encl_dev->phys_connector[id_phys->active_path_number],
3377 bssbp->phys_connector, sizeof(bssbp->phys_connector));
3378
3379 rc = IO_OK;
3380out:
3381 kfree(bssbp);
3382 kfree(id_phys);
3383
3384 if (c)
3385 cmd_free(h, c);
3386
3387 if (rc != IO_OK)
3388 hpsa_show_dev_msg(KERN_INFO, h, encl_dev,
3389 "Error, could not get enclosure information\n");
3390}
3391
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003392static u64 hpsa_get_sas_address_from_report_physical(struct ctlr_info *h,
3393 unsigned char *scsi3addr)
3394{
3395 struct ReportExtendedLUNdata *physdev;
3396 u32 nphysicals;
3397 u64 sa = 0;
3398 int i;
3399
3400 physdev = kzalloc(sizeof(*physdev), GFP_KERNEL);
3401 if (!physdev)
3402 return 0;
3403
3404 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3405 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3406 kfree(physdev);
3407 return 0;
3408 }
3409 nphysicals = get_unaligned_be32(physdev->LUNListLength) / 24;
3410
3411 for (i = 0; i < nphysicals; i++)
3412 if (!memcmp(&physdev->LUN[i].lunid[0], scsi3addr, 8)) {
3413 sa = get_unaligned_be64(&physdev->LUN[i].wwid[0]);
3414 break;
3415 }
3416
3417 kfree(physdev);
3418
3419 return sa;
3420}
3421
3422static void hpsa_get_sas_address(struct ctlr_info *h, unsigned char *scsi3addr,
3423 struct hpsa_scsi_dev_t *dev)
3424{
3425 int rc;
3426 u64 sa = 0;
3427
3428 if (is_hba_lunid(scsi3addr)) {
3429 struct bmic_sense_subsystem_info *ssi;
3430
3431 ssi = kzalloc(sizeof(*ssi), GFP_KERNEL);
3432 if (ssi == NULL) {
3433 dev_warn(&h->pdev->dev,
3434 "%s: out of memory\n", __func__);
3435 return;
3436 }
3437
3438 rc = hpsa_bmic_sense_subsystem_information(h,
3439 scsi3addr, 0, ssi, sizeof(*ssi));
3440 if (rc == 0) {
3441 sa = get_unaligned_be64(ssi->primary_world_wide_id);
3442 h->sas_address = sa;
3443 }
3444
3445 kfree(ssi);
3446 } else
3447 sa = hpsa_get_sas_address_from_report_physical(h, scsi3addr);
3448
3449 dev->sas_address = sa;
3450}
3451
3452/* Get a device id from inquiry page 0x83 */
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003453static int hpsa_vpd_page_supported(struct ctlr_info *h,
3454 unsigned char scsi3addr[], u8 page)
3455{
3456 int rc;
3457 int i;
3458 int pages;
3459 unsigned char *buf, bufsize;
3460
3461 buf = kzalloc(256, GFP_KERNEL);
3462 if (!buf)
3463 return 0;
3464
3465 /* Get the size of the page list first */
3466 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3467 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3468 buf, HPSA_VPD_HEADER_SZ);
3469 if (rc != 0)
3470 goto exit_unsupported;
3471 pages = buf[3];
3472 if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
3473 bufsize = pages + HPSA_VPD_HEADER_SZ;
3474 else
3475 bufsize = 255;
3476
3477 /* Get the whole VPD page list */
3478 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3479 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3480 buf, bufsize);
3481 if (rc != 0)
3482 goto exit_unsupported;
3483
3484 pages = buf[3];
3485 for (i = 1; i <= pages; i++)
3486 if (buf[3 + i] == page)
3487 goto exit_supported;
3488exit_unsupported:
3489 kfree(buf);
3490 return 0;
3491exit_supported:
3492 kfree(buf);
3493 return 1;
3494}
3495
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003496static void hpsa_get_ioaccel_status(struct ctlr_info *h,
3497 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3498{
3499 int rc;
3500 unsigned char *buf;
3501 u8 ioaccel_status;
3502
3503 this_device->offload_config = 0;
3504 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003505 this_device->offload_to_be_enabled = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003506
3507 buf = kzalloc(64, GFP_KERNEL);
3508 if (!buf)
3509 return;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003510 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
3511 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003512 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003513 VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003514 if (rc != 0)
3515 goto out;
3516
3517#define IOACCEL_STATUS_BYTE 4
3518#define OFFLOAD_CONFIGURED_BIT 0x01
3519#define OFFLOAD_ENABLED_BIT 0x02
3520 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
3521 this_device->offload_config =
3522 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
3523 if (this_device->offload_config) {
3524 this_device->offload_enabled =
3525 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
3526 if (hpsa_get_raid_map(h, scsi3addr, this_device))
3527 this_device->offload_enabled = 0;
3528 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003529 this_device->offload_to_be_enabled = this_device->offload_enabled;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003530out:
3531 kfree(buf);
3532 return;
3533}
3534
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003535/* Get the device id from inquiry page 0x83 */
3536static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
Don Brace75d23d82015-11-04 15:51:39 -06003537 unsigned char *device_id, int index, int buflen)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003538{
3539 int rc;
3540 unsigned char *buf;
3541
3542 if (buflen > 16)
3543 buflen = 16;
3544 buf = kzalloc(64, GFP_KERNEL);
3545 if (!buf)
Stephen M. Camerona84d7942014-05-29 10:54:20 -05003546 return -ENOMEM;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003547 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003548 if (rc == 0)
Don Brace75d23d82015-11-04 15:51:39 -06003549 memcpy(device_id, &buf[index], buflen);
3550
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003551 kfree(buf);
Don Brace75d23d82015-11-04 15:51:39 -06003552
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003553 return rc != 0;
3554}
3555
3556static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
Don Brace03383732015-01-23 16:43:30 -06003557 void *buf, int bufsize,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003558 int extended_response)
3559{
3560 int rc = IO_OK;
3561 struct CommandList *c;
3562 unsigned char scsi3addr[8];
3563 struct ErrorInfo *ei;
3564
Stephen Cameron45fcb862015-01-23 16:43:04 -06003565 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003566
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06003567 /* address the controller */
3568 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003569 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
3570 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
3571 rc = -1;
3572 goto out;
3573 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003574 if (extended_response)
3575 c->Request.CDB[1] = extended_response;
Webb Scales25163bd2015-04-23 09:32:00 -05003576 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003577 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003578 if (rc)
3579 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003580 ei = c->err_info;
3581 if (ei->CommandStatus != 0 &&
3582 ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003583 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003584 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003585 } else {
Don Brace03383732015-01-23 16:43:30 -06003586 struct ReportLUNdata *rld = buf;
3587
3588 if (rld->extended_response_flag != extended_response) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003589 dev_err(&h->pdev->dev,
3590 "report luns requested format %u, got %u\n",
3591 extended_response,
Don Brace03383732015-01-23 16:43:30 -06003592 rld->extended_response_flag);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003593 rc = -1;
3594 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003595 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003596out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06003597 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003598 return rc;
3599}
3600
3601static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06003602 struct ReportExtendedLUNdata *buf, int bufsize)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003603{
Don Brace03383732015-01-23 16:43:30 -06003604 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
3605 HPSA_REPORT_PHYS_EXTENDED);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003606}
3607
3608static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
3609 struct ReportLUNdata *buf, int bufsize)
3610{
3611 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
3612}
3613
3614static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
3615 int bus, int target, int lun)
3616{
3617 device->bus = bus;
3618 device->target = target;
3619 device->lun = lun;
3620}
3621
Stephen M. Cameron98465902014-02-21 16:25:00 -06003622/* Use VPD inquiry to get details of volume status */
3623static int hpsa_get_volume_status(struct ctlr_info *h,
3624 unsigned char scsi3addr[])
3625{
3626 int rc;
3627 int status;
3628 int size;
3629 unsigned char *buf;
3630
3631 buf = kzalloc(64, GFP_KERNEL);
3632 if (!buf)
3633 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3634
3635 /* Does controller have VPD for logical volume status? */
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003636 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
Stephen M. Cameron98465902014-02-21 16:25:00 -06003637 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003638
3639 /* Get the size of the VPD return buffer */
3640 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3641 buf, HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003642 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003643 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003644 size = buf[3];
3645
3646 /* Now get the whole VPD buffer */
3647 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3648 buf, size + HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003649 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003650 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003651 status = buf[4]; /* status byte */
3652
3653 kfree(buf);
3654 return status;
3655exit_failed:
3656 kfree(buf);
3657 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3658}
3659
3660/* Determine offline status of a volume.
3661 * Return either:
3662 * 0 (not offline)
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003663 * 0xff (offline for unknown reasons)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003664 * # (integer code indicating one of several NOT READY states
3665 * describing why a volume is to be kept offline)
3666 */
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003667static int hpsa_volume_offline(struct ctlr_info *h,
Stephen M. Cameron98465902014-02-21 16:25:00 -06003668 unsigned char scsi3addr[])
3669{
3670 struct CommandList *c;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003671 unsigned char *sense;
3672 u8 sense_key, asc, ascq;
3673 int sense_len;
Webb Scales25163bd2015-04-23 09:32:00 -05003674 int rc, ldstat = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003675 u16 cmd_status;
3676 u8 scsi_status;
3677#define ASC_LUN_NOT_READY 0x04
3678#define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
3679#define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
3680
3681 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003682
Stephen M. Cameron98465902014-02-21 16:25:00 -06003683 (void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
Don Bracec448ecf2016-04-27 17:13:51 -05003684 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
3685 DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003686 if (rc) {
3687 cmd_free(h, c);
3688 return 0;
3689 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06003690 sense = c->err_info->SenseInfo;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003691 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3692 sense_len = sizeof(c->err_info->SenseInfo);
3693 else
3694 sense_len = c->err_info->SenseLen;
3695 decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
Stephen M. Cameron98465902014-02-21 16:25:00 -06003696 cmd_status = c->err_info->CommandStatus;
3697 scsi_status = c->err_info->ScsiStatus;
3698 cmd_free(h, c);
3699 /* Is the volume 'not ready'? */
3700 if (cmd_status != CMD_TARGET_STATUS ||
3701 scsi_status != SAM_STAT_CHECK_CONDITION ||
3702 sense_key != NOT_READY ||
3703 asc != ASC_LUN_NOT_READY) {
3704 return 0;
3705 }
3706
3707 /* Determine the reason for not ready state */
3708 ldstat = hpsa_get_volume_status(h, scsi3addr);
3709
3710 /* Keep volume offline in certain cases: */
3711 switch (ldstat) {
3712 case HPSA_LV_UNDERGOING_ERASE:
Scott Benesh5ca01202015-07-18 11:13:04 -05003713 case HPSA_LV_NOT_AVAILABLE:
Stephen M. Cameron98465902014-02-21 16:25:00 -06003714 case HPSA_LV_UNDERGOING_RPI:
3715 case HPSA_LV_PENDING_RPI:
3716 case HPSA_LV_ENCRYPTED_NO_KEY:
3717 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
3718 case HPSA_LV_UNDERGOING_ENCRYPTION:
3719 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
3720 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
3721 return ldstat;
3722 case HPSA_VPD_LV_STATUS_UNSUPPORTED:
3723 /* If VPD status page isn't available,
3724 * use ASC/ASCQ to determine state
3725 */
3726 if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
3727 (ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
3728 return ldstat;
3729 break;
3730 default:
3731 break;
3732 }
3733 return 0;
3734}
3735
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003736/*
3737 * Find out if a logical device supports aborts by simply trying one.
3738 * Smart Array may claim not to support aborts on logical drives, but
3739 * if a MSA2000 * is connected, the drives on that will be presented
3740 * by the Smart Array as logical drives, and aborts may be sent to
3741 * those devices successfully. So the simplest way to find out is
3742 * to simply try an abort and see how the device responds.
3743 */
3744static int hpsa_device_supports_aborts(struct ctlr_info *h,
3745 unsigned char *scsi3addr)
3746{
3747 struct CommandList *c;
3748 struct ErrorInfo *ei;
3749 int rc = 0;
3750
3751 u64 tag = (u64) -1; /* bogus tag */
3752
3753 /* Assume that physical devices support aborts */
3754 if (!is_logical_dev_addr_mode(scsi3addr))
3755 return 1;
3756
3757 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003758
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003759 (void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
Don Bracec448ecf2016-04-27 17:13:51 -05003760 (void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
3761 DEFAULT_TIMEOUT);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003762 /* no unmap needed here because no data xfer. */
3763 ei = c->err_info;
3764 switch (ei->CommandStatus) {
3765 case CMD_INVALID:
3766 rc = 0;
3767 break;
3768 case CMD_UNABORTABLE:
3769 case CMD_ABORT_FAILED:
3770 rc = 1;
3771 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003772 case CMD_TMF_STATUS:
3773 rc = hpsa_evaluate_tmf_status(h, c);
3774 break;
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003775 default:
3776 rc = 0;
3777 break;
3778 }
3779 cmd_free(h, c);
3780 return rc;
3781}
3782
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003783static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003784 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
3785 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003786{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003787
3788#define OBDR_SIG_OFFSET 43
3789#define OBDR_TAPE_SIG "$DR-10"
3790#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
3791#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
3792
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003793 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003794 unsigned char *obdr_sig;
Don Brace683fc442015-11-04 15:50:44 -06003795 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003796
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003797 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Don Brace683fc442015-11-04 15:50:44 -06003798 if (!inq_buff) {
3799 rc = -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003800 goto bail_out;
Don Brace683fc442015-11-04 15:50:44 -06003801 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003802
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003803 /* Do an inquiry to the device to see what it is. */
3804 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3805 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3806 /* Inquiry failed (msg printed already) */
3807 dev_err(&h->pdev->dev,
3808 "hpsa_update_device_info: inquiry failed\n");
Don Brace683fc442015-11-04 15:50:44 -06003809 rc = -EIO;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003810 goto bail_out;
3811 }
3812
Don Brace4af61e42016-02-23 15:16:40 -06003813 scsi_sanitize_inquiry_string(&inq_buff[8], 8);
3814 scsi_sanitize_inquiry_string(&inq_buff[16], 16);
Don Brace75d23d82015-11-04 15:51:39 -06003815
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003816 this_device->devtype = (inq_buff[0] & 0x1f);
3817 memcpy(this_device->scsi3addr, scsi3addr, 8);
3818 memcpy(this_device->vendor, &inq_buff[8],
3819 sizeof(this_device->vendor));
3820 memcpy(this_device->model, &inq_buff[16],
3821 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003822 memset(this_device->device_id, 0,
3823 sizeof(this_device->device_id));
Don Brace75d23d82015-11-04 15:51:39 -06003824 hpsa_get_device_id(h, scsi3addr, this_device->device_id, 8,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003825 sizeof(this_device->device_id));
3826
Don Braceaf15ed32016-02-23 15:16:15 -06003827 if ((this_device->devtype == TYPE_DISK ||
3828 this_device->devtype == TYPE_ZBC) &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003829 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003830 int volume_offline;
3831
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003832 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003833 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3834 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003835 volume_offline = hpsa_volume_offline(h, scsi3addr);
3836 if (volume_offline < 0 || volume_offline > 0xff)
3837 volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
3838 this_device->volume_offline = volume_offline & 0xff;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003839 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003840 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003841 this_device->offload_config = 0;
3842 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003843 this_device->offload_to_be_enabled = 0;
Joe Handzika3144e02015-04-23 09:32:59 -05003844 this_device->hba_ioaccel_enabled = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003845 this_device->volume_offline = 0;
Don Brace03383732015-01-23 16:43:30 -06003846 this_device->queue_depth = h->nr_cmds;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003847 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003848
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003849 if (is_OBDR_device) {
3850 /* See if this is a One-Button-Disaster-Recovery device
3851 * by looking for "$DR-10" at offset 43 in inquiry data.
3852 */
3853 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
3854 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
3855 strncmp(obdr_sig, OBDR_TAPE_SIG,
3856 OBDR_SIG_LEN) == 0);
3857 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003858 kfree(inq_buff);
3859 return 0;
3860
3861bail_out:
3862 kfree(inq_buff);
Don Brace683fc442015-11-04 15:50:44 -06003863 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003864}
3865
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003866static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
3867 struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
3868{
3869 unsigned long flags;
3870 int rc, entry;
3871 /*
3872 * See if this device supports aborts. If we already know
3873 * the device, we already know if it supports aborts, otherwise
3874 * we have to find out if it supports aborts by trying one.
3875 */
3876 spin_lock_irqsave(&h->devlock, flags);
3877 rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
3878 if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
3879 entry >= 0 && entry < h->ndevices) {
3880 dev->supports_aborts = h->dev[entry]->supports_aborts;
3881 spin_unlock_irqrestore(&h->devlock, flags);
3882 } else {
3883 spin_unlock_irqrestore(&h->devlock, flags);
3884 dev->supports_aborts =
3885 hpsa_device_supports_aborts(h, scsi3addr);
3886 if (dev->supports_aborts < 0)
3887 dev->supports_aborts = 0;
3888 }
3889}
3890
Kevin Barnettc7955052015-11-04 15:51:45 -06003891/*
3892 * Helper function to assign bus, target, lun mapping of devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003893 * Logical drive target and lun are assigned at this time, but
3894 * physical device lun and target assignment are deferred (assigned
3895 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
Kevin Barnettc7955052015-11-04 15:51:45 -06003896*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003897static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003898 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003899{
Kevin Barnettc7955052015-11-04 15:51:45 -06003900 u32 lunid = get_unaligned_le32(lunaddrbytes);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003901
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003902 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
3903 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003904 if (is_hba_lunid(lunaddrbytes))
Kevin Barnettc7955052015-11-04 15:51:45 -06003905 hpsa_set_bus_target_lun(device,
3906 HPSA_HBA_BUS, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003907 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003908 /* defer target, lun assignment for physical devices */
Kevin Barnettc7955052015-11-04 15:51:45 -06003909 hpsa_set_bus_target_lun(device,
3910 HPSA_PHYSICAL_DEVICE_BUS, -1, -1);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003911 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003912 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003913 /* It's a logical device */
Scott Teel66749d02015-11-04 15:51:57 -06003914 if (device->external) {
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003915 hpsa_set_bus_target_lun(device,
Kevin Barnettc7955052015-11-04 15:51:45 -06003916 HPSA_EXTERNAL_RAID_VOLUME_BUS, (lunid >> 16) & 0x3fff,
3917 lunid & 0x00ff);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003918 return;
3919 }
Kevin Barnettc7955052015-11-04 15:51:45 -06003920 hpsa_set_bus_target_lun(device, HPSA_RAID_VOLUME_BUS,
3921 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003922}
3923
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003924
3925/*
Scott Teel54b6e9e2014-02-18 13:56:45 -06003926 * Get address of physical disk used for an ioaccel2 mode command:
3927 * 1. Extract ioaccel2 handle from the command.
3928 * 2. Find a matching ioaccel2 handle from list of physical disks.
3929 * 3. Return:
3930 * 1 and set scsi3addr to address of matching physical
3931 * 0 if no matching physical disk was found.
3932 */
3933static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
3934 struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
3935{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003936 struct io_accel2_cmd *c2 =
3937 &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
3938 unsigned long flags;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003939 int i;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003940
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003941 spin_lock_irqsave(&h->devlock, flags);
3942 for (i = 0; i < h->ndevices; i++)
3943 if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
3944 memcpy(scsi3addr, h->dev[i]->scsi3addr,
3945 sizeof(h->dev[i]->scsi3addr));
3946 spin_unlock_irqrestore(&h->devlock, flags);
3947 return 1;
3948 }
3949 spin_unlock_irqrestore(&h->devlock, flags);
3950 return 0;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003951}
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003952
Scott Teel66749d02015-11-04 15:51:57 -06003953static int figure_external_status(struct ctlr_info *h, int raid_ctlr_position,
3954 int i, int nphysicals, int nlocal_logicals)
3955{
3956 /* In report logicals, local logicals are listed first,
3957 * then any externals.
3958 */
3959 int logicals_start = nphysicals + (raid_ctlr_position == 0);
3960
3961 if (i == raid_ctlr_position)
3962 return 0;
3963
3964 if (i < logicals_start)
3965 return 0;
3966
3967 /* i is in logicals range, but still within local logicals */
3968 if ((i - nphysicals - (raid_ctlr_position == 0)) < nlocal_logicals)
3969 return 0;
3970
3971 return 1; /* it's an external lun */
3972}
3973
Scott Teel54b6e9e2014-02-18 13:56:45 -06003974/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003975 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
3976 * logdev. The number of luns in physdev and logdev are returned in
3977 * *nphysicals and *nlogicals, respectively.
3978 * Returns 0 on success, -1 otherwise.
3979 */
3980static int hpsa_gather_lun_info(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06003981 struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003982 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003983{
Don Brace03383732015-01-23 16:43:30 -06003984 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003985 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3986 return -1;
3987 }
Don Brace03383732015-01-23 16:43:30 -06003988 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003989 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
Don Brace03383732015-01-23 16:43:30 -06003990 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
3991 HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003992 *nphysicals = HPSA_MAX_PHYS_LUN;
3993 }
Don Brace03383732015-01-23 16:43:30 -06003994 if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003995 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3996 return -1;
3997 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06003998 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003999 /* Reject Logicals in excess of our max capability. */
4000 if (*nlogicals > HPSA_MAX_LUN) {
4001 dev_warn(&h->pdev->dev,
4002 "maximum logical LUNs (%d) exceeded. "
4003 "%d LUNs ignored.\n", HPSA_MAX_LUN,
4004 *nlogicals - HPSA_MAX_LUN);
4005 *nlogicals = HPSA_MAX_LUN;
4006 }
4007 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
4008 dev_warn(&h->pdev->dev,
4009 "maximum logical + physical LUNs (%d) exceeded. "
4010 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
4011 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
4012 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
4013 }
4014 return 0;
4015}
4016
Don Brace42a91642014-11-14 17:26:27 -06004017static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
4018 int i, int nphysicals, int nlogicals,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06004019 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004020 struct ReportLUNdata *logdev_list)
4021{
4022 /* Helper function, figure out where the LUN ID info is coming from
4023 * given index i, lists of physical and logical devices, where in
4024 * the list the raid controller is supposed to appear (first or last)
4025 */
4026
4027 int logicals_start = nphysicals + (raid_ctlr_position == 0);
4028 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
4029
4030 if (i == raid_ctlr_position)
4031 return RAID_CTLR_LUNID;
4032
4033 if (i < logicals_start)
Stephen M. Camerond5b5d962014-05-29 10:53:34 -05004034 return &physdev_list->LUN[i -
4035 (raid_ctlr_position == 0)].lunid[0];
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004036
4037 if (i < last_device)
4038 return &logdev_list->LUN[i - nphysicals -
4039 (raid_ctlr_position == 0)][0];
4040 BUG();
4041 return NULL;
4042}
4043
Don Brace03383732015-01-23 16:43:30 -06004044/* get physical drive ioaccel handle and queue depth */
4045static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
4046 struct hpsa_scsi_dev_t *dev,
Don Bracef2039b02015-11-04 15:51:08 -06004047 struct ReportExtendedLUNdata *rlep, int rle_index,
Don Brace03383732015-01-23 16:43:30 -06004048 struct bmic_identify_physical_device *id_phys)
4049{
4050 int rc;
Don Bracef2039b02015-11-04 15:51:08 -06004051 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
Don Brace03383732015-01-23 16:43:30 -06004052
4053 dev->ioaccel_handle = rle->ioaccel_handle;
Don Bracef2039b02015-11-04 15:51:08 -06004054 if ((rle->device_flags & 0x08) && dev->ioaccel_handle)
Joe Handzika3144e02015-04-23 09:32:59 -05004055 dev->hba_ioaccel_enabled = 1;
Don Brace03383732015-01-23 16:43:30 -06004056 memset(id_phys, 0, sizeof(*id_phys));
Don Bracef2039b02015-11-04 15:51:08 -06004057 rc = hpsa_bmic_id_physical_device(h, &rle->lunid[0],
4058 GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]), id_phys,
Don Brace03383732015-01-23 16:43:30 -06004059 sizeof(*id_phys));
4060 if (!rc)
4061 /* Reserve space for FW operations */
4062#define DRIVE_CMDS_RESERVED_FOR_FW 2
4063#define DRIVE_QUEUE_DEPTH 7
4064 dev->queue_depth =
4065 le16_to_cpu(id_phys->current_queue_depth_limit) -
4066 DRIVE_CMDS_RESERVED_FOR_FW;
4067 else
4068 dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
Don Brace03383732015-01-23 16:43:30 -06004069}
4070
Joe Handzik8270b862015-07-18 11:12:43 -05004071static void hpsa_get_path_info(struct hpsa_scsi_dev_t *this_device,
Don Bracef2039b02015-11-04 15:51:08 -06004072 struct ReportExtendedLUNdata *rlep, int rle_index,
Joe Handzik8270b862015-07-18 11:12:43 -05004073 struct bmic_identify_physical_device *id_phys)
4074{
Don Bracef2039b02015-11-04 15:51:08 -06004075 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
4076
4077 if ((rle->device_flags & 0x08) && this_device->ioaccel_handle)
Joe Handzik8270b862015-07-18 11:12:43 -05004078 this_device->hba_ioaccel_enabled = 1;
4079
4080 memcpy(&this_device->active_path_index,
4081 &id_phys->active_path_number,
4082 sizeof(this_device->active_path_index));
4083 memcpy(&this_device->path_map,
4084 &id_phys->redundant_path_present_map,
4085 sizeof(this_device->path_map));
4086 memcpy(&this_device->box,
4087 &id_phys->alternate_paths_phys_box_on_port,
4088 sizeof(this_device->box));
4089 memcpy(&this_device->phys_connector,
4090 &id_phys->alternate_paths_phys_connector,
4091 sizeof(this_device->phys_connector));
4092 memcpy(&this_device->bay,
4093 &id_phys->phys_bay_in_box,
4094 sizeof(this_device->bay));
4095}
4096
Scott Teel66749d02015-11-04 15:51:57 -06004097/* get number of local logical disks. */
4098static int hpsa_set_local_logical_count(struct ctlr_info *h,
4099 struct bmic_identify_controller *id_ctlr,
4100 u32 *nlocals)
4101{
4102 int rc;
4103
4104 if (!id_ctlr) {
4105 dev_warn(&h->pdev->dev, "%s: id_ctlr buffer is NULL.\n",
4106 __func__);
4107 return -ENOMEM;
4108 }
4109 memset(id_ctlr, 0, sizeof(*id_ctlr));
4110 rc = hpsa_bmic_id_controller(h, id_ctlr, sizeof(*id_ctlr));
4111 if (!rc)
4112 if (id_ctlr->configured_logical_drive_count < 256)
4113 *nlocals = id_ctlr->configured_logical_drive_count;
4114 else
4115 *nlocals = le16_to_cpu(
4116 id_ctlr->extended_logical_unit_count);
4117 else
4118 *nlocals = -1;
4119 return rc;
4120}
4121
Don Brace64ce60c2016-07-01 13:37:31 -05004122static bool hpsa_is_disk_spare(struct ctlr_info *h, u8 *lunaddrbytes)
4123{
4124 struct bmic_identify_physical_device *id_phys;
4125 bool is_spare = false;
4126 int rc;
4127
4128 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
4129 if (!id_phys)
4130 return false;
4131
4132 rc = hpsa_bmic_id_physical_device(h,
4133 lunaddrbytes,
4134 GET_BMIC_DRIVE_NUMBER(lunaddrbytes),
4135 id_phys, sizeof(*id_phys));
4136 if (rc == 0)
4137 is_spare = (id_phys->more_flags >> 6) & 0x01;
4138
4139 kfree(id_phys);
4140 return is_spare;
4141}
4142
4143#define RPL_DEV_FLAG_NON_DISK 0x1
4144#define RPL_DEV_FLAG_UNCONFIG_DISK_REPORTING_SUPPORTED 0x2
4145#define RPL_DEV_FLAG_UNCONFIG_DISK 0x4
4146
4147#define BMIC_DEVICE_TYPE_ENCLOSURE 6
4148
4149static bool hpsa_skip_device(struct ctlr_info *h, u8 *lunaddrbytes,
4150 struct ext_report_lun_entry *rle)
4151{
4152 u8 device_flags;
4153 u8 device_type;
4154
4155 if (!MASKED_DEVICE(lunaddrbytes))
4156 return false;
4157
4158 device_flags = rle->device_flags;
4159 device_type = rle->device_type;
4160
4161 if (device_flags & RPL_DEV_FLAG_NON_DISK) {
4162 if (device_type == BMIC_DEVICE_TYPE_ENCLOSURE)
4163 return false;
4164 return true;
4165 }
4166
4167 if (!(device_flags & RPL_DEV_FLAG_UNCONFIG_DISK_REPORTING_SUPPORTED))
4168 return false;
4169
4170 if (device_flags & RPL_DEV_FLAG_UNCONFIG_DISK)
4171 return false;
4172
4173 /*
4174 * Spares may be spun down, we do not want to
4175 * do an Inquiry to a RAID set spare drive as
4176 * that would have them spun up, that is a
4177 * performance hit because I/O to the RAID device
4178 * stops while the spin up occurs which can take
4179 * over 50 seconds.
4180 */
4181 if (hpsa_is_disk_spare(h, lunaddrbytes))
4182 return true;
4183
4184 return false;
4185}
Scott Teel66749d02015-11-04 15:51:57 -06004186
Don Brace8aa60682015-11-04 15:50:01 -06004187static void hpsa_update_scsi_devices(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004188{
4189 /* the idea here is we could get notified
4190 * that some devices have changed, so we do a report
4191 * physical luns and report logical luns cmd, and adjust
4192 * our list of devices accordingly.
4193 *
4194 * The scsi3addr's of devices won't change so long as the
4195 * adapter is not reset. That means we can rescan and
4196 * tell which devices we already know about, vs. new
4197 * devices, vs. disappearing devices.
4198 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06004199 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004200 struct ReportLUNdata *logdev_list = NULL;
Don Brace03383732015-01-23 16:43:30 -06004201 struct bmic_identify_physical_device *id_phys = NULL;
Scott Teel66749d02015-11-04 15:51:57 -06004202 struct bmic_identify_controller *id_ctlr = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004203 u32 nphysicals = 0;
4204 u32 nlogicals = 0;
Scott Teel66749d02015-11-04 15:51:57 -06004205 u32 nlocal_logicals = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004206 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004207 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
4208 int ncurrent = 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06004209 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004210 int raid_ctlr_position;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004211 bool physical_device;
Scott Teelaca4a522012-01-19 14:01:19 -06004212 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004213
Scott Teelcfe5bad2011-10-26 16:21:07 -05004214 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameron92084712014-11-14 17:26:54 -06004215 physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
4216 logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004217 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
Don Brace03383732015-01-23 16:43:30 -06004218 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
Scott Teel66749d02015-11-04 15:51:57 -06004219 id_ctlr = kzalloc(sizeof(*id_ctlr), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004220
Don Brace03383732015-01-23 16:43:30 -06004221 if (!currentsd || !physdev_list || !logdev_list ||
Scott Teel66749d02015-11-04 15:51:57 -06004222 !tmpdevice || !id_phys || !id_ctlr) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004223 dev_err(&h->pdev->dev, "out of memory\n");
4224 goto out;
4225 }
4226 memset(lunzerobits, 0, sizeof(lunzerobits));
4227
Don Brace853633e2015-11-04 15:50:37 -06004228 h->drv_req_rescan = 0; /* cancel scheduled rescan - we're doing it. */
4229
Don Brace03383732015-01-23 16:43:30 -06004230 if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
Don Brace853633e2015-11-04 15:50:37 -06004231 logdev_list, &nlogicals)) {
4232 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004233 goto out;
Don Brace853633e2015-11-04 15:50:37 -06004234 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004235
Scott Teel66749d02015-11-04 15:51:57 -06004236 /* Set number of local logicals (non PTRAID) */
4237 if (hpsa_set_local_logical_count(h, id_ctlr, &nlocal_logicals)) {
4238 dev_warn(&h->pdev->dev,
4239 "%s: Can't determine number of local logical devices.\n",
4240 __func__);
4241 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004242
Scott Teelaca4a522012-01-19 14:01:19 -06004243 /* We might see up to the maximum number of logical and physical disks
4244 * plus external target devices, and a device for the local RAID
4245 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004246 */
Scott Teelaca4a522012-01-19 14:01:19 -06004247 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004248
4249 /* Allocate the per device structures */
4250 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05004251 if (i >= HPSA_MAX_DEVICES) {
4252 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
4253 " %d devices ignored.\n", HPSA_MAX_DEVICES,
4254 ndevs_to_allocate - HPSA_MAX_DEVICES);
4255 break;
4256 }
4257
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004258 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
4259 if (!currentsd[i]) {
4260 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
4261 __FILE__, __LINE__);
Don Brace853633e2015-11-04 15:50:37 -06004262 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004263 goto out;
4264 }
4265 ndev_allocated++;
4266 }
4267
Stephen M. Cameron86452912014-05-29 10:53:49 -05004268 if (is_scsi_rev_5(h))
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004269 raid_ctlr_position = 0;
4270 else
4271 raid_ctlr_position = nphysicals + nlogicals;
4272
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004273 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06004274 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004275 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004276 u8 *lunaddrbytes, is_OBDR = 0;
Don Brace683fc442015-11-04 15:50:44 -06004277 int rc = 0;
Don Bracef2039b02015-11-04 15:51:08 -06004278 int phys_dev_index = i - (raid_ctlr_position == 0);
Don Brace64ce60c2016-07-01 13:37:31 -05004279 bool skip_device = false;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004280
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004281 physical_device = i < nphysicals + (raid_ctlr_position == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004282
4283 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004284 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
4285 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004286
Don Brace86cf7132016-09-09 16:30:17 -05004287 /* Determine if this is a lun from an external target array */
4288 tmpdevice->external =
4289 figure_external_status(h, raid_ctlr_position, i,
4290 nphysicals, nlocal_logicals);
4291
Don Brace64ce60c2016-07-01 13:37:31 -05004292 /*
4293 * Skip over some devices such as a spare.
4294 */
4295 if (!tmpdevice->external && physical_device) {
4296 skip_device = hpsa_skip_device(h, lunaddrbytes,
4297 &physdev_list->LUN[phys_dev_index]);
4298 if (skip_device)
4299 continue;
4300 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004301
4302 /* Get device type, vendor, model, device id */
Don Brace683fc442015-11-04 15:50:44 -06004303 rc = hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
4304 &is_OBDR);
4305 if (rc == -ENOMEM) {
4306 dev_warn(&h->pdev->dev,
4307 "Out of memory, rescan deferred.\n");
Don Brace853633e2015-11-04 15:50:37 -06004308 h->drv_req_rescan = 1;
Don Brace683fc442015-11-04 15:50:44 -06004309 goto out;
Don Brace853633e2015-11-04 15:50:37 -06004310 }
Don Brace683fc442015-11-04 15:50:44 -06004311 if (rc) {
4312 dev_warn(&h->pdev->dev,
4313 "Inquiry failed, skipping device.\n");
4314 continue;
4315 }
4316
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06004317 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05004318 hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004319 this_device = currentsd[ncurrent];
4320
Scott Teel34592252015-11-04 15:52:09 -06004321 /* Turn on discovery_polling if there are ext target devices.
4322 * Event-based change notification is unreliable for those.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004323 */
Scott Teel34592252015-11-04 15:52:09 -06004324 if (!h->discovery_polling) {
4325 if (tmpdevice->external) {
4326 h->discovery_polling = 1;
4327 dev_info(&h->pdev->dev,
4328 "External target, activate discovery polling.\n");
4329 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004330 }
4331
Scott Teel34592252015-11-04 15:52:09 -06004332
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004333 *this_device = *tmpdevice;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004334 this_device->physical_device = physical_device;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004335
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004336 /*
4337 * Expose all devices except for physical devices that
4338 * are masked.
4339 */
4340 if (MASKED_DEVICE(lunaddrbytes) && this_device->physical_device)
Kevin Barnett2a168202015-11-04 15:51:21 -06004341 this_device->expose_device = 0;
4342 else
4343 this_device->expose_device = 1;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004344
Kevin Barnettd04e62b2015-11-04 15:52:34 -06004345
4346 /*
4347 * Get the SAS address for physical devices that are exposed.
4348 */
4349 if (this_device->physical_device && this_device->expose_device)
4350 hpsa_get_sas_address(h, lunaddrbytes, this_device);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004351
4352 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004353 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004354 /* We don't *really* support actual CD-ROM devices,
4355 * just "One Button Disaster Recovery" tape drive
4356 * which temporarily pretends to be a CD-ROM drive.
4357 * So we check that the device is really an OBDR tape
4358 * device by checking for "$DR-10" in bytes 43-48 of
4359 * the inquiry data.
4360 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004361 if (is_OBDR)
4362 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004363 break;
4364 case TYPE_DISK:
Don Braceaf15ed32016-02-23 15:16:15 -06004365 case TYPE_ZBC:
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004366 if (this_device->physical_device) {
Kevin Barnettb9092b72015-07-18 11:12:59 -05004367 /* The disk is in HBA mode. */
4368 /* Never use RAID mapper in HBA mode. */
Stephen M. Cameron316b2212014-02-21 16:25:15 -06004369 this_device->offload_enabled = 0;
Kevin Barnettb9092b72015-07-18 11:12:59 -05004370 hpsa_get_ioaccel_drive_info(h, this_device,
Don Bracef2039b02015-11-04 15:51:08 -06004371 physdev_list, phys_dev_index, id_phys);
4372 hpsa_get_path_info(this_device,
4373 physdev_list, phys_dev_index, id_phys);
Kevin Barnettb9092b72015-07-18 11:12:59 -05004374 }
Joe Handzikecf418d12015-04-23 09:33:04 -05004375 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004376 break;
4377 case TYPE_TAPE:
4378 case TYPE_MEDIUM_CHANGER:
Don Bracecca8f132015-12-22 10:36:48 -06004379 ncurrent++;
4380 break;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004381 case TYPE_ENCLOSURE:
Don Brace17a9e542016-02-23 15:16:09 -06004382 if (!this_device->external)
4383 hpsa_get_enclosure_info(h, lunaddrbytes,
Don Bracecca8f132015-12-22 10:36:48 -06004384 physdev_list, phys_dev_index,
4385 this_device);
Kevin Barnettb9092b72015-07-18 11:12:59 -05004386 ncurrent++;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004387 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004388 case TYPE_RAID:
4389 /* Only present the Smartarray HBA as a RAID controller.
4390 * If it's a RAID controller other than the HBA itself
4391 * (an external RAID controller, MSA500 or similar)
4392 * don't present it.
4393 */
4394 if (!is_hba_lunid(lunaddrbytes))
4395 break;
4396 ncurrent++;
4397 break;
4398 default:
4399 break;
4400 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05004401 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004402 break;
4403 }
Kevin Barnettd04e62b2015-11-04 15:52:34 -06004404
4405 if (h->sas_host == NULL) {
4406 int rc = 0;
4407
4408 rc = hpsa_add_sas_host(h);
4409 if (rc) {
4410 dev_warn(&h->pdev->dev,
4411 "Could not add sas host %d\n", rc);
4412 goto out;
4413 }
4414 }
4415
Don Brace8aa60682015-11-04 15:50:01 -06004416 adjust_hpsa_scsi_table(h, currentsd, ncurrent);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004417out:
4418 kfree(tmpdevice);
4419 for (i = 0; i < ndev_allocated; i++)
4420 kfree(currentsd[i]);
4421 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004422 kfree(physdev_list);
4423 kfree(logdev_list);
Scott Teel66749d02015-11-04 15:51:57 -06004424 kfree(id_ctlr);
Don Brace03383732015-01-23 16:43:30 -06004425 kfree(id_phys);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004426}
4427
Webb Scalesec5cbf02015-01-23 16:44:45 -06004428static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
4429 struct scatterlist *sg)
4430{
4431 u64 addr64 = (u64) sg_dma_address(sg);
4432 unsigned int len = sg_dma_len(sg);
4433
4434 desc->Addr = cpu_to_le64(addr64);
4435 desc->Len = cpu_to_le32(len);
4436 desc->Ext = 0;
4437}
4438
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004439/*
4440 * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004441 * dma mapping and fills in the scatter gather entries of the
4442 * hpsa command, cp.
4443 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004444static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004445 struct CommandList *cp,
4446 struct scsi_cmnd *cmd)
4447{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004448 struct scatterlist *sg;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004449 int use_sg, i, sg_limit, chained, last_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004450 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004451
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004452 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004453
4454 use_sg = scsi_dma_map(cmd);
4455 if (use_sg < 0)
4456 return use_sg;
4457
4458 if (!use_sg)
4459 goto sglist_finished;
4460
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004461 /*
4462 * If the number of entries is greater than the max for a single list,
4463 * then we have a chained list; we will set up all but one entry in the
4464 * first list (the last entry is saved for link information);
4465 * otherwise, we don't have a chained list and we'll set up at each of
4466 * the entries in the one list.
4467 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004468 curr_sg = cp->SG;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004469 chained = use_sg > h->max_cmd_sg_entries;
4470 sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
4471 last_sg = scsi_sg_count(cmd) - 1;
4472 scsi_for_each_sg(cmd, sg, sg_limit, i) {
Webb Scalesec5cbf02015-01-23 16:44:45 -06004473 hpsa_set_sg_descriptor(curr_sg, sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004474 curr_sg++;
4475 }
Webb Scalesec5cbf02015-01-23 16:44:45 -06004476
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004477 if (chained) {
4478 /*
4479 * Continue with the chained list. Set curr_sg to the chained
4480 * list. Modify the limit to the total count less the entries
4481 * we've already set up. Resume the scan at the list entry
4482 * where the previous loop left off.
4483 */
4484 curr_sg = h->cmd_sg_list[cp->cmdindex];
4485 sg_limit = use_sg - sg_limit;
4486 for_each_sg(sg, sg, sg_limit, i) {
4487 hpsa_set_sg_descriptor(curr_sg, sg);
4488 curr_sg++;
4489 }
4490 }
4491
Webb Scalesec5cbf02015-01-23 16:44:45 -06004492 /* Back the pointer up to the last entry and mark it as "last". */
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004493 (curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004494
4495 if (use_sg + chained > h->maxSG)
4496 h->maxSG = use_sg + chained;
4497
4498 if (chained) {
4499 cp->Header.SGList = h->max_cmd_sg_entries;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004500 cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06004501 if (hpsa_map_sg_chain_block(h, cp)) {
4502 scsi_dma_unmap(cmd);
4503 return -1;
4504 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004505 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004506 }
4507
4508sglist_finished:
4509
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004510 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004511 cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004512 return 0;
4513}
4514
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004515#define IO_ACCEL_INELIGIBLE (1)
4516static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
4517{
4518 int is_write = 0;
4519 u32 block;
4520 u32 block_cnt;
4521
4522 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
4523 switch (cdb[0]) {
4524 case WRITE_6:
4525 case WRITE_12:
4526 is_write = 1;
4527 case READ_6:
4528 case READ_12:
4529 if (*cdb_len == 6) {
Don Bracec8a6c9a2015-11-04 15:50:50 -06004530 block = get_unaligned_be16(&cdb[2]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004531 block_cnt = cdb[4];
Don Bracec8a6c9a2015-11-04 15:50:50 -06004532 if (block_cnt == 0)
4533 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004534 } else {
4535 BUG_ON(*cdb_len != 12);
Don Bracec8a6c9a2015-11-04 15:50:50 -06004536 block = get_unaligned_be32(&cdb[2]);
4537 block_cnt = get_unaligned_be32(&cdb[6]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004538 }
4539 if (block_cnt > 0xffff)
4540 return IO_ACCEL_INELIGIBLE;
4541
4542 cdb[0] = is_write ? WRITE_10 : READ_10;
4543 cdb[1] = 0;
4544 cdb[2] = (u8) (block >> 24);
4545 cdb[3] = (u8) (block >> 16);
4546 cdb[4] = (u8) (block >> 8);
4547 cdb[5] = (u8) (block);
4548 cdb[6] = 0;
4549 cdb[7] = (u8) (block_cnt >> 8);
4550 cdb[8] = (u8) (block_cnt);
4551 cdb[9] = 0;
4552 *cdb_len = 10;
4553 break;
4554 }
4555 return 0;
4556}
4557
Scott Teelc3497752014-02-18 13:56:34 -06004558static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004559 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004560 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Matt Gatese1f7de02014-02-18 13:55:17 -06004561{
4562 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06004563 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
4564 unsigned int len;
4565 unsigned int total_len = 0;
4566 struct scatterlist *sg;
4567 u64 addr64;
4568 int use_sg, i;
4569 struct SGDescriptor *curr_sg;
4570 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
4571
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004572 /* TODO: implement chaining support */
Don Brace03383732015-01-23 16:43:30 -06004573 if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
4574 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004575 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004576 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004577
Matt Gatese1f7de02014-02-18 13:55:17 -06004578 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
4579
Don Brace03383732015-01-23 16:43:30 -06004580 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4581 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004582 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004583 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004584
Matt Gatese1f7de02014-02-18 13:55:17 -06004585 c->cmd_type = CMD_IOACCEL1;
4586
4587 /* Adjust the DMA address to point to the accelerated command buffer */
4588 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
4589 (c->cmdindex * sizeof(*cp));
4590 BUG_ON(c->busaddr & 0x0000007F);
4591
4592 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004593 if (use_sg < 0) {
4594 atomic_dec(&phys_disk->ioaccel_cmds_out);
Matt Gatese1f7de02014-02-18 13:55:17 -06004595 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004596 }
Matt Gatese1f7de02014-02-18 13:55:17 -06004597
4598 if (use_sg) {
4599 curr_sg = cp->SG;
4600 scsi_for_each_sg(cmd, sg, use_sg, i) {
4601 addr64 = (u64) sg_dma_address(sg);
4602 len = sg_dma_len(sg);
4603 total_len += len;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004604 curr_sg->Addr = cpu_to_le64(addr64);
4605 curr_sg->Len = cpu_to_le32(len);
4606 curr_sg->Ext = cpu_to_le32(0);
Matt Gatese1f7de02014-02-18 13:55:17 -06004607 curr_sg++;
4608 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004609 (--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
Matt Gatese1f7de02014-02-18 13:55:17 -06004610
4611 switch (cmd->sc_data_direction) {
4612 case DMA_TO_DEVICE:
4613 control |= IOACCEL1_CONTROL_DATA_OUT;
4614 break;
4615 case DMA_FROM_DEVICE:
4616 control |= IOACCEL1_CONTROL_DATA_IN;
4617 break;
4618 case DMA_NONE:
4619 control |= IOACCEL1_CONTROL_NODATAXFER;
4620 break;
4621 default:
4622 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4623 cmd->sc_data_direction);
4624 BUG();
4625 break;
4626 }
4627 } else {
4628 control |= IOACCEL1_CONTROL_NODATAXFER;
4629 }
4630
Scott Teelc3497752014-02-18 13:56:34 -06004631 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06004632 /* Fill out the command structure to submit */
Don Brace2b08b3e2015-01-23 16:41:09 -06004633 cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
4634 cp->transfer_len = cpu_to_le32(total_len);
4635 cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
4636 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
4637 cp->control = cpu_to_le32(control);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004638 memcpy(cp->CDB, cdb, cdb_len);
4639 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06004640 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004641 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06004642 return 0;
4643}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004644
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004645/*
4646 * Queue a command directly to a device behind the controller using the
4647 * I/O accelerator path.
4648 */
4649static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
4650 struct CommandList *c)
4651{
4652 struct scsi_cmnd *cmd = c->scsi_cmd;
4653 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4654
Don Brace03383732015-01-23 16:43:30 -06004655 c->phys_disk = dev;
4656
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004657 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004658 cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004659}
4660
Scott Teeldd0e19f2014-02-18 13:57:31 -06004661/*
4662 * Set encryption parameters for the ioaccel2 request
4663 */
4664static void set_encrypt_ioaccel2(struct ctlr_info *h,
4665 struct CommandList *c, struct io_accel2_cmd *cp)
4666{
4667 struct scsi_cmnd *cmd = c->scsi_cmd;
4668 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4669 struct raid_map_data *map = &dev->raid_map;
4670 u64 first_block;
4671
Scott Teeldd0e19f2014-02-18 13:57:31 -06004672 /* Are we doing encryption on this device */
Don Brace2b08b3e2015-01-23 16:41:09 -06004673 if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
Scott Teeldd0e19f2014-02-18 13:57:31 -06004674 return;
4675 /* Set the data encryption key index. */
4676 cp->dekindex = map->dekindex;
4677
4678 /* Set the encryption enable flag, encoded into direction field. */
4679 cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
4680
4681 /* Set encryption tweak values based on logical block address
4682 * If block size is 512, tweak value is LBA.
4683 * For other block sizes, tweak is (LBA * block size)/ 512)
4684 */
4685 switch (cmd->cmnd[0]) {
4686 /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
4687 case WRITE_6:
4688 case READ_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06004689 first_block = get_unaligned_be16(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004690 break;
4691 case WRITE_10:
4692 case READ_10:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004693 /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
4694 case WRITE_12:
4695 case READ_12:
Don Brace2b08b3e2015-01-23 16:41:09 -06004696 first_block = get_unaligned_be32(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004697 break;
4698 case WRITE_16:
4699 case READ_16:
Don Brace2b08b3e2015-01-23 16:41:09 -06004700 first_block = get_unaligned_be64(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004701 break;
4702 default:
4703 dev_err(&h->pdev->dev,
Don Brace2b08b3e2015-01-23 16:41:09 -06004704 "ERROR: %s: size (0x%x) not supported for encryption\n",
4705 __func__, cmd->cmnd[0]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004706 BUG();
4707 break;
4708 }
Don Brace2b08b3e2015-01-23 16:41:09 -06004709
4710 if (le32_to_cpu(map->volume_blk_size) != 512)
4711 first_block = first_block *
4712 le32_to_cpu(map->volume_blk_size)/512;
4713
4714 cp->tweak_lower = cpu_to_le32(first_block);
4715 cp->tweak_upper = cpu_to_le32(first_block >> 32);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004716}
4717
Scott Teelc3497752014-02-18 13:56:34 -06004718static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
4719 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004720 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004721{
4722 struct scsi_cmnd *cmd = c->scsi_cmd;
4723 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
4724 struct ioaccel2_sg_element *curr_sg;
4725 int use_sg, i;
4726 struct scatterlist *sg;
4727 u64 addr64;
4728 u32 len;
4729 u32 total_len = 0;
4730
Webb Scalesd9a729f2015-04-23 09:33:27 -05004731 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Scott Teelc3497752014-02-18 13:56:34 -06004732
Don Brace03383732015-01-23 16:43:30 -06004733 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4734 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004735 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004736 }
4737
Scott Teelc3497752014-02-18 13:56:34 -06004738 c->cmd_type = CMD_IOACCEL2;
4739 /* Adjust the DMA address to point to the accelerated command buffer */
4740 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
4741 (c->cmdindex * sizeof(*cp));
4742 BUG_ON(c->busaddr & 0x0000007F);
4743
4744 memset(cp, 0, sizeof(*cp));
4745 cp->IU_type = IOACCEL2_IU_TYPE;
4746
4747 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004748 if (use_sg < 0) {
4749 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004750 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004751 }
Scott Teelc3497752014-02-18 13:56:34 -06004752
4753 if (use_sg) {
Scott Teelc3497752014-02-18 13:56:34 -06004754 curr_sg = cp->sg;
Webb Scalesd9a729f2015-04-23 09:33:27 -05004755 if (use_sg > h->ioaccel_maxsg) {
4756 addr64 = le64_to_cpu(
4757 h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
4758 curr_sg->address = cpu_to_le64(addr64);
4759 curr_sg->length = 0;
4760 curr_sg->reserved[0] = 0;
4761 curr_sg->reserved[1] = 0;
4762 curr_sg->reserved[2] = 0;
4763 curr_sg->chain_indicator = 0x80;
4764
4765 curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
4766 }
Scott Teelc3497752014-02-18 13:56:34 -06004767 scsi_for_each_sg(cmd, sg, use_sg, i) {
4768 addr64 = (u64) sg_dma_address(sg);
4769 len = sg_dma_len(sg);
4770 total_len += len;
4771 curr_sg->address = cpu_to_le64(addr64);
4772 curr_sg->length = cpu_to_le32(len);
4773 curr_sg->reserved[0] = 0;
4774 curr_sg->reserved[1] = 0;
4775 curr_sg->reserved[2] = 0;
4776 curr_sg->chain_indicator = 0;
4777 curr_sg++;
4778 }
4779
4780 switch (cmd->sc_data_direction) {
4781 case DMA_TO_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004782 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4783 cp->direction |= IOACCEL2_DIR_DATA_OUT;
Scott Teelc3497752014-02-18 13:56:34 -06004784 break;
4785 case DMA_FROM_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004786 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4787 cp->direction |= IOACCEL2_DIR_DATA_IN;
Scott Teelc3497752014-02-18 13:56:34 -06004788 break;
4789 case DMA_NONE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004790 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4791 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004792 break;
4793 default:
4794 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4795 cmd->sc_data_direction);
4796 BUG();
4797 break;
4798 }
4799 } else {
Scott Teeldd0e19f2014-02-18 13:57:31 -06004800 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4801 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004802 }
Scott Teeldd0e19f2014-02-18 13:57:31 -06004803
4804 /* Set encryption parameters, if necessary */
4805 set_encrypt_ioaccel2(h, c, cp);
4806
Don Brace2b08b3e2015-01-23 16:41:09 -06004807 cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
Don Bracef2405db2015-01-23 16:43:09 -06004808 cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
Scott Teelc3497752014-02-18 13:56:34 -06004809 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
Scott Teelc3497752014-02-18 13:56:34 -06004810
Scott Teelc3497752014-02-18 13:56:34 -06004811 cp->data_len = cpu_to_le32(total_len);
4812 cp->err_ptr = cpu_to_le64(c->busaddr +
4813 offsetof(struct io_accel2_cmd, error_data));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004814 cp->err_len = cpu_to_le32(sizeof(cp->error_data));
Scott Teelc3497752014-02-18 13:56:34 -06004815
Webb Scalesd9a729f2015-04-23 09:33:27 -05004816 /* fill in sg elements */
4817 if (use_sg > h->ioaccel_maxsg) {
4818 cp->sg_count = 1;
Don Bracea736e9b2015-11-04 15:51:14 -06004819 cp->sg[0].length = cpu_to_le32(use_sg * sizeof(cp->sg[0]));
Webb Scalesd9a729f2015-04-23 09:33:27 -05004820 if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
4821 atomic_dec(&phys_disk->ioaccel_cmds_out);
4822 scsi_dma_unmap(cmd);
4823 return -1;
4824 }
4825 } else
4826 cp->sg_count = (u8) use_sg;
4827
Scott Teelc3497752014-02-18 13:56:34 -06004828 enqueue_cmd_and_start_io(h, c);
4829 return 0;
4830}
4831
4832/*
4833 * Queue a command to the correct I/O accelerator path.
4834 */
4835static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
4836 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004837 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004838{
Don Brace03383732015-01-23 16:43:30 -06004839 /* Try to honor the device's queue depth */
4840 if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
4841 phys_disk->queue_depth) {
4842 atomic_dec(&phys_disk->ioaccel_cmds_out);
4843 return IO_ACCEL_INELIGIBLE;
4844 }
Scott Teelc3497752014-02-18 13:56:34 -06004845 if (h->transMethod & CFGTBL_Trans_io_accel1)
4846 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004847 cdb, cdb_len, scsi3addr,
4848 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004849 else
4850 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004851 cdb, cdb_len, scsi3addr,
4852 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004853}
4854
Scott Teel6b80b182014-02-18 13:56:55 -06004855static void raid_map_helper(struct raid_map_data *map,
4856 int offload_to_mirror, u32 *map_index, u32 *current_group)
4857{
4858 if (offload_to_mirror == 0) {
4859 /* use physical disk in the first mirrored group. */
Don Brace2b08b3e2015-01-23 16:41:09 -06004860 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004861 return;
4862 }
4863 do {
4864 /* determine mirror group that *map_index indicates */
Don Brace2b08b3e2015-01-23 16:41:09 -06004865 *current_group = *map_index /
4866 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004867 if (offload_to_mirror == *current_group)
4868 continue;
Don Brace2b08b3e2015-01-23 16:41:09 -06004869 if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
Scott Teel6b80b182014-02-18 13:56:55 -06004870 /* select map index from next group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004871 *map_index += le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004872 (*current_group)++;
4873 } else {
4874 /* select map index from first group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004875 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004876 *current_group = 0;
4877 }
4878 } while (offload_to_mirror != *current_group);
4879}
4880
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004881/*
4882 * Attempt to perform offload RAID mapping for a logical volume I/O.
4883 */
4884static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4885 struct CommandList *c)
4886{
4887 struct scsi_cmnd *cmd = c->scsi_cmd;
4888 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4889 struct raid_map_data *map = &dev->raid_map;
4890 struct raid_map_disk_data *dd = &map->data[0];
4891 int is_write = 0;
4892 u32 map_index;
4893 u64 first_block, last_block;
4894 u32 block_cnt;
4895 u32 blocks_per_row;
4896 u64 first_row, last_row;
4897 u32 first_row_offset, last_row_offset;
4898 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06004899 u64 r0_first_row, r0_last_row;
4900 u32 r5or6_blocks_per_row;
4901 u64 r5or6_first_row, r5or6_last_row;
4902 u32 r5or6_first_row_offset, r5or6_last_row_offset;
4903 u32 r5or6_first_column, r5or6_last_column;
4904 u32 total_disks_per_row;
4905 u32 stripesize;
4906 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004907 u32 map_row;
4908 u32 disk_handle;
4909 u64 disk_block;
4910 u32 disk_block_cnt;
4911 u8 cdb[16];
4912 u8 cdb_len;
Don Brace2b08b3e2015-01-23 16:41:09 -06004913 u16 strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004914#if BITS_PER_LONG == 32
4915 u64 tmpdiv;
4916#endif
Scott Teel6b80b182014-02-18 13:56:55 -06004917 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004918
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004919 /* check for valid opcode, get LBA and block count */
4920 switch (cmd->cmnd[0]) {
4921 case WRITE_6:
4922 is_write = 1;
4923 case READ_6:
Don Bracec8a6c9a2015-11-04 15:50:50 -06004924 first_block = get_unaligned_be16(&cmd->cmnd[2]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004925 block_cnt = cmd->cmnd[4];
Stephen M. Cameron3fa89a02014-07-03 10:18:14 -05004926 if (block_cnt == 0)
4927 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004928 break;
4929 case WRITE_10:
4930 is_write = 1;
4931 case READ_10:
4932 first_block =
4933 (((u64) cmd->cmnd[2]) << 24) |
4934 (((u64) cmd->cmnd[3]) << 16) |
4935 (((u64) cmd->cmnd[4]) << 8) |
4936 cmd->cmnd[5];
4937 block_cnt =
4938 (((u32) cmd->cmnd[7]) << 8) |
4939 cmd->cmnd[8];
4940 break;
4941 case WRITE_12:
4942 is_write = 1;
4943 case READ_12:
4944 first_block =
4945 (((u64) cmd->cmnd[2]) << 24) |
4946 (((u64) cmd->cmnd[3]) << 16) |
4947 (((u64) cmd->cmnd[4]) << 8) |
4948 cmd->cmnd[5];
4949 block_cnt =
4950 (((u32) cmd->cmnd[6]) << 24) |
4951 (((u32) cmd->cmnd[7]) << 16) |
4952 (((u32) cmd->cmnd[8]) << 8) |
4953 cmd->cmnd[9];
4954 break;
4955 case WRITE_16:
4956 is_write = 1;
4957 case READ_16:
4958 first_block =
4959 (((u64) cmd->cmnd[2]) << 56) |
4960 (((u64) cmd->cmnd[3]) << 48) |
4961 (((u64) cmd->cmnd[4]) << 40) |
4962 (((u64) cmd->cmnd[5]) << 32) |
4963 (((u64) cmd->cmnd[6]) << 24) |
4964 (((u64) cmd->cmnd[7]) << 16) |
4965 (((u64) cmd->cmnd[8]) << 8) |
4966 cmd->cmnd[9];
4967 block_cnt =
4968 (((u32) cmd->cmnd[10]) << 24) |
4969 (((u32) cmd->cmnd[11]) << 16) |
4970 (((u32) cmd->cmnd[12]) << 8) |
4971 cmd->cmnd[13];
4972 break;
4973 default:
4974 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4975 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004976 last_block = first_block + block_cnt - 1;
4977
4978 /* check for write to non-RAID-0 */
4979 if (is_write && dev->raid_level != 0)
4980 return IO_ACCEL_INELIGIBLE;
4981
4982 /* check for invalid block or wraparound */
Don Brace2b08b3e2015-01-23 16:41:09 -06004983 if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
4984 last_block < first_block)
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004985 return IO_ACCEL_INELIGIBLE;
4986
4987 /* calculate stripe information for the request */
Don Brace2b08b3e2015-01-23 16:41:09 -06004988 blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
4989 le16_to_cpu(map->strip_size);
4990 strip_size = le16_to_cpu(map->strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004991#if BITS_PER_LONG == 32
4992 tmpdiv = first_block;
4993 (void) do_div(tmpdiv, blocks_per_row);
4994 first_row = tmpdiv;
4995 tmpdiv = last_block;
4996 (void) do_div(tmpdiv, blocks_per_row);
4997 last_row = tmpdiv;
4998 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4999 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
5000 tmpdiv = first_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06005001 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005002 first_column = tmpdiv;
5003 tmpdiv = last_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06005004 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005005 last_column = tmpdiv;
5006#else
5007 first_row = first_block / blocks_per_row;
5008 last_row = last_block / blocks_per_row;
5009 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
5010 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
Don Brace2b08b3e2015-01-23 16:41:09 -06005011 first_column = first_row_offset / strip_size;
5012 last_column = last_row_offset / strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005013#endif
5014
5015 /* if this isn't a single row/column then give to the controller */
5016 if ((first_row != last_row) || (first_column != last_column))
5017 return IO_ACCEL_INELIGIBLE;
5018
5019 /* proceeding with driver mapping */
Don Brace2b08b3e2015-01-23 16:41:09 -06005020 total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
5021 le16_to_cpu(map->metadata_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005022 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06005023 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06005024 map_index = (map_row * total_disks_per_row) + first_column;
5025
5026 switch (dev->raid_level) {
5027 case HPSA_RAID_0:
5028 break; /* nothing special to do */
5029 case HPSA_RAID_1:
5030 /* Handles load balance across RAID 1 members.
5031 * (2-drive R1 and R10 with even # of drives.)
5032 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005033 */
Don Brace2b08b3e2015-01-23 16:41:09 -06005034 BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005035 if (dev->offload_to_mirror)
Don Brace2b08b3e2015-01-23 16:41:09 -06005036 map_index += le16_to_cpu(map->data_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005037 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06005038 break;
5039 case HPSA_RAID_ADM:
5040 /* Handles N-way mirrors (R1-ADM)
5041 * and R10 with # of drives divisible by 3.)
5042 */
Don Brace2b08b3e2015-01-23 16:41:09 -06005043 BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
Scott Teel6b80b182014-02-18 13:56:55 -06005044
5045 offload_to_mirror = dev->offload_to_mirror;
5046 raid_map_helper(map, offload_to_mirror,
5047 &map_index, &current_group);
5048 /* set mirror group to use next time */
5049 offload_to_mirror =
Don Brace2b08b3e2015-01-23 16:41:09 -06005050 (offload_to_mirror >=
5051 le16_to_cpu(map->layout_map_count) - 1)
Scott Teel6b80b182014-02-18 13:56:55 -06005052 ? 0 : offload_to_mirror + 1;
Scott Teel6b80b182014-02-18 13:56:55 -06005053 dev->offload_to_mirror = offload_to_mirror;
5054 /* Avoid direct use of dev->offload_to_mirror within this
5055 * function since multiple threads might simultaneously
5056 * increment it beyond the range of dev->layout_map_count -1.
5057 */
5058 break;
5059 case HPSA_RAID_5:
5060 case HPSA_RAID_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06005061 if (le16_to_cpu(map->layout_map_count) <= 1)
Scott Teel6b80b182014-02-18 13:56:55 -06005062 break;
5063
5064 /* Verify first and last block are in same RAID group */
5065 r5or6_blocks_per_row =
Don Brace2b08b3e2015-01-23 16:41:09 -06005066 le16_to_cpu(map->strip_size) *
5067 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06005068 BUG_ON(r5or6_blocks_per_row == 0);
Don Brace2b08b3e2015-01-23 16:41:09 -06005069 stripesize = r5or6_blocks_per_row *
5070 le16_to_cpu(map->layout_map_count);
Scott Teel6b80b182014-02-18 13:56:55 -06005071#if BITS_PER_LONG == 32
5072 tmpdiv = first_block;
5073 first_group = do_div(tmpdiv, stripesize);
5074 tmpdiv = first_group;
5075 (void) do_div(tmpdiv, r5or6_blocks_per_row);
5076 first_group = tmpdiv;
5077 tmpdiv = last_block;
5078 last_group = do_div(tmpdiv, stripesize);
5079 tmpdiv = last_group;
5080 (void) do_div(tmpdiv, r5or6_blocks_per_row);
5081 last_group = tmpdiv;
5082#else
5083 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
5084 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
Scott Teel6b80b182014-02-18 13:56:55 -06005085#endif
Stephen M. Cameron000ff7c2014-03-13 17:12:50 -05005086 if (first_group != last_group)
Scott Teel6b80b182014-02-18 13:56:55 -06005087 return IO_ACCEL_INELIGIBLE;
5088
5089 /* Verify request is in a single row of RAID 5/6 */
5090#if BITS_PER_LONG == 32
5091 tmpdiv = first_block;
5092 (void) do_div(tmpdiv, stripesize);
5093 first_row = r5or6_first_row = r0_first_row = tmpdiv;
5094 tmpdiv = last_block;
5095 (void) do_div(tmpdiv, stripesize);
5096 r5or6_last_row = r0_last_row = tmpdiv;
5097#else
5098 first_row = r5or6_first_row = r0_first_row =
5099 first_block / stripesize;
5100 r5or6_last_row = r0_last_row = last_block / stripesize;
5101#endif
5102 if (r5or6_first_row != r5or6_last_row)
5103 return IO_ACCEL_INELIGIBLE;
5104
5105
5106 /* Verify request is in a single column */
5107#if BITS_PER_LONG == 32
5108 tmpdiv = first_block;
5109 first_row_offset = do_div(tmpdiv, stripesize);
5110 tmpdiv = first_row_offset;
5111 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
5112 r5or6_first_row_offset = first_row_offset;
5113 tmpdiv = last_block;
5114 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
5115 tmpdiv = r5or6_last_row_offset;
5116 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
5117 tmpdiv = r5or6_first_row_offset;
5118 (void) do_div(tmpdiv, map->strip_size);
5119 first_column = r5or6_first_column = tmpdiv;
5120 tmpdiv = r5or6_last_row_offset;
5121 (void) do_div(tmpdiv, map->strip_size);
5122 r5or6_last_column = tmpdiv;
5123#else
5124 first_row_offset = r5or6_first_row_offset =
5125 (u32)((first_block % stripesize) %
5126 r5or6_blocks_per_row);
5127
5128 r5or6_last_row_offset =
5129 (u32)((last_block % stripesize) %
5130 r5or6_blocks_per_row);
5131
5132 first_column = r5or6_first_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06005133 r5or6_first_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06005134 r5or6_last_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06005135 r5or6_last_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06005136#endif
5137 if (r5or6_first_column != r5or6_last_column)
5138 return IO_ACCEL_INELIGIBLE;
5139
5140 /* Request is eligible */
5141 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06005142 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06005143
5144 map_index = (first_group *
Don Brace2b08b3e2015-01-23 16:41:09 -06005145 (le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
Scott Teel6b80b182014-02-18 13:56:55 -06005146 (map_row * total_disks_per_row) + first_column;
5147 break;
5148 default:
5149 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005150 }
Scott Teel6b80b182014-02-18 13:56:55 -06005151
Stephen Cameron07543e02015-01-23 16:44:14 -06005152 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
5153 return IO_ACCEL_INELIGIBLE;
5154
Don Brace03383732015-01-23 16:43:30 -06005155 c->phys_disk = dev->phys_disk[map_index];
Don Bracec3390df2016-02-23 15:16:34 -06005156 if (!c->phys_disk)
5157 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06005158
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005159 disk_handle = dd[map_index].ioaccel_handle;
Don Brace2b08b3e2015-01-23 16:41:09 -06005160 disk_block = le64_to_cpu(map->disk_starting_blk) +
5161 first_row * le16_to_cpu(map->strip_size) +
5162 (first_row_offset - first_column *
5163 le16_to_cpu(map->strip_size));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005164 disk_block_cnt = block_cnt;
5165
5166 /* handle differing logical/physical block sizes */
5167 if (map->phys_blk_shift) {
5168 disk_block <<= map->phys_blk_shift;
5169 disk_block_cnt <<= map->phys_blk_shift;
5170 }
5171 BUG_ON(disk_block_cnt > 0xffff);
5172
5173 /* build the new CDB for the physical disk I/O */
5174 if (disk_block > 0xffffffff) {
5175 cdb[0] = is_write ? WRITE_16 : READ_16;
5176 cdb[1] = 0;
5177 cdb[2] = (u8) (disk_block >> 56);
5178 cdb[3] = (u8) (disk_block >> 48);
5179 cdb[4] = (u8) (disk_block >> 40);
5180 cdb[5] = (u8) (disk_block >> 32);
5181 cdb[6] = (u8) (disk_block >> 24);
5182 cdb[7] = (u8) (disk_block >> 16);
5183 cdb[8] = (u8) (disk_block >> 8);
5184 cdb[9] = (u8) (disk_block);
5185 cdb[10] = (u8) (disk_block_cnt >> 24);
5186 cdb[11] = (u8) (disk_block_cnt >> 16);
5187 cdb[12] = (u8) (disk_block_cnt >> 8);
5188 cdb[13] = (u8) (disk_block_cnt);
5189 cdb[14] = 0;
5190 cdb[15] = 0;
5191 cdb_len = 16;
5192 } else {
5193 cdb[0] = is_write ? WRITE_10 : READ_10;
5194 cdb[1] = 0;
5195 cdb[2] = (u8) (disk_block >> 24);
5196 cdb[3] = (u8) (disk_block >> 16);
5197 cdb[4] = (u8) (disk_block >> 8);
5198 cdb[5] = (u8) (disk_block);
5199 cdb[6] = 0;
5200 cdb[7] = (u8) (disk_block_cnt >> 8);
5201 cdb[8] = (u8) (disk_block_cnt);
5202 cdb[9] = 0;
5203 cdb_len = 10;
5204 }
5205 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
Don Brace03383732015-01-23 16:43:30 -06005206 dev->scsi3addr,
5207 dev->phys_disk[map_index]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005208}
5209
Webb Scales25163bd2015-04-23 09:32:00 -05005210/*
5211 * Submit commands down the "normal" RAID stack path
5212 * All callers to hpsa_ciss_submit must check lockup_detected
5213 * beforehand, before (opt.) and after calling cmd_alloc
5214 */
Stephen Cameron574f05d2015-01-23 16:43:20 -06005215static int hpsa_ciss_submit(struct ctlr_info *h,
5216 struct CommandList *c, struct scsi_cmnd *cmd,
5217 unsigned char scsi3addr[])
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005218{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005219 cmd->host_scribble = (unsigned char *) c;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005220 c->cmd_type = CMD_SCSI;
5221 c->scsi_cmd = cmd;
5222 c->Header.ReplyQueue = 0; /* unused in simple mode */
5223 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Bracef2405db2015-01-23 16:43:09 -06005224 c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005225
5226 /* Fill in the request block... */
5227
5228 c->Request.Timeout = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005229 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
5230 c->Request.CDBLen = cmd->cmd_len;
5231 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005232 switch (cmd->sc_data_direction) {
5233 case DMA_TO_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005234 c->Request.type_attr_dir =
5235 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005236 break;
5237 case DMA_FROM_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005238 c->Request.type_attr_dir =
5239 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005240 break;
5241 case DMA_NONE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005242 c->Request.type_attr_dir =
5243 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005244 break;
5245 case DMA_BIDIRECTIONAL:
5246 /* This can happen if a buggy application does a scsi passthru
5247 * and sets both inlen and outlen to non-zero. ( see
5248 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
5249 */
5250
Stephen M. Camerona505b862014-11-14 17:27:04 -06005251 c->Request.type_attr_dir =
5252 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005253 /* This is technically wrong, and hpsa controllers should
5254 * reject it with CMD_INVALID, which is the most correct
5255 * response, but non-fibre backends appear to let it
5256 * slide by, and give the same results as if this field
5257 * were set correctly. Either way is acceptable for
5258 * our purposes here.
5259 */
5260
5261 break;
5262
5263 default:
5264 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
5265 cmd->sc_data_direction);
5266 BUG();
5267 break;
5268 }
5269
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005270 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Webb Scales73153fe2015-04-23 09:35:04 -05005271 hpsa_cmd_resolve_and_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005272 return SCSI_MLQUEUE_HOST_BUSY;
5273 }
5274 enqueue_cmd_and_start_io(h, c);
5275 /* the cmd'll come back via intr handler in complete_scsi_command() */
5276 return 0;
5277}
5278
Stephen Cameron360c73b2015-04-23 09:32:32 -05005279static void hpsa_cmd_init(struct ctlr_info *h, int index,
5280 struct CommandList *c)
5281{
5282 dma_addr_t cmd_dma_handle, err_dma_handle;
5283
5284 /* Zero out all of commandlist except the last field, refcount */
5285 memset(c, 0, offsetof(struct CommandList, refcount));
5286 c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
5287 cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
5288 c->err_info = h->errinfo_pool + index;
5289 memset(c->err_info, 0, sizeof(*c->err_info));
5290 err_dma_handle = h->errinfo_pool_dhandle
5291 + index * sizeof(*c->err_info);
5292 c->cmdindex = index;
5293 c->busaddr = (u32) cmd_dma_handle;
5294 c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
5295 c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
5296 c->h = h;
Webb Scalesa58e7e52015-04-23 09:34:16 -05005297 c->scsi_cmd = SCSI_CMD_IDLE;
Stephen Cameron360c73b2015-04-23 09:32:32 -05005298}
5299
5300static void hpsa_preinitialize_commands(struct ctlr_info *h)
5301{
5302 int i;
5303
5304 for (i = 0; i < h->nr_cmds; i++) {
5305 struct CommandList *c = h->cmd_pool + i;
5306
5307 hpsa_cmd_init(h, i, c);
5308 atomic_set(&c->refcount, 0);
5309 }
5310}
5311
5312static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
5313 struct CommandList *c)
5314{
5315 dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
5316
Webb Scales73153fe2015-04-23 09:35:04 -05005317 BUG_ON(c->cmdindex != index);
5318
Stephen Cameron360c73b2015-04-23 09:32:32 -05005319 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
5320 memset(c->err_info, 0, sizeof(*c->err_info));
5321 c->busaddr = (u32) cmd_dma_handle;
5322}
5323
Webb Scales592a0ad2015-04-23 09:32:48 -05005324static int hpsa_ioaccel_submit(struct ctlr_info *h,
5325 struct CommandList *c, struct scsi_cmnd *cmd,
5326 unsigned char *scsi3addr)
5327{
5328 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
5329 int rc = IO_ACCEL_INELIGIBLE;
5330
5331 cmd->host_scribble = (unsigned char *) c;
5332
5333 if (dev->offload_enabled) {
5334 hpsa_cmd_init(h, c->cmdindex, c);
5335 c->cmd_type = CMD_SCSI;
5336 c->scsi_cmd = cmd;
5337 rc = hpsa_scsi_ioaccel_raid_map(h, c);
5338 if (rc < 0) /* scsi_dma_map failed. */
5339 rc = SCSI_MLQUEUE_HOST_BUSY;
Joe Handzika3144e02015-04-23 09:32:59 -05005340 } else if (dev->hba_ioaccel_enabled) {
Webb Scales592a0ad2015-04-23 09:32:48 -05005341 hpsa_cmd_init(h, c->cmdindex, c);
5342 c->cmd_type = CMD_SCSI;
5343 c->scsi_cmd = cmd;
5344 rc = hpsa_scsi_ioaccel_direct_map(h, c);
5345 if (rc < 0) /* scsi_dma_map failed. */
5346 rc = SCSI_MLQUEUE_HOST_BUSY;
5347 }
5348 return rc;
5349}
5350
Don Brace080ef1c2015-01-23 16:43:25 -06005351static void hpsa_command_resubmit_worker(struct work_struct *work)
5352{
5353 struct scsi_cmnd *cmd;
5354 struct hpsa_scsi_dev_t *dev;
Webb Scales8a0ff922015-04-23 09:34:11 -05005355 struct CommandList *c = container_of(work, struct CommandList, work);
Don Brace080ef1c2015-01-23 16:43:25 -06005356
5357 cmd = c->scsi_cmd;
5358 dev = cmd->device->hostdata;
5359 if (!dev) {
5360 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05005361 return hpsa_cmd_free_and_done(c->h, c, cmd);
Don Brace080ef1c2015-01-23 16:43:25 -06005362 }
Webb Scalesd604f532015-04-23 09:35:22 -05005363 if (c->reset_pending)
5364 return hpsa_cmd_resolve_and_free(c->h, c);
Webb Scalesa58e7e52015-04-23 09:34:16 -05005365 if (c->abort_pending)
5366 return hpsa_cmd_abort_and_free(c->h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05005367 if (c->cmd_type == CMD_IOACCEL2) {
5368 struct ctlr_info *h = c->h;
5369 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5370 int rc;
5371
5372 if (c2->error_data.serv_response ==
5373 IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
5374 rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
5375 if (rc == 0)
5376 return;
5377 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
5378 /*
5379 * If we get here, it means dma mapping failed.
5380 * Try again via scsi mid layer, which will
5381 * then get SCSI_MLQUEUE_HOST_BUSY.
5382 */
5383 cmd->result = DID_IMM_RETRY << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05005384 return hpsa_cmd_free_and_done(h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05005385 }
5386 /* else, fall thru and resubmit down CISS path */
5387 }
5388 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05005389 hpsa_cmd_partial_init(c->h, c->cmdindex, c);
Don Brace080ef1c2015-01-23 16:43:25 -06005390 if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
5391 /*
5392 * If we get here, it means dma mapping failed. Try
5393 * again via scsi mid layer, which will then get
5394 * SCSI_MLQUEUE_HOST_BUSY.
Webb Scales592a0ad2015-04-23 09:32:48 -05005395 *
5396 * hpsa_ciss_submit will have already freed c
5397 * if it encountered a dma mapping failure.
Don Brace080ef1c2015-01-23 16:43:25 -06005398 */
5399 cmd->result = DID_IMM_RETRY << 16;
5400 cmd->scsi_done(cmd);
5401 }
5402}
5403
Stephen Cameron574f05d2015-01-23 16:43:20 -06005404/* Running in struct Scsi_Host->host_lock less mode */
5405static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
5406{
5407 struct ctlr_info *h;
5408 struct hpsa_scsi_dev_t *dev;
5409 unsigned char scsi3addr[8];
5410 struct CommandList *c;
5411 int rc = 0;
5412
5413 /* Get the ptr to our adapter structure out of cmd->host. */
5414 h = sdev_to_hba(cmd->device);
Webb Scales73153fe2015-04-23 09:35:04 -05005415
5416 BUG_ON(cmd->request->tag < 0);
5417
Stephen Cameron574f05d2015-01-23 16:43:20 -06005418 dev = cmd->device->hostdata;
5419 if (!dev) {
Don Braceba74fdc2016-04-27 17:14:17 -05005420 cmd->result = NOT_READY << 16; /* host byte */
5421 cmd->scsi_done(cmd);
5422 return 0;
5423 }
5424
5425 if (dev->removed) {
Stephen Cameron574f05d2015-01-23 16:43:20 -06005426 cmd->result = DID_NO_CONNECT << 16;
5427 cmd->scsi_done(cmd);
5428 return 0;
5429 }
Webb Scales73153fe2015-04-23 09:35:04 -05005430
Stephen Cameron574f05d2015-01-23 16:43:20 -06005431 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
5432
5433 if (unlikely(lockup_detected(h))) {
Webb Scales25163bd2015-04-23 09:32:00 -05005434 cmd->result = DID_NO_CONNECT << 16;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005435 cmd->scsi_done(cmd);
5436 return 0;
5437 }
Webb Scales73153fe2015-04-23 09:35:04 -05005438 c = cmd_tagged_alloc(h, cmd);
Stephen Cameron574f05d2015-01-23 16:43:20 -06005439
Stephen Cameron407863c2015-01-23 16:44:19 -06005440 /*
5441 * Call alternate submit routine for I/O accelerated commands.
Stephen Cameron574f05d2015-01-23 16:43:20 -06005442 * Retries always go down the normal I/O path.
5443 */
5444 if (likely(cmd->retries == 0 &&
5445 cmd->request->cmd_type == REQ_TYPE_FS &&
5446 h->acciopath_status)) {
Webb Scales592a0ad2015-04-23 09:32:48 -05005447 rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
5448 if (rc == 0)
5449 return 0;
5450 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
Webb Scales73153fe2015-04-23 09:35:04 -05005451 hpsa_cmd_resolve_and_free(h, c);
Webb Scales592a0ad2015-04-23 09:32:48 -05005452 return SCSI_MLQUEUE_HOST_BUSY;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005453 }
5454 }
5455 return hpsa_ciss_submit(h, c, cmd, scsi3addr);
5456}
5457
Webb Scales8ebc9242015-01-23 16:44:50 -06005458static void hpsa_scan_complete(struct ctlr_info *h)
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005459{
5460 unsigned long flags;
5461
Webb Scales8ebc9242015-01-23 16:44:50 -06005462 spin_lock_irqsave(&h->scan_lock, flags);
5463 h->scan_finished = 1;
5464 wake_up_all(&h->scan_wait_queue);
5465 spin_unlock_irqrestore(&h->scan_lock, flags);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005466}
5467
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005468static void hpsa_scan_start(struct Scsi_Host *sh)
5469{
5470 struct ctlr_info *h = shost_to_hba(sh);
5471 unsigned long flags;
5472
Webb Scales8ebc9242015-01-23 16:44:50 -06005473 /*
5474 * Don't let rescans be initiated on a controller known to be locked
5475 * up. If the controller locks up *during* a rescan, that thread is
5476 * probably hosed, but at least we can prevent new rescan threads from
5477 * piling up on a locked up controller.
5478 */
5479 if (unlikely(lockup_detected(h)))
5480 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005481
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005482 /* wait until any scan already in progress is finished. */
5483 while (1) {
5484 spin_lock_irqsave(&h->scan_lock, flags);
5485 if (h->scan_finished)
5486 break;
5487 spin_unlock_irqrestore(&h->scan_lock, flags);
5488 wait_event(h->scan_wait_queue, h->scan_finished);
5489 /* Note: We don't need to worry about a race between this
5490 * thread and driver unload because the midlayer will
5491 * have incremented the reference count, so unload won't
5492 * happen if we're in here.
5493 */
5494 }
5495 h->scan_finished = 0; /* mark scan as in progress */
5496 spin_unlock_irqrestore(&h->scan_lock, flags);
5497
Webb Scales8ebc9242015-01-23 16:44:50 -06005498 if (unlikely(lockup_detected(h)))
5499 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005500
Don Brace8aa60682015-11-04 15:50:01 -06005501 hpsa_update_scsi_devices(h);
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005502
Webb Scales8ebc9242015-01-23 16:44:50 -06005503 hpsa_scan_complete(h);
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005504}
5505
Don Brace7c0a0222015-01-23 16:41:30 -06005506static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
5507{
Don Brace03383732015-01-23 16:43:30 -06005508 struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
5509
5510 if (!logical_drive)
5511 return -ENODEV;
Don Brace7c0a0222015-01-23 16:41:30 -06005512
5513 if (qdepth < 1)
5514 qdepth = 1;
Don Brace03383732015-01-23 16:43:30 -06005515 else if (qdepth > logical_drive->queue_depth)
5516 qdepth = logical_drive->queue_depth;
5517
5518 return scsi_change_queue_depth(sdev, qdepth);
Don Brace7c0a0222015-01-23 16:41:30 -06005519}
5520
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005521static int hpsa_scan_finished(struct Scsi_Host *sh,
5522 unsigned long elapsed_time)
5523{
5524 struct ctlr_info *h = shost_to_hba(sh);
5525 unsigned long flags;
5526 int finished;
5527
5528 spin_lock_irqsave(&h->scan_lock, flags);
5529 finished = h->scan_finished;
5530 spin_unlock_irqrestore(&h->scan_lock, flags);
5531 return finished;
5532}
5533
Robert Elliott2946e822015-04-23 09:35:09 -05005534static int hpsa_scsi_host_alloc(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005535{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005536 struct Scsi_Host *sh;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005537
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005538 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
Robert Elliott2946e822015-04-23 09:35:09 -05005539 if (sh == NULL) {
5540 dev_err(&h->pdev->dev, "scsi_host_alloc failed\n");
5541 return -ENOMEM;
5542 }
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005543
5544 sh->io_port = 0;
5545 sh->n_io_port = 0;
5546 sh->this_id = -1;
5547 sh->max_channel = 3;
5548 sh->max_cmd_len = MAX_COMMAND_SIZE;
5549 sh->max_lun = HPSA_MAX_LUN;
5550 sh->max_id = HPSA_MAX_LUN;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05005551 sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
Don Brace03383732015-01-23 16:43:30 -06005552 sh->cmd_per_lun = sh->can_queue;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005553 sh->sg_tablesize = h->maxsgentries;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06005554 sh->transportt = hpsa_sas_transport_template;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005555 sh->hostdata[0] = (unsigned long) h;
5556 sh->irq = h->intr[h->intr_mode];
5557 sh->unique_id = sh->irq;
Christoph Hellwig64d513a2015-10-08 09:28:04 +01005558
Robert Elliott2946e822015-04-23 09:35:09 -05005559 h->scsi_host = sh;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005560 return 0;
Robert Elliott2946e822015-04-23 09:35:09 -05005561}
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005562
Robert Elliott2946e822015-04-23 09:35:09 -05005563static int hpsa_scsi_add_host(struct ctlr_info *h)
5564{
5565 int rv;
5566
5567 rv = scsi_add_host(h->scsi_host, &h->pdev->dev);
5568 if (rv) {
5569 dev_err(&h->pdev->dev, "scsi_add_host failed\n");
5570 return rv;
5571 }
5572 scsi_scan_host(h->scsi_host);
5573 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005574}
5575
Webb Scalesb69324f2015-04-23 09:34:22 -05005576/*
Webb Scales73153fe2015-04-23 09:35:04 -05005577 * The block layer has already gone to the trouble of picking out a unique,
5578 * small-integer tag for this request. We use an offset from that value as
5579 * an index to select our command block. (The offset allows us to reserve the
5580 * low-numbered entries for our own uses.)
5581 */
5582static int hpsa_get_cmd_index(struct scsi_cmnd *scmd)
5583{
5584 int idx = scmd->request->tag;
5585
5586 if (idx < 0)
5587 return idx;
5588
5589 /* Offset to leave space for internal cmds. */
5590 return idx += HPSA_NRESERVED_CMDS;
5591}
5592
5593/*
Webb Scalesb69324f2015-04-23 09:34:22 -05005594 * Send a TEST_UNIT_READY command to the specified LUN using the specified
5595 * reply queue; returns zero if the unit is ready, and non-zero otherwise.
5596 */
5597static int hpsa_send_test_unit_ready(struct ctlr_info *h,
5598 struct CommandList *c, unsigned char lunaddr[],
5599 int reply_queue)
5600{
5601 int rc;
5602
5603 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
5604 (void) fill_cmd(c, TEST_UNIT_READY, h,
5605 NULL, 0, 0, lunaddr, TYPE_CMD);
Don Bracec448ecf2016-04-27 17:13:51 -05005606 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Webb Scalesb69324f2015-04-23 09:34:22 -05005607 if (rc)
5608 return rc;
5609 /* no unmap needed here because no data xfer. */
5610
5611 /* Check if the unit is already ready. */
5612 if (c->err_info->CommandStatus == CMD_SUCCESS)
5613 return 0;
5614
5615 /*
5616 * The first command sent after reset will receive "unit attention" to
5617 * indicate that the LUN has been reset...this is actually what we're
5618 * looking for (but, success is good too).
5619 */
5620 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5621 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
5622 (c->err_info->SenseInfo[2] == NO_SENSE ||
5623 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
5624 return 0;
5625
5626 return 1;
5627}
5628
5629/*
5630 * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
5631 * returns zero when the unit is ready, and non-zero when giving up.
5632 */
5633static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
5634 struct CommandList *c,
5635 unsigned char lunaddr[], int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005636{
Tomas Henzl89193582014-02-21 16:25:05 -06005637 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005638 int count = 0;
5639 int waittime = 1; /* seconds */
Webb Scalesb69324f2015-04-23 09:34:22 -05005640
5641 /* Send test unit ready until device ready, or give up. */
5642 for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
5643
5644 /*
5645 * Wait for a bit. do this first, because if we send
5646 * the TUR right away, the reset will just abort it.
5647 */
5648 msleep(1000 * waittime);
5649
5650 rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
5651 if (!rc)
5652 break;
5653
5654 /* Increase wait time with each try, up to a point. */
5655 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
5656 waittime *= 2;
5657
5658 dev_warn(&h->pdev->dev,
5659 "waiting %d secs for device to become ready.\n",
5660 waittime);
5661 }
5662
5663 return rc;
5664}
5665
5666static int wait_for_device_to_become_ready(struct ctlr_info *h,
5667 unsigned char lunaddr[],
5668 int reply_queue)
5669{
5670 int first_queue;
5671 int last_queue;
5672 int rq;
5673 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005674 struct CommandList *c;
5675
Stephen Cameron45fcb862015-01-23 16:43:04 -06005676 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005677
Webb Scalesb69324f2015-04-23 09:34:22 -05005678 /*
5679 * If no specific reply queue was requested, then send the TUR
5680 * repeatedly, requesting a reply on each reply queue; otherwise execute
5681 * the loop exactly once using only the specified queue.
5682 */
5683 if (reply_queue == DEFAULT_REPLY_QUEUE) {
5684 first_queue = 0;
5685 last_queue = h->nreply_queues - 1;
5686 } else {
5687 first_queue = reply_queue;
5688 last_queue = reply_queue;
5689 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005690
Webb Scalesb69324f2015-04-23 09:34:22 -05005691 for (rq = first_queue; rq <= last_queue; rq++) {
5692 rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
Webb Scales25163bd2015-04-23 09:32:00 -05005693 if (rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005694 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005695 }
5696
5697 if (rc)
5698 dev_warn(&h->pdev->dev, "giving up on device.\n");
5699 else
5700 dev_warn(&h->pdev->dev, "device is ready.\n");
5701
Stephen Cameron45fcb862015-01-23 16:43:04 -06005702 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005703 return rc;
5704}
5705
5706/* Need at least one of these error handlers to keep ../scsi/hosts.c from
5707 * complaining. Doing a host- or bus-reset can't do anything good here.
5708 */
5709static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
5710{
5711 int rc;
5712 struct ctlr_info *h;
5713 struct hpsa_scsi_dev_t *dev;
Scott Teel0b9b7b62015-11-04 15:51:02 -06005714 u8 reset_type;
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005715 char msg[48];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005716
5717 /* find the controller to which the command to be aborted was sent */
5718 h = sdev_to_hba(scsicmd->device);
5719 if (h == NULL) /* paranoia */
5720 return FAILED;
Don Bracee3458932015-01-23 16:44:24 -06005721
5722 if (lockup_detected(h))
5723 return FAILED;
5724
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005725 dev = scsicmd->device->hostdata;
5726 if (!dev) {
Webb Scalesd604f532015-04-23 09:35:22 -05005727 dev_err(&h->pdev->dev, "%s: device lookup failed\n", __func__);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005728 return FAILED;
5729 }
Webb Scales25163bd2015-04-23 09:32:00 -05005730
5731 /* if controller locked up, we can guarantee command won't complete */
5732 if (lockup_detected(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005733 snprintf(msg, sizeof(msg),
5734 "cmd %d RESET FAILED, lockup detected",
5735 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005736 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005737 return FAILED;
5738 }
5739
5740 /* this reset request might be the result of a lockup; check */
5741 if (detect_controller_lockup(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005742 snprintf(msg, sizeof(msg),
5743 "cmd %d RESET FAILED, new lockup detected",
5744 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005745 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005746 return FAILED;
5747 }
5748
Webb Scalesd604f532015-04-23 09:35:22 -05005749 /* Do not attempt on controller */
5750 if (is_hba_lunid(dev->scsi3addr))
5751 return SUCCESS;
5752
Scott Teel0b9b7b62015-11-04 15:51:02 -06005753 if (is_logical_dev_addr_mode(dev->scsi3addr))
5754 reset_type = HPSA_DEVICE_RESET_MSG;
5755 else
5756 reset_type = HPSA_PHYS_TARGET_RESET;
5757
5758 sprintf(msg, "resetting %s",
5759 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ");
5760 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005761
Don Braceda03ded2015-11-04 15:50:56 -06005762 h->reset_in_progress = 1;
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06005763
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005764 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teel0b9b7b62015-11-04 15:51:02 -06005765 rc = hpsa_do_reset(h, dev, dev->scsi3addr, reset_type,
Webb Scalesd604f532015-04-23 09:35:22 -05005766 DEFAULT_REPLY_QUEUE);
Scott Teel0b9b7b62015-11-04 15:51:02 -06005767 sprintf(msg, "reset %s %s",
5768 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ",
5769 rc == 0 ? "completed successfully" : "failed");
Webb Scalesd604f532015-04-23 09:35:22 -05005770 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Don Braceda03ded2015-11-04 15:50:56 -06005771 h->reset_in_progress = 0;
Webb Scalesd604f532015-04-23 09:35:22 -05005772 return rc == 0 ? SUCCESS : FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005773}
5774
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005775static void swizzle_abort_tag(u8 *tag)
5776{
5777 u8 original_tag[8];
5778
5779 memcpy(original_tag, tag, 8);
5780 tag[0] = original_tag[3];
5781 tag[1] = original_tag[2];
5782 tag[2] = original_tag[1];
5783 tag[3] = original_tag[0];
5784 tag[4] = original_tag[7];
5785 tag[5] = original_tag[6];
5786 tag[6] = original_tag[5];
5787 tag[7] = original_tag[4];
5788}
5789
Scott Teel17eb87d2014-02-18 13:55:28 -06005790static void hpsa_get_tag(struct ctlr_info *h,
Don Brace2b08b3e2015-01-23 16:41:09 -06005791 struct CommandList *c, __le32 *taglower, __le32 *tagupper)
Scott Teel17eb87d2014-02-18 13:55:28 -06005792{
Don Brace2b08b3e2015-01-23 16:41:09 -06005793 u64 tag;
Scott Teel17eb87d2014-02-18 13:55:28 -06005794 if (c->cmd_type == CMD_IOACCEL1) {
5795 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
5796 &h->ioaccel_cmd_pool[c->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06005797 tag = le64_to_cpu(cm1->tag);
5798 *tagupper = cpu_to_le32(tag >> 32);
5799 *taglower = cpu_to_le32(tag);
Scott Teel54b6e9e2014-02-18 13:56:45 -06005800 return;
Scott Teel17eb87d2014-02-18 13:55:28 -06005801 }
Scott Teel54b6e9e2014-02-18 13:56:45 -06005802 if (c->cmd_type == CMD_IOACCEL2) {
5803 struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
5804 &h->ioaccel2_cmd_pool[c->cmdindex];
Scott Teeldd0e19f2014-02-18 13:57:31 -06005805 /* upper tag not used in ioaccel2 mode */
5806 memset(tagupper, 0, sizeof(*tagupper));
5807 *taglower = cm2->Tag;
Scott Teel54b6e9e2014-02-18 13:56:45 -06005808 return;
5809 }
Don Brace2b08b3e2015-01-23 16:41:09 -06005810 tag = le64_to_cpu(c->Header.tag);
5811 *tagupper = cpu_to_le32(tag >> 32);
5812 *taglower = cpu_to_le32(tag);
Scott Teel17eb87d2014-02-18 13:55:28 -06005813}
5814
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005815static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005816 struct CommandList *abort, int reply_queue)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005817{
5818 int rc = IO_OK;
5819 struct CommandList *c;
5820 struct ErrorInfo *ei;
Don Brace2b08b3e2015-01-23 16:41:09 -06005821 __le32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005822
Stephen Cameron45fcb862015-01-23 16:43:04 -06005823 c = cmd_alloc(h);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005824
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005825 /* fill_cmd can't fail here, no buffer to map */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005826 (void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005827 0, 0, scsi3addr, TYPE_MSG);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005828 if (h->needs_abort_tags_swizzled)
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005829 swizzle_abort_tag(&c->Request.CDB[4]);
Don Bracec448ecf2016-04-27 17:13:51 -05005830 (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Scott Teel17eb87d2014-02-18 13:55:28 -06005831 hpsa_get_tag(h, abort, &taglower, &tagupper);
Webb Scales25163bd2015-04-23 09:32:00 -05005832 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06005833 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005834 /* no unmap needed here because no data xfer. */
5835
5836 ei = c->err_info;
5837 switch (ei->CommandStatus) {
5838 case CMD_SUCCESS:
5839 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05005840 case CMD_TMF_STATUS:
5841 rc = hpsa_evaluate_tmf_status(h, c);
5842 break;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005843 case CMD_UNABORTABLE: /* Very common, don't make noise. */
5844 rc = -1;
5845 break;
5846 default:
5847 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06005848 __func__, tagupper, taglower);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06005849 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005850 rc = -1;
5851 break;
5852 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06005853 cmd_free(h, c);
Scott Teeldd0e19f2014-02-18 13:57:31 -06005854 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
5855 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005856 return rc;
5857}
5858
Stephen Cameron8be986c2015-04-23 09:34:06 -05005859static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
5860 struct CommandList *command_to_abort, int reply_queue)
5861{
5862 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5863 struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
5864 struct io_accel2_cmd *c2a =
5865 &h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
Webb Scalesa58e7e52015-04-23 09:34:16 -05005866 struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
Stephen Cameron8be986c2015-04-23 09:34:06 -05005867 struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
5868
5869 /*
5870 * We're overlaying struct hpsa_tmf_struct on top of something which
5871 * was allocated as a struct io_accel2_cmd, so we better be sure it
5872 * actually fits, and doesn't overrun the error info space.
5873 */
5874 BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
5875 sizeof(struct io_accel2_cmd));
5876 BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
5877 offsetof(struct hpsa_tmf_struct, error_len) +
5878 sizeof(ac->error_len));
5879
5880 c->cmd_type = IOACCEL2_TMF;
Webb Scalesa58e7e52015-04-23 09:34:16 -05005881 c->scsi_cmd = SCSI_CMD_BUSY;
5882
Stephen Cameron8be986c2015-04-23 09:34:06 -05005883 /* Adjust the DMA address to point to the accelerated command buffer */
5884 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
5885 (c->cmdindex * sizeof(struct io_accel2_cmd));
5886 BUG_ON(c->busaddr & 0x0000007F);
5887
5888 memset(ac, 0, sizeof(*c2)); /* yes this is correct */
5889 ac->iu_type = IOACCEL2_IU_TMF_TYPE;
5890 ac->reply_queue = reply_queue;
5891 ac->tmf = IOACCEL2_TMF_ABORT;
5892 ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
5893 memset(ac->lun_id, 0, sizeof(ac->lun_id));
5894 ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
5895 ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
5896 ac->error_ptr = cpu_to_le64(c->busaddr +
5897 offsetof(struct io_accel2_cmd, error_data));
5898 ac->error_len = cpu_to_le32(sizeof(c2->error_data));
5899}
5900
Scott Teel54b6e9e2014-02-18 13:56:45 -06005901/* ioaccel2 path firmware cannot handle abort task requests.
5902 * Change abort requests to physical target reset, and send to the
5903 * address of the physical disk used for the ioaccel 2 command.
5904 * Return 0 on success (IO_OK)
5905 * -1 on failure
5906 */
5907
5908static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05005909 unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
Scott Teel54b6e9e2014-02-18 13:56:45 -06005910{
5911 int rc = IO_OK;
5912 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
5913 struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
5914 unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
5915 unsigned char *psa = &phys_scsi3addr[0];
5916
5917 /* Get a pointer to the hpsa logical device. */
Stephen Cameron7fa30302015-01-23 16:44:30 -06005918 scmd = abort->scsi_cmd;
Scott Teel54b6e9e2014-02-18 13:56:45 -06005919 dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
5920 if (dev == NULL) {
5921 dev_warn(&h->pdev->dev,
5922 "Cannot abort: no device pointer for command.\n");
5923 return -1; /* not abortable */
5924 }
5925
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005926 if (h->raid_offload_debug > 0)
5927 dev_info(&h->pdev->dev,
Webb Scales0d96ef52015-04-23 09:31:55 -05005928 "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 -06005929 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
Webb Scales0d96ef52015-04-23 09:31:55 -05005930 "Reset as abort",
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005931 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
5932 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
5933
Scott Teel54b6e9e2014-02-18 13:56:45 -06005934 if (!dev->offload_enabled) {
5935 dev_warn(&h->pdev->dev,
5936 "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
5937 return -1; /* not abortable */
5938 }
5939
5940 /* Incoming scsi3addr is logical addr. We need physical disk addr. */
5941 if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
5942 dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
5943 return -1; /* not abortable */
5944 }
5945
5946 /* send the reset */
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005947 if (h->raid_offload_debug > 0)
5948 dev_info(&h->pdev->dev,
5949 "Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5950 psa[0], psa[1], psa[2], psa[3],
5951 psa[4], psa[5], psa[6], psa[7]);
Webb Scalesd604f532015-04-23 09:35:22 -05005952 rc = hpsa_do_reset(h, dev, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
Scott Teel54b6e9e2014-02-18 13:56:45 -06005953 if (rc != 0) {
5954 dev_warn(&h->pdev->dev,
5955 "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5956 psa[0], psa[1], psa[2], psa[3],
5957 psa[4], psa[5], psa[6], psa[7]);
5958 return rc; /* failed to reset */
5959 }
5960
5961 /* wait for device to recover */
Webb Scalesb69324f2015-04-23 09:34:22 -05005962 if (wait_for_device_to_become_ready(h, psa, reply_queue) != 0) {
Scott Teel54b6e9e2014-02-18 13:56:45 -06005963 dev_warn(&h->pdev->dev,
5964 "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5965 psa[0], psa[1], psa[2], psa[3],
5966 psa[4], psa[5], psa[6], psa[7]);
5967 return -1; /* failed to recover */
5968 }
5969
5970 /* device recovered */
5971 dev_info(&h->pdev->dev,
5972 "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5973 psa[0], psa[1], psa[2], psa[3],
5974 psa[4], psa[5], psa[6], psa[7]);
5975
5976 return rc; /* success */
5977}
5978
Stephen Cameron8be986c2015-04-23 09:34:06 -05005979static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
5980 struct CommandList *abort, int reply_queue)
5981{
5982 int rc = IO_OK;
5983 struct CommandList *c;
5984 __le32 taglower, tagupper;
5985 struct hpsa_scsi_dev_t *dev;
5986 struct io_accel2_cmd *c2;
5987
5988 dev = abort->scsi_cmd->device->hostdata;
5989 if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
5990 return -1;
5991
5992 c = cmd_alloc(h);
5993 setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
5994 c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
Don Bracec448ecf2016-04-27 17:13:51 -05005995 (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Stephen Cameron8be986c2015-04-23 09:34:06 -05005996 hpsa_get_tag(h, abort, &taglower, &tagupper);
5997 dev_dbg(&h->pdev->dev,
5998 "%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
5999 __func__, tagupper, taglower);
6000 /* no unmap needed here because no data xfer. */
6001
6002 dev_dbg(&h->pdev->dev,
6003 "%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
6004 __func__, tagupper, taglower, c2->error_data.serv_response);
6005 switch (c2->error_data.serv_response) {
6006 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
6007 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
6008 rc = 0;
6009 break;
6010 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
6011 case IOACCEL2_SERV_RESPONSE_FAILURE:
6012 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
6013 rc = -1;
6014 break;
6015 default:
6016 dev_warn(&h->pdev->dev,
6017 "%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
6018 __func__, tagupper, taglower,
6019 c2->error_data.serv_response);
6020 rc = -1;
6021 }
6022 cmd_free(h, c);
6023 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
6024 tagupper, taglower);
6025 return rc;
6026}
6027
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05006028static int hpsa_send_abort_both_ways(struct ctlr_info *h,
Don Brace39f3deb2016-02-23 15:16:28 -06006029 struct hpsa_scsi_dev_t *dev, struct CommandList *abort, int reply_queue)
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05006030{
Stephen Cameron8be986c2015-04-23 09:34:06 -05006031 /*
6032 * ioccelerator mode 2 commands should be aborted via the
Scott Teel54b6e9e2014-02-18 13:56:45 -06006033 * accelerated path, since RAID path is unaware of these commands,
Stephen Cameron8be986c2015-04-23 09:34:06 -05006034 * but not all underlying firmware can handle abort TMF.
6035 * Change abort to physical device reset when abort TMF is unsupported.
Scott Teel54b6e9e2014-02-18 13:56:45 -06006036 */
Stephen Cameron8be986c2015-04-23 09:34:06 -05006037 if (abort->cmd_type == CMD_IOACCEL2) {
Don Brace39f3deb2016-02-23 15:16:28 -06006038 if ((HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags) ||
6039 dev->physical_device)
Stephen Cameron8be986c2015-04-23 09:34:06 -05006040 return hpsa_send_abort_ioaccel2(h, abort,
6041 reply_queue);
6042 else
Don Brace39f3deb2016-02-23 15:16:28 -06006043 return hpsa_send_reset_as_abort_ioaccel2(h,
6044 dev->scsi3addr,
Webb Scales25163bd2015-04-23 09:32:00 -05006045 abort, reply_queue);
Stephen Cameron8be986c2015-04-23 09:34:06 -05006046 }
Don Brace39f3deb2016-02-23 15:16:28 -06006047 return hpsa_send_abort(h, dev->scsi3addr, abort, reply_queue);
Webb Scales25163bd2015-04-23 09:32:00 -05006048}
6049
6050/* Find out which reply queue a command was meant to return on */
6051static int hpsa_extract_reply_queue(struct ctlr_info *h,
6052 struct CommandList *c)
6053{
6054 if (c->cmd_type == CMD_IOACCEL2)
6055 return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
6056 return c->Header.ReplyQueue;
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05006057}
6058
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006059/*
6060 * Limit concurrency of abort commands to prevent
6061 * over-subscription of commands
6062 */
6063static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
6064{
6065#define ABORT_CMD_WAIT_MSECS 5000
6066 return !wait_event_timeout(h->abort_cmd_wait_queue,
6067 atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
6068 msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
6069}
6070
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006071/* Send an abort for the specified command.
6072 * If the device and controller support it,
6073 * send a task abort request.
6074 */
6075static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
6076{
6077
Webb Scalesa58e7e52015-04-23 09:34:16 -05006078 int rc;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006079 struct ctlr_info *h;
6080 struct hpsa_scsi_dev_t *dev;
6081 struct CommandList *abort; /* pointer to command to be aborted */
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006082 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
6083 char msg[256]; /* For debug messaging. */
6084 int ml = 0;
Don Brace2b08b3e2015-01-23 16:41:09 -06006085 __le32 tagupper, taglower;
Webb Scales25163bd2015-04-23 09:32:00 -05006086 int refcount, reply_queue;
6087
6088 if (sc == NULL)
6089 return FAILED;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006090
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006091 if (sc->device == NULL)
6092 return FAILED;
6093
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006094 /* Find the controller of the command to be aborted */
6095 h = sdev_to_hba(sc->device);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006096 if (h == NULL)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006097 return FAILED;
6098
Webb Scales25163bd2015-04-23 09:32:00 -05006099 /* Find the device of the command to be aborted */
6100 dev = sc->device->hostdata;
6101 if (!dev) {
6102 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
6103 msg);
Don Bracee3458932015-01-23 16:44:24 -06006104 return FAILED;
Webb Scales25163bd2015-04-23 09:32:00 -05006105 }
6106
6107 /* If controller locked up, we can guarantee command won't complete */
6108 if (lockup_detected(h)) {
6109 hpsa_show_dev_msg(KERN_WARNING, h, dev,
6110 "ABORT FAILED, lockup detected");
6111 return FAILED;
6112 }
6113
6114 /* This is a good time to check if controller lockup has occurred */
6115 if (detect_controller_lockup(h)) {
6116 hpsa_show_dev_msg(KERN_WARNING, h, dev,
6117 "ABORT FAILED, new lockup detected");
6118 return FAILED;
6119 }
Don Bracee3458932015-01-23 16:44:24 -06006120
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006121 /* Check that controller supports some kind of task abort */
6122 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
6123 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
6124 return FAILED;
6125
6126 memset(msg, 0, sizeof(msg));
Robert Elliott4b761552015-04-23 09:33:54 -05006127 ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006128 h->scsi_host->host_no, sc->device->channel,
Webb Scales0d96ef52015-04-23 09:31:55 -05006129 sc->device->id, sc->device->lun,
Robert Elliott4b761552015-04-23 09:33:54 -05006130 "Aborting command", sc);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006131
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006132 /* Get SCSI command to be aborted */
6133 abort = (struct CommandList *) sc->host_scribble;
6134 if (abort == NULL) {
Webb Scales281a7fd2015-01-23 16:43:35 -06006135 /* This can happen if the command already completed. */
6136 return SUCCESS;
6137 }
6138 refcount = atomic_inc_return(&abort->refcount);
6139 if (refcount == 1) { /* Command is done already. */
6140 cmd_free(h, abort);
6141 return SUCCESS;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006142 }
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006143
6144 /* Don't bother trying the abort if we know it won't work. */
6145 if (abort->cmd_type != CMD_IOACCEL2 &&
6146 abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
6147 cmd_free(h, abort);
6148 return FAILED;
6149 }
6150
Webb Scalesa58e7e52015-04-23 09:34:16 -05006151 /*
6152 * Check that we're aborting the right command.
6153 * It's possible the CommandList already completed and got re-used.
6154 */
6155 if (abort->scsi_cmd != sc) {
6156 cmd_free(h, abort);
6157 return SUCCESS;
6158 }
6159
6160 abort->abort_pending = true;
Scott Teel17eb87d2014-02-18 13:55:28 -06006161 hpsa_get_tag(h, abort, &taglower, &tagupper);
Webb Scales25163bd2015-04-23 09:32:00 -05006162 reply_queue = hpsa_extract_reply_queue(h, abort);
Scott Teel17eb87d2014-02-18 13:55:28 -06006163 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen Cameron7fa30302015-01-23 16:44:30 -06006164 as = abort->scsi_cmd;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006165 if (as != NULL)
Robert Elliott4b761552015-04-23 09:33:54 -05006166 ml += sprintf(msg+ml,
6167 "CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
6168 as->cmd_len, as->cmnd[0], as->cmnd[1],
6169 as->serial_number);
6170 dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
Webb Scales0d96ef52015-04-23 09:31:55 -05006171 hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
Robert Elliott4b761552015-04-23 09:33:54 -05006172
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006173 /*
6174 * Command is in flight, or possibly already completed
6175 * by the firmware (but not to the scsi mid layer) but we can't
6176 * distinguish which. Send the abort down.
6177 */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006178 if (wait_for_available_abort_cmd(h)) {
6179 dev_warn(&h->pdev->dev,
Robert Elliott4b761552015-04-23 09:33:54 -05006180 "%s FAILED, timeout waiting for an abort command to become available.\n",
6181 msg);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006182 cmd_free(h, abort);
6183 return FAILED;
6184 }
Don Brace39f3deb2016-02-23 15:16:28 -06006185 rc = hpsa_send_abort_both_ways(h, dev, abort, reply_queue);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006186 atomic_inc(&h->abort_cmds_available);
6187 wake_up_all(&h->abort_cmd_wait_queue);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006188 if (rc != 0) {
Robert Elliott4b761552015-04-23 09:33:54 -05006189 dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
Webb Scales0d96ef52015-04-23 09:31:55 -05006190 hpsa_show_dev_msg(KERN_WARNING, h, dev,
Robert Elliott4b761552015-04-23 09:33:54 -05006191 "FAILED to abort command");
Webb Scales281a7fd2015-01-23 16:43:35 -06006192 cmd_free(h, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006193 return FAILED;
6194 }
Robert Elliott4b761552015-04-23 09:33:54 -05006195 dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
Webb Scalesd604f532015-04-23 09:35:22 -05006196 wait_event(h->event_sync_wait_queue,
Webb Scalesa58e7e52015-04-23 09:34:16 -05006197 abort->scsi_cmd != sc || lockup_detected(h));
Webb Scales281a7fd2015-01-23 16:43:35 -06006198 cmd_free(h, abort);
Webb Scalesa58e7e52015-04-23 09:34:16 -05006199 return !lockup_detected(h) ? SUCCESS : FAILED;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006200}
6201
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006202/*
Webb Scales73153fe2015-04-23 09:35:04 -05006203 * For operations with an associated SCSI command, a command block is allocated
6204 * at init, and managed by cmd_tagged_alloc() and cmd_tagged_free() using the
6205 * block request tag as an index into a table of entries. cmd_tagged_free() is
6206 * the complement, although cmd_free() may be called instead.
6207 */
6208static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
6209 struct scsi_cmnd *scmd)
6210{
6211 int idx = hpsa_get_cmd_index(scmd);
6212 struct CommandList *c = h->cmd_pool + idx;
6213
6214 if (idx < HPSA_NRESERVED_CMDS || idx >= h->nr_cmds) {
6215 dev_err(&h->pdev->dev, "Bad block tag: %d not in [%d..%d]\n",
6216 idx, HPSA_NRESERVED_CMDS, h->nr_cmds - 1);
6217 /* The index value comes from the block layer, so if it's out of
6218 * bounds, it's probably not our bug.
6219 */
6220 BUG();
6221 }
6222
6223 atomic_inc(&c->refcount);
6224 if (unlikely(!hpsa_is_cmd_idle(c))) {
6225 /*
6226 * We expect that the SCSI layer will hand us a unique tag
6227 * value. Thus, there should never be a collision here between
6228 * two requests...because if the selected command isn't idle
6229 * then someone is going to be very disappointed.
6230 */
6231 dev_err(&h->pdev->dev,
6232 "tag collision (tag=%d) in cmd_tagged_alloc().\n",
6233 idx);
6234 if (c->scsi_cmd != NULL)
6235 scsi_print_command(c->scsi_cmd);
6236 scsi_print_command(scmd);
6237 }
6238
6239 hpsa_cmd_partial_init(h, idx, c);
6240 return c;
6241}
6242
6243static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c)
6244{
6245 /*
6246 * Release our reference to the block. We don't need to do anything
6247 * else to free it, because it is accessed by index. (There's no point
6248 * in checking the result of the decrement, since we cannot guarantee
6249 * that there isn't a concurrent abort which is also accessing it.)
6250 */
6251 (void)atomic_dec(&c->refcount);
6252}
6253
6254/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006255 * For operations that cannot sleep, a command block is allocated at init,
6256 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
6257 * which ones are free or in use. Lock must be held when calling this.
6258 * cmd_free() is the complement.
Robert Elliottbf43caf2015-04-23 09:33:38 -05006259 * This function never gives up and returns NULL. If it hangs,
6260 * another thread must call cmd_free() to free some tags.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006261 */
Webb Scales281a7fd2015-01-23 16:43:35 -06006262
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006263static struct CommandList *cmd_alloc(struct ctlr_info *h)
6264{
6265 struct CommandList *c;
Stephen Cameron360c73b2015-04-23 09:32:32 -05006266 int refcount, i;
Webb Scales73153fe2015-04-23 09:35:04 -05006267 int offset = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006268
Robert Elliott33811022015-01-23 16:43:41 -06006269 /*
6270 * There is some *extremely* small but non-zero chance that that
Stephen M. Cameron4c413122014-11-14 17:27:29 -06006271 * multiple threads could get in here, and one thread could
6272 * be scanning through the list of bits looking for a free
6273 * one, but the free ones are always behind him, and other
6274 * threads sneak in behind him and eat them before he can
6275 * get to them, so that while there is always a free one, a
6276 * very unlucky thread might be starved anyway, never able to
6277 * beat the other threads. In reality, this happens so
6278 * infrequently as to be indistinguishable from never.
Webb Scales73153fe2015-04-23 09:35:04 -05006279 *
6280 * Note that we start allocating commands before the SCSI host structure
6281 * is initialized. Since the search starts at bit zero, this
6282 * all works, since we have at least one command structure available;
6283 * however, it means that the structures with the low indexes have to be
6284 * reserved for driver-initiated requests, while requests from the block
6285 * layer will use the higher indexes.
Stephen M. Cameron4c413122014-11-14 17:27:29 -06006286 */
6287
Webb Scales281a7fd2015-01-23 16:43:35 -06006288 for (;;) {
Webb Scales73153fe2015-04-23 09:35:04 -05006289 i = find_next_zero_bit(h->cmd_pool_bits,
6290 HPSA_NRESERVED_CMDS,
6291 offset);
6292 if (unlikely(i >= HPSA_NRESERVED_CMDS)) {
Webb Scales281a7fd2015-01-23 16:43:35 -06006293 offset = 0;
6294 continue;
6295 }
6296 c = h->cmd_pool + i;
6297 refcount = atomic_inc_return(&c->refcount);
6298 if (unlikely(refcount > 1)) {
6299 cmd_free(h, c); /* already in use */
Webb Scales73153fe2015-04-23 09:35:04 -05006300 offset = (i + 1) % HPSA_NRESERVED_CMDS;
Webb Scales281a7fd2015-01-23 16:43:35 -06006301 continue;
6302 }
6303 set_bit(i & (BITS_PER_LONG - 1),
6304 h->cmd_pool_bits + (i / BITS_PER_LONG));
6305 break; /* it's ours now. */
6306 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05006307 hpsa_cmd_partial_init(h, i, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006308 return c;
6309}
6310
Webb Scales73153fe2015-04-23 09:35:04 -05006311/*
6312 * This is the complementary operation to cmd_alloc(). Note, however, in some
6313 * corner cases it may also be used to free blocks allocated by
6314 * cmd_tagged_alloc() in which case the ref-count decrement does the trick and
6315 * the clear-bit is harmless.
6316 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006317static void cmd_free(struct ctlr_info *h, struct CommandList *c)
6318{
Webb Scales281a7fd2015-01-23 16:43:35 -06006319 if (atomic_dec_and_test(&c->refcount)) {
6320 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006321
Webb Scales281a7fd2015-01-23 16:43:35 -06006322 i = c - h->cmd_pool;
6323 clear_bit(i & (BITS_PER_LONG - 1),
6324 h->cmd_pool_bits + (i / BITS_PER_LONG));
6325 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006326}
6327
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006328#ifdef CONFIG_COMPAT
6329
Don Brace42a91642014-11-14 17:26:27 -06006330static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
6331 void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006332{
6333 IOCTL32_Command_struct __user *arg32 =
6334 (IOCTL32_Command_struct __user *) arg;
6335 IOCTL_Command_struct arg64;
6336 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
6337 int err;
6338 u32 cp;
6339
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06006340 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006341 err = 0;
6342 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
6343 sizeof(arg64.LUN_info));
6344 err |= copy_from_user(&arg64.Request, &arg32->Request,
6345 sizeof(arg64.Request));
6346 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
6347 sizeof(arg64.error_info));
6348 err |= get_user(arg64.buf_size, &arg32->buf_size);
6349 err |= get_user(cp, &arg32->buf);
6350 arg64.buf = compat_ptr(cp);
6351 err |= copy_to_user(p, &arg64, sizeof(arg64));
6352
6353 if (err)
6354 return -EFAULT;
6355
Don Brace42a91642014-11-14 17:26:27 -06006356 err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006357 if (err)
6358 return err;
6359 err |= copy_in_user(&arg32->error_info, &p->error_info,
6360 sizeof(arg32->error_info));
6361 if (err)
6362 return -EFAULT;
6363 return err;
6364}
6365
6366static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
Don Brace42a91642014-11-14 17:26:27 -06006367 int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006368{
6369 BIG_IOCTL32_Command_struct __user *arg32 =
6370 (BIG_IOCTL32_Command_struct __user *) arg;
6371 BIG_IOCTL_Command_struct arg64;
6372 BIG_IOCTL_Command_struct __user *p =
6373 compat_alloc_user_space(sizeof(arg64));
6374 int err;
6375 u32 cp;
6376
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06006377 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006378 err = 0;
6379 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
6380 sizeof(arg64.LUN_info));
6381 err |= copy_from_user(&arg64.Request, &arg32->Request,
6382 sizeof(arg64.Request));
6383 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
6384 sizeof(arg64.error_info));
6385 err |= get_user(arg64.buf_size, &arg32->buf_size);
6386 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
6387 err |= get_user(cp, &arg32->buf);
6388 arg64.buf = compat_ptr(cp);
6389 err |= copy_to_user(p, &arg64, sizeof(arg64));
6390
6391 if (err)
6392 return -EFAULT;
6393
Don Brace42a91642014-11-14 17:26:27 -06006394 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006395 if (err)
6396 return err;
6397 err |= copy_in_user(&arg32->error_info, &p->error_info,
6398 sizeof(arg32->error_info));
6399 if (err)
6400 return -EFAULT;
6401 return err;
6402}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06006403
Don Brace42a91642014-11-14 17:26:27 -06006404static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06006405{
6406 switch (cmd) {
6407 case CCISS_GETPCIINFO:
6408 case CCISS_GETINTINFO:
6409 case CCISS_SETINTINFO:
6410 case CCISS_GETNODENAME:
6411 case CCISS_SETNODENAME:
6412 case CCISS_GETHEARTBEAT:
6413 case CCISS_GETBUSTYPES:
6414 case CCISS_GETFIRMVER:
6415 case CCISS_GETDRIVVER:
6416 case CCISS_REVALIDVOLS:
6417 case CCISS_DEREGDISK:
6418 case CCISS_REGNEWDISK:
6419 case CCISS_REGNEWD:
6420 case CCISS_RESCANDISK:
6421 case CCISS_GETLUNINFO:
6422 return hpsa_ioctl(dev, cmd, arg);
6423
6424 case CCISS_PASSTHRU32:
6425 return hpsa_ioctl32_passthru(dev, cmd, arg);
6426 case CCISS_BIG_PASSTHRU32:
6427 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
6428
6429 default:
6430 return -ENOIOCTLCMD;
6431 }
6432}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006433#endif
6434
6435static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
6436{
6437 struct hpsa_pci_info pciinfo;
6438
6439 if (!argp)
6440 return -EINVAL;
6441 pciinfo.domain = pci_domain_nr(h->pdev->bus);
6442 pciinfo.bus = h->pdev->bus->number;
6443 pciinfo.dev_fn = h->pdev->devfn;
6444 pciinfo.board_id = h->board_id;
6445 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
6446 return -EFAULT;
6447 return 0;
6448}
6449
6450static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
6451{
6452 DriverVer_type DriverVer;
6453 unsigned char vmaj, vmin, vsubmin;
6454 int rc;
6455
6456 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
6457 &vmaj, &vmin, &vsubmin);
6458 if (rc != 3) {
6459 dev_info(&h->pdev->dev, "driver version string '%s' "
6460 "unrecognized.", HPSA_DRIVER_VERSION);
6461 vmaj = 0;
6462 vmin = 0;
6463 vsubmin = 0;
6464 }
6465 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
6466 if (!argp)
6467 return -EINVAL;
6468 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
6469 return -EFAULT;
6470 return 0;
6471}
6472
6473static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6474{
6475 IOCTL_Command_struct iocommand;
6476 struct CommandList *c;
6477 char *buff = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006478 u64 temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006479 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006480
6481 if (!argp)
6482 return -EINVAL;
6483 if (!capable(CAP_SYS_RAWIO))
6484 return -EPERM;
6485 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
6486 return -EFAULT;
6487 if ((iocommand.buf_size < 1) &&
6488 (iocommand.Request.Type.Direction != XFER_NONE)) {
6489 return -EINVAL;
6490 }
6491 if (iocommand.buf_size > 0) {
6492 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
6493 if (buff == NULL)
Robert Elliott2dd02d72015-04-23 09:33:43 -05006494 return -ENOMEM;
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006495 if (iocommand.Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006496 /* Copy the data into the buffer we created */
6497 if (copy_from_user(buff, iocommand.buf,
6498 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006499 rc = -EFAULT;
6500 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006501 }
6502 } else {
6503 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006504 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006505 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006506 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006507
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006508 /* Fill in the command type */
6509 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006510 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006511 /* Fill in Command Header */
6512 c->Header.ReplyQueue = 0; /* unused in simple mode */
6513 if (iocommand.buf_size > 0) { /* buffer to fill */
6514 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006515 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006516 } else { /* no buffers to fill */
6517 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006518 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006519 }
6520 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006521
6522 /* Fill in Request block */
6523 memcpy(&c->Request, &iocommand.Request,
6524 sizeof(c->Request));
6525
6526 /* Fill in the scatter gather information */
6527 if (iocommand.buf_size > 0) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006528 temp64 = pci_map_single(h->pdev, buff,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006529 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006530 if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
6531 c->SG[0].Addr = cpu_to_le64(0);
6532 c->SG[0].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006533 rc = -ENOMEM;
6534 goto out;
6535 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006536 c->SG[0].Addr = cpu_to_le64(temp64);
6537 c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
6538 c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006539 }
Don Bracec448ecf2016-04-27 17:13:51 -05006540 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
Don Brace3fb134c2016-07-01 13:37:38 -05006541 NO_TIMEOUT);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05006542 if (iocommand.buf_size > 0)
6543 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006544 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006545 if (rc) {
6546 rc = -EIO;
6547 goto out;
6548 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006549
6550 /* Copy the error information out */
6551 memcpy(&iocommand.error_info, c->err_info,
6552 sizeof(iocommand.error_info));
6553 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006554 rc = -EFAULT;
6555 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006556 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006557 if ((iocommand.Request.Type.Direction & XFER_READ) &&
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006558 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006559 /* Copy the data out of the buffer we created */
6560 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006561 rc = -EFAULT;
6562 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006563 }
6564 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006565out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006566 cmd_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006567out_kfree:
6568 kfree(buff);
6569 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006570}
6571
6572static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6573{
6574 BIG_IOCTL_Command_struct *ioc;
6575 struct CommandList *c;
6576 unsigned char **buff = NULL;
6577 int *buff_size = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006578 u64 temp64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006579 BYTE sg_used = 0;
6580 int status = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06006581 u32 left;
6582 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006583 BYTE __user *data_ptr;
6584
6585 if (!argp)
6586 return -EINVAL;
6587 if (!capable(CAP_SYS_RAWIO))
6588 return -EPERM;
6589 ioc = (BIG_IOCTL_Command_struct *)
6590 kmalloc(sizeof(*ioc), GFP_KERNEL);
6591 if (!ioc) {
6592 status = -ENOMEM;
6593 goto cleanup1;
6594 }
6595 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
6596 status = -EFAULT;
6597 goto cleanup1;
6598 }
6599 if ((ioc->buf_size < 1) &&
6600 (ioc->Request.Type.Direction != XFER_NONE)) {
6601 status = -EINVAL;
6602 goto cleanup1;
6603 }
6604 /* Check kmalloc limits using all SGs */
6605 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
6606 status = -EINVAL;
6607 goto cleanup1;
6608 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006609 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006610 status = -EINVAL;
6611 goto cleanup1;
6612 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006613 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006614 if (!buff) {
6615 status = -ENOMEM;
6616 goto cleanup1;
6617 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006618 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006619 if (!buff_size) {
6620 status = -ENOMEM;
6621 goto cleanup1;
6622 }
6623 left = ioc->buf_size;
6624 data_ptr = ioc->buf;
6625 while (left) {
6626 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
6627 buff_size[sg_used] = sz;
6628 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
6629 if (buff[sg_used] == NULL) {
6630 status = -ENOMEM;
6631 goto cleanup1;
6632 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006633 if (ioc->Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006634 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
Stephen M. Cameron0758f4f2014-07-03 10:18:03 -05006635 status = -EFAULT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006636 goto cleanup1;
6637 }
6638 } else
6639 memset(buff[sg_used], 0, sz);
6640 left -= sz;
6641 data_ptr += sz;
6642 sg_used++;
6643 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006644 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006645
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006646 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006647 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006648 c->Header.ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006649 c->Header.SGList = (u8) sg_used;
6650 c->Header.SGTotal = cpu_to_le16(sg_used);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006651 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006652 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
6653 if (ioc->buf_size > 0) {
6654 int i;
6655 for (i = 0; i < sg_used; i++) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006656 temp64 = pci_map_single(h->pdev, buff[i],
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006657 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006658 if (dma_mapping_error(&h->pdev->dev,
6659 (dma_addr_t) temp64)) {
6660 c->SG[i].Addr = cpu_to_le64(0);
6661 c->SG[i].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006662 hpsa_pci_unmap(h->pdev, c, i,
6663 PCI_DMA_BIDIRECTIONAL);
6664 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006665 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006666 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006667 c->SG[i].Addr = cpu_to_le64(temp64);
6668 c->SG[i].Len = cpu_to_le32(buff_size[i]);
6669 c->SG[i].Ext = cpu_to_le32(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006670 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006671 c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006672 }
Don Bracec448ecf2016-04-27 17:13:51 -05006673 status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
Don Brace3fb134c2016-07-01 13:37:38 -05006674 NO_TIMEOUT);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006675 if (sg_used)
6676 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006677 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006678 if (status) {
6679 status = -EIO;
6680 goto cleanup0;
6681 }
6682
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006683 /* Copy the error information out */
6684 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
6685 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006686 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006687 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006688 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006689 if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006690 int i;
6691
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006692 /* Copy the data out of the buffer we created */
6693 BYTE __user *ptr = ioc->buf;
6694 for (i = 0; i < sg_used; i++) {
6695 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006696 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006697 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006698 }
6699 ptr += buff_size[i];
6700 }
6701 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006702 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006703cleanup0:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006704 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006705cleanup1:
6706 if (buff) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006707 int i;
6708
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006709 for (i = 0; i < sg_used; i++)
6710 kfree(buff[i]);
6711 kfree(buff);
6712 }
6713 kfree(buff_size);
6714 kfree(ioc);
6715 return status;
6716}
6717
6718static void check_ioctl_unit_attention(struct ctlr_info *h,
6719 struct CommandList *c)
6720{
6721 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
6722 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
6723 (void) check_for_unit_attention(h, c);
6724}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006725
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006726/*
6727 * ioctl
6728 */
Don Brace42a91642014-11-14 17:26:27 -06006729static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006730{
6731 struct ctlr_info *h;
6732 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006733 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006734
6735 h = sdev_to_hba(dev);
6736
6737 switch (cmd) {
6738 case CCISS_DEREGDISK:
6739 case CCISS_REGNEWDISK:
6740 case CCISS_REGNEWD:
Stephen M. Camerona08a84712010-02-04 08:43:16 -06006741 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006742 return 0;
6743 case CCISS_GETPCIINFO:
6744 return hpsa_getpciinfo_ioctl(h, argp);
6745 case CCISS_GETDRIVVER:
6746 return hpsa_getdrivver_ioctl(h, argp);
6747 case CCISS_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006748 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006749 return -EAGAIN;
6750 rc = hpsa_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006751 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006752 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006753 case CCISS_BIG_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006754 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006755 return -EAGAIN;
6756 rc = hpsa_big_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006757 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006758 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006759 default:
6760 return -ENOTTY;
6761 }
6762}
6763
Robert Elliottbf43caf2015-04-23 09:33:38 -05006764static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006765 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006766{
6767 struct CommandList *c;
6768
6769 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006770
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006771 /* fill_cmd can't fail here, no data buffer to map */
6772 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006773 RAID_CTLR_LUNID, TYPE_MSG);
6774 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
6775 c->waiting = NULL;
6776 enqueue_cmd_and_start_io(h, c);
6777 /* Don't wait for completion, the reset won't complete. Don't free
6778 * the command either. This is the last command we will send before
6779 * re-initializing everything, so it doesn't matter and won't leak.
6780 */
Robert Elliottbf43caf2015-04-23 09:33:38 -05006781 return;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006782}
6783
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006784static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006785 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006786 int cmd_type)
6787{
6788 int pci_dir = XFER_NONE;
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006789 u64 tag; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006790
6791 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006792 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006793 c->Header.ReplyQueue = 0;
6794 if (buff != NULL && size > 0) {
6795 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006796 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006797 } else {
6798 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006799 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006800 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006801 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
6802
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006803 if (cmd_type == TYPE_CMD) {
6804 switch (cmd) {
6805 case HPSA_INQUIRY:
6806 /* are we trying to read a vital product page */
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006807 if (page_code & VPD_PAGE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006808 c->Request.CDB[1] = 0x01;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006809 c->Request.CDB[2] = (page_code & 0xff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006810 }
6811 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006812 c->Request.type_attr_dir =
6813 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006814 c->Request.Timeout = 0;
6815 c->Request.CDB[0] = HPSA_INQUIRY;
6816 c->Request.CDB[4] = size & 0xFF;
6817 break;
6818 case HPSA_REPORT_LOG:
6819 case HPSA_REPORT_PHYS:
6820 /* Talking to controller so It's a physical command
6821 mode = 00 target = 0. Nothing to write.
6822 */
6823 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006824 c->Request.type_attr_dir =
6825 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006826 c->Request.Timeout = 0;
6827 c->Request.CDB[0] = cmd;
6828 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6829 c->Request.CDB[7] = (size >> 16) & 0xFF;
6830 c->Request.CDB[8] = (size >> 8) & 0xFF;
6831 c->Request.CDB[9] = size & 0xFF;
6832 break;
Scott Teelc2adae42015-11-04 15:52:16 -06006833 case BMIC_SENSE_DIAG_OPTIONS:
6834 c->Request.CDBLen = 16;
6835 c->Request.type_attr_dir =
6836 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6837 c->Request.Timeout = 0;
6838 /* Spec says this should be BMIC_WRITE */
6839 c->Request.CDB[0] = BMIC_READ;
6840 c->Request.CDB[6] = BMIC_SENSE_DIAG_OPTIONS;
6841 break;
6842 case BMIC_SET_DIAG_OPTIONS:
6843 c->Request.CDBLen = 16;
6844 c->Request.type_attr_dir =
6845 TYPE_ATTR_DIR(cmd_type,
6846 ATTR_SIMPLE, XFER_WRITE);
6847 c->Request.Timeout = 0;
6848 c->Request.CDB[0] = BMIC_WRITE;
6849 c->Request.CDB[6] = BMIC_SET_DIAG_OPTIONS;
6850 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006851 case HPSA_CACHE_FLUSH:
6852 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006853 c->Request.type_attr_dir =
6854 TYPE_ATTR_DIR(cmd_type,
6855 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006856 c->Request.Timeout = 0;
6857 c->Request.CDB[0] = BMIC_WRITE;
6858 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05006859 c->Request.CDB[7] = (size >> 8) & 0xFF;
6860 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006861 break;
6862 case TEST_UNIT_READY:
6863 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006864 c->Request.type_attr_dir =
6865 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006866 c->Request.Timeout = 0;
6867 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006868 case HPSA_GET_RAID_MAP:
6869 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006870 c->Request.type_attr_dir =
6871 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006872 c->Request.Timeout = 0;
6873 c->Request.CDB[0] = HPSA_CISS_READ;
6874 c->Request.CDB[1] = cmd;
6875 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6876 c->Request.CDB[7] = (size >> 16) & 0xFF;
6877 c->Request.CDB[8] = (size >> 8) & 0xFF;
6878 c->Request.CDB[9] = size & 0xFF;
6879 break;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006880 case BMIC_SENSE_CONTROLLER_PARAMETERS:
6881 c->Request.CDBLen = 10;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006882 c->Request.type_attr_dir =
6883 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006884 c->Request.Timeout = 0;
6885 c->Request.CDB[0] = BMIC_READ;
6886 c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
6887 c->Request.CDB[7] = (size >> 16) & 0xFF;
6888 c->Request.CDB[8] = (size >> 8) & 0xFF;
6889 break;
Don Brace03383732015-01-23 16:43:30 -06006890 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
6891 c->Request.CDBLen = 10;
6892 c->Request.type_attr_dir =
6893 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6894 c->Request.Timeout = 0;
6895 c->Request.CDB[0] = BMIC_READ;
6896 c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
6897 c->Request.CDB[7] = (size >> 16) & 0xFF;
6898 c->Request.CDB[8] = (size >> 8) & 0XFF;
6899 break;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06006900 case BMIC_SENSE_SUBSYSTEM_INFORMATION:
6901 c->Request.CDBLen = 10;
6902 c->Request.type_attr_dir =
6903 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6904 c->Request.Timeout = 0;
6905 c->Request.CDB[0] = BMIC_READ;
6906 c->Request.CDB[6] = BMIC_SENSE_SUBSYSTEM_INFORMATION;
6907 c->Request.CDB[7] = (size >> 16) & 0xFF;
6908 c->Request.CDB[8] = (size >> 8) & 0XFF;
6909 break;
Don Bracecca8f132015-12-22 10:36:48 -06006910 case BMIC_SENSE_STORAGE_BOX_PARAMS:
6911 c->Request.CDBLen = 10;
6912 c->Request.type_attr_dir =
6913 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6914 c->Request.Timeout = 0;
6915 c->Request.CDB[0] = BMIC_READ;
6916 c->Request.CDB[6] = BMIC_SENSE_STORAGE_BOX_PARAMS;
6917 c->Request.CDB[7] = (size >> 16) & 0xFF;
6918 c->Request.CDB[8] = (size >> 8) & 0XFF;
6919 break;
Scott Teel66749d02015-11-04 15:51:57 -06006920 case BMIC_IDENTIFY_CONTROLLER:
6921 c->Request.CDBLen = 10;
6922 c->Request.type_attr_dir =
6923 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6924 c->Request.Timeout = 0;
6925 c->Request.CDB[0] = BMIC_READ;
6926 c->Request.CDB[1] = 0;
6927 c->Request.CDB[2] = 0;
6928 c->Request.CDB[3] = 0;
6929 c->Request.CDB[4] = 0;
6930 c->Request.CDB[5] = 0;
6931 c->Request.CDB[6] = BMIC_IDENTIFY_CONTROLLER;
6932 c->Request.CDB[7] = (size >> 16) & 0xFF;
6933 c->Request.CDB[8] = (size >> 8) & 0XFF;
6934 c->Request.CDB[9] = 0;
6935 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006936 default:
6937 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
6938 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006939 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006940 }
6941 } else if (cmd_type == TYPE_MSG) {
6942 switch (cmd) {
6943
Scott Teel0b9b7b62015-11-04 15:51:02 -06006944 case HPSA_PHYS_TARGET_RESET:
6945 c->Request.CDBLen = 16;
6946 c->Request.type_attr_dir =
6947 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6948 c->Request.Timeout = 0; /* Don't time out */
6949 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
6950 c->Request.CDB[0] = HPSA_RESET;
6951 c->Request.CDB[1] = HPSA_TARGET_RESET_TYPE;
6952 /* Physical target reset needs no control bytes 4-7*/
6953 c->Request.CDB[4] = 0x00;
6954 c->Request.CDB[5] = 0x00;
6955 c->Request.CDB[6] = 0x00;
6956 c->Request.CDB[7] = 0x00;
6957 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006958 case HPSA_DEVICE_RESET_MSG:
6959 c->Request.CDBLen = 16;
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; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006963 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
6964 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05006965 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006966 /* If bytes 4-7 are zero, it means reset the */
6967 /* LunID device */
6968 c->Request.CDB[4] = 0x00;
6969 c->Request.CDB[5] = 0x00;
6970 c->Request.CDB[6] = 0x00;
6971 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006972 break;
6973 case HPSA_ABORT_MSG:
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006974 memcpy(&tag, buff, sizeof(tag));
Don Brace2b08b3e2015-01-23 16:41:09 -06006975 dev_dbg(&h->pdev->dev,
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006976 "Abort Tag:0x%016llx using rqst Tag:0x%016llx",
6977 tag, c->Header.tag);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006978 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006979 c->Request.type_attr_dir =
6980 TYPE_ATTR_DIR(cmd_type,
6981 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006982 c->Request.Timeout = 0; /* Don't time out */
6983 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
6984 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
6985 c->Request.CDB[2] = 0x00; /* reserved */
6986 c->Request.CDB[3] = 0x00; /* reserved */
6987 /* Tag to abort goes in CDB[4]-CDB[11] */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006988 memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006989 c->Request.CDB[12] = 0x00; /* reserved */
6990 c->Request.CDB[13] = 0x00; /* reserved */
6991 c->Request.CDB[14] = 0x00; /* reserved */
6992 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006993 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006994 default:
6995 dev_warn(&h->pdev->dev, "unknown message type %d\n",
6996 cmd);
6997 BUG();
6998 }
6999 } else {
7000 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
7001 BUG();
7002 }
7003
Stephen M. Camerona505b862014-11-14 17:27:04 -06007004 switch (GET_DIR(c->Request.type_attr_dir)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007005 case XFER_READ:
7006 pci_dir = PCI_DMA_FROMDEVICE;
7007 break;
7008 case XFER_WRITE:
7009 pci_dir = PCI_DMA_TODEVICE;
7010 break;
7011 case XFER_NONE:
7012 pci_dir = PCI_DMA_NONE;
7013 break;
7014 default:
7015 pci_dir = PCI_DMA_BIDIRECTIONAL;
7016 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06007017 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
7018 return -1;
7019 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007020}
7021
7022/*
7023 * Map (physical) PCI mem into (virtual) kernel space
7024 */
7025static void __iomem *remap_pci_mem(ulong base, ulong size)
7026{
7027 ulong page_base = ((ulong) base) & PAGE_MASK;
7028 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba34c2012-07-26 11:34:23 -05007029 void __iomem *page_remapped = ioremap_nocache(page_base,
7030 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007031
7032 return page_remapped ? (page_remapped + page_offs) : NULL;
7033}
7034
Matt Gates254f7962012-05-01 11:43:06 -05007035static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007036{
Matt Gates254f7962012-05-01 11:43:06 -05007037 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007038}
7039
Stephen M. Cameron900c5442010-02-04 08:42:35 -06007040static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007041{
7042 return h->access.intr_pending(h);
7043}
7044
7045static inline long interrupt_not_for_us(struct ctlr_info *h)
7046{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007047 return (h->access.intr_pending(h) == 0) ||
7048 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007049}
7050
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06007051static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
7052 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007053{
7054 if (unlikely(tag_index >= h->nr_cmds)) {
7055 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
7056 return 1;
7057 }
7058 return 0;
7059}
7060
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05007061static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007062{
Stephen M. Camerone85c5972012-05-01 11:43:42 -05007063 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06007064 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
7065 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05007066 complete_scsi_command(c);
Stephen Cameron8be986c2015-04-23 09:34:06 -05007067 else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007068 complete(c->waiting);
Stephen M. Camerona104c992010-02-04 08:42:24 -06007069}
7070
Don Brace303932f2010-02-04 08:42:40 -06007071/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05007072static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06007073 u32 raw_tag)
7074{
7075 u32 tag_index;
7076 struct CommandList *c;
7077
Don Bracef2405db2015-01-23 16:43:09 -06007078 tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05007079 if (!bad_tag(h, tag_index, raw_tag)) {
7080 c = h->cmd_pool + tag_index;
7081 finish_cmd(c);
7082 }
Don Brace303932f2010-02-04 08:42:40 -06007083}
7084
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007085/* Some controllers, like p400, will give us one interrupt
7086 * after a soft reset, even if we turned interrupts off.
7087 * Only need to check for this in the hpsa_xxx_discard_completions
7088 * functions.
7089 */
7090static int ignore_bogus_interrupt(struct ctlr_info *h)
7091{
7092 if (likely(!reset_devices))
7093 return 0;
7094
7095 if (likely(h->interrupts_enabled))
7096 return 0;
7097
7098 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
7099 "(known firmware bug.) Ignoring.\n");
7100
7101 return 1;
7102}
7103
Matt Gates254f7962012-05-01 11:43:06 -05007104/*
7105 * Convert &h->q[x] (passed to interrupt handlers) back to h.
7106 * Relies on (h-q[x] == x) being true for x such that
7107 * 0 <= x < MAX_REPLY_QUEUES.
7108 */
7109static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007110{
Matt Gates254f7962012-05-01 11:43:06 -05007111 return container_of((queue - *queue), struct ctlr_info, q[0]);
7112}
7113
7114static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
7115{
7116 struct ctlr_info *h = queue_to_hba(queue);
7117 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007118 u32 raw_tag;
7119
7120 if (ignore_bogus_interrupt(h))
7121 return IRQ_NONE;
7122
7123 if (interrupt_not_for_us(h))
7124 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007125 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007126 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05007127 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007128 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05007129 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007130 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007131 return IRQ_HANDLED;
7132}
7133
Matt Gates254f7962012-05-01 11:43:06 -05007134static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007135{
Matt Gates254f7962012-05-01 11:43:06 -05007136 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007137 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05007138 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007139
7140 if (ignore_bogus_interrupt(h))
7141 return IRQ_NONE;
7142
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007143 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05007144 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007145 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05007146 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007147 return IRQ_HANDLED;
7148}
7149
Matt Gates254f7962012-05-01 11:43:06 -05007150static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007151{
Matt Gates254f7962012-05-01 11:43:06 -05007152 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06007153 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05007154 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007155
7156 if (interrupt_not_for_us(h))
7157 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007158 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007159 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05007160 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007161 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06007162 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05007163 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007164 }
7165 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007166 return IRQ_HANDLED;
7167}
7168
Matt Gates254f7962012-05-01 11:43:06 -05007169static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007170{
Matt Gates254f7962012-05-01 11:43:06 -05007171 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007172 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05007173 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007174
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007175 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05007176 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06007177 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06007178 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05007179 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007180 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007181 return IRQ_HANDLED;
7182}
7183
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06007184/* Send a message CDB to the firmware. Careful, this only works
7185 * in simple mode, not performant mode due to the tag lookup.
7186 * We only ever use this immediately after a controller reset.
7187 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007188static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
7189 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007190{
7191 struct Command {
7192 struct CommandListHeader CommandHeader;
7193 struct RequestBlock Request;
7194 struct ErrDescriptor ErrorDescriptor;
7195 };
7196 struct Command *cmd;
7197 static const size_t cmd_sz = sizeof(*cmd) +
7198 sizeof(cmd->ErrorDescriptor);
7199 dma_addr_t paddr64;
Don Brace2b08b3e2015-01-23 16:41:09 -06007200 __le32 paddr32;
7201 u32 tag;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007202 void __iomem *vaddr;
7203 int i, err;
7204
7205 vaddr = pci_ioremap_bar(pdev, 0);
7206 if (vaddr == NULL)
7207 return -ENOMEM;
7208
7209 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
7210 * CCISS commands, so they must be allocated from the lower 4GiB of
7211 * memory.
7212 */
7213 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
7214 if (err) {
7215 iounmap(vaddr);
Robert Elliott1eaec8f2015-01-23 16:42:37 -06007216 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007217 }
7218
7219 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
7220 if (cmd == NULL) {
7221 iounmap(vaddr);
7222 return -ENOMEM;
7223 }
7224
7225 /* This must fit, because of the 32-bit consistent DMA mask. Also,
7226 * although there's no guarantee, we assume that the address is at
7227 * least 4-byte aligned (most likely, it's page-aligned).
7228 */
Don Brace2b08b3e2015-01-23 16:41:09 -06007229 paddr32 = cpu_to_le32(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007230
7231 cmd->CommandHeader.ReplyQueue = 0;
7232 cmd->CommandHeader.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007233 cmd->CommandHeader.SGTotal = cpu_to_le16(0);
Don Brace2b08b3e2015-01-23 16:41:09 -06007234 cmd->CommandHeader.tag = cpu_to_le64(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007235 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
7236
7237 cmd->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06007238 cmd->Request.type_attr_dir =
7239 TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007240 cmd->Request.Timeout = 0; /* Don't time out */
7241 cmd->Request.CDB[0] = opcode;
7242 cmd->Request.CDB[1] = type;
7243 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007244 cmd->ErrorDescriptor.Addr =
Don Brace2b08b3e2015-01-23 16:41:09 -06007245 cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007246 cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007247
Don Brace2b08b3e2015-01-23 16:41:09 -06007248 writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007249
7250 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
7251 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Don Brace2b08b3e2015-01-23 16:41:09 -06007252 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007253 break;
7254 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
7255 }
7256
7257 iounmap(vaddr);
7258
7259 /* we leak the DMA buffer here ... no choice since the controller could
7260 * still complete the command.
7261 */
7262 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
7263 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
7264 opcode, type);
7265 return -ETIMEDOUT;
7266 }
7267
7268 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
7269
7270 if (tag & HPSA_ERROR_BIT) {
7271 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
7272 opcode, type);
7273 return -EIO;
7274 }
7275
7276 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
7277 opcode, type);
7278 return 0;
7279}
7280
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007281#define hpsa_noop(p) hpsa_message(p, 3, 0)
7282
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007283static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Don Brace42a91642014-11-14 17:26:27 -06007284 void __iomem *vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007285{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007286
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007287 if (use_doorbell) {
7288 /* For everything after the P600, the PCI power state method
7289 * of resetting the controller doesn't work, so we have this
7290 * other way using the doorbell register.
7291 */
7292 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007293 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05007294
Justin Lindley00701a92014-05-29 10:52:47 -05007295 /* PMC hardware guys tell us we need a 10 second delay after
Stephen M. Cameron85009232013-09-23 13:33:36 -05007296 * doorbell reset and before any attempt to talk to the board
7297 * at all to ensure that this actually works and doesn't fall
7298 * over in some weird corner cases.
7299 */
Justin Lindley00701a92014-05-29 10:52:47 -05007300 msleep(10000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007301 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007302
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007303 /* Quoting from the Open CISS Specification: "The Power
7304 * Management Control/Status Register (CSR) controls the power
7305 * state of the device. The normal operating state is D0,
7306 * CSR=00h. The software off state is D3, CSR=03h. To reset
7307 * the controller, place the interface device in D3 then to D0,
7308 * this causes a secondary PCI reset which will reset the
7309 * controller." */
7310
Don Brace2662cab2015-01-23 16:41:25 -06007311 int rc = 0;
7312
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007313 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
Don Brace2662cab2015-01-23 16:41:25 -06007314
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007315 /* enter the D3hot power management state */
Don Brace2662cab2015-01-23 16:41:25 -06007316 rc = pci_set_power_state(pdev, PCI_D3hot);
7317 if (rc)
7318 return rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007319
7320 msleep(500);
7321
7322 /* enter the D0 power management state */
Don Brace2662cab2015-01-23 16:41:25 -06007323 rc = pci_set_power_state(pdev, PCI_D0);
7324 if (rc)
7325 return rc;
Mike Millerc4853ef2011-10-21 08:19:43 +02007326
7327 /*
7328 * The P600 requires a small delay when changing states.
7329 * Otherwise we may think the board did not reset and we bail.
7330 * This for kdump only and is particular to the P600.
7331 */
7332 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007333 }
7334 return 0;
7335}
7336
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007337static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007338{
7339 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06007340 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007341}
7342
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007343static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007344{
7345 char *driver_version;
7346 int i, size = sizeof(cfgtable->driver_version);
7347
7348 driver_version = kmalloc(size, GFP_KERNEL);
7349 if (!driver_version)
7350 return -ENOMEM;
7351
7352 init_driver_version(driver_version, size);
7353 for (i = 0; i < size; i++)
7354 writeb(driver_version[i], &cfgtable->driver_version[i]);
7355 kfree(driver_version);
7356 return 0;
7357}
7358
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007359static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
7360 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007361{
7362 int i;
7363
7364 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
7365 driver_ver[i] = readb(&cfgtable->driver_version[i]);
7366}
7367
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007368static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007369{
7370
7371 char *driver_ver, *old_driver_ver;
7372 int rc, size = sizeof(cfgtable->driver_version);
7373
7374 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
7375 if (!old_driver_ver)
7376 return -ENOMEM;
7377 driver_ver = old_driver_ver + size;
7378
7379 /* After a reset, the 32 bytes of "driver version" in the cfgtable
7380 * should have been changed, otherwise we know the reset failed.
7381 */
7382 init_driver_version(old_driver_ver, size);
7383 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
7384 rc = !memcmp(driver_ver, old_driver_ver, size);
7385 kfree(old_driver_ver);
7386 return rc;
7387}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007388/* This does a hard reset of the controller using PCI power management
7389 * states or the using the doorbell register.
7390 */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007391static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007392{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007393 u64 cfg_offset;
7394 u32 cfg_base_addr;
7395 u64 cfg_base_addr_index;
7396 void __iomem *vaddr;
7397 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007398 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007399 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007400 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007401 u32 use_doorbell;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007402 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007403
7404 /* For controllers as old as the P600, this is very nearly
7405 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007406 *
7407 * pci_save_state(pci_dev);
7408 * pci_set_power_state(pci_dev, PCI_D3hot);
7409 * pci_set_power_state(pci_dev, PCI_D0);
7410 * pci_restore_state(pci_dev);
7411 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007412 * For controllers newer than the P600, the pci power state
7413 * method of resetting doesn't work so we have another way
7414 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007415 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05007416
Robert Elliott60f923b2015-01-23 16:42:06 -06007417 if (!ctlr_is_resettable(board_id)) {
7418 dev_warn(&pdev->dev, "Controller not resettable\n");
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06007419 return -ENODEV;
7420 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05007421
7422 /* if controller is soft- but not hard resettable... */
7423 if (!ctlr_is_hard_resettable(board_id))
7424 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05007425
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007426 /* Save the PCI command register */
7427 pci_read_config_word(pdev, 4, &command_register);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007428 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007429
7430 /* find the first memory BAR, so we can find the cfg table */
7431 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
7432 if (rc)
7433 return rc;
7434 vaddr = remap_pci_mem(paddr, 0x250);
7435 if (!vaddr)
7436 return -ENOMEM;
7437
7438 /* find cfgtable in order to check if reset via doorbell is supported */
7439 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
7440 &cfg_base_addr_index, &cfg_offset);
7441 if (rc)
7442 goto unmap_vaddr;
7443 cfgtable = remap_pci_mem(pci_resource_start(pdev,
7444 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
7445 if (!cfgtable) {
7446 rc = -ENOMEM;
7447 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007448 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007449 rc = write_driver_ver_to_cfgtable(cfgtable);
7450 if (rc)
Tomas Henzl03741d92015-01-23 16:41:14 -06007451 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007452
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007453 /* If reset via doorbell register is supported, use that.
7454 * There are two such methods. Favor the newest method.
7455 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007456 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007457 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
7458 if (use_doorbell) {
7459 use_doorbell = DOORBELL_CTLR_RESET2;
7460 } else {
7461 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
7462 if (use_doorbell) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007463 dev_warn(&pdev->dev,
7464 "Soft reset not supported. Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007465 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007466 goto unmap_cfgtable;
7467 }
7468 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007469
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007470 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
7471 if (rc)
7472 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007473
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007474 pci_restore_state(pdev);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007475 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007476
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007477 /* Some devices (notably the HP Smart Array 5i Controller)
7478 need a little pause here */
7479 msleep(HPSA_POST_RESET_PAUSE_MSECS);
7480
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007481 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
7482 if (rc) {
7483 dev_warn(&pdev->dev,
Stephen Cameron050f7142015-01-23 16:42:22 -06007484 "Failed waiting for board to become ready after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007485 goto unmap_cfgtable;
7486 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007487
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007488 rc = controller_reset_failed(vaddr);
7489 if (rc < 0)
7490 goto unmap_cfgtable;
7491 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007492 dev_warn(&pdev->dev, "Unable to successfully reset "
7493 "controller. Will try soft reset.\n");
7494 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007495 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007496 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007497 }
7498
7499unmap_cfgtable:
7500 iounmap(cfgtable);
7501
7502unmap_vaddr:
7503 iounmap(vaddr);
7504 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007505}
7506
7507/*
7508 * We cannot read the structure directly, for portability we must use
7509 * the io functions.
7510 * This is for debug only.
7511 */
Don Brace42a91642014-11-14 17:26:27 -06007512static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007513{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007514#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007515 int i;
7516 char temp_name[17];
7517
7518 dev_info(dev, "Controller Configuration information\n");
7519 dev_info(dev, "------------------------------------\n");
7520 for (i = 0; i < 4; i++)
7521 temp_name[i] = readb(&(tb->Signature[i]));
7522 temp_name[4] = '\0';
7523 dev_info(dev, " Signature = %s\n", temp_name);
7524 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
7525 dev_info(dev, " Transport methods supported = 0x%x\n",
7526 readl(&(tb->TransportSupport)));
7527 dev_info(dev, " Transport methods active = 0x%x\n",
7528 readl(&(tb->TransportActive)));
7529 dev_info(dev, " Requested transport Method = 0x%x\n",
7530 readl(&(tb->HostWrite.TransportRequest)));
7531 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
7532 readl(&(tb->HostWrite.CoalIntDelay)));
7533 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
7534 readl(&(tb->HostWrite.CoalIntCount)));
Robert Elliott69d6e332015-01-23 16:41:56 -06007535 dev_info(dev, " Max outstanding commands = %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007536 readl(&(tb->CmdsOutMax)));
7537 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
7538 for (i = 0; i < 16; i++)
7539 temp_name[i] = readb(&(tb->ServerName[i]));
7540 temp_name[16] = '\0';
7541 dev_info(dev, " Server Name = %s\n", temp_name);
7542 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
7543 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007544#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007545}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007546
7547static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
7548{
7549 int i, offset, mem_type, bar_type;
7550
7551 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
7552 return 0;
7553 offset = 0;
7554 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
7555 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
7556 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
7557 offset += 4;
7558 else {
7559 mem_type = pci_resource_flags(pdev, i) &
7560 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
7561 switch (mem_type) {
7562 case PCI_BASE_ADDRESS_MEM_TYPE_32:
7563 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
7564 offset += 4; /* 32 bit */
7565 break;
7566 case PCI_BASE_ADDRESS_MEM_TYPE_64:
7567 offset += 8;
7568 break;
7569 default: /* reserved in PCI 2.2 */
7570 dev_warn(&pdev->dev,
7571 "base address is invalid\n");
7572 return -1;
7573 break;
7574 }
7575 }
7576 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
7577 return i + 1;
7578 }
7579 return -1;
7580}
7581
Robert Elliottcc64c812015-04-23 09:33:12 -05007582static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
7583{
7584 if (h->msix_vector) {
7585 if (h->pdev->msix_enabled)
7586 pci_disable_msix(h->pdev);
Robert Elliott105a3db2015-04-23 09:33:48 -05007587 h->msix_vector = 0;
Robert Elliottcc64c812015-04-23 09:33:12 -05007588 } else if (h->msi_vector) {
7589 if (h->pdev->msi_enabled)
7590 pci_disable_msi(h->pdev);
Robert Elliott105a3db2015-04-23 09:33:48 -05007591 h->msi_vector = 0;
Robert Elliottcc64c812015-04-23 09:33:12 -05007592 }
7593}
7594
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007595/* If MSI/MSI-X is supported by the kernel we will try to enable it on
Stephen Cameron050f7142015-01-23 16:42:22 -06007596 * controllers that are capable. If not, we use legacy INTx mode.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007597 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007598static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007599{
7600#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05007601 int err, i;
7602 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
7603
7604 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
7605 hpsa_msix_entries[i].vector = 0;
7606 hpsa_msix_entries[i].entry = i;
7607 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007608
7609 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05007610 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
7611 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007612 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007613 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007614 dev_info(&h->pdev->dev, "MSI-X capable controller\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007615 h->msix_vector = MAX_REPLY_QUEUES;
Stephen M. Cameronf89439b2014-05-29 10:53:02 -05007616 if (h->msix_vector > num_online_cpus())
7617 h->msix_vector = num_online_cpus();
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007618 err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
7619 1, h->msix_vector);
7620 if (err < 0) {
7621 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
7622 h->msix_vector = 0;
7623 goto single_msi_mode;
7624 } else if (err < h->msix_vector) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007625 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007626 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007627 }
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007628 h->msix_vector = err;
7629 for (i = 0; i < h->msix_vector; i++)
7630 h->intr[i] = hpsa_msix_entries[i].vector;
7631 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007632 }
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007633single_msi_mode:
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007634 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007635 dev_info(&h->pdev->dev, "MSI capable controller\n");
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007636 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007637 h->msi_vector = 1;
7638 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007639 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007640 }
7641default_int_mode:
7642#endif /* CONFIG_PCI_MSI */
7643 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06007644 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007645}
7646
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007647static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007648{
7649 int i;
7650 u32 subsystem_vendor_id, subsystem_device_id;
7651
7652 subsystem_vendor_id = pdev->subsystem_vendor;
7653 subsystem_device_id = pdev->subsystem_device;
7654 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
7655 subsystem_vendor_id;
7656
7657 for (i = 0; i < ARRAY_SIZE(products); i++)
7658 if (*board_id == products[i].board_id)
7659 return i;
7660
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05007661 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
7662 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
7663 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007664 dev_warn(&pdev->dev, "unrecognized board ID: "
7665 "0x%08x, ignoring.\n", *board_id);
7666 return -ENODEV;
7667 }
7668 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
7669}
7670
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007671static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
7672 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007673{
7674 int i;
7675
7676 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007677 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007678 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007679 *memory_bar = pci_resource_start(pdev, i);
7680 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007681 *memory_bar);
7682 return 0;
7683 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007684 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007685 return -ENODEV;
7686}
7687
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007688static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
7689 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007690{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007691 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007692 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007693 if (wait_for_ready)
7694 iterations = HPSA_BOARD_READY_ITERATIONS;
7695 else
7696 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007697
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007698 for (i = 0; i < iterations; i++) {
7699 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
7700 if (wait_for_ready) {
7701 if (scratchpad == HPSA_FIRMWARE_READY)
7702 return 0;
7703 } else {
7704 if (scratchpad != HPSA_FIRMWARE_READY)
7705 return 0;
7706 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007707 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
7708 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007709 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007710 return -ENODEV;
7711}
7712
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007713static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
7714 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
7715 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007716{
7717 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
7718 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
7719 *cfg_base_addr &= (u32) 0x0000ffff;
7720 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
7721 if (*cfg_base_addr_index == -1) {
7722 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
7723 return -ENODEV;
7724 }
7725 return 0;
7726}
7727
Robert Elliott195f2c62015-04-23 09:33:17 -05007728static void hpsa_free_cfgtables(struct ctlr_info *h)
7729{
Robert Elliott105a3db2015-04-23 09:33:48 -05007730 if (h->transtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007731 iounmap(h->transtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007732 h->transtable = NULL;
7733 }
7734 if (h->cfgtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007735 iounmap(h->cfgtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007736 h->cfgtable = NULL;
7737 }
Robert Elliott195f2c62015-04-23 09:33:17 -05007738}
7739
7740/* Find and map CISS config table and transfer table
7741+ * several items must be unmapped (freed) later
7742+ * */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007743static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007744{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06007745 u64 cfg_offset;
7746 u32 cfg_base_addr;
7747 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06007748 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007749 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007750
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007751 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7752 &cfg_base_addr_index, &cfg_offset);
7753 if (rc)
7754 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007755 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007756 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007757 if (!h->cfgtable) {
7758 dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007759 return -ENOMEM;
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007760 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007761 rc = write_driver_ver_to_cfgtable(h->cfgtable);
7762 if (rc)
7763 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007764 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007765 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007766 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
7767 cfg_base_addr_index)+cfg_offset+trans_offset,
7768 sizeof(*h->transtable));
Robert Elliott195f2c62015-04-23 09:33:17 -05007769 if (!h->transtable) {
7770 dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
7771 hpsa_free_cfgtables(h);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007772 return -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05007773 }
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007774 return 0;
7775}
7776
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007777static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007778{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007779#define MIN_MAX_COMMANDS 16
7780 BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
7781
7782 h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06007783
7784 /* Limit commands in memory limited kdump scenario. */
7785 if (reset_devices && h->max_commands > 32)
7786 h->max_commands = 32;
7787
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007788 if (h->max_commands < MIN_MAX_COMMANDS) {
7789 dev_warn(&h->pdev->dev,
7790 "Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
7791 h->max_commands,
7792 MIN_MAX_COMMANDS);
7793 h->max_commands = MIN_MAX_COMMANDS;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007794 }
7795}
7796
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007797/* If the controller reports that the total max sg entries is greater than 512,
7798 * then we know that chained SG blocks work. (Original smart arrays did not
7799 * support chained SG blocks and would return zero for max sg entries.)
7800 */
7801static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
7802{
7803 return h->maxsgentries > 512;
7804}
7805
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007806/* Interrogate the hardware for some limits:
7807 * max commands, max SG elements without chaining, and with chaining,
7808 * SG chain block size, etc.
7809 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007810static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007811{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007812 hpsa_get_max_perf_mode_cmds(h);
Stephen Cameron45fcb862015-01-23 16:43:04 -06007813 h->nr_cmds = h->max_commands;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007814 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007815 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007816 if (hpsa_supports_chained_sg_blocks(h)) {
7817 /* Limit in-command s/g elements to 32 save dma'able memory. */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007818 h->max_cmd_sg_entries = 32;
Webb Scales1a63ea62014-11-14 17:26:43 -06007819 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007820 h->maxsgentries--; /* save one for chain pointer */
7821 } else {
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007822 /*
7823 * Original smart arrays supported at most 31 s/g entries
7824 * embedded inline in the command (trying to use more
7825 * would lock up the controller)
7826 */
7827 h->max_cmd_sg_entries = 31;
Webb Scales1a63ea62014-11-14 17:26:43 -06007828 h->maxsgentries = 31; /* default to traditional values */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007829 h->chainsize = 0;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007830 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05007831
7832 /* Find out what task management functions are supported and cache */
7833 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06007834 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
7835 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
7836 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
7837 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen Cameron8be986c2015-04-23 09:34:06 -05007838 if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
7839 dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007840}
7841
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007842static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
7843{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09007844 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007845 dev_err(&h->pdev->dev, "not a valid CISS config table\n");
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007846 return false;
7847 }
7848 return true;
7849}
7850
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007851static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007852{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007853 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007854
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007855 driver_support = readl(&(h->cfgtable->driver_support));
Arnd Bergmann0b9e7b72014-06-26 15:44:52 +02007856 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
7857#ifdef CONFIG_X86
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007858 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007859#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06007860 driver_support |= ENABLE_UNIT_ATTN;
7861 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007862}
7863
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05007864/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
7865 * in a prefetch beyond physical memory.
7866 */
7867static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
7868{
7869 u32 dma_prefetch;
7870
7871 if (h->board_id != 0x3225103C)
7872 return;
7873 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
7874 dma_prefetch |= 0x8000;
7875 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
7876}
7877
Robert Elliottc706a792015-01-23 16:45:01 -06007878static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007879{
7880 int i;
7881 u32 doorbell_value;
7882 unsigned long flags;
7883 /* wait until the clear_event_notify bit 6 is cleared by controller. */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007884 for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007885 spin_lock_irqsave(&h->lock, flags);
7886 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7887 spin_unlock_irqrestore(&h->lock, flags);
7888 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
Robert Elliottc706a792015-01-23 16:45:01 -06007889 goto done;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007890 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007891 msleep(CLEAR_EVENT_WAIT_INTERVAL);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007892 }
Robert Elliottc706a792015-01-23 16:45:01 -06007893 return -ENODEV;
7894done:
7895 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007896}
7897
Robert Elliottc706a792015-01-23 16:45:01 -06007898static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007899{
7900 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007901 u32 doorbell_value;
7902 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007903
7904 /* under certain very rare conditions, this can take awhile.
7905 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
7906 * as we enter this code.)
7907 */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007908 for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
Webb Scales25163bd2015-04-23 09:32:00 -05007909 if (h->remove_in_progress)
7910 goto done;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007911 spin_lock_irqsave(&h->lock, flags);
7912 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7913 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06007914 if (!(doorbell_value & CFGTBL_ChangeReq))
Robert Elliottc706a792015-01-23 16:45:01 -06007915 goto done;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007916 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007917 msleep(MODE_CHANGE_WAIT_INTERVAL);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007918 }
Robert Elliottc706a792015-01-23 16:45:01 -06007919 return -ENODEV;
7920done:
7921 return 0;
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007922}
7923
Robert Elliottc706a792015-01-23 16:45:01 -06007924/* return -ENODEV or other reason on error, 0 on success */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007925static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007926{
7927 u32 trans_support;
7928
7929 trans_support = readl(&(h->cfgtable->TransportSupport));
7930 if (!(trans_support & SIMPLE_MODE))
7931 return -ENOTSUPP;
7932
7933 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007934
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007935 /* Update the field, and then ring the doorbell */
7936 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007937 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007938 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06007939 if (hpsa_wait_for_mode_change_ack(h))
7940 goto error;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007941 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007942 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
7943 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06007944 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007945 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007946error:
Stephen Cameron050f7142015-01-23 16:42:22 -06007947 dev_err(&h->pdev->dev, "failed to enter simple mode\n");
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007948 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007949}
7950
Robert Elliott195f2c62015-04-23 09:33:17 -05007951/* free items allocated or mapped by hpsa_pci_init */
7952static void hpsa_free_pci_init(struct ctlr_info *h)
7953{
7954 hpsa_free_cfgtables(h); /* pci_init 4 */
7955 iounmap(h->vaddr); /* pci_init 3 */
Robert Elliott105a3db2015-04-23 09:33:48 -05007956 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05007957 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Robert Elliott943a7022015-04-23 09:34:32 -05007958 /*
7959 * call pci_disable_device before pci_release_regions per
7960 * Documentation/PCI/pci.txt
7961 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007962 pci_disable_device(h->pdev); /* pci_init 1 */
Robert Elliott943a7022015-04-23 09:34:32 -05007963 pci_release_regions(h->pdev); /* pci_init 2 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007964}
7965
7966/* several items must be freed later */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007967static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007968{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007969 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007970
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007971 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
7972 if (prod_index < 0)
Robert Elliott60f923b2015-01-23 16:42:06 -06007973 return prod_index;
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007974 h->product_name = products[prod_index].product_name;
7975 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007976
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05007977 h->needs_abort_tags_swizzled =
7978 ctlr_needs_abort_tags_swizzled(h->board_id);
7979
Matthew Garrette5a44df2011-11-11 11:14:23 -05007980 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
7981 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
7982
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007983 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007984 if (err) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007985 dev_err(&h->pdev->dev, "failed to enable PCI device\n");
Robert Elliott943a7022015-04-23 09:34:32 -05007986 pci_disable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007987 return err;
7988 }
7989
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06007990 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007991 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007992 dev_err(&h->pdev->dev,
Robert Elliott195f2c62015-04-23 09:33:17 -05007993 "failed to obtain PCI resources\n");
Robert Elliott943a7022015-04-23 09:34:32 -05007994 pci_disable_device(h->pdev);
7995 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007996 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06007997
7998 pci_set_master(h->pdev);
7999
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05008000 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05008001 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05008002 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05008003 goto clean2; /* intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008004 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05008005 if (!h->vaddr) {
Robert Elliott195f2c62015-04-23 09:33:17 -05008006 dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
Stephen M. Cameron204892e2010-05-27 15:13:22 -05008007 err = -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05008008 goto clean2; /* intmode+region, pci */
Stephen M. Cameron204892e2010-05-27 15:13:22 -05008009 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06008010 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05008011 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05008012 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameron77c44952010-05-27 15:13:17 -05008013 err = hpsa_find_cfgtables(h);
8014 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05008015 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05008016 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008017
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05008018 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008019 err = -ENODEV;
Robert Elliott195f2c62015-04-23 09:33:17 -05008020 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008021 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06008022 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05008023 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05008024 err = hpsa_enter_simple_mode(h);
8025 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05008026 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008027 return 0;
8028
Robert Elliott195f2c62015-04-23 09:33:17 -05008029clean4: /* cfgtables, vaddr, intmode+region, pci */
8030 hpsa_free_cfgtables(h);
8031clean3: /* vaddr, intmode+region, pci */
8032 iounmap(h->vaddr);
Robert Elliott105a3db2015-04-23 09:33:48 -05008033 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05008034clean2: /* intmode+region, pci */
8035 hpsa_disable_interrupt_mode(h);
Robert Elliott943a7022015-04-23 09:34:32 -05008036 /*
8037 * call pci_disable_device before pci_release_regions per
8038 * Documentation/PCI/pci.txt
8039 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008040 pci_disable_device(h->pdev);
Robert Elliott943a7022015-04-23 09:34:32 -05008041 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008042 return err;
8043}
8044
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008045static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06008046{
8047 int rc;
8048
8049#define HBA_INQUIRY_BYTE_COUNT 64
8050 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
8051 if (!h->hba_inquiry_data)
8052 return;
8053 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
8054 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
8055 if (rc != 0) {
8056 kfree(h->hba_inquiry_data);
8057 h->hba_inquiry_data = NULL;
8058 }
8059}
8060
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008061static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008062{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05008063 int rc, i;
Tomas Henzl3b747292015-01-23 16:41:20 -06008064 void __iomem *vaddr;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008065
8066 if (!reset_devices)
8067 return 0;
8068
Tomas Henzl132aa222014-08-14 16:12:39 +02008069 /* kdump kernel is loading, we don't know in which state is
8070 * the pci interface. The dev->enable_cnt is equal zero
8071 * so we call enable+disable, wait a while and switch it on.
8072 */
8073 rc = pci_enable_device(pdev);
8074 if (rc) {
8075 dev_warn(&pdev->dev, "Failed to enable PCI device\n");
8076 return -ENODEV;
8077 }
8078 pci_disable_device(pdev);
8079 msleep(260); /* a randomly chosen number */
8080 rc = pci_enable_device(pdev);
8081 if (rc) {
8082 dev_warn(&pdev->dev, "failed to enable device.\n");
8083 return -ENODEV;
8084 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06008085
Tomas Henzl859c75a2014-09-12 14:44:15 +02008086 pci_set_master(pdev);
Robert Elliott4fa604e2014-11-14 17:27:24 -06008087
Tomas Henzl3b747292015-01-23 16:41:20 -06008088 vaddr = pci_ioremap_bar(pdev, 0);
8089 if (vaddr == NULL) {
8090 rc = -ENOMEM;
8091 goto out_disable;
8092 }
8093 writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
8094 iounmap(vaddr);
8095
Stephen M. Cameron1df85522010-06-16 13:51:40 -05008096 /* Reset the controller with a PCI power-cycle or via doorbell */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008097 rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008098
Stephen M. Cameron1df85522010-06-16 13:51:40 -05008099 /* -ENOTSUPP here means we cannot reset the controller
8100 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05008101 * "performant mode". Or, it might be 640x, which can't reset
8102 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05008103 */
Robert Elliottadf1b3a2015-01-23 16:42:01 -06008104 if (rc)
Tomas Henzl132aa222014-08-14 16:12:39 +02008105 goto out_disable;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008106
8107 /* Now try to get the controller to respond to a no-op */
Robert Elliott1ba66c92015-01-23 16:42:11 -06008108 dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008109 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
8110 if (hpsa_noop(pdev) == 0)
8111 break;
8112 else
8113 dev_warn(&pdev->dev, "no-op failed%s\n",
8114 (i < 11 ? "; re-trying" : ""));
8115 }
Tomas Henzl132aa222014-08-14 16:12:39 +02008116
8117out_disable:
8118
8119 pci_disable_device(pdev);
8120 return rc;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008121}
8122
Robert Elliott1fb7c982015-04-23 09:33:22 -05008123static void hpsa_free_cmd_pool(struct ctlr_info *h)
8124{
8125 kfree(h->cmd_pool_bits);
Robert Elliott105a3db2015-04-23 09:33:48 -05008126 h->cmd_pool_bits = NULL;
8127 if (h->cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05008128 pci_free_consistent(h->pdev,
8129 h->nr_cmds * sizeof(struct CommandList),
8130 h->cmd_pool,
8131 h->cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05008132 h->cmd_pool = NULL;
8133 h->cmd_pool_dhandle = 0;
8134 }
8135 if (h->errinfo_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05008136 pci_free_consistent(h->pdev,
8137 h->nr_cmds * sizeof(struct ErrorInfo),
8138 h->errinfo_pool,
8139 h->errinfo_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05008140 h->errinfo_pool = NULL;
8141 h->errinfo_pool_dhandle = 0;
8142 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05008143}
8144
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008145static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008146{
8147 h->cmd_pool_bits = kzalloc(
8148 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
8149 sizeof(unsigned long), GFP_KERNEL);
8150 h->cmd_pool = pci_alloc_consistent(h->pdev,
8151 h->nr_cmds * sizeof(*h->cmd_pool),
8152 &(h->cmd_pool_dhandle));
8153 h->errinfo_pool = pci_alloc_consistent(h->pdev,
8154 h->nr_cmds * sizeof(*h->errinfo_pool),
8155 &(h->errinfo_pool_dhandle));
8156 if ((h->cmd_pool_bits == NULL)
8157 || (h->cmd_pool == NULL)
8158 || (h->errinfo_pool == NULL)) {
8159 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
Robert Elliott2c143342015-01-23 16:42:48 -06008160 goto clean_up;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008161 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05008162 hpsa_preinitialize_commands(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008163 return 0;
Robert Elliott2c143342015-01-23 16:42:48 -06008164clean_up:
8165 hpsa_free_cmd_pool(h);
8166 return -ENOMEM;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008167}
8168
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05008169static void hpsa_irq_affinity_hints(struct ctlr_info *h)
8170{
Fabian Frederickec429952015-01-23 16:41:46 -06008171 int i, cpu;
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05008172
8173 cpu = cpumask_first(cpu_online_mask);
8174 for (i = 0; i < h->msix_vector; i++) {
Fabian Frederickec429952015-01-23 16:41:46 -06008175 irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05008176 cpu = cpumask_next(cpu, cpu_online_mask);
8177 }
8178}
8179
Robert Elliottec501a12015-01-23 16:41:40 -06008180/* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
8181static void hpsa_free_irqs(struct ctlr_info *h)
8182{
8183 int i;
8184
8185 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
8186 /* Single reply queue, only one irq to free */
8187 i = h->intr_mode;
8188 irq_set_affinity_hint(h->intr[i], NULL);
8189 free_irq(h->intr[i], &h->q[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008190 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008191 return;
8192 }
8193
8194 for (i = 0; i < h->msix_vector; i++) {
8195 irq_set_affinity_hint(h->intr[i], NULL);
8196 free_irq(h->intr[i], &h->q[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008197 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008198 }
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008199 for (; i < MAX_REPLY_QUEUES; i++)
8200 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008201}
8202
Robert Elliott9ee61792015-01-23 16:42:32 -06008203/* returns 0 on success; cleans up and returns -Enn on error */
8204static int hpsa_request_irqs(struct ctlr_info *h,
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008205 irqreturn_t (*msixhandler)(int, void *),
8206 irqreturn_t (*intxhandler)(int, void *))
8207{
Matt Gates254f7962012-05-01 11:43:06 -05008208 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008209
Matt Gates254f7962012-05-01 11:43:06 -05008210 /*
8211 * initialize h->q[x] = x so that interrupt handlers know which
8212 * queue to process.
8213 */
8214 for (i = 0; i < MAX_REPLY_QUEUES; i++)
8215 h->q[i] = (u8) i;
8216
Hannes Reineckeeee0f032014-01-15 13:30:53 +01008217 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05008218 /* If performant mode and MSI-X, use multiple reply queues */
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008219 for (i = 0; i < h->msix_vector; i++) {
Robert Elliott8b470042015-04-23 09:34:58 -05008220 sprintf(h->intrname[i], "%s-msix%d", h->devname, i);
Matt Gates254f7962012-05-01 11:43:06 -05008221 rc = request_irq(h->intr[i], msixhandler,
Robert Elliott8b470042015-04-23 09:34:58 -05008222 0, h->intrname[i],
Matt Gates254f7962012-05-01 11:43:06 -05008223 &h->q[i]);
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008224 if (rc) {
8225 int j;
8226
8227 dev_err(&h->pdev->dev,
8228 "failed to get irq %d for %s\n",
8229 h->intr[i], h->devname);
8230 for (j = 0; j < i; j++) {
8231 free_irq(h->intr[j], &h->q[j]);
8232 h->q[j] = 0;
8233 }
8234 for (; j < MAX_REPLY_QUEUES; j++)
8235 h->q[j] = 0;
8236 return rc;
8237 }
8238 }
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05008239 hpsa_irq_affinity_hints(h);
Matt Gates254f7962012-05-01 11:43:06 -05008240 } else {
8241 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01008242 if (h->msix_vector > 0 || h->msi_vector) {
Robert Elliott8b470042015-04-23 09:34:58 -05008243 if (h->msix_vector)
8244 sprintf(h->intrname[h->intr_mode],
8245 "%s-msix", h->devname);
8246 else
8247 sprintf(h->intrname[h->intr_mode],
8248 "%s-msi", h->devname);
Matt Gates254f7962012-05-01 11:43:06 -05008249 rc = request_irq(h->intr[h->intr_mode],
Robert Elliott8b470042015-04-23 09:34:58 -05008250 msixhandler, 0,
8251 h->intrname[h->intr_mode],
Matt Gates254f7962012-05-01 11:43:06 -05008252 &h->q[h->intr_mode]);
8253 } else {
Robert Elliott8b470042015-04-23 09:34:58 -05008254 sprintf(h->intrname[h->intr_mode],
8255 "%s-intx", h->devname);
Matt Gates254f7962012-05-01 11:43:06 -05008256 rc = request_irq(h->intr[h->intr_mode],
Robert Elliott8b470042015-04-23 09:34:58 -05008257 intxhandler, IRQF_SHARED,
8258 h->intrname[h->intr_mode],
Matt Gates254f7962012-05-01 11:43:06 -05008259 &h->q[h->intr_mode]);
8260 }
Robert Elliott105a3db2015-04-23 09:33:48 -05008261 irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
Matt Gates254f7962012-05-01 11:43:06 -05008262 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008263 if (rc) {
Robert Elliott195f2c62015-04-23 09:33:17 -05008264 dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008265 h->intr[h->intr_mode], h->devname);
Robert Elliott195f2c62015-04-23 09:33:17 -05008266 hpsa_free_irqs(h);
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008267 return -ENODEV;
8268 }
8269 return 0;
8270}
8271
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008272static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008273{
Robert Elliott39c53f52015-04-23 09:35:14 -05008274 int rc;
Robert Elliottbf43caf2015-04-23 09:33:38 -05008275 hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008276
8277 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008278 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY);
8279 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008280 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008281 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008282 }
8283
8284 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008285 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
8286 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008287 dev_warn(&h->pdev->dev, "Board failed to become ready "
8288 "after soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008289 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008290 }
8291
8292 return 0;
8293}
8294
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008295static void hpsa_free_reply_queues(struct ctlr_info *h)
8296{
8297 int i;
8298
8299 for (i = 0; i < h->nreply_queues; i++) {
8300 if (!h->reply_queue[i].head)
8301 continue;
Robert Elliott1fb7c982015-04-23 09:33:22 -05008302 pci_free_consistent(h->pdev,
8303 h->reply_queue_size,
8304 h->reply_queue[i].head,
8305 h->reply_queue[i].busaddr);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008306 h->reply_queue[i].head = NULL;
8307 h->reply_queue[i].busaddr = 0;
8308 }
Robert Elliott105a3db2015-04-23 09:33:48 -05008309 h->reply_queue_size = 0;
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008310}
8311
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05008312static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
8313{
Robert Elliott105a3db2015-04-23 09:33:48 -05008314 hpsa_free_performant_mode(h); /* init_one 7 */
8315 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
8316 hpsa_free_cmd_pool(h); /* init_one 5 */
8317 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliott2946e822015-04-23 09:35:09 -05008318 scsi_host_put(h->scsi_host); /* init_one 3 */
8319 h->scsi_host = NULL; /* init_one 3 */
8320 hpsa_free_pci_init(h); /* init_one 2_5 */
Robert Elliott9ecd9532015-04-23 09:34:43 -05008321 free_percpu(h->lockup_detected); /* init_one 2 */
8322 h->lockup_detected = NULL; /* init_one 2 */
8323 if (h->resubmit_wq) {
8324 destroy_workqueue(h->resubmit_wq); /* init_one 1 */
8325 h->resubmit_wq = NULL;
8326 }
8327 if (h->rescan_ctlr_wq) {
8328 destroy_workqueue(h->rescan_ctlr_wq);
8329 h->rescan_ctlr_wq = NULL;
8330 }
Robert Elliott105a3db2015-04-23 09:33:48 -05008331 kfree(h); /* init_one 1 */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008332}
8333
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008334/* Called when controller lockup detected. */
Don Bracef2405db2015-01-23 16:43:09 -06008335static void fail_all_outstanding_cmds(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008336{
Webb Scales281a7fd2015-01-23 16:43:35 -06008337 int i, refcount;
8338 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05008339 int failcount = 0;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008340
Don Brace080ef1c2015-01-23 16:43:25 -06008341 flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
Don Bracef2405db2015-01-23 16:43:09 -06008342 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06008343 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06008344 refcount = atomic_inc_return(&c->refcount);
8345 if (refcount > 1) {
Webb Scales25163bd2015-04-23 09:32:00 -05008346 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
Webb Scales281a7fd2015-01-23 16:43:35 -06008347 finish_cmd(c);
Stephen Cameron433b5f42015-04-23 09:32:11 -05008348 atomic_dec(&h->commands_outstanding);
Webb Scales25163bd2015-04-23 09:32:00 -05008349 failcount++;
Webb Scales281a7fd2015-01-23 16:43:35 -06008350 }
8351 cmd_free(h, c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008352 }
Webb Scales25163bd2015-04-23 09:32:00 -05008353 dev_warn(&h->pdev->dev,
8354 "failed %d commands in fail_all\n", failcount);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008355}
8356
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008357static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
8358{
Rusty Russellc8ed0012015-03-05 10:49:19 +10308359 int cpu;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008360
Rusty Russellc8ed0012015-03-05 10:49:19 +10308361 for_each_online_cpu(cpu) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008362 u32 *lockup_detected;
8363 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
8364 *lockup_detected = value;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008365 }
8366 wmb(); /* be sure the per-cpu variables are out to memory */
8367}
8368
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008369static void controller_lockup_detected(struct ctlr_info *h)
8370{
8371 unsigned long flags;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008372 u32 lockup_detected;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008373
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008374 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8375 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008376 lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
8377 if (!lockup_detected) {
8378 /* no heartbeat, but controller gave us a zero. */
8379 dev_warn(&h->pdev->dev,
Webb Scales25163bd2015-04-23 09:32:00 -05008380 "lockup detected after %d but scratchpad register is zero\n",
8381 h->heartbeat_sample_interval / HZ);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008382 lockup_detected = 0xffffffff;
8383 }
8384 set_lockup_detected_for_all_cpus(h, lockup_detected);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008385 spin_unlock_irqrestore(&h->lock, flags);
Webb Scales25163bd2015-04-23 09:32:00 -05008386 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
8387 lockup_detected, h->heartbeat_sample_interval / HZ);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008388 pci_disable_device(h->pdev);
Don Bracef2405db2015-01-23 16:43:09 -06008389 fail_all_outstanding_cmds(h);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008390}
8391
Webb Scales25163bd2015-04-23 09:32:00 -05008392static int detect_controller_lockup(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008393{
8394 u64 now;
8395 u32 heartbeat;
8396 unsigned long flags;
8397
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008398 now = get_jiffies_64();
8399 /* If we've received an interrupt recently, we're ok. */
8400 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05008401 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05008402 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008403
8404 /*
8405 * If we've already checked the heartbeat recently, we're ok.
8406 * This could happen if someone sends us a signal. We
8407 * otherwise don't care about signals in this thread.
8408 */
8409 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05008410 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05008411 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008412
8413 /* If heartbeat has not changed since we last looked, we're not ok. */
8414 spin_lock_irqsave(&h->lock, flags);
8415 heartbeat = readl(&h->cfgtable->HeartBeat);
8416 spin_unlock_irqrestore(&h->lock, flags);
8417 if (h->last_heartbeat == heartbeat) {
8418 controller_lockup_detected(h);
Webb Scales25163bd2015-04-23 09:32:00 -05008419 return true;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008420 }
8421
8422 /* We're ok. */
8423 h->last_heartbeat = heartbeat;
8424 h->last_heartbeat_timestamp = now;
Webb Scales25163bd2015-04-23 09:32:00 -05008425 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008426}
8427
Stephen M. Cameron98465902014-02-21 16:25:00 -06008428static void hpsa_ack_ctlr_events(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008429{
8430 int i;
8431 char *event_type;
8432
Stephen Camerone4aa3e62015-01-23 16:44:07 -06008433 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
8434 return;
8435
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008436 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06008437 if ((h->transMethod & (CFGTBL_Trans_io_accel1
8438 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008439 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
8440 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
8441
8442 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
8443 event_type = "state change";
8444 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
8445 event_type = "configuration change";
8446 /* Stop sending new RAID offload reqs via the IO accelerator */
8447 scsi_block_requests(h->scsi_host);
Don Brace5323ed72016-04-27 17:13:59 -05008448 for (i = 0; i < h->ndevices; i++) {
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008449 h->dev[i]->offload_enabled = 0;
Don Brace5323ed72016-04-27 17:13:59 -05008450 h->dev[i]->offload_to_be_enabled = 0;
8451 }
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06008452 hpsa_drain_accel_commands(h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008453 /* Set 'accelerator path config change' bit */
8454 dev_warn(&h->pdev->dev,
8455 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
8456 h->events, event_type);
8457 writel(h->events, &(h->cfgtable->clear_event_notify));
8458 /* Set the "clear event notify field update" bit 6 */
8459 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8460 /* Wait until ctlr clears 'clear event notify field', bit 6 */
8461 hpsa_wait_for_clear_event_notify_ack(h);
8462 scsi_unblock_requests(h->scsi_host);
8463 } else {
8464 /* Acknowledge controller notification events. */
8465 writel(h->events, &(h->cfgtable->clear_event_notify));
8466 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8467 hpsa_wait_for_clear_event_notify_ack(h);
8468#if 0
8469 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8470 hpsa_wait_for_mode_change_ack(h);
8471#endif
8472 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008473 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008474}
8475
8476/* Check a register on the controller to see if there are configuration
8477 * changes (added/changed/removed logical drives, etc.) which mean that
Scott Teele863d682014-02-18 13:57:05 -06008478 * we should rescan the controller for devices.
8479 * Also check flag for driver-initiated rescan.
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008480 */
Stephen M. Cameron98465902014-02-21 16:25:00 -06008481static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008482{
Don Brace853633e2015-11-04 15:50:37 -06008483 if (h->drv_req_rescan) {
8484 h->drv_req_rescan = 0;
8485 return 1;
8486 }
8487
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008488 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
Stephen M. Cameron98465902014-02-21 16:25:00 -06008489 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008490
8491 h->events = readl(&(h->cfgtable->event_notify));
Stephen M. Cameron98465902014-02-21 16:25:00 -06008492 return h->events & RESCAN_REQUIRED_EVENT_BITS;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008493}
8494
Stephen M. Cameron98465902014-02-21 16:25:00 -06008495/*
8496 * Check if any of the offline devices have become ready
8497 */
8498static int hpsa_offline_devices_ready(struct ctlr_info *h)
8499{
8500 unsigned long flags;
8501 struct offline_device_entry *d;
8502 struct list_head *this, *tmp;
8503
8504 spin_lock_irqsave(&h->offline_device_lock, flags);
8505 list_for_each_safe(this, tmp, &h->offline_device_list) {
8506 d = list_entry(this, struct offline_device_entry,
8507 offline_list);
8508 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008509 if (!hpsa_volume_offline(h, d->scsi3addr)) {
8510 spin_lock_irqsave(&h->offline_device_lock, flags);
8511 list_del(&d->offline_list);
8512 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008513 return 1;
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008514 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008515 spin_lock_irqsave(&h->offline_device_lock, flags);
8516 }
8517 spin_unlock_irqrestore(&h->offline_device_lock, flags);
8518 return 0;
8519}
8520
Scott Teel34592252015-11-04 15:52:09 -06008521static int hpsa_luns_changed(struct ctlr_info *h)
8522{
8523 int rc = 1; /* assume there are changes */
8524 struct ReportLUNdata *logdev = NULL;
8525
8526 /* if we can't find out if lun data has changed,
8527 * assume that it has.
8528 */
8529
8530 if (!h->lastlogicals)
8531 goto out;
8532
8533 logdev = kzalloc(sizeof(*logdev), GFP_KERNEL);
8534 if (!logdev) {
8535 dev_warn(&h->pdev->dev,
8536 "Out of memory, can't track lun changes.\n");
8537 goto out;
8538 }
8539 if (hpsa_scsi_do_report_luns(h, 1, logdev, sizeof(*logdev), 0)) {
8540 dev_warn(&h->pdev->dev,
8541 "report luns failed, can't track lun changes.\n");
8542 goto out;
8543 }
8544 if (memcmp(logdev, h->lastlogicals, sizeof(*logdev))) {
8545 dev_info(&h->pdev->dev,
8546 "Lun changes detected.\n");
8547 memcpy(h->lastlogicals, logdev, sizeof(*logdev));
8548 goto out;
8549 } else
8550 rc = 0; /* no changes detected. */
8551out:
8552 kfree(logdev);
8553 return rc;
8554}
8555
Don Brace6636e7f2015-01-23 16:45:17 -06008556static void hpsa_rescan_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008557{
8558 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008559 struct ctlr_info *h = container_of(to_delayed_work(work),
Don Brace6636e7f2015-01-23 16:45:17 -06008560 struct ctlr_info, rescan_ctlr_work);
8561
8562
8563 if (h->remove_in_progress)
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008564 return;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008565
8566 if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
8567 scsi_host_get(h->scsi_host);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008568 hpsa_ack_ctlr_events(h);
8569 hpsa_scan_start(h->scsi_host);
8570 scsi_host_put(h->scsi_host);
Scott Teel34592252015-11-04 15:52:09 -06008571 } else if (h->discovery_polling) {
Scott Teelc2adae42015-11-04 15:52:16 -06008572 hpsa_disable_rld_caching(h);
Scott Teel34592252015-11-04 15:52:09 -06008573 if (hpsa_luns_changed(h)) {
8574 struct Scsi_Host *sh = NULL;
8575
8576 dev_info(&h->pdev->dev,
8577 "driver discovery polling rescan.\n");
8578 sh = scsi_host_get(h->scsi_host);
8579 if (sh != NULL) {
8580 hpsa_scan_start(sh);
8581 scsi_host_put(sh);
8582 }
8583 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008584 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008585 spin_lock_irqsave(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06008586 if (!h->remove_in_progress)
8587 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008588 h->heartbeat_sample_interval);
8589 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008590}
8591
Don Brace6636e7f2015-01-23 16:45:17 -06008592static void hpsa_monitor_ctlr_worker(struct work_struct *work)
8593{
8594 unsigned long flags;
8595 struct ctlr_info *h = container_of(to_delayed_work(work),
8596 struct ctlr_info, monitor_ctlr_work);
8597
8598 detect_controller_lockup(h);
8599 if (lockup_detected(h))
8600 return;
8601
8602 spin_lock_irqsave(&h->lock, flags);
8603 if (!h->remove_in_progress)
8604 schedule_delayed_work(&h->monitor_ctlr_work,
8605 h->heartbeat_sample_interval);
8606 spin_unlock_irqrestore(&h->lock, flags);
8607}
8608
8609static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
8610 char *name)
8611{
8612 struct workqueue_struct *wq = NULL;
Don Brace6636e7f2015-01-23 16:45:17 -06008613
Don Brace397ea9c2015-02-06 17:44:15 -06008614 wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
Don Brace6636e7f2015-01-23 16:45:17 -06008615 if (!wq)
8616 dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
8617
8618 return wq;
8619}
8620
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008621static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008622{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008623 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008624 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008625 int try_soft_reset = 0;
8626 unsigned long flags;
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008627 u32 board_id;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008628
8629 if (number_of_controllers == 0)
8630 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008631
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008632 rc = hpsa_lookup_board_id(pdev, &board_id);
8633 if (rc < 0) {
8634 dev_warn(&pdev->dev, "Board ID not found\n");
8635 return rc;
8636 }
8637
8638 rc = hpsa_init_reset_devices(pdev, board_id);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008639 if (rc) {
8640 if (rc != -ENOTSUPP)
8641 return rc;
8642 /* If the reset fails in a particular way (it has no way to do
8643 * a proper hard reset, so returns -ENOTSUPP) we can try to do
8644 * a soft reset once we get the controller configured up to the
8645 * point that it can accept a command.
8646 */
8647 try_soft_reset = 1;
8648 rc = 0;
8649 }
8650
8651reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008652
Don Brace303932f2010-02-04 08:42:40 -06008653 /* Command structures must be aligned on a 32-byte boundary because
8654 * the 5 lower bits of the address are used by the hardware. and by
8655 * the driver. See comments in hpsa.h for more info.
8656 */
Don Brace303932f2010-02-04 08:42:40 -06008657 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008658 h = kzalloc(sizeof(*h), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05008659 if (!h) {
8660 dev_err(&pdev->dev, "Failed to allocate controller head\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008661 return -ENOMEM;
Robert Elliott105a3db2015-04-23 09:33:48 -05008662 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008663
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008664 h->pdev = pdev;
Robert Elliott105a3db2015-04-23 09:33:48 -05008665
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06008666 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008667 INIT_LIST_HEAD(&h->offline_device_list);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008668 spin_lock_init(&h->lock);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008669 spin_lock_init(&h->offline_device_lock);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008670 spin_lock_init(&h->scan_lock);
Don Brace34f0c622015-01-23 16:43:46 -06008671 atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05008672 atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008673
8674 /* Allocate and clear per-cpu variable lockup_detected */
8675 h->lockup_detected = alloc_percpu(u32);
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008676 if (!h->lockup_detected) {
Robert Elliott105a3db2015-04-23 09:33:48 -05008677 dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008678 rc = -ENOMEM;
Robert Elliott2efa5922015-04-23 09:34:53 -05008679 goto clean1; /* aer/h */
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008680 }
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008681 set_lockup_detected_for_all_cpus(h, 0);
8682
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008683 rc = hpsa_pci_init(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05008684 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008685 goto clean2; /* lu, aer/h */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008686
Robert Elliott2946e822015-04-23 09:35:09 -05008687 /* relies on h-> settings made by hpsa_pci_init, including
8688 * interrupt_mode h->intr */
8689 rc = hpsa_scsi_host_alloc(h);
8690 if (rc)
8691 goto clean2_5; /* pci, lu, aer/h */
8692
8693 sprintf(h->devname, HPSA "%d", h->scsi_host->host_no);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008694 h->ctlr = number_of_controllers;
8695 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008696
8697 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008698 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
8699 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008700 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008701 } else {
8702 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8703 if (rc == 0) {
8704 dac = 0;
8705 } else {
8706 dev_err(&pdev->dev, "no suitable DMA available\n");
Robert Elliott2946e822015-04-23 09:35:09 -05008707 goto clean3; /* shost, pci, lu, aer/h */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008708 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008709 }
8710
8711 /* make sure the board interrupts are off */
8712 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05008713
Robert Elliott105a3db2015-04-23 09:33:48 -05008714 rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
8715 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008716 goto clean3; /* shost, pci, lu, aer/h */
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008717 rc = hpsa_alloc_cmd_pool(h);
Robert Elliott8947fd12015-01-23 16:42:54 -06008718 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008719 goto clean4; /* irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008720 rc = hpsa_alloc_sg_chain_blocks(h);
8721 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008722 goto clean5; /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Camerona08a84712010-02-04 08:43:16 -06008723 init_waitqueue_head(&h->scan_wait_queue);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05008724 init_waitqueue_head(&h->abort_cmd_wait_queue);
Webb Scalesd604f532015-04-23 09:35:22 -05008725 init_waitqueue_head(&h->event_sync_wait_queue);
8726 mutex_init(&h->reset_mutex);
Stephen M. Camerona08a84712010-02-04 08:43:16 -06008727 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008728
8729 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008730 h->ndevices = 0;
Robert Elliott2946e822015-04-23 09:35:09 -05008731
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008732 spin_lock_init(&h->devlock);
Robert Elliott105a3db2015-04-23 09:33:48 -05008733 rc = hpsa_put_ctlr_into_performant_mode(h);
8734 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008735 goto clean6; /* sg, cmd, irq, shost, pci, lu, aer/h */
8736
Robert Elliott2efa5922015-04-23 09:34:53 -05008737 /* create the resubmit workqueue */
8738 h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
8739 if (!h->rescan_ctlr_wq) {
8740 rc = -ENOMEM;
8741 goto clean7;
8742 }
8743
8744 h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
8745 if (!h->resubmit_wq) {
8746 rc = -ENOMEM;
8747 goto clean7; /* aer/h */
8748 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008749
Robert Elliott105a3db2015-04-23 09:33:48 -05008750 /*
8751 * At this point, the controller is ready to take commands.
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008752 * Now, if reset_devices and the hard reset didn't work, try
8753 * the soft reset and see if that works.
8754 */
8755 if (try_soft_reset) {
8756
8757 /* This is kind of gross. We may or may not get a completion
8758 * from the soft reset command, and if we do, then the value
8759 * from the fifo may or may not be valid. So, we wait 10 secs
8760 * after the reset throwing away any completions we get during
8761 * that time. Unregister the interrupt handler and register
8762 * fake ones to scoop up any residual completions.
8763 */
8764 spin_lock_irqsave(&h->lock, flags);
8765 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8766 spin_unlock_irqrestore(&h->lock, flags);
Robert Elliottec501a12015-01-23 16:41:40 -06008767 hpsa_free_irqs(h);
Robert Elliott9ee61792015-01-23 16:42:32 -06008768 rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008769 hpsa_intx_discard_completions);
8770 if (rc) {
Robert Elliott9ee61792015-01-23 16:42:32 -06008771 dev_warn(&h->pdev->dev,
8772 "Failed to request_irq after soft reset.\n");
Robert Elliottd4987572015-04-23 09:34:37 -05008773 /*
Robert Elliottb2ef4802015-04-23 09:34:48 -05008774 * cannot goto clean7 or free_irqs will be called
8775 * again. Instead, do its work
8776 */
8777 hpsa_free_performant_mode(h); /* clean7 */
8778 hpsa_free_sg_chain_blocks(h); /* clean6 */
8779 hpsa_free_cmd_pool(h); /* clean5 */
8780 /*
8781 * skip hpsa_free_irqs(h) clean4 since that
8782 * was just called before request_irqs failed
Robert Elliottd4987572015-04-23 09:34:37 -05008783 */
8784 goto clean3;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008785 }
8786
8787 rc = hpsa_kdump_soft_reset(h);
8788 if (rc)
8789 /* Neither hard nor soft reset worked, we're hosed. */
Don Brace7ef73232015-07-18 11:12:33 -05008790 goto clean7;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008791
8792 dev_info(&h->pdev->dev, "Board READY.\n");
8793 dev_info(&h->pdev->dev,
8794 "Waiting for stale completions to drain.\n");
8795 h->access.set_intr_mask(h, HPSA_INTR_ON);
8796 msleep(10000);
8797 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8798
8799 rc = controller_reset_failed(h->cfgtable);
8800 if (rc)
8801 dev_info(&h->pdev->dev,
8802 "Soft reset appears to have failed.\n");
8803
8804 /* since the controller's reset, we have to go back and re-init
8805 * everything. Easiest to just forget what we've done and do it
8806 * all over again.
8807 */
8808 hpsa_undo_allocations_after_kdump_soft_reset(h);
8809 try_soft_reset = 0;
8810 if (rc)
Robert Elliottb2ef4802015-04-23 09:34:48 -05008811 /* don't goto clean, we already unallocated */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008812 return -ENODEV;
8813
8814 goto reinit_after_soft_reset;
8815 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008816
Robert Elliott105a3db2015-04-23 09:33:48 -05008817 /* Enable Accelerated IO path at driver layer */
8818 h->acciopath_status = 1;
Scott Teel34592252015-11-04 15:52:09 -06008819 /* Disable discovery polling.*/
8820 h->discovery_polling = 0;
Scott Teelda0697b2014-02-18 13:57:00 -06008821
Scott Teele863d682014-02-18 13:57:05 -06008822
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008823 /* Turn the interrupts on so we can service requests */
8824 h->access.set_intr_mask(h, HPSA_INTR_ON);
8825
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06008826 hpsa_hba_inquiry(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008827
Scott Teel34592252015-11-04 15:52:09 -06008828 h->lastlogicals = kzalloc(sizeof(*(h->lastlogicals)), GFP_KERNEL);
8829 if (!h->lastlogicals)
8830 dev_info(&h->pdev->dev,
8831 "Can't track change to report lun data\n");
8832
Don Bracecf477232016-04-27 17:13:26 -05008833 /* hook into SCSI subsystem */
8834 rc = hpsa_scsi_add_host(h);
8835 if (rc)
8836 goto clean7; /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
8837
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008838 /* Monitor the controller for firmware lockups */
8839 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
8840 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
8841 schedule_delayed_work(&h->monitor_ctlr_work,
8842 h->heartbeat_sample_interval);
Don Brace6636e7f2015-01-23 16:45:17 -06008843 INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
8844 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
8845 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05008846 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008847
Robert Elliott2946e822015-04-23 09:35:09 -05008848clean7: /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008849 hpsa_free_performant_mode(h);
8850 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8851clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06008852 hpsa_free_sg_chain_blocks(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008853clean5: /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008854 hpsa_free_cmd_pool(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008855clean4: /* irq, shost, pci, lu, aer/h */
Robert Elliottec501a12015-01-23 16:41:40 -06008856 hpsa_free_irqs(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008857clean3: /* shost, pci, lu, aer/h */
8858 scsi_host_put(h->scsi_host);
8859 h->scsi_host = NULL;
8860clean2_5: /* pci, lu, aer/h */
Robert Elliott195f2c62015-04-23 09:33:17 -05008861 hpsa_free_pci_init(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008862clean2: /* lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008863 if (h->lockup_detected) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008864 free_percpu(h->lockup_detected);
Robert Elliott105a3db2015-04-23 09:33:48 -05008865 h->lockup_detected = NULL;
8866 }
8867clean1: /* wq/aer/h */
8868 if (h->resubmit_wq) {
8869 destroy_workqueue(h->resubmit_wq);
8870 h->resubmit_wq = NULL;
8871 }
8872 if (h->rescan_ctlr_wq) {
8873 destroy_workqueue(h->rescan_ctlr_wq);
8874 h->rescan_ctlr_wq = NULL;
8875 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008876 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008877 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008878}
8879
8880static void hpsa_flush_cache(struct ctlr_info *h)
8881{
8882 char *flush_buf;
8883 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05008884 int rc;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008885
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008886 if (unlikely(lockup_detected(h)))
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008887 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008888 flush_buf = kzalloc(4, GFP_KERNEL);
8889 if (!flush_buf)
8890 return;
8891
Stephen Cameron45fcb862015-01-23 16:43:04 -06008892 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05008893
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008894 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
8895 RAID_CTLR_LUNID, TYPE_CMD)) {
8896 goto out;
8897 }
Webb Scales25163bd2015-04-23 09:32:00 -05008898 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008899 PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05008900 if (rc)
8901 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008902 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008903out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008904 dev_warn(&h->pdev->dev,
8905 "error flushing cache on controller\n");
Stephen Cameron45fcb862015-01-23 16:43:04 -06008906 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008907 kfree(flush_buf);
8908}
8909
Scott Teelc2adae42015-11-04 15:52:16 -06008910/* Make controller gather fresh report lun data each time we
8911 * send down a report luns request
8912 */
8913static void hpsa_disable_rld_caching(struct ctlr_info *h)
8914{
8915 u32 *options;
8916 struct CommandList *c;
8917 int rc;
8918
8919 /* Don't bother trying to set diag options if locked up */
8920 if (unlikely(h->lockup_detected))
8921 return;
8922
8923 options = kzalloc(sizeof(*options), GFP_KERNEL);
8924 if (!options) {
8925 dev_err(&h->pdev->dev,
8926 "Error: failed to disable rld caching, during alloc.\n");
8927 return;
8928 }
8929
8930 c = cmd_alloc(h);
8931
8932 /* first, get the current diag options settings */
8933 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
8934 RAID_CTLR_LUNID, TYPE_CMD))
8935 goto errout;
8936
8937 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008938 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06008939 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8940 goto errout;
8941
8942 /* Now, set the bit for disabling the RLD caching */
8943 *options |= HPSA_DIAG_OPTS_DISABLE_RLD_CACHING;
8944
8945 if (fill_cmd(c, BMIC_SET_DIAG_OPTIONS, h, options, 4, 0,
8946 RAID_CTLR_LUNID, TYPE_CMD))
8947 goto errout;
8948
8949 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008950 PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06008951 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8952 goto errout;
8953
8954 /* Now verify that it got set: */
8955 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
8956 RAID_CTLR_LUNID, TYPE_CMD))
8957 goto errout;
8958
8959 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008960 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06008961 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8962 goto errout;
8963
Dan Carpenterd8a080c2015-11-12 12:43:38 +03008964 if (*options & HPSA_DIAG_OPTS_DISABLE_RLD_CACHING)
Scott Teelc2adae42015-11-04 15:52:16 -06008965 goto out;
8966
8967errout:
8968 dev_err(&h->pdev->dev,
8969 "Error: failed to disable report lun data caching.\n");
8970out:
8971 cmd_free(h, c);
8972 kfree(options);
8973}
8974
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008975static void hpsa_shutdown(struct pci_dev *pdev)
8976{
8977 struct ctlr_info *h;
8978
8979 h = pci_get_drvdata(pdev);
8980 /* Turn board interrupts off and send the flush cache command
8981 * sendcmd will turn off interrupt, and send the flush...
8982 * To write all data in the battery backed cache to disks
8983 */
8984 hpsa_flush_cache(h);
8985 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Robert Elliott105a3db2015-04-23 09:33:48 -05008986 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliottcc64c812015-04-23 09:33:12 -05008987 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008988}
8989
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008990static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008991{
8992 int i;
8993
Robert Elliott105a3db2015-04-23 09:33:48 -05008994 for (i = 0; i < h->ndevices; i++) {
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008995 kfree(h->dev[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008996 h->dev[i] = NULL;
8997 }
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008998}
8999
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009000static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009001{
9002 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06009003 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009004
9005 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05009006 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009007 return;
9008 }
9009 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06009010
9011 /* Get rid of any controller monitoring work items */
9012 spin_lock_irqsave(&h->lock, flags);
9013 h->remove_in_progress = 1;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06009014 spin_unlock_irqrestore(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06009015 cancel_delayed_work_sync(&h->monitor_ctlr_work);
9016 cancel_delayed_work_sync(&h->rescan_ctlr_work);
9017 destroy_workqueue(h->rescan_ctlr_wq);
9018 destroy_workqueue(h->resubmit_wq);
Robert Elliottcc64c812015-04-23 09:33:12 -05009019
Don Brace2d041302015-07-18 11:13:15 -05009020 /*
9021 * Call before disabling interrupts.
9022 * scsi_remove_host can trigger I/O operations especially
9023 * when multipath is enabled. There can be SYNCHRONIZE CACHE
9024 * operations which cannot complete and will hang the system.
9025 */
9026 if (h->scsi_host)
9027 scsi_remove_host(h->scsi_host); /* init_one 8 */
Robert Elliott105a3db2015-04-23 09:33:48 -05009028 /* includes hpsa_free_irqs - init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05009029 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009030 hpsa_shutdown(pdev);
Robert Elliottcc64c812015-04-23 09:33:12 -05009031
Robert Elliott105a3db2015-04-23 09:33:48 -05009032 hpsa_free_device_info(h); /* scan */
9033
Robert Elliott2946e822015-04-23 09:35:09 -05009034 kfree(h->hba_inquiry_data); /* init_one 10 */
9035 h->hba_inquiry_data = NULL; /* init_one 10 */
Robert Elliott2946e822015-04-23 09:35:09 -05009036 hpsa_free_ioaccel2_sg_chain_blocks(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05009037 hpsa_free_performant_mode(h); /* init_one 7 */
9038 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
9039 hpsa_free_cmd_pool(h); /* init_one 5 */
Scott Teel34592252015-11-04 15:52:09 -06009040 kfree(h->lastlogicals);
Robert Elliott105a3db2015-04-23 09:33:48 -05009041
9042 /* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05009043
Robert Elliott2946e822015-04-23 09:35:09 -05009044 scsi_host_put(h->scsi_host); /* init_one 3 */
9045 h->scsi_host = NULL; /* init_one 3 */
9046
Robert Elliott195f2c62015-04-23 09:33:17 -05009047 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Robert Elliott2946e822015-04-23 09:35:09 -05009048 hpsa_free_pci_init(h); /* init_one 2.5 */
Robert Elliott195f2c62015-04-23 09:33:17 -05009049
Robert Elliott105a3db2015-04-23 09:33:48 -05009050 free_percpu(h->lockup_detected); /* init_one 2 */
9051 h->lockup_detected = NULL; /* init_one 2 */
9052 /* (void) pci_disable_pcie_error_reporting(pdev); */ /* init_one 1 */
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009053
9054 hpsa_delete_sas_host(h);
9055
Robert Elliott105a3db2015-04-23 09:33:48 -05009056 kfree(h); /* init_one 1 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009057}
9058
9059static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
9060 __attribute__((unused)) pm_message_t state)
9061{
9062 return -ENOSYS;
9063}
9064
9065static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
9066{
9067 return -ENOSYS;
9068}
9069
9070static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06009071 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009072 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009073 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009074 .id_table = hpsa_pci_device_id, /* id_table */
9075 .shutdown = hpsa_shutdown,
9076 .suspend = hpsa_suspend,
9077 .resume = hpsa_resume,
9078};
9079
Don Brace303932f2010-02-04 08:42:40 -06009080/* Fill in bucket_map[], given nsgs (the max number of
9081 * scatter gather elements supported) and bucket[],
9082 * which is an array of 8 integers. The bucket[] array
9083 * contains 8 different DMA transfer sizes (in 16
9084 * byte increments) which the controller uses to fetch
9085 * commands. This function fills in bucket_map[], which
9086 * maps a given number of scatter gather elements to one of
9087 * the 8 DMA transfer sizes. The point of it is to allow the
9088 * controller to only do as much DMA as needed to fetch the
9089 * command, with the DMA transfer size encoded in the lower
9090 * bits of the command address.
9091 */
9092static void calc_bucket_map(int bucket[], int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -06009093 int nsgs, int min_blocks, u32 *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06009094{
9095 int i, j, b, size;
9096
Don Brace303932f2010-02-04 08:42:40 -06009097 /* Note, bucket_map must have nsgs+1 entries. */
9098 for (i = 0; i <= nsgs; i++) {
9099 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06009100 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06009101 b = num_buckets; /* Assume the biggest bucket */
9102 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06009103 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06009104 if (bucket[j] >= size) {
9105 b = j;
9106 break;
9107 }
9108 }
9109 /* for a command with i SG entries, use bucket b. */
9110 bucket_map[i] = b;
9111 }
9112}
9113
Robert Elliott105a3db2015-04-23 09:33:48 -05009114/*
9115 * return -ENODEV on err, 0 on success (or no action)
9116 * allocates numerous items that must be freed later
9117 */
Robert Elliottc706a792015-01-23 16:45:01 -06009118static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06009119{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009120 int i;
9121 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06009122 unsigned long transMethod = CFGTBL_Trans_Performant |
9123 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009124 CFGTBL_Trans_enable_directed_msix |
9125 (trans_support & (CFGTBL_Trans_io_accel1 |
9126 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06009127 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05009128
9129 /* This is a bit complicated. There are 8 registers on
9130 * the controller which we write to to tell it 8 different
9131 * sizes of commands which there may be. It's a way of
9132 * reducing the DMA done to fetch each command. Encoded into
9133 * each command's tag are 3 bits which communicate to the controller
9134 * which of the eight sizes that command fits within. The size of
9135 * each command depends on how many scatter gather entries there are.
9136 * Each SG entry requires 16 bytes. The eight registers are programmed
9137 * with the number of 16-byte blocks a command of that size requires.
9138 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009139 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05009140 * blocks. Note, this only extends to the SG entries contained
9141 * within the command block, and does not extend to chained blocks
9142 * of SG elements. bft[] contains the eight values we write to
9143 * the registers. They are not evenly distributed, but have more
9144 * sizes for small commands, and fewer sizes for larger commands.
9145 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009146 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009147#define MIN_IOACCEL2_BFT_ENTRY 5
9148#define HPSA_IOACCEL2_HEADER_SZ 4
9149 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
9150 13, 14, 15, 16, 17, 18, 19,
9151 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
9152 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
9153 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
9154 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
9155 16 * MIN_IOACCEL2_BFT_ENTRY);
9156 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009157 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06009158 /* 5 = 1 s/g entry or 4k
9159 * 6 = 2 s/g entry or 8k
9160 * 8 = 4 s/g entry or 16k
9161 * 10 = 6 s/g entry or 24k
9162 */
Don Brace303932f2010-02-04 08:42:40 -06009163
Stephen M. Cameronb3a52e72014-05-29 10:53:23 -05009164 /* If the controller supports either ioaccel method then
9165 * we can also use the RAID stack submit path that does not
9166 * perform the superfluous readl() after each command submission.
9167 */
9168 if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
9169 access = SA5_performant_access_no_read;
9170
Don Brace303932f2010-02-04 08:42:40 -06009171 /* Controller spec: zero out this buffer. */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009172 for (i = 0; i < h->nreply_queues; i++)
9173 memset(h->reply_queue[i].head, 0, h->reply_queue_size);
Don Brace303932f2010-02-04 08:42:40 -06009174
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009175 bft[7] = SG_ENTRIES_IN_CMD + 4;
9176 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06009177 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06009178 for (i = 0; i < 8; i++)
9179 writel(bft[i], &h->transtable->BlockFetch[i]);
9180
9181 /* size of controller ring buffer */
9182 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05009183 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06009184 writel(0, &h->transtable->RepQCtrAddrLow32);
9185 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05009186
9187 for (i = 0; i < h->nreply_queues; i++) {
9188 writel(0, &h->transtable->RepQAddr[i].upper);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009189 writel(h->reply_queue[i].busaddr,
Matt Gates254f7962012-05-01 11:43:06 -05009190 &h->transtable->RepQAddr[i].lower);
9191 }
9192
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009193 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06009194 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
9195 /*
9196 * enable outbound interrupt coalescing in accelerator mode;
9197 */
9198 if (trans_support & CFGTBL_Trans_io_accel1) {
9199 access = SA5_ioaccel_mode1_access;
9200 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
9201 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Scott Teelc3497752014-02-18 13:56:34 -06009202 } else {
9203 if (trans_support & CFGTBL_Trans_io_accel2) {
9204 access = SA5_ioaccel_mode2_access;
9205 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
9206 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
9207 }
Matt Gatese1f7de02014-02-18 13:55:17 -06009208 }
Don Brace303932f2010-02-04 08:42:40 -06009209 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06009210 if (hpsa_wait_for_mode_change_ack(h)) {
9211 dev_err(&h->pdev->dev,
9212 "performant mode problem - doorbell timeout\n");
9213 return -ENODEV;
9214 }
Don Brace303932f2010-02-04 08:42:40 -06009215 register_value = readl(&(h->cfgtable->TransportActive));
9216 if (!(register_value & CFGTBL_Trans_Performant)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06009217 dev_err(&h->pdev->dev,
9218 "performant mode problem - transport not active\n");
Robert Elliottc706a792015-01-23 16:45:01 -06009219 return -ENODEV;
Don Brace303932f2010-02-04 08:42:40 -06009220 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06009221 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06009222 h->access = access;
9223 h->transMethod = transMethod;
9224
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009225 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
9226 (trans_support & CFGTBL_Trans_io_accel2)))
Robert Elliottc706a792015-01-23 16:45:01 -06009227 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06009228
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009229 if (trans_support & CFGTBL_Trans_io_accel1) {
9230 /* Set up I/O accelerator mode */
9231 for (i = 0; i < h->nreply_queues; i++) {
9232 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
9233 h->reply_queue[i].current_entry =
9234 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
9235 }
9236 bft[7] = h->ioaccel_maxsg + 8;
9237 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
9238 h->ioaccel1_blockFetchTable);
9239
9240 /* initialize all reply queue entries to unused */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009241 for (i = 0; i < h->nreply_queues; i++)
9242 memset(h->reply_queue[i].head,
9243 (u8) IOACCEL_MODE1_REPLY_UNUSED,
9244 h->reply_queue_size);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009245
9246 /* set all the constant fields in the accelerator command
9247 * frames once at init time to save CPU cycles later.
9248 */
9249 for (i = 0; i < h->nr_cmds; i++) {
9250 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
9251
9252 cp->function = IOACCEL1_FUNCTION_SCSIIO;
9253 cp->err_info = (u32) (h->errinfo_pool_dhandle +
9254 (i * sizeof(struct ErrorInfo)));
9255 cp->err_info_len = sizeof(struct ErrorInfo);
9256 cp->sgl_offset = IOACCEL1_SGLOFFSET;
Don Brace2b08b3e2015-01-23 16:41:09 -06009257 cp->host_context_flags =
9258 cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009259 cp->timeout_sec = 0;
9260 cp->ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009261 cp->tag =
Don Bracef2405db2015-01-23 16:43:09 -06009262 cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009263 cp->host_addr =
9264 cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009265 (i * sizeof(struct io_accel1_cmd)));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009266 }
9267 } else if (trans_support & CFGTBL_Trans_io_accel2) {
9268 u64 cfg_offset, cfg_base_addr_index;
9269 u32 bft2_offset, cfg_base_addr;
9270 int rc;
9271
9272 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
9273 &cfg_base_addr_index, &cfg_offset);
9274 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
9275 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
9276 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
9277 4, h->ioaccel2_blockFetchTable);
9278 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
9279 BUILD_BUG_ON(offsetof(struct CfgTable,
9280 io_accel_request_size_offset) != 0xb8);
9281 h->ioaccel2_bft2_regs =
9282 remap_pci_mem(pci_resource_start(h->pdev,
9283 cfg_base_addr_index) +
9284 cfg_offset + bft2_offset,
9285 ARRAY_SIZE(bft2) *
9286 sizeof(*h->ioaccel2_bft2_regs));
9287 for (i = 0; i < ARRAY_SIZE(bft2); i++)
9288 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06009289 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009290 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06009291 if (hpsa_wait_for_mode_change_ack(h)) {
9292 dev_err(&h->pdev->dev,
9293 "performant mode problem - enabling ioaccel mode\n");
9294 return -ENODEV;
9295 }
9296 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06009297}
9298
Robert Elliott1fb7c982015-04-23 09:33:22 -05009299/* Free ioaccel1 mode command blocks and block fetch table */
9300static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
9301{
Robert Elliott105a3db2015-04-23 09:33:48 -05009302 if (h->ioaccel_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05009303 pci_free_consistent(h->pdev,
9304 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
9305 h->ioaccel_cmd_pool,
9306 h->ioaccel_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05009307 h->ioaccel_cmd_pool = NULL;
9308 h->ioaccel_cmd_pool_dhandle = 0;
9309 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05009310 kfree(h->ioaccel1_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009311 h->ioaccel1_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05009312}
9313
Robert Elliottd37ffbe2015-04-23 09:32:27 -05009314/* Allocate ioaccel1 mode command blocks and block fetch table */
9315static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
Matt Gatese1f7de02014-02-18 13:55:17 -06009316{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06009317 h->ioaccel_maxsg =
9318 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
9319 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
9320 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
9321
Matt Gatese1f7de02014-02-18 13:55:17 -06009322 /* Command structures must be aligned on a 128-byte boundary
9323 * because the 7 lower bits of the address are used by the
9324 * hardware.
9325 */
Matt Gatese1f7de02014-02-18 13:55:17 -06009326 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
9327 IOACCEL1_COMMANDLIST_ALIGNMENT);
9328 h->ioaccel_cmd_pool =
9329 pci_alloc_consistent(h->pdev,
9330 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
9331 &(h->ioaccel_cmd_pool_dhandle));
9332
9333 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06009334 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06009335 sizeof(u32)), GFP_KERNEL);
9336
9337 if ((h->ioaccel_cmd_pool == NULL) ||
9338 (h->ioaccel1_blockFetchTable == NULL))
9339 goto clean_up;
9340
9341 memset(h->ioaccel_cmd_pool, 0,
9342 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
9343 return 0;
9344
9345clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05009346 hpsa_free_ioaccel1_cmd_and_bft(h);
Robert Elliott2dd02d72015-04-23 09:33:43 -05009347 return -ENOMEM;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009348}
9349
Robert Elliott1fb7c982015-04-23 09:33:22 -05009350/* Free ioaccel2 mode command blocks and block fetch table */
9351static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
9352{
Webb Scalesd9a729f2015-04-23 09:33:27 -05009353 hpsa_free_ioaccel2_sg_chain_blocks(h);
9354
Robert Elliott105a3db2015-04-23 09:33:48 -05009355 if (h->ioaccel2_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05009356 pci_free_consistent(h->pdev,
9357 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
9358 h->ioaccel2_cmd_pool,
9359 h->ioaccel2_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05009360 h->ioaccel2_cmd_pool = NULL;
9361 h->ioaccel2_cmd_pool_dhandle = 0;
9362 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05009363 kfree(h->ioaccel2_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009364 h->ioaccel2_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05009365}
9366
Robert Elliottd37ffbe2015-04-23 09:32:27 -05009367/* Allocate ioaccel2 mode command blocks and block fetch table */
9368static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009369{
Webb Scalesd9a729f2015-04-23 09:33:27 -05009370 int rc;
9371
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009372 /* Allocate ioaccel2 mode command blocks and block fetch table */
9373
9374 h->ioaccel_maxsg =
9375 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
9376 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
9377 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
9378
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009379 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
9380 IOACCEL2_COMMANDLIST_ALIGNMENT);
9381 h->ioaccel2_cmd_pool =
9382 pci_alloc_consistent(h->pdev,
9383 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
9384 &(h->ioaccel2_cmd_pool_dhandle));
9385
9386 h->ioaccel2_blockFetchTable =
9387 kmalloc(((h->ioaccel_maxsg + 1) *
9388 sizeof(u32)), GFP_KERNEL);
9389
9390 if ((h->ioaccel2_cmd_pool == NULL) ||
Webb Scalesd9a729f2015-04-23 09:33:27 -05009391 (h->ioaccel2_blockFetchTable == NULL)) {
9392 rc = -ENOMEM;
9393 goto clean_up;
9394 }
9395
9396 rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
9397 if (rc)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009398 goto clean_up;
9399
9400 memset(h->ioaccel2_cmd_pool, 0,
9401 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
9402 return 0;
9403
9404clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05009405 hpsa_free_ioaccel2_cmd_and_bft(h);
Webb Scalesd9a729f2015-04-23 09:33:27 -05009406 return rc;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009407}
9408
Robert Elliott105a3db2015-04-23 09:33:48 -05009409/* Free items allocated by hpsa_put_ctlr_into_performant_mode */
9410static void hpsa_free_performant_mode(struct ctlr_info *h)
9411{
9412 kfree(h->blockFetchTable);
9413 h->blockFetchTable = NULL;
9414 hpsa_free_reply_queues(h);
9415 hpsa_free_ioaccel1_cmd_and_bft(h);
9416 hpsa_free_ioaccel2_cmd_and_bft(h);
9417}
9418
9419/* return -ENODEV on error, 0 on success (or no action)
9420 * allocates numerous items that must be freed later
9421 */
9422static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009423{
9424 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06009425 unsigned long transMethod = CFGTBL_Trans_Performant |
9426 CFGTBL_Trans_use_short_tags;
Robert Elliott105a3db2015-04-23 09:33:48 -05009427 int i, rc;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009428
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06009429 if (hpsa_simple_mode)
Robert Elliott105a3db2015-04-23 09:33:48 -05009430 return 0;
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06009431
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05009432 trans_support = readl(&(h->cfgtable->TransportSupport));
9433 if (!(trans_support & PERFORMANT_MODE))
Robert Elliott105a3db2015-04-23 09:33:48 -05009434 return 0;
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05009435
Matt Gatese1f7de02014-02-18 13:55:17 -06009436 /* Check for I/O accelerator mode support */
9437 if (trans_support & CFGTBL_Trans_io_accel1) {
9438 transMethod |= CFGTBL_Trans_io_accel1 |
9439 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05009440 rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
9441 if (rc)
9442 return rc;
9443 } else if (trans_support & CFGTBL_Trans_io_accel2) {
9444 transMethod |= CFGTBL_Trans_io_accel2 |
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009445 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05009446 rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
9447 if (rc)
9448 return rc;
Matt Gatese1f7de02014-02-18 13:55:17 -06009449 }
9450
Hannes Reineckeeee0f032014-01-15 13:30:53 +01009451 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05009452 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009453 /* Performant mode ring buffer and supporting data structures */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009454 h->reply_queue_size = h->max_commands * sizeof(u64);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009455
Matt Gates254f7962012-05-01 11:43:06 -05009456 for (i = 0; i < h->nreply_queues; i++) {
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009457 h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
9458 h->reply_queue_size,
9459 &(h->reply_queue[i].busaddr));
Robert Elliott105a3db2015-04-23 09:33:48 -05009460 if (!h->reply_queue[i].head) {
9461 rc = -ENOMEM;
9462 goto clean1; /* rq, ioaccel */
9463 }
Matt Gates254f7962012-05-01 11:43:06 -05009464 h->reply_queue[i].size = h->max_commands;
9465 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
9466 h->reply_queue[i].current_entry = 0;
9467 }
9468
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009469 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009470 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009471 sizeof(u32)), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05009472 if (!h->blockFetchTable) {
9473 rc = -ENOMEM;
9474 goto clean1; /* rq, ioaccel */
9475 }
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009476
Robert Elliott105a3db2015-04-23 09:33:48 -05009477 rc = hpsa_enter_performant_mode(h, trans_support);
9478 if (rc)
9479 goto clean2; /* bft, rq, ioaccel */
9480 return 0;
Don Brace303932f2010-02-04 08:42:40 -06009481
Robert Elliott105a3db2015-04-23 09:33:48 -05009482clean2: /* bft, rq, ioaccel */
Don Brace303932f2010-02-04 08:42:40 -06009483 kfree(h->blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009484 h->blockFetchTable = NULL;
9485clean1: /* rq, ioaccel */
9486 hpsa_free_reply_queues(h);
9487 hpsa_free_ioaccel1_cmd_and_bft(h);
9488 hpsa_free_ioaccel2_cmd_and_bft(h);
9489 return rc;
Don Brace303932f2010-02-04 08:42:40 -06009490}
9491
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009492static int is_accelerated_cmd(struct CommandList *c)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009493{
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009494 return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
9495}
9496
9497static void hpsa_drain_accel_commands(struct ctlr_info *h)
9498{
9499 struct CommandList *c = NULL;
Don Bracef2405db2015-01-23 16:43:09 -06009500 int i, accel_cmds_out;
Webb Scales281a7fd2015-01-23 16:43:35 -06009501 int refcount;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009502
Don Bracef2405db2015-01-23 16:43:09 -06009503 do { /* wait for all outstanding ioaccel commands to drain out */
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009504 accel_cmds_out = 0;
Don Bracef2405db2015-01-23 16:43:09 -06009505 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06009506 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06009507 refcount = atomic_inc_return(&c->refcount);
9508 if (refcount > 1) /* Command is allocated */
9509 accel_cmds_out += is_accelerated_cmd(c);
9510 cmd_free(h, c);
Don Bracef2405db2015-01-23 16:43:09 -06009511 }
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009512 if (accel_cmds_out <= 0)
Webb Scales281a7fd2015-01-23 16:43:35 -06009513 break;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009514 msleep(100);
9515 } while (1);
9516}
9517
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009518static struct hpsa_sas_phy *hpsa_alloc_sas_phy(
9519 struct hpsa_sas_port *hpsa_sas_port)
9520{
9521 struct hpsa_sas_phy *hpsa_sas_phy;
9522 struct sas_phy *phy;
9523
9524 hpsa_sas_phy = kzalloc(sizeof(*hpsa_sas_phy), GFP_KERNEL);
9525 if (!hpsa_sas_phy)
9526 return NULL;
9527
9528 phy = sas_phy_alloc(hpsa_sas_port->parent_node->parent_dev,
9529 hpsa_sas_port->next_phy_index);
9530 if (!phy) {
9531 kfree(hpsa_sas_phy);
9532 return NULL;
9533 }
9534
9535 hpsa_sas_port->next_phy_index++;
9536 hpsa_sas_phy->phy = phy;
9537 hpsa_sas_phy->parent_port = hpsa_sas_port;
9538
9539 return hpsa_sas_phy;
9540}
9541
9542static void hpsa_free_sas_phy(struct hpsa_sas_phy *hpsa_sas_phy)
9543{
9544 struct sas_phy *phy = hpsa_sas_phy->phy;
9545
9546 sas_port_delete_phy(hpsa_sas_phy->parent_port->port, phy);
9547 sas_phy_free(phy);
9548 if (hpsa_sas_phy->added_to_port)
9549 list_del(&hpsa_sas_phy->phy_list_entry);
9550 kfree(hpsa_sas_phy);
9551}
9552
9553static int hpsa_sas_port_add_phy(struct hpsa_sas_phy *hpsa_sas_phy)
9554{
9555 int rc;
9556 struct hpsa_sas_port *hpsa_sas_port;
9557 struct sas_phy *phy;
9558 struct sas_identify *identify;
9559
9560 hpsa_sas_port = hpsa_sas_phy->parent_port;
9561 phy = hpsa_sas_phy->phy;
9562
9563 identify = &phy->identify;
9564 memset(identify, 0, sizeof(*identify));
9565 identify->sas_address = hpsa_sas_port->sas_address;
9566 identify->device_type = SAS_END_DEVICE;
9567 identify->initiator_port_protocols = SAS_PROTOCOL_STP;
9568 identify->target_port_protocols = SAS_PROTOCOL_STP;
9569 phy->minimum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
9570 phy->maximum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
9571 phy->minimum_linkrate = SAS_LINK_RATE_UNKNOWN;
9572 phy->maximum_linkrate = SAS_LINK_RATE_UNKNOWN;
9573 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
9574
9575 rc = sas_phy_add(hpsa_sas_phy->phy);
9576 if (rc)
9577 return rc;
9578
9579 sas_port_add_phy(hpsa_sas_port->port, hpsa_sas_phy->phy);
9580 list_add_tail(&hpsa_sas_phy->phy_list_entry,
9581 &hpsa_sas_port->phy_list_head);
9582 hpsa_sas_phy->added_to_port = true;
9583
9584 return 0;
9585}
9586
9587static int
9588 hpsa_sas_port_add_rphy(struct hpsa_sas_port *hpsa_sas_port,
9589 struct sas_rphy *rphy)
9590{
9591 struct sas_identify *identify;
9592
9593 identify = &rphy->identify;
9594 identify->sas_address = hpsa_sas_port->sas_address;
9595 identify->initiator_port_protocols = SAS_PROTOCOL_STP;
9596 identify->target_port_protocols = SAS_PROTOCOL_STP;
9597
9598 return sas_rphy_add(rphy);
9599}
9600
9601static struct hpsa_sas_port
9602 *hpsa_alloc_sas_port(struct hpsa_sas_node *hpsa_sas_node,
9603 u64 sas_address)
9604{
9605 int rc;
9606 struct hpsa_sas_port *hpsa_sas_port;
9607 struct sas_port *port;
9608
9609 hpsa_sas_port = kzalloc(sizeof(*hpsa_sas_port), GFP_KERNEL);
9610 if (!hpsa_sas_port)
9611 return NULL;
9612
9613 INIT_LIST_HEAD(&hpsa_sas_port->phy_list_head);
9614 hpsa_sas_port->parent_node = hpsa_sas_node;
9615
9616 port = sas_port_alloc_num(hpsa_sas_node->parent_dev);
9617 if (!port)
9618 goto free_hpsa_port;
9619
9620 rc = sas_port_add(port);
9621 if (rc)
9622 goto free_sas_port;
9623
9624 hpsa_sas_port->port = port;
9625 hpsa_sas_port->sas_address = sas_address;
9626 list_add_tail(&hpsa_sas_port->port_list_entry,
9627 &hpsa_sas_node->port_list_head);
9628
9629 return hpsa_sas_port;
9630
9631free_sas_port:
9632 sas_port_free(port);
9633free_hpsa_port:
9634 kfree(hpsa_sas_port);
9635
9636 return NULL;
9637}
9638
9639static void hpsa_free_sas_port(struct hpsa_sas_port *hpsa_sas_port)
9640{
9641 struct hpsa_sas_phy *hpsa_sas_phy;
9642 struct hpsa_sas_phy *next;
9643
9644 list_for_each_entry_safe(hpsa_sas_phy, next,
9645 &hpsa_sas_port->phy_list_head, phy_list_entry)
9646 hpsa_free_sas_phy(hpsa_sas_phy);
9647
9648 sas_port_delete(hpsa_sas_port->port);
9649 list_del(&hpsa_sas_port->port_list_entry);
9650 kfree(hpsa_sas_port);
9651}
9652
9653static struct hpsa_sas_node *hpsa_alloc_sas_node(struct device *parent_dev)
9654{
9655 struct hpsa_sas_node *hpsa_sas_node;
9656
9657 hpsa_sas_node = kzalloc(sizeof(*hpsa_sas_node), GFP_KERNEL);
9658 if (hpsa_sas_node) {
9659 hpsa_sas_node->parent_dev = parent_dev;
9660 INIT_LIST_HEAD(&hpsa_sas_node->port_list_head);
9661 }
9662
9663 return hpsa_sas_node;
9664}
9665
9666static void hpsa_free_sas_node(struct hpsa_sas_node *hpsa_sas_node)
9667{
9668 struct hpsa_sas_port *hpsa_sas_port;
9669 struct hpsa_sas_port *next;
9670
9671 if (!hpsa_sas_node)
9672 return;
9673
9674 list_for_each_entry_safe(hpsa_sas_port, next,
9675 &hpsa_sas_node->port_list_head, port_list_entry)
9676 hpsa_free_sas_port(hpsa_sas_port);
9677
9678 kfree(hpsa_sas_node);
9679}
9680
9681static struct hpsa_scsi_dev_t
9682 *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
9683 struct sas_rphy *rphy)
9684{
9685 int i;
9686 struct hpsa_scsi_dev_t *device;
9687
9688 for (i = 0; i < h->ndevices; i++) {
9689 device = h->dev[i];
9690 if (!device->sas_port)
9691 continue;
9692 if (device->sas_port->rphy == rphy)
9693 return device;
9694 }
9695
9696 return NULL;
9697}
9698
9699static int hpsa_add_sas_host(struct ctlr_info *h)
9700{
9701 int rc;
9702 struct device *parent_dev;
9703 struct hpsa_sas_node *hpsa_sas_node;
9704 struct hpsa_sas_port *hpsa_sas_port;
9705 struct hpsa_sas_phy *hpsa_sas_phy;
9706
9707 parent_dev = &h->scsi_host->shost_gendev;
9708
9709 hpsa_sas_node = hpsa_alloc_sas_node(parent_dev);
9710 if (!hpsa_sas_node)
9711 return -ENOMEM;
9712
9713 hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, h->sas_address);
9714 if (!hpsa_sas_port) {
9715 rc = -ENODEV;
9716 goto free_sas_node;
9717 }
9718
9719 hpsa_sas_phy = hpsa_alloc_sas_phy(hpsa_sas_port);
9720 if (!hpsa_sas_phy) {
9721 rc = -ENODEV;
9722 goto free_sas_port;
9723 }
9724
9725 rc = hpsa_sas_port_add_phy(hpsa_sas_phy);
9726 if (rc)
9727 goto free_sas_phy;
9728
9729 h->sas_host = hpsa_sas_node;
9730
9731 return 0;
9732
9733free_sas_phy:
9734 hpsa_free_sas_phy(hpsa_sas_phy);
9735free_sas_port:
9736 hpsa_free_sas_port(hpsa_sas_port);
9737free_sas_node:
9738 hpsa_free_sas_node(hpsa_sas_node);
9739
9740 return rc;
9741}
9742
9743static void hpsa_delete_sas_host(struct ctlr_info *h)
9744{
9745 hpsa_free_sas_node(h->sas_host);
9746}
9747
9748static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
9749 struct hpsa_scsi_dev_t *device)
9750{
9751 int rc;
9752 struct hpsa_sas_port *hpsa_sas_port;
9753 struct sas_rphy *rphy;
9754
9755 hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, device->sas_address);
9756 if (!hpsa_sas_port)
9757 return -ENOMEM;
9758
9759 rphy = sas_end_device_alloc(hpsa_sas_port->port);
9760 if (!rphy) {
9761 rc = -ENODEV;
9762 goto free_sas_port;
9763 }
9764
9765 hpsa_sas_port->rphy = rphy;
9766 device->sas_port = hpsa_sas_port;
9767
9768 rc = hpsa_sas_port_add_rphy(hpsa_sas_port, rphy);
9769 if (rc)
9770 goto free_sas_port;
9771
9772 return 0;
9773
9774free_sas_port:
9775 hpsa_free_sas_port(hpsa_sas_port);
9776 device->sas_port = NULL;
9777
9778 return rc;
9779}
9780
9781static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device)
9782{
9783 if (device->sas_port) {
9784 hpsa_free_sas_port(device->sas_port);
9785 device->sas_port = NULL;
9786 }
9787}
9788
9789static int
9790hpsa_sas_get_linkerrors(struct sas_phy *phy)
9791{
9792 return 0;
9793}
9794
9795static int
9796hpsa_sas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
9797{
Dan Carpenteraa105692016-04-14 12:37:44 +03009798 *identifier = 0;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009799 return 0;
9800}
9801
9802static int
9803hpsa_sas_get_bay_identifier(struct sas_rphy *rphy)
9804{
9805 return -ENXIO;
9806}
9807
9808static int
9809hpsa_sas_phy_reset(struct sas_phy *phy, int hard_reset)
9810{
9811 return 0;
9812}
9813
9814static int
9815hpsa_sas_phy_enable(struct sas_phy *phy, int enable)
9816{
9817 return 0;
9818}
9819
9820static int
9821hpsa_sas_phy_setup(struct sas_phy *phy)
9822{
9823 return 0;
9824}
9825
9826static void
9827hpsa_sas_phy_release(struct sas_phy *phy)
9828{
9829}
9830
9831static int
9832hpsa_sas_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
9833{
9834 return -EINVAL;
9835}
9836
9837/* SMP = Serial Management Protocol */
9838static int
9839hpsa_sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
9840struct request *req)
9841{
9842 return -EINVAL;
9843}
9844
9845static struct sas_function_template hpsa_sas_transport_functions = {
9846 .get_linkerrors = hpsa_sas_get_linkerrors,
9847 .get_enclosure_identifier = hpsa_sas_get_enclosure_identifier,
9848 .get_bay_identifier = hpsa_sas_get_bay_identifier,
9849 .phy_reset = hpsa_sas_phy_reset,
9850 .phy_enable = hpsa_sas_phy_enable,
9851 .phy_setup = hpsa_sas_phy_setup,
9852 .phy_release = hpsa_sas_phy_release,
9853 .set_phy_speed = hpsa_sas_phy_speed,
9854 .smp_handler = hpsa_sas_smp_handler,
9855};
9856
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009857/*
9858 * This is it. Register the PCI driver information for the cards we control
9859 * the OS will call our registered routines when it finds one of our cards.
9860 */
9861static int __init hpsa_init(void)
9862{
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009863 int rc;
9864
9865 hpsa_sas_transport_template =
9866 sas_attach_transport(&hpsa_sas_transport_functions);
9867 if (!hpsa_sas_transport_template)
9868 return -ENODEV;
9869
9870 rc = pci_register_driver(&hpsa_pci_driver);
9871
9872 if (rc)
9873 sas_release_transport(hpsa_sas_transport_template);
9874
9875 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009876}
9877
9878static void __exit hpsa_cleanup(void)
9879{
9880 pci_unregister_driver(&hpsa_pci_driver);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009881 sas_release_transport(hpsa_sas_transport_template);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009882}
9883
Matt Gatese1f7de02014-02-18 13:55:17 -06009884static void __attribute__((unused)) verify_offsets(void)
9885{
9886#define VERIFY_OFFSET(member, offset) \
Scott Teeldd0e19f2014-02-18 13:57:31 -06009887 BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
9888
9889 VERIFY_OFFSET(structure_size, 0);
9890 VERIFY_OFFSET(volume_blk_size, 4);
9891 VERIFY_OFFSET(volume_blk_cnt, 8);
9892 VERIFY_OFFSET(phys_blk_shift, 16);
9893 VERIFY_OFFSET(parity_rotation_shift, 17);
9894 VERIFY_OFFSET(strip_size, 18);
9895 VERIFY_OFFSET(disk_starting_blk, 20);
9896 VERIFY_OFFSET(disk_blk_cnt, 28);
9897 VERIFY_OFFSET(data_disks_per_row, 36);
9898 VERIFY_OFFSET(metadata_disks_per_row, 38);
9899 VERIFY_OFFSET(row_cnt, 40);
9900 VERIFY_OFFSET(layout_map_count, 42);
9901 VERIFY_OFFSET(flags, 44);
9902 VERIFY_OFFSET(dekindex, 46);
9903 /* VERIFY_OFFSET(reserved, 48 */
9904 VERIFY_OFFSET(data, 64);
9905
9906#undef VERIFY_OFFSET
9907
9908#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06009909 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
9910
9911 VERIFY_OFFSET(IU_type, 0);
9912 VERIFY_OFFSET(direction, 1);
9913 VERIFY_OFFSET(reply_queue, 2);
9914 /* VERIFY_OFFSET(reserved1, 3); */
9915 VERIFY_OFFSET(scsi_nexus, 4);
9916 VERIFY_OFFSET(Tag, 8);
9917 VERIFY_OFFSET(cdb, 16);
9918 VERIFY_OFFSET(cciss_lun, 32);
9919 VERIFY_OFFSET(data_len, 40);
9920 VERIFY_OFFSET(cmd_priority_task_attr, 44);
9921 VERIFY_OFFSET(sg_count, 45);
9922 /* VERIFY_OFFSET(reserved3 */
9923 VERIFY_OFFSET(err_ptr, 48);
9924 VERIFY_OFFSET(err_len, 56);
9925 /* VERIFY_OFFSET(reserved4 */
9926 VERIFY_OFFSET(sg, 64);
9927
9928#undef VERIFY_OFFSET
9929
9930#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06009931 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
9932
9933 VERIFY_OFFSET(dev_handle, 0x00);
9934 VERIFY_OFFSET(reserved1, 0x02);
9935 VERIFY_OFFSET(function, 0x03);
9936 VERIFY_OFFSET(reserved2, 0x04);
9937 VERIFY_OFFSET(err_info, 0x0C);
9938 VERIFY_OFFSET(reserved3, 0x10);
9939 VERIFY_OFFSET(err_info_len, 0x12);
9940 VERIFY_OFFSET(reserved4, 0x13);
9941 VERIFY_OFFSET(sgl_offset, 0x14);
9942 VERIFY_OFFSET(reserved5, 0x15);
9943 VERIFY_OFFSET(transfer_len, 0x1C);
9944 VERIFY_OFFSET(reserved6, 0x20);
9945 VERIFY_OFFSET(io_flags, 0x24);
9946 VERIFY_OFFSET(reserved7, 0x26);
9947 VERIFY_OFFSET(LUN, 0x34);
9948 VERIFY_OFFSET(control, 0x3C);
9949 VERIFY_OFFSET(CDB, 0x40);
9950 VERIFY_OFFSET(reserved8, 0x50);
9951 VERIFY_OFFSET(host_context_flags, 0x60);
9952 VERIFY_OFFSET(timeout_sec, 0x62);
9953 VERIFY_OFFSET(ReplyQueue, 0x64);
9954 VERIFY_OFFSET(reserved9, 0x65);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009955 VERIFY_OFFSET(tag, 0x68);
Matt Gatese1f7de02014-02-18 13:55:17 -06009956 VERIFY_OFFSET(host_addr, 0x70);
9957 VERIFY_OFFSET(CISS_LUN, 0x78);
9958 VERIFY_OFFSET(SG, 0x78 + 8);
9959#undef VERIFY_OFFSET
9960}
9961
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009962module_init(hpsa_init);
9963module_exit(hpsa_cleanup);