blob: 589b44ea31075f558466ab03a29fe2cb167ba45a [file] [log] [blame]
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001/*
2 * Disk Array driver for HP Smart Array SAS controllers
Don Brace1358f6d2015-07-18 11:12:38 -05003 * Copyright 2014-2015 PMC-Sierra, Inc.
4 * Copyright 2000,2009-2015 Hewlett-Packard Development Company, L.P.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more details.
14 *
Don Brace1358f6d2015-07-18 11:12:38 -050015 * Questions/Comments/Bugfixes to storagedev@pmcs.com
Stephen M. Cameronedd16362009-12-08 14:09:11 -080016 *
17 */
18
19#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/types.h>
22#include <linux/pci.h>
Matthew Garrette5a44df2011-11-11 11:14:23 -050023#include <linux/pci-aspm.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080024#include <linux/kernel.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
27#include <linux/fs.h>
28#include <linux/timer.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080029#include <linux/init.h>
30#include <linux/spinlock.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080031#include <linux/compat.h>
32#include <linux/blktrace_api.h>
33#include <linux/uaccess.h>
34#include <linux/io.h>
35#include <linux/dma-mapping.h>
36#include <linux/completion.h>
37#include <linux/moduleparam.h>
38#include <scsi/scsi.h>
39#include <scsi/scsi_cmnd.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_host.h>
Stephen M. Cameron667e23d2010-02-25 14:02:51 -060042#include <scsi/scsi_tcq.h>
Stephen Cameron9437ac42015-04-23 09:32:16 -050043#include <scsi/scsi_eh.h>
Kevin Barnettd04e62b2015-11-04 15:52:34 -060044#include <scsi/scsi_transport_sas.h>
Webb Scales73153fe2015-04-23 09:35:04 -050045#include <scsi/scsi_dbg.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080046#include <linux/cciss_ioctl.h>
47#include <linux/string.h>
48#include <linux/bitmap.h>
Arun Sharma600634972011-07-26 16:09:06 -070049#include <linux/atomic.h>
Stephen M. Camerona0c12412011-10-26 16:22:04 -050050#include <linux/jiffies.h>
Don Brace42a91642014-11-14 17:26:27 -060051#include <linux/percpu-defs.h>
Stephen M. Cameron094963d2014-05-29 10:53:18 -050052#include <linux/percpu.h>
Don Brace2b08b3e2015-01-23 16:41:09 -060053#include <asm/unaligned.h>
Stephen M. Cameron283b4a92014-02-18 13:55:33 -060054#include <asm/div64.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080055#include "hpsa_cmd.h"
56#include "hpsa.h"
57
Don Braceec2c3aa2015-11-04 15:52:40 -060058/*
59 * HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.'
60 * with an optional trailing '-' followed by a byte value (0-255).
61 */
62#define HPSA_DRIVER_VERSION "3.4.14-0"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080063#define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -060064#define HPSA "hpsa"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080065
Robert Elliott007e7aa2015-01-23 16:44:56 -060066/* How long to wait for CISS doorbell communication */
67#define CLEAR_EVENT_WAIT_INTERVAL 20 /* ms for each msleep() call */
68#define MODE_CHANGE_WAIT_INTERVAL 10 /* ms for each msleep() call */
69#define MAX_CLEAR_EVENT_WAIT 30000 /* times 20 ms = 600 s */
70#define MAX_MODE_CHANGE_WAIT 2000 /* times 10 ms = 20 s */
Stephen M. Cameronedd16362009-12-08 14:09:11 -080071#define MAX_IOCTL_CONFIG_WAIT 1000
72
73/*define how many times we will try a command because of bus resets */
74#define MAX_CMD_RETRIES 3
75
76/* Embedded module documentation macros - see modules.h */
77MODULE_AUTHOR("Hewlett-Packard Company");
78MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
79 HPSA_DRIVER_VERSION);
80MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
81MODULE_VERSION(HPSA_DRIVER_VERSION);
82MODULE_LICENSE("GPL");
83
84static int hpsa_allow_any;
85module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
86MODULE_PARM_DESC(hpsa_allow_any,
87 "Allow hpsa driver to access unknown HP Smart Array hardware");
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -060088static int hpsa_simple_mode;
89module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
90MODULE_PARM_DESC(hpsa_simple_mode,
91 "Use 'simple mode' rather than 'performant mode'");
Stephen M. Cameronedd16362009-12-08 14:09:11 -080092
93/* define the PCI info for the cards we can control */
94static const struct pci_device_id hpsa_pci_device_id[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -080095 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
96 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
97 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
98 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
99 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
Mike Miller163dbcd2013-09-04 15:11:10 -0500100 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
101 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
Mike Millerf8b01eb2010-02-04 08:42:45 -0600102 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
scameron@beardog.cce.hp.com9143a962011-03-07 10:44:16 -0600103 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350},
104 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351},
105 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352},
106 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353},
107 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354},
108 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355},
109 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356},
Mike Millerfe0c9612012-09-20 16:05:18 -0500110 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1921},
111 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1922},
112 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1923},
113 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1924},
Mike Millerfe0c9612012-09-20 16:05:18 -0500114 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1926},
115 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1928},
Mike Miller97b9f532013-09-04 15:05:55 -0500116 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1929},
117 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BD},
118 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BE},
119 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BF},
120 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C0},
121 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C1},
122 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C2},
123 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C3},
124 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C4},
125 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C5},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500126 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C6},
Mike Miller97b9f532013-09-04 15:05:55 -0500127 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C7},
128 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C8},
129 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C9},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500130 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CA},
131 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CB},
132 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CC},
133 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CD},
134 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CE},
Don Bracefdfa4b62015-04-23 09:35:27 -0500135 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0580},
Don Bracecbb47dc2015-07-18 11:12:54 -0500136 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0581},
137 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0582},
138 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0583},
139 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0584},
140 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0585},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600141 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
142 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
143 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
144 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
145 {PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
Mike Miller7c03b872010-12-01 11:16:07 -0600146 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
Stephen M. Cameron6798cc02010-06-16 13:51:20 -0500147 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800148 {0,}
149};
150
151MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
152
153/* board_id = Subsystem Device ID & Vendor ID
154 * product = Marketing Name for the board
155 * access = Address of the struct of function pointers
156 */
157static struct board_type products[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800158 {0x3241103C, "Smart Array P212", &SA5_access},
159 {0x3243103C, "Smart Array P410", &SA5_access},
160 {0x3245103C, "Smart Array P410i", &SA5_access},
161 {0x3247103C, "Smart Array P411", &SA5_access},
162 {0x3249103C, "Smart Array P812", &SA5_access},
Mike Miller163dbcd2013-09-04 15:11:10 -0500163 {0x324A103C, "Smart Array P712m", &SA5_access},
164 {0x324B103C, "Smart Array P711m", &SA5_access},
Stephen M. Cameron7d2cce52014-11-14 17:26:38 -0600165 {0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
Mike Millerfe0c9612012-09-20 16:05:18 -0500166 {0x3350103C, "Smart Array P222", &SA5_access},
167 {0x3351103C, "Smart Array P420", &SA5_access},
168 {0x3352103C, "Smart Array P421", &SA5_access},
169 {0x3353103C, "Smart Array P822", &SA5_access},
170 {0x3354103C, "Smart Array P420i", &SA5_access},
171 {0x3355103C, "Smart Array P220i", &SA5_access},
172 {0x3356103C, "Smart Array P721m", &SA5_access},
Mike Miller1fd6c8e2013-09-04 15:08:29 -0500173 {0x1921103C, "Smart Array P830i", &SA5_access},
174 {0x1922103C, "Smart Array P430", &SA5_access},
175 {0x1923103C, "Smart Array P431", &SA5_access},
176 {0x1924103C, "Smart Array P830", &SA5_access},
177 {0x1926103C, "Smart Array P731m", &SA5_access},
178 {0x1928103C, "Smart Array P230i", &SA5_access},
179 {0x1929103C, "Smart Array P530", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600180 {0x21BD103C, "Smart Array P244br", &SA5_access},
181 {0x21BE103C, "Smart Array P741m", &SA5_access},
182 {0x21BF103C, "Smart HBA H240ar", &SA5_access},
183 {0x21C0103C, "Smart Array P440ar", &SA5_access},
Don Bracec8ae0ab2015-01-23 16:45:12 -0600184 {0x21C1103C, "Smart Array P840ar", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600185 {0x21C2103C, "Smart Array P440", &SA5_access},
186 {0x21C3103C, "Smart Array P441", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500187 {0x21C4103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600188 {0x21C5103C, "Smart Array P841", &SA5_access},
189 {0x21C6103C, "Smart HBA H244br", &SA5_access},
190 {0x21C7103C, "Smart HBA H240", &SA5_access},
191 {0x21C8103C, "Smart HBA H241", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500192 {0x21C9103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600193 {0x21CA103C, "Smart Array P246br", &SA5_access},
194 {0x21CB103C, "Smart Array P840", &SA5_access},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500195 {0x21CC103C, "Smart Array", &SA5_access},
196 {0x21CD103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600197 {0x21CE103C, "Smart HBA", &SA5_access},
Don Bracefdfa4b62015-04-23 09:35:27 -0500198 {0x05809005, "SmartHBA-SA", &SA5_access},
Don Bracecbb47dc2015-07-18 11:12:54 -0500199 {0x05819005, "SmartHBA-SA 8i", &SA5_access},
200 {0x05829005, "SmartHBA-SA 8i8e", &SA5_access},
201 {0x05839005, "SmartHBA-SA 8e", &SA5_access},
202 {0x05849005, "SmartHBA-SA 16i", &SA5_access},
203 {0x05859005, "SmartHBA-SA 4i4e", &SA5_access},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600204 {0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
205 {0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
206 {0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
207 {0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
208 {0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800209 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
210};
211
Kevin Barnettd04e62b2015-11-04 15:52:34 -0600212static struct scsi_transport_template *hpsa_sas_transport_template;
213static int hpsa_add_sas_host(struct ctlr_info *h);
214static void hpsa_delete_sas_host(struct ctlr_info *h);
215static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
216 struct hpsa_scsi_dev_t *device);
217static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device);
218static struct hpsa_scsi_dev_t
219 *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
220 struct sas_rphy *rphy);
221
Webb Scalesa58e7e52015-04-23 09:34:16 -0500222#define SCSI_CMD_BUSY ((struct scsi_cmnd *)&hpsa_cmd_busy)
223static const struct scsi_cmnd hpsa_cmd_busy;
224#define SCSI_CMD_IDLE ((struct scsi_cmnd *)&hpsa_cmd_idle)
225static const struct scsi_cmnd hpsa_cmd_idle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800226static int number_of_controllers;
227
Stephen M. Cameron10f66012010-06-16 13:51:50 -0500228static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
229static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
Don Brace42a91642014-11-14 17:26:27 -0600230static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800231
232#ifdef CONFIG_COMPAT
Don Brace42a91642014-11-14 17:26:27 -0600233static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
234 void __user *arg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800235#endif
236
237static void cmd_free(struct ctlr_info *h, struct CommandList *c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800238static struct CommandList *cmd_alloc(struct ctlr_info *h);
Webb Scales73153fe2015-04-23 09:35:04 -0500239static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c);
240static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
241 struct scsi_cmnd *scmd);
Stephen M. Camerona2dac132013-02-20 11:24:41 -0600242static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600243 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800244 int cmd_type);
Robert Elliott2c143342015-01-23 16:42:48 -0600245static void hpsa_free_cmd_pool(struct ctlr_info *h);
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600246#define VPD_PAGE (1 << 8)
Don Braceb48d9802015-11-04 15:50:13 -0600247#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800248
Jeff Garzikf2812332010-11-16 02:10:29 -0500249static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Stephen M. Camerona08a8472010-02-04 08:43:16 -0600250static void hpsa_scan_start(struct Scsi_Host *);
251static int hpsa_scan_finished(struct Scsi_Host *sh,
252 unsigned long elapsed_time);
Don Brace7c0a0222015-01-23 16:41:30 -0600253static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800254
255static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500256static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800257static int hpsa_slave_alloc(struct scsi_device *sdev);
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500258static int hpsa_slave_configure(struct scsi_device *sdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800259static void hpsa_slave_destroy(struct scsi_device *sdev);
260
Don Brace8aa60682015-11-04 15:50:01 -0600261static void hpsa_update_scsi_devices(struct ctlr_info *h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800262static int check_for_unit_attention(struct ctlr_info *h,
263 struct CommandList *c);
264static void check_ioctl_unit_attention(struct ctlr_info *h,
265 struct CommandList *c);
Don Brace303932f2010-02-04 08:42:40 -0600266/* performant mode helper functions */
267static void calc_bucket_map(int *bucket, int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -0600268 int nsgs, int min_blocks, u32 *bucket_map);
Robert Elliott105a3db2015-04-23 09:33:48 -0500269static void hpsa_free_performant_mode(struct ctlr_info *h);
270static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
Matt Gates254f7962012-05-01 11:43:06 -0500271static inline u32 next_command(struct ctlr_info *h, u8 q);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800272static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
273 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
274 u64 *cfg_offset);
275static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
276 unsigned long *memory_bar);
277static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
278static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
279 int wait_for_ready);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500280static inline void finish_cmd(struct CommandList *c);
Robert Elliottc706a792015-01-23 16:45:01 -0600281static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -0600282#define BOARD_NOT_READY 0
283#define BOARD_READY 1
Stephen M. Cameron23100dd2014-02-18 13:57:37 -0600284static void hpsa_drain_accel_commands(struct ctlr_info *h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -0600285static void hpsa_flush_cache(struct ctlr_info *h);
Scott Teelc3497752014-02-18 13:56:34 -0600286static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
287 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -0600288 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
Don Brace080ef1c2015-01-23 16:43:25 -0600289static void hpsa_command_resubmit_worker(struct work_struct *work);
Webb Scales25163bd2015-04-23 09:32:00 -0500290static u32 lockup_detected(struct ctlr_info *h);
291static int detect_controller_lockup(struct ctlr_info *h);
Scott Teelc2adae42015-11-04 15:52:16 -0600292static void hpsa_disable_rld_caching(struct ctlr_info *h);
Kevin Barnettd04e62b2015-11-04 15:52:34 -0600293static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
294 struct ReportExtendedLUNdata *buf, int bufsize);
Scott Teel34592252015-11-04 15:52:09 -0600295static int hpsa_luns_changed(struct ctlr_info *h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800296
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800297static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
298{
299 unsigned long *priv = shost_priv(sdev->host);
300 return (struct ctlr_info *) *priv;
301}
302
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600303static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
304{
305 unsigned long *priv = shost_priv(sh);
306 return (struct ctlr_info *) *priv;
307}
308
Webb Scalesa58e7e52015-04-23 09:34:16 -0500309static inline bool hpsa_is_cmd_idle(struct CommandList *c)
310{
311 return c->scsi_cmd == SCSI_CMD_IDLE;
312}
313
Webb Scalesd604f532015-04-23 09:35:22 -0500314static inline bool hpsa_is_pending_event(struct CommandList *c)
315{
316 return c->abort_pending || c->reset_pending;
317}
318
Stephen Cameron9437ac42015-04-23 09:32:16 -0500319/* extract sense key, asc, and ascq from sense data. -1 means invalid. */
320static void decode_sense_data(const u8 *sense_data, int sense_data_len,
321 u8 *sense_key, u8 *asc, u8 *ascq)
322{
323 struct scsi_sense_hdr sshdr;
324 bool rc;
325
326 *sense_key = -1;
327 *asc = -1;
328 *ascq = -1;
329
330 if (sense_data_len < 1)
331 return;
332
333 rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
334 if (rc) {
335 *sense_key = sshdr.sense_key;
336 *asc = sshdr.asc;
337 *ascq = sshdr.ascq;
338 }
339}
340
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800341static int check_for_unit_attention(struct ctlr_info *h,
342 struct CommandList *c)
343{
Stephen Cameron9437ac42015-04-23 09:32:16 -0500344 u8 sense_key, asc, ascq;
345 int sense_len;
346
347 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
348 sense_len = sizeof(c->err_info->SenseInfo);
349 else
350 sense_len = c->err_info->SenseLen;
351
352 decode_sense_data(c->err_info->SenseInfo, sense_len,
353 &sense_key, &asc, &ascq);
Don Brace81c27552015-07-18 11:12:28 -0500354 if (sense_key != UNIT_ATTENTION || asc == 0xff)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800355 return 0;
356
Stephen Cameron9437ac42015-04-23 09:32:16 -0500357 switch (asc) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800358 case STATE_CHANGED:
Stephen Cameron9437ac42015-04-23 09:32:16 -0500359 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500360 "%s: a state change detected, command retried\n",
361 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800362 break;
363 case LUN_FAILED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600364 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500365 "%s: LUN failure detected\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800366 break;
367 case REPORT_LUNS_CHANGED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600368 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500369 "%s: report LUN data changed\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800370 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600371 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
372 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800373 */
374 break;
375 case POWER_OR_RESET:
Robert Elliott2946e822015-04-23 09:35:09 -0500376 dev_warn(&h->pdev->dev,
377 "%s: a power on or device reset detected\n",
378 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800379 break;
380 case UNIT_ATTENTION_CLEARED:
Robert Elliott2946e822015-04-23 09:35:09 -0500381 dev_warn(&h->pdev->dev,
382 "%s: unit attention cleared by another initiator\n",
383 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800384 break;
385 default:
Robert Elliott2946e822015-04-23 09:35:09 -0500386 dev_warn(&h->pdev->dev,
387 "%s: unknown unit attention detected\n",
388 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800389 break;
390 }
391 return 1;
392}
393
Matt Bondurant852af202012-05-01 11:42:35 -0500394static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
395{
396 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
397 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
398 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
399 return 0;
400 dev_warn(&h->pdev->dev, HPSA "device busy");
401 return 1;
402}
403
Stephen Camerone985c582015-04-23 09:32:22 -0500404static u32 lockup_detected(struct ctlr_info *h);
405static ssize_t host_show_lockup_detected(struct device *dev,
406 struct device_attribute *attr, char *buf)
407{
408 int ld;
409 struct ctlr_info *h;
410 struct Scsi_Host *shost = class_to_shost(dev);
411
412 h = shost_to_hba(shost);
413 ld = lockup_detected(h);
414
415 return sprintf(buf, "ld=%d\n", ld);
416}
417
Scott Teelda0697b2014-02-18 13:57:00 -0600418static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
419 struct device_attribute *attr,
420 const char *buf, size_t count)
421{
422 int status, len;
423 struct ctlr_info *h;
424 struct Scsi_Host *shost = class_to_shost(dev);
425 char tmpbuf[10];
426
427 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
428 return -EACCES;
429 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
430 strncpy(tmpbuf, buf, len);
431 tmpbuf[len] = '\0';
432 if (sscanf(tmpbuf, "%d", &status) != 1)
433 return -EINVAL;
434 h = shost_to_hba(shost);
435 h->acciopath_status = !!status;
436 dev_warn(&h->pdev->dev,
437 "hpsa: HP SSD Smart Path %s via sysfs update.\n",
438 h->acciopath_status ? "enabled" : "disabled");
439 return count;
440}
441
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600442static ssize_t host_store_raid_offload_debug(struct device *dev,
443 struct device_attribute *attr,
444 const char *buf, size_t count)
445{
446 int debug_level, len;
447 struct ctlr_info *h;
448 struct Scsi_Host *shost = class_to_shost(dev);
449 char tmpbuf[10];
450
451 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
452 return -EACCES;
453 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
454 strncpy(tmpbuf, buf, len);
455 tmpbuf[len] = '\0';
456 if (sscanf(tmpbuf, "%d", &debug_level) != 1)
457 return -EINVAL;
458 if (debug_level < 0)
459 debug_level = 0;
460 h = shost_to_hba(shost);
461 h->raid_offload_debug = debug_level;
462 dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
463 h->raid_offload_debug);
464 return count;
465}
466
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800467static ssize_t host_store_rescan(struct device *dev,
468 struct device_attribute *attr,
469 const char *buf, size_t count)
470{
471 struct ctlr_info *h;
472 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600473 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600474 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800475 return count;
476}
477
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500478static ssize_t host_show_firmware_revision(struct device *dev,
479 struct device_attribute *attr, char *buf)
480{
481 struct ctlr_info *h;
482 struct Scsi_Host *shost = class_to_shost(dev);
483 unsigned char *fwrev;
484
485 h = shost_to_hba(shost);
486 if (!h->hba_inquiry_data)
487 return 0;
488 fwrev = &h->hba_inquiry_data[32];
489 return snprintf(buf, 20, "%c%c%c%c\n",
490 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
491}
492
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600493static ssize_t host_show_commands_outstanding(struct device *dev,
494 struct device_attribute *attr, char *buf)
495{
496 struct Scsi_Host *shost = class_to_shost(dev);
497 struct ctlr_info *h = shost_to_hba(shost);
498
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600499 return snprintf(buf, 20, "%d\n",
500 atomic_read(&h->commands_outstanding));
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600501}
502
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600503static ssize_t host_show_transport_mode(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
506 struct ctlr_info *h;
507 struct Scsi_Host *shost = class_to_shost(dev);
508
509 h = shost_to_hba(shost);
510 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600511 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600512 "performant" : "simple");
513}
514
Scott Teelda0697b2014-02-18 13:57:00 -0600515static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
516 struct device_attribute *attr, char *buf)
517{
518 struct ctlr_info *h;
519 struct Scsi_Host *shost = class_to_shost(dev);
520
521 h = shost_to_hba(shost);
522 return snprintf(buf, 30, "HP SSD Smart Path %s\n",
523 (h->acciopath_status == 1) ? "enabled" : "disabled");
524}
525
Stephen M. Cameron46380782011-05-03 15:00:01 -0500526/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600527static u32 unresettable_controller[] = {
528 0x324a103C, /* Smart Array P712m */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500529 0x324b103C, /* Smart Array P711m */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600530 0x3223103C, /* Smart Array P800 */
531 0x3234103C, /* Smart Array P400 */
532 0x3235103C, /* Smart Array P400i */
533 0x3211103C, /* Smart Array E200i */
534 0x3212103C, /* Smart Array E200 */
535 0x3213103C, /* Smart Array E200i */
536 0x3214103C, /* Smart Array E200i */
537 0x3215103C, /* Smart Array E200i */
538 0x3237103C, /* Smart Array E500 */
539 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100540 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600541 0x409C0E11, /* Smart Array 6400 */
542 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100543 0x40700E11, /* Smart Array 5300 */
544 0x40820E11, /* Smart Array 532 */
545 0x40830E11, /* Smart Array 5312 */
546 0x409A0E11, /* Smart Array 641 */
547 0x409B0E11, /* Smart Array 642 */
548 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600549};
550
Stephen M. Cameron46380782011-05-03 15:00:01 -0500551/* List of controllers which cannot even be soft reset */
552static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100553 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100554 0x40700E11, /* Smart Array 5300 */
555 0x40820E11, /* Smart Array 532 */
556 0x40830E11, /* Smart Array 5312 */
557 0x409A0E11, /* Smart Array 641 */
558 0x409B0E11, /* Smart Array 642 */
559 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500560 /* Exclude 640x boards. These are two pci devices in one slot
561 * which share a battery backed cache module. One controls the
562 * cache, the other accesses the cache through the one that controls
563 * it. If we reset the one controlling the cache, the other will
564 * likely not be happy. Just forbid resetting this conjoined mess.
565 * The 640x isn't really supported by hpsa anyway.
566 */
567 0x409C0E11, /* Smart Array 6400 */
568 0x409D0E11, /* Smart Array 6400 EM */
569};
570
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500571static u32 needs_abort_tags_swizzled[] = {
572 0x323D103C, /* Smart Array P700m */
573 0x324a103C, /* Smart Array P712m */
574 0x324b103C, /* SmartArray P711m */
575};
576
577static int board_id_in_array(u32 a[], int nelems, u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600578{
579 int i;
580
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500581 for (i = 0; i < nelems; i++)
582 if (a[i] == board_id)
583 return 1;
584 return 0;
585}
586
587static int ctlr_is_hard_resettable(u32 board_id)
588{
589 return !board_id_in_array(unresettable_controller,
590 ARRAY_SIZE(unresettable_controller), board_id);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600591}
592
Stephen M. Cameron46380782011-05-03 15:00:01 -0500593static int ctlr_is_soft_resettable(u32 board_id)
594{
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500595 return !board_id_in_array(soft_unresettable_controller,
596 ARRAY_SIZE(soft_unresettable_controller), board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500597}
598
599static int ctlr_is_resettable(u32 board_id)
600{
601 return ctlr_is_hard_resettable(board_id) ||
602 ctlr_is_soft_resettable(board_id);
603}
604
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500605static int ctlr_needs_abort_tags_swizzled(u32 board_id)
606{
607 return board_id_in_array(needs_abort_tags_swizzled,
608 ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
609}
610
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600611static ssize_t host_show_resettable(struct device *dev,
612 struct device_attribute *attr, char *buf)
613{
614 struct ctlr_info *h;
615 struct Scsi_Host *shost = class_to_shost(dev);
616
617 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500618 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600619}
620
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800621static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
622{
623 return (scsi3addr[3] & 0xC0) == 0x40;
624}
625
Robert Elliottf2ef0ce2015-01-23 16:41:35 -0600626static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
Don Brace7c59a0d2015-11-04 15:52:22 -0600627 "1(+0)ADM", "UNKNOWN", "PHYS DRV"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800628};
Scott Teel6b80b182014-02-18 13:56:55 -0600629#define HPSA_RAID_0 0
630#define HPSA_RAID_4 1
631#define HPSA_RAID_1 2 /* also used for RAID 10 */
632#define HPSA_RAID_5 3 /* also used for RAID 50 */
633#define HPSA_RAID_51 4
634#define HPSA_RAID_6 5 /* also used for RAID 60 */
635#define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
Don Brace7c59a0d2015-11-04 15:52:22 -0600636#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 2)
637#define PHYSICAL_DRIVE (ARRAY_SIZE(raid_label) - 1)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800638
Kevin Barnettf3f01732015-11-04 15:51:33 -0600639static inline bool is_logical_device(struct hpsa_scsi_dev_t *device)
640{
641 return !device->physical_device;
642}
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800643
644static ssize_t raid_level_show(struct device *dev,
645 struct device_attribute *attr, char *buf)
646{
647 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600648 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800649 struct ctlr_info *h;
650 struct scsi_device *sdev;
651 struct hpsa_scsi_dev_t *hdev;
652 unsigned long flags;
653
654 sdev = to_scsi_device(dev);
655 h = sdev_to_hba(sdev);
656 spin_lock_irqsave(&h->lock, flags);
657 hdev = sdev->hostdata;
658 if (!hdev) {
659 spin_unlock_irqrestore(&h->lock, flags);
660 return -ENODEV;
661 }
662
663 /* Is this even a logical drive? */
Kevin Barnettf3f01732015-11-04 15:51:33 -0600664 if (!is_logical_device(hdev)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800665 spin_unlock_irqrestore(&h->lock, flags);
666 l = snprintf(buf, PAGE_SIZE, "N/A\n");
667 return l;
668 }
669
670 rlevel = hdev->raid_level;
671 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600672 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800673 rlevel = RAID_UNKNOWN;
674 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
675 return l;
676}
677
678static ssize_t lunid_show(struct device *dev,
679 struct device_attribute *attr, char *buf)
680{
681 struct ctlr_info *h;
682 struct scsi_device *sdev;
683 struct hpsa_scsi_dev_t *hdev;
684 unsigned long flags;
685 unsigned char lunid[8];
686
687 sdev = to_scsi_device(dev);
688 h = sdev_to_hba(sdev);
689 spin_lock_irqsave(&h->lock, flags);
690 hdev = sdev->hostdata;
691 if (!hdev) {
692 spin_unlock_irqrestore(&h->lock, flags);
693 return -ENODEV;
694 }
695 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
696 spin_unlock_irqrestore(&h->lock, flags);
697 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
698 lunid[0], lunid[1], lunid[2], lunid[3],
699 lunid[4], lunid[5], lunid[6], lunid[7]);
700}
701
702static ssize_t unique_id_show(struct device *dev,
703 struct device_attribute *attr, char *buf)
704{
705 struct ctlr_info *h;
706 struct scsi_device *sdev;
707 struct hpsa_scsi_dev_t *hdev;
708 unsigned long flags;
709 unsigned char sn[16];
710
711 sdev = to_scsi_device(dev);
712 h = sdev_to_hba(sdev);
713 spin_lock_irqsave(&h->lock, flags);
714 hdev = sdev->hostdata;
715 if (!hdev) {
716 spin_unlock_irqrestore(&h->lock, flags);
717 return -ENODEV;
718 }
719 memcpy(sn, hdev->device_id, sizeof(sn));
720 spin_unlock_irqrestore(&h->lock, flags);
721 return snprintf(buf, 16 * 2 + 2,
722 "%02X%02X%02X%02X%02X%02X%02X%02X"
723 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
724 sn[0], sn[1], sn[2], sn[3],
725 sn[4], sn[5], sn[6], sn[7],
726 sn[8], sn[9], sn[10], sn[11],
727 sn[12], sn[13], sn[14], sn[15]);
728}
729
Scott Teelc1988682014-02-18 13:55:54 -0600730static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
731 struct device_attribute *attr, char *buf)
732{
733 struct ctlr_info *h;
734 struct scsi_device *sdev;
735 struct hpsa_scsi_dev_t *hdev;
736 unsigned long flags;
737 int offload_enabled;
738
739 sdev = to_scsi_device(dev);
740 h = sdev_to_hba(sdev);
741 spin_lock_irqsave(&h->lock, flags);
742 hdev = sdev->hostdata;
743 if (!hdev) {
744 spin_unlock_irqrestore(&h->lock, flags);
745 return -ENODEV;
746 }
747 offload_enabled = hdev->offload_enabled;
748 spin_unlock_irqrestore(&h->lock, flags);
749 return snprintf(buf, 20, "%d\n", offload_enabled);
750}
751
Joe Handzik8270b862015-07-18 11:12:43 -0500752#define MAX_PATHS 8
Joe Handzik8270b862015-07-18 11:12:43 -0500753static ssize_t path_info_show(struct device *dev,
754 struct device_attribute *attr, char *buf)
755{
756 struct ctlr_info *h;
757 struct scsi_device *sdev;
758 struct hpsa_scsi_dev_t *hdev;
759 unsigned long flags;
760 int i;
761 int output_len = 0;
762 u8 box;
763 u8 bay;
764 u8 path_map_index = 0;
765 char *active;
766 unsigned char phys_connector[2];
Joe Handzik8270b862015-07-18 11:12:43 -0500767
Joe Handzik8270b862015-07-18 11:12:43 -0500768 sdev = to_scsi_device(dev);
769 h = sdev_to_hba(sdev);
770 spin_lock_irqsave(&h->devlock, flags);
771 hdev = sdev->hostdata;
772 if (!hdev) {
773 spin_unlock_irqrestore(&h->devlock, flags);
774 return -ENODEV;
775 }
776
777 bay = hdev->bay;
778 for (i = 0; i < MAX_PATHS; i++) {
779 path_map_index = 1<<i;
780 if (i == hdev->active_path_index)
781 active = "Active";
782 else if (hdev->path_map & path_map_index)
783 active = "Inactive";
784 else
785 continue;
786
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600787 output_len += scnprintf(buf + output_len,
788 PAGE_SIZE - output_len,
789 "[%d:%d:%d:%d] %20.20s ",
Joe Handzik8270b862015-07-18 11:12:43 -0500790 h->scsi_host->host_no,
791 hdev->bus, hdev->target, hdev->lun,
792 scsi_device_type(hdev->devtype));
793
Don Bracecca8f132015-12-22 10:36:48 -0600794 if (hdev->devtype == TYPE_RAID || is_logical_device(hdev)) {
Don Brace2708f292015-12-22 10:36:36 -0600795 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600796 PAGE_SIZE - output_len,
797 "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500798 continue;
799 }
800
801 box = hdev->box[i];
802 memcpy(&phys_connector, &hdev->phys_connector[i],
803 sizeof(phys_connector));
804 if (phys_connector[0] < '0')
805 phys_connector[0] = '0';
806 if (phys_connector[1] < '0')
807 phys_connector[1] = '0';
Don Bracecca8f132015-12-22 10:36:48 -0600808 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600809 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500810 "PORT: %.2s ",
811 phys_connector);
Don Braceaf15ed32016-02-23 15:16:15 -0600812 if ((hdev->devtype == TYPE_DISK || hdev->devtype == TYPE_ZBC) &&
813 hdev->expose_device) {
Joe Handzik8270b862015-07-18 11:12:43 -0500814 if (box == 0 || box == 0xFF) {
Don Brace2708f292015-12-22 10:36:36 -0600815 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600816 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500817 "BAY: %hhu %s\n",
818 bay, active);
819 } else {
Don Brace2708f292015-12-22 10:36:36 -0600820 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600821 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500822 "BOX: %hhu BAY: %hhu %s\n",
823 box, bay, active);
824 }
825 } else if (box != 0 && box != 0xFF) {
Don Brace2708f292015-12-22 10:36:36 -0600826 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600827 PAGE_SIZE - output_len, "BOX: %hhu %s\n",
Joe Handzik8270b862015-07-18 11:12:43 -0500828 box, active);
829 } else
Don Brace2708f292015-12-22 10:36:36 -0600830 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600831 PAGE_SIZE - output_len, "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500832 }
833
834 spin_unlock_irqrestore(&h->devlock, flags);
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600835 return output_len;
Joe Handzik8270b862015-07-18 11:12:43 -0500836}
837
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600838static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
839static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
840static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
841static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Scott Teelc1988682014-02-18 13:55:54 -0600842static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
843 host_show_hp_ssd_smart_path_enabled, NULL);
Joe Handzik8270b862015-07-18 11:12:43 -0500844static DEVICE_ATTR(path_info, S_IRUGO, path_info_show, NULL);
Scott Teelda0697b2014-02-18 13:57:00 -0600845static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
846 host_show_hp_ssd_smart_path_status,
847 host_store_hp_ssd_smart_path_status);
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600848static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
849 host_store_raid_offload_debug);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600850static DEVICE_ATTR(firmware_revision, S_IRUGO,
851 host_show_firmware_revision, NULL);
852static DEVICE_ATTR(commands_outstanding, S_IRUGO,
853 host_show_commands_outstanding, NULL);
854static DEVICE_ATTR(transport_mode, S_IRUGO,
855 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600856static DEVICE_ATTR(resettable, S_IRUGO,
857 host_show_resettable, NULL);
Stephen Camerone985c582015-04-23 09:32:22 -0500858static DEVICE_ATTR(lockup_detected, S_IRUGO,
859 host_show_lockup_detected, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600860
861static struct device_attribute *hpsa_sdev_attrs[] = {
862 &dev_attr_raid_level,
863 &dev_attr_lunid,
864 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600865 &dev_attr_hp_ssd_smart_path_enabled,
Joe Handzik8270b862015-07-18 11:12:43 -0500866 &dev_attr_path_info,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600867 NULL,
868};
869
870static struct device_attribute *hpsa_shost_attrs[] = {
871 &dev_attr_rescan,
872 &dev_attr_firmware_revision,
873 &dev_attr_commands_outstanding,
874 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600875 &dev_attr_resettable,
Scott Teelda0697b2014-02-18 13:57:00 -0600876 &dev_attr_hp_ssd_smart_path_status,
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600877 &dev_attr_raid_offload_debug,
Tomas Henzlfb53c432015-11-06 16:24:09 +0100878 &dev_attr_lockup_detected,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600879 NULL,
880};
881
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500882#define HPSA_NRESERVED_CMDS (HPSA_CMDS_RESERVED_FOR_ABORTS + \
883 HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
884
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600885static struct scsi_host_template hpsa_driver_template = {
886 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600887 .name = HPSA,
888 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600889 .queuecommand = hpsa_scsi_queue_command,
890 .scan_start = hpsa_scan_start,
891 .scan_finished = hpsa_scan_finished,
Don Brace7c0a0222015-01-23 16:41:30 -0600892 .change_queue_depth = hpsa_change_queue_depth,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600893 .this_id = -1,
894 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500895 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600896 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
897 .ioctl = hpsa_ioctl,
898 .slave_alloc = hpsa_slave_alloc,
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500899 .slave_configure = hpsa_slave_configure,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600900 .slave_destroy = hpsa_slave_destroy,
901#ifdef CONFIG_COMPAT
902 .compat_ioctl = hpsa_compat_ioctl,
903#endif
904 .sdev_attrs = hpsa_sdev_attrs,
905 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500906 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400907 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600908};
909
Matt Gates254f7962012-05-01 11:43:06 -0500910static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600911{
912 u32 a;
Stephen M. Cameron072b0512014-05-29 10:53:07 -0500913 struct reply_queue_buffer *rq = &h->reply_queue[q];
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600914
Matt Gatese1f7de02014-02-18 13:55:17 -0600915 if (h->transMethod & CFGTBL_Trans_io_accel1)
916 return h->access.command_completed(h, q);
917
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600918 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500919 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600920
Matt Gates254f7962012-05-01 11:43:06 -0500921 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
922 a = rq->head[rq->current_entry];
923 rq->current_entry++;
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600924 atomic_dec(&h->commands_outstanding);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600925 } else {
926 a = FIFO_EMPTY;
927 }
928 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500929 if (rq->current_entry == h->max_commands) {
930 rq->current_entry = 0;
931 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600932 }
933 return a;
934}
935
Scott Teelc3497752014-02-18 13:56:34 -0600936/*
937 * There are some special bits in the bus address of the
938 * command that we have to set for the controller to know
939 * how to process the command:
940 *
941 * Normal performant mode:
942 * bit 0: 1 means performant mode, 0 means simple mode.
943 * bits 1-3 = block fetch table entry
944 * bits 4-6 = command type (== 0)
945 *
946 * ioaccel1 mode:
947 * bit 0 = "performant mode" bit.
948 * bits 1-3 = block fetch table entry
949 * bits 4-6 = command type (== 110)
950 * (command type is needed because ioaccel1 mode
951 * commands are submitted through the same register as normal
952 * mode commands, so this is how the controller knows whether
953 * the command is normal mode or ioaccel1 mode.)
954 *
955 * ioaccel2 mode:
956 * bit 0 = "performant mode" bit.
957 * bits 1-4 = block fetch table entry (note extra bit)
958 * bits 4-6 = not needed, because ioaccel2 mode has
959 * a separate special register for submitting commands.
960 */
961
Webb Scales25163bd2015-04-23 09:32:00 -0500962/*
963 * set_performant_mode: Modify the tag for cciss performant
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600964 * set bit 0 for pull model, bits 3-1 for block fetch
965 * register number
966 */
Webb Scales25163bd2015-04-23 09:32:00 -0500967#define DEFAULT_REPLY_QUEUE (-1)
968static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
969 int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600970{
Matt Gates254f7962012-05-01 11:43:06 -0500971 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600972 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Webb Scales25163bd2015-04-23 09:32:00 -0500973 if (unlikely(!h->msix_vector))
974 return;
975 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
Matt Gates254f7962012-05-01 11:43:06 -0500976 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +0200977 raw_smp_processor_id() % h->nreply_queues;
Webb Scales25163bd2015-04-23 09:32:00 -0500978 else
979 c->Header.ReplyQueue = reply_queue % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -0500980 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600981}
982
Scott Teelc3497752014-02-18 13:56:34 -0600983static void set_ioaccel1_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -0500984 struct CommandList *c,
985 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -0600986{
987 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
988
Webb Scales25163bd2015-04-23 09:32:00 -0500989 /*
990 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -0600991 * processor. This seems to give the best I/O throughput.
992 */
Webb Scales25163bd2015-04-23 09:32:00 -0500993 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
994 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
995 else
996 cp->ReplyQueue = reply_queue % h->nreply_queues;
997 /*
998 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -0600999 * - performant mode bit (bit 0)
1000 * - pull count (bits 1-3)
1001 * - command type (bits 4-6)
1002 */
1003 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
1004 IOACCEL1_BUSADDR_CMDTYPE;
1005}
1006
Stephen Cameron8be986c2015-04-23 09:34:06 -05001007static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
1008 struct CommandList *c,
1009 int reply_queue)
1010{
1011 struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
1012 &h->ioaccel2_cmd_pool[c->cmdindex];
1013
1014 /* Tell the controller to post the reply to the queue for this
1015 * processor. This seems to give the best I/O throughput.
1016 */
1017 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1018 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1019 else
1020 cp->reply_queue = reply_queue % h->nreply_queues;
1021 /* Set the bits in the address sent down to include:
1022 * - performant mode bit not used in ioaccel mode 2
1023 * - pull count (bits 0-3)
1024 * - command type isn't needed for ioaccel2
1025 */
1026 c->busaddr |= h->ioaccel2_blockFetchTable[0];
1027}
1028
Scott Teelc3497752014-02-18 13:56:34 -06001029static void set_ioaccel2_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05001030 struct CommandList *c,
1031 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -06001032{
1033 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
1034
Webb Scales25163bd2015-04-23 09:32:00 -05001035 /*
1036 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -06001037 * processor. This seems to give the best I/O throughput.
1038 */
Webb Scales25163bd2015-04-23 09:32:00 -05001039 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1040 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1041 else
1042 cp->reply_queue = reply_queue % h->nreply_queues;
1043 /*
1044 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -06001045 * - performant mode bit not used in ioaccel mode 2
1046 * - pull count (bits 0-3)
1047 * - command type isn't needed for ioaccel2
1048 */
1049 c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
1050}
1051
Stephen M. Camerone85c5972012-05-01 11:43:42 -05001052static int is_firmware_flash_cmd(u8 *cdb)
1053{
1054 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
1055}
1056
1057/*
1058 * During firmware flash, the heartbeat register may not update as frequently
1059 * as it should. So we dial down lockup detection during firmware flash. and
1060 * dial it back up when firmware flash completes.
1061 */
1062#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
1063#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
1064static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
1065 struct CommandList *c)
1066{
1067 if (!is_firmware_flash_cmd(c->Request.CDB))
1068 return;
1069 atomic_inc(&h->firmware_flash_in_progress);
1070 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
1071}
1072
1073static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
1074 struct CommandList *c)
1075{
1076 if (is_firmware_flash_cmd(c->Request.CDB) &&
1077 atomic_dec_and_test(&h->firmware_flash_in_progress))
1078 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
1079}
1080
Webb Scales25163bd2015-04-23 09:32:00 -05001081static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
1082 struct CommandList *c, int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001083{
Stephen Cameronc05e8862015-01-23 16:44:40 -06001084 dial_down_lockup_detection_during_fw_flash(h, c);
1085 atomic_inc(&h->commands_outstanding);
Scott Teelc3497752014-02-18 13:56:34 -06001086 switch (c->cmd_type) {
1087 case CMD_IOACCEL1:
Webb Scales25163bd2015-04-23 09:32:00 -05001088 set_ioaccel1_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001089 writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
Scott Teelc3497752014-02-18 13:56:34 -06001090 break;
1091 case CMD_IOACCEL2:
Webb Scales25163bd2015-04-23 09:32:00 -05001092 set_ioaccel2_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001093 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
Scott Teelc3497752014-02-18 13:56:34 -06001094 break;
Stephen Cameron8be986c2015-04-23 09:34:06 -05001095 case IOACCEL2_TMF:
1096 set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
1097 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
1098 break;
Scott Teelc3497752014-02-18 13:56:34 -06001099 default:
Webb Scales25163bd2015-04-23 09:32:00 -05001100 set_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001101 h->access.submit_command(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06001102 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001103}
1104
Webb Scalesa58e7e52015-04-23 09:34:16 -05001105static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
Webb Scales25163bd2015-04-23 09:32:00 -05001106{
Webb Scalesd604f532015-04-23 09:35:22 -05001107 if (unlikely(hpsa_is_pending_event(c)))
Webb Scalesa58e7e52015-04-23 09:34:16 -05001108 return finish_cmd(c);
1109
Webb Scales25163bd2015-04-23 09:32:00 -05001110 __enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
1111}
1112
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001113static inline int is_hba_lunid(unsigned char scsi3addr[])
1114{
1115 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
1116}
1117
1118static inline int is_scsi_rev_5(struct ctlr_info *h)
1119{
1120 if (!h->hba_inquiry_data)
1121 return 0;
1122 if ((h->hba_inquiry_data[2] & 0x07) == 5)
1123 return 1;
1124 return 0;
1125}
1126
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001127static int hpsa_find_target_lun(struct ctlr_info *h,
1128 unsigned char scsi3addr[], int bus, int *target, int *lun)
1129{
1130 /* finds an unused bus, target, lun for a new physical device
1131 * assumes h->devlock is held
1132 */
1133 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -05001134 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001135
Akinobu Mita263d9402012-01-21 00:15:27 +09001136 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001137
1138 for (i = 0; i < h->ndevices; i++) {
1139 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +09001140 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001141 }
1142
Akinobu Mita263d9402012-01-21 00:15:27 +09001143 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
1144 if (i < HPSA_MAX_DEVICES) {
1145 /* *bus = 1; */
1146 *target = i;
1147 *lun = 0;
1148 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001149 }
1150 return !found;
1151}
1152
Don Brace1d33d852015-11-04 15:50:31 -06001153static void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
Webb Scales0d96ef52015-04-23 09:31:55 -05001154 struct hpsa_scsi_dev_t *dev, char *description)
1155{
Don Brace7c59a0d2015-11-04 15:52:22 -06001156#define LABEL_SIZE 25
1157 char label[LABEL_SIZE];
1158
Don Brace9975ec92015-11-04 15:50:25 -06001159 if (h == NULL || h->pdev == NULL || h->scsi_host == NULL)
1160 return;
1161
Don Brace7c59a0d2015-11-04 15:52:22 -06001162 switch (dev->devtype) {
1163 case TYPE_RAID:
1164 snprintf(label, LABEL_SIZE, "controller");
1165 break;
1166 case TYPE_ENCLOSURE:
1167 snprintf(label, LABEL_SIZE, "enclosure");
1168 break;
1169 case TYPE_DISK:
Don Braceaf15ed32016-02-23 15:16:15 -06001170 case TYPE_ZBC:
Don Brace7c59a0d2015-11-04 15:52:22 -06001171 if (dev->external)
1172 snprintf(label, LABEL_SIZE, "external");
1173 else if (!is_logical_dev_addr_mode(dev->scsi3addr))
1174 snprintf(label, LABEL_SIZE, "%s",
1175 raid_label[PHYSICAL_DRIVE]);
1176 else
1177 snprintf(label, LABEL_SIZE, "RAID-%s",
1178 dev->raid_level > RAID_UNKNOWN ? "?" :
1179 raid_label[dev->raid_level]);
1180 break;
1181 case TYPE_ROM:
1182 snprintf(label, LABEL_SIZE, "rom");
1183 break;
1184 case TYPE_TAPE:
1185 snprintf(label, LABEL_SIZE, "tape");
1186 break;
1187 case TYPE_MEDIUM_CHANGER:
1188 snprintf(label, LABEL_SIZE, "changer");
1189 break;
1190 default:
1191 snprintf(label, LABEL_SIZE, "UNKNOWN");
1192 break;
1193 }
1194
Webb Scales0d96ef52015-04-23 09:31:55 -05001195 dev_printk(level, &h->pdev->dev,
Don Brace7c59a0d2015-11-04 15:52:22 -06001196 "scsi %d:%d:%d:%d: %s %s %.8s %.16s %s SSDSmartPathCap%c En%c Exp=%d\n",
Webb Scales0d96ef52015-04-23 09:31:55 -05001197 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
1198 description,
1199 scsi_device_type(dev->devtype),
1200 dev->vendor,
1201 dev->model,
Don Brace7c59a0d2015-11-04 15:52:22 -06001202 label,
Webb Scales0d96ef52015-04-23 09:31:55 -05001203 dev->offload_config ? '+' : '-',
1204 dev->offload_enabled ? '+' : '-',
Kevin Barnett2a168202015-11-04 15:51:21 -06001205 dev->expose_device);
Webb Scales0d96ef52015-04-23 09:31:55 -05001206}
1207
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001208/* Add an entry into h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001209static int hpsa_scsi_add_entry(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001210 struct hpsa_scsi_dev_t *device,
1211 struct hpsa_scsi_dev_t *added[], int *nadded)
1212{
1213 /* assumes h->devlock is held */
1214 int n = h->ndevices;
1215 int i;
1216 unsigned char addr1[8], addr2[8];
1217 struct hpsa_scsi_dev_t *sd;
1218
Scott Teelcfe5bad2011-10-26 16:21:07 -05001219 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001220 dev_err(&h->pdev->dev, "too many devices, some will be "
1221 "inaccessible.\n");
1222 return -1;
1223 }
1224
1225 /* physical devices do not have lun or target assigned until now. */
1226 if (device->lun != -1)
1227 /* Logical device, lun is already assigned. */
1228 goto lun_assigned;
1229
1230 /* If this device a non-zero lun of a multi-lun device
1231 * byte 4 of the 8-byte LUN addr will contain the logical
Don Brace2b08b3e2015-01-23 16:41:09 -06001232 * unit no, zero otherwise.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001233 */
1234 if (device->scsi3addr[4] == 0) {
1235 /* This is not a non-zero lun of a multi-lun device */
1236 if (hpsa_find_target_lun(h, device->scsi3addr,
1237 device->bus, &device->target, &device->lun) != 0)
1238 return -1;
1239 goto lun_assigned;
1240 }
1241
1242 /* This is a non-zero lun of a multi-lun device.
1243 * Search through our list and find the device which
shane.seymour9a4178b2015-07-18 11:13:09 -05001244 * has the same 8 byte LUN address, excepting byte 4 and 5.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001245 * Assign the same bus and target for this new LUN.
1246 * Use the logical unit number from the firmware.
1247 */
1248 memcpy(addr1, device->scsi3addr, 8);
1249 addr1[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001250 addr1[5] = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001251 for (i = 0; i < n; i++) {
1252 sd = h->dev[i];
1253 memcpy(addr2, sd->scsi3addr, 8);
1254 addr2[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001255 addr2[5] = 0;
1256 /* differ only in byte 4 and 5? */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001257 if (memcmp(addr1, addr2, 8) == 0) {
1258 device->bus = sd->bus;
1259 device->target = sd->target;
1260 device->lun = device->scsi3addr[4];
1261 break;
1262 }
1263 }
1264 if (device->lun == -1) {
1265 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1266 " suspect firmware bug or unsupported hardware "
1267 "configuration.\n");
1268 return -1;
1269 }
1270
1271lun_assigned:
1272
1273 h->dev[n] = device;
1274 h->ndevices++;
1275 added[*nadded] = device;
1276 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001277 hpsa_show_dev_msg(KERN_INFO, h, device,
Kevin Barnett2a168202015-11-04 15:51:21 -06001278 device->expose_device ? "added" : "masked");
Robert Elliotta473d862015-04-23 09:32:54 -05001279 device->offload_to_be_enabled = device->offload_enabled;
1280 device->offload_enabled = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001281 return 0;
1282}
1283
Scott Teelbd9244f2012-01-19 14:01:30 -06001284/* Update an entry in h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001285static void hpsa_scsi_update_entry(struct ctlr_info *h,
Scott Teelbd9244f2012-01-19 14:01:30 -06001286 int entry, struct hpsa_scsi_dev_t *new_entry)
1287{
Robert Elliotta473d862015-04-23 09:32:54 -05001288 int offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001289 /* assumes h->devlock is held */
1290 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1291
1292 /* Raid level changed. */
1293 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001294
Don Brace03383732015-01-23 16:43:30 -06001295 /* Raid offload parameters changed. Careful about the ordering. */
1296 if (new_entry->offload_config && new_entry->offload_enabled) {
1297 /*
1298 * if drive is newly offload_enabled, we want to copy the
1299 * raid map data first. If previously offload_enabled and
1300 * offload_config were set, raid map data had better be
1301 * the same as it was before. if raid map data is changed
1302 * then it had better be the case that
1303 * h->dev[entry]->offload_enabled is currently 0.
1304 */
1305 h->dev[entry]->raid_map = new_entry->raid_map;
1306 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
Don Brace03383732015-01-23 16:43:30 -06001307 }
Joe Handzika3144e02015-04-23 09:32:59 -05001308 if (new_entry->hba_ioaccel_enabled) {
1309 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1310 wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1311 }
1312 h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001313 h->dev[entry]->offload_config = new_entry->offload_config;
Stephen M. Cameron9fb0de22014-02-18 13:56:50 -06001314 h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
Don Brace03383732015-01-23 16:43:30 -06001315 h->dev[entry]->queue_depth = new_entry->queue_depth;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001316
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001317 /*
1318 * We can turn off ioaccel offload now, but need to delay turning
1319 * it on until we can update h->dev[entry]->phys_disk[], but we
1320 * can't do that until all the devices are updated.
1321 */
1322 h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
1323 if (!new_entry->offload_enabled)
1324 h->dev[entry]->offload_enabled = 0;
1325
Robert Elliotta473d862015-04-23 09:32:54 -05001326 offload_enabled = h->dev[entry]->offload_enabled;
1327 h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
Webb Scales0d96ef52015-04-23 09:31:55 -05001328 hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
Robert Elliotta473d862015-04-23 09:32:54 -05001329 h->dev[entry]->offload_enabled = offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001330}
1331
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001332/* Replace an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001333static void hpsa_scsi_replace_entry(struct ctlr_info *h,
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001334 int entry, struct hpsa_scsi_dev_t *new_entry,
1335 struct hpsa_scsi_dev_t *added[], int *nadded,
1336 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1337{
1338 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -05001339 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001340 removed[*nremoved] = h->dev[entry];
1341 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -05001342
1343 /*
1344 * New physical devices won't have target/lun assigned yet
1345 * so we need to preserve the values in the slot we are replacing.
1346 */
1347 if (new_entry->target == -1) {
1348 new_entry->target = h->dev[entry]->target;
1349 new_entry->lun = h->dev[entry]->lun;
1350 }
1351
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001352 h->dev[entry] = new_entry;
1353 added[*nadded] = new_entry;
1354 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001355 hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
Robert Elliotta473d862015-04-23 09:32:54 -05001356 new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1357 new_entry->offload_enabled = 0;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001358}
1359
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001360/* Remove an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001361static void hpsa_scsi_remove_entry(struct ctlr_info *h, int entry,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001362 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1363{
1364 /* assumes h->devlock is held */
1365 int i;
1366 struct hpsa_scsi_dev_t *sd;
1367
Scott Teelcfe5bad2011-10-26 16:21:07 -05001368 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001369
1370 sd = h->dev[entry];
1371 removed[*nremoved] = h->dev[entry];
1372 (*nremoved)++;
1373
1374 for (i = entry; i < h->ndevices-1; i++)
1375 h->dev[i] = h->dev[i+1];
1376 h->ndevices--;
Webb Scales0d96ef52015-04-23 09:31:55 -05001377 hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001378}
1379
1380#define SCSI3ADDR_EQ(a, b) ( \
1381 (a)[7] == (b)[7] && \
1382 (a)[6] == (b)[6] && \
1383 (a)[5] == (b)[5] && \
1384 (a)[4] == (b)[4] && \
1385 (a)[3] == (b)[3] && \
1386 (a)[2] == (b)[2] && \
1387 (a)[1] == (b)[1] && \
1388 (a)[0] == (b)[0])
1389
1390static void fixup_botched_add(struct ctlr_info *h,
1391 struct hpsa_scsi_dev_t *added)
1392{
1393 /* called when scsi_add_device fails in order to re-adjust
1394 * h->dev[] to match the mid layer's view.
1395 */
1396 unsigned long flags;
1397 int i, j;
1398
1399 spin_lock_irqsave(&h->lock, flags);
1400 for (i = 0; i < h->ndevices; i++) {
1401 if (h->dev[i] == added) {
1402 for (j = i; j < h->ndevices-1; j++)
1403 h->dev[j] = h->dev[j+1];
1404 h->ndevices--;
1405 break;
1406 }
1407 }
1408 spin_unlock_irqrestore(&h->lock, flags);
1409 kfree(added);
1410}
1411
1412static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1413 struct hpsa_scsi_dev_t *dev2)
1414{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001415 /* we compare everything except lun and target as these
1416 * are not yet assigned. Compare parts likely
1417 * to differ first
1418 */
1419 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1420 sizeof(dev1->scsi3addr)) != 0)
1421 return 0;
1422 if (memcmp(dev1->device_id, dev2->device_id,
1423 sizeof(dev1->device_id)) != 0)
1424 return 0;
1425 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1426 return 0;
1427 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1428 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001429 if (dev1->devtype != dev2->devtype)
1430 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001431 if (dev1->bus != dev2->bus)
1432 return 0;
1433 return 1;
1434}
1435
Scott Teelbd9244f2012-01-19 14:01:30 -06001436static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1437 struct hpsa_scsi_dev_t *dev2)
1438{
1439 /* Device attributes that can change, but don't mean
1440 * that the device is a different device, nor that the OS
1441 * needs to be told anything about the change.
1442 */
1443 if (dev1->raid_level != dev2->raid_level)
1444 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001445 if (dev1->offload_config != dev2->offload_config)
1446 return 1;
1447 if (dev1->offload_enabled != dev2->offload_enabled)
1448 return 1;
Don Brace93849502015-07-18 11:12:49 -05001449 if (!is_logical_dev_addr_mode(dev1->scsi3addr))
1450 if (dev1->queue_depth != dev2->queue_depth)
1451 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -06001452 return 0;
1453}
1454
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001455/* Find needle in haystack. If exact match found, return DEVICE_SAME,
1456 * and return needle location in *index. If scsi3addr matches, but not
1457 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -06001458 * location in *index.
1459 * In the case of a minor device attribute change, such as RAID level, just
1460 * return DEVICE_UPDATED, along with the updated device's location in index.
1461 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001462 */
1463static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1464 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1465 int *index)
1466{
1467 int i;
1468#define DEVICE_NOT_FOUND 0
1469#define DEVICE_CHANGED 1
1470#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -06001471#define DEVICE_UPDATED 3
Don Brace1d33d852015-11-04 15:50:31 -06001472 if (needle == NULL)
1473 return DEVICE_NOT_FOUND;
1474
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001475 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -06001476 if (haystack[i] == NULL) /* previously removed. */
1477 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001478 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1479 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -06001480 if (device_is_the_same(needle, haystack[i])) {
1481 if (device_updated(needle, haystack[i]))
1482 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001483 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -06001484 } else {
Stephen M. Cameron98465902014-02-21 16:25:00 -06001485 /* Keep offline devices offline */
1486 if (needle->volume_offline)
1487 return DEVICE_NOT_FOUND;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001488 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -06001489 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001490 }
1491 }
1492 *index = -1;
1493 return DEVICE_NOT_FOUND;
1494}
1495
Stephen M. Cameron98465902014-02-21 16:25:00 -06001496static void hpsa_monitor_offline_device(struct ctlr_info *h,
1497 unsigned char scsi3addr[])
1498{
1499 struct offline_device_entry *device;
1500 unsigned long flags;
1501
1502 /* Check to see if device is already on the list */
1503 spin_lock_irqsave(&h->offline_device_lock, flags);
1504 list_for_each_entry(device, &h->offline_device_list, offline_list) {
1505 if (memcmp(device->scsi3addr, scsi3addr,
1506 sizeof(device->scsi3addr)) == 0) {
1507 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1508 return;
1509 }
1510 }
1511 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1512
1513 /* Device is not on the list, add it. */
1514 device = kmalloc(sizeof(*device), GFP_KERNEL);
1515 if (!device) {
1516 dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
1517 return;
1518 }
1519 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1520 spin_lock_irqsave(&h->offline_device_lock, flags);
1521 list_add_tail(&device->offline_list, &h->offline_device_list);
1522 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1523}
1524
1525/* Print a message explaining various offline volume states */
1526static void hpsa_show_volume_status(struct ctlr_info *h,
1527 struct hpsa_scsi_dev_t *sd)
1528{
1529 if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
1530 dev_info(&h->pdev->dev,
1531 "C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
1532 h->scsi_host->host_no,
1533 sd->bus, sd->target, sd->lun);
1534 switch (sd->volume_offline) {
1535 case HPSA_LV_OK:
1536 break;
1537 case HPSA_LV_UNDERGOING_ERASE:
1538 dev_info(&h->pdev->dev,
1539 "C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
1540 h->scsi_host->host_no,
1541 sd->bus, sd->target, sd->lun);
1542 break;
Scott Benesh5ca01202015-07-18 11:13:04 -05001543 case HPSA_LV_NOT_AVAILABLE:
1544 dev_info(&h->pdev->dev,
1545 "C%d:B%d:T%d:L%d Volume is waiting for transforming volume.\n",
1546 h->scsi_host->host_no,
1547 sd->bus, sd->target, sd->lun);
1548 break;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001549 case HPSA_LV_UNDERGOING_RPI:
1550 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001551 "C%d:B%d:T%d:L%d Volume is undergoing rapid parity init.\n",
Stephen M. Cameron98465902014-02-21 16:25:00 -06001552 h->scsi_host->host_no,
1553 sd->bus, sd->target, sd->lun);
1554 break;
1555 case HPSA_LV_PENDING_RPI:
1556 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001557 "C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
1558 h->scsi_host->host_no,
1559 sd->bus, sd->target, sd->lun);
Stephen M. Cameron98465902014-02-21 16:25:00 -06001560 break;
1561 case HPSA_LV_ENCRYPTED_NO_KEY:
1562 dev_info(&h->pdev->dev,
1563 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
1564 h->scsi_host->host_no,
1565 sd->bus, sd->target, sd->lun);
1566 break;
1567 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
1568 dev_info(&h->pdev->dev,
1569 "C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
1570 h->scsi_host->host_no,
1571 sd->bus, sd->target, sd->lun);
1572 break;
1573 case HPSA_LV_UNDERGOING_ENCRYPTION:
1574 dev_info(&h->pdev->dev,
1575 "C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
1576 h->scsi_host->host_no,
1577 sd->bus, sd->target, sd->lun);
1578 break;
1579 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
1580 dev_info(&h->pdev->dev,
1581 "C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
1582 h->scsi_host->host_no,
1583 sd->bus, sd->target, sd->lun);
1584 break;
1585 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1586 dev_info(&h->pdev->dev,
1587 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
1588 h->scsi_host->host_no,
1589 sd->bus, sd->target, sd->lun);
1590 break;
1591 case HPSA_LV_PENDING_ENCRYPTION:
1592 dev_info(&h->pdev->dev,
1593 "C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
1594 h->scsi_host->host_no,
1595 sd->bus, sd->target, sd->lun);
1596 break;
1597 case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
1598 dev_info(&h->pdev->dev,
1599 "C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
1600 h->scsi_host->host_no,
1601 sd->bus, sd->target, sd->lun);
1602 break;
1603 }
1604}
1605
Don Brace03383732015-01-23 16:43:30 -06001606/*
1607 * Figure the list of physical drive pointers for a logical drive with
1608 * raid offload configured.
1609 */
1610static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
1611 struct hpsa_scsi_dev_t *dev[], int ndevices,
1612 struct hpsa_scsi_dev_t *logical_drive)
1613{
1614 struct raid_map_data *map = &logical_drive->raid_map;
1615 struct raid_map_disk_data *dd = &map->data[0];
1616 int i, j;
1617 int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
1618 le16_to_cpu(map->metadata_disks_per_row);
1619 int nraid_map_entries = le16_to_cpu(map->row_cnt) *
1620 le16_to_cpu(map->layout_map_count) *
1621 total_disks_per_row;
1622 int nphys_disk = le16_to_cpu(map->layout_map_count) *
1623 total_disks_per_row;
1624 int qdepth;
1625
1626 if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
1627 nraid_map_entries = RAID_MAP_MAX_ENTRIES;
1628
Webb Scalesd604f532015-04-23 09:35:22 -05001629 logical_drive->nphysical_disks = nraid_map_entries;
1630
Don Brace03383732015-01-23 16:43:30 -06001631 qdepth = 0;
1632 for (i = 0; i < nraid_map_entries; i++) {
1633 logical_drive->phys_disk[i] = NULL;
1634 if (!logical_drive->offload_config)
1635 continue;
1636 for (j = 0; j < ndevices; j++) {
Don Brace1d33d852015-11-04 15:50:31 -06001637 if (dev[j] == NULL)
1638 continue;
Don Brace03383732015-01-23 16:43:30 -06001639 if (dev[j]->devtype != TYPE_DISK)
1640 continue;
Don Braceaf15ed32016-02-23 15:16:15 -06001641 if (dev[j]->devtype != TYPE_ZBC)
1642 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001643 if (is_logical_device(dev[j]))
Don Brace03383732015-01-23 16:43:30 -06001644 continue;
1645 if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
1646 continue;
1647
1648 logical_drive->phys_disk[i] = dev[j];
1649 if (i < nphys_disk)
1650 qdepth = min(h->nr_cmds, qdepth +
1651 logical_drive->phys_disk[i]->queue_depth);
1652 break;
1653 }
1654
1655 /*
1656 * This can happen if a physical drive is removed and
1657 * the logical drive is degraded. In that case, the RAID
1658 * map data will refer to a physical disk which isn't actually
1659 * present. And in that case offload_enabled should already
1660 * be 0, but we'll turn it off here just in case
1661 */
1662 if (!logical_drive->phys_disk[i]) {
1663 logical_drive->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001664 logical_drive->offload_to_be_enabled = 0;
1665 logical_drive->queue_depth = 8;
Don Brace03383732015-01-23 16:43:30 -06001666 }
1667 }
1668 if (nraid_map_entries)
1669 /*
1670 * This is correct for reads, too high for full stripe writes,
1671 * way too high for partial stripe writes
1672 */
1673 logical_drive->queue_depth = qdepth;
1674 else
1675 logical_drive->queue_depth = h->nr_cmds;
1676}
1677
1678static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
1679 struct hpsa_scsi_dev_t *dev[], int ndevices)
1680{
1681 int i;
1682
1683 for (i = 0; i < ndevices; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001684 if (dev[i] == NULL)
1685 continue;
Don Brace03383732015-01-23 16:43:30 -06001686 if (dev[i]->devtype != TYPE_DISK)
1687 continue;
Don Braceaf15ed32016-02-23 15:16:15 -06001688 if (dev[i]->devtype != TYPE_ZBC)
1689 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001690 if (!is_logical_device(dev[i]))
Don Brace03383732015-01-23 16:43:30 -06001691 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001692
1693 /*
1694 * If offload is currently enabled, the RAID map and
1695 * phys_disk[] assignment *better* not be changing
1696 * and since it isn't changing, we do not need to
1697 * update it.
1698 */
1699 if (dev[i]->offload_enabled)
1700 continue;
1701
Don Brace03383732015-01-23 16:43:30 -06001702 hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
1703 }
1704}
1705
Kevin Barnett096ccff2015-11-04 15:51:51 -06001706static int hpsa_add_device(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1707{
1708 int rc = 0;
1709
1710 if (!h->scsi_host)
1711 return 1;
1712
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001713 if (is_logical_device(device)) /* RAID */
1714 rc = scsi_add_device(h->scsi_host, device->bus,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001715 device->target, device->lun);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001716 else /* HBA */
1717 rc = hpsa_add_sas_device(h->sas_host, device);
1718
Kevin Barnett096ccff2015-11-04 15:51:51 -06001719 return rc;
1720}
1721
1722static void hpsa_remove_device(struct ctlr_info *h,
1723 struct hpsa_scsi_dev_t *device)
1724{
1725 struct scsi_device *sdev = NULL;
1726
1727 if (!h->scsi_host)
1728 return;
1729
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001730 if (is_logical_device(device)) { /* RAID */
1731 sdev = scsi_device_lookup(h->scsi_host, device->bus,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001732 device->target, device->lun);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001733 if (sdev) {
1734 scsi_remove_device(sdev);
1735 scsi_device_put(sdev);
1736 } else {
1737 /*
1738 * We don't expect to get here. Future commands
1739 * to this device will get a selection timeout as
1740 * if the device were gone.
1741 */
1742 hpsa_show_dev_msg(KERN_WARNING, h, device,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001743 "didn't find device for removal.");
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001744 }
1745 } else /* HBA */
1746 hpsa_remove_sas_device(device);
Kevin Barnett096ccff2015-11-04 15:51:51 -06001747}
1748
Don Brace8aa60682015-11-04 15:50:01 -06001749static void adjust_hpsa_scsi_table(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001750 struct hpsa_scsi_dev_t *sd[], int nsds)
1751{
1752 /* sd contains scsi3 addresses and devtypes, and inquiry
1753 * data. This function takes what's in sd to be the current
1754 * reality and updates h->dev[] to reflect that reality.
1755 */
1756 int i, entry, device_change, changes = 0;
1757 struct hpsa_scsi_dev_t *csd;
1758 unsigned long flags;
1759 struct hpsa_scsi_dev_t **added, **removed;
1760 int nadded, nremoved;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001761
Don Braceda03ded2015-11-04 15:50:56 -06001762 /*
1763 * A reset can cause a device status to change
1764 * re-schedule the scan to see what happened.
1765 */
1766 if (h->reset_in_progress) {
1767 h->drv_req_rescan = 1;
1768 return;
1769 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001770
Scott Teelcfe5bad2011-10-26 16:21:07 -05001771 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1772 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001773
1774 if (!added || !removed) {
1775 dev_warn(&h->pdev->dev, "out of memory in "
1776 "adjust_hpsa_scsi_table\n");
1777 goto free_and_out;
1778 }
1779
1780 spin_lock_irqsave(&h->devlock, flags);
1781
1782 /* find any devices in h->dev[] that are not in
1783 * sd[] and remove them from h->dev[], and for any
1784 * devices which have changed, remove the old device
1785 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001786 * If minor device attributes change, just update
1787 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001788 */
1789 i = 0;
1790 nremoved = 0;
1791 nadded = 0;
1792 while (i < h->ndevices) {
1793 csd = h->dev[i];
1794 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1795 if (device_change == DEVICE_NOT_FOUND) {
1796 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001797 hpsa_scsi_remove_entry(h, i, removed, &nremoved);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001798 continue; /* remove ^^^, hence i not incremented */
1799 } else if (device_change == DEVICE_CHANGED) {
1800 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001801 hpsa_scsi_replace_entry(h, i, sd[entry],
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001802 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001803 /* Set it to NULL to prevent it from being freed
1804 * at the bottom of hpsa_update_scsi_devices()
1805 */
1806 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001807 } else if (device_change == DEVICE_UPDATED) {
Don Brace8aa60682015-11-04 15:50:01 -06001808 hpsa_scsi_update_entry(h, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001809 }
1810 i++;
1811 }
1812
1813 /* Now, make sure every device listed in sd[] is also
1814 * listed in h->dev[], adding them if they aren't found
1815 */
1816
1817 for (i = 0; i < nsds; i++) {
1818 if (!sd[i]) /* if already added above. */
1819 continue;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001820
1821 /* Don't add devices which are NOT READY, FORMAT IN PROGRESS
1822 * as the SCSI mid-layer does not handle such devices well.
1823 * It relentlessly loops sending TUR at 3Hz, then READ(10)
1824 * at 160Hz, and prevents the system from coming up.
1825 */
1826 if (sd[i]->volume_offline) {
1827 hpsa_show_volume_status(h, sd[i]);
Webb Scales0d96ef52015-04-23 09:31:55 -05001828 hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
Stephen M. Cameron98465902014-02-21 16:25:00 -06001829 continue;
1830 }
1831
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001832 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1833 h->ndevices, &entry);
1834 if (device_change == DEVICE_NOT_FOUND) {
1835 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001836 if (hpsa_scsi_add_entry(h, sd[i], added, &nadded) != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001837 break;
1838 sd[i] = NULL; /* prevent from being freed later. */
1839 } else if (device_change == DEVICE_CHANGED) {
1840 /* should never happen... */
1841 changes++;
1842 dev_warn(&h->pdev->dev,
1843 "device unexpectedly changed.\n");
1844 /* but if it does happen, we just ignore that device */
1845 }
1846 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001847 hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
1848
1849 /* Now that h->dev[]->phys_disk[] is coherent, we can enable
1850 * any logical drives that need it enabled.
1851 */
Don Brace1d33d852015-11-04 15:50:31 -06001852 for (i = 0; i < h->ndevices; i++) {
1853 if (h->dev[i] == NULL)
1854 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001855 h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
Don Brace1d33d852015-11-04 15:50:31 -06001856 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001857
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001858 spin_unlock_irqrestore(&h->devlock, flags);
1859
Stephen M. Cameron98465902014-02-21 16:25:00 -06001860 /* Monitor devices which are in one of several NOT READY states to be
1861 * brought online later. This must be done without holding h->devlock,
1862 * so don't touch h->dev[]
1863 */
1864 for (i = 0; i < nsds; i++) {
1865 if (!sd[i]) /* if already added above. */
1866 continue;
1867 if (sd[i]->volume_offline)
1868 hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
1869 }
1870
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001871 /* Don't notify scsi mid layer of any changes the first time through
1872 * (or if there are no changes) scsi_scan_host will do it later the
1873 * first time through.
1874 */
Don Brace8aa60682015-11-04 15:50:01 -06001875 if (!changes)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001876 goto free_and_out;
1877
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001878 /* Notify scsi mid layer of any removed devices */
1879 for (i = 0; i < nremoved; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001880 if (removed[i] == NULL)
1881 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001882 if (removed[i]->expose_device)
1883 hpsa_remove_device(h, removed[i]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001884 kfree(removed[i]);
1885 removed[i] = NULL;
1886 }
1887
1888 /* Notify scsi mid layer of any added devices */
1889 for (i = 0; i < nadded; i++) {
Kevin Barnett096ccff2015-11-04 15:51:51 -06001890 int rc = 0;
1891
Don Brace1d33d852015-11-04 15:50:31 -06001892 if (added[i] == NULL)
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001893 continue;
Kevin Barnett2a168202015-11-04 15:51:21 -06001894 if (!(added[i]->expose_device))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001895 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001896 rc = hpsa_add_device(h, added[i]);
1897 if (!rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001898 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001899 dev_warn(&h->pdev->dev,
1900 "addition failed %d, device not added.", rc);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001901 /* now we have to remove it from h->dev,
1902 * since it didn't get added to scsi mid layer
1903 */
1904 fixup_botched_add(h, added[i]);
Don Brace853633e2015-11-04 15:50:37 -06001905 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001906 }
1907
1908free_and_out:
1909 kfree(added);
1910 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001911}
1912
1913/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001914 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001915 * Assume's h->devlock is held.
1916 */
1917static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1918 int bus, int target, int lun)
1919{
1920 int i;
1921 struct hpsa_scsi_dev_t *sd;
1922
1923 for (i = 0; i < h->ndevices; i++) {
1924 sd = h->dev[i];
1925 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1926 return sd;
1927 }
1928 return NULL;
1929}
1930
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001931static int hpsa_slave_alloc(struct scsi_device *sdev)
1932{
1933 struct hpsa_scsi_dev_t *sd;
1934 unsigned long flags;
1935 struct ctlr_info *h;
1936
1937 h = sdev_to_hba(sdev);
1938 spin_lock_irqsave(&h->devlock, flags);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001939 if (sdev_channel(sdev) == HPSA_PHYSICAL_DEVICE_BUS) {
1940 struct scsi_target *starget;
1941 struct sas_rphy *rphy;
1942
1943 starget = scsi_target(sdev);
1944 rphy = target_to_rphy(starget);
1945 sd = hpsa_find_device_by_sas_rphy(h, rphy);
1946 if (sd) {
1947 sd->target = sdev_id(sdev);
1948 sd->lun = sdev->lun;
1949 }
1950 } else
1951 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1952 sdev_id(sdev), sdev->lun);
1953
1954 if (sd && sd->expose_device) {
Don Brace03383732015-01-23 16:43:30 -06001955 atomic_set(&sd->ioaccel_cmds_out, 0);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001956 sdev->hostdata = sd;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001957 } else
1958 sdev->hostdata = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001959 spin_unlock_irqrestore(&h->devlock, flags);
1960 return 0;
1961}
1962
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001963/* configure scsi device based on internal per-device structure */
1964static int hpsa_slave_configure(struct scsi_device *sdev)
1965{
1966 struct hpsa_scsi_dev_t *sd;
1967 int queue_depth;
1968
1969 sd = sdev->hostdata;
Kevin Barnett2a168202015-11-04 15:51:21 -06001970 sdev->no_uld_attach = !sd || !sd->expose_device;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001971
1972 if (sd)
1973 queue_depth = sd->queue_depth != 0 ?
1974 sd->queue_depth : sdev->host->can_queue;
1975 else
1976 queue_depth = sdev->host->can_queue;
1977
1978 scsi_change_queue_depth(sdev, queue_depth);
1979
1980 return 0;
1981}
1982
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001983static void hpsa_slave_destroy(struct scsi_device *sdev)
1984{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06001985 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001986}
1987
Webb Scalesd9a729f2015-04-23 09:33:27 -05001988static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1989{
1990 int i;
1991
1992 if (!h->ioaccel2_cmd_sg_list)
1993 return;
1994 for (i = 0; i < h->nr_cmds; i++) {
1995 kfree(h->ioaccel2_cmd_sg_list[i]);
1996 h->ioaccel2_cmd_sg_list[i] = NULL;
1997 }
1998 kfree(h->ioaccel2_cmd_sg_list);
1999 h->ioaccel2_cmd_sg_list = NULL;
2000}
2001
2002static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
2003{
2004 int i;
2005
2006 if (h->chainsize <= 0)
2007 return 0;
2008
2009 h->ioaccel2_cmd_sg_list =
2010 kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
2011 GFP_KERNEL);
2012 if (!h->ioaccel2_cmd_sg_list)
2013 return -ENOMEM;
2014 for (i = 0; i < h->nr_cmds; i++) {
2015 h->ioaccel2_cmd_sg_list[i] =
2016 kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
2017 h->maxsgentries, GFP_KERNEL);
2018 if (!h->ioaccel2_cmd_sg_list[i])
2019 goto clean;
2020 }
2021 return 0;
2022
2023clean:
2024 hpsa_free_ioaccel2_sg_chain_blocks(h);
2025 return -ENOMEM;
2026}
2027
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002028static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
2029{
2030 int i;
2031
2032 if (!h->cmd_sg_list)
2033 return;
2034 for (i = 0; i < h->nr_cmds; i++) {
2035 kfree(h->cmd_sg_list[i]);
2036 h->cmd_sg_list[i] = NULL;
2037 }
2038 kfree(h->cmd_sg_list);
2039 h->cmd_sg_list = NULL;
2040}
2041
Robert Elliott105a3db2015-04-23 09:33:48 -05002042static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002043{
2044 int i;
2045
2046 if (h->chainsize <= 0)
2047 return 0;
2048
2049 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
2050 GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002051 if (!h->cmd_sg_list) {
2052 dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002053 return -ENOMEM;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002054 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002055 for (i = 0; i < h->nr_cmds; i++) {
2056 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
2057 h->chainsize, GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002058 if (!h->cmd_sg_list[i]) {
2059 dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002060 goto clean;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002061 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002062 }
2063 return 0;
2064
2065clean:
2066 hpsa_free_sg_chain_blocks(h);
2067 return -ENOMEM;
2068}
2069
Webb Scalesd9a729f2015-04-23 09:33:27 -05002070static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
2071 struct io_accel2_cmd *cp, struct CommandList *c)
2072{
2073 struct ioaccel2_sg_element *chain_block;
2074 u64 temp64;
2075 u32 chain_size;
2076
2077 chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
Don Bracea736e9b2015-11-04 15:51:14 -06002078 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002079 temp64 = pci_map_single(h->pdev, chain_block, chain_size,
2080 PCI_DMA_TODEVICE);
2081 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2082 /* prevent subsequent unmapping */
2083 cp->sg->address = 0;
2084 return -1;
2085 }
2086 cp->sg->address = cpu_to_le64(temp64);
2087 return 0;
2088}
2089
2090static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
2091 struct io_accel2_cmd *cp)
2092{
2093 struct ioaccel2_sg_element *chain_sg;
2094 u64 temp64;
2095 u32 chain_size;
2096
2097 chain_sg = cp->sg;
2098 temp64 = le64_to_cpu(chain_sg->address);
Don Bracea736e9b2015-11-04 15:51:14 -06002099 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002100 pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
2101}
2102
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002103static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002104 struct CommandList *c)
2105{
2106 struct SGDescriptor *chain_sg, *chain_block;
2107 u64 temp64;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002108 u32 chain_len;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002109
2110 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
2111 chain_block = h->cmd_sg_list[c->cmdindex];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002112 chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
2113 chain_len = sizeof(*chain_sg) *
Don Brace2b08b3e2015-01-23 16:41:09 -06002114 (le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002115 chain_sg->Len = cpu_to_le32(chain_len);
2116 temp64 = pci_map_single(h->pdev, chain_block, chain_len,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002117 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002118 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2119 /* prevent subsequent unmapping */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002120 chain_sg->Addr = cpu_to_le64(0);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002121 return -1;
2122 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002123 chain_sg->Addr = cpu_to_le64(temp64);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002124 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002125}
2126
2127static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
2128 struct CommandList *c)
2129{
2130 struct SGDescriptor *chain_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002131
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002132 if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002133 return;
2134
2135 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002136 pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
2137 le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002138}
2139
Scott Teela09c1442014-02-18 13:57:21 -06002140
2141/* Decode the various types of errors on ioaccel2 path.
2142 * Return 1 for any error that should generate a RAID path retry.
2143 * Return 0 for errors that don't require a RAID path retry.
2144 */
2145static int handle_ioaccel_mode2_error(struct ctlr_info *h,
Scott Teelc3497752014-02-18 13:56:34 -06002146 struct CommandList *c,
2147 struct scsi_cmnd *cmd,
2148 struct io_accel2_cmd *c2)
2149{
2150 int data_len;
Scott Teela09c1442014-02-18 13:57:21 -06002151 int retry = 0;
Joe Handzikc40820d2015-04-23 09:33:32 -05002152 u32 ioaccel2_resid = 0;
Scott Teelc3497752014-02-18 13:56:34 -06002153
2154 switch (c2->error_data.serv_response) {
2155 case IOACCEL2_SERV_RESPONSE_COMPLETE:
2156 switch (c2->error_data.status) {
2157 case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
2158 break;
2159 case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002160 cmd->result |= SAM_STAT_CHECK_CONDITION;
Scott Teelc3497752014-02-18 13:56:34 -06002161 if (c2->error_data.data_present !=
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002162 IOACCEL2_SENSE_DATA_PRESENT) {
2163 memset(cmd->sense_buffer, 0,
2164 SCSI_SENSE_BUFFERSIZE);
Scott Teelc3497752014-02-18 13:56:34 -06002165 break;
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002166 }
Scott Teelc3497752014-02-18 13:56:34 -06002167 /* copy the sense data */
2168 data_len = c2->error_data.sense_data_len;
2169 if (data_len > SCSI_SENSE_BUFFERSIZE)
2170 data_len = SCSI_SENSE_BUFFERSIZE;
2171 if (data_len > sizeof(c2->error_data.sense_data_buff))
2172 data_len =
2173 sizeof(c2->error_data.sense_data_buff);
2174 memcpy(cmd->sense_buffer,
2175 c2->error_data.sense_data_buff, data_len);
Scott Teela09c1442014-02-18 13:57:21 -06002176 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002177 break;
2178 case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
Scott Teela09c1442014-02-18 13:57:21 -06002179 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002180 break;
2181 case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
Scott Teela09c1442014-02-18 13:57:21 -06002182 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002183 break;
2184 case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
Stephen Cameron4a8da222015-04-23 09:32:43 -05002185 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002186 break;
2187 case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
Scott Teela09c1442014-02-18 13:57:21 -06002188 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002189 break;
2190 default:
Scott Teela09c1442014-02-18 13:57:21 -06002191 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002192 break;
2193 }
2194 break;
2195 case IOACCEL2_SERV_RESPONSE_FAILURE:
Joe Handzikc40820d2015-04-23 09:33:32 -05002196 switch (c2->error_data.status) {
2197 case IOACCEL2_STATUS_SR_IO_ERROR:
2198 case IOACCEL2_STATUS_SR_IO_ABORTED:
2199 case IOACCEL2_STATUS_SR_OVERRUN:
2200 retry = 1;
2201 break;
2202 case IOACCEL2_STATUS_SR_UNDERRUN:
2203 cmd->result = (DID_OK << 16); /* host byte */
2204 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
2205 ioaccel2_resid = get_unaligned_le32(
2206 &c2->error_data.resid_cnt[0]);
2207 scsi_set_resid(cmd, ioaccel2_resid);
2208 break;
2209 case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
2210 case IOACCEL2_STATUS_SR_INVALID_DEVICE:
2211 case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
2212 /* We will get an event from ctlr to trigger rescan */
2213 retry = 1;
2214 break;
2215 default:
2216 retry = 1;
Joe Handzikc40820d2015-04-23 09:33:32 -05002217 }
Scott Teelc3497752014-02-18 13:56:34 -06002218 break;
2219 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
2220 break;
2221 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
2222 break;
2223 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
Scott Teela09c1442014-02-18 13:57:21 -06002224 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002225 break;
2226 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
Scott Teelc3497752014-02-18 13:56:34 -06002227 break;
2228 default:
Scott Teela09c1442014-02-18 13:57:21 -06002229 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002230 break;
2231 }
Scott Teela09c1442014-02-18 13:57:21 -06002232
2233 return retry; /* retry on raid path? */
Scott Teelc3497752014-02-18 13:56:34 -06002234}
2235
Webb Scalesa58e7e52015-04-23 09:34:16 -05002236static void hpsa_cmd_resolve_events(struct ctlr_info *h,
2237 struct CommandList *c)
2238{
Webb Scalesd604f532015-04-23 09:35:22 -05002239 bool do_wake = false;
2240
Webb Scalesa58e7e52015-04-23 09:34:16 -05002241 /*
2242 * Prevent the following race in the abort handler:
2243 *
2244 * 1. LLD is requested to abort a SCSI command
2245 * 2. The SCSI command completes
2246 * 3. The struct CommandList associated with step 2 is made available
2247 * 4. New I/O request to LLD to another LUN re-uses struct CommandList
2248 * 5. Abort handler follows scsi_cmnd->host_scribble and
2249 * finds struct CommandList and tries to aborts it
2250 * Now we have aborted the wrong command.
2251 *
Webb Scalesd604f532015-04-23 09:35:22 -05002252 * Reset c->scsi_cmd here so that the abort or reset handler will know
2253 * this command has completed. Then, check to see if the handler is
Webb Scalesa58e7e52015-04-23 09:34:16 -05002254 * waiting for this command, and, if so, wake it.
2255 */
2256 c->scsi_cmd = SCSI_CMD_IDLE;
Webb Scalesd604f532015-04-23 09:35:22 -05002257 mb(); /* Declare command idle before checking for pending events. */
Webb Scalesa58e7e52015-04-23 09:34:16 -05002258 if (c->abort_pending) {
Webb Scalesd604f532015-04-23 09:35:22 -05002259 do_wake = true;
Webb Scalesa58e7e52015-04-23 09:34:16 -05002260 c->abort_pending = false;
Webb Scalesa58e7e52015-04-23 09:34:16 -05002261 }
Webb Scalesd604f532015-04-23 09:35:22 -05002262 if (c->reset_pending) {
2263 unsigned long flags;
2264 struct hpsa_scsi_dev_t *dev;
2265
2266 /*
2267 * There appears to be a reset pending; lock the lock and
2268 * reconfirm. If so, then decrement the count of outstanding
2269 * commands and wake the reset command if this is the last one.
2270 */
2271 spin_lock_irqsave(&h->lock, flags);
2272 dev = c->reset_pending; /* Re-fetch under the lock. */
2273 if (dev && atomic_dec_and_test(&dev->reset_cmds_out))
2274 do_wake = true;
2275 c->reset_pending = NULL;
2276 spin_unlock_irqrestore(&h->lock, flags);
2277 }
2278
2279 if (do_wake)
2280 wake_up_all(&h->event_sync_wait_queue);
Webb Scalesa58e7e52015-04-23 09:34:16 -05002281}
2282
Webb Scales73153fe2015-04-23 09:35:04 -05002283static void hpsa_cmd_resolve_and_free(struct ctlr_info *h,
2284 struct CommandList *c)
2285{
2286 hpsa_cmd_resolve_events(h, c);
2287 cmd_tagged_free(h, c);
2288}
2289
Webb Scales8a0ff922015-04-23 09:34:11 -05002290static void hpsa_cmd_free_and_done(struct ctlr_info *h,
2291 struct CommandList *c, struct scsi_cmnd *cmd)
2292{
Webb Scales73153fe2015-04-23 09:35:04 -05002293 hpsa_cmd_resolve_and_free(h, c);
Webb Scales8a0ff922015-04-23 09:34:11 -05002294 cmd->scsi_done(cmd);
2295}
2296
2297static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
2298{
2299 INIT_WORK(&c->work, hpsa_command_resubmit_worker);
2300 queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
2301}
2302
Webb Scalesa58e7e52015-04-23 09:34:16 -05002303static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
2304{
2305 cmd->result = DID_ABORT << 16;
2306}
2307
2308static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
2309 struct scsi_cmnd *cmd)
2310{
2311 hpsa_set_scsi_cmd_aborted(cmd);
2312 dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2313 c->Request.CDB, c->err_info->ScsiStatus);
Webb Scales73153fe2015-04-23 09:35:04 -05002314 hpsa_cmd_resolve_and_free(h, c);
Webb Scalesa58e7e52015-04-23 09:34:16 -05002315}
2316
Scott Teelc3497752014-02-18 13:56:34 -06002317static void process_ioaccel2_completion(struct ctlr_info *h,
2318 struct CommandList *c, struct scsi_cmnd *cmd,
2319 struct hpsa_scsi_dev_t *dev)
2320{
2321 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2322
2323 /* check for good status */
2324 if (likely(c2->error_data.serv_response == 0 &&
Webb Scales8a0ff922015-04-23 09:34:11 -05002325 c2->error_data.status == 0))
2326 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002327
Webb Scales8a0ff922015-04-23 09:34:11 -05002328 /*
2329 * Any RAID offload error results in retry which will use
Scott Teelc3497752014-02-18 13:56:34 -06002330 * the normal I/O path so the controller can handle whatever's
2331 * wrong.
2332 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002333 if (is_logical_device(dev) &&
Scott Teelc3497752014-02-18 13:56:34 -06002334 c2->error_data.serv_response ==
2335 IOACCEL2_SERV_RESPONSE_FAILURE) {
Don Brace080ef1c2015-01-23 16:43:25 -06002336 if (c2->error_data.status ==
2337 IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
2338 dev->offload_enabled = 0;
Webb Scales8a0ff922015-04-23 09:34:11 -05002339
2340 return hpsa_retry_cmd(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06002341 }
Don Brace080ef1c2015-01-23 16:43:25 -06002342
2343 if (handle_ioaccel_mode2_error(h, c, cmd, c2))
Webb Scales8a0ff922015-04-23 09:34:11 -05002344 return hpsa_retry_cmd(h, c);
Don Brace080ef1c2015-01-23 16:43:25 -06002345
Webb Scales8a0ff922015-04-23 09:34:11 -05002346 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002347}
2348
Stephen Cameron9437ac42015-04-23 09:32:16 -05002349/* Returns 0 on success, < 0 otherwise. */
2350static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
2351 struct CommandList *cp)
2352{
2353 u8 tmf_status = cp->err_info->ScsiStatus;
2354
2355 switch (tmf_status) {
2356 case CISS_TMF_COMPLETE:
2357 /*
2358 * CISS_TMF_COMPLETE never happens, instead,
2359 * ei->CommandStatus == 0 for this case.
2360 */
2361 case CISS_TMF_SUCCESS:
2362 return 0;
2363 case CISS_TMF_INVALID_FRAME:
2364 case CISS_TMF_NOT_SUPPORTED:
2365 case CISS_TMF_FAILED:
2366 case CISS_TMF_WRONG_LUN:
2367 case CISS_TMF_OVERLAPPED_TAG:
2368 break;
2369 default:
2370 dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
2371 tmf_status);
2372 break;
2373 }
2374 return -tmf_status;
2375}
2376
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05002377static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002378{
2379 struct scsi_cmnd *cmd;
2380 struct ctlr_info *h;
2381 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002382 struct hpsa_scsi_dev_t *dev;
Webb Scalesd9a729f2015-04-23 09:33:27 -05002383 struct io_accel2_cmd *c2;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002384
Stephen Cameron9437ac42015-04-23 09:32:16 -05002385 u8 sense_key;
2386 u8 asc; /* additional sense code */
2387 u8 ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05002388 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002389
2390 ei = cp->err_info;
Stephen Cameron7fa30302015-01-23 16:44:30 -06002391 cmd = cp->scsi_cmd;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002392 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002393 dev = cmd->device->hostdata;
Webb Scalesd9a729f2015-04-23 09:33:27 -05002394 c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002395
2396 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06002397 if ((cp->cmd_type == CMD_SCSI) &&
Don Brace2b08b3e2015-01-23 16:41:09 -06002398 (le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002399 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002400
Webb Scalesd9a729f2015-04-23 09:33:27 -05002401 if ((cp->cmd_type == CMD_IOACCEL2) &&
2402 (c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2403 hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2404
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002405 cmd->result = (DID_OK << 16); /* host byte */
2406 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06002407
Don Brace03383732015-01-23 16:43:30 -06002408 if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
2409 atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
2410
Webb Scales25163bd2015-04-23 09:32:00 -05002411 /*
2412 * We check for lockup status here as it may be set for
2413 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
2414 * fail_all_oustanding_cmds()
2415 */
2416 if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
2417 /* DID_NO_CONNECT will prevent a retry */
2418 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05002419 return hpsa_cmd_free_and_done(h, cp, cmd);
Webb Scales25163bd2015-04-23 09:32:00 -05002420 }
2421
Webb Scalesd604f532015-04-23 09:35:22 -05002422 if ((unlikely(hpsa_is_pending_event(cp)))) {
2423 if (cp->reset_pending)
2424 return hpsa_cmd_resolve_and_free(h, cp);
2425 if (cp->abort_pending)
2426 return hpsa_cmd_abort_and_free(h, cp, cmd);
2427 }
2428
Scott Teelc3497752014-02-18 13:56:34 -06002429 if (cp->cmd_type == CMD_IOACCEL2)
2430 return process_ioaccel2_completion(h, cp, cmd, dev);
2431
Robert Elliott6aa4c362014-07-03 10:18:19 -05002432 scsi_set_resid(cmd, ei->ResidualCnt);
Webb Scales8a0ff922015-04-23 09:34:11 -05002433 if (ei->CommandStatus == 0)
2434 return hpsa_cmd_free_and_done(h, cp, cmd);
Robert Elliott6aa4c362014-07-03 10:18:19 -05002435
Matt Gatese1f7de02014-02-18 13:55:17 -06002436 /* For I/O accelerator commands, copy over some fields to the normal
2437 * CISS header used below for error handling.
2438 */
2439 if (cp->cmd_type == CMD_IOACCEL1) {
2440 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06002441 cp->Header.SGList = scsi_sg_count(cmd);
2442 cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
2443 cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
2444 IOACCEL1_IOFLAGS_CDBLEN_MASK;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002445 cp->Header.tag = c->tag;
Matt Gatese1f7de02014-02-18 13:55:17 -06002446 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2447 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002448
2449 /* Any RAID offload error results in retry which will use
2450 * the normal I/O path so the controller can handle whatever's
2451 * wrong.
2452 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002453 if (is_logical_device(dev)) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002454 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2455 dev->offload_enabled = 0;
Webb Scalesd604f532015-04-23 09:35:22 -05002456 return hpsa_retry_cmd(h, cp);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002457 }
Matt Gatese1f7de02014-02-18 13:55:17 -06002458 }
2459
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002460 /* an error has occurred */
2461 switch (ei->CommandStatus) {
2462
2463 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002464 cmd->result |= ei->ScsiStatus;
2465 /* copy the sense data */
2466 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
2467 sense_data_size = SCSI_SENSE_BUFFERSIZE;
2468 else
2469 sense_data_size = sizeof(ei->SenseInfo);
2470 if (ei->SenseLen < sense_data_size)
2471 sense_data_size = ei->SenseLen;
2472 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
2473 if (ei->ScsiStatus)
2474 decode_sense_data(ei->SenseInfo, sense_data_size,
2475 &sense_key, &asc, &ascq);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002476 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates1d3b3602010-02-04 08:43:00 -06002477 if (sense_key == ABORTED_COMMAND) {
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05002478 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06002479 break;
2480 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002481 break;
2482 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002483 /* Problem was not a check condition
2484 * Pass it up to the upper layers...
2485 */
2486 if (ei->ScsiStatus) {
2487 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2488 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2489 "Returning result: 0x%x\n",
2490 cp, ei->ScsiStatus,
2491 sense_key, asc, ascq,
2492 cmd->result);
2493 } else { /* scsi status is zero??? How??? */
2494 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2495 "Returning no connection.\n", cp),
2496
2497 /* Ordinarily, this case should never happen,
2498 * but there is a bug in some released firmware
2499 * revisions that allows it to happen if, for
2500 * example, a 4100 backplane loses power and
2501 * the tape drive is in it. We assume that
2502 * it's a fatal error of some kind because we
2503 * can't show that it wasn't. We will make it
2504 * look like selection timeout since that is
2505 * the most common reason for this to occur,
2506 * and it's severe enough.
2507 */
2508
2509 cmd->result = DID_NO_CONNECT << 16;
2510 }
2511 break;
2512
2513 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2514 break;
2515 case CMD_DATA_OVERRUN:
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002516 dev_warn(&h->pdev->dev,
2517 "CDB %16phN data overrun\n", cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002518 break;
2519 case CMD_INVALID: {
2520 /* print_bytes(cp, sizeof(*cp), 1, 0);
2521 print_cmd(cp); */
2522 /* We get CMD_INVALID if you address a non-existent device
2523 * instead of a selection timeout (no response). You will
2524 * see this if you yank out a drive, then try to access it.
2525 * This is kind of a shame because it means that any other
2526 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2527 * missing target. */
2528 cmd->result = DID_NO_CONNECT << 16;
2529 }
2530 break;
2531 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05002532 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002533 dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2534 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002535 break;
2536 case CMD_HARDWARE_ERR:
2537 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002538 dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2539 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002540 break;
2541 case CMD_CONNECTION_LOST:
2542 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002543 dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2544 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002545 break;
2546 case CMD_ABORTED:
Webb Scalesa58e7e52015-04-23 09:34:16 -05002547 /* Return now to avoid calling scsi_done(). */
2548 return hpsa_cmd_abort_and_free(h, cp, cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002549 case CMD_ABORT_FAILED:
2550 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002551 dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2552 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002553 break;
2554 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05002555 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002556 dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2557 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002558 break;
2559 case CMD_TIMEOUT:
2560 cmd->result = DID_TIME_OUT << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002561 dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2562 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002563 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002564 case CMD_UNABORTABLE:
2565 cmd->result = DID_ERROR << 16;
2566 dev_warn(&h->pdev->dev, "Command unabortable\n");
2567 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002568 case CMD_TMF_STATUS:
2569 if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
2570 cmd->result = DID_ERROR << 16;
2571 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002572 case CMD_IOACCEL_DISABLED:
2573 /* This only handles the direct pass-through case since RAID
2574 * offload is handled above. Just attempt a retry.
2575 */
2576 cmd->result = DID_SOFT_ERROR << 16;
2577 dev_warn(&h->pdev->dev,
2578 "cp %p had HP SSD Smart Path error\n", cp);
2579 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002580 default:
2581 cmd->result = DID_ERROR << 16;
2582 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2583 cp, ei->CommandStatus);
2584 }
Webb Scales8a0ff922015-04-23 09:34:11 -05002585
2586 return hpsa_cmd_free_and_done(h, cp, cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002587}
2588
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002589static void hpsa_pci_unmap(struct pci_dev *pdev,
2590 struct CommandList *c, int sg_used, int data_direction)
2591{
2592 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002593
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002594 for (i = 0; i < sg_used; i++)
2595 pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
2596 le32_to_cpu(c->SG[i].Len),
2597 data_direction);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002598}
2599
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002600static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002601 struct CommandList *cp,
2602 unsigned char *buf,
2603 size_t buflen,
2604 int data_direction)
2605{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002606 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002607
2608 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2609 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002610 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002611 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002612 }
2613
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002614 addr64 = pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06002615 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002616 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06002617 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002618 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002619 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06002620 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002621 cp->SG[0].Addr = cpu_to_le64(addr64);
2622 cp->SG[0].Len = cpu_to_le32(buflen);
2623 cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
2624 cp->Header.SGList = 1; /* no. SGs contig in this cmd */
2625 cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002626 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002627}
2628
Webb Scales25163bd2015-04-23 09:32:00 -05002629#define NO_TIMEOUT ((unsigned long) -1)
2630#define DEFAULT_TIMEOUT 30000 /* milliseconds */
2631static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
2632 struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002633{
2634 DECLARE_COMPLETION_ONSTACK(wait);
2635
2636 c->waiting = &wait;
Webb Scales25163bd2015-04-23 09:32:00 -05002637 __enqueue_cmd_and_start_io(h, c, reply_queue);
2638 if (timeout_msecs == NO_TIMEOUT) {
2639 /* TODO: get rid of this no-timeout thing */
2640 wait_for_completion_io(&wait);
2641 return IO_OK;
2642 }
2643 if (!wait_for_completion_io_timeout(&wait,
2644 msecs_to_jiffies(timeout_msecs))) {
2645 dev_warn(&h->pdev->dev, "Command timed out.\n");
2646 return -ETIMEDOUT;
2647 }
2648 return IO_OK;
2649}
2650
2651static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
2652 int reply_queue, unsigned long timeout_msecs)
2653{
2654 if (unlikely(lockup_detected(h))) {
2655 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
2656 return IO_OK;
2657 }
2658 return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002659}
2660
Stephen M. Cameron094963d2014-05-29 10:53:18 -05002661static u32 lockup_detected(struct ctlr_info *h)
2662{
2663 int cpu;
2664 u32 rc, *lockup_detected;
2665
2666 cpu = get_cpu();
2667 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2668 rc = *lockup_detected;
2669 put_cpu();
2670 return rc;
2671}
2672
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002673#define MAX_DRIVER_CMD_RETRIES 25
Webb Scales25163bd2015-04-23 09:32:00 -05002674static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
2675 struct CommandList *c, int data_direction, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002676{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002677 int backoff_time = 10, retry_count = 0;
Webb Scales25163bd2015-04-23 09:32:00 -05002678 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002679
2680 do {
Joe Perches7630abd2011-05-08 23:32:40 -07002681 memset(c->err_info, 0, sizeof(*c->err_info));
Webb Scales25163bd2015-04-23 09:32:00 -05002682 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
2683 timeout_msecs);
2684 if (rc)
2685 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002686 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002687 if (retry_count > 3) {
2688 msleep(backoff_time);
2689 if (backoff_time < 1000)
2690 backoff_time *= 2;
2691 }
Matt Bondurant852af202012-05-01 11:42:35 -05002692 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002693 check_for_busy(h, c)) &&
2694 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002695 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
Webb Scales25163bd2015-04-23 09:32:00 -05002696 if (retry_count > MAX_DRIVER_CMD_RETRIES)
2697 rc = -EIO;
2698 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002699}
2700
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002701static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2702 struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002703{
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002704 const u8 *cdb = c->Request.CDB;
2705 const u8 *lun = c->Header.LUN.LunAddrBytes;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002706
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002707 dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2708 " CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2709 txt, lun[0], lun[1], lun[2], lun[3],
2710 lun[4], lun[5], lun[6], lun[7],
2711 cdb[0], cdb[1], cdb[2], cdb[3],
2712 cdb[4], cdb[5], cdb[6], cdb[7],
2713 cdb[8], cdb[9], cdb[10], cdb[11],
2714 cdb[12], cdb[13], cdb[14], cdb[15]);
2715}
2716
2717static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2718 struct CommandList *cp)
2719{
2720 const struct ErrorInfo *ei = cp->err_info;
2721 struct device *d = &cp->h->pdev->dev;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002722 u8 sense_key, asc, ascq;
2723 int sense_len;
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002724
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002725 switch (ei->CommandStatus) {
2726 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002727 if (ei->SenseLen > sizeof(ei->SenseInfo))
2728 sense_len = sizeof(ei->SenseInfo);
2729 else
2730 sense_len = ei->SenseLen;
2731 decode_sense_data(ei->SenseInfo, sense_len,
2732 &sense_key, &asc, &ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002733 hpsa_print_cmd(h, "SCSI status", cp);
2734 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
Stephen Cameron9437ac42015-04-23 09:32:16 -05002735 dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
2736 sense_key, asc, ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002737 else
Stephen Cameron9437ac42015-04-23 09:32:16 -05002738 dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002739 if (ei->ScsiStatus == 0)
2740 dev_warn(d, "SCSI status is abnormally zero. "
2741 "(probably indicates selection timeout "
2742 "reported incorrectly due to a known "
2743 "firmware bug, circa July, 2001.)\n");
2744 break;
2745 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002746 break;
2747 case CMD_DATA_OVERRUN:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002748 hpsa_print_cmd(h, "overrun condition", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002749 break;
2750 case CMD_INVALID: {
2751 /* controller unfortunately reports SCSI passthru's
2752 * to non-existent targets as invalid commands.
2753 */
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002754 hpsa_print_cmd(h, "invalid command", cp);
2755 dev_warn(d, "probably means device no longer present\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002756 }
2757 break;
2758 case CMD_PROTOCOL_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002759 hpsa_print_cmd(h, "protocol error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002760 break;
2761 case CMD_HARDWARE_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002762 hpsa_print_cmd(h, "hardware error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002763 break;
2764 case CMD_CONNECTION_LOST:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002765 hpsa_print_cmd(h, "connection lost", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002766 break;
2767 case CMD_ABORTED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002768 hpsa_print_cmd(h, "aborted", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002769 break;
2770 case CMD_ABORT_FAILED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002771 hpsa_print_cmd(h, "abort failed", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002772 break;
2773 case CMD_UNSOLICITED_ABORT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002774 hpsa_print_cmd(h, "unsolicited abort", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002775 break;
2776 case CMD_TIMEOUT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002777 hpsa_print_cmd(h, "timed out", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002778 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002779 case CMD_UNABORTABLE:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002780 hpsa_print_cmd(h, "unabortable", cp);
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002781 break;
Webb Scales25163bd2015-04-23 09:32:00 -05002782 case CMD_CTLR_LOCKUP:
2783 hpsa_print_cmd(h, "controller lockup detected", cp);
2784 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002785 default:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002786 hpsa_print_cmd(h, "unknown status", cp);
2787 dev_warn(d, "Unknown command status %x\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002788 ei->CommandStatus);
2789 }
2790}
2791
2792static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002793 u16 page, unsigned char *buf,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002794 unsigned char bufsize)
2795{
2796 int rc = IO_OK;
2797 struct CommandList *c;
2798 struct ErrorInfo *ei;
2799
Stephen Cameron45fcb862015-01-23 16:43:04 -06002800 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002801
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002802 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2803 page, scsi3addr, TYPE_CMD)) {
2804 rc = -1;
2805 goto out;
2806 }
Webb Scales25163bd2015-04-23 09:32:00 -05002807 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
2808 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
2809 if (rc)
2810 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002811 ei = c->err_info;
2812 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002813 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002814 rc = -1;
2815 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002816out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002817 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002818 return rc;
2819}
2820
Scott Teelbf711ac2014-02-18 13:56:39 -06002821static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Webb Scales25163bd2015-04-23 09:32:00 -05002822 u8 reset_type, int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002823{
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. Cameronedd16362009-12-08 14:09:11 -08002830
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002831 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teel0b9b7b62015-11-04 15:51:02 -06002832 (void) fill_cmd(c, reset_type, h, NULL, 0, 0,
Scott Teelbf711ac2014-02-18 13:56:39 -06002833 scsi3addr, TYPE_MSG);
Webb Scales25163bd2015-04-23 09:32:00 -05002834 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
2835 if (rc) {
2836 dev_warn(&h->pdev->dev, "Failed to send reset command\n");
2837 goto out;
2838 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002839 /* no unmap needed here because no data xfer. */
2840
2841 ei = c->err_info;
2842 if (ei->CommandStatus != 0) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002843 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002844 rc = -1;
2845 }
Webb Scales25163bd2015-04-23 09:32:00 -05002846out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002847 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002848 return rc;
2849}
2850
Webb Scalesd604f532015-04-23 09:35:22 -05002851static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
2852 struct hpsa_scsi_dev_t *dev,
2853 unsigned char *scsi3addr)
2854{
2855 int i;
2856 bool match = false;
2857 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2858 struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
2859
2860 if (hpsa_is_cmd_idle(c))
2861 return false;
2862
2863 switch (c->cmd_type) {
2864 case CMD_SCSI:
2865 case CMD_IOCTL_PEND:
2866 match = !memcmp(scsi3addr, &c->Header.LUN.LunAddrBytes,
2867 sizeof(c->Header.LUN.LunAddrBytes));
2868 break;
2869
2870 case CMD_IOACCEL1:
2871 case CMD_IOACCEL2:
2872 if (c->phys_disk == dev) {
2873 /* HBA mode match */
2874 match = true;
2875 } else {
2876 /* Possible RAID mode -- check each phys dev. */
2877 /* FIXME: Do we need to take out a lock here? If
2878 * so, we could just call hpsa_get_pdisk_of_ioaccel2()
2879 * instead. */
2880 for (i = 0; i < dev->nphysical_disks && !match; i++) {
2881 /* FIXME: an alternate test might be
2882 *
2883 * match = dev->phys_disk[i]->ioaccel_handle
2884 * == c2->scsi_nexus; */
2885 match = dev->phys_disk[i] == c->phys_disk;
2886 }
2887 }
2888 break;
2889
2890 case IOACCEL2_TMF:
2891 for (i = 0; i < dev->nphysical_disks && !match; i++) {
2892 match = dev->phys_disk[i]->ioaccel_handle ==
2893 le32_to_cpu(ac->it_nexus);
2894 }
2895 break;
2896
2897 case 0: /* The command is in the middle of being initialized. */
2898 match = false;
2899 break;
2900
2901 default:
2902 dev_err(&h->pdev->dev, "unexpected cmd_type: %d\n",
2903 c->cmd_type);
2904 BUG();
2905 }
2906
2907 return match;
2908}
2909
2910static int hpsa_do_reset(struct ctlr_info *h, struct hpsa_scsi_dev_t *dev,
2911 unsigned char *scsi3addr, u8 reset_type, int reply_queue)
2912{
2913 int i;
2914 int rc = 0;
2915
2916 /* We can really only handle one reset at a time */
2917 if (mutex_lock_interruptible(&h->reset_mutex) == -EINTR) {
2918 dev_warn(&h->pdev->dev, "concurrent reset wait interrupted.\n");
2919 return -EINTR;
2920 }
2921
2922 BUG_ON(atomic_read(&dev->reset_cmds_out) != 0);
2923
2924 for (i = 0; i < h->nr_cmds; i++) {
2925 struct CommandList *c = h->cmd_pool + i;
2926 int refcount = atomic_inc_return(&c->refcount);
2927
2928 if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev, scsi3addr)) {
2929 unsigned long flags;
2930
2931 /*
2932 * Mark the target command as having a reset pending,
2933 * then lock a lock so that the command cannot complete
2934 * while we're considering it. If the command is not
2935 * idle then count it; otherwise revoke the event.
2936 */
2937 c->reset_pending = dev;
2938 spin_lock_irqsave(&h->lock, flags); /* Implied MB */
2939 if (!hpsa_is_cmd_idle(c))
2940 atomic_inc(&dev->reset_cmds_out);
2941 else
2942 c->reset_pending = NULL;
2943 spin_unlock_irqrestore(&h->lock, flags);
2944 }
2945
2946 cmd_free(h, c);
2947 }
2948
2949 rc = hpsa_send_reset(h, scsi3addr, reset_type, reply_queue);
2950 if (!rc)
2951 wait_event(h->event_sync_wait_queue,
2952 atomic_read(&dev->reset_cmds_out) == 0 ||
2953 lockup_detected(h));
2954
2955 if (unlikely(lockup_detected(h))) {
Don Brace77678d32015-07-18 11:12:22 -05002956 dev_warn(&h->pdev->dev,
2957 "Controller lockup detected during reset wait\n");
2958 rc = -ENODEV;
2959 }
Webb Scalesd604f532015-04-23 09:35:22 -05002960
2961 if (unlikely(rc))
2962 atomic_set(&dev->reset_cmds_out, 0);
2963
2964 mutex_unlock(&h->reset_mutex);
2965 return rc;
2966}
2967
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002968static void hpsa_get_raid_level(struct ctlr_info *h,
2969 unsigned char *scsi3addr, unsigned char *raid_level)
2970{
2971 int rc;
2972 unsigned char *buf;
2973
2974 *raid_level = RAID_UNKNOWN;
2975 buf = kzalloc(64, GFP_KERNEL);
2976 if (!buf)
2977 return;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002978 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002979 if (rc == 0)
2980 *raid_level = buf[8];
2981 if (*raid_level > RAID_UNKNOWN)
2982 *raid_level = RAID_UNKNOWN;
2983 kfree(buf);
2984 return;
2985}
2986
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002987#define HPSA_MAP_DEBUG
2988#ifdef HPSA_MAP_DEBUG
2989static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2990 struct raid_map_data *map_buff)
2991{
2992 struct raid_map_disk_data *dd = &map_buff->data[0];
2993 int map, row, col;
2994 u16 map_cnt, row_cnt, disks_per_row;
2995
2996 if (rc != 0)
2997 return;
2998
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06002999 /* Show details only if debugging has been activated. */
3000 if (h->raid_offload_debug < 2)
3001 return;
3002
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003003 dev_info(&h->pdev->dev, "structure_size = %u\n",
3004 le32_to_cpu(map_buff->structure_size));
3005 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
3006 le32_to_cpu(map_buff->volume_blk_size));
3007 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
3008 le64_to_cpu(map_buff->volume_blk_cnt));
3009 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
3010 map_buff->phys_blk_shift);
3011 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
3012 map_buff->parity_rotation_shift);
3013 dev_info(&h->pdev->dev, "strip_size = %u\n",
3014 le16_to_cpu(map_buff->strip_size));
3015 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
3016 le64_to_cpu(map_buff->disk_starting_blk));
3017 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
3018 le64_to_cpu(map_buff->disk_blk_cnt));
3019 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
3020 le16_to_cpu(map_buff->data_disks_per_row));
3021 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
3022 le16_to_cpu(map_buff->metadata_disks_per_row));
3023 dev_info(&h->pdev->dev, "row_cnt = %u\n",
3024 le16_to_cpu(map_buff->row_cnt));
3025 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
3026 le16_to_cpu(map_buff->layout_map_count));
Don Brace2b08b3e2015-01-23 16:41:09 -06003027 dev_info(&h->pdev->dev, "flags = 0x%x\n",
Scott Teeldd0e19f2014-02-18 13:57:31 -06003028 le16_to_cpu(map_buff->flags));
Don Brace2b08b3e2015-01-23 16:41:09 -06003029 dev_info(&h->pdev->dev, "encrypytion = %s\n",
3030 le16_to_cpu(map_buff->flags) &
3031 RAID_MAP_FLAG_ENCRYPT_ON ? "ON" : "OFF");
Scott Teeldd0e19f2014-02-18 13:57:31 -06003032 dev_info(&h->pdev->dev, "dekindex = %u\n",
3033 le16_to_cpu(map_buff->dekindex));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003034 map_cnt = le16_to_cpu(map_buff->layout_map_count);
3035 for (map = 0; map < map_cnt; map++) {
3036 dev_info(&h->pdev->dev, "Map%u:\n", map);
3037 row_cnt = le16_to_cpu(map_buff->row_cnt);
3038 for (row = 0; row < row_cnt; row++) {
3039 dev_info(&h->pdev->dev, " Row%u:\n", row);
3040 disks_per_row =
3041 le16_to_cpu(map_buff->data_disks_per_row);
3042 for (col = 0; col < disks_per_row; col++, dd++)
3043 dev_info(&h->pdev->dev,
3044 " D%02u: h=0x%04x xor=%u,%u\n",
3045 col, dd->ioaccel_handle,
3046 dd->xor_mult[0], dd->xor_mult[1]);
3047 disks_per_row =
3048 le16_to_cpu(map_buff->metadata_disks_per_row);
3049 for (col = 0; col < disks_per_row; col++, dd++)
3050 dev_info(&h->pdev->dev,
3051 " M%02u: h=0x%04x xor=%u,%u\n",
3052 col, dd->ioaccel_handle,
3053 dd->xor_mult[0], dd->xor_mult[1]);
3054 }
3055 }
3056}
3057#else
3058static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
3059 __attribute__((unused)) int rc,
3060 __attribute__((unused)) struct raid_map_data *map_buff)
3061{
3062}
3063#endif
3064
3065static int hpsa_get_raid_map(struct ctlr_info *h,
3066 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3067{
3068 int rc = 0;
3069 struct CommandList *c;
3070 struct ErrorInfo *ei;
3071
Stephen Cameron45fcb862015-01-23 16:43:04 -06003072 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003073
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003074 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
3075 sizeof(this_device->raid_map), 0,
3076 scsi3addr, TYPE_CMD)) {
Robert Elliott2dd02d72015-04-23 09:33:43 -05003077 dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
3078 cmd_free(h, c);
3079 return -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003080 }
Webb Scales25163bd2015-04-23 09:32:00 -05003081 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
3082 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
3083 if (rc)
3084 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003085 ei = c->err_info;
3086 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003087 hpsa_scsi_interpret_error(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05003088 rc = -1;
3089 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003090 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06003091 cmd_free(h, c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003092
3093 /* @todo in the future, dynamically allocate RAID map memory */
3094 if (le32_to_cpu(this_device->raid_map.structure_size) >
3095 sizeof(this_device->raid_map)) {
3096 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
3097 rc = -1;
3098 }
3099 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
3100 return rc;
Webb Scales25163bd2015-04-23 09:32:00 -05003101out:
3102 cmd_free(h, c);
3103 return rc;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003104}
3105
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003106static int hpsa_bmic_sense_subsystem_information(struct ctlr_info *h,
3107 unsigned char scsi3addr[], u16 bmic_device_index,
3108 struct bmic_sense_subsystem_info *buf, size_t bufsize)
3109{
3110 int rc = IO_OK;
3111 struct CommandList *c;
3112 struct ErrorInfo *ei;
3113
3114 c = cmd_alloc(h);
3115
3116 rc = fill_cmd(c, BMIC_SENSE_SUBSYSTEM_INFORMATION, h, buf, bufsize,
3117 0, RAID_CTLR_LUNID, TYPE_CMD);
3118 if (rc)
3119 goto out;
3120
3121 c->Request.CDB[2] = bmic_device_index & 0xff;
3122 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
3123
3124 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
3125 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
3126 if (rc)
3127 goto out;
3128 ei = c->err_info;
3129 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3130 hpsa_scsi_interpret_error(h, c);
3131 rc = -1;
3132 }
3133out:
3134 cmd_free(h, c);
3135 return rc;
3136}
3137
Scott Teel66749d02015-11-04 15:51:57 -06003138static int hpsa_bmic_id_controller(struct ctlr_info *h,
3139 struct bmic_identify_controller *buf, size_t bufsize)
3140{
3141 int rc = IO_OK;
3142 struct CommandList *c;
3143 struct ErrorInfo *ei;
3144
3145 c = cmd_alloc(h);
3146
3147 rc = fill_cmd(c, BMIC_IDENTIFY_CONTROLLER, h, buf, bufsize,
3148 0, RAID_CTLR_LUNID, TYPE_CMD);
3149 if (rc)
3150 goto out;
3151
3152 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
3153 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
3154 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
Don Brace03383732015-01-23 16:43:30 -06003166static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
3167 unsigned char scsi3addr[], u16 bmic_device_index,
3168 struct bmic_identify_physical_device *buf, size_t bufsize)
3169{
3170 int rc = IO_OK;
3171 struct CommandList *c;
3172 struct ErrorInfo *ei;
3173
3174 c = cmd_alloc(h);
3175 rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
3176 0, RAID_CTLR_LUNID, TYPE_CMD);
3177 if (rc)
3178 goto out;
3179
3180 c->Request.CDB[2] = bmic_device_index & 0xff;
3181 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
3182
Webb Scales25163bd2015-04-23 09:32:00 -05003183 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
3184 NO_TIMEOUT);
Don Brace03383732015-01-23 16:43:30 -06003185 ei = c->err_info;
3186 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3187 hpsa_scsi_interpret_error(h, c);
3188 rc = -1;
3189 }
3190out:
3191 cmd_free(h, c);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003192
Don Brace03383732015-01-23 16:43:30 -06003193 return rc;
3194}
3195
Don Bracecca8f132015-12-22 10:36:48 -06003196/*
3197 * get enclosure information
3198 * struct ReportExtendedLUNdata *rlep - Used for BMIC drive number
3199 * struct hpsa_scsi_dev_t *encl_dev - device entry for enclosure
3200 * Uses id_physical_device to determine the box_index.
3201 */
3202static void hpsa_get_enclosure_info(struct ctlr_info *h,
3203 unsigned char *scsi3addr,
3204 struct ReportExtendedLUNdata *rlep, int rle_index,
3205 struct hpsa_scsi_dev_t *encl_dev)
3206{
3207 int rc = -1;
3208 struct CommandList *c = NULL;
3209 struct ErrorInfo *ei = NULL;
3210 struct bmic_sense_storage_box_params *bssbp = NULL;
3211 struct bmic_identify_physical_device *id_phys = NULL;
3212 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
3213 u16 bmic_device_index = 0;
3214
3215 bmic_device_index = GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]);
3216
Don Brace17a9e542016-02-23 15:16:09 -06003217 if (bmic_device_index == 0xFF00 || MASKED_DEVICE(&rle->lunid[0])) {
3218 rc = IO_OK;
Don Bracecca8f132015-12-22 10:36:48 -06003219 goto out;
Don Brace17a9e542016-02-23 15:16:09 -06003220 }
Don Bracecca8f132015-12-22 10:36:48 -06003221
3222 bssbp = kzalloc(sizeof(*bssbp), GFP_KERNEL);
3223 if (!bssbp)
3224 goto out;
3225
3226 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3227 if (!id_phys)
3228 goto out;
3229
3230 rc = hpsa_bmic_id_physical_device(h, scsi3addr, bmic_device_index,
3231 id_phys, sizeof(*id_phys));
3232 if (rc) {
3233 dev_warn(&h->pdev->dev, "%s: id_phys failed %d bdi[0x%x]\n",
3234 __func__, encl_dev->external, bmic_device_index);
3235 goto out;
3236 }
3237
3238 c = cmd_alloc(h);
3239
3240 rc = fill_cmd(c, BMIC_SENSE_STORAGE_BOX_PARAMS, h, bssbp,
3241 sizeof(*bssbp), 0, RAID_CTLR_LUNID, TYPE_CMD);
3242
3243 if (rc)
3244 goto out;
3245
3246 if (id_phys->phys_connector[1] == 'E')
3247 c->Request.CDB[5] = id_phys->box_index;
3248 else
3249 c->Request.CDB[5] = 0;
3250
3251 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
3252 NO_TIMEOUT);
3253 if (rc)
3254 goto out;
3255
3256 ei = c->err_info;
3257 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3258 rc = -1;
3259 goto out;
3260 }
3261
3262 encl_dev->box[id_phys->active_path_number] = bssbp->phys_box_on_port;
3263 memcpy(&encl_dev->phys_connector[id_phys->active_path_number],
3264 bssbp->phys_connector, sizeof(bssbp->phys_connector));
3265
3266 rc = IO_OK;
3267out:
3268 kfree(bssbp);
3269 kfree(id_phys);
3270
3271 if (c)
3272 cmd_free(h, c);
3273
3274 if (rc != IO_OK)
3275 hpsa_show_dev_msg(KERN_INFO, h, encl_dev,
3276 "Error, could not get enclosure information\n");
3277}
3278
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003279static u64 hpsa_get_sas_address_from_report_physical(struct ctlr_info *h,
3280 unsigned char *scsi3addr)
3281{
3282 struct ReportExtendedLUNdata *physdev;
3283 u32 nphysicals;
3284 u64 sa = 0;
3285 int i;
3286
3287 physdev = kzalloc(sizeof(*physdev), GFP_KERNEL);
3288 if (!physdev)
3289 return 0;
3290
3291 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3292 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3293 kfree(physdev);
3294 return 0;
3295 }
3296 nphysicals = get_unaligned_be32(physdev->LUNListLength) / 24;
3297
3298 for (i = 0; i < nphysicals; i++)
3299 if (!memcmp(&physdev->LUN[i].lunid[0], scsi3addr, 8)) {
3300 sa = get_unaligned_be64(&physdev->LUN[i].wwid[0]);
3301 break;
3302 }
3303
3304 kfree(physdev);
3305
3306 return sa;
3307}
3308
3309static void hpsa_get_sas_address(struct ctlr_info *h, unsigned char *scsi3addr,
3310 struct hpsa_scsi_dev_t *dev)
3311{
3312 int rc;
3313 u64 sa = 0;
3314
3315 if (is_hba_lunid(scsi3addr)) {
3316 struct bmic_sense_subsystem_info *ssi;
3317
3318 ssi = kzalloc(sizeof(*ssi), GFP_KERNEL);
3319 if (ssi == NULL) {
3320 dev_warn(&h->pdev->dev,
3321 "%s: out of memory\n", __func__);
3322 return;
3323 }
3324
3325 rc = hpsa_bmic_sense_subsystem_information(h,
3326 scsi3addr, 0, ssi, sizeof(*ssi));
3327 if (rc == 0) {
3328 sa = get_unaligned_be64(ssi->primary_world_wide_id);
3329 h->sas_address = sa;
3330 }
3331
3332 kfree(ssi);
3333 } else
3334 sa = hpsa_get_sas_address_from_report_physical(h, scsi3addr);
3335
3336 dev->sas_address = sa;
3337}
3338
3339/* Get a device id from inquiry page 0x83 */
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003340static int hpsa_vpd_page_supported(struct ctlr_info *h,
3341 unsigned char scsi3addr[], u8 page)
3342{
3343 int rc;
3344 int i;
3345 int pages;
3346 unsigned char *buf, bufsize;
3347
3348 buf = kzalloc(256, GFP_KERNEL);
3349 if (!buf)
3350 return 0;
3351
3352 /* Get the size of the page list first */
3353 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3354 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3355 buf, HPSA_VPD_HEADER_SZ);
3356 if (rc != 0)
3357 goto exit_unsupported;
3358 pages = buf[3];
3359 if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
3360 bufsize = pages + HPSA_VPD_HEADER_SZ;
3361 else
3362 bufsize = 255;
3363
3364 /* Get the whole VPD page list */
3365 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3366 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3367 buf, bufsize);
3368 if (rc != 0)
3369 goto exit_unsupported;
3370
3371 pages = buf[3];
3372 for (i = 1; i <= pages; i++)
3373 if (buf[3 + i] == page)
3374 goto exit_supported;
3375exit_unsupported:
3376 kfree(buf);
3377 return 0;
3378exit_supported:
3379 kfree(buf);
3380 return 1;
3381}
3382
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003383static void hpsa_get_ioaccel_status(struct ctlr_info *h,
3384 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3385{
3386 int rc;
3387 unsigned char *buf;
3388 u8 ioaccel_status;
3389
3390 this_device->offload_config = 0;
3391 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003392 this_device->offload_to_be_enabled = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003393
3394 buf = kzalloc(64, GFP_KERNEL);
3395 if (!buf)
3396 return;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003397 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
3398 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003399 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003400 VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003401 if (rc != 0)
3402 goto out;
3403
3404#define IOACCEL_STATUS_BYTE 4
3405#define OFFLOAD_CONFIGURED_BIT 0x01
3406#define OFFLOAD_ENABLED_BIT 0x02
3407 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
3408 this_device->offload_config =
3409 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
3410 if (this_device->offload_config) {
3411 this_device->offload_enabled =
3412 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
3413 if (hpsa_get_raid_map(h, scsi3addr, this_device))
3414 this_device->offload_enabled = 0;
3415 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003416 this_device->offload_to_be_enabled = this_device->offload_enabled;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003417out:
3418 kfree(buf);
3419 return;
3420}
3421
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003422/* Get the device id from inquiry page 0x83 */
3423static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
Don Brace75d23d82015-11-04 15:51:39 -06003424 unsigned char *device_id, int index, int buflen)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003425{
3426 int rc;
3427 unsigned char *buf;
3428
3429 if (buflen > 16)
3430 buflen = 16;
3431 buf = kzalloc(64, GFP_KERNEL);
3432 if (!buf)
Stephen M. Camerona84d7942014-05-29 10:54:20 -05003433 return -ENOMEM;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003434 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003435 if (rc == 0)
Don Brace75d23d82015-11-04 15:51:39 -06003436 memcpy(device_id, &buf[index], buflen);
3437
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003438 kfree(buf);
Don Brace75d23d82015-11-04 15:51:39 -06003439
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003440 return rc != 0;
3441}
3442
3443static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
Don Brace03383732015-01-23 16:43:30 -06003444 void *buf, int bufsize,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003445 int extended_response)
3446{
3447 int rc = IO_OK;
3448 struct CommandList *c;
3449 unsigned char scsi3addr[8];
3450 struct ErrorInfo *ei;
3451
Stephen Cameron45fcb862015-01-23 16:43:04 -06003452 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003453
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06003454 /* address the controller */
3455 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003456 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
3457 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
3458 rc = -1;
3459 goto out;
3460 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003461 if (extended_response)
3462 c->Request.CDB[1] = extended_response;
Webb Scales25163bd2015-04-23 09:32:00 -05003463 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
3464 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
3465 if (rc)
3466 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003467 ei = c->err_info;
3468 if (ei->CommandStatus != 0 &&
3469 ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003470 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003471 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003472 } else {
Don Brace03383732015-01-23 16:43:30 -06003473 struct ReportLUNdata *rld = buf;
3474
3475 if (rld->extended_response_flag != extended_response) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003476 dev_err(&h->pdev->dev,
3477 "report luns requested format %u, got %u\n",
3478 extended_response,
Don Brace03383732015-01-23 16:43:30 -06003479 rld->extended_response_flag);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003480 rc = -1;
3481 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003482 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003483out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06003484 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003485 return rc;
3486}
3487
3488static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06003489 struct ReportExtendedLUNdata *buf, int bufsize)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003490{
Don Brace03383732015-01-23 16:43:30 -06003491 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
3492 HPSA_REPORT_PHYS_EXTENDED);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003493}
3494
3495static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
3496 struct ReportLUNdata *buf, int bufsize)
3497{
3498 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
3499}
3500
3501static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
3502 int bus, int target, int lun)
3503{
3504 device->bus = bus;
3505 device->target = target;
3506 device->lun = lun;
3507}
3508
Stephen M. Cameron98465902014-02-21 16:25:00 -06003509/* Use VPD inquiry to get details of volume status */
3510static int hpsa_get_volume_status(struct ctlr_info *h,
3511 unsigned char scsi3addr[])
3512{
3513 int rc;
3514 int status;
3515 int size;
3516 unsigned char *buf;
3517
3518 buf = kzalloc(64, GFP_KERNEL);
3519 if (!buf)
3520 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3521
3522 /* Does controller have VPD for logical volume status? */
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003523 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
Stephen M. Cameron98465902014-02-21 16:25:00 -06003524 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003525
3526 /* Get the size of the VPD return buffer */
3527 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3528 buf, HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003529 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003530 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003531 size = buf[3];
3532
3533 /* Now get the whole VPD buffer */
3534 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3535 buf, size + HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003536 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003537 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003538 status = buf[4]; /* status byte */
3539
3540 kfree(buf);
3541 return status;
3542exit_failed:
3543 kfree(buf);
3544 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3545}
3546
3547/* Determine offline status of a volume.
3548 * Return either:
3549 * 0 (not offline)
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003550 * 0xff (offline for unknown reasons)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003551 * # (integer code indicating one of several NOT READY states
3552 * describing why a volume is to be kept offline)
3553 */
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003554static int hpsa_volume_offline(struct ctlr_info *h,
Stephen M. Cameron98465902014-02-21 16:25:00 -06003555 unsigned char scsi3addr[])
3556{
3557 struct CommandList *c;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003558 unsigned char *sense;
3559 u8 sense_key, asc, ascq;
3560 int sense_len;
Webb Scales25163bd2015-04-23 09:32:00 -05003561 int rc, ldstat = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003562 u16 cmd_status;
3563 u8 scsi_status;
3564#define ASC_LUN_NOT_READY 0x04
3565#define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
3566#define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
3567
3568 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003569
Stephen M. Cameron98465902014-02-21 16:25:00 -06003570 (void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
Webb Scales25163bd2015-04-23 09:32:00 -05003571 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
3572 if (rc) {
3573 cmd_free(h, c);
3574 return 0;
3575 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06003576 sense = c->err_info->SenseInfo;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003577 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3578 sense_len = sizeof(c->err_info->SenseInfo);
3579 else
3580 sense_len = c->err_info->SenseLen;
3581 decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
Stephen M. Cameron98465902014-02-21 16:25:00 -06003582 cmd_status = c->err_info->CommandStatus;
3583 scsi_status = c->err_info->ScsiStatus;
3584 cmd_free(h, c);
3585 /* Is the volume 'not ready'? */
3586 if (cmd_status != CMD_TARGET_STATUS ||
3587 scsi_status != SAM_STAT_CHECK_CONDITION ||
3588 sense_key != NOT_READY ||
3589 asc != ASC_LUN_NOT_READY) {
3590 return 0;
3591 }
3592
3593 /* Determine the reason for not ready state */
3594 ldstat = hpsa_get_volume_status(h, scsi3addr);
3595
3596 /* Keep volume offline in certain cases: */
3597 switch (ldstat) {
3598 case HPSA_LV_UNDERGOING_ERASE:
Scott Benesh5ca01202015-07-18 11:13:04 -05003599 case HPSA_LV_NOT_AVAILABLE:
Stephen M. Cameron98465902014-02-21 16:25:00 -06003600 case HPSA_LV_UNDERGOING_RPI:
3601 case HPSA_LV_PENDING_RPI:
3602 case HPSA_LV_ENCRYPTED_NO_KEY:
3603 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
3604 case HPSA_LV_UNDERGOING_ENCRYPTION:
3605 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
3606 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
3607 return ldstat;
3608 case HPSA_VPD_LV_STATUS_UNSUPPORTED:
3609 /* If VPD status page isn't available,
3610 * use ASC/ASCQ to determine state
3611 */
3612 if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
3613 (ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
3614 return ldstat;
3615 break;
3616 default:
3617 break;
3618 }
3619 return 0;
3620}
3621
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003622/*
3623 * Find out if a logical device supports aborts by simply trying one.
3624 * Smart Array may claim not to support aborts on logical drives, but
3625 * if a MSA2000 * is connected, the drives on that will be presented
3626 * by the Smart Array as logical drives, and aborts may be sent to
3627 * those devices successfully. So the simplest way to find out is
3628 * to simply try an abort and see how the device responds.
3629 */
3630static int hpsa_device_supports_aborts(struct ctlr_info *h,
3631 unsigned char *scsi3addr)
3632{
3633 struct CommandList *c;
3634 struct ErrorInfo *ei;
3635 int rc = 0;
3636
3637 u64 tag = (u64) -1; /* bogus tag */
3638
3639 /* Assume that physical devices support aborts */
3640 if (!is_logical_dev_addr_mode(scsi3addr))
3641 return 1;
3642
3643 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003644
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003645 (void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
3646 (void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
3647 /* no unmap needed here because no data xfer. */
3648 ei = c->err_info;
3649 switch (ei->CommandStatus) {
3650 case CMD_INVALID:
3651 rc = 0;
3652 break;
3653 case CMD_UNABORTABLE:
3654 case CMD_ABORT_FAILED:
3655 rc = 1;
3656 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003657 case CMD_TMF_STATUS:
3658 rc = hpsa_evaluate_tmf_status(h, c);
3659 break;
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003660 default:
3661 rc = 0;
3662 break;
3663 }
3664 cmd_free(h, c);
3665 return rc;
3666}
3667
Don Brace75d23d82015-11-04 15:51:39 -06003668static void sanitize_inquiry_string(unsigned char *s, int len)
3669{
3670 bool terminated = false;
3671
3672 for (; len > 0; (--len, ++s)) {
3673 if (*s == 0)
3674 terminated = true;
3675 if (terminated || *s < 0x20 || *s > 0x7e)
3676 *s = ' ';
3677 }
3678}
3679
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003680static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003681 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
3682 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003683{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003684
3685#define OBDR_SIG_OFFSET 43
3686#define OBDR_TAPE_SIG "$DR-10"
3687#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
3688#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
3689
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003690 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003691 unsigned char *obdr_sig;
Don Brace683fc442015-11-04 15:50:44 -06003692 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003693
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003694 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Don Brace683fc442015-11-04 15:50:44 -06003695 if (!inq_buff) {
3696 rc = -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003697 goto bail_out;
Don Brace683fc442015-11-04 15:50:44 -06003698 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003699
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003700 /* Do an inquiry to the device to see what it is. */
3701 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3702 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3703 /* Inquiry failed (msg printed already) */
3704 dev_err(&h->pdev->dev,
3705 "hpsa_update_device_info: inquiry failed\n");
Don Brace683fc442015-11-04 15:50:44 -06003706 rc = -EIO;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003707 goto bail_out;
3708 }
3709
Don Brace75d23d82015-11-04 15:51:39 -06003710 sanitize_inquiry_string(&inq_buff[8], 8);
3711 sanitize_inquiry_string(&inq_buff[16], 16);
3712
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003713 this_device->devtype = (inq_buff[0] & 0x1f);
3714 memcpy(this_device->scsi3addr, scsi3addr, 8);
3715 memcpy(this_device->vendor, &inq_buff[8],
3716 sizeof(this_device->vendor));
3717 memcpy(this_device->model, &inq_buff[16],
3718 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003719 memset(this_device->device_id, 0,
3720 sizeof(this_device->device_id));
Don Brace75d23d82015-11-04 15:51:39 -06003721 hpsa_get_device_id(h, scsi3addr, this_device->device_id, 8,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003722 sizeof(this_device->device_id));
3723
Don Braceaf15ed32016-02-23 15:16:15 -06003724 if ((this_device->devtype == TYPE_DISK ||
3725 this_device->devtype == TYPE_ZBC) &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003726 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003727 int volume_offline;
3728
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003729 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003730 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3731 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003732 volume_offline = hpsa_volume_offline(h, scsi3addr);
3733 if (volume_offline < 0 || volume_offline > 0xff)
3734 volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
3735 this_device->volume_offline = volume_offline & 0xff;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003736 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003737 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003738 this_device->offload_config = 0;
3739 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003740 this_device->offload_to_be_enabled = 0;
Joe Handzika3144e02015-04-23 09:32:59 -05003741 this_device->hba_ioaccel_enabled = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003742 this_device->volume_offline = 0;
Don Brace03383732015-01-23 16:43:30 -06003743 this_device->queue_depth = h->nr_cmds;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003744 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003745
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003746 if (is_OBDR_device) {
3747 /* See if this is a One-Button-Disaster-Recovery device
3748 * by looking for "$DR-10" at offset 43 in inquiry data.
3749 */
3750 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
3751 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
3752 strncmp(obdr_sig, OBDR_TAPE_SIG,
3753 OBDR_SIG_LEN) == 0);
3754 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003755 kfree(inq_buff);
3756 return 0;
3757
3758bail_out:
3759 kfree(inq_buff);
Don Brace683fc442015-11-04 15:50:44 -06003760 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003761}
3762
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003763static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
3764 struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
3765{
3766 unsigned long flags;
3767 int rc, entry;
3768 /*
3769 * See if this device supports aborts. If we already know
3770 * the device, we already know if it supports aborts, otherwise
3771 * we have to find out if it supports aborts by trying one.
3772 */
3773 spin_lock_irqsave(&h->devlock, flags);
3774 rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
3775 if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
3776 entry >= 0 && entry < h->ndevices) {
3777 dev->supports_aborts = h->dev[entry]->supports_aborts;
3778 spin_unlock_irqrestore(&h->devlock, flags);
3779 } else {
3780 spin_unlock_irqrestore(&h->devlock, flags);
3781 dev->supports_aborts =
3782 hpsa_device_supports_aborts(h, scsi3addr);
3783 if (dev->supports_aborts < 0)
3784 dev->supports_aborts = 0;
3785 }
3786}
3787
Kevin Barnettc7955052015-11-04 15:51:45 -06003788/*
3789 * Helper function to assign bus, target, lun mapping of devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003790 * Logical drive target and lun are assigned at this time, but
3791 * physical device lun and target assignment are deferred (assigned
3792 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
Kevin Barnettc7955052015-11-04 15:51:45 -06003793*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003794static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003795 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003796{
Kevin Barnettc7955052015-11-04 15:51:45 -06003797 u32 lunid = get_unaligned_le32(lunaddrbytes);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003798
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003799 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
3800 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003801 if (is_hba_lunid(lunaddrbytes))
Kevin Barnettc7955052015-11-04 15:51:45 -06003802 hpsa_set_bus_target_lun(device,
3803 HPSA_HBA_BUS, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003804 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003805 /* defer target, lun assignment for physical devices */
Kevin Barnettc7955052015-11-04 15:51:45 -06003806 hpsa_set_bus_target_lun(device,
3807 HPSA_PHYSICAL_DEVICE_BUS, -1, -1);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003808 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003809 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003810 /* It's a logical device */
Scott Teel66749d02015-11-04 15:51:57 -06003811 if (device->external) {
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003812 hpsa_set_bus_target_lun(device,
Kevin Barnettc7955052015-11-04 15:51:45 -06003813 HPSA_EXTERNAL_RAID_VOLUME_BUS, (lunid >> 16) & 0x3fff,
3814 lunid & 0x00ff);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003815 return;
3816 }
Kevin Barnettc7955052015-11-04 15:51:45 -06003817 hpsa_set_bus_target_lun(device, HPSA_RAID_VOLUME_BUS,
3818 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003819}
3820
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003821
3822/*
Scott Teel54b6e9e2014-02-18 13:56:45 -06003823 * Get address of physical disk used for an ioaccel2 mode command:
3824 * 1. Extract ioaccel2 handle from the command.
3825 * 2. Find a matching ioaccel2 handle from list of physical disks.
3826 * 3. Return:
3827 * 1 and set scsi3addr to address of matching physical
3828 * 0 if no matching physical disk was found.
3829 */
3830static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
3831 struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
3832{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003833 struct io_accel2_cmd *c2 =
3834 &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
3835 unsigned long flags;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003836 int i;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003837
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003838 spin_lock_irqsave(&h->devlock, flags);
3839 for (i = 0; i < h->ndevices; i++)
3840 if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
3841 memcpy(scsi3addr, h->dev[i]->scsi3addr,
3842 sizeof(h->dev[i]->scsi3addr));
3843 spin_unlock_irqrestore(&h->devlock, flags);
3844 return 1;
3845 }
3846 spin_unlock_irqrestore(&h->devlock, flags);
3847 return 0;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003848}
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003849
Scott Teel66749d02015-11-04 15:51:57 -06003850static int figure_external_status(struct ctlr_info *h, int raid_ctlr_position,
3851 int i, int nphysicals, int nlocal_logicals)
3852{
3853 /* In report logicals, local logicals are listed first,
3854 * then any externals.
3855 */
3856 int logicals_start = nphysicals + (raid_ctlr_position == 0);
3857
3858 if (i == raid_ctlr_position)
3859 return 0;
3860
3861 if (i < logicals_start)
3862 return 0;
3863
3864 /* i is in logicals range, but still within local logicals */
3865 if ((i - nphysicals - (raid_ctlr_position == 0)) < nlocal_logicals)
3866 return 0;
3867
3868 return 1; /* it's an external lun */
3869}
3870
Scott Teel54b6e9e2014-02-18 13:56:45 -06003871/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003872 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
3873 * logdev. The number of luns in physdev and logdev are returned in
3874 * *nphysicals and *nlogicals, respectively.
3875 * Returns 0 on success, -1 otherwise.
3876 */
3877static int hpsa_gather_lun_info(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06003878 struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003879 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003880{
Don Brace03383732015-01-23 16:43:30 -06003881 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003882 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3883 return -1;
3884 }
Don Brace03383732015-01-23 16:43:30 -06003885 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003886 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
Don Brace03383732015-01-23 16:43:30 -06003887 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
3888 HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003889 *nphysicals = HPSA_MAX_PHYS_LUN;
3890 }
Don Brace03383732015-01-23 16:43:30 -06003891 if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003892 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3893 return -1;
3894 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06003895 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003896 /* Reject Logicals in excess of our max capability. */
3897 if (*nlogicals > HPSA_MAX_LUN) {
3898 dev_warn(&h->pdev->dev,
3899 "maximum logical LUNs (%d) exceeded. "
3900 "%d LUNs ignored.\n", HPSA_MAX_LUN,
3901 *nlogicals - HPSA_MAX_LUN);
3902 *nlogicals = HPSA_MAX_LUN;
3903 }
3904 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
3905 dev_warn(&h->pdev->dev,
3906 "maximum logical + physical LUNs (%d) exceeded. "
3907 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
3908 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
3909 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
3910 }
3911 return 0;
3912}
3913
Don Brace42a91642014-11-14 17:26:27 -06003914static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
3915 int i, int nphysicals, int nlogicals,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06003916 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003917 struct ReportLUNdata *logdev_list)
3918{
3919 /* Helper function, figure out where the LUN ID info is coming from
3920 * given index i, lists of physical and logical devices, where in
3921 * the list the raid controller is supposed to appear (first or last)
3922 */
3923
3924 int logicals_start = nphysicals + (raid_ctlr_position == 0);
3925 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
3926
3927 if (i == raid_ctlr_position)
3928 return RAID_CTLR_LUNID;
3929
3930 if (i < logicals_start)
Stephen M. Camerond5b5d962014-05-29 10:53:34 -05003931 return &physdev_list->LUN[i -
3932 (raid_ctlr_position == 0)].lunid[0];
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003933
3934 if (i < last_device)
3935 return &logdev_list->LUN[i - nphysicals -
3936 (raid_ctlr_position == 0)][0];
3937 BUG();
3938 return NULL;
3939}
3940
Don Brace03383732015-01-23 16:43:30 -06003941/* get physical drive ioaccel handle and queue depth */
3942static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
3943 struct hpsa_scsi_dev_t *dev,
Don Bracef2039b02015-11-04 15:51:08 -06003944 struct ReportExtendedLUNdata *rlep, int rle_index,
Don Brace03383732015-01-23 16:43:30 -06003945 struct bmic_identify_physical_device *id_phys)
3946{
3947 int rc;
Don Bracef2039b02015-11-04 15:51:08 -06003948 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
Don Brace03383732015-01-23 16:43:30 -06003949
3950 dev->ioaccel_handle = rle->ioaccel_handle;
Don Bracef2039b02015-11-04 15:51:08 -06003951 if ((rle->device_flags & 0x08) && dev->ioaccel_handle)
Joe Handzika3144e02015-04-23 09:32:59 -05003952 dev->hba_ioaccel_enabled = 1;
Don Brace03383732015-01-23 16:43:30 -06003953 memset(id_phys, 0, sizeof(*id_phys));
Don Bracef2039b02015-11-04 15:51:08 -06003954 rc = hpsa_bmic_id_physical_device(h, &rle->lunid[0],
3955 GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]), id_phys,
Don Brace03383732015-01-23 16:43:30 -06003956 sizeof(*id_phys));
3957 if (!rc)
3958 /* Reserve space for FW operations */
3959#define DRIVE_CMDS_RESERVED_FOR_FW 2
3960#define DRIVE_QUEUE_DEPTH 7
3961 dev->queue_depth =
3962 le16_to_cpu(id_phys->current_queue_depth_limit) -
3963 DRIVE_CMDS_RESERVED_FOR_FW;
3964 else
3965 dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
Don Brace03383732015-01-23 16:43:30 -06003966}
3967
Joe Handzik8270b862015-07-18 11:12:43 -05003968static void hpsa_get_path_info(struct hpsa_scsi_dev_t *this_device,
Don Bracef2039b02015-11-04 15:51:08 -06003969 struct ReportExtendedLUNdata *rlep, int rle_index,
Joe Handzik8270b862015-07-18 11:12:43 -05003970 struct bmic_identify_physical_device *id_phys)
3971{
Don Bracef2039b02015-11-04 15:51:08 -06003972 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
3973
3974 if ((rle->device_flags & 0x08) && this_device->ioaccel_handle)
Joe Handzik8270b862015-07-18 11:12:43 -05003975 this_device->hba_ioaccel_enabled = 1;
3976
3977 memcpy(&this_device->active_path_index,
3978 &id_phys->active_path_number,
3979 sizeof(this_device->active_path_index));
3980 memcpy(&this_device->path_map,
3981 &id_phys->redundant_path_present_map,
3982 sizeof(this_device->path_map));
3983 memcpy(&this_device->box,
3984 &id_phys->alternate_paths_phys_box_on_port,
3985 sizeof(this_device->box));
3986 memcpy(&this_device->phys_connector,
3987 &id_phys->alternate_paths_phys_connector,
3988 sizeof(this_device->phys_connector));
3989 memcpy(&this_device->bay,
3990 &id_phys->phys_bay_in_box,
3991 sizeof(this_device->bay));
3992}
3993
Scott Teel66749d02015-11-04 15:51:57 -06003994/* get number of local logical disks. */
3995static int hpsa_set_local_logical_count(struct ctlr_info *h,
3996 struct bmic_identify_controller *id_ctlr,
3997 u32 *nlocals)
3998{
3999 int rc;
4000
4001 if (!id_ctlr) {
4002 dev_warn(&h->pdev->dev, "%s: id_ctlr buffer is NULL.\n",
4003 __func__);
4004 return -ENOMEM;
4005 }
4006 memset(id_ctlr, 0, sizeof(*id_ctlr));
4007 rc = hpsa_bmic_id_controller(h, id_ctlr, sizeof(*id_ctlr));
4008 if (!rc)
4009 if (id_ctlr->configured_logical_drive_count < 256)
4010 *nlocals = id_ctlr->configured_logical_drive_count;
4011 else
4012 *nlocals = le16_to_cpu(
4013 id_ctlr->extended_logical_unit_count);
4014 else
4015 *nlocals = -1;
4016 return rc;
4017}
4018
4019
Don Brace8aa60682015-11-04 15:50:01 -06004020static void hpsa_update_scsi_devices(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004021{
4022 /* the idea here is we could get notified
4023 * that some devices have changed, so we do a report
4024 * physical luns and report logical luns cmd, and adjust
4025 * our list of devices accordingly.
4026 *
4027 * The scsi3addr's of devices won't change so long as the
4028 * adapter is not reset. That means we can rescan and
4029 * tell which devices we already know about, vs. new
4030 * devices, vs. disappearing devices.
4031 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06004032 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004033 struct ReportLUNdata *logdev_list = NULL;
Don Brace03383732015-01-23 16:43:30 -06004034 struct bmic_identify_physical_device *id_phys = NULL;
Scott Teel66749d02015-11-04 15:51:57 -06004035 struct bmic_identify_controller *id_ctlr = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004036 u32 nphysicals = 0;
4037 u32 nlogicals = 0;
Scott Teel66749d02015-11-04 15:51:57 -06004038 u32 nlocal_logicals = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004039 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004040 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
4041 int ncurrent = 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06004042 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004043 int raid_ctlr_position;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004044 bool physical_device;
Scott Teelaca4a522012-01-19 14:01:19 -06004045 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004046
Scott Teelcfe5bad2011-10-26 16:21:07 -05004047 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameron92084712014-11-14 17:26:54 -06004048 physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
4049 logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004050 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
Don Brace03383732015-01-23 16:43:30 -06004051 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
Scott Teel66749d02015-11-04 15:51:57 -06004052 id_ctlr = kzalloc(sizeof(*id_ctlr), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004053
Don Brace03383732015-01-23 16:43:30 -06004054 if (!currentsd || !physdev_list || !logdev_list ||
Scott Teel66749d02015-11-04 15:51:57 -06004055 !tmpdevice || !id_phys || !id_ctlr) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004056 dev_err(&h->pdev->dev, "out of memory\n");
4057 goto out;
4058 }
4059 memset(lunzerobits, 0, sizeof(lunzerobits));
4060
Don Brace853633e2015-11-04 15:50:37 -06004061 h->drv_req_rescan = 0; /* cancel scheduled rescan - we're doing it. */
4062
Don Brace03383732015-01-23 16:43:30 -06004063 if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
Don Brace853633e2015-11-04 15:50:37 -06004064 logdev_list, &nlogicals)) {
4065 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004066 goto out;
Don Brace853633e2015-11-04 15:50:37 -06004067 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004068
Scott Teel66749d02015-11-04 15:51:57 -06004069 /* Set number of local logicals (non PTRAID) */
4070 if (hpsa_set_local_logical_count(h, id_ctlr, &nlocal_logicals)) {
4071 dev_warn(&h->pdev->dev,
4072 "%s: Can't determine number of local logical devices.\n",
4073 __func__);
4074 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004075
Scott Teelaca4a522012-01-19 14:01:19 -06004076 /* We might see up to the maximum number of logical and physical disks
4077 * plus external target devices, and a device for the local RAID
4078 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004079 */
Scott Teelaca4a522012-01-19 14:01:19 -06004080 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004081
4082 /* Allocate the per device structures */
4083 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05004084 if (i >= HPSA_MAX_DEVICES) {
4085 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
4086 " %d devices ignored.\n", HPSA_MAX_DEVICES,
4087 ndevs_to_allocate - HPSA_MAX_DEVICES);
4088 break;
4089 }
4090
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004091 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
4092 if (!currentsd[i]) {
4093 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
4094 __FILE__, __LINE__);
Don Brace853633e2015-11-04 15:50:37 -06004095 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004096 goto out;
4097 }
4098 ndev_allocated++;
4099 }
4100
Stephen M. Cameron86452912014-05-29 10:53:49 -05004101 if (is_scsi_rev_5(h))
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004102 raid_ctlr_position = 0;
4103 else
4104 raid_ctlr_position = nphysicals + nlogicals;
4105
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004106 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06004107 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004108 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004109 u8 *lunaddrbytes, is_OBDR = 0;
Don Brace683fc442015-11-04 15:50:44 -06004110 int rc = 0;
Don Bracef2039b02015-11-04 15:51:08 -06004111 int phys_dev_index = i - (raid_ctlr_position == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004112
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004113 physical_device = i < nphysicals + (raid_ctlr_position == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004114
4115 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004116 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
4117 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004118
4119 /* skip masked non-disk devices */
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004120 if (MASKED_DEVICE(lunaddrbytes) && physical_device &&
Don Bracecca8f132015-12-22 10:36:48 -06004121 (physdev_list->LUN[phys_dev_index].device_type != 0x06) &&
4122 (physdev_list->LUN[phys_dev_index].device_flags & 0x01))
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004123 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004124
4125 /* Get device type, vendor, model, device id */
Don Brace683fc442015-11-04 15:50:44 -06004126 rc = hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
4127 &is_OBDR);
4128 if (rc == -ENOMEM) {
4129 dev_warn(&h->pdev->dev,
4130 "Out of memory, rescan deferred.\n");
Don Brace853633e2015-11-04 15:50:37 -06004131 h->drv_req_rescan = 1;
Don Brace683fc442015-11-04 15:50:44 -06004132 goto out;
Don Brace853633e2015-11-04 15:50:37 -06004133 }
Don Brace683fc442015-11-04 15:50:44 -06004134 if (rc) {
4135 dev_warn(&h->pdev->dev,
4136 "Inquiry failed, skipping device.\n");
4137 continue;
4138 }
4139
Scott Teel66749d02015-11-04 15:51:57 -06004140 /* Determine if this is a lun from an external target array */
4141 tmpdevice->external =
4142 figure_external_status(h, raid_ctlr_position, i,
4143 nphysicals, nlocal_logicals);
4144
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06004145 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05004146 hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004147 this_device = currentsd[ncurrent];
4148
Scott Teel34592252015-11-04 15:52:09 -06004149 /* Turn on discovery_polling if there are ext target devices.
4150 * Event-based change notification is unreliable for those.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004151 */
Scott Teel34592252015-11-04 15:52:09 -06004152 if (!h->discovery_polling) {
4153 if (tmpdevice->external) {
4154 h->discovery_polling = 1;
4155 dev_info(&h->pdev->dev,
4156 "External target, activate discovery polling.\n");
4157 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004158 }
4159
Scott Teel34592252015-11-04 15:52:09 -06004160
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004161 *this_device = *tmpdevice;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004162 this_device->physical_device = physical_device;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004163
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004164 /*
4165 * Expose all devices except for physical devices that
4166 * are masked.
4167 */
4168 if (MASKED_DEVICE(lunaddrbytes) && this_device->physical_device)
Kevin Barnett2a168202015-11-04 15:51:21 -06004169 this_device->expose_device = 0;
4170 else
4171 this_device->expose_device = 1;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004172
Kevin Barnettd04e62b2015-11-04 15:52:34 -06004173
4174 /*
4175 * Get the SAS address for physical devices that are exposed.
4176 */
4177 if (this_device->physical_device && this_device->expose_device)
4178 hpsa_get_sas_address(h, lunaddrbytes, this_device);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004179
4180 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004181 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004182 /* We don't *really* support actual CD-ROM devices,
4183 * just "One Button Disaster Recovery" tape drive
4184 * which temporarily pretends to be a CD-ROM drive.
4185 * So we check that the device is really an OBDR tape
4186 * device by checking for "$DR-10" in bytes 43-48 of
4187 * the inquiry data.
4188 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004189 if (is_OBDR)
4190 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004191 break;
4192 case TYPE_DISK:
Don Braceaf15ed32016-02-23 15:16:15 -06004193 case TYPE_ZBC:
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004194 if (this_device->physical_device) {
Kevin Barnettb9092b72015-07-18 11:12:59 -05004195 /* The disk is in HBA mode. */
4196 /* Never use RAID mapper in HBA mode. */
Stephen M. Cameron316b2212014-02-21 16:25:15 -06004197 this_device->offload_enabled = 0;
Kevin Barnettb9092b72015-07-18 11:12:59 -05004198 hpsa_get_ioaccel_drive_info(h, this_device,
Don Bracef2039b02015-11-04 15:51:08 -06004199 physdev_list, phys_dev_index, id_phys);
4200 hpsa_get_path_info(this_device,
4201 physdev_list, phys_dev_index, id_phys);
Kevin Barnettb9092b72015-07-18 11:12:59 -05004202 }
Joe Handzikecf418d12015-04-23 09:33:04 -05004203 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004204 break;
4205 case TYPE_TAPE:
4206 case TYPE_MEDIUM_CHANGER:
Don Bracecca8f132015-12-22 10:36:48 -06004207 ncurrent++;
4208 break;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004209 case TYPE_ENCLOSURE:
Don Brace17a9e542016-02-23 15:16:09 -06004210 if (!this_device->external)
4211 hpsa_get_enclosure_info(h, lunaddrbytes,
Don Bracecca8f132015-12-22 10:36:48 -06004212 physdev_list, phys_dev_index,
4213 this_device);
Kevin Barnettb9092b72015-07-18 11:12:59 -05004214 ncurrent++;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004215 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004216 case TYPE_RAID:
4217 /* Only present the Smartarray HBA as a RAID controller.
4218 * If it's a RAID controller other than the HBA itself
4219 * (an external RAID controller, MSA500 or similar)
4220 * don't present it.
4221 */
4222 if (!is_hba_lunid(lunaddrbytes))
4223 break;
4224 ncurrent++;
4225 break;
4226 default:
4227 break;
4228 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05004229 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004230 break;
4231 }
Kevin Barnettd04e62b2015-11-04 15:52:34 -06004232
4233 if (h->sas_host == NULL) {
4234 int rc = 0;
4235
4236 rc = hpsa_add_sas_host(h);
4237 if (rc) {
4238 dev_warn(&h->pdev->dev,
4239 "Could not add sas host %d\n", rc);
4240 goto out;
4241 }
4242 }
4243
Don Brace8aa60682015-11-04 15:50:01 -06004244 adjust_hpsa_scsi_table(h, currentsd, ncurrent);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004245out:
4246 kfree(tmpdevice);
4247 for (i = 0; i < ndev_allocated; i++)
4248 kfree(currentsd[i]);
4249 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004250 kfree(physdev_list);
4251 kfree(logdev_list);
Scott Teel66749d02015-11-04 15:51:57 -06004252 kfree(id_ctlr);
Don Brace03383732015-01-23 16:43:30 -06004253 kfree(id_phys);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004254}
4255
Webb Scalesec5cbf02015-01-23 16:44:45 -06004256static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
4257 struct scatterlist *sg)
4258{
4259 u64 addr64 = (u64) sg_dma_address(sg);
4260 unsigned int len = sg_dma_len(sg);
4261
4262 desc->Addr = cpu_to_le64(addr64);
4263 desc->Len = cpu_to_le32(len);
4264 desc->Ext = 0;
4265}
4266
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004267/*
4268 * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004269 * dma mapping and fills in the scatter gather entries of the
4270 * hpsa command, cp.
4271 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004272static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004273 struct CommandList *cp,
4274 struct scsi_cmnd *cmd)
4275{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004276 struct scatterlist *sg;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004277 int use_sg, i, sg_limit, chained, last_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004278 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004279
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004280 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004281
4282 use_sg = scsi_dma_map(cmd);
4283 if (use_sg < 0)
4284 return use_sg;
4285
4286 if (!use_sg)
4287 goto sglist_finished;
4288
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004289 /*
4290 * If the number of entries is greater than the max for a single list,
4291 * then we have a chained list; we will set up all but one entry in the
4292 * first list (the last entry is saved for link information);
4293 * otherwise, we don't have a chained list and we'll set up at each of
4294 * the entries in the one list.
4295 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004296 curr_sg = cp->SG;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004297 chained = use_sg > h->max_cmd_sg_entries;
4298 sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
4299 last_sg = scsi_sg_count(cmd) - 1;
4300 scsi_for_each_sg(cmd, sg, sg_limit, i) {
Webb Scalesec5cbf02015-01-23 16:44:45 -06004301 hpsa_set_sg_descriptor(curr_sg, sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004302 curr_sg++;
4303 }
Webb Scalesec5cbf02015-01-23 16:44:45 -06004304
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004305 if (chained) {
4306 /*
4307 * Continue with the chained list. Set curr_sg to the chained
4308 * list. Modify the limit to the total count less the entries
4309 * we've already set up. Resume the scan at the list entry
4310 * where the previous loop left off.
4311 */
4312 curr_sg = h->cmd_sg_list[cp->cmdindex];
4313 sg_limit = use_sg - sg_limit;
4314 for_each_sg(sg, sg, sg_limit, i) {
4315 hpsa_set_sg_descriptor(curr_sg, sg);
4316 curr_sg++;
4317 }
4318 }
4319
Webb Scalesec5cbf02015-01-23 16:44:45 -06004320 /* Back the pointer up to the last entry and mark it as "last". */
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004321 (curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004322
4323 if (use_sg + chained > h->maxSG)
4324 h->maxSG = use_sg + chained;
4325
4326 if (chained) {
4327 cp->Header.SGList = h->max_cmd_sg_entries;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004328 cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06004329 if (hpsa_map_sg_chain_block(h, cp)) {
4330 scsi_dma_unmap(cmd);
4331 return -1;
4332 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004333 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004334 }
4335
4336sglist_finished:
4337
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004338 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004339 cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004340 return 0;
4341}
4342
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004343#define IO_ACCEL_INELIGIBLE (1)
4344static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
4345{
4346 int is_write = 0;
4347 u32 block;
4348 u32 block_cnt;
4349
4350 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
4351 switch (cdb[0]) {
4352 case WRITE_6:
4353 case WRITE_12:
4354 is_write = 1;
4355 case READ_6:
4356 case READ_12:
4357 if (*cdb_len == 6) {
Don Bracec8a6c9a2015-11-04 15:50:50 -06004358 block = get_unaligned_be16(&cdb[2]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004359 block_cnt = cdb[4];
Don Bracec8a6c9a2015-11-04 15:50:50 -06004360 if (block_cnt == 0)
4361 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004362 } else {
4363 BUG_ON(*cdb_len != 12);
Don Bracec8a6c9a2015-11-04 15:50:50 -06004364 block = get_unaligned_be32(&cdb[2]);
4365 block_cnt = get_unaligned_be32(&cdb[6]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004366 }
4367 if (block_cnt > 0xffff)
4368 return IO_ACCEL_INELIGIBLE;
4369
4370 cdb[0] = is_write ? WRITE_10 : READ_10;
4371 cdb[1] = 0;
4372 cdb[2] = (u8) (block >> 24);
4373 cdb[3] = (u8) (block >> 16);
4374 cdb[4] = (u8) (block >> 8);
4375 cdb[5] = (u8) (block);
4376 cdb[6] = 0;
4377 cdb[7] = (u8) (block_cnt >> 8);
4378 cdb[8] = (u8) (block_cnt);
4379 cdb[9] = 0;
4380 *cdb_len = 10;
4381 break;
4382 }
4383 return 0;
4384}
4385
Scott Teelc3497752014-02-18 13:56:34 -06004386static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004387 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004388 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Matt Gatese1f7de02014-02-18 13:55:17 -06004389{
4390 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06004391 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
4392 unsigned int len;
4393 unsigned int total_len = 0;
4394 struct scatterlist *sg;
4395 u64 addr64;
4396 int use_sg, i;
4397 struct SGDescriptor *curr_sg;
4398 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
4399
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004400 /* TODO: implement chaining support */
Don Brace03383732015-01-23 16:43:30 -06004401 if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
4402 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004403 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004404 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004405
Matt Gatese1f7de02014-02-18 13:55:17 -06004406 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
4407
Don Brace03383732015-01-23 16:43:30 -06004408 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4409 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004410 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004411 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004412
Matt Gatese1f7de02014-02-18 13:55:17 -06004413 c->cmd_type = CMD_IOACCEL1;
4414
4415 /* Adjust the DMA address to point to the accelerated command buffer */
4416 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
4417 (c->cmdindex * sizeof(*cp));
4418 BUG_ON(c->busaddr & 0x0000007F);
4419
4420 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004421 if (use_sg < 0) {
4422 atomic_dec(&phys_disk->ioaccel_cmds_out);
Matt Gatese1f7de02014-02-18 13:55:17 -06004423 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004424 }
Matt Gatese1f7de02014-02-18 13:55:17 -06004425
4426 if (use_sg) {
4427 curr_sg = cp->SG;
4428 scsi_for_each_sg(cmd, sg, use_sg, i) {
4429 addr64 = (u64) sg_dma_address(sg);
4430 len = sg_dma_len(sg);
4431 total_len += len;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004432 curr_sg->Addr = cpu_to_le64(addr64);
4433 curr_sg->Len = cpu_to_le32(len);
4434 curr_sg->Ext = cpu_to_le32(0);
Matt Gatese1f7de02014-02-18 13:55:17 -06004435 curr_sg++;
4436 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004437 (--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
Matt Gatese1f7de02014-02-18 13:55:17 -06004438
4439 switch (cmd->sc_data_direction) {
4440 case DMA_TO_DEVICE:
4441 control |= IOACCEL1_CONTROL_DATA_OUT;
4442 break;
4443 case DMA_FROM_DEVICE:
4444 control |= IOACCEL1_CONTROL_DATA_IN;
4445 break;
4446 case DMA_NONE:
4447 control |= IOACCEL1_CONTROL_NODATAXFER;
4448 break;
4449 default:
4450 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4451 cmd->sc_data_direction);
4452 BUG();
4453 break;
4454 }
4455 } else {
4456 control |= IOACCEL1_CONTROL_NODATAXFER;
4457 }
4458
Scott Teelc3497752014-02-18 13:56:34 -06004459 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06004460 /* Fill out the command structure to submit */
Don Brace2b08b3e2015-01-23 16:41:09 -06004461 cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
4462 cp->transfer_len = cpu_to_le32(total_len);
4463 cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
4464 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
4465 cp->control = cpu_to_le32(control);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004466 memcpy(cp->CDB, cdb, cdb_len);
4467 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06004468 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004469 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06004470 return 0;
4471}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004472
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004473/*
4474 * Queue a command directly to a device behind the controller using the
4475 * I/O accelerator path.
4476 */
4477static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
4478 struct CommandList *c)
4479{
4480 struct scsi_cmnd *cmd = c->scsi_cmd;
4481 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4482
Don Brace03383732015-01-23 16:43:30 -06004483 c->phys_disk = dev;
4484
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004485 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004486 cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004487}
4488
Scott Teeldd0e19f2014-02-18 13:57:31 -06004489/*
4490 * Set encryption parameters for the ioaccel2 request
4491 */
4492static void set_encrypt_ioaccel2(struct ctlr_info *h,
4493 struct CommandList *c, struct io_accel2_cmd *cp)
4494{
4495 struct scsi_cmnd *cmd = c->scsi_cmd;
4496 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4497 struct raid_map_data *map = &dev->raid_map;
4498 u64 first_block;
4499
Scott Teeldd0e19f2014-02-18 13:57:31 -06004500 /* Are we doing encryption on this device */
Don Brace2b08b3e2015-01-23 16:41:09 -06004501 if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
Scott Teeldd0e19f2014-02-18 13:57:31 -06004502 return;
4503 /* Set the data encryption key index. */
4504 cp->dekindex = map->dekindex;
4505
4506 /* Set the encryption enable flag, encoded into direction field. */
4507 cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
4508
4509 /* Set encryption tweak values based on logical block address
4510 * If block size is 512, tweak value is LBA.
4511 * For other block sizes, tweak is (LBA * block size)/ 512)
4512 */
4513 switch (cmd->cmnd[0]) {
4514 /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
4515 case WRITE_6:
4516 case READ_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06004517 first_block = get_unaligned_be16(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004518 break;
4519 case WRITE_10:
4520 case READ_10:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004521 /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
4522 case WRITE_12:
4523 case READ_12:
Don Brace2b08b3e2015-01-23 16:41:09 -06004524 first_block = get_unaligned_be32(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004525 break;
4526 case WRITE_16:
4527 case READ_16:
Don Brace2b08b3e2015-01-23 16:41:09 -06004528 first_block = get_unaligned_be64(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004529 break;
4530 default:
4531 dev_err(&h->pdev->dev,
Don Brace2b08b3e2015-01-23 16:41:09 -06004532 "ERROR: %s: size (0x%x) not supported for encryption\n",
4533 __func__, cmd->cmnd[0]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004534 BUG();
4535 break;
4536 }
Don Brace2b08b3e2015-01-23 16:41:09 -06004537
4538 if (le32_to_cpu(map->volume_blk_size) != 512)
4539 first_block = first_block *
4540 le32_to_cpu(map->volume_blk_size)/512;
4541
4542 cp->tweak_lower = cpu_to_le32(first_block);
4543 cp->tweak_upper = cpu_to_le32(first_block >> 32);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004544}
4545
Scott Teelc3497752014-02-18 13:56:34 -06004546static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
4547 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004548 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004549{
4550 struct scsi_cmnd *cmd = c->scsi_cmd;
4551 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
4552 struct ioaccel2_sg_element *curr_sg;
4553 int use_sg, i;
4554 struct scatterlist *sg;
4555 u64 addr64;
4556 u32 len;
4557 u32 total_len = 0;
4558
Webb Scalesd9a729f2015-04-23 09:33:27 -05004559 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Scott Teelc3497752014-02-18 13:56:34 -06004560
Don Brace03383732015-01-23 16:43:30 -06004561 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4562 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004563 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004564 }
4565
Scott Teelc3497752014-02-18 13:56:34 -06004566 c->cmd_type = CMD_IOACCEL2;
4567 /* Adjust the DMA address to point to the accelerated command buffer */
4568 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
4569 (c->cmdindex * sizeof(*cp));
4570 BUG_ON(c->busaddr & 0x0000007F);
4571
4572 memset(cp, 0, sizeof(*cp));
4573 cp->IU_type = IOACCEL2_IU_TYPE;
4574
4575 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004576 if (use_sg < 0) {
4577 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004578 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004579 }
Scott Teelc3497752014-02-18 13:56:34 -06004580
4581 if (use_sg) {
Scott Teelc3497752014-02-18 13:56:34 -06004582 curr_sg = cp->sg;
Webb Scalesd9a729f2015-04-23 09:33:27 -05004583 if (use_sg > h->ioaccel_maxsg) {
4584 addr64 = le64_to_cpu(
4585 h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
4586 curr_sg->address = cpu_to_le64(addr64);
4587 curr_sg->length = 0;
4588 curr_sg->reserved[0] = 0;
4589 curr_sg->reserved[1] = 0;
4590 curr_sg->reserved[2] = 0;
4591 curr_sg->chain_indicator = 0x80;
4592
4593 curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
4594 }
Scott Teelc3497752014-02-18 13:56:34 -06004595 scsi_for_each_sg(cmd, sg, use_sg, i) {
4596 addr64 = (u64) sg_dma_address(sg);
4597 len = sg_dma_len(sg);
4598 total_len += len;
4599 curr_sg->address = cpu_to_le64(addr64);
4600 curr_sg->length = cpu_to_le32(len);
4601 curr_sg->reserved[0] = 0;
4602 curr_sg->reserved[1] = 0;
4603 curr_sg->reserved[2] = 0;
4604 curr_sg->chain_indicator = 0;
4605 curr_sg++;
4606 }
4607
4608 switch (cmd->sc_data_direction) {
4609 case DMA_TO_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004610 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4611 cp->direction |= IOACCEL2_DIR_DATA_OUT;
Scott Teelc3497752014-02-18 13:56:34 -06004612 break;
4613 case DMA_FROM_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004614 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4615 cp->direction |= IOACCEL2_DIR_DATA_IN;
Scott Teelc3497752014-02-18 13:56:34 -06004616 break;
4617 case DMA_NONE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004618 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4619 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004620 break;
4621 default:
4622 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4623 cmd->sc_data_direction);
4624 BUG();
4625 break;
4626 }
4627 } else {
Scott Teeldd0e19f2014-02-18 13:57:31 -06004628 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4629 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004630 }
Scott Teeldd0e19f2014-02-18 13:57:31 -06004631
4632 /* Set encryption parameters, if necessary */
4633 set_encrypt_ioaccel2(h, c, cp);
4634
Don Brace2b08b3e2015-01-23 16:41:09 -06004635 cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
Don Bracef2405db2015-01-23 16:43:09 -06004636 cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
Scott Teelc3497752014-02-18 13:56:34 -06004637 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
Scott Teelc3497752014-02-18 13:56:34 -06004638
Scott Teelc3497752014-02-18 13:56:34 -06004639 cp->data_len = cpu_to_le32(total_len);
4640 cp->err_ptr = cpu_to_le64(c->busaddr +
4641 offsetof(struct io_accel2_cmd, error_data));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004642 cp->err_len = cpu_to_le32(sizeof(cp->error_data));
Scott Teelc3497752014-02-18 13:56:34 -06004643
Webb Scalesd9a729f2015-04-23 09:33:27 -05004644 /* fill in sg elements */
4645 if (use_sg > h->ioaccel_maxsg) {
4646 cp->sg_count = 1;
Don Bracea736e9b2015-11-04 15:51:14 -06004647 cp->sg[0].length = cpu_to_le32(use_sg * sizeof(cp->sg[0]));
Webb Scalesd9a729f2015-04-23 09:33:27 -05004648 if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
4649 atomic_dec(&phys_disk->ioaccel_cmds_out);
4650 scsi_dma_unmap(cmd);
4651 return -1;
4652 }
4653 } else
4654 cp->sg_count = (u8) use_sg;
4655
Scott Teelc3497752014-02-18 13:56:34 -06004656 enqueue_cmd_and_start_io(h, c);
4657 return 0;
4658}
4659
4660/*
4661 * Queue a command to the correct I/O accelerator path.
4662 */
4663static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
4664 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004665 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004666{
Don Brace03383732015-01-23 16:43:30 -06004667 /* Try to honor the device's queue depth */
4668 if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
4669 phys_disk->queue_depth) {
4670 atomic_dec(&phys_disk->ioaccel_cmds_out);
4671 return IO_ACCEL_INELIGIBLE;
4672 }
Scott Teelc3497752014-02-18 13:56:34 -06004673 if (h->transMethod & CFGTBL_Trans_io_accel1)
4674 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004675 cdb, cdb_len, scsi3addr,
4676 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004677 else
4678 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004679 cdb, cdb_len, scsi3addr,
4680 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004681}
4682
Scott Teel6b80b182014-02-18 13:56:55 -06004683static void raid_map_helper(struct raid_map_data *map,
4684 int offload_to_mirror, u32 *map_index, u32 *current_group)
4685{
4686 if (offload_to_mirror == 0) {
4687 /* use physical disk in the first mirrored group. */
Don Brace2b08b3e2015-01-23 16:41:09 -06004688 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004689 return;
4690 }
4691 do {
4692 /* determine mirror group that *map_index indicates */
Don Brace2b08b3e2015-01-23 16:41:09 -06004693 *current_group = *map_index /
4694 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004695 if (offload_to_mirror == *current_group)
4696 continue;
Don Brace2b08b3e2015-01-23 16:41:09 -06004697 if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
Scott Teel6b80b182014-02-18 13:56:55 -06004698 /* select map index from next group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004699 *map_index += le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004700 (*current_group)++;
4701 } else {
4702 /* select map index from first group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004703 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004704 *current_group = 0;
4705 }
4706 } while (offload_to_mirror != *current_group);
4707}
4708
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004709/*
4710 * Attempt to perform offload RAID mapping for a logical volume I/O.
4711 */
4712static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4713 struct CommandList *c)
4714{
4715 struct scsi_cmnd *cmd = c->scsi_cmd;
4716 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4717 struct raid_map_data *map = &dev->raid_map;
4718 struct raid_map_disk_data *dd = &map->data[0];
4719 int is_write = 0;
4720 u32 map_index;
4721 u64 first_block, last_block;
4722 u32 block_cnt;
4723 u32 blocks_per_row;
4724 u64 first_row, last_row;
4725 u32 first_row_offset, last_row_offset;
4726 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06004727 u64 r0_first_row, r0_last_row;
4728 u32 r5or6_blocks_per_row;
4729 u64 r5or6_first_row, r5or6_last_row;
4730 u32 r5or6_first_row_offset, r5or6_last_row_offset;
4731 u32 r5or6_first_column, r5or6_last_column;
4732 u32 total_disks_per_row;
4733 u32 stripesize;
4734 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004735 u32 map_row;
4736 u32 disk_handle;
4737 u64 disk_block;
4738 u32 disk_block_cnt;
4739 u8 cdb[16];
4740 u8 cdb_len;
Don Brace2b08b3e2015-01-23 16:41:09 -06004741 u16 strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004742#if BITS_PER_LONG == 32
4743 u64 tmpdiv;
4744#endif
Scott Teel6b80b182014-02-18 13:56:55 -06004745 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004746
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004747 /* check for valid opcode, get LBA and block count */
4748 switch (cmd->cmnd[0]) {
4749 case WRITE_6:
4750 is_write = 1;
4751 case READ_6:
Don Bracec8a6c9a2015-11-04 15:50:50 -06004752 first_block = get_unaligned_be16(&cmd->cmnd[2]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004753 block_cnt = cmd->cmnd[4];
Stephen M. Cameron3fa89a02014-07-03 10:18:14 -05004754 if (block_cnt == 0)
4755 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004756 break;
4757 case WRITE_10:
4758 is_write = 1;
4759 case READ_10:
4760 first_block =
4761 (((u64) cmd->cmnd[2]) << 24) |
4762 (((u64) cmd->cmnd[3]) << 16) |
4763 (((u64) cmd->cmnd[4]) << 8) |
4764 cmd->cmnd[5];
4765 block_cnt =
4766 (((u32) cmd->cmnd[7]) << 8) |
4767 cmd->cmnd[8];
4768 break;
4769 case WRITE_12:
4770 is_write = 1;
4771 case READ_12:
4772 first_block =
4773 (((u64) cmd->cmnd[2]) << 24) |
4774 (((u64) cmd->cmnd[3]) << 16) |
4775 (((u64) cmd->cmnd[4]) << 8) |
4776 cmd->cmnd[5];
4777 block_cnt =
4778 (((u32) cmd->cmnd[6]) << 24) |
4779 (((u32) cmd->cmnd[7]) << 16) |
4780 (((u32) cmd->cmnd[8]) << 8) |
4781 cmd->cmnd[9];
4782 break;
4783 case WRITE_16:
4784 is_write = 1;
4785 case READ_16:
4786 first_block =
4787 (((u64) cmd->cmnd[2]) << 56) |
4788 (((u64) cmd->cmnd[3]) << 48) |
4789 (((u64) cmd->cmnd[4]) << 40) |
4790 (((u64) cmd->cmnd[5]) << 32) |
4791 (((u64) cmd->cmnd[6]) << 24) |
4792 (((u64) cmd->cmnd[7]) << 16) |
4793 (((u64) cmd->cmnd[8]) << 8) |
4794 cmd->cmnd[9];
4795 block_cnt =
4796 (((u32) cmd->cmnd[10]) << 24) |
4797 (((u32) cmd->cmnd[11]) << 16) |
4798 (((u32) cmd->cmnd[12]) << 8) |
4799 cmd->cmnd[13];
4800 break;
4801 default:
4802 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4803 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004804 last_block = first_block + block_cnt - 1;
4805
4806 /* check for write to non-RAID-0 */
4807 if (is_write && dev->raid_level != 0)
4808 return IO_ACCEL_INELIGIBLE;
4809
4810 /* check for invalid block or wraparound */
Don Brace2b08b3e2015-01-23 16:41:09 -06004811 if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
4812 last_block < first_block)
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004813 return IO_ACCEL_INELIGIBLE;
4814
4815 /* calculate stripe information for the request */
Don Brace2b08b3e2015-01-23 16:41:09 -06004816 blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
4817 le16_to_cpu(map->strip_size);
4818 strip_size = le16_to_cpu(map->strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004819#if BITS_PER_LONG == 32
4820 tmpdiv = first_block;
4821 (void) do_div(tmpdiv, blocks_per_row);
4822 first_row = tmpdiv;
4823 tmpdiv = last_block;
4824 (void) do_div(tmpdiv, blocks_per_row);
4825 last_row = tmpdiv;
4826 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4827 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
4828 tmpdiv = first_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06004829 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004830 first_column = tmpdiv;
4831 tmpdiv = last_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06004832 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004833 last_column = tmpdiv;
4834#else
4835 first_row = first_block / blocks_per_row;
4836 last_row = last_block / blocks_per_row;
4837 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4838 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
Don Brace2b08b3e2015-01-23 16:41:09 -06004839 first_column = first_row_offset / strip_size;
4840 last_column = last_row_offset / strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004841#endif
4842
4843 /* if this isn't a single row/column then give to the controller */
4844 if ((first_row != last_row) || (first_column != last_column))
4845 return IO_ACCEL_INELIGIBLE;
4846
4847 /* proceeding with driver mapping */
Don Brace2b08b3e2015-01-23 16:41:09 -06004848 total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
4849 le16_to_cpu(map->metadata_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004850 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06004851 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06004852 map_index = (map_row * total_disks_per_row) + first_column;
4853
4854 switch (dev->raid_level) {
4855 case HPSA_RAID_0:
4856 break; /* nothing special to do */
4857 case HPSA_RAID_1:
4858 /* Handles load balance across RAID 1 members.
4859 * (2-drive R1 and R10 with even # of drives.)
4860 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004861 */
Don Brace2b08b3e2015-01-23 16:41:09 -06004862 BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004863 if (dev->offload_to_mirror)
Don Brace2b08b3e2015-01-23 16:41:09 -06004864 map_index += le16_to_cpu(map->data_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004865 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06004866 break;
4867 case HPSA_RAID_ADM:
4868 /* Handles N-way mirrors (R1-ADM)
4869 * and R10 with # of drives divisible by 3.)
4870 */
Don Brace2b08b3e2015-01-23 16:41:09 -06004871 BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
Scott Teel6b80b182014-02-18 13:56:55 -06004872
4873 offload_to_mirror = dev->offload_to_mirror;
4874 raid_map_helper(map, offload_to_mirror,
4875 &map_index, &current_group);
4876 /* set mirror group to use next time */
4877 offload_to_mirror =
Don Brace2b08b3e2015-01-23 16:41:09 -06004878 (offload_to_mirror >=
4879 le16_to_cpu(map->layout_map_count) - 1)
Scott Teel6b80b182014-02-18 13:56:55 -06004880 ? 0 : offload_to_mirror + 1;
Scott Teel6b80b182014-02-18 13:56:55 -06004881 dev->offload_to_mirror = offload_to_mirror;
4882 /* Avoid direct use of dev->offload_to_mirror within this
4883 * function since multiple threads might simultaneously
4884 * increment it beyond the range of dev->layout_map_count -1.
4885 */
4886 break;
4887 case HPSA_RAID_5:
4888 case HPSA_RAID_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06004889 if (le16_to_cpu(map->layout_map_count) <= 1)
Scott Teel6b80b182014-02-18 13:56:55 -06004890 break;
4891
4892 /* Verify first and last block are in same RAID group */
4893 r5or6_blocks_per_row =
Don Brace2b08b3e2015-01-23 16:41:09 -06004894 le16_to_cpu(map->strip_size) *
4895 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004896 BUG_ON(r5or6_blocks_per_row == 0);
Don Brace2b08b3e2015-01-23 16:41:09 -06004897 stripesize = r5or6_blocks_per_row *
4898 le16_to_cpu(map->layout_map_count);
Scott Teel6b80b182014-02-18 13:56:55 -06004899#if BITS_PER_LONG == 32
4900 tmpdiv = first_block;
4901 first_group = do_div(tmpdiv, stripesize);
4902 tmpdiv = first_group;
4903 (void) do_div(tmpdiv, r5or6_blocks_per_row);
4904 first_group = tmpdiv;
4905 tmpdiv = last_block;
4906 last_group = do_div(tmpdiv, stripesize);
4907 tmpdiv = last_group;
4908 (void) do_div(tmpdiv, r5or6_blocks_per_row);
4909 last_group = tmpdiv;
4910#else
4911 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
4912 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
Scott Teel6b80b182014-02-18 13:56:55 -06004913#endif
Stephen M. Cameron000ff7c2014-03-13 17:12:50 -05004914 if (first_group != last_group)
Scott Teel6b80b182014-02-18 13:56:55 -06004915 return IO_ACCEL_INELIGIBLE;
4916
4917 /* Verify request is in a single row of RAID 5/6 */
4918#if BITS_PER_LONG == 32
4919 tmpdiv = first_block;
4920 (void) do_div(tmpdiv, stripesize);
4921 first_row = r5or6_first_row = r0_first_row = tmpdiv;
4922 tmpdiv = last_block;
4923 (void) do_div(tmpdiv, stripesize);
4924 r5or6_last_row = r0_last_row = tmpdiv;
4925#else
4926 first_row = r5or6_first_row = r0_first_row =
4927 first_block / stripesize;
4928 r5or6_last_row = r0_last_row = last_block / stripesize;
4929#endif
4930 if (r5or6_first_row != r5or6_last_row)
4931 return IO_ACCEL_INELIGIBLE;
4932
4933
4934 /* Verify request is in a single column */
4935#if BITS_PER_LONG == 32
4936 tmpdiv = first_block;
4937 first_row_offset = do_div(tmpdiv, stripesize);
4938 tmpdiv = first_row_offset;
4939 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
4940 r5or6_first_row_offset = first_row_offset;
4941 tmpdiv = last_block;
4942 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
4943 tmpdiv = r5or6_last_row_offset;
4944 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
4945 tmpdiv = r5or6_first_row_offset;
4946 (void) do_div(tmpdiv, map->strip_size);
4947 first_column = r5or6_first_column = tmpdiv;
4948 tmpdiv = r5or6_last_row_offset;
4949 (void) do_div(tmpdiv, map->strip_size);
4950 r5or6_last_column = tmpdiv;
4951#else
4952 first_row_offset = r5or6_first_row_offset =
4953 (u32)((first_block % stripesize) %
4954 r5or6_blocks_per_row);
4955
4956 r5or6_last_row_offset =
4957 (u32)((last_block % stripesize) %
4958 r5or6_blocks_per_row);
4959
4960 first_column = r5or6_first_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06004961 r5or6_first_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06004962 r5or6_last_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06004963 r5or6_last_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06004964#endif
4965 if (r5or6_first_column != r5or6_last_column)
4966 return IO_ACCEL_INELIGIBLE;
4967
4968 /* Request is eligible */
4969 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06004970 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06004971
4972 map_index = (first_group *
Don Brace2b08b3e2015-01-23 16:41:09 -06004973 (le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
Scott Teel6b80b182014-02-18 13:56:55 -06004974 (map_row * total_disks_per_row) + first_column;
4975 break;
4976 default:
4977 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004978 }
Scott Teel6b80b182014-02-18 13:56:55 -06004979
Stephen Cameron07543e02015-01-23 16:44:14 -06004980 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
4981 return IO_ACCEL_INELIGIBLE;
4982
Don Brace03383732015-01-23 16:43:30 -06004983 c->phys_disk = dev->phys_disk[map_index];
4984
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004985 disk_handle = dd[map_index].ioaccel_handle;
Don Brace2b08b3e2015-01-23 16:41:09 -06004986 disk_block = le64_to_cpu(map->disk_starting_blk) +
4987 first_row * le16_to_cpu(map->strip_size) +
4988 (first_row_offset - first_column *
4989 le16_to_cpu(map->strip_size));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004990 disk_block_cnt = block_cnt;
4991
4992 /* handle differing logical/physical block sizes */
4993 if (map->phys_blk_shift) {
4994 disk_block <<= map->phys_blk_shift;
4995 disk_block_cnt <<= map->phys_blk_shift;
4996 }
4997 BUG_ON(disk_block_cnt > 0xffff);
4998
4999 /* build the new CDB for the physical disk I/O */
5000 if (disk_block > 0xffffffff) {
5001 cdb[0] = is_write ? WRITE_16 : READ_16;
5002 cdb[1] = 0;
5003 cdb[2] = (u8) (disk_block >> 56);
5004 cdb[3] = (u8) (disk_block >> 48);
5005 cdb[4] = (u8) (disk_block >> 40);
5006 cdb[5] = (u8) (disk_block >> 32);
5007 cdb[6] = (u8) (disk_block >> 24);
5008 cdb[7] = (u8) (disk_block >> 16);
5009 cdb[8] = (u8) (disk_block >> 8);
5010 cdb[9] = (u8) (disk_block);
5011 cdb[10] = (u8) (disk_block_cnt >> 24);
5012 cdb[11] = (u8) (disk_block_cnt >> 16);
5013 cdb[12] = (u8) (disk_block_cnt >> 8);
5014 cdb[13] = (u8) (disk_block_cnt);
5015 cdb[14] = 0;
5016 cdb[15] = 0;
5017 cdb_len = 16;
5018 } else {
5019 cdb[0] = is_write ? WRITE_10 : READ_10;
5020 cdb[1] = 0;
5021 cdb[2] = (u8) (disk_block >> 24);
5022 cdb[3] = (u8) (disk_block >> 16);
5023 cdb[4] = (u8) (disk_block >> 8);
5024 cdb[5] = (u8) (disk_block);
5025 cdb[6] = 0;
5026 cdb[7] = (u8) (disk_block_cnt >> 8);
5027 cdb[8] = (u8) (disk_block_cnt);
5028 cdb[9] = 0;
5029 cdb_len = 10;
5030 }
5031 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
Don Brace03383732015-01-23 16:43:30 -06005032 dev->scsi3addr,
5033 dev->phys_disk[map_index]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005034}
5035
Webb Scales25163bd2015-04-23 09:32:00 -05005036/*
5037 * Submit commands down the "normal" RAID stack path
5038 * All callers to hpsa_ciss_submit must check lockup_detected
5039 * beforehand, before (opt.) and after calling cmd_alloc
5040 */
Stephen Cameron574f05d2015-01-23 16:43:20 -06005041static int hpsa_ciss_submit(struct ctlr_info *h,
5042 struct CommandList *c, struct scsi_cmnd *cmd,
5043 unsigned char scsi3addr[])
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005044{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005045 cmd->host_scribble = (unsigned char *) c;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005046 c->cmd_type = CMD_SCSI;
5047 c->scsi_cmd = cmd;
5048 c->Header.ReplyQueue = 0; /* unused in simple mode */
5049 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Bracef2405db2015-01-23 16:43:09 -06005050 c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005051
5052 /* Fill in the request block... */
5053
5054 c->Request.Timeout = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005055 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
5056 c->Request.CDBLen = cmd->cmd_len;
5057 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005058 switch (cmd->sc_data_direction) {
5059 case DMA_TO_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005060 c->Request.type_attr_dir =
5061 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005062 break;
5063 case DMA_FROM_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005064 c->Request.type_attr_dir =
5065 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005066 break;
5067 case DMA_NONE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005068 c->Request.type_attr_dir =
5069 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005070 break;
5071 case DMA_BIDIRECTIONAL:
5072 /* This can happen if a buggy application does a scsi passthru
5073 * and sets both inlen and outlen to non-zero. ( see
5074 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
5075 */
5076
Stephen M. Camerona505b862014-11-14 17:27:04 -06005077 c->Request.type_attr_dir =
5078 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005079 /* This is technically wrong, and hpsa controllers should
5080 * reject it with CMD_INVALID, which is the most correct
5081 * response, but non-fibre backends appear to let it
5082 * slide by, and give the same results as if this field
5083 * were set correctly. Either way is acceptable for
5084 * our purposes here.
5085 */
5086
5087 break;
5088
5089 default:
5090 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
5091 cmd->sc_data_direction);
5092 BUG();
5093 break;
5094 }
5095
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005096 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Webb Scales73153fe2015-04-23 09:35:04 -05005097 hpsa_cmd_resolve_and_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005098 return SCSI_MLQUEUE_HOST_BUSY;
5099 }
5100 enqueue_cmd_and_start_io(h, c);
5101 /* the cmd'll come back via intr handler in complete_scsi_command() */
5102 return 0;
5103}
5104
Stephen Cameron360c73b2015-04-23 09:32:32 -05005105static void hpsa_cmd_init(struct ctlr_info *h, int index,
5106 struct CommandList *c)
5107{
5108 dma_addr_t cmd_dma_handle, err_dma_handle;
5109
5110 /* Zero out all of commandlist except the last field, refcount */
5111 memset(c, 0, offsetof(struct CommandList, refcount));
5112 c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
5113 cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
5114 c->err_info = h->errinfo_pool + index;
5115 memset(c->err_info, 0, sizeof(*c->err_info));
5116 err_dma_handle = h->errinfo_pool_dhandle
5117 + index * sizeof(*c->err_info);
5118 c->cmdindex = index;
5119 c->busaddr = (u32) cmd_dma_handle;
5120 c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
5121 c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
5122 c->h = h;
Webb Scalesa58e7e52015-04-23 09:34:16 -05005123 c->scsi_cmd = SCSI_CMD_IDLE;
Stephen Cameron360c73b2015-04-23 09:32:32 -05005124}
5125
5126static void hpsa_preinitialize_commands(struct ctlr_info *h)
5127{
5128 int i;
5129
5130 for (i = 0; i < h->nr_cmds; i++) {
5131 struct CommandList *c = h->cmd_pool + i;
5132
5133 hpsa_cmd_init(h, i, c);
5134 atomic_set(&c->refcount, 0);
5135 }
5136}
5137
5138static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
5139 struct CommandList *c)
5140{
5141 dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
5142
Webb Scales73153fe2015-04-23 09:35:04 -05005143 BUG_ON(c->cmdindex != index);
5144
Stephen Cameron360c73b2015-04-23 09:32:32 -05005145 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
5146 memset(c->err_info, 0, sizeof(*c->err_info));
5147 c->busaddr = (u32) cmd_dma_handle;
5148}
5149
Webb Scales592a0ad2015-04-23 09:32:48 -05005150static int hpsa_ioaccel_submit(struct ctlr_info *h,
5151 struct CommandList *c, struct scsi_cmnd *cmd,
5152 unsigned char *scsi3addr)
5153{
5154 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
5155 int rc = IO_ACCEL_INELIGIBLE;
5156
5157 cmd->host_scribble = (unsigned char *) c;
5158
5159 if (dev->offload_enabled) {
5160 hpsa_cmd_init(h, c->cmdindex, c);
5161 c->cmd_type = CMD_SCSI;
5162 c->scsi_cmd = cmd;
5163 rc = hpsa_scsi_ioaccel_raid_map(h, c);
5164 if (rc < 0) /* scsi_dma_map failed. */
5165 rc = SCSI_MLQUEUE_HOST_BUSY;
Joe Handzika3144e02015-04-23 09:32:59 -05005166 } else if (dev->hba_ioaccel_enabled) {
Webb Scales592a0ad2015-04-23 09:32:48 -05005167 hpsa_cmd_init(h, c->cmdindex, c);
5168 c->cmd_type = CMD_SCSI;
5169 c->scsi_cmd = cmd;
5170 rc = hpsa_scsi_ioaccel_direct_map(h, c);
5171 if (rc < 0) /* scsi_dma_map failed. */
5172 rc = SCSI_MLQUEUE_HOST_BUSY;
5173 }
5174 return rc;
5175}
5176
Don Brace080ef1c2015-01-23 16:43:25 -06005177static void hpsa_command_resubmit_worker(struct work_struct *work)
5178{
5179 struct scsi_cmnd *cmd;
5180 struct hpsa_scsi_dev_t *dev;
Webb Scales8a0ff922015-04-23 09:34:11 -05005181 struct CommandList *c = container_of(work, struct CommandList, work);
Don Brace080ef1c2015-01-23 16:43:25 -06005182
5183 cmd = c->scsi_cmd;
5184 dev = cmd->device->hostdata;
5185 if (!dev) {
5186 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05005187 return hpsa_cmd_free_and_done(c->h, c, cmd);
Don Brace080ef1c2015-01-23 16:43:25 -06005188 }
Webb Scalesd604f532015-04-23 09:35:22 -05005189 if (c->reset_pending)
5190 return hpsa_cmd_resolve_and_free(c->h, c);
Webb Scalesa58e7e52015-04-23 09:34:16 -05005191 if (c->abort_pending)
5192 return hpsa_cmd_abort_and_free(c->h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05005193 if (c->cmd_type == CMD_IOACCEL2) {
5194 struct ctlr_info *h = c->h;
5195 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5196 int rc;
5197
5198 if (c2->error_data.serv_response ==
5199 IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
5200 rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
5201 if (rc == 0)
5202 return;
5203 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
5204 /*
5205 * If we get here, it means dma mapping failed.
5206 * Try again via scsi mid layer, which will
5207 * then get SCSI_MLQUEUE_HOST_BUSY.
5208 */
5209 cmd->result = DID_IMM_RETRY << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05005210 return hpsa_cmd_free_and_done(h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05005211 }
5212 /* else, fall thru and resubmit down CISS path */
5213 }
5214 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05005215 hpsa_cmd_partial_init(c->h, c->cmdindex, c);
Don Brace080ef1c2015-01-23 16:43:25 -06005216 if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
5217 /*
5218 * If we get here, it means dma mapping failed. Try
5219 * again via scsi mid layer, which will then get
5220 * SCSI_MLQUEUE_HOST_BUSY.
Webb Scales592a0ad2015-04-23 09:32:48 -05005221 *
5222 * hpsa_ciss_submit will have already freed c
5223 * if it encountered a dma mapping failure.
Don Brace080ef1c2015-01-23 16:43:25 -06005224 */
5225 cmd->result = DID_IMM_RETRY << 16;
5226 cmd->scsi_done(cmd);
5227 }
5228}
5229
Stephen Cameron574f05d2015-01-23 16:43:20 -06005230/* Running in struct Scsi_Host->host_lock less mode */
5231static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
5232{
5233 struct ctlr_info *h;
5234 struct hpsa_scsi_dev_t *dev;
5235 unsigned char scsi3addr[8];
5236 struct CommandList *c;
5237 int rc = 0;
5238
5239 /* Get the ptr to our adapter structure out of cmd->host. */
5240 h = sdev_to_hba(cmd->device);
Webb Scales73153fe2015-04-23 09:35:04 -05005241
5242 BUG_ON(cmd->request->tag < 0);
5243
Stephen Cameron574f05d2015-01-23 16:43:20 -06005244 dev = cmd->device->hostdata;
5245 if (!dev) {
5246 cmd->result = DID_NO_CONNECT << 16;
5247 cmd->scsi_done(cmd);
5248 return 0;
5249 }
Webb Scales73153fe2015-04-23 09:35:04 -05005250
Stephen Cameron574f05d2015-01-23 16:43:20 -06005251 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
5252
5253 if (unlikely(lockup_detected(h))) {
Webb Scales25163bd2015-04-23 09:32:00 -05005254 cmd->result = DID_NO_CONNECT << 16;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005255 cmd->scsi_done(cmd);
5256 return 0;
5257 }
Webb Scales73153fe2015-04-23 09:35:04 -05005258 c = cmd_tagged_alloc(h, cmd);
Stephen Cameron574f05d2015-01-23 16:43:20 -06005259
Stephen Cameron407863c2015-01-23 16:44:19 -06005260 /*
5261 * Call alternate submit routine for I/O accelerated commands.
Stephen Cameron574f05d2015-01-23 16:43:20 -06005262 * Retries always go down the normal I/O path.
5263 */
5264 if (likely(cmd->retries == 0 &&
5265 cmd->request->cmd_type == REQ_TYPE_FS &&
5266 h->acciopath_status)) {
Webb Scales592a0ad2015-04-23 09:32:48 -05005267 rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
5268 if (rc == 0)
5269 return 0;
5270 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
Webb Scales73153fe2015-04-23 09:35:04 -05005271 hpsa_cmd_resolve_and_free(h, c);
Webb Scales592a0ad2015-04-23 09:32:48 -05005272 return SCSI_MLQUEUE_HOST_BUSY;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005273 }
5274 }
5275 return hpsa_ciss_submit(h, c, cmd, scsi3addr);
5276}
5277
Webb Scales8ebc9242015-01-23 16:44:50 -06005278static void hpsa_scan_complete(struct ctlr_info *h)
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005279{
5280 unsigned long flags;
5281
Webb Scales8ebc9242015-01-23 16:44:50 -06005282 spin_lock_irqsave(&h->scan_lock, flags);
5283 h->scan_finished = 1;
5284 wake_up_all(&h->scan_wait_queue);
5285 spin_unlock_irqrestore(&h->scan_lock, flags);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005286}
5287
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005288static void hpsa_scan_start(struct Scsi_Host *sh)
5289{
5290 struct ctlr_info *h = shost_to_hba(sh);
5291 unsigned long flags;
5292
Webb Scales8ebc9242015-01-23 16:44:50 -06005293 /*
5294 * Don't let rescans be initiated on a controller known to be locked
5295 * up. If the controller locks up *during* a rescan, that thread is
5296 * probably hosed, but at least we can prevent new rescan threads from
5297 * piling up on a locked up controller.
5298 */
5299 if (unlikely(lockup_detected(h)))
5300 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005301
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005302 /* wait until any scan already in progress is finished. */
5303 while (1) {
5304 spin_lock_irqsave(&h->scan_lock, flags);
5305 if (h->scan_finished)
5306 break;
5307 spin_unlock_irqrestore(&h->scan_lock, flags);
5308 wait_event(h->scan_wait_queue, h->scan_finished);
5309 /* Note: We don't need to worry about a race between this
5310 * thread and driver unload because the midlayer will
5311 * have incremented the reference count, so unload won't
5312 * happen if we're in here.
5313 */
5314 }
5315 h->scan_finished = 0; /* mark scan as in progress */
5316 spin_unlock_irqrestore(&h->scan_lock, flags);
5317
Webb Scales8ebc9242015-01-23 16:44:50 -06005318 if (unlikely(lockup_detected(h)))
5319 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005320
Don Brace8aa60682015-11-04 15:50:01 -06005321 hpsa_update_scsi_devices(h);
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005322
Webb Scales8ebc9242015-01-23 16:44:50 -06005323 hpsa_scan_complete(h);
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005324}
5325
Don Brace7c0a0222015-01-23 16:41:30 -06005326static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
5327{
Don Brace03383732015-01-23 16:43:30 -06005328 struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
5329
5330 if (!logical_drive)
5331 return -ENODEV;
Don Brace7c0a0222015-01-23 16:41:30 -06005332
5333 if (qdepth < 1)
5334 qdepth = 1;
Don Brace03383732015-01-23 16:43:30 -06005335 else if (qdepth > logical_drive->queue_depth)
5336 qdepth = logical_drive->queue_depth;
5337
5338 return scsi_change_queue_depth(sdev, qdepth);
Don Brace7c0a0222015-01-23 16:41:30 -06005339}
5340
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005341static int hpsa_scan_finished(struct Scsi_Host *sh,
5342 unsigned long elapsed_time)
5343{
5344 struct ctlr_info *h = shost_to_hba(sh);
5345 unsigned long flags;
5346 int finished;
5347
5348 spin_lock_irqsave(&h->scan_lock, flags);
5349 finished = h->scan_finished;
5350 spin_unlock_irqrestore(&h->scan_lock, flags);
5351 return finished;
5352}
5353
Robert Elliott2946e822015-04-23 09:35:09 -05005354static int hpsa_scsi_host_alloc(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005355{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005356 struct Scsi_Host *sh;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005357
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005358 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
Robert Elliott2946e822015-04-23 09:35:09 -05005359 if (sh == NULL) {
5360 dev_err(&h->pdev->dev, "scsi_host_alloc failed\n");
5361 return -ENOMEM;
5362 }
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005363
5364 sh->io_port = 0;
5365 sh->n_io_port = 0;
5366 sh->this_id = -1;
5367 sh->max_channel = 3;
5368 sh->max_cmd_len = MAX_COMMAND_SIZE;
5369 sh->max_lun = HPSA_MAX_LUN;
5370 sh->max_id = HPSA_MAX_LUN;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05005371 sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
Don Brace03383732015-01-23 16:43:30 -06005372 sh->cmd_per_lun = sh->can_queue;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005373 sh->sg_tablesize = h->maxsgentries;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06005374 sh->transportt = hpsa_sas_transport_template;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005375 sh->hostdata[0] = (unsigned long) h;
5376 sh->irq = h->intr[h->intr_mode];
5377 sh->unique_id = sh->irq;
Christoph Hellwig64d513a2015-10-08 09:28:04 +01005378
Robert Elliott2946e822015-04-23 09:35:09 -05005379 h->scsi_host = sh;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005380 return 0;
Robert Elliott2946e822015-04-23 09:35:09 -05005381}
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005382
Robert Elliott2946e822015-04-23 09:35:09 -05005383static int hpsa_scsi_add_host(struct ctlr_info *h)
5384{
5385 int rv;
5386
5387 rv = scsi_add_host(h->scsi_host, &h->pdev->dev);
5388 if (rv) {
5389 dev_err(&h->pdev->dev, "scsi_add_host failed\n");
5390 return rv;
5391 }
5392 scsi_scan_host(h->scsi_host);
5393 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005394}
5395
Webb Scalesb69324f2015-04-23 09:34:22 -05005396/*
Webb Scales73153fe2015-04-23 09:35:04 -05005397 * The block layer has already gone to the trouble of picking out a unique,
5398 * small-integer tag for this request. We use an offset from that value as
5399 * an index to select our command block. (The offset allows us to reserve the
5400 * low-numbered entries for our own uses.)
5401 */
5402static int hpsa_get_cmd_index(struct scsi_cmnd *scmd)
5403{
5404 int idx = scmd->request->tag;
5405
5406 if (idx < 0)
5407 return idx;
5408
5409 /* Offset to leave space for internal cmds. */
5410 return idx += HPSA_NRESERVED_CMDS;
5411}
5412
5413/*
Webb Scalesb69324f2015-04-23 09:34:22 -05005414 * Send a TEST_UNIT_READY command to the specified LUN using the specified
5415 * reply queue; returns zero if the unit is ready, and non-zero otherwise.
5416 */
5417static int hpsa_send_test_unit_ready(struct ctlr_info *h,
5418 struct CommandList *c, unsigned char lunaddr[],
5419 int reply_queue)
5420{
5421 int rc;
5422
5423 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
5424 (void) fill_cmd(c, TEST_UNIT_READY, h,
5425 NULL, 0, 0, lunaddr, TYPE_CMD);
5426 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
5427 if (rc)
5428 return rc;
5429 /* no unmap needed here because no data xfer. */
5430
5431 /* Check if the unit is already ready. */
5432 if (c->err_info->CommandStatus == CMD_SUCCESS)
5433 return 0;
5434
5435 /*
5436 * The first command sent after reset will receive "unit attention" to
5437 * indicate that the LUN has been reset...this is actually what we're
5438 * looking for (but, success is good too).
5439 */
5440 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5441 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
5442 (c->err_info->SenseInfo[2] == NO_SENSE ||
5443 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
5444 return 0;
5445
5446 return 1;
5447}
5448
5449/*
5450 * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
5451 * returns zero when the unit is ready, and non-zero when giving up.
5452 */
5453static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
5454 struct CommandList *c,
5455 unsigned char lunaddr[], int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005456{
Tomas Henzl89193582014-02-21 16:25:05 -06005457 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005458 int count = 0;
5459 int waittime = 1; /* seconds */
Webb Scalesb69324f2015-04-23 09:34:22 -05005460
5461 /* Send test unit ready until device ready, or give up. */
5462 for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
5463
5464 /*
5465 * Wait for a bit. do this first, because if we send
5466 * the TUR right away, the reset will just abort it.
5467 */
5468 msleep(1000 * waittime);
5469
5470 rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
5471 if (!rc)
5472 break;
5473
5474 /* Increase wait time with each try, up to a point. */
5475 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
5476 waittime *= 2;
5477
5478 dev_warn(&h->pdev->dev,
5479 "waiting %d secs for device to become ready.\n",
5480 waittime);
5481 }
5482
5483 return rc;
5484}
5485
5486static int wait_for_device_to_become_ready(struct ctlr_info *h,
5487 unsigned char lunaddr[],
5488 int reply_queue)
5489{
5490 int first_queue;
5491 int last_queue;
5492 int rq;
5493 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005494 struct CommandList *c;
5495
Stephen Cameron45fcb862015-01-23 16:43:04 -06005496 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005497
Webb Scalesb69324f2015-04-23 09:34:22 -05005498 /*
5499 * If no specific reply queue was requested, then send the TUR
5500 * repeatedly, requesting a reply on each reply queue; otherwise execute
5501 * the loop exactly once using only the specified queue.
5502 */
5503 if (reply_queue == DEFAULT_REPLY_QUEUE) {
5504 first_queue = 0;
5505 last_queue = h->nreply_queues - 1;
5506 } else {
5507 first_queue = reply_queue;
5508 last_queue = reply_queue;
5509 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005510
Webb Scalesb69324f2015-04-23 09:34:22 -05005511 for (rq = first_queue; rq <= last_queue; rq++) {
5512 rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
Webb Scales25163bd2015-04-23 09:32:00 -05005513 if (rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005514 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005515 }
5516
5517 if (rc)
5518 dev_warn(&h->pdev->dev, "giving up on device.\n");
5519 else
5520 dev_warn(&h->pdev->dev, "device is ready.\n");
5521
Stephen Cameron45fcb862015-01-23 16:43:04 -06005522 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005523 return rc;
5524}
5525
5526/* Need at least one of these error handlers to keep ../scsi/hosts.c from
5527 * complaining. Doing a host- or bus-reset can't do anything good here.
5528 */
5529static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
5530{
5531 int rc;
5532 struct ctlr_info *h;
5533 struct hpsa_scsi_dev_t *dev;
Scott Teel0b9b7b62015-11-04 15:51:02 -06005534 u8 reset_type;
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005535 char msg[48];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005536
5537 /* find the controller to which the command to be aborted was sent */
5538 h = sdev_to_hba(scsicmd->device);
5539 if (h == NULL) /* paranoia */
5540 return FAILED;
Don Bracee3458932015-01-23 16:44:24 -06005541
5542 if (lockup_detected(h))
5543 return FAILED;
5544
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005545 dev = scsicmd->device->hostdata;
5546 if (!dev) {
Webb Scalesd604f532015-04-23 09:35:22 -05005547 dev_err(&h->pdev->dev, "%s: device lookup failed\n", __func__);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005548 return FAILED;
5549 }
Webb Scales25163bd2015-04-23 09:32:00 -05005550
5551 /* if controller locked up, we can guarantee command won't complete */
5552 if (lockup_detected(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005553 snprintf(msg, sizeof(msg),
5554 "cmd %d RESET FAILED, lockup detected",
5555 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005556 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005557 return FAILED;
5558 }
5559
5560 /* this reset request might be the result of a lockup; check */
5561 if (detect_controller_lockup(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005562 snprintf(msg, sizeof(msg),
5563 "cmd %d RESET FAILED, new lockup detected",
5564 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005565 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005566 return FAILED;
5567 }
5568
Webb Scalesd604f532015-04-23 09:35:22 -05005569 /* Do not attempt on controller */
5570 if (is_hba_lunid(dev->scsi3addr))
5571 return SUCCESS;
5572
Scott Teel0b9b7b62015-11-04 15:51:02 -06005573 if (is_logical_dev_addr_mode(dev->scsi3addr))
5574 reset_type = HPSA_DEVICE_RESET_MSG;
5575 else
5576 reset_type = HPSA_PHYS_TARGET_RESET;
5577
5578 sprintf(msg, "resetting %s",
5579 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ");
5580 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005581
Don Braceda03ded2015-11-04 15:50:56 -06005582 h->reset_in_progress = 1;
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06005583
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005584 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teel0b9b7b62015-11-04 15:51:02 -06005585 rc = hpsa_do_reset(h, dev, dev->scsi3addr, reset_type,
Webb Scalesd604f532015-04-23 09:35:22 -05005586 DEFAULT_REPLY_QUEUE);
Scott Teel0b9b7b62015-11-04 15:51:02 -06005587 sprintf(msg, "reset %s %s",
5588 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ",
5589 rc == 0 ? "completed successfully" : "failed");
Webb Scalesd604f532015-04-23 09:35:22 -05005590 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Don Braceda03ded2015-11-04 15:50:56 -06005591 h->reset_in_progress = 0;
Webb Scalesd604f532015-04-23 09:35:22 -05005592 return rc == 0 ? SUCCESS : FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005593}
5594
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005595static void swizzle_abort_tag(u8 *tag)
5596{
5597 u8 original_tag[8];
5598
5599 memcpy(original_tag, tag, 8);
5600 tag[0] = original_tag[3];
5601 tag[1] = original_tag[2];
5602 tag[2] = original_tag[1];
5603 tag[3] = original_tag[0];
5604 tag[4] = original_tag[7];
5605 tag[5] = original_tag[6];
5606 tag[6] = original_tag[5];
5607 tag[7] = original_tag[4];
5608}
5609
Scott Teel17eb87d2014-02-18 13:55:28 -06005610static void hpsa_get_tag(struct ctlr_info *h,
Don Brace2b08b3e2015-01-23 16:41:09 -06005611 struct CommandList *c, __le32 *taglower, __le32 *tagupper)
Scott Teel17eb87d2014-02-18 13:55:28 -06005612{
Don Brace2b08b3e2015-01-23 16:41:09 -06005613 u64 tag;
Scott Teel17eb87d2014-02-18 13:55:28 -06005614 if (c->cmd_type == CMD_IOACCEL1) {
5615 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
5616 &h->ioaccel_cmd_pool[c->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06005617 tag = le64_to_cpu(cm1->tag);
5618 *tagupper = cpu_to_le32(tag >> 32);
5619 *taglower = cpu_to_le32(tag);
Scott Teel54b6e9e2014-02-18 13:56:45 -06005620 return;
Scott Teel17eb87d2014-02-18 13:55:28 -06005621 }
Scott Teel54b6e9e2014-02-18 13:56:45 -06005622 if (c->cmd_type == CMD_IOACCEL2) {
5623 struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
5624 &h->ioaccel2_cmd_pool[c->cmdindex];
Scott Teeldd0e19f2014-02-18 13:57:31 -06005625 /* upper tag not used in ioaccel2 mode */
5626 memset(tagupper, 0, sizeof(*tagupper));
5627 *taglower = cm2->Tag;
Scott Teel54b6e9e2014-02-18 13:56:45 -06005628 return;
5629 }
Don Brace2b08b3e2015-01-23 16:41:09 -06005630 tag = le64_to_cpu(c->Header.tag);
5631 *tagupper = cpu_to_le32(tag >> 32);
5632 *taglower = cpu_to_le32(tag);
Scott Teel17eb87d2014-02-18 13:55:28 -06005633}
5634
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005635static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005636 struct CommandList *abort, int reply_queue)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005637{
5638 int rc = IO_OK;
5639 struct CommandList *c;
5640 struct ErrorInfo *ei;
Don Brace2b08b3e2015-01-23 16:41:09 -06005641 __le32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005642
Stephen Cameron45fcb862015-01-23 16:43:04 -06005643 c = cmd_alloc(h);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005644
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005645 /* fill_cmd can't fail here, no buffer to map */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005646 (void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005647 0, 0, scsi3addr, TYPE_MSG);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005648 if (h->needs_abort_tags_swizzled)
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005649 swizzle_abort_tag(&c->Request.CDB[4]);
Webb Scales25163bd2015-04-23 09:32:00 -05005650 (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
Scott Teel17eb87d2014-02-18 13:55:28 -06005651 hpsa_get_tag(h, abort, &taglower, &tagupper);
Webb Scales25163bd2015-04-23 09:32:00 -05005652 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06005653 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005654 /* no unmap needed here because no data xfer. */
5655
5656 ei = c->err_info;
5657 switch (ei->CommandStatus) {
5658 case CMD_SUCCESS:
5659 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05005660 case CMD_TMF_STATUS:
5661 rc = hpsa_evaluate_tmf_status(h, c);
5662 break;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005663 case CMD_UNABORTABLE: /* Very common, don't make noise. */
5664 rc = -1;
5665 break;
5666 default:
5667 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06005668 __func__, tagupper, taglower);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06005669 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005670 rc = -1;
5671 break;
5672 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06005673 cmd_free(h, c);
Scott Teeldd0e19f2014-02-18 13:57:31 -06005674 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
5675 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005676 return rc;
5677}
5678
Stephen Cameron8be986c2015-04-23 09:34:06 -05005679static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
5680 struct CommandList *command_to_abort, int reply_queue)
5681{
5682 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5683 struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
5684 struct io_accel2_cmd *c2a =
5685 &h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
Webb Scalesa58e7e52015-04-23 09:34:16 -05005686 struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
Stephen Cameron8be986c2015-04-23 09:34:06 -05005687 struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
5688
5689 /*
5690 * We're overlaying struct hpsa_tmf_struct on top of something which
5691 * was allocated as a struct io_accel2_cmd, so we better be sure it
5692 * actually fits, and doesn't overrun the error info space.
5693 */
5694 BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
5695 sizeof(struct io_accel2_cmd));
5696 BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
5697 offsetof(struct hpsa_tmf_struct, error_len) +
5698 sizeof(ac->error_len));
5699
5700 c->cmd_type = IOACCEL2_TMF;
Webb Scalesa58e7e52015-04-23 09:34:16 -05005701 c->scsi_cmd = SCSI_CMD_BUSY;
5702
Stephen Cameron8be986c2015-04-23 09:34:06 -05005703 /* Adjust the DMA address to point to the accelerated command buffer */
5704 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
5705 (c->cmdindex * sizeof(struct io_accel2_cmd));
5706 BUG_ON(c->busaddr & 0x0000007F);
5707
5708 memset(ac, 0, sizeof(*c2)); /* yes this is correct */
5709 ac->iu_type = IOACCEL2_IU_TMF_TYPE;
5710 ac->reply_queue = reply_queue;
5711 ac->tmf = IOACCEL2_TMF_ABORT;
5712 ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
5713 memset(ac->lun_id, 0, sizeof(ac->lun_id));
5714 ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
5715 ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
5716 ac->error_ptr = cpu_to_le64(c->busaddr +
5717 offsetof(struct io_accel2_cmd, error_data));
5718 ac->error_len = cpu_to_le32(sizeof(c2->error_data));
5719}
5720
Scott Teel54b6e9e2014-02-18 13:56:45 -06005721/* ioaccel2 path firmware cannot handle abort task requests.
5722 * Change abort requests to physical target reset, and send to the
5723 * address of the physical disk used for the ioaccel 2 command.
5724 * Return 0 on success (IO_OK)
5725 * -1 on failure
5726 */
5727
5728static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05005729 unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
Scott Teel54b6e9e2014-02-18 13:56:45 -06005730{
5731 int rc = IO_OK;
5732 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
5733 struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
5734 unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
5735 unsigned char *psa = &phys_scsi3addr[0];
5736
5737 /* Get a pointer to the hpsa logical device. */
Stephen Cameron7fa30302015-01-23 16:44:30 -06005738 scmd = abort->scsi_cmd;
Scott Teel54b6e9e2014-02-18 13:56:45 -06005739 dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
5740 if (dev == NULL) {
5741 dev_warn(&h->pdev->dev,
5742 "Cannot abort: no device pointer for command.\n");
5743 return -1; /* not abortable */
5744 }
5745
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005746 if (h->raid_offload_debug > 0)
5747 dev_info(&h->pdev->dev,
Webb Scales0d96ef52015-04-23 09:31:55 -05005748 "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 -06005749 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
Webb Scales0d96ef52015-04-23 09:31:55 -05005750 "Reset as abort",
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005751 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
5752 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
5753
Scott Teel54b6e9e2014-02-18 13:56:45 -06005754 if (!dev->offload_enabled) {
5755 dev_warn(&h->pdev->dev,
5756 "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
5757 return -1; /* not abortable */
5758 }
5759
5760 /* Incoming scsi3addr is logical addr. We need physical disk addr. */
5761 if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
5762 dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
5763 return -1; /* not abortable */
5764 }
5765
5766 /* send the reset */
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005767 if (h->raid_offload_debug > 0)
5768 dev_info(&h->pdev->dev,
5769 "Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5770 psa[0], psa[1], psa[2], psa[3],
5771 psa[4], psa[5], psa[6], psa[7]);
Webb Scalesd604f532015-04-23 09:35:22 -05005772 rc = hpsa_do_reset(h, dev, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
Scott Teel54b6e9e2014-02-18 13:56:45 -06005773 if (rc != 0) {
5774 dev_warn(&h->pdev->dev,
5775 "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5776 psa[0], psa[1], psa[2], psa[3],
5777 psa[4], psa[5], psa[6], psa[7]);
5778 return rc; /* failed to reset */
5779 }
5780
5781 /* wait for device to recover */
Webb Scalesb69324f2015-04-23 09:34:22 -05005782 if (wait_for_device_to_become_ready(h, psa, reply_queue) != 0) {
Scott Teel54b6e9e2014-02-18 13:56:45 -06005783 dev_warn(&h->pdev->dev,
5784 "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5785 psa[0], psa[1], psa[2], psa[3],
5786 psa[4], psa[5], psa[6], psa[7]);
5787 return -1; /* failed to recover */
5788 }
5789
5790 /* device recovered */
5791 dev_info(&h->pdev->dev,
5792 "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5793 psa[0], psa[1], psa[2], psa[3],
5794 psa[4], psa[5], psa[6], psa[7]);
5795
5796 return rc; /* success */
5797}
5798
Stephen Cameron8be986c2015-04-23 09:34:06 -05005799static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
5800 struct CommandList *abort, int reply_queue)
5801{
5802 int rc = IO_OK;
5803 struct CommandList *c;
5804 __le32 taglower, tagupper;
5805 struct hpsa_scsi_dev_t *dev;
5806 struct io_accel2_cmd *c2;
5807
5808 dev = abort->scsi_cmd->device->hostdata;
5809 if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
5810 return -1;
5811
5812 c = cmd_alloc(h);
5813 setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
5814 c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5815 (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
5816 hpsa_get_tag(h, abort, &taglower, &tagupper);
5817 dev_dbg(&h->pdev->dev,
5818 "%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
5819 __func__, tagupper, taglower);
5820 /* no unmap needed here because no data xfer. */
5821
5822 dev_dbg(&h->pdev->dev,
5823 "%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
5824 __func__, tagupper, taglower, c2->error_data.serv_response);
5825 switch (c2->error_data.serv_response) {
5826 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
5827 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
5828 rc = 0;
5829 break;
5830 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
5831 case IOACCEL2_SERV_RESPONSE_FAILURE:
5832 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
5833 rc = -1;
5834 break;
5835 default:
5836 dev_warn(&h->pdev->dev,
5837 "%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
5838 __func__, tagupper, taglower,
5839 c2->error_data.serv_response);
5840 rc = -1;
5841 }
5842 cmd_free(h, c);
5843 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
5844 tagupper, taglower);
5845 return rc;
5846}
5847
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005848static int hpsa_send_abort_both_ways(struct ctlr_info *h,
Don Brace39f3deb2016-02-23 15:16:28 -06005849 struct hpsa_scsi_dev_t *dev, struct CommandList *abort, int reply_queue)
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005850{
Stephen Cameron8be986c2015-04-23 09:34:06 -05005851 /*
5852 * ioccelerator mode 2 commands should be aborted via the
Scott Teel54b6e9e2014-02-18 13:56:45 -06005853 * accelerated path, since RAID path is unaware of these commands,
Stephen Cameron8be986c2015-04-23 09:34:06 -05005854 * but not all underlying firmware can handle abort TMF.
5855 * Change abort to physical device reset when abort TMF is unsupported.
Scott Teel54b6e9e2014-02-18 13:56:45 -06005856 */
Stephen Cameron8be986c2015-04-23 09:34:06 -05005857 if (abort->cmd_type == CMD_IOACCEL2) {
Don Brace39f3deb2016-02-23 15:16:28 -06005858 if ((HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags) ||
5859 dev->physical_device)
Stephen Cameron8be986c2015-04-23 09:34:06 -05005860 return hpsa_send_abort_ioaccel2(h, abort,
5861 reply_queue);
5862 else
Don Brace39f3deb2016-02-23 15:16:28 -06005863 return hpsa_send_reset_as_abort_ioaccel2(h,
5864 dev->scsi3addr,
Webb Scales25163bd2015-04-23 09:32:00 -05005865 abort, reply_queue);
Stephen Cameron8be986c2015-04-23 09:34:06 -05005866 }
Don Brace39f3deb2016-02-23 15:16:28 -06005867 return hpsa_send_abort(h, dev->scsi3addr, abort, reply_queue);
Webb Scales25163bd2015-04-23 09:32:00 -05005868}
5869
5870/* Find out which reply queue a command was meant to return on */
5871static int hpsa_extract_reply_queue(struct ctlr_info *h,
5872 struct CommandList *c)
5873{
5874 if (c->cmd_type == CMD_IOACCEL2)
5875 return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
5876 return c->Header.ReplyQueue;
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005877}
5878
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005879/*
5880 * Limit concurrency of abort commands to prevent
5881 * over-subscription of commands
5882 */
5883static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
5884{
5885#define ABORT_CMD_WAIT_MSECS 5000
5886 return !wait_event_timeout(h->abort_cmd_wait_queue,
5887 atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
5888 msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
5889}
5890
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005891/* Send an abort for the specified command.
5892 * If the device and controller support it,
5893 * send a task abort request.
5894 */
5895static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
5896{
5897
Webb Scalesa58e7e52015-04-23 09:34:16 -05005898 int rc;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005899 struct ctlr_info *h;
5900 struct hpsa_scsi_dev_t *dev;
5901 struct CommandList *abort; /* pointer to command to be aborted */
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005902 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
5903 char msg[256]; /* For debug messaging. */
5904 int ml = 0;
Don Brace2b08b3e2015-01-23 16:41:09 -06005905 __le32 tagupper, taglower;
Webb Scales25163bd2015-04-23 09:32:00 -05005906 int refcount, reply_queue;
5907
5908 if (sc == NULL)
5909 return FAILED;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005910
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005911 if (sc->device == NULL)
5912 return FAILED;
5913
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005914 /* Find the controller of the command to be aborted */
5915 h = sdev_to_hba(sc->device);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005916 if (h == NULL)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005917 return FAILED;
5918
Webb Scales25163bd2015-04-23 09:32:00 -05005919 /* Find the device of the command to be aborted */
5920 dev = sc->device->hostdata;
5921 if (!dev) {
5922 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
5923 msg);
Don Bracee3458932015-01-23 16:44:24 -06005924 return FAILED;
Webb Scales25163bd2015-04-23 09:32:00 -05005925 }
5926
5927 /* If controller locked up, we can guarantee command won't complete */
5928 if (lockup_detected(h)) {
5929 hpsa_show_dev_msg(KERN_WARNING, h, dev,
5930 "ABORT FAILED, lockup detected");
5931 return FAILED;
5932 }
5933
5934 /* This is a good time to check if controller lockup has occurred */
5935 if (detect_controller_lockup(h)) {
5936 hpsa_show_dev_msg(KERN_WARNING, h, dev,
5937 "ABORT FAILED, new lockup detected");
5938 return FAILED;
5939 }
Don Bracee3458932015-01-23 16:44:24 -06005940
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005941 /* Check that controller supports some kind of task abort */
5942 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
5943 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
5944 return FAILED;
5945
5946 memset(msg, 0, sizeof(msg));
Robert Elliott4b761552015-04-23 09:33:54 -05005947 ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005948 h->scsi_host->host_no, sc->device->channel,
Webb Scales0d96ef52015-04-23 09:31:55 -05005949 sc->device->id, sc->device->lun,
Robert Elliott4b761552015-04-23 09:33:54 -05005950 "Aborting command", sc);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005951
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005952 /* Get SCSI command to be aborted */
5953 abort = (struct CommandList *) sc->host_scribble;
5954 if (abort == NULL) {
Webb Scales281a7fd2015-01-23 16:43:35 -06005955 /* This can happen if the command already completed. */
5956 return SUCCESS;
5957 }
5958 refcount = atomic_inc_return(&abort->refcount);
5959 if (refcount == 1) { /* Command is done already. */
5960 cmd_free(h, abort);
5961 return SUCCESS;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005962 }
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005963
5964 /* Don't bother trying the abort if we know it won't work. */
5965 if (abort->cmd_type != CMD_IOACCEL2 &&
5966 abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
5967 cmd_free(h, abort);
5968 return FAILED;
5969 }
5970
Webb Scalesa58e7e52015-04-23 09:34:16 -05005971 /*
5972 * Check that we're aborting the right command.
5973 * It's possible the CommandList already completed and got re-used.
5974 */
5975 if (abort->scsi_cmd != sc) {
5976 cmd_free(h, abort);
5977 return SUCCESS;
5978 }
5979
5980 abort->abort_pending = true;
Scott Teel17eb87d2014-02-18 13:55:28 -06005981 hpsa_get_tag(h, abort, &taglower, &tagupper);
Webb Scales25163bd2015-04-23 09:32:00 -05005982 reply_queue = hpsa_extract_reply_queue(h, abort);
Scott Teel17eb87d2014-02-18 13:55:28 -06005983 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen Cameron7fa30302015-01-23 16:44:30 -06005984 as = abort->scsi_cmd;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005985 if (as != NULL)
Robert Elliott4b761552015-04-23 09:33:54 -05005986 ml += sprintf(msg+ml,
5987 "CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
5988 as->cmd_len, as->cmnd[0], as->cmnd[1],
5989 as->serial_number);
5990 dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
Webb Scales0d96ef52015-04-23 09:31:55 -05005991 hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
Robert Elliott4b761552015-04-23 09:33:54 -05005992
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005993 /*
5994 * Command is in flight, or possibly already completed
5995 * by the firmware (but not to the scsi mid layer) but we can't
5996 * distinguish which. Send the abort down.
5997 */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005998 if (wait_for_available_abort_cmd(h)) {
5999 dev_warn(&h->pdev->dev,
Robert Elliott4b761552015-04-23 09:33:54 -05006000 "%s FAILED, timeout waiting for an abort command to become available.\n",
6001 msg);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006002 cmd_free(h, abort);
6003 return FAILED;
6004 }
Don Brace39f3deb2016-02-23 15:16:28 -06006005 rc = hpsa_send_abort_both_ways(h, dev, abort, reply_queue);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006006 atomic_inc(&h->abort_cmds_available);
6007 wake_up_all(&h->abort_cmd_wait_queue);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006008 if (rc != 0) {
Robert Elliott4b761552015-04-23 09:33:54 -05006009 dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
Webb Scales0d96ef52015-04-23 09:31:55 -05006010 hpsa_show_dev_msg(KERN_WARNING, h, dev,
Robert Elliott4b761552015-04-23 09:33:54 -05006011 "FAILED to abort command");
Webb Scales281a7fd2015-01-23 16:43:35 -06006012 cmd_free(h, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006013 return FAILED;
6014 }
Robert Elliott4b761552015-04-23 09:33:54 -05006015 dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
Webb Scalesd604f532015-04-23 09:35:22 -05006016 wait_event(h->event_sync_wait_queue,
Webb Scalesa58e7e52015-04-23 09:34:16 -05006017 abort->scsi_cmd != sc || lockup_detected(h));
Webb Scales281a7fd2015-01-23 16:43:35 -06006018 cmd_free(h, abort);
Webb Scalesa58e7e52015-04-23 09:34:16 -05006019 return !lockup_detected(h) ? SUCCESS : FAILED;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006020}
6021
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006022/*
Webb Scales73153fe2015-04-23 09:35:04 -05006023 * For operations with an associated SCSI command, a command block is allocated
6024 * at init, and managed by cmd_tagged_alloc() and cmd_tagged_free() using the
6025 * block request tag as an index into a table of entries. cmd_tagged_free() is
6026 * the complement, although cmd_free() may be called instead.
6027 */
6028static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
6029 struct scsi_cmnd *scmd)
6030{
6031 int idx = hpsa_get_cmd_index(scmd);
6032 struct CommandList *c = h->cmd_pool + idx;
6033
6034 if (idx < HPSA_NRESERVED_CMDS || idx >= h->nr_cmds) {
6035 dev_err(&h->pdev->dev, "Bad block tag: %d not in [%d..%d]\n",
6036 idx, HPSA_NRESERVED_CMDS, h->nr_cmds - 1);
6037 /* The index value comes from the block layer, so if it's out of
6038 * bounds, it's probably not our bug.
6039 */
6040 BUG();
6041 }
6042
6043 atomic_inc(&c->refcount);
6044 if (unlikely(!hpsa_is_cmd_idle(c))) {
6045 /*
6046 * We expect that the SCSI layer will hand us a unique tag
6047 * value. Thus, there should never be a collision here between
6048 * two requests...because if the selected command isn't idle
6049 * then someone is going to be very disappointed.
6050 */
6051 dev_err(&h->pdev->dev,
6052 "tag collision (tag=%d) in cmd_tagged_alloc().\n",
6053 idx);
6054 if (c->scsi_cmd != NULL)
6055 scsi_print_command(c->scsi_cmd);
6056 scsi_print_command(scmd);
6057 }
6058
6059 hpsa_cmd_partial_init(h, idx, c);
6060 return c;
6061}
6062
6063static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c)
6064{
6065 /*
6066 * Release our reference to the block. We don't need to do anything
6067 * else to free it, because it is accessed by index. (There's no point
6068 * in checking the result of the decrement, since we cannot guarantee
6069 * that there isn't a concurrent abort which is also accessing it.)
6070 */
6071 (void)atomic_dec(&c->refcount);
6072}
6073
6074/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006075 * For operations that cannot sleep, a command block is allocated at init,
6076 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
6077 * which ones are free or in use. Lock must be held when calling this.
6078 * cmd_free() is the complement.
Robert Elliottbf43caf2015-04-23 09:33:38 -05006079 * This function never gives up and returns NULL. If it hangs,
6080 * another thread must call cmd_free() to free some tags.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006081 */
Webb Scales281a7fd2015-01-23 16:43:35 -06006082
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006083static struct CommandList *cmd_alloc(struct ctlr_info *h)
6084{
6085 struct CommandList *c;
Stephen Cameron360c73b2015-04-23 09:32:32 -05006086 int refcount, i;
Webb Scales73153fe2015-04-23 09:35:04 -05006087 int offset = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006088
Robert Elliott33811022015-01-23 16:43:41 -06006089 /*
6090 * There is some *extremely* small but non-zero chance that that
Stephen M. Cameron4c413122014-11-14 17:27:29 -06006091 * multiple threads could get in here, and one thread could
6092 * be scanning through the list of bits looking for a free
6093 * one, but the free ones are always behind him, and other
6094 * threads sneak in behind him and eat them before he can
6095 * get to them, so that while there is always a free one, a
6096 * very unlucky thread might be starved anyway, never able to
6097 * beat the other threads. In reality, this happens so
6098 * infrequently as to be indistinguishable from never.
Webb Scales73153fe2015-04-23 09:35:04 -05006099 *
6100 * Note that we start allocating commands before the SCSI host structure
6101 * is initialized. Since the search starts at bit zero, this
6102 * all works, since we have at least one command structure available;
6103 * however, it means that the structures with the low indexes have to be
6104 * reserved for driver-initiated requests, while requests from the block
6105 * layer will use the higher indexes.
Stephen M. Cameron4c413122014-11-14 17:27:29 -06006106 */
6107
Webb Scales281a7fd2015-01-23 16:43:35 -06006108 for (;;) {
Webb Scales73153fe2015-04-23 09:35:04 -05006109 i = find_next_zero_bit(h->cmd_pool_bits,
6110 HPSA_NRESERVED_CMDS,
6111 offset);
6112 if (unlikely(i >= HPSA_NRESERVED_CMDS)) {
Webb Scales281a7fd2015-01-23 16:43:35 -06006113 offset = 0;
6114 continue;
6115 }
6116 c = h->cmd_pool + i;
6117 refcount = atomic_inc_return(&c->refcount);
6118 if (unlikely(refcount > 1)) {
6119 cmd_free(h, c); /* already in use */
Webb Scales73153fe2015-04-23 09:35:04 -05006120 offset = (i + 1) % HPSA_NRESERVED_CMDS;
Webb Scales281a7fd2015-01-23 16:43:35 -06006121 continue;
6122 }
6123 set_bit(i & (BITS_PER_LONG - 1),
6124 h->cmd_pool_bits + (i / BITS_PER_LONG));
6125 break; /* it's ours now. */
6126 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05006127 hpsa_cmd_partial_init(h, i, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006128 return c;
6129}
6130
Webb Scales73153fe2015-04-23 09:35:04 -05006131/*
6132 * This is the complementary operation to cmd_alloc(). Note, however, in some
6133 * corner cases it may also be used to free blocks allocated by
6134 * cmd_tagged_alloc() in which case the ref-count decrement does the trick and
6135 * the clear-bit is harmless.
6136 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006137static void cmd_free(struct ctlr_info *h, struct CommandList *c)
6138{
Webb Scales281a7fd2015-01-23 16:43:35 -06006139 if (atomic_dec_and_test(&c->refcount)) {
6140 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006141
Webb Scales281a7fd2015-01-23 16:43:35 -06006142 i = c - h->cmd_pool;
6143 clear_bit(i & (BITS_PER_LONG - 1),
6144 h->cmd_pool_bits + (i / BITS_PER_LONG));
6145 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006146}
6147
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006148#ifdef CONFIG_COMPAT
6149
Don Brace42a91642014-11-14 17:26:27 -06006150static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
6151 void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006152{
6153 IOCTL32_Command_struct __user *arg32 =
6154 (IOCTL32_Command_struct __user *) arg;
6155 IOCTL_Command_struct arg64;
6156 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
6157 int err;
6158 u32 cp;
6159
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06006160 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006161 err = 0;
6162 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
6163 sizeof(arg64.LUN_info));
6164 err |= copy_from_user(&arg64.Request, &arg32->Request,
6165 sizeof(arg64.Request));
6166 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
6167 sizeof(arg64.error_info));
6168 err |= get_user(arg64.buf_size, &arg32->buf_size);
6169 err |= get_user(cp, &arg32->buf);
6170 arg64.buf = compat_ptr(cp);
6171 err |= copy_to_user(p, &arg64, sizeof(arg64));
6172
6173 if (err)
6174 return -EFAULT;
6175
Don Brace42a91642014-11-14 17:26:27 -06006176 err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006177 if (err)
6178 return err;
6179 err |= copy_in_user(&arg32->error_info, &p->error_info,
6180 sizeof(arg32->error_info));
6181 if (err)
6182 return -EFAULT;
6183 return err;
6184}
6185
6186static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
Don Brace42a91642014-11-14 17:26:27 -06006187 int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006188{
6189 BIG_IOCTL32_Command_struct __user *arg32 =
6190 (BIG_IOCTL32_Command_struct __user *) arg;
6191 BIG_IOCTL_Command_struct arg64;
6192 BIG_IOCTL_Command_struct __user *p =
6193 compat_alloc_user_space(sizeof(arg64));
6194 int err;
6195 u32 cp;
6196
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06006197 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006198 err = 0;
6199 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
6200 sizeof(arg64.LUN_info));
6201 err |= copy_from_user(&arg64.Request, &arg32->Request,
6202 sizeof(arg64.Request));
6203 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
6204 sizeof(arg64.error_info));
6205 err |= get_user(arg64.buf_size, &arg32->buf_size);
6206 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
6207 err |= get_user(cp, &arg32->buf);
6208 arg64.buf = compat_ptr(cp);
6209 err |= copy_to_user(p, &arg64, sizeof(arg64));
6210
6211 if (err)
6212 return -EFAULT;
6213
Don Brace42a91642014-11-14 17:26:27 -06006214 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006215 if (err)
6216 return err;
6217 err |= copy_in_user(&arg32->error_info, &p->error_info,
6218 sizeof(arg32->error_info));
6219 if (err)
6220 return -EFAULT;
6221 return err;
6222}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06006223
Don Brace42a91642014-11-14 17:26:27 -06006224static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06006225{
6226 switch (cmd) {
6227 case CCISS_GETPCIINFO:
6228 case CCISS_GETINTINFO:
6229 case CCISS_SETINTINFO:
6230 case CCISS_GETNODENAME:
6231 case CCISS_SETNODENAME:
6232 case CCISS_GETHEARTBEAT:
6233 case CCISS_GETBUSTYPES:
6234 case CCISS_GETFIRMVER:
6235 case CCISS_GETDRIVVER:
6236 case CCISS_REVALIDVOLS:
6237 case CCISS_DEREGDISK:
6238 case CCISS_REGNEWDISK:
6239 case CCISS_REGNEWD:
6240 case CCISS_RESCANDISK:
6241 case CCISS_GETLUNINFO:
6242 return hpsa_ioctl(dev, cmd, arg);
6243
6244 case CCISS_PASSTHRU32:
6245 return hpsa_ioctl32_passthru(dev, cmd, arg);
6246 case CCISS_BIG_PASSTHRU32:
6247 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
6248
6249 default:
6250 return -ENOIOCTLCMD;
6251 }
6252}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006253#endif
6254
6255static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
6256{
6257 struct hpsa_pci_info pciinfo;
6258
6259 if (!argp)
6260 return -EINVAL;
6261 pciinfo.domain = pci_domain_nr(h->pdev->bus);
6262 pciinfo.bus = h->pdev->bus->number;
6263 pciinfo.dev_fn = h->pdev->devfn;
6264 pciinfo.board_id = h->board_id;
6265 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
6266 return -EFAULT;
6267 return 0;
6268}
6269
6270static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
6271{
6272 DriverVer_type DriverVer;
6273 unsigned char vmaj, vmin, vsubmin;
6274 int rc;
6275
6276 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
6277 &vmaj, &vmin, &vsubmin);
6278 if (rc != 3) {
6279 dev_info(&h->pdev->dev, "driver version string '%s' "
6280 "unrecognized.", HPSA_DRIVER_VERSION);
6281 vmaj = 0;
6282 vmin = 0;
6283 vsubmin = 0;
6284 }
6285 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
6286 if (!argp)
6287 return -EINVAL;
6288 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
6289 return -EFAULT;
6290 return 0;
6291}
6292
6293static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6294{
6295 IOCTL_Command_struct iocommand;
6296 struct CommandList *c;
6297 char *buff = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006298 u64 temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006299 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006300
6301 if (!argp)
6302 return -EINVAL;
6303 if (!capable(CAP_SYS_RAWIO))
6304 return -EPERM;
6305 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
6306 return -EFAULT;
6307 if ((iocommand.buf_size < 1) &&
6308 (iocommand.Request.Type.Direction != XFER_NONE)) {
6309 return -EINVAL;
6310 }
6311 if (iocommand.buf_size > 0) {
6312 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
6313 if (buff == NULL)
Robert Elliott2dd02d72015-04-23 09:33:43 -05006314 return -ENOMEM;
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006315 if (iocommand.Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006316 /* Copy the data into the buffer we created */
6317 if (copy_from_user(buff, iocommand.buf,
6318 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006319 rc = -EFAULT;
6320 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006321 }
6322 } else {
6323 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006324 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006325 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006326 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006327
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006328 /* Fill in the command type */
6329 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006330 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006331 /* Fill in Command Header */
6332 c->Header.ReplyQueue = 0; /* unused in simple mode */
6333 if (iocommand.buf_size > 0) { /* buffer to fill */
6334 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006335 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006336 } else { /* no buffers to fill */
6337 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006338 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006339 }
6340 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006341
6342 /* Fill in Request block */
6343 memcpy(&c->Request, &iocommand.Request,
6344 sizeof(c->Request));
6345
6346 /* Fill in the scatter gather information */
6347 if (iocommand.buf_size > 0) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006348 temp64 = pci_map_single(h->pdev, buff,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006349 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006350 if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
6351 c->SG[0].Addr = cpu_to_le64(0);
6352 c->SG[0].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006353 rc = -ENOMEM;
6354 goto out;
6355 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006356 c->SG[0].Addr = cpu_to_le64(temp64);
6357 c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
6358 c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006359 }
Webb Scales25163bd2015-04-23 09:32:00 -05006360 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05006361 if (iocommand.buf_size > 0)
6362 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006363 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006364 if (rc) {
6365 rc = -EIO;
6366 goto out;
6367 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006368
6369 /* Copy the error information out */
6370 memcpy(&iocommand.error_info, c->err_info,
6371 sizeof(iocommand.error_info));
6372 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006373 rc = -EFAULT;
6374 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006375 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006376 if ((iocommand.Request.Type.Direction & XFER_READ) &&
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006377 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006378 /* Copy the data out of the buffer we created */
6379 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006380 rc = -EFAULT;
6381 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006382 }
6383 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006384out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006385 cmd_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006386out_kfree:
6387 kfree(buff);
6388 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006389}
6390
6391static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6392{
6393 BIG_IOCTL_Command_struct *ioc;
6394 struct CommandList *c;
6395 unsigned char **buff = NULL;
6396 int *buff_size = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006397 u64 temp64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006398 BYTE sg_used = 0;
6399 int status = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06006400 u32 left;
6401 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006402 BYTE __user *data_ptr;
6403
6404 if (!argp)
6405 return -EINVAL;
6406 if (!capable(CAP_SYS_RAWIO))
6407 return -EPERM;
6408 ioc = (BIG_IOCTL_Command_struct *)
6409 kmalloc(sizeof(*ioc), GFP_KERNEL);
6410 if (!ioc) {
6411 status = -ENOMEM;
6412 goto cleanup1;
6413 }
6414 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
6415 status = -EFAULT;
6416 goto cleanup1;
6417 }
6418 if ((ioc->buf_size < 1) &&
6419 (ioc->Request.Type.Direction != XFER_NONE)) {
6420 status = -EINVAL;
6421 goto cleanup1;
6422 }
6423 /* Check kmalloc limits using all SGs */
6424 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
6425 status = -EINVAL;
6426 goto cleanup1;
6427 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006428 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006429 status = -EINVAL;
6430 goto cleanup1;
6431 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006432 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006433 if (!buff) {
6434 status = -ENOMEM;
6435 goto cleanup1;
6436 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006437 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006438 if (!buff_size) {
6439 status = -ENOMEM;
6440 goto cleanup1;
6441 }
6442 left = ioc->buf_size;
6443 data_ptr = ioc->buf;
6444 while (left) {
6445 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
6446 buff_size[sg_used] = sz;
6447 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
6448 if (buff[sg_used] == NULL) {
6449 status = -ENOMEM;
6450 goto cleanup1;
6451 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006452 if (ioc->Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006453 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
Stephen M. Cameron0758f4f2014-07-03 10:18:03 -05006454 status = -EFAULT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006455 goto cleanup1;
6456 }
6457 } else
6458 memset(buff[sg_used], 0, sz);
6459 left -= sz;
6460 data_ptr += sz;
6461 sg_used++;
6462 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006463 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006464
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006465 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006466 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006467 c->Header.ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006468 c->Header.SGList = (u8) sg_used;
6469 c->Header.SGTotal = cpu_to_le16(sg_used);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006470 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006471 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
6472 if (ioc->buf_size > 0) {
6473 int i;
6474 for (i = 0; i < sg_used; i++) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006475 temp64 = pci_map_single(h->pdev, buff[i],
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006476 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006477 if (dma_mapping_error(&h->pdev->dev,
6478 (dma_addr_t) temp64)) {
6479 c->SG[i].Addr = cpu_to_le64(0);
6480 c->SG[i].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006481 hpsa_pci_unmap(h->pdev, c, i,
6482 PCI_DMA_BIDIRECTIONAL);
6483 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006484 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006485 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006486 c->SG[i].Addr = cpu_to_le64(temp64);
6487 c->SG[i].Len = cpu_to_le32(buff_size[i]);
6488 c->SG[i].Ext = cpu_to_le32(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006489 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006490 c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006491 }
Webb Scales25163bd2015-04-23 09:32:00 -05006492 status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006493 if (sg_used)
6494 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006495 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006496 if (status) {
6497 status = -EIO;
6498 goto cleanup0;
6499 }
6500
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006501 /* Copy the error information out */
6502 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
6503 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006504 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006505 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006506 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006507 if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006508 int i;
6509
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006510 /* Copy the data out of the buffer we created */
6511 BYTE __user *ptr = ioc->buf;
6512 for (i = 0; i < sg_used; i++) {
6513 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006514 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006515 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006516 }
6517 ptr += buff_size[i];
6518 }
6519 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006520 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006521cleanup0:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006522 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006523cleanup1:
6524 if (buff) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006525 int i;
6526
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006527 for (i = 0; i < sg_used; i++)
6528 kfree(buff[i]);
6529 kfree(buff);
6530 }
6531 kfree(buff_size);
6532 kfree(ioc);
6533 return status;
6534}
6535
6536static void check_ioctl_unit_attention(struct ctlr_info *h,
6537 struct CommandList *c)
6538{
6539 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
6540 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
6541 (void) check_for_unit_attention(h, c);
6542}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006543
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006544/*
6545 * ioctl
6546 */
Don Brace42a91642014-11-14 17:26:27 -06006547static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006548{
6549 struct ctlr_info *h;
6550 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006551 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006552
6553 h = sdev_to_hba(dev);
6554
6555 switch (cmd) {
6556 case CCISS_DEREGDISK:
6557 case CCISS_REGNEWDISK:
6558 case CCISS_REGNEWD:
Stephen M. Camerona08a8472010-02-04 08:43:16 -06006559 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006560 return 0;
6561 case CCISS_GETPCIINFO:
6562 return hpsa_getpciinfo_ioctl(h, argp);
6563 case CCISS_GETDRIVVER:
6564 return hpsa_getdrivver_ioctl(h, argp);
6565 case CCISS_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006566 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006567 return -EAGAIN;
6568 rc = hpsa_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006569 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006570 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006571 case CCISS_BIG_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006572 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006573 return -EAGAIN;
6574 rc = hpsa_big_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006575 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006576 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006577 default:
6578 return -ENOTTY;
6579 }
6580}
6581
Robert Elliottbf43caf2015-04-23 09:33:38 -05006582static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006583 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006584{
6585 struct CommandList *c;
6586
6587 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006588
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006589 /* fill_cmd can't fail here, no data buffer to map */
6590 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006591 RAID_CTLR_LUNID, TYPE_MSG);
6592 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
6593 c->waiting = NULL;
6594 enqueue_cmd_and_start_io(h, c);
6595 /* Don't wait for completion, the reset won't complete. Don't free
6596 * the command either. This is the last command we will send before
6597 * re-initializing everything, so it doesn't matter and won't leak.
6598 */
Robert Elliottbf43caf2015-04-23 09:33:38 -05006599 return;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006600}
6601
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006602static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006603 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006604 int cmd_type)
6605{
6606 int pci_dir = XFER_NONE;
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006607 u64 tag; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006608
6609 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006610 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006611 c->Header.ReplyQueue = 0;
6612 if (buff != NULL && size > 0) {
6613 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006614 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006615 } else {
6616 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006617 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006618 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006619 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
6620
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006621 if (cmd_type == TYPE_CMD) {
6622 switch (cmd) {
6623 case HPSA_INQUIRY:
6624 /* are we trying to read a vital product page */
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006625 if (page_code & VPD_PAGE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006626 c->Request.CDB[1] = 0x01;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006627 c->Request.CDB[2] = (page_code & 0xff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006628 }
6629 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006630 c->Request.type_attr_dir =
6631 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006632 c->Request.Timeout = 0;
6633 c->Request.CDB[0] = HPSA_INQUIRY;
6634 c->Request.CDB[4] = size & 0xFF;
6635 break;
6636 case HPSA_REPORT_LOG:
6637 case HPSA_REPORT_PHYS:
6638 /* Talking to controller so It's a physical command
6639 mode = 00 target = 0. Nothing to write.
6640 */
6641 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006642 c->Request.type_attr_dir =
6643 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006644 c->Request.Timeout = 0;
6645 c->Request.CDB[0] = cmd;
6646 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6647 c->Request.CDB[7] = (size >> 16) & 0xFF;
6648 c->Request.CDB[8] = (size >> 8) & 0xFF;
6649 c->Request.CDB[9] = size & 0xFF;
6650 break;
Scott Teelc2adae42015-11-04 15:52:16 -06006651 case BMIC_SENSE_DIAG_OPTIONS:
6652 c->Request.CDBLen = 16;
6653 c->Request.type_attr_dir =
6654 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6655 c->Request.Timeout = 0;
6656 /* Spec says this should be BMIC_WRITE */
6657 c->Request.CDB[0] = BMIC_READ;
6658 c->Request.CDB[6] = BMIC_SENSE_DIAG_OPTIONS;
6659 break;
6660 case BMIC_SET_DIAG_OPTIONS:
6661 c->Request.CDBLen = 16;
6662 c->Request.type_attr_dir =
6663 TYPE_ATTR_DIR(cmd_type,
6664 ATTR_SIMPLE, XFER_WRITE);
6665 c->Request.Timeout = 0;
6666 c->Request.CDB[0] = BMIC_WRITE;
6667 c->Request.CDB[6] = BMIC_SET_DIAG_OPTIONS;
6668 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006669 case HPSA_CACHE_FLUSH:
6670 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006671 c->Request.type_attr_dir =
6672 TYPE_ATTR_DIR(cmd_type,
6673 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006674 c->Request.Timeout = 0;
6675 c->Request.CDB[0] = BMIC_WRITE;
6676 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05006677 c->Request.CDB[7] = (size >> 8) & 0xFF;
6678 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006679 break;
6680 case TEST_UNIT_READY:
6681 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006682 c->Request.type_attr_dir =
6683 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006684 c->Request.Timeout = 0;
6685 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006686 case HPSA_GET_RAID_MAP:
6687 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006688 c->Request.type_attr_dir =
6689 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006690 c->Request.Timeout = 0;
6691 c->Request.CDB[0] = HPSA_CISS_READ;
6692 c->Request.CDB[1] = cmd;
6693 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6694 c->Request.CDB[7] = (size >> 16) & 0xFF;
6695 c->Request.CDB[8] = (size >> 8) & 0xFF;
6696 c->Request.CDB[9] = size & 0xFF;
6697 break;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006698 case BMIC_SENSE_CONTROLLER_PARAMETERS:
6699 c->Request.CDBLen = 10;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006700 c->Request.type_attr_dir =
6701 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006702 c->Request.Timeout = 0;
6703 c->Request.CDB[0] = BMIC_READ;
6704 c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
6705 c->Request.CDB[7] = (size >> 16) & 0xFF;
6706 c->Request.CDB[8] = (size >> 8) & 0xFF;
6707 break;
Don Brace03383732015-01-23 16:43:30 -06006708 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
6709 c->Request.CDBLen = 10;
6710 c->Request.type_attr_dir =
6711 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6712 c->Request.Timeout = 0;
6713 c->Request.CDB[0] = BMIC_READ;
6714 c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
6715 c->Request.CDB[7] = (size >> 16) & 0xFF;
6716 c->Request.CDB[8] = (size >> 8) & 0XFF;
6717 break;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06006718 case BMIC_SENSE_SUBSYSTEM_INFORMATION:
6719 c->Request.CDBLen = 10;
6720 c->Request.type_attr_dir =
6721 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6722 c->Request.Timeout = 0;
6723 c->Request.CDB[0] = BMIC_READ;
6724 c->Request.CDB[6] = BMIC_SENSE_SUBSYSTEM_INFORMATION;
6725 c->Request.CDB[7] = (size >> 16) & 0xFF;
6726 c->Request.CDB[8] = (size >> 8) & 0XFF;
6727 break;
Don Bracecca8f132015-12-22 10:36:48 -06006728 case BMIC_SENSE_STORAGE_BOX_PARAMS:
6729 c->Request.CDBLen = 10;
6730 c->Request.type_attr_dir =
6731 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6732 c->Request.Timeout = 0;
6733 c->Request.CDB[0] = BMIC_READ;
6734 c->Request.CDB[6] = BMIC_SENSE_STORAGE_BOX_PARAMS;
6735 c->Request.CDB[7] = (size >> 16) & 0xFF;
6736 c->Request.CDB[8] = (size >> 8) & 0XFF;
6737 break;
Scott Teel66749d02015-11-04 15:51:57 -06006738 case BMIC_IDENTIFY_CONTROLLER:
6739 c->Request.CDBLen = 10;
6740 c->Request.type_attr_dir =
6741 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6742 c->Request.Timeout = 0;
6743 c->Request.CDB[0] = BMIC_READ;
6744 c->Request.CDB[1] = 0;
6745 c->Request.CDB[2] = 0;
6746 c->Request.CDB[3] = 0;
6747 c->Request.CDB[4] = 0;
6748 c->Request.CDB[5] = 0;
6749 c->Request.CDB[6] = BMIC_IDENTIFY_CONTROLLER;
6750 c->Request.CDB[7] = (size >> 16) & 0xFF;
6751 c->Request.CDB[8] = (size >> 8) & 0XFF;
6752 c->Request.CDB[9] = 0;
6753 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006754 default:
6755 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
6756 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006757 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006758 }
6759 } else if (cmd_type == TYPE_MSG) {
6760 switch (cmd) {
6761
Scott Teel0b9b7b62015-11-04 15:51:02 -06006762 case HPSA_PHYS_TARGET_RESET:
6763 c->Request.CDBLen = 16;
6764 c->Request.type_attr_dir =
6765 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6766 c->Request.Timeout = 0; /* Don't time out */
6767 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
6768 c->Request.CDB[0] = HPSA_RESET;
6769 c->Request.CDB[1] = HPSA_TARGET_RESET_TYPE;
6770 /* Physical target reset needs no control bytes 4-7*/
6771 c->Request.CDB[4] = 0x00;
6772 c->Request.CDB[5] = 0x00;
6773 c->Request.CDB[6] = 0x00;
6774 c->Request.CDB[7] = 0x00;
6775 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006776 case HPSA_DEVICE_RESET_MSG:
6777 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006778 c->Request.type_attr_dir =
6779 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006780 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006781 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
6782 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05006783 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006784 /* If bytes 4-7 are zero, it means reset the */
6785 /* LunID device */
6786 c->Request.CDB[4] = 0x00;
6787 c->Request.CDB[5] = 0x00;
6788 c->Request.CDB[6] = 0x00;
6789 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006790 break;
6791 case HPSA_ABORT_MSG:
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006792 memcpy(&tag, buff, sizeof(tag));
Don Brace2b08b3e2015-01-23 16:41:09 -06006793 dev_dbg(&h->pdev->dev,
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006794 "Abort Tag:0x%016llx using rqst Tag:0x%016llx",
6795 tag, c->Header.tag);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006796 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006797 c->Request.type_attr_dir =
6798 TYPE_ATTR_DIR(cmd_type,
6799 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006800 c->Request.Timeout = 0; /* Don't time out */
6801 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
6802 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
6803 c->Request.CDB[2] = 0x00; /* reserved */
6804 c->Request.CDB[3] = 0x00; /* reserved */
6805 /* Tag to abort goes in CDB[4]-CDB[11] */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006806 memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006807 c->Request.CDB[12] = 0x00; /* reserved */
6808 c->Request.CDB[13] = 0x00; /* reserved */
6809 c->Request.CDB[14] = 0x00; /* reserved */
6810 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006811 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006812 default:
6813 dev_warn(&h->pdev->dev, "unknown message type %d\n",
6814 cmd);
6815 BUG();
6816 }
6817 } else {
6818 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
6819 BUG();
6820 }
6821
Stephen M. Camerona505b862014-11-14 17:27:04 -06006822 switch (GET_DIR(c->Request.type_attr_dir)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006823 case XFER_READ:
6824 pci_dir = PCI_DMA_FROMDEVICE;
6825 break;
6826 case XFER_WRITE:
6827 pci_dir = PCI_DMA_TODEVICE;
6828 break;
6829 case XFER_NONE:
6830 pci_dir = PCI_DMA_NONE;
6831 break;
6832 default:
6833 pci_dir = PCI_DMA_BIDIRECTIONAL;
6834 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006835 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
6836 return -1;
6837 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006838}
6839
6840/*
6841 * Map (physical) PCI mem into (virtual) kernel space
6842 */
6843static void __iomem *remap_pci_mem(ulong base, ulong size)
6844{
6845 ulong page_base = ((ulong) base) & PAGE_MASK;
6846 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba342012-07-26 11:34:23 -05006847 void __iomem *page_remapped = ioremap_nocache(page_base,
6848 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006849
6850 return page_remapped ? (page_remapped + page_offs) : NULL;
6851}
6852
Matt Gates254f7962012-05-01 11:43:06 -05006853static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006854{
Matt Gates254f7962012-05-01 11:43:06 -05006855 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006856}
6857
Stephen M. Cameron900c5442010-02-04 08:42:35 -06006858static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006859{
6860 return h->access.intr_pending(h);
6861}
6862
6863static inline long interrupt_not_for_us(struct ctlr_info *h)
6864{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006865 return (h->access.intr_pending(h) == 0) ||
6866 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006867}
6868
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06006869static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
6870 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006871{
6872 if (unlikely(tag_index >= h->nr_cmds)) {
6873 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
6874 return 1;
6875 }
6876 return 0;
6877}
6878
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05006879static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006880{
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006881 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06006882 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
6883 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05006884 complete_scsi_command(c);
Stephen Cameron8be986c2015-04-23 09:34:06 -05006885 else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006886 complete(c->waiting);
Stephen M. Camerona104c992010-02-04 08:42:24 -06006887}
6888
Don Brace303932f2010-02-04 08:42:40 -06006889/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05006890static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06006891 u32 raw_tag)
6892{
6893 u32 tag_index;
6894 struct CommandList *c;
6895
Don Bracef2405db2015-01-23 16:43:09 -06006896 tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05006897 if (!bad_tag(h, tag_index, raw_tag)) {
6898 c = h->cmd_pool + tag_index;
6899 finish_cmd(c);
6900 }
Don Brace303932f2010-02-04 08:42:40 -06006901}
6902
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006903/* Some controllers, like p400, will give us one interrupt
6904 * after a soft reset, even if we turned interrupts off.
6905 * Only need to check for this in the hpsa_xxx_discard_completions
6906 * functions.
6907 */
6908static int ignore_bogus_interrupt(struct ctlr_info *h)
6909{
6910 if (likely(!reset_devices))
6911 return 0;
6912
6913 if (likely(h->interrupts_enabled))
6914 return 0;
6915
6916 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
6917 "(known firmware bug.) Ignoring.\n");
6918
6919 return 1;
6920}
6921
Matt Gates254f7962012-05-01 11:43:06 -05006922/*
6923 * Convert &h->q[x] (passed to interrupt handlers) back to h.
6924 * Relies on (h-q[x] == x) being true for x such that
6925 * 0 <= x < MAX_REPLY_QUEUES.
6926 */
6927static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006928{
Matt Gates254f7962012-05-01 11:43:06 -05006929 return container_of((queue - *queue), struct ctlr_info, q[0]);
6930}
6931
6932static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
6933{
6934 struct ctlr_info *h = queue_to_hba(queue);
6935 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006936 u32 raw_tag;
6937
6938 if (ignore_bogus_interrupt(h))
6939 return IRQ_NONE;
6940
6941 if (interrupt_not_for_us(h))
6942 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006943 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006944 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05006945 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006946 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05006947 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006948 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006949 return IRQ_HANDLED;
6950}
6951
Matt Gates254f7962012-05-01 11:43:06 -05006952static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006953{
Matt Gates254f7962012-05-01 11:43:06 -05006954 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006955 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05006956 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006957
6958 if (ignore_bogus_interrupt(h))
6959 return IRQ_NONE;
6960
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006961 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05006962 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006963 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05006964 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006965 return IRQ_HANDLED;
6966}
6967
Matt Gates254f7962012-05-01 11:43:06 -05006968static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006969{
Matt Gates254f7962012-05-01 11:43:06 -05006970 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06006971 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05006972 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006973
6974 if (interrupt_not_for_us(h))
6975 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006976 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006977 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05006978 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006979 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06006980 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05006981 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006982 }
6983 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006984 return IRQ_HANDLED;
6985}
6986
Matt Gates254f7962012-05-01 11:43:06 -05006987static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006988{
Matt Gates254f7962012-05-01 11:43:06 -05006989 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006990 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05006991 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006992
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006993 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05006994 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06006995 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06006996 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05006997 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006998 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006999 return IRQ_HANDLED;
7000}
7001
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06007002/* Send a message CDB to the firmware. Careful, this only works
7003 * in simple mode, not performant mode due to the tag lookup.
7004 * We only ever use this immediately after a controller reset.
7005 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007006static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
7007 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007008{
7009 struct Command {
7010 struct CommandListHeader CommandHeader;
7011 struct RequestBlock Request;
7012 struct ErrDescriptor ErrorDescriptor;
7013 };
7014 struct Command *cmd;
7015 static const size_t cmd_sz = sizeof(*cmd) +
7016 sizeof(cmd->ErrorDescriptor);
7017 dma_addr_t paddr64;
Don Brace2b08b3e2015-01-23 16:41:09 -06007018 __le32 paddr32;
7019 u32 tag;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007020 void __iomem *vaddr;
7021 int i, err;
7022
7023 vaddr = pci_ioremap_bar(pdev, 0);
7024 if (vaddr == NULL)
7025 return -ENOMEM;
7026
7027 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
7028 * CCISS commands, so they must be allocated from the lower 4GiB of
7029 * memory.
7030 */
7031 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
7032 if (err) {
7033 iounmap(vaddr);
Robert Elliott1eaec8f2015-01-23 16:42:37 -06007034 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007035 }
7036
7037 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
7038 if (cmd == NULL) {
7039 iounmap(vaddr);
7040 return -ENOMEM;
7041 }
7042
7043 /* This must fit, because of the 32-bit consistent DMA mask. Also,
7044 * although there's no guarantee, we assume that the address is at
7045 * least 4-byte aligned (most likely, it's page-aligned).
7046 */
Don Brace2b08b3e2015-01-23 16:41:09 -06007047 paddr32 = cpu_to_le32(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007048
7049 cmd->CommandHeader.ReplyQueue = 0;
7050 cmd->CommandHeader.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007051 cmd->CommandHeader.SGTotal = cpu_to_le16(0);
Don Brace2b08b3e2015-01-23 16:41:09 -06007052 cmd->CommandHeader.tag = cpu_to_le64(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007053 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
7054
7055 cmd->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06007056 cmd->Request.type_attr_dir =
7057 TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007058 cmd->Request.Timeout = 0; /* Don't time out */
7059 cmd->Request.CDB[0] = opcode;
7060 cmd->Request.CDB[1] = type;
7061 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007062 cmd->ErrorDescriptor.Addr =
Don Brace2b08b3e2015-01-23 16:41:09 -06007063 cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06007064 cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007065
Don Brace2b08b3e2015-01-23 16:41:09 -06007066 writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007067
7068 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
7069 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Don Brace2b08b3e2015-01-23 16:41:09 -06007070 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007071 break;
7072 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
7073 }
7074
7075 iounmap(vaddr);
7076
7077 /* we leak the DMA buffer here ... no choice since the controller could
7078 * still complete the command.
7079 */
7080 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
7081 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
7082 opcode, type);
7083 return -ETIMEDOUT;
7084 }
7085
7086 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
7087
7088 if (tag & HPSA_ERROR_BIT) {
7089 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
7090 opcode, type);
7091 return -EIO;
7092 }
7093
7094 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
7095 opcode, type);
7096 return 0;
7097}
7098
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007099#define hpsa_noop(p) hpsa_message(p, 3, 0)
7100
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007101static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Don Brace42a91642014-11-14 17:26:27 -06007102 void __iomem *vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007103{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007104
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007105 if (use_doorbell) {
7106 /* For everything after the P600, the PCI power state method
7107 * of resetting the controller doesn't work, so we have this
7108 * other way using the doorbell register.
7109 */
7110 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007111 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05007112
Justin Lindley00701a92014-05-29 10:52:47 -05007113 /* PMC hardware guys tell us we need a 10 second delay after
Stephen M. Cameron85009232013-09-23 13:33:36 -05007114 * doorbell reset and before any attempt to talk to the board
7115 * at all to ensure that this actually works and doesn't fall
7116 * over in some weird corner cases.
7117 */
Justin Lindley00701a92014-05-29 10:52:47 -05007118 msleep(10000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007119 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007120
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007121 /* Quoting from the Open CISS Specification: "The Power
7122 * Management Control/Status Register (CSR) controls the power
7123 * state of the device. The normal operating state is D0,
7124 * CSR=00h. The software off state is D3, CSR=03h. To reset
7125 * the controller, place the interface device in D3 then to D0,
7126 * this causes a secondary PCI reset which will reset the
7127 * controller." */
7128
Don Brace2662cab2015-01-23 16:41:25 -06007129 int rc = 0;
7130
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007131 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
Don Brace2662cab2015-01-23 16:41:25 -06007132
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007133 /* enter the D3hot power management state */
Don Brace2662cab2015-01-23 16:41:25 -06007134 rc = pci_set_power_state(pdev, PCI_D3hot);
7135 if (rc)
7136 return rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007137
7138 msleep(500);
7139
7140 /* enter the D0 power management state */
Don Brace2662cab2015-01-23 16:41:25 -06007141 rc = pci_set_power_state(pdev, PCI_D0);
7142 if (rc)
7143 return rc;
Mike Millerc4853ef2011-10-21 08:19:43 +02007144
7145 /*
7146 * The P600 requires a small delay when changing states.
7147 * Otherwise we may think the board did not reset and we bail.
7148 * This for kdump only and is particular to the P600.
7149 */
7150 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007151 }
7152 return 0;
7153}
7154
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007155static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007156{
7157 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06007158 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007159}
7160
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007161static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007162{
7163 char *driver_version;
7164 int i, size = sizeof(cfgtable->driver_version);
7165
7166 driver_version = kmalloc(size, GFP_KERNEL);
7167 if (!driver_version)
7168 return -ENOMEM;
7169
7170 init_driver_version(driver_version, size);
7171 for (i = 0; i < size; i++)
7172 writeb(driver_version[i], &cfgtable->driver_version[i]);
7173 kfree(driver_version);
7174 return 0;
7175}
7176
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007177static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
7178 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007179{
7180 int i;
7181
7182 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
7183 driver_ver[i] = readb(&cfgtable->driver_version[i]);
7184}
7185
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007186static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007187{
7188
7189 char *driver_ver, *old_driver_ver;
7190 int rc, size = sizeof(cfgtable->driver_version);
7191
7192 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
7193 if (!old_driver_ver)
7194 return -ENOMEM;
7195 driver_ver = old_driver_ver + size;
7196
7197 /* After a reset, the 32 bytes of "driver version" in the cfgtable
7198 * should have been changed, otherwise we know the reset failed.
7199 */
7200 init_driver_version(old_driver_ver, size);
7201 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
7202 rc = !memcmp(driver_ver, old_driver_ver, size);
7203 kfree(old_driver_ver);
7204 return rc;
7205}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007206/* This does a hard reset of the controller using PCI power management
7207 * states or the using the doorbell register.
7208 */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007209static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007210{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007211 u64 cfg_offset;
7212 u32 cfg_base_addr;
7213 u64 cfg_base_addr_index;
7214 void __iomem *vaddr;
7215 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007216 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007217 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007218 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007219 u32 use_doorbell;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007220 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007221
7222 /* For controllers as old as the P600, this is very nearly
7223 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007224 *
7225 * pci_save_state(pci_dev);
7226 * pci_set_power_state(pci_dev, PCI_D3hot);
7227 * pci_set_power_state(pci_dev, PCI_D0);
7228 * pci_restore_state(pci_dev);
7229 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007230 * For controllers newer than the P600, the pci power state
7231 * method of resetting doesn't work so we have another way
7232 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007233 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05007234
Robert Elliott60f923b2015-01-23 16:42:06 -06007235 if (!ctlr_is_resettable(board_id)) {
7236 dev_warn(&pdev->dev, "Controller not resettable\n");
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06007237 return -ENODEV;
7238 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05007239
7240 /* if controller is soft- but not hard resettable... */
7241 if (!ctlr_is_hard_resettable(board_id))
7242 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05007243
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007244 /* Save the PCI command register */
7245 pci_read_config_word(pdev, 4, &command_register);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007246 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007247
7248 /* find the first memory BAR, so we can find the cfg table */
7249 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
7250 if (rc)
7251 return rc;
7252 vaddr = remap_pci_mem(paddr, 0x250);
7253 if (!vaddr)
7254 return -ENOMEM;
7255
7256 /* find cfgtable in order to check if reset via doorbell is supported */
7257 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
7258 &cfg_base_addr_index, &cfg_offset);
7259 if (rc)
7260 goto unmap_vaddr;
7261 cfgtable = remap_pci_mem(pci_resource_start(pdev,
7262 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
7263 if (!cfgtable) {
7264 rc = -ENOMEM;
7265 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007266 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007267 rc = write_driver_ver_to_cfgtable(cfgtable);
7268 if (rc)
Tomas Henzl03741d92015-01-23 16:41:14 -06007269 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007270
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007271 /* If reset via doorbell register is supported, use that.
7272 * There are two such methods. Favor the newest method.
7273 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007274 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007275 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
7276 if (use_doorbell) {
7277 use_doorbell = DOORBELL_CTLR_RESET2;
7278 } else {
7279 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
7280 if (use_doorbell) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007281 dev_warn(&pdev->dev,
7282 "Soft reset not supported. Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007283 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007284 goto unmap_cfgtable;
7285 }
7286 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007287
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007288 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
7289 if (rc)
7290 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007291
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007292 pci_restore_state(pdev);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007293 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007294
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007295 /* Some devices (notably the HP Smart Array 5i Controller)
7296 need a little pause here */
7297 msleep(HPSA_POST_RESET_PAUSE_MSECS);
7298
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007299 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
7300 if (rc) {
7301 dev_warn(&pdev->dev,
Stephen Cameron050f7142015-01-23 16:42:22 -06007302 "Failed waiting for board to become ready after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007303 goto unmap_cfgtable;
7304 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007305
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007306 rc = controller_reset_failed(vaddr);
7307 if (rc < 0)
7308 goto unmap_cfgtable;
7309 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007310 dev_warn(&pdev->dev, "Unable to successfully reset "
7311 "controller. Will try soft reset.\n");
7312 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007313 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007314 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007315 }
7316
7317unmap_cfgtable:
7318 iounmap(cfgtable);
7319
7320unmap_vaddr:
7321 iounmap(vaddr);
7322 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007323}
7324
7325/*
7326 * We cannot read the structure directly, for portability we must use
7327 * the io functions.
7328 * This is for debug only.
7329 */
Don Brace42a91642014-11-14 17:26:27 -06007330static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007331{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007332#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007333 int i;
7334 char temp_name[17];
7335
7336 dev_info(dev, "Controller Configuration information\n");
7337 dev_info(dev, "------------------------------------\n");
7338 for (i = 0; i < 4; i++)
7339 temp_name[i] = readb(&(tb->Signature[i]));
7340 temp_name[4] = '\0';
7341 dev_info(dev, " Signature = %s\n", temp_name);
7342 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
7343 dev_info(dev, " Transport methods supported = 0x%x\n",
7344 readl(&(tb->TransportSupport)));
7345 dev_info(dev, " Transport methods active = 0x%x\n",
7346 readl(&(tb->TransportActive)));
7347 dev_info(dev, " Requested transport Method = 0x%x\n",
7348 readl(&(tb->HostWrite.TransportRequest)));
7349 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
7350 readl(&(tb->HostWrite.CoalIntDelay)));
7351 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
7352 readl(&(tb->HostWrite.CoalIntCount)));
Robert Elliott69d6e332015-01-23 16:41:56 -06007353 dev_info(dev, " Max outstanding commands = %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007354 readl(&(tb->CmdsOutMax)));
7355 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
7356 for (i = 0; i < 16; i++)
7357 temp_name[i] = readb(&(tb->ServerName[i]));
7358 temp_name[16] = '\0';
7359 dev_info(dev, " Server Name = %s\n", temp_name);
7360 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
7361 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007362#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007363}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007364
7365static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
7366{
7367 int i, offset, mem_type, bar_type;
7368
7369 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
7370 return 0;
7371 offset = 0;
7372 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
7373 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
7374 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
7375 offset += 4;
7376 else {
7377 mem_type = pci_resource_flags(pdev, i) &
7378 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
7379 switch (mem_type) {
7380 case PCI_BASE_ADDRESS_MEM_TYPE_32:
7381 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
7382 offset += 4; /* 32 bit */
7383 break;
7384 case PCI_BASE_ADDRESS_MEM_TYPE_64:
7385 offset += 8;
7386 break;
7387 default: /* reserved in PCI 2.2 */
7388 dev_warn(&pdev->dev,
7389 "base address is invalid\n");
7390 return -1;
7391 break;
7392 }
7393 }
7394 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
7395 return i + 1;
7396 }
7397 return -1;
7398}
7399
Robert Elliottcc64c812015-04-23 09:33:12 -05007400static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
7401{
7402 if (h->msix_vector) {
7403 if (h->pdev->msix_enabled)
7404 pci_disable_msix(h->pdev);
Robert Elliott105a3db2015-04-23 09:33:48 -05007405 h->msix_vector = 0;
Robert Elliottcc64c812015-04-23 09:33:12 -05007406 } else if (h->msi_vector) {
7407 if (h->pdev->msi_enabled)
7408 pci_disable_msi(h->pdev);
Robert Elliott105a3db2015-04-23 09:33:48 -05007409 h->msi_vector = 0;
Robert Elliottcc64c812015-04-23 09:33:12 -05007410 }
7411}
7412
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007413/* If MSI/MSI-X is supported by the kernel we will try to enable it on
Stephen Cameron050f7142015-01-23 16:42:22 -06007414 * controllers that are capable. If not, we use legacy INTx mode.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007415 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007416static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007417{
7418#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05007419 int err, i;
7420 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
7421
7422 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
7423 hpsa_msix_entries[i].vector = 0;
7424 hpsa_msix_entries[i].entry = i;
7425 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007426
7427 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05007428 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
7429 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007430 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007431 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007432 dev_info(&h->pdev->dev, "MSI-X capable controller\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007433 h->msix_vector = MAX_REPLY_QUEUES;
Stephen M. Cameronf89439b2014-05-29 10:53:02 -05007434 if (h->msix_vector > num_online_cpus())
7435 h->msix_vector = num_online_cpus();
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007436 err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
7437 1, h->msix_vector);
7438 if (err < 0) {
7439 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
7440 h->msix_vector = 0;
7441 goto single_msi_mode;
7442 } else if (err < h->msix_vector) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007443 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007444 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007445 }
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007446 h->msix_vector = err;
7447 for (i = 0; i < h->msix_vector; i++)
7448 h->intr[i] = hpsa_msix_entries[i].vector;
7449 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007450 }
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007451single_msi_mode:
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007452 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007453 dev_info(&h->pdev->dev, "MSI capable controller\n");
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007454 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007455 h->msi_vector = 1;
7456 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007457 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007458 }
7459default_int_mode:
7460#endif /* CONFIG_PCI_MSI */
7461 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06007462 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007463}
7464
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007465static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007466{
7467 int i;
7468 u32 subsystem_vendor_id, subsystem_device_id;
7469
7470 subsystem_vendor_id = pdev->subsystem_vendor;
7471 subsystem_device_id = pdev->subsystem_device;
7472 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
7473 subsystem_vendor_id;
7474
7475 for (i = 0; i < ARRAY_SIZE(products); i++)
7476 if (*board_id == products[i].board_id)
7477 return i;
7478
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05007479 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
7480 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
7481 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007482 dev_warn(&pdev->dev, "unrecognized board ID: "
7483 "0x%08x, ignoring.\n", *board_id);
7484 return -ENODEV;
7485 }
7486 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
7487}
7488
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007489static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
7490 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007491{
7492 int i;
7493
7494 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007495 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007496 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007497 *memory_bar = pci_resource_start(pdev, i);
7498 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007499 *memory_bar);
7500 return 0;
7501 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007502 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007503 return -ENODEV;
7504}
7505
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007506static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
7507 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007508{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007509 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007510 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007511 if (wait_for_ready)
7512 iterations = HPSA_BOARD_READY_ITERATIONS;
7513 else
7514 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007515
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007516 for (i = 0; i < iterations; i++) {
7517 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
7518 if (wait_for_ready) {
7519 if (scratchpad == HPSA_FIRMWARE_READY)
7520 return 0;
7521 } else {
7522 if (scratchpad != HPSA_FIRMWARE_READY)
7523 return 0;
7524 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007525 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
7526 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007527 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007528 return -ENODEV;
7529}
7530
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007531static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
7532 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
7533 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007534{
7535 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
7536 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
7537 *cfg_base_addr &= (u32) 0x0000ffff;
7538 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
7539 if (*cfg_base_addr_index == -1) {
7540 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
7541 return -ENODEV;
7542 }
7543 return 0;
7544}
7545
Robert Elliott195f2c62015-04-23 09:33:17 -05007546static void hpsa_free_cfgtables(struct ctlr_info *h)
7547{
Robert Elliott105a3db2015-04-23 09:33:48 -05007548 if (h->transtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007549 iounmap(h->transtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007550 h->transtable = NULL;
7551 }
7552 if (h->cfgtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007553 iounmap(h->cfgtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007554 h->cfgtable = NULL;
7555 }
Robert Elliott195f2c62015-04-23 09:33:17 -05007556}
7557
7558/* Find and map CISS config table and transfer table
7559+ * several items must be unmapped (freed) later
7560+ * */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007561static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007562{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06007563 u64 cfg_offset;
7564 u32 cfg_base_addr;
7565 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06007566 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007567 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007568
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007569 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7570 &cfg_base_addr_index, &cfg_offset);
7571 if (rc)
7572 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007573 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007574 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007575 if (!h->cfgtable) {
7576 dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007577 return -ENOMEM;
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007578 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007579 rc = write_driver_ver_to_cfgtable(h->cfgtable);
7580 if (rc)
7581 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007582 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007583 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007584 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
7585 cfg_base_addr_index)+cfg_offset+trans_offset,
7586 sizeof(*h->transtable));
Robert Elliott195f2c62015-04-23 09:33:17 -05007587 if (!h->transtable) {
7588 dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
7589 hpsa_free_cfgtables(h);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007590 return -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05007591 }
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007592 return 0;
7593}
7594
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007595static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007596{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007597#define MIN_MAX_COMMANDS 16
7598 BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
7599
7600 h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06007601
7602 /* Limit commands in memory limited kdump scenario. */
7603 if (reset_devices && h->max_commands > 32)
7604 h->max_commands = 32;
7605
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007606 if (h->max_commands < MIN_MAX_COMMANDS) {
7607 dev_warn(&h->pdev->dev,
7608 "Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
7609 h->max_commands,
7610 MIN_MAX_COMMANDS);
7611 h->max_commands = MIN_MAX_COMMANDS;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007612 }
7613}
7614
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007615/* If the controller reports that the total max sg entries is greater than 512,
7616 * then we know that chained SG blocks work. (Original smart arrays did not
7617 * support chained SG blocks and would return zero for max sg entries.)
7618 */
7619static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
7620{
7621 return h->maxsgentries > 512;
7622}
7623
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007624/* Interrogate the hardware for some limits:
7625 * max commands, max SG elements without chaining, and with chaining,
7626 * SG chain block size, etc.
7627 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007628static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007629{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007630 hpsa_get_max_perf_mode_cmds(h);
Stephen Cameron45fcb862015-01-23 16:43:04 -06007631 h->nr_cmds = h->max_commands;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007632 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007633 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007634 if (hpsa_supports_chained_sg_blocks(h)) {
7635 /* Limit in-command s/g elements to 32 save dma'able memory. */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007636 h->max_cmd_sg_entries = 32;
Webb Scales1a63ea62014-11-14 17:26:43 -06007637 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007638 h->maxsgentries--; /* save one for chain pointer */
7639 } else {
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007640 /*
7641 * Original smart arrays supported at most 31 s/g entries
7642 * embedded inline in the command (trying to use more
7643 * would lock up the controller)
7644 */
7645 h->max_cmd_sg_entries = 31;
Webb Scales1a63ea62014-11-14 17:26:43 -06007646 h->maxsgentries = 31; /* default to traditional values */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007647 h->chainsize = 0;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007648 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05007649
7650 /* Find out what task management functions are supported and cache */
7651 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06007652 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
7653 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
7654 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
7655 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen Cameron8be986c2015-04-23 09:34:06 -05007656 if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
7657 dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007658}
7659
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007660static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
7661{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09007662 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007663 dev_err(&h->pdev->dev, "not a valid CISS config table\n");
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007664 return false;
7665 }
7666 return true;
7667}
7668
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007669static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007670{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007671 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007672
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007673 driver_support = readl(&(h->cfgtable->driver_support));
Arnd Bergmann0b9e7b72014-06-26 15:44:52 +02007674 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
7675#ifdef CONFIG_X86
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007676 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007677#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06007678 driver_support |= ENABLE_UNIT_ATTN;
7679 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007680}
7681
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05007682/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
7683 * in a prefetch beyond physical memory.
7684 */
7685static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
7686{
7687 u32 dma_prefetch;
7688
7689 if (h->board_id != 0x3225103C)
7690 return;
7691 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
7692 dma_prefetch |= 0x8000;
7693 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
7694}
7695
Robert Elliottc706a792015-01-23 16:45:01 -06007696static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007697{
7698 int i;
7699 u32 doorbell_value;
7700 unsigned long flags;
7701 /* wait until the clear_event_notify bit 6 is cleared by controller. */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007702 for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007703 spin_lock_irqsave(&h->lock, flags);
7704 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7705 spin_unlock_irqrestore(&h->lock, flags);
7706 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
Robert Elliottc706a792015-01-23 16:45:01 -06007707 goto done;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007708 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007709 msleep(CLEAR_EVENT_WAIT_INTERVAL);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007710 }
Robert Elliottc706a792015-01-23 16:45:01 -06007711 return -ENODEV;
7712done:
7713 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007714}
7715
Robert Elliottc706a792015-01-23 16:45:01 -06007716static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007717{
7718 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007719 u32 doorbell_value;
7720 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007721
7722 /* under certain very rare conditions, this can take awhile.
7723 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
7724 * as we enter this code.)
7725 */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007726 for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
Webb Scales25163bd2015-04-23 09:32:00 -05007727 if (h->remove_in_progress)
7728 goto done;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007729 spin_lock_irqsave(&h->lock, flags);
7730 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7731 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06007732 if (!(doorbell_value & CFGTBL_ChangeReq))
Robert Elliottc706a792015-01-23 16:45:01 -06007733 goto done;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007734 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007735 msleep(MODE_CHANGE_WAIT_INTERVAL);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007736 }
Robert Elliottc706a792015-01-23 16:45:01 -06007737 return -ENODEV;
7738done:
7739 return 0;
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007740}
7741
Robert Elliottc706a792015-01-23 16:45:01 -06007742/* return -ENODEV or other reason on error, 0 on success */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007743static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007744{
7745 u32 trans_support;
7746
7747 trans_support = readl(&(h->cfgtable->TransportSupport));
7748 if (!(trans_support & SIMPLE_MODE))
7749 return -ENOTSUPP;
7750
7751 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007752
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007753 /* Update the field, and then ring the doorbell */
7754 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007755 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007756 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06007757 if (hpsa_wait_for_mode_change_ack(h))
7758 goto error;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007759 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007760 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
7761 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06007762 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007763 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007764error:
Stephen Cameron050f7142015-01-23 16:42:22 -06007765 dev_err(&h->pdev->dev, "failed to enter simple mode\n");
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007766 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007767}
7768
Robert Elliott195f2c62015-04-23 09:33:17 -05007769/* free items allocated or mapped by hpsa_pci_init */
7770static void hpsa_free_pci_init(struct ctlr_info *h)
7771{
7772 hpsa_free_cfgtables(h); /* pci_init 4 */
7773 iounmap(h->vaddr); /* pci_init 3 */
Robert Elliott105a3db2015-04-23 09:33:48 -05007774 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05007775 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Robert Elliott943a7022015-04-23 09:34:32 -05007776 /*
7777 * call pci_disable_device before pci_release_regions per
7778 * Documentation/PCI/pci.txt
7779 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007780 pci_disable_device(h->pdev); /* pci_init 1 */
Robert Elliott943a7022015-04-23 09:34:32 -05007781 pci_release_regions(h->pdev); /* pci_init 2 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007782}
7783
7784/* several items must be freed later */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007785static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007786{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007787 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007788
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007789 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
7790 if (prod_index < 0)
Robert Elliott60f923b2015-01-23 16:42:06 -06007791 return prod_index;
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007792 h->product_name = products[prod_index].product_name;
7793 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007794
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05007795 h->needs_abort_tags_swizzled =
7796 ctlr_needs_abort_tags_swizzled(h->board_id);
7797
Matthew Garrette5a44df2011-11-11 11:14:23 -05007798 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
7799 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
7800
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007801 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007802 if (err) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007803 dev_err(&h->pdev->dev, "failed to enable PCI device\n");
Robert Elliott943a7022015-04-23 09:34:32 -05007804 pci_disable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007805 return err;
7806 }
7807
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06007808 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007809 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007810 dev_err(&h->pdev->dev,
Robert Elliott195f2c62015-04-23 09:33:17 -05007811 "failed to obtain PCI resources\n");
Robert Elliott943a7022015-04-23 09:34:32 -05007812 pci_disable_device(h->pdev);
7813 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007814 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06007815
7816 pci_set_master(h->pdev);
7817
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05007818 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007819 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007820 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007821 goto clean2; /* intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007822 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007823 if (!h->vaddr) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007824 dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007825 err = -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05007826 goto clean2; /* intmode+region, pci */
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007827 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007828 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007829 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007830 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007831 err = hpsa_find_cfgtables(h);
7832 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007833 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007834 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007835
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007836 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007837 err = -ENODEV;
Robert Elliott195f2c62015-04-23 09:33:17 -05007838 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007839 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007840 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05007841 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007842 err = hpsa_enter_simple_mode(h);
7843 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007844 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007845 return 0;
7846
Robert Elliott195f2c62015-04-23 09:33:17 -05007847clean4: /* cfgtables, vaddr, intmode+region, pci */
7848 hpsa_free_cfgtables(h);
7849clean3: /* vaddr, intmode+region, pci */
7850 iounmap(h->vaddr);
Robert Elliott105a3db2015-04-23 09:33:48 -05007851 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05007852clean2: /* intmode+region, pci */
7853 hpsa_disable_interrupt_mode(h);
Robert Elliott943a7022015-04-23 09:34:32 -05007854 /*
7855 * call pci_disable_device before pci_release_regions per
7856 * Documentation/PCI/pci.txt
7857 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007858 pci_disable_device(h->pdev);
Robert Elliott943a7022015-04-23 09:34:32 -05007859 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007860 return err;
7861}
7862
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007863static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06007864{
7865 int rc;
7866
7867#define HBA_INQUIRY_BYTE_COUNT 64
7868 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
7869 if (!h->hba_inquiry_data)
7870 return;
7871 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
7872 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
7873 if (rc != 0) {
7874 kfree(h->hba_inquiry_data);
7875 h->hba_inquiry_data = NULL;
7876 }
7877}
7878
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007879static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007880{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007881 int rc, i;
Tomas Henzl3b747292015-01-23 16:41:20 -06007882 void __iomem *vaddr;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007883
7884 if (!reset_devices)
7885 return 0;
7886
Tomas Henzl132aa222014-08-14 16:12:39 +02007887 /* kdump kernel is loading, we don't know in which state is
7888 * the pci interface. The dev->enable_cnt is equal zero
7889 * so we call enable+disable, wait a while and switch it on.
7890 */
7891 rc = pci_enable_device(pdev);
7892 if (rc) {
7893 dev_warn(&pdev->dev, "Failed to enable PCI device\n");
7894 return -ENODEV;
7895 }
7896 pci_disable_device(pdev);
7897 msleep(260); /* a randomly chosen number */
7898 rc = pci_enable_device(pdev);
7899 if (rc) {
7900 dev_warn(&pdev->dev, "failed to enable device.\n");
7901 return -ENODEV;
7902 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06007903
Tomas Henzl859c75a2014-09-12 14:44:15 +02007904 pci_set_master(pdev);
Robert Elliott4fa604e2014-11-14 17:27:24 -06007905
Tomas Henzl3b747292015-01-23 16:41:20 -06007906 vaddr = pci_ioremap_bar(pdev, 0);
7907 if (vaddr == NULL) {
7908 rc = -ENOMEM;
7909 goto out_disable;
7910 }
7911 writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
7912 iounmap(vaddr);
7913
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007914 /* Reset the controller with a PCI power-cycle or via doorbell */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007915 rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007916
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007917 /* -ENOTSUPP here means we cannot reset the controller
7918 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05007919 * "performant mode". Or, it might be 640x, which can't reset
7920 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007921 */
Robert Elliottadf1b3a2015-01-23 16:42:01 -06007922 if (rc)
Tomas Henzl132aa222014-08-14 16:12:39 +02007923 goto out_disable;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007924
7925 /* Now try to get the controller to respond to a no-op */
Robert Elliott1ba66c92015-01-23 16:42:11 -06007926 dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007927 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
7928 if (hpsa_noop(pdev) == 0)
7929 break;
7930 else
7931 dev_warn(&pdev->dev, "no-op failed%s\n",
7932 (i < 11 ? "; re-trying" : ""));
7933 }
Tomas Henzl132aa222014-08-14 16:12:39 +02007934
7935out_disable:
7936
7937 pci_disable_device(pdev);
7938 return rc;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007939}
7940
Robert Elliott1fb7c982015-04-23 09:33:22 -05007941static void hpsa_free_cmd_pool(struct ctlr_info *h)
7942{
7943 kfree(h->cmd_pool_bits);
Robert Elliott105a3db2015-04-23 09:33:48 -05007944 h->cmd_pool_bits = NULL;
7945 if (h->cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05007946 pci_free_consistent(h->pdev,
7947 h->nr_cmds * sizeof(struct CommandList),
7948 h->cmd_pool,
7949 h->cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05007950 h->cmd_pool = NULL;
7951 h->cmd_pool_dhandle = 0;
7952 }
7953 if (h->errinfo_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05007954 pci_free_consistent(h->pdev,
7955 h->nr_cmds * sizeof(struct ErrorInfo),
7956 h->errinfo_pool,
7957 h->errinfo_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05007958 h->errinfo_pool = NULL;
7959 h->errinfo_pool_dhandle = 0;
7960 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05007961}
7962
Robert Elliottd37ffbe2015-04-23 09:32:27 -05007963static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007964{
7965 h->cmd_pool_bits = kzalloc(
7966 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
7967 sizeof(unsigned long), GFP_KERNEL);
7968 h->cmd_pool = pci_alloc_consistent(h->pdev,
7969 h->nr_cmds * sizeof(*h->cmd_pool),
7970 &(h->cmd_pool_dhandle));
7971 h->errinfo_pool = pci_alloc_consistent(h->pdev,
7972 h->nr_cmds * sizeof(*h->errinfo_pool),
7973 &(h->errinfo_pool_dhandle));
7974 if ((h->cmd_pool_bits == NULL)
7975 || (h->cmd_pool == NULL)
7976 || (h->errinfo_pool == NULL)) {
7977 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
Robert Elliott2c143342015-01-23 16:42:48 -06007978 goto clean_up;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007979 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05007980 hpsa_preinitialize_commands(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007981 return 0;
Robert Elliott2c143342015-01-23 16:42:48 -06007982clean_up:
7983 hpsa_free_cmd_pool(h);
7984 return -ENOMEM;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007985}
7986
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05007987static void hpsa_irq_affinity_hints(struct ctlr_info *h)
7988{
Fabian Frederickec429952015-01-23 16:41:46 -06007989 int i, cpu;
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05007990
7991 cpu = cpumask_first(cpu_online_mask);
7992 for (i = 0; i < h->msix_vector; i++) {
Fabian Frederickec429952015-01-23 16:41:46 -06007993 irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05007994 cpu = cpumask_next(cpu, cpu_online_mask);
7995 }
7996}
7997
Robert Elliottec501a12015-01-23 16:41:40 -06007998/* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
7999static void hpsa_free_irqs(struct ctlr_info *h)
8000{
8001 int i;
8002
8003 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
8004 /* Single reply queue, only one irq to free */
8005 i = h->intr_mode;
8006 irq_set_affinity_hint(h->intr[i], NULL);
8007 free_irq(h->intr[i], &h->q[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008008 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008009 return;
8010 }
8011
8012 for (i = 0; i < h->msix_vector; i++) {
8013 irq_set_affinity_hint(h->intr[i], NULL);
8014 free_irq(h->intr[i], &h->q[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008015 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008016 }
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008017 for (; i < MAX_REPLY_QUEUES; i++)
8018 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06008019}
8020
Robert Elliott9ee61792015-01-23 16:42:32 -06008021/* returns 0 on success; cleans up and returns -Enn on error */
8022static int hpsa_request_irqs(struct ctlr_info *h,
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008023 irqreturn_t (*msixhandler)(int, void *),
8024 irqreturn_t (*intxhandler)(int, void *))
8025{
Matt Gates254f7962012-05-01 11:43:06 -05008026 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008027
Matt Gates254f7962012-05-01 11:43:06 -05008028 /*
8029 * initialize h->q[x] = x so that interrupt handlers know which
8030 * queue to process.
8031 */
8032 for (i = 0; i < MAX_REPLY_QUEUES; i++)
8033 h->q[i] = (u8) i;
8034
Hannes Reineckeeee0f032014-01-15 13:30:53 +01008035 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05008036 /* If performant mode and MSI-X, use multiple reply queues */
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008037 for (i = 0; i < h->msix_vector; i++) {
Robert Elliott8b470042015-04-23 09:34:58 -05008038 sprintf(h->intrname[i], "%s-msix%d", h->devname, i);
Matt Gates254f7962012-05-01 11:43:06 -05008039 rc = request_irq(h->intr[i], msixhandler,
Robert Elliott8b470042015-04-23 09:34:58 -05008040 0, h->intrname[i],
Matt Gates254f7962012-05-01 11:43:06 -05008041 &h->q[i]);
Robert Elliotta4e17fc2015-01-23 16:41:51 -06008042 if (rc) {
8043 int j;
8044
8045 dev_err(&h->pdev->dev,
8046 "failed to get irq %d for %s\n",
8047 h->intr[i], h->devname);
8048 for (j = 0; j < i; j++) {
8049 free_irq(h->intr[j], &h->q[j]);
8050 h->q[j] = 0;
8051 }
8052 for (; j < MAX_REPLY_QUEUES; j++)
8053 h->q[j] = 0;
8054 return rc;
8055 }
8056 }
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05008057 hpsa_irq_affinity_hints(h);
Matt Gates254f7962012-05-01 11:43:06 -05008058 } else {
8059 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01008060 if (h->msix_vector > 0 || h->msi_vector) {
Robert Elliott8b470042015-04-23 09:34:58 -05008061 if (h->msix_vector)
8062 sprintf(h->intrname[h->intr_mode],
8063 "%s-msix", h->devname);
8064 else
8065 sprintf(h->intrname[h->intr_mode],
8066 "%s-msi", h->devname);
Matt Gates254f7962012-05-01 11:43:06 -05008067 rc = request_irq(h->intr[h->intr_mode],
Robert Elliott8b470042015-04-23 09:34:58 -05008068 msixhandler, 0,
8069 h->intrname[h->intr_mode],
Matt Gates254f7962012-05-01 11:43:06 -05008070 &h->q[h->intr_mode]);
8071 } else {
Robert Elliott8b470042015-04-23 09:34:58 -05008072 sprintf(h->intrname[h->intr_mode],
8073 "%s-intx", h->devname);
Matt Gates254f7962012-05-01 11:43:06 -05008074 rc = request_irq(h->intr[h->intr_mode],
Robert Elliott8b470042015-04-23 09:34:58 -05008075 intxhandler, IRQF_SHARED,
8076 h->intrname[h->intr_mode],
Matt Gates254f7962012-05-01 11:43:06 -05008077 &h->q[h->intr_mode]);
8078 }
Robert Elliott105a3db2015-04-23 09:33:48 -05008079 irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
Matt Gates254f7962012-05-01 11:43:06 -05008080 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008081 if (rc) {
Robert Elliott195f2c62015-04-23 09:33:17 -05008082 dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008083 h->intr[h->intr_mode], h->devname);
Robert Elliott195f2c62015-04-23 09:33:17 -05008084 hpsa_free_irqs(h);
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05008085 return -ENODEV;
8086 }
8087 return 0;
8088}
8089
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008090static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008091{
Robert Elliott39c53f52015-04-23 09:35:14 -05008092 int rc;
Robert Elliottbf43caf2015-04-23 09:33:38 -05008093 hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008094
8095 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008096 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY);
8097 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008098 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008099 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008100 }
8101
8102 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008103 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
8104 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008105 dev_warn(&h->pdev->dev, "Board failed to become ready "
8106 "after soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05008107 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008108 }
8109
8110 return 0;
8111}
8112
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008113static void hpsa_free_reply_queues(struct ctlr_info *h)
8114{
8115 int i;
8116
8117 for (i = 0; i < h->nreply_queues; i++) {
8118 if (!h->reply_queue[i].head)
8119 continue;
Robert Elliott1fb7c982015-04-23 09:33:22 -05008120 pci_free_consistent(h->pdev,
8121 h->reply_queue_size,
8122 h->reply_queue[i].head,
8123 h->reply_queue[i].busaddr);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008124 h->reply_queue[i].head = NULL;
8125 h->reply_queue[i].busaddr = 0;
8126 }
Robert Elliott105a3db2015-04-23 09:33:48 -05008127 h->reply_queue_size = 0;
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008128}
8129
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05008130static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
8131{
Robert Elliott105a3db2015-04-23 09:33:48 -05008132 hpsa_free_performant_mode(h); /* init_one 7 */
8133 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
8134 hpsa_free_cmd_pool(h); /* init_one 5 */
8135 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliott2946e822015-04-23 09:35:09 -05008136 scsi_host_put(h->scsi_host); /* init_one 3 */
8137 h->scsi_host = NULL; /* init_one 3 */
8138 hpsa_free_pci_init(h); /* init_one 2_5 */
Robert Elliott9ecd9532015-04-23 09:34:43 -05008139 free_percpu(h->lockup_detected); /* init_one 2 */
8140 h->lockup_detected = NULL; /* init_one 2 */
8141 if (h->resubmit_wq) {
8142 destroy_workqueue(h->resubmit_wq); /* init_one 1 */
8143 h->resubmit_wq = NULL;
8144 }
8145 if (h->rescan_ctlr_wq) {
8146 destroy_workqueue(h->rescan_ctlr_wq);
8147 h->rescan_ctlr_wq = NULL;
8148 }
Robert Elliott105a3db2015-04-23 09:33:48 -05008149 kfree(h); /* init_one 1 */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008150}
8151
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008152/* Called when controller lockup detected. */
Don Bracef2405db2015-01-23 16:43:09 -06008153static void fail_all_outstanding_cmds(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008154{
Webb Scales281a7fd2015-01-23 16:43:35 -06008155 int i, refcount;
8156 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05008157 int failcount = 0;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008158
Don Brace080ef1c2015-01-23 16:43:25 -06008159 flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
Don Bracef2405db2015-01-23 16:43:09 -06008160 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06008161 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06008162 refcount = atomic_inc_return(&c->refcount);
8163 if (refcount > 1) {
Webb Scales25163bd2015-04-23 09:32:00 -05008164 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
Webb Scales281a7fd2015-01-23 16:43:35 -06008165 finish_cmd(c);
Stephen Cameron433b5f42015-04-23 09:32:11 -05008166 atomic_dec(&h->commands_outstanding);
Webb Scales25163bd2015-04-23 09:32:00 -05008167 failcount++;
Webb Scales281a7fd2015-01-23 16:43:35 -06008168 }
8169 cmd_free(h, c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008170 }
Webb Scales25163bd2015-04-23 09:32:00 -05008171 dev_warn(&h->pdev->dev,
8172 "failed %d commands in fail_all\n", failcount);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008173}
8174
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008175static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
8176{
Rusty Russellc8ed0012015-03-05 10:49:19 +10308177 int cpu;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008178
Rusty Russellc8ed0012015-03-05 10:49:19 +10308179 for_each_online_cpu(cpu) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008180 u32 *lockup_detected;
8181 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
8182 *lockup_detected = value;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008183 }
8184 wmb(); /* be sure the per-cpu variables are out to memory */
8185}
8186
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008187static void controller_lockup_detected(struct ctlr_info *h)
8188{
8189 unsigned long flags;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008190 u32 lockup_detected;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008191
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008192 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8193 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008194 lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
8195 if (!lockup_detected) {
8196 /* no heartbeat, but controller gave us a zero. */
8197 dev_warn(&h->pdev->dev,
Webb Scales25163bd2015-04-23 09:32:00 -05008198 "lockup detected after %d but scratchpad register is zero\n",
8199 h->heartbeat_sample_interval / HZ);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008200 lockup_detected = 0xffffffff;
8201 }
8202 set_lockup_detected_for_all_cpus(h, lockup_detected);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008203 spin_unlock_irqrestore(&h->lock, flags);
Webb Scales25163bd2015-04-23 09:32:00 -05008204 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
8205 lockup_detected, h->heartbeat_sample_interval / HZ);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008206 pci_disable_device(h->pdev);
Don Bracef2405db2015-01-23 16:43:09 -06008207 fail_all_outstanding_cmds(h);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008208}
8209
Webb Scales25163bd2015-04-23 09:32:00 -05008210static int detect_controller_lockup(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008211{
8212 u64 now;
8213 u32 heartbeat;
8214 unsigned long flags;
8215
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008216 now = get_jiffies_64();
8217 /* If we've received an interrupt recently, we're ok. */
8218 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05008219 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05008220 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008221
8222 /*
8223 * If we've already checked the heartbeat recently, we're ok.
8224 * This could happen if someone sends us a signal. We
8225 * otherwise don't care about signals in this thread.
8226 */
8227 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05008228 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05008229 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008230
8231 /* If heartbeat has not changed since we last looked, we're not ok. */
8232 spin_lock_irqsave(&h->lock, flags);
8233 heartbeat = readl(&h->cfgtable->HeartBeat);
8234 spin_unlock_irqrestore(&h->lock, flags);
8235 if (h->last_heartbeat == heartbeat) {
8236 controller_lockup_detected(h);
Webb Scales25163bd2015-04-23 09:32:00 -05008237 return true;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008238 }
8239
8240 /* We're ok. */
8241 h->last_heartbeat = heartbeat;
8242 h->last_heartbeat_timestamp = now;
Webb Scales25163bd2015-04-23 09:32:00 -05008243 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008244}
8245
Stephen M. Cameron98465902014-02-21 16:25:00 -06008246static void hpsa_ack_ctlr_events(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008247{
8248 int i;
8249 char *event_type;
8250
Stephen Camerone4aa3e62015-01-23 16:44:07 -06008251 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
8252 return;
8253
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008254 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06008255 if ((h->transMethod & (CFGTBL_Trans_io_accel1
8256 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008257 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
8258 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
8259
8260 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
8261 event_type = "state change";
8262 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
8263 event_type = "configuration change";
8264 /* Stop sending new RAID offload reqs via the IO accelerator */
8265 scsi_block_requests(h->scsi_host);
8266 for (i = 0; i < h->ndevices; i++)
8267 h->dev[i]->offload_enabled = 0;
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06008268 hpsa_drain_accel_commands(h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008269 /* Set 'accelerator path config change' bit */
8270 dev_warn(&h->pdev->dev,
8271 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
8272 h->events, event_type);
8273 writel(h->events, &(h->cfgtable->clear_event_notify));
8274 /* Set the "clear event notify field update" bit 6 */
8275 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8276 /* Wait until ctlr clears 'clear event notify field', bit 6 */
8277 hpsa_wait_for_clear_event_notify_ack(h);
8278 scsi_unblock_requests(h->scsi_host);
8279 } else {
8280 /* Acknowledge controller notification events. */
8281 writel(h->events, &(h->cfgtable->clear_event_notify));
8282 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8283 hpsa_wait_for_clear_event_notify_ack(h);
8284#if 0
8285 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8286 hpsa_wait_for_mode_change_ack(h);
8287#endif
8288 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008289 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008290}
8291
8292/* Check a register on the controller to see if there are configuration
8293 * changes (added/changed/removed logical drives, etc.) which mean that
Scott Teele863d682014-02-18 13:57:05 -06008294 * we should rescan the controller for devices.
8295 * Also check flag for driver-initiated rescan.
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008296 */
Stephen M. Cameron98465902014-02-21 16:25:00 -06008297static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008298{
Don Brace853633e2015-11-04 15:50:37 -06008299 if (h->drv_req_rescan) {
8300 h->drv_req_rescan = 0;
8301 return 1;
8302 }
8303
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008304 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
Stephen M. Cameron98465902014-02-21 16:25:00 -06008305 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008306
8307 h->events = readl(&(h->cfgtable->event_notify));
Stephen M. Cameron98465902014-02-21 16:25:00 -06008308 return h->events & RESCAN_REQUIRED_EVENT_BITS;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008309}
8310
Stephen M. Cameron98465902014-02-21 16:25:00 -06008311/*
8312 * Check if any of the offline devices have become ready
8313 */
8314static int hpsa_offline_devices_ready(struct ctlr_info *h)
8315{
8316 unsigned long flags;
8317 struct offline_device_entry *d;
8318 struct list_head *this, *tmp;
8319
8320 spin_lock_irqsave(&h->offline_device_lock, flags);
8321 list_for_each_safe(this, tmp, &h->offline_device_list) {
8322 d = list_entry(this, struct offline_device_entry,
8323 offline_list);
8324 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008325 if (!hpsa_volume_offline(h, d->scsi3addr)) {
8326 spin_lock_irqsave(&h->offline_device_lock, flags);
8327 list_del(&d->offline_list);
8328 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008329 return 1;
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008330 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008331 spin_lock_irqsave(&h->offline_device_lock, flags);
8332 }
8333 spin_unlock_irqrestore(&h->offline_device_lock, flags);
8334 return 0;
8335}
8336
Scott Teel34592252015-11-04 15:52:09 -06008337static int hpsa_luns_changed(struct ctlr_info *h)
8338{
8339 int rc = 1; /* assume there are changes */
8340 struct ReportLUNdata *logdev = NULL;
8341
8342 /* if we can't find out if lun data has changed,
8343 * assume that it has.
8344 */
8345
8346 if (!h->lastlogicals)
8347 goto out;
8348
8349 logdev = kzalloc(sizeof(*logdev), GFP_KERNEL);
8350 if (!logdev) {
8351 dev_warn(&h->pdev->dev,
8352 "Out of memory, can't track lun changes.\n");
8353 goto out;
8354 }
8355 if (hpsa_scsi_do_report_luns(h, 1, logdev, sizeof(*logdev), 0)) {
8356 dev_warn(&h->pdev->dev,
8357 "report luns failed, can't track lun changes.\n");
8358 goto out;
8359 }
8360 if (memcmp(logdev, h->lastlogicals, sizeof(*logdev))) {
8361 dev_info(&h->pdev->dev,
8362 "Lun changes detected.\n");
8363 memcpy(h->lastlogicals, logdev, sizeof(*logdev));
8364 goto out;
8365 } else
8366 rc = 0; /* no changes detected. */
8367out:
8368 kfree(logdev);
8369 return rc;
8370}
8371
Don Brace6636e7f2015-01-23 16:45:17 -06008372static void hpsa_rescan_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008373{
8374 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008375 struct ctlr_info *h = container_of(to_delayed_work(work),
Don Brace6636e7f2015-01-23 16:45:17 -06008376 struct ctlr_info, rescan_ctlr_work);
8377
8378
8379 if (h->remove_in_progress)
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008380 return;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008381
8382 if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
8383 scsi_host_get(h->scsi_host);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008384 hpsa_ack_ctlr_events(h);
8385 hpsa_scan_start(h->scsi_host);
8386 scsi_host_put(h->scsi_host);
Scott Teel34592252015-11-04 15:52:09 -06008387 } else if (h->discovery_polling) {
Scott Teelc2adae42015-11-04 15:52:16 -06008388 hpsa_disable_rld_caching(h);
Scott Teel34592252015-11-04 15:52:09 -06008389 if (hpsa_luns_changed(h)) {
8390 struct Scsi_Host *sh = NULL;
8391
8392 dev_info(&h->pdev->dev,
8393 "driver discovery polling rescan.\n");
8394 sh = scsi_host_get(h->scsi_host);
8395 if (sh != NULL) {
8396 hpsa_scan_start(sh);
8397 scsi_host_put(sh);
8398 }
8399 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008400 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008401 spin_lock_irqsave(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06008402 if (!h->remove_in_progress)
8403 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008404 h->heartbeat_sample_interval);
8405 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008406}
8407
Don Brace6636e7f2015-01-23 16:45:17 -06008408static void hpsa_monitor_ctlr_worker(struct work_struct *work)
8409{
8410 unsigned long flags;
8411 struct ctlr_info *h = container_of(to_delayed_work(work),
8412 struct ctlr_info, monitor_ctlr_work);
8413
8414 detect_controller_lockup(h);
8415 if (lockup_detected(h))
8416 return;
8417
8418 spin_lock_irqsave(&h->lock, flags);
8419 if (!h->remove_in_progress)
8420 schedule_delayed_work(&h->monitor_ctlr_work,
8421 h->heartbeat_sample_interval);
8422 spin_unlock_irqrestore(&h->lock, flags);
8423}
8424
8425static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
8426 char *name)
8427{
8428 struct workqueue_struct *wq = NULL;
Don Brace6636e7f2015-01-23 16:45:17 -06008429
Don Brace397ea9c2015-02-06 17:44:15 -06008430 wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
Don Brace6636e7f2015-01-23 16:45:17 -06008431 if (!wq)
8432 dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
8433
8434 return wq;
8435}
8436
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008437static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008438{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008439 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008440 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008441 int try_soft_reset = 0;
8442 unsigned long flags;
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008443 u32 board_id;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008444
8445 if (number_of_controllers == 0)
8446 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008447
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008448 rc = hpsa_lookup_board_id(pdev, &board_id);
8449 if (rc < 0) {
8450 dev_warn(&pdev->dev, "Board ID not found\n");
8451 return rc;
8452 }
8453
8454 rc = hpsa_init_reset_devices(pdev, board_id);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008455 if (rc) {
8456 if (rc != -ENOTSUPP)
8457 return rc;
8458 /* If the reset fails in a particular way (it has no way to do
8459 * a proper hard reset, so returns -ENOTSUPP) we can try to do
8460 * a soft reset once we get the controller configured up to the
8461 * point that it can accept a command.
8462 */
8463 try_soft_reset = 1;
8464 rc = 0;
8465 }
8466
8467reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008468
Don Brace303932f2010-02-04 08:42:40 -06008469 /* Command structures must be aligned on a 32-byte boundary because
8470 * the 5 lower bits of the address are used by the hardware. and by
8471 * the driver. See comments in hpsa.h for more info.
8472 */
Don Brace303932f2010-02-04 08:42:40 -06008473 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008474 h = kzalloc(sizeof(*h), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05008475 if (!h) {
8476 dev_err(&pdev->dev, "Failed to allocate controller head\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008477 return -ENOMEM;
Robert Elliott105a3db2015-04-23 09:33:48 -05008478 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008479
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008480 h->pdev = pdev;
Robert Elliott105a3db2015-04-23 09:33:48 -05008481
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06008482 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008483 INIT_LIST_HEAD(&h->offline_device_list);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008484 spin_lock_init(&h->lock);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008485 spin_lock_init(&h->offline_device_lock);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008486 spin_lock_init(&h->scan_lock);
Don Brace34f0c622015-01-23 16:43:46 -06008487 atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05008488 atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008489
8490 /* Allocate and clear per-cpu variable lockup_detected */
8491 h->lockup_detected = alloc_percpu(u32);
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008492 if (!h->lockup_detected) {
Robert Elliott105a3db2015-04-23 09:33:48 -05008493 dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008494 rc = -ENOMEM;
Robert Elliott2efa5922015-04-23 09:34:53 -05008495 goto clean1; /* aer/h */
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008496 }
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008497 set_lockup_detected_for_all_cpus(h, 0);
8498
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008499 rc = hpsa_pci_init(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05008500 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008501 goto clean2; /* lu, aer/h */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008502
Robert Elliott2946e822015-04-23 09:35:09 -05008503 /* relies on h-> settings made by hpsa_pci_init, including
8504 * interrupt_mode h->intr */
8505 rc = hpsa_scsi_host_alloc(h);
8506 if (rc)
8507 goto clean2_5; /* pci, lu, aer/h */
8508
8509 sprintf(h->devname, HPSA "%d", h->scsi_host->host_no);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008510 h->ctlr = number_of_controllers;
8511 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008512
8513 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008514 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
8515 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008516 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008517 } else {
8518 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8519 if (rc == 0) {
8520 dac = 0;
8521 } else {
8522 dev_err(&pdev->dev, "no suitable DMA available\n");
Robert Elliott2946e822015-04-23 09:35:09 -05008523 goto clean3; /* shost, pci, lu, aer/h */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008524 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008525 }
8526
8527 /* make sure the board interrupts are off */
8528 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05008529
Robert Elliott105a3db2015-04-23 09:33:48 -05008530 rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
8531 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008532 goto clean3; /* shost, pci, lu, aer/h */
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008533 rc = hpsa_alloc_cmd_pool(h);
Robert Elliott8947fd12015-01-23 16:42:54 -06008534 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008535 goto clean4; /* irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008536 rc = hpsa_alloc_sg_chain_blocks(h);
8537 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008538 goto clean5; /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Camerona08a8472010-02-04 08:43:16 -06008539 init_waitqueue_head(&h->scan_wait_queue);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05008540 init_waitqueue_head(&h->abort_cmd_wait_queue);
Webb Scalesd604f532015-04-23 09:35:22 -05008541 init_waitqueue_head(&h->event_sync_wait_queue);
8542 mutex_init(&h->reset_mutex);
Stephen M. Camerona08a8472010-02-04 08:43:16 -06008543 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008544
8545 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008546 h->ndevices = 0;
Robert Elliott2946e822015-04-23 09:35:09 -05008547
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008548 spin_lock_init(&h->devlock);
Robert Elliott105a3db2015-04-23 09:33:48 -05008549 rc = hpsa_put_ctlr_into_performant_mode(h);
8550 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008551 goto clean6; /* sg, cmd, irq, shost, pci, lu, aer/h */
8552
8553 /* hook into SCSI subsystem */
8554 rc = hpsa_scsi_add_host(h);
8555 if (rc)
8556 goto clean7; /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
Robert Elliott2efa5922015-04-23 09:34:53 -05008557
8558 /* create the resubmit workqueue */
8559 h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
8560 if (!h->rescan_ctlr_wq) {
8561 rc = -ENOMEM;
8562 goto clean7;
8563 }
8564
8565 h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
8566 if (!h->resubmit_wq) {
8567 rc = -ENOMEM;
8568 goto clean7; /* aer/h */
8569 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008570
Robert Elliott105a3db2015-04-23 09:33:48 -05008571 /*
8572 * At this point, the controller is ready to take commands.
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008573 * Now, if reset_devices and the hard reset didn't work, try
8574 * the soft reset and see if that works.
8575 */
8576 if (try_soft_reset) {
8577
8578 /* This is kind of gross. We may or may not get a completion
8579 * from the soft reset command, and if we do, then the value
8580 * from the fifo may or may not be valid. So, we wait 10 secs
8581 * after the reset throwing away any completions we get during
8582 * that time. Unregister the interrupt handler and register
8583 * fake ones to scoop up any residual completions.
8584 */
8585 spin_lock_irqsave(&h->lock, flags);
8586 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8587 spin_unlock_irqrestore(&h->lock, flags);
Robert Elliottec501a12015-01-23 16:41:40 -06008588 hpsa_free_irqs(h);
Robert Elliott9ee61792015-01-23 16:42:32 -06008589 rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008590 hpsa_intx_discard_completions);
8591 if (rc) {
Robert Elliott9ee61792015-01-23 16:42:32 -06008592 dev_warn(&h->pdev->dev,
8593 "Failed to request_irq after soft reset.\n");
Robert Elliottd4987572015-04-23 09:34:37 -05008594 /*
Robert Elliottb2ef4802015-04-23 09:34:48 -05008595 * cannot goto clean7 or free_irqs will be called
8596 * again. Instead, do its work
8597 */
8598 hpsa_free_performant_mode(h); /* clean7 */
8599 hpsa_free_sg_chain_blocks(h); /* clean6 */
8600 hpsa_free_cmd_pool(h); /* clean5 */
8601 /*
8602 * skip hpsa_free_irqs(h) clean4 since that
8603 * was just called before request_irqs failed
Robert Elliottd4987572015-04-23 09:34:37 -05008604 */
8605 goto clean3;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008606 }
8607
8608 rc = hpsa_kdump_soft_reset(h);
8609 if (rc)
8610 /* Neither hard nor soft reset worked, we're hosed. */
Don Brace7ef73232015-07-18 11:12:33 -05008611 goto clean7;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008612
8613 dev_info(&h->pdev->dev, "Board READY.\n");
8614 dev_info(&h->pdev->dev,
8615 "Waiting for stale completions to drain.\n");
8616 h->access.set_intr_mask(h, HPSA_INTR_ON);
8617 msleep(10000);
8618 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8619
8620 rc = controller_reset_failed(h->cfgtable);
8621 if (rc)
8622 dev_info(&h->pdev->dev,
8623 "Soft reset appears to have failed.\n");
8624
8625 /* since the controller's reset, we have to go back and re-init
8626 * everything. Easiest to just forget what we've done and do it
8627 * all over again.
8628 */
8629 hpsa_undo_allocations_after_kdump_soft_reset(h);
8630 try_soft_reset = 0;
8631 if (rc)
Robert Elliottb2ef4802015-04-23 09:34:48 -05008632 /* don't goto clean, we already unallocated */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008633 return -ENODEV;
8634
8635 goto reinit_after_soft_reset;
8636 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008637
Robert Elliott105a3db2015-04-23 09:33:48 -05008638 /* Enable Accelerated IO path at driver layer */
8639 h->acciopath_status = 1;
Scott Teel34592252015-11-04 15:52:09 -06008640 /* Disable discovery polling.*/
8641 h->discovery_polling = 0;
Scott Teelda0697b2014-02-18 13:57:00 -06008642
Scott Teele863d682014-02-18 13:57:05 -06008643
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008644 /* Turn the interrupts on so we can service requests */
8645 h->access.set_intr_mask(h, HPSA_INTR_ON);
8646
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06008647 hpsa_hba_inquiry(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008648
Scott Teel34592252015-11-04 15:52:09 -06008649 h->lastlogicals = kzalloc(sizeof(*(h->lastlogicals)), GFP_KERNEL);
8650 if (!h->lastlogicals)
8651 dev_info(&h->pdev->dev,
8652 "Can't track change to report lun data\n");
8653
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008654 /* Monitor the controller for firmware lockups */
8655 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
8656 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
8657 schedule_delayed_work(&h->monitor_ctlr_work,
8658 h->heartbeat_sample_interval);
Don Brace6636e7f2015-01-23 16:45:17 -06008659 INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
8660 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
8661 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05008662 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008663
Robert Elliott2946e822015-04-23 09:35:09 -05008664clean7: /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008665 hpsa_free_performant_mode(h);
8666 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8667clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06008668 hpsa_free_sg_chain_blocks(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008669clean5: /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008670 hpsa_free_cmd_pool(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008671clean4: /* irq, shost, pci, lu, aer/h */
Robert Elliottec501a12015-01-23 16:41:40 -06008672 hpsa_free_irqs(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008673clean3: /* shost, pci, lu, aer/h */
8674 scsi_host_put(h->scsi_host);
8675 h->scsi_host = NULL;
8676clean2_5: /* pci, lu, aer/h */
Robert Elliott195f2c62015-04-23 09:33:17 -05008677 hpsa_free_pci_init(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008678clean2: /* lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008679 if (h->lockup_detected) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008680 free_percpu(h->lockup_detected);
Robert Elliott105a3db2015-04-23 09:33:48 -05008681 h->lockup_detected = NULL;
8682 }
8683clean1: /* wq/aer/h */
8684 if (h->resubmit_wq) {
8685 destroy_workqueue(h->resubmit_wq);
8686 h->resubmit_wq = NULL;
8687 }
8688 if (h->rescan_ctlr_wq) {
8689 destroy_workqueue(h->rescan_ctlr_wq);
8690 h->rescan_ctlr_wq = NULL;
8691 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008692 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008693 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008694}
8695
8696static void hpsa_flush_cache(struct ctlr_info *h)
8697{
8698 char *flush_buf;
8699 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05008700 int rc;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008701
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008702 if (unlikely(lockup_detected(h)))
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008703 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008704 flush_buf = kzalloc(4, GFP_KERNEL);
8705 if (!flush_buf)
8706 return;
8707
Stephen Cameron45fcb862015-01-23 16:43:04 -06008708 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05008709
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008710 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
8711 RAID_CTLR_LUNID, TYPE_CMD)) {
8712 goto out;
8713 }
Webb Scales25163bd2015-04-23 09:32:00 -05008714 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
8715 PCI_DMA_TODEVICE, NO_TIMEOUT);
8716 if (rc)
8717 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008718 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008719out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008720 dev_warn(&h->pdev->dev,
8721 "error flushing cache on controller\n");
Stephen Cameron45fcb862015-01-23 16:43:04 -06008722 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008723 kfree(flush_buf);
8724}
8725
Scott Teelc2adae42015-11-04 15:52:16 -06008726/* Make controller gather fresh report lun data each time we
8727 * send down a report luns request
8728 */
8729static void hpsa_disable_rld_caching(struct ctlr_info *h)
8730{
8731 u32 *options;
8732 struct CommandList *c;
8733 int rc;
8734
8735 /* Don't bother trying to set diag options if locked up */
8736 if (unlikely(h->lockup_detected))
8737 return;
8738
8739 options = kzalloc(sizeof(*options), GFP_KERNEL);
8740 if (!options) {
8741 dev_err(&h->pdev->dev,
8742 "Error: failed to disable rld caching, during alloc.\n");
8743 return;
8744 }
8745
8746 c = cmd_alloc(h);
8747
8748 /* first, get the current diag options settings */
8749 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
8750 RAID_CTLR_LUNID, TYPE_CMD))
8751 goto errout;
8752
8753 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
8754 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
8755 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8756 goto errout;
8757
8758 /* Now, set the bit for disabling the RLD caching */
8759 *options |= HPSA_DIAG_OPTS_DISABLE_RLD_CACHING;
8760
8761 if (fill_cmd(c, BMIC_SET_DIAG_OPTIONS, h, options, 4, 0,
8762 RAID_CTLR_LUNID, TYPE_CMD))
8763 goto errout;
8764
8765 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
8766 PCI_DMA_TODEVICE, NO_TIMEOUT);
8767 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8768 goto errout;
8769
8770 /* Now verify that it got set: */
8771 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
8772 RAID_CTLR_LUNID, TYPE_CMD))
8773 goto errout;
8774
8775 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
8776 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
8777 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8778 goto errout;
8779
Dan Carpenterd8a080c2015-11-12 12:43:38 +03008780 if (*options & HPSA_DIAG_OPTS_DISABLE_RLD_CACHING)
Scott Teelc2adae42015-11-04 15:52:16 -06008781 goto out;
8782
8783errout:
8784 dev_err(&h->pdev->dev,
8785 "Error: failed to disable report lun data caching.\n");
8786out:
8787 cmd_free(h, c);
8788 kfree(options);
8789}
8790
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008791static void hpsa_shutdown(struct pci_dev *pdev)
8792{
8793 struct ctlr_info *h;
8794
8795 h = pci_get_drvdata(pdev);
8796 /* Turn board interrupts off and send the flush cache command
8797 * sendcmd will turn off interrupt, and send the flush...
8798 * To write all data in the battery backed cache to disks
8799 */
8800 hpsa_flush_cache(h);
8801 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Robert Elliott105a3db2015-04-23 09:33:48 -05008802 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliottcc64c812015-04-23 09:33:12 -05008803 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008804}
8805
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008806static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008807{
8808 int i;
8809
Robert Elliott105a3db2015-04-23 09:33:48 -05008810 for (i = 0; i < h->ndevices; i++) {
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008811 kfree(h->dev[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008812 h->dev[i] = NULL;
8813 }
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008814}
8815
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008816static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008817{
8818 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008819 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008820
8821 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008822 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008823 return;
8824 }
8825 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008826
8827 /* Get rid of any controller monitoring work items */
8828 spin_lock_irqsave(&h->lock, flags);
8829 h->remove_in_progress = 1;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008830 spin_unlock_irqrestore(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06008831 cancel_delayed_work_sync(&h->monitor_ctlr_work);
8832 cancel_delayed_work_sync(&h->rescan_ctlr_work);
8833 destroy_workqueue(h->rescan_ctlr_wq);
8834 destroy_workqueue(h->resubmit_wq);
Robert Elliottcc64c812015-04-23 09:33:12 -05008835
Don Brace2d041302015-07-18 11:13:15 -05008836 /*
8837 * Call before disabling interrupts.
8838 * scsi_remove_host can trigger I/O operations especially
8839 * when multipath is enabled. There can be SYNCHRONIZE CACHE
8840 * operations which cannot complete and will hang the system.
8841 */
8842 if (h->scsi_host)
8843 scsi_remove_host(h->scsi_host); /* init_one 8 */
Robert Elliott105a3db2015-04-23 09:33:48 -05008844 /* includes hpsa_free_irqs - init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008845 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008846 hpsa_shutdown(pdev);
Robert Elliottcc64c812015-04-23 09:33:12 -05008847
Robert Elliott105a3db2015-04-23 09:33:48 -05008848 hpsa_free_device_info(h); /* scan */
8849
Robert Elliott2946e822015-04-23 09:35:09 -05008850 kfree(h->hba_inquiry_data); /* init_one 10 */
8851 h->hba_inquiry_data = NULL; /* init_one 10 */
Robert Elliott2946e822015-04-23 09:35:09 -05008852 hpsa_free_ioaccel2_sg_chain_blocks(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05008853 hpsa_free_performant_mode(h); /* init_one 7 */
8854 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
8855 hpsa_free_cmd_pool(h); /* init_one 5 */
Scott Teel34592252015-11-04 15:52:09 -06008856 kfree(h->lastlogicals);
Robert Elliott105a3db2015-04-23 09:33:48 -05008857
8858 /* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008859
Robert Elliott2946e822015-04-23 09:35:09 -05008860 scsi_host_put(h->scsi_host); /* init_one 3 */
8861 h->scsi_host = NULL; /* init_one 3 */
8862
Robert Elliott195f2c62015-04-23 09:33:17 -05008863 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Robert Elliott2946e822015-04-23 09:35:09 -05008864 hpsa_free_pci_init(h); /* init_one 2.5 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008865
Robert Elliott105a3db2015-04-23 09:33:48 -05008866 free_percpu(h->lockup_detected); /* init_one 2 */
8867 h->lockup_detected = NULL; /* init_one 2 */
8868 /* (void) pci_disable_pcie_error_reporting(pdev); */ /* init_one 1 */
Kevin Barnettd04e62b2015-11-04 15:52:34 -06008869
8870 hpsa_delete_sas_host(h);
8871
Robert Elliott105a3db2015-04-23 09:33:48 -05008872 kfree(h); /* init_one 1 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008873}
8874
8875static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
8876 __attribute__((unused)) pm_message_t state)
8877{
8878 return -ENOSYS;
8879}
8880
8881static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
8882{
8883 return -ENOSYS;
8884}
8885
8886static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06008887 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008888 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008889 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008890 .id_table = hpsa_pci_device_id, /* id_table */
8891 .shutdown = hpsa_shutdown,
8892 .suspend = hpsa_suspend,
8893 .resume = hpsa_resume,
8894};
8895
Don Brace303932f2010-02-04 08:42:40 -06008896/* Fill in bucket_map[], given nsgs (the max number of
8897 * scatter gather elements supported) and bucket[],
8898 * which is an array of 8 integers. The bucket[] array
8899 * contains 8 different DMA transfer sizes (in 16
8900 * byte increments) which the controller uses to fetch
8901 * commands. This function fills in bucket_map[], which
8902 * maps a given number of scatter gather elements to one of
8903 * the 8 DMA transfer sizes. The point of it is to allow the
8904 * controller to only do as much DMA as needed to fetch the
8905 * command, with the DMA transfer size encoded in the lower
8906 * bits of the command address.
8907 */
8908static void calc_bucket_map(int bucket[], int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -06008909 int nsgs, int min_blocks, u32 *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06008910{
8911 int i, j, b, size;
8912
Don Brace303932f2010-02-04 08:42:40 -06008913 /* Note, bucket_map must have nsgs+1 entries. */
8914 for (i = 0; i <= nsgs; i++) {
8915 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06008916 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06008917 b = num_buckets; /* Assume the biggest bucket */
8918 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06008919 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06008920 if (bucket[j] >= size) {
8921 b = j;
8922 break;
8923 }
8924 }
8925 /* for a command with i SG entries, use bucket b. */
8926 bucket_map[i] = b;
8927 }
8928}
8929
Robert Elliott105a3db2015-04-23 09:33:48 -05008930/*
8931 * return -ENODEV on err, 0 on success (or no action)
8932 * allocates numerous items that must be freed later
8933 */
Robert Elliottc706a792015-01-23 16:45:01 -06008934static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06008935{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05008936 int i;
8937 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06008938 unsigned long transMethod = CFGTBL_Trans_Performant |
8939 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008940 CFGTBL_Trans_enable_directed_msix |
8941 (trans_support & (CFGTBL_Trans_io_accel1 |
8942 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06008943 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05008944
8945 /* This is a bit complicated. There are 8 registers on
8946 * the controller which we write to to tell it 8 different
8947 * sizes of commands which there may be. It's a way of
8948 * reducing the DMA done to fetch each command. Encoded into
8949 * each command's tag are 3 bits which communicate to the controller
8950 * which of the eight sizes that command fits within. The size of
8951 * each command depends on how many scatter gather entries there are.
8952 * Each SG entry requires 16 bytes. The eight registers are programmed
8953 * with the number of 16-byte blocks a command of that size requires.
8954 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008955 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05008956 * blocks. Note, this only extends to the SG entries contained
8957 * within the command block, and does not extend to chained blocks
8958 * of SG elements. bft[] contains the eight values we write to
8959 * the registers. They are not evenly distributed, but have more
8960 * sizes for small commands, and fewer sizes for larger commands.
8961 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008962 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008963#define MIN_IOACCEL2_BFT_ENTRY 5
8964#define HPSA_IOACCEL2_HEADER_SZ 4
8965 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
8966 13, 14, 15, 16, 17, 18, 19,
8967 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
8968 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
8969 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
8970 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
8971 16 * MIN_IOACCEL2_BFT_ENTRY);
8972 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008973 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06008974 /* 5 = 1 s/g entry or 4k
8975 * 6 = 2 s/g entry or 8k
8976 * 8 = 4 s/g entry or 16k
8977 * 10 = 6 s/g entry or 24k
8978 */
Don Brace303932f2010-02-04 08:42:40 -06008979
Stephen M. Cameronb3a52e72014-05-29 10:53:23 -05008980 /* If the controller supports either ioaccel method then
8981 * we can also use the RAID stack submit path that does not
8982 * perform the superfluous readl() after each command submission.
8983 */
8984 if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
8985 access = SA5_performant_access_no_read;
8986
Don Brace303932f2010-02-04 08:42:40 -06008987 /* Controller spec: zero out this buffer. */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008988 for (i = 0; i < h->nreply_queues; i++)
8989 memset(h->reply_queue[i].head, 0, h->reply_queue_size);
Don Brace303932f2010-02-04 08:42:40 -06008990
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008991 bft[7] = SG_ENTRIES_IN_CMD + 4;
8992 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06008993 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06008994 for (i = 0; i < 8; i++)
8995 writel(bft[i], &h->transtable->BlockFetch[i]);
8996
8997 /* size of controller ring buffer */
8998 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05008999 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06009000 writel(0, &h->transtable->RepQCtrAddrLow32);
9001 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05009002
9003 for (i = 0; i < h->nreply_queues; i++) {
9004 writel(0, &h->transtable->RepQAddr[i].upper);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009005 writel(h->reply_queue[i].busaddr,
Matt Gates254f7962012-05-01 11:43:06 -05009006 &h->transtable->RepQAddr[i].lower);
9007 }
9008
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009009 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06009010 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
9011 /*
9012 * enable outbound interrupt coalescing in accelerator mode;
9013 */
9014 if (trans_support & CFGTBL_Trans_io_accel1) {
9015 access = SA5_ioaccel_mode1_access;
9016 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
9017 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Scott Teelc3497752014-02-18 13:56:34 -06009018 } else {
9019 if (trans_support & CFGTBL_Trans_io_accel2) {
9020 access = SA5_ioaccel_mode2_access;
9021 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
9022 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
9023 }
Matt Gatese1f7de02014-02-18 13:55:17 -06009024 }
Don Brace303932f2010-02-04 08:42:40 -06009025 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06009026 if (hpsa_wait_for_mode_change_ack(h)) {
9027 dev_err(&h->pdev->dev,
9028 "performant mode problem - doorbell timeout\n");
9029 return -ENODEV;
9030 }
Don Brace303932f2010-02-04 08:42:40 -06009031 register_value = readl(&(h->cfgtable->TransportActive));
9032 if (!(register_value & CFGTBL_Trans_Performant)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06009033 dev_err(&h->pdev->dev,
9034 "performant mode problem - transport not active\n");
Robert Elliottc706a792015-01-23 16:45:01 -06009035 return -ENODEV;
Don Brace303932f2010-02-04 08:42:40 -06009036 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06009037 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06009038 h->access = access;
9039 h->transMethod = transMethod;
9040
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009041 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
9042 (trans_support & CFGTBL_Trans_io_accel2)))
Robert Elliottc706a792015-01-23 16:45:01 -06009043 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06009044
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009045 if (trans_support & CFGTBL_Trans_io_accel1) {
9046 /* Set up I/O accelerator mode */
9047 for (i = 0; i < h->nreply_queues; i++) {
9048 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
9049 h->reply_queue[i].current_entry =
9050 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
9051 }
9052 bft[7] = h->ioaccel_maxsg + 8;
9053 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
9054 h->ioaccel1_blockFetchTable);
9055
9056 /* initialize all reply queue entries to unused */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009057 for (i = 0; i < h->nreply_queues; i++)
9058 memset(h->reply_queue[i].head,
9059 (u8) IOACCEL_MODE1_REPLY_UNUSED,
9060 h->reply_queue_size);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009061
9062 /* set all the constant fields in the accelerator command
9063 * frames once at init time to save CPU cycles later.
9064 */
9065 for (i = 0; i < h->nr_cmds; i++) {
9066 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
9067
9068 cp->function = IOACCEL1_FUNCTION_SCSIIO;
9069 cp->err_info = (u32) (h->errinfo_pool_dhandle +
9070 (i * sizeof(struct ErrorInfo)));
9071 cp->err_info_len = sizeof(struct ErrorInfo);
9072 cp->sgl_offset = IOACCEL1_SGLOFFSET;
Don Brace2b08b3e2015-01-23 16:41:09 -06009073 cp->host_context_flags =
9074 cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009075 cp->timeout_sec = 0;
9076 cp->ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009077 cp->tag =
Don Bracef2405db2015-01-23 16:43:09 -06009078 cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009079 cp->host_addr =
9080 cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009081 (i * sizeof(struct io_accel1_cmd)));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009082 }
9083 } else if (trans_support & CFGTBL_Trans_io_accel2) {
9084 u64 cfg_offset, cfg_base_addr_index;
9085 u32 bft2_offset, cfg_base_addr;
9086 int rc;
9087
9088 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
9089 &cfg_base_addr_index, &cfg_offset);
9090 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
9091 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
9092 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
9093 4, h->ioaccel2_blockFetchTable);
9094 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
9095 BUILD_BUG_ON(offsetof(struct CfgTable,
9096 io_accel_request_size_offset) != 0xb8);
9097 h->ioaccel2_bft2_regs =
9098 remap_pci_mem(pci_resource_start(h->pdev,
9099 cfg_base_addr_index) +
9100 cfg_offset + bft2_offset,
9101 ARRAY_SIZE(bft2) *
9102 sizeof(*h->ioaccel2_bft2_regs));
9103 for (i = 0; i < ARRAY_SIZE(bft2); i++)
9104 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06009105 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06009106 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06009107 if (hpsa_wait_for_mode_change_ack(h)) {
9108 dev_err(&h->pdev->dev,
9109 "performant mode problem - enabling ioaccel mode\n");
9110 return -ENODEV;
9111 }
9112 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06009113}
9114
Robert Elliott1fb7c982015-04-23 09:33:22 -05009115/* Free ioaccel1 mode command blocks and block fetch table */
9116static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
9117{
Robert Elliott105a3db2015-04-23 09:33:48 -05009118 if (h->ioaccel_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05009119 pci_free_consistent(h->pdev,
9120 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
9121 h->ioaccel_cmd_pool,
9122 h->ioaccel_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05009123 h->ioaccel_cmd_pool = NULL;
9124 h->ioaccel_cmd_pool_dhandle = 0;
9125 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05009126 kfree(h->ioaccel1_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009127 h->ioaccel1_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05009128}
9129
Robert Elliottd37ffbe2015-04-23 09:32:27 -05009130/* Allocate ioaccel1 mode command blocks and block fetch table */
9131static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
Matt Gatese1f7de02014-02-18 13:55:17 -06009132{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06009133 h->ioaccel_maxsg =
9134 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
9135 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
9136 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
9137
Matt Gatese1f7de02014-02-18 13:55:17 -06009138 /* Command structures must be aligned on a 128-byte boundary
9139 * because the 7 lower bits of the address are used by the
9140 * hardware.
9141 */
Matt Gatese1f7de02014-02-18 13:55:17 -06009142 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
9143 IOACCEL1_COMMANDLIST_ALIGNMENT);
9144 h->ioaccel_cmd_pool =
9145 pci_alloc_consistent(h->pdev,
9146 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
9147 &(h->ioaccel_cmd_pool_dhandle));
9148
9149 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06009150 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06009151 sizeof(u32)), GFP_KERNEL);
9152
9153 if ((h->ioaccel_cmd_pool == NULL) ||
9154 (h->ioaccel1_blockFetchTable == NULL))
9155 goto clean_up;
9156
9157 memset(h->ioaccel_cmd_pool, 0,
9158 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
9159 return 0;
9160
9161clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05009162 hpsa_free_ioaccel1_cmd_and_bft(h);
Robert Elliott2dd02d72015-04-23 09:33:43 -05009163 return -ENOMEM;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009164}
9165
Robert Elliott1fb7c982015-04-23 09:33:22 -05009166/* Free ioaccel2 mode command blocks and block fetch table */
9167static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
9168{
Webb Scalesd9a729f2015-04-23 09:33:27 -05009169 hpsa_free_ioaccel2_sg_chain_blocks(h);
9170
Robert Elliott105a3db2015-04-23 09:33:48 -05009171 if (h->ioaccel2_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05009172 pci_free_consistent(h->pdev,
9173 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
9174 h->ioaccel2_cmd_pool,
9175 h->ioaccel2_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05009176 h->ioaccel2_cmd_pool = NULL;
9177 h->ioaccel2_cmd_pool_dhandle = 0;
9178 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05009179 kfree(h->ioaccel2_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009180 h->ioaccel2_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05009181}
9182
Robert Elliottd37ffbe2015-04-23 09:32:27 -05009183/* Allocate ioaccel2 mode command blocks and block fetch table */
9184static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009185{
Webb Scalesd9a729f2015-04-23 09:33:27 -05009186 int rc;
9187
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009188 /* Allocate ioaccel2 mode command blocks and block fetch table */
9189
9190 h->ioaccel_maxsg =
9191 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
9192 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
9193 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
9194
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009195 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
9196 IOACCEL2_COMMANDLIST_ALIGNMENT);
9197 h->ioaccel2_cmd_pool =
9198 pci_alloc_consistent(h->pdev,
9199 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
9200 &(h->ioaccel2_cmd_pool_dhandle));
9201
9202 h->ioaccel2_blockFetchTable =
9203 kmalloc(((h->ioaccel_maxsg + 1) *
9204 sizeof(u32)), GFP_KERNEL);
9205
9206 if ((h->ioaccel2_cmd_pool == NULL) ||
Webb Scalesd9a729f2015-04-23 09:33:27 -05009207 (h->ioaccel2_blockFetchTable == NULL)) {
9208 rc = -ENOMEM;
9209 goto clean_up;
9210 }
9211
9212 rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
9213 if (rc)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009214 goto clean_up;
9215
9216 memset(h->ioaccel2_cmd_pool, 0,
9217 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
9218 return 0;
9219
9220clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05009221 hpsa_free_ioaccel2_cmd_and_bft(h);
Webb Scalesd9a729f2015-04-23 09:33:27 -05009222 return rc;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009223}
9224
Robert Elliott105a3db2015-04-23 09:33:48 -05009225/* Free items allocated by hpsa_put_ctlr_into_performant_mode */
9226static void hpsa_free_performant_mode(struct ctlr_info *h)
9227{
9228 kfree(h->blockFetchTable);
9229 h->blockFetchTable = NULL;
9230 hpsa_free_reply_queues(h);
9231 hpsa_free_ioaccel1_cmd_and_bft(h);
9232 hpsa_free_ioaccel2_cmd_and_bft(h);
9233}
9234
9235/* return -ENODEV on error, 0 on success (or no action)
9236 * allocates numerous items that must be freed later
9237 */
9238static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009239{
9240 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06009241 unsigned long transMethod = CFGTBL_Trans_Performant |
9242 CFGTBL_Trans_use_short_tags;
Robert Elliott105a3db2015-04-23 09:33:48 -05009243 int i, rc;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009244
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06009245 if (hpsa_simple_mode)
Robert Elliott105a3db2015-04-23 09:33:48 -05009246 return 0;
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06009247
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05009248 trans_support = readl(&(h->cfgtable->TransportSupport));
9249 if (!(trans_support & PERFORMANT_MODE))
Robert Elliott105a3db2015-04-23 09:33:48 -05009250 return 0;
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05009251
Matt Gatese1f7de02014-02-18 13:55:17 -06009252 /* Check for I/O accelerator mode support */
9253 if (trans_support & CFGTBL_Trans_io_accel1) {
9254 transMethod |= CFGTBL_Trans_io_accel1 |
9255 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05009256 rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
9257 if (rc)
9258 return rc;
9259 } else if (trans_support & CFGTBL_Trans_io_accel2) {
9260 transMethod |= CFGTBL_Trans_io_accel2 |
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009261 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05009262 rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
9263 if (rc)
9264 return rc;
Matt Gatese1f7de02014-02-18 13:55:17 -06009265 }
9266
Hannes Reineckeeee0f032014-01-15 13:30:53 +01009267 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05009268 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009269 /* Performant mode ring buffer and supporting data structures */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009270 h->reply_queue_size = h->max_commands * sizeof(u64);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009271
Matt Gates254f7962012-05-01 11:43:06 -05009272 for (i = 0; i < h->nreply_queues; i++) {
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009273 h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
9274 h->reply_queue_size,
9275 &(h->reply_queue[i].busaddr));
Robert Elliott105a3db2015-04-23 09:33:48 -05009276 if (!h->reply_queue[i].head) {
9277 rc = -ENOMEM;
9278 goto clean1; /* rq, ioaccel */
9279 }
Matt Gates254f7962012-05-01 11:43:06 -05009280 h->reply_queue[i].size = h->max_commands;
9281 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
9282 h->reply_queue[i].current_entry = 0;
9283 }
9284
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009285 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009286 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009287 sizeof(u32)), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05009288 if (!h->blockFetchTable) {
9289 rc = -ENOMEM;
9290 goto clean1; /* rq, ioaccel */
9291 }
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009292
Robert Elliott105a3db2015-04-23 09:33:48 -05009293 rc = hpsa_enter_performant_mode(h, trans_support);
9294 if (rc)
9295 goto clean2; /* bft, rq, ioaccel */
9296 return 0;
Don Brace303932f2010-02-04 08:42:40 -06009297
Robert Elliott105a3db2015-04-23 09:33:48 -05009298clean2: /* bft, rq, ioaccel */
Don Brace303932f2010-02-04 08:42:40 -06009299 kfree(h->blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009300 h->blockFetchTable = NULL;
9301clean1: /* rq, ioaccel */
9302 hpsa_free_reply_queues(h);
9303 hpsa_free_ioaccel1_cmd_and_bft(h);
9304 hpsa_free_ioaccel2_cmd_and_bft(h);
9305 return rc;
Don Brace303932f2010-02-04 08:42:40 -06009306}
9307
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009308static int is_accelerated_cmd(struct CommandList *c)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009309{
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009310 return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
9311}
9312
9313static void hpsa_drain_accel_commands(struct ctlr_info *h)
9314{
9315 struct CommandList *c = NULL;
Don Bracef2405db2015-01-23 16:43:09 -06009316 int i, accel_cmds_out;
Webb Scales281a7fd2015-01-23 16:43:35 -06009317 int refcount;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009318
Don Bracef2405db2015-01-23 16:43:09 -06009319 do { /* wait for all outstanding ioaccel commands to drain out */
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009320 accel_cmds_out = 0;
Don Bracef2405db2015-01-23 16:43:09 -06009321 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06009322 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06009323 refcount = atomic_inc_return(&c->refcount);
9324 if (refcount > 1) /* Command is allocated */
9325 accel_cmds_out += is_accelerated_cmd(c);
9326 cmd_free(h, c);
Don Bracef2405db2015-01-23 16:43:09 -06009327 }
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009328 if (accel_cmds_out <= 0)
Webb Scales281a7fd2015-01-23 16:43:35 -06009329 break;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009330 msleep(100);
9331 } while (1);
9332}
9333
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009334static struct hpsa_sas_phy *hpsa_alloc_sas_phy(
9335 struct hpsa_sas_port *hpsa_sas_port)
9336{
9337 struct hpsa_sas_phy *hpsa_sas_phy;
9338 struct sas_phy *phy;
9339
9340 hpsa_sas_phy = kzalloc(sizeof(*hpsa_sas_phy), GFP_KERNEL);
9341 if (!hpsa_sas_phy)
9342 return NULL;
9343
9344 phy = sas_phy_alloc(hpsa_sas_port->parent_node->parent_dev,
9345 hpsa_sas_port->next_phy_index);
9346 if (!phy) {
9347 kfree(hpsa_sas_phy);
9348 return NULL;
9349 }
9350
9351 hpsa_sas_port->next_phy_index++;
9352 hpsa_sas_phy->phy = phy;
9353 hpsa_sas_phy->parent_port = hpsa_sas_port;
9354
9355 return hpsa_sas_phy;
9356}
9357
9358static void hpsa_free_sas_phy(struct hpsa_sas_phy *hpsa_sas_phy)
9359{
9360 struct sas_phy *phy = hpsa_sas_phy->phy;
9361
9362 sas_port_delete_phy(hpsa_sas_phy->parent_port->port, phy);
9363 sas_phy_free(phy);
9364 if (hpsa_sas_phy->added_to_port)
9365 list_del(&hpsa_sas_phy->phy_list_entry);
9366 kfree(hpsa_sas_phy);
9367}
9368
9369static int hpsa_sas_port_add_phy(struct hpsa_sas_phy *hpsa_sas_phy)
9370{
9371 int rc;
9372 struct hpsa_sas_port *hpsa_sas_port;
9373 struct sas_phy *phy;
9374 struct sas_identify *identify;
9375
9376 hpsa_sas_port = hpsa_sas_phy->parent_port;
9377 phy = hpsa_sas_phy->phy;
9378
9379 identify = &phy->identify;
9380 memset(identify, 0, sizeof(*identify));
9381 identify->sas_address = hpsa_sas_port->sas_address;
9382 identify->device_type = SAS_END_DEVICE;
9383 identify->initiator_port_protocols = SAS_PROTOCOL_STP;
9384 identify->target_port_protocols = SAS_PROTOCOL_STP;
9385 phy->minimum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
9386 phy->maximum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
9387 phy->minimum_linkrate = SAS_LINK_RATE_UNKNOWN;
9388 phy->maximum_linkrate = SAS_LINK_RATE_UNKNOWN;
9389 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
9390
9391 rc = sas_phy_add(hpsa_sas_phy->phy);
9392 if (rc)
9393 return rc;
9394
9395 sas_port_add_phy(hpsa_sas_port->port, hpsa_sas_phy->phy);
9396 list_add_tail(&hpsa_sas_phy->phy_list_entry,
9397 &hpsa_sas_port->phy_list_head);
9398 hpsa_sas_phy->added_to_port = true;
9399
9400 return 0;
9401}
9402
9403static int
9404 hpsa_sas_port_add_rphy(struct hpsa_sas_port *hpsa_sas_port,
9405 struct sas_rphy *rphy)
9406{
9407 struct sas_identify *identify;
9408
9409 identify = &rphy->identify;
9410 identify->sas_address = hpsa_sas_port->sas_address;
9411 identify->initiator_port_protocols = SAS_PROTOCOL_STP;
9412 identify->target_port_protocols = SAS_PROTOCOL_STP;
9413
9414 return sas_rphy_add(rphy);
9415}
9416
9417static struct hpsa_sas_port
9418 *hpsa_alloc_sas_port(struct hpsa_sas_node *hpsa_sas_node,
9419 u64 sas_address)
9420{
9421 int rc;
9422 struct hpsa_sas_port *hpsa_sas_port;
9423 struct sas_port *port;
9424
9425 hpsa_sas_port = kzalloc(sizeof(*hpsa_sas_port), GFP_KERNEL);
9426 if (!hpsa_sas_port)
9427 return NULL;
9428
9429 INIT_LIST_HEAD(&hpsa_sas_port->phy_list_head);
9430 hpsa_sas_port->parent_node = hpsa_sas_node;
9431
9432 port = sas_port_alloc_num(hpsa_sas_node->parent_dev);
9433 if (!port)
9434 goto free_hpsa_port;
9435
9436 rc = sas_port_add(port);
9437 if (rc)
9438 goto free_sas_port;
9439
9440 hpsa_sas_port->port = port;
9441 hpsa_sas_port->sas_address = sas_address;
9442 list_add_tail(&hpsa_sas_port->port_list_entry,
9443 &hpsa_sas_node->port_list_head);
9444
9445 return hpsa_sas_port;
9446
9447free_sas_port:
9448 sas_port_free(port);
9449free_hpsa_port:
9450 kfree(hpsa_sas_port);
9451
9452 return NULL;
9453}
9454
9455static void hpsa_free_sas_port(struct hpsa_sas_port *hpsa_sas_port)
9456{
9457 struct hpsa_sas_phy *hpsa_sas_phy;
9458 struct hpsa_sas_phy *next;
9459
9460 list_for_each_entry_safe(hpsa_sas_phy, next,
9461 &hpsa_sas_port->phy_list_head, phy_list_entry)
9462 hpsa_free_sas_phy(hpsa_sas_phy);
9463
9464 sas_port_delete(hpsa_sas_port->port);
9465 list_del(&hpsa_sas_port->port_list_entry);
9466 kfree(hpsa_sas_port);
9467}
9468
9469static struct hpsa_sas_node *hpsa_alloc_sas_node(struct device *parent_dev)
9470{
9471 struct hpsa_sas_node *hpsa_sas_node;
9472
9473 hpsa_sas_node = kzalloc(sizeof(*hpsa_sas_node), GFP_KERNEL);
9474 if (hpsa_sas_node) {
9475 hpsa_sas_node->parent_dev = parent_dev;
9476 INIT_LIST_HEAD(&hpsa_sas_node->port_list_head);
9477 }
9478
9479 return hpsa_sas_node;
9480}
9481
9482static void hpsa_free_sas_node(struct hpsa_sas_node *hpsa_sas_node)
9483{
9484 struct hpsa_sas_port *hpsa_sas_port;
9485 struct hpsa_sas_port *next;
9486
9487 if (!hpsa_sas_node)
9488 return;
9489
9490 list_for_each_entry_safe(hpsa_sas_port, next,
9491 &hpsa_sas_node->port_list_head, port_list_entry)
9492 hpsa_free_sas_port(hpsa_sas_port);
9493
9494 kfree(hpsa_sas_node);
9495}
9496
9497static struct hpsa_scsi_dev_t
9498 *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
9499 struct sas_rphy *rphy)
9500{
9501 int i;
9502 struct hpsa_scsi_dev_t *device;
9503
9504 for (i = 0; i < h->ndevices; i++) {
9505 device = h->dev[i];
9506 if (!device->sas_port)
9507 continue;
9508 if (device->sas_port->rphy == rphy)
9509 return device;
9510 }
9511
9512 return NULL;
9513}
9514
9515static int hpsa_add_sas_host(struct ctlr_info *h)
9516{
9517 int rc;
9518 struct device *parent_dev;
9519 struct hpsa_sas_node *hpsa_sas_node;
9520 struct hpsa_sas_port *hpsa_sas_port;
9521 struct hpsa_sas_phy *hpsa_sas_phy;
9522
9523 parent_dev = &h->scsi_host->shost_gendev;
9524
9525 hpsa_sas_node = hpsa_alloc_sas_node(parent_dev);
9526 if (!hpsa_sas_node)
9527 return -ENOMEM;
9528
9529 hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, h->sas_address);
9530 if (!hpsa_sas_port) {
9531 rc = -ENODEV;
9532 goto free_sas_node;
9533 }
9534
9535 hpsa_sas_phy = hpsa_alloc_sas_phy(hpsa_sas_port);
9536 if (!hpsa_sas_phy) {
9537 rc = -ENODEV;
9538 goto free_sas_port;
9539 }
9540
9541 rc = hpsa_sas_port_add_phy(hpsa_sas_phy);
9542 if (rc)
9543 goto free_sas_phy;
9544
9545 h->sas_host = hpsa_sas_node;
9546
9547 return 0;
9548
9549free_sas_phy:
9550 hpsa_free_sas_phy(hpsa_sas_phy);
9551free_sas_port:
9552 hpsa_free_sas_port(hpsa_sas_port);
9553free_sas_node:
9554 hpsa_free_sas_node(hpsa_sas_node);
9555
9556 return rc;
9557}
9558
9559static void hpsa_delete_sas_host(struct ctlr_info *h)
9560{
9561 hpsa_free_sas_node(h->sas_host);
9562}
9563
9564static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
9565 struct hpsa_scsi_dev_t *device)
9566{
9567 int rc;
9568 struct hpsa_sas_port *hpsa_sas_port;
9569 struct sas_rphy *rphy;
9570
9571 hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, device->sas_address);
9572 if (!hpsa_sas_port)
9573 return -ENOMEM;
9574
9575 rphy = sas_end_device_alloc(hpsa_sas_port->port);
9576 if (!rphy) {
9577 rc = -ENODEV;
9578 goto free_sas_port;
9579 }
9580
9581 hpsa_sas_port->rphy = rphy;
9582 device->sas_port = hpsa_sas_port;
9583
9584 rc = hpsa_sas_port_add_rphy(hpsa_sas_port, rphy);
9585 if (rc)
9586 goto free_sas_port;
9587
9588 return 0;
9589
9590free_sas_port:
9591 hpsa_free_sas_port(hpsa_sas_port);
9592 device->sas_port = NULL;
9593
9594 return rc;
9595}
9596
9597static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device)
9598{
9599 if (device->sas_port) {
9600 hpsa_free_sas_port(device->sas_port);
9601 device->sas_port = NULL;
9602 }
9603}
9604
9605static int
9606hpsa_sas_get_linkerrors(struct sas_phy *phy)
9607{
9608 return 0;
9609}
9610
9611static int
9612hpsa_sas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
9613{
9614 return 0;
9615}
9616
9617static int
9618hpsa_sas_get_bay_identifier(struct sas_rphy *rphy)
9619{
9620 return -ENXIO;
9621}
9622
9623static int
9624hpsa_sas_phy_reset(struct sas_phy *phy, int hard_reset)
9625{
9626 return 0;
9627}
9628
9629static int
9630hpsa_sas_phy_enable(struct sas_phy *phy, int enable)
9631{
9632 return 0;
9633}
9634
9635static int
9636hpsa_sas_phy_setup(struct sas_phy *phy)
9637{
9638 return 0;
9639}
9640
9641static void
9642hpsa_sas_phy_release(struct sas_phy *phy)
9643{
9644}
9645
9646static int
9647hpsa_sas_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
9648{
9649 return -EINVAL;
9650}
9651
9652/* SMP = Serial Management Protocol */
9653static int
9654hpsa_sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
9655struct request *req)
9656{
9657 return -EINVAL;
9658}
9659
9660static struct sas_function_template hpsa_sas_transport_functions = {
9661 .get_linkerrors = hpsa_sas_get_linkerrors,
9662 .get_enclosure_identifier = hpsa_sas_get_enclosure_identifier,
9663 .get_bay_identifier = hpsa_sas_get_bay_identifier,
9664 .phy_reset = hpsa_sas_phy_reset,
9665 .phy_enable = hpsa_sas_phy_enable,
9666 .phy_setup = hpsa_sas_phy_setup,
9667 .phy_release = hpsa_sas_phy_release,
9668 .set_phy_speed = hpsa_sas_phy_speed,
9669 .smp_handler = hpsa_sas_smp_handler,
9670};
9671
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009672/*
9673 * This is it. Register the PCI driver information for the cards we control
9674 * the OS will call our registered routines when it finds one of our cards.
9675 */
9676static int __init hpsa_init(void)
9677{
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009678 int rc;
9679
9680 hpsa_sas_transport_template =
9681 sas_attach_transport(&hpsa_sas_transport_functions);
9682 if (!hpsa_sas_transport_template)
9683 return -ENODEV;
9684
9685 rc = pci_register_driver(&hpsa_pci_driver);
9686
9687 if (rc)
9688 sas_release_transport(hpsa_sas_transport_template);
9689
9690 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009691}
9692
9693static void __exit hpsa_cleanup(void)
9694{
9695 pci_unregister_driver(&hpsa_pci_driver);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009696 sas_release_transport(hpsa_sas_transport_template);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009697}
9698
Matt Gatese1f7de02014-02-18 13:55:17 -06009699static void __attribute__((unused)) verify_offsets(void)
9700{
9701#define VERIFY_OFFSET(member, offset) \
Scott Teeldd0e19f2014-02-18 13:57:31 -06009702 BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
9703
9704 VERIFY_OFFSET(structure_size, 0);
9705 VERIFY_OFFSET(volume_blk_size, 4);
9706 VERIFY_OFFSET(volume_blk_cnt, 8);
9707 VERIFY_OFFSET(phys_blk_shift, 16);
9708 VERIFY_OFFSET(parity_rotation_shift, 17);
9709 VERIFY_OFFSET(strip_size, 18);
9710 VERIFY_OFFSET(disk_starting_blk, 20);
9711 VERIFY_OFFSET(disk_blk_cnt, 28);
9712 VERIFY_OFFSET(data_disks_per_row, 36);
9713 VERIFY_OFFSET(metadata_disks_per_row, 38);
9714 VERIFY_OFFSET(row_cnt, 40);
9715 VERIFY_OFFSET(layout_map_count, 42);
9716 VERIFY_OFFSET(flags, 44);
9717 VERIFY_OFFSET(dekindex, 46);
9718 /* VERIFY_OFFSET(reserved, 48 */
9719 VERIFY_OFFSET(data, 64);
9720
9721#undef VERIFY_OFFSET
9722
9723#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06009724 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
9725
9726 VERIFY_OFFSET(IU_type, 0);
9727 VERIFY_OFFSET(direction, 1);
9728 VERIFY_OFFSET(reply_queue, 2);
9729 /* VERIFY_OFFSET(reserved1, 3); */
9730 VERIFY_OFFSET(scsi_nexus, 4);
9731 VERIFY_OFFSET(Tag, 8);
9732 VERIFY_OFFSET(cdb, 16);
9733 VERIFY_OFFSET(cciss_lun, 32);
9734 VERIFY_OFFSET(data_len, 40);
9735 VERIFY_OFFSET(cmd_priority_task_attr, 44);
9736 VERIFY_OFFSET(sg_count, 45);
9737 /* VERIFY_OFFSET(reserved3 */
9738 VERIFY_OFFSET(err_ptr, 48);
9739 VERIFY_OFFSET(err_len, 56);
9740 /* VERIFY_OFFSET(reserved4 */
9741 VERIFY_OFFSET(sg, 64);
9742
9743#undef VERIFY_OFFSET
9744
9745#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06009746 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
9747
9748 VERIFY_OFFSET(dev_handle, 0x00);
9749 VERIFY_OFFSET(reserved1, 0x02);
9750 VERIFY_OFFSET(function, 0x03);
9751 VERIFY_OFFSET(reserved2, 0x04);
9752 VERIFY_OFFSET(err_info, 0x0C);
9753 VERIFY_OFFSET(reserved3, 0x10);
9754 VERIFY_OFFSET(err_info_len, 0x12);
9755 VERIFY_OFFSET(reserved4, 0x13);
9756 VERIFY_OFFSET(sgl_offset, 0x14);
9757 VERIFY_OFFSET(reserved5, 0x15);
9758 VERIFY_OFFSET(transfer_len, 0x1C);
9759 VERIFY_OFFSET(reserved6, 0x20);
9760 VERIFY_OFFSET(io_flags, 0x24);
9761 VERIFY_OFFSET(reserved7, 0x26);
9762 VERIFY_OFFSET(LUN, 0x34);
9763 VERIFY_OFFSET(control, 0x3C);
9764 VERIFY_OFFSET(CDB, 0x40);
9765 VERIFY_OFFSET(reserved8, 0x50);
9766 VERIFY_OFFSET(host_context_flags, 0x60);
9767 VERIFY_OFFSET(timeout_sec, 0x62);
9768 VERIFY_OFFSET(ReplyQueue, 0x64);
9769 VERIFY_OFFSET(reserved9, 0x65);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009770 VERIFY_OFFSET(tag, 0x68);
Matt Gatese1f7de02014-02-18 13:55:17 -06009771 VERIFY_OFFSET(host_addr, 0x70);
9772 VERIFY_OFFSET(CISS_LUN, 0x78);
9773 VERIFY_OFFSET(SG, 0x78 + 8);
9774#undef VERIFY_OFFSET
9775}
9776
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009777module_init(hpsa_init);
9778module_exit(hpsa_cleanup);