blob: 9a631e399169c91399df69bd07e5dc92ff3a2511 [file] [log] [blame]
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001/*
2 * Disk Array driver for HP Smart Array SAS controllers
Don Brace94c7bc32016-02-23 15:16:46 -06003 * Copyright 2016 Microsemi Corporation
Don Brace1358f6d2015-07-18 11:12:38 -05004 * Copyright 2014-2015 PMC-Sierra, Inc.
5 * Copyright 2000,2009-2015 Hewlett-Packard Development Company, L.P.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more details.
15 *
Don Brace94c7bc32016-02-23 15:16:46 -060016 * Questions/Comments/Bugfixes to esc.storagedev@microsemi.com
Stephen M. Cameronedd16362009-12-08 14:09:11 -080017 *
18 */
19
20#include <linux/module.h>
21#include <linux/interrupt.h>
22#include <linux/types.h>
23#include <linux/pci.h>
Matthew Garrette5a44df2011-11-11 11:14:23 -050024#include <linux/pci-aspm.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080025#include <linux/kernel.h>
26#include <linux/slab.h>
27#include <linux/delay.h>
28#include <linux/fs.h>
29#include <linux/timer.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080030#include <linux/init.h>
31#include <linux/spinlock.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080032#include <linux/compat.h>
33#include <linux/blktrace_api.h>
34#include <linux/uaccess.h>
35#include <linux/io.h>
36#include <linux/dma-mapping.h>
37#include <linux/completion.h>
38#include <linux/moduleparam.h>
39#include <scsi/scsi.h>
40#include <scsi/scsi_cmnd.h>
41#include <scsi/scsi_device.h>
42#include <scsi/scsi_host.h>
Stephen M. Cameron667e23d2010-02-25 14:02:51 -060043#include <scsi/scsi_tcq.h>
Stephen Cameron9437ac42015-04-23 09:32:16 -050044#include <scsi/scsi_eh.h>
Kevin Barnettd04e62b2015-11-04 15:52:34 -060045#include <scsi/scsi_transport_sas.h>
Webb Scales73153fe2015-04-23 09:35:04 -050046#include <scsi/scsi_dbg.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080047#include <linux/cciss_ioctl.h>
48#include <linux/string.h>
49#include <linux/bitmap.h>
Arun Sharma600634972011-07-26 16:09:06 -070050#include <linux/atomic.h>
Stephen M. Camerona0c12412011-10-26 16:22:04 -050051#include <linux/jiffies.h>
Don Brace42a91642014-11-14 17:26:27 -060052#include <linux/percpu-defs.h>
Stephen M. Cameron094963d2014-05-29 10:53:18 -050053#include <linux/percpu.h>
Don Brace2b08b3e2015-01-23 16:41:09 -060054#include <asm/unaligned.h>
Stephen M. Cameron283b4a92014-02-18 13:55:33 -060055#include <asm/div64.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080056#include "hpsa_cmd.h"
57#include "hpsa.h"
58
Don Braceec2c3aa2015-11-04 15:52:40 -060059/*
60 * HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.'
61 * with an optional trailing '-' followed by a byte value (0-255).
62 */
Don Brace5765d182017-03-28 16:40:20 -050063#define HPSA_DRIVER_VERSION "3.4.18-0"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080064#define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -060065#define HPSA "hpsa"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080066
Robert Elliott007e7aa2015-01-23 16:44:56 -060067/* How long to wait for CISS doorbell communication */
68#define CLEAR_EVENT_WAIT_INTERVAL 20 /* ms for each msleep() call */
69#define MODE_CHANGE_WAIT_INTERVAL 10 /* ms for each msleep() call */
70#define MAX_CLEAR_EVENT_WAIT 30000 /* times 20 ms = 600 s */
71#define MAX_MODE_CHANGE_WAIT 2000 /* times 10 ms = 20 s */
Stephen M. Cameronedd16362009-12-08 14:09:11 -080072#define MAX_IOCTL_CONFIG_WAIT 1000
73
74/*define how many times we will try a command because of bus resets */
75#define MAX_CMD_RETRIES 3
76
77/* Embedded module documentation macros - see modules.h */
78MODULE_AUTHOR("Hewlett-Packard Company");
79MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
80 HPSA_DRIVER_VERSION);
81MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
82MODULE_VERSION(HPSA_DRIVER_VERSION);
83MODULE_LICENSE("GPL");
84
85static int hpsa_allow_any;
86module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
87MODULE_PARM_DESC(hpsa_allow_any,
88 "Allow hpsa driver to access unknown HP Smart Array hardware");
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -060089static int hpsa_simple_mode;
90module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
91MODULE_PARM_DESC(hpsa_simple_mode,
92 "Use 'simple mode' rather than 'performant mode'");
Stephen M. Cameronedd16362009-12-08 14:09:11 -080093
94/* define the PCI info for the cards we can control */
95static const struct pci_device_id hpsa_pci_device_id[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -080096 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
97 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
98 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
99 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
100 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
Mike Miller163dbcd2013-09-04 15:11:10 -0500101 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
102 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
Mike Millerf8b01eb2010-02-04 08:42:45 -0600103 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
scameron@beardog.cce.hp.com9143a962011-03-07 10:44:16 -0600104 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350},
105 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351},
106 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352},
107 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353},
108 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354},
109 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355},
110 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356},
Don Brace7f1974a2017-03-28 16:40:13 -0500111 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103c, 0x1920},
Mike Millerfe0c9612012-09-20 16:05:18 -0500112 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1921},
113 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1922},
114 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1923},
115 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1924},
Don Brace7f1974a2017-03-28 16:40:13 -0500116 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103c, 0x1925},
Mike Millerfe0c9612012-09-20 16:05:18 -0500117 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1926},
118 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1928},
Mike Miller97b9f532013-09-04 15:05:55 -0500119 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1929},
120 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BD},
121 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BE},
122 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BF},
123 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C0},
124 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C1},
125 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C2},
126 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C3},
127 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C4},
128 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C5},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500129 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C6},
Mike Miller97b9f532013-09-04 15:05:55 -0500130 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C7},
131 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C8},
132 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C9},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500133 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CA},
134 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CB},
135 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CC},
136 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CD},
137 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CE},
Don Bracefdfa4b62015-04-23 09:35:27 -0500138 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0580},
Don Bracecbb47dc2015-07-18 11:12:54 -0500139 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0581},
140 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0582},
141 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0583},
142 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0584},
143 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0585},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600144 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
145 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
146 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
147 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
148 {PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
Mike Miller7c03b872010-12-01 11:16:07 -0600149 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
Stephen M. Cameron6798cc02010-06-16 13:51:20 -0500150 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800151 {0,}
152};
153
154MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
155
156/* board_id = Subsystem Device ID & Vendor ID
157 * product = Marketing Name for the board
158 * access = Address of the struct of function pointers
159 */
160static struct board_type products[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800161 {0x3241103C, "Smart Array P212", &SA5_access},
162 {0x3243103C, "Smart Array P410", &SA5_access},
163 {0x3245103C, "Smart Array P410i", &SA5_access},
164 {0x3247103C, "Smart Array P411", &SA5_access},
165 {0x3249103C, "Smart Array P812", &SA5_access},
Mike Miller163dbcd2013-09-04 15:11:10 -0500166 {0x324A103C, "Smart Array P712m", &SA5_access},
167 {0x324B103C, "Smart Array P711m", &SA5_access},
Stephen M. Cameron7d2cce52014-11-14 17:26:38 -0600168 {0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
Mike Millerfe0c9612012-09-20 16:05:18 -0500169 {0x3350103C, "Smart Array P222", &SA5_access},
170 {0x3351103C, "Smart Array P420", &SA5_access},
171 {0x3352103C, "Smart Array P421", &SA5_access},
172 {0x3353103C, "Smart Array P822", &SA5_access},
173 {0x3354103C, "Smart Array P420i", &SA5_access},
174 {0x3355103C, "Smart Array P220i", &SA5_access},
175 {0x3356103C, "Smart Array P721m", &SA5_access},
Don Brace7f1974a2017-03-28 16:40:13 -0500176 {0x1920103C, "Smart Array P430i", &SA5_access},
Mike Miller1fd6c8e2013-09-04 15:08:29 -0500177 {0x1921103C, "Smart Array P830i", &SA5_access},
178 {0x1922103C, "Smart Array P430", &SA5_access},
179 {0x1923103C, "Smart Array P431", &SA5_access},
180 {0x1924103C, "Smart Array P830", &SA5_access},
Don Brace7f1974a2017-03-28 16:40:13 -0500181 {0x1925103C, "Smart Array P831", &SA5_access},
Mike Miller1fd6c8e2013-09-04 15:08:29 -0500182 {0x1926103C, "Smart Array P731m", &SA5_access},
183 {0x1928103C, "Smart Array P230i", &SA5_access},
184 {0x1929103C, "Smart Array P530", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600185 {0x21BD103C, "Smart Array P244br", &SA5_access},
186 {0x21BE103C, "Smart Array P741m", &SA5_access},
187 {0x21BF103C, "Smart HBA H240ar", &SA5_access},
188 {0x21C0103C, "Smart Array P440ar", &SA5_access},
Don Bracec8ae0ab2015-01-23 16:45:12 -0600189 {0x21C1103C, "Smart Array P840ar", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600190 {0x21C2103C, "Smart Array P440", &SA5_access},
191 {0x21C3103C, "Smart Array P441", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500192 {0x21C4103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600193 {0x21C5103C, "Smart Array P841", &SA5_access},
194 {0x21C6103C, "Smart HBA H244br", &SA5_access},
195 {0x21C7103C, "Smart HBA H240", &SA5_access},
196 {0x21C8103C, "Smart HBA H241", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500197 {0x21C9103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600198 {0x21CA103C, "Smart Array P246br", &SA5_access},
199 {0x21CB103C, "Smart Array P840", &SA5_access},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500200 {0x21CC103C, "Smart Array", &SA5_access},
201 {0x21CD103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600202 {0x21CE103C, "Smart HBA", &SA5_access},
Don Bracefdfa4b62015-04-23 09:35:27 -0500203 {0x05809005, "SmartHBA-SA", &SA5_access},
Don Bracecbb47dc2015-07-18 11:12:54 -0500204 {0x05819005, "SmartHBA-SA 8i", &SA5_access},
205 {0x05829005, "SmartHBA-SA 8i8e", &SA5_access},
206 {0x05839005, "SmartHBA-SA 8e", &SA5_access},
207 {0x05849005, "SmartHBA-SA 16i", &SA5_access},
208 {0x05859005, "SmartHBA-SA 4i4e", &SA5_access},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600209 {0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
210 {0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
211 {0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
212 {0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
213 {0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800214 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
215};
216
Kevin Barnettd04e62b2015-11-04 15:52:34 -0600217static struct scsi_transport_template *hpsa_sas_transport_template;
218static int hpsa_add_sas_host(struct ctlr_info *h);
219static void hpsa_delete_sas_host(struct ctlr_info *h);
220static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
221 struct hpsa_scsi_dev_t *device);
222static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device);
223static struct hpsa_scsi_dev_t
224 *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
225 struct sas_rphy *rphy);
226
Webb Scalesa58e7e52015-04-23 09:34:16 -0500227#define SCSI_CMD_BUSY ((struct scsi_cmnd *)&hpsa_cmd_busy)
228static const struct scsi_cmnd hpsa_cmd_busy;
229#define SCSI_CMD_IDLE ((struct scsi_cmnd *)&hpsa_cmd_idle)
230static const struct scsi_cmnd hpsa_cmd_idle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800231static int number_of_controllers;
232
Stephen M. Cameron10f66012010-06-16 13:51:50 -0500233static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
234static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
Don Brace42a91642014-11-14 17:26:27 -0600235static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800236
237#ifdef CONFIG_COMPAT
Don Brace42a91642014-11-14 17:26:27 -0600238static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
239 void __user *arg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800240#endif
241
242static void cmd_free(struct ctlr_info *h, struct CommandList *c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800243static struct CommandList *cmd_alloc(struct ctlr_info *h);
Webb Scales73153fe2015-04-23 09:35:04 -0500244static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c);
245static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
246 struct scsi_cmnd *scmd);
Stephen M. Camerona2dac132013-02-20 11:24:41 -0600247static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600248 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800249 int cmd_type);
Robert Elliott2c143342015-01-23 16:42:48 -0600250static void hpsa_free_cmd_pool(struct ctlr_info *h);
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600251#define VPD_PAGE (1 << 8)
Don Braceb48d9802015-11-04 15:50:13 -0600252#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800253
Jeff Garzikf2812332010-11-16 02:10:29 -0500254static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Stephen M. Camerona08a84712010-02-04 08:43:16 -0600255static void hpsa_scan_start(struct Scsi_Host *);
256static int hpsa_scan_finished(struct Scsi_Host *sh,
257 unsigned long elapsed_time);
Don Brace7c0a0222015-01-23 16:41:30 -0600258static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800259
260static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
261static int hpsa_slave_alloc(struct scsi_device *sdev);
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500262static int hpsa_slave_configure(struct scsi_device *sdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800263static void hpsa_slave_destroy(struct scsi_device *sdev);
264
Don Brace8aa60682015-11-04 15:50:01 -0600265static void hpsa_update_scsi_devices(struct ctlr_info *h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800266static int check_for_unit_attention(struct ctlr_info *h,
267 struct CommandList *c);
268static void check_ioctl_unit_attention(struct ctlr_info *h,
269 struct CommandList *c);
Don Brace303932f2010-02-04 08:42:40 -0600270/* performant mode helper functions */
271static void calc_bucket_map(int *bucket, int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -0600272 int nsgs, int min_blocks, u32 *bucket_map);
Robert Elliott105a3db2015-04-23 09:33:48 -0500273static void hpsa_free_performant_mode(struct ctlr_info *h);
274static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
Matt Gates254f7962012-05-01 11:43:06 -0500275static inline u32 next_command(struct ctlr_info *h, u8 q);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800276static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
277 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
278 u64 *cfg_offset);
279static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
280 unsigned long *memory_bar);
281static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
Don Bracebfd75462016-11-15 14:45:32 -0600282static int wait_for_device_to_become_ready(struct ctlr_info *h,
283 unsigned char lunaddr[],
284 int reply_queue);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800285static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
286 int wait_for_ready);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500287static inline void finish_cmd(struct CommandList *c);
Robert Elliottc706a792015-01-23 16:45:01 -0600288static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -0600289#define BOARD_NOT_READY 0
290#define BOARD_READY 1
Stephen M. Cameron23100dd2014-02-18 13:57:37 -0600291static void hpsa_drain_accel_commands(struct ctlr_info *h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -0600292static void hpsa_flush_cache(struct ctlr_info *h);
Scott Teelc3497752014-02-18 13:56:34 -0600293static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
294 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -0600295 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
Don Brace080ef1c2015-01-23 16:43:25 -0600296static void hpsa_command_resubmit_worker(struct work_struct *work);
Webb Scales25163bd2015-04-23 09:32:00 -0500297static u32 lockup_detected(struct ctlr_info *h);
298static int detect_controller_lockup(struct ctlr_info *h);
Scott Teelc2adae42015-11-04 15:52:16 -0600299static void hpsa_disable_rld_caching(struct ctlr_info *h);
Kevin Barnettd04e62b2015-11-04 15:52:34 -0600300static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
301 struct ReportExtendedLUNdata *buf, int bufsize);
Scott Teel83832782016-09-09 16:30:29 -0500302static bool hpsa_vpd_page_supported(struct ctlr_info *h,
303 unsigned char scsi3addr[], u8 page);
Scott Teel34592252015-11-04 15:52:09 -0600304static int hpsa_luns_changed(struct ctlr_info *h);
Don Braceba74fdc2016-04-27 17:14:17 -0500305static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
306 struct hpsa_scsi_dev_t *dev,
307 unsigned char *scsi3addr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800308
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800309static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
310{
311 unsigned long *priv = shost_priv(sdev->host);
312 return (struct ctlr_info *) *priv;
313}
314
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600315static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
316{
317 unsigned long *priv = shost_priv(sh);
318 return (struct ctlr_info *) *priv;
319}
320
Webb Scalesa58e7e52015-04-23 09:34:16 -0500321static inline bool hpsa_is_cmd_idle(struct CommandList *c)
322{
323 return c->scsi_cmd == SCSI_CMD_IDLE;
324}
325
Webb Scalesd604f532015-04-23 09:35:22 -0500326static inline bool hpsa_is_pending_event(struct CommandList *c)
327{
Don Brace08ec46f2017-05-04 17:51:49 -0500328 return c->reset_pending;
Webb Scalesd604f532015-04-23 09:35:22 -0500329}
330
Stephen Cameron9437ac42015-04-23 09:32:16 -0500331/* extract sense key, asc, and ascq from sense data. -1 means invalid. */
332static void decode_sense_data(const u8 *sense_data, int sense_data_len,
333 u8 *sense_key, u8 *asc, u8 *ascq)
334{
335 struct scsi_sense_hdr sshdr;
336 bool rc;
337
338 *sense_key = -1;
339 *asc = -1;
340 *ascq = -1;
341
342 if (sense_data_len < 1)
343 return;
344
345 rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
346 if (rc) {
347 *sense_key = sshdr.sense_key;
348 *asc = sshdr.asc;
349 *ascq = sshdr.ascq;
350 }
351}
352
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800353static int check_for_unit_attention(struct ctlr_info *h,
354 struct CommandList *c)
355{
Stephen Cameron9437ac42015-04-23 09:32:16 -0500356 u8 sense_key, asc, ascq;
357 int sense_len;
358
359 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
360 sense_len = sizeof(c->err_info->SenseInfo);
361 else
362 sense_len = c->err_info->SenseLen;
363
364 decode_sense_data(c->err_info->SenseInfo, sense_len,
365 &sense_key, &asc, &ascq);
Don Brace81c27552015-07-18 11:12:28 -0500366 if (sense_key != UNIT_ATTENTION || asc == 0xff)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800367 return 0;
368
Stephen Cameron9437ac42015-04-23 09:32:16 -0500369 switch (asc) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800370 case STATE_CHANGED:
Stephen Cameron9437ac42015-04-23 09:32:16 -0500371 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500372 "%s: a state change detected, command retried\n",
373 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800374 break;
375 case LUN_FAILED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600376 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500377 "%s: LUN failure detected\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800378 break;
379 case REPORT_LUNS_CHANGED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600380 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500381 "%s: report LUN data changed\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800382 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600383 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
384 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800385 */
386 break;
387 case POWER_OR_RESET:
Robert Elliott2946e822015-04-23 09:35:09 -0500388 dev_warn(&h->pdev->dev,
389 "%s: a power on or device reset detected\n",
390 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800391 break;
392 case UNIT_ATTENTION_CLEARED:
Robert Elliott2946e822015-04-23 09:35:09 -0500393 dev_warn(&h->pdev->dev,
394 "%s: unit attention cleared by another initiator\n",
395 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800396 break;
397 default:
Robert Elliott2946e822015-04-23 09:35:09 -0500398 dev_warn(&h->pdev->dev,
399 "%s: unknown unit attention detected\n",
400 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800401 break;
402 }
403 return 1;
404}
405
Matt Bondurant852af202012-05-01 11:42:35 -0500406static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
407{
408 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
409 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
410 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
411 return 0;
412 dev_warn(&h->pdev->dev, HPSA "device busy");
413 return 1;
414}
415
Stephen Camerone985c582015-04-23 09:32:22 -0500416static u32 lockup_detected(struct ctlr_info *h);
417static ssize_t host_show_lockup_detected(struct device *dev,
418 struct device_attribute *attr, char *buf)
419{
420 int ld;
421 struct ctlr_info *h;
422 struct Scsi_Host *shost = class_to_shost(dev);
423
424 h = shost_to_hba(shost);
425 ld = lockup_detected(h);
426
427 return sprintf(buf, "ld=%d\n", ld);
428}
429
Scott Teelda0697b2014-02-18 13:57:00 -0600430static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
431 struct device_attribute *attr,
432 const char *buf, size_t count)
433{
434 int status, len;
435 struct ctlr_info *h;
436 struct Scsi_Host *shost = class_to_shost(dev);
437 char tmpbuf[10];
438
439 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
440 return -EACCES;
441 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
442 strncpy(tmpbuf, buf, len);
443 tmpbuf[len] = '\0';
444 if (sscanf(tmpbuf, "%d", &status) != 1)
445 return -EINVAL;
446 h = shost_to_hba(shost);
447 h->acciopath_status = !!status;
448 dev_warn(&h->pdev->dev,
449 "hpsa: HP SSD Smart Path %s via sysfs update.\n",
450 h->acciopath_status ? "enabled" : "disabled");
451 return count;
452}
453
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600454static ssize_t host_store_raid_offload_debug(struct device *dev,
455 struct device_attribute *attr,
456 const char *buf, size_t count)
457{
458 int debug_level, len;
459 struct ctlr_info *h;
460 struct Scsi_Host *shost = class_to_shost(dev);
461 char tmpbuf[10];
462
463 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
464 return -EACCES;
465 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
466 strncpy(tmpbuf, buf, len);
467 tmpbuf[len] = '\0';
468 if (sscanf(tmpbuf, "%d", &debug_level) != 1)
469 return -EINVAL;
470 if (debug_level < 0)
471 debug_level = 0;
472 h = shost_to_hba(shost);
473 h->raid_offload_debug = debug_level;
474 dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
475 h->raid_offload_debug);
476 return count;
477}
478
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800479static ssize_t host_store_rescan(struct device *dev,
480 struct device_attribute *attr,
481 const char *buf, size_t count)
482{
483 struct ctlr_info *h;
484 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600485 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600486 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800487 return count;
488}
489
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500490static ssize_t host_show_firmware_revision(struct device *dev,
491 struct device_attribute *attr, char *buf)
492{
493 struct ctlr_info *h;
494 struct Scsi_Host *shost = class_to_shost(dev);
495 unsigned char *fwrev;
496
497 h = shost_to_hba(shost);
498 if (!h->hba_inquiry_data)
499 return 0;
500 fwrev = &h->hba_inquiry_data[32];
501 return snprintf(buf, 20, "%c%c%c%c\n",
502 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
503}
504
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600505static ssize_t host_show_commands_outstanding(struct device *dev,
506 struct device_attribute *attr, char *buf)
507{
508 struct Scsi_Host *shost = class_to_shost(dev);
509 struct ctlr_info *h = shost_to_hba(shost);
510
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600511 return snprintf(buf, 20, "%d\n",
512 atomic_read(&h->commands_outstanding));
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600513}
514
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600515static ssize_t host_show_transport_mode(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, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600523 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600524 "performant" : "simple");
525}
526
Scott Teelda0697b2014-02-18 13:57:00 -0600527static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
528 struct device_attribute *attr, char *buf)
529{
530 struct ctlr_info *h;
531 struct Scsi_Host *shost = class_to_shost(dev);
532
533 h = shost_to_hba(shost);
534 return snprintf(buf, 30, "HP SSD Smart Path %s\n",
535 (h->acciopath_status == 1) ? "enabled" : "disabled");
536}
537
Stephen M. Cameron46380782011-05-03 15:00:01 -0500538/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600539static u32 unresettable_controller[] = {
540 0x324a103C, /* Smart Array P712m */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500541 0x324b103C, /* Smart Array P711m */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600542 0x3223103C, /* Smart Array P800 */
543 0x3234103C, /* Smart Array P400 */
544 0x3235103C, /* Smart Array P400i */
545 0x3211103C, /* Smart Array E200i */
546 0x3212103C, /* Smart Array E200 */
547 0x3213103C, /* Smart Array E200i */
548 0x3214103C, /* Smart Array E200i */
549 0x3215103C, /* Smart Array E200i */
550 0x3237103C, /* Smart Array E500 */
551 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100552 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600553 0x409C0E11, /* Smart Array 6400 */
554 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100555 0x40700E11, /* Smart Array 5300 */
556 0x40820E11, /* Smart Array 532 */
557 0x40830E11, /* Smart Array 5312 */
558 0x409A0E11, /* Smart Array 641 */
559 0x409B0E11, /* Smart Array 642 */
560 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600561};
562
Stephen M. Cameron46380782011-05-03 15:00:01 -0500563/* List of controllers which cannot even be soft reset */
564static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100565 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100566 0x40700E11, /* Smart Array 5300 */
567 0x40820E11, /* Smart Array 532 */
568 0x40830E11, /* Smart Array 5312 */
569 0x409A0E11, /* Smart Array 641 */
570 0x409B0E11, /* Smart Array 642 */
571 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500572 /* Exclude 640x boards. These are two pci devices in one slot
573 * which share a battery backed cache module. One controls the
574 * cache, the other accesses the cache through the one that controls
575 * it. If we reset the one controlling the cache, the other will
576 * likely not be happy. Just forbid resetting this conjoined mess.
577 * The 640x isn't really supported by hpsa anyway.
578 */
579 0x409C0E11, /* Smart Array 6400 */
580 0x409D0E11, /* Smart Array 6400 EM */
581};
582
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500583static int board_id_in_array(u32 a[], int nelems, u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600584{
585 int i;
586
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500587 for (i = 0; i < nelems; i++)
588 if (a[i] == board_id)
589 return 1;
590 return 0;
591}
592
593static int ctlr_is_hard_resettable(u32 board_id)
594{
595 return !board_id_in_array(unresettable_controller,
596 ARRAY_SIZE(unresettable_controller), board_id);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600597}
598
Stephen M. Cameron46380782011-05-03 15:00:01 -0500599static int ctlr_is_soft_resettable(u32 board_id)
600{
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500601 return !board_id_in_array(soft_unresettable_controller,
602 ARRAY_SIZE(soft_unresettable_controller), board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500603}
604
605static int ctlr_is_resettable(u32 board_id)
606{
607 return ctlr_is_hard_resettable(board_id) ||
608 ctlr_is_soft_resettable(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);
Rasmus Villemoes609a70d2016-11-30 23:35:47 +0100697 return snprintf(buf, 20, "0x%8phN\n", lunid);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800698}
699
700static ssize_t unique_id_show(struct device *dev,
701 struct device_attribute *attr, char *buf)
702{
703 struct ctlr_info *h;
704 struct scsi_device *sdev;
705 struct hpsa_scsi_dev_t *hdev;
706 unsigned long flags;
707 unsigned char sn[16];
708
709 sdev = to_scsi_device(dev);
710 h = sdev_to_hba(sdev);
711 spin_lock_irqsave(&h->lock, flags);
712 hdev = sdev->hostdata;
713 if (!hdev) {
714 spin_unlock_irqrestore(&h->lock, flags);
715 return -ENODEV;
716 }
717 memcpy(sn, hdev->device_id, sizeof(sn));
718 spin_unlock_irqrestore(&h->lock, flags);
719 return snprintf(buf, 16 * 2 + 2,
720 "%02X%02X%02X%02X%02X%02X%02X%02X"
721 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
722 sn[0], sn[1], sn[2], sn[3],
723 sn[4], sn[5], sn[6], sn[7],
724 sn[8], sn[9], sn[10], sn[11],
725 sn[12], sn[13], sn[14], sn[15]);
726}
727
Joseph T Handzikded1be42016-04-27 17:13:33 -0500728static ssize_t sas_address_show(struct device *dev,
729 struct device_attribute *attr, char *buf)
730{
731 struct ctlr_info *h;
732 struct scsi_device *sdev;
733 struct hpsa_scsi_dev_t *hdev;
734 unsigned long flags;
735 u64 sas_address;
736
737 sdev = to_scsi_device(dev);
738 h = sdev_to_hba(sdev);
739 spin_lock_irqsave(&h->lock, flags);
740 hdev = sdev->hostdata;
741 if (!hdev || is_logical_device(hdev) || !hdev->expose_device) {
742 spin_unlock_irqrestore(&h->lock, flags);
743 return -ENODEV;
744 }
745 sas_address = hdev->sas_address;
746 spin_unlock_irqrestore(&h->lock, flags);
747
748 return snprintf(buf, PAGE_SIZE, "0x%016llx\n", sas_address);
749}
750
Scott Teelc1988682014-02-18 13:55:54 -0600751static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
752 struct device_attribute *attr, char *buf)
753{
754 struct ctlr_info *h;
755 struct scsi_device *sdev;
756 struct hpsa_scsi_dev_t *hdev;
757 unsigned long flags;
758 int offload_enabled;
759
760 sdev = to_scsi_device(dev);
761 h = sdev_to_hba(sdev);
762 spin_lock_irqsave(&h->lock, flags);
763 hdev = sdev->hostdata;
764 if (!hdev) {
765 spin_unlock_irqrestore(&h->lock, flags);
766 return -ENODEV;
767 }
768 offload_enabled = hdev->offload_enabled;
769 spin_unlock_irqrestore(&h->lock, flags);
770 return snprintf(buf, 20, "%d\n", offload_enabled);
771}
772
Joe Handzik8270b862015-07-18 11:12:43 -0500773#define MAX_PATHS 8
Joe Handzik8270b862015-07-18 11:12:43 -0500774static ssize_t path_info_show(struct device *dev,
775 struct device_attribute *attr, char *buf)
776{
777 struct ctlr_info *h;
778 struct scsi_device *sdev;
779 struct hpsa_scsi_dev_t *hdev;
780 unsigned long flags;
781 int i;
782 int output_len = 0;
783 u8 box;
784 u8 bay;
785 u8 path_map_index = 0;
786 char *active;
787 unsigned char phys_connector[2];
Joe Handzik8270b862015-07-18 11:12:43 -0500788
Joe Handzik8270b862015-07-18 11:12:43 -0500789 sdev = to_scsi_device(dev);
790 h = sdev_to_hba(sdev);
791 spin_lock_irqsave(&h->devlock, flags);
792 hdev = sdev->hostdata;
793 if (!hdev) {
794 spin_unlock_irqrestore(&h->devlock, flags);
795 return -ENODEV;
796 }
797
798 bay = hdev->bay;
799 for (i = 0; i < MAX_PATHS; i++) {
800 path_map_index = 1<<i;
801 if (i == hdev->active_path_index)
802 active = "Active";
803 else if (hdev->path_map & path_map_index)
804 active = "Inactive";
805 else
806 continue;
807
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600808 output_len += scnprintf(buf + output_len,
809 PAGE_SIZE - output_len,
810 "[%d:%d:%d:%d] %20.20s ",
Joe Handzik8270b862015-07-18 11:12:43 -0500811 h->scsi_host->host_no,
812 hdev->bus, hdev->target, hdev->lun,
813 scsi_device_type(hdev->devtype));
814
Don Bracecca8f132015-12-22 10:36:48 -0600815 if (hdev->devtype == TYPE_RAID || is_logical_device(hdev)) {
Don Brace2708f292015-12-22 10:36:36 -0600816 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600817 PAGE_SIZE - output_len,
818 "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500819 continue;
820 }
821
822 box = hdev->box[i];
823 memcpy(&phys_connector, &hdev->phys_connector[i],
824 sizeof(phys_connector));
825 if (phys_connector[0] < '0')
826 phys_connector[0] = '0';
827 if (phys_connector[1] < '0')
828 phys_connector[1] = '0';
Don Bracecca8f132015-12-22 10:36:48 -0600829 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600830 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500831 "PORT: %.2s ",
832 phys_connector);
Don Braceaf15ed32016-02-23 15:16:15 -0600833 if ((hdev->devtype == TYPE_DISK || hdev->devtype == TYPE_ZBC) &&
834 hdev->expose_device) {
Joe Handzik8270b862015-07-18 11:12:43 -0500835 if (box == 0 || box == 0xFF) {
Don Brace2708f292015-12-22 10:36:36 -0600836 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600837 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500838 "BAY: %hhu %s\n",
839 bay, active);
840 } else {
Don Brace2708f292015-12-22 10:36:36 -0600841 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600842 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500843 "BOX: %hhu BAY: %hhu %s\n",
844 box, bay, active);
845 }
846 } else if (box != 0 && box != 0xFF) {
Don Brace2708f292015-12-22 10:36:36 -0600847 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600848 PAGE_SIZE - output_len, "BOX: %hhu %s\n",
Joe Handzik8270b862015-07-18 11:12:43 -0500849 box, active);
850 } else
Don Brace2708f292015-12-22 10:36:36 -0600851 output_len += scnprintf(buf + output_len,
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600852 PAGE_SIZE - output_len, "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500853 }
854
855 spin_unlock_irqrestore(&h->devlock, flags);
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600856 return output_len;
Joe Handzik8270b862015-07-18 11:12:43 -0500857}
858
Hannes Reinecke16961202016-11-18 08:32:49 +0100859static ssize_t host_show_ctlr_num(struct device *dev,
860 struct device_attribute *attr, char *buf)
861{
862 struct ctlr_info *h;
863 struct Scsi_Host *shost = class_to_shost(dev);
864
865 h = shost_to_hba(shost);
866 return snprintf(buf, 20, "%d\n", h->ctlr);
867}
868
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600869static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
870static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
871static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
872static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Joseph T Handzikded1be42016-04-27 17:13:33 -0500873static DEVICE_ATTR(sas_address, S_IRUGO, sas_address_show, NULL);
Scott Teelc1988682014-02-18 13:55:54 -0600874static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
875 host_show_hp_ssd_smart_path_enabled, NULL);
Joe Handzik8270b862015-07-18 11:12:43 -0500876static DEVICE_ATTR(path_info, S_IRUGO, path_info_show, NULL);
Scott Teelda0697b2014-02-18 13:57:00 -0600877static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
878 host_show_hp_ssd_smart_path_status,
879 host_store_hp_ssd_smart_path_status);
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600880static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
881 host_store_raid_offload_debug);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600882static DEVICE_ATTR(firmware_revision, S_IRUGO,
883 host_show_firmware_revision, NULL);
884static DEVICE_ATTR(commands_outstanding, S_IRUGO,
885 host_show_commands_outstanding, NULL);
886static DEVICE_ATTR(transport_mode, S_IRUGO,
887 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600888static DEVICE_ATTR(resettable, S_IRUGO,
889 host_show_resettable, NULL);
Stephen Camerone985c582015-04-23 09:32:22 -0500890static DEVICE_ATTR(lockup_detected, S_IRUGO,
891 host_show_lockup_detected, NULL);
Hannes Reinecke16961202016-11-18 08:32:49 +0100892static DEVICE_ATTR(ctlr_num, S_IRUGO,
893 host_show_ctlr_num, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600894
895static struct device_attribute *hpsa_sdev_attrs[] = {
896 &dev_attr_raid_level,
897 &dev_attr_lunid,
898 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600899 &dev_attr_hp_ssd_smart_path_enabled,
Joe Handzik8270b862015-07-18 11:12:43 -0500900 &dev_attr_path_info,
Joseph T Handzikded1be42016-04-27 17:13:33 -0500901 &dev_attr_sas_address,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600902 NULL,
903};
904
905static struct device_attribute *hpsa_shost_attrs[] = {
906 &dev_attr_rescan,
907 &dev_attr_firmware_revision,
908 &dev_attr_commands_outstanding,
909 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600910 &dev_attr_resettable,
Scott Teelda0697b2014-02-18 13:57:00 -0600911 &dev_attr_hp_ssd_smart_path_status,
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600912 &dev_attr_raid_offload_debug,
Tomas Henzlfb53c432015-11-06 16:24:09 +0100913 &dev_attr_lockup_detected,
Hannes Reinecke16961202016-11-18 08:32:49 +0100914 &dev_attr_ctlr_num,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600915 NULL,
916};
917
Don Brace08ec46f2017-05-04 17:51:49 -0500918#define HPSA_NRESERVED_CMDS (HPSA_CMDS_RESERVED_FOR_DRIVER +\
919 HPSA_MAX_CONCURRENT_PASSTHRUS)
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500920
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600921static struct scsi_host_template hpsa_driver_template = {
922 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600923 .name = HPSA,
924 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600925 .queuecommand = hpsa_scsi_queue_command,
926 .scan_start = hpsa_scan_start,
927 .scan_finished = hpsa_scan_finished,
Don Brace7c0a0222015-01-23 16:41:30 -0600928 .change_queue_depth = hpsa_change_queue_depth,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600929 .this_id = -1,
930 .use_clustering = ENABLE_CLUSTERING,
931 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
932 .ioctl = hpsa_ioctl,
933 .slave_alloc = hpsa_slave_alloc,
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500934 .slave_configure = hpsa_slave_configure,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600935 .slave_destroy = hpsa_slave_destroy,
936#ifdef CONFIG_COMPAT
937 .compat_ioctl = hpsa_compat_ioctl,
938#endif
939 .sdev_attrs = hpsa_sdev_attrs,
940 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500941 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400942 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600943};
944
Matt Gates254f7962012-05-01 11:43:06 -0500945static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600946{
947 u32 a;
Stephen M. Cameron072b0512014-05-29 10:53:07 -0500948 struct reply_queue_buffer *rq = &h->reply_queue[q];
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600949
Matt Gatese1f7de02014-02-18 13:55:17 -0600950 if (h->transMethod & CFGTBL_Trans_io_accel1)
951 return h->access.command_completed(h, q);
952
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600953 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500954 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600955
Matt Gates254f7962012-05-01 11:43:06 -0500956 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
957 a = rq->head[rq->current_entry];
958 rq->current_entry++;
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600959 atomic_dec(&h->commands_outstanding);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600960 } else {
961 a = FIFO_EMPTY;
962 }
963 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500964 if (rq->current_entry == h->max_commands) {
965 rq->current_entry = 0;
966 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600967 }
968 return a;
969}
970
Scott Teelc3497752014-02-18 13:56:34 -0600971/*
972 * There are some special bits in the bus address of the
973 * command that we have to set for the controller to know
974 * how to process the command:
975 *
976 * Normal performant mode:
977 * bit 0: 1 means performant mode, 0 means simple mode.
978 * bits 1-3 = block fetch table entry
979 * bits 4-6 = command type (== 0)
980 *
981 * ioaccel1 mode:
982 * bit 0 = "performant mode" bit.
983 * bits 1-3 = block fetch table entry
984 * bits 4-6 = command type (== 110)
985 * (command type is needed because ioaccel1 mode
986 * commands are submitted through the same register as normal
987 * mode commands, so this is how the controller knows whether
988 * the command is normal mode or ioaccel1 mode.)
989 *
990 * ioaccel2 mode:
991 * bit 0 = "performant mode" bit.
992 * bits 1-4 = block fetch table entry (note extra bit)
993 * bits 4-6 = not needed, because ioaccel2 mode has
994 * a separate special register for submitting commands.
995 */
996
Webb Scales25163bd2015-04-23 09:32:00 -0500997/*
998 * set_performant_mode: Modify the tag for cciss performant
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600999 * set bit 0 for pull model, bits 3-1 for block fetch
1000 * register number
1001 */
Webb Scales25163bd2015-04-23 09:32:00 -05001002#define DEFAULT_REPLY_QUEUE (-1)
1003static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
1004 int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001005{
Matt Gates254f7962012-05-01 11:43:06 -05001006 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001007 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08001008 if (unlikely(!h->msix_vectors))
Webb Scales25163bd2015-04-23 09:32:00 -05001009 return;
1010 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
Matt Gates254f7962012-05-01 11:43:06 -05001011 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +02001012 raw_smp_processor_id() % h->nreply_queues;
Webb Scales25163bd2015-04-23 09:32:00 -05001013 else
1014 c->Header.ReplyQueue = reply_queue % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -05001015 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001016}
1017
Scott Teelc3497752014-02-18 13:56:34 -06001018static void set_ioaccel1_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05001019 struct CommandList *c,
1020 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -06001021{
1022 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
1023
Webb Scales25163bd2015-04-23 09:32:00 -05001024 /*
1025 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -06001026 * processor. This seems to give the best I/O throughput.
1027 */
Webb Scales25163bd2015-04-23 09:32:00 -05001028 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1029 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
1030 else
1031 cp->ReplyQueue = reply_queue % h->nreply_queues;
1032 /*
1033 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -06001034 * - performant mode bit (bit 0)
1035 * - pull count (bits 1-3)
1036 * - command type (bits 4-6)
1037 */
1038 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
1039 IOACCEL1_BUSADDR_CMDTYPE;
1040}
1041
Stephen Cameron8be986c2015-04-23 09:34:06 -05001042static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
1043 struct CommandList *c,
1044 int reply_queue)
1045{
1046 struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
1047 &h->ioaccel2_cmd_pool[c->cmdindex];
1048
1049 /* Tell the controller to post the reply to the queue for this
1050 * processor. This seems to give the best I/O throughput.
1051 */
1052 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1053 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1054 else
1055 cp->reply_queue = reply_queue % h->nreply_queues;
1056 /* Set the bits in the address sent down to include:
1057 * - performant mode bit not used in ioaccel mode 2
1058 * - pull count (bits 0-3)
1059 * - command type isn't needed for ioaccel2
1060 */
1061 c->busaddr |= h->ioaccel2_blockFetchTable[0];
1062}
1063
Scott Teelc3497752014-02-18 13:56:34 -06001064static void set_ioaccel2_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05001065 struct CommandList *c,
1066 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -06001067{
1068 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
1069
Webb Scales25163bd2015-04-23 09:32:00 -05001070 /*
1071 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -06001072 * processor. This seems to give the best I/O throughput.
1073 */
Webb Scales25163bd2015-04-23 09:32:00 -05001074 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1075 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1076 else
1077 cp->reply_queue = reply_queue % h->nreply_queues;
1078 /*
1079 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -06001080 * - performant mode bit not used in ioaccel mode 2
1081 * - pull count (bits 0-3)
1082 * - command type isn't needed for ioaccel2
1083 */
1084 c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
1085}
1086
Stephen M. Camerone85c5972012-05-01 11:43:42 -05001087static int is_firmware_flash_cmd(u8 *cdb)
1088{
1089 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
1090}
1091
1092/*
1093 * During firmware flash, the heartbeat register may not update as frequently
1094 * as it should. So we dial down lockup detection during firmware flash. and
1095 * dial it back up when firmware flash completes.
1096 */
1097#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
1098#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
Scott Teel3d38f002017-05-04 17:51:36 -05001099#define HPSA_EVENT_MONITOR_INTERVAL (15 * HZ)
Stephen M. Camerone85c5972012-05-01 11:43:42 -05001100static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
1101 struct CommandList *c)
1102{
1103 if (!is_firmware_flash_cmd(c->Request.CDB))
1104 return;
1105 atomic_inc(&h->firmware_flash_in_progress);
1106 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
1107}
1108
1109static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
1110 struct CommandList *c)
1111{
1112 if (is_firmware_flash_cmd(c->Request.CDB) &&
1113 atomic_dec_and_test(&h->firmware_flash_in_progress))
1114 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
1115}
1116
Webb Scales25163bd2015-04-23 09:32:00 -05001117static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
1118 struct CommandList *c, int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001119{
Stephen Cameronc05e8862015-01-23 16:44:40 -06001120 dial_down_lockup_detection_during_fw_flash(h, c);
1121 atomic_inc(&h->commands_outstanding);
Scott Teelc3497752014-02-18 13:56:34 -06001122 switch (c->cmd_type) {
1123 case CMD_IOACCEL1:
Webb Scales25163bd2015-04-23 09:32:00 -05001124 set_ioaccel1_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001125 writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
Scott Teelc3497752014-02-18 13:56:34 -06001126 break;
1127 case CMD_IOACCEL2:
Webb Scales25163bd2015-04-23 09:32:00 -05001128 set_ioaccel2_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001129 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
Scott Teelc3497752014-02-18 13:56:34 -06001130 break;
Stephen Cameron8be986c2015-04-23 09:34:06 -05001131 case IOACCEL2_TMF:
1132 set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
1133 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
1134 break;
Scott Teelc3497752014-02-18 13:56:34 -06001135 default:
Webb Scales25163bd2015-04-23 09:32:00 -05001136 set_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001137 h->access.submit_command(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06001138 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001139}
1140
Webb Scalesa58e7e52015-04-23 09:34:16 -05001141static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
Webb Scales25163bd2015-04-23 09:32:00 -05001142{
Webb Scalesd604f532015-04-23 09:35:22 -05001143 if (unlikely(hpsa_is_pending_event(c)))
Webb Scalesa58e7e52015-04-23 09:34:16 -05001144 return finish_cmd(c);
1145
Webb Scales25163bd2015-04-23 09:32:00 -05001146 __enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
1147}
1148
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001149static inline int is_hba_lunid(unsigned char scsi3addr[])
1150{
1151 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
1152}
1153
1154static inline int is_scsi_rev_5(struct ctlr_info *h)
1155{
1156 if (!h->hba_inquiry_data)
1157 return 0;
1158 if ((h->hba_inquiry_data[2] & 0x07) == 5)
1159 return 1;
1160 return 0;
1161}
1162
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001163static int hpsa_find_target_lun(struct ctlr_info *h,
1164 unsigned char scsi3addr[], int bus, int *target, int *lun)
1165{
1166 /* finds an unused bus, target, lun for a new physical device
1167 * assumes h->devlock is held
1168 */
1169 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -05001170 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001171
Akinobu Mita263d9402012-01-21 00:15:27 +09001172 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001173
1174 for (i = 0; i < h->ndevices; i++) {
1175 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +09001176 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001177 }
1178
Akinobu Mita263d9402012-01-21 00:15:27 +09001179 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
1180 if (i < HPSA_MAX_DEVICES) {
1181 /* *bus = 1; */
1182 *target = i;
1183 *lun = 0;
1184 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001185 }
1186 return !found;
1187}
1188
Don Brace1d33d852015-11-04 15:50:31 -06001189static void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
Webb Scales0d96ef52015-04-23 09:31:55 -05001190 struct hpsa_scsi_dev_t *dev, char *description)
1191{
Don Brace7c59a0d2015-11-04 15:52:22 -06001192#define LABEL_SIZE 25
1193 char label[LABEL_SIZE];
1194
Don Brace9975ec92015-11-04 15:50:25 -06001195 if (h == NULL || h->pdev == NULL || h->scsi_host == NULL)
1196 return;
1197
Don Brace7c59a0d2015-11-04 15:52:22 -06001198 switch (dev->devtype) {
1199 case TYPE_RAID:
1200 snprintf(label, LABEL_SIZE, "controller");
1201 break;
1202 case TYPE_ENCLOSURE:
1203 snprintf(label, LABEL_SIZE, "enclosure");
1204 break;
1205 case TYPE_DISK:
Don Braceaf15ed32016-02-23 15:16:15 -06001206 case TYPE_ZBC:
Don Brace7c59a0d2015-11-04 15:52:22 -06001207 if (dev->external)
1208 snprintf(label, LABEL_SIZE, "external");
1209 else if (!is_logical_dev_addr_mode(dev->scsi3addr))
1210 snprintf(label, LABEL_SIZE, "%s",
1211 raid_label[PHYSICAL_DRIVE]);
1212 else
1213 snprintf(label, LABEL_SIZE, "RAID-%s",
1214 dev->raid_level > RAID_UNKNOWN ? "?" :
1215 raid_label[dev->raid_level]);
1216 break;
1217 case TYPE_ROM:
1218 snprintf(label, LABEL_SIZE, "rom");
1219 break;
1220 case TYPE_TAPE:
1221 snprintf(label, LABEL_SIZE, "tape");
1222 break;
1223 case TYPE_MEDIUM_CHANGER:
1224 snprintf(label, LABEL_SIZE, "changer");
1225 break;
1226 default:
1227 snprintf(label, LABEL_SIZE, "UNKNOWN");
1228 break;
1229 }
1230
Webb Scales0d96ef52015-04-23 09:31:55 -05001231 dev_printk(level, &h->pdev->dev,
Don Brace7c59a0d2015-11-04 15:52:22 -06001232 "scsi %d:%d:%d:%d: %s %s %.8s %.16s %s SSDSmartPathCap%c En%c Exp=%d\n",
Webb Scales0d96ef52015-04-23 09:31:55 -05001233 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
1234 description,
1235 scsi_device_type(dev->devtype),
1236 dev->vendor,
1237 dev->model,
Don Brace7c59a0d2015-11-04 15:52:22 -06001238 label,
Webb Scales0d96ef52015-04-23 09:31:55 -05001239 dev->offload_config ? '+' : '-',
1240 dev->offload_enabled ? '+' : '-',
Kevin Barnett2a168202015-11-04 15:51:21 -06001241 dev->expose_device);
Webb Scales0d96ef52015-04-23 09:31:55 -05001242}
1243
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001244/* Add an entry into h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001245static int hpsa_scsi_add_entry(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001246 struct hpsa_scsi_dev_t *device,
1247 struct hpsa_scsi_dev_t *added[], int *nadded)
1248{
1249 /* assumes h->devlock is held */
1250 int n = h->ndevices;
1251 int i;
1252 unsigned char addr1[8], addr2[8];
1253 struct hpsa_scsi_dev_t *sd;
1254
Scott Teelcfe5bad2011-10-26 16:21:07 -05001255 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001256 dev_err(&h->pdev->dev, "too many devices, some will be "
1257 "inaccessible.\n");
1258 return -1;
1259 }
1260
1261 /* physical devices do not have lun or target assigned until now. */
1262 if (device->lun != -1)
1263 /* Logical device, lun is already assigned. */
1264 goto lun_assigned;
1265
1266 /* If this device a non-zero lun of a multi-lun device
1267 * byte 4 of the 8-byte LUN addr will contain the logical
Don Brace2b08b3e2015-01-23 16:41:09 -06001268 * unit no, zero otherwise.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001269 */
1270 if (device->scsi3addr[4] == 0) {
1271 /* This is not a non-zero lun of a multi-lun device */
1272 if (hpsa_find_target_lun(h, device->scsi3addr,
1273 device->bus, &device->target, &device->lun) != 0)
1274 return -1;
1275 goto lun_assigned;
1276 }
1277
1278 /* This is a non-zero lun of a multi-lun device.
1279 * Search through our list and find the device which
shane.seymour9a4178b2015-07-18 11:13:09 -05001280 * has the same 8 byte LUN address, excepting byte 4 and 5.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001281 * Assign the same bus and target for this new LUN.
1282 * Use the logical unit number from the firmware.
1283 */
1284 memcpy(addr1, device->scsi3addr, 8);
1285 addr1[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001286 addr1[5] = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001287 for (i = 0; i < n; i++) {
1288 sd = h->dev[i];
1289 memcpy(addr2, sd->scsi3addr, 8);
1290 addr2[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001291 addr2[5] = 0;
1292 /* differ only in byte 4 and 5? */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001293 if (memcmp(addr1, addr2, 8) == 0) {
1294 device->bus = sd->bus;
1295 device->target = sd->target;
1296 device->lun = device->scsi3addr[4];
1297 break;
1298 }
1299 }
1300 if (device->lun == -1) {
1301 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1302 " suspect firmware bug or unsupported hardware "
1303 "configuration.\n");
1304 return -1;
1305 }
1306
1307lun_assigned:
1308
1309 h->dev[n] = device;
1310 h->ndevices++;
1311 added[*nadded] = device;
1312 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001313 hpsa_show_dev_msg(KERN_INFO, h, device,
Kevin Barnett2a168202015-11-04 15:51:21 -06001314 device->expose_device ? "added" : "masked");
Robert Elliotta473d862015-04-23 09:32:54 -05001315 device->offload_to_be_enabled = device->offload_enabled;
1316 device->offload_enabled = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001317 return 0;
1318}
1319
Scott Teelbd9244f2012-01-19 14:01:30 -06001320/* Update an entry in h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001321static void hpsa_scsi_update_entry(struct ctlr_info *h,
Scott Teelbd9244f2012-01-19 14:01:30 -06001322 int entry, struct hpsa_scsi_dev_t *new_entry)
1323{
Robert Elliotta473d862015-04-23 09:32:54 -05001324 int offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001325 /* assumes h->devlock is held */
1326 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1327
1328 /* Raid level changed. */
1329 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001330
Don Brace03383732015-01-23 16:43:30 -06001331 /* Raid offload parameters changed. Careful about the ordering. */
1332 if (new_entry->offload_config && new_entry->offload_enabled) {
1333 /*
1334 * if drive is newly offload_enabled, we want to copy the
1335 * raid map data first. If previously offload_enabled and
1336 * offload_config were set, raid map data had better be
1337 * the same as it was before. if raid map data is changed
1338 * then it had better be the case that
1339 * h->dev[entry]->offload_enabled is currently 0.
1340 */
1341 h->dev[entry]->raid_map = new_entry->raid_map;
1342 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
Don Brace03383732015-01-23 16:43:30 -06001343 }
Joe Handzika3144e02015-04-23 09:32:59 -05001344 if (new_entry->hba_ioaccel_enabled) {
1345 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1346 wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1347 }
1348 h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001349 h->dev[entry]->offload_config = new_entry->offload_config;
Stephen M. Cameron9fb0de22014-02-18 13:56:50 -06001350 h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
Don Brace03383732015-01-23 16:43:30 -06001351 h->dev[entry]->queue_depth = new_entry->queue_depth;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001352
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001353 /*
1354 * We can turn off ioaccel offload now, but need to delay turning
1355 * it on until we can update h->dev[entry]->phys_disk[], but we
1356 * can't do that until all the devices are updated.
1357 */
1358 h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
1359 if (!new_entry->offload_enabled)
1360 h->dev[entry]->offload_enabled = 0;
1361
Robert Elliotta473d862015-04-23 09:32:54 -05001362 offload_enabled = h->dev[entry]->offload_enabled;
1363 h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
Webb Scales0d96ef52015-04-23 09:31:55 -05001364 hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
Robert Elliotta473d862015-04-23 09:32:54 -05001365 h->dev[entry]->offload_enabled = offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001366}
1367
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001368/* Replace an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001369static void hpsa_scsi_replace_entry(struct ctlr_info *h,
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001370 int entry, struct hpsa_scsi_dev_t *new_entry,
1371 struct hpsa_scsi_dev_t *added[], int *nadded,
1372 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1373{
1374 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -05001375 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001376 removed[*nremoved] = h->dev[entry];
1377 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -05001378
1379 /*
1380 * New physical devices won't have target/lun assigned yet
1381 * so we need to preserve the values in the slot we are replacing.
1382 */
1383 if (new_entry->target == -1) {
1384 new_entry->target = h->dev[entry]->target;
1385 new_entry->lun = h->dev[entry]->lun;
1386 }
1387
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001388 h->dev[entry] = new_entry;
1389 added[*nadded] = new_entry;
1390 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001391 hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
Robert Elliotta473d862015-04-23 09:32:54 -05001392 new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1393 new_entry->offload_enabled = 0;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001394}
1395
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001396/* Remove an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001397static void hpsa_scsi_remove_entry(struct ctlr_info *h, int entry,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001398 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1399{
1400 /* assumes h->devlock is held */
1401 int i;
1402 struct hpsa_scsi_dev_t *sd;
1403
Scott Teelcfe5bad2011-10-26 16:21:07 -05001404 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001405
1406 sd = h->dev[entry];
1407 removed[*nremoved] = h->dev[entry];
1408 (*nremoved)++;
1409
1410 for (i = entry; i < h->ndevices-1; i++)
1411 h->dev[i] = h->dev[i+1];
1412 h->ndevices--;
Webb Scales0d96ef52015-04-23 09:31:55 -05001413 hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001414}
1415
1416#define SCSI3ADDR_EQ(a, b) ( \
1417 (a)[7] == (b)[7] && \
1418 (a)[6] == (b)[6] && \
1419 (a)[5] == (b)[5] && \
1420 (a)[4] == (b)[4] && \
1421 (a)[3] == (b)[3] && \
1422 (a)[2] == (b)[2] && \
1423 (a)[1] == (b)[1] && \
1424 (a)[0] == (b)[0])
1425
1426static void fixup_botched_add(struct ctlr_info *h,
1427 struct hpsa_scsi_dev_t *added)
1428{
1429 /* called when scsi_add_device fails in order to re-adjust
1430 * h->dev[] to match the mid layer's view.
1431 */
1432 unsigned long flags;
1433 int i, j;
1434
1435 spin_lock_irqsave(&h->lock, flags);
1436 for (i = 0; i < h->ndevices; i++) {
1437 if (h->dev[i] == added) {
1438 for (j = i; j < h->ndevices-1; j++)
1439 h->dev[j] = h->dev[j+1];
1440 h->ndevices--;
1441 break;
1442 }
1443 }
1444 spin_unlock_irqrestore(&h->lock, flags);
1445 kfree(added);
1446}
1447
1448static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1449 struct hpsa_scsi_dev_t *dev2)
1450{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001451 /* we compare everything except lun and target as these
1452 * are not yet assigned. Compare parts likely
1453 * to differ first
1454 */
1455 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1456 sizeof(dev1->scsi3addr)) != 0)
1457 return 0;
1458 if (memcmp(dev1->device_id, dev2->device_id,
1459 sizeof(dev1->device_id)) != 0)
1460 return 0;
1461 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1462 return 0;
1463 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1464 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001465 if (dev1->devtype != dev2->devtype)
1466 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001467 if (dev1->bus != dev2->bus)
1468 return 0;
1469 return 1;
1470}
1471
Scott Teelbd9244f2012-01-19 14:01:30 -06001472static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1473 struct hpsa_scsi_dev_t *dev2)
1474{
1475 /* Device attributes that can change, but don't mean
1476 * that the device is a different device, nor that the OS
1477 * needs to be told anything about the change.
1478 */
1479 if (dev1->raid_level != dev2->raid_level)
1480 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001481 if (dev1->offload_config != dev2->offload_config)
1482 return 1;
1483 if (dev1->offload_enabled != dev2->offload_enabled)
1484 return 1;
Don Brace93849502015-07-18 11:12:49 -05001485 if (!is_logical_dev_addr_mode(dev1->scsi3addr))
1486 if (dev1->queue_depth != dev2->queue_depth)
1487 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -06001488 return 0;
1489}
1490
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001491/* Find needle in haystack. If exact match found, return DEVICE_SAME,
1492 * and return needle location in *index. If scsi3addr matches, but not
1493 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -06001494 * location in *index.
1495 * In the case of a minor device attribute change, such as RAID level, just
1496 * return DEVICE_UPDATED, along with the updated device's location in index.
1497 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001498 */
1499static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1500 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1501 int *index)
1502{
1503 int i;
1504#define DEVICE_NOT_FOUND 0
1505#define DEVICE_CHANGED 1
1506#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -06001507#define DEVICE_UPDATED 3
Don Brace1d33d852015-11-04 15:50:31 -06001508 if (needle == NULL)
1509 return DEVICE_NOT_FOUND;
1510
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001511 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -06001512 if (haystack[i] == NULL) /* previously removed. */
1513 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001514 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1515 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -06001516 if (device_is_the_same(needle, haystack[i])) {
1517 if (device_updated(needle, haystack[i]))
1518 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001519 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -06001520 } else {
Stephen M. Cameron98465902014-02-21 16:25:00 -06001521 /* Keep offline devices offline */
1522 if (needle->volume_offline)
1523 return DEVICE_NOT_FOUND;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001524 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -06001525 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001526 }
1527 }
1528 *index = -1;
1529 return DEVICE_NOT_FOUND;
1530}
1531
Stephen M. Cameron98465902014-02-21 16:25:00 -06001532static void hpsa_monitor_offline_device(struct ctlr_info *h,
1533 unsigned char scsi3addr[])
1534{
1535 struct offline_device_entry *device;
1536 unsigned long flags;
1537
1538 /* Check to see if device is already on the list */
1539 spin_lock_irqsave(&h->offline_device_lock, flags);
1540 list_for_each_entry(device, &h->offline_device_list, offline_list) {
1541 if (memcmp(device->scsi3addr, scsi3addr,
1542 sizeof(device->scsi3addr)) == 0) {
1543 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1544 return;
1545 }
1546 }
1547 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1548
1549 /* Device is not on the list, add it. */
1550 device = kmalloc(sizeof(*device), GFP_KERNEL);
Amit Kushwaha7e8a9482016-12-12 16:34:21 +05301551 if (!device)
Stephen M. Cameron98465902014-02-21 16:25:00 -06001552 return;
Amit Kushwaha7e8a9482016-12-12 16:34:21 +05301553
Stephen M. Cameron98465902014-02-21 16:25:00 -06001554 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1555 spin_lock_irqsave(&h->offline_device_lock, flags);
1556 list_add_tail(&device->offline_list, &h->offline_device_list);
1557 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1558}
1559
1560/* Print a message explaining various offline volume states */
1561static void hpsa_show_volume_status(struct ctlr_info *h,
1562 struct hpsa_scsi_dev_t *sd)
1563{
1564 if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
1565 dev_info(&h->pdev->dev,
1566 "C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
1567 h->scsi_host->host_no,
1568 sd->bus, sd->target, sd->lun);
1569 switch (sd->volume_offline) {
1570 case HPSA_LV_OK:
1571 break;
1572 case HPSA_LV_UNDERGOING_ERASE:
1573 dev_info(&h->pdev->dev,
1574 "C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
1575 h->scsi_host->host_no,
1576 sd->bus, sd->target, sd->lun);
1577 break;
Scott Benesh5ca01202015-07-18 11:13:04 -05001578 case HPSA_LV_NOT_AVAILABLE:
1579 dev_info(&h->pdev->dev,
1580 "C%d:B%d:T%d:L%d Volume is waiting for transforming volume.\n",
1581 h->scsi_host->host_no,
1582 sd->bus, sd->target, sd->lun);
1583 break;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001584 case HPSA_LV_UNDERGOING_RPI:
1585 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001586 "C%d:B%d:T%d:L%d Volume is undergoing rapid parity init.\n",
Stephen M. Cameron98465902014-02-21 16:25:00 -06001587 h->scsi_host->host_no,
1588 sd->bus, sd->target, sd->lun);
1589 break;
1590 case HPSA_LV_PENDING_RPI:
1591 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001592 "C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
1593 h->scsi_host->host_no,
1594 sd->bus, sd->target, sd->lun);
Stephen M. Cameron98465902014-02-21 16:25:00 -06001595 break;
1596 case HPSA_LV_ENCRYPTED_NO_KEY:
1597 dev_info(&h->pdev->dev,
1598 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
1599 h->scsi_host->host_no,
1600 sd->bus, sd->target, sd->lun);
1601 break;
1602 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
1603 dev_info(&h->pdev->dev,
1604 "C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
1605 h->scsi_host->host_no,
1606 sd->bus, sd->target, sd->lun);
1607 break;
1608 case HPSA_LV_UNDERGOING_ENCRYPTION:
1609 dev_info(&h->pdev->dev,
1610 "C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
1611 h->scsi_host->host_no,
1612 sd->bus, sd->target, sd->lun);
1613 break;
1614 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
1615 dev_info(&h->pdev->dev,
1616 "C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
1617 h->scsi_host->host_no,
1618 sd->bus, sd->target, sd->lun);
1619 break;
1620 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1621 dev_info(&h->pdev->dev,
1622 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
1623 h->scsi_host->host_no,
1624 sd->bus, sd->target, sd->lun);
1625 break;
1626 case HPSA_LV_PENDING_ENCRYPTION:
1627 dev_info(&h->pdev->dev,
1628 "C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
1629 h->scsi_host->host_no,
1630 sd->bus, sd->target, sd->lun);
1631 break;
1632 case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
1633 dev_info(&h->pdev->dev,
1634 "C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
1635 h->scsi_host->host_no,
1636 sd->bus, sd->target, sd->lun);
1637 break;
1638 }
1639}
1640
Don Brace03383732015-01-23 16:43:30 -06001641/*
1642 * Figure the list of physical drive pointers for a logical drive with
1643 * raid offload configured.
1644 */
1645static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
1646 struct hpsa_scsi_dev_t *dev[], int ndevices,
1647 struct hpsa_scsi_dev_t *logical_drive)
1648{
1649 struct raid_map_data *map = &logical_drive->raid_map;
1650 struct raid_map_disk_data *dd = &map->data[0];
1651 int i, j;
1652 int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
1653 le16_to_cpu(map->metadata_disks_per_row);
1654 int nraid_map_entries = le16_to_cpu(map->row_cnt) *
1655 le16_to_cpu(map->layout_map_count) *
1656 total_disks_per_row;
1657 int nphys_disk = le16_to_cpu(map->layout_map_count) *
1658 total_disks_per_row;
1659 int qdepth;
1660
1661 if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
1662 nraid_map_entries = RAID_MAP_MAX_ENTRIES;
1663
Webb Scalesd604f532015-04-23 09:35:22 -05001664 logical_drive->nphysical_disks = nraid_map_entries;
1665
Don Brace03383732015-01-23 16:43:30 -06001666 qdepth = 0;
1667 for (i = 0; i < nraid_map_entries; i++) {
1668 logical_drive->phys_disk[i] = NULL;
1669 if (!logical_drive->offload_config)
1670 continue;
1671 for (j = 0; j < ndevices; j++) {
Don Brace1d33d852015-11-04 15:50:31 -06001672 if (dev[j] == NULL)
1673 continue;
Petros Koutoupisff615f02016-05-09 13:44:10 -05001674 if (dev[j]->devtype != TYPE_DISK &&
1675 dev[j]->devtype != TYPE_ZBC)
Don Braceaf15ed32016-02-23 15:16:15 -06001676 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001677 if (is_logical_device(dev[j]))
Don Brace03383732015-01-23 16:43:30 -06001678 continue;
1679 if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
1680 continue;
1681
1682 logical_drive->phys_disk[i] = dev[j];
1683 if (i < nphys_disk)
1684 qdepth = min(h->nr_cmds, qdepth +
1685 logical_drive->phys_disk[i]->queue_depth);
1686 break;
1687 }
1688
1689 /*
1690 * This can happen if a physical drive is removed and
1691 * the logical drive is degraded. In that case, the RAID
1692 * map data will refer to a physical disk which isn't actually
1693 * present. And in that case offload_enabled should already
1694 * be 0, but we'll turn it off here just in case
1695 */
1696 if (!logical_drive->phys_disk[i]) {
1697 logical_drive->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001698 logical_drive->offload_to_be_enabled = 0;
1699 logical_drive->queue_depth = 8;
Don Brace03383732015-01-23 16:43:30 -06001700 }
1701 }
1702 if (nraid_map_entries)
1703 /*
1704 * This is correct for reads, too high for full stripe writes,
1705 * way too high for partial stripe writes
1706 */
1707 logical_drive->queue_depth = qdepth;
1708 else
1709 logical_drive->queue_depth = h->nr_cmds;
1710}
1711
1712static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
1713 struct hpsa_scsi_dev_t *dev[], int ndevices)
1714{
1715 int i;
1716
1717 for (i = 0; i < ndevices; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001718 if (dev[i] == NULL)
1719 continue;
Petros Koutoupisff615f02016-05-09 13:44:10 -05001720 if (dev[i]->devtype != TYPE_DISK &&
1721 dev[i]->devtype != TYPE_ZBC)
Don Braceaf15ed32016-02-23 15:16:15 -06001722 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001723 if (!is_logical_device(dev[i]))
Don Brace03383732015-01-23 16:43:30 -06001724 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001725
1726 /*
1727 * If offload is currently enabled, the RAID map and
1728 * phys_disk[] assignment *better* not be changing
1729 * and since it isn't changing, we do not need to
1730 * update it.
1731 */
1732 if (dev[i]->offload_enabled)
1733 continue;
1734
Don Brace03383732015-01-23 16:43:30 -06001735 hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
1736 }
1737}
1738
Kevin Barnett096ccff2015-11-04 15:51:51 -06001739static int hpsa_add_device(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1740{
1741 int rc = 0;
1742
1743 if (!h->scsi_host)
1744 return 1;
1745
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001746 if (is_logical_device(device)) /* RAID */
1747 rc = scsi_add_device(h->scsi_host, device->bus,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001748 device->target, device->lun);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001749 else /* HBA */
1750 rc = hpsa_add_sas_device(h->sas_host, device);
1751
Kevin Barnett096ccff2015-11-04 15:51:51 -06001752 return rc;
1753}
1754
Don Braceba74fdc2016-04-27 17:14:17 -05001755static int hpsa_find_outstanding_commands_for_dev(struct ctlr_info *h,
1756 struct hpsa_scsi_dev_t *dev)
1757{
1758 int i;
1759 int count = 0;
1760
1761 for (i = 0; i < h->nr_cmds; i++) {
1762 struct CommandList *c = h->cmd_pool + i;
1763 int refcount = atomic_inc_return(&c->refcount);
1764
1765 if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev,
1766 dev->scsi3addr)) {
1767 unsigned long flags;
1768
1769 spin_lock_irqsave(&h->lock, flags); /* Implied MB */
1770 if (!hpsa_is_cmd_idle(c))
1771 ++count;
1772 spin_unlock_irqrestore(&h->lock, flags);
1773 }
1774
1775 cmd_free(h, c);
1776 }
1777
1778 return count;
1779}
1780
1781static void hpsa_wait_for_outstanding_commands_for_dev(struct ctlr_info *h,
1782 struct hpsa_scsi_dev_t *device)
1783{
1784 int cmds = 0;
1785 int waits = 0;
1786
1787 while (1) {
1788 cmds = hpsa_find_outstanding_commands_for_dev(h, device);
1789 if (cmds == 0)
1790 break;
1791 if (++waits > 20)
1792 break;
1793 dev_warn(&h->pdev->dev,
1794 "%s: removing device with %d outstanding commands!\n",
1795 __func__, cmds);
1796 msleep(1000);
1797 }
1798}
1799
Kevin Barnett096ccff2015-11-04 15:51:51 -06001800static void hpsa_remove_device(struct ctlr_info *h,
1801 struct hpsa_scsi_dev_t *device)
1802{
1803 struct scsi_device *sdev = NULL;
1804
1805 if (!h->scsi_host)
1806 return;
1807
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001808 if (is_logical_device(device)) { /* RAID */
1809 sdev = scsi_device_lookup(h->scsi_host, device->bus,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001810 device->target, device->lun);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001811 if (sdev) {
1812 scsi_remove_device(sdev);
1813 scsi_device_put(sdev);
1814 } else {
1815 /*
1816 * We don't expect to get here. Future commands
1817 * to this device will get a selection timeout as
1818 * if the device were gone.
1819 */
1820 hpsa_show_dev_msg(KERN_WARNING, h, device,
Kevin Barnett096ccff2015-11-04 15:51:51 -06001821 "didn't find device for removal.");
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001822 }
Don Braceba74fdc2016-04-27 17:14:17 -05001823 } else { /* HBA */
1824
1825 device->removed = 1;
1826 hpsa_wait_for_outstanding_commands_for_dev(h, device);
1827
Kevin Barnettd04e62b2015-11-04 15:52:34 -06001828 hpsa_remove_sas_device(device);
Don Braceba74fdc2016-04-27 17:14:17 -05001829 }
Kevin Barnett096ccff2015-11-04 15:51:51 -06001830}
1831
Don Brace8aa60682015-11-04 15:50:01 -06001832static void adjust_hpsa_scsi_table(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001833 struct hpsa_scsi_dev_t *sd[], int nsds)
1834{
1835 /* sd contains scsi3 addresses and devtypes, and inquiry
1836 * data. This function takes what's in sd to be the current
1837 * reality and updates h->dev[] to reflect that reality.
1838 */
1839 int i, entry, device_change, changes = 0;
1840 struct hpsa_scsi_dev_t *csd;
1841 unsigned long flags;
1842 struct hpsa_scsi_dev_t **added, **removed;
1843 int nadded, nremoved;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001844
Don Braceda03ded2015-11-04 15:50:56 -06001845 /*
1846 * A reset can cause a device status to change
1847 * re-schedule the scan to see what happened.
1848 */
Don Bracec59d04f2017-05-04 17:51:22 -05001849 spin_lock_irqsave(&h->reset_lock, flags);
Don Braceda03ded2015-11-04 15:50:56 -06001850 if (h->reset_in_progress) {
1851 h->drv_req_rescan = 1;
Don Bracec59d04f2017-05-04 17:51:22 -05001852 spin_unlock_irqrestore(&h->reset_lock, flags);
Don Braceda03ded2015-11-04 15:50:56 -06001853 return;
1854 }
Don Bracec59d04f2017-05-04 17:51:22 -05001855 spin_unlock_irqrestore(&h->reset_lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001856
Scott Teelcfe5bad2011-10-26 16:21:07 -05001857 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1858 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001859
1860 if (!added || !removed) {
1861 dev_warn(&h->pdev->dev, "out of memory in "
1862 "adjust_hpsa_scsi_table\n");
1863 goto free_and_out;
1864 }
1865
1866 spin_lock_irqsave(&h->devlock, flags);
1867
1868 /* find any devices in h->dev[] that are not in
1869 * sd[] and remove them from h->dev[], and for any
1870 * devices which have changed, remove the old device
1871 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001872 * If minor device attributes change, just update
1873 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001874 */
1875 i = 0;
1876 nremoved = 0;
1877 nadded = 0;
1878 while (i < h->ndevices) {
1879 csd = h->dev[i];
1880 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1881 if (device_change == DEVICE_NOT_FOUND) {
1882 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001883 hpsa_scsi_remove_entry(h, i, removed, &nremoved);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001884 continue; /* remove ^^^, hence i not incremented */
1885 } else if (device_change == DEVICE_CHANGED) {
1886 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001887 hpsa_scsi_replace_entry(h, i, sd[entry],
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001888 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001889 /* Set it to NULL to prevent it from being freed
1890 * at the bottom of hpsa_update_scsi_devices()
1891 */
1892 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001893 } else if (device_change == DEVICE_UPDATED) {
Don Brace8aa60682015-11-04 15:50:01 -06001894 hpsa_scsi_update_entry(h, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001895 }
1896 i++;
1897 }
1898
1899 /* Now, make sure every device listed in sd[] is also
1900 * listed in h->dev[], adding them if they aren't found
1901 */
1902
1903 for (i = 0; i < nsds; i++) {
1904 if (!sd[i]) /* if already added above. */
1905 continue;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001906
1907 /* Don't add devices which are NOT READY, FORMAT IN PROGRESS
1908 * as the SCSI mid-layer does not handle such devices well.
1909 * It relentlessly loops sending TUR at 3Hz, then READ(10)
1910 * at 160Hz, and prevents the system from coming up.
1911 */
1912 if (sd[i]->volume_offline) {
1913 hpsa_show_volume_status(h, sd[i]);
Webb Scales0d96ef52015-04-23 09:31:55 -05001914 hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
Stephen M. Cameron98465902014-02-21 16:25:00 -06001915 continue;
1916 }
1917
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001918 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1919 h->ndevices, &entry);
1920 if (device_change == DEVICE_NOT_FOUND) {
1921 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001922 if (hpsa_scsi_add_entry(h, sd[i], added, &nadded) != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001923 break;
1924 sd[i] = NULL; /* prevent from being freed later. */
1925 } else if (device_change == DEVICE_CHANGED) {
1926 /* should never happen... */
1927 changes++;
1928 dev_warn(&h->pdev->dev,
1929 "device unexpectedly changed.\n");
1930 /* but if it does happen, we just ignore that device */
1931 }
1932 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001933 hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
1934
1935 /* Now that h->dev[]->phys_disk[] is coherent, we can enable
1936 * any logical drives that need it enabled.
1937 */
Don Brace1d33d852015-11-04 15:50:31 -06001938 for (i = 0; i < h->ndevices; i++) {
1939 if (h->dev[i] == NULL)
1940 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001941 h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
Don Brace1d33d852015-11-04 15:50:31 -06001942 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001943
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001944 spin_unlock_irqrestore(&h->devlock, flags);
1945
Stephen M. Cameron98465902014-02-21 16:25:00 -06001946 /* Monitor devices which are in one of several NOT READY states to be
1947 * brought online later. This must be done without holding h->devlock,
1948 * so don't touch h->dev[]
1949 */
1950 for (i = 0; i < nsds; i++) {
1951 if (!sd[i]) /* if already added above. */
1952 continue;
1953 if (sd[i]->volume_offline)
1954 hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
1955 }
1956
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001957 /* Don't notify scsi mid layer of any changes the first time through
1958 * (or if there are no changes) scsi_scan_host will do it later the
1959 * first time through.
1960 */
Don Brace8aa60682015-11-04 15:50:01 -06001961 if (!changes)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001962 goto free_and_out;
1963
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001964 /* Notify scsi mid layer of any removed devices */
1965 for (i = 0; i < nremoved; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001966 if (removed[i] == NULL)
1967 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001968 if (removed[i]->expose_device)
1969 hpsa_remove_device(h, removed[i]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001970 kfree(removed[i]);
1971 removed[i] = NULL;
1972 }
1973
1974 /* Notify scsi mid layer of any added devices */
1975 for (i = 0; i < nadded; i++) {
Kevin Barnett096ccff2015-11-04 15:51:51 -06001976 int rc = 0;
1977
Don Brace1d33d852015-11-04 15:50:31 -06001978 if (added[i] == NULL)
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001979 continue;
Kevin Barnett2a168202015-11-04 15:51:21 -06001980 if (!(added[i]->expose_device))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001981 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001982 rc = hpsa_add_device(h, added[i]);
1983 if (!rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001984 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001985 dev_warn(&h->pdev->dev,
1986 "addition failed %d, device not added.", rc);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001987 /* now we have to remove it from h->dev,
1988 * since it didn't get added to scsi mid layer
1989 */
1990 fixup_botched_add(h, added[i]);
Don Brace853633e2015-11-04 15:50:37 -06001991 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001992 }
1993
1994free_and_out:
1995 kfree(added);
1996 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001997}
1998
1999/*
Joe Perches9e03aa22013-09-03 13:45:58 -07002000 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002001 * Assume's h->devlock is held.
2002 */
2003static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
2004 int bus, int target, int lun)
2005{
2006 int i;
2007 struct hpsa_scsi_dev_t *sd;
2008
2009 for (i = 0; i < h->ndevices; i++) {
2010 sd = h->dev[i];
2011 if (sd->bus == bus && sd->target == target && sd->lun == lun)
2012 return sd;
2013 }
2014 return NULL;
2015}
2016
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002017static int hpsa_slave_alloc(struct scsi_device *sdev)
2018{
Hannes Reinecke7630b3a2016-11-17 12:15:56 +01002019 struct hpsa_scsi_dev_t *sd = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002020 unsigned long flags;
2021 struct ctlr_info *h;
2022
2023 h = sdev_to_hba(sdev);
2024 spin_lock_irqsave(&h->devlock, flags);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06002025 if (sdev_channel(sdev) == HPSA_PHYSICAL_DEVICE_BUS) {
2026 struct scsi_target *starget;
2027 struct sas_rphy *rphy;
2028
2029 starget = scsi_target(sdev);
2030 rphy = target_to_rphy(starget);
2031 sd = hpsa_find_device_by_sas_rphy(h, rphy);
2032 if (sd) {
2033 sd->target = sdev_id(sdev);
2034 sd->lun = sdev->lun;
2035 }
Hannes Reinecke7630b3a2016-11-17 12:15:56 +01002036 }
2037 if (!sd)
Kevin Barnettd04e62b2015-11-04 15:52:34 -06002038 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
2039 sdev_id(sdev), sdev->lun);
2040
2041 if (sd && sd->expose_device) {
Don Brace03383732015-01-23 16:43:30 -06002042 atomic_set(&sd->ioaccel_cmds_out, 0);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06002043 sdev->hostdata = sd;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002044 } else
2045 sdev->hostdata = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002046 spin_unlock_irqrestore(&h->devlock, flags);
2047 return 0;
2048}
2049
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002050/* configure scsi device based on internal per-device structure */
2051static int hpsa_slave_configure(struct scsi_device *sdev)
2052{
2053 struct hpsa_scsi_dev_t *sd;
2054 int queue_depth;
2055
2056 sd = sdev->hostdata;
Kevin Barnett2a168202015-11-04 15:51:21 -06002057 sdev->no_uld_attach = !sd || !sd->expose_device;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002058
Don Brace50864352017-05-04 17:51:28 -05002059 if (sd) {
2060 if (sd->external)
2061 queue_depth = EXTERNAL_QD;
2062 else
2063 queue_depth = sd->queue_depth != 0 ?
2064 sd->queue_depth : sdev->host->can_queue;
2065 } else
Stephen Cameron41ce4c32015-04-23 09:31:47 -05002066 queue_depth = sdev->host->can_queue;
2067
2068 scsi_change_queue_depth(sdev, queue_depth);
2069
2070 return 0;
2071}
2072
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002073static void hpsa_slave_destroy(struct scsi_device *sdev)
2074{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06002075 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002076}
2077
Webb Scalesd9a729f2015-04-23 09:33:27 -05002078static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
2079{
2080 int i;
2081
2082 if (!h->ioaccel2_cmd_sg_list)
2083 return;
2084 for (i = 0; i < h->nr_cmds; i++) {
2085 kfree(h->ioaccel2_cmd_sg_list[i]);
2086 h->ioaccel2_cmd_sg_list[i] = NULL;
2087 }
2088 kfree(h->ioaccel2_cmd_sg_list);
2089 h->ioaccel2_cmd_sg_list = NULL;
2090}
2091
2092static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
2093{
2094 int i;
2095
2096 if (h->chainsize <= 0)
2097 return 0;
2098
2099 h->ioaccel2_cmd_sg_list =
2100 kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
2101 GFP_KERNEL);
2102 if (!h->ioaccel2_cmd_sg_list)
2103 return -ENOMEM;
2104 for (i = 0; i < h->nr_cmds; i++) {
2105 h->ioaccel2_cmd_sg_list[i] =
2106 kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
2107 h->maxsgentries, GFP_KERNEL);
2108 if (!h->ioaccel2_cmd_sg_list[i])
2109 goto clean;
2110 }
2111 return 0;
2112
2113clean:
2114 hpsa_free_ioaccel2_sg_chain_blocks(h);
2115 return -ENOMEM;
2116}
2117
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002118static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
2119{
2120 int i;
2121
2122 if (!h->cmd_sg_list)
2123 return;
2124 for (i = 0; i < h->nr_cmds; i++) {
2125 kfree(h->cmd_sg_list[i]);
2126 h->cmd_sg_list[i] = NULL;
2127 }
2128 kfree(h->cmd_sg_list);
2129 h->cmd_sg_list = NULL;
2130}
2131
Robert Elliott105a3db2015-04-23 09:33:48 -05002132static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002133{
2134 int i;
2135
2136 if (h->chainsize <= 0)
2137 return 0;
2138
2139 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
2140 GFP_KERNEL);
Amit Kushwaha7e8a9482016-12-12 16:34:21 +05302141 if (!h->cmd_sg_list)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002142 return -ENOMEM;
Amit Kushwaha7e8a9482016-12-12 16:34:21 +05302143
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002144 for (i = 0; i < h->nr_cmds; i++) {
2145 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
2146 h->chainsize, GFP_KERNEL);
Amit Kushwaha7e8a9482016-12-12 16:34:21 +05302147 if (!h->cmd_sg_list[i])
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002148 goto clean;
Amit Kushwaha7e8a9482016-12-12 16:34:21 +05302149
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002150 }
2151 return 0;
2152
2153clean:
2154 hpsa_free_sg_chain_blocks(h);
2155 return -ENOMEM;
2156}
2157
Webb Scalesd9a729f2015-04-23 09:33:27 -05002158static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
2159 struct io_accel2_cmd *cp, struct CommandList *c)
2160{
2161 struct ioaccel2_sg_element *chain_block;
2162 u64 temp64;
2163 u32 chain_size;
2164
2165 chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
Don Bracea736e9b2015-11-04 15:51:14 -06002166 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002167 temp64 = pci_map_single(h->pdev, chain_block, chain_size,
2168 PCI_DMA_TODEVICE);
2169 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2170 /* prevent subsequent unmapping */
2171 cp->sg->address = 0;
2172 return -1;
2173 }
2174 cp->sg->address = cpu_to_le64(temp64);
2175 return 0;
2176}
2177
2178static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
2179 struct io_accel2_cmd *cp)
2180{
2181 struct ioaccel2_sg_element *chain_sg;
2182 u64 temp64;
2183 u32 chain_size;
2184
2185 chain_sg = cp->sg;
2186 temp64 = le64_to_cpu(chain_sg->address);
Don Bracea736e9b2015-11-04 15:51:14 -06002187 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002188 pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
2189}
2190
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002191static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002192 struct CommandList *c)
2193{
2194 struct SGDescriptor *chain_sg, *chain_block;
2195 u64 temp64;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002196 u32 chain_len;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002197
2198 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
2199 chain_block = h->cmd_sg_list[c->cmdindex];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002200 chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
2201 chain_len = sizeof(*chain_sg) *
Don Brace2b08b3e2015-01-23 16:41:09 -06002202 (le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002203 chain_sg->Len = cpu_to_le32(chain_len);
2204 temp64 = pci_map_single(h->pdev, chain_block, chain_len,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002205 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002206 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2207 /* prevent subsequent unmapping */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002208 chain_sg->Addr = cpu_to_le64(0);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002209 return -1;
2210 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002211 chain_sg->Addr = cpu_to_le64(temp64);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002212 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002213}
2214
2215static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
2216 struct CommandList *c)
2217{
2218 struct SGDescriptor *chain_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002219
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002220 if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002221 return;
2222
2223 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002224 pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
2225 le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002226}
2227
Scott Teela09c1442014-02-18 13:57:21 -06002228
2229/* Decode the various types of errors on ioaccel2 path.
2230 * Return 1 for any error that should generate a RAID path retry.
2231 * Return 0 for errors that don't require a RAID path retry.
2232 */
2233static int handle_ioaccel_mode2_error(struct ctlr_info *h,
Scott Teelc3497752014-02-18 13:56:34 -06002234 struct CommandList *c,
2235 struct scsi_cmnd *cmd,
Don Braceba74fdc2016-04-27 17:14:17 -05002236 struct io_accel2_cmd *c2,
2237 struct hpsa_scsi_dev_t *dev)
Scott Teelc3497752014-02-18 13:56:34 -06002238{
2239 int data_len;
Scott Teela09c1442014-02-18 13:57:21 -06002240 int retry = 0;
Joe Handzikc40820d2015-04-23 09:33:32 -05002241 u32 ioaccel2_resid = 0;
Scott Teelc3497752014-02-18 13:56:34 -06002242
2243 switch (c2->error_data.serv_response) {
2244 case IOACCEL2_SERV_RESPONSE_COMPLETE:
2245 switch (c2->error_data.status) {
2246 case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
2247 break;
2248 case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002249 cmd->result |= SAM_STAT_CHECK_CONDITION;
Scott Teelc3497752014-02-18 13:56:34 -06002250 if (c2->error_data.data_present !=
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002251 IOACCEL2_SENSE_DATA_PRESENT) {
2252 memset(cmd->sense_buffer, 0,
2253 SCSI_SENSE_BUFFERSIZE);
Scott Teelc3497752014-02-18 13:56:34 -06002254 break;
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002255 }
Scott Teelc3497752014-02-18 13:56:34 -06002256 /* copy the sense data */
2257 data_len = c2->error_data.sense_data_len;
2258 if (data_len > SCSI_SENSE_BUFFERSIZE)
2259 data_len = SCSI_SENSE_BUFFERSIZE;
2260 if (data_len > sizeof(c2->error_data.sense_data_buff))
2261 data_len =
2262 sizeof(c2->error_data.sense_data_buff);
2263 memcpy(cmd->sense_buffer,
2264 c2->error_data.sense_data_buff, data_len);
Scott Teela09c1442014-02-18 13:57:21 -06002265 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002266 break;
2267 case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
Scott Teela09c1442014-02-18 13:57:21 -06002268 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002269 break;
2270 case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
Scott Teela09c1442014-02-18 13:57:21 -06002271 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002272 break;
2273 case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
Stephen Cameron4a8da222015-04-23 09:32:43 -05002274 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002275 break;
2276 case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
Scott Teela09c1442014-02-18 13:57:21 -06002277 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002278 break;
2279 default:
Scott Teela09c1442014-02-18 13:57:21 -06002280 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002281 break;
2282 }
2283 break;
2284 case IOACCEL2_SERV_RESPONSE_FAILURE:
Joe Handzikc40820d2015-04-23 09:33:32 -05002285 switch (c2->error_data.status) {
2286 case IOACCEL2_STATUS_SR_IO_ERROR:
2287 case IOACCEL2_STATUS_SR_IO_ABORTED:
2288 case IOACCEL2_STATUS_SR_OVERRUN:
2289 retry = 1;
2290 break;
2291 case IOACCEL2_STATUS_SR_UNDERRUN:
2292 cmd->result = (DID_OK << 16); /* host byte */
2293 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
2294 ioaccel2_resid = get_unaligned_le32(
2295 &c2->error_data.resid_cnt[0]);
2296 scsi_set_resid(cmd, ioaccel2_resid);
2297 break;
2298 case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
2299 case IOACCEL2_STATUS_SR_INVALID_DEVICE:
2300 case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
Don Braceba74fdc2016-04-27 17:14:17 -05002301 /*
2302 * Did an HBA disk disappear? We will eventually
2303 * get a state change event from the controller but
2304 * in the meantime, we need to tell the OS that the
2305 * HBA disk is no longer there and stop I/O
2306 * from going down. This allows the potential re-insert
2307 * of the disk to get the same device node.
2308 */
2309 if (dev->physical_device && dev->expose_device) {
2310 cmd->result = DID_NO_CONNECT << 16;
2311 dev->removed = 1;
2312 h->drv_req_rescan = 1;
2313 dev_warn(&h->pdev->dev,
2314 "%s: device is gone!\n", __func__);
2315 } else
2316 /*
2317 * Retry by sending down the RAID path.
2318 * We will get an event from ctlr to
2319 * trigger rescan regardless.
2320 */
2321 retry = 1;
Joe Handzikc40820d2015-04-23 09:33:32 -05002322 break;
2323 default:
2324 retry = 1;
Joe Handzikc40820d2015-04-23 09:33:32 -05002325 }
Scott Teelc3497752014-02-18 13:56:34 -06002326 break;
2327 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
2328 break;
2329 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
2330 break;
2331 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
Scott Teela09c1442014-02-18 13:57:21 -06002332 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002333 break;
2334 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
Scott Teelc3497752014-02-18 13:56:34 -06002335 break;
2336 default:
Scott Teela09c1442014-02-18 13:57:21 -06002337 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002338 break;
2339 }
Scott Teela09c1442014-02-18 13:57:21 -06002340
2341 return retry; /* retry on raid path? */
Scott Teelc3497752014-02-18 13:56:34 -06002342}
2343
Webb Scalesa58e7e52015-04-23 09:34:16 -05002344static void hpsa_cmd_resolve_events(struct ctlr_info *h,
2345 struct CommandList *c)
2346{
Webb Scalesd604f532015-04-23 09:35:22 -05002347 bool do_wake = false;
2348
Webb Scalesa58e7e52015-04-23 09:34:16 -05002349 /*
Don Brace08ec46f2017-05-04 17:51:49 -05002350 * Reset c->scsi_cmd here so that the reset handler will know
Webb Scalesd604f532015-04-23 09:35:22 -05002351 * this command has completed. Then, check to see if the handler is
Webb Scalesa58e7e52015-04-23 09:34:16 -05002352 * waiting for this command, and, if so, wake it.
2353 */
2354 c->scsi_cmd = SCSI_CMD_IDLE;
Webb Scalesd604f532015-04-23 09:35:22 -05002355 mb(); /* Declare command idle before checking for pending events. */
Webb Scalesd604f532015-04-23 09:35:22 -05002356 if (c->reset_pending) {
2357 unsigned long flags;
2358 struct hpsa_scsi_dev_t *dev;
2359
2360 /*
2361 * There appears to be a reset pending; lock the lock and
2362 * reconfirm. If so, then decrement the count of outstanding
2363 * commands and wake the reset command if this is the last one.
2364 */
2365 spin_lock_irqsave(&h->lock, flags);
2366 dev = c->reset_pending; /* Re-fetch under the lock. */
2367 if (dev && atomic_dec_and_test(&dev->reset_cmds_out))
2368 do_wake = true;
2369 c->reset_pending = NULL;
2370 spin_unlock_irqrestore(&h->lock, flags);
2371 }
2372
2373 if (do_wake)
2374 wake_up_all(&h->event_sync_wait_queue);
Webb Scalesa58e7e52015-04-23 09:34:16 -05002375}
2376
Webb Scales73153fe2015-04-23 09:35:04 -05002377static void hpsa_cmd_resolve_and_free(struct ctlr_info *h,
2378 struct CommandList *c)
2379{
2380 hpsa_cmd_resolve_events(h, c);
2381 cmd_tagged_free(h, c);
2382}
2383
Webb Scales8a0ff922015-04-23 09:34:11 -05002384static void hpsa_cmd_free_and_done(struct ctlr_info *h,
2385 struct CommandList *c, struct scsi_cmnd *cmd)
2386{
Webb Scales73153fe2015-04-23 09:35:04 -05002387 hpsa_cmd_resolve_and_free(h, c);
Don Braced49c2072016-09-09 16:30:23 -05002388 if (cmd && cmd->scsi_done)
2389 cmd->scsi_done(cmd);
Webb Scales8a0ff922015-04-23 09:34:11 -05002390}
2391
2392static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
2393{
2394 INIT_WORK(&c->work, hpsa_command_resubmit_worker);
2395 queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
2396}
2397
Scott Teelc3497752014-02-18 13:56:34 -06002398static void process_ioaccel2_completion(struct ctlr_info *h,
2399 struct CommandList *c, struct scsi_cmnd *cmd,
2400 struct hpsa_scsi_dev_t *dev)
2401{
2402 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2403
2404 /* check for good status */
2405 if (likely(c2->error_data.serv_response == 0 &&
Webb Scales8a0ff922015-04-23 09:34:11 -05002406 c2->error_data.status == 0))
2407 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002408
Webb Scales8a0ff922015-04-23 09:34:11 -05002409 /*
2410 * Any RAID offload error results in retry which will use
Scott Teelc3497752014-02-18 13:56:34 -06002411 * the normal I/O path so the controller can handle whatever's
2412 * wrong.
2413 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002414 if (is_logical_device(dev) &&
Scott Teelc3497752014-02-18 13:56:34 -06002415 c2->error_data.serv_response ==
2416 IOACCEL2_SERV_RESPONSE_FAILURE) {
Don Brace080ef1c2015-01-23 16:43:25 -06002417 if (c2->error_data.status ==
Don Brace064d1b12016-04-27 17:14:07 -05002418 IOACCEL2_STATUS_SR_IOACCEL_DISABLED) {
Don Brace080ef1c2015-01-23 16:43:25 -06002419 dev->offload_enabled = 0;
Don Brace064d1b12016-04-27 17:14:07 -05002420 dev->offload_to_be_enabled = 0;
2421 }
Webb Scales8a0ff922015-04-23 09:34:11 -05002422
2423 return hpsa_retry_cmd(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06002424 }
Don Brace080ef1c2015-01-23 16:43:25 -06002425
Don Braceba74fdc2016-04-27 17:14:17 -05002426 if (handle_ioaccel_mode2_error(h, c, cmd, c2, dev))
Webb Scales8a0ff922015-04-23 09:34:11 -05002427 return hpsa_retry_cmd(h, c);
Don Brace080ef1c2015-01-23 16:43:25 -06002428
Webb Scales8a0ff922015-04-23 09:34:11 -05002429 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002430}
2431
Stephen Cameron9437ac42015-04-23 09:32:16 -05002432/* Returns 0 on success, < 0 otherwise. */
2433static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
2434 struct CommandList *cp)
2435{
2436 u8 tmf_status = cp->err_info->ScsiStatus;
2437
2438 switch (tmf_status) {
2439 case CISS_TMF_COMPLETE:
2440 /*
2441 * CISS_TMF_COMPLETE never happens, instead,
2442 * ei->CommandStatus == 0 for this case.
2443 */
2444 case CISS_TMF_SUCCESS:
2445 return 0;
2446 case CISS_TMF_INVALID_FRAME:
2447 case CISS_TMF_NOT_SUPPORTED:
2448 case CISS_TMF_FAILED:
2449 case CISS_TMF_WRONG_LUN:
2450 case CISS_TMF_OVERLAPPED_TAG:
2451 break;
2452 default:
2453 dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
2454 tmf_status);
2455 break;
2456 }
2457 return -tmf_status;
2458}
2459
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05002460static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002461{
2462 struct scsi_cmnd *cmd;
2463 struct ctlr_info *h;
2464 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002465 struct hpsa_scsi_dev_t *dev;
Webb Scalesd9a729f2015-04-23 09:33:27 -05002466 struct io_accel2_cmd *c2;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002467
Stephen Cameron9437ac42015-04-23 09:32:16 -05002468 u8 sense_key;
2469 u8 asc; /* additional sense code */
2470 u8 ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05002471 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002472
2473 ei = cp->err_info;
Stephen Cameron7fa30302015-01-23 16:44:30 -06002474 cmd = cp->scsi_cmd;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002475 h = cp->h;
Don Braced49c2072016-09-09 16:30:23 -05002476
2477 if (!cmd->device) {
2478 cmd->result = DID_NO_CONNECT << 16;
2479 return hpsa_cmd_free_and_done(h, cp, cmd);
2480 }
2481
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002482 dev = cmd->device->hostdata;
Don Brace45e596c2016-09-09 16:30:42 -05002483 if (!dev) {
2484 cmd->result = DID_NO_CONNECT << 16;
2485 return hpsa_cmd_free_and_done(h, cp, cmd);
2486 }
Webb Scalesd9a729f2015-04-23 09:33:27 -05002487 c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002488
2489 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06002490 if ((cp->cmd_type == CMD_SCSI) &&
Don Brace2b08b3e2015-01-23 16:41:09 -06002491 (le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002492 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002493
Webb Scalesd9a729f2015-04-23 09:33:27 -05002494 if ((cp->cmd_type == CMD_IOACCEL2) &&
2495 (c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2496 hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2497
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002498 cmd->result = (DID_OK << 16); /* host byte */
2499 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06002500
Don Braced49c2072016-09-09 16:30:23 -05002501 if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1) {
2502 if (dev->physical_device && dev->expose_device &&
2503 dev->removed) {
2504 cmd->result = DID_NO_CONNECT << 16;
2505 return hpsa_cmd_free_and_done(h, cp, cmd);
2506 }
2507 if (likely(cp->phys_disk != NULL))
2508 atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
2509 }
Don Brace03383732015-01-23 16:43:30 -06002510
Webb Scales25163bd2015-04-23 09:32:00 -05002511 /*
2512 * We check for lockup status here as it may be set for
2513 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
2514 * fail_all_oustanding_cmds()
2515 */
2516 if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
2517 /* DID_NO_CONNECT will prevent a retry */
2518 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05002519 return hpsa_cmd_free_and_done(h, cp, cmd);
Webb Scales25163bd2015-04-23 09:32:00 -05002520 }
2521
Don Brace08ec46f2017-05-04 17:51:49 -05002522 if ((unlikely(hpsa_is_pending_event(cp))))
Webb Scalesd604f532015-04-23 09:35:22 -05002523 if (cp->reset_pending)
Don Bracebfd75462016-11-15 14:45:32 -06002524 return hpsa_cmd_free_and_done(h, cp, cmd);
Webb Scalesd604f532015-04-23 09:35:22 -05002525
Scott Teelc3497752014-02-18 13:56:34 -06002526 if (cp->cmd_type == CMD_IOACCEL2)
2527 return process_ioaccel2_completion(h, cp, cmd, dev);
2528
Robert Elliott6aa4c362014-07-03 10:18:19 -05002529 scsi_set_resid(cmd, ei->ResidualCnt);
Webb Scales8a0ff922015-04-23 09:34:11 -05002530 if (ei->CommandStatus == 0)
2531 return hpsa_cmd_free_and_done(h, cp, cmd);
Robert Elliott6aa4c362014-07-03 10:18:19 -05002532
Matt Gatese1f7de02014-02-18 13:55:17 -06002533 /* For I/O accelerator commands, copy over some fields to the normal
2534 * CISS header used below for error handling.
2535 */
2536 if (cp->cmd_type == CMD_IOACCEL1) {
2537 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06002538 cp->Header.SGList = scsi_sg_count(cmd);
2539 cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
2540 cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
2541 IOACCEL1_IOFLAGS_CDBLEN_MASK;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002542 cp->Header.tag = c->tag;
Matt Gatese1f7de02014-02-18 13:55:17 -06002543 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2544 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002545
2546 /* Any RAID offload error results in retry which will use
2547 * the normal I/O path so the controller can handle whatever's
2548 * wrong.
2549 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002550 if (is_logical_device(dev)) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002551 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2552 dev->offload_enabled = 0;
Webb Scalesd604f532015-04-23 09:35:22 -05002553 return hpsa_retry_cmd(h, cp);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002554 }
Matt Gatese1f7de02014-02-18 13:55:17 -06002555 }
2556
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002557 /* an error has occurred */
2558 switch (ei->CommandStatus) {
2559
2560 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002561 cmd->result |= ei->ScsiStatus;
2562 /* copy the sense data */
2563 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
2564 sense_data_size = SCSI_SENSE_BUFFERSIZE;
2565 else
2566 sense_data_size = sizeof(ei->SenseInfo);
2567 if (ei->SenseLen < sense_data_size)
2568 sense_data_size = ei->SenseLen;
2569 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
2570 if (ei->ScsiStatus)
2571 decode_sense_data(ei->SenseInfo, sense_data_size,
2572 &sense_key, &asc, &ascq);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002573 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates1d3b3602010-02-04 08:43:00 -06002574 if (sense_key == ABORTED_COMMAND) {
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05002575 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06002576 break;
2577 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002578 break;
2579 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002580 /* Problem was not a check condition
2581 * Pass it up to the upper layers...
2582 */
2583 if (ei->ScsiStatus) {
2584 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2585 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2586 "Returning result: 0x%x\n",
2587 cp, ei->ScsiStatus,
2588 sense_key, asc, ascq,
2589 cmd->result);
2590 } else { /* scsi status is zero??? How??? */
2591 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2592 "Returning no connection.\n", cp),
2593
2594 /* Ordinarily, this case should never happen,
2595 * but there is a bug in some released firmware
2596 * revisions that allows it to happen if, for
2597 * example, a 4100 backplane loses power and
2598 * the tape drive is in it. We assume that
2599 * it's a fatal error of some kind because we
2600 * can't show that it wasn't. We will make it
2601 * look like selection timeout since that is
2602 * the most common reason for this to occur,
2603 * and it's severe enough.
2604 */
2605
2606 cmd->result = DID_NO_CONNECT << 16;
2607 }
2608 break;
2609
2610 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2611 break;
2612 case CMD_DATA_OVERRUN:
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002613 dev_warn(&h->pdev->dev,
2614 "CDB %16phN data overrun\n", cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002615 break;
2616 case CMD_INVALID: {
2617 /* print_bytes(cp, sizeof(*cp), 1, 0);
2618 print_cmd(cp); */
2619 /* We get CMD_INVALID if you address a non-existent device
2620 * instead of a selection timeout (no response). You will
2621 * see this if you yank out a drive, then try to access it.
2622 * This is kind of a shame because it means that any other
2623 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2624 * missing target. */
2625 cmd->result = DID_NO_CONNECT << 16;
2626 }
2627 break;
2628 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05002629 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002630 dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2631 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002632 break;
2633 case CMD_HARDWARE_ERR:
2634 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002635 dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2636 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002637 break;
2638 case CMD_CONNECTION_LOST:
2639 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002640 dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2641 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002642 break;
2643 case CMD_ABORTED:
Don Brace08ec46f2017-05-04 17:51:49 -05002644 cmd->result = DID_ABORT << 16;
2645 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002646 case CMD_ABORT_FAILED:
2647 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002648 dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2649 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002650 break;
2651 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05002652 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002653 dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2654 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002655 break;
2656 case CMD_TIMEOUT:
2657 cmd->result = DID_TIME_OUT << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002658 dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2659 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002660 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002661 case CMD_UNABORTABLE:
2662 cmd->result = DID_ERROR << 16;
2663 dev_warn(&h->pdev->dev, "Command unabortable\n");
2664 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002665 case CMD_TMF_STATUS:
2666 if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
2667 cmd->result = DID_ERROR << 16;
2668 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002669 case CMD_IOACCEL_DISABLED:
2670 /* This only handles the direct pass-through case since RAID
2671 * offload is handled above. Just attempt a retry.
2672 */
2673 cmd->result = DID_SOFT_ERROR << 16;
2674 dev_warn(&h->pdev->dev,
2675 "cp %p had HP SSD Smart Path error\n", cp);
2676 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002677 default:
2678 cmd->result = DID_ERROR << 16;
2679 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2680 cp, ei->CommandStatus);
2681 }
Webb Scales8a0ff922015-04-23 09:34:11 -05002682
2683 return hpsa_cmd_free_and_done(h, cp, cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002684}
2685
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002686static void hpsa_pci_unmap(struct pci_dev *pdev,
2687 struct CommandList *c, int sg_used, int data_direction)
2688{
2689 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002690
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002691 for (i = 0; i < sg_used; i++)
2692 pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
2693 le32_to_cpu(c->SG[i].Len),
2694 data_direction);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002695}
2696
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002697static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002698 struct CommandList *cp,
2699 unsigned char *buf,
2700 size_t buflen,
2701 int data_direction)
2702{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002703 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002704
2705 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2706 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002707 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002708 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002709 }
2710
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002711 addr64 = pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06002712 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002713 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06002714 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002715 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002716 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06002717 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002718 cp->SG[0].Addr = cpu_to_le64(addr64);
2719 cp->SG[0].Len = cpu_to_le32(buflen);
2720 cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
2721 cp->Header.SGList = 1; /* no. SGs contig in this cmd */
2722 cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002723 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002724}
2725
Webb Scales25163bd2015-04-23 09:32:00 -05002726#define NO_TIMEOUT ((unsigned long) -1)
2727#define DEFAULT_TIMEOUT 30000 /* milliseconds */
2728static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
2729 struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002730{
2731 DECLARE_COMPLETION_ONSTACK(wait);
2732
2733 c->waiting = &wait;
Webb Scales25163bd2015-04-23 09:32:00 -05002734 __enqueue_cmd_and_start_io(h, c, reply_queue);
2735 if (timeout_msecs == NO_TIMEOUT) {
2736 /* TODO: get rid of this no-timeout thing */
2737 wait_for_completion_io(&wait);
2738 return IO_OK;
2739 }
2740 if (!wait_for_completion_io_timeout(&wait,
2741 msecs_to_jiffies(timeout_msecs))) {
2742 dev_warn(&h->pdev->dev, "Command timed out.\n");
2743 return -ETIMEDOUT;
2744 }
2745 return IO_OK;
2746}
2747
2748static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
2749 int reply_queue, unsigned long timeout_msecs)
2750{
2751 if (unlikely(lockup_detected(h))) {
2752 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
2753 return IO_OK;
2754 }
2755 return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002756}
2757
Stephen M. Cameron094963d2014-05-29 10:53:18 -05002758static u32 lockup_detected(struct ctlr_info *h)
2759{
2760 int cpu;
2761 u32 rc, *lockup_detected;
2762
2763 cpu = get_cpu();
2764 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2765 rc = *lockup_detected;
2766 put_cpu();
2767 return rc;
2768}
2769
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002770#define MAX_DRIVER_CMD_RETRIES 25
Webb Scales25163bd2015-04-23 09:32:00 -05002771static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
2772 struct CommandList *c, int data_direction, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002773{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002774 int backoff_time = 10, retry_count = 0;
Webb Scales25163bd2015-04-23 09:32:00 -05002775 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002776
2777 do {
Joe Perches7630abd2011-05-08 23:32:40 -07002778 memset(c->err_info, 0, sizeof(*c->err_info));
Webb Scales25163bd2015-04-23 09:32:00 -05002779 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
2780 timeout_msecs);
2781 if (rc)
2782 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002783 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002784 if (retry_count > 3) {
2785 msleep(backoff_time);
2786 if (backoff_time < 1000)
2787 backoff_time *= 2;
2788 }
Matt Bondurant852af202012-05-01 11:42:35 -05002789 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002790 check_for_busy(h, c)) &&
2791 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002792 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
Webb Scales25163bd2015-04-23 09:32:00 -05002793 if (retry_count > MAX_DRIVER_CMD_RETRIES)
2794 rc = -EIO;
2795 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002796}
2797
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002798static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2799 struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002800{
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002801 const u8 *cdb = c->Request.CDB;
2802 const u8 *lun = c->Header.LUN.LunAddrBytes;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002803
Rasmus Villemoes609a70d2016-11-30 23:35:47 +01002804 dev_warn(&h->pdev->dev, "%s: LUN:%8phN CDB:%16phN\n",
2805 txt, lun, cdb);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002806}
2807
2808static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2809 struct CommandList *cp)
2810{
2811 const struct ErrorInfo *ei = cp->err_info;
2812 struct device *d = &cp->h->pdev->dev;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002813 u8 sense_key, asc, ascq;
2814 int sense_len;
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002815
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002816 switch (ei->CommandStatus) {
2817 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002818 if (ei->SenseLen > sizeof(ei->SenseInfo))
2819 sense_len = sizeof(ei->SenseInfo);
2820 else
2821 sense_len = ei->SenseLen;
2822 decode_sense_data(ei->SenseInfo, sense_len,
2823 &sense_key, &asc, &ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002824 hpsa_print_cmd(h, "SCSI status", cp);
2825 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
Stephen Cameron9437ac42015-04-23 09:32:16 -05002826 dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
2827 sense_key, asc, ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002828 else
Stephen Cameron9437ac42015-04-23 09:32:16 -05002829 dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002830 if (ei->ScsiStatus == 0)
2831 dev_warn(d, "SCSI status is abnormally zero. "
2832 "(probably indicates selection timeout "
2833 "reported incorrectly due to a known "
2834 "firmware bug, circa July, 2001.)\n");
2835 break;
2836 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002837 break;
2838 case CMD_DATA_OVERRUN:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002839 hpsa_print_cmd(h, "overrun condition", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002840 break;
2841 case CMD_INVALID: {
2842 /* controller unfortunately reports SCSI passthru's
2843 * to non-existent targets as invalid commands.
2844 */
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002845 hpsa_print_cmd(h, "invalid command", cp);
2846 dev_warn(d, "probably means device no longer present\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002847 }
2848 break;
2849 case CMD_PROTOCOL_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002850 hpsa_print_cmd(h, "protocol error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002851 break;
2852 case CMD_HARDWARE_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002853 hpsa_print_cmd(h, "hardware error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002854 break;
2855 case CMD_CONNECTION_LOST:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002856 hpsa_print_cmd(h, "connection lost", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002857 break;
2858 case CMD_ABORTED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002859 hpsa_print_cmd(h, "aborted", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002860 break;
2861 case CMD_ABORT_FAILED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002862 hpsa_print_cmd(h, "abort failed", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002863 break;
2864 case CMD_UNSOLICITED_ABORT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002865 hpsa_print_cmd(h, "unsolicited abort", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002866 break;
2867 case CMD_TIMEOUT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002868 hpsa_print_cmd(h, "timed out", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002869 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002870 case CMD_UNABORTABLE:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002871 hpsa_print_cmd(h, "unabortable", cp);
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002872 break;
Webb Scales25163bd2015-04-23 09:32:00 -05002873 case CMD_CTLR_LOCKUP:
2874 hpsa_print_cmd(h, "controller lockup detected", cp);
2875 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002876 default:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002877 hpsa_print_cmd(h, "unknown status", cp);
2878 dev_warn(d, "Unknown command status %x\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002879 ei->CommandStatus);
2880 }
2881}
2882
2883static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002884 u16 page, unsigned char *buf,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002885 unsigned char bufsize)
2886{
2887 int rc = IO_OK;
2888 struct CommandList *c;
2889 struct ErrorInfo *ei;
2890
Stephen Cameron45fcb862015-01-23 16:43:04 -06002891 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002892
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002893 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2894 page, scsi3addr, TYPE_CMD)) {
2895 rc = -1;
2896 goto out;
2897 }
Webb Scales25163bd2015-04-23 09:32:00 -05002898 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05002899 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05002900 if (rc)
2901 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002902 ei = c->err_info;
2903 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002904 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002905 rc = -1;
2906 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002907out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002908 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002909 return rc;
2910}
2911
Scott Teelbf711ac2014-02-18 13:56:39 -06002912static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Webb Scales25163bd2015-04-23 09:32:00 -05002913 u8 reset_type, int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002914{
2915 int rc = IO_OK;
2916 struct CommandList *c;
2917 struct ErrorInfo *ei;
2918
Stephen Cameron45fcb862015-01-23 16:43:04 -06002919 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002920
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002921
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002922 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teel0b9b7b62015-11-04 15:51:02 -06002923 (void) fill_cmd(c, reset_type, h, NULL, 0, 0,
Scott Teelbf711ac2014-02-18 13:56:39 -06002924 scsi3addr, TYPE_MSG);
Don Brace2ef28842017-03-10 14:35:23 -06002925 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05002926 if (rc) {
2927 dev_warn(&h->pdev->dev, "Failed to send reset command\n");
2928 goto out;
2929 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002930 /* no unmap needed here because no data xfer. */
2931
2932 ei = c->err_info;
2933 if (ei->CommandStatus != 0) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002934 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002935 rc = -1;
2936 }
Webb Scales25163bd2015-04-23 09:32:00 -05002937out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002938 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002939 return rc;
2940}
2941
Webb Scalesd604f532015-04-23 09:35:22 -05002942static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
2943 struct hpsa_scsi_dev_t *dev,
2944 unsigned char *scsi3addr)
2945{
2946 int i;
2947 bool match = false;
2948 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2949 struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
2950
2951 if (hpsa_is_cmd_idle(c))
2952 return false;
2953
2954 switch (c->cmd_type) {
2955 case CMD_SCSI:
2956 case CMD_IOCTL_PEND:
2957 match = !memcmp(scsi3addr, &c->Header.LUN.LunAddrBytes,
2958 sizeof(c->Header.LUN.LunAddrBytes));
2959 break;
2960
2961 case CMD_IOACCEL1:
2962 case CMD_IOACCEL2:
2963 if (c->phys_disk == dev) {
2964 /* HBA mode match */
2965 match = true;
2966 } else {
2967 /* Possible RAID mode -- check each phys dev. */
2968 /* FIXME: Do we need to take out a lock here? If
2969 * so, we could just call hpsa_get_pdisk_of_ioaccel2()
2970 * instead. */
2971 for (i = 0; i < dev->nphysical_disks && !match; i++) {
2972 /* FIXME: an alternate test might be
2973 *
2974 * match = dev->phys_disk[i]->ioaccel_handle
2975 * == c2->scsi_nexus; */
2976 match = dev->phys_disk[i] == c->phys_disk;
2977 }
2978 }
2979 break;
2980
2981 case IOACCEL2_TMF:
2982 for (i = 0; i < dev->nphysical_disks && !match; i++) {
2983 match = dev->phys_disk[i]->ioaccel_handle ==
2984 le32_to_cpu(ac->it_nexus);
2985 }
2986 break;
2987
2988 case 0: /* The command is in the middle of being initialized. */
2989 match = false;
2990 break;
2991
2992 default:
2993 dev_err(&h->pdev->dev, "unexpected cmd_type: %d\n",
2994 c->cmd_type);
2995 BUG();
2996 }
2997
2998 return match;
2999}
3000
3001static int hpsa_do_reset(struct ctlr_info *h, struct hpsa_scsi_dev_t *dev,
3002 unsigned char *scsi3addr, u8 reset_type, int reply_queue)
3003{
3004 int i;
3005 int rc = 0;
3006
3007 /* We can really only handle one reset at a time */
3008 if (mutex_lock_interruptible(&h->reset_mutex) == -EINTR) {
3009 dev_warn(&h->pdev->dev, "concurrent reset wait interrupted.\n");
3010 return -EINTR;
3011 }
3012
3013 BUG_ON(atomic_read(&dev->reset_cmds_out) != 0);
3014
3015 for (i = 0; i < h->nr_cmds; i++) {
3016 struct CommandList *c = h->cmd_pool + i;
3017 int refcount = atomic_inc_return(&c->refcount);
3018
3019 if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev, scsi3addr)) {
3020 unsigned long flags;
3021
3022 /*
3023 * Mark the target command as having a reset pending,
3024 * then lock a lock so that the command cannot complete
3025 * while we're considering it. If the command is not
3026 * idle then count it; otherwise revoke the event.
3027 */
3028 c->reset_pending = dev;
3029 spin_lock_irqsave(&h->lock, flags); /* Implied MB */
3030 if (!hpsa_is_cmd_idle(c))
3031 atomic_inc(&dev->reset_cmds_out);
3032 else
3033 c->reset_pending = NULL;
3034 spin_unlock_irqrestore(&h->lock, flags);
3035 }
3036
3037 cmd_free(h, c);
3038 }
3039
3040 rc = hpsa_send_reset(h, scsi3addr, reset_type, reply_queue);
3041 if (!rc)
3042 wait_event(h->event_sync_wait_queue,
3043 atomic_read(&dev->reset_cmds_out) == 0 ||
3044 lockup_detected(h));
3045
3046 if (unlikely(lockup_detected(h))) {
Don Brace77678d32015-07-18 11:12:22 -05003047 dev_warn(&h->pdev->dev,
3048 "Controller lockup detected during reset wait\n");
3049 rc = -ENODEV;
3050 }
Webb Scalesd604f532015-04-23 09:35:22 -05003051
3052 if (unlikely(rc))
3053 atomic_set(&dev->reset_cmds_out, 0);
Don Bracebfd75462016-11-15 14:45:32 -06003054 else
Don Brace8516a2d2017-05-04 17:50:58 -05003055 rc = wait_for_device_to_become_ready(h, scsi3addr, 0);
Webb Scalesd604f532015-04-23 09:35:22 -05003056
3057 mutex_unlock(&h->reset_mutex);
3058 return rc;
3059}
3060
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003061static void hpsa_get_raid_level(struct ctlr_info *h,
3062 unsigned char *scsi3addr, unsigned char *raid_level)
3063{
3064 int rc;
3065 unsigned char *buf;
3066
3067 *raid_level = RAID_UNKNOWN;
3068 buf = kzalloc(64, GFP_KERNEL);
3069 if (!buf)
3070 return;
Scott Teel83832782016-09-09 16:30:29 -05003071
3072 if (!hpsa_vpd_page_supported(h, scsi3addr,
3073 HPSA_VPD_LV_DEVICE_GEOMETRY))
3074 goto exit;
3075
3076 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE |
3077 HPSA_VPD_LV_DEVICE_GEOMETRY, buf, 64);
3078
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003079 if (rc == 0)
3080 *raid_level = buf[8];
3081 if (*raid_level > RAID_UNKNOWN)
3082 *raid_level = RAID_UNKNOWN;
Scott Teel83832782016-09-09 16:30:29 -05003083exit:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003084 kfree(buf);
3085 return;
3086}
3087
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003088#define HPSA_MAP_DEBUG
3089#ifdef HPSA_MAP_DEBUG
3090static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
3091 struct raid_map_data *map_buff)
3092{
3093 struct raid_map_disk_data *dd = &map_buff->data[0];
3094 int map, row, col;
3095 u16 map_cnt, row_cnt, disks_per_row;
3096
3097 if (rc != 0)
3098 return;
3099
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06003100 /* Show details only if debugging has been activated. */
3101 if (h->raid_offload_debug < 2)
3102 return;
3103
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003104 dev_info(&h->pdev->dev, "structure_size = %u\n",
3105 le32_to_cpu(map_buff->structure_size));
3106 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
3107 le32_to_cpu(map_buff->volume_blk_size));
3108 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
3109 le64_to_cpu(map_buff->volume_blk_cnt));
3110 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
3111 map_buff->phys_blk_shift);
3112 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
3113 map_buff->parity_rotation_shift);
3114 dev_info(&h->pdev->dev, "strip_size = %u\n",
3115 le16_to_cpu(map_buff->strip_size));
3116 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
3117 le64_to_cpu(map_buff->disk_starting_blk));
3118 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
3119 le64_to_cpu(map_buff->disk_blk_cnt));
3120 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
3121 le16_to_cpu(map_buff->data_disks_per_row));
3122 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
3123 le16_to_cpu(map_buff->metadata_disks_per_row));
3124 dev_info(&h->pdev->dev, "row_cnt = %u\n",
3125 le16_to_cpu(map_buff->row_cnt));
3126 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
3127 le16_to_cpu(map_buff->layout_map_count));
Don Brace2b08b3e2015-01-23 16:41:09 -06003128 dev_info(&h->pdev->dev, "flags = 0x%x\n",
Scott Teeldd0e19f2014-02-18 13:57:31 -06003129 le16_to_cpu(map_buff->flags));
Don Brace2b08b3e2015-01-23 16:41:09 -06003130 dev_info(&h->pdev->dev, "encrypytion = %s\n",
3131 le16_to_cpu(map_buff->flags) &
3132 RAID_MAP_FLAG_ENCRYPT_ON ? "ON" : "OFF");
Scott Teeldd0e19f2014-02-18 13:57:31 -06003133 dev_info(&h->pdev->dev, "dekindex = %u\n",
3134 le16_to_cpu(map_buff->dekindex));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003135 map_cnt = le16_to_cpu(map_buff->layout_map_count);
3136 for (map = 0; map < map_cnt; map++) {
3137 dev_info(&h->pdev->dev, "Map%u:\n", map);
3138 row_cnt = le16_to_cpu(map_buff->row_cnt);
3139 for (row = 0; row < row_cnt; row++) {
3140 dev_info(&h->pdev->dev, " Row%u:\n", row);
3141 disks_per_row =
3142 le16_to_cpu(map_buff->data_disks_per_row);
3143 for (col = 0; col < disks_per_row; col++, dd++)
3144 dev_info(&h->pdev->dev,
3145 " D%02u: h=0x%04x xor=%u,%u\n",
3146 col, dd->ioaccel_handle,
3147 dd->xor_mult[0], dd->xor_mult[1]);
3148 disks_per_row =
3149 le16_to_cpu(map_buff->metadata_disks_per_row);
3150 for (col = 0; col < disks_per_row; col++, dd++)
3151 dev_info(&h->pdev->dev,
3152 " M%02u: h=0x%04x xor=%u,%u\n",
3153 col, dd->ioaccel_handle,
3154 dd->xor_mult[0], dd->xor_mult[1]);
3155 }
3156 }
3157}
3158#else
3159static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
3160 __attribute__((unused)) int rc,
3161 __attribute__((unused)) struct raid_map_data *map_buff)
3162{
3163}
3164#endif
3165
3166static int hpsa_get_raid_map(struct ctlr_info *h,
3167 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3168{
3169 int rc = 0;
3170 struct CommandList *c;
3171 struct ErrorInfo *ei;
3172
Stephen Cameron45fcb862015-01-23 16:43:04 -06003173 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003174
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003175 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
3176 sizeof(this_device->raid_map), 0,
3177 scsi3addr, TYPE_CMD)) {
Robert Elliott2dd02d72015-04-23 09:33:43 -05003178 dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
3179 cmd_free(h, c);
3180 return -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003181 }
Webb Scales25163bd2015-04-23 09:32:00 -05003182 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003183 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003184 if (rc)
3185 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003186 ei = c->err_info;
3187 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003188 hpsa_scsi_interpret_error(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05003189 rc = -1;
3190 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003191 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06003192 cmd_free(h, c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003193
3194 /* @todo in the future, dynamically allocate RAID map memory */
3195 if (le32_to_cpu(this_device->raid_map.structure_size) >
3196 sizeof(this_device->raid_map)) {
3197 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
3198 rc = -1;
3199 }
3200 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
3201 return rc;
Webb Scales25163bd2015-04-23 09:32:00 -05003202out:
3203 cmd_free(h, c);
3204 return rc;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003205}
3206
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003207static int hpsa_bmic_sense_subsystem_information(struct ctlr_info *h,
3208 unsigned char scsi3addr[], u16 bmic_device_index,
3209 struct bmic_sense_subsystem_info *buf, size_t bufsize)
3210{
3211 int rc = IO_OK;
3212 struct CommandList *c;
3213 struct ErrorInfo *ei;
3214
3215 c = cmd_alloc(h);
3216
3217 rc = fill_cmd(c, BMIC_SENSE_SUBSYSTEM_INFORMATION, h, buf, bufsize,
3218 0, RAID_CTLR_LUNID, TYPE_CMD);
3219 if (rc)
3220 goto out;
3221
3222 c->Request.CDB[2] = bmic_device_index & 0xff;
3223 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
3224
3225 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003226 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003227 if (rc)
3228 goto out;
3229 ei = c->err_info;
3230 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3231 hpsa_scsi_interpret_error(h, c);
3232 rc = -1;
3233 }
3234out:
3235 cmd_free(h, c);
3236 return rc;
3237}
3238
Scott Teel66749d02015-11-04 15:51:57 -06003239static int hpsa_bmic_id_controller(struct ctlr_info *h,
3240 struct bmic_identify_controller *buf, size_t bufsize)
3241{
3242 int rc = IO_OK;
3243 struct CommandList *c;
3244 struct ErrorInfo *ei;
3245
3246 c = cmd_alloc(h);
3247
3248 rc = fill_cmd(c, BMIC_IDENTIFY_CONTROLLER, h, buf, bufsize,
3249 0, RAID_CTLR_LUNID, TYPE_CMD);
3250 if (rc)
3251 goto out;
3252
3253 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003254 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teel66749d02015-11-04 15:51:57 -06003255 if (rc)
3256 goto out;
3257 ei = c->err_info;
3258 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3259 hpsa_scsi_interpret_error(h, c);
3260 rc = -1;
3261 }
3262out:
3263 cmd_free(h, c);
3264 return rc;
3265}
3266
Don Brace03383732015-01-23 16:43:30 -06003267static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
3268 unsigned char scsi3addr[], u16 bmic_device_index,
3269 struct bmic_identify_physical_device *buf, size_t bufsize)
3270{
3271 int rc = IO_OK;
3272 struct CommandList *c;
3273 struct ErrorInfo *ei;
3274
3275 c = cmd_alloc(h);
3276 rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
3277 0, RAID_CTLR_LUNID, TYPE_CMD);
3278 if (rc)
3279 goto out;
3280
3281 c->Request.CDB[2] = bmic_device_index & 0xff;
3282 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
3283
Webb Scales25163bd2015-04-23 09:32:00 -05003284 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
Don Bracec448ecf2016-04-27 17:13:51 -05003285 DEFAULT_TIMEOUT);
Don Brace03383732015-01-23 16:43:30 -06003286 ei = c->err_info;
3287 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3288 hpsa_scsi_interpret_error(h, c);
3289 rc = -1;
3290 }
3291out:
3292 cmd_free(h, c);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003293
Don Brace03383732015-01-23 16:43:30 -06003294 return rc;
3295}
3296
Don Bracecca8f132015-12-22 10:36:48 -06003297/*
3298 * get enclosure information
3299 * struct ReportExtendedLUNdata *rlep - Used for BMIC drive number
3300 * struct hpsa_scsi_dev_t *encl_dev - device entry for enclosure
3301 * Uses id_physical_device to determine the box_index.
3302 */
3303static void hpsa_get_enclosure_info(struct ctlr_info *h,
3304 unsigned char *scsi3addr,
3305 struct ReportExtendedLUNdata *rlep, int rle_index,
3306 struct hpsa_scsi_dev_t *encl_dev)
3307{
3308 int rc = -1;
3309 struct CommandList *c = NULL;
3310 struct ErrorInfo *ei = NULL;
3311 struct bmic_sense_storage_box_params *bssbp = NULL;
3312 struct bmic_identify_physical_device *id_phys = NULL;
3313 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
3314 u16 bmic_device_index = 0;
3315
3316 bmic_device_index = GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]);
3317
Don Brace5ac517b2017-05-04 17:50:50 -05003318 if (encl_dev->target == -1 || encl_dev->lun == -1) {
3319 rc = IO_OK;
3320 goto out;
3321 }
3322
Don Brace17a9e542016-02-23 15:16:09 -06003323 if (bmic_device_index == 0xFF00 || MASKED_DEVICE(&rle->lunid[0])) {
3324 rc = IO_OK;
Don Bracecca8f132015-12-22 10:36:48 -06003325 goto out;
Don Brace17a9e542016-02-23 15:16:09 -06003326 }
Don Bracecca8f132015-12-22 10:36:48 -06003327
3328 bssbp = kzalloc(sizeof(*bssbp), GFP_KERNEL);
3329 if (!bssbp)
3330 goto out;
3331
3332 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3333 if (!id_phys)
3334 goto out;
3335
3336 rc = hpsa_bmic_id_physical_device(h, scsi3addr, bmic_device_index,
3337 id_phys, sizeof(*id_phys));
3338 if (rc) {
3339 dev_warn(&h->pdev->dev, "%s: id_phys failed %d bdi[0x%x]\n",
3340 __func__, encl_dev->external, bmic_device_index);
3341 goto out;
3342 }
3343
3344 c = cmd_alloc(h);
3345
3346 rc = fill_cmd(c, BMIC_SENSE_STORAGE_BOX_PARAMS, h, bssbp,
3347 sizeof(*bssbp), 0, RAID_CTLR_LUNID, TYPE_CMD);
3348
3349 if (rc)
3350 goto out;
3351
3352 if (id_phys->phys_connector[1] == 'E')
3353 c->Request.CDB[5] = id_phys->box_index;
3354 else
3355 c->Request.CDB[5] = 0;
3356
3357 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
Don Bracec448ecf2016-04-27 17:13:51 -05003358 DEFAULT_TIMEOUT);
Don Bracecca8f132015-12-22 10:36:48 -06003359 if (rc)
3360 goto out;
3361
3362 ei = c->err_info;
3363 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3364 rc = -1;
3365 goto out;
3366 }
3367
3368 encl_dev->box[id_phys->active_path_number] = bssbp->phys_box_on_port;
3369 memcpy(&encl_dev->phys_connector[id_phys->active_path_number],
3370 bssbp->phys_connector, sizeof(bssbp->phys_connector));
3371
3372 rc = IO_OK;
3373out:
3374 kfree(bssbp);
3375 kfree(id_phys);
3376
3377 if (c)
3378 cmd_free(h, c);
3379
3380 if (rc != IO_OK)
3381 hpsa_show_dev_msg(KERN_INFO, h, encl_dev,
3382 "Error, could not get enclosure information\n");
3383}
3384
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003385static u64 hpsa_get_sas_address_from_report_physical(struct ctlr_info *h,
3386 unsigned char *scsi3addr)
3387{
3388 struct ReportExtendedLUNdata *physdev;
3389 u32 nphysicals;
3390 u64 sa = 0;
3391 int i;
3392
3393 physdev = kzalloc(sizeof(*physdev), GFP_KERNEL);
3394 if (!physdev)
3395 return 0;
3396
3397 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3398 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3399 kfree(physdev);
3400 return 0;
3401 }
3402 nphysicals = get_unaligned_be32(physdev->LUNListLength) / 24;
3403
3404 for (i = 0; i < nphysicals; i++)
3405 if (!memcmp(&physdev->LUN[i].lunid[0], scsi3addr, 8)) {
3406 sa = get_unaligned_be64(&physdev->LUN[i].wwid[0]);
3407 break;
3408 }
3409
3410 kfree(physdev);
3411
3412 return sa;
3413}
3414
3415static void hpsa_get_sas_address(struct ctlr_info *h, unsigned char *scsi3addr,
3416 struct hpsa_scsi_dev_t *dev)
3417{
3418 int rc;
3419 u64 sa = 0;
3420
3421 if (is_hba_lunid(scsi3addr)) {
3422 struct bmic_sense_subsystem_info *ssi;
3423
3424 ssi = kzalloc(sizeof(*ssi), GFP_KERNEL);
Amit Kushwaha7e8a9482016-12-12 16:34:21 +05303425 if (!ssi)
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003426 return;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06003427
3428 rc = hpsa_bmic_sense_subsystem_information(h,
3429 scsi3addr, 0, ssi, sizeof(*ssi));
3430 if (rc == 0) {
3431 sa = get_unaligned_be64(ssi->primary_world_wide_id);
3432 h->sas_address = sa;
3433 }
3434
3435 kfree(ssi);
3436 } else
3437 sa = hpsa_get_sas_address_from_report_physical(h, scsi3addr);
3438
3439 dev->sas_address = sa;
3440}
3441
3442/* Get a device id from inquiry page 0x83 */
Scott Teel83832782016-09-09 16:30:29 -05003443static bool hpsa_vpd_page_supported(struct ctlr_info *h,
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003444 unsigned char scsi3addr[], u8 page)
3445{
3446 int rc;
3447 int i;
3448 int pages;
3449 unsigned char *buf, bufsize;
3450
3451 buf = kzalloc(256, GFP_KERNEL);
3452 if (!buf)
Scott Teel83832782016-09-09 16:30:29 -05003453 return false;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003454
3455 /* Get the size of the page list first */
3456 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3457 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3458 buf, HPSA_VPD_HEADER_SZ);
3459 if (rc != 0)
3460 goto exit_unsupported;
3461 pages = buf[3];
3462 if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
3463 bufsize = pages + HPSA_VPD_HEADER_SZ;
3464 else
3465 bufsize = 255;
3466
3467 /* Get the whole VPD page list */
3468 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3469 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3470 buf, bufsize);
3471 if (rc != 0)
3472 goto exit_unsupported;
3473
3474 pages = buf[3];
3475 for (i = 1; i <= pages; i++)
3476 if (buf[3 + i] == page)
3477 goto exit_supported;
3478exit_unsupported:
3479 kfree(buf);
Scott Teel83832782016-09-09 16:30:29 -05003480 return false;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003481exit_supported:
3482 kfree(buf);
Scott Teel83832782016-09-09 16:30:29 -05003483 return true;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003484}
3485
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003486static void hpsa_get_ioaccel_status(struct ctlr_info *h,
3487 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3488{
3489 int rc;
3490 unsigned char *buf;
3491 u8 ioaccel_status;
3492
3493 this_device->offload_config = 0;
3494 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003495 this_device->offload_to_be_enabled = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003496
3497 buf = kzalloc(64, GFP_KERNEL);
3498 if (!buf)
3499 return;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003500 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
3501 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003502 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003503 VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003504 if (rc != 0)
3505 goto out;
3506
3507#define IOACCEL_STATUS_BYTE 4
3508#define OFFLOAD_CONFIGURED_BIT 0x01
3509#define OFFLOAD_ENABLED_BIT 0x02
3510 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
3511 this_device->offload_config =
3512 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
3513 if (this_device->offload_config) {
3514 this_device->offload_enabled =
3515 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
3516 if (hpsa_get_raid_map(h, scsi3addr, this_device))
3517 this_device->offload_enabled = 0;
3518 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003519 this_device->offload_to_be_enabled = this_device->offload_enabled;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003520out:
3521 kfree(buf);
3522 return;
3523}
3524
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003525/* Get the device id from inquiry page 0x83 */
3526static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
Don Brace75d23d82015-11-04 15:51:39 -06003527 unsigned char *device_id, int index, int buflen)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003528{
3529 int rc;
3530 unsigned char *buf;
3531
Scott Teel83832782016-09-09 16:30:29 -05003532 /* Does controller have VPD for device id? */
3533 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_DEVICE_ID))
3534 return 1; /* not supported */
3535
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003536 buf = kzalloc(64, GFP_KERNEL);
3537 if (!buf)
Stephen M. Camerona84d7942014-05-29 10:54:20 -05003538 return -ENOMEM;
Scott Teel83832782016-09-09 16:30:29 -05003539
3540 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE |
3541 HPSA_VPD_LV_DEVICE_ID, buf, 64);
3542 if (rc == 0) {
3543 if (buflen > 16)
3544 buflen = 16;
3545 memcpy(device_id, &buf[8], buflen);
3546 }
Don Brace75d23d82015-11-04 15:51:39 -06003547
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003548 kfree(buf);
Don Brace75d23d82015-11-04 15:51:39 -06003549
Scott Teel83832782016-09-09 16:30:29 -05003550 return rc; /*0 - got id, otherwise, didn't */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003551}
3552
3553static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
Don Brace03383732015-01-23 16:43:30 -06003554 void *buf, int bufsize,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003555 int extended_response)
3556{
3557 int rc = IO_OK;
3558 struct CommandList *c;
3559 unsigned char scsi3addr[8];
3560 struct ErrorInfo *ei;
3561
Stephen Cameron45fcb862015-01-23 16:43:04 -06003562 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003563
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06003564 /* address the controller */
3565 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003566 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
3567 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
3568 rc = -1;
3569 goto out;
3570 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003571 if (extended_response)
3572 c->Request.CDB[1] = extended_response;
Webb Scales25163bd2015-04-23 09:32:00 -05003573 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05003574 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003575 if (rc)
3576 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003577 ei = c->err_info;
3578 if (ei->CommandStatus != 0 &&
3579 ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003580 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003581 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003582 } else {
Don Brace03383732015-01-23 16:43:30 -06003583 struct ReportLUNdata *rld = buf;
3584
3585 if (rld->extended_response_flag != extended_response) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003586 dev_err(&h->pdev->dev,
3587 "report luns requested format %u, got %u\n",
3588 extended_response,
Don Brace03383732015-01-23 16:43:30 -06003589 rld->extended_response_flag);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003590 rc = -1;
3591 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003592 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003593out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06003594 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003595 return rc;
3596}
3597
3598static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06003599 struct ReportExtendedLUNdata *buf, int bufsize)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003600{
Hannes Reinecke2a80d542016-12-02 11:36:13 +01003601 int rc;
3602 struct ReportLUNdata *lbuf;
3603
3604 rc = hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
3605 HPSA_REPORT_PHYS_EXTENDED);
3606 if (!rc || !hpsa_allow_any)
3607 return rc;
3608
3609 /* REPORT PHYS EXTENDED is not supported */
3610 lbuf = kzalloc(sizeof(*lbuf), GFP_KERNEL);
3611 if (!lbuf)
3612 return -ENOMEM;
3613
3614 rc = hpsa_scsi_do_report_luns(h, 0, lbuf, sizeof(*lbuf), 0);
3615 if (!rc) {
3616 int i;
3617 u32 nphys;
3618
3619 /* Copy ReportLUNdata header */
3620 memcpy(buf, lbuf, 8);
3621 nphys = be32_to_cpu(*((__be32 *)lbuf->LUNListLength)) / 8;
3622 for (i = 0; i < nphys; i++)
3623 memcpy(buf->LUN[i].lunid, lbuf->LUN[i], 8);
3624 }
3625 kfree(lbuf);
3626 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003627}
3628
3629static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
3630 struct ReportLUNdata *buf, int bufsize)
3631{
3632 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
3633}
3634
3635static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
3636 int bus, int target, int lun)
3637{
3638 device->bus = bus;
3639 device->target = target;
3640 device->lun = lun;
3641}
3642
Stephen M. Cameron98465902014-02-21 16:25:00 -06003643/* Use VPD inquiry to get details of volume status */
3644static int hpsa_get_volume_status(struct ctlr_info *h,
3645 unsigned char scsi3addr[])
3646{
3647 int rc;
3648 int status;
3649 int size;
3650 unsigned char *buf;
3651
3652 buf = kzalloc(64, GFP_KERNEL);
3653 if (!buf)
3654 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3655
3656 /* Does controller have VPD for logical volume status? */
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003657 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
Stephen M. Cameron98465902014-02-21 16:25:00 -06003658 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003659
3660 /* Get the size of the VPD return buffer */
3661 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3662 buf, HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003663 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003664 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003665 size = buf[3];
3666
3667 /* Now get the whole VPD buffer */
3668 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3669 buf, size + HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003670 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003671 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003672 status = buf[4]; /* status byte */
3673
3674 kfree(buf);
3675 return status;
3676exit_failed:
3677 kfree(buf);
3678 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3679}
3680
3681/* Determine offline status of a volume.
3682 * Return either:
3683 * 0 (not offline)
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003684 * 0xff (offline for unknown reasons)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003685 * # (integer code indicating one of several NOT READY states
3686 * describing why a volume is to be kept offline)
3687 */
Don Brace85b29002017-03-10 14:35:11 -06003688static unsigned char hpsa_volume_offline(struct ctlr_info *h,
Stephen M. Cameron98465902014-02-21 16:25:00 -06003689 unsigned char scsi3addr[])
3690{
3691 struct CommandList *c;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003692 unsigned char *sense;
3693 u8 sense_key, asc, ascq;
3694 int sense_len;
Webb Scales25163bd2015-04-23 09:32:00 -05003695 int rc, ldstat = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003696 u16 cmd_status;
3697 u8 scsi_status;
3698#define ASC_LUN_NOT_READY 0x04
3699#define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
3700#define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
3701
3702 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003703
Stephen M. Cameron98465902014-02-21 16:25:00 -06003704 (void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
Don Bracec448ecf2016-04-27 17:13:51 -05003705 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
3706 DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05003707 if (rc) {
3708 cmd_free(h, c);
Don Brace85b29002017-03-10 14:35:11 -06003709 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
Webb Scales25163bd2015-04-23 09:32:00 -05003710 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06003711 sense = c->err_info->SenseInfo;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003712 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3713 sense_len = sizeof(c->err_info->SenseInfo);
3714 else
3715 sense_len = c->err_info->SenseLen;
3716 decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
Stephen M. Cameron98465902014-02-21 16:25:00 -06003717 cmd_status = c->err_info->CommandStatus;
3718 scsi_status = c->err_info->ScsiStatus;
3719 cmd_free(h, c);
Stephen M. Cameron98465902014-02-21 16:25:00 -06003720
3721 /* Determine the reason for not ready state */
3722 ldstat = hpsa_get_volume_status(h, scsi3addr);
3723
3724 /* Keep volume offline in certain cases: */
3725 switch (ldstat) {
Don Brace85b29002017-03-10 14:35:11 -06003726 case HPSA_LV_FAILED:
Stephen M. Cameron98465902014-02-21 16:25:00 -06003727 case HPSA_LV_UNDERGOING_ERASE:
Scott Benesh5ca01202015-07-18 11:13:04 -05003728 case HPSA_LV_NOT_AVAILABLE:
Stephen M. Cameron98465902014-02-21 16:25:00 -06003729 case HPSA_LV_UNDERGOING_RPI:
3730 case HPSA_LV_PENDING_RPI:
3731 case HPSA_LV_ENCRYPTED_NO_KEY:
3732 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
3733 case HPSA_LV_UNDERGOING_ENCRYPTION:
3734 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
3735 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
3736 return ldstat;
3737 case HPSA_VPD_LV_STATUS_UNSUPPORTED:
3738 /* If VPD status page isn't available,
3739 * use ASC/ASCQ to determine state
3740 */
3741 if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
3742 (ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
3743 return ldstat;
3744 break;
3745 default:
3746 break;
3747 }
Don Brace85b29002017-03-10 14:35:11 -06003748 return HPSA_LV_OK;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003749}
3750
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003751static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003752 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
3753 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003754{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003755
3756#define OBDR_SIG_OFFSET 43
3757#define OBDR_TAPE_SIG "$DR-10"
3758#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
3759#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
3760
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003761 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003762 unsigned char *obdr_sig;
Don Brace683fc442015-11-04 15:50:44 -06003763 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003764
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003765 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Don Brace683fc442015-11-04 15:50:44 -06003766 if (!inq_buff) {
3767 rc = -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003768 goto bail_out;
Don Brace683fc442015-11-04 15:50:44 -06003769 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003770
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003771 /* Do an inquiry to the device to see what it is. */
3772 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3773 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003774 dev_err(&h->pdev->dev,
Don Brace85b29002017-03-10 14:35:11 -06003775 "%s: inquiry failed, device will be skipped.\n",
3776 __func__);
3777 rc = HPSA_INQUIRY_FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003778 goto bail_out;
3779 }
3780
Don Brace4af61e42016-02-23 15:16:40 -06003781 scsi_sanitize_inquiry_string(&inq_buff[8], 8);
3782 scsi_sanitize_inquiry_string(&inq_buff[16], 16);
Don Brace75d23d82015-11-04 15:51:39 -06003783
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003784 this_device->devtype = (inq_buff[0] & 0x1f);
3785 memcpy(this_device->scsi3addr, scsi3addr, 8);
3786 memcpy(this_device->vendor, &inq_buff[8],
3787 sizeof(this_device->vendor));
3788 memcpy(this_device->model, &inq_buff[16],
3789 sizeof(this_device->model));
Hannes Reinecke7630b3a2016-11-17 12:15:56 +01003790 this_device->rev = inq_buff[2];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003791 memset(this_device->device_id, 0,
3792 sizeof(this_device->device_id));
Scott Teel83832782016-09-09 16:30:29 -05003793 if (hpsa_get_device_id(h, scsi3addr, this_device->device_id, 8,
3794 sizeof(this_device->device_id)))
3795 dev_err(&h->pdev->dev,
3796 "hpsa%d: %s: can't get device id for host %d:C0:T%d:L%d\t%s\t%.16s\n",
3797 h->ctlr, __func__,
3798 h->scsi_host->host_no,
3799 this_device->target, this_device->lun,
3800 scsi_device_type(this_device->devtype),
3801 this_device->model);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003802
Don Braceaf15ed32016-02-23 15:16:15 -06003803 if ((this_device->devtype == TYPE_DISK ||
3804 this_device->devtype == TYPE_ZBC) &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003805 is_logical_dev_addr_mode(scsi3addr)) {
Don Brace85b29002017-03-10 14:35:11 -06003806 unsigned char volume_offline;
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003807
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003808 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003809 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3810 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003811 volume_offline = hpsa_volume_offline(h, scsi3addr);
Tomas Henzleb945882017-03-20 16:42:48 +01003812 this_device->volume_offline = volume_offline;
Don Brace85b29002017-03-10 14:35:11 -06003813 if (volume_offline == HPSA_LV_FAILED) {
3814 rc = HPSA_LV_FAILED;
3815 dev_err(&h->pdev->dev,
3816 "%s: LV failed, device will be skipped.\n",
3817 __func__);
3818 goto bail_out;
3819 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003820 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003821 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003822 this_device->offload_config = 0;
3823 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003824 this_device->offload_to_be_enabled = 0;
Joe Handzika3144e02015-04-23 09:32:59 -05003825 this_device->hba_ioaccel_enabled = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003826 this_device->volume_offline = 0;
Don Brace03383732015-01-23 16:43:30 -06003827 this_device->queue_depth = h->nr_cmds;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003828 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003829
Don Brace50864352017-05-04 17:51:28 -05003830 if (this_device->external)
3831 this_device->queue_depth = EXTERNAL_QD;
3832
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003833 if (is_OBDR_device) {
3834 /* See if this is a One-Button-Disaster-Recovery device
3835 * by looking for "$DR-10" at offset 43 in inquiry data.
3836 */
3837 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
3838 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
3839 strncmp(obdr_sig, OBDR_TAPE_SIG,
3840 OBDR_SIG_LEN) == 0);
3841 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003842 kfree(inq_buff);
3843 return 0;
3844
3845bail_out:
3846 kfree(inq_buff);
Don Brace683fc442015-11-04 15:50:44 -06003847 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003848}
3849
Kevin Barnettc7955052015-11-04 15:51:45 -06003850/*
3851 * Helper function to assign bus, target, lun mapping of devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003852 * Logical drive target and lun are assigned at this time, but
3853 * physical device lun and target assignment are deferred (assigned
3854 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
Kevin Barnettc7955052015-11-04 15:51:45 -06003855*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003856static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003857 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003858{
Kevin Barnettc7955052015-11-04 15:51:45 -06003859 u32 lunid = get_unaligned_le32(lunaddrbytes);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003860
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003861 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
3862 /* physical device, target and lun filled in later */
Hannes Reinecke7630b3a2016-11-17 12:15:56 +01003863 if (is_hba_lunid(lunaddrbytes)) {
3864 int bus = HPSA_HBA_BUS;
3865
3866 if (!device->rev)
3867 bus = HPSA_LEGACY_HBA_BUS;
Kevin Barnettc7955052015-11-04 15:51:45 -06003868 hpsa_set_bus_target_lun(device,
Hannes Reinecke7630b3a2016-11-17 12:15:56 +01003869 bus, 0, lunid & 0x3fff);
3870 } else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003871 /* defer target, lun assignment for physical devices */
Kevin Barnettc7955052015-11-04 15:51:45 -06003872 hpsa_set_bus_target_lun(device,
3873 HPSA_PHYSICAL_DEVICE_BUS, -1, -1);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003874 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003875 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003876 /* It's a logical device */
Scott Teel66749d02015-11-04 15:51:57 -06003877 if (device->external) {
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003878 hpsa_set_bus_target_lun(device,
Kevin Barnettc7955052015-11-04 15:51:45 -06003879 HPSA_EXTERNAL_RAID_VOLUME_BUS, (lunid >> 16) & 0x3fff,
3880 lunid & 0x00ff);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003881 return;
3882 }
Kevin Barnettc7955052015-11-04 15:51:45 -06003883 hpsa_set_bus_target_lun(device, HPSA_RAID_VOLUME_BUS,
3884 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003885}
3886
Scott Teel66749d02015-11-04 15:51:57 -06003887static int figure_external_status(struct ctlr_info *h, int raid_ctlr_position,
3888 int i, int nphysicals, int nlocal_logicals)
3889{
3890 /* In report logicals, local logicals are listed first,
3891 * then any externals.
3892 */
3893 int logicals_start = nphysicals + (raid_ctlr_position == 0);
3894
3895 if (i == raid_ctlr_position)
3896 return 0;
3897
3898 if (i < logicals_start)
3899 return 0;
3900
3901 /* i is in logicals range, but still within local logicals */
3902 if ((i - nphysicals - (raid_ctlr_position == 0)) < nlocal_logicals)
3903 return 0;
3904
3905 return 1; /* it's an external lun */
3906}
3907
Scott Teel54b6e9e2014-02-18 13:56:45 -06003908/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003909 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
3910 * logdev. The number of luns in physdev and logdev are returned in
3911 * *nphysicals and *nlogicals, respectively.
3912 * Returns 0 on success, -1 otherwise.
3913 */
3914static int hpsa_gather_lun_info(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06003915 struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003916 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003917{
Don Brace03383732015-01-23 16:43:30 -06003918 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003919 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3920 return -1;
3921 }
Don Brace03383732015-01-23 16:43:30 -06003922 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003923 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
Don Brace03383732015-01-23 16:43:30 -06003924 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
3925 HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003926 *nphysicals = HPSA_MAX_PHYS_LUN;
3927 }
Don Brace03383732015-01-23 16:43:30 -06003928 if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003929 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3930 return -1;
3931 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06003932 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003933 /* Reject Logicals in excess of our max capability. */
3934 if (*nlogicals > HPSA_MAX_LUN) {
3935 dev_warn(&h->pdev->dev,
3936 "maximum logical LUNs (%d) exceeded. "
3937 "%d LUNs ignored.\n", HPSA_MAX_LUN,
3938 *nlogicals - HPSA_MAX_LUN);
3939 *nlogicals = HPSA_MAX_LUN;
3940 }
3941 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
3942 dev_warn(&h->pdev->dev,
3943 "maximum logical + physical LUNs (%d) exceeded. "
3944 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
3945 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
3946 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
3947 }
3948 return 0;
3949}
3950
Don Brace42a91642014-11-14 17:26:27 -06003951static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
3952 int i, int nphysicals, int nlogicals,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06003953 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003954 struct ReportLUNdata *logdev_list)
3955{
3956 /* Helper function, figure out where the LUN ID info is coming from
3957 * given index i, lists of physical and logical devices, where in
3958 * the list the raid controller is supposed to appear (first or last)
3959 */
3960
3961 int logicals_start = nphysicals + (raid_ctlr_position == 0);
3962 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
3963
3964 if (i == raid_ctlr_position)
3965 return RAID_CTLR_LUNID;
3966
3967 if (i < logicals_start)
Stephen M. Camerond5b5d962014-05-29 10:53:34 -05003968 return &physdev_list->LUN[i -
3969 (raid_ctlr_position == 0)].lunid[0];
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003970
3971 if (i < last_device)
3972 return &logdev_list->LUN[i - nphysicals -
3973 (raid_ctlr_position == 0)][0];
3974 BUG();
3975 return NULL;
3976}
3977
Don Brace03383732015-01-23 16:43:30 -06003978/* get physical drive ioaccel handle and queue depth */
3979static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
3980 struct hpsa_scsi_dev_t *dev,
Don Bracef2039b02015-11-04 15:51:08 -06003981 struct ReportExtendedLUNdata *rlep, int rle_index,
Don Brace03383732015-01-23 16:43:30 -06003982 struct bmic_identify_physical_device *id_phys)
3983{
3984 int rc;
Scott Teel4b6e5592016-09-09 16:30:36 -05003985 struct ext_report_lun_entry *rle;
3986
Scott Teel4b6e5592016-09-09 16:30:36 -05003987 rle = &rlep->LUN[rle_index];
Don Brace03383732015-01-23 16:43:30 -06003988
3989 dev->ioaccel_handle = rle->ioaccel_handle;
Don Bracef2039b02015-11-04 15:51:08 -06003990 if ((rle->device_flags & 0x08) && dev->ioaccel_handle)
Joe Handzika3144e02015-04-23 09:32:59 -05003991 dev->hba_ioaccel_enabled = 1;
Don Brace03383732015-01-23 16:43:30 -06003992 memset(id_phys, 0, sizeof(*id_phys));
Don Bracef2039b02015-11-04 15:51:08 -06003993 rc = hpsa_bmic_id_physical_device(h, &rle->lunid[0],
3994 GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]), id_phys,
Don Brace03383732015-01-23 16:43:30 -06003995 sizeof(*id_phys));
3996 if (!rc)
3997 /* Reserve space for FW operations */
3998#define DRIVE_CMDS_RESERVED_FOR_FW 2
3999#define DRIVE_QUEUE_DEPTH 7
4000 dev->queue_depth =
4001 le16_to_cpu(id_phys->current_queue_depth_limit) -
4002 DRIVE_CMDS_RESERVED_FOR_FW;
4003 else
4004 dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
Don Brace03383732015-01-23 16:43:30 -06004005}
4006
Joe Handzik8270b862015-07-18 11:12:43 -05004007static void hpsa_get_path_info(struct hpsa_scsi_dev_t *this_device,
Don Bracef2039b02015-11-04 15:51:08 -06004008 struct ReportExtendedLUNdata *rlep, int rle_index,
Joe Handzik8270b862015-07-18 11:12:43 -05004009 struct bmic_identify_physical_device *id_phys)
4010{
Don Bracef2039b02015-11-04 15:51:08 -06004011 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
4012
4013 if ((rle->device_flags & 0x08) && this_device->ioaccel_handle)
Joe Handzik8270b862015-07-18 11:12:43 -05004014 this_device->hba_ioaccel_enabled = 1;
4015
4016 memcpy(&this_device->active_path_index,
4017 &id_phys->active_path_number,
4018 sizeof(this_device->active_path_index));
4019 memcpy(&this_device->path_map,
4020 &id_phys->redundant_path_present_map,
4021 sizeof(this_device->path_map));
4022 memcpy(&this_device->box,
4023 &id_phys->alternate_paths_phys_box_on_port,
4024 sizeof(this_device->box));
4025 memcpy(&this_device->phys_connector,
4026 &id_phys->alternate_paths_phys_connector,
4027 sizeof(this_device->phys_connector));
4028 memcpy(&this_device->bay,
4029 &id_phys->phys_bay_in_box,
4030 sizeof(this_device->bay));
4031}
4032
Scott Teel66749d02015-11-04 15:51:57 -06004033/* get number of local logical disks. */
4034static int hpsa_set_local_logical_count(struct ctlr_info *h,
4035 struct bmic_identify_controller *id_ctlr,
4036 u32 *nlocals)
4037{
4038 int rc;
4039
4040 if (!id_ctlr) {
4041 dev_warn(&h->pdev->dev, "%s: id_ctlr buffer is NULL.\n",
4042 __func__);
4043 return -ENOMEM;
4044 }
4045 memset(id_ctlr, 0, sizeof(*id_ctlr));
4046 rc = hpsa_bmic_id_controller(h, id_ctlr, sizeof(*id_ctlr));
4047 if (!rc)
4048 if (id_ctlr->configured_logical_drive_count < 256)
4049 *nlocals = id_ctlr->configured_logical_drive_count;
4050 else
4051 *nlocals = le16_to_cpu(
4052 id_ctlr->extended_logical_unit_count);
4053 else
4054 *nlocals = -1;
4055 return rc;
4056}
4057
Don Brace64ce60c2016-07-01 13:37:31 -05004058static bool hpsa_is_disk_spare(struct ctlr_info *h, u8 *lunaddrbytes)
4059{
4060 struct bmic_identify_physical_device *id_phys;
4061 bool is_spare = false;
4062 int rc;
4063
4064 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
4065 if (!id_phys)
4066 return false;
4067
4068 rc = hpsa_bmic_id_physical_device(h,
4069 lunaddrbytes,
4070 GET_BMIC_DRIVE_NUMBER(lunaddrbytes),
4071 id_phys, sizeof(*id_phys));
4072 if (rc == 0)
4073 is_spare = (id_phys->more_flags >> 6) & 0x01;
4074
4075 kfree(id_phys);
4076 return is_spare;
4077}
4078
4079#define RPL_DEV_FLAG_NON_DISK 0x1
4080#define RPL_DEV_FLAG_UNCONFIG_DISK_REPORTING_SUPPORTED 0x2
4081#define RPL_DEV_FLAG_UNCONFIG_DISK 0x4
4082
4083#define BMIC_DEVICE_TYPE_ENCLOSURE 6
4084
4085static bool hpsa_skip_device(struct ctlr_info *h, u8 *lunaddrbytes,
4086 struct ext_report_lun_entry *rle)
4087{
4088 u8 device_flags;
4089 u8 device_type;
4090
4091 if (!MASKED_DEVICE(lunaddrbytes))
4092 return false;
4093
4094 device_flags = rle->device_flags;
4095 device_type = rle->device_type;
4096
4097 if (device_flags & RPL_DEV_FLAG_NON_DISK) {
4098 if (device_type == BMIC_DEVICE_TYPE_ENCLOSURE)
4099 return false;
4100 return true;
4101 }
4102
4103 if (!(device_flags & RPL_DEV_FLAG_UNCONFIG_DISK_REPORTING_SUPPORTED))
4104 return false;
4105
4106 if (device_flags & RPL_DEV_FLAG_UNCONFIG_DISK)
4107 return false;
4108
4109 /*
4110 * Spares may be spun down, we do not want to
4111 * do an Inquiry to a RAID set spare drive as
4112 * that would have them spun up, that is a
4113 * performance hit because I/O to the RAID device
4114 * stops while the spin up occurs which can take
4115 * over 50 seconds.
4116 */
4117 if (hpsa_is_disk_spare(h, lunaddrbytes))
4118 return true;
4119
4120 return false;
4121}
Scott Teel66749d02015-11-04 15:51:57 -06004122
Don Brace8aa60682015-11-04 15:50:01 -06004123static void hpsa_update_scsi_devices(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004124{
4125 /* the idea here is we could get notified
4126 * that some devices have changed, so we do a report
4127 * physical luns and report logical luns cmd, and adjust
4128 * our list of devices accordingly.
4129 *
4130 * The scsi3addr's of devices won't change so long as the
4131 * adapter is not reset. That means we can rescan and
4132 * tell which devices we already know about, vs. new
4133 * devices, vs. disappearing devices.
4134 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06004135 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004136 struct ReportLUNdata *logdev_list = NULL;
Don Brace03383732015-01-23 16:43:30 -06004137 struct bmic_identify_physical_device *id_phys = NULL;
Scott Teel66749d02015-11-04 15:51:57 -06004138 struct bmic_identify_controller *id_ctlr = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004139 u32 nphysicals = 0;
4140 u32 nlogicals = 0;
Scott Teel66749d02015-11-04 15:51:57 -06004141 u32 nlocal_logicals = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004142 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004143 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
4144 int ncurrent = 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06004145 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004146 int raid_ctlr_position;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004147 bool physical_device;
Scott Teelaca4a522012-01-19 14:01:19 -06004148 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004149
Scott Teelcfe5bad2011-10-26 16:21:07 -05004150 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameron92084712014-11-14 17:26:54 -06004151 physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
4152 logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004153 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
Don Brace03383732015-01-23 16:43:30 -06004154 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
Scott Teel66749d02015-11-04 15:51:57 -06004155 id_ctlr = kzalloc(sizeof(*id_ctlr), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004156
Don Brace03383732015-01-23 16:43:30 -06004157 if (!currentsd || !physdev_list || !logdev_list ||
Scott Teel66749d02015-11-04 15:51:57 -06004158 !tmpdevice || !id_phys || !id_ctlr) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004159 dev_err(&h->pdev->dev, "out of memory\n");
4160 goto out;
4161 }
4162 memset(lunzerobits, 0, sizeof(lunzerobits));
4163
Don Brace853633e2015-11-04 15:50:37 -06004164 h->drv_req_rescan = 0; /* cancel scheduled rescan - we're doing it. */
4165
Don Brace03383732015-01-23 16:43:30 -06004166 if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
Don Brace853633e2015-11-04 15:50:37 -06004167 logdev_list, &nlogicals)) {
4168 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004169 goto out;
Don Brace853633e2015-11-04 15:50:37 -06004170 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004171
Scott Teel66749d02015-11-04 15:51:57 -06004172 /* Set number of local logicals (non PTRAID) */
4173 if (hpsa_set_local_logical_count(h, id_ctlr, &nlocal_logicals)) {
4174 dev_warn(&h->pdev->dev,
4175 "%s: Can't determine number of local logical devices.\n",
4176 __func__);
4177 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004178
Scott Teelaca4a522012-01-19 14:01:19 -06004179 /* We might see up to the maximum number of logical and physical disks
4180 * plus external target devices, and a device for the local RAID
4181 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004182 */
Scott Teelaca4a522012-01-19 14:01:19 -06004183 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004184
4185 /* Allocate the per device structures */
4186 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05004187 if (i >= HPSA_MAX_DEVICES) {
4188 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
4189 " %d devices ignored.\n", HPSA_MAX_DEVICES,
4190 ndevs_to_allocate - HPSA_MAX_DEVICES);
4191 break;
4192 }
4193
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004194 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
4195 if (!currentsd[i]) {
Don Brace853633e2015-11-04 15:50:37 -06004196 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004197 goto out;
4198 }
4199 ndev_allocated++;
4200 }
4201
Stephen M. Cameron86452912014-05-29 10:53:49 -05004202 if (is_scsi_rev_5(h))
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004203 raid_ctlr_position = 0;
4204 else
4205 raid_ctlr_position = nphysicals + nlogicals;
4206
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004207 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06004208 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004209 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004210 u8 *lunaddrbytes, is_OBDR = 0;
Don Brace683fc442015-11-04 15:50:44 -06004211 int rc = 0;
Don Bracef2039b02015-11-04 15:51:08 -06004212 int phys_dev_index = i - (raid_ctlr_position == 0);
Don Brace64ce60c2016-07-01 13:37:31 -05004213 bool skip_device = false;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004214
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004215 physical_device = i < nphysicals + (raid_ctlr_position == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004216
4217 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06004218 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
4219 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004220
Don Brace86cf7132016-09-09 16:30:17 -05004221 /* Determine if this is a lun from an external target array */
4222 tmpdevice->external =
4223 figure_external_status(h, raid_ctlr_position, i,
4224 nphysicals, nlocal_logicals);
4225
Don Brace64ce60c2016-07-01 13:37:31 -05004226 /*
4227 * Skip over some devices such as a spare.
4228 */
4229 if (!tmpdevice->external && physical_device) {
4230 skip_device = hpsa_skip_device(h, lunaddrbytes,
4231 &physdev_list->LUN[phys_dev_index]);
4232 if (skip_device)
4233 continue;
4234 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004235
4236 /* Get device type, vendor, model, device id */
Don Brace683fc442015-11-04 15:50:44 -06004237 rc = hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
4238 &is_OBDR);
4239 if (rc == -ENOMEM) {
4240 dev_warn(&h->pdev->dev,
4241 "Out of memory, rescan deferred.\n");
Don Brace853633e2015-11-04 15:50:37 -06004242 h->drv_req_rescan = 1;
Don Brace683fc442015-11-04 15:50:44 -06004243 goto out;
Don Brace853633e2015-11-04 15:50:37 -06004244 }
Don Brace683fc442015-11-04 15:50:44 -06004245 if (rc) {
Don Brace85b29002017-03-10 14:35:11 -06004246 h->drv_req_rescan = 1;
Don Brace683fc442015-11-04 15:50:44 -06004247 continue;
4248 }
4249
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06004250 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004251 this_device = currentsd[ncurrent];
4252
Scott Teel34592252015-11-04 15:52:09 -06004253 /* Turn on discovery_polling if there are ext target devices.
4254 * Event-based change notification is unreliable for those.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004255 */
Scott Teel34592252015-11-04 15:52:09 -06004256 if (!h->discovery_polling) {
4257 if (tmpdevice->external) {
4258 h->discovery_polling = 1;
4259 dev_info(&h->pdev->dev,
4260 "External target, activate discovery polling.\n");
4261 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004262 }
4263
Scott Teel34592252015-11-04 15:52:09 -06004264
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004265 *this_device = *tmpdevice;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004266 this_device->physical_device = physical_device;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004267
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004268 /*
4269 * Expose all devices except for physical devices that
4270 * are masked.
4271 */
4272 if (MASKED_DEVICE(lunaddrbytes) && this_device->physical_device)
Kevin Barnett2a168202015-11-04 15:51:21 -06004273 this_device->expose_device = 0;
4274 else
4275 this_device->expose_device = 1;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004276
Kevin Barnettd04e62b2015-11-04 15:52:34 -06004277
4278 /*
4279 * Get the SAS address for physical devices that are exposed.
4280 */
4281 if (this_device->physical_device && this_device->expose_device)
4282 hpsa_get_sas_address(h, lunaddrbytes, this_device);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004283
4284 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004285 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004286 /* We don't *really* support actual CD-ROM devices,
4287 * just "One Button Disaster Recovery" tape drive
4288 * which temporarily pretends to be a CD-ROM drive.
4289 * So we check that the device is really an OBDR tape
4290 * device by checking for "$DR-10" in bytes 43-48 of
4291 * the inquiry data.
4292 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05004293 if (is_OBDR)
4294 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004295 break;
4296 case TYPE_DISK:
Don Braceaf15ed32016-02-23 15:16:15 -06004297 case TYPE_ZBC:
Kevin Barnett04fa2f42015-11-04 15:51:27 -06004298 if (this_device->physical_device) {
Kevin Barnettb9092b72015-07-18 11:12:59 -05004299 /* The disk is in HBA mode. */
4300 /* Never use RAID mapper in HBA mode. */
Stephen M. Cameron316b2212014-02-21 16:25:15 -06004301 this_device->offload_enabled = 0;
Kevin Barnettb9092b72015-07-18 11:12:59 -05004302 hpsa_get_ioaccel_drive_info(h, this_device,
Don Bracef2039b02015-11-04 15:51:08 -06004303 physdev_list, phys_dev_index, id_phys);
4304 hpsa_get_path_info(this_device,
4305 physdev_list, phys_dev_index, id_phys);
Kevin Barnettb9092b72015-07-18 11:12:59 -05004306 }
Joe Handzikecf418d12015-04-23 09:33:04 -05004307 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004308 break;
4309 case TYPE_TAPE:
4310 case TYPE_MEDIUM_CHANGER:
Don Bracecca8f132015-12-22 10:36:48 -06004311 ncurrent++;
4312 break;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004313 case TYPE_ENCLOSURE:
Don Brace17a9e542016-02-23 15:16:09 -06004314 if (!this_device->external)
4315 hpsa_get_enclosure_info(h, lunaddrbytes,
Don Bracecca8f132015-12-22 10:36:48 -06004316 physdev_list, phys_dev_index,
4317 this_device);
Kevin Barnettb9092b72015-07-18 11:12:59 -05004318 ncurrent++;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05004319 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004320 case TYPE_RAID:
4321 /* Only present the Smartarray HBA as a RAID controller.
4322 * If it's a RAID controller other than the HBA itself
4323 * (an external RAID controller, MSA500 or similar)
4324 * don't present it.
4325 */
4326 if (!is_hba_lunid(lunaddrbytes))
4327 break;
4328 ncurrent++;
4329 break;
4330 default:
4331 break;
4332 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05004333 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004334 break;
4335 }
Kevin Barnettd04e62b2015-11-04 15:52:34 -06004336
4337 if (h->sas_host == NULL) {
4338 int rc = 0;
4339
4340 rc = hpsa_add_sas_host(h);
4341 if (rc) {
4342 dev_warn(&h->pdev->dev,
4343 "Could not add sas host %d\n", rc);
4344 goto out;
4345 }
4346 }
4347
Don Brace8aa60682015-11-04 15:50:01 -06004348 adjust_hpsa_scsi_table(h, currentsd, ncurrent);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004349out:
4350 kfree(tmpdevice);
4351 for (i = 0; i < ndev_allocated; i++)
4352 kfree(currentsd[i]);
4353 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004354 kfree(physdev_list);
4355 kfree(logdev_list);
Scott Teel66749d02015-11-04 15:51:57 -06004356 kfree(id_ctlr);
Don Brace03383732015-01-23 16:43:30 -06004357 kfree(id_phys);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004358}
4359
Webb Scalesec5cbf02015-01-23 16:44:45 -06004360static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
4361 struct scatterlist *sg)
4362{
4363 u64 addr64 = (u64) sg_dma_address(sg);
4364 unsigned int len = sg_dma_len(sg);
4365
4366 desc->Addr = cpu_to_le64(addr64);
4367 desc->Len = cpu_to_le32(len);
4368 desc->Ext = 0;
4369}
4370
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004371/*
4372 * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004373 * dma mapping and fills in the scatter gather entries of the
4374 * hpsa command, cp.
4375 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004376static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004377 struct CommandList *cp,
4378 struct scsi_cmnd *cmd)
4379{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004380 struct scatterlist *sg;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004381 int use_sg, i, sg_limit, chained, last_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004382 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004383
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004384 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004385
4386 use_sg = scsi_dma_map(cmd);
4387 if (use_sg < 0)
4388 return use_sg;
4389
4390 if (!use_sg)
4391 goto sglist_finished;
4392
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004393 /*
4394 * If the number of entries is greater than the max for a single list,
4395 * then we have a chained list; we will set up all but one entry in the
4396 * first list (the last entry is saved for link information);
4397 * otherwise, we don't have a chained list and we'll set up at each of
4398 * the entries in the one list.
4399 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004400 curr_sg = cp->SG;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004401 chained = use_sg > h->max_cmd_sg_entries;
4402 sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
4403 last_sg = scsi_sg_count(cmd) - 1;
4404 scsi_for_each_sg(cmd, sg, sg_limit, i) {
Webb Scalesec5cbf02015-01-23 16:44:45 -06004405 hpsa_set_sg_descriptor(curr_sg, sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004406 curr_sg++;
4407 }
Webb Scalesec5cbf02015-01-23 16:44:45 -06004408
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004409 if (chained) {
4410 /*
4411 * Continue with the chained list. Set curr_sg to the chained
4412 * list. Modify the limit to the total count less the entries
4413 * we've already set up. Resume the scan at the list entry
4414 * where the previous loop left off.
4415 */
4416 curr_sg = h->cmd_sg_list[cp->cmdindex];
4417 sg_limit = use_sg - sg_limit;
4418 for_each_sg(sg, sg, sg_limit, i) {
4419 hpsa_set_sg_descriptor(curr_sg, sg);
4420 curr_sg++;
4421 }
4422 }
4423
Webb Scalesec5cbf02015-01-23 16:44:45 -06004424 /* Back the pointer up to the last entry and mark it as "last". */
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004425 (curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004426
4427 if (use_sg + chained > h->maxSG)
4428 h->maxSG = use_sg + chained;
4429
4430 if (chained) {
4431 cp->Header.SGList = h->max_cmd_sg_entries;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004432 cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06004433 if (hpsa_map_sg_chain_block(h, cp)) {
4434 scsi_dma_unmap(cmd);
4435 return -1;
4436 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004437 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004438 }
4439
4440sglist_finished:
4441
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004442 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004443 cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004444 return 0;
4445}
4446
Don Braceb63c64a2017-05-04 17:51:42 -05004447#define BUFLEN 128
4448static inline void warn_zero_length_transfer(struct ctlr_info *h,
4449 u8 *cdb, int cdb_len,
4450 const char *func)
4451{
4452 char buf[BUFLEN];
4453 int outlen;
4454 int i;
4455
4456 outlen = scnprintf(buf, BUFLEN,
4457 "%s: Blocking zero-length request: CDB:", func);
4458 for (i = 0; i < cdb_len; i++)
4459 outlen += scnprintf(buf+outlen, BUFLEN - outlen,
4460 "%02hhx", cdb[i]);
4461 dev_warn(&h->pdev->dev, "%s\n", buf);
4462}
4463
4464#define IO_ACCEL_INELIGIBLE 1
4465/* zero-length transfers trigger hardware errors. */
4466static bool is_zero_length_transfer(u8 *cdb)
4467{
4468 u32 block_cnt;
4469
4470 /* Block zero-length transfer sizes on certain commands. */
4471 switch (cdb[0]) {
4472 case READ_10:
4473 case WRITE_10:
4474 case VERIFY: /* 0x2F */
4475 case WRITE_VERIFY: /* 0x2E */
4476 block_cnt = get_unaligned_be16(&cdb[7]);
4477 break;
4478 case READ_12:
4479 case WRITE_12:
4480 case VERIFY_12: /* 0xAF */
4481 case WRITE_VERIFY_12: /* 0xAE */
4482 block_cnt = get_unaligned_be32(&cdb[6]);
4483 break;
4484 case READ_16:
4485 case WRITE_16:
4486 case VERIFY_16: /* 0x8F */
4487 block_cnt = get_unaligned_be32(&cdb[10]);
4488 break;
4489 default:
4490 return false;
4491 }
4492
4493 return block_cnt == 0;
4494}
4495
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004496static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
4497{
4498 int is_write = 0;
4499 u32 block;
4500 u32 block_cnt;
4501
4502 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
4503 switch (cdb[0]) {
4504 case WRITE_6:
4505 case WRITE_12:
4506 is_write = 1;
4507 case READ_6:
4508 case READ_12:
4509 if (*cdb_len == 6) {
Mahesh Rajashekharaabbada72016-09-16 14:54:23 -05004510 block = (((cdb[1] & 0x1F) << 16) |
4511 (cdb[2] << 8) |
4512 cdb[3]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004513 block_cnt = cdb[4];
Don Bracec8a6c9a2015-11-04 15:50:50 -06004514 if (block_cnt == 0)
4515 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004516 } else {
4517 BUG_ON(*cdb_len != 12);
Don Bracec8a6c9a2015-11-04 15:50:50 -06004518 block = get_unaligned_be32(&cdb[2]);
4519 block_cnt = get_unaligned_be32(&cdb[6]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004520 }
4521 if (block_cnt > 0xffff)
4522 return IO_ACCEL_INELIGIBLE;
4523
4524 cdb[0] = is_write ? WRITE_10 : READ_10;
4525 cdb[1] = 0;
4526 cdb[2] = (u8) (block >> 24);
4527 cdb[3] = (u8) (block >> 16);
4528 cdb[4] = (u8) (block >> 8);
4529 cdb[5] = (u8) (block);
4530 cdb[6] = 0;
4531 cdb[7] = (u8) (block_cnt >> 8);
4532 cdb[8] = (u8) (block_cnt);
4533 cdb[9] = 0;
4534 *cdb_len = 10;
4535 break;
4536 }
4537 return 0;
4538}
4539
Scott Teelc3497752014-02-18 13:56:34 -06004540static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004541 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004542 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Matt Gatese1f7de02014-02-18 13:55:17 -06004543{
4544 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06004545 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
4546 unsigned int len;
4547 unsigned int total_len = 0;
4548 struct scatterlist *sg;
4549 u64 addr64;
4550 int use_sg, i;
4551 struct SGDescriptor *curr_sg;
4552 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
4553
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004554 /* TODO: implement chaining support */
Don Brace03383732015-01-23 16:43:30 -06004555 if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
4556 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004557 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004558 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004559
Matt Gatese1f7de02014-02-18 13:55:17 -06004560 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
4561
Don Braceb63c64a2017-05-04 17:51:42 -05004562 if (is_zero_length_transfer(cdb)) {
4563 warn_zero_length_transfer(h, cdb, cdb_len, __func__);
4564 atomic_dec(&phys_disk->ioaccel_cmds_out);
4565 return IO_ACCEL_INELIGIBLE;
4566 }
4567
Don Brace03383732015-01-23 16:43:30 -06004568 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4569 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004570 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004571 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004572
Matt Gatese1f7de02014-02-18 13:55:17 -06004573 c->cmd_type = CMD_IOACCEL1;
4574
4575 /* Adjust the DMA address to point to the accelerated command buffer */
4576 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
4577 (c->cmdindex * sizeof(*cp));
4578 BUG_ON(c->busaddr & 0x0000007F);
4579
4580 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004581 if (use_sg < 0) {
4582 atomic_dec(&phys_disk->ioaccel_cmds_out);
Matt Gatese1f7de02014-02-18 13:55:17 -06004583 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004584 }
Matt Gatese1f7de02014-02-18 13:55:17 -06004585
4586 if (use_sg) {
4587 curr_sg = cp->SG;
4588 scsi_for_each_sg(cmd, sg, use_sg, i) {
4589 addr64 = (u64) sg_dma_address(sg);
4590 len = sg_dma_len(sg);
4591 total_len += len;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004592 curr_sg->Addr = cpu_to_le64(addr64);
4593 curr_sg->Len = cpu_to_le32(len);
4594 curr_sg->Ext = cpu_to_le32(0);
Matt Gatese1f7de02014-02-18 13:55:17 -06004595 curr_sg++;
4596 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004597 (--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
Matt Gatese1f7de02014-02-18 13:55:17 -06004598
4599 switch (cmd->sc_data_direction) {
4600 case DMA_TO_DEVICE:
4601 control |= IOACCEL1_CONTROL_DATA_OUT;
4602 break;
4603 case DMA_FROM_DEVICE:
4604 control |= IOACCEL1_CONTROL_DATA_IN;
4605 break;
4606 case DMA_NONE:
4607 control |= IOACCEL1_CONTROL_NODATAXFER;
4608 break;
4609 default:
4610 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4611 cmd->sc_data_direction);
4612 BUG();
4613 break;
4614 }
4615 } else {
4616 control |= IOACCEL1_CONTROL_NODATAXFER;
4617 }
4618
Scott Teelc3497752014-02-18 13:56:34 -06004619 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06004620 /* Fill out the command structure to submit */
Don Brace2b08b3e2015-01-23 16:41:09 -06004621 cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
4622 cp->transfer_len = cpu_to_le32(total_len);
4623 cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
4624 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
4625 cp->control = cpu_to_le32(control);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004626 memcpy(cp->CDB, cdb, cdb_len);
4627 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06004628 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004629 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06004630 return 0;
4631}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004632
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004633/*
4634 * Queue a command directly to a device behind the controller using the
4635 * I/O accelerator path.
4636 */
4637static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
4638 struct CommandList *c)
4639{
4640 struct scsi_cmnd *cmd = c->scsi_cmd;
4641 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4642
Don Brace45e596c2016-09-09 16:30:42 -05004643 if (!dev)
4644 return -1;
4645
Don Brace03383732015-01-23 16:43:30 -06004646 c->phys_disk = dev;
4647
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004648 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004649 cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004650}
4651
Scott Teeldd0e19f2014-02-18 13:57:31 -06004652/*
4653 * Set encryption parameters for the ioaccel2 request
4654 */
4655static void set_encrypt_ioaccel2(struct ctlr_info *h,
4656 struct CommandList *c, struct io_accel2_cmd *cp)
4657{
4658 struct scsi_cmnd *cmd = c->scsi_cmd;
4659 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4660 struct raid_map_data *map = &dev->raid_map;
4661 u64 first_block;
4662
Scott Teeldd0e19f2014-02-18 13:57:31 -06004663 /* Are we doing encryption on this device */
Don Brace2b08b3e2015-01-23 16:41:09 -06004664 if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
Scott Teeldd0e19f2014-02-18 13:57:31 -06004665 return;
4666 /* Set the data encryption key index. */
4667 cp->dekindex = map->dekindex;
4668
4669 /* Set the encryption enable flag, encoded into direction field. */
4670 cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
4671
4672 /* Set encryption tweak values based on logical block address
4673 * If block size is 512, tweak value is LBA.
4674 * For other block sizes, tweak is (LBA * block size)/ 512)
4675 */
4676 switch (cmd->cmnd[0]) {
4677 /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
Scott Teeldd0e19f2014-02-18 13:57:31 -06004678 case READ_6:
Mahesh Rajashekharaabbada72016-09-16 14:54:23 -05004679 case WRITE_6:
4680 first_block = (((cmd->cmnd[1] & 0x1F) << 16) |
4681 (cmd->cmnd[2] << 8) |
4682 cmd->cmnd[3]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004683 break;
4684 case WRITE_10:
4685 case READ_10:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004686 /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
4687 case WRITE_12:
4688 case READ_12:
Don Brace2b08b3e2015-01-23 16:41:09 -06004689 first_block = get_unaligned_be32(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004690 break;
4691 case WRITE_16:
4692 case READ_16:
Don Brace2b08b3e2015-01-23 16:41:09 -06004693 first_block = get_unaligned_be64(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004694 break;
4695 default:
4696 dev_err(&h->pdev->dev,
Don Brace2b08b3e2015-01-23 16:41:09 -06004697 "ERROR: %s: size (0x%x) not supported for encryption\n",
4698 __func__, cmd->cmnd[0]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004699 BUG();
4700 break;
4701 }
Don Brace2b08b3e2015-01-23 16:41:09 -06004702
4703 if (le32_to_cpu(map->volume_blk_size) != 512)
4704 first_block = first_block *
4705 le32_to_cpu(map->volume_blk_size)/512;
4706
4707 cp->tweak_lower = cpu_to_le32(first_block);
4708 cp->tweak_upper = cpu_to_le32(first_block >> 32);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004709}
4710
Scott Teelc3497752014-02-18 13:56:34 -06004711static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
4712 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004713 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004714{
4715 struct scsi_cmnd *cmd = c->scsi_cmd;
4716 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
4717 struct ioaccel2_sg_element *curr_sg;
4718 int use_sg, i;
4719 struct scatterlist *sg;
4720 u64 addr64;
4721 u32 len;
4722 u32 total_len = 0;
4723
Don Brace45e596c2016-09-09 16:30:42 -05004724 if (!cmd->device)
4725 return -1;
4726
4727 if (!cmd->device->hostdata)
4728 return -1;
4729
Webb Scalesd9a729f2015-04-23 09:33:27 -05004730 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Scott Teelc3497752014-02-18 13:56:34 -06004731
Don Braceb63c64a2017-05-04 17:51:42 -05004732 if (is_zero_length_transfer(cdb)) {
4733 warn_zero_length_transfer(h, cdb, cdb_len, __func__);
4734 atomic_dec(&phys_disk->ioaccel_cmds_out);
4735 return IO_ACCEL_INELIGIBLE;
4736 }
4737
Don Brace03383732015-01-23 16:43:30 -06004738 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4739 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004740 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004741 }
4742
Scott Teelc3497752014-02-18 13:56:34 -06004743 c->cmd_type = CMD_IOACCEL2;
4744 /* Adjust the DMA address to point to the accelerated command buffer */
4745 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
4746 (c->cmdindex * sizeof(*cp));
4747 BUG_ON(c->busaddr & 0x0000007F);
4748
4749 memset(cp, 0, sizeof(*cp));
4750 cp->IU_type = IOACCEL2_IU_TYPE;
4751
4752 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004753 if (use_sg < 0) {
4754 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004755 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004756 }
Scott Teelc3497752014-02-18 13:56:34 -06004757
4758 if (use_sg) {
Scott Teelc3497752014-02-18 13:56:34 -06004759 curr_sg = cp->sg;
Webb Scalesd9a729f2015-04-23 09:33:27 -05004760 if (use_sg > h->ioaccel_maxsg) {
4761 addr64 = le64_to_cpu(
4762 h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
4763 curr_sg->address = cpu_to_le64(addr64);
4764 curr_sg->length = 0;
4765 curr_sg->reserved[0] = 0;
4766 curr_sg->reserved[1] = 0;
4767 curr_sg->reserved[2] = 0;
4768 curr_sg->chain_indicator = 0x80;
4769
4770 curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
4771 }
Scott Teelc3497752014-02-18 13:56:34 -06004772 scsi_for_each_sg(cmd, sg, use_sg, i) {
4773 addr64 = (u64) sg_dma_address(sg);
4774 len = sg_dma_len(sg);
4775 total_len += len;
4776 curr_sg->address = cpu_to_le64(addr64);
4777 curr_sg->length = cpu_to_le32(len);
4778 curr_sg->reserved[0] = 0;
4779 curr_sg->reserved[1] = 0;
4780 curr_sg->reserved[2] = 0;
4781 curr_sg->chain_indicator = 0;
4782 curr_sg++;
4783 }
4784
4785 switch (cmd->sc_data_direction) {
4786 case DMA_TO_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004787 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4788 cp->direction |= IOACCEL2_DIR_DATA_OUT;
Scott Teelc3497752014-02-18 13:56:34 -06004789 break;
4790 case DMA_FROM_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004791 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4792 cp->direction |= IOACCEL2_DIR_DATA_IN;
Scott Teelc3497752014-02-18 13:56:34 -06004793 break;
4794 case DMA_NONE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004795 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4796 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004797 break;
4798 default:
4799 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4800 cmd->sc_data_direction);
4801 BUG();
4802 break;
4803 }
4804 } else {
Scott Teeldd0e19f2014-02-18 13:57:31 -06004805 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4806 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004807 }
Scott Teeldd0e19f2014-02-18 13:57:31 -06004808
4809 /* Set encryption parameters, if necessary */
4810 set_encrypt_ioaccel2(h, c, cp);
4811
Don Brace2b08b3e2015-01-23 16:41:09 -06004812 cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
Don Bracef2405db2015-01-23 16:43:09 -06004813 cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
Scott Teelc3497752014-02-18 13:56:34 -06004814 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
Scott Teelc3497752014-02-18 13:56:34 -06004815
Scott Teelc3497752014-02-18 13:56:34 -06004816 cp->data_len = cpu_to_le32(total_len);
4817 cp->err_ptr = cpu_to_le64(c->busaddr +
4818 offsetof(struct io_accel2_cmd, error_data));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004819 cp->err_len = cpu_to_le32(sizeof(cp->error_data));
Scott Teelc3497752014-02-18 13:56:34 -06004820
Webb Scalesd9a729f2015-04-23 09:33:27 -05004821 /* fill in sg elements */
4822 if (use_sg > h->ioaccel_maxsg) {
4823 cp->sg_count = 1;
Don Bracea736e9b2015-11-04 15:51:14 -06004824 cp->sg[0].length = cpu_to_le32(use_sg * sizeof(cp->sg[0]));
Webb Scalesd9a729f2015-04-23 09:33:27 -05004825 if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
4826 atomic_dec(&phys_disk->ioaccel_cmds_out);
4827 scsi_dma_unmap(cmd);
4828 return -1;
4829 }
4830 } else
4831 cp->sg_count = (u8) use_sg;
4832
Scott Teelc3497752014-02-18 13:56:34 -06004833 enqueue_cmd_and_start_io(h, c);
4834 return 0;
4835}
4836
4837/*
4838 * Queue a command to the correct I/O accelerator path.
4839 */
4840static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
4841 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004842 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004843{
Don Brace45e596c2016-09-09 16:30:42 -05004844 if (!c->scsi_cmd->device)
4845 return -1;
4846
4847 if (!c->scsi_cmd->device->hostdata)
4848 return -1;
4849
Don Brace03383732015-01-23 16:43:30 -06004850 /* Try to honor the device's queue depth */
4851 if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
4852 phys_disk->queue_depth) {
4853 atomic_dec(&phys_disk->ioaccel_cmds_out);
4854 return IO_ACCEL_INELIGIBLE;
4855 }
Scott Teelc3497752014-02-18 13:56:34 -06004856 if (h->transMethod & CFGTBL_Trans_io_accel1)
4857 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004858 cdb, cdb_len, scsi3addr,
4859 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004860 else
4861 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004862 cdb, cdb_len, scsi3addr,
4863 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004864}
4865
Scott Teel6b80b182014-02-18 13:56:55 -06004866static void raid_map_helper(struct raid_map_data *map,
4867 int offload_to_mirror, u32 *map_index, u32 *current_group)
4868{
4869 if (offload_to_mirror == 0) {
4870 /* use physical disk in the first mirrored group. */
Don Brace2b08b3e2015-01-23 16:41:09 -06004871 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004872 return;
4873 }
4874 do {
4875 /* determine mirror group that *map_index indicates */
Don Brace2b08b3e2015-01-23 16:41:09 -06004876 *current_group = *map_index /
4877 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004878 if (offload_to_mirror == *current_group)
4879 continue;
Don Brace2b08b3e2015-01-23 16:41:09 -06004880 if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
Scott Teel6b80b182014-02-18 13:56:55 -06004881 /* select map index from next group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004882 *map_index += le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004883 (*current_group)++;
4884 } else {
4885 /* select map index from first group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004886 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004887 *current_group = 0;
4888 }
4889 } while (offload_to_mirror != *current_group);
4890}
4891
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004892/*
4893 * Attempt to perform offload RAID mapping for a logical volume I/O.
4894 */
4895static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4896 struct CommandList *c)
4897{
4898 struct scsi_cmnd *cmd = c->scsi_cmd;
4899 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4900 struct raid_map_data *map = &dev->raid_map;
4901 struct raid_map_disk_data *dd = &map->data[0];
4902 int is_write = 0;
4903 u32 map_index;
4904 u64 first_block, last_block;
4905 u32 block_cnt;
4906 u32 blocks_per_row;
4907 u64 first_row, last_row;
4908 u32 first_row_offset, last_row_offset;
4909 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06004910 u64 r0_first_row, r0_last_row;
4911 u32 r5or6_blocks_per_row;
4912 u64 r5or6_first_row, r5or6_last_row;
4913 u32 r5or6_first_row_offset, r5or6_last_row_offset;
4914 u32 r5or6_first_column, r5or6_last_column;
4915 u32 total_disks_per_row;
4916 u32 stripesize;
4917 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004918 u32 map_row;
4919 u32 disk_handle;
4920 u64 disk_block;
4921 u32 disk_block_cnt;
4922 u8 cdb[16];
4923 u8 cdb_len;
Don Brace2b08b3e2015-01-23 16:41:09 -06004924 u16 strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004925#if BITS_PER_LONG == 32
4926 u64 tmpdiv;
4927#endif
Scott Teel6b80b182014-02-18 13:56:55 -06004928 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004929
Don Brace45e596c2016-09-09 16:30:42 -05004930 if (!dev)
4931 return -1;
4932
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004933 /* check for valid opcode, get LBA and block count */
4934 switch (cmd->cmnd[0]) {
4935 case WRITE_6:
4936 is_write = 1;
4937 case READ_6:
Mahesh Rajashekharaabbada72016-09-16 14:54:23 -05004938 first_block = (((cmd->cmnd[1] & 0x1F) << 16) |
4939 (cmd->cmnd[2] << 8) |
4940 cmd->cmnd[3]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004941 block_cnt = cmd->cmnd[4];
Stephen M. Cameron3fa89a02014-07-03 10:18:14 -05004942 if (block_cnt == 0)
4943 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004944 break;
4945 case WRITE_10:
4946 is_write = 1;
4947 case READ_10:
4948 first_block =
4949 (((u64) cmd->cmnd[2]) << 24) |
4950 (((u64) cmd->cmnd[3]) << 16) |
4951 (((u64) cmd->cmnd[4]) << 8) |
4952 cmd->cmnd[5];
4953 block_cnt =
4954 (((u32) cmd->cmnd[7]) << 8) |
4955 cmd->cmnd[8];
4956 break;
4957 case WRITE_12:
4958 is_write = 1;
4959 case READ_12:
4960 first_block =
4961 (((u64) cmd->cmnd[2]) << 24) |
4962 (((u64) cmd->cmnd[3]) << 16) |
4963 (((u64) cmd->cmnd[4]) << 8) |
4964 cmd->cmnd[5];
4965 block_cnt =
4966 (((u32) cmd->cmnd[6]) << 24) |
4967 (((u32) cmd->cmnd[7]) << 16) |
4968 (((u32) cmd->cmnd[8]) << 8) |
4969 cmd->cmnd[9];
4970 break;
4971 case WRITE_16:
4972 is_write = 1;
4973 case READ_16:
4974 first_block =
4975 (((u64) cmd->cmnd[2]) << 56) |
4976 (((u64) cmd->cmnd[3]) << 48) |
4977 (((u64) cmd->cmnd[4]) << 40) |
4978 (((u64) cmd->cmnd[5]) << 32) |
4979 (((u64) cmd->cmnd[6]) << 24) |
4980 (((u64) cmd->cmnd[7]) << 16) |
4981 (((u64) cmd->cmnd[8]) << 8) |
4982 cmd->cmnd[9];
4983 block_cnt =
4984 (((u32) cmd->cmnd[10]) << 24) |
4985 (((u32) cmd->cmnd[11]) << 16) |
4986 (((u32) cmd->cmnd[12]) << 8) |
4987 cmd->cmnd[13];
4988 break;
4989 default:
4990 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4991 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004992 last_block = first_block + block_cnt - 1;
4993
4994 /* check for write to non-RAID-0 */
4995 if (is_write && dev->raid_level != 0)
4996 return IO_ACCEL_INELIGIBLE;
4997
4998 /* check for invalid block or wraparound */
Don Brace2b08b3e2015-01-23 16:41:09 -06004999 if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
5000 last_block < first_block)
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005001 return IO_ACCEL_INELIGIBLE;
5002
5003 /* calculate stripe information for the request */
Don Brace2b08b3e2015-01-23 16:41:09 -06005004 blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
5005 le16_to_cpu(map->strip_size);
5006 strip_size = le16_to_cpu(map->strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005007#if BITS_PER_LONG == 32
5008 tmpdiv = first_block;
5009 (void) do_div(tmpdiv, blocks_per_row);
5010 first_row = tmpdiv;
5011 tmpdiv = last_block;
5012 (void) do_div(tmpdiv, blocks_per_row);
5013 last_row = tmpdiv;
5014 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
5015 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
5016 tmpdiv = first_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06005017 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005018 first_column = tmpdiv;
5019 tmpdiv = last_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06005020 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005021 last_column = tmpdiv;
5022#else
5023 first_row = first_block / blocks_per_row;
5024 last_row = last_block / blocks_per_row;
5025 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
5026 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
Don Brace2b08b3e2015-01-23 16:41:09 -06005027 first_column = first_row_offset / strip_size;
5028 last_column = last_row_offset / strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005029#endif
5030
5031 /* if this isn't a single row/column then give to the controller */
5032 if ((first_row != last_row) || (first_column != last_column))
5033 return IO_ACCEL_INELIGIBLE;
5034
5035 /* proceeding with driver mapping */
Don Brace2b08b3e2015-01-23 16:41:09 -06005036 total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
5037 le16_to_cpu(map->metadata_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005038 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06005039 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06005040 map_index = (map_row * total_disks_per_row) + first_column;
5041
5042 switch (dev->raid_level) {
5043 case HPSA_RAID_0:
5044 break; /* nothing special to do */
5045 case HPSA_RAID_1:
5046 /* Handles load balance across RAID 1 members.
5047 * (2-drive R1 and R10 with even # of drives.)
5048 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005049 */
Don Brace2b08b3e2015-01-23 16:41:09 -06005050 BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005051 if (dev->offload_to_mirror)
Don Brace2b08b3e2015-01-23 16:41:09 -06005052 map_index += le16_to_cpu(map->data_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005053 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06005054 break;
5055 case HPSA_RAID_ADM:
5056 /* Handles N-way mirrors (R1-ADM)
5057 * and R10 with # of drives divisible by 3.)
5058 */
Don Brace2b08b3e2015-01-23 16:41:09 -06005059 BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
Scott Teel6b80b182014-02-18 13:56:55 -06005060
5061 offload_to_mirror = dev->offload_to_mirror;
5062 raid_map_helper(map, offload_to_mirror,
5063 &map_index, &current_group);
5064 /* set mirror group to use next time */
5065 offload_to_mirror =
Don Brace2b08b3e2015-01-23 16:41:09 -06005066 (offload_to_mirror >=
5067 le16_to_cpu(map->layout_map_count) - 1)
Scott Teel6b80b182014-02-18 13:56:55 -06005068 ? 0 : offload_to_mirror + 1;
Scott Teel6b80b182014-02-18 13:56:55 -06005069 dev->offload_to_mirror = offload_to_mirror;
5070 /* Avoid direct use of dev->offload_to_mirror within this
5071 * function since multiple threads might simultaneously
5072 * increment it beyond the range of dev->layout_map_count -1.
5073 */
5074 break;
5075 case HPSA_RAID_5:
5076 case HPSA_RAID_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06005077 if (le16_to_cpu(map->layout_map_count) <= 1)
Scott Teel6b80b182014-02-18 13:56:55 -06005078 break;
5079
5080 /* Verify first and last block are in same RAID group */
5081 r5or6_blocks_per_row =
Don Brace2b08b3e2015-01-23 16:41:09 -06005082 le16_to_cpu(map->strip_size) *
5083 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06005084 BUG_ON(r5or6_blocks_per_row == 0);
Don Brace2b08b3e2015-01-23 16:41:09 -06005085 stripesize = r5or6_blocks_per_row *
5086 le16_to_cpu(map->layout_map_count);
Scott Teel6b80b182014-02-18 13:56:55 -06005087#if BITS_PER_LONG == 32
5088 tmpdiv = first_block;
5089 first_group = do_div(tmpdiv, stripesize);
5090 tmpdiv = first_group;
5091 (void) do_div(tmpdiv, r5or6_blocks_per_row);
5092 first_group = tmpdiv;
5093 tmpdiv = last_block;
5094 last_group = do_div(tmpdiv, stripesize);
5095 tmpdiv = last_group;
5096 (void) do_div(tmpdiv, r5or6_blocks_per_row);
5097 last_group = tmpdiv;
5098#else
5099 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
5100 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
Scott Teel6b80b182014-02-18 13:56:55 -06005101#endif
Stephen M. Cameron000ff7c2014-03-13 17:12:50 -05005102 if (first_group != last_group)
Scott Teel6b80b182014-02-18 13:56:55 -06005103 return IO_ACCEL_INELIGIBLE;
5104
5105 /* Verify request is in a single row of RAID 5/6 */
5106#if BITS_PER_LONG == 32
5107 tmpdiv = first_block;
5108 (void) do_div(tmpdiv, stripesize);
5109 first_row = r5or6_first_row = r0_first_row = tmpdiv;
5110 tmpdiv = last_block;
5111 (void) do_div(tmpdiv, stripesize);
5112 r5or6_last_row = r0_last_row = tmpdiv;
5113#else
5114 first_row = r5or6_first_row = r0_first_row =
5115 first_block / stripesize;
5116 r5or6_last_row = r0_last_row = last_block / stripesize;
5117#endif
5118 if (r5or6_first_row != r5or6_last_row)
5119 return IO_ACCEL_INELIGIBLE;
5120
5121
5122 /* Verify request is in a single column */
5123#if BITS_PER_LONG == 32
5124 tmpdiv = first_block;
5125 first_row_offset = do_div(tmpdiv, stripesize);
5126 tmpdiv = first_row_offset;
5127 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
5128 r5or6_first_row_offset = first_row_offset;
5129 tmpdiv = last_block;
5130 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
5131 tmpdiv = r5or6_last_row_offset;
5132 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
5133 tmpdiv = r5or6_first_row_offset;
5134 (void) do_div(tmpdiv, map->strip_size);
5135 first_column = r5or6_first_column = tmpdiv;
5136 tmpdiv = r5or6_last_row_offset;
5137 (void) do_div(tmpdiv, map->strip_size);
5138 r5or6_last_column = tmpdiv;
5139#else
5140 first_row_offset = r5or6_first_row_offset =
5141 (u32)((first_block % stripesize) %
5142 r5or6_blocks_per_row);
5143
5144 r5or6_last_row_offset =
5145 (u32)((last_block % stripesize) %
5146 r5or6_blocks_per_row);
5147
5148 first_column = r5or6_first_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06005149 r5or6_first_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06005150 r5or6_last_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06005151 r5or6_last_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06005152#endif
5153 if (r5or6_first_column != r5or6_last_column)
5154 return IO_ACCEL_INELIGIBLE;
5155
5156 /* Request is eligible */
5157 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06005158 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06005159
5160 map_index = (first_group *
Don Brace2b08b3e2015-01-23 16:41:09 -06005161 (le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
Scott Teel6b80b182014-02-18 13:56:55 -06005162 (map_row * total_disks_per_row) + first_column;
5163 break;
5164 default:
5165 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005166 }
Scott Teel6b80b182014-02-18 13:56:55 -06005167
Stephen Cameron07543e02015-01-23 16:44:14 -06005168 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
5169 return IO_ACCEL_INELIGIBLE;
5170
Don Brace03383732015-01-23 16:43:30 -06005171 c->phys_disk = dev->phys_disk[map_index];
Don Bracec3390df2016-02-23 15:16:34 -06005172 if (!c->phys_disk)
5173 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06005174
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005175 disk_handle = dd[map_index].ioaccel_handle;
Don Brace2b08b3e2015-01-23 16:41:09 -06005176 disk_block = le64_to_cpu(map->disk_starting_blk) +
5177 first_row * le16_to_cpu(map->strip_size) +
5178 (first_row_offset - first_column *
5179 le16_to_cpu(map->strip_size));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005180 disk_block_cnt = block_cnt;
5181
5182 /* handle differing logical/physical block sizes */
5183 if (map->phys_blk_shift) {
5184 disk_block <<= map->phys_blk_shift;
5185 disk_block_cnt <<= map->phys_blk_shift;
5186 }
5187 BUG_ON(disk_block_cnt > 0xffff);
5188
5189 /* build the new CDB for the physical disk I/O */
5190 if (disk_block > 0xffffffff) {
5191 cdb[0] = is_write ? WRITE_16 : READ_16;
5192 cdb[1] = 0;
5193 cdb[2] = (u8) (disk_block >> 56);
5194 cdb[3] = (u8) (disk_block >> 48);
5195 cdb[4] = (u8) (disk_block >> 40);
5196 cdb[5] = (u8) (disk_block >> 32);
5197 cdb[6] = (u8) (disk_block >> 24);
5198 cdb[7] = (u8) (disk_block >> 16);
5199 cdb[8] = (u8) (disk_block >> 8);
5200 cdb[9] = (u8) (disk_block);
5201 cdb[10] = (u8) (disk_block_cnt >> 24);
5202 cdb[11] = (u8) (disk_block_cnt >> 16);
5203 cdb[12] = (u8) (disk_block_cnt >> 8);
5204 cdb[13] = (u8) (disk_block_cnt);
5205 cdb[14] = 0;
5206 cdb[15] = 0;
5207 cdb_len = 16;
5208 } else {
5209 cdb[0] = is_write ? WRITE_10 : READ_10;
5210 cdb[1] = 0;
5211 cdb[2] = (u8) (disk_block >> 24);
5212 cdb[3] = (u8) (disk_block >> 16);
5213 cdb[4] = (u8) (disk_block >> 8);
5214 cdb[5] = (u8) (disk_block);
5215 cdb[6] = 0;
5216 cdb[7] = (u8) (disk_block_cnt >> 8);
5217 cdb[8] = (u8) (disk_block_cnt);
5218 cdb[9] = 0;
5219 cdb_len = 10;
5220 }
5221 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
Don Brace03383732015-01-23 16:43:30 -06005222 dev->scsi3addr,
5223 dev->phys_disk[map_index]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005224}
5225
Webb Scales25163bd2015-04-23 09:32:00 -05005226/*
5227 * Submit commands down the "normal" RAID stack path
5228 * All callers to hpsa_ciss_submit must check lockup_detected
5229 * beforehand, before (opt.) and after calling cmd_alloc
5230 */
Stephen Cameron574f05d2015-01-23 16:43:20 -06005231static int hpsa_ciss_submit(struct ctlr_info *h,
5232 struct CommandList *c, struct scsi_cmnd *cmd,
5233 unsigned char scsi3addr[])
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005234{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005235 cmd->host_scribble = (unsigned char *) c;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005236 c->cmd_type = CMD_SCSI;
5237 c->scsi_cmd = cmd;
5238 c->Header.ReplyQueue = 0; /* unused in simple mode */
5239 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Bracef2405db2015-01-23 16:43:09 -06005240 c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005241
5242 /* Fill in the request block... */
5243
5244 c->Request.Timeout = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005245 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
5246 c->Request.CDBLen = cmd->cmd_len;
5247 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005248 switch (cmd->sc_data_direction) {
5249 case DMA_TO_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005250 c->Request.type_attr_dir =
5251 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005252 break;
5253 case DMA_FROM_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005254 c->Request.type_attr_dir =
5255 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005256 break;
5257 case DMA_NONE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06005258 c->Request.type_attr_dir =
5259 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005260 break;
5261 case DMA_BIDIRECTIONAL:
5262 /* This can happen if a buggy application does a scsi passthru
5263 * and sets both inlen and outlen to non-zero. ( see
5264 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
5265 */
5266
Stephen M. Camerona505b862014-11-14 17:27:04 -06005267 c->Request.type_attr_dir =
5268 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005269 /* This is technically wrong, and hpsa controllers should
5270 * reject it with CMD_INVALID, which is the most correct
5271 * response, but non-fibre backends appear to let it
5272 * slide by, and give the same results as if this field
5273 * were set correctly. Either way is acceptable for
5274 * our purposes here.
5275 */
5276
5277 break;
5278
5279 default:
5280 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
5281 cmd->sc_data_direction);
5282 BUG();
5283 break;
5284 }
5285
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005286 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Webb Scales73153fe2015-04-23 09:35:04 -05005287 hpsa_cmd_resolve_and_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005288 return SCSI_MLQUEUE_HOST_BUSY;
5289 }
5290 enqueue_cmd_and_start_io(h, c);
5291 /* the cmd'll come back via intr handler in complete_scsi_command() */
5292 return 0;
5293}
5294
Stephen Cameron360c73b2015-04-23 09:32:32 -05005295static void hpsa_cmd_init(struct ctlr_info *h, int index,
5296 struct CommandList *c)
5297{
5298 dma_addr_t cmd_dma_handle, err_dma_handle;
5299
5300 /* Zero out all of commandlist except the last field, refcount */
5301 memset(c, 0, offsetof(struct CommandList, refcount));
5302 c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
5303 cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
5304 c->err_info = h->errinfo_pool + index;
5305 memset(c->err_info, 0, sizeof(*c->err_info));
5306 err_dma_handle = h->errinfo_pool_dhandle
5307 + index * sizeof(*c->err_info);
5308 c->cmdindex = index;
5309 c->busaddr = (u32) cmd_dma_handle;
5310 c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
5311 c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
5312 c->h = h;
Webb Scalesa58e7e52015-04-23 09:34:16 -05005313 c->scsi_cmd = SCSI_CMD_IDLE;
Stephen Cameron360c73b2015-04-23 09:32:32 -05005314}
5315
5316static void hpsa_preinitialize_commands(struct ctlr_info *h)
5317{
5318 int i;
5319
5320 for (i = 0; i < h->nr_cmds; i++) {
5321 struct CommandList *c = h->cmd_pool + i;
5322
5323 hpsa_cmd_init(h, i, c);
5324 atomic_set(&c->refcount, 0);
5325 }
5326}
5327
5328static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
5329 struct CommandList *c)
5330{
5331 dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
5332
Webb Scales73153fe2015-04-23 09:35:04 -05005333 BUG_ON(c->cmdindex != index);
5334
Stephen Cameron360c73b2015-04-23 09:32:32 -05005335 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
5336 memset(c->err_info, 0, sizeof(*c->err_info));
5337 c->busaddr = (u32) cmd_dma_handle;
5338}
5339
Webb Scales592a0ad2015-04-23 09:32:48 -05005340static int hpsa_ioaccel_submit(struct ctlr_info *h,
5341 struct CommandList *c, struct scsi_cmnd *cmd,
5342 unsigned char *scsi3addr)
5343{
5344 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
5345 int rc = IO_ACCEL_INELIGIBLE;
5346
Don Brace45e596c2016-09-09 16:30:42 -05005347 if (!dev)
5348 return SCSI_MLQUEUE_HOST_BUSY;
5349
Webb Scales592a0ad2015-04-23 09:32:48 -05005350 cmd->host_scribble = (unsigned char *) c;
5351
5352 if (dev->offload_enabled) {
5353 hpsa_cmd_init(h, c->cmdindex, c);
5354 c->cmd_type = CMD_SCSI;
5355 c->scsi_cmd = cmd;
5356 rc = hpsa_scsi_ioaccel_raid_map(h, c);
5357 if (rc < 0) /* scsi_dma_map failed. */
5358 rc = SCSI_MLQUEUE_HOST_BUSY;
Joe Handzika3144e02015-04-23 09:32:59 -05005359 } else if (dev->hba_ioaccel_enabled) {
Webb Scales592a0ad2015-04-23 09:32:48 -05005360 hpsa_cmd_init(h, c->cmdindex, c);
5361 c->cmd_type = CMD_SCSI;
5362 c->scsi_cmd = cmd;
5363 rc = hpsa_scsi_ioaccel_direct_map(h, c);
5364 if (rc < 0) /* scsi_dma_map failed. */
5365 rc = SCSI_MLQUEUE_HOST_BUSY;
5366 }
5367 return rc;
5368}
5369
Don Brace080ef1c2015-01-23 16:43:25 -06005370static void hpsa_command_resubmit_worker(struct work_struct *work)
5371{
5372 struct scsi_cmnd *cmd;
5373 struct hpsa_scsi_dev_t *dev;
Webb Scales8a0ff922015-04-23 09:34:11 -05005374 struct CommandList *c = container_of(work, struct CommandList, work);
Don Brace080ef1c2015-01-23 16:43:25 -06005375
5376 cmd = c->scsi_cmd;
5377 dev = cmd->device->hostdata;
5378 if (!dev) {
5379 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05005380 return hpsa_cmd_free_and_done(c->h, c, cmd);
Don Brace080ef1c2015-01-23 16:43:25 -06005381 }
Webb Scalesd604f532015-04-23 09:35:22 -05005382 if (c->reset_pending)
Don Braced2315ce2017-05-04 17:51:16 -05005383 return hpsa_cmd_free_and_done(c->h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05005384 if (c->cmd_type == CMD_IOACCEL2) {
5385 struct ctlr_info *h = c->h;
5386 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5387 int rc;
5388
5389 if (c2->error_data.serv_response ==
5390 IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
5391 rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
5392 if (rc == 0)
5393 return;
5394 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
5395 /*
5396 * If we get here, it means dma mapping failed.
5397 * Try again via scsi mid layer, which will
5398 * then get SCSI_MLQUEUE_HOST_BUSY.
5399 */
5400 cmd->result = DID_IMM_RETRY << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05005401 return hpsa_cmd_free_and_done(h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05005402 }
5403 /* else, fall thru and resubmit down CISS path */
5404 }
5405 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05005406 hpsa_cmd_partial_init(c->h, c->cmdindex, c);
Don Brace080ef1c2015-01-23 16:43:25 -06005407 if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
5408 /*
5409 * If we get here, it means dma mapping failed. Try
5410 * again via scsi mid layer, which will then get
5411 * SCSI_MLQUEUE_HOST_BUSY.
Webb Scales592a0ad2015-04-23 09:32:48 -05005412 *
5413 * hpsa_ciss_submit will have already freed c
5414 * if it encountered a dma mapping failure.
Don Brace080ef1c2015-01-23 16:43:25 -06005415 */
5416 cmd->result = DID_IMM_RETRY << 16;
5417 cmd->scsi_done(cmd);
5418 }
5419}
5420
Stephen Cameron574f05d2015-01-23 16:43:20 -06005421/* Running in struct Scsi_Host->host_lock less mode */
5422static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
5423{
5424 struct ctlr_info *h;
5425 struct hpsa_scsi_dev_t *dev;
5426 unsigned char scsi3addr[8];
5427 struct CommandList *c;
5428 int rc = 0;
5429
5430 /* Get the ptr to our adapter structure out of cmd->host. */
5431 h = sdev_to_hba(cmd->device);
Webb Scales73153fe2015-04-23 09:35:04 -05005432
5433 BUG_ON(cmd->request->tag < 0);
5434
Stephen Cameron574f05d2015-01-23 16:43:20 -06005435 dev = cmd->device->hostdata;
5436 if (!dev) {
Hannes Reinecke1ccde702016-11-18 08:32:47 +01005437 cmd->result = DID_NO_CONNECT << 16;
Don Braceba74fdc2016-04-27 17:14:17 -05005438 cmd->scsi_done(cmd);
5439 return 0;
5440 }
5441
5442 if (dev->removed) {
Stephen Cameron574f05d2015-01-23 16:43:20 -06005443 cmd->result = DID_NO_CONNECT << 16;
5444 cmd->scsi_done(cmd);
5445 return 0;
5446 }
Webb Scales73153fe2015-04-23 09:35:04 -05005447
Stephen Cameron574f05d2015-01-23 16:43:20 -06005448 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
5449
5450 if (unlikely(lockup_detected(h))) {
Webb Scales25163bd2015-04-23 09:32:00 -05005451 cmd->result = DID_NO_CONNECT << 16;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005452 cmd->scsi_done(cmd);
5453 return 0;
5454 }
Webb Scales73153fe2015-04-23 09:35:04 -05005455 c = cmd_tagged_alloc(h, cmd);
Stephen Cameron574f05d2015-01-23 16:43:20 -06005456
Stephen Cameron407863c2015-01-23 16:44:19 -06005457 /*
5458 * Call alternate submit routine for I/O accelerated commands.
Stephen Cameron574f05d2015-01-23 16:43:20 -06005459 * Retries always go down the normal I/O path.
5460 */
5461 if (likely(cmd->retries == 0 &&
Christoph Hellwig57292b52017-01-31 16:57:29 +01005462 !blk_rq_is_passthrough(cmd->request) &&
5463 h->acciopath_status)) {
Webb Scales592a0ad2015-04-23 09:32:48 -05005464 rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
5465 if (rc == 0)
5466 return 0;
5467 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
Webb Scales73153fe2015-04-23 09:35:04 -05005468 hpsa_cmd_resolve_and_free(h, c);
Webb Scales592a0ad2015-04-23 09:32:48 -05005469 return SCSI_MLQUEUE_HOST_BUSY;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005470 }
5471 }
5472 return hpsa_ciss_submit(h, c, cmd, scsi3addr);
5473}
5474
Webb Scales8ebc9242015-01-23 16:44:50 -06005475static void hpsa_scan_complete(struct ctlr_info *h)
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005476{
5477 unsigned long flags;
5478
Webb Scales8ebc9242015-01-23 16:44:50 -06005479 spin_lock_irqsave(&h->scan_lock, flags);
5480 h->scan_finished = 1;
Don Brace87b9e6a2017-03-10 14:35:17 -06005481 wake_up(&h->scan_wait_queue);
Webb Scales8ebc9242015-01-23 16:44:50 -06005482 spin_unlock_irqrestore(&h->scan_lock, flags);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005483}
5484
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005485static void hpsa_scan_start(struct Scsi_Host *sh)
5486{
5487 struct ctlr_info *h = shost_to_hba(sh);
5488 unsigned long flags;
5489
Webb Scales8ebc9242015-01-23 16:44:50 -06005490 /*
5491 * Don't let rescans be initiated on a controller known to be locked
5492 * up. If the controller locks up *during* a rescan, that thread is
5493 * probably hosed, but at least we can prevent new rescan threads from
5494 * piling up on a locked up controller.
5495 */
5496 if (unlikely(lockup_detected(h)))
5497 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005498
Don Brace87b9e6a2017-03-10 14:35:17 -06005499 /*
5500 * If a scan is already waiting to run, no need to add another
5501 */
5502 spin_lock_irqsave(&h->scan_lock, flags);
5503 if (h->scan_waiting) {
5504 spin_unlock_irqrestore(&h->scan_lock, flags);
5505 return;
5506 }
5507
5508 spin_unlock_irqrestore(&h->scan_lock, flags);
5509
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005510 /* wait until any scan already in progress is finished. */
5511 while (1) {
5512 spin_lock_irqsave(&h->scan_lock, flags);
5513 if (h->scan_finished)
5514 break;
Don Brace87b9e6a2017-03-10 14:35:17 -06005515 h->scan_waiting = 1;
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005516 spin_unlock_irqrestore(&h->scan_lock, flags);
5517 wait_event(h->scan_wait_queue, h->scan_finished);
5518 /* Note: We don't need to worry about a race between this
5519 * thread and driver unload because the midlayer will
5520 * have incremented the reference count, so unload won't
5521 * happen if we're in here.
5522 */
5523 }
5524 h->scan_finished = 0; /* mark scan as in progress */
Don Brace87b9e6a2017-03-10 14:35:17 -06005525 h->scan_waiting = 0;
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005526 spin_unlock_irqrestore(&h->scan_lock, flags);
5527
Webb Scales8ebc9242015-01-23 16:44:50 -06005528 if (unlikely(lockup_detected(h)))
5529 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005530
Don Bracebfd75462016-11-15 14:45:32 -06005531 /*
5532 * Do the scan after a reset completion
5533 */
Don Bracec59d04f2017-05-04 17:51:22 -05005534 spin_lock_irqsave(&h->reset_lock, flags);
Don Bracebfd75462016-11-15 14:45:32 -06005535 if (h->reset_in_progress) {
5536 h->drv_req_rescan = 1;
Don Bracec59d04f2017-05-04 17:51:22 -05005537 spin_unlock_irqrestore(&h->reset_lock, flags);
Don Brace3b476aa22017-05-04 17:51:10 -05005538 hpsa_scan_complete(h);
Don Bracebfd75462016-11-15 14:45:32 -06005539 return;
5540 }
Don Bracec59d04f2017-05-04 17:51:22 -05005541 spin_unlock_irqrestore(&h->reset_lock, flags);
Don Bracebfd75462016-11-15 14:45:32 -06005542
Don Brace8aa60682015-11-04 15:50:01 -06005543 hpsa_update_scsi_devices(h);
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005544
Webb Scales8ebc9242015-01-23 16:44:50 -06005545 hpsa_scan_complete(h);
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005546}
5547
Don Brace7c0a0222015-01-23 16:41:30 -06005548static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
5549{
Don Brace03383732015-01-23 16:43:30 -06005550 struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
5551
5552 if (!logical_drive)
5553 return -ENODEV;
Don Brace7c0a0222015-01-23 16:41:30 -06005554
5555 if (qdepth < 1)
5556 qdepth = 1;
Don Brace03383732015-01-23 16:43:30 -06005557 else if (qdepth > logical_drive->queue_depth)
5558 qdepth = logical_drive->queue_depth;
5559
5560 return scsi_change_queue_depth(sdev, qdepth);
Don Brace7c0a0222015-01-23 16:41:30 -06005561}
5562
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005563static int hpsa_scan_finished(struct Scsi_Host *sh,
5564 unsigned long elapsed_time)
5565{
5566 struct ctlr_info *h = shost_to_hba(sh);
5567 unsigned long flags;
5568 int finished;
5569
5570 spin_lock_irqsave(&h->scan_lock, flags);
5571 finished = h->scan_finished;
5572 spin_unlock_irqrestore(&h->scan_lock, flags);
5573 return finished;
5574}
5575
Robert Elliott2946e822015-04-23 09:35:09 -05005576static int hpsa_scsi_host_alloc(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005577{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005578 struct Scsi_Host *sh;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005579
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005580 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
Robert Elliott2946e822015-04-23 09:35:09 -05005581 if (sh == NULL) {
5582 dev_err(&h->pdev->dev, "scsi_host_alloc failed\n");
5583 return -ENOMEM;
5584 }
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005585
5586 sh->io_port = 0;
5587 sh->n_io_port = 0;
5588 sh->this_id = -1;
5589 sh->max_channel = 3;
5590 sh->max_cmd_len = MAX_COMMAND_SIZE;
5591 sh->max_lun = HPSA_MAX_LUN;
5592 sh->max_id = HPSA_MAX_LUN;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05005593 sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
Don Brace03383732015-01-23 16:43:30 -06005594 sh->cmd_per_lun = sh->can_queue;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005595 sh->sg_tablesize = h->maxsgentries;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06005596 sh->transportt = hpsa_sas_transport_template;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005597 sh->hostdata[0] = (unsigned long) h;
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08005598 sh->irq = pci_irq_vector(h->pdev, 0);
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005599 sh->unique_id = sh->irq;
Christoph Hellwig64d513a2015-10-08 09:28:04 +01005600
Robert Elliott2946e822015-04-23 09:35:09 -05005601 h->scsi_host = sh;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005602 return 0;
Robert Elliott2946e822015-04-23 09:35:09 -05005603}
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005604
Robert Elliott2946e822015-04-23 09:35:09 -05005605static int hpsa_scsi_add_host(struct ctlr_info *h)
5606{
5607 int rv;
5608
5609 rv = scsi_add_host(h->scsi_host, &h->pdev->dev);
5610 if (rv) {
5611 dev_err(&h->pdev->dev, "scsi_add_host failed\n");
5612 return rv;
5613 }
5614 scsi_scan_host(h->scsi_host);
5615 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005616}
5617
Webb Scalesb69324f2015-04-23 09:34:22 -05005618/*
Webb Scales73153fe2015-04-23 09:35:04 -05005619 * The block layer has already gone to the trouble of picking out a unique,
5620 * small-integer tag for this request. We use an offset from that value as
5621 * an index to select our command block. (The offset allows us to reserve the
5622 * low-numbered entries for our own uses.)
5623 */
5624static int hpsa_get_cmd_index(struct scsi_cmnd *scmd)
5625{
5626 int idx = scmd->request->tag;
5627
5628 if (idx < 0)
5629 return idx;
5630
5631 /* Offset to leave space for internal cmds. */
5632 return idx += HPSA_NRESERVED_CMDS;
5633}
5634
5635/*
Webb Scalesb69324f2015-04-23 09:34:22 -05005636 * Send a TEST_UNIT_READY command to the specified LUN using the specified
5637 * reply queue; returns zero if the unit is ready, and non-zero otherwise.
5638 */
5639static int hpsa_send_test_unit_ready(struct ctlr_info *h,
5640 struct CommandList *c, unsigned char lunaddr[],
5641 int reply_queue)
5642{
5643 int rc;
5644
5645 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
5646 (void) fill_cmd(c, TEST_UNIT_READY, h,
5647 NULL, 0, 0, lunaddr, TYPE_CMD);
Don Bracec448ecf2016-04-27 17:13:51 -05005648 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
Webb Scalesb69324f2015-04-23 09:34:22 -05005649 if (rc)
5650 return rc;
5651 /* no unmap needed here because no data xfer. */
5652
5653 /* Check if the unit is already ready. */
5654 if (c->err_info->CommandStatus == CMD_SUCCESS)
5655 return 0;
5656
5657 /*
5658 * The first command sent after reset will receive "unit attention" to
5659 * indicate that the LUN has been reset...this is actually what we're
5660 * looking for (but, success is good too).
5661 */
5662 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5663 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
5664 (c->err_info->SenseInfo[2] == NO_SENSE ||
5665 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
5666 return 0;
5667
5668 return 1;
5669}
5670
5671/*
5672 * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
5673 * returns zero when the unit is ready, and non-zero when giving up.
5674 */
5675static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
5676 struct CommandList *c,
5677 unsigned char lunaddr[], int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005678{
Tomas Henzl89193582014-02-21 16:25:05 -06005679 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005680 int count = 0;
5681 int waittime = 1; /* seconds */
Webb Scalesb69324f2015-04-23 09:34:22 -05005682
5683 /* Send test unit ready until device ready, or give up. */
5684 for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
5685
5686 /*
5687 * Wait for a bit. do this first, because if we send
5688 * the TUR right away, the reset will just abort it.
5689 */
5690 msleep(1000 * waittime);
5691
5692 rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
5693 if (!rc)
5694 break;
5695
5696 /* Increase wait time with each try, up to a point. */
5697 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
5698 waittime *= 2;
5699
5700 dev_warn(&h->pdev->dev,
5701 "waiting %d secs for device to become ready.\n",
5702 waittime);
5703 }
5704
5705 return rc;
5706}
5707
5708static int wait_for_device_to_become_ready(struct ctlr_info *h,
5709 unsigned char lunaddr[],
5710 int reply_queue)
5711{
5712 int first_queue;
5713 int last_queue;
5714 int rq;
5715 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005716 struct CommandList *c;
5717
Stephen Cameron45fcb862015-01-23 16:43:04 -06005718 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005719
Webb Scalesb69324f2015-04-23 09:34:22 -05005720 /*
5721 * If no specific reply queue was requested, then send the TUR
5722 * repeatedly, requesting a reply on each reply queue; otherwise execute
5723 * the loop exactly once using only the specified queue.
5724 */
5725 if (reply_queue == DEFAULT_REPLY_QUEUE) {
5726 first_queue = 0;
5727 last_queue = h->nreply_queues - 1;
5728 } else {
5729 first_queue = reply_queue;
5730 last_queue = reply_queue;
5731 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005732
Webb Scalesb69324f2015-04-23 09:34:22 -05005733 for (rq = first_queue; rq <= last_queue; rq++) {
5734 rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
Webb Scales25163bd2015-04-23 09:32:00 -05005735 if (rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005736 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005737 }
5738
5739 if (rc)
5740 dev_warn(&h->pdev->dev, "giving up on device.\n");
5741 else
5742 dev_warn(&h->pdev->dev, "device is ready.\n");
5743
Stephen Cameron45fcb862015-01-23 16:43:04 -06005744 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005745 return rc;
5746}
5747
5748/* Need at least one of these error handlers to keep ../scsi/hosts.c from
5749 * complaining. Doing a host- or bus-reset can't do anything good here.
5750 */
5751static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
5752{
Don Bracec59d04f2017-05-04 17:51:22 -05005753 int rc = SUCCESS;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005754 struct ctlr_info *h;
5755 struct hpsa_scsi_dev_t *dev;
Scott Teel0b9b7b62015-11-04 15:51:02 -06005756 u8 reset_type;
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005757 char msg[48];
Don Bracec59d04f2017-05-04 17:51:22 -05005758 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005759
5760 /* find the controller to which the command to be aborted was sent */
5761 h = sdev_to_hba(scsicmd->device);
5762 if (h == NULL) /* paranoia */
5763 return FAILED;
Don Bracee3458932015-01-23 16:44:24 -06005764
Don Bracec59d04f2017-05-04 17:51:22 -05005765 spin_lock_irqsave(&h->reset_lock, flags);
5766 h->reset_in_progress = 1;
5767 spin_unlock_irqrestore(&h->reset_lock, flags);
5768
5769 if (lockup_detected(h)) {
5770 rc = FAILED;
5771 goto return_reset_status;
5772 }
Don Bracee3458932015-01-23 16:44:24 -06005773
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005774 dev = scsicmd->device->hostdata;
5775 if (!dev) {
Webb Scalesd604f532015-04-23 09:35:22 -05005776 dev_err(&h->pdev->dev, "%s: device lookup failed\n", __func__);
Don Bracec59d04f2017-05-04 17:51:22 -05005777 rc = FAILED;
5778 goto return_reset_status;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005779 }
Webb Scales25163bd2015-04-23 09:32:00 -05005780
Don Bracec59d04f2017-05-04 17:51:22 -05005781 if (dev->devtype == TYPE_ENCLOSURE) {
5782 rc = SUCCESS;
5783 goto return_reset_status;
5784 }
Don Braceef8a5202017-05-04 17:51:04 -05005785
Webb Scales25163bd2015-04-23 09:32:00 -05005786 /* if controller locked up, we can guarantee command won't complete */
5787 if (lockup_detected(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005788 snprintf(msg, sizeof(msg),
5789 "cmd %d RESET FAILED, lockup detected",
5790 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005791 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Don Bracec59d04f2017-05-04 17:51:22 -05005792 rc = FAILED;
5793 goto return_reset_status;
Webb Scales25163bd2015-04-23 09:32:00 -05005794 }
5795
5796 /* this reset request might be the result of a lockup; check */
5797 if (detect_controller_lockup(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005798 snprintf(msg, sizeof(msg),
5799 "cmd %d RESET FAILED, new lockup detected",
5800 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005801 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Don Bracec59d04f2017-05-04 17:51:22 -05005802 rc = FAILED;
5803 goto return_reset_status;
Webb Scales25163bd2015-04-23 09:32:00 -05005804 }
5805
Webb Scalesd604f532015-04-23 09:35:22 -05005806 /* Do not attempt on controller */
Don Bracec59d04f2017-05-04 17:51:22 -05005807 if (is_hba_lunid(dev->scsi3addr)) {
5808 rc = SUCCESS;
5809 goto return_reset_status;
5810 }
Webb Scalesd604f532015-04-23 09:35:22 -05005811
Scott Teel0b9b7b62015-11-04 15:51:02 -06005812 if (is_logical_dev_addr_mode(dev->scsi3addr))
5813 reset_type = HPSA_DEVICE_RESET_MSG;
5814 else
5815 reset_type = HPSA_PHYS_TARGET_RESET;
5816
5817 sprintf(msg, "resetting %s",
5818 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ");
5819 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005820
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005821 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teel0b9b7b62015-11-04 15:51:02 -06005822 rc = hpsa_do_reset(h, dev, dev->scsi3addr, reset_type,
Webb Scalesd604f532015-04-23 09:35:22 -05005823 DEFAULT_REPLY_QUEUE);
Don Bracec59d04f2017-05-04 17:51:22 -05005824 if (rc == 0)
5825 rc = SUCCESS;
5826 else
5827 rc = FAILED;
5828
Scott Teel0b9b7b62015-11-04 15:51:02 -06005829 sprintf(msg, "reset %s %s",
5830 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ",
Don Bracec59d04f2017-05-04 17:51:22 -05005831 rc == SUCCESS ? "completed successfully" : "failed");
Webb Scalesd604f532015-04-23 09:35:22 -05005832 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Don Bracec59d04f2017-05-04 17:51:22 -05005833
5834return_reset_status:
5835 spin_lock_irqsave(&h->reset_lock, flags);
Don Braceda03ded2015-11-04 15:50:56 -06005836 h->reset_in_progress = 0;
Don Bracec59d04f2017-05-04 17:51:22 -05005837 spin_unlock_irqrestore(&h->reset_lock, flags);
5838 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005839}
5840
5841/*
Webb Scales73153fe2015-04-23 09:35:04 -05005842 * For operations with an associated SCSI command, a command block is allocated
5843 * at init, and managed by cmd_tagged_alloc() and cmd_tagged_free() using the
5844 * block request tag as an index into a table of entries. cmd_tagged_free() is
5845 * the complement, although cmd_free() may be called instead.
5846 */
5847static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
5848 struct scsi_cmnd *scmd)
5849{
5850 int idx = hpsa_get_cmd_index(scmd);
5851 struct CommandList *c = h->cmd_pool + idx;
5852
5853 if (idx < HPSA_NRESERVED_CMDS || idx >= h->nr_cmds) {
5854 dev_err(&h->pdev->dev, "Bad block tag: %d not in [%d..%d]\n",
5855 idx, HPSA_NRESERVED_CMDS, h->nr_cmds - 1);
5856 /* The index value comes from the block layer, so if it's out of
5857 * bounds, it's probably not our bug.
5858 */
5859 BUG();
5860 }
5861
5862 atomic_inc(&c->refcount);
5863 if (unlikely(!hpsa_is_cmd_idle(c))) {
5864 /*
5865 * We expect that the SCSI layer will hand us a unique tag
5866 * value. Thus, there should never be a collision here between
5867 * two requests...because if the selected command isn't idle
5868 * then someone is going to be very disappointed.
5869 */
5870 dev_err(&h->pdev->dev,
5871 "tag collision (tag=%d) in cmd_tagged_alloc().\n",
5872 idx);
5873 if (c->scsi_cmd != NULL)
5874 scsi_print_command(c->scsi_cmd);
5875 scsi_print_command(scmd);
5876 }
5877
5878 hpsa_cmd_partial_init(h, idx, c);
5879 return c;
5880}
5881
5882static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c)
5883{
5884 /*
5885 * Release our reference to the block. We don't need to do anything
Don Brace08ec46f2017-05-04 17:51:49 -05005886 * else to free it, because it is accessed by index.
Webb Scales73153fe2015-04-23 09:35:04 -05005887 */
5888 (void)atomic_dec(&c->refcount);
5889}
5890
5891/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005892 * For operations that cannot sleep, a command block is allocated at init,
5893 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
5894 * which ones are free or in use. Lock must be held when calling this.
5895 * cmd_free() is the complement.
Robert Elliottbf43caf2015-04-23 09:33:38 -05005896 * This function never gives up and returns NULL. If it hangs,
5897 * another thread must call cmd_free() to free some tags.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005898 */
Webb Scales281a7fd2015-01-23 16:43:35 -06005899
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005900static struct CommandList *cmd_alloc(struct ctlr_info *h)
5901{
5902 struct CommandList *c;
Stephen Cameron360c73b2015-04-23 09:32:32 -05005903 int refcount, i;
Webb Scales73153fe2015-04-23 09:35:04 -05005904 int offset = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005905
Robert Elliott33811022015-01-23 16:43:41 -06005906 /*
5907 * There is some *extremely* small but non-zero chance that that
Stephen M. Cameron4c413122014-11-14 17:27:29 -06005908 * multiple threads could get in here, and one thread could
5909 * be scanning through the list of bits looking for a free
5910 * one, but the free ones are always behind him, and other
5911 * threads sneak in behind him and eat them before he can
5912 * get to them, so that while there is always a free one, a
5913 * very unlucky thread might be starved anyway, never able to
5914 * beat the other threads. In reality, this happens so
5915 * infrequently as to be indistinguishable from never.
Webb Scales73153fe2015-04-23 09:35:04 -05005916 *
5917 * Note that we start allocating commands before the SCSI host structure
5918 * is initialized. Since the search starts at bit zero, this
5919 * all works, since we have at least one command structure available;
5920 * however, it means that the structures with the low indexes have to be
5921 * reserved for driver-initiated requests, while requests from the block
5922 * layer will use the higher indexes.
Stephen M. Cameron4c413122014-11-14 17:27:29 -06005923 */
5924
Webb Scales281a7fd2015-01-23 16:43:35 -06005925 for (;;) {
Webb Scales73153fe2015-04-23 09:35:04 -05005926 i = find_next_zero_bit(h->cmd_pool_bits,
5927 HPSA_NRESERVED_CMDS,
5928 offset);
5929 if (unlikely(i >= HPSA_NRESERVED_CMDS)) {
Webb Scales281a7fd2015-01-23 16:43:35 -06005930 offset = 0;
5931 continue;
5932 }
5933 c = h->cmd_pool + i;
5934 refcount = atomic_inc_return(&c->refcount);
5935 if (unlikely(refcount > 1)) {
5936 cmd_free(h, c); /* already in use */
Webb Scales73153fe2015-04-23 09:35:04 -05005937 offset = (i + 1) % HPSA_NRESERVED_CMDS;
Webb Scales281a7fd2015-01-23 16:43:35 -06005938 continue;
5939 }
5940 set_bit(i & (BITS_PER_LONG - 1),
5941 h->cmd_pool_bits + (i / BITS_PER_LONG));
5942 break; /* it's ours now. */
5943 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05005944 hpsa_cmd_partial_init(h, i, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005945 return c;
5946}
5947
Webb Scales73153fe2015-04-23 09:35:04 -05005948/*
5949 * This is the complementary operation to cmd_alloc(). Note, however, in some
5950 * corner cases it may also be used to free blocks allocated by
5951 * cmd_tagged_alloc() in which case the ref-count decrement does the trick and
5952 * the clear-bit is harmless.
5953 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005954static void cmd_free(struct ctlr_info *h, struct CommandList *c)
5955{
Webb Scales281a7fd2015-01-23 16:43:35 -06005956 if (atomic_dec_and_test(&c->refcount)) {
5957 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005958
Webb Scales281a7fd2015-01-23 16:43:35 -06005959 i = c - h->cmd_pool;
5960 clear_bit(i & (BITS_PER_LONG - 1),
5961 h->cmd_pool_bits + (i / BITS_PER_LONG));
5962 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005963}
5964
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005965#ifdef CONFIG_COMPAT
5966
Don Brace42a91642014-11-14 17:26:27 -06005967static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
5968 void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005969{
5970 IOCTL32_Command_struct __user *arg32 =
5971 (IOCTL32_Command_struct __user *) arg;
5972 IOCTL_Command_struct arg64;
5973 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
5974 int err;
5975 u32 cp;
5976
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06005977 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005978 err = 0;
5979 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5980 sizeof(arg64.LUN_info));
5981 err |= copy_from_user(&arg64.Request, &arg32->Request,
5982 sizeof(arg64.Request));
5983 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5984 sizeof(arg64.error_info));
5985 err |= get_user(arg64.buf_size, &arg32->buf_size);
5986 err |= get_user(cp, &arg32->buf);
5987 arg64.buf = compat_ptr(cp);
5988 err |= copy_to_user(p, &arg64, sizeof(arg64));
5989
5990 if (err)
5991 return -EFAULT;
5992
Don Brace42a91642014-11-14 17:26:27 -06005993 err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005994 if (err)
5995 return err;
5996 err |= copy_in_user(&arg32->error_info, &p->error_info,
5997 sizeof(arg32->error_info));
5998 if (err)
5999 return -EFAULT;
6000 return err;
6001}
6002
6003static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
Don Brace42a91642014-11-14 17:26:27 -06006004 int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006005{
6006 BIG_IOCTL32_Command_struct __user *arg32 =
6007 (BIG_IOCTL32_Command_struct __user *) arg;
6008 BIG_IOCTL_Command_struct arg64;
6009 BIG_IOCTL_Command_struct __user *p =
6010 compat_alloc_user_space(sizeof(arg64));
6011 int err;
6012 u32 cp;
6013
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06006014 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006015 err = 0;
6016 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
6017 sizeof(arg64.LUN_info));
6018 err |= copy_from_user(&arg64.Request, &arg32->Request,
6019 sizeof(arg64.Request));
6020 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
6021 sizeof(arg64.error_info));
6022 err |= get_user(arg64.buf_size, &arg32->buf_size);
6023 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
6024 err |= get_user(cp, &arg32->buf);
6025 arg64.buf = compat_ptr(cp);
6026 err |= copy_to_user(p, &arg64, sizeof(arg64));
6027
6028 if (err)
6029 return -EFAULT;
6030
Don Brace42a91642014-11-14 17:26:27 -06006031 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006032 if (err)
6033 return err;
6034 err |= copy_in_user(&arg32->error_info, &p->error_info,
6035 sizeof(arg32->error_info));
6036 if (err)
6037 return -EFAULT;
6038 return err;
6039}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06006040
Don Brace42a91642014-11-14 17:26:27 -06006041static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06006042{
6043 switch (cmd) {
6044 case CCISS_GETPCIINFO:
6045 case CCISS_GETINTINFO:
6046 case CCISS_SETINTINFO:
6047 case CCISS_GETNODENAME:
6048 case CCISS_SETNODENAME:
6049 case CCISS_GETHEARTBEAT:
6050 case CCISS_GETBUSTYPES:
6051 case CCISS_GETFIRMVER:
6052 case CCISS_GETDRIVVER:
6053 case CCISS_REVALIDVOLS:
6054 case CCISS_DEREGDISK:
6055 case CCISS_REGNEWDISK:
6056 case CCISS_REGNEWD:
6057 case CCISS_RESCANDISK:
6058 case CCISS_GETLUNINFO:
6059 return hpsa_ioctl(dev, cmd, arg);
6060
6061 case CCISS_PASSTHRU32:
6062 return hpsa_ioctl32_passthru(dev, cmd, arg);
6063 case CCISS_BIG_PASSTHRU32:
6064 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
6065
6066 default:
6067 return -ENOIOCTLCMD;
6068 }
6069}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006070#endif
6071
6072static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
6073{
6074 struct hpsa_pci_info pciinfo;
6075
6076 if (!argp)
6077 return -EINVAL;
6078 pciinfo.domain = pci_domain_nr(h->pdev->bus);
6079 pciinfo.bus = h->pdev->bus->number;
6080 pciinfo.dev_fn = h->pdev->devfn;
6081 pciinfo.board_id = h->board_id;
6082 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
6083 return -EFAULT;
6084 return 0;
6085}
6086
6087static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
6088{
6089 DriverVer_type DriverVer;
6090 unsigned char vmaj, vmin, vsubmin;
6091 int rc;
6092
6093 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
6094 &vmaj, &vmin, &vsubmin);
6095 if (rc != 3) {
6096 dev_info(&h->pdev->dev, "driver version string '%s' "
6097 "unrecognized.", HPSA_DRIVER_VERSION);
6098 vmaj = 0;
6099 vmin = 0;
6100 vsubmin = 0;
6101 }
6102 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
6103 if (!argp)
6104 return -EINVAL;
6105 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
6106 return -EFAULT;
6107 return 0;
6108}
6109
6110static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6111{
6112 IOCTL_Command_struct iocommand;
6113 struct CommandList *c;
6114 char *buff = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006115 u64 temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006116 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006117
6118 if (!argp)
6119 return -EINVAL;
6120 if (!capable(CAP_SYS_RAWIO))
6121 return -EPERM;
6122 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
6123 return -EFAULT;
6124 if ((iocommand.buf_size < 1) &&
6125 (iocommand.Request.Type.Direction != XFER_NONE)) {
6126 return -EINVAL;
6127 }
6128 if (iocommand.buf_size > 0) {
6129 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
6130 if (buff == NULL)
Robert Elliott2dd02d72015-04-23 09:33:43 -05006131 return -ENOMEM;
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006132 if (iocommand.Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006133 /* Copy the data into the buffer we created */
6134 if (copy_from_user(buff, iocommand.buf,
6135 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006136 rc = -EFAULT;
6137 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006138 }
6139 } else {
6140 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006141 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006142 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006143 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006144
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006145 /* Fill in the command type */
6146 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006147 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006148 /* Fill in Command Header */
6149 c->Header.ReplyQueue = 0; /* unused in simple mode */
6150 if (iocommand.buf_size > 0) { /* buffer to fill */
6151 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006152 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006153 } else { /* no buffers to fill */
6154 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006155 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006156 }
6157 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006158
6159 /* Fill in Request block */
6160 memcpy(&c->Request, &iocommand.Request,
6161 sizeof(c->Request));
6162
6163 /* Fill in the scatter gather information */
6164 if (iocommand.buf_size > 0) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006165 temp64 = pci_map_single(h->pdev, buff,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006166 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006167 if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
6168 c->SG[0].Addr = cpu_to_le64(0);
6169 c->SG[0].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006170 rc = -ENOMEM;
6171 goto out;
6172 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006173 c->SG[0].Addr = cpu_to_le64(temp64);
6174 c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
6175 c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006176 }
Don Bracec448ecf2016-04-27 17:13:51 -05006177 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
Don Brace3fb134c2016-07-01 13:37:38 -05006178 NO_TIMEOUT);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05006179 if (iocommand.buf_size > 0)
6180 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006181 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006182 if (rc) {
6183 rc = -EIO;
6184 goto out;
6185 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006186
6187 /* Copy the error information out */
6188 memcpy(&iocommand.error_info, c->err_info,
6189 sizeof(iocommand.error_info));
6190 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006191 rc = -EFAULT;
6192 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006193 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006194 if ((iocommand.Request.Type.Direction & XFER_READ) &&
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006195 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006196 /* Copy the data out of the buffer we created */
6197 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006198 rc = -EFAULT;
6199 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006200 }
6201 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006202out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006203 cmd_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006204out_kfree:
6205 kfree(buff);
6206 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006207}
6208
6209static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6210{
6211 BIG_IOCTL_Command_struct *ioc;
6212 struct CommandList *c;
6213 unsigned char **buff = NULL;
6214 int *buff_size = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006215 u64 temp64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006216 BYTE sg_used = 0;
6217 int status = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06006218 u32 left;
6219 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006220 BYTE __user *data_ptr;
6221
6222 if (!argp)
6223 return -EINVAL;
6224 if (!capable(CAP_SYS_RAWIO))
6225 return -EPERM;
Javier Martinez Canillas19be606b2016-10-13 13:10:08 -03006226 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006227 if (!ioc) {
6228 status = -ENOMEM;
6229 goto cleanup1;
6230 }
6231 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
6232 status = -EFAULT;
6233 goto cleanup1;
6234 }
6235 if ((ioc->buf_size < 1) &&
6236 (ioc->Request.Type.Direction != XFER_NONE)) {
6237 status = -EINVAL;
6238 goto cleanup1;
6239 }
6240 /* Check kmalloc limits using all SGs */
6241 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
6242 status = -EINVAL;
6243 goto cleanup1;
6244 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006245 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006246 status = -EINVAL;
6247 goto cleanup1;
6248 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006249 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006250 if (!buff) {
6251 status = -ENOMEM;
6252 goto cleanup1;
6253 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006254 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006255 if (!buff_size) {
6256 status = -ENOMEM;
6257 goto cleanup1;
6258 }
6259 left = ioc->buf_size;
6260 data_ptr = ioc->buf;
6261 while (left) {
6262 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
6263 buff_size[sg_used] = sz;
6264 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
6265 if (buff[sg_used] == NULL) {
6266 status = -ENOMEM;
6267 goto cleanup1;
6268 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006269 if (ioc->Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006270 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
Stephen M. Cameron0758f4f2014-07-03 10:18:03 -05006271 status = -EFAULT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006272 goto cleanup1;
6273 }
6274 } else
6275 memset(buff[sg_used], 0, sz);
6276 left -= sz;
6277 data_ptr += sz;
6278 sg_used++;
6279 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006280 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006281
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006282 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006283 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006284 c->Header.ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006285 c->Header.SGList = (u8) sg_used;
6286 c->Header.SGTotal = cpu_to_le16(sg_used);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006287 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006288 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
6289 if (ioc->buf_size > 0) {
6290 int i;
6291 for (i = 0; i < sg_used; i++) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006292 temp64 = pci_map_single(h->pdev, buff[i],
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006293 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006294 if (dma_mapping_error(&h->pdev->dev,
6295 (dma_addr_t) temp64)) {
6296 c->SG[i].Addr = cpu_to_le64(0);
6297 c->SG[i].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006298 hpsa_pci_unmap(h->pdev, c, i,
6299 PCI_DMA_BIDIRECTIONAL);
6300 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006301 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006302 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006303 c->SG[i].Addr = cpu_to_le64(temp64);
6304 c->SG[i].Len = cpu_to_le32(buff_size[i]);
6305 c->SG[i].Ext = cpu_to_le32(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006306 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006307 c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006308 }
Don Bracec448ecf2016-04-27 17:13:51 -05006309 status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
Don Brace3fb134c2016-07-01 13:37:38 -05006310 NO_TIMEOUT);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006311 if (sg_used)
6312 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006313 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006314 if (status) {
6315 status = -EIO;
6316 goto cleanup0;
6317 }
6318
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006319 /* Copy the error information out */
6320 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
6321 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006322 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006323 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006324 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006325 if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006326 int i;
6327
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006328 /* Copy the data out of the buffer we created */
6329 BYTE __user *ptr = ioc->buf;
6330 for (i = 0; i < sg_used; i++) {
6331 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006332 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006333 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006334 }
6335 ptr += buff_size[i];
6336 }
6337 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006338 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006339cleanup0:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006340 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006341cleanup1:
6342 if (buff) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006343 int i;
6344
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006345 for (i = 0; i < sg_used; i++)
6346 kfree(buff[i]);
6347 kfree(buff);
6348 }
6349 kfree(buff_size);
6350 kfree(ioc);
6351 return status;
6352}
6353
6354static void check_ioctl_unit_attention(struct ctlr_info *h,
6355 struct CommandList *c)
6356{
6357 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
6358 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
6359 (void) check_for_unit_attention(h, c);
6360}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006361
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006362/*
6363 * ioctl
6364 */
Don Brace42a91642014-11-14 17:26:27 -06006365static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006366{
6367 struct ctlr_info *h;
6368 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006369 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006370
6371 h = sdev_to_hba(dev);
6372
6373 switch (cmd) {
6374 case CCISS_DEREGDISK:
6375 case CCISS_REGNEWDISK:
6376 case CCISS_REGNEWD:
Stephen M. Camerona08a84712010-02-04 08:43:16 -06006377 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006378 return 0;
6379 case CCISS_GETPCIINFO:
6380 return hpsa_getpciinfo_ioctl(h, argp);
6381 case CCISS_GETDRIVVER:
6382 return hpsa_getdrivver_ioctl(h, argp);
6383 case CCISS_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006384 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006385 return -EAGAIN;
6386 rc = hpsa_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006387 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006388 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006389 case CCISS_BIG_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006390 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006391 return -EAGAIN;
6392 rc = hpsa_big_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006393 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006394 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006395 default:
6396 return -ENOTTY;
6397 }
6398}
6399
Robert Elliottbf43caf2015-04-23 09:33:38 -05006400static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006401 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006402{
6403 struct CommandList *c;
6404
6405 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006406
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006407 /* fill_cmd can't fail here, no data buffer to map */
6408 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006409 RAID_CTLR_LUNID, TYPE_MSG);
6410 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
6411 c->waiting = NULL;
6412 enqueue_cmd_and_start_io(h, c);
6413 /* Don't wait for completion, the reset won't complete. Don't free
6414 * the command either. This is the last command we will send before
6415 * re-initializing everything, so it doesn't matter and won't leak.
6416 */
Robert Elliottbf43caf2015-04-23 09:33:38 -05006417 return;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006418}
6419
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006420static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006421 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006422 int cmd_type)
6423{
6424 int pci_dir = XFER_NONE;
6425
6426 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006427 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006428 c->Header.ReplyQueue = 0;
6429 if (buff != NULL && size > 0) {
6430 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006431 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006432 } else {
6433 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006434 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006435 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006436 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
6437
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006438 if (cmd_type == TYPE_CMD) {
6439 switch (cmd) {
6440 case HPSA_INQUIRY:
6441 /* are we trying to read a vital product page */
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006442 if (page_code & VPD_PAGE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006443 c->Request.CDB[1] = 0x01;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006444 c->Request.CDB[2] = (page_code & 0xff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006445 }
6446 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006447 c->Request.type_attr_dir =
6448 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006449 c->Request.Timeout = 0;
6450 c->Request.CDB[0] = HPSA_INQUIRY;
6451 c->Request.CDB[4] = size & 0xFF;
6452 break;
6453 case HPSA_REPORT_LOG:
6454 case HPSA_REPORT_PHYS:
6455 /* Talking to controller so It's a physical command
6456 mode = 00 target = 0. Nothing to write.
6457 */
6458 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006459 c->Request.type_attr_dir =
6460 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006461 c->Request.Timeout = 0;
6462 c->Request.CDB[0] = cmd;
6463 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6464 c->Request.CDB[7] = (size >> 16) & 0xFF;
6465 c->Request.CDB[8] = (size >> 8) & 0xFF;
6466 c->Request.CDB[9] = size & 0xFF;
6467 break;
Scott Teelc2adae42015-11-04 15:52:16 -06006468 case BMIC_SENSE_DIAG_OPTIONS:
6469 c->Request.CDBLen = 16;
6470 c->Request.type_attr_dir =
6471 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6472 c->Request.Timeout = 0;
6473 /* Spec says this should be BMIC_WRITE */
6474 c->Request.CDB[0] = BMIC_READ;
6475 c->Request.CDB[6] = BMIC_SENSE_DIAG_OPTIONS;
6476 break;
6477 case BMIC_SET_DIAG_OPTIONS:
6478 c->Request.CDBLen = 16;
6479 c->Request.type_attr_dir =
6480 TYPE_ATTR_DIR(cmd_type,
6481 ATTR_SIMPLE, XFER_WRITE);
6482 c->Request.Timeout = 0;
6483 c->Request.CDB[0] = BMIC_WRITE;
6484 c->Request.CDB[6] = BMIC_SET_DIAG_OPTIONS;
6485 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006486 case HPSA_CACHE_FLUSH:
6487 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006488 c->Request.type_attr_dir =
6489 TYPE_ATTR_DIR(cmd_type,
6490 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006491 c->Request.Timeout = 0;
6492 c->Request.CDB[0] = BMIC_WRITE;
6493 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05006494 c->Request.CDB[7] = (size >> 8) & 0xFF;
6495 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006496 break;
6497 case TEST_UNIT_READY:
6498 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006499 c->Request.type_attr_dir =
6500 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006501 c->Request.Timeout = 0;
6502 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006503 case HPSA_GET_RAID_MAP:
6504 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006505 c->Request.type_attr_dir =
6506 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006507 c->Request.Timeout = 0;
6508 c->Request.CDB[0] = HPSA_CISS_READ;
6509 c->Request.CDB[1] = cmd;
6510 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6511 c->Request.CDB[7] = (size >> 16) & 0xFF;
6512 c->Request.CDB[8] = (size >> 8) & 0xFF;
6513 c->Request.CDB[9] = size & 0xFF;
6514 break;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006515 case BMIC_SENSE_CONTROLLER_PARAMETERS:
6516 c->Request.CDBLen = 10;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006517 c->Request.type_attr_dir =
6518 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006519 c->Request.Timeout = 0;
6520 c->Request.CDB[0] = BMIC_READ;
6521 c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
6522 c->Request.CDB[7] = (size >> 16) & 0xFF;
6523 c->Request.CDB[8] = (size >> 8) & 0xFF;
6524 break;
Don Brace03383732015-01-23 16:43:30 -06006525 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
6526 c->Request.CDBLen = 10;
6527 c->Request.type_attr_dir =
6528 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6529 c->Request.Timeout = 0;
6530 c->Request.CDB[0] = BMIC_READ;
6531 c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
6532 c->Request.CDB[7] = (size >> 16) & 0xFF;
6533 c->Request.CDB[8] = (size >> 8) & 0XFF;
6534 break;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06006535 case BMIC_SENSE_SUBSYSTEM_INFORMATION:
6536 c->Request.CDBLen = 10;
6537 c->Request.type_attr_dir =
6538 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6539 c->Request.Timeout = 0;
6540 c->Request.CDB[0] = BMIC_READ;
6541 c->Request.CDB[6] = BMIC_SENSE_SUBSYSTEM_INFORMATION;
6542 c->Request.CDB[7] = (size >> 16) & 0xFF;
6543 c->Request.CDB[8] = (size >> 8) & 0XFF;
6544 break;
Don Bracecca8f132015-12-22 10:36:48 -06006545 case BMIC_SENSE_STORAGE_BOX_PARAMS:
6546 c->Request.CDBLen = 10;
6547 c->Request.type_attr_dir =
6548 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6549 c->Request.Timeout = 0;
6550 c->Request.CDB[0] = BMIC_READ;
6551 c->Request.CDB[6] = BMIC_SENSE_STORAGE_BOX_PARAMS;
6552 c->Request.CDB[7] = (size >> 16) & 0xFF;
6553 c->Request.CDB[8] = (size >> 8) & 0XFF;
6554 break;
Scott Teel66749d02015-11-04 15:51:57 -06006555 case BMIC_IDENTIFY_CONTROLLER:
6556 c->Request.CDBLen = 10;
6557 c->Request.type_attr_dir =
6558 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6559 c->Request.Timeout = 0;
6560 c->Request.CDB[0] = BMIC_READ;
6561 c->Request.CDB[1] = 0;
6562 c->Request.CDB[2] = 0;
6563 c->Request.CDB[3] = 0;
6564 c->Request.CDB[4] = 0;
6565 c->Request.CDB[5] = 0;
6566 c->Request.CDB[6] = BMIC_IDENTIFY_CONTROLLER;
6567 c->Request.CDB[7] = (size >> 16) & 0xFF;
6568 c->Request.CDB[8] = (size >> 8) & 0XFF;
6569 c->Request.CDB[9] = 0;
6570 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006571 default:
6572 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
6573 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006574 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006575 }
6576 } else if (cmd_type == TYPE_MSG) {
6577 switch (cmd) {
6578
Scott Teel0b9b7b62015-11-04 15:51:02 -06006579 case HPSA_PHYS_TARGET_RESET:
6580 c->Request.CDBLen = 16;
6581 c->Request.type_attr_dir =
6582 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6583 c->Request.Timeout = 0; /* Don't time out */
6584 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
6585 c->Request.CDB[0] = HPSA_RESET;
6586 c->Request.CDB[1] = HPSA_TARGET_RESET_TYPE;
6587 /* Physical target reset needs no control bytes 4-7*/
6588 c->Request.CDB[4] = 0x00;
6589 c->Request.CDB[5] = 0x00;
6590 c->Request.CDB[6] = 0x00;
6591 c->Request.CDB[7] = 0x00;
6592 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006593 case HPSA_DEVICE_RESET_MSG:
6594 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006595 c->Request.type_attr_dir =
6596 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006597 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006598 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
6599 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05006600 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006601 /* If bytes 4-7 are zero, it means reset the */
6602 /* LunID device */
6603 c->Request.CDB[4] = 0x00;
6604 c->Request.CDB[5] = 0x00;
6605 c->Request.CDB[6] = 0x00;
6606 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006607 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006608 default:
6609 dev_warn(&h->pdev->dev, "unknown message type %d\n",
6610 cmd);
6611 BUG();
6612 }
6613 } else {
6614 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
6615 BUG();
6616 }
6617
Stephen M. Camerona505b862014-11-14 17:27:04 -06006618 switch (GET_DIR(c->Request.type_attr_dir)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006619 case XFER_READ:
6620 pci_dir = PCI_DMA_FROMDEVICE;
6621 break;
6622 case XFER_WRITE:
6623 pci_dir = PCI_DMA_TODEVICE;
6624 break;
6625 case XFER_NONE:
6626 pci_dir = PCI_DMA_NONE;
6627 break;
6628 default:
6629 pci_dir = PCI_DMA_BIDIRECTIONAL;
6630 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006631 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
6632 return -1;
6633 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006634}
6635
6636/*
6637 * Map (physical) PCI mem into (virtual) kernel space
6638 */
6639static void __iomem *remap_pci_mem(ulong base, ulong size)
6640{
6641 ulong page_base = ((ulong) base) & PAGE_MASK;
6642 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba34c2012-07-26 11:34:23 -05006643 void __iomem *page_remapped = ioremap_nocache(page_base,
6644 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006645
6646 return page_remapped ? (page_remapped + page_offs) : NULL;
6647}
6648
Matt Gates254f7962012-05-01 11:43:06 -05006649static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006650{
Matt Gates254f7962012-05-01 11:43:06 -05006651 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006652}
6653
Stephen M. Cameron900c5442010-02-04 08:42:35 -06006654static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006655{
6656 return h->access.intr_pending(h);
6657}
6658
6659static inline long interrupt_not_for_us(struct ctlr_info *h)
6660{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006661 return (h->access.intr_pending(h) == 0) ||
6662 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006663}
6664
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06006665static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
6666 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006667{
6668 if (unlikely(tag_index >= h->nr_cmds)) {
6669 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
6670 return 1;
6671 }
6672 return 0;
6673}
6674
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05006675static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006676{
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006677 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06006678 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
6679 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05006680 complete_scsi_command(c);
Stephen Cameron8be986c2015-04-23 09:34:06 -05006681 else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006682 complete(c->waiting);
Stephen M. Camerona104c992010-02-04 08:42:24 -06006683}
6684
Don Brace303932f2010-02-04 08:42:40 -06006685/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05006686static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06006687 u32 raw_tag)
6688{
6689 u32 tag_index;
6690 struct CommandList *c;
6691
Don Bracef2405db2015-01-23 16:43:09 -06006692 tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05006693 if (!bad_tag(h, tag_index, raw_tag)) {
6694 c = h->cmd_pool + tag_index;
6695 finish_cmd(c);
6696 }
Don Brace303932f2010-02-04 08:42:40 -06006697}
6698
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006699/* Some controllers, like p400, will give us one interrupt
6700 * after a soft reset, even if we turned interrupts off.
6701 * Only need to check for this in the hpsa_xxx_discard_completions
6702 * functions.
6703 */
6704static int ignore_bogus_interrupt(struct ctlr_info *h)
6705{
6706 if (likely(!reset_devices))
6707 return 0;
6708
6709 if (likely(h->interrupts_enabled))
6710 return 0;
6711
6712 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
6713 "(known firmware bug.) Ignoring.\n");
6714
6715 return 1;
6716}
6717
Matt Gates254f7962012-05-01 11:43:06 -05006718/*
6719 * Convert &h->q[x] (passed to interrupt handlers) back to h.
6720 * Relies on (h-q[x] == x) being true for x such that
6721 * 0 <= x < MAX_REPLY_QUEUES.
6722 */
6723static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006724{
Matt Gates254f7962012-05-01 11:43:06 -05006725 return container_of((queue - *queue), struct ctlr_info, q[0]);
6726}
6727
6728static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
6729{
6730 struct ctlr_info *h = queue_to_hba(queue);
6731 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006732 u32 raw_tag;
6733
6734 if (ignore_bogus_interrupt(h))
6735 return IRQ_NONE;
6736
6737 if (interrupt_not_for_us(h))
6738 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006739 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006740 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05006741 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006742 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05006743 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006744 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006745 return IRQ_HANDLED;
6746}
6747
Matt Gates254f7962012-05-01 11:43:06 -05006748static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006749{
Matt Gates254f7962012-05-01 11:43:06 -05006750 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006751 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05006752 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006753
6754 if (ignore_bogus_interrupt(h))
6755 return IRQ_NONE;
6756
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006757 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05006758 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006759 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05006760 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006761 return IRQ_HANDLED;
6762}
6763
Matt Gates254f7962012-05-01 11:43:06 -05006764static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006765{
Matt Gates254f7962012-05-01 11:43:06 -05006766 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06006767 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05006768 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006769
6770 if (interrupt_not_for_us(h))
6771 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006772 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006773 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05006774 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006775 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06006776 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05006777 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006778 }
6779 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006780 return IRQ_HANDLED;
6781}
6782
Matt Gates254f7962012-05-01 11:43:06 -05006783static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006784{
Matt Gates254f7962012-05-01 11:43:06 -05006785 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006786 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05006787 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006788
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006789 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05006790 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06006791 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06006792 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05006793 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006794 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006795 return IRQ_HANDLED;
6796}
6797
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006798/* Send a message CDB to the firmware. Careful, this only works
6799 * in simple mode, not performant mode due to the tag lookup.
6800 * We only ever use this immediately after a controller reset.
6801 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006802static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
6803 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006804{
6805 struct Command {
6806 struct CommandListHeader CommandHeader;
6807 struct RequestBlock Request;
6808 struct ErrDescriptor ErrorDescriptor;
6809 };
6810 struct Command *cmd;
6811 static const size_t cmd_sz = sizeof(*cmd) +
6812 sizeof(cmd->ErrorDescriptor);
6813 dma_addr_t paddr64;
Don Brace2b08b3e2015-01-23 16:41:09 -06006814 __le32 paddr32;
6815 u32 tag;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006816 void __iomem *vaddr;
6817 int i, err;
6818
6819 vaddr = pci_ioremap_bar(pdev, 0);
6820 if (vaddr == NULL)
6821 return -ENOMEM;
6822
6823 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
6824 * CCISS commands, so they must be allocated from the lower 4GiB of
6825 * memory.
6826 */
6827 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
6828 if (err) {
6829 iounmap(vaddr);
Robert Elliott1eaec8f2015-01-23 16:42:37 -06006830 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006831 }
6832
6833 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
6834 if (cmd == NULL) {
6835 iounmap(vaddr);
6836 return -ENOMEM;
6837 }
6838
6839 /* This must fit, because of the 32-bit consistent DMA mask. Also,
6840 * although there's no guarantee, we assume that the address is at
6841 * least 4-byte aligned (most likely, it's page-aligned).
6842 */
Don Brace2b08b3e2015-01-23 16:41:09 -06006843 paddr32 = cpu_to_le32(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006844
6845 cmd->CommandHeader.ReplyQueue = 0;
6846 cmd->CommandHeader.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006847 cmd->CommandHeader.SGTotal = cpu_to_le16(0);
Don Brace2b08b3e2015-01-23 16:41:09 -06006848 cmd->CommandHeader.tag = cpu_to_le64(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006849 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
6850
6851 cmd->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006852 cmd->Request.type_attr_dir =
6853 TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006854 cmd->Request.Timeout = 0; /* Don't time out */
6855 cmd->Request.CDB[0] = opcode;
6856 cmd->Request.CDB[1] = type;
6857 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006858 cmd->ErrorDescriptor.Addr =
Don Brace2b08b3e2015-01-23 16:41:09 -06006859 cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006860 cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006861
Don Brace2b08b3e2015-01-23 16:41:09 -06006862 writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006863
6864 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
6865 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Don Brace2b08b3e2015-01-23 16:41:09 -06006866 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006867 break;
6868 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
6869 }
6870
6871 iounmap(vaddr);
6872
6873 /* we leak the DMA buffer here ... no choice since the controller could
6874 * still complete the command.
6875 */
6876 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
6877 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
6878 opcode, type);
6879 return -ETIMEDOUT;
6880 }
6881
6882 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
6883
6884 if (tag & HPSA_ERROR_BIT) {
6885 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
6886 opcode, type);
6887 return -EIO;
6888 }
6889
6890 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
6891 opcode, type);
6892 return 0;
6893}
6894
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006895#define hpsa_noop(p) hpsa_message(p, 3, 0)
6896
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006897static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Don Brace42a91642014-11-14 17:26:27 -06006898 void __iomem *vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006899{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006900
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006901 if (use_doorbell) {
6902 /* For everything after the P600, the PCI power state method
6903 * of resetting the controller doesn't work, so we have this
6904 * other way using the doorbell register.
6905 */
6906 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05006907 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05006908
Justin Lindley00701a92014-05-29 10:52:47 -05006909 /* PMC hardware guys tell us we need a 10 second delay after
Stephen M. Cameron85009232013-09-23 13:33:36 -05006910 * doorbell reset and before any attempt to talk to the board
6911 * at all to ensure that this actually works and doesn't fall
6912 * over in some weird corner cases.
6913 */
Justin Lindley00701a92014-05-29 10:52:47 -05006914 msleep(10000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006915 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006916
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006917 /* Quoting from the Open CISS Specification: "The Power
6918 * Management Control/Status Register (CSR) controls the power
6919 * state of the device. The normal operating state is D0,
6920 * CSR=00h. The software off state is D3, CSR=03h. To reset
6921 * the controller, place the interface device in D3 then to D0,
6922 * this causes a secondary PCI reset which will reset the
6923 * controller." */
6924
Don Brace2662cab2015-01-23 16:41:25 -06006925 int rc = 0;
6926
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006927 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
Don Brace2662cab2015-01-23 16:41:25 -06006928
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006929 /* enter the D3hot power management state */
Don Brace2662cab2015-01-23 16:41:25 -06006930 rc = pci_set_power_state(pdev, PCI_D3hot);
6931 if (rc)
6932 return rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006933
6934 msleep(500);
6935
6936 /* enter the D0 power management state */
Don Brace2662cab2015-01-23 16:41:25 -06006937 rc = pci_set_power_state(pdev, PCI_D0);
6938 if (rc)
6939 return rc;
Mike Millerc4853ef2011-10-21 08:19:43 +02006940
6941 /*
6942 * The P600 requires a small delay when changing states.
6943 * Otherwise we may think the board did not reset and we bail.
6944 * This for kdump only and is particular to the P600.
6945 */
6946 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006947 }
6948 return 0;
6949}
6950
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006951static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006952{
6953 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006954 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006955}
6956
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006957static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006958{
6959 char *driver_version;
6960 int i, size = sizeof(cfgtable->driver_version);
6961
6962 driver_version = kmalloc(size, GFP_KERNEL);
6963 if (!driver_version)
6964 return -ENOMEM;
6965
6966 init_driver_version(driver_version, size);
6967 for (i = 0; i < size; i++)
6968 writeb(driver_version[i], &cfgtable->driver_version[i]);
6969 kfree(driver_version);
6970 return 0;
6971}
6972
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006973static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
6974 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006975{
6976 int i;
6977
6978 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
6979 driver_ver[i] = readb(&cfgtable->driver_version[i]);
6980}
6981
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006982static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006983{
6984
6985 char *driver_ver, *old_driver_ver;
6986 int rc, size = sizeof(cfgtable->driver_version);
6987
6988 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
6989 if (!old_driver_ver)
6990 return -ENOMEM;
6991 driver_ver = old_driver_ver + size;
6992
6993 /* After a reset, the 32 bytes of "driver version" in the cfgtable
6994 * should have been changed, otherwise we know the reset failed.
6995 */
6996 init_driver_version(old_driver_ver, size);
6997 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
6998 rc = !memcmp(driver_ver, old_driver_ver, size);
6999 kfree(old_driver_ver);
7000 return rc;
7001}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007002/* This does a hard reset of the controller using PCI power management
7003 * states or the using the doorbell register.
7004 */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007005static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007006{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007007 u64 cfg_offset;
7008 u32 cfg_base_addr;
7009 u64 cfg_base_addr_index;
7010 void __iomem *vaddr;
7011 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007012 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007013 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007014 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007015 u32 use_doorbell;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007016 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007017
7018 /* For controllers as old as the P600, this is very nearly
7019 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007020 *
7021 * pci_save_state(pci_dev);
7022 * pci_set_power_state(pci_dev, PCI_D3hot);
7023 * pci_set_power_state(pci_dev, PCI_D0);
7024 * pci_restore_state(pci_dev);
7025 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007026 * For controllers newer than the P600, the pci power state
7027 * method of resetting doesn't work so we have another way
7028 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007029 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05007030
Robert Elliott60f923b2015-01-23 16:42:06 -06007031 if (!ctlr_is_resettable(board_id)) {
7032 dev_warn(&pdev->dev, "Controller not resettable\n");
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06007033 return -ENODEV;
7034 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05007035
7036 /* if controller is soft- but not hard resettable... */
7037 if (!ctlr_is_hard_resettable(board_id))
7038 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05007039
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007040 /* Save the PCI command register */
7041 pci_read_config_word(pdev, 4, &command_register);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007042 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007043
7044 /* find the first memory BAR, so we can find the cfg table */
7045 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
7046 if (rc)
7047 return rc;
7048 vaddr = remap_pci_mem(paddr, 0x250);
7049 if (!vaddr)
7050 return -ENOMEM;
7051
7052 /* find cfgtable in order to check if reset via doorbell is supported */
7053 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
7054 &cfg_base_addr_index, &cfg_offset);
7055 if (rc)
7056 goto unmap_vaddr;
7057 cfgtable = remap_pci_mem(pci_resource_start(pdev,
7058 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
7059 if (!cfgtable) {
7060 rc = -ENOMEM;
7061 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007062 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007063 rc = write_driver_ver_to_cfgtable(cfgtable);
7064 if (rc)
Tomas Henzl03741d92015-01-23 16:41:14 -06007065 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007066
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007067 /* If reset via doorbell register is supported, use that.
7068 * There are two such methods. Favor the newest method.
7069 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007070 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007071 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
7072 if (use_doorbell) {
7073 use_doorbell = DOORBELL_CTLR_RESET2;
7074 } else {
7075 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
7076 if (use_doorbell) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007077 dev_warn(&pdev->dev,
7078 "Soft reset not supported. Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007079 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007080 goto unmap_cfgtable;
7081 }
7082 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007083
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007084 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
7085 if (rc)
7086 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007087
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007088 pci_restore_state(pdev);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007089 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007090
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007091 /* Some devices (notably the HP Smart Array 5i Controller)
7092 need a little pause here */
7093 msleep(HPSA_POST_RESET_PAUSE_MSECS);
7094
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007095 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
7096 if (rc) {
7097 dev_warn(&pdev->dev,
Stephen Cameron050f7142015-01-23 16:42:22 -06007098 "Failed waiting for board to become ready after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007099 goto unmap_cfgtable;
7100 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007101
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007102 rc = controller_reset_failed(vaddr);
7103 if (rc < 0)
7104 goto unmap_cfgtable;
7105 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007106 dev_warn(&pdev->dev, "Unable to successfully reset "
7107 "controller. Will try soft reset.\n");
7108 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007109 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007110 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007111 }
7112
7113unmap_cfgtable:
7114 iounmap(cfgtable);
7115
7116unmap_vaddr:
7117 iounmap(vaddr);
7118 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007119}
7120
7121/*
7122 * We cannot read the structure directly, for portability we must use
7123 * the io functions.
7124 * This is for debug only.
7125 */
Don Brace42a91642014-11-14 17:26:27 -06007126static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007127{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007128#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007129 int i;
7130 char temp_name[17];
7131
7132 dev_info(dev, "Controller Configuration information\n");
7133 dev_info(dev, "------------------------------------\n");
7134 for (i = 0; i < 4; i++)
7135 temp_name[i] = readb(&(tb->Signature[i]));
7136 temp_name[4] = '\0';
7137 dev_info(dev, " Signature = %s\n", temp_name);
7138 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
7139 dev_info(dev, " Transport methods supported = 0x%x\n",
7140 readl(&(tb->TransportSupport)));
7141 dev_info(dev, " Transport methods active = 0x%x\n",
7142 readl(&(tb->TransportActive)));
7143 dev_info(dev, " Requested transport Method = 0x%x\n",
7144 readl(&(tb->HostWrite.TransportRequest)));
7145 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
7146 readl(&(tb->HostWrite.CoalIntDelay)));
7147 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
7148 readl(&(tb->HostWrite.CoalIntCount)));
Robert Elliott69d6e332015-01-23 16:41:56 -06007149 dev_info(dev, " Max outstanding commands = %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007150 readl(&(tb->CmdsOutMax)));
7151 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
7152 for (i = 0; i < 16; i++)
7153 temp_name[i] = readb(&(tb->ServerName[i]));
7154 temp_name[16] = '\0';
7155 dev_info(dev, " Server Name = %s\n", temp_name);
7156 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
7157 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007158#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007159}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007160
7161static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
7162{
7163 int i, offset, mem_type, bar_type;
7164
7165 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
7166 return 0;
7167 offset = 0;
7168 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
7169 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
7170 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
7171 offset += 4;
7172 else {
7173 mem_type = pci_resource_flags(pdev, i) &
7174 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
7175 switch (mem_type) {
7176 case PCI_BASE_ADDRESS_MEM_TYPE_32:
7177 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
7178 offset += 4; /* 32 bit */
7179 break;
7180 case PCI_BASE_ADDRESS_MEM_TYPE_64:
7181 offset += 8;
7182 break;
7183 default: /* reserved in PCI 2.2 */
7184 dev_warn(&pdev->dev,
7185 "base address is invalid\n");
7186 return -1;
7187 break;
7188 }
7189 }
7190 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
7191 return i + 1;
7192 }
7193 return -1;
7194}
7195
Robert Elliottcc64c812015-04-23 09:33:12 -05007196static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
7197{
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007198 pci_free_irq_vectors(h->pdev);
7199 h->msix_vectors = 0;
Robert Elliottcc64c812015-04-23 09:33:12 -05007200}
7201
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007202/* If MSI/MSI-X is supported by the kernel we will try to enable it on
Stephen Cameron050f7142015-01-23 16:42:22 -06007203 * controllers that are capable. If not, we use legacy INTx mode.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007204 */
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007205static int hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007206{
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007207 unsigned int flags = PCI_IRQ_LEGACY;
7208 int ret;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007209
7210 /* Some boards advertise MSI but don't really support it */
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007211 switch (h->board_id) {
7212 case 0x40700E11:
7213 case 0x40800E11:
7214 case 0x40820E11:
7215 case 0x40830E11:
7216 break;
7217 default:
7218 ret = pci_alloc_irq_vectors(h->pdev, 1, MAX_REPLY_QUEUES,
7219 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
7220 if (ret > 0) {
7221 h->msix_vectors = ret;
7222 return 0;
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007223 }
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007224
7225 flags |= PCI_IRQ_MSI;
7226 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007227 }
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007228
7229 ret = pci_alloc_irq_vectors(h->pdev, 1, 1, flags);
7230 if (ret < 0)
7231 return ret;
7232 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007233}
7234
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007235static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007236{
7237 int i;
7238 u32 subsystem_vendor_id, subsystem_device_id;
7239
7240 subsystem_vendor_id = pdev->subsystem_vendor;
7241 subsystem_device_id = pdev->subsystem_device;
7242 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
7243 subsystem_vendor_id;
7244
7245 for (i = 0; i < ARRAY_SIZE(products); i++)
7246 if (*board_id == products[i].board_id)
7247 return i;
7248
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05007249 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
7250 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
7251 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007252 dev_warn(&pdev->dev, "unrecognized board ID: "
7253 "0x%08x, ignoring.\n", *board_id);
7254 return -ENODEV;
7255 }
7256 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
7257}
7258
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007259static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
7260 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007261{
7262 int i;
7263
7264 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007265 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007266 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007267 *memory_bar = pci_resource_start(pdev, i);
7268 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007269 *memory_bar);
7270 return 0;
7271 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007272 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007273 return -ENODEV;
7274}
7275
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007276static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
7277 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007278{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007279 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007280 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007281 if (wait_for_ready)
7282 iterations = HPSA_BOARD_READY_ITERATIONS;
7283 else
7284 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007285
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007286 for (i = 0; i < iterations; i++) {
7287 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
7288 if (wait_for_ready) {
7289 if (scratchpad == HPSA_FIRMWARE_READY)
7290 return 0;
7291 } else {
7292 if (scratchpad != HPSA_FIRMWARE_READY)
7293 return 0;
7294 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007295 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
7296 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007297 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007298 return -ENODEV;
7299}
7300
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007301static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
7302 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
7303 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007304{
7305 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
7306 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
7307 *cfg_base_addr &= (u32) 0x0000ffff;
7308 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
7309 if (*cfg_base_addr_index == -1) {
7310 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
7311 return -ENODEV;
7312 }
7313 return 0;
7314}
7315
Robert Elliott195f2c62015-04-23 09:33:17 -05007316static void hpsa_free_cfgtables(struct ctlr_info *h)
7317{
Robert Elliott105a3db2015-04-23 09:33:48 -05007318 if (h->transtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007319 iounmap(h->transtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007320 h->transtable = NULL;
7321 }
7322 if (h->cfgtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007323 iounmap(h->cfgtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007324 h->cfgtable = NULL;
7325 }
Robert Elliott195f2c62015-04-23 09:33:17 -05007326}
7327
7328/* Find and map CISS config table and transfer table
7329+ * several items must be unmapped (freed) later
7330+ * */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007331static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007332{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06007333 u64 cfg_offset;
7334 u32 cfg_base_addr;
7335 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06007336 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007337 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007338
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007339 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7340 &cfg_base_addr_index, &cfg_offset);
7341 if (rc)
7342 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007343 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007344 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007345 if (!h->cfgtable) {
7346 dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007347 return -ENOMEM;
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007348 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007349 rc = write_driver_ver_to_cfgtable(h->cfgtable);
7350 if (rc)
7351 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007352 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007353 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007354 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
7355 cfg_base_addr_index)+cfg_offset+trans_offset,
7356 sizeof(*h->transtable));
Robert Elliott195f2c62015-04-23 09:33:17 -05007357 if (!h->transtable) {
7358 dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
7359 hpsa_free_cfgtables(h);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007360 return -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05007361 }
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007362 return 0;
7363}
7364
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007365static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007366{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007367#define MIN_MAX_COMMANDS 16
7368 BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
7369
7370 h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06007371
7372 /* Limit commands in memory limited kdump scenario. */
7373 if (reset_devices && h->max_commands > 32)
7374 h->max_commands = 32;
7375
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007376 if (h->max_commands < MIN_MAX_COMMANDS) {
7377 dev_warn(&h->pdev->dev,
7378 "Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
7379 h->max_commands,
7380 MIN_MAX_COMMANDS);
7381 h->max_commands = MIN_MAX_COMMANDS;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007382 }
7383}
7384
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007385/* If the controller reports that the total max sg entries is greater than 512,
7386 * then we know that chained SG blocks work. (Original smart arrays did not
7387 * support chained SG blocks and would return zero for max sg entries.)
7388 */
7389static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
7390{
7391 return h->maxsgentries > 512;
7392}
7393
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007394/* Interrogate the hardware for some limits:
7395 * max commands, max SG elements without chaining, and with chaining,
7396 * SG chain block size, etc.
7397 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007398static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007399{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007400 hpsa_get_max_perf_mode_cmds(h);
Stephen Cameron45fcb862015-01-23 16:43:04 -06007401 h->nr_cmds = h->max_commands;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007402 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007403 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007404 if (hpsa_supports_chained_sg_blocks(h)) {
7405 /* Limit in-command s/g elements to 32 save dma'able memory. */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007406 h->max_cmd_sg_entries = 32;
Webb Scales1a63ea62014-11-14 17:26:43 -06007407 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007408 h->maxsgentries--; /* save one for chain pointer */
7409 } else {
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007410 /*
7411 * Original smart arrays supported at most 31 s/g entries
7412 * embedded inline in the command (trying to use more
7413 * would lock up the controller)
7414 */
7415 h->max_cmd_sg_entries = 31;
Webb Scales1a63ea62014-11-14 17:26:43 -06007416 h->maxsgentries = 31; /* default to traditional values */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007417 h->chainsize = 0;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007418 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05007419
7420 /* Find out what task management functions are supported and cache */
7421 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06007422 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
7423 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
7424 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
7425 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen Cameron8be986c2015-04-23 09:34:06 -05007426 if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
7427 dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007428}
7429
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007430static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
7431{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09007432 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007433 dev_err(&h->pdev->dev, "not a valid CISS config table\n");
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007434 return false;
7435 }
7436 return true;
7437}
7438
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007439static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007440{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007441 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007442
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007443 driver_support = readl(&(h->cfgtable->driver_support));
Arnd Bergmann0b9e7b72014-06-26 15:44:52 +02007444 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
7445#ifdef CONFIG_X86
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007446 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007447#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06007448 driver_support |= ENABLE_UNIT_ATTN;
7449 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007450}
7451
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05007452/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
7453 * in a prefetch beyond physical memory.
7454 */
7455static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
7456{
7457 u32 dma_prefetch;
7458
7459 if (h->board_id != 0x3225103C)
7460 return;
7461 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
7462 dma_prefetch |= 0x8000;
7463 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
7464}
7465
Robert Elliottc706a792015-01-23 16:45:01 -06007466static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007467{
7468 int i;
7469 u32 doorbell_value;
7470 unsigned long flags;
7471 /* wait until the clear_event_notify bit 6 is cleared by controller. */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007472 for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007473 spin_lock_irqsave(&h->lock, flags);
7474 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7475 spin_unlock_irqrestore(&h->lock, flags);
7476 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
Robert Elliottc706a792015-01-23 16:45:01 -06007477 goto done;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007478 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007479 msleep(CLEAR_EVENT_WAIT_INTERVAL);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007480 }
Robert Elliottc706a792015-01-23 16:45:01 -06007481 return -ENODEV;
7482done:
7483 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007484}
7485
Robert Elliottc706a792015-01-23 16:45:01 -06007486static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007487{
7488 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007489 u32 doorbell_value;
7490 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007491
7492 /* under certain very rare conditions, this can take awhile.
7493 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
7494 * as we enter this code.)
7495 */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007496 for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
Webb Scales25163bd2015-04-23 09:32:00 -05007497 if (h->remove_in_progress)
7498 goto done;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007499 spin_lock_irqsave(&h->lock, flags);
7500 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7501 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06007502 if (!(doorbell_value & CFGTBL_ChangeReq))
Robert Elliottc706a792015-01-23 16:45:01 -06007503 goto done;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007504 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007505 msleep(MODE_CHANGE_WAIT_INTERVAL);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007506 }
Robert Elliottc706a792015-01-23 16:45:01 -06007507 return -ENODEV;
7508done:
7509 return 0;
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007510}
7511
Robert Elliottc706a792015-01-23 16:45:01 -06007512/* return -ENODEV or other reason on error, 0 on success */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007513static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007514{
7515 u32 trans_support;
7516
7517 trans_support = readl(&(h->cfgtable->TransportSupport));
7518 if (!(trans_support & SIMPLE_MODE))
7519 return -ENOTSUPP;
7520
7521 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007522
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007523 /* Update the field, and then ring the doorbell */
7524 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007525 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007526 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06007527 if (hpsa_wait_for_mode_change_ack(h))
7528 goto error;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007529 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007530 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
7531 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06007532 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007533 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007534error:
Stephen Cameron050f7142015-01-23 16:42:22 -06007535 dev_err(&h->pdev->dev, "failed to enter simple mode\n");
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007536 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007537}
7538
Robert Elliott195f2c62015-04-23 09:33:17 -05007539/* free items allocated or mapped by hpsa_pci_init */
7540static void hpsa_free_pci_init(struct ctlr_info *h)
7541{
7542 hpsa_free_cfgtables(h); /* pci_init 4 */
7543 iounmap(h->vaddr); /* pci_init 3 */
Robert Elliott105a3db2015-04-23 09:33:48 -05007544 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05007545 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Robert Elliott943a7022015-04-23 09:34:32 -05007546 /*
7547 * call pci_disable_device before pci_release_regions per
7548 * Documentation/PCI/pci.txt
7549 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007550 pci_disable_device(h->pdev); /* pci_init 1 */
Robert Elliott943a7022015-04-23 09:34:32 -05007551 pci_release_regions(h->pdev); /* pci_init 2 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007552}
7553
7554/* several items must be freed later */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007555static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007556{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007557 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007558
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007559 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
7560 if (prod_index < 0)
Robert Elliott60f923b2015-01-23 16:42:06 -06007561 return prod_index;
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007562 h->product_name = products[prod_index].product_name;
7563 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007564
Matthew Garrette5a44df2011-11-11 11:14:23 -05007565 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
7566 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
7567
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007568 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007569 if (err) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007570 dev_err(&h->pdev->dev, "failed to enable PCI device\n");
Robert Elliott943a7022015-04-23 09:34:32 -05007571 pci_disable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007572 return err;
7573 }
7574
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06007575 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007576 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007577 dev_err(&h->pdev->dev,
Robert Elliott195f2c62015-04-23 09:33:17 -05007578 "failed to obtain PCI resources\n");
Robert Elliott943a7022015-04-23 09:34:32 -05007579 pci_disable_device(h->pdev);
7580 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007581 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06007582
7583 pci_set_master(h->pdev);
7584
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007585 err = hpsa_interrupt_mode(h);
7586 if (err)
7587 goto clean1;
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007588 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007589 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007590 goto clean2; /* intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007591 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007592 if (!h->vaddr) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007593 dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007594 err = -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05007595 goto clean2; /* intmode+region, pci */
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007596 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007597 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007598 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007599 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007600 err = hpsa_find_cfgtables(h);
7601 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007602 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007603 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007604
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007605 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007606 err = -ENODEV;
Robert Elliott195f2c62015-04-23 09:33:17 -05007607 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007608 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007609 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05007610 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007611 err = hpsa_enter_simple_mode(h);
7612 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007613 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007614 return 0;
7615
Robert Elliott195f2c62015-04-23 09:33:17 -05007616clean4: /* cfgtables, vaddr, intmode+region, pci */
7617 hpsa_free_cfgtables(h);
7618clean3: /* vaddr, intmode+region, pci */
7619 iounmap(h->vaddr);
Robert Elliott105a3db2015-04-23 09:33:48 -05007620 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05007621clean2: /* intmode+region, pci */
7622 hpsa_disable_interrupt_mode(h);
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007623clean1:
Robert Elliott943a7022015-04-23 09:34:32 -05007624 /*
7625 * call pci_disable_device before pci_release_regions per
7626 * Documentation/PCI/pci.txt
7627 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007628 pci_disable_device(h->pdev);
Robert Elliott943a7022015-04-23 09:34:32 -05007629 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007630 return err;
7631}
7632
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007633static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06007634{
7635 int rc;
7636
7637#define HBA_INQUIRY_BYTE_COUNT 64
7638 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
7639 if (!h->hba_inquiry_data)
7640 return;
7641 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
7642 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
7643 if (rc != 0) {
7644 kfree(h->hba_inquiry_data);
7645 h->hba_inquiry_data = NULL;
7646 }
7647}
7648
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007649static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007650{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007651 int rc, i;
Tomas Henzl3b747292015-01-23 16:41:20 -06007652 void __iomem *vaddr;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007653
7654 if (!reset_devices)
7655 return 0;
7656
Tomas Henzl132aa222014-08-14 16:12:39 +02007657 /* kdump kernel is loading, we don't know in which state is
7658 * the pci interface. The dev->enable_cnt is equal zero
7659 * so we call enable+disable, wait a while and switch it on.
7660 */
7661 rc = pci_enable_device(pdev);
7662 if (rc) {
7663 dev_warn(&pdev->dev, "Failed to enable PCI device\n");
7664 return -ENODEV;
7665 }
7666 pci_disable_device(pdev);
7667 msleep(260); /* a randomly chosen number */
7668 rc = pci_enable_device(pdev);
7669 if (rc) {
7670 dev_warn(&pdev->dev, "failed to enable device.\n");
7671 return -ENODEV;
7672 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06007673
Tomas Henzl859c75a2014-09-12 14:44:15 +02007674 pci_set_master(pdev);
Robert Elliott4fa604e2014-11-14 17:27:24 -06007675
Tomas Henzl3b747292015-01-23 16:41:20 -06007676 vaddr = pci_ioremap_bar(pdev, 0);
7677 if (vaddr == NULL) {
7678 rc = -ENOMEM;
7679 goto out_disable;
7680 }
7681 writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
7682 iounmap(vaddr);
7683
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007684 /* Reset the controller with a PCI power-cycle or via doorbell */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007685 rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007686
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007687 /* -ENOTSUPP here means we cannot reset the controller
7688 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05007689 * "performant mode". Or, it might be 640x, which can't reset
7690 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007691 */
Robert Elliottadf1b3a2015-01-23 16:42:01 -06007692 if (rc)
Tomas Henzl132aa222014-08-14 16:12:39 +02007693 goto out_disable;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007694
7695 /* Now try to get the controller to respond to a no-op */
Robert Elliott1ba66c92015-01-23 16:42:11 -06007696 dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007697 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
7698 if (hpsa_noop(pdev) == 0)
7699 break;
7700 else
7701 dev_warn(&pdev->dev, "no-op failed%s\n",
7702 (i < 11 ? "; re-trying" : ""));
7703 }
Tomas Henzl132aa222014-08-14 16:12:39 +02007704
7705out_disable:
7706
7707 pci_disable_device(pdev);
7708 return rc;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007709}
7710
Robert Elliott1fb7c982015-04-23 09:33:22 -05007711static void hpsa_free_cmd_pool(struct ctlr_info *h)
7712{
7713 kfree(h->cmd_pool_bits);
Robert Elliott105a3db2015-04-23 09:33:48 -05007714 h->cmd_pool_bits = NULL;
7715 if (h->cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05007716 pci_free_consistent(h->pdev,
7717 h->nr_cmds * sizeof(struct CommandList),
7718 h->cmd_pool,
7719 h->cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05007720 h->cmd_pool = NULL;
7721 h->cmd_pool_dhandle = 0;
7722 }
7723 if (h->errinfo_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05007724 pci_free_consistent(h->pdev,
7725 h->nr_cmds * sizeof(struct ErrorInfo),
7726 h->errinfo_pool,
7727 h->errinfo_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05007728 h->errinfo_pool = NULL;
7729 h->errinfo_pool_dhandle = 0;
7730 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05007731}
7732
Robert Elliottd37ffbe2015-04-23 09:32:27 -05007733static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007734{
7735 h->cmd_pool_bits = kzalloc(
7736 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
7737 sizeof(unsigned long), GFP_KERNEL);
7738 h->cmd_pool = pci_alloc_consistent(h->pdev,
7739 h->nr_cmds * sizeof(*h->cmd_pool),
7740 &(h->cmd_pool_dhandle));
7741 h->errinfo_pool = pci_alloc_consistent(h->pdev,
7742 h->nr_cmds * sizeof(*h->errinfo_pool),
7743 &(h->errinfo_pool_dhandle));
7744 if ((h->cmd_pool_bits == NULL)
7745 || (h->cmd_pool == NULL)
7746 || (h->errinfo_pool == NULL)) {
7747 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
Robert Elliott2c143342015-01-23 16:42:48 -06007748 goto clean_up;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007749 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05007750 hpsa_preinitialize_commands(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007751 return 0;
Robert Elliott2c143342015-01-23 16:42:48 -06007752clean_up:
7753 hpsa_free_cmd_pool(h);
7754 return -ENOMEM;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007755}
7756
Robert Elliottec501a12015-01-23 16:41:40 -06007757/* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
7758static void hpsa_free_irqs(struct ctlr_info *h)
7759{
7760 int i;
7761
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007762 if (!h->msix_vectors || h->intr_mode != PERF_MODE_INT) {
Robert Elliottec501a12015-01-23 16:41:40 -06007763 /* Single reply queue, only one irq to free */
Colin Ian King7dc62d92016-11-14 12:59:35 +00007764 free_irq(pci_irq_vector(h->pdev, 0), &h->q[h->intr_mode]);
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007765 h->q[h->intr_mode] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06007766 return;
7767 }
7768
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007769 for (i = 0; i < h->msix_vectors; i++) {
7770 free_irq(pci_irq_vector(h->pdev, i), &h->q[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05007771 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06007772 }
Robert Elliotta4e17fc2015-01-23 16:41:51 -06007773 for (; i < MAX_REPLY_QUEUES; i++)
7774 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06007775}
7776
Robert Elliott9ee61792015-01-23 16:42:32 -06007777/* returns 0 on success; cleans up and returns -Enn on error */
7778static int hpsa_request_irqs(struct ctlr_info *h,
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05007779 irqreturn_t (*msixhandler)(int, void *),
7780 irqreturn_t (*intxhandler)(int, void *))
7781{
Matt Gates254f7962012-05-01 11:43:06 -05007782 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05007783
Matt Gates254f7962012-05-01 11:43:06 -05007784 /*
7785 * initialize h->q[x] = x so that interrupt handlers know which
7786 * queue to process.
7787 */
7788 for (i = 0; i < MAX_REPLY_QUEUES; i++)
7789 h->q[i] = (u8) i;
7790
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007791 if (h->intr_mode == PERF_MODE_INT && h->msix_vectors > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05007792 /* If performant mode and MSI-X, use multiple reply queues */
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007793 for (i = 0; i < h->msix_vectors; i++) {
Robert Elliott8b470042015-04-23 09:34:58 -05007794 sprintf(h->intrname[i], "%s-msix%d", h->devname, i);
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007795 rc = request_irq(pci_irq_vector(h->pdev, i), msixhandler,
Robert Elliott8b470042015-04-23 09:34:58 -05007796 0, h->intrname[i],
Matt Gates254f7962012-05-01 11:43:06 -05007797 &h->q[i]);
Robert Elliotta4e17fc2015-01-23 16:41:51 -06007798 if (rc) {
7799 int j;
7800
7801 dev_err(&h->pdev->dev,
7802 "failed to get irq %d for %s\n",
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007803 pci_irq_vector(h->pdev, i), h->devname);
Robert Elliotta4e17fc2015-01-23 16:41:51 -06007804 for (j = 0; j < i; j++) {
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007805 free_irq(pci_irq_vector(h->pdev, j), &h->q[j]);
Robert Elliotta4e17fc2015-01-23 16:41:51 -06007806 h->q[j] = 0;
7807 }
7808 for (; j < MAX_REPLY_QUEUES; j++)
7809 h->q[j] = 0;
7810 return rc;
7811 }
7812 }
Matt Gates254f7962012-05-01 11:43:06 -05007813 } else {
7814 /* Use single reply pool */
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007815 if (h->msix_vectors > 0 || h->pdev->msi_enabled) {
7816 sprintf(h->intrname[0], "%s-msi%s", h->devname,
7817 h->msix_vectors ? "x" : "");
7818 rc = request_irq(pci_irq_vector(h->pdev, 0),
Robert Elliott8b470042015-04-23 09:34:58 -05007819 msixhandler, 0,
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007820 h->intrname[0],
Matt Gates254f7962012-05-01 11:43:06 -05007821 &h->q[h->intr_mode]);
7822 } else {
Robert Elliott8b470042015-04-23 09:34:58 -05007823 sprintf(h->intrname[h->intr_mode],
7824 "%s-intx", h->devname);
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007825 rc = request_irq(pci_irq_vector(h->pdev, 0),
Robert Elliott8b470042015-04-23 09:34:58 -05007826 intxhandler, IRQF_SHARED,
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007827 h->intrname[0],
Matt Gates254f7962012-05-01 11:43:06 -05007828 &h->q[h->intr_mode]);
7829 }
7830 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05007831 if (rc) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007832 dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08007833 pci_irq_vector(h->pdev, 0), h->devname);
Robert Elliott195f2c62015-04-23 09:33:17 -05007834 hpsa_free_irqs(h);
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05007835 return -ENODEV;
7836 }
7837 return 0;
7838}
7839
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007840static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007841{
Robert Elliott39c53f52015-04-23 09:35:14 -05007842 int rc;
Robert Elliottbf43caf2015-04-23 09:33:38 -05007843 hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007844
7845 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05007846 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY);
7847 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007848 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05007849 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007850 }
7851
7852 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05007853 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
7854 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007855 dev_warn(&h->pdev->dev, "Board failed to become ready "
7856 "after soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05007857 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007858 }
7859
7860 return 0;
7861}
7862
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007863static void hpsa_free_reply_queues(struct ctlr_info *h)
7864{
7865 int i;
7866
7867 for (i = 0; i < h->nreply_queues; i++) {
7868 if (!h->reply_queue[i].head)
7869 continue;
Robert Elliott1fb7c982015-04-23 09:33:22 -05007870 pci_free_consistent(h->pdev,
7871 h->reply_queue_size,
7872 h->reply_queue[i].head,
7873 h->reply_queue[i].busaddr);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007874 h->reply_queue[i].head = NULL;
7875 h->reply_queue[i].busaddr = 0;
7876 }
Robert Elliott105a3db2015-04-23 09:33:48 -05007877 h->reply_queue_size = 0;
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007878}
7879
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05007880static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
7881{
Robert Elliott105a3db2015-04-23 09:33:48 -05007882 hpsa_free_performant_mode(h); /* init_one 7 */
7883 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
7884 hpsa_free_cmd_pool(h); /* init_one 5 */
7885 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliott2946e822015-04-23 09:35:09 -05007886 scsi_host_put(h->scsi_host); /* init_one 3 */
7887 h->scsi_host = NULL; /* init_one 3 */
7888 hpsa_free_pci_init(h); /* init_one 2_5 */
Robert Elliott9ecd9532015-04-23 09:34:43 -05007889 free_percpu(h->lockup_detected); /* init_one 2 */
7890 h->lockup_detected = NULL; /* init_one 2 */
7891 if (h->resubmit_wq) {
7892 destroy_workqueue(h->resubmit_wq); /* init_one 1 */
7893 h->resubmit_wq = NULL;
7894 }
7895 if (h->rescan_ctlr_wq) {
7896 destroy_workqueue(h->rescan_ctlr_wq);
7897 h->rescan_ctlr_wq = NULL;
7898 }
Robert Elliott105a3db2015-04-23 09:33:48 -05007899 kfree(h); /* init_one 1 */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007900}
7901
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007902/* Called when controller lockup detected. */
Don Bracef2405db2015-01-23 16:43:09 -06007903static void fail_all_outstanding_cmds(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007904{
Webb Scales281a7fd2015-01-23 16:43:35 -06007905 int i, refcount;
7906 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05007907 int failcount = 0;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007908
Don Brace080ef1c2015-01-23 16:43:25 -06007909 flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
Don Bracef2405db2015-01-23 16:43:09 -06007910 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06007911 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06007912 refcount = atomic_inc_return(&c->refcount);
7913 if (refcount > 1) {
Webb Scales25163bd2015-04-23 09:32:00 -05007914 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
Webb Scales281a7fd2015-01-23 16:43:35 -06007915 finish_cmd(c);
Stephen Cameron433b5f42015-04-23 09:32:11 -05007916 atomic_dec(&h->commands_outstanding);
Webb Scales25163bd2015-04-23 09:32:00 -05007917 failcount++;
Webb Scales281a7fd2015-01-23 16:43:35 -06007918 }
7919 cmd_free(h, c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007920 }
Webb Scales25163bd2015-04-23 09:32:00 -05007921 dev_warn(&h->pdev->dev,
7922 "failed %d commands in fail_all\n", failcount);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007923}
7924
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007925static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
7926{
Rusty Russellc8ed0012015-03-05 10:49:19 +10307927 int cpu;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007928
Rusty Russellc8ed0012015-03-05 10:49:19 +10307929 for_each_online_cpu(cpu) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007930 u32 *lockup_detected;
7931 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
7932 *lockup_detected = value;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007933 }
7934 wmb(); /* be sure the per-cpu variables are out to memory */
7935}
7936
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007937static void controller_lockup_detected(struct ctlr_info *h)
7938{
7939 unsigned long flags;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007940 u32 lockup_detected;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007941
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007942 h->access.set_intr_mask(h, HPSA_INTR_OFF);
7943 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007944 lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
7945 if (!lockup_detected) {
7946 /* no heartbeat, but controller gave us a zero. */
7947 dev_warn(&h->pdev->dev,
Webb Scales25163bd2015-04-23 09:32:00 -05007948 "lockup detected after %d but scratchpad register is zero\n",
7949 h->heartbeat_sample_interval / HZ);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007950 lockup_detected = 0xffffffff;
7951 }
7952 set_lockup_detected_for_all_cpus(h, lockup_detected);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007953 spin_unlock_irqrestore(&h->lock, flags);
Webb Scales25163bd2015-04-23 09:32:00 -05007954 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
7955 lockup_detected, h->heartbeat_sample_interval / HZ);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007956 pci_disable_device(h->pdev);
Don Bracef2405db2015-01-23 16:43:09 -06007957 fail_all_outstanding_cmds(h);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007958}
7959
Webb Scales25163bd2015-04-23 09:32:00 -05007960static int detect_controller_lockup(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007961{
7962 u64 now;
7963 u32 heartbeat;
7964 unsigned long flags;
7965
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007966 now = get_jiffies_64();
7967 /* If we've received an interrupt recently, we're ok. */
7968 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05007969 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05007970 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007971
7972 /*
7973 * If we've already checked the heartbeat recently, we're ok.
7974 * This could happen if someone sends us a signal. We
7975 * otherwise don't care about signals in this thread.
7976 */
7977 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05007978 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05007979 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007980
7981 /* If heartbeat has not changed since we last looked, we're not ok. */
7982 spin_lock_irqsave(&h->lock, flags);
7983 heartbeat = readl(&h->cfgtable->HeartBeat);
7984 spin_unlock_irqrestore(&h->lock, flags);
7985 if (h->last_heartbeat == heartbeat) {
7986 controller_lockup_detected(h);
Webb Scales25163bd2015-04-23 09:32:00 -05007987 return true;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007988 }
7989
7990 /* We're ok. */
7991 h->last_heartbeat = heartbeat;
7992 h->last_heartbeat_timestamp = now;
Webb Scales25163bd2015-04-23 09:32:00 -05007993 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007994}
7995
Stephen M. Cameron98465902014-02-21 16:25:00 -06007996static void hpsa_ack_ctlr_events(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007997{
7998 int i;
7999 char *event_type;
8000
Stephen Camerone4aa3e62015-01-23 16:44:07 -06008001 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
8002 return;
8003
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008004 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06008005 if ((h->transMethod & (CFGTBL_Trans_io_accel1
8006 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008007 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
8008 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
8009
8010 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
8011 event_type = "state change";
8012 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
8013 event_type = "configuration change";
8014 /* Stop sending new RAID offload reqs via the IO accelerator */
8015 scsi_block_requests(h->scsi_host);
Don Brace5323ed72016-04-27 17:13:59 -05008016 for (i = 0; i < h->ndevices; i++) {
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008017 h->dev[i]->offload_enabled = 0;
Don Brace5323ed72016-04-27 17:13:59 -05008018 h->dev[i]->offload_to_be_enabled = 0;
8019 }
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06008020 hpsa_drain_accel_commands(h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008021 /* Set 'accelerator path config change' bit */
8022 dev_warn(&h->pdev->dev,
8023 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
8024 h->events, event_type);
8025 writel(h->events, &(h->cfgtable->clear_event_notify));
8026 /* Set the "clear event notify field update" bit 6 */
8027 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8028 /* Wait until ctlr clears 'clear event notify field', bit 6 */
8029 hpsa_wait_for_clear_event_notify_ack(h);
8030 scsi_unblock_requests(h->scsi_host);
8031 } else {
8032 /* Acknowledge controller notification events. */
8033 writel(h->events, &(h->cfgtable->clear_event_notify));
8034 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8035 hpsa_wait_for_clear_event_notify_ack(h);
8036#if 0
8037 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8038 hpsa_wait_for_mode_change_ack(h);
8039#endif
8040 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008041 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008042}
8043
8044/* Check a register on the controller to see if there are configuration
8045 * changes (added/changed/removed logical drives, etc.) which mean that
Scott Teele863d682014-02-18 13:57:05 -06008046 * we should rescan the controller for devices.
8047 * Also check flag for driver-initiated rescan.
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008048 */
Stephen M. Cameron98465902014-02-21 16:25:00 -06008049static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008050{
Don Brace853633e2015-11-04 15:50:37 -06008051 if (h->drv_req_rescan) {
8052 h->drv_req_rescan = 0;
8053 return 1;
8054 }
8055
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008056 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
Stephen M. Cameron98465902014-02-21 16:25:00 -06008057 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008058
8059 h->events = readl(&(h->cfgtable->event_notify));
Stephen M. Cameron98465902014-02-21 16:25:00 -06008060 return h->events & RESCAN_REQUIRED_EVENT_BITS;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008061}
8062
Stephen M. Cameron98465902014-02-21 16:25:00 -06008063/*
8064 * Check if any of the offline devices have become ready
8065 */
8066static int hpsa_offline_devices_ready(struct ctlr_info *h)
8067{
8068 unsigned long flags;
8069 struct offline_device_entry *d;
8070 struct list_head *this, *tmp;
8071
8072 spin_lock_irqsave(&h->offline_device_lock, flags);
8073 list_for_each_safe(this, tmp, &h->offline_device_list) {
8074 d = list_entry(this, struct offline_device_entry,
8075 offline_list);
8076 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008077 if (!hpsa_volume_offline(h, d->scsi3addr)) {
8078 spin_lock_irqsave(&h->offline_device_lock, flags);
8079 list_del(&d->offline_list);
8080 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008081 return 1;
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008082 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008083 spin_lock_irqsave(&h->offline_device_lock, flags);
8084 }
8085 spin_unlock_irqrestore(&h->offline_device_lock, flags);
8086 return 0;
8087}
8088
Scott Teel34592252015-11-04 15:52:09 -06008089static int hpsa_luns_changed(struct ctlr_info *h)
8090{
8091 int rc = 1; /* assume there are changes */
8092 struct ReportLUNdata *logdev = NULL;
8093
8094 /* if we can't find out if lun data has changed,
8095 * assume that it has.
8096 */
8097
8098 if (!h->lastlogicals)
Amit Kushwaha7e8a9482016-12-12 16:34:21 +05308099 return rc;
Scott Teel34592252015-11-04 15:52:09 -06008100
8101 logdev = kzalloc(sizeof(*logdev), GFP_KERNEL);
Amit Kushwaha7e8a9482016-12-12 16:34:21 +05308102 if (!logdev)
8103 return rc;
8104
Scott Teel34592252015-11-04 15:52:09 -06008105 if (hpsa_scsi_do_report_luns(h, 1, logdev, sizeof(*logdev), 0)) {
8106 dev_warn(&h->pdev->dev,
8107 "report luns failed, can't track lun changes.\n");
8108 goto out;
8109 }
8110 if (memcmp(logdev, h->lastlogicals, sizeof(*logdev))) {
8111 dev_info(&h->pdev->dev,
8112 "Lun changes detected.\n");
8113 memcpy(h->lastlogicals, logdev, sizeof(*logdev));
8114 goto out;
8115 } else
8116 rc = 0; /* no changes detected. */
8117out:
8118 kfree(logdev);
8119 return rc;
8120}
8121
Scott Teel3d38f002017-05-04 17:51:36 -05008122static void hpsa_perform_rescan(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008123{
Scott Teel3d38f002017-05-04 17:51:36 -05008124 struct Scsi_Host *sh = NULL;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008125 unsigned long flags;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008126
Don Bracebfd75462016-11-15 14:45:32 -06008127 /*
8128 * Do the scan after the reset
8129 */
Don Bracec59d04f2017-05-04 17:51:22 -05008130 spin_lock_irqsave(&h->reset_lock, flags);
Don Bracebfd75462016-11-15 14:45:32 -06008131 if (h->reset_in_progress) {
8132 h->drv_req_rescan = 1;
Don Bracec59d04f2017-05-04 17:51:22 -05008133 spin_unlock_irqrestore(&h->reset_lock, flags);
Don Bracebfd75462016-11-15 14:45:32 -06008134 return;
8135 }
Don Bracec59d04f2017-05-04 17:51:22 -05008136 spin_unlock_irqrestore(&h->reset_lock, flags);
Don Bracebfd75462016-11-15 14:45:32 -06008137
Scott Teel3d38f002017-05-04 17:51:36 -05008138 sh = scsi_host_get(h->scsi_host);
8139 if (sh != NULL) {
8140 hpsa_scan_start(sh);
8141 scsi_host_put(sh);
8142 h->drv_req_rescan = 0;
8143 }
8144}
8145
8146/*
8147 * watch for controller events
8148 */
8149static void hpsa_event_monitor_worker(struct work_struct *work)
8150{
8151 struct ctlr_info *h = container_of(to_delayed_work(work),
8152 struct ctlr_info, event_monitor_work);
8153 unsigned long flags;
8154
8155 spin_lock_irqsave(&h->lock, flags);
8156 if (h->remove_in_progress) {
8157 spin_unlock_irqrestore(&h->lock, flags);
8158 return;
8159 }
8160 spin_unlock_irqrestore(&h->lock, flags);
8161
8162 if (hpsa_ctlr_needs_rescan(h)) {
Stephen M. Cameron98465902014-02-21 16:25:00 -06008163 hpsa_ack_ctlr_events(h);
Scott Teel3d38f002017-05-04 17:51:36 -05008164 hpsa_perform_rescan(h);
8165 }
8166
8167 spin_lock_irqsave(&h->lock, flags);
8168 if (!h->remove_in_progress)
8169 schedule_delayed_work(&h->event_monitor_work,
8170 HPSA_EVENT_MONITOR_INTERVAL);
8171 spin_unlock_irqrestore(&h->lock, flags);
8172}
8173
8174static void hpsa_rescan_ctlr_worker(struct work_struct *work)
8175{
8176 unsigned long flags;
8177 struct ctlr_info *h = container_of(to_delayed_work(work),
8178 struct ctlr_info, rescan_ctlr_work);
8179
8180 spin_lock_irqsave(&h->lock, flags);
8181 if (h->remove_in_progress) {
8182 spin_unlock_irqrestore(&h->lock, flags);
8183 return;
8184 }
8185 spin_unlock_irqrestore(&h->lock, flags);
8186
8187 if (h->drv_req_rescan || hpsa_offline_devices_ready(h)) {
8188 hpsa_perform_rescan(h);
Scott Teel34592252015-11-04 15:52:09 -06008189 } else if (h->discovery_polling) {
Scott Teelc2adae42015-11-04 15:52:16 -06008190 hpsa_disable_rld_caching(h);
Scott Teel34592252015-11-04 15:52:09 -06008191 if (hpsa_luns_changed(h)) {
Scott Teel34592252015-11-04 15:52:09 -06008192 dev_info(&h->pdev->dev,
8193 "driver discovery polling rescan.\n");
Scott Teel3d38f002017-05-04 17:51:36 -05008194 hpsa_perform_rescan(h);
Scott Teel34592252015-11-04 15:52:09 -06008195 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008196 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008197 spin_lock_irqsave(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06008198 if (!h->remove_in_progress)
8199 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008200 h->heartbeat_sample_interval);
8201 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008202}
8203
Don Brace6636e7f2015-01-23 16:45:17 -06008204static void hpsa_monitor_ctlr_worker(struct work_struct *work)
8205{
8206 unsigned long flags;
8207 struct ctlr_info *h = container_of(to_delayed_work(work),
8208 struct ctlr_info, monitor_ctlr_work);
8209
8210 detect_controller_lockup(h);
8211 if (lockup_detected(h))
8212 return;
8213
8214 spin_lock_irqsave(&h->lock, flags);
8215 if (!h->remove_in_progress)
8216 schedule_delayed_work(&h->monitor_ctlr_work,
8217 h->heartbeat_sample_interval);
8218 spin_unlock_irqrestore(&h->lock, flags);
8219}
8220
8221static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
8222 char *name)
8223{
8224 struct workqueue_struct *wq = NULL;
Don Brace6636e7f2015-01-23 16:45:17 -06008225
Don Brace397ea9c2015-02-06 17:44:15 -06008226 wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
Don Brace6636e7f2015-01-23 16:45:17 -06008227 if (!wq)
8228 dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
8229
8230 return wq;
8231}
8232
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008233static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008234{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008235 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008236 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008237 int try_soft_reset = 0;
8238 unsigned long flags;
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008239 u32 board_id;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008240
8241 if (number_of_controllers == 0)
8242 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008243
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008244 rc = hpsa_lookup_board_id(pdev, &board_id);
8245 if (rc < 0) {
8246 dev_warn(&pdev->dev, "Board ID not found\n");
8247 return rc;
8248 }
8249
8250 rc = hpsa_init_reset_devices(pdev, board_id);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008251 if (rc) {
8252 if (rc != -ENOTSUPP)
8253 return rc;
8254 /* If the reset fails in a particular way (it has no way to do
8255 * a proper hard reset, so returns -ENOTSUPP) we can try to do
8256 * a soft reset once we get the controller configured up to the
8257 * point that it can accept a command.
8258 */
8259 try_soft_reset = 1;
8260 rc = 0;
8261 }
8262
8263reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008264
Don Brace303932f2010-02-04 08:42:40 -06008265 /* Command structures must be aligned on a 32-byte boundary because
8266 * the 5 lower bits of the address are used by the hardware. and by
8267 * the driver. See comments in hpsa.h for more info.
8268 */
Don Brace303932f2010-02-04 08:42:40 -06008269 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008270 h = kzalloc(sizeof(*h), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05008271 if (!h) {
8272 dev_err(&pdev->dev, "Failed to allocate controller head\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008273 return -ENOMEM;
Robert Elliott105a3db2015-04-23 09:33:48 -05008274 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008275
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008276 h->pdev = pdev;
Robert Elliott105a3db2015-04-23 09:33:48 -05008277
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06008278 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008279 INIT_LIST_HEAD(&h->offline_device_list);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008280 spin_lock_init(&h->lock);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008281 spin_lock_init(&h->offline_device_lock);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008282 spin_lock_init(&h->scan_lock);
Don Bracec59d04f2017-05-04 17:51:22 -05008283 spin_lock_init(&h->reset_lock);
Don Brace34f0c622015-01-23 16:43:46 -06008284 atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008285
8286 /* Allocate and clear per-cpu variable lockup_detected */
8287 h->lockup_detected = alloc_percpu(u32);
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008288 if (!h->lockup_detected) {
Robert Elliott105a3db2015-04-23 09:33:48 -05008289 dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008290 rc = -ENOMEM;
Robert Elliott2efa5922015-04-23 09:34:53 -05008291 goto clean1; /* aer/h */
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008292 }
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008293 set_lockup_detected_for_all_cpus(h, 0);
8294
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008295 rc = hpsa_pci_init(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05008296 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008297 goto clean2; /* lu, aer/h */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008298
Robert Elliott2946e822015-04-23 09:35:09 -05008299 /* relies on h-> settings made by hpsa_pci_init, including
8300 * interrupt_mode h->intr */
8301 rc = hpsa_scsi_host_alloc(h);
8302 if (rc)
8303 goto clean2_5; /* pci, lu, aer/h */
8304
8305 sprintf(h->devname, HPSA "%d", h->scsi_host->host_no);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008306 h->ctlr = number_of_controllers;
8307 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008308
8309 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008310 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
8311 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008312 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008313 } else {
8314 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8315 if (rc == 0) {
8316 dac = 0;
8317 } else {
8318 dev_err(&pdev->dev, "no suitable DMA available\n");
Robert Elliott2946e822015-04-23 09:35:09 -05008319 goto clean3; /* shost, pci, lu, aer/h */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008320 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008321 }
8322
8323 /* make sure the board interrupts are off */
8324 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05008325
Robert Elliott105a3db2015-04-23 09:33:48 -05008326 rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
8327 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008328 goto clean3; /* shost, pci, lu, aer/h */
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008329 rc = hpsa_alloc_cmd_pool(h);
Robert Elliott8947fd12015-01-23 16:42:54 -06008330 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008331 goto clean4; /* irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008332 rc = hpsa_alloc_sg_chain_blocks(h);
8333 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008334 goto clean5; /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Camerona08a84712010-02-04 08:43:16 -06008335 init_waitqueue_head(&h->scan_wait_queue);
Webb Scalesd604f532015-04-23 09:35:22 -05008336 init_waitqueue_head(&h->event_sync_wait_queue);
8337 mutex_init(&h->reset_mutex);
Stephen M. Camerona08a84712010-02-04 08:43:16 -06008338 h->scan_finished = 1; /* no scan currently in progress */
Don Brace87b9e6a2017-03-10 14:35:17 -06008339 h->scan_waiting = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008340
8341 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008342 h->ndevices = 0;
Robert Elliott2946e822015-04-23 09:35:09 -05008343
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008344 spin_lock_init(&h->devlock);
Robert Elliott105a3db2015-04-23 09:33:48 -05008345 rc = hpsa_put_ctlr_into_performant_mode(h);
8346 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008347 goto clean6; /* sg, cmd, irq, shost, pci, lu, aer/h */
8348
Robert Elliott2efa5922015-04-23 09:34:53 -05008349 /* create the resubmit workqueue */
8350 h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
8351 if (!h->rescan_ctlr_wq) {
8352 rc = -ENOMEM;
8353 goto clean7;
8354 }
8355
8356 h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
8357 if (!h->resubmit_wq) {
8358 rc = -ENOMEM;
8359 goto clean7; /* aer/h */
8360 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008361
Robert Elliott105a3db2015-04-23 09:33:48 -05008362 /*
8363 * At this point, the controller is ready to take commands.
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008364 * Now, if reset_devices and the hard reset didn't work, try
8365 * the soft reset and see if that works.
8366 */
8367 if (try_soft_reset) {
8368
8369 /* This is kind of gross. We may or may not get a completion
8370 * from the soft reset command, and if we do, then the value
8371 * from the fifo may or may not be valid. So, we wait 10 secs
8372 * after the reset throwing away any completions we get during
8373 * that time. Unregister the interrupt handler and register
8374 * fake ones to scoop up any residual completions.
8375 */
8376 spin_lock_irqsave(&h->lock, flags);
8377 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8378 spin_unlock_irqrestore(&h->lock, flags);
Robert Elliottec501a12015-01-23 16:41:40 -06008379 hpsa_free_irqs(h);
Robert Elliott9ee61792015-01-23 16:42:32 -06008380 rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008381 hpsa_intx_discard_completions);
8382 if (rc) {
Robert Elliott9ee61792015-01-23 16:42:32 -06008383 dev_warn(&h->pdev->dev,
8384 "Failed to request_irq after soft reset.\n");
Robert Elliottd4987572015-04-23 09:34:37 -05008385 /*
Robert Elliottb2ef4802015-04-23 09:34:48 -05008386 * cannot goto clean7 or free_irqs will be called
8387 * again. Instead, do its work
8388 */
8389 hpsa_free_performant_mode(h); /* clean7 */
8390 hpsa_free_sg_chain_blocks(h); /* clean6 */
8391 hpsa_free_cmd_pool(h); /* clean5 */
8392 /*
8393 * skip hpsa_free_irqs(h) clean4 since that
8394 * was just called before request_irqs failed
Robert Elliottd4987572015-04-23 09:34:37 -05008395 */
8396 goto clean3;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008397 }
8398
8399 rc = hpsa_kdump_soft_reset(h);
8400 if (rc)
8401 /* Neither hard nor soft reset worked, we're hosed. */
Don Brace7ef73232015-07-18 11:12:33 -05008402 goto clean7;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008403
8404 dev_info(&h->pdev->dev, "Board READY.\n");
8405 dev_info(&h->pdev->dev,
8406 "Waiting for stale completions to drain.\n");
8407 h->access.set_intr_mask(h, HPSA_INTR_ON);
8408 msleep(10000);
8409 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8410
8411 rc = controller_reset_failed(h->cfgtable);
8412 if (rc)
8413 dev_info(&h->pdev->dev,
8414 "Soft reset appears to have failed.\n");
8415
8416 /* since the controller's reset, we have to go back and re-init
8417 * everything. Easiest to just forget what we've done and do it
8418 * all over again.
8419 */
8420 hpsa_undo_allocations_after_kdump_soft_reset(h);
8421 try_soft_reset = 0;
8422 if (rc)
Robert Elliottb2ef4802015-04-23 09:34:48 -05008423 /* don't goto clean, we already unallocated */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008424 return -ENODEV;
8425
8426 goto reinit_after_soft_reset;
8427 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008428
Robert Elliott105a3db2015-04-23 09:33:48 -05008429 /* Enable Accelerated IO path at driver layer */
8430 h->acciopath_status = 1;
Scott Teel34592252015-11-04 15:52:09 -06008431 /* Disable discovery polling.*/
8432 h->discovery_polling = 0;
Scott Teelda0697b2014-02-18 13:57:00 -06008433
Scott Teele863d682014-02-18 13:57:05 -06008434
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008435 /* Turn the interrupts on so we can service requests */
8436 h->access.set_intr_mask(h, HPSA_INTR_ON);
8437
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06008438 hpsa_hba_inquiry(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008439
Scott Teel34592252015-11-04 15:52:09 -06008440 h->lastlogicals = kzalloc(sizeof(*(h->lastlogicals)), GFP_KERNEL);
8441 if (!h->lastlogicals)
8442 dev_info(&h->pdev->dev,
8443 "Can't track change to report lun data\n");
8444
Don Bracecf477232016-04-27 17:13:26 -05008445 /* hook into SCSI subsystem */
8446 rc = hpsa_scsi_add_host(h);
8447 if (rc)
8448 goto clean7; /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
8449
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008450 /* Monitor the controller for firmware lockups */
8451 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
8452 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
8453 schedule_delayed_work(&h->monitor_ctlr_work,
8454 h->heartbeat_sample_interval);
Don Brace6636e7f2015-01-23 16:45:17 -06008455 INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
8456 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
8457 h->heartbeat_sample_interval);
Scott Teel3d38f002017-05-04 17:51:36 -05008458 INIT_DELAYED_WORK(&h->event_monitor_work, hpsa_event_monitor_worker);
8459 schedule_delayed_work(&h->event_monitor_work,
8460 HPSA_EVENT_MONITOR_INTERVAL);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05008461 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008462
Robert Elliott2946e822015-04-23 09:35:09 -05008463clean7: /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008464 hpsa_free_performant_mode(h);
8465 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8466clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06008467 hpsa_free_sg_chain_blocks(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008468clean5: /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008469 hpsa_free_cmd_pool(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008470clean4: /* irq, shost, pci, lu, aer/h */
Robert Elliottec501a12015-01-23 16:41:40 -06008471 hpsa_free_irqs(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008472clean3: /* shost, pci, lu, aer/h */
8473 scsi_host_put(h->scsi_host);
8474 h->scsi_host = NULL;
8475clean2_5: /* pci, lu, aer/h */
Robert Elliott195f2c62015-04-23 09:33:17 -05008476 hpsa_free_pci_init(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008477clean2: /* lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008478 if (h->lockup_detected) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008479 free_percpu(h->lockup_detected);
Robert Elliott105a3db2015-04-23 09:33:48 -05008480 h->lockup_detected = NULL;
8481 }
8482clean1: /* wq/aer/h */
8483 if (h->resubmit_wq) {
8484 destroy_workqueue(h->resubmit_wq);
8485 h->resubmit_wq = NULL;
8486 }
8487 if (h->rescan_ctlr_wq) {
8488 destroy_workqueue(h->rescan_ctlr_wq);
8489 h->rescan_ctlr_wq = NULL;
8490 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008491 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008492 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008493}
8494
8495static void hpsa_flush_cache(struct ctlr_info *h)
8496{
8497 char *flush_buf;
8498 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05008499 int rc;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008500
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008501 if (unlikely(lockup_detected(h)))
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008502 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008503 flush_buf = kzalloc(4, GFP_KERNEL);
8504 if (!flush_buf)
8505 return;
8506
Stephen Cameron45fcb862015-01-23 16:43:04 -06008507 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05008508
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008509 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
8510 RAID_CTLR_LUNID, TYPE_CMD)) {
8511 goto out;
8512 }
Webb Scales25163bd2015-04-23 09:32:00 -05008513 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008514 PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
Webb Scales25163bd2015-04-23 09:32:00 -05008515 if (rc)
8516 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008517 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008518out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008519 dev_warn(&h->pdev->dev,
8520 "error flushing cache on controller\n");
Stephen Cameron45fcb862015-01-23 16:43:04 -06008521 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008522 kfree(flush_buf);
8523}
8524
Scott Teelc2adae42015-11-04 15:52:16 -06008525/* Make controller gather fresh report lun data each time we
8526 * send down a report luns request
8527 */
8528static void hpsa_disable_rld_caching(struct ctlr_info *h)
8529{
8530 u32 *options;
8531 struct CommandList *c;
8532 int rc;
8533
8534 /* Don't bother trying to set diag options if locked up */
8535 if (unlikely(h->lockup_detected))
8536 return;
8537
8538 options = kzalloc(sizeof(*options), GFP_KERNEL);
Amit Kushwaha7e8a9482016-12-12 16:34:21 +05308539 if (!options)
Scott Teelc2adae42015-11-04 15:52:16 -06008540 return;
Scott Teelc2adae42015-11-04 15:52:16 -06008541
8542 c = cmd_alloc(h);
8543
8544 /* first, get the current diag options settings */
8545 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
8546 RAID_CTLR_LUNID, TYPE_CMD))
8547 goto errout;
8548
8549 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008550 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06008551 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8552 goto errout;
8553
8554 /* Now, set the bit for disabling the RLD caching */
8555 *options |= HPSA_DIAG_OPTS_DISABLE_RLD_CACHING;
8556
8557 if (fill_cmd(c, BMIC_SET_DIAG_OPTIONS, h, options, 4, 0,
8558 RAID_CTLR_LUNID, TYPE_CMD))
8559 goto errout;
8560
8561 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008562 PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06008563 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8564 goto errout;
8565
8566 /* Now verify that it got set: */
8567 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
8568 RAID_CTLR_LUNID, TYPE_CMD))
8569 goto errout;
8570
8571 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
Don Bracec448ecf2016-04-27 17:13:51 -05008572 PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
Scott Teelc2adae42015-11-04 15:52:16 -06008573 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8574 goto errout;
8575
Dan Carpenterd8a080c2015-11-12 12:43:38 +03008576 if (*options & HPSA_DIAG_OPTS_DISABLE_RLD_CACHING)
Scott Teelc2adae42015-11-04 15:52:16 -06008577 goto out;
8578
8579errout:
8580 dev_err(&h->pdev->dev,
8581 "Error: failed to disable report lun data caching.\n");
8582out:
8583 cmd_free(h, c);
8584 kfree(options);
8585}
8586
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008587static void hpsa_shutdown(struct pci_dev *pdev)
8588{
8589 struct ctlr_info *h;
8590
8591 h = pci_get_drvdata(pdev);
8592 /* Turn board interrupts off and send the flush cache command
8593 * sendcmd will turn off interrupt, and send the flush...
8594 * To write all data in the battery backed cache to disks
8595 */
8596 hpsa_flush_cache(h);
8597 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Robert Elliott105a3db2015-04-23 09:33:48 -05008598 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliottcc64c812015-04-23 09:33:12 -05008599 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008600}
8601
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008602static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008603{
8604 int i;
8605
Robert Elliott105a3db2015-04-23 09:33:48 -05008606 for (i = 0; i < h->ndevices; i++) {
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008607 kfree(h->dev[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008608 h->dev[i] = NULL;
8609 }
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008610}
8611
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008612static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008613{
8614 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008615 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008616
8617 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008618 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008619 return;
8620 }
8621 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008622
8623 /* Get rid of any controller monitoring work items */
8624 spin_lock_irqsave(&h->lock, flags);
8625 h->remove_in_progress = 1;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008626 spin_unlock_irqrestore(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06008627 cancel_delayed_work_sync(&h->monitor_ctlr_work);
8628 cancel_delayed_work_sync(&h->rescan_ctlr_work);
Scott Teel3d38f002017-05-04 17:51:36 -05008629 cancel_delayed_work_sync(&h->event_monitor_work);
Don Brace6636e7f2015-01-23 16:45:17 -06008630 destroy_workqueue(h->rescan_ctlr_wq);
8631 destroy_workqueue(h->resubmit_wq);
Robert Elliottcc64c812015-04-23 09:33:12 -05008632
Don Brace2d041302015-07-18 11:13:15 -05008633 /*
8634 * Call before disabling interrupts.
8635 * scsi_remove_host can trigger I/O operations especially
8636 * when multipath is enabled. There can be SYNCHRONIZE CACHE
8637 * operations which cannot complete and will hang the system.
8638 */
8639 if (h->scsi_host)
8640 scsi_remove_host(h->scsi_host); /* init_one 8 */
Robert Elliott105a3db2015-04-23 09:33:48 -05008641 /* includes hpsa_free_irqs - init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008642 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008643 hpsa_shutdown(pdev);
Robert Elliottcc64c812015-04-23 09:33:12 -05008644
Robert Elliott105a3db2015-04-23 09:33:48 -05008645 hpsa_free_device_info(h); /* scan */
8646
Robert Elliott2946e822015-04-23 09:35:09 -05008647 kfree(h->hba_inquiry_data); /* init_one 10 */
8648 h->hba_inquiry_data = NULL; /* init_one 10 */
Robert Elliott2946e822015-04-23 09:35:09 -05008649 hpsa_free_ioaccel2_sg_chain_blocks(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05008650 hpsa_free_performant_mode(h); /* init_one 7 */
8651 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
8652 hpsa_free_cmd_pool(h); /* init_one 5 */
Scott Teel34592252015-11-04 15:52:09 -06008653 kfree(h->lastlogicals);
Robert Elliott105a3db2015-04-23 09:33:48 -05008654
8655 /* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008656
Robert Elliott2946e822015-04-23 09:35:09 -05008657 scsi_host_put(h->scsi_host); /* init_one 3 */
8658 h->scsi_host = NULL; /* init_one 3 */
8659
Robert Elliott195f2c62015-04-23 09:33:17 -05008660 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Robert Elliott2946e822015-04-23 09:35:09 -05008661 hpsa_free_pci_init(h); /* init_one 2.5 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008662
Robert Elliott105a3db2015-04-23 09:33:48 -05008663 free_percpu(h->lockup_detected); /* init_one 2 */
8664 h->lockup_detected = NULL; /* init_one 2 */
8665 /* (void) pci_disable_pcie_error_reporting(pdev); */ /* init_one 1 */
Kevin Barnettd04e62b2015-11-04 15:52:34 -06008666
8667 hpsa_delete_sas_host(h);
8668
Robert Elliott105a3db2015-04-23 09:33:48 -05008669 kfree(h); /* init_one 1 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008670}
8671
8672static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
8673 __attribute__((unused)) pm_message_t state)
8674{
8675 return -ENOSYS;
8676}
8677
8678static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
8679{
8680 return -ENOSYS;
8681}
8682
8683static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06008684 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008685 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008686 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008687 .id_table = hpsa_pci_device_id, /* id_table */
8688 .shutdown = hpsa_shutdown,
8689 .suspend = hpsa_suspend,
8690 .resume = hpsa_resume,
8691};
8692
Don Brace303932f2010-02-04 08:42:40 -06008693/* Fill in bucket_map[], given nsgs (the max number of
8694 * scatter gather elements supported) and bucket[],
8695 * which is an array of 8 integers. The bucket[] array
8696 * contains 8 different DMA transfer sizes (in 16
8697 * byte increments) which the controller uses to fetch
8698 * commands. This function fills in bucket_map[], which
8699 * maps a given number of scatter gather elements to one of
8700 * the 8 DMA transfer sizes. The point of it is to allow the
8701 * controller to only do as much DMA as needed to fetch the
8702 * command, with the DMA transfer size encoded in the lower
8703 * bits of the command address.
8704 */
8705static void calc_bucket_map(int bucket[], int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -06008706 int nsgs, int min_blocks, u32 *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06008707{
8708 int i, j, b, size;
8709
Don Brace303932f2010-02-04 08:42:40 -06008710 /* Note, bucket_map must have nsgs+1 entries. */
8711 for (i = 0; i <= nsgs; i++) {
8712 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06008713 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06008714 b = num_buckets; /* Assume the biggest bucket */
8715 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06008716 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06008717 if (bucket[j] >= size) {
8718 b = j;
8719 break;
8720 }
8721 }
8722 /* for a command with i SG entries, use bucket b. */
8723 bucket_map[i] = b;
8724 }
8725}
8726
Robert Elliott105a3db2015-04-23 09:33:48 -05008727/*
8728 * return -ENODEV on err, 0 on success (or no action)
8729 * allocates numerous items that must be freed later
8730 */
Robert Elliottc706a792015-01-23 16:45:01 -06008731static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06008732{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05008733 int i;
8734 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06008735 unsigned long transMethod = CFGTBL_Trans_Performant |
8736 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008737 CFGTBL_Trans_enable_directed_msix |
8738 (trans_support & (CFGTBL_Trans_io_accel1 |
8739 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06008740 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05008741
8742 /* This is a bit complicated. There are 8 registers on
8743 * the controller which we write to to tell it 8 different
8744 * sizes of commands which there may be. It's a way of
8745 * reducing the DMA done to fetch each command. Encoded into
8746 * each command's tag are 3 bits which communicate to the controller
8747 * which of the eight sizes that command fits within. The size of
8748 * each command depends on how many scatter gather entries there are.
8749 * Each SG entry requires 16 bytes. The eight registers are programmed
8750 * with the number of 16-byte blocks a command of that size requires.
8751 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008752 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05008753 * blocks. Note, this only extends to the SG entries contained
8754 * within the command block, and does not extend to chained blocks
8755 * of SG elements. bft[] contains the eight values we write to
8756 * the registers. They are not evenly distributed, but have more
8757 * sizes for small commands, and fewer sizes for larger commands.
8758 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008759 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008760#define MIN_IOACCEL2_BFT_ENTRY 5
8761#define HPSA_IOACCEL2_HEADER_SZ 4
8762 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
8763 13, 14, 15, 16, 17, 18, 19,
8764 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
8765 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
8766 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
8767 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
8768 16 * MIN_IOACCEL2_BFT_ENTRY);
8769 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008770 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06008771 /* 5 = 1 s/g entry or 4k
8772 * 6 = 2 s/g entry or 8k
8773 * 8 = 4 s/g entry or 16k
8774 * 10 = 6 s/g entry or 24k
8775 */
Don Brace303932f2010-02-04 08:42:40 -06008776
Stephen M. Cameronb3a52e72014-05-29 10:53:23 -05008777 /* If the controller supports either ioaccel method then
8778 * we can also use the RAID stack submit path that does not
8779 * perform the superfluous readl() after each command submission.
8780 */
8781 if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
8782 access = SA5_performant_access_no_read;
8783
Don Brace303932f2010-02-04 08:42:40 -06008784 /* Controller spec: zero out this buffer. */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008785 for (i = 0; i < h->nreply_queues; i++)
8786 memset(h->reply_queue[i].head, 0, h->reply_queue_size);
Don Brace303932f2010-02-04 08:42:40 -06008787
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008788 bft[7] = SG_ENTRIES_IN_CMD + 4;
8789 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06008790 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06008791 for (i = 0; i < 8; i++)
8792 writel(bft[i], &h->transtable->BlockFetch[i]);
8793
8794 /* size of controller ring buffer */
8795 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05008796 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06008797 writel(0, &h->transtable->RepQCtrAddrLow32);
8798 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05008799
8800 for (i = 0; i < h->nreply_queues; i++) {
8801 writel(0, &h->transtable->RepQAddr[i].upper);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008802 writel(h->reply_queue[i].busaddr,
Matt Gates254f7962012-05-01 11:43:06 -05008803 &h->transtable->RepQAddr[i].lower);
8804 }
8805
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008806 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06008807 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
8808 /*
8809 * enable outbound interrupt coalescing in accelerator mode;
8810 */
8811 if (trans_support & CFGTBL_Trans_io_accel1) {
8812 access = SA5_ioaccel_mode1_access;
8813 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8814 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Don Brace96b6ce42017-01-30 16:05:17 -06008815 } else
8816 if (trans_support & CFGTBL_Trans_io_accel2)
Scott Teelc3497752014-02-18 13:56:34 -06008817 access = SA5_ioaccel_mode2_access;
Don Brace303932f2010-02-04 08:42:40 -06008818 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06008819 if (hpsa_wait_for_mode_change_ack(h)) {
8820 dev_err(&h->pdev->dev,
8821 "performant mode problem - doorbell timeout\n");
8822 return -ENODEV;
8823 }
Don Brace303932f2010-02-04 08:42:40 -06008824 register_value = readl(&(h->cfgtable->TransportActive));
8825 if (!(register_value & CFGTBL_Trans_Performant)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06008826 dev_err(&h->pdev->dev,
8827 "performant mode problem - transport not active\n");
Robert Elliottc706a792015-01-23 16:45:01 -06008828 return -ENODEV;
Don Brace303932f2010-02-04 08:42:40 -06008829 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06008830 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06008831 h->access = access;
8832 h->transMethod = transMethod;
8833
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008834 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
8835 (trans_support & CFGTBL_Trans_io_accel2)))
Robert Elliottc706a792015-01-23 16:45:01 -06008836 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06008837
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008838 if (trans_support & CFGTBL_Trans_io_accel1) {
8839 /* Set up I/O accelerator mode */
8840 for (i = 0; i < h->nreply_queues; i++) {
8841 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
8842 h->reply_queue[i].current_entry =
8843 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
8844 }
8845 bft[7] = h->ioaccel_maxsg + 8;
8846 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
8847 h->ioaccel1_blockFetchTable);
8848
8849 /* initialize all reply queue entries to unused */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008850 for (i = 0; i < h->nreply_queues; i++)
8851 memset(h->reply_queue[i].head,
8852 (u8) IOACCEL_MODE1_REPLY_UNUSED,
8853 h->reply_queue_size);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008854
8855 /* set all the constant fields in the accelerator command
8856 * frames once at init time to save CPU cycles later.
8857 */
8858 for (i = 0; i < h->nr_cmds; i++) {
8859 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
8860
8861 cp->function = IOACCEL1_FUNCTION_SCSIIO;
8862 cp->err_info = (u32) (h->errinfo_pool_dhandle +
8863 (i * sizeof(struct ErrorInfo)));
8864 cp->err_info_len = sizeof(struct ErrorInfo);
8865 cp->sgl_offset = IOACCEL1_SGLOFFSET;
Don Brace2b08b3e2015-01-23 16:41:09 -06008866 cp->host_context_flags =
8867 cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008868 cp->timeout_sec = 0;
8869 cp->ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06008870 cp->tag =
Don Bracef2405db2015-01-23 16:43:09 -06008871 cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06008872 cp->host_addr =
8873 cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008874 (i * sizeof(struct io_accel1_cmd)));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008875 }
8876 } else if (trans_support & CFGTBL_Trans_io_accel2) {
8877 u64 cfg_offset, cfg_base_addr_index;
8878 u32 bft2_offset, cfg_base_addr;
8879 int rc;
8880
8881 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
8882 &cfg_base_addr_index, &cfg_offset);
8883 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
8884 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
8885 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
8886 4, h->ioaccel2_blockFetchTable);
8887 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
8888 BUILD_BUG_ON(offsetof(struct CfgTable,
8889 io_accel_request_size_offset) != 0xb8);
8890 h->ioaccel2_bft2_regs =
8891 remap_pci_mem(pci_resource_start(h->pdev,
8892 cfg_base_addr_index) +
8893 cfg_offset + bft2_offset,
8894 ARRAY_SIZE(bft2) *
8895 sizeof(*h->ioaccel2_bft2_regs));
8896 for (i = 0; i < ARRAY_SIZE(bft2); i++)
8897 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06008898 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008899 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06008900 if (hpsa_wait_for_mode_change_ack(h)) {
8901 dev_err(&h->pdev->dev,
8902 "performant mode problem - enabling ioaccel mode\n");
8903 return -ENODEV;
8904 }
8905 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06008906}
8907
Robert Elliott1fb7c982015-04-23 09:33:22 -05008908/* Free ioaccel1 mode command blocks and block fetch table */
8909static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
8910{
Robert Elliott105a3db2015-04-23 09:33:48 -05008911 if (h->ioaccel_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05008912 pci_free_consistent(h->pdev,
8913 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
8914 h->ioaccel_cmd_pool,
8915 h->ioaccel_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05008916 h->ioaccel_cmd_pool = NULL;
8917 h->ioaccel_cmd_pool_dhandle = 0;
8918 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05008919 kfree(h->ioaccel1_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05008920 h->ioaccel1_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05008921}
8922
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008923/* Allocate ioaccel1 mode command blocks and block fetch table */
8924static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
Matt Gatese1f7de02014-02-18 13:55:17 -06008925{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06008926 h->ioaccel_maxsg =
8927 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8928 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
8929 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
8930
Matt Gatese1f7de02014-02-18 13:55:17 -06008931 /* Command structures must be aligned on a 128-byte boundary
8932 * because the 7 lower bits of the address are used by the
8933 * hardware.
8934 */
Matt Gatese1f7de02014-02-18 13:55:17 -06008935 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
8936 IOACCEL1_COMMANDLIST_ALIGNMENT);
8937 h->ioaccel_cmd_pool =
8938 pci_alloc_consistent(h->pdev,
8939 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
8940 &(h->ioaccel_cmd_pool_dhandle));
8941
8942 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06008943 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06008944 sizeof(u32)), GFP_KERNEL);
8945
8946 if ((h->ioaccel_cmd_pool == NULL) ||
8947 (h->ioaccel1_blockFetchTable == NULL))
8948 goto clean_up;
8949
8950 memset(h->ioaccel_cmd_pool, 0,
8951 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
8952 return 0;
8953
8954clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05008955 hpsa_free_ioaccel1_cmd_and_bft(h);
Robert Elliott2dd02d72015-04-23 09:33:43 -05008956 return -ENOMEM;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05008957}
8958
Robert Elliott1fb7c982015-04-23 09:33:22 -05008959/* Free ioaccel2 mode command blocks and block fetch table */
8960static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
8961{
Webb Scalesd9a729f2015-04-23 09:33:27 -05008962 hpsa_free_ioaccel2_sg_chain_blocks(h);
8963
Robert Elliott105a3db2015-04-23 09:33:48 -05008964 if (h->ioaccel2_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05008965 pci_free_consistent(h->pdev,
8966 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
8967 h->ioaccel2_cmd_pool,
8968 h->ioaccel2_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05008969 h->ioaccel2_cmd_pool = NULL;
8970 h->ioaccel2_cmd_pool_dhandle = 0;
8971 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05008972 kfree(h->ioaccel2_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05008973 h->ioaccel2_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05008974}
8975
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008976/* Allocate ioaccel2 mode command blocks and block fetch table */
8977static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06008978{
Webb Scalesd9a729f2015-04-23 09:33:27 -05008979 int rc;
8980
Stephen M. Cameronaca90122014-02-18 13:56:14 -06008981 /* Allocate ioaccel2 mode command blocks and block fetch table */
8982
8983 h->ioaccel_maxsg =
8984 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8985 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
8986 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
8987
Stephen M. Cameronaca90122014-02-18 13:56:14 -06008988 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
8989 IOACCEL2_COMMANDLIST_ALIGNMENT);
8990 h->ioaccel2_cmd_pool =
8991 pci_alloc_consistent(h->pdev,
8992 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
8993 &(h->ioaccel2_cmd_pool_dhandle));
8994
8995 h->ioaccel2_blockFetchTable =
8996 kmalloc(((h->ioaccel_maxsg + 1) *
8997 sizeof(u32)), GFP_KERNEL);
8998
8999 if ((h->ioaccel2_cmd_pool == NULL) ||
Webb Scalesd9a729f2015-04-23 09:33:27 -05009000 (h->ioaccel2_blockFetchTable == NULL)) {
9001 rc = -ENOMEM;
9002 goto clean_up;
9003 }
9004
9005 rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
9006 if (rc)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009007 goto clean_up;
9008
9009 memset(h->ioaccel2_cmd_pool, 0,
9010 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
9011 return 0;
9012
9013clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05009014 hpsa_free_ioaccel2_cmd_and_bft(h);
Webb Scalesd9a729f2015-04-23 09:33:27 -05009015 return rc;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009016}
9017
Robert Elliott105a3db2015-04-23 09:33:48 -05009018/* Free items allocated by hpsa_put_ctlr_into_performant_mode */
9019static void hpsa_free_performant_mode(struct ctlr_info *h)
9020{
9021 kfree(h->blockFetchTable);
9022 h->blockFetchTable = NULL;
9023 hpsa_free_reply_queues(h);
9024 hpsa_free_ioaccel1_cmd_and_bft(h);
9025 hpsa_free_ioaccel2_cmd_and_bft(h);
9026}
9027
9028/* return -ENODEV on error, 0 on success (or no action)
9029 * allocates numerous items that must be freed later
9030 */
9031static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009032{
9033 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06009034 unsigned long transMethod = CFGTBL_Trans_Performant |
9035 CFGTBL_Trans_use_short_tags;
Robert Elliott105a3db2015-04-23 09:33:48 -05009036 int i, rc;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009037
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06009038 if (hpsa_simple_mode)
Robert Elliott105a3db2015-04-23 09:33:48 -05009039 return 0;
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06009040
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05009041 trans_support = readl(&(h->cfgtable->TransportSupport));
9042 if (!(trans_support & PERFORMANT_MODE))
Robert Elliott105a3db2015-04-23 09:33:48 -05009043 return 0;
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05009044
Matt Gatese1f7de02014-02-18 13:55:17 -06009045 /* Check for I/O accelerator mode support */
9046 if (trans_support & CFGTBL_Trans_io_accel1) {
9047 transMethod |= CFGTBL_Trans_io_accel1 |
9048 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05009049 rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
9050 if (rc)
9051 return rc;
9052 } else if (trans_support & CFGTBL_Trans_io_accel2) {
9053 transMethod |= CFGTBL_Trans_io_accel2 |
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009054 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05009055 rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
9056 if (rc)
9057 return rc;
Matt Gatese1f7de02014-02-18 13:55:17 -06009058 }
9059
Christoph Hellwigbc2bb152016-11-09 10:42:22 -08009060 h->nreply_queues = h->msix_vectors > 0 ? h->msix_vectors : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05009061 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009062 /* Performant mode ring buffer and supporting data structures */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009063 h->reply_queue_size = h->max_commands * sizeof(u64);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009064
Matt Gates254f7962012-05-01 11:43:06 -05009065 for (i = 0; i < h->nreply_queues; i++) {
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009066 h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
9067 h->reply_queue_size,
9068 &(h->reply_queue[i].busaddr));
Robert Elliott105a3db2015-04-23 09:33:48 -05009069 if (!h->reply_queue[i].head) {
9070 rc = -ENOMEM;
9071 goto clean1; /* rq, ioaccel */
9072 }
Matt Gates254f7962012-05-01 11:43:06 -05009073 h->reply_queue[i].size = h->max_commands;
9074 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
9075 h->reply_queue[i].current_entry = 0;
9076 }
9077
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009078 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009079 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009080 sizeof(u32)), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05009081 if (!h->blockFetchTable) {
9082 rc = -ENOMEM;
9083 goto clean1; /* rq, ioaccel */
9084 }
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009085
Robert Elliott105a3db2015-04-23 09:33:48 -05009086 rc = hpsa_enter_performant_mode(h, trans_support);
9087 if (rc)
9088 goto clean2; /* bft, rq, ioaccel */
9089 return 0;
Don Brace303932f2010-02-04 08:42:40 -06009090
Robert Elliott105a3db2015-04-23 09:33:48 -05009091clean2: /* bft, rq, ioaccel */
Don Brace303932f2010-02-04 08:42:40 -06009092 kfree(h->blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009093 h->blockFetchTable = NULL;
9094clean1: /* rq, ioaccel */
9095 hpsa_free_reply_queues(h);
9096 hpsa_free_ioaccel1_cmd_and_bft(h);
9097 hpsa_free_ioaccel2_cmd_and_bft(h);
9098 return rc;
Don Brace303932f2010-02-04 08:42:40 -06009099}
9100
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009101static int is_accelerated_cmd(struct CommandList *c)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009102{
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009103 return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
9104}
9105
9106static void hpsa_drain_accel_commands(struct ctlr_info *h)
9107{
9108 struct CommandList *c = NULL;
Don Bracef2405db2015-01-23 16:43:09 -06009109 int i, accel_cmds_out;
Webb Scales281a7fd2015-01-23 16:43:35 -06009110 int refcount;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009111
Don Bracef2405db2015-01-23 16:43:09 -06009112 do { /* wait for all outstanding ioaccel commands to drain out */
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009113 accel_cmds_out = 0;
Don Bracef2405db2015-01-23 16:43:09 -06009114 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06009115 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06009116 refcount = atomic_inc_return(&c->refcount);
9117 if (refcount > 1) /* Command is allocated */
9118 accel_cmds_out += is_accelerated_cmd(c);
9119 cmd_free(h, c);
Don Bracef2405db2015-01-23 16:43:09 -06009120 }
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009121 if (accel_cmds_out <= 0)
Webb Scales281a7fd2015-01-23 16:43:35 -06009122 break;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009123 msleep(100);
9124 } while (1);
9125}
9126
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009127static struct hpsa_sas_phy *hpsa_alloc_sas_phy(
9128 struct hpsa_sas_port *hpsa_sas_port)
9129{
9130 struct hpsa_sas_phy *hpsa_sas_phy;
9131 struct sas_phy *phy;
9132
9133 hpsa_sas_phy = kzalloc(sizeof(*hpsa_sas_phy), GFP_KERNEL);
9134 if (!hpsa_sas_phy)
9135 return NULL;
9136
9137 phy = sas_phy_alloc(hpsa_sas_port->parent_node->parent_dev,
9138 hpsa_sas_port->next_phy_index);
9139 if (!phy) {
9140 kfree(hpsa_sas_phy);
9141 return NULL;
9142 }
9143
9144 hpsa_sas_port->next_phy_index++;
9145 hpsa_sas_phy->phy = phy;
9146 hpsa_sas_phy->parent_port = hpsa_sas_port;
9147
9148 return hpsa_sas_phy;
9149}
9150
9151static void hpsa_free_sas_phy(struct hpsa_sas_phy *hpsa_sas_phy)
9152{
9153 struct sas_phy *phy = hpsa_sas_phy->phy;
9154
9155 sas_port_delete_phy(hpsa_sas_phy->parent_port->port, phy);
9156 sas_phy_free(phy);
9157 if (hpsa_sas_phy->added_to_port)
9158 list_del(&hpsa_sas_phy->phy_list_entry);
9159 kfree(hpsa_sas_phy);
9160}
9161
9162static int hpsa_sas_port_add_phy(struct hpsa_sas_phy *hpsa_sas_phy)
9163{
9164 int rc;
9165 struct hpsa_sas_port *hpsa_sas_port;
9166 struct sas_phy *phy;
9167 struct sas_identify *identify;
9168
9169 hpsa_sas_port = hpsa_sas_phy->parent_port;
9170 phy = hpsa_sas_phy->phy;
9171
9172 identify = &phy->identify;
9173 memset(identify, 0, sizeof(*identify));
9174 identify->sas_address = hpsa_sas_port->sas_address;
9175 identify->device_type = SAS_END_DEVICE;
9176 identify->initiator_port_protocols = SAS_PROTOCOL_STP;
9177 identify->target_port_protocols = SAS_PROTOCOL_STP;
9178 phy->minimum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
9179 phy->maximum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
9180 phy->minimum_linkrate = SAS_LINK_RATE_UNKNOWN;
9181 phy->maximum_linkrate = SAS_LINK_RATE_UNKNOWN;
9182 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
9183
9184 rc = sas_phy_add(hpsa_sas_phy->phy);
9185 if (rc)
9186 return rc;
9187
9188 sas_port_add_phy(hpsa_sas_port->port, hpsa_sas_phy->phy);
9189 list_add_tail(&hpsa_sas_phy->phy_list_entry,
9190 &hpsa_sas_port->phy_list_head);
9191 hpsa_sas_phy->added_to_port = true;
9192
9193 return 0;
9194}
9195
9196static int
9197 hpsa_sas_port_add_rphy(struct hpsa_sas_port *hpsa_sas_port,
9198 struct sas_rphy *rphy)
9199{
9200 struct sas_identify *identify;
9201
9202 identify = &rphy->identify;
9203 identify->sas_address = hpsa_sas_port->sas_address;
9204 identify->initiator_port_protocols = SAS_PROTOCOL_STP;
9205 identify->target_port_protocols = SAS_PROTOCOL_STP;
9206
9207 return sas_rphy_add(rphy);
9208}
9209
9210static struct hpsa_sas_port
9211 *hpsa_alloc_sas_port(struct hpsa_sas_node *hpsa_sas_node,
9212 u64 sas_address)
9213{
9214 int rc;
9215 struct hpsa_sas_port *hpsa_sas_port;
9216 struct sas_port *port;
9217
9218 hpsa_sas_port = kzalloc(sizeof(*hpsa_sas_port), GFP_KERNEL);
9219 if (!hpsa_sas_port)
9220 return NULL;
9221
9222 INIT_LIST_HEAD(&hpsa_sas_port->phy_list_head);
9223 hpsa_sas_port->parent_node = hpsa_sas_node;
9224
9225 port = sas_port_alloc_num(hpsa_sas_node->parent_dev);
9226 if (!port)
9227 goto free_hpsa_port;
9228
9229 rc = sas_port_add(port);
9230 if (rc)
9231 goto free_sas_port;
9232
9233 hpsa_sas_port->port = port;
9234 hpsa_sas_port->sas_address = sas_address;
9235 list_add_tail(&hpsa_sas_port->port_list_entry,
9236 &hpsa_sas_node->port_list_head);
9237
9238 return hpsa_sas_port;
9239
9240free_sas_port:
9241 sas_port_free(port);
9242free_hpsa_port:
9243 kfree(hpsa_sas_port);
9244
9245 return NULL;
9246}
9247
9248static void hpsa_free_sas_port(struct hpsa_sas_port *hpsa_sas_port)
9249{
9250 struct hpsa_sas_phy *hpsa_sas_phy;
9251 struct hpsa_sas_phy *next;
9252
9253 list_for_each_entry_safe(hpsa_sas_phy, next,
9254 &hpsa_sas_port->phy_list_head, phy_list_entry)
9255 hpsa_free_sas_phy(hpsa_sas_phy);
9256
9257 sas_port_delete(hpsa_sas_port->port);
9258 list_del(&hpsa_sas_port->port_list_entry);
9259 kfree(hpsa_sas_port);
9260}
9261
9262static struct hpsa_sas_node *hpsa_alloc_sas_node(struct device *parent_dev)
9263{
9264 struct hpsa_sas_node *hpsa_sas_node;
9265
9266 hpsa_sas_node = kzalloc(sizeof(*hpsa_sas_node), GFP_KERNEL);
9267 if (hpsa_sas_node) {
9268 hpsa_sas_node->parent_dev = parent_dev;
9269 INIT_LIST_HEAD(&hpsa_sas_node->port_list_head);
9270 }
9271
9272 return hpsa_sas_node;
9273}
9274
9275static void hpsa_free_sas_node(struct hpsa_sas_node *hpsa_sas_node)
9276{
9277 struct hpsa_sas_port *hpsa_sas_port;
9278 struct hpsa_sas_port *next;
9279
9280 if (!hpsa_sas_node)
9281 return;
9282
9283 list_for_each_entry_safe(hpsa_sas_port, next,
9284 &hpsa_sas_node->port_list_head, port_list_entry)
9285 hpsa_free_sas_port(hpsa_sas_port);
9286
9287 kfree(hpsa_sas_node);
9288}
9289
9290static struct hpsa_scsi_dev_t
9291 *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
9292 struct sas_rphy *rphy)
9293{
9294 int i;
9295 struct hpsa_scsi_dev_t *device;
9296
9297 for (i = 0; i < h->ndevices; i++) {
9298 device = h->dev[i];
9299 if (!device->sas_port)
9300 continue;
9301 if (device->sas_port->rphy == rphy)
9302 return device;
9303 }
9304
9305 return NULL;
9306}
9307
9308static int hpsa_add_sas_host(struct ctlr_info *h)
9309{
9310 int rc;
9311 struct device *parent_dev;
9312 struct hpsa_sas_node *hpsa_sas_node;
9313 struct hpsa_sas_port *hpsa_sas_port;
9314 struct hpsa_sas_phy *hpsa_sas_phy;
9315
9316 parent_dev = &h->scsi_host->shost_gendev;
9317
9318 hpsa_sas_node = hpsa_alloc_sas_node(parent_dev);
9319 if (!hpsa_sas_node)
9320 return -ENOMEM;
9321
9322 hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, h->sas_address);
9323 if (!hpsa_sas_port) {
9324 rc = -ENODEV;
9325 goto free_sas_node;
9326 }
9327
9328 hpsa_sas_phy = hpsa_alloc_sas_phy(hpsa_sas_port);
9329 if (!hpsa_sas_phy) {
9330 rc = -ENODEV;
9331 goto free_sas_port;
9332 }
9333
9334 rc = hpsa_sas_port_add_phy(hpsa_sas_phy);
9335 if (rc)
9336 goto free_sas_phy;
9337
9338 h->sas_host = hpsa_sas_node;
9339
9340 return 0;
9341
9342free_sas_phy:
9343 hpsa_free_sas_phy(hpsa_sas_phy);
9344free_sas_port:
9345 hpsa_free_sas_port(hpsa_sas_port);
9346free_sas_node:
9347 hpsa_free_sas_node(hpsa_sas_node);
9348
9349 return rc;
9350}
9351
9352static void hpsa_delete_sas_host(struct ctlr_info *h)
9353{
9354 hpsa_free_sas_node(h->sas_host);
9355}
9356
9357static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
9358 struct hpsa_scsi_dev_t *device)
9359{
9360 int rc;
9361 struct hpsa_sas_port *hpsa_sas_port;
9362 struct sas_rphy *rphy;
9363
9364 hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, device->sas_address);
9365 if (!hpsa_sas_port)
9366 return -ENOMEM;
9367
9368 rphy = sas_end_device_alloc(hpsa_sas_port->port);
9369 if (!rphy) {
9370 rc = -ENODEV;
9371 goto free_sas_port;
9372 }
9373
9374 hpsa_sas_port->rphy = rphy;
9375 device->sas_port = hpsa_sas_port;
9376
9377 rc = hpsa_sas_port_add_rphy(hpsa_sas_port, rphy);
9378 if (rc)
9379 goto free_sas_port;
9380
9381 return 0;
9382
9383free_sas_port:
9384 hpsa_free_sas_port(hpsa_sas_port);
9385 device->sas_port = NULL;
9386
9387 return rc;
9388}
9389
9390static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device)
9391{
9392 if (device->sas_port) {
9393 hpsa_free_sas_port(device->sas_port);
9394 device->sas_port = NULL;
9395 }
9396}
9397
9398static int
9399hpsa_sas_get_linkerrors(struct sas_phy *phy)
9400{
9401 return 0;
9402}
9403
9404static int
9405hpsa_sas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
9406{
Dan Carpenteraa105692016-04-14 12:37:44 +03009407 *identifier = 0;
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009408 return 0;
9409}
9410
9411static int
9412hpsa_sas_get_bay_identifier(struct sas_rphy *rphy)
9413{
9414 return -ENXIO;
9415}
9416
9417static int
9418hpsa_sas_phy_reset(struct sas_phy *phy, int hard_reset)
9419{
9420 return 0;
9421}
9422
9423static int
9424hpsa_sas_phy_enable(struct sas_phy *phy, int enable)
9425{
9426 return 0;
9427}
9428
9429static int
9430hpsa_sas_phy_setup(struct sas_phy *phy)
9431{
9432 return 0;
9433}
9434
9435static void
9436hpsa_sas_phy_release(struct sas_phy *phy)
9437{
9438}
9439
9440static int
9441hpsa_sas_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
9442{
9443 return -EINVAL;
9444}
9445
9446/* SMP = Serial Management Protocol */
9447static int
9448hpsa_sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
9449struct request *req)
9450{
9451 return -EINVAL;
9452}
9453
9454static struct sas_function_template hpsa_sas_transport_functions = {
9455 .get_linkerrors = hpsa_sas_get_linkerrors,
9456 .get_enclosure_identifier = hpsa_sas_get_enclosure_identifier,
9457 .get_bay_identifier = hpsa_sas_get_bay_identifier,
9458 .phy_reset = hpsa_sas_phy_reset,
9459 .phy_enable = hpsa_sas_phy_enable,
9460 .phy_setup = hpsa_sas_phy_setup,
9461 .phy_release = hpsa_sas_phy_release,
9462 .set_phy_speed = hpsa_sas_phy_speed,
9463 .smp_handler = hpsa_sas_smp_handler,
9464};
9465
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009466/*
9467 * This is it. Register the PCI driver information for the cards we control
9468 * the OS will call our registered routines when it finds one of our cards.
9469 */
9470static int __init hpsa_init(void)
9471{
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009472 int rc;
9473
9474 hpsa_sas_transport_template =
9475 sas_attach_transport(&hpsa_sas_transport_functions);
9476 if (!hpsa_sas_transport_template)
9477 return -ENODEV;
9478
9479 rc = pci_register_driver(&hpsa_pci_driver);
9480
9481 if (rc)
9482 sas_release_transport(hpsa_sas_transport_template);
9483
9484 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009485}
9486
9487static void __exit hpsa_cleanup(void)
9488{
9489 pci_unregister_driver(&hpsa_pci_driver);
Kevin Barnettd04e62b2015-11-04 15:52:34 -06009490 sas_release_transport(hpsa_sas_transport_template);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009491}
9492
Matt Gatese1f7de02014-02-18 13:55:17 -06009493static void __attribute__((unused)) verify_offsets(void)
9494{
9495#define VERIFY_OFFSET(member, offset) \
Scott Teeldd0e19f2014-02-18 13:57:31 -06009496 BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
9497
9498 VERIFY_OFFSET(structure_size, 0);
9499 VERIFY_OFFSET(volume_blk_size, 4);
9500 VERIFY_OFFSET(volume_blk_cnt, 8);
9501 VERIFY_OFFSET(phys_blk_shift, 16);
9502 VERIFY_OFFSET(parity_rotation_shift, 17);
9503 VERIFY_OFFSET(strip_size, 18);
9504 VERIFY_OFFSET(disk_starting_blk, 20);
9505 VERIFY_OFFSET(disk_blk_cnt, 28);
9506 VERIFY_OFFSET(data_disks_per_row, 36);
9507 VERIFY_OFFSET(metadata_disks_per_row, 38);
9508 VERIFY_OFFSET(row_cnt, 40);
9509 VERIFY_OFFSET(layout_map_count, 42);
9510 VERIFY_OFFSET(flags, 44);
9511 VERIFY_OFFSET(dekindex, 46);
9512 /* VERIFY_OFFSET(reserved, 48 */
9513 VERIFY_OFFSET(data, 64);
9514
9515#undef VERIFY_OFFSET
9516
9517#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06009518 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
9519
9520 VERIFY_OFFSET(IU_type, 0);
9521 VERIFY_OFFSET(direction, 1);
9522 VERIFY_OFFSET(reply_queue, 2);
9523 /* VERIFY_OFFSET(reserved1, 3); */
9524 VERIFY_OFFSET(scsi_nexus, 4);
9525 VERIFY_OFFSET(Tag, 8);
9526 VERIFY_OFFSET(cdb, 16);
9527 VERIFY_OFFSET(cciss_lun, 32);
9528 VERIFY_OFFSET(data_len, 40);
9529 VERIFY_OFFSET(cmd_priority_task_attr, 44);
9530 VERIFY_OFFSET(sg_count, 45);
9531 /* VERIFY_OFFSET(reserved3 */
9532 VERIFY_OFFSET(err_ptr, 48);
9533 VERIFY_OFFSET(err_len, 56);
9534 /* VERIFY_OFFSET(reserved4 */
9535 VERIFY_OFFSET(sg, 64);
9536
9537#undef VERIFY_OFFSET
9538
9539#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06009540 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
9541
9542 VERIFY_OFFSET(dev_handle, 0x00);
9543 VERIFY_OFFSET(reserved1, 0x02);
9544 VERIFY_OFFSET(function, 0x03);
9545 VERIFY_OFFSET(reserved2, 0x04);
9546 VERIFY_OFFSET(err_info, 0x0C);
9547 VERIFY_OFFSET(reserved3, 0x10);
9548 VERIFY_OFFSET(err_info_len, 0x12);
9549 VERIFY_OFFSET(reserved4, 0x13);
9550 VERIFY_OFFSET(sgl_offset, 0x14);
9551 VERIFY_OFFSET(reserved5, 0x15);
9552 VERIFY_OFFSET(transfer_len, 0x1C);
9553 VERIFY_OFFSET(reserved6, 0x20);
9554 VERIFY_OFFSET(io_flags, 0x24);
9555 VERIFY_OFFSET(reserved7, 0x26);
9556 VERIFY_OFFSET(LUN, 0x34);
9557 VERIFY_OFFSET(control, 0x3C);
9558 VERIFY_OFFSET(CDB, 0x40);
9559 VERIFY_OFFSET(reserved8, 0x50);
9560 VERIFY_OFFSET(host_context_flags, 0x60);
9561 VERIFY_OFFSET(timeout_sec, 0x62);
9562 VERIFY_OFFSET(ReplyQueue, 0x64);
9563 VERIFY_OFFSET(reserved9, 0x65);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009564 VERIFY_OFFSET(tag, 0x68);
Matt Gatese1f7de02014-02-18 13:55:17 -06009565 VERIFY_OFFSET(host_addr, 0x70);
9566 VERIFY_OFFSET(CISS_LUN, 0x78);
9567 VERIFY_OFFSET(SG, 0x78 + 8);
9568#undef VERIFY_OFFSET
9569}
9570
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009571module_init(hpsa_init);
9572module_exit(hpsa_cleanup);