blob: 4f8e3285baa9dff8a94ce9c921737cc2bebcca5e [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 */
63#define HPSA_DRIVER_VERSION "3.4.14-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);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800297
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800298static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
299{
300 unsigned long *priv = shost_priv(sdev->host);
301 return (struct ctlr_info *) *priv;
302}
303
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600304static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
305{
306 unsigned long *priv = shost_priv(sh);
307 return (struct ctlr_info *) *priv;
308}
309
Webb Scalesa58e7e52015-04-23 09:34:16 -0500310static inline bool hpsa_is_cmd_idle(struct CommandList *c)
311{
312 return c->scsi_cmd == SCSI_CMD_IDLE;
313}
314
Webb Scalesd604f532015-04-23 09:35:22 -0500315static inline bool hpsa_is_pending_event(struct CommandList *c)
316{
317 return c->abort_pending || c->reset_pending;
318}
319
Stephen Cameron9437ac42015-04-23 09:32:16 -0500320/* extract sense key, asc, and ascq from sense data. -1 means invalid. */
321static void decode_sense_data(const u8 *sense_data, int sense_data_len,
322 u8 *sense_key, u8 *asc, u8 *ascq)
323{
324 struct scsi_sense_hdr sshdr;
325 bool rc;
326
327 *sense_key = -1;
328 *asc = -1;
329 *ascq = -1;
330
331 if (sense_data_len < 1)
332 return;
333
334 rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
335 if (rc) {
336 *sense_key = sshdr.sense_key;
337 *asc = sshdr.asc;
338 *ascq = sshdr.ascq;
339 }
340}
341
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800342static int check_for_unit_attention(struct ctlr_info *h,
343 struct CommandList *c)
344{
Stephen Cameron9437ac42015-04-23 09:32:16 -0500345 u8 sense_key, asc, ascq;
346 int sense_len;
347
348 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
349 sense_len = sizeof(c->err_info->SenseInfo);
350 else
351 sense_len = c->err_info->SenseLen;
352
353 decode_sense_data(c->err_info->SenseInfo, sense_len,
354 &sense_key, &asc, &ascq);
Don Brace81c27552015-07-18 11:12:28 -0500355 if (sense_key != UNIT_ATTENTION || asc == 0xff)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800356 return 0;
357
Stephen Cameron9437ac42015-04-23 09:32:16 -0500358 switch (asc) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800359 case STATE_CHANGED:
Stephen Cameron9437ac42015-04-23 09:32:16 -0500360 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500361 "%s: a state change detected, command retried\n",
362 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800363 break;
364 case LUN_FAILED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600365 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500366 "%s: LUN failure detected\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800367 break;
368 case REPORT_LUNS_CHANGED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600369 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500370 "%s: report LUN data changed\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800371 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600372 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
373 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800374 */
375 break;
376 case POWER_OR_RESET:
Robert Elliott2946e822015-04-23 09:35:09 -0500377 dev_warn(&h->pdev->dev,
378 "%s: a power on or device reset detected\n",
379 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800380 break;
381 case UNIT_ATTENTION_CLEARED:
Robert Elliott2946e822015-04-23 09:35:09 -0500382 dev_warn(&h->pdev->dev,
383 "%s: unit attention cleared by another initiator\n",
384 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800385 break;
386 default:
Robert Elliott2946e822015-04-23 09:35:09 -0500387 dev_warn(&h->pdev->dev,
388 "%s: unknown unit attention detected\n",
389 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800390 break;
391 }
392 return 1;
393}
394
Matt Bondurant852af202012-05-01 11:42:35 -0500395static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
396{
397 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
398 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
399 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
400 return 0;
401 dev_warn(&h->pdev->dev, HPSA "device busy");
402 return 1;
403}
404
Stephen Camerone985c582015-04-23 09:32:22 -0500405static u32 lockup_detected(struct ctlr_info *h);
406static ssize_t host_show_lockup_detected(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
409 int ld;
410 struct ctlr_info *h;
411 struct Scsi_Host *shost = class_to_shost(dev);
412
413 h = shost_to_hba(shost);
414 ld = lockup_detected(h);
415
416 return sprintf(buf, "ld=%d\n", ld);
417}
418
Scott Teelda0697b2014-02-18 13:57:00 -0600419static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
420 struct device_attribute *attr,
421 const char *buf, size_t count)
422{
423 int status, len;
424 struct ctlr_info *h;
425 struct Scsi_Host *shost = class_to_shost(dev);
426 char tmpbuf[10];
427
428 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
429 return -EACCES;
430 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
431 strncpy(tmpbuf, buf, len);
432 tmpbuf[len] = '\0';
433 if (sscanf(tmpbuf, "%d", &status) != 1)
434 return -EINVAL;
435 h = shost_to_hba(shost);
436 h->acciopath_status = !!status;
437 dev_warn(&h->pdev->dev,
438 "hpsa: HP SSD Smart Path %s via sysfs update.\n",
439 h->acciopath_status ? "enabled" : "disabled");
440 return count;
441}
442
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600443static ssize_t host_store_raid_offload_debug(struct device *dev,
444 struct device_attribute *attr,
445 const char *buf, size_t count)
446{
447 int debug_level, len;
448 struct ctlr_info *h;
449 struct Scsi_Host *shost = class_to_shost(dev);
450 char tmpbuf[10];
451
452 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
453 return -EACCES;
454 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
455 strncpy(tmpbuf, buf, len);
456 tmpbuf[len] = '\0';
457 if (sscanf(tmpbuf, "%d", &debug_level) != 1)
458 return -EINVAL;
459 if (debug_level < 0)
460 debug_level = 0;
461 h = shost_to_hba(shost);
462 h->raid_offload_debug = debug_level;
463 dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
464 h->raid_offload_debug);
465 return count;
466}
467
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800468static ssize_t host_store_rescan(struct device *dev,
469 struct device_attribute *attr,
470 const char *buf, size_t count)
471{
472 struct ctlr_info *h;
473 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600474 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600475 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800476 return count;
477}
478
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500479static ssize_t host_show_firmware_revision(struct device *dev,
480 struct device_attribute *attr, char *buf)
481{
482 struct ctlr_info *h;
483 struct Scsi_Host *shost = class_to_shost(dev);
484 unsigned char *fwrev;
485
486 h = shost_to_hba(shost);
487 if (!h->hba_inquiry_data)
488 return 0;
489 fwrev = &h->hba_inquiry_data[32];
490 return snprintf(buf, 20, "%c%c%c%c\n",
491 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
492}
493
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600494static ssize_t host_show_commands_outstanding(struct device *dev,
495 struct device_attribute *attr, char *buf)
496{
497 struct Scsi_Host *shost = class_to_shost(dev);
498 struct ctlr_info *h = shost_to_hba(shost);
499
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600500 return snprintf(buf, 20, "%d\n",
501 atomic_read(&h->commands_outstanding));
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600502}
503
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600504static ssize_t host_show_transport_mode(struct device *dev,
505 struct device_attribute *attr, char *buf)
506{
507 struct ctlr_info *h;
508 struct Scsi_Host *shost = class_to_shost(dev);
509
510 h = shost_to_hba(shost);
511 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600512 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600513 "performant" : "simple");
514}
515
Scott Teelda0697b2014-02-18 13:57:00 -0600516static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
517 struct device_attribute *attr, char *buf)
518{
519 struct ctlr_info *h;
520 struct Scsi_Host *shost = class_to_shost(dev);
521
522 h = shost_to_hba(shost);
523 return snprintf(buf, 30, "HP SSD Smart Path %s\n",
524 (h->acciopath_status == 1) ? "enabled" : "disabled");
525}
526
Stephen M. Cameron46380782011-05-03 15:00:01 -0500527/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600528static u32 unresettable_controller[] = {
529 0x324a103C, /* Smart Array P712m */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500530 0x324b103C, /* Smart Array P711m */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600531 0x3223103C, /* Smart Array P800 */
532 0x3234103C, /* Smart Array P400 */
533 0x3235103C, /* Smart Array P400i */
534 0x3211103C, /* Smart Array E200i */
535 0x3212103C, /* Smart Array E200 */
536 0x3213103C, /* Smart Array E200i */
537 0x3214103C, /* Smart Array E200i */
538 0x3215103C, /* Smart Array E200i */
539 0x3237103C, /* Smart Array E500 */
540 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100541 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600542 0x409C0E11, /* Smart Array 6400 */
543 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100544 0x40700E11, /* Smart Array 5300 */
545 0x40820E11, /* Smart Array 532 */
546 0x40830E11, /* Smart Array 5312 */
547 0x409A0E11, /* Smart Array 641 */
548 0x409B0E11, /* Smart Array 642 */
549 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600550};
551
Stephen M. Cameron46380782011-05-03 15:00:01 -0500552/* List of controllers which cannot even be soft reset */
553static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100554 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100555 0x40700E11, /* Smart Array 5300 */
556 0x40820E11, /* Smart Array 532 */
557 0x40830E11, /* Smart Array 5312 */
558 0x409A0E11, /* Smart Array 641 */
559 0x409B0E11, /* Smart Array 642 */
560 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500561 /* Exclude 640x boards. These are two pci devices in one slot
562 * which share a battery backed cache module. One controls the
563 * cache, the other accesses the cache through the one that controls
564 * it. If we reset the one controlling the cache, the other will
565 * likely not be happy. Just forbid resetting this conjoined mess.
566 * The 640x isn't really supported by hpsa anyway.
567 */
568 0x409C0E11, /* Smart Array 6400 */
569 0x409D0E11, /* Smart Array 6400 EM */
570};
571
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500572static u32 needs_abort_tags_swizzled[] = {
573 0x323D103C, /* Smart Array P700m */
574 0x324a103C, /* Smart Array P712m */
575 0x324b103C, /* SmartArray P711m */
576};
577
578static int board_id_in_array(u32 a[], int nelems, u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600579{
580 int i;
581
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500582 for (i = 0; i < nelems; i++)
583 if (a[i] == board_id)
584 return 1;
585 return 0;
586}
587
588static int ctlr_is_hard_resettable(u32 board_id)
589{
590 return !board_id_in_array(unresettable_controller,
591 ARRAY_SIZE(unresettable_controller), board_id);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600592}
593
Stephen M. Cameron46380782011-05-03 15:00:01 -0500594static int ctlr_is_soft_resettable(u32 board_id)
595{
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500596 return !board_id_in_array(soft_unresettable_controller,
597 ARRAY_SIZE(soft_unresettable_controller), board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500598}
599
600static int ctlr_is_resettable(u32 board_id)
601{
602 return ctlr_is_hard_resettable(board_id) ||
603 ctlr_is_soft_resettable(board_id);
604}
605
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500606static int ctlr_needs_abort_tags_swizzled(u32 board_id)
607{
608 return board_id_in_array(needs_abort_tags_swizzled,
609 ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
610}
611
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600612static ssize_t host_show_resettable(struct device *dev,
613 struct device_attribute *attr, char *buf)
614{
615 struct ctlr_info *h;
616 struct Scsi_Host *shost = class_to_shost(dev);
617
618 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500619 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600620}
621
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800622static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
623{
624 return (scsi3addr[3] & 0xC0) == 0x40;
625}
626
Robert Elliottf2ef0ce2015-01-23 16:41:35 -0600627static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
Don Brace7c59a0d2015-11-04 15:52:22 -0600628 "1(+0)ADM", "UNKNOWN", "PHYS DRV"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800629};
Scott Teel6b80b182014-02-18 13:56:55 -0600630#define HPSA_RAID_0 0
631#define HPSA_RAID_4 1
632#define HPSA_RAID_1 2 /* also used for RAID 10 */
633#define HPSA_RAID_5 3 /* also used for RAID 50 */
634#define HPSA_RAID_51 4
635#define HPSA_RAID_6 5 /* also used for RAID 60 */
636#define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
Don Brace7c59a0d2015-11-04 15:52:22 -0600637#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 2)
638#define PHYSICAL_DRIVE (ARRAY_SIZE(raid_label) - 1)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800639
Kevin Barnettf3f01732015-11-04 15:51:33 -0600640static inline bool is_logical_device(struct hpsa_scsi_dev_t *device)
641{
642 return !device->physical_device;
643}
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800644
645static ssize_t raid_level_show(struct device *dev,
646 struct device_attribute *attr, char *buf)
647{
648 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600649 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800650 struct ctlr_info *h;
651 struct scsi_device *sdev;
652 struct hpsa_scsi_dev_t *hdev;
653 unsigned long flags;
654
655 sdev = to_scsi_device(dev);
656 h = sdev_to_hba(sdev);
657 spin_lock_irqsave(&h->lock, flags);
658 hdev = sdev->hostdata;
659 if (!hdev) {
660 spin_unlock_irqrestore(&h->lock, flags);
661 return -ENODEV;
662 }
663
664 /* Is this even a logical drive? */
Kevin Barnettf3f01732015-11-04 15:51:33 -0600665 if (!is_logical_device(hdev)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800666 spin_unlock_irqrestore(&h->lock, flags);
667 l = snprintf(buf, PAGE_SIZE, "N/A\n");
668 return l;
669 }
670
671 rlevel = hdev->raid_level;
672 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600673 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800674 rlevel = RAID_UNKNOWN;
675 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
676 return l;
677}
678
679static ssize_t lunid_show(struct device *dev,
680 struct device_attribute *attr, char *buf)
681{
682 struct ctlr_info *h;
683 struct scsi_device *sdev;
684 struct hpsa_scsi_dev_t *hdev;
685 unsigned long flags;
686 unsigned char lunid[8];
687
688 sdev = to_scsi_device(dev);
689 h = sdev_to_hba(sdev);
690 spin_lock_irqsave(&h->lock, flags);
691 hdev = sdev->hostdata;
692 if (!hdev) {
693 spin_unlock_irqrestore(&h->lock, flags);
694 return -ENODEV;
695 }
696 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
697 spin_unlock_irqrestore(&h->lock, flags);
698 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
699 lunid[0], lunid[1], lunid[2], lunid[3],
700 lunid[4], lunid[5], lunid[6], lunid[7]);
701}
702
703static ssize_t unique_id_show(struct device *dev,
704 struct device_attribute *attr, char *buf)
705{
706 struct ctlr_info *h;
707 struct scsi_device *sdev;
708 struct hpsa_scsi_dev_t *hdev;
709 unsigned long flags;
710 unsigned char sn[16];
711
712 sdev = to_scsi_device(dev);
713 h = sdev_to_hba(sdev);
714 spin_lock_irqsave(&h->lock, flags);
715 hdev = sdev->hostdata;
716 if (!hdev) {
717 spin_unlock_irqrestore(&h->lock, flags);
718 return -ENODEV;
719 }
720 memcpy(sn, hdev->device_id, sizeof(sn));
721 spin_unlock_irqrestore(&h->lock, flags);
722 return snprintf(buf, 16 * 2 + 2,
723 "%02X%02X%02X%02X%02X%02X%02X%02X"
724 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
725 sn[0], sn[1], sn[2], sn[3],
726 sn[4], sn[5], sn[6], sn[7],
727 sn[8], sn[9], sn[10], sn[11],
728 sn[12], sn[13], sn[14], sn[15]);
729}
730
Joseph T Handzikded1be42016-04-27 17:13:33 -0500731static ssize_t sas_address_show(struct device *dev,
732 struct device_attribute *attr, char *buf)
733{
734 struct ctlr_info *h;
735 struct scsi_device *sdev;
736 struct hpsa_scsi_dev_t *hdev;
737 unsigned long flags;
738 u64 sas_address;
739
740 sdev = to_scsi_device(dev);
741 h = sdev_to_hba(sdev);
742 spin_lock_irqsave(&h->lock, flags);
743 hdev = sdev->hostdata;
744 if (!hdev || is_logical_device(hdev) || !hdev->expose_device) {
745 spin_unlock_irqrestore(&h->lock, flags);
746 return -ENODEV;
747 }
748 sas_address = hdev->sas_address;
749 spin_unlock_irqrestore(&h->lock, flags);
750
751 return snprintf(buf, PAGE_SIZE, "0x%016llx\n", sas_address);
752}
753
Scott Teelc1988682014-02-18 13:55:54 -0600754static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
755 struct device_attribute *attr, char *buf)
756{
757 struct ctlr_info *h;
758 struct scsi_device *sdev;
759 struct hpsa_scsi_dev_t *hdev;
760 unsigned long flags;
761 int offload_enabled;
762
763 sdev = to_scsi_device(dev);
764 h = sdev_to_hba(sdev);
765 spin_lock_irqsave(&h->lock, flags);
766 hdev = sdev->hostdata;
767 if (!hdev) {
768 spin_unlock_irqrestore(&h->lock, flags);
769 return -ENODEV;
770 }
771 offload_enabled = hdev->offload_enabled;
772 spin_unlock_irqrestore(&h->lock, flags);
773 return snprintf(buf, 20, "%d\n", offload_enabled);
774}
775
Joe Handzik8270b862015-07-18 11:12:43 -0500776#define MAX_PATHS 8
Joe Handzik8270b862015-07-18 11:12:43 -0500777static ssize_t path_info_show(struct device *dev,
778 struct device_attribute *attr, char *buf)
779{
780 struct ctlr_info *h;
781 struct scsi_device *sdev;
782 struct hpsa_scsi_dev_t *hdev;
783 unsigned long flags;
784 int i;
785 int output_len = 0;
786 u8 box;
787 u8 bay;
788 u8 path_map_index = 0;
789 char *active;
790 unsigned char phys_connector[2];
Joe Handzik8270b862015-07-18 11:12:43 -0500791
Joe Handzik8270b862015-07-18 11:12:43 -0500792 sdev = to_scsi_device(dev);
793 h = sdev_to_hba(sdev);
794 spin_lock_irqsave(&h->devlock, flags);
795 hdev = sdev->hostdata;
796 if (!hdev) {
797 spin_unlock_irqrestore(&h->devlock, flags);
798 return -ENODEV;
799 }
800
801 bay = hdev->bay;
802 for (i = 0; i < MAX_PATHS; i++) {
803 path_map_index = 1<<i;
804 if (i == hdev->active_path_index)
805 active = "Active";
806 else if (hdev->path_map & path_map_index)
807 active = "Inactive";
808 else
809 continue;
810
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600811 output_len += scnprintf(buf + output_len,
812 PAGE_SIZE - output_len,
813 "[%d:%d:%d:%d] %20.20s ",
Joe Handzik8270b862015-07-18 11:12:43 -0500814 h->scsi_host->host_no,
815 hdev->bus, hdev->target, hdev->lun,
816 scsi_device_type(hdev->devtype));
817
Don Bracecca8f132015-12-22 10:36:48 -0600818 if (hdev->devtype == TYPE_RAID || is_logical_device(hdev)) {
Don Brace2708f292015-12-22 10:36:36 -0600819 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600820 PAGE_SIZE - output_len,
821 "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500822 continue;
823 }
824
825 box = hdev->box[i];
826 memcpy(&phys_connector, &hdev->phys_connector[i],
827 sizeof(phys_connector));
828 if (phys_connector[0] < '0')
829 phys_connector[0] = '0';
830 if (phys_connector[1] < '0')
831 phys_connector[1] = '0';
Don Bracecca8f132015-12-22 10:36:48 -0600832 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600833 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500834 "PORT: %.2s ",
835 phys_connector);
Don Braceaf15ed32016-02-23 15:16:15 -0600836 if ((hdev->devtype == TYPE_DISK || hdev->devtype == TYPE_ZBC) &&
837 hdev->expose_device) {
Joe Handzik8270b862015-07-18 11:12:43 -0500838 if (box == 0 || box == 0xFF) {
Don Brace2708f292015-12-22 10:36:36 -0600839 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600840 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500841 "BAY: %hhu %s\n",
842 bay, active);
843 } else {
Don Brace2708f292015-12-22 10:36:36 -0600844 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600845 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500846 "BOX: %hhu BAY: %hhu %s\n",
847 box, bay, active);
848 }
849 } else if (box != 0 && box != 0xFF) {
Don Brace2708f292015-12-22 10:36:36 -0600850 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600851 PAGE_SIZE - output_len, "BOX: %hhu %s\n",
Joe Handzik8270b862015-07-18 11:12:43 -0500852 box, active);
853 } else
Don Brace2708f292015-12-22 10:36:36 -0600854 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600855 PAGE_SIZE - output_len, "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500856 }
857
858 spin_unlock_irqrestore(&h->devlock, flags);
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600859 return output_len;
Joe Handzik8270b862015-07-18 11:12:43 -0500860}
861
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600862static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
863static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
864static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
865static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Joseph T Handzikded1be42016-04-27 17:13:33 -0500866static DEVICE_ATTR(sas_address, S_IRUGO, sas_address_show, NULL);
Scott Teelc1988682014-02-18 13:55:54 -0600867static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
868 host_show_hp_ssd_smart_path_enabled, NULL);
Joe Handzik8270b862015-07-18 11:12:43 -0500869static DEVICE_ATTR(path_info, S_IRUGO, path_info_show, NULL);
Scott Teelda0697b2014-02-18 13:57:00 -0600870static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
871 host_show_hp_ssd_smart_path_status,
872 host_store_hp_ssd_smart_path_status);
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600873static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
874 host_store_raid_offload_debug);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600875static DEVICE_ATTR(firmware_revision, S_IRUGO,
876 host_show_firmware_revision, NULL);
877static DEVICE_ATTR(commands_outstanding, S_IRUGO,
878 host_show_commands_outstanding, NULL);
879static DEVICE_ATTR(transport_mode, S_IRUGO,
880 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600881static DEVICE_ATTR(resettable, S_IRUGO,
882 host_show_resettable, NULL);
Stephen Camerone985c582015-04-23 09:32:22 -0500883static DEVICE_ATTR(lockup_detected, S_IRUGO,
884 host_show_lockup_detected, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600885
886static struct device_attribute *hpsa_sdev_attrs[] = {
887 &dev_attr_raid_level,
888 &dev_attr_lunid,
889 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600890 &dev_attr_hp_ssd_smart_path_enabled,
Joe Handzik8270b862015-07-18 11:12:43 -0500891 &dev_attr_path_info,
Joseph T Handzikded1be42016-04-27 17:13:33 -0500892 &dev_attr_sas_address,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600893 NULL,
894};
895
896static struct device_attribute *hpsa_shost_attrs[] = {
897 &dev_attr_rescan,
898 &dev_attr_firmware_revision,
899 &dev_attr_commands_outstanding,
900 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600901 &dev_attr_resettable,
Scott Teelda0697b2014-02-18 13:57:00 -0600902 &dev_attr_hp_ssd_smart_path_status,
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600903 &dev_attr_raid_offload_debug,
Tomas Henzlfb53c432015-11-06 16:24:09 +0100904 &dev_attr_lockup_detected,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600905 NULL,
906};
907
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500908#define HPSA_NRESERVED_CMDS (HPSA_CMDS_RESERVED_FOR_ABORTS + \
909 HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
910
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600911static struct scsi_host_template hpsa_driver_template = {
912 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600913 .name = HPSA,
914 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600915 .queuecommand = hpsa_scsi_queue_command,
916 .scan_start = hpsa_scan_start,
917 .scan_finished = hpsa_scan_finished,
Don Brace7c0a0222015-01-23 16:41:30 -0600918 .change_queue_depth = hpsa_change_queue_depth,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600919 .this_id = -1,
920 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500921 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600922 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
923 .ioctl = hpsa_ioctl,
924 .slave_alloc = hpsa_slave_alloc,
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500925 .slave_configure = hpsa_slave_configure,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600926 .slave_destroy = hpsa_slave_destroy,
927#ifdef CONFIG_COMPAT
928 .compat_ioctl = hpsa_compat_ioctl,
929#endif
930 .sdev_attrs = hpsa_sdev_attrs,
931 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500932 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400933 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600934};
935
Matt Gates254f7962012-05-01 11:43:06 -0500936static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600937{
938 u32 a;
Stephen M. Cameron072b0512014-05-29 10:53:07 -0500939 struct reply_queue_buffer *rq = &h->reply_queue[q];
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600940
Matt Gatese1f7de02014-02-18 13:55:17 -0600941 if (h->transMethod & CFGTBL_Trans_io_accel1)
942 return h->access.command_completed(h, q);
943
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600944 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500945 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600946
Matt Gates254f7962012-05-01 11:43:06 -0500947 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
948 a = rq->head[rq->current_entry];
949 rq->current_entry++;
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600950 atomic_dec(&h->commands_outstanding);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600951 } else {
952 a = FIFO_EMPTY;
953 }
954 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500955 if (rq->current_entry == h->max_commands) {
956 rq->current_entry = 0;
957 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600958 }
959 return a;
960}
961
Scott Teelc3497752014-02-18 13:56:34 -0600962/*
963 * There are some special bits in the bus address of the
964 * command that we have to set for the controller to know
965 * how to process the command:
966 *
967 * Normal performant mode:
968 * bit 0: 1 means performant mode, 0 means simple mode.
969 * bits 1-3 = block fetch table entry
970 * bits 4-6 = command type (== 0)
971 *
972 * ioaccel1 mode:
973 * bit 0 = "performant mode" bit.
974 * bits 1-3 = block fetch table entry
975 * bits 4-6 = command type (== 110)
976 * (command type is needed because ioaccel1 mode
977 * commands are submitted through the same register as normal
978 * mode commands, so this is how the controller knows whether
979 * the command is normal mode or ioaccel1 mode.)
980 *
981 * ioaccel2 mode:
982 * bit 0 = "performant mode" bit.
983 * bits 1-4 = block fetch table entry (note extra bit)
984 * bits 4-6 = not needed, because ioaccel2 mode has
985 * a separate special register for submitting commands.
986 */
987
Webb Scales25163bd2015-04-23 09:32:00 -0500988/*
989 * set_performant_mode: Modify the tag for cciss performant
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600990 * set bit 0 for pull model, bits 3-1 for block fetch
991 * register number
992 */
Webb Scales25163bd2015-04-23 09:32:00 -0500993#define DEFAULT_REPLY_QUEUE (-1)
994static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
995 int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600996{
Matt Gates254f7962012-05-01 11:43:06 -0500997 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600998 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Webb Scales25163bd2015-04-23 09:32:00 -0500999 if (unlikely(!h->msix_vector))
1000 return;
1001 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
Matt Gates254f7962012-05-01 11:43:06 -05001002 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +02001003 raw_smp_processor_id() % h->nreply_queues;
Webb Scales25163bd2015-04-23 09:32:00 -05001004 else
1005 c->Header.ReplyQueue = reply_queue % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -05001006 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001007}
1008
Scott Teelc3497752014-02-18 13:56:34 -06001009static void set_ioaccel1_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05001010 struct CommandList *c,
1011 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -06001012{
1013 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
1014
Webb Scales25163bd2015-04-23 09:32:00 -05001015 /*
1016 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -06001017 * processor. This seems to give the best I/O throughput.
1018 */
Webb Scales25163bd2015-04-23 09:32:00 -05001019 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1020 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
1021 else
1022 cp->ReplyQueue = reply_queue % h->nreply_queues;
1023 /*
1024 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -06001025 * - performant mode bit (bit 0)
1026 * - pull count (bits 1-3)
1027 * - command type (bits 4-6)
1028 */
1029 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
1030 IOACCEL1_BUSADDR_CMDTYPE;
1031}
1032
Stephen Cameron8be986c2015-04-23 09:34:06 -05001033static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
1034 struct CommandList *c,
1035 int reply_queue)
1036{
1037 struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
1038 &h->ioaccel2_cmd_pool[c->cmdindex];
1039
1040 /* Tell the controller to post the reply to the queue for this
1041 * processor. This seems to give the best I/O throughput.
1042 */
1043 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1044 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1045 else
1046 cp->reply_queue = reply_queue % h->nreply_queues;
1047 /* Set the bits in the address sent down to include:
1048 * - performant mode bit not used in ioaccel mode 2
1049 * - pull count (bits 0-3)
1050 * - command type isn't needed for ioaccel2
1051 */
1052 c->busaddr |= h->ioaccel2_blockFetchTable[0];
1053}
1054
Scott Teelc3497752014-02-18 13:56:34 -06001055static void set_ioaccel2_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05001056 struct CommandList *c,
1057 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -06001058{
1059 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
1060
Webb Scales25163bd2015-04-23 09:32:00 -05001061 /*
1062 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -06001063 * processor. This seems to give the best I/O throughput.
1064 */
Webb Scales25163bd2015-04-23 09:32:00 -05001065 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1066 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1067 else
1068 cp->reply_queue = reply_queue % h->nreply_queues;
1069 /*
1070 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -06001071 * - performant mode bit not used in ioaccel mode 2
1072 * - pull count (bits 0-3)
1073 * - command type isn't needed for ioaccel2
1074 */
1075 c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
1076}
1077
Stephen M. Camerone85c5972012-05-01 11:43:42 -05001078static int is_firmware_flash_cmd(u8 *cdb)
1079{
1080 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
1081}
1082
1083/*
1084 * During firmware flash, the heartbeat register may not update as frequently
1085 * as it should. So we dial down lockup detection during firmware flash. and
1086 * dial it back up when firmware flash completes.
1087 */
1088#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
1089#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
1090static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
1091 struct CommandList *c)
1092{
1093 if (!is_firmware_flash_cmd(c->Request.CDB))
1094 return;
1095 atomic_inc(&h->firmware_flash_in_progress);
1096 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
1097}
1098
1099static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
1100 struct CommandList *c)
1101{
1102 if (is_firmware_flash_cmd(c->Request.CDB) &&
1103 atomic_dec_and_test(&h->firmware_flash_in_progress))
1104 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
1105}
1106
Webb Scales25163bd2015-04-23 09:32:00 -05001107static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
1108 struct CommandList *c, int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001109{
Stephen Cameronc05e8862015-01-23 16:44:40 -06001110 dial_down_lockup_detection_during_fw_flash(h, c);
1111 atomic_inc(&h->commands_outstanding);
Scott Teelc3497752014-02-18 13:56:34 -06001112 switch (c->cmd_type) {
1113 case CMD_IOACCEL1:
Webb Scales25163bd2015-04-23 09:32:00 -05001114 set_ioaccel1_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001115 writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
Scott Teelc3497752014-02-18 13:56:34 -06001116 break;
1117 case CMD_IOACCEL2:
Webb Scales25163bd2015-04-23 09:32:00 -05001118 set_ioaccel2_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001119 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
Scott Teelc3497752014-02-18 13:56:34 -06001120 break;
Stephen Cameron8be986c2015-04-23 09:34:06 -05001121 case IOACCEL2_TMF:
1122 set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
1123 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
1124 break;
Scott Teelc3497752014-02-18 13:56:34 -06001125 default:
Webb Scales25163bd2015-04-23 09:32:00 -05001126 set_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001127 h->access.submit_command(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06001128 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001129}
1130
Webb Scalesa58e7e52015-04-23 09:34:16 -05001131static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
Webb Scales25163bd2015-04-23 09:32:00 -05001132{
Webb Scalesd604f532015-04-23 09:35:22 -05001133 if (unlikely(hpsa_is_pending_event(c)))
Webb Scalesa58e7e52015-04-23 09:34:16 -05001134 return finish_cmd(c);
1135
Webb Scales25163bd2015-04-23 09:32:00 -05001136 __enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
1137}
1138
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001139static inline int is_hba_lunid(unsigned char scsi3addr[])
1140{
1141 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
1142}
1143
1144static inline int is_scsi_rev_5(struct ctlr_info *h)
1145{
1146 if (!h->hba_inquiry_data)
1147 return 0;
1148 if ((h->hba_inquiry_data[2] & 0x07) == 5)
1149 return 1;
1150 return 0;
1151}
1152
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001153static int hpsa_find_target_lun(struct ctlr_info *h,
1154 unsigned char scsi3addr[], int bus, int *target, int *lun)
1155{
1156 /* finds an unused bus, target, lun for a new physical device
1157 * assumes h->devlock is held
1158 */
1159 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -05001160 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001161
Akinobu Mita263d9402012-01-21 00:15:27 +09001162 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001163
1164 for (i = 0; i < h->ndevices; i++) {
1165 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +09001166 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001167 }
1168
Akinobu Mita263d9402012-01-21 00:15:27 +09001169 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
1170 if (i < HPSA_MAX_DEVICES) {
1171 /* *bus = 1; */
1172 *target = i;
1173 *lun = 0;
1174 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001175 }
1176 return !found;
1177}
1178
Don Brace1d33d852015-11-04 15:50:31 -06001179static void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
Webb Scales0d96ef52015-04-23 09:31:55 -05001180 struct hpsa_scsi_dev_t *dev, char *description)
1181{
Don Brace7c59a0d2015-11-04 15:52:22 -06001182#define LABEL_SIZE 25
1183 char label[LABEL_SIZE];
1184
Don Brace9975ec92015-11-04 15:50:25 -06001185 if (h == NULL || h->pdev == NULL || h->scsi_host == NULL)
1186 return;
1187
Don Brace7c59a0d2015-11-04 15:52:22 -06001188 switch (dev->devtype) {
1189 case TYPE_RAID:
1190 snprintf(label, LABEL_SIZE, "controller");
1191 break;
1192 case TYPE_ENCLOSURE:
1193 snprintf(label, LABEL_SIZE, "enclosure");
1194 break;
1195 case TYPE_DISK:
Don Braceaf15ed32016-02-23 15:16:15 -06001196 case TYPE_ZBC:
Don Brace7c59a0d2015-11-04 15:52:22 -06001197 if (dev->external)
1198 snprintf(label, LABEL_SIZE, "external");
1199 else if (!is_logical_dev_addr_mode(dev->scsi3addr))
1200 snprintf(label, LABEL_SIZE, "%s",
1201 raid_label[PHYSICAL_DRIVE]);
1202 else
1203 snprintf(label, LABEL_SIZE, "RAID-%s",
1204 dev->raid_level > RAID_UNKNOWN ? "?" :
1205 raid_label[dev->raid_level]);
1206 break;
1207 case TYPE_ROM:
1208 snprintf(label, LABEL_SIZE, "rom");
1209 break;
1210 case TYPE_TAPE:
1211 snprintf(label, LABEL_SIZE, "tape");
1212 break;
1213 case TYPE_MEDIUM_CHANGER:
1214 snprintf(label, LABEL_SIZE, "changer");
1215 break;
1216 default:
1217 snprintf(label, LABEL_SIZE, "UNKNOWN");
1218 break;
1219 }
1220
Webb Scales0d96ef52015-04-23 09:31:55 -05001221 dev_printk(level, &h->pdev->dev,
Don Brace7c59a0d2015-11-04 15:52:22 -06001222 "scsi %d:%d:%d:%d: %s %s %.8s %.16s %s SSDSmartPathCap%c En%c Exp=%d\n",
Webb Scales0d96ef52015-04-23 09:31:55 -05001223 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
1224 description,
1225 scsi_device_type(dev->devtype),
1226 dev->vendor,
1227 dev->model,
Don Brace7c59a0d2015-11-04 15:52:22 -06001228 label,
Webb Scales0d96ef52015-04-23 09:31:55 -05001229 dev->offload_config ? '+' : '-',
1230 dev->offload_enabled ? '+' : '-',
Kevin Barnett2a168202015-11-04 15:51:21 -06001231 dev->expose_device);
Webb Scales0d96ef52015-04-23 09:31:55 -05001232}
1233
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001234/* Add an entry into h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001235static int hpsa_scsi_add_entry(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001236 struct hpsa_scsi_dev_t *device,
1237 struct hpsa_scsi_dev_t *added[], int *nadded)
1238{
1239 /* assumes h->devlock is held */
1240 int n = h->ndevices;
1241 int i;
1242 unsigned char addr1[8], addr2[8];
1243 struct hpsa_scsi_dev_t *sd;
1244
Scott Teelcfe5bad2011-10-26 16:21:07 -05001245 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001246 dev_err(&h->pdev->dev, "too many devices, some will be "
1247 "inaccessible.\n");
1248 return -1;
1249 }
1250
1251 /* physical devices do not have lun or target assigned until now. */
1252 if (device->lun != -1)
1253 /* Logical device, lun is already assigned. */
1254 goto lun_assigned;
1255
1256 /* If this device a non-zero lun of a multi-lun device
1257 * byte 4 of the 8-byte LUN addr will contain the logical
Don Brace2b08b3e2015-01-23 16:41:09 -06001258 * unit no, zero otherwise.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001259 */
1260 if (device->scsi3addr[4] == 0) {
1261 /* This is not a non-zero lun of a multi-lun device */
1262 if (hpsa_find_target_lun(h, device->scsi3addr,
1263 device->bus, &device->target, &device->lun) != 0)
1264 return -1;
1265 goto lun_assigned;
1266 }
1267
1268 /* This is a non-zero lun of a multi-lun device.
1269 * Search through our list and find the device which
shane.seymour9a4178b2015-07-18 11:13:09 -05001270 * has the same 8 byte LUN address, excepting byte 4 and 5.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001271 * Assign the same bus and target for this new LUN.
1272 * Use the logical unit number from the firmware.
1273 */
1274 memcpy(addr1, device->scsi3addr, 8);
1275 addr1[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001276 addr1[5] = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001277 for (i = 0; i < n; i++) {
1278 sd = h->dev[i];
1279 memcpy(addr2, sd->scsi3addr, 8);
1280 addr2[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001281 addr2[5] = 0;
1282 /* differ only in byte 4 and 5? */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001283 if (memcmp(addr1, addr2, 8) == 0) {
1284 device->bus = sd->bus;
1285 device->target = sd->target;
1286 device->lun = device->scsi3addr[4];
1287 break;
1288 }
1289 }
1290 if (device->lun == -1) {
1291 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1292 " suspect firmware bug or unsupported hardware "
1293 "configuration.\n");
1294 return -1;
1295 }
1296
1297lun_assigned:
1298
1299 h->dev[n] = device;
1300 h->ndevices++;
1301 added[*nadded] = device;
1302 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001303 hpsa_show_dev_msg(KERN_INFO, h, device,
Kevin Barnett2a168202015-11-04 15:51:21 -06001304 device->expose_device ? "added" : "masked");
Robert Elliotta473d862015-04-23 09:32:54 -05001305 device->offload_to_be_enabled = device->offload_enabled;
1306 device->offload_enabled = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001307 return 0;
1308}
1309
Scott Teelbd9244f2012-01-19 14:01:30 -06001310/* Update an entry in h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001311static void hpsa_scsi_update_entry(struct ctlr_info *h,
Scott Teelbd9244f2012-01-19 14:01:30 -06001312 int entry, struct hpsa_scsi_dev_t *new_entry)
1313{
Robert Elliotta473d862015-04-23 09:32:54 -05001314 int offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001315 /* assumes h->devlock is held */
1316 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1317
1318 /* Raid level changed. */
1319 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001320
Don Brace03383732015-01-23 16:43:30 -06001321 /* Raid offload parameters changed. Careful about the ordering. */
1322 if (new_entry->offload_config && new_entry->offload_enabled) {
1323 /*
1324 * if drive is newly offload_enabled, we want to copy the
1325 * raid map data first. If previously offload_enabled and
1326 * offload_config were set, raid map data had better be
1327 * the same as it was before. if raid map data is changed
1328 * then it had better be the case that
1329 * h->dev[entry]->offload_enabled is currently 0.
1330 */
1331 h->dev[entry]->raid_map = new_entry->raid_map;
1332 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
Don Brace03383732015-01-23 16:43:30 -06001333 }
Joe Handzika3144e02015-04-23 09:32:59 -05001334 if (new_entry->hba_ioaccel_enabled) {
1335 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1336 wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1337 }
1338 h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001339 h->dev[entry]->offload_config = new_entry->offload_config;
Stephen M. Cameron9fb0de22014-02-18 13:56:50 -06001340 h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
Don Brace03383732015-01-23 16:43:30 -06001341 h->dev[entry]->queue_depth = new_entry->queue_depth;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001342
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001343 /*
1344 * We can turn off ioaccel offload now, but need to delay turning
1345 * it on until we can update h->dev[entry]->phys_disk[], but we
1346 * can't do that until all the devices are updated.
1347 */
1348 h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
1349 if (!new_entry->offload_enabled)
1350 h->dev[entry]->offload_enabled = 0;
1351
Robert Elliotta473d862015-04-23 09:32:54 -05001352 offload_enabled = h->dev[entry]->offload_enabled;
1353 h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
Webb Scales0d96ef52015-04-23 09:31:55 -05001354 hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
Robert Elliotta473d862015-04-23 09:32:54 -05001355 h->dev[entry]->offload_enabled = offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001356}
1357
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001358/* Replace an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001359static void hpsa_scsi_replace_entry(struct ctlr_info *h,
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001360 int entry, struct hpsa_scsi_dev_t *new_entry,
1361 struct hpsa_scsi_dev_t *added[], int *nadded,
1362 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1363{
1364 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -05001365 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001366 removed[*nremoved] = h->dev[entry];
1367 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -05001368
1369 /*
1370 * New physical devices won't have target/lun assigned yet
1371 * so we need to preserve the values in the slot we are replacing.
1372 */
1373 if (new_entry->target == -1) {
1374 new_entry->target = h->dev[entry]->target;
1375 new_entry->lun = h->dev[entry]->lun;
1376 }
1377
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001378 h->dev[entry] = new_entry;
1379 added[*nadded] = new_entry;
1380 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001381 hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
Robert Elliotta473d862015-04-23 09:32:54 -05001382 new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1383 new_entry->offload_enabled = 0;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001384}
1385
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001386/* Remove an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001387static void hpsa_scsi_remove_entry(struct ctlr_info *h, int entry,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001388 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1389{
1390 /* assumes h->devlock is held */
1391 int i;
1392 struct hpsa_scsi_dev_t *sd;
1393
Scott Teelcfe5bad2011-10-26 16:21:07 -05001394 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001395
1396 sd = h->dev[entry];
1397 removed[*nremoved] = h->dev[entry];
1398 (*nremoved)++;
1399
1400 for (i = entry; i < h->ndevices-1; i++)
1401 h->dev[i] = h->dev[i+1];
1402 h->ndevices--;
Webb Scales0d96ef52015-04-23 09:31:55 -05001403 hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001404}
1405
1406#define SCSI3ADDR_EQ(a, b) ( \
1407 (a)[7] == (b)[7] && \
1408 (a)[6] == (b)[6] && \
1409 (a)[5] == (b)[5] && \
1410 (a)[4] == (b)[4] && \
1411 (a)[3] == (b)[3] && \
1412 (a)[2] == (b)[2] && \
1413 (a)[1] == (b)[1] && \
1414 (a)[0] == (b)[0])
1415
1416static void fixup_botched_add(struct ctlr_info *h,
1417 struct hpsa_scsi_dev_t *added)
1418{
1419 /* called when scsi_add_device fails in order to re-adjust
1420 * h->dev[] to match the mid layer's view.
1421 */
1422 unsigned long flags;
1423 int i, j;
1424
1425 spin_lock_irqsave(&h->lock, flags);
1426 for (i = 0; i < h->ndevices; i++) {
1427 if (h->dev[i] == added) {
1428 for (j = i; j < h->ndevices-1; j++)
1429 h->dev[j] = h->dev[j+1];
1430 h->ndevices--;
1431 break;
1432 }
1433 }
1434 spin_unlock_irqrestore(&h->lock, flags);
1435 kfree(added);
1436}
1437
1438static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1439 struct hpsa_scsi_dev_t *dev2)
1440{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001441 /* we compare everything except lun and target as these
1442 * are not yet assigned. Compare parts likely
1443 * to differ first
1444 */
1445 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1446 sizeof(dev1->scsi3addr)) != 0)
1447 return 0;
1448 if (memcmp(dev1->device_id, dev2->device_id,
1449 sizeof(dev1->device_id)) != 0)
1450 return 0;
1451 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1452 return 0;
1453 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1454 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001455 if (dev1->devtype != dev2->devtype)
1456 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001457 if (dev1->bus != dev2->bus)
1458 return 0;
1459 return 1;
1460}
1461
Scott Teelbd9244f2012-01-19 14:01:30 -06001462static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1463 struct hpsa_scsi_dev_t *dev2)
1464{
1465 /* Device attributes that can change, but don't mean
1466 * that the device is a different device, nor that the OS
1467 * needs to be told anything about the change.
1468 */
1469 if (dev1->raid_level != dev2->raid_level)
1470 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001471 if (dev1->offload_config != dev2->offload_config)
1472 return 1;
1473 if (dev1->offload_enabled != dev2->offload_enabled)
1474 return 1;
Don Brace93849502015-07-18 11:12:49 -05001475 if (!is_logical_dev_addr_mode(dev1->scsi3addr))
1476 if (dev1->queue_depth != dev2->queue_depth)
1477 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -06001478 return 0;
1479}
1480
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001481/* Find needle in haystack. If exact match found, return DEVICE_SAME,
1482 * and return needle location in *index. If scsi3addr matches, but not
1483 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -06001484 * location in *index.
1485 * In the case of a minor device attribute change, such as RAID level, just
1486 * return DEVICE_UPDATED, along with the updated device's location in index.
1487 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001488 */
1489static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1490 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1491 int *index)
1492{
1493 int i;
1494#define DEVICE_NOT_FOUND 0
1495#define DEVICE_CHANGED 1
1496#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -06001497#define DEVICE_UPDATED 3
Don Brace1d33d852015-11-04 15:50:31 -06001498 if (needle == NULL)
1499 return DEVICE_NOT_FOUND;
1500
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001501 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -06001502 if (haystack[i] == NULL) /* previously removed. */
1503 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001504 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1505 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -06001506 if (device_is_the_same(needle, haystack[i])) {
1507 if (device_updated(needle, haystack[i]))
1508 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001509 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -06001510 } else {
Stephen M. Cameron98465902014-02-21 16:25:00 -06001511 /* Keep offline devices offline */
1512 if (needle->volume_offline)
1513 return DEVICE_NOT_FOUND;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001514 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -06001515 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001516 }
1517 }
1518 *index = -1;
1519 return DEVICE_NOT_FOUND;
1520}
1521
Stephen M. Cameron98465902014-02-21 16:25:00 -06001522static void hpsa_monitor_offline_device(struct ctlr_info *h,
1523 unsigned char scsi3addr[])
1524{
1525 struct offline_device_entry *device;
1526 unsigned long flags;
1527
1528 /* Check to see if device is already on the list */
1529 spin_lock_irqsave(&h->offline_device_lock, flags);
1530 list_for_each_entry(device, &h->offline_device_list, offline_list) {
1531 if (memcmp(device->scsi3addr, scsi3addr,
1532 sizeof(device->scsi3addr)) == 0) {
1533 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1534 return;
1535 }
1536 }
1537 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1538
1539 /* Device is not on the list, add it. */
1540 device = kmalloc(sizeof(*device), GFP_KERNEL);
1541 if (!device) {
1542 dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
1543 return;
1544 }
1545 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1546 spin_lock_irqsave(&h->offline_device_lock, flags);
1547 list_add_tail(&device->offline_list, &h->offline_device_list);
1548 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1549}
1550
1551/* Print a message explaining various offline volume states */
1552static void hpsa_show_volume_status(struct ctlr_info *h,
1553 struct hpsa_scsi_dev_t *sd)
1554{
1555 if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
1556 dev_info(&h->pdev->dev,
1557 "C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
1558 h->scsi_host->host_no,
1559 sd->bus, sd->target, sd->lun);
1560 switch (sd->volume_offline) {
1561 case HPSA_LV_OK:
1562 break;
1563 case HPSA_LV_UNDERGOING_ERASE:
1564 dev_info(&h->pdev->dev,
1565 "C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
1566 h->scsi_host->host_no,
1567 sd->bus, sd->target, sd->lun);
1568 break;
Scott Benesh5ca01202015-07-18 11:13:04 -05001569 case HPSA_LV_NOT_AVAILABLE:
1570 dev_info(&h->pdev->dev,
1571 "C%d:B%d:T%d:L%d Volume is waiting for transforming volume.\n",
1572 h->scsi_host->host_no,
1573 sd->bus, sd->target, sd->lun);
1574 break;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001575 case HPSA_LV_UNDERGOING_RPI:
1576 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001577 "C%d:B%d:T%d:L%d Volume is undergoing rapid parity init.\n",
Stephen M. Cameron98465902014-02-21 16:25:00 -06001578 h->scsi_host->host_no,
1579 sd->bus, sd->target, sd->lun);
1580 break;
1581 case HPSA_LV_PENDING_RPI:
1582 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001583 "C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
1584 h->scsi_host->host_no,
1585 sd->bus, sd->target, sd->lun);
Stephen M. Cameron98465902014-02-21 16:25:00 -06001586 break;
1587 case HPSA_LV_ENCRYPTED_NO_KEY:
1588 dev_info(&h->pdev->dev,
1589 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
1590 h->scsi_host->host_no,
1591 sd->bus, sd->target, sd->lun);
1592 break;
1593 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
1594 dev_info(&h->pdev->dev,
1595 "C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
1596 h->scsi_host->host_no,
1597 sd->bus, sd->target, sd->lun);
1598 break;
1599 case HPSA_LV_UNDERGOING_ENCRYPTION:
1600 dev_info(&h->pdev->dev,
1601 "C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
1602 h->scsi_host->host_no,
1603 sd->bus, sd->target, sd->lun);
1604 break;
1605 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
1606 dev_info(&h->pdev->dev,
1607 "C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
1608 h->scsi_host->host_no,
1609 sd->bus, sd->target, sd->lun);
1610 break;
1611 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1612 dev_info(&h->pdev->dev,
1613 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
1614 h->scsi_host->host_no,
1615 sd->bus, sd->target, sd->lun);
1616 break;
1617 case HPSA_LV_PENDING_ENCRYPTION:
1618 dev_info(&h->pdev->dev,
1619 "C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
1620 h->scsi_host->host_no,
1621 sd->bus, sd->target, sd->lun);
1622 break;
1623 case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
1624 dev_info(&h->pdev->dev,
1625 "C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
1626 h->scsi_host->host_no,
1627 sd->bus, sd->target, sd->lun);
1628 break;
1629 }
1630}
1631
Don Brace03383732015-01-23 16:43:30 -06001632/*
1633 * Figure the list of physical drive pointers for a logical drive with
1634 * raid offload configured.
1635 */
1636static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
1637 struct hpsa_scsi_dev_t *dev[], int ndevices,
1638 struct hpsa_scsi_dev_t *logical_drive)
1639{
1640 struct raid_map_data *map = &logical_drive->raid_map;
1641 struct raid_map_disk_data *dd = &map->data[0];
1642 int i, j;
1643 int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
1644 le16_to_cpu(map->metadata_disks_per_row);
1645 int nraid_map_entries = le16_to_cpu(map->row_cnt) *
1646 le16_to_cpu(map->layout_map_count) *
1647 total_disks_per_row;
1648 int nphys_disk = le16_to_cpu(map->layout_map_count) *
1649 total_disks_per_row;
1650 int qdepth;
1651
1652 if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
1653 nraid_map_entries = RAID_MAP_MAX_ENTRIES;
1654
Webb Scalesd604f532015-04-23 09:35:22 -05001655 logical_drive->nphysical_disks = nraid_map_entries;
1656
Don Brace03383732015-01-23 16:43:30 -06001657 qdepth = 0;
1658 for (i = 0; i < nraid_map_entries; i++) {
1659 logical_drive->phys_disk[i] = NULL;
1660 if (!logical_drive->offload_config)
1661 continue;
1662 for (j = 0; j < ndevices; j++) {
Don Brace1d33d852015-11-04 15:50:31 -06001663 if (dev[j] == NULL)
1664 continue;
Don Brace03383732015-01-23 16:43:30 -06001665 if (dev[j]->devtype != TYPE_DISK)
1666 continue;
Don Braceaf15ed32016-02-23 15:16:15 -06001667 if (dev[j]->devtype != TYPE_ZBC)
1668 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001669 if (is_logical_device(dev[j]))
Don Brace03383732015-01-23 16:43:30 -06001670 continue;
1671 if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
1672 continue;
1673
1674 logical_drive->phys_disk[i] = dev[j];
1675 if (i < nphys_disk)
1676 qdepth = min(h->nr_cmds, qdepth +
1677 logical_drive->phys_disk[i]->queue_depth);
1678 break;
1679 }
1680
1681 /*
1682 * This can happen if a physical drive is removed and
1683 * the logical drive is degraded. In that case, the RAID
1684 * map data will refer to a physical disk which isn't actually
1685 * present. And in that case offload_enabled should already
1686 * be 0, but we'll turn it off here just in case
1687 */
1688 if (!logical_drive->phys_disk[i]) {
1689 logical_drive->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001690 logical_drive->offload_to_be_enabled = 0;
1691 logical_drive->queue_depth = 8;
Don Brace03383732015-01-23 16:43:30 -06001692 }
1693 }
1694 if (nraid_map_entries)
1695 /*
1696 * This is correct for reads, too high for full stripe writes,
1697 * way too high for partial stripe writes
1698 */
1699 logical_drive->queue_depth = qdepth;
1700 else
1701 logical_drive->queue_depth = h->nr_cmds;
1702}
1703
1704static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
1705 struct hpsa_scsi_dev_t *dev[], int ndevices)
1706{
1707 int i;
1708
1709 for (i = 0; i < ndevices; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001710 if (dev[i] == NULL)
1711 continue;
Don Brace03383732015-01-23 16:43:30 -06001712 if (dev[i]->devtype != TYPE_DISK)
1713 continue;
Don Braceaf15ed32016-02-23 15:16:15 -06001714 if (dev[i]->devtype != TYPE_ZBC)
1715 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001716 if (!is_logical_device(dev[i]))
Don Brace03383732015-01-23 16:43:30 -06001717 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001718
1719 /*
1720 * If offload is currently enabled, the RAID map and
1721 * phys_disk[] assignment *better* not be changing
1722 * and since it isn't changing, we do not need to
1723 * update it.
1724 */
1725 if (dev[i]->offload_enabled)
1726 continue;
1727
Don Brace03383732015-01-23 16:43:30 -06001728 hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
1729 }
1730}
1731
Kevin Barnett096ccff2015-11-04 15:51:51 -06001732static int hpsa_add_device(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1733{
1734 int rc = 0;
1735
1736 if (!h->scsi_host)
1737 return 1;
1738
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001739 if (is_logical_device(device)) /* RAID */
1740 rc = scsi_add_device(h->scsi_host, device->bus,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001741 device->target, device->lun);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001742 else /* HBA */
1743 rc = hpsa_add_sas_device(h->sas_host, device);
1744
Kevin Barnett096ccff2015-11-04 15:51:51 -06001745 return rc;
1746}
1747
1748static void hpsa_remove_device(struct ctlr_info *h,
1749 struct hpsa_scsi_dev_t *device)
1750{
1751 struct scsi_device *sdev = NULL;
1752
1753 if (!h->scsi_host)
1754 return;
1755
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001756 if (is_logical_device(device)) { /* RAID */
1757 sdev = scsi_device_lookup(h->scsi_host, device->bus,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001758 device->target, device->lun);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001759 if (sdev) {
1760 scsi_remove_device(sdev);
1761 scsi_device_put(sdev);
1762 } else {
1763 /*
1764 * We don't expect to get here. Future commands
1765 * to this device will get a selection timeout as
1766 * if the device were gone.
1767 */
1768 hpsa_show_dev_msg(KERN_WARNING, h, device,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001769 "didn't find device for removal.");
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001770 }
1771 } else /* HBA */
1772 hpsa_remove_sas_device(device);
Kevin Barnett096ccff2015-11-04 15:51:51 -06001773}
1774
Don Brace8aa60682015-11-04 15:50:01 -06001775static void adjust_hpsa_scsi_table(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001776 struct hpsa_scsi_dev_t *sd[], int nsds)
1777{
1778 /* sd contains scsi3 addresses and devtypes, and inquiry
1779 * data. This function takes what's in sd to be the current
1780 * reality and updates h->dev[] to reflect that reality.
1781 */
1782 int i, entry, device_change, changes = 0;
1783 struct hpsa_scsi_dev_t *csd;
1784 unsigned long flags;
1785 struct hpsa_scsi_dev_t **added, **removed;
1786 int nadded, nremoved;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001787
Don Braceda03ded2015-11-04 15:50:56 -06001788 /*
1789 * A reset can cause a device status to change
1790 * re-schedule the scan to see what happened.
1791 */
1792 if (h->reset_in_progress) {
1793 h->drv_req_rescan = 1;
1794 return;
1795 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001796
Scott Teelcfe5bad2011-10-26 16:21:07 -05001797 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1798 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001799
1800 if (!added || !removed) {
1801 dev_warn(&h->pdev->dev, "out of memory in "
1802 "adjust_hpsa_scsi_table\n");
1803 goto free_and_out;
1804 }
1805
1806 spin_lock_irqsave(&h->devlock, flags);
1807
1808 /* find any devices in h->dev[] that are not in
1809 * sd[] and remove them from h->dev[], and for any
1810 * devices which have changed, remove the old device
1811 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001812 * If minor device attributes change, just update
1813 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001814 */
1815 i = 0;
1816 nremoved = 0;
1817 nadded = 0;
1818 while (i < h->ndevices) {
1819 csd = h->dev[i];
1820 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1821 if (device_change == DEVICE_NOT_FOUND) {
1822 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001823 hpsa_scsi_remove_entry(h, i, removed, &nremoved);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001824 continue; /* remove ^^^, hence i not incremented */
1825 } else if (device_change == DEVICE_CHANGED) {
1826 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001827 hpsa_scsi_replace_entry(h, i, sd[entry],
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001828 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001829 /* Set it to NULL to prevent it from being freed
1830 * at the bottom of hpsa_update_scsi_devices()
1831 */
1832 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001833 } else if (device_change == DEVICE_UPDATED) {
Don Brace8aa60682015-11-04 15:50:01 -06001834 hpsa_scsi_update_entry(h, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001835 }
1836 i++;
1837 }
1838
1839 /* Now, make sure every device listed in sd[] is also
1840 * listed in h->dev[], adding them if they aren't found
1841 */
1842
1843 for (i = 0; i < nsds; i++) {
1844 if (!sd[i]) /* if already added above. */
1845 continue;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001846
1847 /* Don't add devices which are NOT READY, FORMAT IN PROGRESS
1848 * as the SCSI mid-layer does not handle such devices well.
1849 * It relentlessly loops sending TUR at 3Hz, then READ(10)
1850 * at 160Hz, and prevents the system from coming up.
1851 */
1852 if (sd[i]->volume_offline) {
1853 hpsa_show_volume_status(h, sd[i]);
Webb Scales0d96ef52015-04-23 09:31:55 -05001854 hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
Stephen M. Cameron98465902014-02-21 16:25:00 -06001855 continue;
1856 }
1857
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001858 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1859 h->ndevices, &entry);
1860 if (device_change == DEVICE_NOT_FOUND) {
1861 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001862 if (hpsa_scsi_add_entry(h, sd[i], added, &nadded) != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001863 break;
1864 sd[i] = NULL; /* prevent from being freed later. */
1865 } else if (device_change == DEVICE_CHANGED) {
1866 /* should never happen... */
1867 changes++;
1868 dev_warn(&h->pdev->dev,
1869 "device unexpectedly changed.\n");
1870 /* but if it does happen, we just ignore that device */
1871 }
1872 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001873 hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
1874
1875 /* Now that h->dev[]->phys_disk[] is coherent, we can enable
1876 * any logical drives that need it enabled.
1877 */
Don Brace1d33d852015-11-04 15:50:31 -06001878 for (i = 0; i < h->ndevices; i++) {
1879 if (h->dev[i] == NULL)
1880 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001881 h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
Don Brace1d33d852015-11-04 15:50:31 -06001882 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001883
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001884 spin_unlock_irqrestore(&h->devlock, flags);
1885
Stephen M. Cameron98465902014-02-21 16:25:00 -06001886 /* Monitor devices which are in one of several NOT READY states to be
1887 * brought online later. This must be done without holding h->devlock,
1888 * so don't touch h->dev[]
1889 */
1890 for (i = 0; i < nsds; i++) {
1891 if (!sd[i]) /* if already added above. */
1892 continue;
1893 if (sd[i]->volume_offline)
1894 hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
1895 }
1896
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001897 /* Don't notify scsi mid layer of any changes the first time through
1898 * (or if there are no changes) scsi_scan_host will do it later the
1899 * first time through.
1900 */
Don Brace8aa60682015-11-04 15:50:01 -06001901 if (!changes)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001902 goto free_and_out;
1903
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001904 /* Notify scsi mid layer of any removed devices */
1905 for (i = 0; i < nremoved; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001906 if (removed[i] == NULL)
1907 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001908 if (removed[i]->expose_device)
1909 hpsa_remove_device(h, removed[i]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001910 kfree(removed[i]);
1911 removed[i] = NULL;
1912 }
1913
1914 /* Notify scsi mid layer of any added devices */
1915 for (i = 0; i < nadded; i++) {
Kevin Barnett096ccff2015-11-04 15:51:51 -06001916 int rc = 0;
1917
Don Brace1d33d852015-11-04 15:50:31 -06001918 if (added[i] == NULL)
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001919 continue;
Kevin Barnett2a168202015-11-04 15:51:21 -06001920 if (!(added[i]->expose_device))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001921 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001922 rc = hpsa_add_device(h, added[i]);
1923 if (!rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001924 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001925 dev_warn(&h->pdev->dev,
1926 "addition failed %d, device not added.", rc);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001927 /* now we have to remove it from h->dev,
1928 * since it didn't get added to scsi mid layer
1929 */
1930 fixup_botched_add(h, added[i]);
Don Brace853633e2015-11-04 15:50:37 -06001931 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001932 }
1933
1934free_and_out:
1935 kfree(added);
1936 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001937}
1938
1939/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001940 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001941 * Assume's h->devlock is held.
1942 */
1943static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1944 int bus, int target, int lun)
1945{
1946 int i;
1947 struct hpsa_scsi_dev_t *sd;
1948
1949 for (i = 0; i < h->ndevices; i++) {
1950 sd = h->dev[i];
1951 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1952 return sd;
1953 }
1954 return NULL;
1955}
1956
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001957static int hpsa_slave_alloc(struct scsi_device *sdev)
1958{
1959 struct hpsa_scsi_dev_t *sd;
1960 unsigned long flags;
1961 struct ctlr_info *h;
1962
1963 h = sdev_to_hba(sdev);
1964 spin_lock_irqsave(&h->devlock, flags);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001965 if (sdev_channel(sdev) == HPSA_PHYSICAL_DEVICE_BUS) {
1966 struct scsi_target *starget;
1967 struct sas_rphy *rphy;
1968
1969 starget = scsi_target(sdev);
1970 rphy = target_to_rphy(starget);
1971 sd = hpsa_find_device_by_sas_rphy(h, rphy);
1972 if (sd) {
1973 sd->target = sdev_id(sdev);
1974 sd->lun = sdev->lun;
1975 }
1976 } else
1977 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1978 sdev_id(sdev), sdev->lun);
1979
1980 if (sd && sd->expose_device) {
Don Brace03383732015-01-23 16:43:30 -06001981 atomic_set(&sd->ioaccel_cmds_out, 0);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001982 sdev->hostdata = sd;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001983 } else
1984 sdev->hostdata = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001985 spin_unlock_irqrestore(&h->devlock, flags);
1986 return 0;
1987}
1988
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001989/* configure scsi device based on internal per-device structure */
1990static int hpsa_slave_configure(struct scsi_device *sdev)
1991{
1992 struct hpsa_scsi_dev_t *sd;
1993 int queue_depth;
1994
1995 sd = sdev->hostdata;
Kevin Barnett2a168202015-11-04 15:51:21 -06001996 sdev->no_uld_attach = !sd || !sd->expose_device;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001997
1998 if (sd)
1999 queue_depth = sd->queue_depth != 0 ?
2000 sd->queue_depth : sdev->host->can_queue;
2001 else
2002 queue_depth = sdev->host->can_queue;
2003
2004 scsi_change_queue_depth(sdev, queue_depth);
2005
2006 return 0;
2007}
2008
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002009static void hpsa_slave_destroy(struct scsi_device *sdev)
2010{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06002011 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002012}
2013
Webb Scalesd9a729f2015-04-23 09:33:27 -05002014static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
2015{
2016 int i;
2017
2018 if (!h->ioaccel2_cmd_sg_list)
2019 return;
2020 for (i = 0; i < h->nr_cmds; i++) {
2021 kfree(h->ioaccel2_cmd_sg_list[i]);
2022 h->ioaccel2_cmd_sg_list[i] = NULL;
2023 }
2024 kfree(h->ioaccel2_cmd_sg_list);
2025 h->ioaccel2_cmd_sg_list = NULL;
2026}
2027
2028static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
2029{
2030 int i;
2031
2032 if (h->chainsize <= 0)
2033 return 0;
2034
2035 h->ioaccel2_cmd_sg_list =
2036 kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
2037 GFP_KERNEL);
2038 if (!h->ioaccel2_cmd_sg_list)
2039 return -ENOMEM;
2040 for (i = 0; i < h->nr_cmds; i++) {
2041 h->ioaccel2_cmd_sg_list[i] =
2042 kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
2043 h->maxsgentries, GFP_KERNEL);
2044 if (!h->ioaccel2_cmd_sg_list[i])
2045 goto clean;
2046 }
2047 return 0;
2048
2049clean:
2050 hpsa_free_ioaccel2_sg_chain_blocks(h);
2051 return -ENOMEM;
2052}
2053
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002054static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
2055{
2056 int i;
2057
2058 if (!h->cmd_sg_list)
2059 return;
2060 for (i = 0; i < h->nr_cmds; i++) {
2061 kfree(h->cmd_sg_list[i]);
2062 h->cmd_sg_list[i] = NULL;
2063 }
2064 kfree(h->cmd_sg_list);
2065 h->cmd_sg_list = NULL;
2066}
2067
Robert Elliott105a3db2015-04-23 09:33:48 -05002068static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002069{
2070 int i;
2071
2072 if (h->chainsize <= 0)
2073 return 0;
2074
2075 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
2076 GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002077 if (!h->cmd_sg_list) {
2078 dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002079 return -ENOMEM;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002080 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002081 for (i = 0; i < h->nr_cmds; i++) {
2082 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
2083 h->chainsize, GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002084 if (!h->cmd_sg_list[i]) {
2085 dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002086 goto clean;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002087 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002088 }
2089 return 0;
2090
2091clean:
2092 hpsa_free_sg_chain_blocks(h);
2093 return -ENOMEM;
2094}
2095
Webb Scalesd9a729f2015-04-23 09:33:27 -05002096static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
2097 struct io_accel2_cmd *cp, struct CommandList *c)
2098{
2099 struct ioaccel2_sg_element *chain_block;
2100 u64 temp64;
2101 u32 chain_size;
2102
2103 chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
Don Bracea736e9b2015-11-04 15:51:14 -06002104 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002105 temp64 = pci_map_single(h->pdev, chain_block, chain_size,
2106 PCI_DMA_TODEVICE);
2107 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2108 /* prevent subsequent unmapping */
2109 cp->sg->address = 0;
2110 return -1;
2111 }
2112 cp->sg->address = cpu_to_le64(temp64);
2113 return 0;
2114}
2115
2116static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
2117 struct io_accel2_cmd *cp)
2118{
2119 struct ioaccel2_sg_element *chain_sg;
2120 u64 temp64;
2121 u32 chain_size;
2122
2123 chain_sg = cp->sg;
2124 temp64 = le64_to_cpu(chain_sg->address);
Don Bracea736e9b2015-11-04 15:51:14 -06002125 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002126 pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
2127}
2128
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002129static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002130 struct CommandList *c)
2131{
2132 struct SGDescriptor *chain_sg, *chain_block;
2133 u64 temp64;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002134 u32 chain_len;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002135
2136 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
2137 chain_block = h->cmd_sg_list[c->cmdindex];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002138 chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
2139 chain_len = sizeof(*chain_sg) *
Don Brace2b08b3e2015-01-23 16:41:09 -06002140 (le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002141 chain_sg->Len = cpu_to_le32(chain_len);
2142 temp64 = pci_map_single(h->pdev, chain_block, chain_len,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002143 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002144 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2145 /* prevent subsequent unmapping */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002146 chain_sg->Addr = cpu_to_le64(0);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002147 return -1;
2148 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002149 chain_sg->Addr = cpu_to_le64(temp64);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002150 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002151}
2152
2153static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
2154 struct CommandList *c)
2155{
2156 struct SGDescriptor *chain_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002157
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002158 if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002159 return;
2160
2161 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002162 pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
2163 le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002164}
2165
Scott Teela09c1442014-02-18 13:57:21 -06002166
2167/* Decode the various types of errors on ioaccel2 path.
2168 * Return 1 for any error that should generate a RAID path retry.
2169 * Return 0 for errors that don't require a RAID path retry.
2170 */
2171static int handle_ioaccel_mode2_error(struct ctlr_info *h,
Scott Teelc3497752014-02-18 13:56:34 -06002172 struct CommandList *c,
2173 struct scsi_cmnd *cmd,
2174 struct io_accel2_cmd *c2)
2175{
2176 int data_len;
Scott Teela09c1442014-02-18 13:57:21 -06002177 int retry = 0;
Joe Handzikc40820d2015-04-23 09:33:32 -05002178 u32 ioaccel2_resid = 0;
Scott Teelc3497752014-02-18 13:56:34 -06002179
2180 switch (c2->error_data.serv_response) {
2181 case IOACCEL2_SERV_RESPONSE_COMPLETE:
2182 switch (c2->error_data.status) {
2183 case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
2184 break;
2185 case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002186 cmd->result |= SAM_STAT_CHECK_CONDITION;
Scott Teelc3497752014-02-18 13:56:34 -06002187 if (c2->error_data.data_present !=
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002188 IOACCEL2_SENSE_DATA_PRESENT) {
2189 memset(cmd->sense_buffer, 0,
2190 SCSI_SENSE_BUFFERSIZE);
Scott Teelc3497752014-02-18 13:56:34 -06002191 break;
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002192 }
Scott Teelc3497752014-02-18 13:56:34 -06002193 /* copy the sense data */
2194 data_len = c2->error_data.sense_data_len;
2195 if (data_len > SCSI_SENSE_BUFFERSIZE)
2196 data_len = SCSI_SENSE_BUFFERSIZE;
2197 if (data_len > sizeof(c2->error_data.sense_data_buff))
2198 data_len =
2199 sizeof(c2->error_data.sense_data_buff);
2200 memcpy(cmd->sense_buffer,
2201 c2->error_data.sense_data_buff, data_len);
Scott Teela09c1442014-02-18 13:57:21 -06002202 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002203 break;
2204 case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
Scott Teela09c1442014-02-18 13:57:21 -06002205 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002206 break;
2207 case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
Scott Teela09c1442014-02-18 13:57:21 -06002208 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002209 break;
2210 case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
Stephen Cameron4a8da222015-04-23 09:32:43 -05002211 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002212 break;
2213 case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
Scott Teela09c1442014-02-18 13:57:21 -06002214 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002215 break;
2216 default:
Scott Teela09c1442014-02-18 13:57:21 -06002217 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002218 break;
2219 }
2220 break;
2221 case IOACCEL2_SERV_RESPONSE_FAILURE:
Joe Handzikc40820d2015-04-23 09:33:32 -05002222 switch (c2->error_data.status) {
2223 case IOACCEL2_STATUS_SR_IO_ERROR:
2224 case IOACCEL2_STATUS_SR_IO_ABORTED:
2225 case IOACCEL2_STATUS_SR_OVERRUN:
2226 retry = 1;
2227 break;
2228 case IOACCEL2_STATUS_SR_UNDERRUN:
2229 cmd->result = (DID_OK << 16); /* host byte */
2230 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
2231 ioaccel2_resid = get_unaligned_le32(
2232 &c2->error_data.resid_cnt[0]);
2233 scsi_set_resid(cmd, ioaccel2_resid);
2234 break;
2235 case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
2236 case IOACCEL2_STATUS_SR_INVALID_DEVICE:
2237 case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
2238 /* We will get an event from ctlr to trigger rescan */
2239 retry = 1;
2240 break;
2241 default:
2242 retry = 1;
Joe Handzikc40820d2015-04-23 09:33:32 -05002243 }
Scott Teelc3497752014-02-18 13:56:34 -06002244 break;
2245 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
2246 break;
2247 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
2248 break;
2249 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
Scott Teela09c1442014-02-18 13:57:21 -06002250 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002251 break;
2252 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
Scott Teelc3497752014-02-18 13:56:34 -06002253 break;
2254 default:
Scott Teela09c1442014-02-18 13:57:21 -06002255 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002256 break;
2257 }
Scott Teela09c1442014-02-18 13:57:21 -06002258
2259 return retry; /* retry on raid path? */
Scott Teelc3497752014-02-18 13:56:34 -06002260}
2261
Webb Scalesa58e7e52015-04-23 09:34:16 -05002262static void hpsa_cmd_resolve_events(struct ctlr_info *h,
2263 struct CommandList *c)
2264{
Webb Scalesd604f532015-04-23 09:35:22 -05002265 bool do_wake = false;
2266
Webb Scalesa58e7e52015-04-23 09:34:16 -05002267 /*
2268 * Prevent the following race in the abort handler:
2269 *
2270 * 1. LLD is requested to abort a SCSI command
2271 * 2. The SCSI command completes
2272 * 3. The struct CommandList associated with step 2 is made available
2273 * 4. New I/O request to LLD to another LUN re-uses struct CommandList
2274 * 5. Abort handler follows scsi_cmnd->host_scribble and
2275 * finds struct CommandList and tries to aborts it
2276 * Now we have aborted the wrong command.
2277 *
Webb Scalesd604f532015-04-23 09:35:22 -05002278 * Reset c->scsi_cmd here so that the abort or reset handler will know
2279 * this command has completed. Then, check to see if the handler is
Webb Scalesa58e7e52015-04-23 09:34:16 -05002280 * waiting for this command, and, if so, wake it.
2281 */
2282 c->scsi_cmd = SCSI_CMD_IDLE;
Webb Scalesd604f532015-04-23 09:35:22 -05002283 mb(); /* Declare command idle before checking for pending events. */
Webb Scalesa58e7e52015-04-23 09:34:16 -05002284 if (c->abort_pending) {
Webb Scalesd604f532015-04-23 09:35:22 -05002285 do_wake = true;
Webb Scalesa58e7e52015-04-23 09:34:16 -05002286 c->abort_pending = false;
Webb Scalesa58e7e52015-04-23 09:34:16 -05002287 }
Webb Scalesd604f532015-04-23 09:35:22 -05002288 if (c->reset_pending) {
2289 unsigned long flags;
2290 struct hpsa_scsi_dev_t *dev;
2291
2292 /*
2293 * There appears to be a reset pending; lock the lock and
2294 * reconfirm. If so, then decrement the count of outstanding
2295 * commands and wake the reset command if this is the last one.
2296 */
2297 spin_lock_irqsave(&h->lock, flags);
2298 dev = c->reset_pending; /* Re-fetch under the lock. */
2299 if (dev && atomic_dec_and_test(&dev->reset_cmds_out))
2300 do_wake = true;
2301 c->reset_pending = NULL;
2302 spin_unlock_irqrestore(&h->lock, flags);
2303 }
2304
2305 if (do_wake)
2306 wake_up_all(&h->event_sync_wait_queue);
Webb Scalesa58e7e52015-04-23 09:34:16 -05002307}
2308
Webb Scales73153fe2015-04-23 09:35:04 -05002309static void hpsa_cmd_resolve_and_free(struct ctlr_info *h,
2310 struct CommandList *c)
2311{
2312 hpsa_cmd_resolve_events(h, c);
2313 cmd_tagged_free(h, c);
2314}
2315
Webb Scales8a0ff922015-04-23 09:34:11 -05002316static void hpsa_cmd_free_and_done(struct ctlr_info *h,
2317 struct CommandList *c, struct scsi_cmnd *cmd)
2318{
Webb Scales73153fe2015-04-23 09:35:04 -05002319 hpsa_cmd_resolve_and_free(h, c);
Webb Scales8a0ff922015-04-23 09:34:11 -05002320 cmd->scsi_done(cmd);
2321}
2322
2323static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
2324{
2325 INIT_WORK(&c->work, hpsa_command_resubmit_worker);
2326 queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
2327}
2328
Webb Scalesa58e7e52015-04-23 09:34:16 -05002329static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
2330{
2331 cmd->result = DID_ABORT << 16;
2332}
2333
2334static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
2335 struct scsi_cmnd *cmd)
2336{
2337 hpsa_set_scsi_cmd_aborted(cmd);
2338 dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2339 c->Request.CDB, c->err_info->ScsiStatus);
Webb Scales73153fe2015-04-23 09:35:04 -05002340 hpsa_cmd_resolve_and_free(h, c);
Webb Scalesa58e7e52015-04-23 09:34:16 -05002341}
2342
Scott Teelc3497752014-02-18 13:56:34 -06002343static void process_ioaccel2_completion(struct ctlr_info *h,
2344 struct CommandList *c, struct scsi_cmnd *cmd,
2345 struct hpsa_scsi_dev_t *dev)
2346{
2347 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2348
2349 /* check for good status */
2350 if (likely(c2->error_data.serv_response == 0 &&
Webb Scales8a0ff922015-04-23 09:34:11 -05002351 c2->error_data.status == 0))
2352 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002353
Webb Scales8a0ff922015-04-23 09:34:11 -05002354 /*
2355 * Any RAID offload error results in retry which will use
Scott Teelc3497752014-02-18 13:56:34 -06002356 * the normal I/O path so the controller can handle whatever's
2357 * wrong.
2358 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002359 if (is_logical_device(dev) &&
Scott Teelc3497752014-02-18 13:56:34 -06002360 c2->error_data.serv_response ==
2361 IOACCEL2_SERV_RESPONSE_FAILURE) {
Don Brace080ef1c2015-01-23 16:43:25 -06002362 if (c2->error_data.status ==
Don Brace064d1b12016-04-27 17:14:07 -05002363 IOACCEL2_STATUS_SR_IOACCEL_DISABLED) {
Don Brace080ef1c2015-01-23 16:43:25 -06002364 dev->offload_enabled = 0;
Don Brace064d1b12016-04-27 17:14:07 -05002365 dev->offload_to_be_enabled = 0;
2366 }
Webb Scales8a0ff922015-04-23 09:34:11 -05002367
2368 return hpsa_retry_cmd(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06002369 }
Don Brace080ef1c2015-01-23 16:43:25 -06002370
2371 if (handle_ioaccel_mode2_error(h, c, cmd, c2))
Webb Scales8a0ff922015-04-23 09:34:11 -05002372 return hpsa_retry_cmd(h, c);
Don Brace080ef1c2015-01-23 16:43:25 -06002373
Webb Scales8a0ff922015-04-23 09:34:11 -05002374 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002375}
2376
Stephen Cameron9437ac42015-04-23 09:32:16 -05002377/* Returns 0 on success, < 0 otherwise. */
2378static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
2379 struct CommandList *cp)
2380{
2381 u8 tmf_status = cp->err_info->ScsiStatus;
2382
2383 switch (tmf_status) {
2384 case CISS_TMF_COMPLETE:
2385 /*
2386 * CISS_TMF_COMPLETE never happens, instead,
2387 * ei->CommandStatus == 0 for this case.
2388 */
2389 case CISS_TMF_SUCCESS:
2390 return 0;
2391 case CISS_TMF_INVALID_FRAME:
2392 case CISS_TMF_NOT_SUPPORTED:
2393 case CISS_TMF_FAILED:
2394 case CISS_TMF_WRONG_LUN:
2395 case CISS_TMF_OVERLAPPED_TAG:
2396 break;
2397 default:
2398 dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
2399 tmf_status);
2400 break;
2401 }
2402 return -tmf_status;
2403}
2404
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05002405static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002406{
2407 struct scsi_cmnd *cmd;
2408 struct ctlr_info *h;
2409 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002410 struct hpsa_scsi_dev_t *dev;
Webb Scalesd9a729f2015-04-23 09:33:27 -05002411 struct io_accel2_cmd *c2;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002412
Stephen Cameron9437ac42015-04-23 09:32:16 -05002413 u8 sense_key;
2414 u8 asc; /* additional sense code */
2415 u8 ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05002416 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002417
2418 ei = cp->err_info;
Stephen Cameron7fa30302015-01-23 16:44:30 -06002419 cmd = cp->scsi_cmd;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002420 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002421 dev = cmd->device->hostdata;
Webb Scalesd9a729f2015-04-23 09:33:27 -05002422 c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002423
2424 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06002425 if ((cp->cmd_type == CMD_SCSI) &&
Don Brace2b08b3e2015-01-23 16:41:09 -06002426 (le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002427 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002428
Webb Scalesd9a729f2015-04-23 09:33:27 -05002429 if ((cp->cmd_type == CMD_IOACCEL2) &&
2430 (c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2431 hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2432
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002433 cmd->result = (DID_OK << 16); /* host byte */
2434 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06002435
Don Brace03383732015-01-23 16:43:30 -06002436 if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
2437 atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
2438
Webb Scales25163bd2015-04-23 09:32:00 -05002439 /*
2440 * We check for lockup status here as it may be set for
2441 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
2442 * fail_all_oustanding_cmds()
2443 */
2444 if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
2445 /* DID_NO_CONNECT will prevent a retry */
2446 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05002447 return hpsa_cmd_free_and_done(h, cp, cmd);
Webb Scales25163bd2015-04-23 09:32:00 -05002448 }
2449
Webb Scalesd604f532015-04-23 09:35:22 -05002450 if ((unlikely(hpsa_is_pending_event(cp)))) {
2451 if (cp->reset_pending)
2452 return hpsa_cmd_resolve_and_free(h, cp);
2453 if (cp->abort_pending)
2454 return hpsa_cmd_abort_and_free(h, cp, cmd);
2455 }
2456
Scott Teelc3497752014-02-18 13:56:34 -06002457 if (cp->cmd_type == CMD_IOACCEL2)
2458 return process_ioaccel2_completion(h, cp, cmd, dev);
2459
Robert Elliott6aa4c362014-07-03 10:18:19 -05002460 scsi_set_resid(cmd, ei->ResidualCnt);
Webb Scales8a0ff922015-04-23 09:34:11 -05002461 if (ei->CommandStatus == 0)
2462 return hpsa_cmd_free_and_done(h, cp, cmd);
Robert Elliott6aa4c362014-07-03 10:18:19 -05002463
Matt Gatese1f7de02014-02-18 13:55:17 -06002464 /* For I/O accelerator commands, copy over some fields to the normal
2465 * CISS header used below for error handling.
2466 */
2467 if (cp->cmd_type == CMD_IOACCEL1) {
2468 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06002469 cp->Header.SGList = scsi_sg_count(cmd);
2470 cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
2471 cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
2472 IOACCEL1_IOFLAGS_CDBLEN_MASK;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002473 cp->Header.tag = c->tag;
Matt Gatese1f7de02014-02-18 13:55:17 -06002474 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2475 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002476
2477 /* Any RAID offload error results in retry which will use
2478 * the normal I/O path so the controller can handle whatever's
2479 * wrong.
2480 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002481 if (is_logical_device(dev)) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002482 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2483 dev->offload_enabled = 0;
Webb Scalesd604f532015-04-23 09:35:22 -05002484 return hpsa_retry_cmd(h, cp);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002485 }
Matt Gatese1f7de02014-02-18 13:55:17 -06002486 }
2487
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002488 /* an error has occurred */
2489 switch (ei->CommandStatus) {
2490
2491 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002492 cmd->result |= ei->ScsiStatus;
2493 /* copy the sense data */
2494 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
2495 sense_data_size = SCSI_SENSE_BUFFERSIZE;
2496 else
2497 sense_data_size = sizeof(ei->SenseInfo);
2498 if (ei->SenseLen < sense_data_size)
2499 sense_data_size = ei->SenseLen;
2500 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
2501 if (ei->ScsiStatus)
2502 decode_sense_data(ei->SenseInfo, sense_data_size,
2503 &sense_key, &asc, &ascq);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002504 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates1d3b3602010-02-04 08:43:00 -06002505 if (sense_key == ABORTED_COMMAND) {
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05002506 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06002507 break;
2508 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002509 break;
2510 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002511 /* Problem was not a check condition
2512 * Pass it up to the upper layers...
2513 */
2514 if (ei->ScsiStatus) {
2515 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2516 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2517 "Returning result: 0x%x\n",
2518 cp, ei->ScsiStatus,
2519 sense_key, asc, ascq,
2520 cmd->result);
2521 } else { /* scsi status is zero??? How??? */
2522 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2523 "Returning no connection.\n", cp),
2524
2525 /* Ordinarily, this case should never happen,
2526 * but there is a bug in some released firmware
2527 * revisions that allows it to happen if, for
2528 * example, a 4100 backplane loses power and
2529 * the tape drive is in it. We assume that
2530 * it's a fatal error of some kind because we
2531 * can't show that it wasn't. We will make it
2532 * look like selection timeout since that is
2533 * the most common reason for this to occur,
2534 * and it's severe enough.
2535 */
2536
2537 cmd->result = DID_NO_CONNECT << 16;
2538 }
2539 break;
2540
2541 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2542 break;
2543 case CMD_DATA_OVERRUN:
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002544 dev_warn(&h->pdev->dev,
2545 "CDB %16phN data overrun\n", cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002546 break;
2547 case CMD_INVALID: {
2548 /* print_bytes(cp, sizeof(*cp), 1, 0);
2549 print_cmd(cp); */
2550 /* We get CMD_INVALID if you address a non-existent device
2551 * instead of a selection timeout (no response). You will
2552 * see this if you yank out a drive, then try to access it.
2553 * This is kind of a shame because it means that any other
2554 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2555 * missing target. */
2556 cmd->result = DID_NO_CONNECT << 16;
2557 }
2558 break;
2559 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05002560 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002561 dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2562 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002563 break;
2564 case CMD_HARDWARE_ERR:
2565 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002566 dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2567 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002568 break;
2569 case CMD_CONNECTION_LOST:
2570 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002571 dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2572 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002573 break;
2574 case CMD_ABORTED:
Webb Scalesa58e7e52015-04-23 09:34:16 -05002575 /* Return now to avoid calling scsi_done(). */
2576 return hpsa_cmd_abort_and_free(h, cp, cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002577 case CMD_ABORT_FAILED:
2578 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002579 dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2580 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002581 break;
2582 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05002583 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002584 dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2585 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002586 break;
2587 case CMD_TIMEOUT:
2588 cmd->result = DID_TIME_OUT << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002589 dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2590 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002591 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002592 case CMD_UNABORTABLE:
2593 cmd->result = DID_ERROR << 16;
2594 dev_warn(&h->pdev->dev, "Command unabortable\n");
2595 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002596 case CMD_TMF_STATUS:
2597 if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
2598 cmd->result = DID_ERROR << 16;
2599 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002600 case CMD_IOACCEL_DISABLED:
2601 /* This only handles the direct pass-through case since RAID
2602 * offload is handled above. Just attempt a retry.
2603 */
2604 cmd->result = DID_SOFT_ERROR << 16;
2605 dev_warn(&h->pdev->dev,
2606 "cp %p had HP SSD Smart Path error\n", cp);
2607 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002608 default:
2609 cmd->result = DID_ERROR << 16;
2610 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2611 cp, ei->CommandStatus);
2612 }
Webb Scales8a0ff922015-04-23 09:34:11 -05002613
2614 return hpsa_cmd_free_and_done(h, cp, cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002615}
2616
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002617static void hpsa_pci_unmap(struct pci_dev *pdev,
2618 struct CommandList *c, int sg_used, int data_direction)
2619{
2620 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002621
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002622 for (i = 0; i < sg_used; i++)
2623 pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
2624 le32_to_cpu(c->SG[i].Len),
2625 data_direction);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002626}
2627
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002628static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002629 struct CommandList *cp,
2630 unsigned char *buf,
2631 size_t buflen,
2632 int data_direction)
2633{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002634 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002635
2636 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2637 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002638 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002639 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002640 }
2641
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002642 addr64 = pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06002643 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002644 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06002645 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002646 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002647 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06002648 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002649 cp->SG[0].Addr = cpu_to_le64(addr64);
2650 cp->SG[0].Len = cpu_to_le32(buflen);
2651 cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
2652 cp->Header.SGList = 1; /* no. SGs contig in this cmd */
2653 cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002654 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002655}
2656
Webb Scales25163bd2015-04-23 09:32:00 -05002657#define NO_TIMEOUT ((unsigned long) -1)
2658#define DEFAULT_TIMEOUT 30000 /* milliseconds */
2659static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
2660 struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002661{
2662 DECLARE_COMPLETION_ONSTACK(wait);
2663
2664 c->waiting = &wait;
Webb Scales25163bd2015-04-23 09:32:00 -05002665 __enqueue_cmd_and_start_io(h, c, reply_queue);
2666 if (timeout_msecs == NO_TIMEOUT) {
2667 /* TODO: get rid of this no-timeout thing */
2668 wait_for_completion_io(&wait);
2669 return IO_OK;
2670 }
2671 if (!wait_for_completion_io_timeout(&wait,
2672 msecs_to_jiffies(timeout_msecs))) {
2673 dev_warn(&h->pdev->dev, "Command timed out.\n");
2674 return -ETIMEDOUT;
2675 }
2676 return IO_OK;
2677}
2678
2679static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
2680 int reply_queue, unsigned long timeout_msecs)
2681{
2682 if (unlikely(lockup_detected(h))) {
2683 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
2684 return IO_OK;
2685 }
2686 return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002687}
2688
Stephen M. Cameron094963d2014-05-29 10:53:18 -05002689static u32 lockup_detected(struct ctlr_info *h)
2690{
2691 int cpu;
2692 u32 rc, *lockup_detected;
2693
2694 cpu = get_cpu();
2695 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2696 rc = *lockup_detected;
2697 put_cpu();
2698 return rc;
2699}
2700
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002701#define MAX_DRIVER_CMD_RETRIES 25
Webb Scales25163bd2015-04-23 09:32:00 -05002702static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
2703 struct CommandList *c, int data_direction, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002704{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002705 int backoff_time = 10, retry_count = 0;
Webb Scales25163bd2015-04-23 09:32:00 -05002706 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002707
2708 do {
Joe Perches7630abd2011-05-08 23:32:40 -07002709 memset(c->err_info, 0, sizeof(*c->err_info));
Webb Scales25163bd2015-04-23 09:32:00 -05002710 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
2711 timeout_msecs);
2712 if (rc)
2713 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002714 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002715 if (retry_count > 3) {
2716 msleep(backoff_time);
2717 if (backoff_time < 1000)
2718 backoff_time *= 2;
2719 }
Matt Bondurant852af202012-05-01 11:42:35 -05002720 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002721 check_for_busy(h, c)) &&
2722 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002723 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
Webb Scales25163bd2015-04-23 09:32:00 -05002724 if (retry_count > MAX_DRIVER_CMD_RETRIES)
2725 rc = -EIO;
2726 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002727}
2728
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002729static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2730 struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002731{
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002732 const u8 *cdb = c->Request.CDB;
2733 const u8 *lun = c->Header.LUN.LunAddrBytes;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002734
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002735 dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2736 " CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2737 txt, lun[0], lun[1], lun[2], lun[3],
2738 lun[4], lun[5], lun[6], lun[7],
2739 cdb[0], cdb[1], cdb[2], cdb[3],
2740 cdb[4], cdb[5], cdb[6], cdb[7],
2741 cdb[8], cdb[9], cdb[10], cdb[11],
2742 cdb[12], cdb[13], cdb[14], cdb[15]);
2743}
2744
2745static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2746 struct CommandList *cp)
2747{
2748 const struct ErrorInfo *ei = cp->err_info;
2749 struct device *d = &cp->h->pdev->dev;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002750 u8 sense_key, asc, ascq;
2751 int sense_len;
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002752
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002753 switch (ei->CommandStatus) {
2754 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002755 if (ei->SenseLen > sizeof(ei->SenseInfo))
2756 sense_len = sizeof(ei->SenseInfo);
2757 else
2758 sense_len = ei->SenseLen;
2759 decode_sense_data(ei->SenseInfo, sense_len,
2760 &sense_key, &asc, &ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002761 hpsa_print_cmd(h, "SCSI status", cp);
2762 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
Stephen Cameron9437ac42015-04-23 09:32:16 -05002763 dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
2764 sense_key, asc, ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002765 else
Stephen Cameron9437ac42015-04-23 09:32:16 -05002766 dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002767 if (ei->ScsiStatus == 0)
2768 dev_warn(d, "SCSI status is abnormally zero. "
2769 "(probably indicates selection timeout "
2770 "reported incorrectly due to a known "
2771 "firmware bug, circa July, 2001.)\n");
2772 break;
2773 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002774 break;
2775 case CMD_DATA_OVERRUN:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002776 hpsa_print_cmd(h, "overrun condition", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002777 break;
2778 case CMD_INVALID: {
2779 /* controller unfortunately reports SCSI passthru's
2780 * to non-existent targets as invalid commands.
2781 */
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002782 hpsa_print_cmd(h, "invalid command", cp);
2783 dev_warn(d, "probably means device no longer present\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002784 }
2785 break;
2786 case CMD_PROTOCOL_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002787 hpsa_print_cmd(h, "protocol error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002788 break;
2789 case CMD_HARDWARE_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002790 hpsa_print_cmd(h, "hardware error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002791 break;
2792 case CMD_CONNECTION_LOST:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002793 hpsa_print_cmd(h, "connection lost", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002794 break;
2795 case CMD_ABORTED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002796 hpsa_print_cmd(h, "aborted", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002797 break;
2798 case CMD_ABORT_FAILED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002799 hpsa_print_cmd(h, "abort failed", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002800 break;
2801 case CMD_UNSOLICITED_ABORT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002802 hpsa_print_cmd(h, "unsolicited abort", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002803 break;
2804 case CMD_TIMEOUT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002805 hpsa_print_cmd(h, "timed out", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002806 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002807 case CMD_UNABORTABLE:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002808 hpsa_print_cmd(h, "unabortable", cp);
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002809 break;
Webb Scales25163bd2015-04-23 09:32:00 -05002810 case CMD_CTLR_LOCKUP:
2811 hpsa_print_cmd(h, "controller lockup detected", cp);
2812 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002813 default:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002814 hpsa_print_cmd(h, "unknown status", cp);
2815 dev_warn(d, "Unknown command status %x\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002816 ei->CommandStatus);
2817 }
2818}
2819
2820static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002821 u16 page, unsigned char *buf,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002822 unsigned char bufsize)
2823{
2824 int rc = IO_OK;
2825 struct CommandList *c;
2826 struct ErrorInfo *ei;
2827
Stephen Cameron45fcb862015-01-23 16:43:04 -06002828 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002829
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002830 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2831 page, scsi3addr, TYPE_CMD)) {
2832 rc = -1;
2833 goto out;
2834 }
Webb Scales25163bd2015-04-23 09:32:00 -05002835 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05002836 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05002837 if (rc)
2838 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002839 ei = c->err_info;
2840 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002841 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002842 rc = -1;
2843 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002844out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002845 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002846 return rc;
2847}
2848
Scott Teelbf711ac2014-02-18 13:56:39 -06002849static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Webb Scales25163bd2015-04-23 09:32:00 -05002850 u8 reset_type, int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002851{
2852 int rc = IO_OK;
2853 struct CommandList *c;
2854 struct ErrorInfo *ei;
2855
Stephen Cameron45fcb862015-01-23 16:43:04 -06002856 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002857
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002858
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002859 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teel0b9b7b62015-11-04 15:51:02 -06002860 (void) fill_cmd(c, reset_type, h, NULL, 0, 0,
Scott Teelbf711ac2014-02-18 13:56:39 -06002861 scsi3addr, TYPE_MSG);
Don Bracec448ecf2016-04-27 17:13:51 -05002862 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05002863 if (rc) {
2864 dev_warn(&h->pdev->dev, "Failed to send reset command\n");
2865 goto out;
2866 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002867 /* no unmap needed here because no data xfer. */
2868
2869 ei = c->err_info;
2870 if (ei->CommandStatus != 0) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002871 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002872 rc = -1;
2873 }
Webb Scales25163bd2015-04-23 09:32:00 -05002874out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002875 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002876 return rc;
2877}
2878
Webb Scalesd604f532015-04-23 09:35:22 -05002879static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
2880 struct hpsa_scsi_dev_t *dev,
2881 unsigned char *scsi3addr)
2882{
2883 int i;
2884 bool match = false;
2885 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2886 struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
2887
2888 if (hpsa_is_cmd_idle(c))
2889 return false;
2890
2891 switch (c->cmd_type) {
2892 case CMD_SCSI:
2893 case CMD_IOCTL_PEND:
2894 match = !memcmp(scsi3addr, &c->Header.LUN.LunAddrBytes,
2895 sizeof(c->Header.LUN.LunAddrBytes));
2896 break;
2897
2898 case CMD_IOACCEL1:
2899 case CMD_IOACCEL2:
2900 if (c->phys_disk == dev) {
2901 /* HBA mode match */
2902 match = true;
2903 } else {
2904 /* Possible RAID mode -- check each phys dev. */
2905 /* FIXME: Do we need to take out a lock here? If
2906 * so, we could just call hpsa_get_pdisk_of_ioaccel2()
2907 * instead. */
2908 for (i = 0; i < dev->nphysical_disks && !match; i++) {
2909 /* FIXME: an alternate test might be
2910 *
2911 * match = dev->phys_disk[i]->ioaccel_handle
2912 * == c2->scsi_nexus; */
2913 match = dev->phys_disk[i] == c->phys_disk;
2914 }
2915 }
2916 break;
2917
2918 case IOACCEL2_TMF:
2919 for (i = 0; i < dev->nphysical_disks && !match; i++) {
2920 match = dev->phys_disk[i]->ioaccel_handle ==
2921 le32_to_cpu(ac->it_nexus);
2922 }
2923 break;
2924
2925 case 0: /* The command is in the middle of being initialized. */
2926 match = false;
2927 break;
2928
2929 default:
2930 dev_err(&h->pdev->dev, "unexpected cmd_type: %d\n",
2931 c->cmd_type);
2932 BUG();
2933 }
2934
2935 return match;
2936}
2937
2938static int hpsa_do_reset(struct ctlr_info *h, struct hpsa_scsi_dev_t *dev,
2939 unsigned char *scsi3addr, u8 reset_type, int reply_queue)
2940{
2941 int i;
2942 int rc = 0;
2943
2944 /* We can really only handle one reset at a time */
2945 if (mutex_lock_interruptible(&h->reset_mutex) == -EINTR) {
2946 dev_warn(&h->pdev->dev, "concurrent reset wait interrupted.\n");
2947 return -EINTR;
2948 }
2949
2950 BUG_ON(atomic_read(&dev->reset_cmds_out) != 0);
2951
2952 for (i = 0; i < h->nr_cmds; i++) {
2953 struct CommandList *c = h->cmd_pool + i;
2954 int refcount = atomic_inc_return(&c->refcount);
2955
2956 if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev, scsi3addr)) {
2957 unsigned long flags;
2958
2959 /*
2960 * Mark the target command as having a reset pending,
2961 * then lock a lock so that the command cannot complete
2962 * while we're considering it. If the command is not
2963 * idle then count it; otherwise revoke the event.
2964 */
2965 c->reset_pending = dev;
2966 spin_lock_irqsave(&h->lock, flags); /* Implied MB */
2967 if (!hpsa_is_cmd_idle(c))
2968 atomic_inc(&dev->reset_cmds_out);
2969 else
2970 c->reset_pending = NULL;
2971 spin_unlock_irqrestore(&h->lock, flags);
2972 }
2973
2974 cmd_free(h, c);
2975 }
2976
2977 rc = hpsa_send_reset(h, scsi3addr, reset_type, reply_queue);
2978 if (!rc)
2979 wait_event(h->event_sync_wait_queue,
2980 atomic_read(&dev->reset_cmds_out) == 0 ||
2981 lockup_detected(h));
2982
2983 if (unlikely(lockup_detected(h))) {
Don Brace77678d32015-07-18 11:12:22 -05002984 dev_warn(&h->pdev->dev,
2985 "Controller lockup detected during reset wait\n");
2986 rc = -ENODEV;
2987 }
Webb Scalesd604f532015-04-23 09:35:22 -05002988
2989 if (unlikely(rc))
2990 atomic_set(&dev->reset_cmds_out, 0);
2991
2992 mutex_unlock(&h->reset_mutex);
2993 return rc;
2994}
2995
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002996static void hpsa_get_raid_level(struct ctlr_info *h,
2997 unsigned char *scsi3addr, unsigned char *raid_level)
2998{
2999 int rc;
3000 unsigned char *buf;
3001
3002 *raid_level = RAID_UNKNOWN;
3003 buf = kzalloc(64, GFP_KERNEL);
3004 if (!buf)
3005 return;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003006 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003007 if (rc == 0)
3008 *raid_level = buf[8];
3009 if (*raid_level > RAID_UNKNOWN)
3010 *raid_level = RAID_UNKNOWN;
3011 kfree(buf);
3012 return;
3013}
3014
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003015#define HPSA_MAP_DEBUG
3016#ifdef HPSA_MAP_DEBUG
3017static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
3018 struct raid_map_data *map_buff)
3019{
3020 struct raid_map_disk_data *dd = &map_buff->data[0];
3021 int map, row, col;
3022 u16 map_cnt, row_cnt, disks_per_row;
3023
3024 if (rc != 0)
3025 return;
3026
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06003027 /* Show details only if debugging has been activated. */
3028 if (h->raid_offload_debug < 2)
3029 return;
3030
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003031 dev_info(&h->pdev->dev, "structure_size = %u\n",
3032 le32_to_cpu(map_buff->structure_size));
3033 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
3034 le32_to_cpu(map_buff->volume_blk_size));
3035 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
3036 le64_to_cpu(map_buff->volume_blk_cnt));
3037 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
3038 map_buff->phys_blk_shift);
3039 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
3040 map_buff->parity_rotation_shift);
3041 dev_info(&h->pdev->dev, "strip_size = %u\n",
3042 le16_to_cpu(map_buff->strip_size));
3043 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
3044 le64_to_cpu(map_buff->disk_starting_blk));
3045 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
3046 le64_to_cpu(map_buff->disk_blk_cnt));
3047 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
3048 le16_to_cpu(map_buff->data_disks_per_row));
3049 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
3050 le16_to_cpu(map_buff->metadata_disks_per_row));
3051 dev_info(&h->pdev->dev, "row_cnt = %u\n",
3052 le16_to_cpu(map_buff->row_cnt));
3053 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
3054 le16_to_cpu(map_buff->layout_map_count));
Don Brace2b08b3e2015-01-23 16:41:09 -06003055 dev_info(&h->pdev->dev, "flags = 0x%x\n",
Scott Teeldd0e19f2014-02-18 13:57:31 -06003056 le16_to_cpu(map_buff->flags));
Don Brace2b08b3e2015-01-23 16:41:09 -06003057 dev_info(&h->pdev->dev, "encrypytion = %s\n",
3058 le16_to_cpu(map_buff->flags) &
3059 RAID_MAP_FLAG_ENCRYPT_ON ? "ON" : "OFF");
Scott Teeldd0e19f2014-02-18 13:57:31 -06003060 dev_info(&h->pdev->dev, "dekindex = %u\n",
3061 le16_to_cpu(map_buff->dekindex));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003062 map_cnt = le16_to_cpu(map_buff->layout_map_count);
3063 for (map = 0; map < map_cnt; map++) {
3064 dev_info(&h->pdev->dev, "Map%u:\n", map);
3065 row_cnt = le16_to_cpu(map_buff->row_cnt);
3066 for (row = 0; row < row_cnt; row++) {
3067 dev_info(&h->pdev->dev, " Row%u:\n", row);
3068 disks_per_row =
3069 le16_to_cpu(map_buff->data_disks_per_row);
3070 for (col = 0; col < disks_per_row; col++, dd++)
3071 dev_info(&h->pdev->dev,
3072 " D%02u: h=0x%04x xor=%u,%u\n",
3073 col, dd->ioaccel_handle,
3074 dd->xor_mult[0], dd->xor_mult[1]);
3075 disks_per_row =
3076 le16_to_cpu(map_buff->metadata_disks_per_row);
3077 for (col = 0; col < disks_per_row; col++, dd++)
3078 dev_info(&h->pdev->dev,
3079 " M%02u: h=0x%04x xor=%u,%u\n",
3080 col, dd->ioaccel_handle,
3081 dd->xor_mult[0], dd->xor_mult[1]);
3082 }
3083 }
3084}
3085#else
3086static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
3087 __attribute__((unused)) int rc,
3088 __attribute__((unused)) struct raid_map_data *map_buff)
3089{
3090}
3091#endif
3092
3093static int hpsa_get_raid_map(struct ctlr_info *h,
3094 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3095{
3096 int rc = 0;
3097 struct CommandList *c;
3098 struct ErrorInfo *ei;
3099
Stephen Cameron45fcb862015-01-23 16:43:04 -06003100 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003101
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003102 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
3103 sizeof(this_device->raid_map), 0,
3104 scsi3addr, TYPE_CMD)) {
Robert Elliott2dd02d72015-04-23 09:33:43 -05003105 dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
3106 cmd_free(h, c);
3107 return -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003108 }
Webb Scales25163bd2015-04-23 09:32:00 -05003109 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003110 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003111 if (rc)
3112 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003113 ei = c->err_info;
3114 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003115 hpsa_scsi_interpret_error(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05003116 rc = -1;
3117 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003118 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06003119 cmd_free(h, c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003120
3121 /* @todo in the future, dynamically allocate RAID map memory */
3122 if (le32_to_cpu(this_device->raid_map.structure_size) >
3123 sizeof(this_device->raid_map)) {
3124 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
3125 rc = -1;
3126 }
3127 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
3128 return rc;
Webb Scales25163bd2015-04-23 09:32:00 -05003129out:
3130 cmd_free(h, c);
3131 return rc;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003132}
3133
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003134static int hpsa_bmic_sense_subsystem_information(struct ctlr_info *h,
3135 unsigned char scsi3addr[], u16 bmic_device_index,
3136 struct bmic_sense_subsystem_info *buf, size_t bufsize)
3137{
3138 int rc = IO_OK;
3139 struct CommandList *c;
3140 struct ErrorInfo *ei;
3141
3142 c = cmd_alloc(h);
3143
3144 rc = fill_cmd(c, BMIC_SENSE_SUBSYSTEM_INFORMATION, h, buf, bufsize,
3145 0, RAID_CTLR_LUNID, TYPE_CMD);
3146 if (rc)
3147 goto out;
3148
3149 c->Request.CDB[2] = bmic_device_index & 0xff;
3150 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
3151
3152 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003153 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003154 if (rc)
3155 goto out;
3156 ei = c->err_info;
3157 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3158 hpsa_scsi_interpret_error(h, c);
3159 rc = -1;
3160 }
3161out:
3162 cmd_free(h, c);
3163 return rc;
3164}
3165
Scott Teel66749d02015-11-04 15:51:57 -06003166static int hpsa_bmic_id_controller(struct ctlr_info *h,
3167 struct bmic_identify_controller *buf, size_t bufsize)
3168{
3169 int rc = IO_OK;
3170 struct CommandList *c;
3171 struct ErrorInfo *ei;
3172
3173 c = cmd_alloc(h);
3174
3175 rc = fill_cmd(c, BMIC_IDENTIFY_CONTROLLER, h, buf, bufsize,
3176 0, RAID_CTLR_LUNID, TYPE_CMD);
3177 if (rc)
3178 goto out;
3179
3180 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003181 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teel66749d02015-11-04 15:51:57 -06003182 if (rc)
3183 goto out;
3184 ei = c->err_info;
3185 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3186 hpsa_scsi_interpret_error(h, c);
3187 rc = -1;
3188 }
3189out:
3190 cmd_free(h, c);
3191 return rc;
3192}
3193
Don Brace03383732015-01-23 16:43:30 -06003194static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
3195 unsigned char scsi3addr[], u16 bmic_device_index,
3196 struct bmic_identify_physical_device *buf, size_t bufsize)
3197{
3198 int rc = IO_OK;
3199 struct CommandList *c;
3200 struct ErrorInfo *ei;
3201
3202 c = cmd_alloc(h);
3203 rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
3204 0, RAID_CTLR_LUNID, TYPE_CMD);
3205 if (rc)
3206 goto out;
3207
3208 c->Request.CDB[2] = bmic_device_index & 0xff;
3209 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
3210
Webb Scales25163bd2015-04-23 09:32:00 -05003211 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
Don Bracec448ecf2016-04-27 17:13:51 -05003212 DEFAULT_TIMEOUT);
Don Brace03383732015-01-23 16:43:30 -06003213 ei = c->err_info;
3214 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3215 hpsa_scsi_interpret_error(h, c);
3216 rc = -1;
3217 }
3218out:
3219 cmd_free(h, c);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003220
Don Brace03383732015-01-23 16:43:30 -06003221 return rc;
3222}
3223
Don Bracecca8f132015-12-22 10:36:48 -06003224/*
3225 * get enclosure information
3226 * struct ReportExtendedLUNdata *rlep - Used for BMIC drive number
3227 * struct hpsa_scsi_dev_t *encl_dev - device entry for enclosure
3228 * Uses id_physical_device to determine the box_index.
3229 */
3230static void hpsa_get_enclosure_info(struct ctlr_info *h,
3231 unsigned char *scsi3addr,
3232 struct ReportExtendedLUNdata *rlep, int rle_index,
3233 struct hpsa_scsi_dev_t *encl_dev)
3234{
3235 int rc = -1;
3236 struct CommandList *c = NULL;
3237 struct ErrorInfo *ei = NULL;
3238 struct bmic_sense_storage_box_params *bssbp = NULL;
3239 struct bmic_identify_physical_device *id_phys = NULL;
3240 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
3241 u16 bmic_device_index = 0;
3242
3243 bmic_device_index = GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]);
3244
Don Brace17a9e542016-02-23 15:16:09 -06003245 if (bmic_device_index == 0xFF00 || MASKED_DEVICE(&rle->lunid[0])) {
3246 rc = IO_OK;
Don Bracecca8f132015-12-22 10:36:48 -06003247 goto out;
Don Brace17a9e542016-02-23 15:16:09 -06003248 }
Don Bracecca8f132015-12-22 10:36:48 -06003249
3250 bssbp = kzalloc(sizeof(*bssbp), GFP_KERNEL);
3251 if (!bssbp)
3252 goto out;
3253
3254 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3255 if (!id_phys)
3256 goto out;
3257
3258 rc = hpsa_bmic_id_physical_device(h, scsi3addr, bmic_device_index,
3259 id_phys, sizeof(*id_phys));
3260 if (rc) {
3261 dev_warn(&h->pdev->dev, "%s: id_phys failed %d bdi[0x%x]\n",
3262 __func__, encl_dev->external, bmic_device_index);
3263 goto out;
3264 }
3265
3266 c = cmd_alloc(h);
3267
3268 rc = fill_cmd(c, BMIC_SENSE_STORAGE_BOX_PARAMS, h, bssbp,
3269 sizeof(*bssbp), 0, RAID_CTLR_LUNID, TYPE_CMD);
3270
3271 if (rc)
3272 goto out;
3273
3274 if (id_phys->phys_connector[1] == 'E')
3275 c->Request.CDB[5] = id_phys->box_index;
3276 else
3277 c->Request.CDB[5] = 0;
3278
3279 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
Don Bracec448ecf2016-04-27 17:13:51 -05003280 DEFAULT_TIMEOUT);
Don Bracecca8f132015-12-22 10:36:48 -06003281 if (rc)
3282 goto out;
3283
3284 ei = c->err_info;
3285 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3286 rc = -1;
3287 goto out;
3288 }
3289
3290 encl_dev->box[id_phys->active_path_number] = bssbp->phys_box_on_port;
3291 memcpy(&encl_dev->phys_connector[id_phys->active_path_number],
3292 bssbp->phys_connector, sizeof(bssbp->phys_connector));
3293
3294 rc = IO_OK;
3295out:
3296 kfree(bssbp);
3297 kfree(id_phys);
3298
3299 if (c)
3300 cmd_free(h, c);
3301
3302 if (rc != IO_OK)
3303 hpsa_show_dev_msg(KERN_INFO, h, encl_dev,
3304 "Error, could not get enclosure information\n");
3305}
3306
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003307static u64 hpsa_get_sas_address_from_report_physical(struct ctlr_info *h,
3308 unsigned char *scsi3addr)
3309{
3310 struct ReportExtendedLUNdata *physdev;
3311 u32 nphysicals;
3312 u64 sa = 0;
3313 int i;
3314
3315 physdev = kzalloc(sizeof(*physdev), GFP_KERNEL);
3316 if (!physdev)
3317 return 0;
3318
3319 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3320 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3321 kfree(physdev);
3322 return 0;
3323 }
3324 nphysicals = get_unaligned_be32(physdev->LUNListLength) / 24;
3325
3326 for (i = 0; i < nphysicals; i++)
3327 if (!memcmp(&physdev->LUN[i].lunid[0], scsi3addr, 8)) {
3328 sa = get_unaligned_be64(&physdev->LUN[i].wwid[0]);
3329 break;
3330 }
3331
3332 kfree(physdev);
3333
3334 return sa;
3335}
3336
3337static void hpsa_get_sas_address(struct ctlr_info *h, unsigned char *scsi3addr,
3338 struct hpsa_scsi_dev_t *dev)
3339{
3340 int rc;
3341 u64 sa = 0;
3342
3343 if (is_hba_lunid(scsi3addr)) {
3344 struct bmic_sense_subsystem_info *ssi;
3345
3346 ssi = kzalloc(sizeof(*ssi), GFP_KERNEL);
3347 if (ssi == NULL) {
3348 dev_warn(&h->pdev->dev,
3349 "%s: out of memory\n", __func__);
3350 return;
3351 }
3352
3353 rc = hpsa_bmic_sense_subsystem_information(h,
3354 scsi3addr, 0, ssi, sizeof(*ssi));
3355 if (rc == 0) {
3356 sa = get_unaligned_be64(ssi->primary_world_wide_id);
3357 h->sas_address = sa;
3358 }
3359
3360 kfree(ssi);
3361 } else
3362 sa = hpsa_get_sas_address_from_report_physical(h, scsi3addr);
3363
3364 dev->sas_address = sa;
3365}
3366
3367/* Get a device id from inquiry page 0x83 */
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003368static int hpsa_vpd_page_supported(struct ctlr_info *h,
3369 unsigned char scsi3addr[], u8 page)
3370{
3371 int rc;
3372 int i;
3373 int pages;
3374 unsigned char *buf, bufsize;
3375
3376 buf = kzalloc(256, GFP_KERNEL);
3377 if (!buf)
3378 return 0;
3379
3380 /* Get the size of the page list first */
3381 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3382 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3383 buf, HPSA_VPD_HEADER_SZ);
3384 if (rc != 0)
3385 goto exit_unsupported;
3386 pages = buf[3];
3387 if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
3388 bufsize = pages + HPSA_VPD_HEADER_SZ;
3389 else
3390 bufsize = 255;
3391
3392 /* Get the whole VPD page list */
3393 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3394 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3395 buf, bufsize);
3396 if (rc != 0)
3397 goto exit_unsupported;
3398
3399 pages = buf[3];
3400 for (i = 1; i <= pages; i++)
3401 if (buf[3 + i] == page)
3402 goto exit_supported;
3403exit_unsupported:
3404 kfree(buf);
3405 return 0;
3406exit_supported:
3407 kfree(buf);
3408 return 1;
3409}
3410
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003411static void hpsa_get_ioaccel_status(struct ctlr_info *h,
3412 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3413{
3414 int rc;
3415 unsigned char *buf;
3416 u8 ioaccel_status;
3417
3418 this_device->offload_config = 0;
3419 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003420 this_device->offload_to_be_enabled = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003421
3422 buf = kzalloc(64, GFP_KERNEL);
3423 if (!buf)
3424 return;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003425 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
3426 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003427 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003428 VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003429 if (rc != 0)
3430 goto out;
3431
3432#define IOACCEL_STATUS_BYTE 4
3433#define OFFLOAD_CONFIGURED_BIT 0x01
3434#define OFFLOAD_ENABLED_BIT 0x02
3435 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
3436 this_device->offload_config =
3437 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
3438 if (this_device->offload_config) {
3439 this_device->offload_enabled =
3440 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
3441 if (hpsa_get_raid_map(h, scsi3addr, this_device))
3442 this_device->offload_enabled = 0;
3443 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003444 this_device->offload_to_be_enabled = this_device->offload_enabled;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003445out:
3446 kfree(buf);
3447 return;
3448}
3449
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003450/* Get the device id from inquiry page 0x83 */
3451static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
Don Brace75d23d82015-11-04 15:51:39 -06003452 unsigned char *device_id, int index, int buflen)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003453{
3454 int rc;
3455 unsigned char *buf;
3456
3457 if (buflen > 16)
3458 buflen = 16;
3459 buf = kzalloc(64, GFP_KERNEL);
3460 if (!buf)
Stephen M. Camerona84d7942014-05-29 10:54:20 -05003461 return -ENOMEM;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003462 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003463 if (rc == 0)
Don Brace75d23d82015-11-04 15:51:39 -06003464 memcpy(device_id, &buf[index], buflen);
3465
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003466 kfree(buf);
Don Brace75d23d82015-11-04 15:51:39 -06003467
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003468 return rc != 0;
3469}
3470
3471static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
Don Brace03383732015-01-23 16:43:30 -06003472 void *buf, int bufsize,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003473 int extended_response)
3474{
3475 int rc = IO_OK;
3476 struct CommandList *c;
3477 unsigned char scsi3addr[8];
3478 struct ErrorInfo *ei;
3479
Stephen Cameron45fcb862015-01-23 16:43:04 -06003480 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003481
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06003482 /* address the controller */
3483 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003484 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
3485 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
3486 rc = -1;
3487 goto out;
3488 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003489 if (extended_response)
3490 c->Request.CDB[1] = extended_response;
Webb Scales25163bd2015-04-23 09:32:00 -05003491 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003492 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003493 if (rc)
3494 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003495 ei = c->err_info;
3496 if (ei->CommandStatus != 0 &&
3497 ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003498 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003499 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003500 } else {
Don Brace03383732015-01-23 16:43:30 -06003501 struct ReportLUNdata *rld = buf;
3502
3503 if (rld->extended_response_flag != extended_response) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003504 dev_err(&h->pdev->dev,
3505 "report luns requested format %u, got %u\n",
3506 extended_response,
Don Brace03383732015-01-23 16:43:30 -06003507 rld->extended_response_flag);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003508 rc = -1;
3509 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003510 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003511out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06003512 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003513 return rc;
3514}
3515
3516static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06003517 struct ReportExtendedLUNdata *buf, int bufsize)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003518{
Don Brace03383732015-01-23 16:43:30 -06003519 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
3520 HPSA_REPORT_PHYS_EXTENDED);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003521}
3522
3523static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
3524 struct ReportLUNdata *buf, int bufsize)
3525{
3526 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
3527}
3528
3529static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
3530 int bus, int target, int lun)
3531{
3532 device->bus = bus;
3533 device->target = target;
3534 device->lun = lun;
3535}
3536
Stephen M. Cameron98465902014-02-21 16:25:00 -06003537/* Use VPD inquiry to get details of volume status */
3538static int hpsa_get_volume_status(struct ctlr_info *h,
3539 unsigned char scsi3addr[])
3540{
3541 int rc;
3542 int status;
3543 int size;
3544 unsigned char *buf;
3545
3546 buf = kzalloc(64, GFP_KERNEL);
3547 if (!buf)
3548 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3549
3550 /* Does controller have VPD for logical volume status? */
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003551 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
Stephen M. Cameron98465902014-02-21 16:25:00 -06003552 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003553
3554 /* Get the size of the VPD return buffer */
3555 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3556 buf, HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003557 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003558 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003559 size = buf[3];
3560
3561 /* Now get the whole VPD buffer */
3562 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3563 buf, size + HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003564 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003565 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003566 status = buf[4]; /* status byte */
3567
3568 kfree(buf);
3569 return status;
3570exit_failed:
3571 kfree(buf);
3572 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3573}
3574
3575/* Determine offline status of a volume.
3576 * Return either:
3577 * 0 (not offline)
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003578 * 0xff (offline for unknown reasons)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003579 * # (integer code indicating one of several NOT READY states
3580 * describing why a volume is to be kept offline)
3581 */
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003582static int hpsa_volume_offline(struct ctlr_info *h,
Stephen M. Cameron98465902014-02-21 16:25:00 -06003583 unsigned char scsi3addr[])
3584{
3585 struct CommandList *c;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003586 unsigned char *sense;
3587 u8 sense_key, asc, ascq;
3588 int sense_len;
Webb Scales25163bd2015-04-23 09:32:00 -05003589 int rc, ldstat = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003590 u16 cmd_status;
3591 u8 scsi_status;
3592#define ASC_LUN_NOT_READY 0x04
3593#define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
3594#define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
3595
3596 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003597
Stephen M. Cameron98465902014-02-21 16:25:00 -06003598 (void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
Don Bracec448ecf2016-04-27 17:13:51 -05003599 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
3600 DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003601 if (rc) {
3602 cmd_free(h, c);
3603 return 0;
3604 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06003605 sense = c->err_info->SenseInfo;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003606 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3607 sense_len = sizeof(c->err_info->SenseInfo);
3608 else
3609 sense_len = c->err_info->SenseLen;
3610 decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
Stephen M. Cameron98465902014-02-21 16:25:00 -06003611 cmd_status = c->err_info->CommandStatus;
3612 scsi_status = c->err_info->ScsiStatus;
3613 cmd_free(h, c);
3614 /* Is the volume 'not ready'? */
3615 if (cmd_status != CMD_TARGET_STATUS ||
3616 scsi_status != SAM_STAT_CHECK_CONDITION ||
3617 sense_key != NOT_READY ||
3618 asc != ASC_LUN_NOT_READY) {
3619 return 0;
3620 }
3621
3622 /* Determine the reason for not ready state */
3623 ldstat = hpsa_get_volume_status(h, scsi3addr);
3624
3625 /* Keep volume offline in certain cases: */
3626 switch (ldstat) {
3627 case HPSA_LV_UNDERGOING_ERASE:
Scott Benesh5ca01202015-07-18 11:13:04 -05003628 case HPSA_LV_NOT_AVAILABLE:
Stephen M. Cameron98465902014-02-21 16:25:00 -06003629 case HPSA_LV_UNDERGOING_RPI:
3630 case HPSA_LV_PENDING_RPI:
3631 case HPSA_LV_ENCRYPTED_NO_KEY:
3632 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
3633 case HPSA_LV_UNDERGOING_ENCRYPTION:
3634 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
3635 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
3636 return ldstat;
3637 case HPSA_VPD_LV_STATUS_UNSUPPORTED:
3638 /* If VPD status page isn't available,
3639 * use ASC/ASCQ to determine state
3640 */
3641 if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
3642 (ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
3643 return ldstat;
3644 break;
3645 default:
3646 break;
3647 }
3648 return 0;
3649}
3650
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003651/*
3652 * Find out if a logical device supports aborts by simply trying one.
3653 * Smart Array may claim not to support aborts on logical drives, but
3654 * if a MSA2000 * is connected, the drives on that will be presented
3655 * by the Smart Array as logical drives, and aborts may be sent to
3656 * those devices successfully. So the simplest way to find out is
3657 * to simply try an abort and see how the device responds.
3658 */
3659static int hpsa_device_supports_aborts(struct ctlr_info *h,
3660 unsigned char *scsi3addr)
3661{
3662 struct CommandList *c;
3663 struct ErrorInfo *ei;
3664 int rc = 0;
3665
3666 u64 tag = (u64) -1; /* bogus tag */
3667
3668 /* Assume that physical devices support aborts */
3669 if (!is_logical_dev_addr_mode(scsi3addr))
3670 return 1;
3671
3672 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003673
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003674 (void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
Don Bracec448ecf2016-04-27 17:13:51 -05003675 (void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
3676 DEFAULT_TIMEOUT);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003677 /* no unmap needed here because no data xfer. */
3678 ei = c->err_info;
3679 switch (ei->CommandStatus) {
3680 case CMD_INVALID:
3681 rc = 0;
3682 break;
3683 case CMD_UNABORTABLE:
3684 case CMD_ABORT_FAILED:
3685 rc = 1;
3686 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003687 case CMD_TMF_STATUS:
3688 rc = hpsa_evaluate_tmf_status(h, c);
3689 break;
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003690 default:
3691 rc = 0;
3692 break;
3693 }
3694 cmd_free(h, c);
3695 return rc;
3696}
3697
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003698static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003699 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
3700 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003701{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003702
3703#define OBDR_SIG_OFFSET 43
3704#define OBDR_TAPE_SIG "$DR-10"
3705#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
3706#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
3707
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003708 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003709 unsigned char *obdr_sig;
Don Brace683fc442015-11-04 15:50:44 -06003710 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003711
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003712 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Don Brace683fc442015-11-04 15:50:44 -06003713 if (!inq_buff) {
3714 rc = -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003715 goto bail_out;
Don Brace683fc442015-11-04 15:50:44 -06003716 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003717
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003718 /* Do an inquiry to the device to see what it is. */
3719 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3720 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3721 /* Inquiry failed (msg printed already) */
3722 dev_err(&h->pdev->dev,
3723 "hpsa_update_device_info: inquiry failed\n");
Don Brace683fc442015-11-04 15:50:44 -06003724 rc = -EIO;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003725 goto bail_out;
3726 }
3727
Don Brace4af61e42016-02-23 15:16:40 -06003728 scsi_sanitize_inquiry_string(&inq_buff[8], 8);
3729 scsi_sanitize_inquiry_string(&inq_buff[16], 16);
Don Brace75d23d82015-11-04 15:51:39 -06003730
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003731 this_device->devtype = (inq_buff[0] & 0x1f);
3732 memcpy(this_device->scsi3addr, scsi3addr, 8);
3733 memcpy(this_device->vendor, &inq_buff[8],
3734 sizeof(this_device->vendor));
3735 memcpy(this_device->model, &inq_buff[16],
3736 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003737 memset(this_device->device_id, 0,
3738 sizeof(this_device->device_id));
Don Brace75d23d82015-11-04 15:51:39 -06003739 hpsa_get_device_id(h, scsi3addr, this_device->device_id, 8,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003740 sizeof(this_device->device_id));
3741
Don Braceaf15ed32016-02-23 15:16:15 -06003742 if ((this_device->devtype == TYPE_DISK ||
3743 this_device->devtype == TYPE_ZBC) &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003744 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003745 int volume_offline;
3746
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003747 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003748 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3749 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003750 volume_offline = hpsa_volume_offline(h, scsi3addr);
3751 if (volume_offline < 0 || volume_offline > 0xff)
3752 volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
3753 this_device->volume_offline = volume_offline & 0xff;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003754 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003755 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003756 this_device->offload_config = 0;
3757 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003758 this_device->offload_to_be_enabled = 0;
Joe Handzika3144e02015-04-23 09:32:59 -05003759 this_device->hba_ioaccel_enabled = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003760 this_device->volume_offline = 0;
Don Brace03383732015-01-23 16:43:30 -06003761 this_device->queue_depth = h->nr_cmds;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003762 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003763
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003764 if (is_OBDR_device) {
3765 /* See if this is a One-Button-Disaster-Recovery device
3766 * by looking for "$DR-10" at offset 43 in inquiry data.
3767 */
3768 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
3769 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
3770 strncmp(obdr_sig, OBDR_TAPE_SIG,
3771 OBDR_SIG_LEN) == 0);
3772 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003773 kfree(inq_buff);
3774 return 0;
3775
3776bail_out:
3777 kfree(inq_buff);
Don Brace683fc442015-11-04 15:50:44 -06003778 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003779}
3780
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003781static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
3782 struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
3783{
3784 unsigned long flags;
3785 int rc, entry;
3786 /*
3787 * See if this device supports aborts. If we already know
3788 * the device, we already know if it supports aborts, otherwise
3789 * we have to find out if it supports aborts by trying one.
3790 */
3791 spin_lock_irqsave(&h->devlock, flags);
3792 rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
3793 if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
3794 entry >= 0 && entry < h->ndevices) {
3795 dev->supports_aborts = h->dev[entry]->supports_aborts;
3796 spin_unlock_irqrestore(&h->devlock, flags);
3797 } else {
3798 spin_unlock_irqrestore(&h->devlock, flags);
3799 dev->supports_aborts =
3800 hpsa_device_supports_aborts(h, scsi3addr);
3801 if (dev->supports_aborts < 0)
3802 dev->supports_aborts = 0;
3803 }
3804}
3805
Kevin Barnettc7955052015-11-04 15:51:45 -06003806/*
3807 * Helper function to assign bus, target, lun mapping of devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003808 * Logical drive target and lun are assigned at this time, but
3809 * physical device lun and target assignment are deferred (assigned
3810 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
Kevin Barnettc7955052015-11-04 15:51:45 -06003811*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003812static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003813 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003814{
Kevin Barnettc7955052015-11-04 15:51:45 -06003815 u32 lunid = get_unaligned_le32(lunaddrbytes);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003816
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003817 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
3818 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003819 if (is_hba_lunid(lunaddrbytes))
Kevin Barnettc7955052015-11-04 15:51:45 -06003820 hpsa_set_bus_target_lun(device,
3821 HPSA_HBA_BUS, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003822 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003823 /* defer target, lun assignment for physical devices */
Kevin Barnettc7955052015-11-04 15:51:45 -06003824 hpsa_set_bus_target_lun(device,
3825 HPSA_PHYSICAL_DEVICE_BUS, -1, -1);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003826 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003827 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003828 /* It's a logical device */
Scott Teel66749d02015-11-04 15:51:57 -06003829 if (device->external) {
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003830 hpsa_set_bus_target_lun(device,
Kevin Barnettc7955052015-11-04 15:51:45 -06003831 HPSA_EXTERNAL_RAID_VOLUME_BUS, (lunid >> 16) & 0x3fff,
3832 lunid & 0x00ff);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003833 return;
3834 }
Kevin Barnettc7955052015-11-04 15:51:45 -06003835 hpsa_set_bus_target_lun(device, HPSA_RAID_VOLUME_BUS,
3836 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003837}
3838
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003839
3840/*
Scott Teel54b6e9e2014-02-18 13:56:45 -06003841 * Get address of physical disk used for an ioaccel2 mode command:
3842 * 1. Extract ioaccel2 handle from the command.
3843 * 2. Find a matching ioaccel2 handle from list of physical disks.
3844 * 3. Return:
3845 * 1 and set scsi3addr to address of matching physical
3846 * 0 if no matching physical disk was found.
3847 */
3848static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
3849 struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
3850{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003851 struct io_accel2_cmd *c2 =
3852 &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
3853 unsigned long flags;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003854 int i;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003855
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003856 spin_lock_irqsave(&h->devlock, flags);
3857 for (i = 0; i < h->ndevices; i++)
3858 if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
3859 memcpy(scsi3addr, h->dev[i]->scsi3addr,
3860 sizeof(h->dev[i]->scsi3addr));
3861 spin_unlock_irqrestore(&h->devlock, flags);
3862 return 1;
3863 }
3864 spin_unlock_irqrestore(&h->devlock, flags);
3865 return 0;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003866}
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003867
Scott Teel66749d02015-11-04 15:51:57 -06003868static int figure_external_status(struct ctlr_info *h, int raid_ctlr_position,
3869 int i, int nphysicals, int nlocal_logicals)
3870{
3871 /* In report logicals, local logicals are listed first,
3872 * then any externals.
3873 */
3874 int logicals_start = nphysicals + (raid_ctlr_position == 0);
3875
3876 if (i == raid_ctlr_position)
3877 return 0;
3878
3879 if (i < logicals_start)
3880 return 0;
3881
3882 /* i is in logicals range, but still within local logicals */
3883 if ((i - nphysicals - (raid_ctlr_position == 0)) < nlocal_logicals)
3884 return 0;
3885
3886 return 1; /* it's an external lun */
3887}
3888
Scott Teel54b6e9e2014-02-18 13:56:45 -06003889/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003890 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
3891 * logdev. The number of luns in physdev and logdev are returned in
3892 * *nphysicals and *nlogicals, respectively.
3893 * Returns 0 on success, -1 otherwise.
3894 */
3895static int hpsa_gather_lun_info(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06003896 struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003897 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003898{
Don Brace03383732015-01-23 16:43:30 -06003899 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003900 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3901 return -1;
3902 }
Don Brace03383732015-01-23 16:43:30 -06003903 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003904 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
Don Brace03383732015-01-23 16:43:30 -06003905 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
3906 HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003907 *nphysicals = HPSA_MAX_PHYS_LUN;
3908 }
Don Brace03383732015-01-23 16:43:30 -06003909 if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003910 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3911 return -1;
3912 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06003913 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003914 /* Reject Logicals in excess of our max capability. */
3915 if (*nlogicals > HPSA_MAX_LUN) {
3916 dev_warn(&h->pdev->dev,
3917 "maximum logical LUNs (%d) exceeded. "
3918 "%d LUNs ignored.\n", HPSA_MAX_LUN,
3919 *nlogicals - HPSA_MAX_LUN);
3920 *nlogicals = HPSA_MAX_LUN;
3921 }
3922 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
3923 dev_warn(&h->pdev->dev,
3924 "maximum logical + physical LUNs (%d) exceeded. "
3925 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
3926 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
3927 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
3928 }
3929 return 0;
3930}
3931
Don Brace42a91642014-11-14 17:26:27 -06003932static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
3933 int i, int nphysicals, int nlogicals,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06003934 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003935 struct ReportLUNdata *logdev_list)
3936{
3937 /* Helper function, figure out where the LUN ID info is coming from
3938 * given index i, lists of physical and logical devices, where in
3939 * the list the raid controller is supposed to appear (first or last)
3940 */
3941
3942 int logicals_start = nphysicals + (raid_ctlr_position == 0);
3943 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
3944
3945 if (i == raid_ctlr_position)
3946 return RAID_CTLR_LUNID;
3947
3948 if (i < logicals_start)
Stephen M. Camerond5b5d962014-05-29 10:53:34 -05003949 return &physdev_list->LUN[i -
3950 (raid_ctlr_position == 0)].lunid[0];
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003951
3952 if (i < last_device)
3953 return &logdev_list->LUN[i - nphysicals -
3954 (raid_ctlr_position == 0)][0];
3955 BUG();
3956 return NULL;
3957}
3958
Don Brace03383732015-01-23 16:43:30 -06003959/* get physical drive ioaccel handle and queue depth */
3960static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
3961 struct hpsa_scsi_dev_t *dev,
Don Bracef2039b02015-11-04 15:51:08 -06003962 struct ReportExtendedLUNdata *rlep, int rle_index,
Don Brace03383732015-01-23 16:43:30 -06003963 struct bmic_identify_physical_device *id_phys)
3964{
3965 int rc;
Don Bracef2039b02015-11-04 15:51:08 -06003966 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
Don Brace03383732015-01-23 16:43:30 -06003967
3968 dev->ioaccel_handle = rle->ioaccel_handle;
Don Bracef2039b02015-11-04 15:51:08 -06003969 if ((rle->device_flags & 0x08) && dev->ioaccel_handle)
Joe Handzika3144e02015-04-23 09:32:59 -05003970 dev->hba_ioaccel_enabled = 1;
Don Brace03383732015-01-23 16:43:30 -06003971 memset(id_phys, 0, sizeof(*id_phys));
Don Bracef2039b02015-11-04 15:51:08 -06003972 rc = hpsa_bmic_id_physical_device(h, &rle->lunid[0],
3973 GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]), id_phys,
Don Brace03383732015-01-23 16:43:30 -06003974 sizeof(*id_phys));
3975 if (!rc)
3976 /* Reserve space for FW operations */
3977#define DRIVE_CMDS_RESERVED_FOR_FW 2
3978#define DRIVE_QUEUE_DEPTH 7
3979 dev->queue_depth =
3980 le16_to_cpu(id_phys->current_queue_depth_limit) -
3981 DRIVE_CMDS_RESERVED_FOR_FW;
3982 else
3983 dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
Don Brace03383732015-01-23 16:43:30 -06003984}
3985
Joe Handzik8270b862015-07-18 11:12:43 -05003986static void hpsa_get_path_info(struct hpsa_scsi_dev_t *this_device,
Don Bracef2039b02015-11-04 15:51:08 -06003987 struct ReportExtendedLUNdata *rlep, int rle_index,
Joe Handzik8270b862015-07-18 11:12:43 -05003988 struct bmic_identify_physical_device *id_phys)
3989{
Don Bracef2039b02015-11-04 15:51:08 -06003990 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
3991
3992 if ((rle->device_flags & 0x08) && this_device->ioaccel_handle)
Joe Handzik8270b862015-07-18 11:12:43 -05003993 this_device->hba_ioaccel_enabled = 1;
3994
3995 memcpy(&this_device->active_path_index,
3996 &id_phys->active_path_number,
3997 sizeof(this_device->active_path_index));
3998 memcpy(&this_device->path_map,
3999 &id_phys->redundant_path_present_map,
4000 sizeof(this_device->path_map));
4001 memcpy(&this_device->box,
4002 &id_phys->alternate_paths_phys_box_on_port,
4003 sizeof(this_device->box));
4004 memcpy(&this_device->phys_connector,
4005 &id_phys->alternate_paths_phys_connector,
4006 sizeof(this_device->phys_connector));
4007 memcpy(&this_device->bay,
4008 &id_phys->phys_bay_in_box,
4009 sizeof(this_device->bay));
4010}
4011
Scott Teel66749d02015-11-04 15:51:57 -06004012/* get number of local logical disks. */
4013static int hpsa_set_local_logical_count(struct ctlr_info *h,
4014 struct bmic_identify_controller *id_ctlr,
4015 u32 *nlocals)
4016{
4017 int rc;
4018
4019 if (!id_ctlr) {
4020 dev_warn(&h->pdev->dev, "%s: id_ctlr buffer is NULL.\n",
4021 __func__);
4022 return -ENOMEM;
4023 }
4024 memset(id_ctlr, 0, sizeof(*id_ctlr));
4025 rc = hpsa_bmic_id_controller(h, id_ctlr, sizeof(*id_ctlr));
4026 if (!rc)
4027 if (id_ctlr->configured_logical_drive_count < 256)
4028 *nlocals = id_ctlr->configured_logical_drive_count;
4029 else
4030 *nlocals = le16_to_cpu(
4031 id_ctlr->extended_logical_unit_count);
4032 else
4033 *nlocals = -1;
4034 return rc;
4035}
4036
4037
Don Brace8aa60682015-11-04 15:50:01 -06004038static void hpsa_update_scsi_devices(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004039{
4040 /* the idea here is we could get notified
4041 * that some devices have changed, so we do a report
4042 * physical luns and report logical luns cmd, and adjust
4043 * our list of devices accordingly.
4044 *
4045 * The scsi3addr's of devices won't change so long as the
4046 * adapter is not reset. That means we can rescan and
4047 * tell which devices we already know about, vs. new
4048 * devices, vs. disappearing devices.
4049 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06004050 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004051 struct ReportLUNdata *logdev_list = NULL;
Don Brace03383732015-01-23 16:43:30 -06004052 struct bmic_identify_physical_device *id_phys = NULL;
Scott Teel66749d02015-11-04 15:51:57 -06004053 struct bmic_identify_controller *id_ctlr = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004054 u32 nphysicals = 0;
4055 u32 nlogicals = 0;
Scott Teel66749d02015-11-04 15:51:57 -06004056 u32 nlocal_logicals = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004057 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004058 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
4059 int ncurrent = 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06004060 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004061 int raid_ctlr_position;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004062 bool physical_device;
Scott Teelaca4a522012-01-19 14:01:19 -06004063 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004064
Scott Teelcfe5bad2011-10-26 16:21:07 -05004065 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameron92084712014-11-14 17:26:54 -06004066 physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
4067 logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004068 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
Don Brace03383732015-01-23 16:43:30 -06004069 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
Scott Teel66749d02015-11-04 15:51:57 -06004070 id_ctlr = kzalloc(sizeof(*id_ctlr), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004071
Don Brace03383732015-01-23 16:43:30 -06004072 if (!currentsd || !physdev_list || !logdev_list ||
Scott Teel66749d02015-11-04 15:51:57 -06004073 !tmpdevice || !id_phys || !id_ctlr) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004074 dev_err(&h->pdev->dev, "out of memory\n");
4075 goto out;
4076 }
4077 memset(lunzerobits, 0, sizeof(lunzerobits));
4078
Don Brace853633e2015-11-04 15:50:37 -06004079 h->drv_req_rescan = 0; /* cancel scheduled rescan - we're doing it. */
4080
Don Brace03383732015-01-23 16:43:30 -06004081 if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
Don Brace853633e2015-11-04 15:50:37 -06004082 logdev_list, &nlogicals)) {
4083 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004084 goto out;
Don Brace853633e2015-11-04 15:50:37 -06004085 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004086
Scott Teel66749d02015-11-04 15:51:57 -06004087 /* Set number of local logicals (non PTRAID) */
4088 if (hpsa_set_local_logical_count(h, id_ctlr, &nlocal_logicals)) {
4089 dev_warn(&h->pdev->dev,
4090 "%s: Can't determine number of local logical devices.\n",
4091 __func__);
4092 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004093
Scott Teelaca4a522012-01-19 14:01:19 -06004094 /* We might see up to the maximum number of logical and physical disks
4095 * plus external target devices, and a device for the local RAID
4096 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004097 */
Scott Teelaca4a522012-01-19 14:01:19 -06004098 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004099
4100 /* Allocate the per device structures */
4101 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05004102 if (i >= HPSA_MAX_DEVICES) {
4103 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
4104 " %d devices ignored.\n", HPSA_MAX_DEVICES,
4105 ndevs_to_allocate - HPSA_MAX_DEVICES);
4106 break;
4107 }
4108
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004109 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
4110 if (!currentsd[i]) {
4111 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
4112 __FILE__, __LINE__);
Don Brace853633e2015-11-04 15:50:37 -06004113 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004114 goto out;
4115 }
4116 ndev_allocated++;
4117 }
4118
Stephen M. Cameron86452912014-05-29 10:53:49 -05004119 if (is_scsi_rev_5(h))
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004120 raid_ctlr_position = 0;
4121 else
4122 raid_ctlr_position = nphysicals + nlogicals;
4123
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004124 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06004125 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004126 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004127 u8 *lunaddrbytes, is_OBDR = 0;
Don Brace683fc442015-11-04 15:50:44 -06004128 int rc = 0;
Don Bracef2039b02015-11-04 15:51:08 -06004129 int phys_dev_index = i - (raid_ctlr_position == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004130
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004131 physical_device = i < nphysicals + (raid_ctlr_position == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004132
4133 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004134 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
4135 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004136
4137 /* skip masked non-disk devices */
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004138 if (MASKED_DEVICE(lunaddrbytes) && physical_device &&
Don Bracecca8f132015-12-22 10:36:48 -06004139 (physdev_list->LUN[phys_dev_index].device_type != 0x06) &&
4140 (physdev_list->LUN[phys_dev_index].device_flags & 0x01))
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004141 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004142
4143 /* Get device type, vendor, model, device id */
Don Brace683fc442015-11-04 15:50:44 -06004144 rc = hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
4145 &is_OBDR);
4146 if (rc == -ENOMEM) {
4147 dev_warn(&h->pdev->dev,
4148 "Out of memory, rescan deferred.\n");
Don Brace853633e2015-11-04 15:50:37 -06004149 h->drv_req_rescan = 1;
Don Brace683fc442015-11-04 15:50:44 -06004150 goto out;
Don Brace853633e2015-11-04 15:50:37 -06004151 }
Don Brace683fc442015-11-04 15:50:44 -06004152 if (rc) {
4153 dev_warn(&h->pdev->dev,
4154 "Inquiry failed, skipping device.\n");
4155 continue;
4156 }
4157
Scott Teel66749d02015-11-04 15:51:57 -06004158 /* Determine if this is a lun from an external target array */
4159 tmpdevice->external =
4160 figure_external_status(h, raid_ctlr_position, i,
4161 nphysicals, nlocal_logicals);
4162
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06004163 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05004164 hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004165 this_device = currentsd[ncurrent];
4166
Scott Teel34592252015-11-04 15:52:09 -06004167 /* Turn on discovery_polling if there are ext target devices.
4168 * Event-based change notification is unreliable for those.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004169 */
Scott Teel34592252015-11-04 15:52:09 -06004170 if (!h->discovery_polling) {
4171 if (tmpdevice->external) {
4172 h->discovery_polling = 1;
4173 dev_info(&h->pdev->dev,
4174 "External target, activate discovery polling.\n");
4175 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004176 }
4177
Scott Teel34592252015-11-04 15:52:09 -06004178
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004179 *this_device = *tmpdevice;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004180 this_device->physical_device = physical_device;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004181
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004182 /*
4183 * Expose all devices except for physical devices that
4184 * are masked.
4185 */
4186 if (MASKED_DEVICE(lunaddrbytes) && this_device->physical_device)
Kevin Barnett2a168202015-11-04 15:51:21 -06004187 this_device->expose_device = 0;
4188 else
4189 this_device->expose_device = 1;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004190
Kevin Barnettd04e62b2015-11-04 15:52:34 -06004191
4192 /*
4193 * Get the SAS address for physical devices that are exposed.
4194 */
4195 if (this_device->physical_device && this_device->expose_device)
4196 hpsa_get_sas_address(h, lunaddrbytes, this_device);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004197
4198 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004199 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004200 /* We don't *really* support actual CD-ROM devices,
4201 * just "One Button Disaster Recovery" tape drive
4202 * which temporarily pretends to be a CD-ROM drive.
4203 * So we check that the device is really an OBDR tape
4204 * device by checking for "$DR-10" in bytes 43-48 of
4205 * the inquiry data.
4206 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004207 if (is_OBDR)
4208 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004209 break;
4210 case TYPE_DISK:
Don Braceaf15ed32016-02-23 15:16:15 -06004211 case TYPE_ZBC:
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004212 if (this_device->physical_device) {
Kevin Barnettb9092b72015-07-18 11:12:59 -05004213 /* The disk is in HBA mode. */
4214 /* Never use RAID mapper in HBA mode. */
Stephen M. Cameron316b2212014-02-21 16:25:15 -06004215 this_device->offload_enabled = 0;
Kevin Barnettb9092b72015-07-18 11:12:59 -05004216 hpsa_get_ioaccel_drive_info(h, this_device,
Don Bracef2039b02015-11-04 15:51:08 -06004217 physdev_list, phys_dev_index, id_phys);
4218 hpsa_get_path_info(this_device,
4219 physdev_list, phys_dev_index, id_phys);
Kevin Barnettb9092b72015-07-18 11:12:59 -05004220 }
Joe Handzikecf418d12015-04-23 09:33:04 -05004221 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004222 break;
4223 case TYPE_TAPE:
4224 case TYPE_MEDIUM_CHANGER:
Don Bracecca8f132015-12-22 10:36:48 -06004225 ncurrent++;
4226 break;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004227 case TYPE_ENCLOSURE:
Don Brace17a9e542016-02-23 15:16:09 -06004228 if (!this_device->external)
4229 hpsa_get_enclosure_info(h, lunaddrbytes,
Don Bracecca8f132015-12-22 10:36:48 -06004230 physdev_list, phys_dev_index,
4231 this_device);
Kevin Barnettb9092b72015-07-18 11:12:59 -05004232 ncurrent++;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004233 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004234 case TYPE_RAID:
4235 /* Only present the Smartarray HBA as a RAID controller.
4236 * If it's a RAID controller other than the HBA itself
4237 * (an external RAID controller, MSA500 or similar)
4238 * don't present it.
4239 */
4240 if (!is_hba_lunid(lunaddrbytes))
4241 break;
4242 ncurrent++;
4243 break;
4244 default:
4245 break;
4246 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05004247 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004248 break;
4249 }
Kevin Barnettd04e62b2015-11-04 15:52:34 -06004250
4251 if (h->sas_host == NULL) {
4252 int rc = 0;
4253
4254 rc = hpsa_add_sas_host(h);
4255 if (rc) {
4256 dev_warn(&h->pdev->dev,
4257 "Could not add sas host %d\n", rc);
4258 goto out;
4259 }
4260 }
4261
Don Brace8aa60682015-11-04 15:50:01 -06004262 adjust_hpsa_scsi_table(h, currentsd, ncurrent);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004263out:
4264 kfree(tmpdevice);
4265 for (i = 0; i < ndev_allocated; i++)
4266 kfree(currentsd[i]);
4267 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004268 kfree(physdev_list);
4269 kfree(logdev_list);
Scott Teel66749d02015-11-04 15:51:57 -06004270 kfree(id_ctlr);
Don Brace03383732015-01-23 16:43:30 -06004271 kfree(id_phys);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004272}
4273
Webb Scalesec5cbf02015-01-23 16:44:45 -06004274static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
4275 struct scatterlist *sg)
4276{
4277 u64 addr64 = (u64) sg_dma_address(sg);
4278 unsigned int len = sg_dma_len(sg);
4279
4280 desc->Addr = cpu_to_le64(addr64);
4281 desc->Len = cpu_to_le32(len);
4282 desc->Ext = 0;
4283}
4284
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004285/*
4286 * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004287 * dma mapping and fills in the scatter gather entries of the
4288 * hpsa command, cp.
4289 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004290static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004291 struct CommandList *cp,
4292 struct scsi_cmnd *cmd)
4293{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004294 struct scatterlist *sg;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004295 int use_sg, i, sg_limit, chained, last_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004296 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004297
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004298 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004299
4300 use_sg = scsi_dma_map(cmd);
4301 if (use_sg < 0)
4302 return use_sg;
4303
4304 if (!use_sg)
4305 goto sglist_finished;
4306
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004307 /*
4308 * If the number of entries is greater than the max for a single list,
4309 * then we have a chained list; we will set up all but one entry in the
4310 * first list (the last entry is saved for link information);
4311 * otherwise, we don't have a chained list and we'll set up at each of
4312 * the entries in the one list.
4313 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004314 curr_sg = cp->SG;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004315 chained = use_sg > h->max_cmd_sg_entries;
4316 sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
4317 last_sg = scsi_sg_count(cmd) - 1;
4318 scsi_for_each_sg(cmd, sg, sg_limit, i) {
Webb Scalesec5cbf02015-01-23 16:44:45 -06004319 hpsa_set_sg_descriptor(curr_sg, sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004320 curr_sg++;
4321 }
Webb Scalesec5cbf02015-01-23 16:44:45 -06004322
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004323 if (chained) {
4324 /*
4325 * Continue with the chained list. Set curr_sg to the chained
4326 * list. Modify the limit to the total count less the entries
4327 * we've already set up. Resume the scan at the list entry
4328 * where the previous loop left off.
4329 */
4330 curr_sg = h->cmd_sg_list[cp->cmdindex];
4331 sg_limit = use_sg - sg_limit;
4332 for_each_sg(sg, sg, sg_limit, i) {
4333 hpsa_set_sg_descriptor(curr_sg, sg);
4334 curr_sg++;
4335 }
4336 }
4337
Webb Scalesec5cbf02015-01-23 16:44:45 -06004338 /* Back the pointer up to the last entry and mark it as "last". */
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004339 (curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004340
4341 if (use_sg + chained > h->maxSG)
4342 h->maxSG = use_sg + chained;
4343
4344 if (chained) {
4345 cp->Header.SGList = h->max_cmd_sg_entries;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004346 cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06004347 if (hpsa_map_sg_chain_block(h, cp)) {
4348 scsi_dma_unmap(cmd);
4349 return -1;
4350 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004351 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004352 }
4353
4354sglist_finished:
4355
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004356 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004357 cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004358 return 0;
4359}
4360
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004361#define IO_ACCEL_INELIGIBLE (1)
4362static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
4363{
4364 int is_write = 0;
4365 u32 block;
4366 u32 block_cnt;
4367
4368 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
4369 switch (cdb[0]) {
4370 case WRITE_6:
4371 case WRITE_12:
4372 is_write = 1;
4373 case READ_6:
4374 case READ_12:
4375 if (*cdb_len == 6) {
Don Bracec8a6c9a2015-11-04 15:50:50 -06004376 block = get_unaligned_be16(&cdb[2]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004377 block_cnt = cdb[4];
Don Bracec8a6c9a2015-11-04 15:50:50 -06004378 if (block_cnt == 0)
4379 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004380 } else {
4381 BUG_ON(*cdb_len != 12);
Don Bracec8a6c9a2015-11-04 15:50:50 -06004382 block = get_unaligned_be32(&cdb[2]);
4383 block_cnt = get_unaligned_be32(&cdb[6]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004384 }
4385 if (block_cnt > 0xffff)
4386 return IO_ACCEL_INELIGIBLE;
4387
4388 cdb[0] = is_write ? WRITE_10 : READ_10;
4389 cdb[1] = 0;
4390 cdb[2] = (u8) (block >> 24);
4391 cdb[3] = (u8) (block >> 16);
4392 cdb[4] = (u8) (block >> 8);
4393 cdb[5] = (u8) (block);
4394 cdb[6] = 0;
4395 cdb[7] = (u8) (block_cnt >> 8);
4396 cdb[8] = (u8) (block_cnt);
4397 cdb[9] = 0;
4398 *cdb_len = 10;
4399 break;
4400 }
4401 return 0;
4402}
4403
Scott Teelc3497752014-02-18 13:56:34 -06004404static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004405 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004406 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Matt Gatese1f7de02014-02-18 13:55:17 -06004407{
4408 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06004409 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
4410 unsigned int len;
4411 unsigned int total_len = 0;
4412 struct scatterlist *sg;
4413 u64 addr64;
4414 int use_sg, i;
4415 struct SGDescriptor *curr_sg;
4416 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
4417
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004418 /* TODO: implement chaining support */
Don Brace03383732015-01-23 16:43:30 -06004419 if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
4420 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004421 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004422 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004423
Matt Gatese1f7de02014-02-18 13:55:17 -06004424 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
4425
Don Brace03383732015-01-23 16:43:30 -06004426 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4427 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004428 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004429 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004430
Matt Gatese1f7de02014-02-18 13:55:17 -06004431 c->cmd_type = CMD_IOACCEL1;
4432
4433 /* Adjust the DMA address to point to the accelerated command buffer */
4434 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
4435 (c->cmdindex * sizeof(*cp));
4436 BUG_ON(c->busaddr & 0x0000007F);
4437
4438 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004439 if (use_sg < 0) {
4440 atomic_dec(&phys_disk->ioaccel_cmds_out);
Matt Gatese1f7de02014-02-18 13:55:17 -06004441 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004442 }
Matt Gatese1f7de02014-02-18 13:55:17 -06004443
4444 if (use_sg) {
4445 curr_sg = cp->SG;
4446 scsi_for_each_sg(cmd, sg, use_sg, i) {
4447 addr64 = (u64) sg_dma_address(sg);
4448 len = sg_dma_len(sg);
4449 total_len += len;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004450 curr_sg->Addr = cpu_to_le64(addr64);
4451 curr_sg->Len = cpu_to_le32(len);
4452 curr_sg->Ext = cpu_to_le32(0);
Matt Gatese1f7de02014-02-18 13:55:17 -06004453 curr_sg++;
4454 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004455 (--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
Matt Gatese1f7de02014-02-18 13:55:17 -06004456
4457 switch (cmd->sc_data_direction) {
4458 case DMA_TO_DEVICE:
4459 control |= IOACCEL1_CONTROL_DATA_OUT;
4460 break;
4461 case DMA_FROM_DEVICE:
4462 control |= IOACCEL1_CONTROL_DATA_IN;
4463 break;
4464 case DMA_NONE:
4465 control |= IOACCEL1_CONTROL_NODATAXFER;
4466 break;
4467 default:
4468 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4469 cmd->sc_data_direction);
4470 BUG();
4471 break;
4472 }
4473 } else {
4474 control |= IOACCEL1_CONTROL_NODATAXFER;
4475 }
4476
Scott Teelc3497752014-02-18 13:56:34 -06004477 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06004478 /* Fill out the command structure to submit */
Don Brace2b08b3e2015-01-23 16:41:09 -06004479 cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
4480 cp->transfer_len = cpu_to_le32(total_len);
4481 cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
4482 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
4483 cp->control = cpu_to_le32(control);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004484 memcpy(cp->CDB, cdb, cdb_len);
4485 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06004486 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004487 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06004488 return 0;
4489}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004490
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004491/*
4492 * Queue a command directly to a device behind the controller using the
4493 * I/O accelerator path.
4494 */
4495static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
4496 struct CommandList *c)
4497{
4498 struct scsi_cmnd *cmd = c->scsi_cmd;
4499 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4500
Don Brace03383732015-01-23 16:43:30 -06004501 c->phys_disk = dev;
4502
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004503 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004504 cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004505}
4506
Scott Teeldd0e19f2014-02-18 13:57:31 -06004507/*
4508 * Set encryption parameters for the ioaccel2 request
4509 */
4510static void set_encrypt_ioaccel2(struct ctlr_info *h,
4511 struct CommandList *c, struct io_accel2_cmd *cp)
4512{
4513 struct scsi_cmnd *cmd = c->scsi_cmd;
4514 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4515 struct raid_map_data *map = &dev->raid_map;
4516 u64 first_block;
4517
Scott Teeldd0e19f2014-02-18 13:57:31 -06004518 /* Are we doing encryption on this device */
Don Brace2b08b3e2015-01-23 16:41:09 -06004519 if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
Scott Teeldd0e19f2014-02-18 13:57:31 -06004520 return;
4521 /* Set the data encryption key index. */
4522 cp->dekindex = map->dekindex;
4523
4524 /* Set the encryption enable flag, encoded into direction field. */
4525 cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
4526
4527 /* Set encryption tweak values based on logical block address
4528 * If block size is 512, tweak value is LBA.
4529 * For other block sizes, tweak is (LBA * block size)/ 512)
4530 */
4531 switch (cmd->cmnd[0]) {
4532 /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
4533 case WRITE_6:
4534 case READ_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06004535 first_block = get_unaligned_be16(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004536 break;
4537 case WRITE_10:
4538 case READ_10:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004539 /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
4540 case WRITE_12:
4541 case READ_12:
Don Brace2b08b3e2015-01-23 16:41:09 -06004542 first_block = get_unaligned_be32(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004543 break;
4544 case WRITE_16:
4545 case READ_16:
Don Brace2b08b3e2015-01-23 16:41:09 -06004546 first_block = get_unaligned_be64(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004547 break;
4548 default:
4549 dev_err(&h->pdev->dev,
Don Brace2b08b3e2015-01-23 16:41:09 -06004550 "ERROR: %s: size (0x%x) not supported for encryption\n",
4551 __func__, cmd->cmnd[0]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004552 BUG();
4553 break;
4554 }
Don Brace2b08b3e2015-01-23 16:41:09 -06004555
4556 if (le32_to_cpu(map->volume_blk_size) != 512)
4557 first_block = first_block *
4558 le32_to_cpu(map->volume_blk_size)/512;
4559
4560 cp->tweak_lower = cpu_to_le32(first_block);
4561 cp->tweak_upper = cpu_to_le32(first_block >> 32);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004562}
4563
Scott Teelc3497752014-02-18 13:56:34 -06004564static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
4565 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004566 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004567{
4568 struct scsi_cmnd *cmd = c->scsi_cmd;
4569 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
4570 struct ioaccel2_sg_element *curr_sg;
4571 int use_sg, i;
4572 struct scatterlist *sg;
4573 u64 addr64;
4574 u32 len;
4575 u32 total_len = 0;
4576
Webb Scalesd9a729f2015-04-23 09:33:27 -05004577 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Scott Teelc3497752014-02-18 13:56:34 -06004578
Don Brace03383732015-01-23 16:43:30 -06004579 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4580 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004581 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004582 }
4583
Scott Teelc3497752014-02-18 13:56:34 -06004584 c->cmd_type = CMD_IOACCEL2;
4585 /* Adjust the DMA address to point to the accelerated command buffer */
4586 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
4587 (c->cmdindex * sizeof(*cp));
4588 BUG_ON(c->busaddr & 0x0000007F);
4589
4590 memset(cp, 0, sizeof(*cp));
4591 cp->IU_type = IOACCEL2_IU_TYPE;
4592
4593 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004594 if (use_sg < 0) {
4595 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004596 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004597 }
Scott Teelc3497752014-02-18 13:56:34 -06004598
4599 if (use_sg) {
Scott Teelc3497752014-02-18 13:56:34 -06004600 curr_sg = cp->sg;
Webb Scalesd9a729f2015-04-23 09:33:27 -05004601 if (use_sg > h->ioaccel_maxsg) {
4602 addr64 = le64_to_cpu(
4603 h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
4604 curr_sg->address = cpu_to_le64(addr64);
4605 curr_sg->length = 0;
4606 curr_sg->reserved[0] = 0;
4607 curr_sg->reserved[1] = 0;
4608 curr_sg->reserved[2] = 0;
4609 curr_sg->chain_indicator = 0x80;
4610
4611 curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
4612 }
Scott Teelc3497752014-02-18 13:56:34 -06004613 scsi_for_each_sg(cmd, sg, use_sg, i) {
4614 addr64 = (u64) sg_dma_address(sg);
4615 len = sg_dma_len(sg);
4616 total_len += len;
4617 curr_sg->address = cpu_to_le64(addr64);
4618 curr_sg->length = cpu_to_le32(len);
4619 curr_sg->reserved[0] = 0;
4620 curr_sg->reserved[1] = 0;
4621 curr_sg->reserved[2] = 0;
4622 curr_sg->chain_indicator = 0;
4623 curr_sg++;
4624 }
4625
4626 switch (cmd->sc_data_direction) {
4627 case DMA_TO_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004628 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4629 cp->direction |= IOACCEL2_DIR_DATA_OUT;
Scott Teelc3497752014-02-18 13:56:34 -06004630 break;
4631 case DMA_FROM_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004632 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4633 cp->direction |= IOACCEL2_DIR_DATA_IN;
Scott Teelc3497752014-02-18 13:56:34 -06004634 break;
4635 case DMA_NONE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004636 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4637 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004638 break;
4639 default:
4640 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4641 cmd->sc_data_direction);
4642 BUG();
4643 break;
4644 }
4645 } else {
Scott Teeldd0e19f2014-02-18 13:57:31 -06004646 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4647 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004648 }
Scott Teeldd0e19f2014-02-18 13:57:31 -06004649
4650 /* Set encryption parameters, if necessary */
4651 set_encrypt_ioaccel2(h, c, cp);
4652
Don Brace2b08b3e2015-01-23 16:41:09 -06004653 cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
Don Bracef2405db2015-01-23 16:43:09 -06004654 cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
Scott Teelc3497752014-02-18 13:56:34 -06004655 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
Scott Teelc3497752014-02-18 13:56:34 -06004656
Scott Teelc3497752014-02-18 13:56:34 -06004657 cp->data_len = cpu_to_le32(total_len);
4658 cp->err_ptr = cpu_to_le64(c->busaddr +
4659 offsetof(struct io_accel2_cmd, error_data));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004660 cp->err_len = cpu_to_le32(sizeof(cp->error_data));
Scott Teelc3497752014-02-18 13:56:34 -06004661
Webb Scalesd9a729f2015-04-23 09:33:27 -05004662 /* fill in sg elements */
4663 if (use_sg > h->ioaccel_maxsg) {
4664 cp->sg_count = 1;
Don Bracea736e9b2015-11-04 15:51:14 -06004665 cp->sg[0].length = cpu_to_le32(use_sg * sizeof(cp->sg[0]));
Webb Scalesd9a729f2015-04-23 09:33:27 -05004666 if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
4667 atomic_dec(&phys_disk->ioaccel_cmds_out);
4668 scsi_dma_unmap(cmd);
4669 return -1;
4670 }
4671 } else
4672 cp->sg_count = (u8) use_sg;
4673
Scott Teelc3497752014-02-18 13:56:34 -06004674 enqueue_cmd_and_start_io(h, c);
4675 return 0;
4676}
4677
4678/*
4679 * Queue a command to the correct I/O accelerator path.
4680 */
4681static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
4682 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004683 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004684{
Don Brace03383732015-01-23 16:43:30 -06004685 /* Try to honor the device's queue depth */
4686 if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
4687 phys_disk->queue_depth) {
4688 atomic_dec(&phys_disk->ioaccel_cmds_out);
4689 return IO_ACCEL_INELIGIBLE;
4690 }
Scott Teelc3497752014-02-18 13:56:34 -06004691 if (h->transMethod & CFGTBL_Trans_io_accel1)
4692 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004693 cdb, cdb_len, scsi3addr,
4694 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004695 else
4696 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004697 cdb, cdb_len, scsi3addr,
4698 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004699}
4700
Scott Teel6b80b182014-02-18 13:56:55 -06004701static void raid_map_helper(struct raid_map_data *map,
4702 int offload_to_mirror, u32 *map_index, u32 *current_group)
4703{
4704 if (offload_to_mirror == 0) {
4705 /* use physical disk in the first mirrored group. */
Don Brace2b08b3e2015-01-23 16:41:09 -06004706 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004707 return;
4708 }
4709 do {
4710 /* determine mirror group that *map_index indicates */
Don Brace2b08b3e2015-01-23 16:41:09 -06004711 *current_group = *map_index /
4712 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004713 if (offload_to_mirror == *current_group)
4714 continue;
Don Brace2b08b3e2015-01-23 16:41:09 -06004715 if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
Scott Teel6b80b182014-02-18 13:56:55 -06004716 /* select map index from next group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004717 *map_index += le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004718 (*current_group)++;
4719 } else {
4720 /* select map index from first group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004721 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004722 *current_group = 0;
4723 }
4724 } while (offload_to_mirror != *current_group);
4725}
4726
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004727/*
4728 * Attempt to perform offload RAID mapping for a logical volume I/O.
4729 */
4730static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4731 struct CommandList *c)
4732{
4733 struct scsi_cmnd *cmd = c->scsi_cmd;
4734 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4735 struct raid_map_data *map = &dev->raid_map;
4736 struct raid_map_disk_data *dd = &map->data[0];
4737 int is_write = 0;
4738 u32 map_index;
4739 u64 first_block, last_block;
4740 u32 block_cnt;
4741 u32 blocks_per_row;
4742 u64 first_row, last_row;
4743 u32 first_row_offset, last_row_offset;
4744 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06004745 u64 r0_first_row, r0_last_row;
4746 u32 r5or6_blocks_per_row;
4747 u64 r5or6_first_row, r5or6_last_row;
4748 u32 r5or6_first_row_offset, r5or6_last_row_offset;
4749 u32 r5or6_first_column, r5or6_last_column;
4750 u32 total_disks_per_row;
4751 u32 stripesize;
4752 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004753 u32 map_row;
4754 u32 disk_handle;
4755 u64 disk_block;
4756 u32 disk_block_cnt;
4757 u8 cdb[16];
4758 u8 cdb_len;
Don Brace2b08b3e2015-01-23 16:41:09 -06004759 u16 strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004760#if BITS_PER_LONG == 32
4761 u64 tmpdiv;
4762#endif
Scott Teel6b80b182014-02-18 13:56:55 -06004763 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004764
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004765 /* check for valid opcode, get LBA and block count */
4766 switch (cmd->cmnd[0]) {
4767 case WRITE_6:
4768 is_write = 1;
4769 case READ_6:
Don Bracec8a6c9a2015-11-04 15:50:50 -06004770 first_block = get_unaligned_be16(&cmd->cmnd[2]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004771 block_cnt = cmd->cmnd[4];
Stephen M. Cameron3fa89a02014-07-03 10:18:14 -05004772 if (block_cnt == 0)
4773 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004774 break;
4775 case WRITE_10:
4776 is_write = 1;
4777 case READ_10:
4778 first_block =
4779 (((u64) cmd->cmnd[2]) << 24) |
4780 (((u64) cmd->cmnd[3]) << 16) |
4781 (((u64) cmd->cmnd[4]) << 8) |
4782 cmd->cmnd[5];
4783 block_cnt =
4784 (((u32) cmd->cmnd[7]) << 8) |
4785 cmd->cmnd[8];
4786 break;
4787 case WRITE_12:
4788 is_write = 1;
4789 case READ_12:
4790 first_block =
4791 (((u64) cmd->cmnd[2]) << 24) |
4792 (((u64) cmd->cmnd[3]) << 16) |
4793 (((u64) cmd->cmnd[4]) << 8) |
4794 cmd->cmnd[5];
4795 block_cnt =
4796 (((u32) cmd->cmnd[6]) << 24) |
4797 (((u32) cmd->cmnd[7]) << 16) |
4798 (((u32) cmd->cmnd[8]) << 8) |
4799 cmd->cmnd[9];
4800 break;
4801 case WRITE_16:
4802 is_write = 1;
4803 case READ_16:
4804 first_block =
4805 (((u64) cmd->cmnd[2]) << 56) |
4806 (((u64) cmd->cmnd[3]) << 48) |
4807 (((u64) cmd->cmnd[4]) << 40) |
4808 (((u64) cmd->cmnd[5]) << 32) |
4809 (((u64) cmd->cmnd[6]) << 24) |
4810 (((u64) cmd->cmnd[7]) << 16) |
4811 (((u64) cmd->cmnd[8]) << 8) |
4812 cmd->cmnd[9];
4813 block_cnt =
4814 (((u32) cmd->cmnd[10]) << 24) |
4815 (((u32) cmd->cmnd[11]) << 16) |
4816 (((u32) cmd->cmnd[12]) << 8) |
4817 cmd->cmnd[13];
4818 break;
4819 default:
4820 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4821 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004822 last_block = first_block + block_cnt - 1;
4823
4824 /* check for write to non-RAID-0 */
4825 if (is_write && dev->raid_level != 0)
4826 return IO_ACCEL_INELIGIBLE;
4827
4828 /* check for invalid block or wraparound */
Don Brace2b08b3e2015-01-23 16:41:09 -06004829 if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
4830 last_block < first_block)
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004831 return IO_ACCEL_INELIGIBLE;
4832
4833 /* calculate stripe information for the request */
Don Brace2b08b3e2015-01-23 16:41:09 -06004834 blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
4835 le16_to_cpu(map->strip_size);
4836 strip_size = le16_to_cpu(map->strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004837#if BITS_PER_LONG == 32
4838 tmpdiv = first_block;
4839 (void) do_div(tmpdiv, blocks_per_row);
4840 first_row = tmpdiv;
4841 tmpdiv = last_block;
4842 (void) do_div(tmpdiv, blocks_per_row);
4843 last_row = tmpdiv;
4844 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4845 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
4846 tmpdiv = first_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06004847 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004848 first_column = tmpdiv;
4849 tmpdiv = last_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06004850 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004851 last_column = tmpdiv;
4852#else
4853 first_row = first_block / blocks_per_row;
4854 last_row = last_block / blocks_per_row;
4855 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4856 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
Don Brace2b08b3e2015-01-23 16:41:09 -06004857 first_column = first_row_offset / strip_size;
4858 last_column = last_row_offset / strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004859#endif
4860
4861 /* if this isn't a single row/column then give to the controller */
4862 if ((first_row != last_row) || (first_column != last_column))
4863 return IO_ACCEL_INELIGIBLE;
4864
4865 /* proceeding with driver mapping */
Don Brace2b08b3e2015-01-23 16:41:09 -06004866 total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
4867 le16_to_cpu(map->metadata_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004868 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06004869 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06004870 map_index = (map_row * total_disks_per_row) + first_column;
4871
4872 switch (dev->raid_level) {
4873 case HPSA_RAID_0:
4874 break; /* nothing special to do */
4875 case HPSA_RAID_1:
4876 /* Handles load balance across RAID 1 members.
4877 * (2-drive R1 and R10 with even # of drives.)
4878 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004879 */
Don Brace2b08b3e2015-01-23 16:41:09 -06004880 BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004881 if (dev->offload_to_mirror)
Don Brace2b08b3e2015-01-23 16:41:09 -06004882 map_index += le16_to_cpu(map->data_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004883 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06004884 break;
4885 case HPSA_RAID_ADM:
4886 /* Handles N-way mirrors (R1-ADM)
4887 * and R10 with # of drives divisible by 3.)
4888 */
Don Brace2b08b3e2015-01-23 16:41:09 -06004889 BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
Scott Teel6b80b182014-02-18 13:56:55 -06004890
4891 offload_to_mirror = dev->offload_to_mirror;
4892 raid_map_helper(map, offload_to_mirror,
4893 &map_index, &current_group);
4894 /* set mirror group to use next time */
4895 offload_to_mirror =
Don Brace2b08b3e2015-01-23 16:41:09 -06004896 (offload_to_mirror >=
4897 le16_to_cpu(map->layout_map_count) - 1)
Scott Teel6b80b182014-02-18 13:56:55 -06004898 ? 0 : offload_to_mirror + 1;
Scott Teel6b80b182014-02-18 13:56:55 -06004899 dev->offload_to_mirror = offload_to_mirror;
4900 /* Avoid direct use of dev->offload_to_mirror within this
4901 * function since multiple threads might simultaneously
4902 * increment it beyond the range of dev->layout_map_count -1.
4903 */
4904 break;
4905 case HPSA_RAID_5:
4906 case HPSA_RAID_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06004907 if (le16_to_cpu(map->layout_map_count) <= 1)
Scott Teel6b80b182014-02-18 13:56:55 -06004908 break;
4909
4910 /* Verify first and last block are in same RAID group */
4911 r5or6_blocks_per_row =
Don Brace2b08b3e2015-01-23 16:41:09 -06004912 le16_to_cpu(map->strip_size) *
4913 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004914 BUG_ON(r5or6_blocks_per_row == 0);
Don Brace2b08b3e2015-01-23 16:41:09 -06004915 stripesize = r5or6_blocks_per_row *
4916 le16_to_cpu(map->layout_map_count);
Scott Teel6b80b182014-02-18 13:56:55 -06004917#if BITS_PER_LONG == 32
4918 tmpdiv = first_block;
4919 first_group = do_div(tmpdiv, stripesize);
4920 tmpdiv = first_group;
4921 (void) do_div(tmpdiv, r5or6_blocks_per_row);
4922 first_group = tmpdiv;
4923 tmpdiv = last_block;
4924 last_group = do_div(tmpdiv, stripesize);
4925 tmpdiv = last_group;
4926 (void) do_div(tmpdiv, r5or6_blocks_per_row);
4927 last_group = tmpdiv;
4928#else
4929 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
4930 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
Scott Teel6b80b182014-02-18 13:56:55 -06004931#endif
Stephen M. Cameron000ff7c2014-03-13 17:12:50 -05004932 if (first_group != last_group)
Scott Teel6b80b182014-02-18 13:56:55 -06004933 return IO_ACCEL_INELIGIBLE;
4934
4935 /* Verify request is in a single row of RAID 5/6 */
4936#if BITS_PER_LONG == 32
4937 tmpdiv = first_block;
4938 (void) do_div(tmpdiv, stripesize);
4939 first_row = r5or6_first_row = r0_first_row = tmpdiv;
4940 tmpdiv = last_block;
4941 (void) do_div(tmpdiv, stripesize);
4942 r5or6_last_row = r0_last_row = tmpdiv;
4943#else
4944 first_row = r5or6_first_row = r0_first_row =
4945 first_block / stripesize;
4946 r5or6_last_row = r0_last_row = last_block / stripesize;
4947#endif
4948 if (r5or6_first_row != r5or6_last_row)
4949 return IO_ACCEL_INELIGIBLE;
4950
4951
4952 /* Verify request is in a single column */
4953#if BITS_PER_LONG == 32
4954 tmpdiv = first_block;
4955 first_row_offset = do_div(tmpdiv, stripesize);
4956 tmpdiv = first_row_offset;
4957 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
4958 r5or6_first_row_offset = first_row_offset;
4959 tmpdiv = last_block;
4960 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
4961 tmpdiv = r5or6_last_row_offset;
4962 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
4963 tmpdiv = r5or6_first_row_offset;
4964 (void) do_div(tmpdiv, map->strip_size);
4965 first_column = r5or6_first_column = tmpdiv;
4966 tmpdiv = r5or6_last_row_offset;
4967 (void) do_div(tmpdiv, map->strip_size);
4968 r5or6_last_column = tmpdiv;
4969#else
4970 first_row_offset = r5or6_first_row_offset =
4971 (u32)((first_block % stripesize) %
4972 r5or6_blocks_per_row);
4973
4974 r5or6_last_row_offset =
4975 (u32)((last_block % stripesize) %
4976 r5or6_blocks_per_row);
4977
4978 first_column = r5or6_first_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06004979 r5or6_first_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06004980 r5or6_last_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06004981 r5or6_last_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06004982#endif
4983 if (r5or6_first_column != r5or6_last_column)
4984 return IO_ACCEL_INELIGIBLE;
4985
4986 /* Request is eligible */
4987 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06004988 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06004989
4990 map_index = (first_group *
Don Brace2b08b3e2015-01-23 16:41:09 -06004991 (le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
Scott Teel6b80b182014-02-18 13:56:55 -06004992 (map_row * total_disks_per_row) + first_column;
4993 break;
4994 default:
4995 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004996 }
Scott Teel6b80b182014-02-18 13:56:55 -06004997
Stephen Cameron07543e02015-01-23 16:44:14 -06004998 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
4999 return IO_ACCEL_INELIGIBLE;
5000
Don Brace03383732015-01-23 16:43:30 -06005001 c->phys_disk = dev->phys_disk[map_index];
Don Bracec3390df2016-02-23 15:16:34 -06005002 if (!c->phys_disk)
5003 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06005004
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005005 disk_handle = dd[map_index].ioaccel_handle;
Don Brace2b08b3e2015-01-23 16:41:09 -06005006 disk_block = le64_to_cpu(map->disk_starting_blk) +
5007 first_row * le16_to_cpu(map->strip_size) +
5008 (first_row_offset - first_column *
5009 le16_to_cpu(map->strip_size));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005010 disk_block_cnt = block_cnt;
5011
5012 /* handle differing logical/physical block sizes */
5013 if (map->phys_blk_shift) {
5014 disk_block <<= map->phys_blk_shift;
5015 disk_block_cnt <<= map->phys_blk_shift;
5016 }
5017 BUG_ON(disk_block_cnt > 0xffff);
5018
5019 /* build the new CDB for the physical disk I/O */
5020 if (disk_block > 0xffffffff) {
5021 cdb[0] = is_write ? WRITE_16 : READ_16;
5022 cdb[1] = 0;
5023 cdb[2] = (u8) (disk_block >> 56);
5024 cdb[3] = (u8) (disk_block >> 48);
5025 cdb[4] = (u8) (disk_block >> 40);
5026 cdb[5] = (u8) (disk_block >> 32);
5027 cdb[6] = (u8) (disk_block >> 24);
5028 cdb[7] = (u8) (disk_block >> 16);
5029 cdb[8] = (u8) (disk_block >> 8);
5030 cdb[9] = (u8) (disk_block);
5031 cdb[10] = (u8) (disk_block_cnt >> 24);
5032 cdb[11] = (u8) (disk_block_cnt >> 16);
5033 cdb[12] = (u8) (disk_block_cnt >> 8);
5034 cdb[13] = (u8) (disk_block_cnt);
5035 cdb[14] = 0;
5036 cdb[15] = 0;
5037 cdb_len = 16;
5038 } else {
5039 cdb[0] = is_write ? WRITE_10 : READ_10;
5040 cdb[1] = 0;
5041 cdb[2] = (u8) (disk_block >> 24);
5042 cdb[3] = (u8) (disk_block >> 16);
5043 cdb[4] = (u8) (disk_block >> 8);
5044 cdb[5] = (u8) (disk_block);
5045 cdb[6] = 0;
5046 cdb[7] = (u8) (disk_block_cnt >> 8);
5047 cdb[8] = (u8) (disk_block_cnt);
5048 cdb[9] = 0;
5049 cdb_len = 10;
5050 }
5051 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
Don Brace03383732015-01-23 16:43:30 -06005052 dev->scsi3addr,
5053 dev->phys_disk[map_index]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005054}
5055
Webb Scales25163bd2015-04-23 09:32:00 -05005056/*
5057 * Submit commands down the "normal" RAID stack path
5058 * All callers to hpsa_ciss_submit must check lockup_detected
5059 * beforehand, before (opt.) and after calling cmd_alloc
5060 */
Stephen Cameron574f05d2015-01-23 16:43:20 -06005061static int hpsa_ciss_submit(struct ctlr_info *h,
5062 struct CommandList *c, struct scsi_cmnd *cmd,
5063 unsigned char scsi3addr[])
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005064{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005065 cmd->host_scribble = (unsigned char *) c;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005066 c->cmd_type = CMD_SCSI;
5067 c->scsi_cmd = cmd;
5068 c->Header.ReplyQueue = 0; /* unused in simple mode */
5069 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Bracef2405db2015-01-23 16:43:09 -06005070 c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005071
5072 /* Fill in the request block... */
5073
5074 c->Request.Timeout = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005075 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
5076 c->Request.CDBLen = cmd->cmd_len;
5077 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005078 switch (cmd->sc_data_direction) {
5079 case DMA_TO_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005080 c->Request.type_attr_dir =
5081 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005082 break;
5083 case DMA_FROM_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005084 c->Request.type_attr_dir =
5085 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005086 break;
5087 case DMA_NONE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005088 c->Request.type_attr_dir =
5089 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005090 break;
5091 case DMA_BIDIRECTIONAL:
5092 /* This can happen if a buggy application does a scsi passthru
5093 * and sets both inlen and outlen to non-zero. ( see
5094 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
5095 */
5096
Stephen M. Camerona505b862014-11-14 17:27:04 -06005097 c->Request.type_attr_dir =
5098 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005099 /* This is technically wrong, and hpsa controllers should
5100 * reject it with CMD_INVALID, which is the most correct
5101 * response, but non-fibre backends appear to let it
5102 * slide by, and give the same results as if this field
5103 * were set correctly. Either way is acceptable for
5104 * our purposes here.
5105 */
5106
5107 break;
5108
5109 default:
5110 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
5111 cmd->sc_data_direction);
5112 BUG();
5113 break;
5114 }
5115
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005116 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Webb Scales73153fe2015-04-23 09:35:04 -05005117 hpsa_cmd_resolve_and_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005118 return SCSI_MLQUEUE_HOST_BUSY;
5119 }
5120 enqueue_cmd_and_start_io(h, c);
5121 /* the cmd'll come back via intr handler in complete_scsi_command() */
5122 return 0;
5123}
5124
Stephen Cameron360c73b2015-04-23 09:32:32 -05005125static void hpsa_cmd_init(struct ctlr_info *h, int index,
5126 struct CommandList *c)
5127{
5128 dma_addr_t cmd_dma_handle, err_dma_handle;
5129
5130 /* Zero out all of commandlist except the last field, refcount */
5131 memset(c, 0, offsetof(struct CommandList, refcount));
5132 c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
5133 cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
5134 c->err_info = h->errinfo_pool + index;
5135 memset(c->err_info, 0, sizeof(*c->err_info));
5136 err_dma_handle = h->errinfo_pool_dhandle
5137 + index * sizeof(*c->err_info);
5138 c->cmdindex = index;
5139 c->busaddr = (u32) cmd_dma_handle;
5140 c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
5141 c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
5142 c->h = h;
Webb Scalesa58e7e52015-04-23 09:34:16 -05005143 c->scsi_cmd = SCSI_CMD_IDLE;
Stephen Cameron360c73b2015-04-23 09:32:32 -05005144}
5145
5146static void hpsa_preinitialize_commands(struct ctlr_info *h)
5147{
5148 int i;
5149
5150 for (i = 0; i < h->nr_cmds; i++) {
5151 struct CommandList *c = h->cmd_pool + i;
5152
5153 hpsa_cmd_init(h, i, c);
5154 atomic_set(&c->refcount, 0);
5155 }
5156}
5157
5158static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
5159 struct CommandList *c)
5160{
5161 dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
5162
Webb Scales73153fe2015-04-23 09:35:04 -05005163 BUG_ON(c->cmdindex != index);
5164
Stephen Cameron360c73b2015-04-23 09:32:32 -05005165 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
5166 memset(c->err_info, 0, sizeof(*c->err_info));
5167 c->busaddr = (u32) cmd_dma_handle;
5168}
5169
Webb Scales592a0ad2015-04-23 09:32:48 -05005170static int hpsa_ioaccel_submit(struct ctlr_info *h,
5171 struct CommandList *c, struct scsi_cmnd *cmd,
5172 unsigned char *scsi3addr)
5173{
5174 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
5175 int rc = IO_ACCEL_INELIGIBLE;
5176
5177 cmd->host_scribble = (unsigned char *) c;
5178
5179 if (dev->offload_enabled) {
5180 hpsa_cmd_init(h, c->cmdindex, c);
5181 c->cmd_type = CMD_SCSI;
5182 c->scsi_cmd = cmd;
5183 rc = hpsa_scsi_ioaccel_raid_map(h, c);
5184 if (rc < 0) /* scsi_dma_map failed. */
5185 rc = SCSI_MLQUEUE_HOST_BUSY;
Joe Handzika3144e02015-04-23 09:32:59 -05005186 } else if (dev->hba_ioaccel_enabled) {
Webb Scales592a0ad2015-04-23 09:32:48 -05005187 hpsa_cmd_init(h, c->cmdindex, c);
5188 c->cmd_type = CMD_SCSI;
5189 c->scsi_cmd = cmd;
5190 rc = hpsa_scsi_ioaccel_direct_map(h, c);
5191 if (rc < 0) /* scsi_dma_map failed. */
5192 rc = SCSI_MLQUEUE_HOST_BUSY;
5193 }
5194 return rc;
5195}
5196
Don Brace080ef1c2015-01-23 16:43:25 -06005197static void hpsa_command_resubmit_worker(struct work_struct *work)
5198{
5199 struct scsi_cmnd *cmd;
5200 struct hpsa_scsi_dev_t *dev;
Webb Scales8a0ff922015-04-23 09:34:11 -05005201 struct CommandList *c = container_of(work, struct CommandList, work);
Don Brace080ef1c2015-01-23 16:43:25 -06005202
5203 cmd = c->scsi_cmd;
5204 dev = cmd->device->hostdata;
5205 if (!dev) {
5206 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05005207 return hpsa_cmd_free_and_done(c->h, c, cmd);
Don Brace080ef1c2015-01-23 16:43:25 -06005208 }
Webb Scalesd604f532015-04-23 09:35:22 -05005209 if (c->reset_pending)
5210 return hpsa_cmd_resolve_and_free(c->h, c);
Webb Scalesa58e7e52015-04-23 09:34:16 -05005211 if (c->abort_pending)
5212 return hpsa_cmd_abort_and_free(c->h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05005213 if (c->cmd_type == CMD_IOACCEL2) {
5214 struct ctlr_info *h = c->h;
5215 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5216 int rc;
5217
5218 if (c2->error_data.serv_response ==
5219 IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
5220 rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
5221 if (rc == 0)
5222 return;
5223 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
5224 /*
5225 * If we get here, it means dma mapping failed.
5226 * Try again via scsi mid layer, which will
5227 * then get SCSI_MLQUEUE_HOST_BUSY.
5228 */
5229 cmd->result = DID_IMM_RETRY << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05005230 return hpsa_cmd_free_and_done(h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05005231 }
5232 /* else, fall thru and resubmit down CISS path */
5233 }
5234 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05005235 hpsa_cmd_partial_init(c->h, c->cmdindex, c);
Don Brace080ef1c2015-01-23 16:43:25 -06005236 if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
5237 /*
5238 * If we get here, it means dma mapping failed. Try
5239 * again via scsi mid layer, which will then get
5240 * SCSI_MLQUEUE_HOST_BUSY.
Webb Scales592a0ad2015-04-23 09:32:48 -05005241 *
5242 * hpsa_ciss_submit will have already freed c
5243 * if it encountered a dma mapping failure.
Don Brace080ef1c2015-01-23 16:43:25 -06005244 */
5245 cmd->result = DID_IMM_RETRY << 16;
5246 cmd->scsi_done(cmd);
5247 }
5248}
5249
Stephen Cameron574f05d2015-01-23 16:43:20 -06005250/* Running in struct Scsi_Host->host_lock less mode */
5251static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
5252{
5253 struct ctlr_info *h;
5254 struct hpsa_scsi_dev_t *dev;
5255 unsigned char scsi3addr[8];
5256 struct CommandList *c;
5257 int rc = 0;
5258
5259 /* Get the ptr to our adapter structure out of cmd->host. */
5260 h = sdev_to_hba(cmd->device);
Webb Scales73153fe2015-04-23 09:35:04 -05005261
5262 BUG_ON(cmd->request->tag < 0);
5263
Stephen Cameron574f05d2015-01-23 16:43:20 -06005264 dev = cmd->device->hostdata;
5265 if (!dev) {
5266 cmd->result = DID_NO_CONNECT << 16;
5267 cmd->scsi_done(cmd);
5268 return 0;
5269 }
Webb Scales73153fe2015-04-23 09:35:04 -05005270
Stephen Cameron574f05d2015-01-23 16:43:20 -06005271 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
5272
5273 if (unlikely(lockup_detected(h))) {
Webb Scales25163bd2015-04-23 09:32:00 -05005274 cmd->result = DID_NO_CONNECT << 16;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005275 cmd->scsi_done(cmd);
5276 return 0;
5277 }
Webb Scales73153fe2015-04-23 09:35:04 -05005278 c = cmd_tagged_alloc(h, cmd);
Stephen Cameron574f05d2015-01-23 16:43:20 -06005279
Stephen Cameron407863c2015-01-23 16:44:19 -06005280 /*
5281 * Call alternate submit routine for I/O accelerated commands.
Stephen Cameron574f05d2015-01-23 16:43:20 -06005282 * Retries always go down the normal I/O path.
5283 */
5284 if (likely(cmd->retries == 0 &&
5285 cmd->request->cmd_type == REQ_TYPE_FS &&
5286 h->acciopath_status)) {
Webb Scales592a0ad2015-04-23 09:32:48 -05005287 rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
5288 if (rc == 0)
5289 return 0;
5290 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
Webb Scales73153fe2015-04-23 09:35:04 -05005291 hpsa_cmd_resolve_and_free(h, c);
Webb Scales592a0ad2015-04-23 09:32:48 -05005292 return SCSI_MLQUEUE_HOST_BUSY;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005293 }
5294 }
5295 return hpsa_ciss_submit(h, c, cmd, scsi3addr);
5296}
5297
Webb Scales8ebc9242015-01-23 16:44:50 -06005298static void hpsa_scan_complete(struct ctlr_info *h)
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005299{
5300 unsigned long flags;
5301
Webb Scales8ebc9242015-01-23 16:44:50 -06005302 spin_lock_irqsave(&h->scan_lock, flags);
5303 h->scan_finished = 1;
5304 wake_up_all(&h->scan_wait_queue);
5305 spin_unlock_irqrestore(&h->scan_lock, flags);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005306}
5307
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005308static void hpsa_scan_start(struct Scsi_Host *sh)
5309{
5310 struct ctlr_info *h = shost_to_hba(sh);
5311 unsigned long flags;
5312
Webb Scales8ebc9242015-01-23 16:44:50 -06005313 /*
5314 * Don't let rescans be initiated on a controller known to be locked
5315 * up. If the controller locks up *during* a rescan, that thread is
5316 * probably hosed, but at least we can prevent new rescan threads from
5317 * piling up on a locked up controller.
5318 */
5319 if (unlikely(lockup_detected(h)))
5320 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005321
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005322 /* wait until any scan already in progress is finished. */
5323 while (1) {
5324 spin_lock_irqsave(&h->scan_lock, flags);
5325 if (h->scan_finished)
5326 break;
5327 spin_unlock_irqrestore(&h->scan_lock, flags);
5328 wait_event(h->scan_wait_queue, h->scan_finished);
5329 /* Note: We don't need to worry about a race between this
5330 * thread and driver unload because the midlayer will
5331 * have incremented the reference count, so unload won't
5332 * happen if we're in here.
5333 */
5334 }
5335 h->scan_finished = 0; /* mark scan as in progress */
5336 spin_unlock_irqrestore(&h->scan_lock, flags);
5337
Webb Scales8ebc9242015-01-23 16:44:50 -06005338 if (unlikely(lockup_detected(h)))
5339 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005340
Don Brace8aa60682015-11-04 15:50:01 -06005341 hpsa_update_scsi_devices(h);
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005342
Webb Scales8ebc9242015-01-23 16:44:50 -06005343 hpsa_scan_complete(h);
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005344}
5345
Don Brace7c0a0222015-01-23 16:41:30 -06005346static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
5347{
Don Brace03383732015-01-23 16:43:30 -06005348 struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
5349
5350 if (!logical_drive)
5351 return -ENODEV;
Don Brace7c0a0222015-01-23 16:41:30 -06005352
5353 if (qdepth < 1)
5354 qdepth = 1;
Don Brace03383732015-01-23 16:43:30 -06005355 else if (qdepth > logical_drive->queue_depth)
5356 qdepth = logical_drive->queue_depth;
5357
5358 return scsi_change_queue_depth(sdev, qdepth);
Don Brace7c0a0222015-01-23 16:41:30 -06005359}
5360
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005361static int hpsa_scan_finished(struct Scsi_Host *sh,
5362 unsigned long elapsed_time)
5363{
5364 struct ctlr_info *h = shost_to_hba(sh);
5365 unsigned long flags;
5366 int finished;
5367
5368 spin_lock_irqsave(&h->scan_lock, flags);
5369 finished = h->scan_finished;
5370 spin_unlock_irqrestore(&h->scan_lock, flags);
5371 return finished;
5372}
5373
Robert Elliott2946e822015-04-23 09:35:09 -05005374static int hpsa_scsi_host_alloc(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005375{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005376 struct Scsi_Host *sh;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005377
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005378 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
Robert Elliott2946e822015-04-23 09:35:09 -05005379 if (sh == NULL) {
5380 dev_err(&h->pdev->dev, "scsi_host_alloc failed\n");
5381 return -ENOMEM;
5382 }
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005383
5384 sh->io_port = 0;
5385 sh->n_io_port = 0;
5386 sh->this_id = -1;
5387 sh->max_channel = 3;
5388 sh->max_cmd_len = MAX_COMMAND_SIZE;
5389 sh->max_lun = HPSA_MAX_LUN;
5390 sh->max_id = HPSA_MAX_LUN;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05005391 sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
Don Brace03383732015-01-23 16:43:30 -06005392 sh->cmd_per_lun = sh->can_queue;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005393 sh->sg_tablesize = h->maxsgentries;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06005394 sh->transportt = hpsa_sas_transport_template;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005395 sh->hostdata[0] = (unsigned long) h;
5396 sh->irq = h->intr[h->intr_mode];
5397 sh->unique_id = sh->irq;
Christoph Hellwig64d513a2015-10-08 09:28:04 +01005398
Robert Elliott2946e822015-04-23 09:35:09 -05005399 h->scsi_host = sh;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005400 return 0;
Robert Elliott2946e822015-04-23 09:35:09 -05005401}
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005402
Robert Elliott2946e822015-04-23 09:35:09 -05005403static int hpsa_scsi_add_host(struct ctlr_info *h)
5404{
5405 int rv;
5406
5407 rv = scsi_add_host(h->scsi_host, &h->pdev->dev);
5408 if (rv) {
5409 dev_err(&h->pdev->dev, "scsi_add_host failed\n");
5410 return rv;
5411 }
5412 scsi_scan_host(h->scsi_host);
5413 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005414}
5415
Webb Scalesb69324f2015-04-23 09:34:22 -05005416/*
Webb Scales73153fe2015-04-23 09:35:04 -05005417 * The block layer has already gone to the trouble of picking out a unique,
5418 * small-integer tag for this request. We use an offset from that value as
5419 * an index to select our command block. (The offset allows us to reserve the
5420 * low-numbered entries for our own uses.)
5421 */
5422static int hpsa_get_cmd_index(struct scsi_cmnd *scmd)
5423{
5424 int idx = scmd->request->tag;
5425
5426 if (idx < 0)
5427 return idx;
5428
5429 /* Offset to leave space for internal cmds. */
5430 return idx += HPSA_NRESERVED_CMDS;
5431}
5432
5433/*
Webb Scalesb69324f2015-04-23 09:34:22 -05005434 * Send a TEST_UNIT_READY command to the specified LUN using the specified
5435 * reply queue; returns zero if the unit is ready, and non-zero otherwise.
5436 */
5437static int hpsa_send_test_unit_ready(struct ctlr_info *h,
5438 struct CommandList *c, unsigned char lunaddr[],
5439 int reply_queue)
5440{
5441 int rc;
5442
5443 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
5444 (void) fill_cmd(c, TEST_UNIT_READY, h,
5445 NULL, 0, 0, lunaddr, TYPE_CMD);
Don Bracec448ecf2016-04-27 17:13:51 -05005446 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Webb Scalesb69324f2015-04-23 09:34:22 -05005447 if (rc)
5448 return rc;
5449 /* no unmap needed here because no data xfer. */
5450
5451 /* Check if the unit is already ready. */
5452 if (c->err_info->CommandStatus == CMD_SUCCESS)
5453 return 0;
5454
5455 /*
5456 * The first command sent after reset will receive "unit attention" to
5457 * indicate that the LUN has been reset...this is actually what we're
5458 * looking for (but, success is good too).
5459 */
5460 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5461 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
5462 (c->err_info->SenseInfo[2] == NO_SENSE ||
5463 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
5464 return 0;
5465
5466 return 1;
5467}
5468
5469/*
5470 * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
5471 * returns zero when the unit is ready, and non-zero when giving up.
5472 */
5473static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
5474 struct CommandList *c,
5475 unsigned char lunaddr[], int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005476{
Tomas Henzl89193582014-02-21 16:25:05 -06005477 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005478 int count = 0;
5479 int waittime = 1; /* seconds */
Webb Scalesb69324f2015-04-23 09:34:22 -05005480
5481 /* Send test unit ready until device ready, or give up. */
5482 for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
5483
5484 /*
5485 * Wait for a bit. do this first, because if we send
5486 * the TUR right away, the reset will just abort it.
5487 */
5488 msleep(1000 * waittime);
5489
5490 rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
5491 if (!rc)
5492 break;
5493
5494 /* Increase wait time with each try, up to a point. */
5495 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
5496 waittime *= 2;
5497
5498 dev_warn(&h->pdev->dev,
5499 "waiting %d secs for device to become ready.\n",
5500 waittime);
5501 }
5502
5503 return rc;
5504}
5505
5506static int wait_for_device_to_become_ready(struct ctlr_info *h,
5507 unsigned char lunaddr[],
5508 int reply_queue)
5509{
5510 int first_queue;
5511 int last_queue;
5512 int rq;
5513 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005514 struct CommandList *c;
5515
Stephen Cameron45fcb862015-01-23 16:43:04 -06005516 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005517
Webb Scalesb69324f2015-04-23 09:34:22 -05005518 /*
5519 * If no specific reply queue was requested, then send the TUR
5520 * repeatedly, requesting a reply on each reply queue; otherwise execute
5521 * the loop exactly once using only the specified queue.
5522 */
5523 if (reply_queue == DEFAULT_REPLY_QUEUE) {
5524 first_queue = 0;
5525 last_queue = h->nreply_queues - 1;
5526 } else {
5527 first_queue = reply_queue;
5528 last_queue = reply_queue;
5529 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005530
Webb Scalesb69324f2015-04-23 09:34:22 -05005531 for (rq = first_queue; rq <= last_queue; rq++) {
5532 rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
Webb Scales25163bd2015-04-23 09:32:00 -05005533 if (rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005534 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005535 }
5536
5537 if (rc)
5538 dev_warn(&h->pdev->dev, "giving up on device.\n");
5539 else
5540 dev_warn(&h->pdev->dev, "device is ready.\n");
5541
Stephen Cameron45fcb862015-01-23 16:43:04 -06005542 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005543 return rc;
5544}
5545
5546/* Need at least one of these error handlers to keep ../scsi/hosts.c from
5547 * complaining. Doing a host- or bus-reset can't do anything good here.
5548 */
5549static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
5550{
5551 int rc;
5552 struct ctlr_info *h;
5553 struct hpsa_scsi_dev_t *dev;
Scott Teel0b9b7b62015-11-04 15:51:02 -06005554 u8 reset_type;
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005555 char msg[48];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005556
5557 /* find the controller to which the command to be aborted was sent */
5558 h = sdev_to_hba(scsicmd->device);
5559 if (h == NULL) /* paranoia */
5560 return FAILED;
Don Bracee3458932015-01-23 16:44:24 -06005561
5562 if (lockup_detected(h))
5563 return FAILED;
5564
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005565 dev = scsicmd->device->hostdata;
5566 if (!dev) {
Webb Scalesd604f532015-04-23 09:35:22 -05005567 dev_err(&h->pdev->dev, "%s: device lookup failed\n", __func__);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005568 return FAILED;
5569 }
Webb Scales25163bd2015-04-23 09:32:00 -05005570
5571 /* if controller locked up, we can guarantee command won't complete */
5572 if (lockup_detected(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005573 snprintf(msg, sizeof(msg),
5574 "cmd %d RESET FAILED, lockup detected",
5575 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005576 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005577 return FAILED;
5578 }
5579
5580 /* this reset request might be the result of a lockup; check */
5581 if (detect_controller_lockup(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005582 snprintf(msg, sizeof(msg),
5583 "cmd %d RESET FAILED, new lockup detected",
5584 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005585 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005586 return FAILED;
5587 }
5588
Webb Scalesd604f532015-04-23 09:35:22 -05005589 /* Do not attempt on controller */
5590 if (is_hba_lunid(dev->scsi3addr))
5591 return SUCCESS;
5592
Scott Teel0b9b7b62015-11-04 15:51:02 -06005593 if (is_logical_dev_addr_mode(dev->scsi3addr))
5594 reset_type = HPSA_DEVICE_RESET_MSG;
5595 else
5596 reset_type = HPSA_PHYS_TARGET_RESET;
5597
5598 sprintf(msg, "resetting %s",
5599 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ");
5600 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005601
Don Braceda03ded2015-11-04 15:50:56 -06005602 h->reset_in_progress = 1;
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06005603
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005604 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teel0b9b7b62015-11-04 15:51:02 -06005605 rc = hpsa_do_reset(h, dev, dev->scsi3addr, reset_type,
Webb Scalesd604f532015-04-23 09:35:22 -05005606 DEFAULT_REPLY_QUEUE);
Scott Teel0b9b7b62015-11-04 15:51:02 -06005607 sprintf(msg, "reset %s %s",
5608 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ",
5609 rc == 0 ? "completed successfully" : "failed");
Webb Scalesd604f532015-04-23 09:35:22 -05005610 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Don Braceda03ded2015-11-04 15:50:56 -06005611 h->reset_in_progress = 0;
Webb Scalesd604f532015-04-23 09:35:22 -05005612 return rc == 0 ? SUCCESS : FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005613}
5614
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005615static void swizzle_abort_tag(u8 *tag)
5616{
5617 u8 original_tag[8];
5618
5619 memcpy(original_tag, tag, 8);
5620 tag[0] = original_tag[3];
5621 tag[1] = original_tag[2];
5622 tag[2] = original_tag[1];
5623 tag[3] = original_tag[0];
5624 tag[4] = original_tag[7];
5625 tag[5] = original_tag[6];
5626 tag[6] = original_tag[5];
5627 tag[7] = original_tag[4];
5628}
5629
Scott Teel17eb87d2014-02-18 13:55:28 -06005630static void hpsa_get_tag(struct ctlr_info *h,
Don Brace2b08b3e2015-01-23 16:41:09 -06005631 struct CommandList *c, __le32 *taglower, __le32 *tagupper)
Scott Teel17eb87d2014-02-18 13:55:28 -06005632{
Don Brace2b08b3e2015-01-23 16:41:09 -06005633 u64 tag;
Scott Teel17eb87d2014-02-18 13:55:28 -06005634 if (c->cmd_type == CMD_IOACCEL1) {
5635 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
5636 &h->ioaccel_cmd_pool[c->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06005637 tag = le64_to_cpu(cm1->tag);
5638 *tagupper = cpu_to_le32(tag >> 32);
5639 *taglower = cpu_to_le32(tag);
Scott Teel54b6e9e2014-02-18 13:56:45 -06005640 return;
Scott Teel17eb87d2014-02-18 13:55:28 -06005641 }
Scott Teel54b6e9e2014-02-18 13:56:45 -06005642 if (c->cmd_type == CMD_IOACCEL2) {
5643 struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
5644 &h->ioaccel2_cmd_pool[c->cmdindex];
Scott Teeldd0e19f2014-02-18 13:57:31 -06005645 /* upper tag not used in ioaccel2 mode */
5646 memset(tagupper, 0, sizeof(*tagupper));
5647 *taglower = cm2->Tag;
Scott Teel54b6e9e2014-02-18 13:56:45 -06005648 return;
5649 }
Don Brace2b08b3e2015-01-23 16:41:09 -06005650 tag = le64_to_cpu(c->Header.tag);
5651 *tagupper = cpu_to_le32(tag >> 32);
5652 *taglower = cpu_to_le32(tag);
Scott Teel17eb87d2014-02-18 13:55:28 -06005653}
5654
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005655static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005656 struct CommandList *abort, int reply_queue)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005657{
5658 int rc = IO_OK;
5659 struct CommandList *c;
5660 struct ErrorInfo *ei;
Don Brace2b08b3e2015-01-23 16:41:09 -06005661 __le32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005662
Stephen Cameron45fcb862015-01-23 16:43:04 -06005663 c = cmd_alloc(h);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005664
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005665 /* fill_cmd can't fail here, no buffer to map */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005666 (void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005667 0, 0, scsi3addr, TYPE_MSG);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005668 if (h->needs_abort_tags_swizzled)
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005669 swizzle_abort_tag(&c->Request.CDB[4]);
Don Bracec448ecf2016-04-27 17:13:51 -05005670 (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Scott Teel17eb87d2014-02-18 13:55:28 -06005671 hpsa_get_tag(h, abort, &taglower, &tagupper);
Webb Scales25163bd2015-04-23 09:32:00 -05005672 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06005673 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005674 /* no unmap needed here because no data xfer. */
5675
5676 ei = c->err_info;
5677 switch (ei->CommandStatus) {
5678 case CMD_SUCCESS:
5679 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05005680 case CMD_TMF_STATUS:
5681 rc = hpsa_evaluate_tmf_status(h, c);
5682 break;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005683 case CMD_UNABORTABLE: /* Very common, don't make noise. */
5684 rc = -1;
5685 break;
5686 default:
5687 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06005688 __func__, tagupper, taglower);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06005689 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005690 rc = -1;
5691 break;
5692 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06005693 cmd_free(h, c);
Scott Teeldd0e19f2014-02-18 13:57:31 -06005694 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
5695 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005696 return rc;
5697}
5698
Stephen Cameron8be986c2015-04-23 09:34:06 -05005699static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
5700 struct CommandList *command_to_abort, int reply_queue)
5701{
5702 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5703 struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
5704 struct io_accel2_cmd *c2a =
5705 &h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
Webb Scalesa58e7e52015-04-23 09:34:16 -05005706 struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
Stephen Cameron8be986c2015-04-23 09:34:06 -05005707 struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
5708
5709 /*
5710 * We're overlaying struct hpsa_tmf_struct on top of something which
5711 * was allocated as a struct io_accel2_cmd, so we better be sure it
5712 * actually fits, and doesn't overrun the error info space.
5713 */
5714 BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
5715 sizeof(struct io_accel2_cmd));
5716 BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
5717 offsetof(struct hpsa_tmf_struct, error_len) +
5718 sizeof(ac->error_len));
5719
5720 c->cmd_type = IOACCEL2_TMF;
Webb Scalesa58e7e52015-04-23 09:34:16 -05005721 c->scsi_cmd = SCSI_CMD_BUSY;
5722
Stephen Cameron8be986c2015-04-23 09:34:06 -05005723 /* Adjust the DMA address to point to the accelerated command buffer */
5724 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
5725 (c->cmdindex * sizeof(struct io_accel2_cmd));
5726 BUG_ON(c->busaddr & 0x0000007F);
5727
5728 memset(ac, 0, sizeof(*c2)); /* yes this is correct */
5729 ac->iu_type = IOACCEL2_IU_TMF_TYPE;
5730 ac->reply_queue = reply_queue;
5731 ac->tmf = IOACCEL2_TMF_ABORT;
5732 ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
5733 memset(ac->lun_id, 0, sizeof(ac->lun_id));
5734 ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
5735 ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
5736 ac->error_ptr = cpu_to_le64(c->busaddr +
5737 offsetof(struct io_accel2_cmd, error_data));
5738 ac->error_len = cpu_to_le32(sizeof(c2->error_data));
5739}
5740
Scott Teel54b6e9e2014-02-18 13:56:45 -06005741/* ioaccel2 path firmware cannot handle abort task requests.
5742 * Change abort requests to physical target reset, and send to the
5743 * address of the physical disk used for the ioaccel 2 command.
5744 * Return 0 on success (IO_OK)
5745 * -1 on failure
5746 */
5747
5748static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05005749 unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
Scott Teel54b6e9e2014-02-18 13:56:45 -06005750{
5751 int rc = IO_OK;
5752 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
5753 struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
5754 unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
5755 unsigned char *psa = &phys_scsi3addr[0];
5756
5757 /* Get a pointer to the hpsa logical device. */
Stephen Cameron7fa30302015-01-23 16:44:30 -06005758 scmd = abort->scsi_cmd;
Scott Teel54b6e9e2014-02-18 13:56:45 -06005759 dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
5760 if (dev == NULL) {
5761 dev_warn(&h->pdev->dev,
5762 "Cannot abort: no device pointer for command.\n");
5763 return -1; /* not abortable */
5764 }
5765
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005766 if (h->raid_offload_debug > 0)
5767 dev_info(&h->pdev->dev,
Webb Scales0d96ef52015-04-23 09:31:55 -05005768 "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 -06005769 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
Webb Scales0d96ef52015-04-23 09:31:55 -05005770 "Reset as abort",
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005771 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
5772 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
5773
Scott Teel54b6e9e2014-02-18 13:56:45 -06005774 if (!dev->offload_enabled) {
5775 dev_warn(&h->pdev->dev,
5776 "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
5777 return -1; /* not abortable */
5778 }
5779
5780 /* Incoming scsi3addr is logical addr. We need physical disk addr. */
5781 if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
5782 dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
5783 return -1; /* not abortable */
5784 }
5785
5786 /* send the reset */
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005787 if (h->raid_offload_debug > 0)
5788 dev_info(&h->pdev->dev,
5789 "Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5790 psa[0], psa[1], psa[2], psa[3],
5791 psa[4], psa[5], psa[6], psa[7]);
Webb Scalesd604f532015-04-23 09:35:22 -05005792 rc = hpsa_do_reset(h, dev, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
Scott Teel54b6e9e2014-02-18 13:56:45 -06005793 if (rc != 0) {
5794 dev_warn(&h->pdev->dev,
5795 "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5796 psa[0], psa[1], psa[2], psa[3],
5797 psa[4], psa[5], psa[6], psa[7]);
5798 return rc; /* failed to reset */
5799 }
5800
5801 /* wait for device to recover */
Webb Scalesb69324f2015-04-23 09:34:22 -05005802 if (wait_for_device_to_become_ready(h, psa, reply_queue) != 0) {
Scott Teel54b6e9e2014-02-18 13:56:45 -06005803 dev_warn(&h->pdev->dev,
5804 "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5805 psa[0], psa[1], psa[2], psa[3],
5806 psa[4], psa[5], psa[6], psa[7]);
5807 return -1; /* failed to recover */
5808 }
5809
5810 /* device recovered */
5811 dev_info(&h->pdev->dev,
5812 "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5813 psa[0], psa[1], psa[2], psa[3],
5814 psa[4], psa[5], psa[6], psa[7]);
5815
5816 return rc; /* success */
5817}
5818
Stephen Cameron8be986c2015-04-23 09:34:06 -05005819static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
5820 struct CommandList *abort, int reply_queue)
5821{
5822 int rc = IO_OK;
5823 struct CommandList *c;
5824 __le32 taglower, tagupper;
5825 struct hpsa_scsi_dev_t *dev;
5826 struct io_accel2_cmd *c2;
5827
5828 dev = abort->scsi_cmd->device->hostdata;
5829 if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
5830 return -1;
5831
5832 c = cmd_alloc(h);
5833 setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
5834 c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
Don Bracec448ecf2016-04-27 17:13:51 -05005835 (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Stephen Cameron8be986c2015-04-23 09:34:06 -05005836 hpsa_get_tag(h, abort, &taglower, &tagupper);
5837 dev_dbg(&h->pdev->dev,
5838 "%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
5839 __func__, tagupper, taglower);
5840 /* no unmap needed here because no data xfer. */
5841
5842 dev_dbg(&h->pdev->dev,
5843 "%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
5844 __func__, tagupper, taglower, c2->error_data.serv_response);
5845 switch (c2->error_data.serv_response) {
5846 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
5847 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
5848 rc = 0;
5849 break;
5850 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
5851 case IOACCEL2_SERV_RESPONSE_FAILURE:
5852 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
5853 rc = -1;
5854 break;
5855 default:
5856 dev_warn(&h->pdev->dev,
5857 "%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
5858 __func__, tagupper, taglower,
5859 c2->error_data.serv_response);
5860 rc = -1;
5861 }
5862 cmd_free(h, c);
5863 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
5864 tagupper, taglower);
5865 return rc;
5866}
5867
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005868static int hpsa_send_abort_both_ways(struct ctlr_info *h,
Don Brace39f3deb2016-02-23 15:16:28 -06005869 struct hpsa_scsi_dev_t *dev, struct CommandList *abort, int reply_queue)
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005870{
Stephen Cameron8be986c2015-04-23 09:34:06 -05005871 /*
5872 * ioccelerator mode 2 commands should be aborted via the
Scott Teel54b6e9e2014-02-18 13:56:45 -06005873 * accelerated path, since RAID path is unaware of these commands,
Stephen Cameron8be986c2015-04-23 09:34:06 -05005874 * but not all underlying firmware can handle abort TMF.
5875 * Change abort to physical device reset when abort TMF is unsupported.
Scott Teel54b6e9e2014-02-18 13:56:45 -06005876 */
Stephen Cameron8be986c2015-04-23 09:34:06 -05005877 if (abort->cmd_type == CMD_IOACCEL2) {
Don Brace39f3deb2016-02-23 15:16:28 -06005878 if ((HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags) ||
5879 dev->physical_device)
Stephen Cameron8be986c2015-04-23 09:34:06 -05005880 return hpsa_send_abort_ioaccel2(h, abort,
5881 reply_queue);
5882 else
Don Brace39f3deb2016-02-23 15:16:28 -06005883 return hpsa_send_reset_as_abort_ioaccel2(h,
5884 dev->scsi3addr,
Webb Scales25163bd2015-04-23 09:32:00 -05005885 abort, reply_queue);
Stephen Cameron8be986c2015-04-23 09:34:06 -05005886 }
Don Brace39f3deb2016-02-23 15:16:28 -06005887 return hpsa_send_abort(h, dev->scsi3addr, abort, reply_queue);
Webb Scales25163bd2015-04-23 09:32:00 -05005888}
5889
5890/* Find out which reply queue a command was meant to return on */
5891static int hpsa_extract_reply_queue(struct ctlr_info *h,
5892 struct CommandList *c)
5893{
5894 if (c->cmd_type == CMD_IOACCEL2)
5895 return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
5896 return c->Header.ReplyQueue;
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005897}
5898
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005899/*
5900 * Limit concurrency of abort commands to prevent
5901 * over-subscription of commands
5902 */
5903static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
5904{
5905#define ABORT_CMD_WAIT_MSECS 5000
5906 return !wait_event_timeout(h->abort_cmd_wait_queue,
5907 atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
5908 msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
5909}
5910
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005911/* Send an abort for the specified command.
5912 * If the device and controller support it,
5913 * send a task abort request.
5914 */
5915static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
5916{
5917
Webb Scalesa58e7e52015-04-23 09:34:16 -05005918 int rc;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005919 struct ctlr_info *h;
5920 struct hpsa_scsi_dev_t *dev;
5921 struct CommandList *abort; /* pointer to command to be aborted */
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005922 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
5923 char msg[256]; /* For debug messaging. */
5924 int ml = 0;
Don Brace2b08b3e2015-01-23 16:41:09 -06005925 __le32 tagupper, taglower;
Webb Scales25163bd2015-04-23 09:32:00 -05005926 int refcount, reply_queue;
5927
5928 if (sc == NULL)
5929 return FAILED;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005930
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005931 if (sc->device == NULL)
5932 return FAILED;
5933
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005934 /* Find the controller of the command to be aborted */
5935 h = sdev_to_hba(sc->device);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005936 if (h == NULL)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005937 return FAILED;
5938
Webb Scales25163bd2015-04-23 09:32:00 -05005939 /* Find the device of the command to be aborted */
5940 dev = sc->device->hostdata;
5941 if (!dev) {
5942 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
5943 msg);
Don Bracee3458932015-01-23 16:44:24 -06005944 return FAILED;
Webb Scales25163bd2015-04-23 09:32:00 -05005945 }
5946
5947 /* If controller locked up, we can guarantee command won't complete */
5948 if (lockup_detected(h)) {
5949 hpsa_show_dev_msg(KERN_WARNING, h, dev,
5950 "ABORT FAILED, lockup detected");
5951 return FAILED;
5952 }
5953
5954 /* This is a good time to check if controller lockup has occurred */
5955 if (detect_controller_lockup(h)) {
5956 hpsa_show_dev_msg(KERN_WARNING, h, dev,
5957 "ABORT FAILED, new lockup detected");
5958 return FAILED;
5959 }
Don Bracee3458932015-01-23 16:44:24 -06005960
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005961 /* Check that controller supports some kind of task abort */
5962 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
5963 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
5964 return FAILED;
5965
5966 memset(msg, 0, sizeof(msg));
Robert Elliott4b761552015-04-23 09:33:54 -05005967 ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005968 h->scsi_host->host_no, sc->device->channel,
Webb Scales0d96ef52015-04-23 09:31:55 -05005969 sc->device->id, sc->device->lun,
Robert Elliott4b761552015-04-23 09:33:54 -05005970 "Aborting command", sc);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005971
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005972 /* Get SCSI command to be aborted */
5973 abort = (struct CommandList *) sc->host_scribble;
5974 if (abort == NULL) {
Webb Scales281a7fd2015-01-23 16:43:35 -06005975 /* This can happen if the command already completed. */
5976 return SUCCESS;
5977 }
5978 refcount = atomic_inc_return(&abort->refcount);
5979 if (refcount == 1) { /* Command is done already. */
5980 cmd_free(h, abort);
5981 return SUCCESS;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005982 }
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005983
5984 /* Don't bother trying the abort if we know it won't work. */
5985 if (abort->cmd_type != CMD_IOACCEL2 &&
5986 abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
5987 cmd_free(h, abort);
5988 return FAILED;
5989 }
5990
Webb Scalesa58e7e52015-04-23 09:34:16 -05005991 /*
5992 * Check that we're aborting the right command.
5993 * It's possible the CommandList already completed and got re-used.
5994 */
5995 if (abort->scsi_cmd != sc) {
5996 cmd_free(h, abort);
5997 return SUCCESS;
5998 }
5999
6000 abort->abort_pending = true;
Scott Teel17eb87d2014-02-18 13:55:28 -06006001 hpsa_get_tag(h, abort, &taglower, &tagupper);
Webb Scales25163bd2015-04-23 09:32:00 -05006002 reply_queue = hpsa_extract_reply_queue(h, abort);
Scott Teel17eb87d2014-02-18 13:55:28 -06006003 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen Cameron7fa30302015-01-23 16:44:30 -06006004 as = abort->scsi_cmd;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006005 if (as != NULL)
Robert Elliott4b761552015-04-23 09:33:54 -05006006 ml += sprintf(msg+ml,
6007 "CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
6008 as->cmd_len, as->cmnd[0], as->cmnd[1],
6009 as->serial_number);
6010 dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
Webb Scales0d96ef52015-04-23 09:31:55 -05006011 hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
Robert Elliott4b761552015-04-23 09:33:54 -05006012
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006013 /*
6014 * Command is in flight, or possibly already completed
6015 * by the firmware (but not to the scsi mid layer) but we can't
6016 * distinguish which. Send the abort down.
6017 */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006018 if (wait_for_available_abort_cmd(h)) {
6019 dev_warn(&h->pdev->dev,
Robert Elliott4b761552015-04-23 09:33:54 -05006020 "%s FAILED, timeout waiting for an abort command to become available.\n",
6021 msg);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006022 cmd_free(h, abort);
6023 return FAILED;
6024 }
Don Brace39f3deb2016-02-23 15:16:28 -06006025 rc = hpsa_send_abort_both_ways(h, dev, abort, reply_queue);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006026 atomic_inc(&h->abort_cmds_available);
6027 wake_up_all(&h->abort_cmd_wait_queue);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006028 if (rc != 0) {
Robert Elliott4b761552015-04-23 09:33:54 -05006029 dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
Webb Scales0d96ef52015-04-23 09:31:55 -05006030 hpsa_show_dev_msg(KERN_WARNING, h, dev,
Robert Elliott4b761552015-04-23 09:33:54 -05006031 "FAILED to abort command");
Webb Scales281a7fd2015-01-23 16:43:35 -06006032 cmd_free(h, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006033 return FAILED;
6034 }
Robert Elliott4b761552015-04-23 09:33:54 -05006035 dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
Webb Scalesd604f532015-04-23 09:35:22 -05006036 wait_event(h->event_sync_wait_queue,
Webb Scalesa58e7e52015-04-23 09:34:16 -05006037 abort->scsi_cmd != sc || lockup_detected(h));
Webb Scales281a7fd2015-01-23 16:43:35 -06006038 cmd_free(h, abort);
Webb Scalesa58e7e52015-04-23 09:34:16 -05006039 return !lockup_detected(h) ? SUCCESS : FAILED;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006040}
6041
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006042/*
Webb Scales73153fe2015-04-23 09:35:04 -05006043 * For operations with an associated SCSI command, a command block is allocated
6044 * at init, and managed by cmd_tagged_alloc() and cmd_tagged_free() using the
6045 * block request tag as an index into a table of entries. cmd_tagged_free() is
6046 * the complement, although cmd_free() may be called instead.
6047 */
6048static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
6049 struct scsi_cmnd *scmd)
6050{
6051 int idx = hpsa_get_cmd_index(scmd);
6052 struct CommandList *c = h->cmd_pool + idx;
6053
6054 if (idx < HPSA_NRESERVED_CMDS || idx >= h->nr_cmds) {
6055 dev_err(&h->pdev->dev, "Bad block tag: %d not in [%d..%d]\n",
6056 idx, HPSA_NRESERVED_CMDS, h->nr_cmds - 1);
6057 /* The index value comes from the block layer, so if it's out of
6058 * bounds, it's probably not our bug.
6059 */
6060 BUG();
6061 }
6062
6063 atomic_inc(&c->refcount);
6064 if (unlikely(!hpsa_is_cmd_idle(c))) {
6065 /*
6066 * We expect that the SCSI layer will hand us a unique tag
6067 * value. Thus, there should never be a collision here between
6068 * two requests...because if the selected command isn't idle
6069 * then someone is going to be very disappointed.
6070 */
6071 dev_err(&h->pdev->dev,
6072 "tag collision (tag=%d) in cmd_tagged_alloc().\n",
6073 idx);
6074 if (c->scsi_cmd != NULL)
6075 scsi_print_command(c->scsi_cmd);
6076 scsi_print_command(scmd);
6077 }
6078
6079 hpsa_cmd_partial_init(h, idx, c);
6080 return c;
6081}
6082
6083static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c)
6084{
6085 /*
6086 * Release our reference to the block. We don't need to do anything
6087 * else to free it, because it is accessed by index. (There's no point
6088 * in checking the result of the decrement, since we cannot guarantee
6089 * that there isn't a concurrent abort which is also accessing it.)
6090 */
6091 (void)atomic_dec(&c->refcount);
6092}
6093
6094/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006095 * For operations that cannot sleep, a command block is allocated at init,
6096 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
6097 * which ones are free or in use. Lock must be held when calling this.
6098 * cmd_free() is the complement.
Robert Elliottbf43caf2015-04-23 09:33:38 -05006099 * This function never gives up and returns NULL. If it hangs,
6100 * another thread must call cmd_free() to free some tags.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006101 */
Webb Scales281a7fd2015-01-23 16:43:35 -06006102
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006103static struct CommandList *cmd_alloc(struct ctlr_info *h)
6104{
6105 struct CommandList *c;
Stephen Cameron360c73b2015-04-23 09:32:32 -05006106 int refcount, i;
Webb Scales73153fe2015-04-23 09:35:04 -05006107 int offset = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006108
Robert Elliott33811022015-01-23 16:43:41 -06006109 /*
6110 * There is some *extremely* small but non-zero chance that that
Stephen M. Cameron4c413122014-11-14 17:27:29 -06006111 * multiple threads could get in here, and one thread could
6112 * be scanning through the list of bits looking for a free
6113 * one, but the free ones are always behind him, and other
6114 * threads sneak in behind him and eat them before he can
6115 * get to them, so that while there is always a free one, a
6116 * very unlucky thread might be starved anyway, never able to
6117 * beat the other threads. In reality, this happens so
6118 * infrequently as to be indistinguishable from never.
Webb Scales73153fe2015-04-23 09:35:04 -05006119 *
6120 * Note that we start allocating commands before the SCSI host structure
6121 * is initialized. Since the search starts at bit zero, this
6122 * all works, since we have at least one command structure available;
6123 * however, it means that the structures with the low indexes have to be
6124 * reserved for driver-initiated requests, while requests from the block
6125 * layer will use the higher indexes.
Stephen M. Cameron4c413122014-11-14 17:27:29 -06006126 */
6127
Webb Scales281a7fd2015-01-23 16:43:35 -06006128 for (;;) {
Webb Scales73153fe2015-04-23 09:35:04 -05006129 i = find_next_zero_bit(h->cmd_pool_bits,
6130 HPSA_NRESERVED_CMDS,
6131 offset);
6132 if (unlikely(i >= HPSA_NRESERVED_CMDS)) {
Webb Scales281a7fd2015-01-23 16:43:35 -06006133 offset = 0;
6134 continue;
6135 }
6136 c = h->cmd_pool + i;
6137 refcount = atomic_inc_return(&c->refcount);
6138 if (unlikely(refcount > 1)) {
6139 cmd_free(h, c); /* already in use */
Webb Scales73153fe2015-04-23 09:35:04 -05006140 offset = (i + 1) % HPSA_NRESERVED_CMDS;
Webb Scales281a7fd2015-01-23 16:43:35 -06006141 continue;
6142 }
6143 set_bit(i & (BITS_PER_LONG - 1),
6144 h->cmd_pool_bits + (i / BITS_PER_LONG));
6145 break; /* it's ours now. */
6146 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05006147 hpsa_cmd_partial_init(h, i, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006148 return c;
6149}
6150
Webb Scales73153fe2015-04-23 09:35:04 -05006151/*
6152 * This is the complementary operation to cmd_alloc(). Note, however, in some
6153 * corner cases it may also be used to free blocks allocated by
6154 * cmd_tagged_alloc() in which case the ref-count decrement does the trick and
6155 * the clear-bit is harmless.
6156 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006157static void cmd_free(struct ctlr_info *h, struct CommandList *c)
6158{
Webb Scales281a7fd2015-01-23 16:43:35 -06006159 if (atomic_dec_and_test(&c->refcount)) {
6160 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006161
Webb Scales281a7fd2015-01-23 16:43:35 -06006162 i = c - h->cmd_pool;
6163 clear_bit(i & (BITS_PER_LONG - 1),
6164 h->cmd_pool_bits + (i / BITS_PER_LONG));
6165 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006166}
6167
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006168#ifdef CONFIG_COMPAT
6169
Don Brace42a91642014-11-14 17:26:27 -06006170static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
6171 void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006172{
6173 IOCTL32_Command_struct __user *arg32 =
6174 (IOCTL32_Command_struct __user *) arg;
6175 IOCTL_Command_struct arg64;
6176 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
6177 int err;
6178 u32 cp;
6179
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06006180 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006181 err = 0;
6182 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
6183 sizeof(arg64.LUN_info));
6184 err |= copy_from_user(&arg64.Request, &arg32->Request,
6185 sizeof(arg64.Request));
6186 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
6187 sizeof(arg64.error_info));
6188 err |= get_user(arg64.buf_size, &arg32->buf_size);
6189 err |= get_user(cp, &arg32->buf);
6190 arg64.buf = compat_ptr(cp);
6191 err |= copy_to_user(p, &arg64, sizeof(arg64));
6192
6193 if (err)
6194 return -EFAULT;
6195
Don Brace42a91642014-11-14 17:26:27 -06006196 err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006197 if (err)
6198 return err;
6199 err |= copy_in_user(&arg32->error_info, &p->error_info,
6200 sizeof(arg32->error_info));
6201 if (err)
6202 return -EFAULT;
6203 return err;
6204}
6205
6206static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
Don Brace42a91642014-11-14 17:26:27 -06006207 int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006208{
6209 BIG_IOCTL32_Command_struct __user *arg32 =
6210 (BIG_IOCTL32_Command_struct __user *) arg;
6211 BIG_IOCTL_Command_struct arg64;
6212 BIG_IOCTL_Command_struct __user *p =
6213 compat_alloc_user_space(sizeof(arg64));
6214 int err;
6215 u32 cp;
6216
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06006217 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006218 err = 0;
6219 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
6220 sizeof(arg64.LUN_info));
6221 err |= copy_from_user(&arg64.Request, &arg32->Request,
6222 sizeof(arg64.Request));
6223 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
6224 sizeof(arg64.error_info));
6225 err |= get_user(arg64.buf_size, &arg32->buf_size);
6226 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
6227 err |= get_user(cp, &arg32->buf);
6228 arg64.buf = compat_ptr(cp);
6229 err |= copy_to_user(p, &arg64, sizeof(arg64));
6230
6231 if (err)
6232 return -EFAULT;
6233
Don Brace42a91642014-11-14 17:26:27 -06006234 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006235 if (err)
6236 return err;
6237 err |= copy_in_user(&arg32->error_info, &p->error_info,
6238 sizeof(arg32->error_info));
6239 if (err)
6240 return -EFAULT;
6241 return err;
6242}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06006243
Don Brace42a91642014-11-14 17:26:27 -06006244static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06006245{
6246 switch (cmd) {
6247 case CCISS_GETPCIINFO:
6248 case CCISS_GETINTINFO:
6249 case CCISS_SETINTINFO:
6250 case CCISS_GETNODENAME:
6251 case CCISS_SETNODENAME:
6252 case CCISS_GETHEARTBEAT:
6253 case CCISS_GETBUSTYPES:
6254 case CCISS_GETFIRMVER:
6255 case CCISS_GETDRIVVER:
6256 case CCISS_REVALIDVOLS:
6257 case CCISS_DEREGDISK:
6258 case CCISS_REGNEWDISK:
6259 case CCISS_REGNEWD:
6260 case CCISS_RESCANDISK:
6261 case CCISS_GETLUNINFO:
6262 return hpsa_ioctl(dev, cmd, arg);
6263
6264 case CCISS_PASSTHRU32:
6265 return hpsa_ioctl32_passthru(dev, cmd, arg);
6266 case CCISS_BIG_PASSTHRU32:
6267 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
6268
6269 default:
6270 return -ENOIOCTLCMD;
6271 }
6272}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006273#endif
6274
6275static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
6276{
6277 struct hpsa_pci_info pciinfo;
6278
6279 if (!argp)
6280 return -EINVAL;
6281 pciinfo.domain = pci_domain_nr(h->pdev->bus);
6282 pciinfo.bus = h->pdev->bus->number;
6283 pciinfo.dev_fn = h->pdev->devfn;
6284 pciinfo.board_id = h->board_id;
6285 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
6286 return -EFAULT;
6287 return 0;
6288}
6289
6290static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
6291{
6292 DriverVer_type DriverVer;
6293 unsigned char vmaj, vmin, vsubmin;
6294 int rc;
6295
6296 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
6297 &vmaj, &vmin, &vsubmin);
6298 if (rc != 3) {
6299 dev_info(&h->pdev->dev, "driver version string '%s' "
6300 "unrecognized.", HPSA_DRIVER_VERSION);
6301 vmaj = 0;
6302 vmin = 0;
6303 vsubmin = 0;
6304 }
6305 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
6306 if (!argp)
6307 return -EINVAL;
6308 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
6309 return -EFAULT;
6310 return 0;
6311}
6312
6313static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6314{
6315 IOCTL_Command_struct iocommand;
6316 struct CommandList *c;
6317 char *buff = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006318 u64 temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006319 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006320
6321 if (!argp)
6322 return -EINVAL;
6323 if (!capable(CAP_SYS_RAWIO))
6324 return -EPERM;
6325 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
6326 return -EFAULT;
6327 if ((iocommand.buf_size < 1) &&
6328 (iocommand.Request.Type.Direction != XFER_NONE)) {
6329 return -EINVAL;
6330 }
6331 if (iocommand.buf_size > 0) {
6332 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
6333 if (buff == NULL)
Robert Elliott2dd02d72015-04-23 09:33:43 -05006334 return -ENOMEM;
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006335 if (iocommand.Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006336 /* Copy the data into the buffer we created */
6337 if (copy_from_user(buff, iocommand.buf,
6338 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006339 rc = -EFAULT;
6340 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006341 }
6342 } else {
6343 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006344 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006345 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006346 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006347
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006348 /* Fill in the command type */
6349 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006350 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006351 /* Fill in Command Header */
6352 c->Header.ReplyQueue = 0; /* unused in simple mode */
6353 if (iocommand.buf_size > 0) { /* buffer to fill */
6354 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006355 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006356 } else { /* no buffers to fill */
6357 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006358 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006359 }
6360 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006361
6362 /* Fill in Request block */
6363 memcpy(&c->Request, &iocommand.Request,
6364 sizeof(c->Request));
6365
6366 /* Fill in the scatter gather information */
6367 if (iocommand.buf_size > 0) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006368 temp64 = pci_map_single(h->pdev, buff,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006369 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006370 if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
6371 c->SG[0].Addr = cpu_to_le64(0);
6372 c->SG[0].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006373 rc = -ENOMEM;
6374 goto out;
6375 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006376 c->SG[0].Addr = cpu_to_le64(temp64);
6377 c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
6378 c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006379 }
Don Bracec448ecf2016-04-27 17:13:51 -05006380 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
6381 DEFAULT_TIMEOUT);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05006382 if (iocommand.buf_size > 0)
6383 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006384 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006385 if (rc) {
6386 rc = -EIO;
6387 goto out;
6388 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006389
6390 /* Copy the error information out */
6391 memcpy(&iocommand.error_info, c->err_info,
6392 sizeof(iocommand.error_info));
6393 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006394 rc = -EFAULT;
6395 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006396 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006397 if ((iocommand.Request.Type.Direction & XFER_READ) &&
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006398 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006399 /* Copy the data out of the buffer we created */
6400 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006401 rc = -EFAULT;
6402 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006403 }
6404 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006405out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006406 cmd_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006407out_kfree:
6408 kfree(buff);
6409 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006410}
6411
6412static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6413{
6414 BIG_IOCTL_Command_struct *ioc;
6415 struct CommandList *c;
6416 unsigned char **buff = NULL;
6417 int *buff_size = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006418 u64 temp64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006419 BYTE sg_used = 0;
6420 int status = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06006421 u32 left;
6422 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006423 BYTE __user *data_ptr;
6424
6425 if (!argp)
6426 return -EINVAL;
6427 if (!capable(CAP_SYS_RAWIO))
6428 return -EPERM;
6429 ioc = (BIG_IOCTL_Command_struct *)
6430 kmalloc(sizeof(*ioc), GFP_KERNEL);
6431 if (!ioc) {
6432 status = -ENOMEM;
6433 goto cleanup1;
6434 }
6435 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
6436 status = -EFAULT;
6437 goto cleanup1;
6438 }
6439 if ((ioc->buf_size < 1) &&
6440 (ioc->Request.Type.Direction != XFER_NONE)) {
6441 status = -EINVAL;
6442 goto cleanup1;
6443 }
6444 /* Check kmalloc limits using all SGs */
6445 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
6446 status = -EINVAL;
6447 goto cleanup1;
6448 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006449 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006450 status = -EINVAL;
6451 goto cleanup1;
6452 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006453 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006454 if (!buff) {
6455 status = -ENOMEM;
6456 goto cleanup1;
6457 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006458 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006459 if (!buff_size) {
6460 status = -ENOMEM;
6461 goto cleanup1;
6462 }
6463 left = ioc->buf_size;
6464 data_ptr = ioc->buf;
6465 while (left) {
6466 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
6467 buff_size[sg_used] = sz;
6468 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
6469 if (buff[sg_used] == NULL) {
6470 status = -ENOMEM;
6471 goto cleanup1;
6472 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006473 if (ioc->Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006474 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
Stephen M. Cameron0758f4f2014-07-03 10:18:03 -05006475 status = -EFAULT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006476 goto cleanup1;
6477 }
6478 } else
6479 memset(buff[sg_used], 0, sz);
6480 left -= sz;
6481 data_ptr += sz;
6482 sg_used++;
6483 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006484 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006485
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006486 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006487 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006488 c->Header.ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006489 c->Header.SGList = (u8) sg_used;
6490 c->Header.SGTotal = cpu_to_le16(sg_used);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006491 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006492 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
6493 if (ioc->buf_size > 0) {
6494 int i;
6495 for (i = 0; i < sg_used; i++) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006496 temp64 = pci_map_single(h->pdev, buff[i],
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006497 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006498 if (dma_mapping_error(&h->pdev->dev,
6499 (dma_addr_t) temp64)) {
6500 c->SG[i].Addr = cpu_to_le64(0);
6501 c->SG[i].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006502 hpsa_pci_unmap(h->pdev, c, i,
6503 PCI_DMA_BIDIRECTIONAL);
6504 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006505 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006506 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006507 c->SG[i].Addr = cpu_to_le64(temp64);
6508 c->SG[i].Len = cpu_to_le32(buff_size[i]);
6509 c->SG[i].Ext = cpu_to_le32(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006510 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006511 c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006512 }
Don Bracec448ecf2016-04-27 17:13:51 -05006513 status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
6514 DEFAULT_TIMEOUT);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006515 if (sg_used)
6516 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006517 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006518 if (status) {
6519 status = -EIO;
6520 goto cleanup0;
6521 }
6522
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006523 /* Copy the error information out */
6524 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
6525 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006526 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006527 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006528 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006529 if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006530 int i;
6531
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006532 /* Copy the data out of the buffer we created */
6533 BYTE __user *ptr = ioc->buf;
6534 for (i = 0; i < sg_used; i++) {
6535 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006536 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006537 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006538 }
6539 ptr += buff_size[i];
6540 }
6541 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006542 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006543cleanup0:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006544 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006545cleanup1:
6546 if (buff) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006547 int i;
6548
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006549 for (i = 0; i < sg_used; i++)
6550 kfree(buff[i]);
6551 kfree(buff);
6552 }
6553 kfree(buff_size);
6554 kfree(ioc);
6555 return status;
6556}
6557
6558static void check_ioctl_unit_attention(struct ctlr_info *h,
6559 struct CommandList *c)
6560{
6561 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
6562 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
6563 (void) check_for_unit_attention(h, c);
6564}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006565
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006566/*
6567 * ioctl
6568 */
Don Brace42a91642014-11-14 17:26:27 -06006569static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006570{
6571 struct ctlr_info *h;
6572 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006573 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006574
6575 h = sdev_to_hba(dev);
6576
6577 switch (cmd) {
6578 case CCISS_DEREGDISK:
6579 case CCISS_REGNEWDISK:
6580 case CCISS_REGNEWD:
Stephen M. Camerona08a84712010-02-04 08:43:16 -06006581 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006582 return 0;
6583 case CCISS_GETPCIINFO:
6584 return hpsa_getpciinfo_ioctl(h, argp);
6585 case CCISS_GETDRIVVER:
6586 return hpsa_getdrivver_ioctl(h, argp);
6587 case CCISS_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006588 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006589 return -EAGAIN;
6590 rc = hpsa_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006591 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006592 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006593 case CCISS_BIG_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006594 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006595 return -EAGAIN;
6596 rc = hpsa_big_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006597 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006598 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006599 default:
6600 return -ENOTTY;
6601 }
6602}
6603
Robert Elliottbf43caf2015-04-23 09:33:38 -05006604static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006605 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006606{
6607 struct CommandList *c;
6608
6609 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006610
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006611 /* fill_cmd can't fail here, no data buffer to map */
6612 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006613 RAID_CTLR_LUNID, TYPE_MSG);
6614 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
6615 c->waiting = NULL;
6616 enqueue_cmd_and_start_io(h, c);
6617 /* Don't wait for completion, the reset won't complete. Don't free
6618 * the command either. This is the last command we will send before
6619 * re-initializing everything, so it doesn't matter and won't leak.
6620 */
Robert Elliottbf43caf2015-04-23 09:33:38 -05006621 return;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006622}
6623
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006624static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006625 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006626 int cmd_type)
6627{
6628 int pci_dir = XFER_NONE;
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006629 u64 tag; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006630
6631 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006632 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006633 c->Header.ReplyQueue = 0;
6634 if (buff != NULL && size > 0) {
6635 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006636 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006637 } else {
6638 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006639 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006640 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006641 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
6642
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006643 if (cmd_type == TYPE_CMD) {
6644 switch (cmd) {
6645 case HPSA_INQUIRY:
6646 /* are we trying to read a vital product page */
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006647 if (page_code & VPD_PAGE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006648 c->Request.CDB[1] = 0x01;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006649 c->Request.CDB[2] = (page_code & 0xff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006650 }
6651 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006652 c->Request.type_attr_dir =
6653 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006654 c->Request.Timeout = 0;
6655 c->Request.CDB[0] = HPSA_INQUIRY;
6656 c->Request.CDB[4] = size & 0xFF;
6657 break;
6658 case HPSA_REPORT_LOG:
6659 case HPSA_REPORT_PHYS:
6660 /* Talking to controller so It's a physical command
6661 mode = 00 target = 0. Nothing to write.
6662 */
6663 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006664 c->Request.type_attr_dir =
6665 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006666 c->Request.Timeout = 0;
6667 c->Request.CDB[0] = cmd;
6668 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6669 c->Request.CDB[7] = (size >> 16) & 0xFF;
6670 c->Request.CDB[8] = (size >> 8) & 0xFF;
6671 c->Request.CDB[9] = size & 0xFF;
6672 break;
Scott Teelc2adae42015-11-04 15:52:16 -06006673 case BMIC_SENSE_DIAG_OPTIONS:
6674 c->Request.CDBLen = 16;
6675 c->Request.type_attr_dir =
6676 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6677 c->Request.Timeout = 0;
6678 /* Spec says this should be BMIC_WRITE */
6679 c->Request.CDB[0] = BMIC_READ;
6680 c->Request.CDB[6] = BMIC_SENSE_DIAG_OPTIONS;
6681 break;
6682 case BMIC_SET_DIAG_OPTIONS:
6683 c->Request.CDBLen = 16;
6684 c->Request.type_attr_dir =
6685 TYPE_ATTR_DIR(cmd_type,
6686 ATTR_SIMPLE, XFER_WRITE);
6687 c->Request.Timeout = 0;
6688 c->Request.CDB[0] = BMIC_WRITE;
6689 c->Request.CDB[6] = BMIC_SET_DIAG_OPTIONS;
6690 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006691 case HPSA_CACHE_FLUSH:
6692 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006693 c->Request.type_attr_dir =
6694 TYPE_ATTR_DIR(cmd_type,
6695 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006696 c->Request.Timeout = 0;
6697 c->Request.CDB[0] = BMIC_WRITE;
6698 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05006699 c->Request.CDB[7] = (size >> 8) & 0xFF;
6700 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006701 break;
6702 case TEST_UNIT_READY:
6703 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006704 c->Request.type_attr_dir =
6705 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006706 c->Request.Timeout = 0;
6707 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006708 case HPSA_GET_RAID_MAP:
6709 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006710 c->Request.type_attr_dir =
6711 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006712 c->Request.Timeout = 0;
6713 c->Request.CDB[0] = HPSA_CISS_READ;
6714 c->Request.CDB[1] = cmd;
6715 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6716 c->Request.CDB[7] = (size >> 16) & 0xFF;
6717 c->Request.CDB[8] = (size >> 8) & 0xFF;
6718 c->Request.CDB[9] = size & 0xFF;
6719 break;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006720 case BMIC_SENSE_CONTROLLER_PARAMETERS:
6721 c->Request.CDBLen = 10;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006722 c->Request.type_attr_dir =
6723 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006724 c->Request.Timeout = 0;
6725 c->Request.CDB[0] = BMIC_READ;
6726 c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
6727 c->Request.CDB[7] = (size >> 16) & 0xFF;
6728 c->Request.CDB[8] = (size >> 8) & 0xFF;
6729 break;
Don Brace03383732015-01-23 16:43:30 -06006730 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
6731 c->Request.CDBLen = 10;
6732 c->Request.type_attr_dir =
6733 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6734 c->Request.Timeout = 0;
6735 c->Request.CDB[0] = BMIC_READ;
6736 c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
6737 c->Request.CDB[7] = (size >> 16) & 0xFF;
6738 c->Request.CDB[8] = (size >> 8) & 0XFF;
6739 break;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06006740 case BMIC_SENSE_SUBSYSTEM_INFORMATION:
6741 c->Request.CDBLen = 10;
6742 c->Request.type_attr_dir =
6743 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6744 c->Request.Timeout = 0;
6745 c->Request.CDB[0] = BMIC_READ;
6746 c->Request.CDB[6] = BMIC_SENSE_SUBSYSTEM_INFORMATION;
6747 c->Request.CDB[7] = (size >> 16) & 0xFF;
6748 c->Request.CDB[8] = (size >> 8) & 0XFF;
6749 break;
Don Bracecca8f132015-12-22 10:36:48 -06006750 case BMIC_SENSE_STORAGE_BOX_PARAMS:
6751 c->Request.CDBLen = 10;
6752 c->Request.type_attr_dir =
6753 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6754 c->Request.Timeout = 0;
6755 c->Request.CDB[0] = BMIC_READ;
6756 c->Request.CDB[6] = BMIC_SENSE_STORAGE_BOX_PARAMS;
6757 c->Request.CDB[7] = (size >> 16) & 0xFF;
6758 c->Request.CDB[8] = (size >> 8) & 0XFF;
6759 break;
Scott Teel66749d02015-11-04 15:51:57 -06006760 case BMIC_IDENTIFY_CONTROLLER:
6761 c->Request.CDBLen = 10;
6762 c->Request.type_attr_dir =
6763 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6764 c->Request.Timeout = 0;
6765 c->Request.CDB[0] = BMIC_READ;
6766 c->Request.CDB[1] = 0;
6767 c->Request.CDB[2] = 0;
6768 c->Request.CDB[3] = 0;
6769 c->Request.CDB[4] = 0;
6770 c->Request.CDB[5] = 0;
6771 c->Request.CDB[6] = BMIC_IDENTIFY_CONTROLLER;
6772 c->Request.CDB[7] = (size >> 16) & 0xFF;
6773 c->Request.CDB[8] = (size >> 8) & 0XFF;
6774 c->Request.CDB[9] = 0;
6775 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006776 default:
6777 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
6778 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006779 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006780 }
6781 } else if (cmd_type == TYPE_MSG) {
6782 switch (cmd) {
6783
Scott Teel0b9b7b62015-11-04 15:51:02 -06006784 case HPSA_PHYS_TARGET_RESET:
6785 c->Request.CDBLen = 16;
6786 c->Request.type_attr_dir =
6787 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6788 c->Request.Timeout = 0; /* Don't time out */
6789 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
6790 c->Request.CDB[0] = HPSA_RESET;
6791 c->Request.CDB[1] = HPSA_TARGET_RESET_TYPE;
6792 /* Physical target reset needs no control bytes 4-7*/
6793 c->Request.CDB[4] = 0x00;
6794 c->Request.CDB[5] = 0x00;
6795 c->Request.CDB[6] = 0x00;
6796 c->Request.CDB[7] = 0x00;
6797 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006798 case HPSA_DEVICE_RESET_MSG:
6799 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006800 c->Request.type_attr_dir =
6801 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006802 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006803 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
6804 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05006805 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006806 /* If bytes 4-7 are zero, it means reset the */
6807 /* LunID device */
6808 c->Request.CDB[4] = 0x00;
6809 c->Request.CDB[5] = 0x00;
6810 c->Request.CDB[6] = 0x00;
6811 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006812 break;
6813 case HPSA_ABORT_MSG:
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006814 memcpy(&tag, buff, sizeof(tag));
Don Brace2b08b3e2015-01-23 16:41:09 -06006815 dev_dbg(&h->pdev->dev,
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006816 "Abort Tag:0x%016llx using rqst Tag:0x%016llx",
6817 tag, c->Header.tag);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006818 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006819 c->Request.type_attr_dir =
6820 TYPE_ATTR_DIR(cmd_type,
6821 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006822 c->Request.Timeout = 0; /* Don't time out */
6823 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
6824 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
6825 c->Request.CDB[2] = 0x00; /* reserved */
6826 c->Request.CDB[3] = 0x00; /* reserved */
6827 /* Tag to abort goes in CDB[4]-CDB[11] */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006828 memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006829 c->Request.CDB[12] = 0x00; /* reserved */
6830 c->Request.CDB[13] = 0x00; /* reserved */
6831 c->Request.CDB[14] = 0x00; /* reserved */
6832 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006833 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006834 default:
6835 dev_warn(&h->pdev->dev, "unknown message type %d\n",
6836 cmd);
6837 BUG();
6838 }
6839 } else {
6840 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
6841 BUG();
6842 }
6843
Stephen M. Camerona505b862014-11-14 17:27:04 -06006844 switch (GET_DIR(c->Request.type_attr_dir)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006845 case XFER_READ:
6846 pci_dir = PCI_DMA_FROMDEVICE;
6847 break;
6848 case XFER_WRITE:
6849 pci_dir = PCI_DMA_TODEVICE;
6850 break;
6851 case XFER_NONE:
6852 pci_dir = PCI_DMA_NONE;
6853 break;
6854 default:
6855 pci_dir = PCI_DMA_BIDIRECTIONAL;
6856 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006857 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
6858 return -1;
6859 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006860}
6861
6862/*
6863 * Map (physical) PCI mem into (virtual) kernel space
6864 */
6865static void __iomem *remap_pci_mem(ulong base, ulong size)
6866{
6867 ulong page_base = ((ulong) base) & PAGE_MASK;
6868 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba34c2012-07-26 11:34:23 -05006869 void __iomem *page_remapped = ioremap_nocache(page_base,
6870 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006871
6872 return page_remapped ? (page_remapped + page_offs) : NULL;
6873}
6874
Matt Gates254f7962012-05-01 11:43:06 -05006875static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006876{
Matt Gates254f7962012-05-01 11:43:06 -05006877 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006878}
6879
Stephen M. Cameron900c5442010-02-04 08:42:35 -06006880static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006881{
6882 return h->access.intr_pending(h);
6883}
6884
6885static inline long interrupt_not_for_us(struct ctlr_info *h)
6886{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006887 return (h->access.intr_pending(h) == 0) ||
6888 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006889}
6890
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06006891static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
6892 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006893{
6894 if (unlikely(tag_index >= h->nr_cmds)) {
6895 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
6896 return 1;
6897 }
6898 return 0;
6899}
6900
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05006901static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006902{
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006903 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06006904 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
6905 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05006906 complete_scsi_command(c);
Stephen Cameron8be986c2015-04-23 09:34:06 -05006907 else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006908 complete(c->waiting);
Stephen M. Camerona104c992010-02-04 08:42:24 -06006909}
6910
Don Brace303932f2010-02-04 08:42:40 -06006911/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05006912static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06006913 u32 raw_tag)
6914{
6915 u32 tag_index;
6916 struct CommandList *c;
6917
Don Bracef2405db2015-01-23 16:43:09 -06006918 tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05006919 if (!bad_tag(h, tag_index, raw_tag)) {
6920 c = h->cmd_pool + tag_index;
6921 finish_cmd(c);
6922 }
Don Brace303932f2010-02-04 08:42:40 -06006923}
6924
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006925/* Some controllers, like p400, will give us one interrupt
6926 * after a soft reset, even if we turned interrupts off.
6927 * Only need to check for this in the hpsa_xxx_discard_completions
6928 * functions.
6929 */
6930static int ignore_bogus_interrupt(struct ctlr_info *h)
6931{
6932 if (likely(!reset_devices))
6933 return 0;
6934
6935 if (likely(h->interrupts_enabled))
6936 return 0;
6937
6938 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
6939 "(known firmware bug.) Ignoring.\n");
6940
6941 return 1;
6942}
6943
Matt Gates254f7962012-05-01 11:43:06 -05006944/*
6945 * Convert &h->q[x] (passed to interrupt handlers) back to h.
6946 * Relies on (h-q[x] == x) being true for x such that
6947 * 0 <= x < MAX_REPLY_QUEUES.
6948 */
6949static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006950{
Matt Gates254f7962012-05-01 11:43:06 -05006951 return container_of((queue - *queue), struct ctlr_info, q[0]);
6952}
6953
6954static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
6955{
6956 struct ctlr_info *h = queue_to_hba(queue);
6957 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006958 u32 raw_tag;
6959
6960 if (ignore_bogus_interrupt(h))
6961 return IRQ_NONE;
6962
6963 if (interrupt_not_for_us(h))
6964 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006965 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006966 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05006967 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006968 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05006969 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006970 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006971 return IRQ_HANDLED;
6972}
6973
Matt Gates254f7962012-05-01 11:43:06 -05006974static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006975{
Matt Gates254f7962012-05-01 11:43:06 -05006976 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006977 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05006978 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006979
6980 if (ignore_bogus_interrupt(h))
6981 return IRQ_NONE;
6982
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006983 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05006984 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006985 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05006986 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006987 return IRQ_HANDLED;
6988}
6989
Matt Gates254f7962012-05-01 11:43:06 -05006990static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006991{
Matt Gates254f7962012-05-01 11:43:06 -05006992 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06006993 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05006994 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006995
6996 if (interrupt_not_for_us(h))
6997 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006998 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006999 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05007000 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007001 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06007002 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05007003 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007004 }
7005 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007006 return IRQ_HANDLED;
7007}
7008
Matt Gates254f7962012-05-01 11:43:06 -05007009static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007010{
Matt Gates254f7962012-05-01 11:43:06 -05007011 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007012 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05007013 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05007014
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007015 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05007016 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06007017 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06007018 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05007019 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007020 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007021 return IRQ_HANDLED;
7022}
7023
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06007024/* Send a message CDB to the firmware. Careful, this only works
7025 * in simple mode, not performant mode due to the tag lookup.
7026 * We only ever use this immediately after a controller reset.
7027 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007028static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
7029 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007030{
7031 struct Command {
7032 struct CommandListHeader CommandHeader;
7033 struct RequestBlock Request;
7034 struct ErrDescriptor ErrorDescriptor;
7035 };
7036 struct Command *cmd;
7037 static const size_t cmd_sz = sizeof(*cmd) +
7038 sizeof(cmd->ErrorDescriptor);
7039 dma_addr_t paddr64;
Don Brace2b08b3e2015-01-23 16:41:09 -06007040 __le32 paddr32;
7041 u32 tag;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007042 void __iomem *vaddr;
7043 int i, err;
7044
7045 vaddr = pci_ioremap_bar(pdev, 0);
7046 if (vaddr == NULL)
7047 return -ENOMEM;
7048
7049 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
7050 * CCISS commands, so they must be allocated from the lower 4GiB of
7051 * memory.
7052 */
7053 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
7054 if (err) {
7055 iounmap(vaddr);
Robert Elliott1eaec8f2015-01-23 16:42:37 -06007056 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007057 }
7058
7059 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
7060 if (cmd == NULL) {
7061 iounmap(vaddr);
7062 return -ENOMEM;
7063 }
7064
7065 /* This must fit, because of the 32-bit consistent DMA mask. Also,
7066 * although there's no guarantee, we assume that the address is at
7067 * least 4-byte aligned (most likely, it's page-aligned).
7068 */
Don Brace2b08b3e2015-01-23 16:41:09 -06007069 paddr32 = cpu_to_le32(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007070
7071 cmd->CommandHeader.ReplyQueue = 0;
7072 cmd->CommandHeader.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007073 cmd->CommandHeader.SGTotal = cpu_to_le16(0);
Don Brace2b08b3e2015-01-23 16:41:09 -06007074 cmd->CommandHeader.tag = cpu_to_le64(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007075 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
7076
7077 cmd->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06007078 cmd->Request.type_attr_dir =
7079 TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007080 cmd->Request.Timeout = 0; /* Don't time out */
7081 cmd->Request.CDB[0] = opcode;
7082 cmd->Request.CDB[1] = type;
7083 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007084 cmd->ErrorDescriptor.Addr =
Don Brace2b08b3e2015-01-23 16:41:09 -06007085 cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007086 cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007087
Don Brace2b08b3e2015-01-23 16:41:09 -06007088 writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007089
7090 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
7091 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Don Brace2b08b3e2015-01-23 16:41:09 -06007092 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007093 break;
7094 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
7095 }
7096
7097 iounmap(vaddr);
7098
7099 /* we leak the DMA buffer here ... no choice since the controller could
7100 * still complete the command.
7101 */
7102 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
7103 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
7104 opcode, type);
7105 return -ETIMEDOUT;
7106 }
7107
7108 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
7109
7110 if (tag & HPSA_ERROR_BIT) {
7111 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
7112 opcode, type);
7113 return -EIO;
7114 }
7115
7116 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
7117 opcode, type);
7118 return 0;
7119}
7120
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007121#define hpsa_noop(p) hpsa_message(p, 3, 0)
7122
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007123static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Don Brace42a91642014-11-14 17:26:27 -06007124 void __iomem *vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007125{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007126
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007127 if (use_doorbell) {
7128 /* For everything after the P600, the PCI power state method
7129 * of resetting the controller doesn't work, so we have this
7130 * other way using the doorbell register.
7131 */
7132 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007133 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05007134
Justin Lindley00701a92014-05-29 10:52:47 -05007135 /* PMC hardware guys tell us we need a 10 second delay after
Stephen M. Cameron85009232013-09-23 13:33:36 -05007136 * doorbell reset and before any attempt to talk to the board
7137 * at all to ensure that this actually works and doesn't fall
7138 * over in some weird corner cases.
7139 */
Justin Lindley00701a92014-05-29 10:52:47 -05007140 msleep(10000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007141 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007142
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007143 /* Quoting from the Open CISS Specification: "The Power
7144 * Management Control/Status Register (CSR) controls the power
7145 * state of the device. The normal operating state is D0,
7146 * CSR=00h. The software off state is D3, CSR=03h. To reset
7147 * the controller, place the interface device in D3 then to D0,
7148 * this causes a secondary PCI reset which will reset the
7149 * controller." */
7150
Don Brace2662cab2015-01-23 16:41:25 -06007151 int rc = 0;
7152
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007153 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
Don Brace2662cab2015-01-23 16:41:25 -06007154
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007155 /* enter the D3hot power management state */
Don Brace2662cab2015-01-23 16:41:25 -06007156 rc = pci_set_power_state(pdev, PCI_D3hot);
7157 if (rc)
7158 return rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007159
7160 msleep(500);
7161
7162 /* enter the D0 power management state */
Don Brace2662cab2015-01-23 16:41:25 -06007163 rc = pci_set_power_state(pdev, PCI_D0);
7164 if (rc)
7165 return rc;
Mike Millerc4853ef2011-10-21 08:19:43 +02007166
7167 /*
7168 * The P600 requires a small delay when changing states.
7169 * Otherwise we may think the board did not reset and we bail.
7170 * This for kdump only and is particular to the P600.
7171 */
7172 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007173 }
7174 return 0;
7175}
7176
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007177static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007178{
7179 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06007180 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007181}
7182
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007183static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007184{
7185 char *driver_version;
7186 int i, size = sizeof(cfgtable->driver_version);
7187
7188 driver_version = kmalloc(size, GFP_KERNEL);
7189 if (!driver_version)
7190 return -ENOMEM;
7191
7192 init_driver_version(driver_version, size);
7193 for (i = 0; i < size; i++)
7194 writeb(driver_version[i], &cfgtable->driver_version[i]);
7195 kfree(driver_version);
7196 return 0;
7197}
7198
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007199static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
7200 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007201{
7202 int i;
7203
7204 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
7205 driver_ver[i] = readb(&cfgtable->driver_version[i]);
7206}
7207
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007208static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007209{
7210
7211 char *driver_ver, *old_driver_ver;
7212 int rc, size = sizeof(cfgtable->driver_version);
7213
7214 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
7215 if (!old_driver_ver)
7216 return -ENOMEM;
7217 driver_ver = old_driver_ver + size;
7218
7219 /* After a reset, the 32 bytes of "driver version" in the cfgtable
7220 * should have been changed, otherwise we know the reset failed.
7221 */
7222 init_driver_version(old_driver_ver, size);
7223 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
7224 rc = !memcmp(driver_ver, old_driver_ver, size);
7225 kfree(old_driver_ver);
7226 return rc;
7227}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007228/* This does a hard reset of the controller using PCI power management
7229 * states or the using the doorbell register.
7230 */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007231static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007232{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007233 u64 cfg_offset;
7234 u32 cfg_base_addr;
7235 u64 cfg_base_addr_index;
7236 void __iomem *vaddr;
7237 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007238 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007239 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007240 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007241 u32 use_doorbell;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007242 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007243
7244 /* For controllers as old as the P600, this is very nearly
7245 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007246 *
7247 * pci_save_state(pci_dev);
7248 * pci_set_power_state(pci_dev, PCI_D3hot);
7249 * pci_set_power_state(pci_dev, PCI_D0);
7250 * pci_restore_state(pci_dev);
7251 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007252 * For controllers newer than the P600, the pci power state
7253 * method of resetting doesn't work so we have another way
7254 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007255 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05007256
Robert Elliott60f923b2015-01-23 16:42:06 -06007257 if (!ctlr_is_resettable(board_id)) {
7258 dev_warn(&pdev->dev, "Controller not resettable\n");
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06007259 return -ENODEV;
7260 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05007261
7262 /* if controller is soft- but not hard resettable... */
7263 if (!ctlr_is_hard_resettable(board_id))
7264 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05007265
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007266 /* Save the PCI command register */
7267 pci_read_config_word(pdev, 4, &command_register);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007268 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007269
7270 /* find the first memory BAR, so we can find the cfg table */
7271 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
7272 if (rc)
7273 return rc;
7274 vaddr = remap_pci_mem(paddr, 0x250);
7275 if (!vaddr)
7276 return -ENOMEM;
7277
7278 /* find cfgtable in order to check if reset via doorbell is supported */
7279 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
7280 &cfg_base_addr_index, &cfg_offset);
7281 if (rc)
7282 goto unmap_vaddr;
7283 cfgtable = remap_pci_mem(pci_resource_start(pdev,
7284 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
7285 if (!cfgtable) {
7286 rc = -ENOMEM;
7287 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007288 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007289 rc = write_driver_ver_to_cfgtable(cfgtable);
7290 if (rc)
Tomas Henzl03741d92015-01-23 16:41:14 -06007291 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007292
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007293 /* If reset via doorbell register is supported, use that.
7294 * There are two such methods. Favor the newest method.
7295 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007296 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007297 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
7298 if (use_doorbell) {
7299 use_doorbell = DOORBELL_CTLR_RESET2;
7300 } else {
7301 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
7302 if (use_doorbell) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007303 dev_warn(&pdev->dev,
7304 "Soft reset not supported. Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007305 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007306 goto unmap_cfgtable;
7307 }
7308 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007309
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007310 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
7311 if (rc)
7312 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007313
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007314 pci_restore_state(pdev);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007315 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007316
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007317 /* Some devices (notably the HP Smart Array 5i Controller)
7318 need a little pause here */
7319 msleep(HPSA_POST_RESET_PAUSE_MSECS);
7320
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007321 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
7322 if (rc) {
7323 dev_warn(&pdev->dev,
Stephen Cameron050f7142015-01-23 16:42:22 -06007324 "Failed waiting for board to become ready after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007325 goto unmap_cfgtable;
7326 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007327
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007328 rc = controller_reset_failed(vaddr);
7329 if (rc < 0)
7330 goto unmap_cfgtable;
7331 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007332 dev_warn(&pdev->dev, "Unable to successfully reset "
7333 "controller. Will try soft reset.\n");
7334 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007335 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007336 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007337 }
7338
7339unmap_cfgtable:
7340 iounmap(cfgtable);
7341
7342unmap_vaddr:
7343 iounmap(vaddr);
7344 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007345}
7346
7347/*
7348 * We cannot read the structure directly, for portability we must use
7349 * the io functions.
7350 * This is for debug only.
7351 */
Don Brace42a91642014-11-14 17:26:27 -06007352static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007353{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007354#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007355 int i;
7356 char temp_name[17];
7357
7358 dev_info(dev, "Controller Configuration information\n");
7359 dev_info(dev, "------------------------------------\n");
7360 for (i = 0; i < 4; i++)
7361 temp_name[i] = readb(&(tb->Signature[i]));
7362 temp_name[4] = '\0';
7363 dev_info(dev, " Signature = %s\n", temp_name);
7364 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
7365 dev_info(dev, " Transport methods supported = 0x%x\n",
7366 readl(&(tb->TransportSupport)));
7367 dev_info(dev, " Transport methods active = 0x%x\n",
7368 readl(&(tb->TransportActive)));
7369 dev_info(dev, " Requested transport Method = 0x%x\n",
7370 readl(&(tb->HostWrite.TransportRequest)));
7371 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
7372 readl(&(tb->HostWrite.CoalIntDelay)));
7373 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
7374 readl(&(tb->HostWrite.CoalIntCount)));
Robert Elliott69d6e332015-01-23 16:41:56 -06007375 dev_info(dev, " Max outstanding commands = %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007376 readl(&(tb->CmdsOutMax)));
7377 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
7378 for (i = 0; i < 16; i++)
7379 temp_name[i] = readb(&(tb->ServerName[i]));
7380 temp_name[16] = '\0';
7381 dev_info(dev, " Server Name = %s\n", temp_name);
7382 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
7383 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007384#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007385}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007386
7387static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
7388{
7389 int i, offset, mem_type, bar_type;
7390
7391 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
7392 return 0;
7393 offset = 0;
7394 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
7395 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
7396 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
7397 offset += 4;
7398 else {
7399 mem_type = pci_resource_flags(pdev, i) &
7400 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
7401 switch (mem_type) {
7402 case PCI_BASE_ADDRESS_MEM_TYPE_32:
7403 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
7404 offset += 4; /* 32 bit */
7405 break;
7406 case PCI_BASE_ADDRESS_MEM_TYPE_64:
7407 offset += 8;
7408 break;
7409 default: /* reserved in PCI 2.2 */
7410 dev_warn(&pdev->dev,
7411 "base address is invalid\n");
7412 return -1;
7413 break;
7414 }
7415 }
7416 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
7417 return i + 1;
7418 }
7419 return -1;
7420}
7421
Robert Elliottcc64c812015-04-23 09:33:12 -05007422static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
7423{
7424 if (h->msix_vector) {
7425 if (h->pdev->msix_enabled)
7426 pci_disable_msix(h->pdev);
Robert Elliott105a3db2015-04-23 09:33:48 -05007427 h->msix_vector = 0;
Robert Elliottcc64c812015-04-23 09:33:12 -05007428 } else if (h->msi_vector) {
7429 if (h->pdev->msi_enabled)
7430 pci_disable_msi(h->pdev);
Robert Elliott105a3db2015-04-23 09:33:48 -05007431 h->msi_vector = 0;
Robert Elliottcc64c812015-04-23 09:33:12 -05007432 }
7433}
7434
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007435/* If MSI/MSI-X is supported by the kernel we will try to enable it on
Stephen Cameron050f7142015-01-23 16:42:22 -06007436 * controllers that are capable. If not, we use legacy INTx mode.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007437 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007438static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007439{
7440#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05007441 int err, i;
7442 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
7443
7444 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
7445 hpsa_msix_entries[i].vector = 0;
7446 hpsa_msix_entries[i].entry = i;
7447 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007448
7449 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05007450 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
7451 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007452 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007453 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007454 dev_info(&h->pdev->dev, "MSI-X capable controller\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007455 h->msix_vector = MAX_REPLY_QUEUES;
Stephen M. Cameronf89439b2014-05-29 10:53:02 -05007456 if (h->msix_vector > num_online_cpus())
7457 h->msix_vector = num_online_cpus();
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007458 err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
7459 1, h->msix_vector);
7460 if (err < 0) {
7461 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
7462 h->msix_vector = 0;
7463 goto single_msi_mode;
7464 } else if (err < h->msix_vector) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007465 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007466 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007467 }
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007468 h->msix_vector = err;
7469 for (i = 0; i < h->msix_vector; i++)
7470 h->intr[i] = hpsa_msix_entries[i].vector;
7471 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007472 }
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007473single_msi_mode:
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007474 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007475 dev_info(&h->pdev->dev, "MSI capable controller\n");
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007476 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007477 h->msi_vector = 1;
7478 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007479 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007480 }
7481default_int_mode:
7482#endif /* CONFIG_PCI_MSI */
7483 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06007484 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007485}
7486
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007487static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007488{
7489 int i;
7490 u32 subsystem_vendor_id, subsystem_device_id;
7491
7492 subsystem_vendor_id = pdev->subsystem_vendor;
7493 subsystem_device_id = pdev->subsystem_device;
7494 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
7495 subsystem_vendor_id;
7496
7497 for (i = 0; i < ARRAY_SIZE(products); i++)
7498 if (*board_id == products[i].board_id)
7499 return i;
7500
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05007501 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
7502 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
7503 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007504 dev_warn(&pdev->dev, "unrecognized board ID: "
7505 "0x%08x, ignoring.\n", *board_id);
7506 return -ENODEV;
7507 }
7508 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
7509}
7510
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007511static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
7512 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007513{
7514 int i;
7515
7516 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007517 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007518 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007519 *memory_bar = pci_resource_start(pdev, i);
7520 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007521 *memory_bar);
7522 return 0;
7523 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007524 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007525 return -ENODEV;
7526}
7527
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007528static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
7529 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007530{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007531 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007532 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007533 if (wait_for_ready)
7534 iterations = HPSA_BOARD_READY_ITERATIONS;
7535 else
7536 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007537
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007538 for (i = 0; i < iterations; i++) {
7539 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
7540 if (wait_for_ready) {
7541 if (scratchpad == HPSA_FIRMWARE_READY)
7542 return 0;
7543 } else {
7544 if (scratchpad != HPSA_FIRMWARE_READY)
7545 return 0;
7546 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007547 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
7548 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007549 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007550 return -ENODEV;
7551}
7552
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007553static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
7554 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
7555 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007556{
7557 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
7558 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
7559 *cfg_base_addr &= (u32) 0x0000ffff;
7560 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
7561 if (*cfg_base_addr_index == -1) {
7562 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
7563 return -ENODEV;
7564 }
7565 return 0;
7566}
7567
Robert Elliott195f2c62015-04-23 09:33:17 -05007568static void hpsa_free_cfgtables(struct ctlr_info *h)
7569{
Robert Elliott105a3db2015-04-23 09:33:48 -05007570 if (h->transtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007571 iounmap(h->transtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007572 h->transtable = NULL;
7573 }
7574 if (h->cfgtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007575 iounmap(h->cfgtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007576 h->cfgtable = NULL;
7577 }
Robert Elliott195f2c62015-04-23 09:33:17 -05007578}
7579
7580/* Find and map CISS config table and transfer table
7581+ * several items must be unmapped (freed) later
7582+ * */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007583static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007584{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06007585 u64 cfg_offset;
7586 u32 cfg_base_addr;
7587 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06007588 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007589 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007590
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007591 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7592 &cfg_base_addr_index, &cfg_offset);
7593 if (rc)
7594 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007595 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007596 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007597 if (!h->cfgtable) {
7598 dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007599 return -ENOMEM;
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007600 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007601 rc = write_driver_ver_to_cfgtable(h->cfgtable);
7602 if (rc)
7603 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007604 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007605 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007606 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
7607 cfg_base_addr_index)+cfg_offset+trans_offset,
7608 sizeof(*h->transtable));
Robert Elliott195f2c62015-04-23 09:33:17 -05007609 if (!h->transtable) {
7610 dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
7611 hpsa_free_cfgtables(h);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007612 return -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05007613 }
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007614 return 0;
7615}
7616
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007617static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007618{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007619#define MIN_MAX_COMMANDS 16
7620 BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
7621
7622 h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06007623
7624 /* Limit commands in memory limited kdump scenario. */
7625 if (reset_devices && h->max_commands > 32)
7626 h->max_commands = 32;
7627
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007628 if (h->max_commands < MIN_MAX_COMMANDS) {
7629 dev_warn(&h->pdev->dev,
7630 "Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
7631 h->max_commands,
7632 MIN_MAX_COMMANDS);
7633 h->max_commands = MIN_MAX_COMMANDS;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007634 }
7635}
7636
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007637/* If the controller reports that the total max sg entries is greater than 512,
7638 * then we know that chained SG blocks work. (Original smart arrays did not
7639 * support chained SG blocks and would return zero for max sg entries.)
7640 */
7641static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
7642{
7643 return h->maxsgentries > 512;
7644}
7645
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007646/* Interrogate the hardware for some limits:
7647 * max commands, max SG elements without chaining, and with chaining,
7648 * SG chain block size, etc.
7649 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007650static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007651{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007652 hpsa_get_max_perf_mode_cmds(h);
Stephen Cameron45fcb862015-01-23 16:43:04 -06007653 h->nr_cmds = h->max_commands;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007654 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007655 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007656 if (hpsa_supports_chained_sg_blocks(h)) {
7657 /* Limit in-command s/g elements to 32 save dma'able memory. */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007658 h->max_cmd_sg_entries = 32;
Webb Scales1a63ea62014-11-14 17:26:43 -06007659 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007660 h->maxsgentries--; /* save one for chain pointer */
7661 } else {
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007662 /*
7663 * Original smart arrays supported at most 31 s/g entries
7664 * embedded inline in the command (trying to use more
7665 * would lock up the controller)
7666 */
7667 h->max_cmd_sg_entries = 31;
Webb Scales1a63ea62014-11-14 17:26:43 -06007668 h->maxsgentries = 31; /* default to traditional values */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007669 h->chainsize = 0;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007670 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05007671
7672 /* Find out what task management functions are supported and cache */
7673 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06007674 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
7675 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
7676 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
7677 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen Cameron8be986c2015-04-23 09:34:06 -05007678 if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
7679 dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007680}
7681
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007682static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
7683{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09007684 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007685 dev_err(&h->pdev->dev, "not a valid CISS config table\n");
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007686 return false;
7687 }
7688 return true;
7689}
7690
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007691static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007692{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007693 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007694
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007695 driver_support = readl(&(h->cfgtable->driver_support));
Arnd Bergmann0b9e7b72014-06-26 15:44:52 +02007696 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
7697#ifdef CONFIG_X86
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007698 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007699#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06007700 driver_support |= ENABLE_UNIT_ATTN;
7701 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007702}
7703
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05007704/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
7705 * in a prefetch beyond physical memory.
7706 */
7707static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
7708{
7709 u32 dma_prefetch;
7710
7711 if (h->board_id != 0x3225103C)
7712 return;
7713 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
7714 dma_prefetch |= 0x8000;
7715 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
7716}
7717
Robert Elliottc706a792015-01-23 16:45:01 -06007718static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007719{
7720 int i;
7721 u32 doorbell_value;
7722 unsigned long flags;
7723 /* wait until the clear_event_notify bit 6 is cleared by controller. */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007724 for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007725 spin_lock_irqsave(&h->lock, flags);
7726 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7727 spin_unlock_irqrestore(&h->lock, flags);
7728 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
Robert Elliottc706a792015-01-23 16:45:01 -06007729 goto done;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007730 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007731 msleep(CLEAR_EVENT_WAIT_INTERVAL);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007732 }
Robert Elliottc706a792015-01-23 16:45:01 -06007733 return -ENODEV;
7734done:
7735 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007736}
7737
Robert Elliottc706a792015-01-23 16:45:01 -06007738static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007739{
7740 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007741 u32 doorbell_value;
7742 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007743
7744 /* under certain very rare conditions, this can take awhile.
7745 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
7746 * as we enter this code.)
7747 */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007748 for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
Webb Scales25163bd2015-04-23 09:32:00 -05007749 if (h->remove_in_progress)
7750 goto done;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007751 spin_lock_irqsave(&h->lock, flags);
7752 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7753 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06007754 if (!(doorbell_value & CFGTBL_ChangeReq))
Robert Elliottc706a792015-01-23 16:45:01 -06007755 goto done;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007756 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007757 msleep(MODE_CHANGE_WAIT_INTERVAL);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007758 }
Robert Elliottc706a792015-01-23 16:45:01 -06007759 return -ENODEV;
7760done:
7761 return 0;
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007762}
7763
Robert Elliottc706a792015-01-23 16:45:01 -06007764/* return -ENODEV or other reason on error, 0 on success */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007765static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007766{
7767 u32 trans_support;
7768
7769 trans_support = readl(&(h->cfgtable->TransportSupport));
7770 if (!(trans_support & SIMPLE_MODE))
7771 return -ENOTSUPP;
7772
7773 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007774
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007775 /* Update the field, and then ring the doorbell */
7776 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007777 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007778 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06007779 if (hpsa_wait_for_mode_change_ack(h))
7780 goto error;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007781 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007782 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
7783 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06007784 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007785 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007786error:
Stephen Cameron050f7142015-01-23 16:42:22 -06007787 dev_err(&h->pdev->dev, "failed to enter simple mode\n");
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007788 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007789}
7790
Robert Elliott195f2c62015-04-23 09:33:17 -05007791/* free items allocated or mapped by hpsa_pci_init */
7792static void hpsa_free_pci_init(struct ctlr_info *h)
7793{
7794 hpsa_free_cfgtables(h); /* pci_init 4 */
7795 iounmap(h->vaddr); /* pci_init 3 */
Robert Elliott105a3db2015-04-23 09:33:48 -05007796 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05007797 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Robert Elliott943a7022015-04-23 09:34:32 -05007798 /*
7799 * call pci_disable_device before pci_release_regions per
7800 * Documentation/PCI/pci.txt
7801 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007802 pci_disable_device(h->pdev); /* pci_init 1 */
Robert Elliott943a7022015-04-23 09:34:32 -05007803 pci_release_regions(h->pdev); /* pci_init 2 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007804}
7805
7806/* several items must be freed later */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007807static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007808{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007809 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007810
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007811 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
7812 if (prod_index < 0)
Robert Elliott60f923b2015-01-23 16:42:06 -06007813 return prod_index;
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007814 h->product_name = products[prod_index].product_name;
7815 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007816
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05007817 h->needs_abort_tags_swizzled =
7818 ctlr_needs_abort_tags_swizzled(h->board_id);
7819
Matthew Garrette5a44df2011-11-11 11:14:23 -05007820 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
7821 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
7822
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007823 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007824 if (err) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007825 dev_err(&h->pdev->dev, "failed to enable PCI device\n");
Robert Elliott943a7022015-04-23 09:34:32 -05007826 pci_disable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007827 return err;
7828 }
7829
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06007830 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007831 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007832 dev_err(&h->pdev->dev,
Robert Elliott195f2c62015-04-23 09:33:17 -05007833 "failed to obtain PCI resources\n");
Robert Elliott943a7022015-04-23 09:34:32 -05007834 pci_disable_device(h->pdev);
7835 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007836 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06007837
7838 pci_set_master(h->pdev);
7839
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05007840 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007841 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007842 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007843 goto clean2; /* intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007844 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007845 if (!h->vaddr) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007846 dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007847 err = -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05007848 goto clean2; /* intmode+region, pci */
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007849 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007850 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007851 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007852 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007853 err = hpsa_find_cfgtables(h);
7854 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007855 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007856 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007857
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007858 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007859 err = -ENODEV;
Robert Elliott195f2c62015-04-23 09:33:17 -05007860 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007861 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007862 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05007863 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007864 err = hpsa_enter_simple_mode(h);
7865 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007866 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007867 return 0;
7868
Robert Elliott195f2c62015-04-23 09:33:17 -05007869clean4: /* cfgtables, vaddr, intmode+region, pci */
7870 hpsa_free_cfgtables(h);
7871clean3: /* vaddr, intmode+region, pci */
7872 iounmap(h->vaddr);
Robert Elliott105a3db2015-04-23 09:33:48 -05007873 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05007874clean2: /* intmode+region, pci */
7875 hpsa_disable_interrupt_mode(h);
Robert Elliott943a7022015-04-23 09:34:32 -05007876 /*
7877 * call pci_disable_device before pci_release_regions per
7878 * Documentation/PCI/pci.txt
7879 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007880 pci_disable_device(h->pdev);
Robert Elliott943a7022015-04-23 09:34:32 -05007881 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007882 return err;
7883}
7884
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007885static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06007886{
7887 int rc;
7888
7889#define HBA_INQUIRY_BYTE_COUNT 64
7890 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
7891 if (!h->hba_inquiry_data)
7892 return;
7893 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
7894 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
7895 if (rc != 0) {
7896 kfree(h->hba_inquiry_data);
7897 h->hba_inquiry_data = NULL;
7898 }
7899}
7900
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007901static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007902{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007903 int rc, i;
Tomas Henzl3b747292015-01-23 16:41:20 -06007904 void __iomem *vaddr;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007905
7906 if (!reset_devices)
7907 return 0;
7908
Tomas Henzl132aa222014-08-14 16:12:39 +02007909 /* kdump kernel is loading, we don't know in which state is
7910 * the pci interface. The dev->enable_cnt is equal zero
7911 * so we call enable+disable, wait a while and switch it on.
7912 */
7913 rc = pci_enable_device(pdev);
7914 if (rc) {
7915 dev_warn(&pdev->dev, "Failed to enable PCI device\n");
7916 return -ENODEV;
7917 }
7918 pci_disable_device(pdev);
7919 msleep(260); /* a randomly chosen number */
7920 rc = pci_enable_device(pdev);
7921 if (rc) {
7922 dev_warn(&pdev->dev, "failed to enable device.\n");
7923 return -ENODEV;
7924 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06007925
Tomas Henzl859c75a2014-09-12 14:44:15 +02007926 pci_set_master(pdev);
Robert Elliott4fa604e2014-11-14 17:27:24 -06007927
Tomas Henzl3b747292015-01-23 16:41:20 -06007928 vaddr = pci_ioremap_bar(pdev, 0);
7929 if (vaddr == NULL) {
7930 rc = -ENOMEM;
7931 goto out_disable;
7932 }
7933 writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
7934 iounmap(vaddr);
7935
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007936 /* Reset the controller with a PCI power-cycle or via doorbell */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007937 rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007938
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007939 /* -ENOTSUPP here means we cannot reset the controller
7940 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05007941 * "performant mode". Or, it might be 640x, which can't reset
7942 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007943 */
Robert Elliottadf1b3a2015-01-23 16:42:01 -06007944 if (rc)
Tomas Henzl132aa222014-08-14 16:12:39 +02007945 goto out_disable;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007946
7947 /* Now try to get the controller to respond to a no-op */
Robert Elliott1ba66c92015-01-23 16:42:11 -06007948 dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007949 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
7950 if (hpsa_noop(pdev) == 0)
7951 break;
7952 else
7953 dev_warn(&pdev->dev, "no-op failed%s\n",
7954 (i < 11 ? "; re-trying" : ""));
7955 }
Tomas Henzl132aa222014-08-14 16:12:39 +02007956
7957out_disable:
7958
7959 pci_disable_device(pdev);
7960 return rc;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007961}
7962
Robert Elliott1fb7c982015-04-23 09:33:22 -05007963static void hpsa_free_cmd_pool(struct ctlr_info *h)
7964{
7965 kfree(h->cmd_pool_bits);
Robert Elliott105a3db2015-04-23 09:33:48 -05007966 h->cmd_pool_bits = NULL;
7967 if (h->cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05007968 pci_free_consistent(h->pdev,
7969 h->nr_cmds * sizeof(struct CommandList),
7970 h->cmd_pool,
7971 h->cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05007972 h->cmd_pool = NULL;
7973 h->cmd_pool_dhandle = 0;
7974 }
7975 if (h->errinfo_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05007976 pci_free_consistent(h->pdev,
7977 h->nr_cmds * sizeof(struct ErrorInfo),
7978 h->errinfo_pool,
7979 h->errinfo_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05007980 h->errinfo_pool = NULL;
7981 h->errinfo_pool_dhandle = 0;
7982 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05007983}
7984
Robert Elliottd37ffbe2015-04-23 09:32:27 -05007985static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007986{
7987 h->cmd_pool_bits = kzalloc(
7988 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
7989 sizeof(unsigned long), GFP_KERNEL);
7990 h->cmd_pool = pci_alloc_consistent(h->pdev,
7991 h->nr_cmds * sizeof(*h->cmd_pool),
7992 &(h->cmd_pool_dhandle));
7993 h->errinfo_pool = pci_alloc_consistent(h->pdev,
7994 h->nr_cmds * sizeof(*h->errinfo_pool),
7995 &(h->errinfo_pool_dhandle));
7996 if ((h->cmd_pool_bits == NULL)
7997 || (h->cmd_pool == NULL)
7998 || (h->errinfo_pool == NULL)) {
7999 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
Robert Elliott2c143342015-01-23 16:42:48 -06008000 goto clean_up;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008001 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05008002 hpsa_preinitialize_commands(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008003 return 0;
Robert Elliott2c143342015-01-23 16:42:48 -06008004clean_up:
8005 hpsa_free_cmd_pool(h);
8006 return -ENOMEM;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008007}
8008
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05008009static void hpsa_irq_affinity_hints(struct ctlr_info *h)
8010{
Fabian Frederickec429952015-01-23 16:41:46 -06008011 int i, cpu;
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05008012
8013 cpu = cpumask_first(cpu_online_mask);
8014 for (i = 0; i < h->msix_vector; i++) {
Fabian Frederickec429952015-01-23 16:41:46 -06008015 irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05008016 cpu = cpumask_next(cpu, cpu_online_mask);
8017 }
8018}
8019
Robert Elliottec501a12015-01-23 16:41:40 -06008020/* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
8021static void hpsa_free_irqs(struct ctlr_info *h)
8022{
8023 int i;
8024
8025 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
8026 /* Single reply queue, only one irq to free */
8027 i = h->intr_mode;
8028 irq_set_affinity_hint(h->intr[i], NULL);
8029 free_irq(h->intr[i], &h->q[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008030 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008031 return;
8032 }
8033
8034 for (i = 0; i < h->msix_vector; i++) {
8035 irq_set_affinity_hint(h->intr[i], NULL);
8036 free_irq(h->intr[i], &h->q[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008037 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008038 }
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008039 for (; i < MAX_REPLY_QUEUES; i++)
8040 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008041}
8042
Robert Elliott9ee61792015-01-23 16:42:32 -06008043/* returns 0 on success; cleans up and returns -Enn on error */
8044static int hpsa_request_irqs(struct ctlr_info *h,
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008045 irqreturn_t (*msixhandler)(int, void *),
8046 irqreturn_t (*intxhandler)(int, void *))
8047{
Matt Gates254f7962012-05-01 11:43:06 -05008048 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008049
Matt Gates254f7962012-05-01 11:43:06 -05008050 /*
8051 * initialize h->q[x] = x so that interrupt handlers know which
8052 * queue to process.
8053 */
8054 for (i = 0; i < MAX_REPLY_QUEUES; i++)
8055 h->q[i] = (u8) i;
8056
Hannes Reineckeeee0f032014-01-15 13:30:53 +01008057 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05008058 /* If performant mode and MSI-X, use multiple reply queues */
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008059 for (i = 0; i < h->msix_vector; i++) {
Robert Elliott8b470042015-04-23 09:34:58 -05008060 sprintf(h->intrname[i], "%s-msix%d", h->devname, i);
Matt Gates254f7962012-05-01 11:43:06 -05008061 rc = request_irq(h->intr[i], msixhandler,
Robert Elliott8b470042015-04-23 09:34:58 -05008062 0, h->intrname[i],
Matt Gates254f7962012-05-01 11:43:06 -05008063 &h->q[i]);
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008064 if (rc) {
8065 int j;
8066
8067 dev_err(&h->pdev->dev,
8068 "failed to get irq %d for %s\n",
8069 h->intr[i], h->devname);
8070 for (j = 0; j < i; j++) {
8071 free_irq(h->intr[j], &h->q[j]);
8072 h->q[j] = 0;
8073 }
8074 for (; j < MAX_REPLY_QUEUES; j++)
8075 h->q[j] = 0;
8076 return rc;
8077 }
8078 }
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05008079 hpsa_irq_affinity_hints(h);
Matt Gates254f7962012-05-01 11:43:06 -05008080 } else {
8081 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01008082 if (h->msix_vector > 0 || h->msi_vector) {
Robert Elliott8b470042015-04-23 09:34:58 -05008083 if (h->msix_vector)
8084 sprintf(h->intrname[h->intr_mode],
8085 "%s-msix", h->devname);
8086 else
8087 sprintf(h->intrname[h->intr_mode],
8088 "%s-msi", h->devname);
Matt Gates254f7962012-05-01 11:43:06 -05008089 rc = request_irq(h->intr[h->intr_mode],
Robert Elliott8b470042015-04-23 09:34:58 -05008090 msixhandler, 0,
8091 h->intrname[h->intr_mode],
Matt Gates254f7962012-05-01 11:43:06 -05008092 &h->q[h->intr_mode]);
8093 } else {
Robert Elliott8b470042015-04-23 09:34:58 -05008094 sprintf(h->intrname[h->intr_mode],
8095 "%s-intx", h->devname);
Matt Gates254f7962012-05-01 11:43:06 -05008096 rc = request_irq(h->intr[h->intr_mode],
Robert Elliott8b470042015-04-23 09:34:58 -05008097 intxhandler, IRQF_SHARED,
8098 h->intrname[h->intr_mode],
Matt Gates254f7962012-05-01 11:43:06 -05008099 &h->q[h->intr_mode]);
8100 }
Robert Elliott105a3db2015-04-23 09:33:48 -05008101 irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
Matt Gates254f7962012-05-01 11:43:06 -05008102 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008103 if (rc) {
Robert Elliott195f2c62015-04-23 09:33:17 -05008104 dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008105 h->intr[h->intr_mode], h->devname);
Robert Elliott195f2c62015-04-23 09:33:17 -05008106 hpsa_free_irqs(h);
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008107 return -ENODEV;
8108 }
8109 return 0;
8110}
8111
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008112static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008113{
Robert Elliott39c53f52015-04-23 09:35:14 -05008114 int rc;
Robert Elliottbf43caf2015-04-23 09:33:38 -05008115 hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008116
8117 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008118 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY);
8119 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008120 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008121 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008122 }
8123
8124 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008125 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
8126 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008127 dev_warn(&h->pdev->dev, "Board failed to become ready "
8128 "after soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008129 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008130 }
8131
8132 return 0;
8133}
8134
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008135static void hpsa_free_reply_queues(struct ctlr_info *h)
8136{
8137 int i;
8138
8139 for (i = 0; i < h->nreply_queues; i++) {
8140 if (!h->reply_queue[i].head)
8141 continue;
Robert Elliott1fb7c982015-04-23 09:33:22 -05008142 pci_free_consistent(h->pdev,
8143 h->reply_queue_size,
8144 h->reply_queue[i].head,
8145 h->reply_queue[i].busaddr);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008146 h->reply_queue[i].head = NULL;
8147 h->reply_queue[i].busaddr = 0;
8148 }
Robert Elliott105a3db2015-04-23 09:33:48 -05008149 h->reply_queue_size = 0;
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008150}
8151
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05008152static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
8153{
Robert Elliott105a3db2015-04-23 09:33:48 -05008154 hpsa_free_performant_mode(h); /* init_one 7 */
8155 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
8156 hpsa_free_cmd_pool(h); /* init_one 5 */
8157 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliott2946e822015-04-23 09:35:09 -05008158 scsi_host_put(h->scsi_host); /* init_one 3 */
8159 h->scsi_host = NULL; /* init_one 3 */
8160 hpsa_free_pci_init(h); /* init_one 2_5 */
Robert Elliott9ecd9532015-04-23 09:34:43 -05008161 free_percpu(h->lockup_detected); /* init_one 2 */
8162 h->lockup_detected = NULL; /* init_one 2 */
8163 if (h->resubmit_wq) {
8164 destroy_workqueue(h->resubmit_wq); /* init_one 1 */
8165 h->resubmit_wq = NULL;
8166 }
8167 if (h->rescan_ctlr_wq) {
8168 destroy_workqueue(h->rescan_ctlr_wq);
8169 h->rescan_ctlr_wq = NULL;
8170 }
Robert Elliott105a3db2015-04-23 09:33:48 -05008171 kfree(h); /* init_one 1 */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008172}
8173
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008174/* Called when controller lockup detected. */
Don Bracef2405db2015-01-23 16:43:09 -06008175static void fail_all_outstanding_cmds(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008176{
Webb Scales281a7fd2015-01-23 16:43:35 -06008177 int i, refcount;
8178 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05008179 int failcount = 0;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008180
Don Brace080ef1c2015-01-23 16:43:25 -06008181 flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
Don Bracef2405db2015-01-23 16:43:09 -06008182 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06008183 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06008184 refcount = atomic_inc_return(&c->refcount);
8185 if (refcount > 1) {
Webb Scales25163bd2015-04-23 09:32:00 -05008186 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
Webb Scales281a7fd2015-01-23 16:43:35 -06008187 finish_cmd(c);
Stephen Cameron433b5f42015-04-23 09:32:11 -05008188 atomic_dec(&h->commands_outstanding);
Webb Scales25163bd2015-04-23 09:32:00 -05008189 failcount++;
Webb Scales281a7fd2015-01-23 16:43:35 -06008190 }
8191 cmd_free(h, c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008192 }
Webb Scales25163bd2015-04-23 09:32:00 -05008193 dev_warn(&h->pdev->dev,
8194 "failed %d commands in fail_all\n", failcount);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008195}
8196
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008197static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
8198{
Rusty Russellc8ed0012015-03-05 10:49:19 +10308199 int cpu;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008200
Rusty Russellc8ed0012015-03-05 10:49:19 +10308201 for_each_online_cpu(cpu) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008202 u32 *lockup_detected;
8203 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
8204 *lockup_detected = value;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008205 }
8206 wmb(); /* be sure the per-cpu variables are out to memory */
8207}
8208
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008209static void controller_lockup_detected(struct ctlr_info *h)
8210{
8211 unsigned long flags;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008212 u32 lockup_detected;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008213
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008214 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8215 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008216 lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
8217 if (!lockup_detected) {
8218 /* no heartbeat, but controller gave us a zero. */
8219 dev_warn(&h->pdev->dev,
Webb Scales25163bd2015-04-23 09:32:00 -05008220 "lockup detected after %d but scratchpad register is zero\n",
8221 h->heartbeat_sample_interval / HZ);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008222 lockup_detected = 0xffffffff;
8223 }
8224 set_lockup_detected_for_all_cpus(h, lockup_detected);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008225 spin_unlock_irqrestore(&h->lock, flags);
Webb Scales25163bd2015-04-23 09:32:00 -05008226 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
8227 lockup_detected, h->heartbeat_sample_interval / HZ);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008228 pci_disable_device(h->pdev);
Don Bracef2405db2015-01-23 16:43:09 -06008229 fail_all_outstanding_cmds(h);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008230}
8231
Webb Scales25163bd2015-04-23 09:32:00 -05008232static int detect_controller_lockup(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008233{
8234 u64 now;
8235 u32 heartbeat;
8236 unsigned long flags;
8237
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008238 now = get_jiffies_64();
8239 /* If we've received an interrupt recently, we're ok. */
8240 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05008241 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05008242 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008243
8244 /*
8245 * If we've already checked the heartbeat recently, we're ok.
8246 * This could happen if someone sends us a signal. We
8247 * otherwise don't care about signals in this thread.
8248 */
8249 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05008250 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05008251 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008252
8253 /* If heartbeat has not changed since we last looked, we're not ok. */
8254 spin_lock_irqsave(&h->lock, flags);
8255 heartbeat = readl(&h->cfgtable->HeartBeat);
8256 spin_unlock_irqrestore(&h->lock, flags);
8257 if (h->last_heartbeat == heartbeat) {
8258 controller_lockup_detected(h);
Webb Scales25163bd2015-04-23 09:32:00 -05008259 return true;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008260 }
8261
8262 /* We're ok. */
8263 h->last_heartbeat = heartbeat;
8264 h->last_heartbeat_timestamp = now;
Webb Scales25163bd2015-04-23 09:32:00 -05008265 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008266}
8267
Stephen M. Cameron98465902014-02-21 16:25:00 -06008268static void hpsa_ack_ctlr_events(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008269{
8270 int i;
8271 char *event_type;
8272
Stephen Camerone4aa3e62015-01-23 16:44:07 -06008273 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
8274 return;
8275
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008276 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06008277 if ((h->transMethod & (CFGTBL_Trans_io_accel1
8278 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008279 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
8280 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
8281
8282 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
8283 event_type = "state change";
8284 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
8285 event_type = "configuration change";
8286 /* Stop sending new RAID offload reqs via the IO accelerator */
8287 scsi_block_requests(h->scsi_host);
Don Brace5323ed72016-04-27 17:13:59 -05008288 for (i = 0; i < h->ndevices; i++) {
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008289 h->dev[i]->offload_enabled = 0;
Don Brace5323ed72016-04-27 17:13:59 -05008290 h->dev[i]->offload_to_be_enabled = 0;
8291 }
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06008292 hpsa_drain_accel_commands(h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008293 /* Set 'accelerator path config change' bit */
8294 dev_warn(&h->pdev->dev,
8295 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
8296 h->events, event_type);
8297 writel(h->events, &(h->cfgtable->clear_event_notify));
8298 /* Set the "clear event notify field update" bit 6 */
8299 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8300 /* Wait until ctlr clears 'clear event notify field', bit 6 */
8301 hpsa_wait_for_clear_event_notify_ack(h);
8302 scsi_unblock_requests(h->scsi_host);
8303 } else {
8304 /* Acknowledge controller notification events. */
8305 writel(h->events, &(h->cfgtable->clear_event_notify));
8306 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8307 hpsa_wait_for_clear_event_notify_ack(h);
8308#if 0
8309 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8310 hpsa_wait_for_mode_change_ack(h);
8311#endif
8312 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008313 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008314}
8315
8316/* Check a register on the controller to see if there are configuration
8317 * changes (added/changed/removed logical drives, etc.) which mean that
Scott Teele863d682014-02-18 13:57:05 -06008318 * we should rescan the controller for devices.
8319 * Also check flag for driver-initiated rescan.
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008320 */
Stephen M. Cameron98465902014-02-21 16:25:00 -06008321static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008322{
Don Brace853633e2015-11-04 15:50:37 -06008323 if (h->drv_req_rescan) {
8324 h->drv_req_rescan = 0;
8325 return 1;
8326 }
8327
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008328 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
Stephen M. Cameron98465902014-02-21 16:25:00 -06008329 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008330
8331 h->events = readl(&(h->cfgtable->event_notify));
Stephen M. Cameron98465902014-02-21 16:25:00 -06008332 return h->events & RESCAN_REQUIRED_EVENT_BITS;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008333}
8334
Stephen M. Cameron98465902014-02-21 16:25:00 -06008335/*
8336 * Check if any of the offline devices have become ready
8337 */
8338static int hpsa_offline_devices_ready(struct ctlr_info *h)
8339{
8340 unsigned long flags;
8341 struct offline_device_entry *d;
8342 struct list_head *this, *tmp;
8343
8344 spin_lock_irqsave(&h->offline_device_lock, flags);
8345 list_for_each_safe(this, tmp, &h->offline_device_list) {
8346 d = list_entry(this, struct offline_device_entry,
8347 offline_list);
8348 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008349 if (!hpsa_volume_offline(h, d->scsi3addr)) {
8350 spin_lock_irqsave(&h->offline_device_lock, flags);
8351 list_del(&d->offline_list);
8352 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008353 return 1;
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008354 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008355 spin_lock_irqsave(&h->offline_device_lock, flags);
8356 }
8357 spin_unlock_irqrestore(&h->offline_device_lock, flags);
8358 return 0;
8359}
8360
Scott Teel34592252015-11-04 15:52:09 -06008361static int hpsa_luns_changed(struct ctlr_info *h)
8362{
8363 int rc = 1; /* assume there are changes */
8364 struct ReportLUNdata *logdev = NULL;
8365
8366 /* if we can't find out if lun data has changed,
8367 * assume that it has.
8368 */
8369
8370 if (!h->lastlogicals)
8371 goto out;
8372
8373 logdev = kzalloc(sizeof(*logdev), GFP_KERNEL);
8374 if (!logdev) {
8375 dev_warn(&h->pdev->dev,
8376 "Out of memory, can't track lun changes.\n");
8377 goto out;
8378 }
8379 if (hpsa_scsi_do_report_luns(h, 1, logdev, sizeof(*logdev), 0)) {
8380 dev_warn(&h->pdev->dev,
8381 "report luns failed, can't track lun changes.\n");
8382 goto out;
8383 }
8384 if (memcmp(logdev, h->lastlogicals, sizeof(*logdev))) {
8385 dev_info(&h->pdev->dev,
8386 "Lun changes detected.\n");
8387 memcpy(h->lastlogicals, logdev, sizeof(*logdev));
8388 goto out;
8389 } else
8390 rc = 0; /* no changes detected. */
8391out:
8392 kfree(logdev);
8393 return rc;
8394}
8395
Don Brace6636e7f2015-01-23 16:45:17 -06008396static void hpsa_rescan_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008397{
8398 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008399 struct ctlr_info *h = container_of(to_delayed_work(work),
Don Brace6636e7f2015-01-23 16:45:17 -06008400 struct ctlr_info, rescan_ctlr_work);
8401
8402
8403 if (h->remove_in_progress)
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008404 return;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008405
8406 if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
8407 scsi_host_get(h->scsi_host);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008408 hpsa_ack_ctlr_events(h);
8409 hpsa_scan_start(h->scsi_host);
8410 scsi_host_put(h->scsi_host);
Scott Teel34592252015-11-04 15:52:09 -06008411 } else if (h->discovery_polling) {
Scott Teelc2adae42015-11-04 15:52:16 -06008412 hpsa_disable_rld_caching(h);
Scott Teel34592252015-11-04 15:52:09 -06008413 if (hpsa_luns_changed(h)) {
8414 struct Scsi_Host *sh = NULL;
8415
8416 dev_info(&h->pdev->dev,
8417 "driver discovery polling rescan.\n");
8418 sh = scsi_host_get(h->scsi_host);
8419 if (sh != NULL) {
8420 hpsa_scan_start(sh);
8421 scsi_host_put(sh);
8422 }
8423 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008424 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008425 spin_lock_irqsave(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06008426 if (!h->remove_in_progress)
8427 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008428 h->heartbeat_sample_interval);
8429 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008430}
8431
Don Brace6636e7f2015-01-23 16:45:17 -06008432static void hpsa_monitor_ctlr_worker(struct work_struct *work)
8433{
8434 unsigned long flags;
8435 struct ctlr_info *h = container_of(to_delayed_work(work),
8436 struct ctlr_info, monitor_ctlr_work);
8437
8438 detect_controller_lockup(h);
8439 if (lockup_detected(h))
8440 return;
8441
8442 spin_lock_irqsave(&h->lock, flags);
8443 if (!h->remove_in_progress)
8444 schedule_delayed_work(&h->monitor_ctlr_work,
8445 h->heartbeat_sample_interval);
8446 spin_unlock_irqrestore(&h->lock, flags);
8447}
8448
8449static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
8450 char *name)
8451{
8452 struct workqueue_struct *wq = NULL;
Don Brace6636e7f2015-01-23 16:45:17 -06008453
Don Brace397ea9c2015-02-06 17:44:15 -06008454 wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
Don Brace6636e7f2015-01-23 16:45:17 -06008455 if (!wq)
8456 dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
8457
8458 return wq;
8459}
8460
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008461static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008462{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008463 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008464 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008465 int try_soft_reset = 0;
8466 unsigned long flags;
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008467 u32 board_id;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008468
8469 if (number_of_controllers == 0)
8470 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008471
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008472 rc = hpsa_lookup_board_id(pdev, &board_id);
8473 if (rc < 0) {
8474 dev_warn(&pdev->dev, "Board ID not found\n");
8475 return rc;
8476 }
8477
8478 rc = hpsa_init_reset_devices(pdev, board_id);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008479 if (rc) {
8480 if (rc != -ENOTSUPP)
8481 return rc;
8482 /* If the reset fails in a particular way (it has no way to do
8483 * a proper hard reset, so returns -ENOTSUPP) we can try to do
8484 * a soft reset once we get the controller configured up to the
8485 * point that it can accept a command.
8486 */
8487 try_soft_reset = 1;
8488 rc = 0;
8489 }
8490
8491reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008492
Don Brace303932f2010-02-04 08:42:40 -06008493 /* Command structures must be aligned on a 32-byte boundary because
8494 * the 5 lower bits of the address are used by the hardware. and by
8495 * the driver. See comments in hpsa.h for more info.
8496 */
Don Brace303932f2010-02-04 08:42:40 -06008497 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008498 h = kzalloc(sizeof(*h), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05008499 if (!h) {
8500 dev_err(&pdev->dev, "Failed to allocate controller head\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008501 return -ENOMEM;
Robert Elliott105a3db2015-04-23 09:33:48 -05008502 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008503
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008504 h->pdev = pdev;
Robert Elliott105a3db2015-04-23 09:33:48 -05008505
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06008506 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008507 INIT_LIST_HEAD(&h->offline_device_list);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008508 spin_lock_init(&h->lock);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008509 spin_lock_init(&h->offline_device_lock);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008510 spin_lock_init(&h->scan_lock);
Don Brace34f0c622015-01-23 16:43:46 -06008511 atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05008512 atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008513
8514 /* Allocate and clear per-cpu variable lockup_detected */
8515 h->lockup_detected = alloc_percpu(u32);
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008516 if (!h->lockup_detected) {
Robert Elliott105a3db2015-04-23 09:33:48 -05008517 dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008518 rc = -ENOMEM;
Robert Elliott2efa5922015-04-23 09:34:53 -05008519 goto clean1; /* aer/h */
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008520 }
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008521 set_lockup_detected_for_all_cpus(h, 0);
8522
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008523 rc = hpsa_pci_init(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05008524 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008525 goto clean2; /* lu, aer/h */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008526
Robert Elliott2946e822015-04-23 09:35:09 -05008527 /* relies on h-> settings made by hpsa_pci_init, including
8528 * interrupt_mode h->intr */
8529 rc = hpsa_scsi_host_alloc(h);
8530 if (rc)
8531 goto clean2_5; /* pci, lu, aer/h */
8532
8533 sprintf(h->devname, HPSA "%d", h->scsi_host->host_no);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008534 h->ctlr = number_of_controllers;
8535 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008536
8537 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008538 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
8539 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008540 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008541 } else {
8542 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8543 if (rc == 0) {
8544 dac = 0;
8545 } else {
8546 dev_err(&pdev->dev, "no suitable DMA available\n");
Robert Elliott2946e822015-04-23 09:35:09 -05008547 goto clean3; /* shost, pci, lu, aer/h */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008548 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008549 }
8550
8551 /* make sure the board interrupts are off */
8552 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05008553
Robert Elliott105a3db2015-04-23 09:33:48 -05008554 rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
8555 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008556 goto clean3; /* shost, pci, lu, aer/h */
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008557 rc = hpsa_alloc_cmd_pool(h);
Robert Elliott8947fd12015-01-23 16:42:54 -06008558 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008559 goto clean4; /* irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008560 rc = hpsa_alloc_sg_chain_blocks(h);
8561 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008562 goto clean5; /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Camerona08a84712010-02-04 08:43:16 -06008563 init_waitqueue_head(&h->scan_wait_queue);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05008564 init_waitqueue_head(&h->abort_cmd_wait_queue);
Webb Scalesd604f532015-04-23 09:35:22 -05008565 init_waitqueue_head(&h->event_sync_wait_queue);
8566 mutex_init(&h->reset_mutex);
Stephen M. Camerona08a84712010-02-04 08:43:16 -06008567 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008568
8569 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008570 h->ndevices = 0;
Robert Elliott2946e822015-04-23 09:35:09 -05008571
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008572 spin_lock_init(&h->devlock);
Robert Elliott105a3db2015-04-23 09:33:48 -05008573 rc = hpsa_put_ctlr_into_performant_mode(h);
8574 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008575 goto clean6; /* sg, cmd, irq, shost, pci, lu, aer/h */
8576
Robert Elliott2efa5922015-04-23 09:34:53 -05008577 /* create the resubmit workqueue */
8578 h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
8579 if (!h->rescan_ctlr_wq) {
8580 rc = -ENOMEM;
8581 goto clean7;
8582 }
8583
8584 h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
8585 if (!h->resubmit_wq) {
8586 rc = -ENOMEM;
8587 goto clean7; /* aer/h */
8588 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008589
Robert Elliott105a3db2015-04-23 09:33:48 -05008590 /*
8591 * At this point, the controller is ready to take commands.
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008592 * Now, if reset_devices and the hard reset didn't work, try
8593 * the soft reset and see if that works.
8594 */
8595 if (try_soft_reset) {
8596
8597 /* This is kind of gross. We may or may not get a completion
8598 * from the soft reset command, and if we do, then the value
8599 * from the fifo may or may not be valid. So, we wait 10 secs
8600 * after the reset throwing away any completions we get during
8601 * that time. Unregister the interrupt handler and register
8602 * fake ones to scoop up any residual completions.
8603 */
8604 spin_lock_irqsave(&h->lock, flags);
8605 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8606 spin_unlock_irqrestore(&h->lock, flags);
Robert Elliottec501a12015-01-23 16:41:40 -06008607 hpsa_free_irqs(h);
Robert Elliott9ee61792015-01-23 16:42:32 -06008608 rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008609 hpsa_intx_discard_completions);
8610 if (rc) {
Robert Elliott9ee61792015-01-23 16:42:32 -06008611 dev_warn(&h->pdev->dev,
8612 "Failed to request_irq after soft reset.\n");
Robert Elliottd4987572015-04-23 09:34:37 -05008613 /*
Robert Elliottb2ef4802015-04-23 09:34:48 -05008614 * cannot goto clean7 or free_irqs will be called
8615 * again. Instead, do its work
8616 */
8617 hpsa_free_performant_mode(h); /* clean7 */
8618 hpsa_free_sg_chain_blocks(h); /* clean6 */
8619 hpsa_free_cmd_pool(h); /* clean5 */
8620 /*
8621 * skip hpsa_free_irqs(h) clean4 since that
8622 * was just called before request_irqs failed
Robert Elliottd4987572015-04-23 09:34:37 -05008623 */
8624 goto clean3;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008625 }
8626
8627 rc = hpsa_kdump_soft_reset(h);
8628 if (rc)
8629 /* Neither hard nor soft reset worked, we're hosed. */
Don Brace7ef73232015-07-18 11:12:33 -05008630 goto clean7;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008631
8632 dev_info(&h->pdev->dev, "Board READY.\n");
8633 dev_info(&h->pdev->dev,
8634 "Waiting for stale completions to drain.\n");
8635 h->access.set_intr_mask(h, HPSA_INTR_ON);
8636 msleep(10000);
8637 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8638
8639 rc = controller_reset_failed(h->cfgtable);
8640 if (rc)
8641 dev_info(&h->pdev->dev,
8642 "Soft reset appears to have failed.\n");
8643
8644 /* since the controller's reset, we have to go back and re-init
8645 * everything. Easiest to just forget what we've done and do it
8646 * all over again.
8647 */
8648 hpsa_undo_allocations_after_kdump_soft_reset(h);
8649 try_soft_reset = 0;
8650 if (rc)
Robert Elliottb2ef4802015-04-23 09:34:48 -05008651 /* don't goto clean, we already unallocated */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008652 return -ENODEV;
8653
8654 goto reinit_after_soft_reset;
8655 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008656
Robert Elliott105a3db2015-04-23 09:33:48 -05008657 /* Enable Accelerated IO path at driver layer */
8658 h->acciopath_status = 1;
Scott Teel34592252015-11-04 15:52:09 -06008659 /* Disable discovery polling.*/
8660 h->discovery_polling = 0;
Scott Teelda0697b2014-02-18 13:57:00 -06008661
Scott Teele863d682014-02-18 13:57:05 -06008662
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008663 /* Turn the interrupts on so we can service requests */
8664 h->access.set_intr_mask(h, HPSA_INTR_ON);
8665
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06008666 hpsa_hba_inquiry(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008667
Scott Teel34592252015-11-04 15:52:09 -06008668 h->lastlogicals = kzalloc(sizeof(*(h->lastlogicals)), GFP_KERNEL);
8669 if (!h->lastlogicals)
8670 dev_info(&h->pdev->dev,
8671 "Can't track change to report lun data\n");
8672
Don Bracecf477232016-04-27 17:13:26 -05008673 /* hook into SCSI subsystem */
8674 rc = hpsa_scsi_add_host(h);
8675 if (rc)
8676 goto clean7; /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
8677
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008678 /* Monitor the controller for firmware lockups */
8679 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
8680 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
8681 schedule_delayed_work(&h->monitor_ctlr_work,
8682 h->heartbeat_sample_interval);
Don Brace6636e7f2015-01-23 16:45:17 -06008683 INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
8684 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
8685 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05008686 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008687
Robert Elliott2946e822015-04-23 09:35:09 -05008688clean7: /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008689 hpsa_free_performant_mode(h);
8690 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8691clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06008692 hpsa_free_sg_chain_blocks(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008693clean5: /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008694 hpsa_free_cmd_pool(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008695clean4: /* irq, shost, pci, lu, aer/h */
Robert Elliottec501a12015-01-23 16:41:40 -06008696 hpsa_free_irqs(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008697clean3: /* shost, pci, lu, aer/h */
8698 scsi_host_put(h->scsi_host);
8699 h->scsi_host = NULL;
8700clean2_5: /* pci, lu, aer/h */
Robert Elliott195f2c62015-04-23 09:33:17 -05008701 hpsa_free_pci_init(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008702clean2: /* lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008703 if (h->lockup_detected) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008704 free_percpu(h->lockup_detected);
Robert Elliott105a3db2015-04-23 09:33:48 -05008705 h->lockup_detected = NULL;
8706 }
8707clean1: /* wq/aer/h */
8708 if (h->resubmit_wq) {
8709 destroy_workqueue(h->resubmit_wq);
8710 h->resubmit_wq = NULL;
8711 }
8712 if (h->rescan_ctlr_wq) {
8713 destroy_workqueue(h->rescan_ctlr_wq);
8714 h->rescan_ctlr_wq = NULL;
8715 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008716 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008717 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008718}
8719
8720static void hpsa_flush_cache(struct ctlr_info *h)
8721{
8722 char *flush_buf;
8723 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05008724 int rc;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008725
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008726 if (unlikely(lockup_detected(h)))
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008727 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008728 flush_buf = kzalloc(4, GFP_KERNEL);
8729 if (!flush_buf)
8730 return;
8731
Stephen Cameron45fcb862015-01-23 16:43:04 -06008732 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05008733
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008734 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
8735 RAID_CTLR_LUNID, TYPE_CMD)) {
8736 goto out;
8737 }
Webb Scales25163bd2015-04-23 09:32:00 -05008738 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008739 PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05008740 if (rc)
8741 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008742 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008743out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008744 dev_warn(&h->pdev->dev,
8745 "error flushing cache on controller\n");
Stephen Cameron45fcb862015-01-23 16:43:04 -06008746 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008747 kfree(flush_buf);
8748}
8749
Scott Teelc2adae42015-11-04 15:52:16 -06008750/* Make controller gather fresh report lun data each time we
8751 * send down a report luns request
8752 */
8753static void hpsa_disable_rld_caching(struct ctlr_info *h)
8754{
8755 u32 *options;
8756 struct CommandList *c;
8757 int rc;
8758
8759 /* Don't bother trying to set diag options if locked up */
8760 if (unlikely(h->lockup_detected))
8761 return;
8762
8763 options = kzalloc(sizeof(*options), GFP_KERNEL);
8764 if (!options) {
8765 dev_err(&h->pdev->dev,
8766 "Error: failed to disable rld caching, during alloc.\n");
8767 return;
8768 }
8769
8770 c = cmd_alloc(h);
8771
8772 /* first, get the current diag options settings */
8773 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
8774 RAID_CTLR_LUNID, TYPE_CMD))
8775 goto errout;
8776
8777 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008778 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06008779 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8780 goto errout;
8781
8782 /* Now, set the bit for disabling the RLD caching */
8783 *options |= HPSA_DIAG_OPTS_DISABLE_RLD_CACHING;
8784
8785 if (fill_cmd(c, BMIC_SET_DIAG_OPTIONS, h, options, 4, 0,
8786 RAID_CTLR_LUNID, TYPE_CMD))
8787 goto errout;
8788
8789 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008790 PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06008791 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8792 goto errout;
8793
8794 /* Now verify that it got set: */
8795 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
8796 RAID_CTLR_LUNID, TYPE_CMD))
8797 goto errout;
8798
8799 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008800 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06008801 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8802 goto errout;
8803
Dan Carpenterd8a080c2015-11-12 12:43:38 +03008804 if (*options & HPSA_DIAG_OPTS_DISABLE_RLD_CACHING)
Scott Teelc2adae42015-11-04 15:52:16 -06008805 goto out;
8806
8807errout:
8808 dev_err(&h->pdev->dev,
8809 "Error: failed to disable report lun data caching.\n");
8810out:
8811 cmd_free(h, c);
8812 kfree(options);
8813}
8814
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008815static void hpsa_shutdown(struct pci_dev *pdev)
8816{
8817 struct ctlr_info *h;
8818
8819 h = pci_get_drvdata(pdev);
8820 /* Turn board interrupts off and send the flush cache command
8821 * sendcmd will turn off interrupt, and send the flush...
8822 * To write all data in the battery backed cache to disks
8823 */
8824 hpsa_flush_cache(h);
8825 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Robert Elliott105a3db2015-04-23 09:33:48 -05008826 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliottcc64c812015-04-23 09:33:12 -05008827 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008828}
8829
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008830static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008831{
8832 int i;
8833
Robert Elliott105a3db2015-04-23 09:33:48 -05008834 for (i = 0; i < h->ndevices; i++) {
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008835 kfree(h->dev[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008836 h->dev[i] = NULL;
8837 }
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008838}
8839
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008840static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008841{
8842 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008843 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008844
8845 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008846 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008847 return;
8848 }
8849 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008850
8851 /* Get rid of any controller monitoring work items */
8852 spin_lock_irqsave(&h->lock, flags);
8853 h->remove_in_progress = 1;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008854 spin_unlock_irqrestore(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06008855 cancel_delayed_work_sync(&h->monitor_ctlr_work);
8856 cancel_delayed_work_sync(&h->rescan_ctlr_work);
8857 destroy_workqueue(h->rescan_ctlr_wq);
8858 destroy_workqueue(h->resubmit_wq);
Robert Elliottcc64c812015-04-23 09:33:12 -05008859
Don Brace2d041302015-07-18 11:13:15 -05008860 /*
8861 * Call before disabling interrupts.
8862 * scsi_remove_host can trigger I/O operations especially
8863 * when multipath is enabled. There can be SYNCHRONIZE CACHE
8864 * operations which cannot complete and will hang the system.
8865 */
8866 if (h->scsi_host)
8867 scsi_remove_host(h->scsi_host); /* init_one 8 */
Robert Elliott105a3db2015-04-23 09:33:48 -05008868 /* includes hpsa_free_irqs - init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008869 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008870 hpsa_shutdown(pdev);
Robert Elliottcc64c812015-04-23 09:33:12 -05008871
Robert Elliott105a3db2015-04-23 09:33:48 -05008872 hpsa_free_device_info(h); /* scan */
8873
Robert Elliott2946e822015-04-23 09:35:09 -05008874 kfree(h->hba_inquiry_data); /* init_one 10 */
8875 h->hba_inquiry_data = NULL; /* init_one 10 */
Robert Elliott2946e822015-04-23 09:35:09 -05008876 hpsa_free_ioaccel2_sg_chain_blocks(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05008877 hpsa_free_performant_mode(h); /* init_one 7 */
8878 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
8879 hpsa_free_cmd_pool(h); /* init_one 5 */
Scott Teel34592252015-11-04 15:52:09 -06008880 kfree(h->lastlogicals);
Robert Elliott105a3db2015-04-23 09:33:48 -05008881
8882 /* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008883
Robert Elliott2946e822015-04-23 09:35:09 -05008884 scsi_host_put(h->scsi_host); /* init_one 3 */
8885 h->scsi_host = NULL; /* init_one 3 */
8886
Robert Elliott195f2c62015-04-23 09:33:17 -05008887 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Robert Elliott2946e822015-04-23 09:35:09 -05008888 hpsa_free_pci_init(h); /* init_one 2.5 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008889
Robert Elliott105a3db2015-04-23 09:33:48 -05008890 free_percpu(h->lockup_detected); /* init_one 2 */
8891 h->lockup_detected = NULL; /* init_one 2 */
8892 /* (void) pci_disable_pcie_error_reporting(pdev); */ /* init_one 1 */
Kevin Barnettd04e62b2015-11-04 15:52:34 -06008893
8894 hpsa_delete_sas_host(h);
8895
Robert Elliott105a3db2015-04-23 09:33:48 -05008896 kfree(h); /* init_one 1 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008897}
8898
8899static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
8900 __attribute__((unused)) pm_message_t state)
8901{
8902 return -ENOSYS;
8903}
8904
8905static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
8906{
8907 return -ENOSYS;
8908}
8909
8910static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06008911 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008912 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008913 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008914 .id_table = hpsa_pci_device_id, /* id_table */
8915 .shutdown = hpsa_shutdown,
8916 .suspend = hpsa_suspend,
8917 .resume = hpsa_resume,
8918};
8919
Don Brace303932f2010-02-04 08:42:40 -06008920/* Fill in bucket_map[], given nsgs (the max number of
8921 * scatter gather elements supported) and bucket[],
8922 * which is an array of 8 integers. The bucket[] array
8923 * contains 8 different DMA transfer sizes (in 16
8924 * byte increments) which the controller uses to fetch
8925 * commands. This function fills in bucket_map[], which
8926 * maps a given number of scatter gather elements to one of
8927 * the 8 DMA transfer sizes. The point of it is to allow the
8928 * controller to only do as much DMA as needed to fetch the
8929 * command, with the DMA transfer size encoded in the lower
8930 * bits of the command address.
8931 */
8932static void calc_bucket_map(int bucket[], int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -06008933 int nsgs, int min_blocks, u32 *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06008934{
8935 int i, j, b, size;
8936
Don Brace303932f2010-02-04 08:42:40 -06008937 /* Note, bucket_map must have nsgs+1 entries. */
8938 for (i = 0; i <= nsgs; i++) {
8939 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06008940 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06008941 b = num_buckets; /* Assume the biggest bucket */
8942 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06008943 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06008944 if (bucket[j] >= size) {
8945 b = j;
8946 break;
8947 }
8948 }
8949 /* for a command with i SG entries, use bucket b. */
8950 bucket_map[i] = b;
8951 }
8952}
8953
Robert Elliott105a3db2015-04-23 09:33:48 -05008954/*
8955 * return -ENODEV on err, 0 on success (or no action)
8956 * allocates numerous items that must be freed later
8957 */
Robert Elliottc706a792015-01-23 16:45:01 -06008958static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06008959{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05008960 int i;
8961 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06008962 unsigned long transMethod = CFGTBL_Trans_Performant |
8963 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008964 CFGTBL_Trans_enable_directed_msix |
8965 (trans_support & (CFGTBL_Trans_io_accel1 |
8966 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06008967 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05008968
8969 /* This is a bit complicated. There are 8 registers on
8970 * the controller which we write to to tell it 8 different
8971 * sizes of commands which there may be. It's a way of
8972 * reducing the DMA done to fetch each command. Encoded into
8973 * each command's tag are 3 bits which communicate to the controller
8974 * which of the eight sizes that command fits within. The size of
8975 * each command depends on how many scatter gather entries there are.
8976 * Each SG entry requires 16 bytes. The eight registers are programmed
8977 * with the number of 16-byte blocks a command of that size requires.
8978 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008979 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05008980 * blocks. Note, this only extends to the SG entries contained
8981 * within the command block, and does not extend to chained blocks
8982 * of SG elements. bft[] contains the eight values we write to
8983 * the registers. They are not evenly distributed, but have more
8984 * sizes for small commands, and fewer sizes for larger commands.
8985 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008986 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008987#define MIN_IOACCEL2_BFT_ENTRY 5
8988#define HPSA_IOACCEL2_HEADER_SZ 4
8989 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
8990 13, 14, 15, 16, 17, 18, 19,
8991 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
8992 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
8993 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
8994 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
8995 16 * MIN_IOACCEL2_BFT_ENTRY);
8996 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008997 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06008998 /* 5 = 1 s/g entry or 4k
8999 * 6 = 2 s/g entry or 8k
9000 * 8 = 4 s/g entry or 16k
9001 * 10 = 6 s/g entry or 24k
9002 */
Don Brace303932f2010-02-04 08:42:40 -06009003
Stephen M. Cameronb3a52e72014-05-29 10:53:23 -05009004 /* If the controller supports either ioaccel method then
9005 * we can also use the RAID stack submit path that does not
9006 * perform the superfluous readl() after each command submission.
9007 */
9008 if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
9009 access = SA5_performant_access_no_read;
9010
Don Brace303932f2010-02-04 08:42:40 -06009011 /* Controller spec: zero out this buffer. */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009012 for (i = 0; i < h->nreply_queues; i++)
9013 memset(h->reply_queue[i].head, 0, h->reply_queue_size);
Don Brace303932f2010-02-04 08:42:40 -06009014
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009015 bft[7] = SG_ENTRIES_IN_CMD + 4;
9016 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06009017 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06009018 for (i = 0; i < 8; i++)
9019 writel(bft[i], &h->transtable->BlockFetch[i]);
9020
9021 /* size of controller ring buffer */
9022 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05009023 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06009024 writel(0, &h->transtable->RepQCtrAddrLow32);
9025 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05009026
9027 for (i = 0; i < h->nreply_queues; i++) {
9028 writel(0, &h->transtable->RepQAddr[i].upper);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009029 writel(h->reply_queue[i].busaddr,
Matt Gates254f7962012-05-01 11:43:06 -05009030 &h->transtable->RepQAddr[i].lower);
9031 }
9032
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009033 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06009034 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
9035 /*
9036 * enable outbound interrupt coalescing in accelerator mode;
9037 */
9038 if (trans_support & CFGTBL_Trans_io_accel1) {
9039 access = SA5_ioaccel_mode1_access;
9040 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
9041 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Scott Teelc3497752014-02-18 13:56:34 -06009042 } else {
9043 if (trans_support & CFGTBL_Trans_io_accel2) {
9044 access = SA5_ioaccel_mode2_access;
9045 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
9046 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
9047 }
Matt Gatese1f7de02014-02-18 13:55:17 -06009048 }
Don Brace303932f2010-02-04 08:42:40 -06009049 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06009050 if (hpsa_wait_for_mode_change_ack(h)) {
9051 dev_err(&h->pdev->dev,
9052 "performant mode problem - doorbell timeout\n");
9053 return -ENODEV;
9054 }
Don Brace303932f2010-02-04 08:42:40 -06009055 register_value = readl(&(h->cfgtable->TransportActive));
9056 if (!(register_value & CFGTBL_Trans_Performant)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06009057 dev_err(&h->pdev->dev,
9058 "performant mode problem - transport not active\n");
Robert Elliottc706a792015-01-23 16:45:01 -06009059 return -ENODEV;
Don Brace303932f2010-02-04 08:42:40 -06009060 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06009061 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06009062 h->access = access;
9063 h->transMethod = transMethod;
9064
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009065 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
9066 (trans_support & CFGTBL_Trans_io_accel2)))
Robert Elliottc706a792015-01-23 16:45:01 -06009067 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06009068
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009069 if (trans_support & CFGTBL_Trans_io_accel1) {
9070 /* Set up I/O accelerator mode */
9071 for (i = 0; i < h->nreply_queues; i++) {
9072 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
9073 h->reply_queue[i].current_entry =
9074 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
9075 }
9076 bft[7] = h->ioaccel_maxsg + 8;
9077 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
9078 h->ioaccel1_blockFetchTable);
9079
9080 /* initialize all reply queue entries to unused */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009081 for (i = 0; i < h->nreply_queues; i++)
9082 memset(h->reply_queue[i].head,
9083 (u8) IOACCEL_MODE1_REPLY_UNUSED,
9084 h->reply_queue_size);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009085
9086 /* set all the constant fields in the accelerator command
9087 * frames once at init time to save CPU cycles later.
9088 */
9089 for (i = 0; i < h->nr_cmds; i++) {
9090 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
9091
9092 cp->function = IOACCEL1_FUNCTION_SCSIIO;
9093 cp->err_info = (u32) (h->errinfo_pool_dhandle +
9094 (i * sizeof(struct ErrorInfo)));
9095 cp->err_info_len = sizeof(struct ErrorInfo);
9096 cp->sgl_offset = IOACCEL1_SGLOFFSET;
Don Brace2b08b3e2015-01-23 16:41:09 -06009097 cp->host_context_flags =
9098 cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009099 cp->timeout_sec = 0;
9100 cp->ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009101 cp->tag =
Don Bracef2405db2015-01-23 16:43:09 -06009102 cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009103 cp->host_addr =
9104 cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009105 (i * sizeof(struct io_accel1_cmd)));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009106 }
9107 } else if (trans_support & CFGTBL_Trans_io_accel2) {
9108 u64 cfg_offset, cfg_base_addr_index;
9109 u32 bft2_offset, cfg_base_addr;
9110 int rc;
9111
9112 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
9113 &cfg_base_addr_index, &cfg_offset);
9114 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
9115 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
9116 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
9117 4, h->ioaccel2_blockFetchTable);
9118 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
9119 BUILD_BUG_ON(offsetof(struct CfgTable,
9120 io_accel_request_size_offset) != 0xb8);
9121 h->ioaccel2_bft2_regs =
9122 remap_pci_mem(pci_resource_start(h->pdev,
9123 cfg_base_addr_index) +
9124 cfg_offset + bft2_offset,
9125 ARRAY_SIZE(bft2) *
9126 sizeof(*h->ioaccel2_bft2_regs));
9127 for (i = 0; i < ARRAY_SIZE(bft2); i++)
9128 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06009129 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009130 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06009131 if (hpsa_wait_for_mode_change_ack(h)) {
9132 dev_err(&h->pdev->dev,
9133 "performant mode problem - enabling ioaccel mode\n");
9134 return -ENODEV;
9135 }
9136 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06009137}
9138
Robert Elliott1fb7c982015-04-23 09:33:22 -05009139/* Free ioaccel1 mode command blocks and block fetch table */
9140static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
9141{
Robert Elliott105a3db2015-04-23 09:33:48 -05009142 if (h->ioaccel_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05009143 pci_free_consistent(h->pdev,
9144 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
9145 h->ioaccel_cmd_pool,
9146 h->ioaccel_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05009147 h->ioaccel_cmd_pool = NULL;
9148 h->ioaccel_cmd_pool_dhandle = 0;
9149 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05009150 kfree(h->ioaccel1_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009151 h->ioaccel1_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05009152}
9153
Robert Elliottd37ffbe2015-04-23 09:32:27 -05009154/* Allocate ioaccel1 mode command blocks and block fetch table */
9155static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
Matt Gatese1f7de02014-02-18 13:55:17 -06009156{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06009157 h->ioaccel_maxsg =
9158 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
9159 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
9160 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
9161
Matt Gatese1f7de02014-02-18 13:55:17 -06009162 /* Command structures must be aligned on a 128-byte boundary
9163 * because the 7 lower bits of the address are used by the
9164 * hardware.
9165 */
Matt Gatese1f7de02014-02-18 13:55:17 -06009166 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
9167 IOACCEL1_COMMANDLIST_ALIGNMENT);
9168 h->ioaccel_cmd_pool =
9169 pci_alloc_consistent(h->pdev,
9170 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
9171 &(h->ioaccel_cmd_pool_dhandle));
9172
9173 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06009174 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06009175 sizeof(u32)), GFP_KERNEL);
9176
9177 if ((h->ioaccel_cmd_pool == NULL) ||
9178 (h->ioaccel1_blockFetchTable == NULL))
9179 goto clean_up;
9180
9181 memset(h->ioaccel_cmd_pool, 0,
9182 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
9183 return 0;
9184
9185clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05009186 hpsa_free_ioaccel1_cmd_and_bft(h);
Robert Elliott2dd02d72015-04-23 09:33:43 -05009187 return -ENOMEM;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009188}
9189
Robert Elliott1fb7c982015-04-23 09:33:22 -05009190/* Free ioaccel2 mode command blocks and block fetch table */
9191static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
9192{
Webb Scalesd9a729f2015-04-23 09:33:27 -05009193 hpsa_free_ioaccel2_sg_chain_blocks(h);
9194
Robert Elliott105a3db2015-04-23 09:33:48 -05009195 if (h->ioaccel2_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05009196 pci_free_consistent(h->pdev,
9197 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
9198 h->ioaccel2_cmd_pool,
9199 h->ioaccel2_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05009200 h->ioaccel2_cmd_pool = NULL;
9201 h->ioaccel2_cmd_pool_dhandle = 0;
9202 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05009203 kfree(h->ioaccel2_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009204 h->ioaccel2_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05009205}
9206
Robert Elliottd37ffbe2015-04-23 09:32:27 -05009207/* Allocate ioaccel2 mode command blocks and block fetch table */
9208static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009209{
Webb Scalesd9a729f2015-04-23 09:33:27 -05009210 int rc;
9211
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009212 /* Allocate ioaccel2 mode command blocks and block fetch table */
9213
9214 h->ioaccel_maxsg =
9215 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
9216 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
9217 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
9218
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009219 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
9220 IOACCEL2_COMMANDLIST_ALIGNMENT);
9221 h->ioaccel2_cmd_pool =
9222 pci_alloc_consistent(h->pdev,
9223 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
9224 &(h->ioaccel2_cmd_pool_dhandle));
9225
9226 h->ioaccel2_blockFetchTable =
9227 kmalloc(((h->ioaccel_maxsg + 1) *
9228 sizeof(u32)), GFP_KERNEL);
9229
9230 if ((h->ioaccel2_cmd_pool == NULL) ||
Webb Scalesd9a729f2015-04-23 09:33:27 -05009231 (h->ioaccel2_blockFetchTable == NULL)) {
9232 rc = -ENOMEM;
9233 goto clean_up;
9234 }
9235
9236 rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
9237 if (rc)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009238 goto clean_up;
9239
9240 memset(h->ioaccel2_cmd_pool, 0,
9241 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
9242 return 0;
9243
9244clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05009245 hpsa_free_ioaccel2_cmd_and_bft(h);
Webb Scalesd9a729f2015-04-23 09:33:27 -05009246 return rc;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009247}
9248
Robert Elliott105a3db2015-04-23 09:33:48 -05009249/* Free items allocated by hpsa_put_ctlr_into_performant_mode */
9250static void hpsa_free_performant_mode(struct ctlr_info *h)
9251{
9252 kfree(h->blockFetchTable);
9253 h->blockFetchTable = NULL;
9254 hpsa_free_reply_queues(h);
9255 hpsa_free_ioaccel1_cmd_and_bft(h);
9256 hpsa_free_ioaccel2_cmd_and_bft(h);
9257}
9258
9259/* return -ENODEV on error, 0 on success (or no action)
9260 * allocates numerous items that must be freed later
9261 */
9262static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009263{
9264 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06009265 unsigned long transMethod = CFGTBL_Trans_Performant |
9266 CFGTBL_Trans_use_short_tags;
Robert Elliott105a3db2015-04-23 09:33:48 -05009267 int i, rc;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009268
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06009269 if (hpsa_simple_mode)
Robert Elliott105a3db2015-04-23 09:33:48 -05009270 return 0;
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06009271
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05009272 trans_support = readl(&(h->cfgtable->TransportSupport));
9273 if (!(trans_support & PERFORMANT_MODE))
Robert Elliott105a3db2015-04-23 09:33:48 -05009274 return 0;
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05009275
Matt Gatese1f7de02014-02-18 13:55:17 -06009276 /* Check for I/O accelerator mode support */
9277 if (trans_support & CFGTBL_Trans_io_accel1) {
9278 transMethod |= CFGTBL_Trans_io_accel1 |
9279 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05009280 rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
9281 if (rc)
9282 return rc;
9283 } else if (trans_support & CFGTBL_Trans_io_accel2) {
9284 transMethod |= CFGTBL_Trans_io_accel2 |
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009285 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05009286 rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
9287 if (rc)
9288 return rc;
Matt Gatese1f7de02014-02-18 13:55:17 -06009289 }
9290
Hannes Reineckeeee0f032014-01-15 13:30:53 +01009291 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05009292 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009293 /* Performant mode ring buffer and supporting data structures */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009294 h->reply_queue_size = h->max_commands * sizeof(u64);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009295
Matt Gates254f7962012-05-01 11:43:06 -05009296 for (i = 0; i < h->nreply_queues; i++) {
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009297 h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
9298 h->reply_queue_size,
9299 &(h->reply_queue[i].busaddr));
Robert Elliott105a3db2015-04-23 09:33:48 -05009300 if (!h->reply_queue[i].head) {
9301 rc = -ENOMEM;
9302 goto clean1; /* rq, ioaccel */
9303 }
Matt Gates254f7962012-05-01 11:43:06 -05009304 h->reply_queue[i].size = h->max_commands;
9305 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
9306 h->reply_queue[i].current_entry = 0;
9307 }
9308
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009309 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009310 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009311 sizeof(u32)), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05009312 if (!h->blockFetchTable) {
9313 rc = -ENOMEM;
9314 goto clean1; /* rq, ioaccel */
9315 }
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009316
Robert Elliott105a3db2015-04-23 09:33:48 -05009317 rc = hpsa_enter_performant_mode(h, trans_support);
9318 if (rc)
9319 goto clean2; /* bft, rq, ioaccel */
9320 return 0;
Don Brace303932f2010-02-04 08:42:40 -06009321
Robert Elliott105a3db2015-04-23 09:33:48 -05009322clean2: /* bft, rq, ioaccel */
Don Brace303932f2010-02-04 08:42:40 -06009323 kfree(h->blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009324 h->blockFetchTable = NULL;
9325clean1: /* rq, ioaccel */
9326 hpsa_free_reply_queues(h);
9327 hpsa_free_ioaccel1_cmd_and_bft(h);
9328 hpsa_free_ioaccel2_cmd_and_bft(h);
9329 return rc;
Don Brace303932f2010-02-04 08:42:40 -06009330}
9331
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009332static int is_accelerated_cmd(struct CommandList *c)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009333{
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009334 return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
9335}
9336
9337static void hpsa_drain_accel_commands(struct ctlr_info *h)
9338{
9339 struct CommandList *c = NULL;
Don Bracef2405db2015-01-23 16:43:09 -06009340 int i, accel_cmds_out;
Webb Scales281a7fd2015-01-23 16:43:35 -06009341 int refcount;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009342
Don Bracef2405db2015-01-23 16:43:09 -06009343 do { /* wait for all outstanding ioaccel commands to drain out */
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009344 accel_cmds_out = 0;
Don Bracef2405db2015-01-23 16:43:09 -06009345 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06009346 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06009347 refcount = atomic_inc_return(&c->refcount);
9348 if (refcount > 1) /* Command is allocated */
9349 accel_cmds_out += is_accelerated_cmd(c);
9350 cmd_free(h, c);
Don Bracef2405db2015-01-23 16:43:09 -06009351 }
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009352 if (accel_cmds_out <= 0)
Webb Scales281a7fd2015-01-23 16:43:35 -06009353 break;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009354 msleep(100);
9355 } while (1);
9356}
9357
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009358static struct hpsa_sas_phy *hpsa_alloc_sas_phy(
9359 struct hpsa_sas_port *hpsa_sas_port)
9360{
9361 struct hpsa_sas_phy *hpsa_sas_phy;
9362 struct sas_phy *phy;
9363
9364 hpsa_sas_phy = kzalloc(sizeof(*hpsa_sas_phy), GFP_KERNEL);
9365 if (!hpsa_sas_phy)
9366 return NULL;
9367
9368 phy = sas_phy_alloc(hpsa_sas_port->parent_node->parent_dev,
9369 hpsa_sas_port->next_phy_index);
9370 if (!phy) {
9371 kfree(hpsa_sas_phy);
9372 return NULL;
9373 }
9374
9375 hpsa_sas_port->next_phy_index++;
9376 hpsa_sas_phy->phy = phy;
9377 hpsa_sas_phy->parent_port = hpsa_sas_port;
9378
9379 return hpsa_sas_phy;
9380}
9381
9382static void hpsa_free_sas_phy(struct hpsa_sas_phy *hpsa_sas_phy)
9383{
9384 struct sas_phy *phy = hpsa_sas_phy->phy;
9385
9386 sas_port_delete_phy(hpsa_sas_phy->parent_port->port, phy);
9387 sas_phy_free(phy);
9388 if (hpsa_sas_phy->added_to_port)
9389 list_del(&hpsa_sas_phy->phy_list_entry);
9390 kfree(hpsa_sas_phy);
9391}
9392
9393static int hpsa_sas_port_add_phy(struct hpsa_sas_phy *hpsa_sas_phy)
9394{
9395 int rc;
9396 struct hpsa_sas_port *hpsa_sas_port;
9397 struct sas_phy *phy;
9398 struct sas_identify *identify;
9399
9400 hpsa_sas_port = hpsa_sas_phy->parent_port;
9401 phy = hpsa_sas_phy->phy;
9402
9403 identify = &phy->identify;
9404 memset(identify, 0, sizeof(*identify));
9405 identify->sas_address = hpsa_sas_port->sas_address;
9406 identify->device_type = SAS_END_DEVICE;
9407 identify->initiator_port_protocols = SAS_PROTOCOL_STP;
9408 identify->target_port_protocols = SAS_PROTOCOL_STP;
9409 phy->minimum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
9410 phy->maximum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
9411 phy->minimum_linkrate = SAS_LINK_RATE_UNKNOWN;
9412 phy->maximum_linkrate = SAS_LINK_RATE_UNKNOWN;
9413 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
9414
9415 rc = sas_phy_add(hpsa_sas_phy->phy);
9416 if (rc)
9417 return rc;
9418
9419 sas_port_add_phy(hpsa_sas_port->port, hpsa_sas_phy->phy);
9420 list_add_tail(&hpsa_sas_phy->phy_list_entry,
9421 &hpsa_sas_port->phy_list_head);
9422 hpsa_sas_phy->added_to_port = true;
9423
9424 return 0;
9425}
9426
9427static int
9428 hpsa_sas_port_add_rphy(struct hpsa_sas_port *hpsa_sas_port,
9429 struct sas_rphy *rphy)
9430{
9431 struct sas_identify *identify;
9432
9433 identify = &rphy->identify;
9434 identify->sas_address = hpsa_sas_port->sas_address;
9435 identify->initiator_port_protocols = SAS_PROTOCOL_STP;
9436 identify->target_port_protocols = SAS_PROTOCOL_STP;
9437
9438 return sas_rphy_add(rphy);
9439}
9440
9441static struct hpsa_sas_port
9442 *hpsa_alloc_sas_port(struct hpsa_sas_node *hpsa_sas_node,
9443 u64 sas_address)
9444{
9445 int rc;
9446 struct hpsa_sas_port *hpsa_sas_port;
9447 struct sas_port *port;
9448
9449 hpsa_sas_port = kzalloc(sizeof(*hpsa_sas_port), GFP_KERNEL);
9450 if (!hpsa_sas_port)
9451 return NULL;
9452
9453 INIT_LIST_HEAD(&hpsa_sas_port->phy_list_head);
9454 hpsa_sas_port->parent_node = hpsa_sas_node;
9455
9456 port = sas_port_alloc_num(hpsa_sas_node->parent_dev);
9457 if (!port)
9458 goto free_hpsa_port;
9459
9460 rc = sas_port_add(port);
9461 if (rc)
9462 goto free_sas_port;
9463
9464 hpsa_sas_port->port = port;
9465 hpsa_sas_port->sas_address = sas_address;
9466 list_add_tail(&hpsa_sas_port->port_list_entry,
9467 &hpsa_sas_node->port_list_head);
9468
9469 return hpsa_sas_port;
9470
9471free_sas_port:
9472 sas_port_free(port);
9473free_hpsa_port:
9474 kfree(hpsa_sas_port);
9475
9476 return NULL;
9477}
9478
9479static void hpsa_free_sas_port(struct hpsa_sas_port *hpsa_sas_port)
9480{
9481 struct hpsa_sas_phy *hpsa_sas_phy;
9482 struct hpsa_sas_phy *next;
9483
9484 list_for_each_entry_safe(hpsa_sas_phy, next,
9485 &hpsa_sas_port->phy_list_head, phy_list_entry)
9486 hpsa_free_sas_phy(hpsa_sas_phy);
9487
9488 sas_port_delete(hpsa_sas_port->port);
9489 list_del(&hpsa_sas_port->port_list_entry);
9490 kfree(hpsa_sas_port);
9491}
9492
9493static struct hpsa_sas_node *hpsa_alloc_sas_node(struct device *parent_dev)
9494{
9495 struct hpsa_sas_node *hpsa_sas_node;
9496
9497 hpsa_sas_node = kzalloc(sizeof(*hpsa_sas_node), GFP_KERNEL);
9498 if (hpsa_sas_node) {
9499 hpsa_sas_node->parent_dev = parent_dev;
9500 INIT_LIST_HEAD(&hpsa_sas_node->port_list_head);
9501 }
9502
9503 return hpsa_sas_node;
9504}
9505
9506static void hpsa_free_sas_node(struct hpsa_sas_node *hpsa_sas_node)
9507{
9508 struct hpsa_sas_port *hpsa_sas_port;
9509 struct hpsa_sas_port *next;
9510
9511 if (!hpsa_sas_node)
9512 return;
9513
9514 list_for_each_entry_safe(hpsa_sas_port, next,
9515 &hpsa_sas_node->port_list_head, port_list_entry)
9516 hpsa_free_sas_port(hpsa_sas_port);
9517
9518 kfree(hpsa_sas_node);
9519}
9520
9521static struct hpsa_scsi_dev_t
9522 *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
9523 struct sas_rphy *rphy)
9524{
9525 int i;
9526 struct hpsa_scsi_dev_t *device;
9527
9528 for (i = 0; i < h->ndevices; i++) {
9529 device = h->dev[i];
9530 if (!device->sas_port)
9531 continue;
9532 if (device->sas_port->rphy == rphy)
9533 return device;
9534 }
9535
9536 return NULL;
9537}
9538
9539static int hpsa_add_sas_host(struct ctlr_info *h)
9540{
9541 int rc;
9542 struct device *parent_dev;
9543 struct hpsa_sas_node *hpsa_sas_node;
9544 struct hpsa_sas_port *hpsa_sas_port;
9545 struct hpsa_sas_phy *hpsa_sas_phy;
9546
9547 parent_dev = &h->scsi_host->shost_gendev;
9548
9549 hpsa_sas_node = hpsa_alloc_sas_node(parent_dev);
9550 if (!hpsa_sas_node)
9551 return -ENOMEM;
9552
9553 hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, h->sas_address);
9554 if (!hpsa_sas_port) {
9555 rc = -ENODEV;
9556 goto free_sas_node;
9557 }
9558
9559 hpsa_sas_phy = hpsa_alloc_sas_phy(hpsa_sas_port);
9560 if (!hpsa_sas_phy) {
9561 rc = -ENODEV;
9562 goto free_sas_port;
9563 }
9564
9565 rc = hpsa_sas_port_add_phy(hpsa_sas_phy);
9566 if (rc)
9567 goto free_sas_phy;
9568
9569 h->sas_host = hpsa_sas_node;
9570
9571 return 0;
9572
9573free_sas_phy:
9574 hpsa_free_sas_phy(hpsa_sas_phy);
9575free_sas_port:
9576 hpsa_free_sas_port(hpsa_sas_port);
9577free_sas_node:
9578 hpsa_free_sas_node(hpsa_sas_node);
9579
9580 return rc;
9581}
9582
9583static void hpsa_delete_sas_host(struct ctlr_info *h)
9584{
9585 hpsa_free_sas_node(h->sas_host);
9586}
9587
9588static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
9589 struct hpsa_scsi_dev_t *device)
9590{
9591 int rc;
9592 struct hpsa_sas_port *hpsa_sas_port;
9593 struct sas_rphy *rphy;
9594
9595 hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, device->sas_address);
9596 if (!hpsa_sas_port)
9597 return -ENOMEM;
9598
9599 rphy = sas_end_device_alloc(hpsa_sas_port->port);
9600 if (!rphy) {
9601 rc = -ENODEV;
9602 goto free_sas_port;
9603 }
9604
9605 hpsa_sas_port->rphy = rphy;
9606 device->sas_port = hpsa_sas_port;
9607
9608 rc = hpsa_sas_port_add_rphy(hpsa_sas_port, rphy);
9609 if (rc)
9610 goto free_sas_port;
9611
9612 return 0;
9613
9614free_sas_port:
9615 hpsa_free_sas_port(hpsa_sas_port);
9616 device->sas_port = NULL;
9617
9618 return rc;
9619}
9620
9621static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device)
9622{
9623 if (device->sas_port) {
9624 hpsa_free_sas_port(device->sas_port);
9625 device->sas_port = NULL;
9626 }
9627}
9628
9629static int
9630hpsa_sas_get_linkerrors(struct sas_phy *phy)
9631{
9632 return 0;
9633}
9634
9635static int
9636hpsa_sas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
9637{
Dan Carpenteraa105692016-04-14 12:37:44 +03009638 *identifier = 0;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009639 return 0;
9640}
9641
9642static int
9643hpsa_sas_get_bay_identifier(struct sas_rphy *rphy)
9644{
9645 return -ENXIO;
9646}
9647
9648static int
9649hpsa_sas_phy_reset(struct sas_phy *phy, int hard_reset)
9650{
9651 return 0;
9652}
9653
9654static int
9655hpsa_sas_phy_enable(struct sas_phy *phy, int enable)
9656{
9657 return 0;
9658}
9659
9660static int
9661hpsa_sas_phy_setup(struct sas_phy *phy)
9662{
9663 return 0;
9664}
9665
9666static void
9667hpsa_sas_phy_release(struct sas_phy *phy)
9668{
9669}
9670
9671static int
9672hpsa_sas_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
9673{
9674 return -EINVAL;
9675}
9676
9677/* SMP = Serial Management Protocol */
9678static int
9679hpsa_sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
9680struct request *req)
9681{
9682 return -EINVAL;
9683}
9684
9685static struct sas_function_template hpsa_sas_transport_functions = {
9686 .get_linkerrors = hpsa_sas_get_linkerrors,
9687 .get_enclosure_identifier = hpsa_sas_get_enclosure_identifier,
9688 .get_bay_identifier = hpsa_sas_get_bay_identifier,
9689 .phy_reset = hpsa_sas_phy_reset,
9690 .phy_enable = hpsa_sas_phy_enable,
9691 .phy_setup = hpsa_sas_phy_setup,
9692 .phy_release = hpsa_sas_phy_release,
9693 .set_phy_speed = hpsa_sas_phy_speed,
9694 .smp_handler = hpsa_sas_smp_handler,
9695};
9696
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009697/*
9698 * This is it. Register the PCI driver information for the cards we control
9699 * the OS will call our registered routines when it finds one of our cards.
9700 */
9701static int __init hpsa_init(void)
9702{
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009703 int rc;
9704
9705 hpsa_sas_transport_template =
9706 sas_attach_transport(&hpsa_sas_transport_functions);
9707 if (!hpsa_sas_transport_template)
9708 return -ENODEV;
9709
9710 rc = pci_register_driver(&hpsa_pci_driver);
9711
9712 if (rc)
9713 sas_release_transport(hpsa_sas_transport_template);
9714
9715 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009716}
9717
9718static void __exit hpsa_cleanup(void)
9719{
9720 pci_unregister_driver(&hpsa_pci_driver);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009721 sas_release_transport(hpsa_sas_transport_template);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009722}
9723
Matt Gatese1f7de02014-02-18 13:55:17 -06009724static void __attribute__((unused)) verify_offsets(void)
9725{
9726#define VERIFY_OFFSET(member, offset) \
Scott Teeldd0e19f2014-02-18 13:57:31 -06009727 BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
9728
9729 VERIFY_OFFSET(structure_size, 0);
9730 VERIFY_OFFSET(volume_blk_size, 4);
9731 VERIFY_OFFSET(volume_blk_cnt, 8);
9732 VERIFY_OFFSET(phys_blk_shift, 16);
9733 VERIFY_OFFSET(parity_rotation_shift, 17);
9734 VERIFY_OFFSET(strip_size, 18);
9735 VERIFY_OFFSET(disk_starting_blk, 20);
9736 VERIFY_OFFSET(disk_blk_cnt, 28);
9737 VERIFY_OFFSET(data_disks_per_row, 36);
9738 VERIFY_OFFSET(metadata_disks_per_row, 38);
9739 VERIFY_OFFSET(row_cnt, 40);
9740 VERIFY_OFFSET(layout_map_count, 42);
9741 VERIFY_OFFSET(flags, 44);
9742 VERIFY_OFFSET(dekindex, 46);
9743 /* VERIFY_OFFSET(reserved, 48 */
9744 VERIFY_OFFSET(data, 64);
9745
9746#undef VERIFY_OFFSET
9747
9748#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06009749 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
9750
9751 VERIFY_OFFSET(IU_type, 0);
9752 VERIFY_OFFSET(direction, 1);
9753 VERIFY_OFFSET(reply_queue, 2);
9754 /* VERIFY_OFFSET(reserved1, 3); */
9755 VERIFY_OFFSET(scsi_nexus, 4);
9756 VERIFY_OFFSET(Tag, 8);
9757 VERIFY_OFFSET(cdb, 16);
9758 VERIFY_OFFSET(cciss_lun, 32);
9759 VERIFY_OFFSET(data_len, 40);
9760 VERIFY_OFFSET(cmd_priority_task_attr, 44);
9761 VERIFY_OFFSET(sg_count, 45);
9762 /* VERIFY_OFFSET(reserved3 */
9763 VERIFY_OFFSET(err_ptr, 48);
9764 VERIFY_OFFSET(err_len, 56);
9765 /* VERIFY_OFFSET(reserved4 */
9766 VERIFY_OFFSET(sg, 64);
9767
9768#undef VERIFY_OFFSET
9769
9770#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06009771 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
9772
9773 VERIFY_OFFSET(dev_handle, 0x00);
9774 VERIFY_OFFSET(reserved1, 0x02);
9775 VERIFY_OFFSET(function, 0x03);
9776 VERIFY_OFFSET(reserved2, 0x04);
9777 VERIFY_OFFSET(err_info, 0x0C);
9778 VERIFY_OFFSET(reserved3, 0x10);
9779 VERIFY_OFFSET(err_info_len, 0x12);
9780 VERIFY_OFFSET(reserved4, 0x13);
9781 VERIFY_OFFSET(sgl_offset, 0x14);
9782 VERIFY_OFFSET(reserved5, 0x15);
9783 VERIFY_OFFSET(transfer_len, 0x1C);
9784 VERIFY_OFFSET(reserved6, 0x20);
9785 VERIFY_OFFSET(io_flags, 0x24);
9786 VERIFY_OFFSET(reserved7, 0x26);
9787 VERIFY_OFFSET(LUN, 0x34);
9788 VERIFY_OFFSET(control, 0x3C);
9789 VERIFY_OFFSET(CDB, 0x40);
9790 VERIFY_OFFSET(reserved8, 0x50);
9791 VERIFY_OFFSET(host_context_flags, 0x60);
9792 VERIFY_OFFSET(timeout_sec, 0x62);
9793 VERIFY_OFFSET(ReplyQueue, 0x64);
9794 VERIFY_OFFSET(reserved9, 0x65);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009795 VERIFY_OFFSET(tag, 0x68);
Matt Gatese1f7de02014-02-18 13:55:17 -06009796 VERIFY_OFFSET(host_addr, 0x70);
9797 VERIFY_OFFSET(CISS_LUN, 0x78);
9798 VERIFY_OFFSET(SG, 0x78 + 8);
9799#undef VERIFY_OFFSET
9800}
9801
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009802module_init(hpsa_init);
9803module_exit(hpsa_cleanup);