blob: 910b2d1513c0c4d74e1f0ac4d675aab28a12b955 [file] [log] [blame]
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001/*
2 * Disk Array driver for HP Smart Array SAS controllers
Don Brace1358f6d2015-07-18 11:12:38 -05003 * Copyright 2014-2015 PMC-Sierra, Inc.
4 * Copyright 2000,2009-2015 Hewlett-Packard Development Company, L.P.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more details.
14 *
Don Brace1358f6d2015-07-18 11:12:38 -050015 * Questions/Comments/Bugfixes to storagedev@pmcs.com
Stephen M. Cameronedd16362009-12-08 14:09:11 -080016 *
17 */
18
19#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/types.h>
22#include <linux/pci.h>
Matthew Garrette5a44df2011-11-11 11:14:23 -050023#include <linux/pci-aspm.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080024#include <linux/kernel.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
27#include <linux/fs.h>
28#include <linux/timer.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080029#include <linux/init.h>
30#include <linux/spinlock.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080031#include <linux/compat.h>
32#include <linux/blktrace_api.h>
33#include <linux/uaccess.h>
34#include <linux/io.h>
35#include <linux/dma-mapping.h>
36#include <linux/completion.h>
37#include <linux/moduleparam.h>
38#include <scsi/scsi.h>
39#include <scsi/scsi_cmnd.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_host.h>
Stephen M. Cameron667e23d2010-02-25 14:02:51 -060042#include <scsi/scsi_tcq.h>
Stephen Cameron9437ac42015-04-23 09:32:16 -050043#include <scsi/scsi_eh.h>
Webb Scales73153fe2015-04-23 09:35:04 -050044#include <scsi/scsi_dbg.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080045#include <linux/cciss_ioctl.h>
46#include <linux/string.h>
47#include <linux/bitmap.h>
Arun Sharma600634972011-07-26 16:09:06 -070048#include <linux/atomic.h>
Stephen M. Camerona0c12412011-10-26 16:22:04 -050049#include <linux/jiffies.h>
Don Brace42a91642014-11-14 17:26:27 -060050#include <linux/percpu-defs.h>
Stephen M. Cameron094963d2014-05-29 10:53:18 -050051#include <linux/percpu.h>
Don Brace2b08b3e2015-01-23 16:41:09 -060052#include <asm/unaligned.h>
Stephen M. Cameron283b4a92014-02-18 13:55:33 -060053#include <asm/div64.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080054#include "hpsa_cmd.h"
55#include "hpsa.h"
56
57/* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
Don Bracef532a3f2015-04-23 09:35:33 -050058#define HPSA_DRIVER_VERSION "3.4.10-0"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080059#define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -060060#define HPSA "hpsa"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080061
Robert Elliott007e7aa2015-01-23 16:44:56 -060062/* How long to wait for CISS doorbell communication */
63#define CLEAR_EVENT_WAIT_INTERVAL 20 /* ms for each msleep() call */
64#define MODE_CHANGE_WAIT_INTERVAL 10 /* ms for each msleep() call */
65#define MAX_CLEAR_EVENT_WAIT 30000 /* times 20 ms = 600 s */
66#define MAX_MODE_CHANGE_WAIT 2000 /* times 10 ms = 20 s */
Stephen M. Cameronedd16362009-12-08 14:09:11 -080067#define MAX_IOCTL_CONFIG_WAIT 1000
68
69/*define how many times we will try a command because of bus resets */
70#define MAX_CMD_RETRIES 3
71
72/* Embedded module documentation macros - see modules.h */
73MODULE_AUTHOR("Hewlett-Packard Company");
74MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
75 HPSA_DRIVER_VERSION);
76MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
77MODULE_VERSION(HPSA_DRIVER_VERSION);
78MODULE_LICENSE("GPL");
79
80static int hpsa_allow_any;
81module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
82MODULE_PARM_DESC(hpsa_allow_any,
83 "Allow hpsa driver to access unknown HP Smart Array hardware");
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -060084static int hpsa_simple_mode;
85module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
86MODULE_PARM_DESC(hpsa_simple_mode,
87 "Use 'simple mode' rather than 'performant mode'");
Stephen M. Cameronedd16362009-12-08 14:09:11 -080088
89/* define the PCI info for the cards we can control */
90static const struct pci_device_id hpsa_pci_device_id[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -080091 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
92 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
93 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
94 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
95 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
Mike Miller163dbcd2013-09-04 15:11:10 -050096 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
97 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
Mike Millerf8b01eb2010-02-04 08:42:45 -060098 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
scameron@beardog.cce.hp.com9143a962011-03-07 10:44:16 -060099 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350},
100 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351},
101 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352},
102 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353},
103 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354},
104 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355},
105 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356},
Mike Millerfe0c9612012-09-20 16:05:18 -0500106 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1921},
107 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1922},
108 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1923},
109 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1924},
Mike Millerfe0c9612012-09-20 16:05:18 -0500110 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1926},
111 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1928},
Mike Miller97b9f532013-09-04 15:05:55 -0500112 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1929},
113 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BD},
114 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BE},
115 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BF},
116 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C0},
117 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C1},
118 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C2},
119 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C3},
120 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C4},
121 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C5},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500122 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C6},
Mike Miller97b9f532013-09-04 15:05:55 -0500123 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C7},
124 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C8},
125 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C9},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500126 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CA},
127 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CB},
128 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CC},
129 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CD},
130 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CE},
Don Bracefdfa4b62015-04-23 09:35:27 -0500131 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0580},
Don Bracecbb47dc2015-07-18 11:12:54 -0500132 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0581},
133 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0582},
134 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0583},
135 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0584},
136 {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0585},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600137 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
138 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
139 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
140 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
141 {PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
Mike Miller7c03b872010-12-01 11:16:07 -0600142 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
Stephen M. Cameron6798cc02010-06-16 13:51:20 -0500143 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800144 {0,}
145};
146
147MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
148
149/* board_id = Subsystem Device ID & Vendor ID
150 * product = Marketing Name for the board
151 * access = Address of the struct of function pointers
152 */
153static struct board_type products[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800154 {0x3241103C, "Smart Array P212", &SA5_access},
155 {0x3243103C, "Smart Array P410", &SA5_access},
156 {0x3245103C, "Smart Array P410i", &SA5_access},
157 {0x3247103C, "Smart Array P411", &SA5_access},
158 {0x3249103C, "Smart Array P812", &SA5_access},
Mike Miller163dbcd2013-09-04 15:11:10 -0500159 {0x324A103C, "Smart Array P712m", &SA5_access},
160 {0x324B103C, "Smart Array P711m", &SA5_access},
Stephen M. Cameron7d2cce52014-11-14 17:26:38 -0600161 {0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
Mike Millerfe0c9612012-09-20 16:05:18 -0500162 {0x3350103C, "Smart Array P222", &SA5_access},
163 {0x3351103C, "Smart Array P420", &SA5_access},
164 {0x3352103C, "Smart Array P421", &SA5_access},
165 {0x3353103C, "Smart Array P822", &SA5_access},
166 {0x3354103C, "Smart Array P420i", &SA5_access},
167 {0x3355103C, "Smart Array P220i", &SA5_access},
168 {0x3356103C, "Smart Array P721m", &SA5_access},
Mike Miller1fd6c8e2013-09-04 15:08:29 -0500169 {0x1921103C, "Smart Array P830i", &SA5_access},
170 {0x1922103C, "Smart Array P430", &SA5_access},
171 {0x1923103C, "Smart Array P431", &SA5_access},
172 {0x1924103C, "Smart Array P830", &SA5_access},
173 {0x1926103C, "Smart Array P731m", &SA5_access},
174 {0x1928103C, "Smart Array P230i", &SA5_access},
175 {0x1929103C, "Smart Array P530", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600176 {0x21BD103C, "Smart Array P244br", &SA5_access},
177 {0x21BE103C, "Smart Array P741m", &SA5_access},
178 {0x21BF103C, "Smart HBA H240ar", &SA5_access},
179 {0x21C0103C, "Smart Array P440ar", &SA5_access},
Don Bracec8ae0ab2015-01-23 16:45:12 -0600180 {0x21C1103C, "Smart Array P840ar", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600181 {0x21C2103C, "Smart Array P440", &SA5_access},
182 {0x21C3103C, "Smart Array P441", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500183 {0x21C4103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600184 {0x21C5103C, "Smart Array P841", &SA5_access},
185 {0x21C6103C, "Smart HBA H244br", &SA5_access},
186 {0x21C7103C, "Smart HBA H240", &SA5_access},
187 {0x21C8103C, "Smart HBA H241", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500188 {0x21C9103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600189 {0x21CA103C, "Smart Array P246br", &SA5_access},
190 {0x21CB103C, "Smart Array P840", &SA5_access},
Joe Handzik3b7a45e2014-05-08 14:27:24 -0500191 {0x21CC103C, "Smart Array", &SA5_access},
192 {0x21CD103C, "Smart Array", &SA5_access},
Don Brace27fb8132015-01-23 16:45:07 -0600193 {0x21CE103C, "Smart HBA", &SA5_access},
Don Bracefdfa4b62015-04-23 09:35:27 -0500194 {0x05809005, "SmartHBA-SA", &SA5_access},
Don Bracecbb47dc2015-07-18 11:12:54 -0500195 {0x05819005, "SmartHBA-SA 8i", &SA5_access},
196 {0x05829005, "SmartHBA-SA 8i8e", &SA5_access},
197 {0x05839005, "SmartHBA-SA 8e", &SA5_access},
198 {0x05849005, "SmartHBA-SA 16i", &SA5_access},
199 {0x05859005, "SmartHBA-SA 4i4e", &SA5_access},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600200 {0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
201 {0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
202 {0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
203 {0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
204 {0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800205 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
206};
207
Webb Scalesa58e7e52015-04-23 09:34:16 -0500208#define SCSI_CMD_BUSY ((struct scsi_cmnd *)&hpsa_cmd_busy)
209static const struct scsi_cmnd hpsa_cmd_busy;
210#define SCSI_CMD_IDLE ((struct scsi_cmnd *)&hpsa_cmd_idle)
211static const struct scsi_cmnd hpsa_cmd_idle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800212static int number_of_controllers;
213
Stephen M. Cameron10f66012010-06-16 13:51:50 -0500214static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
215static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
Don Brace42a91642014-11-14 17:26:27 -0600216static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800217
218#ifdef CONFIG_COMPAT
Don Brace42a91642014-11-14 17:26:27 -0600219static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
220 void __user *arg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800221#endif
222
223static void cmd_free(struct ctlr_info *h, struct CommandList *c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800224static struct CommandList *cmd_alloc(struct ctlr_info *h);
Webb Scales73153fe2015-04-23 09:35:04 -0500225static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c);
226static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
227 struct scsi_cmnd *scmd);
Stephen M. Camerona2dac132013-02-20 11:24:41 -0600228static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600229 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800230 int cmd_type);
Robert Elliott2c143342015-01-23 16:42:48 -0600231static void hpsa_free_cmd_pool(struct ctlr_info *h);
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600232#define VPD_PAGE (1 << 8)
Don Braceb48d9802015-11-04 15:50:13 -0600233#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800234
Jeff Garzikf2812332010-11-16 02:10:29 -0500235static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Stephen M. Camerona08a8472010-02-04 08:43:16 -0600236static void hpsa_scan_start(struct Scsi_Host *);
237static int hpsa_scan_finished(struct Scsi_Host *sh,
238 unsigned long elapsed_time);
Don Brace7c0a0222015-01-23 16:41:30 -0600239static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800240
241static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500242static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800243static int hpsa_slave_alloc(struct scsi_device *sdev);
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500244static int hpsa_slave_configure(struct scsi_device *sdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800245static void hpsa_slave_destroy(struct scsi_device *sdev);
246
Don Brace8aa60682015-11-04 15:50:01 -0600247static void hpsa_update_scsi_devices(struct ctlr_info *h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800248static int check_for_unit_attention(struct ctlr_info *h,
249 struct CommandList *c);
250static void check_ioctl_unit_attention(struct ctlr_info *h,
251 struct CommandList *c);
Don Brace303932f2010-02-04 08:42:40 -0600252/* performant mode helper functions */
253static void calc_bucket_map(int *bucket, int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -0600254 int nsgs, int min_blocks, u32 *bucket_map);
Robert Elliott105a3db2015-04-23 09:33:48 -0500255static void hpsa_free_performant_mode(struct ctlr_info *h);
256static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
Matt Gates254f7962012-05-01 11:43:06 -0500257static inline u32 next_command(struct ctlr_info *h, u8 q);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800258static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
259 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
260 u64 *cfg_offset);
261static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
262 unsigned long *memory_bar);
263static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
264static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
265 int wait_for_ready);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500266static inline void finish_cmd(struct CommandList *c);
Robert Elliottc706a792015-01-23 16:45:01 -0600267static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -0600268#define BOARD_NOT_READY 0
269#define BOARD_READY 1
Stephen M. Cameron23100dd2014-02-18 13:57:37 -0600270static void hpsa_drain_accel_commands(struct ctlr_info *h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -0600271static void hpsa_flush_cache(struct ctlr_info *h);
Scott Teelc3497752014-02-18 13:56:34 -0600272static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
273 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -0600274 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
Don Brace080ef1c2015-01-23 16:43:25 -0600275static void hpsa_command_resubmit_worker(struct work_struct *work);
Webb Scales25163bd2015-04-23 09:32:00 -0500276static u32 lockup_detected(struct ctlr_info *h);
277static int detect_controller_lockup(struct ctlr_info *h);
Scott Teelc2adae42015-11-04 15:52:16 -0600278static void hpsa_disable_rld_caching(struct ctlr_info *h);
Scott Teel34592252015-11-04 15:52:09 -0600279static int hpsa_luns_changed(struct ctlr_info *h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800280
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800281static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
282{
283 unsigned long *priv = shost_priv(sdev->host);
284 return (struct ctlr_info *) *priv;
285}
286
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600287static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
288{
289 unsigned long *priv = shost_priv(sh);
290 return (struct ctlr_info *) *priv;
291}
292
Webb Scalesa58e7e52015-04-23 09:34:16 -0500293static inline bool hpsa_is_cmd_idle(struct CommandList *c)
294{
295 return c->scsi_cmd == SCSI_CMD_IDLE;
296}
297
Webb Scalesd604f532015-04-23 09:35:22 -0500298static inline bool hpsa_is_pending_event(struct CommandList *c)
299{
300 return c->abort_pending || c->reset_pending;
301}
302
Stephen Cameron9437ac42015-04-23 09:32:16 -0500303/* extract sense key, asc, and ascq from sense data. -1 means invalid. */
304static void decode_sense_data(const u8 *sense_data, int sense_data_len,
305 u8 *sense_key, u8 *asc, u8 *ascq)
306{
307 struct scsi_sense_hdr sshdr;
308 bool rc;
309
310 *sense_key = -1;
311 *asc = -1;
312 *ascq = -1;
313
314 if (sense_data_len < 1)
315 return;
316
317 rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
318 if (rc) {
319 *sense_key = sshdr.sense_key;
320 *asc = sshdr.asc;
321 *ascq = sshdr.ascq;
322 }
323}
324
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800325static int check_for_unit_attention(struct ctlr_info *h,
326 struct CommandList *c)
327{
Stephen Cameron9437ac42015-04-23 09:32:16 -0500328 u8 sense_key, asc, ascq;
329 int sense_len;
330
331 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
332 sense_len = sizeof(c->err_info->SenseInfo);
333 else
334 sense_len = c->err_info->SenseLen;
335
336 decode_sense_data(c->err_info->SenseInfo, sense_len,
337 &sense_key, &asc, &ascq);
Don Brace81c27552015-07-18 11:12:28 -0500338 if (sense_key != UNIT_ATTENTION || asc == 0xff)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800339 return 0;
340
Stephen Cameron9437ac42015-04-23 09:32:16 -0500341 switch (asc) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800342 case STATE_CHANGED:
Stephen Cameron9437ac42015-04-23 09:32:16 -0500343 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500344 "%s: a state change detected, command retried\n",
345 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800346 break;
347 case LUN_FAILED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600348 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500349 "%s: LUN failure detected\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800350 break;
351 case REPORT_LUNS_CHANGED:
Stephen M. Cameron7f736952014-11-14 17:26:48 -0600352 dev_warn(&h->pdev->dev,
Robert Elliott2946e822015-04-23 09:35:09 -0500353 "%s: report LUN data changed\n", h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800354 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600355 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
356 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800357 */
358 break;
359 case POWER_OR_RESET:
Robert Elliott2946e822015-04-23 09:35:09 -0500360 dev_warn(&h->pdev->dev,
361 "%s: a power on or device reset detected\n",
362 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800363 break;
364 case UNIT_ATTENTION_CLEARED:
Robert Elliott2946e822015-04-23 09:35:09 -0500365 dev_warn(&h->pdev->dev,
366 "%s: unit attention cleared by another initiator\n",
367 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800368 break;
369 default:
Robert Elliott2946e822015-04-23 09:35:09 -0500370 dev_warn(&h->pdev->dev,
371 "%s: unknown unit attention detected\n",
372 h->devname);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800373 break;
374 }
375 return 1;
376}
377
Matt Bondurant852af202012-05-01 11:42:35 -0500378static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
379{
380 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
381 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
382 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
383 return 0;
384 dev_warn(&h->pdev->dev, HPSA "device busy");
385 return 1;
386}
387
Stephen Camerone985c582015-04-23 09:32:22 -0500388static u32 lockup_detected(struct ctlr_info *h);
389static ssize_t host_show_lockup_detected(struct device *dev,
390 struct device_attribute *attr, char *buf)
391{
392 int ld;
393 struct ctlr_info *h;
394 struct Scsi_Host *shost = class_to_shost(dev);
395
396 h = shost_to_hba(shost);
397 ld = lockup_detected(h);
398
399 return sprintf(buf, "ld=%d\n", ld);
400}
401
Scott Teelda0697b2014-02-18 13:57:00 -0600402static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
403 struct device_attribute *attr,
404 const char *buf, size_t count)
405{
406 int status, len;
407 struct ctlr_info *h;
408 struct Scsi_Host *shost = class_to_shost(dev);
409 char tmpbuf[10];
410
411 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
412 return -EACCES;
413 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
414 strncpy(tmpbuf, buf, len);
415 tmpbuf[len] = '\0';
416 if (sscanf(tmpbuf, "%d", &status) != 1)
417 return -EINVAL;
418 h = shost_to_hba(shost);
419 h->acciopath_status = !!status;
420 dev_warn(&h->pdev->dev,
421 "hpsa: HP SSD Smart Path %s via sysfs update.\n",
422 h->acciopath_status ? "enabled" : "disabled");
423 return count;
424}
425
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600426static ssize_t host_store_raid_offload_debug(struct device *dev,
427 struct device_attribute *attr,
428 const char *buf, size_t count)
429{
430 int debug_level, len;
431 struct ctlr_info *h;
432 struct Scsi_Host *shost = class_to_shost(dev);
433 char tmpbuf[10];
434
435 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
436 return -EACCES;
437 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
438 strncpy(tmpbuf, buf, len);
439 tmpbuf[len] = '\0';
440 if (sscanf(tmpbuf, "%d", &debug_level) != 1)
441 return -EINVAL;
442 if (debug_level < 0)
443 debug_level = 0;
444 h = shost_to_hba(shost);
445 h->raid_offload_debug = debug_level;
446 dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
447 h->raid_offload_debug);
448 return count;
449}
450
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800451static ssize_t host_store_rescan(struct device *dev,
452 struct device_attribute *attr,
453 const char *buf, size_t count)
454{
455 struct ctlr_info *h;
456 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600457 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600458 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800459 return count;
460}
461
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500462static ssize_t host_show_firmware_revision(struct device *dev,
463 struct device_attribute *attr, char *buf)
464{
465 struct ctlr_info *h;
466 struct Scsi_Host *shost = class_to_shost(dev);
467 unsigned char *fwrev;
468
469 h = shost_to_hba(shost);
470 if (!h->hba_inquiry_data)
471 return 0;
472 fwrev = &h->hba_inquiry_data[32];
473 return snprintf(buf, 20, "%c%c%c%c\n",
474 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
475}
476
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600477static ssize_t host_show_commands_outstanding(struct device *dev,
478 struct device_attribute *attr, char *buf)
479{
480 struct Scsi_Host *shost = class_to_shost(dev);
481 struct ctlr_info *h = shost_to_hba(shost);
482
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600483 return snprintf(buf, 20, "%d\n",
484 atomic_read(&h->commands_outstanding));
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600485}
486
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600487static ssize_t host_show_transport_mode(struct device *dev,
488 struct device_attribute *attr, char *buf)
489{
490 struct ctlr_info *h;
491 struct Scsi_Host *shost = class_to_shost(dev);
492
493 h = shost_to_hba(shost);
494 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e72011-02-15 15:33:03 -0600495 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600496 "performant" : "simple");
497}
498
Scott Teelda0697b2014-02-18 13:57:00 -0600499static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
500 struct device_attribute *attr, char *buf)
501{
502 struct ctlr_info *h;
503 struct Scsi_Host *shost = class_to_shost(dev);
504
505 h = shost_to_hba(shost);
506 return snprintf(buf, 30, "HP SSD Smart Path %s\n",
507 (h->acciopath_status == 1) ? "enabled" : "disabled");
508}
509
Stephen M. Cameron46380782011-05-03 15:00:01 -0500510/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600511static u32 unresettable_controller[] = {
512 0x324a103C, /* Smart Array P712m */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500513 0x324b103C, /* Smart Array P711m */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600514 0x3223103C, /* Smart Array P800 */
515 0x3234103C, /* Smart Array P400 */
516 0x3235103C, /* Smart Array P400i */
517 0x3211103C, /* Smart Array E200i */
518 0x3212103C, /* Smart Array E200 */
519 0x3213103C, /* Smart Array E200i */
520 0x3214103C, /* Smart Array E200i */
521 0x3215103C, /* Smart Array E200i */
522 0x3237103C, /* Smart Array E500 */
523 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100524 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600525 0x409C0E11, /* Smart Array 6400 */
526 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100527 0x40700E11, /* Smart Array 5300 */
528 0x40820E11, /* Smart Array 532 */
529 0x40830E11, /* Smart Array 5312 */
530 0x409A0E11, /* Smart Array 641 */
531 0x409B0E11, /* Smart Array 642 */
532 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600533};
534
Stephen M. Cameron46380782011-05-03 15:00:01 -0500535/* List of controllers which cannot even be soft reset */
536static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100537 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100538 0x40700E11, /* Smart Array 5300 */
539 0x40820E11, /* Smart Array 532 */
540 0x40830E11, /* Smart Array 5312 */
541 0x409A0E11, /* Smart Array 641 */
542 0x409B0E11, /* Smart Array 642 */
543 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500544 /* Exclude 640x boards. These are two pci devices in one slot
545 * which share a battery backed cache module. One controls the
546 * cache, the other accesses the cache through the one that controls
547 * it. If we reset the one controlling the cache, the other will
548 * likely not be happy. Just forbid resetting this conjoined mess.
549 * The 640x isn't really supported by hpsa anyway.
550 */
551 0x409C0E11, /* Smart Array 6400 */
552 0x409D0E11, /* Smart Array 6400 EM */
553};
554
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500555static u32 needs_abort_tags_swizzled[] = {
556 0x323D103C, /* Smart Array P700m */
557 0x324a103C, /* Smart Array P712m */
558 0x324b103C, /* SmartArray P711m */
559};
560
561static int board_id_in_array(u32 a[], int nelems, u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600562{
563 int i;
564
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500565 for (i = 0; i < nelems; i++)
566 if (a[i] == board_id)
567 return 1;
568 return 0;
569}
570
571static int ctlr_is_hard_resettable(u32 board_id)
572{
573 return !board_id_in_array(unresettable_controller,
574 ARRAY_SIZE(unresettable_controller), board_id);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600575}
576
Stephen M. Cameron46380782011-05-03 15:00:01 -0500577static int ctlr_is_soft_resettable(u32 board_id)
578{
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500579 return !board_id_in_array(soft_unresettable_controller,
580 ARRAY_SIZE(soft_unresettable_controller), board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500581}
582
583static int ctlr_is_resettable(u32 board_id)
584{
585 return ctlr_is_hard_resettable(board_id) ||
586 ctlr_is_soft_resettable(board_id);
587}
588
Stephen Cameron9b5c48c2015-04-23 09:32:06 -0500589static int ctlr_needs_abort_tags_swizzled(u32 board_id)
590{
591 return board_id_in_array(needs_abort_tags_swizzled,
592 ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
593}
594
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600595static ssize_t host_show_resettable(struct device *dev,
596 struct device_attribute *attr, char *buf)
597{
598 struct ctlr_info *h;
599 struct Scsi_Host *shost = class_to_shost(dev);
600
601 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500602 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600603}
604
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800605static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
606{
607 return (scsi3addr[3] & 0xC0) == 0x40;
608}
609
Robert Elliottf2ef0ce2015-01-23 16:41:35 -0600610static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
Don Brace7c59a0d2015-11-04 15:52:22 -0600611 "1(+0)ADM", "UNKNOWN", "PHYS DRV"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800612};
Scott Teel6b80b182014-02-18 13:56:55 -0600613#define HPSA_RAID_0 0
614#define HPSA_RAID_4 1
615#define HPSA_RAID_1 2 /* also used for RAID 10 */
616#define HPSA_RAID_5 3 /* also used for RAID 50 */
617#define HPSA_RAID_51 4
618#define HPSA_RAID_6 5 /* also used for RAID 60 */
619#define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
Don Brace7c59a0d2015-11-04 15:52:22 -0600620#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 2)
621#define PHYSICAL_DRIVE (ARRAY_SIZE(raid_label) - 1)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800622
Kevin Barnettf3f01732015-11-04 15:51:33 -0600623static inline bool is_logical_device(struct hpsa_scsi_dev_t *device)
624{
625 return !device->physical_device;
626}
627
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800628static ssize_t raid_level_show(struct device *dev,
629 struct device_attribute *attr, char *buf)
630{
631 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600632 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800633 struct ctlr_info *h;
634 struct scsi_device *sdev;
635 struct hpsa_scsi_dev_t *hdev;
636 unsigned long flags;
637
638 sdev = to_scsi_device(dev);
639 h = sdev_to_hba(sdev);
640 spin_lock_irqsave(&h->lock, flags);
641 hdev = sdev->hostdata;
642 if (!hdev) {
643 spin_unlock_irqrestore(&h->lock, flags);
644 return -ENODEV;
645 }
646
647 /* Is this even a logical drive? */
Kevin Barnettf3f01732015-11-04 15:51:33 -0600648 if (!is_logical_device(hdev)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800649 spin_unlock_irqrestore(&h->lock, flags);
650 l = snprintf(buf, PAGE_SIZE, "N/A\n");
651 return l;
652 }
653
654 rlevel = hdev->raid_level;
655 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600656 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800657 rlevel = RAID_UNKNOWN;
658 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
659 return l;
660}
661
662static ssize_t lunid_show(struct device *dev,
663 struct device_attribute *attr, char *buf)
664{
665 struct ctlr_info *h;
666 struct scsi_device *sdev;
667 struct hpsa_scsi_dev_t *hdev;
668 unsigned long flags;
669 unsigned char lunid[8];
670
671 sdev = to_scsi_device(dev);
672 h = sdev_to_hba(sdev);
673 spin_lock_irqsave(&h->lock, flags);
674 hdev = sdev->hostdata;
675 if (!hdev) {
676 spin_unlock_irqrestore(&h->lock, flags);
677 return -ENODEV;
678 }
679 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
680 spin_unlock_irqrestore(&h->lock, flags);
681 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
682 lunid[0], lunid[1], lunid[2], lunid[3],
683 lunid[4], lunid[5], lunid[6], lunid[7]);
684}
685
686static ssize_t unique_id_show(struct device *dev,
687 struct device_attribute *attr, char *buf)
688{
689 struct ctlr_info *h;
690 struct scsi_device *sdev;
691 struct hpsa_scsi_dev_t *hdev;
692 unsigned long flags;
693 unsigned char sn[16];
694
695 sdev = to_scsi_device(dev);
696 h = sdev_to_hba(sdev);
697 spin_lock_irqsave(&h->lock, flags);
698 hdev = sdev->hostdata;
699 if (!hdev) {
700 spin_unlock_irqrestore(&h->lock, flags);
701 return -ENODEV;
702 }
703 memcpy(sn, hdev->device_id, sizeof(sn));
704 spin_unlock_irqrestore(&h->lock, flags);
705 return snprintf(buf, 16 * 2 + 2,
706 "%02X%02X%02X%02X%02X%02X%02X%02X"
707 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
708 sn[0], sn[1], sn[2], sn[3],
709 sn[4], sn[5], sn[6], sn[7],
710 sn[8], sn[9], sn[10], sn[11],
711 sn[12], sn[13], sn[14], sn[15]);
712}
713
Scott Teelc1988682014-02-18 13:55:54 -0600714static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
715 struct device_attribute *attr, char *buf)
716{
717 struct ctlr_info *h;
718 struct scsi_device *sdev;
719 struct hpsa_scsi_dev_t *hdev;
720 unsigned long flags;
721 int offload_enabled;
722
723 sdev = to_scsi_device(dev);
724 h = sdev_to_hba(sdev);
725 spin_lock_irqsave(&h->lock, flags);
726 hdev = sdev->hostdata;
727 if (!hdev) {
728 spin_unlock_irqrestore(&h->lock, flags);
729 return -ENODEV;
730 }
731 offload_enabled = hdev->offload_enabled;
732 spin_unlock_irqrestore(&h->lock, flags);
733 return snprintf(buf, 20, "%d\n", offload_enabled);
734}
735
Joe Handzik8270b862015-07-18 11:12:43 -0500736#define MAX_PATHS 8
Joe Handzik8270b862015-07-18 11:12:43 -0500737
738static ssize_t path_info_show(struct device *dev,
739 struct device_attribute *attr, char *buf)
740{
741 struct ctlr_info *h;
742 struct scsi_device *sdev;
743 struct hpsa_scsi_dev_t *hdev;
744 unsigned long flags;
745 int i;
746 int output_len = 0;
747 u8 box;
748 u8 bay;
749 u8 path_map_index = 0;
750 char *active;
751 unsigned char phys_connector[2];
Joe Handzik8270b862015-07-18 11:12:43 -0500752
Joe Handzik8270b862015-07-18 11:12:43 -0500753 sdev = to_scsi_device(dev);
754 h = sdev_to_hba(sdev);
755 spin_lock_irqsave(&h->devlock, flags);
756 hdev = sdev->hostdata;
757 if (!hdev) {
758 spin_unlock_irqrestore(&h->devlock, flags);
759 return -ENODEV;
760 }
761
762 bay = hdev->bay;
763 for (i = 0; i < MAX_PATHS; i++) {
764 path_map_index = 1<<i;
765 if (i == hdev->active_path_index)
766 active = "Active";
767 else if (hdev->path_map & path_map_index)
768 active = "Inactive";
769 else
770 continue;
771
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600772 output_len += scnprintf(buf + output_len,
773 PAGE_SIZE - output_len,
774 "[%d:%d:%d:%d] %20.20s ",
Joe Handzik8270b862015-07-18 11:12:43 -0500775 h->scsi_host->host_no,
776 hdev->bus, hdev->target, hdev->lun,
777 scsi_device_type(hdev->devtype));
778
Scott Teel66749d02015-11-04 15:51:57 -0600779 if (hdev->external ||
Kevin Barnettf3f01732015-11-04 15:51:33 -0600780 hdev->devtype == TYPE_RAID ||
781 is_logical_device(hdev)) {
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600782 output_len += snprintf(buf + output_len,
783 PAGE_SIZE - output_len,
784 "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500785 continue;
786 }
787
788 box = hdev->box[i];
789 memcpy(&phys_connector, &hdev->phys_connector[i],
790 sizeof(phys_connector));
791 if (phys_connector[0] < '0')
792 phys_connector[0] = '0';
793 if (phys_connector[1] < '0')
794 phys_connector[1] = '0';
795 if (hdev->phys_connector[i] > 0)
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600796 output_len += snprintf(buf + output_len,
797 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500798 "PORT: %.2s ",
799 phys_connector);
Kevin Barnett2a168202015-11-04 15:51:21 -0600800 if (hdev->devtype == TYPE_DISK && hdev->expose_device) {
Joe Handzik8270b862015-07-18 11:12:43 -0500801 if (box == 0 || box == 0xFF) {
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600802 output_len += snprintf(buf + output_len,
803 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500804 "BAY: %hhu %s\n",
805 bay, active);
806 } else {
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600807 output_len += snprintf(buf + output_len,
808 PAGE_SIZE - output_len,
Joe Handzik8270b862015-07-18 11:12:43 -0500809 "BOX: %hhu BAY: %hhu %s\n",
810 box, bay, active);
811 }
812 } else if (box != 0 && box != 0xFF) {
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600813 output_len += snprintf(buf + output_len,
814 PAGE_SIZE - output_len, "BOX: %hhu %s\n",
Joe Handzik8270b862015-07-18 11:12:43 -0500815 box, active);
816 } else
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600817 output_len += snprintf(buf + output_len,
818 PAGE_SIZE - output_len, "%s\n", active);
Joe Handzik8270b862015-07-18 11:12:43 -0500819 }
820
821 spin_unlock_irqrestore(&h->devlock, flags);
Rasmus Villemoes1faf0722015-11-04 15:52:28 -0600822 return output_len;
Joe Handzik8270b862015-07-18 11:12:43 -0500823}
824
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600825static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
826static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
827static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
828static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Scott Teelc1988682014-02-18 13:55:54 -0600829static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
830 host_show_hp_ssd_smart_path_enabled, NULL);
Joe Handzik8270b862015-07-18 11:12:43 -0500831static DEVICE_ATTR(path_info, S_IRUGO, path_info_show, NULL);
Scott Teelda0697b2014-02-18 13:57:00 -0600832static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
833 host_show_hp_ssd_smart_path_status,
834 host_store_hp_ssd_smart_path_status);
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600835static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
836 host_store_raid_offload_debug);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600837static DEVICE_ATTR(firmware_revision, S_IRUGO,
838 host_show_firmware_revision, NULL);
839static DEVICE_ATTR(commands_outstanding, S_IRUGO,
840 host_show_commands_outstanding, NULL);
841static DEVICE_ATTR(transport_mode, S_IRUGO,
842 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600843static DEVICE_ATTR(resettable, S_IRUGO,
844 host_show_resettable, NULL);
Stephen Camerone985c582015-04-23 09:32:22 -0500845static DEVICE_ATTR(lockup_detected, S_IRUGO,
846 host_show_lockup_detected, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600847
848static struct device_attribute *hpsa_sdev_attrs[] = {
849 &dev_attr_raid_level,
850 &dev_attr_lunid,
851 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600852 &dev_attr_hp_ssd_smart_path_enabled,
Joe Handzik8270b862015-07-18 11:12:43 -0500853 &dev_attr_path_info,
Stephen Camerone985c582015-04-23 09:32:22 -0500854 &dev_attr_lockup_detected,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600855 NULL,
856};
857
858static struct device_attribute *hpsa_shost_attrs[] = {
859 &dev_attr_rescan,
860 &dev_attr_firmware_revision,
861 &dev_attr_commands_outstanding,
862 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600863 &dev_attr_resettable,
Scott Teelda0697b2014-02-18 13:57:00 -0600864 &dev_attr_hp_ssd_smart_path_status,
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600865 &dev_attr_raid_offload_debug,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600866 NULL,
867};
868
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500869#define HPSA_NRESERVED_CMDS (HPSA_CMDS_RESERVED_FOR_ABORTS + \
870 HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
871
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600872static struct scsi_host_template hpsa_driver_template = {
873 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600874 .name = HPSA,
875 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600876 .queuecommand = hpsa_scsi_queue_command,
877 .scan_start = hpsa_scan_start,
878 .scan_finished = hpsa_scan_finished,
Don Brace7c0a0222015-01-23 16:41:30 -0600879 .change_queue_depth = hpsa_change_queue_depth,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600880 .this_id = -1,
881 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500882 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600883 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
884 .ioctl = hpsa_ioctl,
885 .slave_alloc = hpsa_slave_alloc,
Stephen Cameron41ce4c32015-04-23 09:31:47 -0500886 .slave_configure = hpsa_slave_configure,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600887 .slave_destroy = hpsa_slave_destroy,
888#ifdef CONFIG_COMPAT
889 .compat_ioctl = hpsa_compat_ioctl,
890#endif
891 .sdev_attrs = hpsa_sdev_attrs,
892 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500893 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400894 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600895};
896
Matt Gates254f7962012-05-01 11:43:06 -0500897static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600898{
899 u32 a;
Stephen M. Cameron072b0512014-05-29 10:53:07 -0500900 struct reply_queue_buffer *rq = &h->reply_queue[q];
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600901
Matt Gatese1f7de02014-02-18 13:55:17 -0600902 if (h->transMethod & CFGTBL_Trans_io_accel1)
903 return h->access.command_completed(h, q);
904
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600905 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500906 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600907
Matt Gates254f7962012-05-01 11:43:06 -0500908 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
909 a = rq->head[rq->current_entry];
910 rq->current_entry++;
Stephen M. Cameron0cbf7682014-11-14 17:27:09 -0600911 atomic_dec(&h->commands_outstanding);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600912 } else {
913 a = FIFO_EMPTY;
914 }
915 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500916 if (rq->current_entry == h->max_commands) {
917 rq->current_entry = 0;
918 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600919 }
920 return a;
921}
922
Scott Teelc3497752014-02-18 13:56:34 -0600923/*
924 * There are some special bits in the bus address of the
925 * command that we have to set for the controller to know
926 * how to process the command:
927 *
928 * Normal performant mode:
929 * bit 0: 1 means performant mode, 0 means simple mode.
930 * bits 1-3 = block fetch table entry
931 * bits 4-6 = command type (== 0)
932 *
933 * ioaccel1 mode:
934 * bit 0 = "performant mode" bit.
935 * bits 1-3 = block fetch table entry
936 * bits 4-6 = command type (== 110)
937 * (command type is needed because ioaccel1 mode
938 * commands are submitted through the same register as normal
939 * mode commands, so this is how the controller knows whether
940 * the command is normal mode or ioaccel1 mode.)
941 *
942 * ioaccel2 mode:
943 * bit 0 = "performant mode" bit.
944 * bits 1-4 = block fetch table entry (note extra bit)
945 * bits 4-6 = not needed, because ioaccel2 mode has
946 * a separate special register for submitting commands.
947 */
948
Webb Scales25163bd2015-04-23 09:32:00 -0500949/*
950 * set_performant_mode: Modify the tag for cciss performant
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600951 * set bit 0 for pull model, bits 3-1 for block fetch
952 * register number
953 */
Webb Scales25163bd2015-04-23 09:32:00 -0500954#define DEFAULT_REPLY_QUEUE (-1)
955static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
956 int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600957{
Matt Gates254f7962012-05-01 11:43:06 -0500958 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600959 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Webb Scales25163bd2015-04-23 09:32:00 -0500960 if (unlikely(!h->msix_vector))
961 return;
962 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
Matt Gates254f7962012-05-01 11:43:06 -0500963 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +0200964 raw_smp_processor_id() % h->nreply_queues;
Webb Scales25163bd2015-04-23 09:32:00 -0500965 else
966 c->Header.ReplyQueue = reply_queue % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -0500967 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600968}
969
Scott Teelc3497752014-02-18 13:56:34 -0600970static void set_ioaccel1_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -0500971 struct CommandList *c,
972 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -0600973{
974 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
975
Webb Scales25163bd2015-04-23 09:32:00 -0500976 /*
977 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -0600978 * processor. This seems to give the best I/O throughput.
979 */
Webb Scales25163bd2015-04-23 09:32:00 -0500980 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
981 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
982 else
983 cp->ReplyQueue = reply_queue % h->nreply_queues;
984 /*
985 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -0600986 * - performant mode bit (bit 0)
987 * - pull count (bits 1-3)
988 * - command type (bits 4-6)
989 */
990 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
991 IOACCEL1_BUSADDR_CMDTYPE;
992}
993
Stephen Cameron8be986c2015-04-23 09:34:06 -0500994static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
995 struct CommandList *c,
996 int reply_queue)
997{
998 struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
999 &h->ioaccel2_cmd_pool[c->cmdindex];
1000
1001 /* Tell the controller to post the reply to the queue for this
1002 * processor. This seems to give the best I/O throughput.
1003 */
1004 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1005 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1006 else
1007 cp->reply_queue = reply_queue % h->nreply_queues;
1008 /* Set the bits in the address sent down to include:
1009 * - performant mode bit not used in ioaccel mode 2
1010 * - pull count (bits 0-3)
1011 * - command type isn't needed for ioaccel2
1012 */
1013 c->busaddr |= h->ioaccel2_blockFetchTable[0];
1014}
1015
Scott Teelc3497752014-02-18 13:56:34 -06001016static void set_ioaccel2_performant_mode(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05001017 struct CommandList *c,
1018 int reply_queue)
Scott Teelc3497752014-02-18 13:56:34 -06001019{
1020 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
1021
Webb Scales25163bd2015-04-23 09:32:00 -05001022 /*
1023 * Tell the controller to post the reply to the queue for this
Scott Teelc3497752014-02-18 13:56:34 -06001024 * processor. This seems to give the best I/O throughput.
1025 */
Webb Scales25163bd2015-04-23 09:32:00 -05001026 if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1027 cp->reply_queue = smp_processor_id() % h->nreply_queues;
1028 else
1029 cp->reply_queue = reply_queue % h->nreply_queues;
1030 /*
1031 * Set the bits in the address sent down to include:
Scott Teelc3497752014-02-18 13:56:34 -06001032 * - performant mode bit not used in ioaccel mode 2
1033 * - pull count (bits 0-3)
1034 * - command type isn't needed for ioaccel2
1035 */
1036 c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
1037}
1038
Stephen M. Camerone85c5972012-05-01 11:43:42 -05001039static int is_firmware_flash_cmd(u8 *cdb)
1040{
1041 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
1042}
1043
1044/*
1045 * During firmware flash, the heartbeat register may not update as frequently
1046 * as it should. So we dial down lockup detection during firmware flash. and
1047 * dial it back up when firmware flash completes.
1048 */
1049#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
1050#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
1051static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
1052 struct CommandList *c)
1053{
1054 if (!is_firmware_flash_cmd(c->Request.CDB))
1055 return;
1056 atomic_inc(&h->firmware_flash_in_progress);
1057 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
1058}
1059
1060static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
1061 struct CommandList *c)
1062{
1063 if (is_firmware_flash_cmd(c->Request.CDB) &&
1064 atomic_dec_and_test(&h->firmware_flash_in_progress))
1065 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
1066}
1067
Webb Scales25163bd2015-04-23 09:32:00 -05001068static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
1069 struct CommandList *c, int reply_queue)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001070{
Stephen Cameronc05e8862015-01-23 16:44:40 -06001071 dial_down_lockup_detection_during_fw_flash(h, c);
1072 atomic_inc(&h->commands_outstanding);
Scott Teelc3497752014-02-18 13:56:34 -06001073 switch (c->cmd_type) {
1074 case CMD_IOACCEL1:
Webb Scales25163bd2015-04-23 09:32:00 -05001075 set_ioaccel1_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001076 writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
Scott Teelc3497752014-02-18 13:56:34 -06001077 break;
1078 case CMD_IOACCEL2:
Webb Scales25163bd2015-04-23 09:32:00 -05001079 set_ioaccel2_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001080 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
Scott Teelc3497752014-02-18 13:56:34 -06001081 break;
Stephen Cameron8be986c2015-04-23 09:34:06 -05001082 case IOACCEL2_TMF:
1083 set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
1084 writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
1085 break;
Scott Teelc3497752014-02-18 13:56:34 -06001086 default:
Webb Scales25163bd2015-04-23 09:32:00 -05001087 set_performant_mode(h, c, reply_queue);
Stephen Cameronc05e8862015-01-23 16:44:40 -06001088 h->access.submit_command(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06001089 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001090}
1091
Webb Scalesa58e7e52015-04-23 09:34:16 -05001092static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
Webb Scales25163bd2015-04-23 09:32:00 -05001093{
Webb Scalesd604f532015-04-23 09:35:22 -05001094 if (unlikely(hpsa_is_pending_event(c)))
Webb Scalesa58e7e52015-04-23 09:34:16 -05001095 return finish_cmd(c);
1096
Webb Scales25163bd2015-04-23 09:32:00 -05001097 __enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
1098}
1099
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -06001100static inline int is_hba_lunid(unsigned char scsi3addr[])
1101{
1102 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
1103}
1104
1105static inline int is_scsi_rev_5(struct ctlr_info *h)
1106{
1107 if (!h->hba_inquiry_data)
1108 return 0;
1109 if ((h->hba_inquiry_data[2] & 0x07) == 5)
1110 return 1;
1111 return 0;
1112}
1113
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001114static int hpsa_find_target_lun(struct ctlr_info *h,
1115 unsigned char scsi3addr[], int bus, int *target, int *lun)
1116{
1117 /* finds an unused bus, target, lun for a new physical device
1118 * assumes h->devlock is held
1119 */
1120 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -05001121 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001122
Akinobu Mita263d9402012-01-21 00:15:27 +09001123 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001124
1125 for (i = 0; i < h->ndevices; i++) {
1126 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +09001127 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001128 }
1129
Akinobu Mita263d9402012-01-21 00:15:27 +09001130 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
1131 if (i < HPSA_MAX_DEVICES) {
1132 /* *bus = 1; */
1133 *target = i;
1134 *lun = 0;
1135 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001136 }
1137 return !found;
1138}
1139
Don Brace1d33d852015-11-04 15:50:31 -06001140static void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
Webb Scales0d96ef52015-04-23 09:31:55 -05001141 struct hpsa_scsi_dev_t *dev, char *description)
1142{
Don Brace7c59a0d2015-11-04 15:52:22 -06001143#define LABEL_SIZE 25
1144 char label[LABEL_SIZE];
1145
Don Brace9975ec92015-11-04 15:50:25 -06001146 if (h == NULL || h->pdev == NULL || h->scsi_host == NULL)
1147 return;
1148
Don Brace7c59a0d2015-11-04 15:52:22 -06001149 switch (dev->devtype) {
1150 case TYPE_RAID:
1151 snprintf(label, LABEL_SIZE, "controller");
1152 break;
1153 case TYPE_ENCLOSURE:
1154 snprintf(label, LABEL_SIZE, "enclosure");
1155 break;
1156 case TYPE_DISK:
1157 if (dev->external)
1158 snprintf(label, LABEL_SIZE, "external");
1159 else if (!is_logical_dev_addr_mode(dev->scsi3addr))
1160 snprintf(label, LABEL_SIZE, "%s",
1161 raid_label[PHYSICAL_DRIVE]);
1162 else
1163 snprintf(label, LABEL_SIZE, "RAID-%s",
1164 dev->raid_level > RAID_UNKNOWN ? "?" :
1165 raid_label[dev->raid_level]);
1166 break;
1167 case TYPE_ROM:
1168 snprintf(label, LABEL_SIZE, "rom");
1169 break;
1170 case TYPE_TAPE:
1171 snprintf(label, LABEL_SIZE, "tape");
1172 break;
1173 case TYPE_MEDIUM_CHANGER:
1174 snprintf(label, LABEL_SIZE, "changer");
1175 break;
1176 default:
1177 snprintf(label, LABEL_SIZE, "UNKNOWN");
1178 break;
1179 }
1180
Webb Scales0d96ef52015-04-23 09:31:55 -05001181 dev_printk(level, &h->pdev->dev,
Don Brace7c59a0d2015-11-04 15:52:22 -06001182 "scsi %d:%d:%d:%d: %s %s %.8s %.16s %s SSDSmartPathCap%c En%c Exp=%d\n",
Webb Scales0d96ef52015-04-23 09:31:55 -05001183 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
1184 description,
1185 scsi_device_type(dev->devtype),
1186 dev->vendor,
1187 dev->model,
Don Brace7c59a0d2015-11-04 15:52:22 -06001188 label,
Webb Scales0d96ef52015-04-23 09:31:55 -05001189 dev->offload_config ? '+' : '-',
1190 dev->offload_enabled ? '+' : '-',
Kevin Barnett2a168202015-11-04 15:51:21 -06001191 dev->expose_device);
Webb Scales0d96ef52015-04-23 09:31:55 -05001192}
1193
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001194/* Add an entry into h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001195static int hpsa_scsi_add_entry(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001196 struct hpsa_scsi_dev_t *device,
1197 struct hpsa_scsi_dev_t *added[], int *nadded)
1198{
1199 /* assumes h->devlock is held */
1200 int n = h->ndevices;
1201 int i;
1202 unsigned char addr1[8], addr2[8];
1203 struct hpsa_scsi_dev_t *sd;
1204
Scott Teelcfe5bad2011-10-26 16:21:07 -05001205 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001206 dev_err(&h->pdev->dev, "too many devices, some will be "
1207 "inaccessible.\n");
1208 return -1;
1209 }
1210
1211 /* physical devices do not have lun or target assigned until now. */
1212 if (device->lun != -1)
1213 /* Logical device, lun is already assigned. */
1214 goto lun_assigned;
1215
1216 /* If this device a non-zero lun of a multi-lun device
1217 * byte 4 of the 8-byte LUN addr will contain the logical
Don Brace2b08b3e2015-01-23 16:41:09 -06001218 * unit no, zero otherwise.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001219 */
1220 if (device->scsi3addr[4] == 0) {
1221 /* This is not a non-zero lun of a multi-lun device */
1222 if (hpsa_find_target_lun(h, device->scsi3addr,
1223 device->bus, &device->target, &device->lun) != 0)
1224 return -1;
1225 goto lun_assigned;
1226 }
1227
1228 /* This is a non-zero lun of a multi-lun device.
1229 * Search through our list and find the device which
shane.seymour9a4178b2015-07-18 11:13:09 -05001230 * has the same 8 byte LUN address, excepting byte 4 and 5.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001231 * Assign the same bus and target for this new LUN.
1232 * Use the logical unit number from the firmware.
1233 */
1234 memcpy(addr1, device->scsi3addr, 8);
1235 addr1[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001236 addr1[5] = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001237 for (i = 0; i < n; i++) {
1238 sd = h->dev[i];
1239 memcpy(addr2, sd->scsi3addr, 8);
1240 addr2[4] = 0;
shane.seymour9a4178b2015-07-18 11:13:09 -05001241 addr2[5] = 0;
1242 /* differ only in byte 4 and 5? */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001243 if (memcmp(addr1, addr2, 8) == 0) {
1244 device->bus = sd->bus;
1245 device->target = sd->target;
1246 device->lun = device->scsi3addr[4];
1247 break;
1248 }
1249 }
1250 if (device->lun == -1) {
1251 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1252 " suspect firmware bug or unsupported hardware "
1253 "configuration.\n");
1254 return -1;
1255 }
1256
1257lun_assigned:
1258
1259 h->dev[n] = device;
1260 h->ndevices++;
1261 added[*nadded] = device;
1262 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001263 hpsa_show_dev_msg(KERN_INFO, h, device,
Kevin Barnett2a168202015-11-04 15:51:21 -06001264 device->expose_device ? "added" : "masked");
Robert Elliotta473d862015-04-23 09:32:54 -05001265 device->offload_to_be_enabled = device->offload_enabled;
1266 device->offload_enabled = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001267 return 0;
1268}
1269
Scott Teelbd9244f2012-01-19 14:01:30 -06001270/* Update an entry in h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001271static void hpsa_scsi_update_entry(struct ctlr_info *h,
Scott Teelbd9244f2012-01-19 14:01:30 -06001272 int entry, struct hpsa_scsi_dev_t *new_entry)
1273{
Robert Elliotta473d862015-04-23 09:32:54 -05001274 int offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001275 /* assumes h->devlock is held */
1276 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1277
1278 /* Raid level changed. */
1279 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001280
Don Brace03383732015-01-23 16:43:30 -06001281 /* Raid offload parameters changed. Careful about the ordering. */
1282 if (new_entry->offload_config && new_entry->offload_enabled) {
1283 /*
1284 * if drive is newly offload_enabled, we want to copy the
1285 * raid map data first. If previously offload_enabled and
1286 * offload_config were set, raid map data had better be
1287 * the same as it was before. if raid map data is changed
1288 * then it had better be the case that
1289 * h->dev[entry]->offload_enabled is currently 0.
1290 */
1291 h->dev[entry]->raid_map = new_entry->raid_map;
1292 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
Don Brace03383732015-01-23 16:43:30 -06001293 }
Joe Handzika3144e02015-04-23 09:32:59 -05001294 if (new_entry->hba_ioaccel_enabled) {
1295 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1296 wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1297 }
1298 h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001299 h->dev[entry]->offload_config = new_entry->offload_config;
Stephen M. Cameron9fb0de22014-02-18 13:56:50 -06001300 h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
Don Brace03383732015-01-23 16:43:30 -06001301 h->dev[entry]->queue_depth = new_entry->queue_depth;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001302
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001303 /*
1304 * We can turn off ioaccel offload now, but need to delay turning
1305 * it on until we can update h->dev[entry]->phys_disk[], but we
1306 * can't do that until all the devices are updated.
1307 */
1308 h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
1309 if (!new_entry->offload_enabled)
1310 h->dev[entry]->offload_enabled = 0;
1311
Robert Elliotta473d862015-04-23 09:32:54 -05001312 offload_enabled = h->dev[entry]->offload_enabled;
1313 h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
Webb Scales0d96ef52015-04-23 09:31:55 -05001314 hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
Robert Elliotta473d862015-04-23 09:32:54 -05001315 h->dev[entry]->offload_enabled = offload_enabled;
Scott Teelbd9244f2012-01-19 14:01:30 -06001316}
1317
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001318/* Replace an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001319static void hpsa_scsi_replace_entry(struct ctlr_info *h,
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001320 int entry, struct hpsa_scsi_dev_t *new_entry,
1321 struct hpsa_scsi_dev_t *added[], int *nadded,
1322 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1323{
1324 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -05001325 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001326 removed[*nremoved] = h->dev[entry];
1327 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -05001328
1329 /*
1330 * New physical devices won't have target/lun assigned yet
1331 * so we need to preserve the values in the slot we are replacing.
1332 */
1333 if (new_entry->target == -1) {
1334 new_entry->target = h->dev[entry]->target;
1335 new_entry->lun = h->dev[entry]->lun;
1336 }
1337
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001338 h->dev[entry] = new_entry;
1339 added[*nadded] = new_entry;
1340 (*nadded)++;
Webb Scales0d96ef52015-04-23 09:31:55 -05001341 hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
Robert Elliotta473d862015-04-23 09:32:54 -05001342 new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1343 new_entry->offload_enabled = 0;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001344}
1345
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001346/* Remove an entry from h->dev[] array. */
Don Brace8aa60682015-11-04 15:50:01 -06001347static void hpsa_scsi_remove_entry(struct ctlr_info *h, int entry,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001348 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1349{
1350 /* assumes h->devlock is held */
1351 int i;
1352 struct hpsa_scsi_dev_t *sd;
1353
Scott Teelcfe5bad2011-10-26 16:21:07 -05001354 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001355
1356 sd = h->dev[entry];
1357 removed[*nremoved] = h->dev[entry];
1358 (*nremoved)++;
1359
1360 for (i = entry; i < h->ndevices-1; i++)
1361 h->dev[i] = h->dev[i+1];
1362 h->ndevices--;
Webb Scales0d96ef52015-04-23 09:31:55 -05001363 hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001364}
1365
1366#define SCSI3ADDR_EQ(a, b) ( \
1367 (a)[7] == (b)[7] && \
1368 (a)[6] == (b)[6] && \
1369 (a)[5] == (b)[5] && \
1370 (a)[4] == (b)[4] && \
1371 (a)[3] == (b)[3] && \
1372 (a)[2] == (b)[2] && \
1373 (a)[1] == (b)[1] && \
1374 (a)[0] == (b)[0])
1375
1376static void fixup_botched_add(struct ctlr_info *h,
1377 struct hpsa_scsi_dev_t *added)
1378{
1379 /* called when scsi_add_device fails in order to re-adjust
1380 * h->dev[] to match the mid layer's view.
1381 */
1382 unsigned long flags;
1383 int i, j;
1384
1385 spin_lock_irqsave(&h->lock, flags);
1386 for (i = 0; i < h->ndevices; i++) {
1387 if (h->dev[i] == added) {
1388 for (j = i; j < h->ndevices-1; j++)
1389 h->dev[j] = h->dev[j+1];
1390 h->ndevices--;
1391 break;
1392 }
1393 }
1394 spin_unlock_irqrestore(&h->lock, flags);
1395 kfree(added);
1396}
1397
1398static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1399 struct hpsa_scsi_dev_t *dev2)
1400{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001401 /* we compare everything except lun and target as these
1402 * are not yet assigned. Compare parts likely
1403 * to differ first
1404 */
1405 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1406 sizeof(dev1->scsi3addr)) != 0)
1407 return 0;
1408 if (memcmp(dev1->device_id, dev2->device_id,
1409 sizeof(dev1->device_id)) != 0)
1410 return 0;
1411 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1412 return 0;
1413 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1414 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001415 if (dev1->devtype != dev2->devtype)
1416 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001417 if (dev1->bus != dev2->bus)
1418 return 0;
1419 return 1;
1420}
1421
Scott Teelbd9244f2012-01-19 14:01:30 -06001422static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1423 struct hpsa_scsi_dev_t *dev2)
1424{
1425 /* Device attributes that can change, but don't mean
1426 * that the device is a different device, nor that the OS
1427 * needs to be told anything about the change.
1428 */
1429 if (dev1->raid_level != dev2->raid_level)
1430 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001431 if (dev1->offload_config != dev2->offload_config)
1432 return 1;
1433 if (dev1->offload_enabled != dev2->offload_enabled)
1434 return 1;
Don Brace93849502015-07-18 11:12:49 -05001435 if (!is_logical_dev_addr_mode(dev1->scsi3addr))
1436 if (dev1->queue_depth != dev2->queue_depth)
1437 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -06001438 return 0;
1439}
1440
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001441/* Find needle in haystack. If exact match found, return DEVICE_SAME,
1442 * and return needle location in *index. If scsi3addr matches, but not
1443 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -06001444 * location in *index.
1445 * In the case of a minor device attribute change, such as RAID level, just
1446 * return DEVICE_UPDATED, along with the updated device's location in index.
1447 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001448 */
1449static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1450 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1451 int *index)
1452{
1453 int i;
1454#define DEVICE_NOT_FOUND 0
1455#define DEVICE_CHANGED 1
1456#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -06001457#define DEVICE_UPDATED 3
Don Brace1d33d852015-11-04 15:50:31 -06001458 if (needle == NULL)
1459 return DEVICE_NOT_FOUND;
1460
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001461 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -06001462 if (haystack[i] == NULL) /* previously removed. */
1463 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001464 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1465 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -06001466 if (device_is_the_same(needle, haystack[i])) {
1467 if (device_updated(needle, haystack[i]))
1468 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001469 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -06001470 } else {
Stephen M. Cameron98465902014-02-21 16:25:00 -06001471 /* Keep offline devices offline */
1472 if (needle->volume_offline)
1473 return DEVICE_NOT_FOUND;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001474 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -06001475 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001476 }
1477 }
1478 *index = -1;
1479 return DEVICE_NOT_FOUND;
1480}
1481
Stephen M. Cameron98465902014-02-21 16:25:00 -06001482static void hpsa_monitor_offline_device(struct ctlr_info *h,
1483 unsigned char scsi3addr[])
1484{
1485 struct offline_device_entry *device;
1486 unsigned long flags;
1487
1488 /* Check to see if device is already on the list */
1489 spin_lock_irqsave(&h->offline_device_lock, flags);
1490 list_for_each_entry(device, &h->offline_device_list, offline_list) {
1491 if (memcmp(device->scsi3addr, scsi3addr,
1492 sizeof(device->scsi3addr)) == 0) {
1493 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1494 return;
1495 }
1496 }
1497 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1498
1499 /* Device is not on the list, add it. */
1500 device = kmalloc(sizeof(*device), GFP_KERNEL);
1501 if (!device) {
1502 dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
1503 return;
1504 }
1505 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1506 spin_lock_irqsave(&h->offline_device_lock, flags);
1507 list_add_tail(&device->offline_list, &h->offline_device_list);
1508 spin_unlock_irqrestore(&h->offline_device_lock, flags);
1509}
1510
1511/* Print a message explaining various offline volume states */
1512static void hpsa_show_volume_status(struct ctlr_info *h,
1513 struct hpsa_scsi_dev_t *sd)
1514{
1515 if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
1516 dev_info(&h->pdev->dev,
1517 "C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
1518 h->scsi_host->host_no,
1519 sd->bus, sd->target, sd->lun);
1520 switch (sd->volume_offline) {
1521 case HPSA_LV_OK:
1522 break;
1523 case HPSA_LV_UNDERGOING_ERASE:
1524 dev_info(&h->pdev->dev,
1525 "C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
1526 h->scsi_host->host_no,
1527 sd->bus, sd->target, sd->lun);
1528 break;
Scott Benesh5ca01202015-07-18 11:13:04 -05001529 case HPSA_LV_NOT_AVAILABLE:
1530 dev_info(&h->pdev->dev,
1531 "C%d:B%d:T%d:L%d Volume is waiting for transforming volume.\n",
1532 h->scsi_host->host_no,
1533 sd->bus, sd->target, sd->lun);
1534 break;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001535 case HPSA_LV_UNDERGOING_RPI:
1536 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001537 "C%d:B%d:T%d:L%d Volume is undergoing rapid parity init.\n",
Stephen M. Cameron98465902014-02-21 16:25:00 -06001538 h->scsi_host->host_no,
1539 sd->bus, sd->target, sd->lun);
1540 break;
1541 case HPSA_LV_PENDING_RPI:
1542 dev_info(&h->pdev->dev,
Scott Benesh5ca01202015-07-18 11:13:04 -05001543 "C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
1544 h->scsi_host->host_no,
1545 sd->bus, sd->target, sd->lun);
Stephen M. Cameron98465902014-02-21 16:25:00 -06001546 break;
1547 case HPSA_LV_ENCRYPTED_NO_KEY:
1548 dev_info(&h->pdev->dev,
1549 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
1550 h->scsi_host->host_no,
1551 sd->bus, sd->target, sd->lun);
1552 break;
1553 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
1554 dev_info(&h->pdev->dev,
1555 "C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
1556 h->scsi_host->host_no,
1557 sd->bus, sd->target, sd->lun);
1558 break;
1559 case HPSA_LV_UNDERGOING_ENCRYPTION:
1560 dev_info(&h->pdev->dev,
1561 "C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
1562 h->scsi_host->host_no,
1563 sd->bus, sd->target, sd->lun);
1564 break;
1565 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
1566 dev_info(&h->pdev->dev,
1567 "C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
1568 h->scsi_host->host_no,
1569 sd->bus, sd->target, sd->lun);
1570 break;
1571 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1572 dev_info(&h->pdev->dev,
1573 "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
1574 h->scsi_host->host_no,
1575 sd->bus, sd->target, sd->lun);
1576 break;
1577 case HPSA_LV_PENDING_ENCRYPTION:
1578 dev_info(&h->pdev->dev,
1579 "C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
1580 h->scsi_host->host_no,
1581 sd->bus, sd->target, sd->lun);
1582 break;
1583 case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
1584 dev_info(&h->pdev->dev,
1585 "C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
1586 h->scsi_host->host_no,
1587 sd->bus, sd->target, sd->lun);
1588 break;
1589 }
1590}
1591
Don Brace03383732015-01-23 16:43:30 -06001592/*
1593 * Figure the list of physical drive pointers for a logical drive with
1594 * raid offload configured.
1595 */
1596static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
1597 struct hpsa_scsi_dev_t *dev[], int ndevices,
1598 struct hpsa_scsi_dev_t *logical_drive)
1599{
1600 struct raid_map_data *map = &logical_drive->raid_map;
1601 struct raid_map_disk_data *dd = &map->data[0];
1602 int i, j;
1603 int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
1604 le16_to_cpu(map->metadata_disks_per_row);
1605 int nraid_map_entries = le16_to_cpu(map->row_cnt) *
1606 le16_to_cpu(map->layout_map_count) *
1607 total_disks_per_row;
1608 int nphys_disk = le16_to_cpu(map->layout_map_count) *
1609 total_disks_per_row;
1610 int qdepth;
1611
1612 if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
1613 nraid_map_entries = RAID_MAP_MAX_ENTRIES;
1614
Webb Scalesd604f532015-04-23 09:35:22 -05001615 logical_drive->nphysical_disks = nraid_map_entries;
1616
Don Brace03383732015-01-23 16:43:30 -06001617 qdepth = 0;
1618 for (i = 0; i < nraid_map_entries; i++) {
1619 logical_drive->phys_disk[i] = NULL;
1620 if (!logical_drive->offload_config)
1621 continue;
1622 for (j = 0; j < ndevices; j++) {
Don Brace1d33d852015-11-04 15:50:31 -06001623 if (dev[j] == NULL)
1624 continue;
Don Brace03383732015-01-23 16:43:30 -06001625 if (dev[j]->devtype != TYPE_DISK)
1626 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001627 if (is_logical_device(dev[j]))
Don Brace03383732015-01-23 16:43:30 -06001628 continue;
1629 if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
1630 continue;
1631
1632 logical_drive->phys_disk[i] = dev[j];
1633 if (i < nphys_disk)
1634 qdepth = min(h->nr_cmds, qdepth +
1635 logical_drive->phys_disk[i]->queue_depth);
1636 break;
1637 }
1638
1639 /*
1640 * This can happen if a physical drive is removed and
1641 * the logical drive is degraded. In that case, the RAID
1642 * map data will refer to a physical disk which isn't actually
1643 * present. And in that case offload_enabled should already
1644 * be 0, but we'll turn it off here just in case
1645 */
1646 if (!logical_drive->phys_disk[i]) {
1647 logical_drive->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001648 logical_drive->offload_to_be_enabled = 0;
1649 logical_drive->queue_depth = 8;
Don Brace03383732015-01-23 16:43:30 -06001650 }
1651 }
1652 if (nraid_map_entries)
1653 /*
1654 * This is correct for reads, too high for full stripe writes,
1655 * way too high for partial stripe writes
1656 */
1657 logical_drive->queue_depth = qdepth;
1658 else
1659 logical_drive->queue_depth = h->nr_cmds;
1660}
1661
1662static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
1663 struct hpsa_scsi_dev_t *dev[], int ndevices)
1664{
1665 int i;
1666
1667 for (i = 0; i < ndevices; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001668 if (dev[i] == NULL)
1669 continue;
Don Brace03383732015-01-23 16:43:30 -06001670 if (dev[i]->devtype != TYPE_DISK)
1671 continue;
Kevin Barnettf3f01732015-11-04 15:51:33 -06001672 if (!is_logical_device(dev[i]))
Don Brace03383732015-01-23 16:43:30 -06001673 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001674
1675 /*
1676 * If offload is currently enabled, the RAID map and
1677 * phys_disk[] assignment *better* not be changing
1678 * and since it isn't changing, we do not need to
1679 * update it.
1680 */
1681 if (dev[i]->offload_enabled)
1682 continue;
1683
Don Brace03383732015-01-23 16:43:30 -06001684 hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
1685 }
1686}
1687
Kevin Barnett096ccff2015-11-04 15:51:51 -06001688static int hpsa_add_device(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1689{
1690 int rc = 0;
1691
1692 if (!h->scsi_host)
1693 return 1;
1694
1695 rc = scsi_add_device(h->scsi_host, device->bus,
1696 device->target, device->lun);
1697 return rc;
1698}
1699
1700static void hpsa_remove_device(struct ctlr_info *h,
1701 struct hpsa_scsi_dev_t *device)
1702{
1703 struct scsi_device *sdev = NULL;
1704
1705 if (!h->scsi_host)
1706 return;
1707
1708 sdev = scsi_device_lookup(h->scsi_host, device->bus,
1709 device->target, device->lun);
1710
1711 if (sdev) {
1712 scsi_remove_device(sdev);
1713 scsi_device_put(sdev);
1714 } else {
1715 /*
1716 * We don't expect to get here. Future commands
1717 * to this device will get a selection timeout as
1718 * if the device were gone.
1719 */
1720 hpsa_show_dev_msg(KERN_WARNING, h, device,
1721 "didn't find device for removal.");
1722 }
1723}
1724
Don Brace8aa60682015-11-04 15:50:01 -06001725static void adjust_hpsa_scsi_table(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001726 struct hpsa_scsi_dev_t *sd[], int nsds)
1727{
1728 /* sd contains scsi3 addresses and devtypes, and inquiry
1729 * data. This function takes what's in sd to be the current
1730 * reality and updates h->dev[] to reflect that reality.
1731 */
1732 int i, entry, device_change, changes = 0;
1733 struct hpsa_scsi_dev_t *csd;
1734 unsigned long flags;
1735 struct hpsa_scsi_dev_t **added, **removed;
1736 int nadded, nremoved;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001737
Don Braceda03ded2015-11-04 15:50:56 -06001738 /*
1739 * A reset can cause a device status to change
1740 * re-schedule the scan to see what happened.
1741 */
1742 if (h->reset_in_progress) {
1743 h->drv_req_rescan = 1;
1744 return;
1745 }
1746
Scott Teelcfe5bad2011-10-26 16:21:07 -05001747 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1748 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001749
1750 if (!added || !removed) {
1751 dev_warn(&h->pdev->dev, "out of memory in "
1752 "adjust_hpsa_scsi_table\n");
1753 goto free_and_out;
1754 }
1755
1756 spin_lock_irqsave(&h->devlock, flags);
1757
1758 /* find any devices in h->dev[] that are not in
1759 * sd[] and remove them from h->dev[], and for any
1760 * devices which have changed, remove the old device
1761 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001762 * If minor device attributes change, just update
1763 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001764 */
1765 i = 0;
1766 nremoved = 0;
1767 nadded = 0;
1768 while (i < h->ndevices) {
1769 csd = h->dev[i];
1770 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1771 if (device_change == DEVICE_NOT_FOUND) {
1772 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001773 hpsa_scsi_remove_entry(h, i, removed, &nremoved);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001774 continue; /* remove ^^^, hence i not incremented */
1775 } else if (device_change == DEVICE_CHANGED) {
1776 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001777 hpsa_scsi_replace_entry(h, i, sd[entry],
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001778 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001779 /* Set it to NULL to prevent it from being freed
1780 * at the bottom of hpsa_update_scsi_devices()
1781 */
1782 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001783 } else if (device_change == DEVICE_UPDATED) {
Don Brace8aa60682015-11-04 15:50:01 -06001784 hpsa_scsi_update_entry(h, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001785 }
1786 i++;
1787 }
1788
1789 /* Now, make sure every device listed in sd[] is also
1790 * listed in h->dev[], adding them if they aren't found
1791 */
1792
1793 for (i = 0; i < nsds; i++) {
1794 if (!sd[i]) /* if already added above. */
1795 continue;
Stephen M. Cameron98465902014-02-21 16:25:00 -06001796
1797 /* Don't add devices which are NOT READY, FORMAT IN PROGRESS
1798 * as the SCSI mid-layer does not handle such devices well.
1799 * It relentlessly loops sending TUR at 3Hz, then READ(10)
1800 * at 160Hz, and prevents the system from coming up.
1801 */
1802 if (sd[i]->volume_offline) {
1803 hpsa_show_volume_status(h, sd[i]);
Webb Scales0d96ef52015-04-23 09:31:55 -05001804 hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
Stephen M. Cameron98465902014-02-21 16:25:00 -06001805 continue;
1806 }
1807
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001808 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1809 h->ndevices, &entry);
1810 if (device_change == DEVICE_NOT_FOUND) {
1811 changes++;
Don Brace8aa60682015-11-04 15:50:01 -06001812 if (hpsa_scsi_add_entry(h, sd[i], added, &nadded) != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001813 break;
1814 sd[i] = NULL; /* prevent from being freed later. */
1815 } else if (device_change == DEVICE_CHANGED) {
1816 /* should never happen... */
1817 changes++;
1818 dev_warn(&h->pdev->dev,
1819 "device unexpectedly changed.\n");
1820 /* but if it does happen, we just ignore that device */
1821 }
1822 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001823 hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
1824
1825 /* Now that h->dev[]->phys_disk[] is coherent, we can enable
1826 * any logical drives that need it enabled.
1827 */
Don Brace1d33d852015-11-04 15:50:31 -06001828 for (i = 0; i < h->ndevices; i++) {
1829 if (h->dev[i] == NULL)
1830 continue;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001831 h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
Don Brace1d33d852015-11-04 15:50:31 -06001832 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001833
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001834 spin_unlock_irqrestore(&h->devlock, flags);
1835
Stephen M. Cameron98465902014-02-21 16:25:00 -06001836 /* Monitor devices which are in one of several NOT READY states to be
1837 * brought online later. This must be done without holding h->devlock,
1838 * so don't touch h->dev[]
1839 */
1840 for (i = 0; i < nsds; i++) {
1841 if (!sd[i]) /* if already added above. */
1842 continue;
1843 if (sd[i]->volume_offline)
1844 hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
1845 }
1846
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001847 /* Don't notify scsi mid layer of any changes the first time through
1848 * (or if there are no changes) scsi_scan_host will do it later the
1849 * first time through.
1850 */
Don Brace8aa60682015-11-04 15:50:01 -06001851 if (!changes)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001852 goto free_and_out;
1853
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001854 /* Notify scsi mid layer of any removed devices */
1855 for (i = 0; i < nremoved; i++) {
Don Brace1d33d852015-11-04 15:50:31 -06001856 if (removed[i] == NULL)
1857 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001858 if (removed[i]->expose_device)
1859 hpsa_remove_device(h, removed[i]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001860 kfree(removed[i]);
1861 removed[i] = NULL;
1862 }
1863
1864 /* Notify scsi mid layer of any added devices */
1865 for (i = 0; i < nadded; i++) {
Kevin Barnett096ccff2015-11-04 15:51:51 -06001866 int rc = 0;
1867
Don Brace1d33d852015-11-04 15:50:31 -06001868 if (added[i] == NULL)
1869 continue;
Kevin Barnett2a168202015-11-04 15:51:21 -06001870 if (!(added[i]->expose_device))
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001871 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001872 rc = hpsa_add_device(h, added[i]);
1873 if (!rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001874 continue;
Kevin Barnett096ccff2015-11-04 15:51:51 -06001875 dev_warn(&h->pdev->dev,
1876 "addition failed %d, device not added.", rc);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001877 /* now we have to remove it from h->dev,
1878 * since it didn't get added to scsi mid layer
1879 */
1880 fixup_botched_add(h, added[i]);
Don Brace853633e2015-11-04 15:50:37 -06001881 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001882 }
1883
1884free_and_out:
1885 kfree(added);
1886 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001887}
1888
1889/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001890 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001891 * Assume's h->devlock is held.
1892 */
1893static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1894 int bus, int target, int lun)
1895{
1896 int i;
1897 struct hpsa_scsi_dev_t *sd;
1898
1899 for (i = 0; i < h->ndevices; i++) {
1900 sd = h->dev[i];
1901 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1902 return sd;
1903 }
1904 return NULL;
1905}
1906
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001907static int hpsa_slave_alloc(struct scsi_device *sdev)
1908{
1909 struct hpsa_scsi_dev_t *sd;
1910 unsigned long flags;
1911 struct ctlr_info *h;
1912
1913 h = sdev_to_hba(sdev);
1914 spin_lock_irqsave(&h->devlock, flags);
1915 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1916 sdev_id(sdev), sdev->lun);
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001917 if (likely(sd)) {
Don Brace03383732015-01-23 16:43:30 -06001918 atomic_set(&sd->ioaccel_cmds_out, 0);
Kevin Barnett2a168202015-11-04 15:51:21 -06001919 sdev->hostdata = sd->expose_device ? sd : NULL;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001920 } else
1921 sdev->hostdata = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001922 spin_unlock_irqrestore(&h->devlock, flags);
1923 return 0;
1924}
1925
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001926/* configure scsi device based on internal per-device structure */
1927static int hpsa_slave_configure(struct scsi_device *sdev)
1928{
1929 struct hpsa_scsi_dev_t *sd;
1930 int queue_depth;
1931
1932 sd = sdev->hostdata;
Kevin Barnett2a168202015-11-04 15:51:21 -06001933 sdev->no_uld_attach = !sd || !sd->expose_device;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05001934
1935 if (sd)
1936 queue_depth = sd->queue_depth != 0 ?
1937 sd->queue_depth : sdev->host->can_queue;
1938 else
1939 queue_depth = sdev->host->can_queue;
1940
1941 scsi_change_queue_depth(sdev, queue_depth);
1942
1943 return 0;
1944}
1945
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001946static void hpsa_slave_destroy(struct scsi_device *sdev)
1947{
Stephen M. Cameronbcc442552010-02-04 08:41:54 -06001948 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001949}
1950
Webb Scalesd9a729f2015-04-23 09:33:27 -05001951static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1952{
1953 int i;
1954
1955 if (!h->ioaccel2_cmd_sg_list)
1956 return;
1957 for (i = 0; i < h->nr_cmds; i++) {
1958 kfree(h->ioaccel2_cmd_sg_list[i]);
1959 h->ioaccel2_cmd_sg_list[i] = NULL;
1960 }
1961 kfree(h->ioaccel2_cmd_sg_list);
1962 h->ioaccel2_cmd_sg_list = NULL;
1963}
1964
1965static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1966{
1967 int i;
1968
1969 if (h->chainsize <= 0)
1970 return 0;
1971
1972 h->ioaccel2_cmd_sg_list =
1973 kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
1974 GFP_KERNEL);
1975 if (!h->ioaccel2_cmd_sg_list)
1976 return -ENOMEM;
1977 for (i = 0; i < h->nr_cmds; i++) {
1978 h->ioaccel2_cmd_sg_list[i] =
1979 kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
1980 h->maxsgentries, GFP_KERNEL);
1981 if (!h->ioaccel2_cmd_sg_list[i])
1982 goto clean;
1983 }
1984 return 0;
1985
1986clean:
1987 hpsa_free_ioaccel2_sg_chain_blocks(h);
1988 return -ENOMEM;
1989}
1990
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001991static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
1992{
1993 int i;
1994
1995 if (!h->cmd_sg_list)
1996 return;
1997 for (i = 0; i < h->nr_cmds; i++) {
1998 kfree(h->cmd_sg_list[i]);
1999 h->cmd_sg_list[i] = NULL;
2000 }
2001 kfree(h->cmd_sg_list);
2002 h->cmd_sg_list = NULL;
2003}
2004
Robert Elliott105a3db2015-04-23 09:33:48 -05002005static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002006{
2007 int i;
2008
2009 if (h->chainsize <= 0)
2010 return 0;
2011
2012 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
2013 GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002014 if (!h->cmd_sg_list) {
2015 dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002016 return -ENOMEM;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002017 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002018 for (i = 0; i < h->nr_cmds; i++) {
2019 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
2020 h->chainsize, GFP_KERNEL);
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002021 if (!h->cmd_sg_list[i]) {
2022 dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002023 goto clean;
Robert Elliott3d4e6af2015-01-23 16:42:42 -06002024 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002025 }
2026 return 0;
2027
2028clean:
2029 hpsa_free_sg_chain_blocks(h);
2030 return -ENOMEM;
2031}
2032
Webb Scalesd9a729f2015-04-23 09:33:27 -05002033static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
2034 struct io_accel2_cmd *cp, struct CommandList *c)
2035{
2036 struct ioaccel2_sg_element *chain_block;
2037 u64 temp64;
2038 u32 chain_size;
2039
2040 chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
Don Bracea736e9b2015-11-04 15:51:14 -06002041 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002042 temp64 = pci_map_single(h->pdev, chain_block, chain_size,
2043 PCI_DMA_TODEVICE);
2044 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2045 /* prevent subsequent unmapping */
2046 cp->sg->address = 0;
2047 return -1;
2048 }
2049 cp->sg->address = cpu_to_le64(temp64);
2050 return 0;
2051}
2052
2053static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
2054 struct io_accel2_cmd *cp)
2055{
2056 struct ioaccel2_sg_element *chain_sg;
2057 u64 temp64;
2058 u32 chain_size;
2059
2060 chain_sg = cp->sg;
2061 temp64 = le64_to_cpu(chain_sg->address);
Don Bracea736e9b2015-11-04 15:51:14 -06002062 chain_size = le32_to_cpu(cp->sg[0].length);
Webb Scalesd9a729f2015-04-23 09:33:27 -05002063 pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
2064}
2065
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002066static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002067 struct CommandList *c)
2068{
2069 struct SGDescriptor *chain_sg, *chain_block;
2070 u64 temp64;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002071 u32 chain_len;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002072
2073 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
2074 chain_block = h->cmd_sg_list[c->cmdindex];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002075 chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
2076 chain_len = sizeof(*chain_sg) *
Don Brace2b08b3e2015-01-23 16:41:09 -06002077 (le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002078 chain_sg->Len = cpu_to_le32(chain_len);
2079 temp64 = pci_map_single(h->pdev, chain_block, chain_len,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002080 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002081 if (dma_mapping_error(&h->pdev->dev, temp64)) {
2082 /* prevent subsequent unmapping */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002083 chain_sg->Addr = cpu_to_le64(0);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002084 return -1;
2085 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002086 chain_sg->Addr = cpu_to_le64(temp64);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002087 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002088}
2089
2090static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
2091 struct CommandList *c)
2092{
2093 struct SGDescriptor *chain_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002094
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002095 if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002096 return;
2097
2098 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002099 pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
2100 le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002101}
2102
Scott Teela09c1442014-02-18 13:57:21 -06002103
2104/* Decode the various types of errors on ioaccel2 path.
2105 * Return 1 for any error that should generate a RAID path retry.
2106 * Return 0 for errors that don't require a RAID path retry.
2107 */
2108static int handle_ioaccel_mode2_error(struct ctlr_info *h,
Scott Teelc3497752014-02-18 13:56:34 -06002109 struct CommandList *c,
2110 struct scsi_cmnd *cmd,
2111 struct io_accel2_cmd *c2)
2112{
2113 int data_len;
Scott Teela09c1442014-02-18 13:57:21 -06002114 int retry = 0;
Joe Handzikc40820d2015-04-23 09:33:32 -05002115 u32 ioaccel2_resid = 0;
Scott Teelc3497752014-02-18 13:56:34 -06002116
2117 switch (c2->error_data.serv_response) {
2118 case IOACCEL2_SERV_RESPONSE_COMPLETE:
2119 switch (c2->error_data.status) {
2120 case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
2121 break;
2122 case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002123 cmd->result |= SAM_STAT_CHECK_CONDITION;
Scott Teelc3497752014-02-18 13:56:34 -06002124 if (c2->error_data.data_present !=
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002125 IOACCEL2_SENSE_DATA_PRESENT) {
2126 memset(cmd->sense_buffer, 0,
2127 SCSI_SENSE_BUFFERSIZE);
Scott Teelc3497752014-02-18 13:56:34 -06002128 break;
Stephen M. Cameronee6b1882014-05-29 10:53:54 -05002129 }
Scott Teelc3497752014-02-18 13:56:34 -06002130 /* copy the sense data */
2131 data_len = c2->error_data.sense_data_len;
2132 if (data_len > SCSI_SENSE_BUFFERSIZE)
2133 data_len = SCSI_SENSE_BUFFERSIZE;
2134 if (data_len > sizeof(c2->error_data.sense_data_buff))
2135 data_len =
2136 sizeof(c2->error_data.sense_data_buff);
2137 memcpy(cmd->sense_buffer,
2138 c2->error_data.sense_data_buff, data_len);
Scott Teela09c1442014-02-18 13:57:21 -06002139 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002140 break;
2141 case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
Scott Teela09c1442014-02-18 13:57:21 -06002142 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002143 break;
2144 case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
Scott Teela09c1442014-02-18 13:57:21 -06002145 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002146 break;
2147 case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
Stephen Cameron4a8da222015-04-23 09:32:43 -05002148 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002149 break;
2150 case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
Scott Teela09c1442014-02-18 13:57:21 -06002151 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002152 break;
2153 default:
Scott Teela09c1442014-02-18 13:57:21 -06002154 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002155 break;
2156 }
2157 break;
2158 case IOACCEL2_SERV_RESPONSE_FAILURE:
Joe Handzikc40820d2015-04-23 09:33:32 -05002159 switch (c2->error_data.status) {
2160 case IOACCEL2_STATUS_SR_IO_ERROR:
2161 case IOACCEL2_STATUS_SR_IO_ABORTED:
2162 case IOACCEL2_STATUS_SR_OVERRUN:
2163 retry = 1;
2164 break;
2165 case IOACCEL2_STATUS_SR_UNDERRUN:
2166 cmd->result = (DID_OK << 16); /* host byte */
2167 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
2168 ioaccel2_resid = get_unaligned_le32(
2169 &c2->error_data.resid_cnt[0]);
2170 scsi_set_resid(cmd, ioaccel2_resid);
2171 break;
2172 case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
2173 case IOACCEL2_STATUS_SR_INVALID_DEVICE:
2174 case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
2175 /* We will get an event from ctlr to trigger rescan */
2176 retry = 1;
2177 break;
2178 default:
2179 retry = 1;
Joe Handzikc40820d2015-04-23 09:33:32 -05002180 }
Scott Teelc3497752014-02-18 13:56:34 -06002181 break;
2182 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
2183 break;
2184 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
2185 break;
2186 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
Scott Teela09c1442014-02-18 13:57:21 -06002187 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002188 break;
2189 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
Scott Teelc3497752014-02-18 13:56:34 -06002190 break;
2191 default:
Scott Teela09c1442014-02-18 13:57:21 -06002192 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06002193 break;
2194 }
Scott Teela09c1442014-02-18 13:57:21 -06002195
2196 return retry; /* retry on raid path? */
Scott Teelc3497752014-02-18 13:56:34 -06002197}
2198
Webb Scalesa58e7e52015-04-23 09:34:16 -05002199static void hpsa_cmd_resolve_events(struct ctlr_info *h,
2200 struct CommandList *c)
2201{
Webb Scalesd604f532015-04-23 09:35:22 -05002202 bool do_wake = false;
2203
Webb Scalesa58e7e52015-04-23 09:34:16 -05002204 /*
2205 * Prevent the following race in the abort handler:
2206 *
2207 * 1. LLD is requested to abort a SCSI command
2208 * 2. The SCSI command completes
2209 * 3. The struct CommandList associated with step 2 is made available
2210 * 4. New I/O request to LLD to another LUN re-uses struct CommandList
2211 * 5. Abort handler follows scsi_cmnd->host_scribble and
2212 * finds struct CommandList and tries to aborts it
2213 * Now we have aborted the wrong command.
2214 *
Webb Scalesd604f532015-04-23 09:35:22 -05002215 * Reset c->scsi_cmd here so that the abort or reset handler will know
2216 * this command has completed. Then, check to see if the handler is
Webb Scalesa58e7e52015-04-23 09:34:16 -05002217 * waiting for this command, and, if so, wake it.
2218 */
2219 c->scsi_cmd = SCSI_CMD_IDLE;
Webb Scalesd604f532015-04-23 09:35:22 -05002220 mb(); /* Declare command idle before checking for pending events. */
Webb Scalesa58e7e52015-04-23 09:34:16 -05002221 if (c->abort_pending) {
Webb Scalesd604f532015-04-23 09:35:22 -05002222 do_wake = true;
Webb Scalesa58e7e52015-04-23 09:34:16 -05002223 c->abort_pending = false;
Webb Scalesa58e7e52015-04-23 09:34:16 -05002224 }
Webb Scalesd604f532015-04-23 09:35:22 -05002225 if (c->reset_pending) {
2226 unsigned long flags;
2227 struct hpsa_scsi_dev_t *dev;
2228
2229 /*
2230 * There appears to be a reset pending; lock the lock and
2231 * reconfirm. If so, then decrement the count of outstanding
2232 * commands and wake the reset command if this is the last one.
2233 */
2234 spin_lock_irqsave(&h->lock, flags);
2235 dev = c->reset_pending; /* Re-fetch under the lock. */
2236 if (dev && atomic_dec_and_test(&dev->reset_cmds_out))
2237 do_wake = true;
2238 c->reset_pending = NULL;
2239 spin_unlock_irqrestore(&h->lock, flags);
2240 }
2241
2242 if (do_wake)
2243 wake_up_all(&h->event_sync_wait_queue);
Webb Scalesa58e7e52015-04-23 09:34:16 -05002244}
2245
Webb Scales73153fe2015-04-23 09:35:04 -05002246static void hpsa_cmd_resolve_and_free(struct ctlr_info *h,
2247 struct CommandList *c)
2248{
2249 hpsa_cmd_resolve_events(h, c);
2250 cmd_tagged_free(h, c);
2251}
2252
Webb Scales8a0ff922015-04-23 09:34:11 -05002253static void hpsa_cmd_free_and_done(struct ctlr_info *h,
2254 struct CommandList *c, struct scsi_cmnd *cmd)
2255{
Webb Scales73153fe2015-04-23 09:35:04 -05002256 hpsa_cmd_resolve_and_free(h, c);
Webb Scales8a0ff922015-04-23 09:34:11 -05002257 cmd->scsi_done(cmd);
2258}
2259
2260static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
2261{
2262 INIT_WORK(&c->work, hpsa_command_resubmit_worker);
2263 queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
2264}
2265
Webb Scalesa58e7e52015-04-23 09:34:16 -05002266static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
2267{
2268 cmd->result = DID_ABORT << 16;
2269}
2270
2271static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
2272 struct scsi_cmnd *cmd)
2273{
2274 hpsa_set_scsi_cmd_aborted(cmd);
2275 dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2276 c->Request.CDB, c->err_info->ScsiStatus);
Webb Scales73153fe2015-04-23 09:35:04 -05002277 hpsa_cmd_resolve_and_free(h, c);
Webb Scalesa58e7e52015-04-23 09:34:16 -05002278}
2279
Scott Teelc3497752014-02-18 13:56:34 -06002280static void process_ioaccel2_completion(struct ctlr_info *h,
2281 struct CommandList *c, struct scsi_cmnd *cmd,
2282 struct hpsa_scsi_dev_t *dev)
2283{
2284 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2285
2286 /* check for good status */
2287 if (likely(c2->error_data.serv_response == 0 &&
Webb Scales8a0ff922015-04-23 09:34:11 -05002288 c2->error_data.status == 0))
2289 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002290
Webb Scales8a0ff922015-04-23 09:34:11 -05002291 /*
2292 * Any RAID offload error results in retry which will use
Scott Teelc3497752014-02-18 13:56:34 -06002293 * the normal I/O path so the controller can handle whatever's
2294 * wrong.
2295 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002296 if (is_logical_device(dev) &&
Scott Teelc3497752014-02-18 13:56:34 -06002297 c2->error_data.serv_response ==
2298 IOACCEL2_SERV_RESPONSE_FAILURE) {
Don Brace080ef1c2015-01-23 16:43:25 -06002299 if (c2->error_data.status ==
2300 IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
2301 dev->offload_enabled = 0;
Webb Scales8a0ff922015-04-23 09:34:11 -05002302
2303 return hpsa_retry_cmd(h, c);
Scott Teelc3497752014-02-18 13:56:34 -06002304 }
Don Brace080ef1c2015-01-23 16:43:25 -06002305
2306 if (handle_ioaccel_mode2_error(h, c, cmd, c2))
Webb Scales8a0ff922015-04-23 09:34:11 -05002307 return hpsa_retry_cmd(h, c);
Don Brace080ef1c2015-01-23 16:43:25 -06002308
Webb Scales8a0ff922015-04-23 09:34:11 -05002309 return hpsa_cmd_free_and_done(h, c, cmd);
Scott Teelc3497752014-02-18 13:56:34 -06002310}
2311
Stephen Cameron9437ac42015-04-23 09:32:16 -05002312/* Returns 0 on success, < 0 otherwise. */
2313static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
2314 struct CommandList *cp)
2315{
2316 u8 tmf_status = cp->err_info->ScsiStatus;
2317
2318 switch (tmf_status) {
2319 case CISS_TMF_COMPLETE:
2320 /*
2321 * CISS_TMF_COMPLETE never happens, instead,
2322 * ei->CommandStatus == 0 for this case.
2323 */
2324 case CISS_TMF_SUCCESS:
2325 return 0;
2326 case CISS_TMF_INVALID_FRAME:
2327 case CISS_TMF_NOT_SUPPORTED:
2328 case CISS_TMF_FAILED:
2329 case CISS_TMF_WRONG_LUN:
2330 case CISS_TMF_OVERLAPPED_TAG:
2331 break;
2332 default:
2333 dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
2334 tmf_status);
2335 break;
2336 }
2337 return -tmf_status;
2338}
2339
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05002340static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002341{
2342 struct scsi_cmnd *cmd;
2343 struct ctlr_info *h;
2344 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002345 struct hpsa_scsi_dev_t *dev;
Webb Scalesd9a729f2015-04-23 09:33:27 -05002346 struct io_accel2_cmd *c2;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002347
Stephen Cameron9437ac42015-04-23 09:32:16 -05002348 u8 sense_key;
2349 u8 asc; /* additional sense code */
2350 u8 ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05002351 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002352
2353 ei = cp->err_info;
Stephen Cameron7fa30302015-01-23 16:44:30 -06002354 cmd = cp->scsi_cmd;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002355 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002356 dev = cmd->device->hostdata;
Webb Scalesd9a729f2015-04-23 09:33:27 -05002357 c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002358
2359 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06002360 if ((cp->cmd_type == CMD_SCSI) &&
Don Brace2b08b3e2015-01-23 16:41:09 -06002361 (le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002362 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002363
Webb Scalesd9a729f2015-04-23 09:33:27 -05002364 if ((cp->cmd_type == CMD_IOACCEL2) &&
2365 (c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2366 hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2367
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002368 cmd->result = (DID_OK << 16); /* host byte */
2369 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06002370
Don Brace03383732015-01-23 16:43:30 -06002371 if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
2372 atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
2373
Webb Scales25163bd2015-04-23 09:32:00 -05002374 /*
2375 * We check for lockup status here as it may be set for
2376 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
2377 * fail_all_oustanding_cmds()
2378 */
2379 if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
2380 /* DID_NO_CONNECT will prevent a retry */
2381 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05002382 return hpsa_cmd_free_and_done(h, cp, cmd);
Webb Scales25163bd2015-04-23 09:32:00 -05002383 }
2384
Webb Scalesd604f532015-04-23 09:35:22 -05002385 if ((unlikely(hpsa_is_pending_event(cp)))) {
2386 if (cp->reset_pending)
2387 return hpsa_cmd_resolve_and_free(h, cp);
2388 if (cp->abort_pending)
2389 return hpsa_cmd_abort_and_free(h, cp, cmd);
2390 }
2391
Scott Teelc3497752014-02-18 13:56:34 -06002392 if (cp->cmd_type == CMD_IOACCEL2)
2393 return process_ioaccel2_completion(h, cp, cmd, dev);
2394
Robert Elliott6aa4c362014-07-03 10:18:19 -05002395 scsi_set_resid(cmd, ei->ResidualCnt);
Webb Scales8a0ff922015-04-23 09:34:11 -05002396 if (ei->CommandStatus == 0)
2397 return hpsa_cmd_free_and_done(h, cp, cmd);
Robert Elliott6aa4c362014-07-03 10:18:19 -05002398
Matt Gatese1f7de02014-02-18 13:55:17 -06002399 /* For I/O accelerator commands, copy over some fields to the normal
2400 * CISS header used below for error handling.
2401 */
2402 if (cp->cmd_type == CMD_IOACCEL1) {
2403 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06002404 cp->Header.SGList = scsi_sg_count(cmd);
2405 cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
2406 cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
2407 IOACCEL1_IOFLAGS_CDBLEN_MASK;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002408 cp->Header.tag = c->tag;
Matt Gatese1f7de02014-02-18 13:55:17 -06002409 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2410 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002411
2412 /* Any RAID offload error results in retry which will use
2413 * the normal I/O path so the controller can handle whatever's
2414 * wrong.
2415 */
Kevin Barnettf3f01732015-11-04 15:51:33 -06002416 if (is_logical_device(dev)) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002417 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2418 dev->offload_enabled = 0;
Webb Scalesd604f532015-04-23 09:35:22 -05002419 return hpsa_retry_cmd(h, cp);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002420 }
Matt Gatese1f7de02014-02-18 13:55:17 -06002421 }
2422
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002423 /* an error has occurred */
2424 switch (ei->CommandStatus) {
2425
2426 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002427 cmd->result |= ei->ScsiStatus;
2428 /* copy the sense data */
2429 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
2430 sense_data_size = SCSI_SENSE_BUFFERSIZE;
2431 else
2432 sense_data_size = sizeof(ei->SenseInfo);
2433 if (ei->SenseLen < sense_data_size)
2434 sense_data_size = ei->SenseLen;
2435 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
2436 if (ei->ScsiStatus)
2437 decode_sense_data(ei->SenseInfo, sense_data_size,
2438 &sense_key, &asc, &ascq);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002439 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates1d3b3602010-02-04 08:43:00 -06002440 if (sense_key == ABORTED_COMMAND) {
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05002441 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06002442 break;
2443 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002444 break;
2445 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002446 /* Problem was not a check condition
2447 * Pass it up to the upper layers...
2448 */
2449 if (ei->ScsiStatus) {
2450 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2451 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2452 "Returning result: 0x%x\n",
2453 cp, ei->ScsiStatus,
2454 sense_key, asc, ascq,
2455 cmd->result);
2456 } else { /* scsi status is zero??? How??? */
2457 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2458 "Returning no connection.\n", cp),
2459
2460 /* Ordinarily, this case should never happen,
2461 * but there is a bug in some released firmware
2462 * revisions that allows it to happen if, for
2463 * example, a 4100 backplane loses power and
2464 * the tape drive is in it. We assume that
2465 * it's a fatal error of some kind because we
2466 * can't show that it wasn't. We will make it
2467 * look like selection timeout since that is
2468 * the most common reason for this to occur,
2469 * and it's severe enough.
2470 */
2471
2472 cmd->result = DID_NO_CONNECT << 16;
2473 }
2474 break;
2475
2476 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2477 break;
2478 case CMD_DATA_OVERRUN:
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002479 dev_warn(&h->pdev->dev,
2480 "CDB %16phN data overrun\n", cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002481 break;
2482 case CMD_INVALID: {
2483 /* print_bytes(cp, sizeof(*cp), 1, 0);
2484 print_cmd(cp); */
2485 /* We get CMD_INVALID if you address a non-existent device
2486 * instead of a selection timeout (no response). You will
2487 * see this if you yank out a drive, then try to access it.
2488 * This is kind of a shame because it means that any other
2489 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2490 * missing target. */
2491 cmd->result = DID_NO_CONNECT << 16;
2492 }
2493 break;
2494 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05002495 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002496 dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2497 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002498 break;
2499 case CMD_HARDWARE_ERR:
2500 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002501 dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2502 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002503 break;
2504 case CMD_CONNECTION_LOST:
2505 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002506 dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2507 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002508 break;
2509 case CMD_ABORTED:
Webb Scalesa58e7e52015-04-23 09:34:16 -05002510 /* Return now to avoid calling scsi_done(). */
2511 return hpsa_cmd_abort_and_free(h, cp, cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002512 case CMD_ABORT_FAILED:
2513 cmd->result = DID_ERROR << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002514 dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2515 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002516 break;
2517 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05002518 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002519 dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2520 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002521 break;
2522 case CMD_TIMEOUT:
2523 cmd->result = DID_TIME_OUT << 16;
Stephen Cameronf42e81e2015-01-23 16:44:35 -06002524 dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2525 cp->Request.CDB);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002526 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002527 case CMD_UNABORTABLE:
2528 cmd->result = DID_ERROR << 16;
2529 dev_warn(&h->pdev->dev, "Command unabortable\n");
2530 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002531 case CMD_TMF_STATUS:
2532 if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
2533 cmd->result = DID_ERROR << 16;
2534 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002535 case CMD_IOACCEL_DISABLED:
2536 /* This only handles the direct pass-through case since RAID
2537 * offload is handled above. Just attempt a retry.
2538 */
2539 cmd->result = DID_SOFT_ERROR << 16;
2540 dev_warn(&h->pdev->dev,
2541 "cp %p had HP SSD Smart Path error\n", cp);
2542 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002543 default:
2544 cmd->result = DID_ERROR << 16;
2545 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2546 cp, ei->CommandStatus);
2547 }
Webb Scales8a0ff922015-04-23 09:34:11 -05002548
2549 return hpsa_cmd_free_and_done(h, cp, cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002550}
2551
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002552static void hpsa_pci_unmap(struct pci_dev *pdev,
2553 struct CommandList *c, int sg_used, int data_direction)
2554{
2555 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002556
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002557 for (i = 0; i < sg_used; i++)
2558 pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
2559 le32_to_cpu(c->SG[i].Len),
2560 data_direction);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002561}
2562
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002563static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002564 struct CommandList *cp,
2565 unsigned char *buf,
2566 size_t buflen,
2567 int data_direction)
2568{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002569 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002570
2571 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2572 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002573 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002574 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002575 }
2576
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002577 addr64 = pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06002578 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002579 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06002580 cp->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002581 cp->Header.SGTotal = cpu_to_le16(0);
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002582 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06002583 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06002584 cp->SG[0].Addr = cpu_to_le64(addr64);
2585 cp->SG[0].Len = cpu_to_le32(buflen);
2586 cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
2587 cp->Header.SGList = 1; /* no. SGs contig in this cmd */
2588 cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002589 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002590}
2591
Webb Scales25163bd2015-04-23 09:32:00 -05002592#define NO_TIMEOUT ((unsigned long) -1)
2593#define DEFAULT_TIMEOUT 30000 /* milliseconds */
2594static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
2595 struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002596{
2597 DECLARE_COMPLETION_ONSTACK(wait);
2598
2599 c->waiting = &wait;
Webb Scales25163bd2015-04-23 09:32:00 -05002600 __enqueue_cmd_and_start_io(h, c, reply_queue);
2601 if (timeout_msecs == NO_TIMEOUT) {
2602 /* TODO: get rid of this no-timeout thing */
2603 wait_for_completion_io(&wait);
2604 return IO_OK;
2605 }
2606 if (!wait_for_completion_io_timeout(&wait,
2607 msecs_to_jiffies(timeout_msecs))) {
2608 dev_warn(&h->pdev->dev, "Command timed out.\n");
2609 return -ETIMEDOUT;
2610 }
2611 return IO_OK;
2612}
2613
2614static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
2615 int reply_queue, unsigned long timeout_msecs)
2616{
2617 if (unlikely(lockup_detected(h))) {
2618 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
2619 return IO_OK;
2620 }
2621 return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002622}
2623
Stephen M. Cameron094963d2014-05-29 10:53:18 -05002624static u32 lockup_detected(struct ctlr_info *h)
2625{
2626 int cpu;
2627 u32 rc, *lockup_detected;
2628
2629 cpu = get_cpu();
2630 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2631 rc = *lockup_detected;
2632 put_cpu();
2633 return rc;
2634}
2635
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002636#define MAX_DRIVER_CMD_RETRIES 25
Webb Scales25163bd2015-04-23 09:32:00 -05002637static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
2638 struct CommandList *c, int data_direction, unsigned long timeout_msecs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002639{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002640 int backoff_time = 10, retry_count = 0;
Webb Scales25163bd2015-04-23 09:32:00 -05002641 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002642
2643 do {
Joe Perches7630abd2011-05-08 23:32:40 -07002644 memset(c->err_info, 0, sizeof(*c->err_info));
Webb Scales25163bd2015-04-23 09:32:00 -05002645 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
2646 timeout_msecs);
2647 if (rc)
2648 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002649 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002650 if (retry_count > 3) {
2651 msleep(backoff_time);
2652 if (backoff_time < 1000)
2653 backoff_time *= 2;
2654 }
Matt Bondurant852af202012-05-01 11:42:35 -05002655 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05002656 check_for_busy(h, c)) &&
2657 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002658 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
Webb Scales25163bd2015-04-23 09:32:00 -05002659 if (retry_count > MAX_DRIVER_CMD_RETRIES)
2660 rc = -EIO;
2661 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002662}
2663
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002664static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2665 struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002666{
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002667 const u8 *cdb = c->Request.CDB;
2668 const u8 *lun = c->Header.LUN.LunAddrBytes;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002669
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002670 dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2671 " CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2672 txt, lun[0], lun[1], lun[2], lun[3],
2673 lun[4], lun[5], lun[6], lun[7],
2674 cdb[0], cdb[1], cdb[2], cdb[3],
2675 cdb[4], cdb[5], cdb[6], cdb[7],
2676 cdb[8], cdb[9], cdb[10], cdb[11],
2677 cdb[12], cdb[13], cdb[14], cdb[15]);
2678}
2679
2680static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2681 struct CommandList *cp)
2682{
2683 const struct ErrorInfo *ei = cp->err_info;
2684 struct device *d = &cp->h->pdev->dev;
Stephen Cameron9437ac42015-04-23 09:32:16 -05002685 u8 sense_key, asc, ascq;
2686 int sense_len;
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002687
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002688 switch (ei->CommandStatus) {
2689 case CMD_TARGET_STATUS:
Stephen Cameron9437ac42015-04-23 09:32:16 -05002690 if (ei->SenseLen > sizeof(ei->SenseInfo))
2691 sense_len = sizeof(ei->SenseInfo);
2692 else
2693 sense_len = ei->SenseLen;
2694 decode_sense_data(ei->SenseInfo, sense_len,
2695 &sense_key, &asc, &ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002696 hpsa_print_cmd(h, "SCSI status", cp);
2697 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
Stephen Cameron9437ac42015-04-23 09:32:16 -05002698 dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
2699 sense_key, asc, ascq);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002700 else
Stephen Cameron9437ac42015-04-23 09:32:16 -05002701 dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002702 if (ei->ScsiStatus == 0)
2703 dev_warn(d, "SCSI status is abnormally zero. "
2704 "(probably indicates selection timeout "
2705 "reported incorrectly due to a known "
2706 "firmware bug, circa July, 2001.)\n");
2707 break;
2708 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002709 break;
2710 case CMD_DATA_OVERRUN:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002711 hpsa_print_cmd(h, "overrun condition", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002712 break;
2713 case CMD_INVALID: {
2714 /* controller unfortunately reports SCSI passthru's
2715 * to non-existent targets as invalid commands.
2716 */
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002717 hpsa_print_cmd(h, "invalid command", cp);
2718 dev_warn(d, "probably means device no longer present\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002719 }
2720 break;
2721 case CMD_PROTOCOL_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002722 hpsa_print_cmd(h, "protocol error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002723 break;
2724 case CMD_HARDWARE_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002725 hpsa_print_cmd(h, "hardware error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002726 break;
2727 case CMD_CONNECTION_LOST:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002728 hpsa_print_cmd(h, "connection lost", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002729 break;
2730 case CMD_ABORTED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002731 hpsa_print_cmd(h, "aborted", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002732 break;
2733 case CMD_ABORT_FAILED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002734 hpsa_print_cmd(h, "abort failed", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002735 break;
2736 case CMD_UNSOLICITED_ABORT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002737 hpsa_print_cmd(h, "unsolicited abort", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002738 break;
2739 case CMD_TIMEOUT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002740 hpsa_print_cmd(h, "timed out", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002741 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002742 case CMD_UNABORTABLE:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002743 hpsa_print_cmd(h, "unabortable", cp);
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06002744 break;
Webb Scales25163bd2015-04-23 09:32:00 -05002745 case CMD_CTLR_LOCKUP:
2746 hpsa_print_cmd(h, "controller lockup detected", cp);
2747 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002748 default:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002749 hpsa_print_cmd(h, "unknown status", cp);
2750 dev_warn(d, "Unknown command status %x\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002751 ei->CommandStatus);
2752 }
2753}
2754
2755static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002756 u16 page, unsigned char *buf,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002757 unsigned char bufsize)
2758{
2759 int rc = IO_OK;
2760 struct CommandList *c;
2761 struct ErrorInfo *ei;
2762
Stephen Cameron45fcb862015-01-23 16:43:04 -06002763 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002764
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002765 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2766 page, scsi3addr, TYPE_CMD)) {
2767 rc = -1;
2768 goto out;
2769 }
Webb Scales25163bd2015-04-23 09:32:00 -05002770 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
2771 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
2772 if (rc)
2773 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002774 ei = c->err_info;
2775 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002776 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002777 rc = -1;
2778 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002779out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002780 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002781 return rc;
2782}
2783
Scott Teelbf711ac2014-02-18 13:56:39 -06002784static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Webb Scales25163bd2015-04-23 09:32:00 -05002785 u8 reset_type, int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002786{
2787 int rc = IO_OK;
2788 struct CommandList *c;
2789 struct ErrorInfo *ei;
2790
Stephen Cameron45fcb862015-01-23 16:43:04 -06002791 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002792
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002793
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002794 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teel0b9b7b62015-11-04 15:51:02 -06002795 (void) fill_cmd(c, reset_type, h, NULL, 0, 0,
Scott Teelbf711ac2014-02-18 13:56:39 -06002796 scsi3addr, TYPE_MSG);
Webb Scales25163bd2015-04-23 09:32:00 -05002797 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
2798 if (rc) {
2799 dev_warn(&h->pdev->dev, "Failed to send reset command\n");
2800 goto out;
2801 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002802 /* no unmap needed here because no data xfer. */
2803
2804 ei = c->err_info;
2805 if (ei->CommandStatus != 0) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002806 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002807 rc = -1;
2808 }
Webb Scales25163bd2015-04-23 09:32:00 -05002809out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06002810 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002811 return rc;
2812}
2813
Webb Scalesd604f532015-04-23 09:35:22 -05002814static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
2815 struct hpsa_scsi_dev_t *dev,
2816 unsigned char *scsi3addr)
2817{
2818 int i;
2819 bool match = false;
2820 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2821 struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
2822
2823 if (hpsa_is_cmd_idle(c))
2824 return false;
2825
2826 switch (c->cmd_type) {
2827 case CMD_SCSI:
2828 case CMD_IOCTL_PEND:
2829 match = !memcmp(scsi3addr, &c->Header.LUN.LunAddrBytes,
2830 sizeof(c->Header.LUN.LunAddrBytes));
2831 break;
2832
2833 case CMD_IOACCEL1:
2834 case CMD_IOACCEL2:
2835 if (c->phys_disk == dev) {
2836 /* HBA mode match */
2837 match = true;
2838 } else {
2839 /* Possible RAID mode -- check each phys dev. */
2840 /* FIXME: Do we need to take out a lock here? If
2841 * so, we could just call hpsa_get_pdisk_of_ioaccel2()
2842 * instead. */
2843 for (i = 0; i < dev->nphysical_disks && !match; i++) {
2844 /* FIXME: an alternate test might be
2845 *
2846 * match = dev->phys_disk[i]->ioaccel_handle
2847 * == c2->scsi_nexus; */
2848 match = dev->phys_disk[i] == c->phys_disk;
2849 }
2850 }
2851 break;
2852
2853 case IOACCEL2_TMF:
2854 for (i = 0; i < dev->nphysical_disks && !match; i++) {
2855 match = dev->phys_disk[i]->ioaccel_handle ==
2856 le32_to_cpu(ac->it_nexus);
2857 }
2858 break;
2859
2860 case 0: /* The command is in the middle of being initialized. */
2861 match = false;
2862 break;
2863
2864 default:
2865 dev_err(&h->pdev->dev, "unexpected cmd_type: %d\n",
2866 c->cmd_type);
2867 BUG();
2868 }
2869
2870 return match;
2871}
2872
2873static int hpsa_do_reset(struct ctlr_info *h, struct hpsa_scsi_dev_t *dev,
2874 unsigned char *scsi3addr, u8 reset_type, int reply_queue)
2875{
2876 int i;
2877 int rc = 0;
2878
2879 /* We can really only handle one reset at a time */
2880 if (mutex_lock_interruptible(&h->reset_mutex) == -EINTR) {
2881 dev_warn(&h->pdev->dev, "concurrent reset wait interrupted.\n");
2882 return -EINTR;
2883 }
2884
2885 BUG_ON(atomic_read(&dev->reset_cmds_out) != 0);
2886
2887 for (i = 0; i < h->nr_cmds; i++) {
2888 struct CommandList *c = h->cmd_pool + i;
2889 int refcount = atomic_inc_return(&c->refcount);
2890
2891 if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev, scsi3addr)) {
2892 unsigned long flags;
2893
2894 /*
2895 * Mark the target command as having a reset pending,
2896 * then lock a lock so that the command cannot complete
2897 * while we're considering it. If the command is not
2898 * idle then count it; otherwise revoke the event.
2899 */
2900 c->reset_pending = dev;
2901 spin_lock_irqsave(&h->lock, flags); /* Implied MB */
2902 if (!hpsa_is_cmd_idle(c))
2903 atomic_inc(&dev->reset_cmds_out);
2904 else
2905 c->reset_pending = NULL;
2906 spin_unlock_irqrestore(&h->lock, flags);
2907 }
2908
2909 cmd_free(h, c);
2910 }
2911
2912 rc = hpsa_send_reset(h, scsi3addr, reset_type, reply_queue);
2913 if (!rc)
2914 wait_event(h->event_sync_wait_queue,
2915 atomic_read(&dev->reset_cmds_out) == 0 ||
2916 lockup_detected(h));
2917
2918 if (unlikely(lockup_detected(h))) {
Don Brace77678d32015-07-18 11:12:22 -05002919 dev_warn(&h->pdev->dev,
2920 "Controller lockup detected during reset wait\n");
2921 rc = -ENODEV;
2922 }
Webb Scalesd604f532015-04-23 09:35:22 -05002923
2924 if (unlikely(rc))
2925 atomic_set(&dev->reset_cmds_out, 0);
2926
2927 mutex_unlock(&h->reset_mutex);
2928 return rc;
2929}
2930
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002931static void hpsa_get_raid_level(struct ctlr_info *h,
2932 unsigned char *scsi3addr, unsigned char *raid_level)
2933{
2934 int rc;
2935 unsigned char *buf;
2936
2937 *raid_level = RAID_UNKNOWN;
2938 buf = kzalloc(64, GFP_KERNEL);
2939 if (!buf)
2940 return;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002941 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002942 if (rc == 0)
2943 *raid_level = buf[8];
2944 if (*raid_level > RAID_UNKNOWN)
2945 *raid_level = RAID_UNKNOWN;
2946 kfree(buf);
2947 return;
2948}
2949
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002950#define HPSA_MAP_DEBUG
2951#ifdef HPSA_MAP_DEBUG
2952static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2953 struct raid_map_data *map_buff)
2954{
2955 struct raid_map_disk_data *dd = &map_buff->data[0];
2956 int map, row, col;
2957 u16 map_cnt, row_cnt, disks_per_row;
2958
2959 if (rc != 0)
2960 return;
2961
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06002962 /* Show details only if debugging has been activated. */
2963 if (h->raid_offload_debug < 2)
2964 return;
2965
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002966 dev_info(&h->pdev->dev, "structure_size = %u\n",
2967 le32_to_cpu(map_buff->structure_size));
2968 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2969 le32_to_cpu(map_buff->volume_blk_size));
2970 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2971 le64_to_cpu(map_buff->volume_blk_cnt));
2972 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2973 map_buff->phys_blk_shift);
2974 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2975 map_buff->parity_rotation_shift);
2976 dev_info(&h->pdev->dev, "strip_size = %u\n",
2977 le16_to_cpu(map_buff->strip_size));
2978 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2979 le64_to_cpu(map_buff->disk_starting_blk));
2980 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2981 le64_to_cpu(map_buff->disk_blk_cnt));
2982 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2983 le16_to_cpu(map_buff->data_disks_per_row));
2984 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2985 le16_to_cpu(map_buff->metadata_disks_per_row));
2986 dev_info(&h->pdev->dev, "row_cnt = %u\n",
2987 le16_to_cpu(map_buff->row_cnt));
2988 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2989 le16_to_cpu(map_buff->layout_map_count));
Don Brace2b08b3e2015-01-23 16:41:09 -06002990 dev_info(&h->pdev->dev, "flags = 0x%x\n",
Scott Teeldd0e19f2014-02-18 13:57:31 -06002991 le16_to_cpu(map_buff->flags));
Don Brace2b08b3e2015-01-23 16:41:09 -06002992 dev_info(&h->pdev->dev, "encrypytion = %s\n",
2993 le16_to_cpu(map_buff->flags) &
2994 RAID_MAP_FLAG_ENCRYPT_ON ? "ON" : "OFF");
Scott Teeldd0e19f2014-02-18 13:57:31 -06002995 dev_info(&h->pdev->dev, "dekindex = %u\n",
2996 le16_to_cpu(map_buff->dekindex));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002997 map_cnt = le16_to_cpu(map_buff->layout_map_count);
2998 for (map = 0; map < map_cnt; map++) {
2999 dev_info(&h->pdev->dev, "Map%u:\n", map);
3000 row_cnt = le16_to_cpu(map_buff->row_cnt);
3001 for (row = 0; row < row_cnt; row++) {
3002 dev_info(&h->pdev->dev, " Row%u:\n", row);
3003 disks_per_row =
3004 le16_to_cpu(map_buff->data_disks_per_row);
3005 for (col = 0; col < disks_per_row; col++, dd++)
3006 dev_info(&h->pdev->dev,
3007 " D%02u: h=0x%04x xor=%u,%u\n",
3008 col, dd->ioaccel_handle,
3009 dd->xor_mult[0], dd->xor_mult[1]);
3010 disks_per_row =
3011 le16_to_cpu(map_buff->metadata_disks_per_row);
3012 for (col = 0; col < disks_per_row; col++, dd++)
3013 dev_info(&h->pdev->dev,
3014 " M%02u: h=0x%04x xor=%u,%u\n",
3015 col, dd->ioaccel_handle,
3016 dd->xor_mult[0], dd->xor_mult[1]);
3017 }
3018 }
3019}
3020#else
3021static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
3022 __attribute__((unused)) int rc,
3023 __attribute__((unused)) struct raid_map_data *map_buff)
3024{
3025}
3026#endif
3027
3028static int hpsa_get_raid_map(struct ctlr_info *h,
3029 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3030{
3031 int rc = 0;
3032 struct CommandList *c;
3033 struct ErrorInfo *ei;
3034
Stephen Cameron45fcb862015-01-23 16:43:04 -06003035 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003036
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003037 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
3038 sizeof(this_device->raid_map), 0,
3039 scsi3addr, TYPE_CMD)) {
Robert Elliott2dd02d72015-04-23 09:33:43 -05003040 dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
3041 cmd_free(h, c);
3042 return -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003043 }
Webb Scales25163bd2015-04-23 09:32:00 -05003044 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
3045 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
3046 if (rc)
3047 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003048 ei = c->err_info;
3049 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003050 hpsa_scsi_interpret_error(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05003051 rc = -1;
3052 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003053 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06003054 cmd_free(h, c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003055
3056 /* @todo in the future, dynamically allocate RAID map memory */
3057 if (le32_to_cpu(this_device->raid_map.structure_size) >
3058 sizeof(this_device->raid_map)) {
3059 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
3060 rc = -1;
3061 }
3062 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
3063 return rc;
Webb Scales25163bd2015-04-23 09:32:00 -05003064out:
3065 cmd_free(h, c);
3066 return rc;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003067}
3068
Scott Teel66749d02015-11-04 15:51:57 -06003069static int hpsa_bmic_id_controller(struct ctlr_info *h,
3070 struct bmic_identify_controller *buf, size_t bufsize)
3071{
3072 int rc = IO_OK;
3073 struct CommandList *c;
3074 struct ErrorInfo *ei;
3075
3076 c = cmd_alloc(h);
3077
3078 rc = fill_cmd(c, BMIC_IDENTIFY_CONTROLLER, h, buf, bufsize,
3079 0, RAID_CTLR_LUNID, TYPE_CMD);
3080 if (rc)
3081 goto out;
3082
3083 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
3084 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
3085 if (rc)
3086 goto out;
3087 ei = c->err_info;
3088 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3089 hpsa_scsi_interpret_error(h, c);
3090 rc = -1;
3091 }
3092out:
3093 cmd_free(h, c);
3094 return rc;
3095}
3096
3097
Don Brace03383732015-01-23 16:43:30 -06003098static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
3099 unsigned char scsi3addr[], u16 bmic_device_index,
3100 struct bmic_identify_physical_device *buf, size_t bufsize)
3101{
3102 int rc = IO_OK;
3103 struct CommandList *c;
3104 struct ErrorInfo *ei;
3105
3106 c = cmd_alloc(h);
3107 rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
3108 0, RAID_CTLR_LUNID, TYPE_CMD);
3109 if (rc)
3110 goto out;
3111
3112 c->Request.CDB[2] = bmic_device_index & 0xff;
3113 c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
3114
Webb Scales25163bd2015-04-23 09:32:00 -05003115 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
3116 NO_TIMEOUT);
Don Brace03383732015-01-23 16:43:30 -06003117 ei = c->err_info;
3118 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
3119 hpsa_scsi_interpret_error(h, c);
3120 rc = -1;
3121 }
3122out:
3123 cmd_free(h, c);
3124 return rc;
3125}
3126
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003127static int hpsa_vpd_page_supported(struct ctlr_info *h,
3128 unsigned char scsi3addr[], u8 page)
3129{
3130 int rc;
3131 int i;
3132 int pages;
3133 unsigned char *buf, bufsize;
3134
3135 buf = kzalloc(256, GFP_KERNEL);
3136 if (!buf)
3137 return 0;
3138
3139 /* Get the size of the page list first */
3140 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3141 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3142 buf, HPSA_VPD_HEADER_SZ);
3143 if (rc != 0)
3144 goto exit_unsupported;
3145 pages = buf[3];
3146 if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
3147 bufsize = pages + HPSA_VPD_HEADER_SZ;
3148 else
3149 bufsize = 255;
3150
3151 /* Get the whole VPD page list */
3152 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3153 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
3154 buf, bufsize);
3155 if (rc != 0)
3156 goto exit_unsupported;
3157
3158 pages = buf[3];
3159 for (i = 1; i <= pages; i++)
3160 if (buf[3 + i] == page)
3161 goto exit_supported;
3162exit_unsupported:
3163 kfree(buf);
3164 return 0;
3165exit_supported:
3166 kfree(buf);
3167 return 1;
3168}
3169
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003170static void hpsa_get_ioaccel_status(struct ctlr_info *h,
3171 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3172{
3173 int rc;
3174 unsigned char *buf;
3175 u8 ioaccel_status;
3176
3177 this_device->offload_config = 0;
3178 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003179 this_device->offload_to_be_enabled = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003180
3181 buf = kzalloc(64, GFP_KERNEL);
3182 if (!buf)
3183 return;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06003184 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
3185 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003186 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003187 VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003188 if (rc != 0)
3189 goto out;
3190
3191#define IOACCEL_STATUS_BYTE 4
3192#define OFFLOAD_CONFIGURED_BIT 0x01
3193#define OFFLOAD_ENABLED_BIT 0x02
3194 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
3195 this_device->offload_config =
3196 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
3197 if (this_device->offload_config) {
3198 this_device->offload_enabled =
3199 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
3200 if (hpsa_get_raid_map(h, scsi3addr, this_device))
3201 this_device->offload_enabled = 0;
3202 }
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003203 this_device->offload_to_be_enabled = this_device->offload_enabled;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003204out:
3205 kfree(buf);
3206 return;
3207}
3208
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003209/* Get the device id from inquiry page 0x83 */
3210static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
Don Brace75d23d82015-11-04 15:51:39 -06003211 unsigned char *device_id, int index, int buflen)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003212{
3213 int rc;
3214 unsigned char *buf;
3215
3216 if (buflen > 16)
3217 buflen = 16;
3218 buf = kzalloc(64, GFP_KERNEL);
3219 if (!buf)
Stephen M. Camerona84d7942014-05-29 10:54:20 -05003220 return -ENOMEM;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06003221 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003222 if (rc == 0)
Don Brace75d23d82015-11-04 15:51:39 -06003223 memcpy(device_id, &buf[index], buflen);
3224
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003225 kfree(buf);
Don Brace75d23d82015-11-04 15:51:39 -06003226
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003227 return rc != 0;
3228}
3229
3230static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
Don Brace03383732015-01-23 16:43:30 -06003231 void *buf, int bufsize,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003232 int extended_response)
3233{
3234 int rc = IO_OK;
3235 struct CommandList *c;
3236 unsigned char scsi3addr[8];
3237 struct ErrorInfo *ei;
3238
Stephen Cameron45fcb862015-01-23 16:43:04 -06003239 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003240
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06003241 /* address the controller */
3242 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003243 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
3244 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
3245 rc = -1;
3246 goto out;
3247 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003248 if (extended_response)
3249 c->Request.CDB[1] = extended_response;
Webb Scales25163bd2015-04-23 09:32:00 -05003250 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
3251 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
3252 if (rc)
3253 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003254 ei = c->err_info;
3255 if (ei->CommandStatus != 0 &&
3256 ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06003257 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003258 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003259 } else {
Don Brace03383732015-01-23 16:43:30 -06003260 struct ReportLUNdata *rld = buf;
3261
3262 if (rld->extended_response_flag != extended_response) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003263 dev_err(&h->pdev->dev,
3264 "report luns requested format %u, got %u\n",
3265 extended_response,
Don Brace03383732015-01-23 16:43:30 -06003266 rld->extended_response_flag);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003267 rc = -1;
3268 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003269 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003270out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06003271 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003272 return rc;
3273}
3274
3275static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06003276 struct ReportExtendedLUNdata *buf, int bufsize)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003277{
Don Brace03383732015-01-23 16:43:30 -06003278 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
3279 HPSA_REPORT_PHYS_EXTENDED);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003280}
3281
3282static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
3283 struct ReportLUNdata *buf, int bufsize)
3284{
3285 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
3286}
3287
3288static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
3289 int bus, int target, int lun)
3290{
3291 device->bus = bus;
3292 device->target = target;
3293 device->lun = lun;
3294}
3295
Stephen M. Cameron98465902014-02-21 16:25:00 -06003296/* Use VPD inquiry to get details of volume status */
3297static int hpsa_get_volume_status(struct ctlr_info *h,
3298 unsigned char scsi3addr[])
3299{
3300 int rc;
3301 int status;
3302 int size;
3303 unsigned char *buf;
3304
3305 buf = kzalloc(64, GFP_KERNEL);
3306 if (!buf)
3307 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3308
3309 /* Does controller have VPD for logical volume status? */
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003310 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
Stephen M. Cameron98465902014-02-21 16:25:00 -06003311 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003312
3313 /* Get the size of the VPD return buffer */
3314 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3315 buf, HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003316 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003317 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003318 size = buf[3];
3319
3320 /* Now get the whole VPD buffer */
3321 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
3322 buf, size + HPSA_VPD_HEADER_SZ);
Stephen M. Cameron24a4b072014-05-29 10:54:10 -05003323 if (rc != 0)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003324 goto exit_failed;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003325 status = buf[4]; /* status byte */
3326
3327 kfree(buf);
3328 return status;
3329exit_failed:
3330 kfree(buf);
3331 return HPSA_VPD_LV_STATUS_UNSUPPORTED;
3332}
3333
3334/* Determine offline status of a volume.
3335 * Return either:
3336 * 0 (not offline)
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003337 * 0xff (offline for unknown reasons)
Stephen M. Cameron98465902014-02-21 16:25:00 -06003338 * # (integer code indicating one of several NOT READY states
3339 * describing why a volume is to be kept offline)
3340 */
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003341static int hpsa_volume_offline(struct ctlr_info *h,
Stephen M. Cameron98465902014-02-21 16:25:00 -06003342 unsigned char scsi3addr[])
3343{
3344 struct CommandList *c;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003345 unsigned char *sense;
3346 u8 sense_key, asc, ascq;
3347 int sense_len;
Webb Scales25163bd2015-04-23 09:32:00 -05003348 int rc, ldstat = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003349 u16 cmd_status;
3350 u8 scsi_status;
3351#define ASC_LUN_NOT_READY 0x04
3352#define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
3353#define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
3354
3355 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003356
Stephen M. Cameron98465902014-02-21 16:25:00 -06003357 (void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
Webb Scales25163bd2015-04-23 09:32:00 -05003358 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
3359 if (rc) {
3360 cmd_free(h, c);
3361 return 0;
3362 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06003363 sense = c->err_info->SenseInfo;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003364 if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3365 sense_len = sizeof(c->err_info->SenseInfo);
3366 else
3367 sense_len = c->err_info->SenseLen;
3368 decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
Stephen M. Cameron98465902014-02-21 16:25:00 -06003369 cmd_status = c->err_info->CommandStatus;
3370 scsi_status = c->err_info->ScsiStatus;
3371 cmd_free(h, c);
3372 /* Is the volume 'not ready'? */
3373 if (cmd_status != CMD_TARGET_STATUS ||
3374 scsi_status != SAM_STAT_CHECK_CONDITION ||
3375 sense_key != NOT_READY ||
3376 asc != ASC_LUN_NOT_READY) {
3377 return 0;
3378 }
3379
3380 /* Determine the reason for not ready state */
3381 ldstat = hpsa_get_volume_status(h, scsi3addr);
3382
3383 /* Keep volume offline in certain cases: */
3384 switch (ldstat) {
3385 case HPSA_LV_UNDERGOING_ERASE:
Scott Benesh5ca01202015-07-18 11:13:04 -05003386 case HPSA_LV_NOT_AVAILABLE:
Stephen M. Cameron98465902014-02-21 16:25:00 -06003387 case HPSA_LV_UNDERGOING_RPI:
3388 case HPSA_LV_PENDING_RPI:
3389 case HPSA_LV_ENCRYPTED_NO_KEY:
3390 case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
3391 case HPSA_LV_UNDERGOING_ENCRYPTION:
3392 case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
3393 case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
3394 return ldstat;
3395 case HPSA_VPD_LV_STATUS_UNSUPPORTED:
3396 /* If VPD status page isn't available,
3397 * use ASC/ASCQ to determine state
3398 */
3399 if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
3400 (ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
3401 return ldstat;
3402 break;
3403 default:
3404 break;
3405 }
3406 return 0;
3407}
3408
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003409/*
3410 * Find out if a logical device supports aborts by simply trying one.
3411 * Smart Array may claim not to support aborts on logical drives, but
3412 * if a MSA2000 * is connected, the drives on that will be presented
3413 * by the Smart Array as logical drives, and aborts may be sent to
3414 * those devices successfully. So the simplest way to find out is
3415 * to simply try an abort and see how the device responds.
3416 */
3417static int hpsa_device_supports_aborts(struct ctlr_info *h,
3418 unsigned char *scsi3addr)
3419{
3420 struct CommandList *c;
3421 struct ErrorInfo *ei;
3422 int rc = 0;
3423
3424 u64 tag = (u64) -1; /* bogus tag */
3425
3426 /* Assume that physical devices support aborts */
3427 if (!is_logical_dev_addr_mode(scsi3addr))
3428 return 1;
3429
3430 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05003431
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003432 (void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
3433 (void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
3434 /* no unmap needed here because no data xfer. */
3435 ei = c->err_info;
3436 switch (ei->CommandStatus) {
3437 case CMD_INVALID:
3438 rc = 0;
3439 break;
3440 case CMD_UNABORTABLE:
3441 case CMD_ABORT_FAILED:
3442 rc = 1;
3443 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05003444 case CMD_TMF_STATUS:
3445 rc = hpsa_evaluate_tmf_status(h, c);
3446 break;
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003447 default:
3448 rc = 0;
3449 break;
3450 }
3451 cmd_free(h, c);
3452 return rc;
3453}
3454
Don Brace75d23d82015-11-04 15:51:39 -06003455static void sanitize_inquiry_string(unsigned char *s, int len)
3456{
3457 bool terminated = false;
3458
3459 for (; len > 0; (--len, ++s)) {
3460 if (*s == 0)
3461 terminated = true;
3462 if (terminated || *s < 0x20 || *s > 0x7e)
3463 *s = ' ';
3464 }
3465}
3466
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003467static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003468 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
3469 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003470{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003471
3472#define OBDR_SIG_OFFSET 43
3473#define OBDR_TAPE_SIG "$DR-10"
3474#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
3475#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
3476
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003477 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003478 unsigned char *obdr_sig;
Don Brace683fc442015-11-04 15:50:44 -06003479 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003480
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06003481 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Don Brace683fc442015-11-04 15:50:44 -06003482 if (!inq_buff) {
3483 rc = -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003484 goto bail_out;
Don Brace683fc442015-11-04 15:50:44 -06003485 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003486
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003487 /* Do an inquiry to the device to see what it is. */
3488 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3489 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3490 /* Inquiry failed (msg printed already) */
3491 dev_err(&h->pdev->dev,
3492 "hpsa_update_device_info: inquiry failed\n");
Don Brace683fc442015-11-04 15:50:44 -06003493 rc = -EIO;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003494 goto bail_out;
3495 }
3496
Don Brace75d23d82015-11-04 15:51:39 -06003497 sanitize_inquiry_string(&inq_buff[8], 8);
3498 sanitize_inquiry_string(&inq_buff[16], 16);
3499
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003500 this_device->devtype = (inq_buff[0] & 0x1f);
3501 memcpy(this_device->scsi3addr, scsi3addr, 8);
3502 memcpy(this_device->vendor, &inq_buff[8],
3503 sizeof(this_device->vendor));
3504 memcpy(this_device->model, &inq_buff[16],
3505 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003506 memset(this_device->device_id, 0,
3507 sizeof(this_device->device_id));
Don Brace75d23d82015-11-04 15:51:39 -06003508 hpsa_get_device_id(h, scsi3addr, this_device->device_id, 8,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003509 sizeof(this_device->device_id));
3510
3511 if (this_device->devtype == TYPE_DISK &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003512 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003513 int volume_offline;
3514
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003515 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003516 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3517 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
Stephen M. Cameron67955ba2014-05-29 10:54:25 -05003518 volume_offline = hpsa_volume_offline(h, scsi3addr);
3519 if (volume_offline < 0 || volume_offline > 0xff)
3520 volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
3521 this_device->volume_offline = volume_offline & 0xff;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003522 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003523 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003524 this_device->offload_config = 0;
3525 this_device->offload_enabled = 0;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003526 this_device->offload_to_be_enabled = 0;
Joe Handzika3144e02015-04-23 09:32:59 -05003527 this_device->hba_ioaccel_enabled = 0;
Stephen M. Cameron98465902014-02-21 16:25:00 -06003528 this_device->volume_offline = 0;
Don Brace03383732015-01-23 16:43:30 -06003529 this_device->queue_depth = h->nr_cmds;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003530 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003531
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003532 if (is_OBDR_device) {
3533 /* See if this is a One-Button-Disaster-Recovery device
3534 * by looking for "$DR-10" at offset 43 in inquiry data.
3535 */
3536 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
3537 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
3538 strncmp(obdr_sig, OBDR_TAPE_SIG,
3539 OBDR_SIG_LEN) == 0);
3540 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003541 kfree(inq_buff);
3542 return 0;
3543
3544bail_out:
3545 kfree(inq_buff);
Don Brace683fc442015-11-04 15:50:44 -06003546 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003547}
3548
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003549static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
3550 struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
3551{
3552 unsigned long flags;
3553 int rc, entry;
3554 /*
3555 * See if this device supports aborts. If we already know
3556 * the device, we already know if it supports aborts, otherwise
3557 * we have to find out if it supports aborts by trying one.
3558 */
3559 spin_lock_irqsave(&h->devlock, flags);
3560 rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
3561 if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
3562 entry >= 0 && entry < h->ndevices) {
3563 dev->supports_aborts = h->dev[entry]->supports_aborts;
3564 spin_unlock_irqrestore(&h->devlock, flags);
3565 } else {
3566 spin_unlock_irqrestore(&h->devlock, flags);
3567 dev->supports_aborts =
3568 hpsa_device_supports_aborts(h, scsi3addr);
3569 if (dev->supports_aborts < 0)
3570 dev->supports_aborts = 0;
3571 }
3572}
3573
Kevin Barnettc7955052015-11-04 15:51:45 -06003574/*
3575 * Helper function to assign bus, target, lun mapping of devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003576 * Logical drive target and lun are assigned at this time, but
3577 * physical device lun and target assignment are deferred (assigned
3578 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
Kevin Barnettc7955052015-11-04 15:51:45 -06003579*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003580static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003581 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003582{
Kevin Barnettc7955052015-11-04 15:51:45 -06003583 u32 lunid = get_unaligned_le32(lunaddrbytes);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003584
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003585 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
3586 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003587 if (is_hba_lunid(lunaddrbytes))
Kevin Barnettc7955052015-11-04 15:51:45 -06003588 hpsa_set_bus_target_lun(device,
3589 HPSA_HBA_BUS, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003590 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003591 /* defer target, lun assignment for physical devices */
Kevin Barnettc7955052015-11-04 15:51:45 -06003592 hpsa_set_bus_target_lun(device,
3593 HPSA_PHYSICAL_DEVICE_BUS, -1, -1);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003594 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003595 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003596 /* It's a logical device */
Scott Teel66749d02015-11-04 15:51:57 -06003597 if (device->external) {
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003598 hpsa_set_bus_target_lun(device,
Kevin Barnettc7955052015-11-04 15:51:45 -06003599 HPSA_EXTERNAL_RAID_VOLUME_BUS, (lunid >> 16) & 0x3fff,
3600 lunid & 0x00ff);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003601 return;
3602 }
Kevin Barnettc7955052015-11-04 15:51:45 -06003603 hpsa_set_bus_target_lun(device, HPSA_RAID_VOLUME_BUS,
3604 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003605}
3606
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003607
3608/*
Scott Teel54b6e9e2014-02-18 13:56:45 -06003609 * Get address of physical disk used for an ioaccel2 mode command:
3610 * 1. Extract ioaccel2 handle from the command.
3611 * 2. Find a matching ioaccel2 handle from list of physical disks.
3612 * 3. Return:
3613 * 1 and set scsi3addr to address of matching physical
3614 * 0 if no matching physical disk was found.
3615 */
3616static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
3617 struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
3618{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003619 struct io_accel2_cmd *c2 =
3620 &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
3621 unsigned long flags;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003622 int i;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003623
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003624 spin_lock_irqsave(&h->devlock, flags);
3625 for (i = 0; i < h->ndevices; i++)
3626 if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
3627 memcpy(scsi3addr, h->dev[i]->scsi3addr,
3628 sizeof(h->dev[i]->scsi3addr));
3629 spin_unlock_irqrestore(&h->devlock, flags);
3630 return 1;
3631 }
3632 spin_unlock_irqrestore(&h->devlock, flags);
3633 return 0;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003634}
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003635
Scott Teel66749d02015-11-04 15:51:57 -06003636static int figure_external_status(struct ctlr_info *h, int raid_ctlr_position,
3637 int i, int nphysicals, int nlocal_logicals)
3638{
3639 /* In report logicals, local logicals are listed first,
3640 * then any externals.
3641 */
3642 int logicals_start = nphysicals + (raid_ctlr_position == 0);
3643
3644 if (i == raid_ctlr_position)
3645 return 0;
3646
3647 if (i < logicals_start)
3648 return 0;
3649
3650 /* i is in logicals range, but still within local logicals */
3651 if ((i - nphysicals - (raid_ctlr_position == 0)) < nlocal_logicals)
3652 return 0;
3653
3654 return 1; /* it's an external lun */
3655}
3656
Scott Teel54b6e9e2014-02-18 13:56:45 -06003657/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003658 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
3659 * logdev. The number of luns in physdev and logdev are returned in
3660 * *nphysicals and *nlogicals, respectively.
3661 * Returns 0 on success, -1 otherwise.
3662 */
3663static int hpsa_gather_lun_info(struct ctlr_info *h,
Don Brace03383732015-01-23 16:43:30 -06003664 struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003665 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003666{
Don Brace03383732015-01-23 16:43:30 -06003667 if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003668 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3669 return -1;
3670 }
Don Brace03383732015-01-23 16:43:30 -06003671 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003672 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
Don Brace03383732015-01-23 16:43:30 -06003673 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
3674 HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003675 *nphysicals = HPSA_MAX_PHYS_LUN;
3676 }
Don Brace03383732015-01-23 16:43:30 -06003677 if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003678 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3679 return -1;
3680 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06003681 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003682 /* Reject Logicals in excess of our max capability. */
3683 if (*nlogicals > HPSA_MAX_LUN) {
3684 dev_warn(&h->pdev->dev,
3685 "maximum logical LUNs (%d) exceeded. "
3686 "%d LUNs ignored.\n", HPSA_MAX_LUN,
3687 *nlogicals - HPSA_MAX_LUN);
3688 *nlogicals = HPSA_MAX_LUN;
3689 }
3690 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
3691 dev_warn(&h->pdev->dev,
3692 "maximum logical + physical LUNs (%d) exceeded. "
3693 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
3694 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
3695 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
3696 }
3697 return 0;
3698}
3699
Don Brace42a91642014-11-14 17:26:27 -06003700static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
3701 int i, int nphysicals, int nlogicals,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06003702 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003703 struct ReportLUNdata *logdev_list)
3704{
3705 /* Helper function, figure out where the LUN ID info is coming from
3706 * given index i, lists of physical and logical devices, where in
3707 * the list the raid controller is supposed to appear (first or last)
3708 */
3709
3710 int logicals_start = nphysicals + (raid_ctlr_position == 0);
3711 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
3712
3713 if (i == raid_ctlr_position)
3714 return RAID_CTLR_LUNID;
3715
3716 if (i < logicals_start)
Stephen M. Camerond5b5d962014-05-29 10:53:34 -05003717 return &physdev_list->LUN[i -
3718 (raid_ctlr_position == 0)].lunid[0];
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003719
3720 if (i < last_device)
3721 return &logdev_list->LUN[i - nphysicals -
3722 (raid_ctlr_position == 0)][0];
3723 BUG();
3724 return NULL;
3725}
3726
Don Brace03383732015-01-23 16:43:30 -06003727/* get physical drive ioaccel handle and queue depth */
3728static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
3729 struct hpsa_scsi_dev_t *dev,
Don Bracef2039b02015-11-04 15:51:08 -06003730 struct ReportExtendedLUNdata *rlep, int rle_index,
Don Brace03383732015-01-23 16:43:30 -06003731 struct bmic_identify_physical_device *id_phys)
3732{
3733 int rc;
Don Bracef2039b02015-11-04 15:51:08 -06003734 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
Don Brace03383732015-01-23 16:43:30 -06003735
3736 dev->ioaccel_handle = rle->ioaccel_handle;
Don Bracef2039b02015-11-04 15:51:08 -06003737 if ((rle->device_flags & 0x08) && dev->ioaccel_handle)
Joe Handzika3144e02015-04-23 09:32:59 -05003738 dev->hba_ioaccel_enabled = 1;
Don Brace03383732015-01-23 16:43:30 -06003739 memset(id_phys, 0, sizeof(*id_phys));
Don Bracef2039b02015-11-04 15:51:08 -06003740 rc = hpsa_bmic_id_physical_device(h, &rle->lunid[0],
3741 GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]), id_phys,
Don Brace03383732015-01-23 16:43:30 -06003742 sizeof(*id_phys));
3743 if (!rc)
3744 /* Reserve space for FW operations */
3745#define DRIVE_CMDS_RESERVED_FOR_FW 2
3746#define DRIVE_QUEUE_DEPTH 7
3747 dev->queue_depth =
3748 le16_to_cpu(id_phys->current_queue_depth_limit) -
3749 DRIVE_CMDS_RESERVED_FOR_FW;
3750 else
3751 dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
Don Brace03383732015-01-23 16:43:30 -06003752}
3753
Joe Handzik8270b862015-07-18 11:12:43 -05003754static void hpsa_get_path_info(struct hpsa_scsi_dev_t *this_device,
Don Bracef2039b02015-11-04 15:51:08 -06003755 struct ReportExtendedLUNdata *rlep, int rle_index,
Joe Handzik8270b862015-07-18 11:12:43 -05003756 struct bmic_identify_physical_device *id_phys)
3757{
Don Bracef2039b02015-11-04 15:51:08 -06003758 struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
3759
3760 if ((rle->device_flags & 0x08) && this_device->ioaccel_handle)
Joe Handzik8270b862015-07-18 11:12:43 -05003761 this_device->hba_ioaccel_enabled = 1;
3762
3763 memcpy(&this_device->active_path_index,
3764 &id_phys->active_path_number,
3765 sizeof(this_device->active_path_index));
3766 memcpy(&this_device->path_map,
3767 &id_phys->redundant_path_present_map,
3768 sizeof(this_device->path_map));
3769 memcpy(&this_device->box,
3770 &id_phys->alternate_paths_phys_box_on_port,
3771 sizeof(this_device->box));
3772 memcpy(&this_device->phys_connector,
3773 &id_phys->alternate_paths_phys_connector,
3774 sizeof(this_device->phys_connector));
3775 memcpy(&this_device->bay,
3776 &id_phys->phys_bay_in_box,
3777 sizeof(this_device->bay));
3778}
3779
Scott Teel66749d02015-11-04 15:51:57 -06003780/* get number of local logical disks. */
3781static int hpsa_set_local_logical_count(struct ctlr_info *h,
3782 struct bmic_identify_controller *id_ctlr,
3783 u32 *nlocals)
3784{
3785 int rc;
3786
3787 if (!id_ctlr) {
3788 dev_warn(&h->pdev->dev, "%s: id_ctlr buffer is NULL.\n",
3789 __func__);
3790 return -ENOMEM;
3791 }
3792 memset(id_ctlr, 0, sizeof(*id_ctlr));
3793 rc = hpsa_bmic_id_controller(h, id_ctlr, sizeof(*id_ctlr));
3794 if (!rc)
3795 if (id_ctlr->configured_logical_drive_count < 256)
3796 *nlocals = id_ctlr->configured_logical_drive_count;
3797 else
3798 *nlocals = le16_to_cpu(
3799 id_ctlr->extended_logical_unit_count);
3800 else
3801 *nlocals = -1;
3802 return rc;
3803}
3804
3805
Don Brace8aa60682015-11-04 15:50:01 -06003806static void hpsa_update_scsi_devices(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003807{
3808 /* the idea here is we could get notified
3809 * that some devices have changed, so we do a report
3810 * physical luns and report logical luns cmd, and adjust
3811 * our list of devices accordingly.
3812 *
3813 * The scsi3addr's of devices won't change so long as the
3814 * adapter is not reset. That means we can rescan and
3815 * tell which devices we already know about, vs. new
3816 * devices, vs. disappearing devices.
3817 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06003818 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003819 struct ReportLUNdata *logdev_list = NULL;
Don Brace03383732015-01-23 16:43:30 -06003820 struct bmic_identify_physical_device *id_phys = NULL;
Scott Teel66749d02015-11-04 15:51:57 -06003821 struct bmic_identify_controller *id_ctlr = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003822 u32 nphysicals = 0;
3823 u32 nlogicals = 0;
Scott Teel66749d02015-11-04 15:51:57 -06003824 u32 nlocal_logicals = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003825 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003826 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
3827 int ncurrent = 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06003828 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003829 int raid_ctlr_position;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06003830 bool physical_device;
Scott Teelaca4a522012-01-19 14:01:19 -06003831 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003832
Scott Teelcfe5bad2011-10-26 16:21:07 -05003833 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameron92084712014-11-14 17:26:54 -06003834 physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
3835 logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003836 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
Don Brace03383732015-01-23 16:43:30 -06003837 id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
Scott Teel66749d02015-11-04 15:51:57 -06003838 id_ctlr = kzalloc(sizeof(*id_ctlr), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003839
Don Brace03383732015-01-23 16:43:30 -06003840 if (!currentsd || !physdev_list || !logdev_list ||
Scott Teel66749d02015-11-04 15:51:57 -06003841 !tmpdevice || !id_phys || !id_ctlr) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003842 dev_err(&h->pdev->dev, "out of memory\n");
3843 goto out;
3844 }
3845 memset(lunzerobits, 0, sizeof(lunzerobits));
3846
Don Brace853633e2015-11-04 15:50:37 -06003847 h->drv_req_rescan = 0; /* cancel scheduled rescan - we're doing it. */
3848
Don Brace03383732015-01-23 16:43:30 -06003849 if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
Don Brace853633e2015-11-04 15:50:37 -06003850 logdev_list, &nlogicals)) {
3851 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003852 goto out;
Don Brace853633e2015-11-04 15:50:37 -06003853 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003854
Scott Teel66749d02015-11-04 15:51:57 -06003855 /* Set number of local logicals (non PTRAID) */
3856 if (hpsa_set_local_logical_count(h, id_ctlr, &nlocal_logicals)) {
3857 dev_warn(&h->pdev->dev,
3858 "%s: Can't determine number of local logical devices.\n",
3859 __func__);
3860 }
3861
Scott Teelaca4a522012-01-19 14:01:19 -06003862 /* We might see up to the maximum number of logical and physical disks
3863 * plus external target devices, and a device for the local RAID
3864 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003865 */
Scott Teelaca4a522012-01-19 14:01:19 -06003866 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003867
3868 /* Allocate the per device structures */
3869 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05003870 if (i >= HPSA_MAX_DEVICES) {
3871 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
3872 " %d devices ignored.\n", HPSA_MAX_DEVICES,
3873 ndevs_to_allocate - HPSA_MAX_DEVICES);
3874 break;
3875 }
3876
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003877 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
3878 if (!currentsd[i]) {
3879 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
3880 __FILE__, __LINE__);
Don Brace853633e2015-11-04 15:50:37 -06003881 h->drv_req_rescan = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003882 goto out;
3883 }
3884 ndev_allocated++;
3885 }
3886
Stephen M. Cameron86452912014-05-29 10:53:49 -05003887 if (is_scsi_rev_5(h))
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003888 raid_ctlr_position = 0;
3889 else
3890 raid_ctlr_position = nphysicals + nlogicals;
3891
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003892 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06003893 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003894 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003895 u8 *lunaddrbytes, is_OBDR = 0;
Don Brace683fc442015-11-04 15:50:44 -06003896 int rc = 0;
Don Bracef2039b02015-11-04 15:51:08 -06003897 int phys_dev_index = i - (raid_ctlr_position == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003898
Kevin Barnett04fa2f42015-11-04 15:51:27 -06003899 physical_device = i < nphysicals + (raid_ctlr_position == 0);
3900
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003901 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06003902 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3903 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003904
3905 /* skip masked non-disk devices */
Kevin Barnett04fa2f42015-11-04 15:51:27 -06003906 if (MASKED_DEVICE(lunaddrbytes) && physical_device &&
3907 (physdev_list->LUN[phys_dev_index].device_flags & 0x01))
3908 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003909
3910 /* Get device type, vendor, model, device id */
Don Brace683fc442015-11-04 15:50:44 -06003911 rc = hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
3912 &is_OBDR);
3913 if (rc == -ENOMEM) {
3914 dev_warn(&h->pdev->dev,
3915 "Out of memory, rescan deferred.\n");
Don Brace853633e2015-11-04 15:50:37 -06003916 h->drv_req_rescan = 1;
Don Brace683fc442015-11-04 15:50:44 -06003917 goto out;
Don Brace853633e2015-11-04 15:50:37 -06003918 }
Don Brace683fc442015-11-04 15:50:44 -06003919 if (rc) {
3920 dev_warn(&h->pdev->dev,
3921 "Inquiry failed, skipping device.\n");
3922 continue;
3923 }
3924
Scott Teel66749d02015-11-04 15:51:57 -06003925 /* Determine if this is a lun from an external target array */
3926 tmpdevice->external =
3927 figure_external_status(h, raid_ctlr_position, i,
3928 nphysicals, nlocal_logicals);
3929
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06003930 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05003931 hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003932 this_device = currentsd[ncurrent];
3933
Scott Teel34592252015-11-04 15:52:09 -06003934 /* Turn on discovery_polling if there are ext target devices.
3935 * Event-based change notification is unreliable for those.
3936 */
3937 if (!h->discovery_polling) {
3938 if (tmpdevice->external) {
3939 h->discovery_polling = 1;
3940 dev_info(&h->pdev->dev,
3941 "External target, activate discovery polling.\n");
3942 }
3943 }
3944
3945
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003946 *this_device = *tmpdevice;
Kevin Barnett04fa2f42015-11-04 15:51:27 -06003947 this_device->physical_device = physical_device;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003948
Kevin Barnett04fa2f42015-11-04 15:51:27 -06003949 /*
3950 * Expose all devices except for physical devices that
3951 * are masked.
3952 */
3953 if (MASKED_DEVICE(lunaddrbytes) && this_device->physical_device)
Kevin Barnett2a168202015-11-04 15:51:21 -06003954 this_device->expose_device = 0;
3955 else
3956 this_device->expose_device = 1;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003957
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003958 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003959 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003960 /* We don't *really* support actual CD-ROM devices,
3961 * just "One Button Disaster Recovery" tape drive
3962 * which temporarily pretends to be a CD-ROM drive.
3963 * So we check that the device is really an OBDR tape
3964 * device by checking for "$DR-10" in bytes 43-48 of
3965 * the inquiry data.
3966 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05003967 if (is_OBDR)
3968 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003969 break;
3970 case TYPE_DISK:
Kevin Barnett04fa2f42015-11-04 15:51:27 -06003971 if (this_device->physical_device) {
Kevin Barnettb9092b72015-07-18 11:12:59 -05003972 /* The disk is in HBA mode. */
3973 /* Never use RAID mapper in HBA mode. */
Stephen M. Cameron316b2212014-02-21 16:25:15 -06003974 this_device->offload_enabled = 0;
Kevin Barnettb9092b72015-07-18 11:12:59 -05003975 hpsa_get_ioaccel_drive_info(h, this_device,
Don Bracef2039b02015-11-04 15:51:08 -06003976 physdev_list, phys_dev_index, id_phys);
3977 hpsa_get_path_info(this_device,
3978 physdev_list, phys_dev_index, id_phys);
Kevin Barnettb9092b72015-07-18 11:12:59 -05003979 }
Joe Handzikecf418d12015-04-23 09:33:04 -05003980 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003981 break;
3982 case TYPE_TAPE:
3983 case TYPE_MEDIUM_CHANGER:
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003984 case TYPE_ENCLOSURE:
Kevin Barnettb9092b72015-07-18 11:12:59 -05003985 ncurrent++;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05003986 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003987 case TYPE_RAID:
3988 /* Only present the Smartarray HBA as a RAID controller.
3989 * If it's a RAID controller other than the HBA itself
3990 * (an external RAID controller, MSA500 or similar)
3991 * don't present it.
3992 */
3993 if (!is_hba_lunid(lunaddrbytes))
3994 break;
3995 ncurrent++;
3996 break;
3997 default:
3998 break;
3999 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05004000 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004001 break;
4002 }
Don Brace8aa60682015-11-04 15:50:01 -06004003 adjust_hpsa_scsi_table(h, currentsd, ncurrent);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004004out:
4005 kfree(tmpdevice);
4006 for (i = 0; i < ndev_allocated; i++)
4007 kfree(currentsd[i]);
4008 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004009 kfree(physdev_list);
4010 kfree(logdev_list);
Scott Teel66749d02015-11-04 15:51:57 -06004011 kfree(id_ctlr);
Don Brace03383732015-01-23 16:43:30 -06004012 kfree(id_phys);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004013}
4014
Webb Scalesec5cbf02015-01-23 16:44:45 -06004015static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
4016 struct scatterlist *sg)
4017{
4018 u64 addr64 = (u64) sg_dma_address(sg);
4019 unsigned int len = sg_dma_len(sg);
4020
4021 desc->Addr = cpu_to_le64(addr64);
4022 desc->Len = cpu_to_le32(len);
4023 desc->Ext = 0;
4024}
4025
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004026/*
4027 * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004028 * dma mapping and fills in the scatter gather entries of the
4029 * hpsa command, cp.
4030 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004031static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004032 struct CommandList *cp,
4033 struct scsi_cmnd *cmd)
4034{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004035 struct scatterlist *sg;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004036 int use_sg, i, sg_limit, chained, last_sg;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004037 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004038
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004039 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004040
4041 use_sg = scsi_dma_map(cmd);
4042 if (use_sg < 0)
4043 return use_sg;
4044
4045 if (!use_sg)
4046 goto sglist_finished;
4047
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004048 /*
4049 * If the number of entries is greater than the max for a single list,
4050 * then we have a chained list; we will set up all but one entry in the
4051 * first list (the last entry is saved for link information);
4052 * otherwise, we don't have a chained list and we'll set up at each of
4053 * the entries in the one list.
4054 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004055 curr_sg = cp->SG;
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004056 chained = use_sg > h->max_cmd_sg_entries;
4057 sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
4058 last_sg = scsi_sg_count(cmd) - 1;
4059 scsi_for_each_sg(cmd, sg, sg_limit, i) {
Webb Scalesec5cbf02015-01-23 16:44:45 -06004060 hpsa_set_sg_descriptor(curr_sg, sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004061 curr_sg++;
4062 }
Webb Scalesec5cbf02015-01-23 16:44:45 -06004063
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004064 if (chained) {
4065 /*
4066 * Continue with the chained list. Set curr_sg to the chained
4067 * list. Modify the limit to the total count less the entries
4068 * we've already set up. Resume the scan at the list entry
4069 * where the previous loop left off.
4070 */
4071 curr_sg = h->cmd_sg_list[cp->cmdindex];
4072 sg_limit = use_sg - sg_limit;
4073 for_each_sg(sg, sg, sg_limit, i) {
4074 hpsa_set_sg_descriptor(curr_sg, sg);
4075 curr_sg++;
4076 }
4077 }
4078
Webb Scalesec5cbf02015-01-23 16:44:45 -06004079 /* Back the pointer up to the last entry and mark it as "last". */
Webb Scalesb3a7ba72015-04-23 09:34:27 -05004080 (curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004081
4082 if (use_sg + chained > h->maxSG)
4083 h->maxSG = use_sg + chained;
4084
4085 if (chained) {
4086 cp->Header.SGList = h->max_cmd_sg_entries;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004087 cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06004088 if (hpsa_map_sg_chain_block(h, cp)) {
4089 scsi_dma_unmap(cmd);
4090 return -1;
4091 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004092 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004093 }
4094
4095sglist_finished:
4096
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004097 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06004098 cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004099 return 0;
4100}
4101
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004102#define IO_ACCEL_INELIGIBLE (1)
4103static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
4104{
4105 int is_write = 0;
4106 u32 block;
4107 u32 block_cnt;
4108
4109 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
4110 switch (cdb[0]) {
4111 case WRITE_6:
4112 case WRITE_12:
4113 is_write = 1;
4114 case READ_6:
4115 case READ_12:
4116 if (*cdb_len == 6) {
Don Bracec8a6c9a2015-11-04 15:50:50 -06004117 block = get_unaligned_be16(&cdb[2]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004118 block_cnt = cdb[4];
Don Bracec8a6c9a2015-11-04 15:50:50 -06004119 if (block_cnt == 0)
4120 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004121 } else {
4122 BUG_ON(*cdb_len != 12);
Don Bracec8a6c9a2015-11-04 15:50:50 -06004123 block = get_unaligned_be32(&cdb[2]);
4124 block_cnt = get_unaligned_be32(&cdb[6]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004125 }
4126 if (block_cnt > 0xffff)
4127 return IO_ACCEL_INELIGIBLE;
4128
4129 cdb[0] = is_write ? WRITE_10 : READ_10;
4130 cdb[1] = 0;
4131 cdb[2] = (u8) (block >> 24);
4132 cdb[3] = (u8) (block >> 16);
4133 cdb[4] = (u8) (block >> 8);
4134 cdb[5] = (u8) (block);
4135 cdb[6] = 0;
4136 cdb[7] = (u8) (block_cnt >> 8);
4137 cdb[8] = (u8) (block_cnt);
4138 cdb[9] = 0;
4139 *cdb_len = 10;
4140 break;
4141 }
4142 return 0;
4143}
4144
Scott Teelc3497752014-02-18 13:56:34 -06004145static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004146 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004147 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Matt Gatese1f7de02014-02-18 13:55:17 -06004148{
4149 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06004150 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
4151 unsigned int len;
4152 unsigned int total_len = 0;
4153 struct scatterlist *sg;
4154 u64 addr64;
4155 int use_sg, i;
4156 struct SGDescriptor *curr_sg;
4157 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
4158
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004159 /* TODO: implement chaining support */
Don Brace03383732015-01-23 16:43:30 -06004160 if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
4161 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004162 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004163 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004164
Matt Gatese1f7de02014-02-18 13:55:17 -06004165 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
4166
Don Brace03383732015-01-23 16:43:30 -06004167 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4168 atomic_dec(&phys_disk->ioaccel_cmds_out);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004169 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004170 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004171
Matt Gatese1f7de02014-02-18 13:55:17 -06004172 c->cmd_type = CMD_IOACCEL1;
4173
4174 /* Adjust the DMA address to point to the accelerated command buffer */
4175 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
4176 (c->cmdindex * sizeof(*cp));
4177 BUG_ON(c->busaddr & 0x0000007F);
4178
4179 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004180 if (use_sg < 0) {
4181 atomic_dec(&phys_disk->ioaccel_cmds_out);
Matt Gatese1f7de02014-02-18 13:55:17 -06004182 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004183 }
Matt Gatese1f7de02014-02-18 13:55:17 -06004184
4185 if (use_sg) {
4186 curr_sg = cp->SG;
4187 scsi_for_each_sg(cmd, sg, use_sg, i) {
4188 addr64 = (u64) sg_dma_address(sg);
4189 len = sg_dma_len(sg);
4190 total_len += len;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004191 curr_sg->Addr = cpu_to_le64(addr64);
4192 curr_sg->Len = cpu_to_le32(len);
4193 curr_sg->Ext = cpu_to_le32(0);
Matt Gatese1f7de02014-02-18 13:55:17 -06004194 curr_sg++;
4195 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004196 (--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
Matt Gatese1f7de02014-02-18 13:55:17 -06004197
4198 switch (cmd->sc_data_direction) {
4199 case DMA_TO_DEVICE:
4200 control |= IOACCEL1_CONTROL_DATA_OUT;
4201 break;
4202 case DMA_FROM_DEVICE:
4203 control |= IOACCEL1_CONTROL_DATA_IN;
4204 break;
4205 case DMA_NONE:
4206 control |= IOACCEL1_CONTROL_NODATAXFER;
4207 break;
4208 default:
4209 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4210 cmd->sc_data_direction);
4211 BUG();
4212 break;
4213 }
4214 } else {
4215 control |= IOACCEL1_CONTROL_NODATAXFER;
4216 }
4217
Scott Teelc3497752014-02-18 13:56:34 -06004218 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06004219 /* Fill out the command structure to submit */
Don Brace2b08b3e2015-01-23 16:41:09 -06004220 cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
4221 cp->transfer_len = cpu_to_le32(total_len);
4222 cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
4223 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
4224 cp->control = cpu_to_le32(control);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004225 memcpy(cp->CDB, cdb, cdb_len);
4226 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06004227 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004228 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06004229 return 0;
4230}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004231
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004232/*
4233 * Queue a command directly to a device behind the controller using the
4234 * I/O accelerator path.
4235 */
4236static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
4237 struct CommandList *c)
4238{
4239 struct scsi_cmnd *cmd = c->scsi_cmd;
4240 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4241
Don Brace03383732015-01-23 16:43:30 -06004242 c->phys_disk = dev;
4243
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004244 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004245 cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004246}
4247
Scott Teeldd0e19f2014-02-18 13:57:31 -06004248/*
4249 * Set encryption parameters for the ioaccel2 request
4250 */
4251static void set_encrypt_ioaccel2(struct ctlr_info *h,
4252 struct CommandList *c, struct io_accel2_cmd *cp)
4253{
4254 struct scsi_cmnd *cmd = c->scsi_cmd;
4255 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4256 struct raid_map_data *map = &dev->raid_map;
4257 u64 first_block;
4258
Scott Teeldd0e19f2014-02-18 13:57:31 -06004259 /* Are we doing encryption on this device */
Don Brace2b08b3e2015-01-23 16:41:09 -06004260 if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
Scott Teeldd0e19f2014-02-18 13:57:31 -06004261 return;
4262 /* Set the data encryption key index. */
4263 cp->dekindex = map->dekindex;
4264
4265 /* Set the encryption enable flag, encoded into direction field. */
4266 cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
4267
4268 /* Set encryption tweak values based on logical block address
4269 * If block size is 512, tweak value is LBA.
4270 * For other block sizes, tweak is (LBA * block size)/ 512)
4271 */
4272 switch (cmd->cmnd[0]) {
4273 /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
4274 case WRITE_6:
4275 case READ_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06004276 first_block = get_unaligned_be16(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004277 break;
4278 case WRITE_10:
4279 case READ_10:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004280 /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
4281 case WRITE_12:
4282 case READ_12:
Don Brace2b08b3e2015-01-23 16:41:09 -06004283 first_block = get_unaligned_be32(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004284 break;
4285 case WRITE_16:
4286 case READ_16:
Don Brace2b08b3e2015-01-23 16:41:09 -06004287 first_block = get_unaligned_be64(&cmd->cmnd[2]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004288 break;
4289 default:
4290 dev_err(&h->pdev->dev,
Don Brace2b08b3e2015-01-23 16:41:09 -06004291 "ERROR: %s: size (0x%x) not supported for encryption\n",
4292 __func__, cmd->cmnd[0]);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004293 BUG();
4294 break;
4295 }
Don Brace2b08b3e2015-01-23 16:41:09 -06004296
4297 if (le32_to_cpu(map->volume_blk_size) != 512)
4298 first_block = first_block *
4299 le32_to_cpu(map->volume_blk_size)/512;
4300
4301 cp->tweak_lower = cpu_to_le32(first_block);
4302 cp->tweak_upper = cpu_to_le32(first_block >> 32);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004303}
4304
Scott Teelc3497752014-02-18 13:56:34 -06004305static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
4306 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004307 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004308{
4309 struct scsi_cmnd *cmd = c->scsi_cmd;
4310 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
4311 struct ioaccel2_sg_element *curr_sg;
4312 int use_sg, i;
4313 struct scatterlist *sg;
4314 u64 addr64;
4315 u32 len;
4316 u32 total_len = 0;
4317
Webb Scalesd9a729f2015-04-23 09:33:27 -05004318 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Scott Teelc3497752014-02-18 13:56:34 -06004319
Don Brace03383732015-01-23 16:43:30 -06004320 if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
4321 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004322 return IO_ACCEL_INELIGIBLE;
Don Brace03383732015-01-23 16:43:30 -06004323 }
4324
Scott Teelc3497752014-02-18 13:56:34 -06004325 c->cmd_type = CMD_IOACCEL2;
4326 /* Adjust the DMA address to point to the accelerated command buffer */
4327 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
4328 (c->cmdindex * sizeof(*cp));
4329 BUG_ON(c->busaddr & 0x0000007F);
4330
4331 memset(cp, 0, sizeof(*cp));
4332 cp->IU_type = IOACCEL2_IU_TYPE;
4333
4334 use_sg = scsi_dma_map(cmd);
Don Brace03383732015-01-23 16:43:30 -06004335 if (use_sg < 0) {
4336 atomic_dec(&phys_disk->ioaccel_cmds_out);
Scott Teelc3497752014-02-18 13:56:34 -06004337 return use_sg;
Don Brace03383732015-01-23 16:43:30 -06004338 }
Scott Teelc3497752014-02-18 13:56:34 -06004339
4340 if (use_sg) {
Scott Teelc3497752014-02-18 13:56:34 -06004341 curr_sg = cp->sg;
Webb Scalesd9a729f2015-04-23 09:33:27 -05004342 if (use_sg > h->ioaccel_maxsg) {
4343 addr64 = le64_to_cpu(
4344 h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
4345 curr_sg->address = cpu_to_le64(addr64);
4346 curr_sg->length = 0;
4347 curr_sg->reserved[0] = 0;
4348 curr_sg->reserved[1] = 0;
4349 curr_sg->reserved[2] = 0;
4350 curr_sg->chain_indicator = 0x80;
4351
4352 curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
4353 }
Scott Teelc3497752014-02-18 13:56:34 -06004354 scsi_for_each_sg(cmd, sg, use_sg, i) {
4355 addr64 = (u64) sg_dma_address(sg);
4356 len = sg_dma_len(sg);
4357 total_len += len;
4358 curr_sg->address = cpu_to_le64(addr64);
4359 curr_sg->length = cpu_to_le32(len);
4360 curr_sg->reserved[0] = 0;
4361 curr_sg->reserved[1] = 0;
4362 curr_sg->reserved[2] = 0;
4363 curr_sg->chain_indicator = 0;
4364 curr_sg++;
4365 }
4366
4367 switch (cmd->sc_data_direction) {
4368 case DMA_TO_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004369 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4370 cp->direction |= IOACCEL2_DIR_DATA_OUT;
Scott Teelc3497752014-02-18 13:56:34 -06004371 break;
4372 case DMA_FROM_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004373 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4374 cp->direction |= IOACCEL2_DIR_DATA_IN;
Scott Teelc3497752014-02-18 13:56:34 -06004375 break;
4376 case DMA_NONE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06004377 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4378 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004379 break;
4380 default:
4381 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4382 cmd->sc_data_direction);
4383 BUG();
4384 break;
4385 }
4386 } else {
Scott Teeldd0e19f2014-02-18 13:57:31 -06004387 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4388 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06004389 }
Scott Teeldd0e19f2014-02-18 13:57:31 -06004390
4391 /* Set encryption parameters, if necessary */
4392 set_encrypt_ioaccel2(h, c, cp);
4393
Don Brace2b08b3e2015-01-23 16:41:09 -06004394 cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
Don Bracef2405db2015-01-23 16:43:09 -06004395 cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
Scott Teelc3497752014-02-18 13:56:34 -06004396 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
Scott Teelc3497752014-02-18 13:56:34 -06004397
Scott Teelc3497752014-02-18 13:56:34 -06004398 cp->data_len = cpu_to_le32(total_len);
4399 cp->err_ptr = cpu_to_le64(c->busaddr +
4400 offsetof(struct io_accel2_cmd, error_data));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06004401 cp->err_len = cpu_to_le32(sizeof(cp->error_data));
Scott Teelc3497752014-02-18 13:56:34 -06004402
Webb Scalesd9a729f2015-04-23 09:33:27 -05004403 /* fill in sg elements */
4404 if (use_sg > h->ioaccel_maxsg) {
4405 cp->sg_count = 1;
Don Bracea736e9b2015-11-04 15:51:14 -06004406 cp->sg[0].length = cpu_to_le32(use_sg * sizeof(cp->sg[0]));
Webb Scalesd9a729f2015-04-23 09:33:27 -05004407 if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
4408 atomic_dec(&phys_disk->ioaccel_cmds_out);
4409 scsi_dma_unmap(cmd);
4410 return -1;
4411 }
4412 } else
4413 cp->sg_count = (u8) use_sg;
4414
Scott Teelc3497752014-02-18 13:56:34 -06004415 enqueue_cmd_and_start_io(h, c);
4416 return 0;
4417}
4418
4419/*
4420 * Queue a command to the correct I/O accelerator path.
4421 */
4422static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
4423 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004424 u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
Scott Teelc3497752014-02-18 13:56:34 -06004425{
Don Brace03383732015-01-23 16:43:30 -06004426 /* Try to honor the device's queue depth */
4427 if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
4428 phys_disk->queue_depth) {
4429 atomic_dec(&phys_disk->ioaccel_cmds_out);
4430 return IO_ACCEL_INELIGIBLE;
4431 }
Scott Teelc3497752014-02-18 13:56:34 -06004432 if (h->transMethod & CFGTBL_Trans_io_accel1)
4433 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004434 cdb, cdb_len, scsi3addr,
4435 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004436 else
4437 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
Don Brace03383732015-01-23 16:43:30 -06004438 cdb, cdb_len, scsi3addr,
4439 phys_disk);
Scott Teelc3497752014-02-18 13:56:34 -06004440}
4441
Scott Teel6b80b182014-02-18 13:56:55 -06004442static void raid_map_helper(struct raid_map_data *map,
4443 int offload_to_mirror, u32 *map_index, u32 *current_group)
4444{
4445 if (offload_to_mirror == 0) {
4446 /* use physical disk in the first mirrored group. */
Don Brace2b08b3e2015-01-23 16:41:09 -06004447 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004448 return;
4449 }
4450 do {
4451 /* determine mirror group that *map_index indicates */
Don Brace2b08b3e2015-01-23 16:41:09 -06004452 *current_group = *map_index /
4453 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004454 if (offload_to_mirror == *current_group)
4455 continue;
Don Brace2b08b3e2015-01-23 16:41:09 -06004456 if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
Scott Teel6b80b182014-02-18 13:56:55 -06004457 /* select map index from next group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004458 *map_index += le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004459 (*current_group)++;
4460 } else {
4461 /* select map index from first group */
Don Brace2b08b3e2015-01-23 16:41:09 -06004462 *map_index %= le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004463 *current_group = 0;
4464 }
4465 } while (offload_to_mirror != *current_group);
4466}
4467
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004468/*
4469 * Attempt to perform offload RAID mapping for a logical volume I/O.
4470 */
4471static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4472 struct CommandList *c)
4473{
4474 struct scsi_cmnd *cmd = c->scsi_cmd;
4475 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4476 struct raid_map_data *map = &dev->raid_map;
4477 struct raid_map_disk_data *dd = &map->data[0];
4478 int is_write = 0;
4479 u32 map_index;
4480 u64 first_block, last_block;
4481 u32 block_cnt;
4482 u32 blocks_per_row;
4483 u64 first_row, last_row;
4484 u32 first_row_offset, last_row_offset;
4485 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06004486 u64 r0_first_row, r0_last_row;
4487 u32 r5or6_blocks_per_row;
4488 u64 r5or6_first_row, r5or6_last_row;
4489 u32 r5or6_first_row_offset, r5or6_last_row_offset;
4490 u32 r5or6_first_column, r5or6_last_column;
4491 u32 total_disks_per_row;
4492 u32 stripesize;
4493 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004494 u32 map_row;
4495 u32 disk_handle;
4496 u64 disk_block;
4497 u32 disk_block_cnt;
4498 u8 cdb[16];
4499 u8 cdb_len;
Don Brace2b08b3e2015-01-23 16:41:09 -06004500 u16 strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004501#if BITS_PER_LONG == 32
4502 u64 tmpdiv;
4503#endif
Scott Teel6b80b182014-02-18 13:56:55 -06004504 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004505
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004506 /* check for valid opcode, get LBA and block count */
4507 switch (cmd->cmnd[0]) {
4508 case WRITE_6:
4509 is_write = 1;
4510 case READ_6:
Don Bracec8a6c9a2015-11-04 15:50:50 -06004511 first_block = get_unaligned_be16(&cmd->cmnd[2]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004512 block_cnt = cmd->cmnd[4];
Stephen M. Cameron3fa89a02014-07-03 10:18:14 -05004513 if (block_cnt == 0)
4514 block_cnt = 256;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004515 break;
4516 case WRITE_10:
4517 is_write = 1;
4518 case READ_10:
4519 first_block =
4520 (((u64) cmd->cmnd[2]) << 24) |
4521 (((u64) cmd->cmnd[3]) << 16) |
4522 (((u64) cmd->cmnd[4]) << 8) |
4523 cmd->cmnd[5];
4524 block_cnt =
4525 (((u32) cmd->cmnd[7]) << 8) |
4526 cmd->cmnd[8];
4527 break;
4528 case WRITE_12:
4529 is_write = 1;
4530 case READ_12:
4531 first_block =
4532 (((u64) cmd->cmnd[2]) << 24) |
4533 (((u64) cmd->cmnd[3]) << 16) |
4534 (((u64) cmd->cmnd[4]) << 8) |
4535 cmd->cmnd[5];
4536 block_cnt =
4537 (((u32) cmd->cmnd[6]) << 24) |
4538 (((u32) cmd->cmnd[7]) << 16) |
4539 (((u32) cmd->cmnd[8]) << 8) |
4540 cmd->cmnd[9];
4541 break;
4542 case WRITE_16:
4543 is_write = 1;
4544 case READ_16:
4545 first_block =
4546 (((u64) cmd->cmnd[2]) << 56) |
4547 (((u64) cmd->cmnd[3]) << 48) |
4548 (((u64) cmd->cmnd[4]) << 40) |
4549 (((u64) cmd->cmnd[5]) << 32) |
4550 (((u64) cmd->cmnd[6]) << 24) |
4551 (((u64) cmd->cmnd[7]) << 16) |
4552 (((u64) cmd->cmnd[8]) << 8) |
4553 cmd->cmnd[9];
4554 block_cnt =
4555 (((u32) cmd->cmnd[10]) << 24) |
4556 (((u32) cmd->cmnd[11]) << 16) |
4557 (((u32) cmd->cmnd[12]) << 8) |
4558 cmd->cmnd[13];
4559 break;
4560 default:
4561 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4562 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004563 last_block = first_block + block_cnt - 1;
4564
4565 /* check for write to non-RAID-0 */
4566 if (is_write && dev->raid_level != 0)
4567 return IO_ACCEL_INELIGIBLE;
4568
4569 /* check for invalid block or wraparound */
Don Brace2b08b3e2015-01-23 16:41:09 -06004570 if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
4571 last_block < first_block)
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004572 return IO_ACCEL_INELIGIBLE;
4573
4574 /* calculate stripe information for the request */
Don Brace2b08b3e2015-01-23 16:41:09 -06004575 blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
4576 le16_to_cpu(map->strip_size);
4577 strip_size = le16_to_cpu(map->strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004578#if BITS_PER_LONG == 32
4579 tmpdiv = first_block;
4580 (void) do_div(tmpdiv, blocks_per_row);
4581 first_row = tmpdiv;
4582 tmpdiv = last_block;
4583 (void) do_div(tmpdiv, blocks_per_row);
4584 last_row = tmpdiv;
4585 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4586 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
4587 tmpdiv = first_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06004588 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004589 first_column = tmpdiv;
4590 tmpdiv = last_row_offset;
Don Brace2b08b3e2015-01-23 16:41:09 -06004591 (void) do_div(tmpdiv, strip_size);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004592 last_column = tmpdiv;
4593#else
4594 first_row = first_block / blocks_per_row;
4595 last_row = last_block / blocks_per_row;
4596 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4597 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
Don Brace2b08b3e2015-01-23 16:41:09 -06004598 first_column = first_row_offset / strip_size;
4599 last_column = last_row_offset / strip_size;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004600#endif
4601
4602 /* if this isn't a single row/column then give to the controller */
4603 if ((first_row != last_row) || (first_column != last_column))
4604 return IO_ACCEL_INELIGIBLE;
4605
4606 /* proceeding with driver mapping */
Don Brace2b08b3e2015-01-23 16:41:09 -06004607 total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
4608 le16_to_cpu(map->metadata_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004609 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06004610 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06004611 map_index = (map_row * total_disks_per_row) + first_column;
4612
4613 switch (dev->raid_level) {
4614 case HPSA_RAID_0:
4615 break; /* nothing special to do */
4616 case HPSA_RAID_1:
4617 /* Handles load balance across RAID 1 members.
4618 * (2-drive R1 and R10 with even # of drives.)
4619 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004620 */
Don Brace2b08b3e2015-01-23 16:41:09 -06004621 BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004622 if (dev->offload_to_mirror)
Don Brace2b08b3e2015-01-23 16:41:09 -06004623 map_index += le16_to_cpu(map->data_disks_per_row);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004624 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06004625 break;
4626 case HPSA_RAID_ADM:
4627 /* Handles N-way mirrors (R1-ADM)
4628 * and R10 with # of drives divisible by 3.)
4629 */
Don Brace2b08b3e2015-01-23 16:41:09 -06004630 BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
Scott Teel6b80b182014-02-18 13:56:55 -06004631
4632 offload_to_mirror = dev->offload_to_mirror;
4633 raid_map_helper(map, offload_to_mirror,
4634 &map_index, &current_group);
4635 /* set mirror group to use next time */
4636 offload_to_mirror =
Don Brace2b08b3e2015-01-23 16:41:09 -06004637 (offload_to_mirror >=
4638 le16_to_cpu(map->layout_map_count) - 1)
Scott Teel6b80b182014-02-18 13:56:55 -06004639 ? 0 : offload_to_mirror + 1;
Scott Teel6b80b182014-02-18 13:56:55 -06004640 dev->offload_to_mirror = offload_to_mirror;
4641 /* Avoid direct use of dev->offload_to_mirror within this
4642 * function since multiple threads might simultaneously
4643 * increment it beyond the range of dev->layout_map_count -1.
4644 */
4645 break;
4646 case HPSA_RAID_5:
4647 case HPSA_RAID_6:
Don Brace2b08b3e2015-01-23 16:41:09 -06004648 if (le16_to_cpu(map->layout_map_count) <= 1)
Scott Teel6b80b182014-02-18 13:56:55 -06004649 break;
4650
4651 /* Verify first and last block are in same RAID group */
4652 r5or6_blocks_per_row =
Don Brace2b08b3e2015-01-23 16:41:09 -06004653 le16_to_cpu(map->strip_size) *
4654 le16_to_cpu(map->data_disks_per_row);
Scott Teel6b80b182014-02-18 13:56:55 -06004655 BUG_ON(r5or6_blocks_per_row == 0);
Don Brace2b08b3e2015-01-23 16:41:09 -06004656 stripesize = r5or6_blocks_per_row *
4657 le16_to_cpu(map->layout_map_count);
Scott Teel6b80b182014-02-18 13:56:55 -06004658#if BITS_PER_LONG == 32
4659 tmpdiv = first_block;
4660 first_group = do_div(tmpdiv, stripesize);
4661 tmpdiv = first_group;
4662 (void) do_div(tmpdiv, r5or6_blocks_per_row);
4663 first_group = tmpdiv;
4664 tmpdiv = last_block;
4665 last_group = do_div(tmpdiv, stripesize);
4666 tmpdiv = last_group;
4667 (void) do_div(tmpdiv, r5or6_blocks_per_row);
4668 last_group = tmpdiv;
4669#else
4670 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
4671 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
Scott Teel6b80b182014-02-18 13:56:55 -06004672#endif
Stephen M. Cameron000ff7c2014-03-13 17:12:50 -05004673 if (first_group != last_group)
Scott Teel6b80b182014-02-18 13:56:55 -06004674 return IO_ACCEL_INELIGIBLE;
4675
4676 /* Verify request is in a single row of RAID 5/6 */
4677#if BITS_PER_LONG == 32
4678 tmpdiv = first_block;
4679 (void) do_div(tmpdiv, stripesize);
4680 first_row = r5or6_first_row = r0_first_row = tmpdiv;
4681 tmpdiv = last_block;
4682 (void) do_div(tmpdiv, stripesize);
4683 r5or6_last_row = r0_last_row = tmpdiv;
4684#else
4685 first_row = r5or6_first_row = r0_first_row =
4686 first_block / stripesize;
4687 r5or6_last_row = r0_last_row = last_block / stripesize;
4688#endif
4689 if (r5or6_first_row != r5or6_last_row)
4690 return IO_ACCEL_INELIGIBLE;
4691
4692
4693 /* Verify request is in a single column */
4694#if BITS_PER_LONG == 32
4695 tmpdiv = first_block;
4696 first_row_offset = do_div(tmpdiv, stripesize);
4697 tmpdiv = first_row_offset;
4698 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
4699 r5or6_first_row_offset = first_row_offset;
4700 tmpdiv = last_block;
4701 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
4702 tmpdiv = r5or6_last_row_offset;
4703 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
4704 tmpdiv = r5or6_first_row_offset;
4705 (void) do_div(tmpdiv, map->strip_size);
4706 first_column = r5or6_first_column = tmpdiv;
4707 tmpdiv = r5or6_last_row_offset;
4708 (void) do_div(tmpdiv, map->strip_size);
4709 r5or6_last_column = tmpdiv;
4710#else
4711 first_row_offset = r5or6_first_row_offset =
4712 (u32)((first_block % stripesize) %
4713 r5or6_blocks_per_row);
4714
4715 r5or6_last_row_offset =
4716 (u32)((last_block % stripesize) %
4717 r5or6_blocks_per_row);
4718
4719 first_column = r5or6_first_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06004720 r5or6_first_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06004721 r5or6_last_column =
Don Brace2b08b3e2015-01-23 16:41:09 -06004722 r5or6_last_row_offset / le16_to_cpu(map->strip_size);
Scott Teel6b80b182014-02-18 13:56:55 -06004723#endif
4724 if (r5or6_first_column != r5or6_last_column)
4725 return IO_ACCEL_INELIGIBLE;
4726
4727 /* Request is eligible */
4728 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
Don Brace2b08b3e2015-01-23 16:41:09 -06004729 le16_to_cpu(map->row_cnt);
Scott Teel6b80b182014-02-18 13:56:55 -06004730
4731 map_index = (first_group *
Don Brace2b08b3e2015-01-23 16:41:09 -06004732 (le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
Scott Teel6b80b182014-02-18 13:56:55 -06004733 (map_row * total_disks_per_row) + first_column;
4734 break;
4735 default:
4736 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004737 }
Scott Teel6b80b182014-02-18 13:56:55 -06004738
Stephen Cameron07543e02015-01-23 16:44:14 -06004739 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
4740 return IO_ACCEL_INELIGIBLE;
4741
Don Brace03383732015-01-23 16:43:30 -06004742 c->phys_disk = dev->phys_disk[map_index];
4743
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004744 disk_handle = dd[map_index].ioaccel_handle;
Don Brace2b08b3e2015-01-23 16:41:09 -06004745 disk_block = le64_to_cpu(map->disk_starting_blk) +
4746 first_row * le16_to_cpu(map->strip_size) +
4747 (first_row_offset - first_column *
4748 le16_to_cpu(map->strip_size));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004749 disk_block_cnt = block_cnt;
4750
4751 /* handle differing logical/physical block sizes */
4752 if (map->phys_blk_shift) {
4753 disk_block <<= map->phys_blk_shift;
4754 disk_block_cnt <<= map->phys_blk_shift;
4755 }
4756 BUG_ON(disk_block_cnt > 0xffff);
4757
4758 /* build the new CDB for the physical disk I/O */
4759 if (disk_block > 0xffffffff) {
4760 cdb[0] = is_write ? WRITE_16 : READ_16;
4761 cdb[1] = 0;
4762 cdb[2] = (u8) (disk_block >> 56);
4763 cdb[3] = (u8) (disk_block >> 48);
4764 cdb[4] = (u8) (disk_block >> 40);
4765 cdb[5] = (u8) (disk_block >> 32);
4766 cdb[6] = (u8) (disk_block >> 24);
4767 cdb[7] = (u8) (disk_block >> 16);
4768 cdb[8] = (u8) (disk_block >> 8);
4769 cdb[9] = (u8) (disk_block);
4770 cdb[10] = (u8) (disk_block_cnt >> 24);
4771 cdb[11] = (u8) (disk_block_cnt >> 16);
4772 cdb[12] = (u8) (disk_block_cnt >> 8);
4773 cdb[13] = (u8) (disk_block_cnt);
4774 cdb[14] = 0;
4775 cdb[15] = 0;
4776 cdb_len = 16;
4777 } else {
4778 cdb[0] = is_write ? WRITE_10 : READ_10;
4779 cdb[1] = 0;
4780 cdb[2] = (u8) (disk_block >> 24);
4781 cdb[3] = (u8) (disk_block >> 16);
4782 cdb[4] = (u8) (disk_block >> 8);
4783 cdb[5] = (u8) (disk_block);
4784 cdb[6] = 0;
4785 cdb[7] = (u8) (disk_block_cnt >> 8);
4786 cdb[8] = (u8) (disk_block_cnt);
4787 cdb[9] = 0;
4788 cdb_len = 10;
4789 }
4790 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
Don Brace03383732015-01-23 16:43:30 -06004791 dev->scsi3addr,
4792 dev->phys_disk[map_index]);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004793}
4794
Webb Scales25163bd2015-04-23 09:32:00 -05004795/*
4796 * Submit commands down the "normal" RAID stack path
4797 * All callers to hpsa_ciss_submit must check lockup_detected
4798 * beforehand, before (opt.) and after calling cmd_alloc
4799 */
Stephen Cameron574f05d2015-01-23 16:43:20 -06004800static int hpsa_ciss_submit(struct ctlr_info *h,
4801 struct CommandList *c, struct scsi_cmnd *cmd,
4802 unsigned char scsi3addr[])
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004803{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004804 cmd->host_scribble = (unsigned char *) c;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004805 c->cmd_type = CMD_SCSI;
4806 c->scsi_cmd = cmd;
4807 c->Header.ReplyQueue = 0; /* unused in simple mode */
4808 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Bracef2405db2015-01-23 16:43:09 -06004809 c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004810
4811 /* Fill in the request block... */
4812
4813 c->Request.Timeout = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004814 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4815 c->Request.CDBLen = cmd->cmd_len;
4816 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004817 switch (cmd->sc_data_direction) {
4818 case DMA_TO_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06004819 c->Request.type_attr_dir =
4820 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004821 break;
4822 case DMA_FROM_DEVICE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06004823 c->Request.type_attr_dir =
4824 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004825 break;
4826 case DMA_NONE:
Stephen M. Camerona505b862014-11-14 17:27:04 -06004827 c->Request.type_attr_dir =
4828 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004829 break;
4830 case DMA_BIDIRECTIONAL:
4831 /* This can happen if a buggy application does a scsi passthru
4832 * and sets both inlen and outlen to non-zero. ( see
4833 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4834 */
4835
Stephen M. Camerona505b862014-11-14 17:27:04 -06004836 c->Request.type_attr_dir =
4837 TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004838 /* This is technically wrong, and hpsa controllers should
4839 * reject it with CMD_INVALID, which is the most correct
4840 * response, but non-fibre backends appear to let it
4841 * slide by, and give the same results as if this field
4842 * were set correctly. Either way is acceptable for
4843 * our purposes here.
4844 */
4845
4846 break;
4847
4848 default:
4849 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4850 cmd->sc_data_direction);
4851 BUG();
4852 break;
4853 }
4854
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06004855 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Webb Scales73153fe2015-04-23 09:35:04 -05004856 hpsa_cmd_resolve_and_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004857 return SCSI_MLQUEUE_HOST_BUSY;
4858 }
4859 enqueue_cmd_and_start_io(h, c);
4860 /* the cmd'll come back via intr handler in complete_scsi_command() */
4861 return 0;
4862}
4863
Stephen Cameron360c73b2015-04-23 09:32:32 -05004864static void hpsa_cmd_init(struct ctlr_info *h, int index,
4865 struct CommandList *c)
4866{
4867 dma_addr_t cmd_dma_handle, err_dma_handle;
4868
4869 /* Zero out all of commandlist except the last field, refcount */
4870 memset(c, 0, offsetof(struct CommandList, refcount));
4871 c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
4872 cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4873 c->err_info = h->errinfo_pool + index;
4874 memset(c->err_info, 0, sizeof(*c->err_info));
4875 err_dma_handle = h->errinfo_pool_dhandle
4876 + index * sizeof(*c->err_info);
4877 c->cmdindex = index;
4878 c->busaddr = (u32) cmd_dma_handle;
4879 c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
4880 c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
4881 c->h = h;
Webb Scalesa58e7e52015-04-23 09:34:16 -05004882 c->scsi_cmd = SCSI_CMD_IDLE;
Stephen Cameron360c73b2015-04-23 09:32:32 -05004883}
4884
4885static void hpsa_preinitialize_commands(struct ctlr_info *h)
4886{
4887 int i;
4888
4889 for (i = 0; i < h->nr_cmds; i++) {
4890 struct CommandList *c = h->cmd_pool + i;
4891
4892 hpsa_cmd_init(h, i, c);
4893 atomic_set(&c->refcount, 0);
4894 }
4895}
4896
4897static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
4898 struct CommandList *c)
4899{
4900 dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4901
Webb Scales73153fe2015-04-23 09:35:04 -05004902 BUG_ON(c->cmdindex != index);
4903
Stephen Cameron360c73b2015-04-23 09:32:32 -05004904 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4905 memset(c->err_info, 0, sizeof(*c->err_info));
4906 c->busaddr = (u32) cmd_dma_handle;
4907}
4908
Webb Scales592a0ad2015-04-23 09:32:48 -05004909static int hpsa_ioaccel_submit(struct ctlr_info *h,
4910 struct CommandList *c, struct scsi_cmnd *cmd,
4911 unsigned char *scsi3addr)
4912{
4913 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4914 int rc = IO_ACCEL_INELIGIBLE;
4915
4916 cmd->host_scribble = (unsigned char *) c;
4917
4918 if (dev->offload_enabled) {
4919 hpsa_cmd_init(h, c->cmdindex, c);
4920 c->cmd_type = CMD_SCSI;
4921 c->scsi_cmd = cmd;
4922 rc = hpsa_scsi_ioaccel_raid_map(h, c);
4923 if (rc < 0) /* scsi_dma_map failed. */
4924 rc = SCSI_MLQUEUE_HOST_BUSY;
Joe Handzika3144e02015-04-23 09:32:59 -05004925 } else if (dev->hba_ioaccel_enabled) {
Webb Scales592a0ad2015-04-23 09:32:48 -05004926 hpsa_cmd_init(h, c->cmdindex, c);
4927 c->cmd_type = CMD_SCSI;
4928 c->scsi_cmd = cmd;
4929 rc = hpsa_scsi_ioaccel_direct_map(h, c);
4930 if (rc < 0) /* scsi_dma_map failed. */
4931 rc = SCSI_MLQUEUE_HOST_BUSY;
4932 }
4933 return rc;
4934}
4935
Don Brace080ef1c2015-01-23 16:43:25 -06004936static void hpsa_command_resubmit_worker(struct work_struct *work)
4937{
4938 struct scsi_cmnd *cmd;
4939 struct hpsa_scsi_dev_t *dev;
Webb Scales8a0ff922015-04-23 09:34:11 -05004940 struct CommandList *c = container_of(work, struct CommandList, work);
Don Brace080ef1c2015-01-23 16:43:25 -06004941
4942 cmd = c->scsi_cmd;
4943 dev = cmd->device->hostdata;
4944 if (!dev) {
4945 cmd->result = DID_NO_CONNECT << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05004946 return hpsa_cmd_free_and_done(c->h, c, cmd);
Don Brace080ef1c2015-01-23 16:43:25 -06004947 }
Webb Scalesd604f532015-04-23 09:35:22 -05004948 if (c->reset_pending)
4949 return hpsa_cmd_resolve_and_free(c->h, c);
Webb Scalesa58e7e52015-04-23 09:34:16 -05004950 if (c->abort_pending)
4951 return hpsa_cmd_abort_and_free(c->h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05004952 if (c->cmd_type == CMD_IOACCEL2) {
4953 struct ctlr_info *h = c->h;
4954 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
4955 int rc;
4956
4957 if (c2->error_data.serv_response ==
4958 IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
4959 rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
4960 if (rc == 0)
4961 return;
4962 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4963 /*
4964 * If we get here, it means dma mapping failed.
4965 * Try again via scsi mid layer, which will
4966 * then get SCSI_MLQUEUE_HOST_BUSY.
4967 */
4968 cmd->result = DID_IMM_RETRY << 16;
Webb Scales8a0ff922015-04-23 09:34:11 -05004969 return hpsa_cmd_free_and_done(h, c, cmd);
Webb Scales592a0ad2015-04-23 09:32:48 -05004970 }
4971 /* else, fall thru and resubmit down CISS path */
4972 }
4973 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05004974 hpsa_cmd_partial_init(c->h, c->cmdindex, c);
Don Brace080ef1c2015-01-23 16:43:25 -06004975 if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
4976 /*
4977 * If we get here, it means dma mapping failed. Try
4978 * again via scsi mid layer, which will then get
4979 * SCSI_MLQUEUE_HOST_BUSY.
Webb Scales592a0ad2015-04-23 09:32:48 -05004980 *
4981 * hpsa_ciss_submit will have already freed c
4982 * if it encountered a dma mapping failure.
Don Brace080ef1c2015-01-23 16:43:25 -06004983 */
4984 cmd->result = DID_IMM_RETRY << 16;
4985 cmd->scsi_done(cmd);
4986 }
4987}
4988
Stephen Cameron574f05d2015-01-23 16:43:20 -06004989/* Running in struct Scsi_Host->host_lock less mode */
4990static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
4991{
4992 struct ctlr_info *h;
4993 struct hpsa_scsi_dev_t *dev;
4994 unsigned char scsi3addr[8];
4995 struct CommandList *c;
4996 int rc = 0;
4997
4998 /* Get the ptr to our adapter structure out of cmd->host. */
4999 h = sdev_to_hba(cmd->device);
Webb Scales73153fe2015-04-23 09:35:04 -05005000
5001 BUG_ON(cmd->request->tag < 0);
5002
Stephen Cameron574f05d2015-01-23 16:43:20 -06005003 dev = cmd->device->hostdata;
5004 if (!dev) {
5005 cmd->result = DID_NO_CONNECT << 16;
5006 cmd->scsi_done(cmd);
5007 return 0;
5008 }
Webb Scales73153fe2015-04-23 09:35:04 -05005009
Stephen Cameron574f05d2015-01-23 16:43:20 -06005010 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
5011
5012 if (unlikely(lockup_detected(h))) {
Webb Scales25163bd2015-04-23 09:32:00 -05005013 cmd->result = DID_NO_CONNECT << 16;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005014 cmd->scsi_done(cmd);
5015 return 0;
5016 }
Webb Scales73153fe2015-04-23 09:35:04 -05005017 c = cmd_tagged_alloc(h, cmd);
Stephen Cameron574f05d2015-01-23 16:43:20 -06005018
Stephen Cameron407863c2015-01-23 16:44:19 -06005019 /*
5020 * Call alternate submit routine for I/O accelerated commands.
Stephen Cameron574f05d2015-01-23 16:43:20 -06005021 * Retries always go down the normal I/O path.
5022 */
5023 if (likely(cmd->retries == 0 &&
5024 cmd->request->cmd_type == REQ_TYPE_FS &&
5025 h->acciopath_status)) {
Webb Scales592a0ad2015-04-23 09:32:48 -05005026 rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
5027 if (rc == 0)
5028 return 0;
5029 if (rc == SCSI_MLQUEUE_HOST_BUSY) {
Webb Scales73153fe2015-04-23 09:35:04 -05005030 hpsa_cmd_resolve_and_free(h, c);
Webb Scales592a0ad2015-04-23 09:32:48 -05005031 return SCSI_MLQUEUE_HOST_BUSY;
Stephen Cameron574f05d2015-01-23 16:43:20 -06005032 }
5033 }
5034 return hpsa_ciss_submit(h, c, cmd, scsi3addr);
5035}
5036
Webb Scales8ebc9242015-01-23 16:44:50 -06005037static void hpsa_scan_complete(struct ctlr_info *h)
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005038{
5039 unsigned long flags;
5040
Webb Scales8ebc9242015-01-23 16:44:50 -06005041 spin_lock_irqsave(&h->scan_lock, flags);
5042 h->scan_finished = 1;
5043 wake_up_all(&h->scan_wait_queue);
5044 spin_unlock_irqrestore(&h->scan_lock, flags);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005045}
5046
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005047static void hpsa_scan_start(struct Scsi_Host *sh)
5048{
5049 struct ctlr_info *h = shost_to_hba(sh);
5050 unsigned long flags;
5051
Webb Scales8ebc9242015-01-23 16:44:50 -06005052 /*
5053 * Don't let rescans be initiated on a controller known to be locked
5054 * up. If the controller locks up *during* a rescan, that thread is
5055 * probably hosed, but at least we can prevent new rescan threads from
5056 * piling up on a locked up controller.
5057 */
5058 if (unlikely(lockup_detected(h)))
5059 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005060
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005061 /* wait until any scan already in progress is finished. */
5062 while (1) {
5063 spin_lock_irqsave(&h->scan_lock, flags);
5064 if (h->scan_finished)
5065 break;
5066 spin_unlock_irqrestore(&h->scan_lock, flags);
5067 wait_event(h->scan_wait_queue, h->scan_finished);
5068 /* Note: We don't need to worry about a race between this
5069 * thread and driver unload because the midlayer will
5070 * have incremented the reference count, so unload won't
5071 * happen if we're in here.
5072 */
5073 }
5074 h->scan_finished = 0; /* mark scan as in progress */
5075 spin_unlock_irqrestore(&h->scan_lock, flags);
5076
Webb Scales8ebc9242015-01-23 16:44:50 -06005077 if (unlikely(lockup_detected(h)))
5078 return hpsa_scan_complete(h);
Stephen M. Cameron5f389362014-02-18 13:55:48 -06005079
Don Brace8aa60682015-11-04 15:50:01 -06005080 hpsa_update_scsi_devices(h);
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005081
Webb Scales8ebc9242015-01-23 16:44:50 -06005082 hpsa_scan_complete(h);
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005083}
5084
Don Brace7c0a0222015-01-23 16:41:30 -06005085static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
5086{
Don Brace03383732015-01-23 16:43:30 -06005087 struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
5088
5089 if (!logical_drive)
5090 return -ENODEV;
Don Brace7c0a0222015-01-23 16:41:30 -06005091
5092 if (qdepth < 1)
5093 qdepth = 1;
Don Brace03383732015-01-23 16:43:30 -06005094 else if (qdepth > logical_drive->queue_depth)
5095 qdepth = logical_drive->queue_depth;
5096
5097 return scsi_change_queue_depth(sdev, qdepth);
Don Brace7c0a0222015-01-23 16:41:30 -06005098}
5099
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005100static int hpsa_scan_finished(struct Scsi_Host *sh,
5101 unsigned long elapsed_time)
5102{
5103 struct ctlr_info *h = shost_to_hba(sh);
5104 unsigned long flags;
5105 int finished;
5106
5107 spin_lock_irqsave(&h->scan_lock, flags);
5108 finished = h->scan_finished;
5109 spin_unlock_irqrestore(&h->scan_lock, flags);
5110 return finished;
5111}
5112
Robert Elliott2946e822015-04-23 09:35:09 -05005113static int hpsa_scsi_host_alloc(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005114{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005115 struct Scsi_Host *sh;
5116 int error;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005117
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005118 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
Robert Elliott2946e822015-04-23 09:35:09 -05005119 if (sh == NULL) {
5120 dev_err(&h->pdev->dev, "scsi_host_alloc failed\n");
5121 return -ENOMEM;
5122 }
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005123
5124 sh->io_port = 0;
5125 sh->n_io_port = 0;
5126 sh->this_id = -1;
5127 sh->max_channel = 3;
5128 sh->max_cmd_len = MAX_COMMAND_SIZE;
5129 sh->max_lun = HPSA_MAX_LUN;
5130 sh->max_id = HPSA_MAX_LUN;
Stephen Cameron41ce4c32015-04-23 09:31:47 -05005131 sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
Don Brace03383732015-01-23 16:43:30 -06005132 sh->cmd_per_lun = sh->can_queue;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005133 sh->sg_tablesize = h->maxsgentries;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005134 sh->hostdata[0] = (unsigned long) h;
5135 sh->irq = h->intr[h->intr_mode];
5136 sh->unique_id = sh->irq;
Webb Scales73153fe2015-04-23 09:35:04 -05005137 error = scsi_init_shared_tag_map(sh, sh->can_queue);
5138 if (error) {
5139 dev_err(&h->pdev->dev,
5140 "%s: scsi_init_shared_tag_map failed for controller %d\n",
5141 __func__, h->ctlr);
Robert Elliott2946e822015-04-23 09:35:09 -05005142 scsi_host_put(sh);
5143 return error;
Webb Scales73153fe2015-04-23 09:35:04 -05005144 }
Robert Elliott2946e822015-04-23 09:35:09 -05005145 h->scsi_host = sh;
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005146 return 0;
Robert Elliott2946e822015-04-23 09:35:09 -05005147}
Stephen M. Cameronb7056902012-01-19 14:00:53 -06005148
Robert Elliott2946e822015-04-23 09:35:09 -05005149static int hpsa_scsi_add_host(struct ctlr_info *h)
5150{
5151 int rv;
5152
5153 rv = scsi_add_host(h->scsi_host, &h->pdev->dev);
5154 if (rv) {
5155 dev_err(&h->pdev->dev, "scsi_add_host failed\n");
5156 return rv;
5157 }
5158 scsi_scan_host(h->scsi_host);
5159 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005160}
5161
Webb Scalesb69324f2015-04-23 09:34:22 -05005162/*
Webb Scales73153fe2015-04-23 09:35:04 -05005163 * The block layer has already gone to the trouble of picking out a unique,
5164 * small-integer tag for this request. We use an offset from that value as
5165 * an index to select our command block. (The offset allows us to reserve the
5166 * low-numbered entries for our own uses.)
5167 */
5168static int hpsa_get_cmd_index(struct scsi_cmnd *scmd)
5169{
5170 int idx = scmd->request->tag;
5171
5172 if (idx < 0)
5173 return idx;
5174
5175 /* Offset to leave space for internal cmds. */
5176 return idx += HPSA_NRESERVED_CMDS;
5177}
5178
5179/*
Webb Scalesb69324f2015-04-23 09:34:22 -05005180 * Send a TEST_UNIT_READY command to the specified LUN using the specified
5181 * reply queue; returns zero if the unit is ready, and non-zero otherwise.
5182 */
5183static int hpsa_send_test_unit_ready(struct ctlr_info *h,
5184 struct CommandList *c, unsigned char lunaddr[],
5185 int reply_queue)
5186{
5187 int rc;
5188
5189 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
5190 (void) fill_cmd(c, TEST_UNIT_READY, h,
5191 NULL, 0, 0, lunaddr, TYPE_CMD);
5192 rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
5193 if (rc)
5194 return rc;
5195 /* no unmap needed here because no data xfer. */
5196
5197 /* Check if the unit is already ready. */
5198 if (c->err_info->CommandStatus == CMD_SUCCESS)
5199 return 0;
5200
5201 /*
5202 * The first command sent after reset will receive "unit attention" to
5203 * indicate that the LUN has been reset...this is actually what we're
5204 * looking for (but, success is good too).
5205 */
5206 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5207 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
5208 (c->err_info->SenseInfo[2] == NO_SENSE ||
5209 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
5210 return 0;
5211
5212 return 1;
5213}
5214
5215/*
5216 * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
5217 * returns zero when the unit is ready, and non-zero when giving up.
5218 */
5219static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
5220 struct CommandList *c,
5221 unsigned char lunaddr[], int reply_queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005222{
Tomas Henzl89193582014-02-21 16:25:05 -06005223 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005224 int count = 0;
5225 int waittime = 1; /* seconds */
Webb Scalesb69324f2015-04-23 09:34:22 -05005226
5227 /* Send test unit ready until device ready, or give up. */
5228 for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
5229
5230 /*
5231 * Wait for a bit. do this first, because if we send
5232 * the TUR right away, the reset will just abort it.
5233 */
5234 msleep(1000 * waittime);
5235
5236 rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
5237 if (!rc)
5238 break;
5239
5240 /* Increase wait time with each try, up to a point. */
5241 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
5242 waittime *= 2;
5243
5244 dev_warn(&h->pdev->dev,
5245 "waiting %d secs for device to become ready.\n",
5246 waittime);
5247 }
5248
5249 return rc;
5250}
5251
5252static int wait_for_device_to_become_ready(struct ctlr_info *h,
5253 unsigned char lunaddr[],
5254 int reply_queue)
5255{
5256 int first_queue;
5257 int last_queue;
5258 int rq;
5259 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005260 struct CommandList *c;
5261
Stephen Cameron45fcb862015-01-23 16:43:04 -06005262 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005263
Webb Scalesb69324f2015-04-23 09:34:22 -05005264 /*
5265 * If no specific reply queue was requested, then send the TUR
5266 * repeatedly, requesting a reply on each reply queue; otherwise execute
5267 * the loop exactly once using only the specified queue.
5268 */
5269 if (reply_queue == DEFAULT_REPLY_QUEUE) {
5270 first_queue = 0;
5271 last_queue = h->nreply_queues - 1;
5272 } else {
5273 first_queue = reply_queue;
5274 last_queue = reply_queue;
5275 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005276
Webb Scalesb69324f2015-04-23 09:34:22 -05005277 for (rq = first_queue; rq <= last_queue; rq++) {
5278 rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
Webb Scales25163bd2015-04-23 09:32:00 -05005279 if (rc)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005280 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005281 }
5282
5283 if (rc)
5284 dev_warn(&h->pdev->dev, "giving up on device.\n");
5285 else
5286 dev_warn(&h->pdev->dev, "device is ready.\n");
5287
Stephen Cameron45fcb862015-01-23 16:43:04 -06005288 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005289 return rc;
5290}
5291
5292/* Need at least one of these error handlers to keep ../scsi/hosts.c from
5293 * complaining. Doing a host- or bus-reset can't do anything good here.
5294 */
5295static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
5296{
5297 int rc;
5298 struct ctlr_info *h;
5299 struct hpsa_scsi_dev_t *dev;
Scott Teel0b9b7b62015-11-04 15:51:02 -06005300 u8 reset_type;
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005301 char msg[48];
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005302
5303 /* find the controller to which the command to be aborted was sent */
5304 h = sdev_to_hba(scsicmd->device);
5305 if (h == NULL) /* paranoia */
5306 return FAILED;
Don Bracee3458932015-01-23 16:44:24 -06005307
5308 if (lockup_detected(h))
5309 return FAILED;
5310
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005311 dev = scsicmd->device->hostdata;
5312 if (!dev) {
Webb Scalesd604f532015-04-23 09:35:22 -05005313 dev_err(&h->pdev->dev, "%s: device lookup failed\n", __func__);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005314 return FAILED;
5315 }
Webb Scales25163bd2015-04-23 09:32:00 -05005316
5317 /* if controller locked up, we can guarantee command won't complete */
5318 if (lockup_detected(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005319 snprintf(msg, sizeof(msg),
5320 "cmd %d RESET FAILED, lockup detected",
5321 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005322 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005323 return FAILED;
5324 }
5325
5326 /* this reset request might be the result of a lockup; check */
5327 if (detect_controller_lockup(h)) {
Dan Carpenter2dc127b2015-06-04 17:47:56 +03005328 snprintf(msg, sizeof(msg),
5329 "cmd %d RESET FAILED, new lockup detected",
5330 hpsa_get_cmd_index(scsicmd));
Webb Scales73153fe2015-04-23 09:35:04 -05005331 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005332 return FAILED;
5333 }
5334
Webb Scalesd604f532015-04-23 09:35:22 -05005335 /* Do not attempt on controller */
5336 if (is_hba_lunid(dev->scsi3addr))
5337 return SUCCESS;
5338
Scott Teel0b9b7b62015-11-04 15:51:02 -06005339 if (is_logical_dev_addr_mode(dev->scsi3addr))
5340 reset_type = HPSA_DEVICE_RESET_MSG;
5341 else
5342 reset_type = HPSA_PHYS_TARGET_RESET;
5343
5344 sprintf(msg, "resetting %s",
5345 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ");
5346 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Webb Scales25163bd2015-04-23 09:32:00 -05005347
Don Braceda03ded2015-11-04 15:50:56 -06005348 h->reset_in_progress = 1;
5349
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005350 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teel0b9b7b62015-11-04 15:51:02 -06005351 rc = hpsa_do_reset(h, dev, dev->scsi3addr, reset_type,
Webb Scalesd604f532015-04-23 09:35:22 -05005352 DEFAULT_REPLY_QUEUE);
Scott Teel0b9b7b62015-11-04 15:51:02 -06005353 sprintf(msg, "reset %s %s",
5354 reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ",
5355 rc == 0 ? "completed successfully" : "failed");
Webb Scalesd604f532015-04-23 09:35:22 -05005356 hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
Don Braceda03ded2015-11-04 15:50:56 -06005357 h->reset_in_progress = 0;
Webb Scalesd604f532015-04-23 09:35:22 -05005358 return rc == 0 ? SUCCESS : FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005359}
5360
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005361static void swizzle_abort_tag(u8 *tag)
5362{
5363 u8 original_tag[8];
5364
5365 memcpy(original_tag, tag, 8);
5366 tag[0] = original_tag[3];
5367 tag[1] = original_tag[2];
5368 tag[2] = original_tag[1];
5369 tag[3] = original_tag[0];
5370 tag[4] = original_tag[7];
5371 tag[5] = original_tag[6];
5372 tag[6] = original_tag[5];
5373 tag[7] = original_tag[4];
5374}
5375
Scott Teel17eb87d2014-02-18 13:55:28 -06005376static void hpsa_get_tag(struct ctlr_info *h,
Don Brace2b08b3e2015-01-23 16:41:09 -06005377 struct CommandList *c, __le32 *taglower, __le32 *tagupper)
Scott Teel17eb87d2014-02-18 13:55:28 -06005378{
Don Brace2b08b3e2015-01-23 16:41:09 -06005379 u64 tag;
Scott Teel17eb87d2014-02-18 13:55:28 -06005380 if (c->cmd_type == CMD_IOACCEL1) {
5381 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
5382 &h->ioaccel_cmd_pool[c->cmdindex];
Don Brace2b08b3e2015-01-23 16:41:09 -06005383 tag = le64_to_cpu(cm1->tag);
5384 *tagupper = cpu_to_le32(tag >> 32);
5385 *taglower = cpu_to_le32(tag);
Scott Teel54b6e9e2014-02-18 13:56:45 -06005386 return;
Scott Teel17eb87d2014-02-18 13:55:28 -06005387 }
Scott Teel54b6e9e2014-02-18 13:56:45 -06005388 if (c->cmd_type == CMD_IOACCEL2) {
5389 struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
5390 &h->ioaccel2_cmd_pool[c->cmdindex];
Scott Teeldd0e19f2014-02-18 13:57:31 -06005391 /* upper tag not used in ioaccel2 mode */
5392 memset(tagupper, 0, sizeof(*tagupper));
5393 *taglower = cm2->Tag;
Scott Teel54b6e9e2014-02-18 13:56:45 -06005394 return;
5395 }
Don Brace2b08b3e2015-01-23 16:41:09 -06005396 tag = le64_to_cpu(c->Header.tag);
5397 *tagupper = cpu_to_le32(tag >> 32);
5398 *taglower = cpu_to_le32(tag);
Scott Teel17eb87d2014-02-18 13:55:28 -06005399}
5400
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005401static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005402 struct CommandList *abort, int reply_queue)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005403{
5404 int rc = IO_OK;
5405 struct CommandList *c;
5406 struct ErrorInfo *ei;
Don Brace2b08b3e2015-01-23 16:41:09 -06005407 __le32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005408
Stephen Cameron45fcb862015-01-23 16:43:04 -06005409 c = cmd_alloc(h);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005410
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005411 /* fill_cmd can't fail here, no buffer to map */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005412 (void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005413 0, 0, scsi3addr, TYPE_MSG);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005414 if (h->needs_abort_tags_swizzled)
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005415 swizzle_abort_tag(&c->Request.CDB[4]);
Webb Scales25163bd2015-04-23 09:32:00 -05005416 (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
Scott Teel17eb87d2014-02-18 13:55:28 -06005417 hpsa_get_tag(h, abort, &taglower, &tagupper);
Webb Scales25163bd2015-04-23 09:32:00 -05005418 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06005419 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005420 /* no unmap needed here because no data xfer. */
5421
5422 ei = c->err_info;
5423 switch (ei->CommandStatus) {
5424 case CMD_SUCCESS:
5425 break;
Stephen Cameron9437ac42015-04-23 09:32:16 -05005426 case CMD_TMF_STATUS:
5427 rc = hpsa_evaluate_tmf_status(h, c);
5428 break;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005429 case CMD_UNABORTABLE: /* Very common, don't make noise. */
5430 rc = -1;
5431 break;
5432 default:
5433 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06005434 __func__, tagupper, taglower);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06005435 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005436 rc = -1;
5437 break;
5438 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06005439 cmd_free(h, c);
Scott Teeldd0e19f2014-02-18 13:57:31 -06005440 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
5441 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005442 return rc;
5443}
5444
Stephen Cameron8be986c2015-04-23 09:34:06 -05005445static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
5446 struct CommandList *command_to_abort, int reply_queue)
5447{
5448 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5449 struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
5450 struct io_accel2_cmd *c2a =
5451 &h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
Webb Scalesa58e7e52015-04-23 09:34:16 -05005452 struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
Stephen Cameron8be986c2015-04-23 09:34:06 -05005453 struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
5454
5455 /*
5456 * We're overlaying struct hpsa_tmf_struct on top of something which
5457 * was allocated as a struct io_accel2_cmd, so we better be sure it
5458 * actually fits, and doesn't overrun the error info space.
5459 */
5460 BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
5461 sizeof(struct io_accel2_cmd));
5462 BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
5463 offsetof(struct hpsa_tmf_struct, error_len) +
5464 sizeof(ac->error_len));
5465
5466 c->cmd_type = IOACCEL2_TMF;
Webb Scalesa58e7e52015-04-23 09:34:16 -05005467 c->scsi_cmd = SCSI_CMD_BUSY;
5468
Stephen Cameron8be986c2015-04-23 09:34:06 -05005469 /* Adjust the DMA address to point to the accelerated command buffer */
5470 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
5471 (c->cmdindex * sizeof(struct io_accel2_cmd));
5472 BUG_ON(c->busaddr & 0x0000007F);
5473
5474 memset(ac, 0, sizeof(*c2)); /* yes this is correct */
5475 ac->iu_type = IOACCEL2_IU_TMF_TYPE;
5476 ac->reply_queue = reply_queue;
5477 ac->tmf = IOACCEL2_TMF_ABORT;
5478 ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
5479 memset(ac->lun_id, 0, sizeof(ac->lun_id));
5480 ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
5481 ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
5482 ac->error_ptr = cpu_to_le64(c->busaddr +
5483 offsetof(struct io_accel2_cmd, error_data));
5484 ac->error_len = cpu_to_le32(sizeof(c2->error_data));
5485}
5486
Scott Teel54b6e9e2014-02-18 13:56:45 -06005487/* ioaccel2 path firmware cannot handle abort task requests.
5488 * Change abort requests to physical target reset, and send to the
5489 * address of the physical disk used for the ioaccel 2 command.
5490 * Return 0 on success (IO_OK)
5491 * -1 on failure
5492 */
5493
5494static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05005495 unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
Scott Teel54b6e9e2014-02-18 13:56:45 -06005496{
5497 int rc = IO_OK;
5498 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
5499 struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
5500 unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
5501 unsigned char *psa = &phys_scsi3addr[0];
5502
5503 /* Get a pointer to the hpsa logical device. */
Stephen Cameron7fa30302015-01-23 16:44:30 -06005504 scmd = abort->scsi_cmd;
Scott Teel54b6e9e2014-02-18 13:56:45 -06005505 dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
5506 if (dev == NULL) {
5507 dev_warn(&h->pdev->dev,
5508 "Cannot abort: no device pointer for command.\n");
5509 return -1; /* not abortable */
5510 }
5511
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005512 if (h->raid_offload_debug > 0)
5513 dev_info(&h->pdev->dev,
Webb Scales0d96ef52015-04-23 09:31:55 -05005514 "scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005515 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
Webb Scales0d96ef52015-04-23 09:31:55 -05005516 "Reset as abort",
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005517 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
5518 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
5519
Scott Teel54b6e9e2014-02-18 13:56:45 -06005520 if (!dev->offload_enabled) {
5521 dev_warn(&h->pdev->dev,
5522 "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
5523 return -1; /* not abortable */
5524 }
5525
5526 /* Incoming scsi3addr is logical addr. We need physical disk addr. */
5527 if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
5528 dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
5529 return -1; /* not abortable */
5530 }
5531
5532 /* send the reset */
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06005533 if (h->raid_offload_debug > 0)
5534 dev_info(&h->pdev->dev,
5535 "Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5536 psa[0], psa[1], psa[2], psa[3],
5537 psa[4], psa[5], psa[6], psa[7]);
Webb Scalesd604f532015-04-23 09:35:22 -05005538 rc = hpsa_do_reset(h, dev, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
Scott Teel54b6e9e2014-02-18 13:56:45 -06005539 if (rc != 0) {
5540 dev_warn(&h->pdev->dev,
5541 "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5542 psa[0], psa[1], psa[2], psa[3],
5543 psa[4], psa[5], psa[6], psa[7]);
5544 return rc; /* failed to reset */
5545 }
5546
5547 /* wait for device to recover */
Webb Scalesb69324f2015-04-23 09:34:22 -05005548 if (wait_for_device_to_become_ready(h, psa, reply_queue) != 0) {
Scott Teel54b6e9e2014-02-18 13:56:45 -06005549 dev_warn(&h->pdev->dev,
5550 "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5551 psa[0], psa[1], psa[2], psa[3],
5552 psa[4], psa[5], psa[6], psa[7]);
5553 return -1; /* failed to recover */
5554 }
5555
5556 /* device recovered */
5557 dev_info(&h->pdev->dev,
5558 "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
5559 psa[0], psa[1], psa[2], psa[3],
5560 psa[4], psa[5], psa[6], psa[7]);
5561
5562 return rc; /* success */
5563}
5564
Stephen Cameron8be986c2015-04-23 09:34:06 -05005565static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
5566 struct CommandList *abort, int reply_queue)
5567{
5568 int rc = IO_OK;
5569 struct CommandList *c;
5570 __le32 taglower, tagupper;
5571 struct hpsa_scsi_dev_t *dev;
5572 struct io_accel2_cmd *c2;
5573
5574 dev = abort->scsi_cmd->device->hostdata;
5575 if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
5576 return -1;
5577
5578 c = cmd_alloc(h);
5579 setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
5580 c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5581 (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
5582 hpsa_get_tag(h, abort, &taglower, &tagupper);
5583 dev_dbg(&h->pdev->dev,
5584 "%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
5585 __func__, tagupper, taglower);
5586 /* no unmap needed here because no data xfer. */
5587
5588 dev_dbg(&h->pdev->dev,
5589 "%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
5590 __func__, tagupper, taglower, c2->error_data.serv_response);
5591 switch (c2->error_data.serv_response) {
5592 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
5593 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
5594 rc = 0;
5595 break;
5596 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
5597 case IOACCEL2_SERV_RESPONSE_FAILURE:
5598 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
5599 rc = -1;
5600 break;
5601 default:
5602 dev_warn(&h->pdev->dev,
5603 "%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
5604 __func__, tagupper, taglower,
5605 c2->error_data.serv_response);
5606 rc = -1;
5607 }
5608 cmd_free(h, c);
5609 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
5610 tagupper, taglower);
5611 return rc;
5612}
5613
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005614static int hpsa_send_abort_both_ways(struct ctlr_info *h,
Webb Scales25163bd2015-04-23 09:32:00 -05005615 unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005616{
Stephen Cameron8be986c2015-04-23 09:34:06 -05005617 /*
5618 * ioccelerator mode 2 commands should be aborted via the
Scott Teel54b6e9e2014-02-18 13:56:45 -06005619 * accelerated path, since RAID path is unaware of these commands,
Stephen Cameron8be986c2015-04-23 09:34:06 -05005620 * but not all underlying firmware can handle abort TMF.
5621 * Change abort to physical device reset when abort TMF is unsupported.
Scott Teel54b6e9e2014-02-18 13:56:45 -06005622 */
Stephen Cameron8be986c2015-04-23 09:34:06 -05005623 if (abort->cmd_type == CMD_IOACCEL2) {
5624 if (HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags)
5625 return hpsa_send_abort_ioaccel2(h, abort,
5626 reply_queue);
5627 else
5628 return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr,
Webb Scales25163bd2015-04-23 09:32:00 -05005629 abort, reply_queue);
Stephen Cameron8be986c2015-04-23 09:34:06 -05005630 }
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005631 return hpsa_send_abort(h, scsi3addr, abort, reply_queue);
Webb Scales25163bd2015-04-23 09:32:00 -05005632}
5633
5634/* Find out which reply queue a command was meant to return on */
5635static int hpsa_extract_reply_queue(struct ctlr_info *h,
5636 struct CommandList *c)
5637{
5638 if (c->cmd_type == CMD_IOACCEL2)
5639 return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
5640 return c->Header.ReplyQueue;
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05005641}
5642
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005643/*
5644 * Limit concurrency of abort commands to prevent
5645 * over-subscription of commands
5646 */
5647static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
5648{
5649#define ABORT_CMD_WAIT_MSECS 5000
5650 return !wait_event_timeout(h->abort_cmd_wait_queue,
5651 atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
5652 msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
5653}
5654
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005655/* Send an abort for the specified command.
5656 * If the device and controller support it,
5657 * send a task abort request.
5658 */
5659static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
5660{
5661
Webb Scalesa58e7e52015-04-23 09:34:16 -05005662 int rc;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005663 struct ctlr_info *h;
5664 struct hpsa_scsi_dev_t *dev;
5665 struct CommandList *abort; /* pointer to command to be aborted */
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005666 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
5667 char msg[256]; /* For debug messaging. */
5668 int ml = 0;
Don Brace2b08b3e2015-01-23 16:41:09 -06005669 __le32 tagupper, taglower;
Webb Scales25163bd2015-04-23 09:32:00 -05005670 int refcount, reply_queue;
5671
5672 if (sc == NULL)
5673 return FAILED;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005674
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005675 if (sc->device == NULL)
5676 return FAILED;
5677
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005678 /* Find the controller of the command to be aborted */
5679 h = sdev_to_hba(sc->device);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005680 if (h == NULL)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005681 return FAILED;
5682
Webb Scales25163bd2015-04-23 09:32:00 -05005683 /* Find the device of the command to be aborted */
5684 dev = sc->device->hostdata;
5685 if (!dev) {
5686 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
5687 msg);
Don Bracee3458932015-01-23 16:44:24 -06005688 return FAILED;
Webb Scales25163bd2015-04-23 09:32:00 -05005689 }
5690
5691 /* If controller locked up, we can guarantee command won't complete */
5692 if (lockup_detected(h)) {
5693 hpsa_show_dev_msg(KERN_WARNING, h, dev,
5694 "ABORT FAILED, lockup detected");
5695 return FAILED;
5696 }
5697
5698 /* This is a good time to check if controller lockup has occurred */
5699 if (detect_controller_lockup(h)) {
5700 hpsa_show_dev_msg(KERN_WARNING, h, dev,
5701 "ABORT FAILED, new lockup detected");
5702 return FAILED;
5703 }
Don Bracee3458932015-01-23 16:44:24 -06005704
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005705 /* Check that controller supports some kind of task abort */
5706 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
5707 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
5708 return FAILED;
5709
5710 memset(msg, 0, sizeof(msg));
Robert Elliott4b761552015-04-23 09:33:54 -05005711 ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005712 h->scsi_host->host_no, sc->device->channel,
Webb Scales0d96ef52015-04-23 09:31:55 -05005713 sc->device->id, sc->device->lun,
Robert Elliott4b761552015-04-23 09:33:54 -05005714 "Aborting command", sc);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005715
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005716 /* Get SCSI command to be aborted */
5717 abort = (struct CommandList *) sc->host_scribble;
5718 if (abort == NULL) {
Webb Scales281a7fd2015-01-23 16:43:35 -06005719 /* This can happen if the command already completed. */
5720 return SUCCESS;
5721 }
5722 refcount = atomic_inc_return(&abort->refcount);
5723 if (refcount == 1) { /* Command is done already. */
5724 cmd_free(h, abort);
5725 return SUCCESS;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005726 }
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005727
5728 /* Don't bother trying the abort if we know it won't work. */
5729 if (abort->cmd_type != CMD_IOACCEL2 &&
5730 abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
5731 cmd_free(h, abort);
5732 return FAILED;
5733 }
5734
Webb Scalesa58e7e52015-04-23 09:34:16 -05005735 /*
5736 * Check that we're aborting the right command.
5737 * It's possible the CommandList already completed and got re-used.
5738 */
5739 if (abort->scsi_cmd != sc) {
5740 cmd_free(h, abort);
5741 return SUCCESS;
5742 }
5743
5744 abort->abort_pending = true;
Scott Teel17eb87d2014-02-18 13:55:28 -06005745 hpsa_get_tag(h, abort, &taglower, &tagupper);
Webb Scales25163bd2015-04-23 09:32:00 -05005746 reply_queue = hpsa_extract_reply_queue(h, abort);
Scott Teel17eb87d2014-02-18 13:55:28 -06005747 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen Cameron7fa30302015-01-23 16:44:30 -06005748 as = abort->scsi_cmd;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005749 if (as != NULL)
Robert Elliott4b761552015-04-23 09:33:54 -05005750 ml += sprintf(msg+ml,
5751 "CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
5752 as->cmd_len, as->cmnd[0], as->cmnd[1],
5753 as->serial_number);
5754 dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
Webb Scales0d96ef52015-04-23 09:31:55 -05005755 hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
Robert Elliott4b761552015-04-23 09:33:54 -05005756
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005757 /*
5758 * Command is in flight, or possibly already completed
5759 * by the firmware (but not to the scsi mid layer) but we can't
5760 * distinguish which. Send the abort down.
5761 */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005762 if (wait_for_available_abort_cmd(h)) {
5763 dev_warn(&h->pdev->dev,
Robert Elliott4b761552015-04-23 09:33:54 -05005764 "%s FAILED, timeout waiting for an abort command to become available.\n",
5765 msg);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005766 cmd_free(h, abort);
5767 return FAILED;
5768 }
Webb Scales25163bd2015-04-23 09:32:00 -05005769 rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort, reply_queue);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05005770 atomic_inc(&h->abort_cmds_available);
5771 wake_up_all(&h->abort_cmd_wait_queue);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005772 if (rc != 0) {
Robert Elliott4b761552015-04-23 09:33:54 -05005773 dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
Webb Scales0d96ef52015-04-23 09:31:55 -05005774 hpsa_show_dev_msg(KERN_WARNING, h, dev,
Robert Elliott4b761552015-04-23 09:33:54 -05005775 "FAILED to abort command");
Webb Scales281a7fd2015-01-23 16:43:35 -06005776 cmd_free(h, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005777 return FAILED;
5778 }
Robert Elliott4b761552015-04-23 09:33:54 -05005779 dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
Webb Scalesd604f532015-04-23 09:35:22 -05005780 wait_event(h->event_sync_wait_queue,
Webb Scalesa58e7e52015-04-23 09:34:16 -05005781 abort->scsi_cmd != sc || lockup_detected(h));
Webb Scales281a7fd2015-01-23 16:43:35 -06005782 cmd_free(h, abort);
Webb Scalesa58e7e52015-04-23 09:34:16 -05005783 return !lockup_detected(h) ? SUCCESS : FAILED;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005784}
5785
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005786/*
Webb Scales73153fe2015-04-23 09:35:04 -05005787 * For operations with an associated SCSI command, a command block is allocated
5788 * at init, and managed by cmd_tagged_alloc() and cmd_tagged_free() using the
5789 * block request tag as an index into a table of entries. cmd_tagged_free() is
5790 * the complement, although cmd_free() may be called instead.
5791 */
5792static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
5793 struct scsi_cmnd *scmd)
5794{
5795 int idx = hpsa_get_cmd_index(scmd);
5796 struct CommandList *c = h->cmd_pool + idx;
5797
5798 if (idx < HPSA_NRESERVED_CMDS || idx >= h->nr_cmds) {
5799 dev_err(&h->pdev->dev, "Bad block tag: %d not in [%d..%d]\n",
5800 idx, HPSA_NRESERVED_CMDS, h->nr_cmds - 1);
5801 /* The index value comes from the block layer, so if it's out of
5802 * bounds, it's probably not our bug.
5803 */
5804 BUG();
5805 }
5806
5807 atomic_inc(&c->refcount);
5808 if (unlikely(!hpsa_is_cmd_idle(c))) {
5809 /*
5810 * We expect that the SCSI layer will hand us a unique tag
5811 * value. Thus, there should never be a collision here between
5812 * two requests...because if the selected command isn't idle
5813 * then someone is going to be very disappointed.
5814 */
5815 dev_err(&h->pdev->dev,
5816 "tag collision (tag=%d) in cmd_tagged_alloc().\n",
5817 idx);
5818 if (c->scsi_cmd != NULL)
5819 scsi_print_command(c->scsi_cmd);
5820 scsi_print_command(scmd);
5821 }
5822
5823 hpsa_cmd_partial_init(h, idx, c);
5824 return c;
5825}
5826
5827static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c)
5828{
5829 /*
5830 * Release our reference to the block. We don't need to do anything
5831 * else to free it, because it is accessed by index. (There's no point
5832 * in checking the result of the decrement, since we cannot guarantee
5833 * that there isn't a concurrent abort which is also accessing it.)
5834 */
5835 (void)atomic_dec(&c->refcount);
5836}
5837
5838/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005839 * For operations that cannot sleep, a command block is allocated at init,
5840 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
5841 * which ones are free or in use. Lock must be held when calling this.
5842 * cmd_free() is the complement.
Robert Elliottbf43caf2015-04-23 09:33:38 -05005843 * This function never gives up and returns NULL. If it hangs,
5844 * another thread must call cmd_free() to free some tags.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005845 */
Webb Scales281a7fd2015-01-23 16:43:35 -06005846
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005847static struct CommandList *cmd_alloc(struct ctlr_info *h)
5848{
5849 struct CommandList *c;
Stephen Cameron360c73b2015-04-23 09:32:32 -05005850 int refcount, i;
Webb Scales73153fe2015-04-23 09:35:04 -05005851 int offset = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005852
Robert Elliott33811022015-01-23 16:43:41 -06005853 /*
5854 * There is some *extremely* small but non-zero chance that that
Stephen M. Cameron4c413122014-11-14 17:27:29 -06005855 * multiple threads could get in here, and one thread could
5856 * be scanning through the list of bits looking for a free
5857 * one, but the free ones are always behind him, and other
5858 * threads sneak in behind him and eat them before he can
5859 * get to them, so that while there is always a free one, a
5860 * very unlucky thread might be starved anyway, never able to
5861 * beat the other threads. In reality, this happens so
5862 * infrequently as to be indistinguishable from never.
Webb Scales73153fe2015-04-23 09:35:04 -05005863 *
5864 * Note that we start allocating commands before the SCSI host structure
5865 * is initialized. Since the search starts at bit zero, this
5866 * all works, since we have at least one command structure available;
5867 * however, it means that the structures with the low indexes have to be
5868 * reserved for driver-initiated requests, while requests from the block
5869 * layer will use the higher indexes.
Stephen M. Cameron4c413122014-11-14 17:27:29 -06005870 */
5871
Webb Scales281a7fd2015-01-23 16:43:35 -06005872 for (;;) {
Webb Scales73153fe2015-04-23 09:35:04 -05005873 i = find_next_zero_bit(h->cmd_pool_bits,
5874 HPSA_NRESERVED_CMDS,
5875 offset);
5876 if (unlikely(i >= HPSA_NRESERVED_CMDS)) {
Webb Scales281a7fd2015-01-23 16:43:35 -06005877 offset = 0;
5878 continue;
5879 }
5880 c = h->cmd_pool + i;
5881 refcount = atomic_inc_return(&c->refcount);
5882 if (unlikely(refcount > 1)) {
5883 cmd_free(h, c); /* already in use */
Webb Scales73153fe2015-04-23 09:35:04 -05005884 offset = (i + 1) % HPSA_NRESERVED_CMDS;
Webb Scales281a7fd2015-01-23 16:43:35 -06005885 continue;
5886 }
5887 set_bit(i & (BITS_PER_LONG - 1),
5888 h->cmd_pool_bits + (i / BITS_PER_LONG));
5889 break; /* it's ours now. */
5890 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05005891 hpsa_cmd_partial_init(h, i, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005892 return c;
5893}
5894
Webb Scales73153fe2015-04-23 09:35:04 -05005895/*
5896 * This is the complementary operation to cmd_alloc(). Note, however, in some
5897 * corner cases it may also be used to free blocks allocated by
5898 * cmd_tagged_alloc() in which case the ref-count decrement does the trick and
5899 * the clear-bit is harmless.
5900 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005901static void cmd_free(struct ctlr_info *h, struct CommandList *c)
5902{
Webb Scales281a7fd2015-01-23 16:43:35 -06005903 if (atomic_dec_and_test(&c->refcount)) {
5904 int i;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005905
Webb Scales281a7fd2015-01-23 16:43:35 -06005906 i = c - h->cmd_pool;
5907 clear_bit(i & (BITS_PER_LONG - 1),
5908 h->cmd_pool_bits + (i / BITS_PER_LONG));
5909 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005910}
5911
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005912#ifdef CONFIG_COMPAT
5913
Don Brace42a91642014-11-14 17:26:27 -06005914static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
5915 void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005916{
5917 IOCTL32_Command_struct __user *arg32 =
5918 (IOCTL32_Command_struct __user *) arg;
5919 IOCTL_Command_struct arg64;
5920 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
5921 int err;
5922 u32 cp;
5923
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06005924 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005925 err = 0;
5926 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5927 sizeof(arg64.LUN_info));
5928 err |= copy_from_user(&arg64.Request, &arg32->Request,
5929 sizeof(arg64.Request));
5930 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5931 sizeof(arg64.error_info));
5932 err |= get_user(arg64.buf_size, &arg32->buf_size);
5933 err |= get_user(cp, &arg32->buf);
5934 arg64.buf = compat_ptr(cp);
5935 err |= copy_to_user(p, &arg64, sizeof(arg64));
5936
5937 if (err)
5938 return -EFAULT;
5939
Don Brace42a91642014-11-14 17:26:27 -06005940 err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005941 if (err)
5942 return err;
5943 err |= copy_in_user(&arg32->error_info, &p->error_info,
5944 sizeof(arg32->error_info));
5945 if (err)
5946 return -EFAULT;
5947 return err;
5948}
5949
5950static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
Don Brace42a91642014-11-14 17:26:27 -06005951 int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005952{
5953 BIG_IOCTL32_Command_struct __user *arg32 =
5954 (BIG_IOCTL32_Command_struct __user *) arg;
5955 BIG_IOCTL_Command_struct arg64;
5956 BIG_IOCTL_Command_struct __user *p =
5957 compat_alloc_user_space(sizeof(arg64));
5958 int err;
5959 u32 cp;
5960
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06005961 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005962 err = 0;
5963 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5964 sizeof(arg64.LUN_info));
5965 err |= copy_from_user(&arg64.Request, &arg32->Request,
5966 sizeof(arg64.Request));
5967 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5968 sizeof(arg64.error_info));
5969 err |= get_user(arg64.buf_size, &arg32->buf_size);
5970 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
5971 err |= get_user(cp, &arg32->buf);
5972 arg64.buf = compat_ptr(cp);
5973 err |= copy_to_user(p, &arg64, sizeof(arg64));
5974
5975 if (err)
5976 return -EFAULT;
5977
Don Brace42a91642014-11-14 17:26:27 -06005978 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005979 if (err)
5980 return err;
5981 err |= copy_in_user(&arg32->error_info, &p->error_info,
5982 sizeof(arg32->error_info));
5983 if (err)
5984 return -EFAULT;
5985 return err;
5986}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06005987
Don Brace42a91642014-11-14 17:26:27 -06005988static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06005989{
5990 switch (cmd) {
5991 case CCISS_GETPCIINFO:
5992 case CCISS_GETINTINFO:
5993 case CCISS_SETINTINFO:
5994 case CCISS_GETNODENAME:
5995 case CCISS_SETNODENAME:
5996 case CCISS_GETHEARTBEAT:
5997 case CCISS_GETBUSTYPES:
5998 case CCISS_GETFIRMVER:
5999 case CCISS_GETDRIVVER:
6000 case CCISS_REVALIDVOLS:
6001 case CCISS_DEREGDISK:
6002 case CCISS_REGNEWDISK:
6003 case CCISS_REGNEWD:
6004 case CCISS_RESCANDISK:
6005 case CCISS_GETLUNINFO:
6006 return hpsa_ioctl(dev, cmd, arg);
6007
6008 case CCISS_PASSTHRU32:
6009 return hpsa_ioctl32_passthru(dev, cmd, arg);
6010 case CCISS_BIG_PASSTHRU32:
6011 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
6012
6013 default:
6014 return -ENOIOCTLCMD;
6015 }
6016}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006017#endif
6018
6019static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
6020{
6021 struct hpsa_pci_info pciinfo;
6022
6023 if (!argp)
6024 return -EINVAL;
6025 pciinfo.domain = pci_domain_nr(h->pdev->bus);
6026 pciinfo.bus = h->pdev->bus->number;
6027 pciinfo.dev_fn = h->pdev->devfn;
6028 pciinfo.board_id = h->board_id;
6029 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
6030 return -EFAULT;
6031 return 0;
6032}
6033
6034static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
6035{
6036 DriverVer_type DriverVer;
6037 unsigned char vmaj, vmin, vsubmin;
6038 int rc;
6039
6040 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
6041 &vmaj, &vmin, &vsubmin);
6042 if (rc != 3) {
6043 dev_info(&h->pdev->dev, "driver version string '%s' "
6044 "unrecognized.", HPSA_DRIVER_VERSION);
6045 vmaj = 0;
6046 vmin = 0;
6047 vsubmin = 0;
6048 }
6049 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
6050 if (!argp)
6051 return -EINVAL;
6052 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
6053 return -EFAULT;
6054 return 0;
6055}
6056
6057static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6058{
6059 IOCTL_Command_struct iocommand;
6060 struct CommandList *c;
6061 char *buff = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006062 u64 temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006063 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006064
6065 if (!argp)
6066 return -EINVAL;
6067 if (!capable(CAP_SYS_RAWIO))
6068 return -EPERM;
6069 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
6070 return -EFAULT;
6071 if ((iocommand.buf_size < 1) &&
6072 (iocommand.Request.Type.Direction != XFER_NONE)) {
6073 return -EINVAL;
6074 }
6075 if (iocommand.buf_size > 0) {
6076 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
6077 if (buff == NULL)
Robert Elliott2dd02d72015-04-23 09:33:43 -05006078 return -ENOMEM;
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006079 if (iocommand.Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006080 /* Copy the data into the buffer we created */
6081 if (copy_from_user(buff, iocommand.buf,
6082 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006083 rc = -EFAULT;
6084 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006085 }
6086 } else {
6087 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006088 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006089 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006090 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006091
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006092 /* Fill in the command type */
6093 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006094 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006095 /* Fill in Command Header */
6096 c->Header.ReplyQueue = 0; /* unused in simple mode */
6097 if (iocommand.buf_size > 0) { /* buffer to fill */
6098 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006099 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006100 } else { /* no buffers to fill */
6101 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006102 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006103 }
6104 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006105
6106 /* Fill in Request block */
6107 memcpy(&c->Request, &iocommand.Request,
6108 sizeof(c->Request));
6109
6110 /* Fill in the scatter gather information */
6111 if (iocommand.buf_size > 0) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006112 temp64 = pci_map_single(h->pdev, buff,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006113 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006114 if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
6115 c->SG[0].Addr = cpu_to_le64(0);
6116 c->SG[0].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006117 rc = -ENOMEM;
6118 goto out;
6119 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006120 c->SG[0].Addr = cpu_to_le64(temp64);
6121 c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
6122 c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006123 }
Webb Scales25163bd2015-04-23 09:32:00 -05006124 rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05006125 if (iocommand.buf_size > 0)
6126 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006127 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006128 if (rc) {
6129 rc = -EIO;
6130 goto out;
6131 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006132
6133 /* Copy the error information out */
6134 memcpy(&iocommand.error_info, c->err_info,
6135 sizeof(iocommand.error_info));
6136 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006137 rc = -EFAULT;
6138 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006139 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006140 if ((iocommand.Request.Type.Direction & XFER_READ) &&
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006141 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006142 /* Copy the data out of the buffer we created */
6143 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006144 rc = -EFAULT;
6145 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006146 }
6147 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006148out:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006149 cmd_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06006150out_kfree:
6151 kfree(buff);
6152 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006153}
6154
6155static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6156{
6157 BIG_IOCTL_Command_struct *ioc;
6158 struct CommandList *c;
6159 unsigned char **buff = NULL;
6160 int *buff_size = NULL;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006161 u64 temp64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006162 BYTE sg_used = 0;
6163 int status = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06006164 u32 left;
6165 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006166 BYTE __user *data_ptr;
6167
6168 if (!argp)
6169 return -EINVAL;
6170 if (!capable(CAP_SYS_RAWIO))
6171 return -EPERM;
6172 ioc = (BIG_IOCTL_Command_struct *)
6173 kmalloc(sizeof(*ioc), GFP_KERNEL);
6174 if (!ioc) {
6175 status = -ENOMEM;
6176 goto cleanup1;
6177 }
6178 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
6179 status = -EFAULT;
6180 goto cleanup1;
6181 }
6182 if ((ioc->buf_size < 1) &&
6183 (ioc->Request.Type.Direction != XFER_NONE)) {
6184 status = -EINVAL;
6185 goto cleanup1;
6186 }
6187 /* Check kmalloc limits using all SGs */
6188 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
6189 status = -EINVAL;
6190 goto cleanup1;
6191 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006192 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006193 status = -EINVAL;
6194 goto cleanup1;
6195 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006196 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006197 if (!buff) {
6198 status = -ENOMEM;
6199 goto cleanup1;
6200 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006201 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006202 if (!buff_size) {
6203 status = -ENOMEM;
6204 goto cleanup1;
6205 }
6206 left = ioc->buf_size;
6207 data_ptr = ioc->buf;
6208 while (left) {
6209 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
6210 buff_size[sg_used] = sz;
6211 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
6212 if (buff[sg_used] == NULL) {
6213 status = -ENOMEM;
6214 goto cleanup1;
6215 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006216 if (ioc->Request.Type.Direction & XFER_WRITE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006217 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
Stephen M. Cameron0758f4f2014-07-03 10:18:03 -05006218 status = -EFAULT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006219 goto cleanup1;
6220 }
6221 } else
6222 memset(buff[sg_used], 0, sz);
6223 left -= sz;
6224 data_ptr += sz;
6225 sg_used++;
6226 }
Stephen Cameron45fcb862015-01-23 16:43:04 -06006227 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006228
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006229 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006230 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006231 c->Header.ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006232 c->Header.SGList = (u8) sg_used;
6233 c->Header.SGTotal = cpu_to_le16(sg_used);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006234 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006235 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
6236 if (ioc->buf_size > 0) {
6237 int i;
6238 for (i = 0; i < sg_used; i++) {
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006239 temp64 = pci_map_single(h->pdev, buff[i],
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006240 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006241 if (dma_mapping_error(&h->pdev->dev,
6242 (dma_addr_t) temp64)) {
6243 c->SG[i].Addr = cpu_to_le64(0);
6244 c->SG[i].Len = cpu_to_le32(0);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006245 hpsa_pci_unmap(h->pdev, c, i,
6246 PCI_DMA_BIDIRECTIONAL);
6247 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006248 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06006249 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006250 c->SG[i].Addr = cpu_to_le64(temp64);
6251 c->SG[i].Len = cpu_to_le32(buff_size[i]);
6252 c->SG[i].Ext = cpu_to_le32(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006253 }
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006254 c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006255 }
Webb Scales25163bd2015-04-23 09:32:00 -05006256 status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06006257 if (sg_used)
6258 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006259 check_ioctl_unit_attention(h, c);
Webb Scales25163bd2015-04-23 09:32:00 -05006260 if (status) {
6261 status = -EIO;
6262 goto cleanup0;
6263 }
6264
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006265 /* Copy the error information out */
6266 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
6267 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006268 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006269 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006270 }
Stephen M. Cameron9233fb12014-05-29 10:52:41 -05006271 if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006272 int i;
6273
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006274 /* Copy the data out of the buffer we created */
6275 BYTE __user *ptr = ioc->buf;
6276 for (i = 0; i < sg_used; i++) {
6277 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006278 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006279 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006280 }
6281 ptr += buff_size[i];
6282 }
6283 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006284 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05006285cleanup0:
Stephen Cameron45fcb862015-01-23 16:43:04 -06006286 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006287cleanup1:
6288 if (buff) {
Don Brace2b08b3e2015-01-23 16:41:09 -06006289 int i;
6290
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006291 for (i = 0; i < sg_used; i++)
6292 kfree(buff[i]);
6293 kfree(buff);
6294 }
6295 kfree(buff_size);
6296 kfree(ioc);
6297 return status;
6298}
6299
6300static void check_ioctl_unit_attention(struct ctlr_info *h,
6301 struct CommandList *c)
6302{
6303 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
6304 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
6305 (void) check_for_unit_attention(h, c);
6306}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006307
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006308/*
6309 * ioctl
6310 */
Don Brace42a91642014-11-14 17:26:27 -06006311static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006312{
6313 struct ctlr_info *h;
6314 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006315 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006316
6317 h = sdev_to_hba(dev);
6318
6319 switch (cmd) {
6320 case CCISS_DEREGDISK:
6321 case CCISS_REGNEWDISK:
6322 case CCISS_REGNEWD:
Stephen M. Camerona08a8472010-02-04 08:43:16 -06006323 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006324 return 0;
6325 case CCISS_GETPCIINFO:
6326 return hpsa_getpciinfo_ioctl(h, argp);
6327 case CCISS_GETDRIVVER:
6328 return hpsa_getdrivver_ioctl(h, argp);
6329 case CCISS_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006330 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006331 return -EAGAIN;
6332 rc = hpsa_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006333 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006334 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006335 case CCISS_BIG_PASSTHRU:
Don Brace34f0c622015-01-23 16:43:46 -06006336 if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006337 return -EAGAIN;
6338 rc = hpsa_big_passthru_ioctl(h, argp);
Don Brace34f0c622015-01-23 16:43:46 -06006339 atomic_inc(&h->passthru_cmds_avail);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006340 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006341 default:
6342 return -ENOTTY;
6343 }
6344}
6345
Robert Elliottbf43caf2015-04-23 09:33:38 -05006346static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006347 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006348{
6349 struct CommandList *c;
6350
6351 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05006352
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006353 /* fill_cmd can't fail here, no data buffer to map */
6354 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006355 RAID_CTLR_LUNID, TYPE_MSG);
6356 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
6357 c->waiting = NULL;
6358 enqueue_cmd_and_start_io(h, c);
6359 /* Don't wait for completion, the reset won't complete. Don't free
6360 * the command either. This is the last command we will send before
6361 * re-initializing everything, so it doesn't matter and won't leak.
6362 */
Robert Elliottbf43caf2015-04-23 09:33:38 -05006363 return;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006364}
6365
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006366static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006367 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006368 int cmd_type)
6369{
6370 int pci_dir = XFER_NONE;
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006371 u64 tag; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006372
6373 c->cmd_type = CMD_IOCTL_PEND;
Webb Scalesa58e7e52015-04-23 09:34:16 -05006374 c->scsi_cmd = SCSI_CMD_BUSY;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006375 c->Header.ReplyQueue = 0;
6376 if (buff != NULL && size > 0) {
6377 c->Header.SGList = 1;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006378 c->Header.SGTotal = cpu_to_le16(1);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006379 } else {
6380 c->Header.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006381 c->Header.SGTotal = cpu_to_le16(0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006382 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006383 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
6384
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006385 if (cmd_type == TYPE_CMD) {
6386 switch (cmd) {
6387 case HPSA_INQUIRY:
6388 /* are we trying to read a vital product page */
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006389 if (page_code & VPD_PAGE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006390 c->Request.CDB[1] = 0x01;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06006391 c->Request.CDB[2] = (page_code & 0xff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006392 }
6393 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006394 c->Request.type_attr_dir =
6395 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006396 c->Request.Timeout = 0;
6397 c->Request.CDB[0] = HPSA_INQUIRY;
6398 c->Request.CDB[4] = size & 0xFF;
6399 break;
6400 case HPSA_REPORT_LOG:
6401 case HPSA_REPORT_PHYS:
6402 /* Talking to controller so It's a physical command
6403 mode = 00 target = 0. Nothing to write.
6404 */
6405 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006406 c->Request.type_attr_dir =
6407 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006408 c->Request.Timeout = 0;
6409 c->Request.CDB[0] = cmd;
6410 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6411 c->Request.CDB[7] = (size >> 16) & 0xFF;
6412 c->Request.CDB[8] = (size >> 8) & 0xFF;
6413 c->Request.CDB[9] = size & 0xFF;
6414 break;
Scott Teelc2adae42015-11-04 15:52:16 -06006415 case BMIC_SENSE_DIAG_OPTIONS:
6416 c->Request.CDBLen = 16;
6417 c->Request.type_attr_dir =
6418 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6419 c->Request.Timeout = 0;
6420 /* Spec says this should be BMIC_WRITE */
6421 c->Request.CDB[0] = BMIC_READ;
6422 c->Request.CDB[6] = BMIC_SENSE_DIAG_OPTIONS;
6423 break;
6424 case BMIC_SET_DIAG_OPTIONS:
6425 c->Request.CDBLen = 16;
6426 c->Request.type_attr_dir =
6427 TYPE_ATTR_DIR(cmd_type,
6428 ATTR_SIMPLE, XFER_WRITE);
6429 c->Request.Timeout = 0;
6430 c->Request.CDB[0] = BMIC_WRITE;
6431 c->Request.CDB[6] = BMIC_SET_DIAG_OPTIONS;
6432 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006433 case HPSA_CACHE_FLUSH:
6434 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006435 c->Request.type_attr_dir =
6436 TYPE_ATTR_DIR(cmd_type,
6437 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006438 c->Request.Timeout = 0;
6439 c->Request.CDB[0] = BMIC_WRITE;
6440 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05006441 c->Request.CDB[7] = (size >> 8) & 0xFF;
6442 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006443 break;
6444 case TEST_UNIT_READY:
6445 c->Request.CDBLen = 6;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006446 c->Request.type_attr_dir =
6447 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006448 c->Request.Timeout = 0;
6449 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006450 case HPSA_GET_RAID_MAP:
6451 c->Request.CDBLen = 12;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006452 c->Request.type_attr_dir =
6453 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006454 c->Request.Timeout = 0;
6455 c->Request.CDB[0] = HPSA_CISS_READ;
6456 c->Request.CDB[1] = cmd;
6457 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6458 c->Request.CDB[7] = (size >> 16) & 0xFF;
6459 c->Request.CDB[8] = (size >> 8) & 0xFF;
6460 c->Request.CDB[9] = size & 0xFF;
6461 break;
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006462 case BMIC_SENSE_CONTROLLER_PARAMETERS:
6463 c->Request.CDBLen = 10;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006464 c->Request.type_attr_dir =
6465 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
Stephen M. Cameron316b2212014-02-21 16:25:15 -06006466 c->Request.Timeout = 0;
6467 c->Request.CDB[0] = BMIC_READ;
6468 c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
6469 c->Request.CDB[7] = (size >> 16) & 0xFF;
6470 c->Request.CDB[8] = (size >> 8) & 0xFF;
6471 break;
Don Brace03383732015-01-23 16:43:30 -06006472 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
6473 c->Request.CDBLen = 10;
6474 c->Request.type_attr_dir =
6475 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6476 c->Request.Timeout = 0;
6477 c->Request.CDB[0] = BMIC_READ;
6478 c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
6479 c->Request.CDB[7] = (size >> 16) & 0xFF;
6480 c->Request.CDB[8] = (size >> 8) & 0XFF;
6481 break;
Scott Teel66749d02015-11-04 15:51:57 -06006482 case BMIC_IDENTIFY_CONTROLLER:
6483 c->Request.CDBLen = 10;
6484 c->Request.type_attr_dir =
6485 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6486 c->Request.Timeout = 0;
6487 c->Request.CDB[0] = BMIC_READ;
6488 c->Request.CDB[1] = 0;
6489 c->Request.CDB[2] = 0;
6490 c->Request.CDB[3] = 0;
6491 c->Request.CDB[4] = 0;
6492 c->Request.CDB[5] = 0;
6493 c->Request.CDB[6] = BMIC_IDENTIFY_CONTROLLER;
6494 c->Request.CDB[7] = (size >> 16) & 0xFF;
6495 c->Request.CDB[8] = (size >> 8) & 0XFF;
6496 c->Request.CDB[9] = 0;
6497 break;
6498
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006499 default:
6500 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
6501 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006502 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006503 }
6504 } else if (cmd_type == TYPE_MSG) {
6505 switch (cmd) {
6506
Scott Teel0b9b7b62015-11-04 15:51:02 -06006507 case HPSA_PHYS_TARGET_RESET:
6508 c->Request.CDBLen = 16;
6509 c->Request.type_attr_dir =
6510 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6511 c->Request.Timeout = 0; /* Don't time out */
6512 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
6513 c->Request.CDB[0] = HPSA_RESET;
6514 c->Request.CDB[1] = HPSA_TARGET_RESET_TYPE;
6515 /* Physical target reset needs no control bytes 4-7*/
6516 c->Request.CDB[4] = 0x00;
6517 c->Request.CDB[5] = 0x00;
6518 c->Request.CDB[6] = 0x00;
6519 c->Request.CDB[7] = 0x00;
6520 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006521 case HPSA_DEVICE_RESET_MSG:
6522 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006523 c->Request.type_attr_dir =
6524 TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006525 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006526 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
6527 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05006528 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006529 /* If bytes 4-7 are zero, it means reset the */
6530 /* LunID device */
6531 c->Request.CDB[4] = 0x00;
6532 c->Request.CDB[5] = 0x00;
6533 c->Request.CDB[6] = 0x00;
6534 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006535 break;
6536 case HPSA_ABORT_MSG:
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006537 memcpy(&tag, buff, sizeof(tag));
Don Brace2b08b3e2015-01-23 16:41:09 -06006538 dev_dbg(&h->pdev->dev,
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006539 "Abort Tag:0x%016llx using rqst Tag:0x%016llx",
6540 tag, c->Header.tag);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006541 c->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006542 c->Request.type_attr_dir =
6543 TYPE_ATTR_DIR(cmd_type,
6544 ATTR_SIMPLE, XFER_WRITE);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006545 c->Request.Timeout = 0; /* Don't time out */
6546 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
6547 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
6548 c->Request.CDB[2] = 0x00; /* reserved */
6549 c->Request.CDB[3] = 0x00; /* reserved */
6550 /* Tag to abort goes in CDB[4]-CDB[11] */
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05006551 memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006552 c->Request.CDB[12] = 0x00; /* reserved */
6553 c->Request.CDB[13] = 0x00; /* reserved */
6554 c->Request.CDB[14] = 0x00; /* reserved */
6555 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006556 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006557 default:
6558 dev_warn(&h->pdev->dev, "unknown message type %d\n",
6559 cmd);
6560 BUG();
6561 }
6562 } else {
6563 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
6564 BUG();
6565 }
6566
Stephen M. Camerona505b862014-11-14 17:27:04 -06006567 switch (GET_DIR(c->Request.type_attr_dir)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006568 case XFER_READ:
6569 pci_dir = PCI_DMA_FROMDEVICE;
6570 break;
6571 case XFER_WRITE:
6572 pci_dir = PCI_DMA_TODEVICE;
6573 break;
6574 case XFER_NONE:
6575 pci_dir = PCI_DMA_NONE;
6576 break;
6577 default:
6578 pci_dir = PCI_DMA_BIDIRECTIONAL;
6579 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006580 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
6581 return -1;
6582 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006583}
6584
6585/*
6586 * Map (physical) PCI mem into (virtual) kernel space
6587 */
6588static void __iomem *remap_pci_mem(ulong base, ulong size)
6589{
6590 ulong page_base = ((ulong) base) & PAGE_MASK;
6591 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba34c2012-07-26 11:34:23 -05006592 void __iomem *page_remapped = ioremap_nocache(page_base,
6593 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006594
6595 return page_remapped ? (page_remapped + page_offs) : NULL;
6596}
6597
Matt Gates254f7962012-05-01 11:43:06 -05006598static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006599{
Matt Gates254f7962012-05-01 11:43:06 -05006600 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006601}
6602
Stephen M. Cameron900c5442010-02-04 08:42:35 -06006603static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006604{
6605 return h->access.intr_pending(h);
6606}
6607
6608static inline long interrupt_not_for_us(struct ctlr_info *h)
6609{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006610 return (h->access.intr_pending(h) == 0) ||
6611 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006612}
6613
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06006614static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
6615 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006616{
6617 if (unlikely(tag_index >= h->nr_cmds)) {
6618 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
6619 return 1;
6620 }
6621 return 0;
6622}
6623
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05006624static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006625{
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006626 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06006627 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
6628 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05006629 complete_scsi_command(c);
Stephen Cameron8be986c2015-04-23 09:34:06 -05006630 else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006631 complete(c->waiting);
Stephen M. Camerona104c992010-02-04 08:42:24 -06006632}
6633
Don Brace303932f2010-02-04 08:42:40 -06006634/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05006635static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06006636 u32 raw_tag)
6637{
6638 u32 tag_index;
6639 struct CommandList *c;
6640
Don Bracef2405db2015-01-23 16:43:09 -06006641 tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05006642 if (!bad_tag(h, tag_index, raw_tag)) {
6643 c = h->cmd_pool + tag_index;
6644 finish_cmd(c);
6645 }
Don Brace303932f2010-02-04 08:42:40 -06006646}
6647
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006648/* Some controllers, like p400, will give us one interrupt
6649 * after a soft reset, even if we turned interrupts off.
6650 * Only need to check for this in the hpsa_xxx_discard_completions
6651 * functions.
6652 */
6653static int ignore_bogus_interrupt(struct ctlr_info *h)
6654{
6655 if (likely(!reset_devices))
6656 return 0;
6657
6658 if (likely(h->interrupts_enabled))
6659 return 0;
6660
6661 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
6662 "(known firmware bug.) Ignoring.\n");
6663
6664 return 1;
6665}
6666
Matt Gates254f7962012-05-01 11:43:06 -05006667/*
6668 * Convert &h->q[x] (passed to interrupt handlers) back to h.
6669 * Relies on (h-q[x] == x) being true for x such that
6670 * 0 <= x < MAX_REPLY_QUEUES.
6671 */
6672static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006673{
Matt Gates254f7962012-05-01 11:43:06 -05006674 return container_of((queue - *queue), struct ctlr_info, q[0]);
6675}
6676
6677static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
6678{
6679 struct ctlr_info *h = queue_to_hba(queue);
6680 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006681 u32 raw_tag;
6682
6683 if (ignore_bogus_interrupt(h))
6684 return IRQ_NONE;
6685
6686 if (interrupt_not_for_us(h))
6687 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006688 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006689 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05006690 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006691 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05006692 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006693 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006694 return IRQ_HANDLED;
6695}
6696
Matt Gates254f7962012-05-01 11:43:06 -05006697static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006698{
Matt Gates254f7962012-05-01 11:43:06 -05006699 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006700 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05006701 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006702
6703 if (ignore_bogus_interrupt(h))
6704 return IRQ_NONE;
6705
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006706 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05006707 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006708 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05006709 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006710 return IRQ_HANDLED;
6711}
6712
Matt Gates254f7962012-05-01 11:43:06 -05006713static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006714{
Matt Gates254f7962012-05-01 11:43:06 -05006715 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06006716 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05006717 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006718
6719 if (interrupt_not_for_us(h))
6720 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006721 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006722 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05006723 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006724 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06006725 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05006726 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006727 }
6728 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006729 return IRQ_HANDLED;
6730}
6731
Matt Gates254f7962012-05-01 11:43:06 -05006732static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006733{
Matt Gates254f7962012-05-01 11:43:06 -05006734 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006735 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05006736 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006737
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006738 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05006739 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06006740 while (raw_tag != FIFO_EMPTY) {
Don Bracef2405db2015-01-23 16:43:09 -06006741 process_indexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05006742 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006743 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006744 return IRQ_HANDLED;
6745}
6746
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006747/* Send a message CDB to the firmware. Careful, this only works
6748 * in simple mode, not performant mode due to the tag lookup.
6749 * We only ever use this immediately after a controller reset.
6750 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006751static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
6752 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006753{
6754 struct Command {
6755 struct CommandListHeader CommandHeader;
6756 struct RequestBlock Request;
6757 struct ErrDescriptor ErrorDescriptor;
6758 };
6759 struct Command *cmd;
6760 static const size_t cmd_sz = sizeof(*cmd) +
6761 sizeof(cmd->ErrorDescriptor);
6762 dma_addr_t paddr64;
Don Brace2b08b3e2015-01-23 16:41:09 -06006763 __le32 paddr32;
6764 u32 tag;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006765 void __iomem *vaddr;
6766 int i, err;
6767
6768 vaddr = pci_ioremap_bar(pdev, 0);
6769 if (vaddr == NULL)
6770 return -ENOMEM;
6771
6772 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
6773 * CCISS commands, so they must be allocated from the lower 4GiB of
6774 * memory.
6775 */
6776 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
6777 if (err) {
6778 iounmap(vaddr);
Robert Elliott1eaec8f2015-01-23 16:42:37 -06006779 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006780 }
6781
6782 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
6783 if (cmd == NULL) {
6784 iounmap(vaddr);
6785 return -ENOMEM;
6786 }
6787
6788 /* This must fit, because of the 32-bit consistent DMA mask. Also,
6789 * although there's no guarantee, we assume that the address is at
6790 * least 4-byte aligned (most likely, it's page-aligned).
6791 */
Don Brace2b08b3e2015-01-23 16:41:09 -06006792 paddr32 = cpu_to_le32(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006793
6794 cmd->CommandHeader.ReplyQueue = 0;
6795 cmd->CommandHeader.SGList = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006796 cmd->CommandHeader.SGTotal = cpu_to_le16(0);
Don Brace2b08b3e2015-01-23 16:41:09 -06006797 cmd->CommandHeader.tag = cpu_to_le64(paddr64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006798 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
6799
6800 cmd->Request.CDBLen = 16;
Stephen M. Camerona505b862014-11-14 17:27:04 -06006801 cmd->Request.type_attr_dir =
6802 TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006803 cmd->Request.Timeout = 0; /* Don't time out */
6804 cmd->Request.CDB[0] = opcode;
6805 cmd->Request.CDB[1] = type;
6806 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006807 cmd->ErrorDescriptor.Addr =
Don Brace2b08b3e2015-01-23 16:41:09 -06006808 cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06006809 cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006810
Don Brace2b08b3e2015-01-23 16:41:09 -06006811 writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006812
6813 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
6814 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Don Brace2b08b3e2015-01-23 16:41:09 -06006815 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006816 break;
6817 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
6818 }
6819
6820 iounmap(vaddr);
6821
6822 /* we leak the DMA buffer here ... no choice since the controller could
6823 * still complete the command.
6824 */
6825 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
6826 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
6827 opcode, type);
6828 return -ETIMEDOUT;
6829 }
6830
6831 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
6832
6833 if (tag & HPSA_ERROR_BIT) {
6834 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
6835 opcode, type);
6836 return -EIO;
6837 }
6838
6839 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
6840 opcode, type);
6841 return 0;
6842}
6843
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006844#define hpsa_noop(p) hpsa_message(p, 3, 0)
6845
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006846static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Don Brace42a91642014-11-14 17:26:27 -06006847 void __iomem *vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006848{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006849
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006850 if (use_doorbell) {
6851 /* For everything after the P600, the PCI power state method
6852 * of resetting the controller doesn't work, so we have this
6853 * other way using the doorbell register.
6854 */
6855 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05006856 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05006857
Justin Lindley00701a92014-05-29 10:52:47 -05006858 /* PMC hardware guys tell us we need a 10 second delay after
Stephen M. Cameron85009232013-09-23 13:33:36 -05006859 * doorbell reset and before any attempt to talk to the board
6860 * at all to ensure that this actually works and doesn't fall
6861 * over in some weird corner cases.
6862 */
Justin Lindley00701a92014-05-29 10:52:47 -05006863 msleep(10000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006864 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006865
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006866 /* Quoting from the Open CISS Specification: "The Power
6867 * Management Control/Status Register (CSR) controls the power
6868 * state of the device. The normal operating state is D0,
6869 * CSR=00h. The software off state is D3, CSR=03h. To reset
6870 * the controller, place the interface device in D3 then to D0,
6871 * this causes a secondary PCI reset which will reset the
6872 * controller." */
6873
Don Brace2662cab2015-01-23 16:41:25 -06006874 int rc = 0;
6875
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006876 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
Don Brace2662cab2015-01-23 16:41:25 -06006877
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006878 /* enter the D3hot power management state */
Don Brace2662cab2015-01-23 16:41:25 -06006879 rc = pci_set_power_state(pdev, PCI_D3hot);
6880 if (rc)
6881 return rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006882
6883 msleep(500);
6884
6885 /* enter the D0 power management state */
Don Brace2662cab2015-01-23 16:41:25 -06006886 rc = pci_set_power_state(pdev, PCI_D0);
6887 if (rc)
6888 return rc;
Mike Millerc4853ef2011-10-21 08:19:43 +02006889
6890 /*
6891 * The P600 requires a small delay when changing states.
6892 * Otherwise we may think the board did not reset and we bail.
6893 * This for kdump only and is particular to the P600.
6894 */
6895 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006896 }
6897 return 0;
6898}
6899
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006900static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006901{
6902 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006903 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006904}
6905
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006906static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006907{
6908 char *driver_version;
6909 int i, size = sizeof(cfgtable->driver_version);
6910
6911 driver_version = kmalloc(size, GFP_KERNEL);
6912 if (!driver_version)
6913 return -ENOMEM;
6914
6915 init_driver_version(driver_version, size);
6916 for (i = 0; i < size; i++)
6917 writeb(driver_version[i], &cfgtable->driver_version[i]);
6918 kfree(driver_version);
6919 return 0;
6920}
6921
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006922static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
6923 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006924{
6925 int i;
6926
6927 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
6928 driver_ver[i] = readb(&cfgtable->driver_version[i]);
6929}
6930
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006931static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006932{
6933
6934 char *driver_ver, *old_driver_ver;
6935 int rc, size = sizeof(cfgtable->driver_version);
6936
6937 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
6938 if (!old_driver_ver)
6939 return -ENOMEM;
6940 driver_ver = old_driver_ver + size;
6941
6942 /* After a reset, the 32 bytes of "driver version" in the cfgtable
6943 * should have been changed, otherwise we know the reset failed.
6944 */
6945 init_driver_version(old_driver_ver, size);
6946 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
6947 rc = !memcmp(driver_ver, old_driver_ver, size);
6948 kfree(old_driver_ver);
6949 return rc;
6950}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006951/* This does a hard reset of the controller using PCI power management
6952 * states or the using the doorbell register.
6953 */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02006954static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006955{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006956 u64 cfg_offset;
6957 u32 cfg_base_addr;
6958 u64 cfg_base_addr_index;
6959 void __iomem *vaddr;
6960 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05006961 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06006962 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006963 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05006964 u32 use_doorbell;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06006965 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006966
6967 /* For controllers as old as the P600, this is very nearly
6968 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006969 *
6970 * pci_save_state(pci_dev);
6971 * pci_set_power_state(pci_dev, PCI_D3hot);
6972 * pci_set_power_state(pci_dev, PCI_D0);
6973 * pci_restore_state(pci_dev);
6974 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006975 * For controllers newer than the P600, the pci power state
6976 * method of resetting doesn't work so we have another way
6977 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006978 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05006979
Robert Elliott60f923b2015-01-23 16:42:06 -06006980 if (!ctlr_is_resettable(board_id)) {
6981 dev_warn(&pdev->dev, "Controller not resettable\n");
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06006982 return -ENODEV;
6983 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05006984
6985 /* if controller is soft- but not hard resettable... */
6986 if (!ctlr_is_hard_resettable(board_id))
6987 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05006988
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06006989 /* Save the PCI command register */
6990 pci_read_config_word(pdev, 4, &command_register);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06006991 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006992
6993 /* find the first memory BAR, so we can find the cfg table */
6994 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
6995 if (rc)
6996 return rc;
6997 vaddr = remap_pci_mem(paddr, 0x250);
6998 if (!vaddr)
6999 return -ENOMEM;
7000
7001 /* find cfgtable in order to check if reset via doorbell is supported */
7002 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
7003 &cfg_base_addr_index, &cfg_offset);
7004 if (rc)
7005 goto unmap_vaddr;
7006 cfgtable = remap_pci_mem(pci_resource_start(pdev,
7007 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
7008 if (!cfgtable) {
7009 rc = -ENOMEM;
7010 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007011 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007012 rc = write_driver_ver_to_cfgtable(cfgtable);
7013 if (rc)
Tomas Henzl03741d92015-01-23 16:41:14 -06007014 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007015
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007016 /* If reset via doorbell register is supported, use that.
7017 * There are two such methods. Favor the newest method.
7018 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007019 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007020 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
7021 if (use_doorbell) {
7022 use_doorbell = DOORBELL_CTLR_RESET2;
7023 } else {
7024 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
7025 if (use_doorbell) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007026 dev_warn(&pdev->dev,
7027 "Soft reset not supported. Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007028 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05007029 goto unmap_cfgtable;
7030 }
7031 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007032
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007033 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
7034 if (rc)
7035 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007036
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007037 pci_restore_state(pdev);
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06007038 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007039
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007040 /* Some devices (notably the HP Smart Array 5i Controller)
7041 need a little pause here */
7042 msleep(HPSA_POST_RESET_PAUSE_MSECS);
7043
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007044 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
7045 if (rc) {
7046 dev_warn(&pdev->dev,
Stephen Cameron050f7142015-01-23 16:42:22 -06007047 "Failed waiting for board to become ready after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007048 goto unmap_cfgtable;
7049 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007050
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007051 rc = controller_reset_failed(vaddr);
7052 if (rc < 0)
7053 goto unmap_cfgtable;
7054 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007055 dev_warn(&pdev->dev, "Unable to successfully reset "
7056 "controller. Will try soft reset.\n");
7057 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007058 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007059 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007060 }
7061
7062unmap_cfgtable:
7063 iounmap(cfgtable);
7064
7065unmap_vaddr:
7066 iounmap(vaddr);
7067 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007068}
7069
7070/*
7071 * We cannot read the structure directly, for portability we must use
7072 * the io functions.
7073 * This is for debug only.
7074 */
Don Brace42a91642014-11-14 17:26:27 -06007075static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007076{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007077#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007078 int i;
7079 char temp_name[17];
7080
7081 dev_info(dev, "Controller Configuration information\n");
7082 dev_info(dev, "------------------------------------\n");
7083 for (i = 0; i < 4; i++)
7084 temp_name[i] = readb(&(tb->Signature[i]));
7085 temp_name[4] = '\0';
7086 dev_info(dev, " Signature = %s\n", temp_name);
7087 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
7088 dev_info(dev, " Transport methods supported = 0x%x\n",
7089 readl(&(tb->TransportSupport)));
7090 dev_info(dev, " Transport methods active = 0x%x\n",
7091 readl(&(tb->TransportActive)));
7092 dev_info(dev, " Requested transport Method = 0x%x\n",
7093 readl(&(tb->HostWrite.TransportRequest)));
7094 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
7095 readl(&(tb->HostWrite.CoalIntDelay)));
7096 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
7097 readl(&(tb->HostWrite.CoalIntCount)));
Robert Elliott69d6e332015-01-23 16:41:56 -06007098 dev_info(dev, " Max outstanding commands = %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007099 readl(&(tb->CmdsOutMax)));
7100 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
7101 for (i = 0; i < 16; i++)
7102 temp_name[i] = readb(&(tb->ServerName[i]));
7103 temp_name[16] = '\0';
7104 dev_info(dev, " Server Name = %s\n", temp_name);
7105 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
7106 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007107#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05007108}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007109
7110static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
7111{
7112 int i, offset, mem_type, bar_type;
7113
7114 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
7115 return 0;
7116 offset = 0;
7117 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
7118 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
7119 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
7120 offset += 4;
7121 else {
7122 mem_type = pci_resource_flags(pdev, i) &
7123 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
7124 switch (mem_type) {
7125 case PCI_BASE_ADDRESS_MEM_TYPE_32:
7126 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
7127 offset += 4; /* 32 bit */
7128 break;
7129 case PCI_BASE_ADDRESS_MEM_TYPE_64:
7130 offset += 8;
7131 break;
7132 default: /* reserved in PCI 2.2 */
7133 dev_warn(&pdev->dev,
7134 "base address is invalid\n");
7135 return -1;
7136 break;
7137 }
7138 }
7139 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
7140 return i + 1;
7141 }
7142 return -1;
7143}
7144
Robert Elliottcc64c812015-04-23 09:33:12 -05007145static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
7146{
7147 if (h->msix_vector) {
7148 if (h->pdev->msix_enabled)
7149 pci_disable_msix(h->pdev);
Robert Elliott105a3db2015-04-23 09:33:48 -05007150 h->msix_vector = 0;
Robert Elliottcc64c812015-04-23 09:33:12 -05007151 } else if (h->msi_vector) {
7152 if (h->pdev->msi_enabled)
7153 pci_disable_msi(h->pdev);
Robert Elliott105a3db2015-04-23 09:33:48 -05007154 h->msi_vector = 0;
Robert Elliottcc64c812015-04-23 09:33:12 -05007155 }
7156}
7157
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007158/* If MSI/MSI-X is supported by the kernel we will try to enable it on
Stephen Cameron050f7142015-01-23 16:42:22 -06007159 * controllers that are capable. If not, we use legacy INTx mode.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007160 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007161static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007162{
7163#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05007164 int err, i;
7165 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
7166
7167 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
7168 hpsa_msix_entries[i].vector = 0;
7169 hpsa_msix_entries[i].entry = i;
7170 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007171
7172 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05007173 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
7174 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007175 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007176 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007177 dev_info(&h->pdev->dev, "MSI-X capable controller\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007178 h->msix_vector = MAX_REPLY_QUEUES;
Stephen M. Cameronf89439b2014-05-29 10:53:02 -05007179 if (h->msix_vector > num_online_cpus())
7180 h->msix_vector = num_online_cpus();
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007181 err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
7182 1, h->msix_vector);
7183 if (err < 0) {
7184 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
7185 h->msix_vector = 0;
7186 goto single_msi_mode;
7187 } else if (err < h->msix_vector) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007188 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007189 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007190 }
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007191 h->msix_vector = err;
7192 for (i = 0; i < h->msix_vector; i++)
7193 h->intr[i] = hpsa_msix_entries[i].vector;
7194 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007195 }
Alexander Gordeev18fce3c2014-08-18 08:01:42 +02007196single_msi_mode:
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007197 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007198 dev_info(&h->pdev->dev, "MSI capable controller\n");
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007199 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007200 h->msi_vector = 1;
7201 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007202 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007203 }
7204default_int_mode:
7205#endif /* CONFIG_PCI_MSI */
7206 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06007207 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007208}
7209
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007210static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007211{
7212 int i;
7213 u32 subsystem_vendor_id, subsystem_device_id;
7214
7215 subsystem_vendor_id = pdev->subsystem_vendor;
7216 subsystem_device_id = pdev->subsystem_device;
7217 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
7218 subsystem_vendor_id;
7219
7220 for (i = 0; i < ARRAY_SIZE(products); i++)
7221 if (*board_id == products[i].board_id)
7222 return i;
7223
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05007224 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
7225 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
7226 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007227 dev_warn(&pdev->dev, "unrecognized board ID: "
7228 "0x%08x, ignoring.\n", *board_id);
7229 return -ENODEV;
7230 }
7231 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
7232}
7233
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007234static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
7235 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007236{
7237 int i;
7238
7239 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007240 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007241 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007242 *memory_bar = pci_resource_start(pdev, i);
7243 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007244 *memory_bar);
7245 return 0;
7246 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007247 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007248 return -ENODEV;
7249}
7250
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007251static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
7252 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007253{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007254 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007255 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007256 if (wait_for_ready)
7257 iterations = HPSA_BOARD_READY_ITERATIONS;
7258 else
7259 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007260
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007261 for (i = 0; i < iterations; i++) {
7262 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
7263 if (wait_for_ready) {
7264 if (scratchpad == HPSA_FIRMWARE_READY)
7265 return 0;
7266 } else {
7267 if (scratchpad != HPSA_FIRMWARE_READY)
7268 return 0;
7269 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007270 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
7271 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007272 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007273 return -ENODEV;
7274}
7275
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007276static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
7277 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
7278 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007279{
7280 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
7281 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
7282 *cfg_base_addr &= (u32) 0x0000ffff;
7283 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
7284 if (*cfg_base_addr_index == -1) {
7285 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
7286 return -ENODEV;
7287 }
7288 return 0;
7289}
7290
Robert Elliott195f2c62015-04-23 09:33:17 -05007291static void hpsa_free_cfgtables(struct ctlr_info *h)
7292{
Robert Elliott105a3db2015-04-23 09:33:48 -05007293 if (h->transtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007294 iounmap(h->transtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007295 h->transtable = NULL;
7296 }
7297 if (h->cfgtable) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007298 iounmap(h->cfgtable);
Robert Elliott105a3db2015-04-23 09:33:48 -05007299 h->cfgtable = NULL;
7300 }
Robert Elliott195f2c62015-04-23 09:33:17 -05007301}
7302
7303/* Find and map CISS config table and transfer table
7304+ * several items must be unmapped (freed) later
7305+ * */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007306static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007307{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06007308 u64 cfg_offset;
7309 u32 cfg_base_addr;
7310 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06007311 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007312 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007313
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007314 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7315 &cfg_base_addr_index, &cfg_offset);
7316 if (rc)
7317 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007318 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007319 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007320 if (!h->cfgtable) {
7321 dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007322 return -ENOMEM;
Robert Elliottcd3c81c2015-01-23 16:42:27 -06007323 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05007324 rc = write_driver_ver_to_cfgtable(h->cfgtable);
7325 if (rc)
7326 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007327 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05007328 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007329 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
7330 cfg_base_addr_index)+cfg_offset+trans_offset,
7331 sizeof(*h->transtable));
Robert Elliott195f2c62015-04-23 09:33:17 -05007332 if (!h->transtable) {
7333 dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
7334 hpsa_free_cfgtables(h);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007335 return -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05007336 }
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007337 return 0;
7338}
7339
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007340static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007341{
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007342#define MIN_MAX_COMMANDS 16
7343 BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
7344
7345 h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06007346
7347 /* Limit commands in memory limited kdump scenario. */
7348 if (reset_devices && h->max_commands > 32)
7349 h->max_commands = 32;
7350
Stephen Cameron41ce4c32015-04-23 09:31:47 -05007351 if (h->max_commands < MIN_MAX_COMMANDS) {
7352 dev_warn(&h->pdev->dev,
7353 "Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
7354 h->max_commands,
7355 MIN_MAX_COMMANDS);
7356 h->max_commands = MIN_MAX_COMMANDS;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007357 }
7358}
7359
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007360/* If the controller reports that the total max sg entries is greater than 512,
7361 * then we know that chained SG blocks work. (Original smart arrays did not
7362 * support chained SG blocks and would return zero for max sg entries.)
7363 */
7364static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
7365{
7366 return h->maxsgentries > 512;
7367}
7368
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007369/* Interrogate the hardware for some limits:
7370 * max commands, max SG elements without chaining, and with chaining,
7371 * SG chain block size, etc.
7372 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007373static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007374{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007375 hpsa_get_max_perf_mode_cmds(h);
Stephen Cameron45fcb862015-01-23 16:43:04 -06007376 h->nr_cmds = h->max_commands;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007377 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007378 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007379 if (hpsa_supports_chained_sg_blocks(h)) {
7380 /* Limit in-command s/g elements to 32 save dma'able memory. */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007381 h->max_cmd_sg_entries = 32;
Webb Scales1a63ea62014-11-14 17:26:43 -06007382 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007383 h->maxsgentries--; /* save one for chain pointer */
7384 } else {
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007385 /*
7386 * Original smart arrays supported at most 31 s/g entries
7387 * embedded inline in the command (trying to use more
7388 * would lock up the controller)
7389 */
7390 h->max_cmd_sg_entries = 31;
Webb Scales1a63ea62014-11-14 17:26:43 -06007391 h->maxsgentries = 31; /* default to traditional values */
Webb Scalesc7ee65b2015-01-23 16:42:17 -06007392 h->chainsize = 0;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007393 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05007394
7395 /* Find out what task management functions are supported and cache */
7396 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06007397 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
7398 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
7399 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
7400 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen Cameron8be986c2015-04-23 09:34:06 -05007401 if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
7402 dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007403}
7404
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007405static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
7406{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09007407 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06007408 dev_err(&h->pdev->dev, "not a valid CISS config table\n");
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007409 return false;
7410 }
7411 return true;
7412}
7413
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007414static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007415{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007416 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007417
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007418 driver_support = readl(&(h->cfgtable->driver_support));
Arnd Bergmann0b9e7b72014-06-26 15:44:52 +02007419 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
7420#ifdef CONFIG_X86
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007421 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007422#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06007423 driver_support |= ENABLE_UNIT_ATTN;
7424 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05007425}
7426
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05007427/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
7428 * in a prefetch beyond physical memory.
7429 */
7430static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
7431{
7432 u32 dma_prefetch;
7433
7434 if (h->board_id != 0x3225103C)
7435 return;
7436 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
7437 dma_prefetch |= 0x8000;
7438 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
7439}
7440
Robert Elliottc706a792015-01-23 16:45:01 -06007441static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007442{
7443 int i;
7444 u32 doorbell_value;
7445 unsigned long flags;
7446 /* wait until the clear_event_notify bit 6 is cleared by controller. */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007447 for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007448 spin_lock_irqsave(&h->lock, flags);
7449 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7450 spin_unlock_irqrestore(&h->lock, flags);
7451 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
Robert Elliottc706a792015-01-23 16:45:01 -06007452 goto done;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007453 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007454 msleep(CLEAR_EVENT_WAIT_INTERVAL);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007455 }
Robert Elliottc706a792015-01-23 16:45:01 -06007456 return -ENODEV;
7457done:
7458 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007459}
7460
Robert Elliottc706a792015-01-23 16:45:01 -06007461static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007462{
7463 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007464 u32 doorbell_value;
7465 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007466
7467 /* under certain very rare conditions, this can take awhile.
7468 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
7469 * as we enter this code.)
7470 */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007471 for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
Webb Scales25163bd2015-04-23 09:32:00 -05007472 if (h->remove_in_progress)
7473 goto done;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06007474 spin_lock_irqsave(&h->lock, flags);
7475 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
7476 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06007477 if (!(doorbell_value & CFGTBL_ChangeReq))
Robert Elliottc706a792015-01-23 16:45:01 -06007478 goto done;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007479 /* delay and try again */
Robert Elliott007e7aa2015-01-23 16:44:56 -06007480 msleep(MODE_CHANGE_WAIT_INTERVAL);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007481 }
Robert Elliottc706a792015-01-23 16:45:01 -06007482 return -ENODEV;
7483done:
7484 return 0;
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007485}
7486
Robert Elliottc706a792015-01-23 16:45:01 -06007487/* return -ENODEV or other reason on error, 0 on success */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007488static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007489{
7490 u32 trans_support;
7491
7492 trans_support = readl(&(h->cfgtable->TransportSupport));
7493 if (!(trans_support & SIMPLE_MODE))
7494 return -ENOTSUPP;
7495
7496 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007497
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007498 /* Update the field, and then ring the doorbell */
7499 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007500 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05007501 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06007502 if (hpsa_wait_for_mode_change_ack(h))
7503 goto error;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007504 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007505 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
7506 goto error;
Stephen M. Cameron960a30e72011-02-15 15:33:03 -06007507 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007508 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007509error:
Stephen Cameron050f7142015-01-23 16:42:22 -06007510 dev_err(&h->pdev->dev, "failed to enter simple mode\n");
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007511 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007512}
7513
Robert Elliott195f2c62015-04-23 09:33:17 -05007514/* free items allocated or mapped by hpsa_pci_init */
7515static void hpsa_free_pci_init(struct ctlr_info *h)
7516{
7517 hpsa_free_cfgtables(h); /* pci_init 4 */
7518 iounmap(h->vaddr); /* pci_init 3 */
Robert Elliott105a3db2015-04-23 09:33:48 -05007519 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05007520 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Robert Elliott943a7022015-04-23 09:34:32 -05007521 /*
7522 * call pci_disable_device before pci_release_regions per
7523 * Documentation/PCI/pci.txt
7524 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007525 pci_disable_device(h->pdev); /* pci_init 1 */
Robert Elliott943a7022015-04-23 09:34:32 -05007526 pci_release_regions(h->pdev); /* pci_init 2 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007527}
7528
7529/* several items must be freed later */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007530static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007531{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007532 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007533
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007534 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
7535 if (prod_index < 0)
Robert Elliott60f923b2015-01-23 16:42:06 -06007536 return prod_index;
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05007537 h->product_name = products[prod_index].product_name;
7538 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007539
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05007540 h->needs_abort_tags_swizzled =
7541 ctlr_needs_abort_tags_swizzled(h->board_id);
7542
Matthew Garrette5a44df2011-11-11 11:14:23 -05007543 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
7544 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
7545
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007546 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007547 if (err) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007548 dev_err(&h->pdev->dev, "failed to enable PCI device\n");
Robert Elliott943a7022015-04-23 09:34:32 -05007549 pci_disable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007550 return err;
7551 }
7552
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06007553 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007554 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05007555 dev_err(&h->pdev->dev,
Robert Elliott195f2c62015-04-23 09:33:17 -05007556 "failed to obtain PCI resources\n");
Robert Elliott943a7022015-04-23 09:34:32 -05007557 pci_disable_device(h->pdev);
7558 return err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007559 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06007560
7561 pci_set_master(h->pdev);
7562
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05007563 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05007564 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05007565 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007566 goto clean2; /* intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007567 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007568 if (!h->vaddr) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007569 dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007570 err = -ENOMEM;
Robert Elliott195f2c62015-04-23 09:33:17 -05007571 goto clean2; /* intmode+region, pci */
Stephen M. Cameron204892e2010-05-27 15:13:22 -05007572 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06007573 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05007574 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007575 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameron77c44952010-05-27 15:13:17 -05007576 err = hpsa_find_cfgtables(h);
7577 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007578 goto clean3; /* vaddr, intmode+region, pci */
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05007579 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007580
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05007581 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007582 err = -ENODEV;
Robert Elliott195f2c62015-04-23 09:33:17 -05007583 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007584 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06007585 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05007586 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05007587 err = hpsa_enter_simple_mode(h);
7588 if (err)
Robert Elliott195f2c62015-04-23 09:33:17 -05007589 goto clean4; /* cfgtables, vaddr, intmode+region, pci */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007590 return 0;
7591
Robert Elliott195f2c62015-04-23 09:33:17 -05007592clean4: /* cfgtables, vaddr, intmode+region, pci */
7593 hpsa_free_cfgtables(h);
7594clean3: /* vaddr, intmode+region, pci */
7595 iounmap(h->vaddr);
Robert Elliott105a3db2015-04-23 09:33:48 -05007596 h->vaddr = NULL;
Robert Elliott195f2c62015-04-23 09:33:17 -05007597clean2: /* intmode+region, pci */
7598 hpsa_disable_interrupt_mode(h);
Robert Elliott943a7022015-04-23 09:34:32 -05007599 /*
7600 * call pci_disable_device before pci_release_regions per
7601 * Documentation/PCI/pci.txt
7602 */
Robert Elliott195f2c62015-04-23 09:33:17 -05007603 pci_disable_device(h->pdev);
Robert Elliott943a7022015-04-23 09:34:32 -05007604 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007605 return err;
7606}
7607
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007608static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06007609{
7610 int rc;
7611
7612#define HBA_INQUIRY_BYTE_COUNT 64
7613 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
7614 if (!h->hba_inquiry_data)
7615 return;
7616 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
7617 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
7618 if (rc != 0) {
7619 kfree(h->hba_inquiry_data);
7620 h->hba_inquiry_data = NULL;
7621 }
7622}
7623
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007624static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007625{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007626 int rc, i;
Tomas Henzl3b747292015-01-23 16:41:20 -06007627 void __iomem *vaddr;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007628
7629 if (!reset_devices)
7630 return 0;
7631
Tomas Henzl132aa222014-08-14 16:12:39 +02007632 /* kdump kernel is loading, we don't know in which state is
7633 * the pci interface. The dev->enable_cnt is equal zero
7634 * so we call enable+disable, wait a while and switch it on.
7635 */
7636 rc = pci_enable_device(pdev);
7637 if (rc) {
7638 dev_warn(&pdev->dev, "Failed to enable PCI device\n");
7639 return -ENODEV;
7640 }
7641 pci_disable_device(pdev);
7642 msleep(260); /* a randomly chosen number */
7643 rc = pci_enable_device(pdev);
7644 if (rc) {
7645 dev_warn(&pdev->dev, "failed to enable device.\n");
7646 return -ENODEV;
7647 }
Robert Elliott4fa604e2014-11-14 17:27:24 -06007648
Tomas Henzl859c75a2014-09-12 14:44:15 +02007649 pci_set_master(pdev);
Robert Elliott4fa604e2014-11-14 17:27:24 -06007650
Tomas Henzl3b747292015-01-23 16:41:20 -06007651 vaddr = pci_ioremap_bar(pdev, 0);
7652 if (vaddr == NULL) {
7653 rc = -ENOMEM;
7654 goto out_disable;
7655 }
7656 writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
7657 iounmap(vaddr);
7658
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007659 /* Reset the controller with a PCI power-cycle or via doorbell */
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02007660 rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007661
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007662 /* -ENOTSUPP here means we cannot reset the controller
7663 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05007664 * "performant mode". Or, it might be 640x, which can't reset
7665 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05007666 */
Robert Elliottadf1b3a2015-01-23 16:42:01 -06007667 if (rc)
Tomas Henzl132aa222014-08-14 16:12:39 +02007668 goto out_disable;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007669
7670 /* Now try to get the controller to respond to a no-op */
Robert Elliott1ba66c92015-01-23 16:42:11 -06007671 dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007672 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
7673 if (hpsa_noop(pdev) == 0)
7674 break;
7675 else
7676 dev_warn(&pdev->dev, "no-op failed%s\n",
7677 (i < 11 ? "; re-trying" : ""));
7678 }
Tomas Henzl132aa222014-08-14 16:12:39 +02007679
7680out_disable:
7681
7682 pci_disable_device(pdev);
7683 return rc;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05007684}
7685
Robert Elliott1fb7c982015-04-23 09:33:22 -05007686static void hpsa_free_cmd_pool(struct ctlr_info *h)
7687{
7688 kfree(h->cmd_pool_bits);
Robert Elliott105a3db2015-04-23 09:33:48 -05007689 h->cmd_pool_bits = NULL;
7690 if (h->cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05007691 pci_free_consistent(h->pdev,
7692 h->nr_cmds * sizeof(struct CommandList),
7693 h->cmd_pool,
7694 h->cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05007695 h->cmd_pool = NULL;
7696 h->cmd_pool_dhandle = 0;
7697 }
7698 if (h->errinfo_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05007699 pci_free_consistent(h->pdev,
7700 h->nr_cmds * sizeof(struct ErrorInfo),
7701 h->errinfo_pool,
7702 h->errinfo_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05007703 h->errinfo_pool = NULL;
7704 h->errinfo_pool_dhandle = 0;
7705 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05007706}
7707
Robert Elliottd37ffbe2015-04-23 09:32:27 -05007708static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007709{
7710 h->cmd_pool_bits = kzalloc(
7711 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
7712 sizeof(unsigned long), GFP_KERNEL);
7713 h->cmd_pool = pci_alloc_consistent(h->pdev,
7714 h->nr_cmds * sizeof(*h->cmd_pool),
7715 &(h->cmd_pool_dhandle));
7716 h->errinfo_pool = pci_alloc_consistent(h->pdev,
7717 h->nr_cmds * sizeof(*h->errinfo_pool),
7718 &(h->errinfo_pool_dhandle));
7719 if ((h->cmd_pool_bits == NULL)
7720 || (h->cmd_pool == NULL)
7721 || (h->errinfo_pool == NULL)) {
7722 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
Robert Elliott2c143342015-01-23 16:42:48 -06007723 goto clean_up;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007724 }
Stephen Cameron360c73b2015-04-23 09:32:32 -05007725 hpsa_preinitialize_commands(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007726 return 0;
Robert Elliott2c143342015-01-23 16:42:48 -06007727clean_up:
7728 hpsa_free_cmd_pool(h);
7729 return -ENOMEM;
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05007730}
7731
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05007732static void hpsa_irq_affinity_hints(struct ctlr_info *h)
7733{
Fabian Frederickec429952015-01-23 16:41:46 -06007734 int i, cpu;
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05007735
7736 cpu = cpumask_first(cpu_online_mask);
7737 for (i = 0; i < h->msix_vector; i++) {
Fabian Frederickec429952015-01-23 16:41:46 -06007738 irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05007739 cpu = cpumask_next(cpu, cpu_online_mask);
7740 }
7741}
7742
Robert Elliottec501a12015-01-23 16:41:40 -06007743/* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
7744static void hpsa_free_irqs(struct ctlr_info *h)
7745{
7746 int i;
7747
7748 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
7749 /* Single reply queue, only one irq to free */
7750 i = h->intr_mode;
7751 irq_set_affinity_hint(h->intr[i], NULL);
7752 free_irq(h->intr[i], &h->q[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05007753 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06007754 return;
7755 }
7756
7757 for (i = 0; i < h->msix_vector; i++) {
7758 irq_set_affinity_hint(h->intr[i], NULL);
7759 free_irq(h->intr[i], &h->q[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05007760 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06007761 }
Robert Elliotta4e17fc2015-01-23 16:41:51 -06007762 for (; i < MAX_REPLY_QUEUES; i++)
7763 h->q[i] = 0;
Robert Elliottec501a12015-01-23 16:41:40 -06007764}
7765
Robert Elliott9ee61792015-01-23 16:42:32 -06007766/* returns 0 on success; cleans up and returns -Enn on error */
7767static int hpsa_request_irqs(struct ctlr_info *h,
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05007768 irqreturn_t (*msixhandler)(int, void *),
7769 irqreturn_t (*intxhandler)(int, void *))
7770{
Matt Gates254f7962012-05-01 11:43:06 -05007771 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05007772
Matt Gates254f7962012-05-01 11:43:06 -05007773 /*
7774 * initialize h->q[x] = x so that interrupt handlers know which
7775 * queue to process.
7776 */
7777 for (i = 0; i < MAX_REPLY_QUEUES; i++)
7778 h->q[i] = (u8) i;
7779
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007780 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05007781 /* If performant mode and MSI-X, use multiple reply queues */
Robert Elliotta4e17fc2015-01-23 16:41:51 -06007782 for (i = 0; i < h->msix_vector; i++) {
Robert Elliott8b470042015-04-23 09:34:58 -05007783 sprintf(h->intrname[i], "%s-msix%d", h->devname, i);
Matt Gates254f7962012-05-01 11:43:06 -05007784 rc = request_irq(h->intr[i], msixhandler,
Robert Elliott8b470042015-04-23 09:34:58 -05007785 0, h->intrname[i],
Matt Gates254f7962012-05-01 11:43:06 -05007786 &h->q[i]);
Robert Elliotta4e17fc2015-01-23 16:41:51 -06007787 if (rc) {
7788 int j;
7789
7790 dev_err(&h->pdev->dev,
7791 "failed to get irq %d for %s\n",
7792 h->intr[i], h->devname);
7793 for (j = 0; j < i; j++) {
7794 free_irq(h->intr[j], &h->q[j]);
7795 h->q[j] = 0;
7796 }
7797 for (; j < MAX_REPLY_QUEUES; j++)
7798 h->q[j] = 0;
7799 return rc;
7800 }
7801 }
Stephen M. Cameron41b3cf02014-05-29 10:53:13 -05007802 hpsa_irq_affinity_hints(h);
Matt Gates254f7962012-05-01 11:43:06 -05007803 } else {
7804 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007805 if (h->msix_vector > 0 || h->msi_vector) {
Robert Elliott8b470042015-04-23 09:34:58 -05007806 if (h->msix_vector)
7807 sprintf(h->intrname[h->intr_mode],
7808 "%s-msix", h->devname);
7809 else
7810 sprintf(h->intrname[h->intr_mode],
7811 "%s-msi", h->devname);
Matt Gates254f7962012-05-01 11:43:06 -05007812 rc = request_irq(h->intr[h->intr_mode],
Robert Elliott8b470042015-04-23 09:34:58 -05007813 msixhandler, 0,
7814 h->intrname[h->intr_mode],
Matt Gates254f7962012-05-01 11:43:06 -05007815 &h->q[h->intr_mode]);
7816 } else {
Robert Elliott8b470042015-04-23 09:34:58 -05007817 sprintf(h->intrname[h->intr_mode],
7818 "%s-intx", h->devname);
Matt Gates254f7962012-05-01 11:43:06 -05007819 rc = request_irq(h->intr[h->intr_mode],
Robert Elliott8b470042015-04-23 09:34:58 -05007820 intxhandler, IRQF_SHARED,
7821 h->intrname[h->intr_mode],
Matt Gates254f7962012-05-01 11:43:06 -05007822 &h->q[h->intr_mode]);
7823 }
Robert Elliott105a3db2015-04-23 09:33:48 -05007824 irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
Matt Gates254f7962012-05-01 11:43:06 -05007825 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05007826 if (rc) {
Robert Elliott195f2c62015-04-23 09:33:17 -05007827 dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05007828 h->intr[h->intr_mode], h->devname);
Robert Elliott195f2c62015-04-23 09:33:17 -05007829 hpsa_free_irqs(h);
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05007830 return -ENODEV;
7831 }
7832 return 0;
7833}
7834
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007835static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007836{
Robert Elliott39c53f52015-04-23 09:35:14 -05007837 int rc;
Robert Elliottbf43caf2015-04-23 09:33:38 -05007838 hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007839
7840 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05007841 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY);
7842 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007843 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05007844 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007845 }
7846
7847 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05007848 rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
7849 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007850 dev_warn(&h->pdev->dev, "Board failed to become ready "
7851 "after soft reset.\n");
Robert Elliott39c53f52015-04-23 09:35:14 -05007852 return rc;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007853 }
7854
7855 return 0;
7856}
7857
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007858static void hpsa_free_reply_queues(struct ctlr_info *h)
7859{
7860 int i;
7861
7862 for (i = 0; i < h->nreply_queues; i++) {
7863 if (!h->reply_queue[i].head)
7864 continue;
Robert Elliott1fb7c982015-04-23 09:33:22 -05007865 pci_free_consistent(h->pdev,
7866 h->reply_queue_size,
7867 h->reply_queue[i].head,
7868 h->reply_queue[i].busaddr);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007869 h->reply_queue[i].head = NULL;
7870 h->reply_queue[i].busaddr = 0;
7871 }
Robert Elliott105a3db2015-04-23 09:33:48 -05007872 h->reply_queue_size = 0;
Stephen M. Cameron072b0512014-05-29 10:53:07 -05007873}
7874
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05007875static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
7876{
Robert Elliott105a3db2015-04-23 09:33:48 -05007877 hpsa_free_performant_mode(h); /* init_one 7 */
7878 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
7879 hpsa_free_cmd_pool(h); /* init_one 5 */
7880 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliott2946e822015-04-23 09:35:09 -05007881 scsi_host_put(h->scsi_host); /* init_one 3 */
7882 h->scsi_host = NULL; /* init_one 3 */
7883 hpsa_free_pci_init(h); /* init_one 2_5 */
Robert Elliott9ecd9532015-04-23 09:34:43 -05007884 free_percpu(h->lockup_detected); /* init_one 2 */
7885 h->lockup_detected = NULL; /* init_one 2 */
7886 if (h->resubmit_wq) {
7887 destroy_workqueue(h->resubmit_wq); /* init_one 1 */
7888 h->resubmit_wq = NULL;
7889 }
7890 if (h->rescan_ctlr_wq) {
7891 destroy_workqueue(h->rescan_ctlr_wq);
7892 h->rescan_ctlr_wq = NULL;
7893 }
Robert Elliott105a3db2015-04-23 09:33:48 -05007894 kfree(h); /* init_one 1 */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05007895}
7896
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007897/* Called when controller lockup detected. */
Don Bracef2405db2015-01-23 16:43:09 -06007898static void fail_all_outstanding_cmds(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007899{
Webb Scales281a7fd2015-01-23 16:43:35 -06007900 int i, refcount;
7901 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05007902 int failcount = 0;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007903
Don Brace080ef1c2015-01-23 16:43:25 -06007904 flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
Don Bracef2405db2015-01-23 16:43:09 -06007905 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06007906 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06007907 refcount = atomic_inc_return(&c->refcount);
7908 if (refcount > 1) {
Webb Scales25163bd2015-04-23 09:32:00 -05007909 c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
Webb Scales281a7fd2015-01-23 16:43:35 -06007910 finish_cmd(c);
Stephen Cameron433b5f42015-04-23 09:32:11 -05007911 atomic_dec(&h->commands_outstanding);
Webb Scales25163bd2015-04-23 09:32:00 -05007912 failcount++;
Webb Scales281a7fd2015-01-23 16:43:35 -06007913 }
7914 cmd_free(h, c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007915 }
Webb Scales25163bd2015-04-23 09:32:00 -05007916 dev_warn(&h->pdev->dev,
7917 "failed %d commands in fail_all\n", failcount);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007918}
7919
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007920static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
7921{
Rusty Russellc8ed0012015-03-05 10:49:19 +10307922 int cpu;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007923
Rusty Russellc8ed0012015-03-05 10:49:19 +10307924 for_each_online_cpu(cpu) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007925 u32 *lockup_detected;
7926 lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
7927 *lockup_detected = value;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007928 }
7929 wmb(); /* be sure the per-cpu variables are out to memory */
7930}
7931
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007932static void controller_lockup_detected(struct ctlr_info *h)
7933{
7934 unsigned long flags;
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007935 u32 lockup_detected;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007936
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007937 h->access.set_intr_mask(h, HPSA_INTR_OFF);
7938 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007939 lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
7940 if (!lockup_detected) {
7941 /* no heartbeat, but controller gave us a zero. */
7942 dev_warn(&h->pdev->dev,
Webb Scales25163bd2015-04-23 09:32:00 -05007943 "lockup detected after %d but scratchpad register is zero\n",
7944 h->heartbeat_sample_interval / HZ);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05007945 lockup_detected = 0xffffffff;
7946 }
7947 set_lockup_detected_for_all_cpus(h, lockup_detected);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007948 spin_unlock_irqrestore(&h->lock, flags);
Webb Scales25163bd2015-04-23 09:32:00 -05007949 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
7950 lockup_detected, h->heartbeat_sample_interval / HZ);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007951 pci_disable_device(h->pdev);
Don Bracef2405db2015-01-23 16:43:09 -06007952 fail_all_outstanding_cmds(h);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007953}
7954
Webb Scales25163bd2015-04-23 09:32:00 -05007955static int detect_controller_lockup(struct ctlr_info *h)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007956{
7957 u64 now;
7958 u32 heartbeat;
7959 unsigned long flags;
7960
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007961 now = get_jiffies_64();
7962 /* If we've received an interrupt recently, we're ok. */
7963 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05007964 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05007965 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007966
7967 /*
7968 * If we've already checked the heartbeat recently, we're ok.
7969 * This could happen if someone sends us a signal. We
7970 * otherwise don't care about signals in this thread.
7971 */
7972 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05007973 (h->heartbeat_sample_interval), now))
Webb Scales25163bd2015-04-23 09:32:00 -05007974 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007975
7976 /* If heartbeat has not changed since we last looked, we're not ok. */
7977 spin_lock_irqsave(&h->lock, flags);
7978 heartbeat = readl(&h->cfgtable->HeartBeat);
7979 spin_unlock_irqrestore(&h->lock, flags);
7980 if (h->last_heartbeat == heartbeat) {
7981 controller_lockup_detected(h);
Webb Scales25163bd2015-04-23 09:32:00 -05007982 return true;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007983 }
7984
7985 /* We're ok. */
7986 h->last_heartbeat = heartbeat;
7987 h->last_heartbeat_timestamp = now;
Webb Scales25163bd2015-04-23 09:32:00 -05007988 return false;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05007989}
7990
Stephen M. Cameron98465902014-02-21 16:25:00 -06007991static void hpsa_ack_ctlr_events(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007992{
7993 int i;
7994 char *event_type;
7995
Stephen Camerone4aa3e62015-01-23 16:44:07 -06007996 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
7997 return;
7998
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007999 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06008000 if ((h->transMethod & (CFGTBL_Trans_io_accel1
8001 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008002 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
8003 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
8004
8005 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
8006 event_type = "state change";
8007 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
8008 event_type = "configuration change";
8009 /* Stop sending new RAID offload reqs via the IO accelerator */
8010 scsi_block_requests(h->scsi_host);
8011 for (i = 0; i < h->ndevices; i++)
8012 h->dev[i]->offload_enabled = 0;
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06008013 hpsa_drain_accel_commands(h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008014 /* Set 'accelerator path config change' bit */
8015 dev_warn(&h->pdev->dev,
8016 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
8017 h->events, event_type);
8018 writel(h->events, &(h->cfgtable->clear_event_notify));
8019 /* Set the "clear event notify field update" bit 6 */
8020 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8021 /* Wait until ctlr clears 'clear event notify field', bit 6 */
8022 hpsa_wait_for_clear_event_notify_ack(h);
8023 scsi_unblock_requests(h->scsi_host);
8024 } else {
8025 /* Acknowledge controller notification events. */
8026 writel(h->events, &(h->cfgtable->clear_event_notify));
8027 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
8028 hpsa_wait_for_clear_event_notify_ack(h);
8029#if 0
8030 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8031 hpsa_wait_for_mode_change_ack(h);
8032#endif
8033 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008034 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008035}
8036
8037/* Check a register on the controller to see if there are configuration
8038 * changes (added/changed/removed logical drives, etc.) which mean that
Scott Teele863d682014-02-18 13:57:05 -06008039 * we should rescan the controller for devices.
8040 * Also check flag for driver-initiated rescan.
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008041 */
Stephen M. Cameron98465902014-02-21 16:25:00 -06008042static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008043{
Don Brace853633e2015-11-04 15:50:37 -06008044 if (h->drv_req_rescan) {
8045 h->drv_req_rescan = 0;
8046 return 1;
8047 }
8048
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008049 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
Stephen M. Cameron98465902014-02-21 16:25:00 -06008050 return 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008051
8052 h->events = readl(&(h->cfgtable->event_notify));
Stephen M. Cameron98465902014-02-21 16:25:00 -06008053 return h->events & RESCAN_REQUIRED_EVENT_BITS;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06008054}
8055
Stephen M. Cameron98465902014-02-21 16:25:00 -06008056/*
8057 * Check if any of the offline devices have become ready
8058 */
8059static int hpsa_offline_devices_ready(struct ctlr_info *h)
8060{
8061 unsigned long flags;
8062 struct offline_device_entry *d;
8063 struct list_head *this, *tmp;
8064
8065 spin_lock_irqsave(&h->offline_device_lock, flags);
8066 list_for_each_safe(this, tmp, &h->offline_device_list) {
8067 d = list_entry(this, struct offline_device_entry,
8068 offline_list);
8069 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008070 if (!hpsa_volume_offline(h, d->scsi3addr)) {
8071 spin_lock_irqsave(&h->offline_device_lock, flags);
8072 list_del(&d->offline_list);
8073 spin_unlock_irqrestore(&h->offline_device_lock, flags);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008074 return 1;
Stephen M. Camerond1fea472014-07-03 10:17:58 -05008075 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008076 spin_lock_irqsave(&h->offline_device_lock, flags);
8077 }
8078 spin_unlock_irqrestore(&h->offline_device_lock, flags);
8079 return 0;
8080}
8081
Scott Teel34592252015-11-04 15:52:09 -06008082static int hpsa_luns_changed(struct ctlr_info *h)
8083{
8084 int rc = 1; /* assume there are changes */
8085 struct ReportLUNdata *logdev = NULL;
8086
8087 /* if we can't find out if lun data has changed,
8088 * assume that it has.
8089 */
8090
8091 if (!h->lastlogicals)
8092 goto out;
8093
8094 logdev = kzalloc(sizeof(*logdev), GFP_KERNEL);
8095 if (!logdev) {
8096 dev_warn(&h->pdev->dev,
8097 "Out of memory, can't track lun changes.\n");
8098 goto out;
8099 }
8100 if (hpsa_scsi_do_report_luns(h, 1, logdev, sizeof(*logdev), 0)) {
8101 dev_warn(&h->pdev->dev,
8102 "report luns failed, can't track lun changes.\n");
8103 goto out;
8104 }
8105 if (memcmp(logdev, h->lastlogicals, sizeof(*logdev))) {
8106 dev_info(&h->pdev->dev,
8107 "Lun changes detected.\n");
8108 memcpy(h->lastlogicals, logdev, sizeof(*logdev));
8109 goto out;
8110 } else
8111 rc = 0; /* no changes detected. */
8112out:
8113 kfree(logdev);
8114 return rc;
8115}
8116
Don Brace6636e7f2015-01-23 16:45:17 -06008117static void hpsa_rescan_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008118{
8119 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008120 struct ctlr_info *h = container_of(to_delayed_work(work),
Don Brace6636e7f2015-01-23 16:45:17 -06008121 struct ctlr_info, rescan_ctlr_work);
8122
8123
8124 if (h->remove_in_progress)
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008125 return;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008126
8127 if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
8128 scsi_host_get(h->scsi_host);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008129 hpsa_ack_ctlr_events(h);
8130 hpsa_scan_start(h->scsi_host);
8131 scsi_host_put(h->scsi_host);
Scott Teel34592252015-11-04 15:52:09 -06008132 } else if (h->discovery_polling) {
Scott Teelc2adae42015-11-04 15:52:16 -06008133 hpsa_disable_rld_caching(h);
Scott Teel34592252015-11-04 15:52:09 -06008134 if (hpsa_luns_changed(h)) {
8135 struct Scsi_Host *sh = NULL;
8136
8137 dev_info(&h->pdev->dev,
8138 "driver discovery polling rescan.\n");
8139 sh = scsi_host_get(h->scsi_host);
8140 if (sh != NULL) {
8141 hpsa_scan_start(sh);
8142 scsi_host_put(sh);
8143 }
8144 }
Stephen M. Cameron98465902014-02-21 16:25:00 -06008145 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008146 spin_lock_irqsave(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06008147 if (!h->remove_in_progress)
8148 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008149 h->heartbeat_sample_interval);
8150 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008151}
8152
Don Brace6636e7f2015-01-23 16:45:17 -06008153static void hpsa_monitor_ctlr_worker(struct work_struct *work)
8154{
8155 unsigned long flags;
8156 struct ctlr_info *h = container_of(to_delayed_work(work),
8157 struct ctlr_info, monitor_ctlr_work);
8158
8159 detect_controller_lockup(h);
8160 if (lockup_detected(h))
8161 return;
8162
8163 spin_lock_irqsave(&h->lock, flags);
8164 if (!h->remove_in_progress)
8165 schedule_delayed_work(&h->monitor_ctlr_work,
8166 h->heartbeat_sample_interval);
8167 spin_unlock_irqrestore(&h->lock, flags);
8168}
8169
8170static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
8171 char *name)
8172{
8173 struct workqueue_struct *wq = NULL;
Don Brace6636e7f2015-01-23 16:45:17 -06008174
Don Brace397ea9c2015-02-06 17:44:15 -06008175 wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
Don Brace6636e7f2015-01-23 16:45:17 -06008176 if (!wq)
8177 dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
8178
8179 return wq;
8180}
8181
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008182static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008183{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05008184 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008185 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008186 int try_soft_reset = 0;
8187 unsigned long flags;
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008188 u32 board_id;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008189
8190 if (number_of_controllers == 0)
8191 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008192
Tomas Henzl6b6c1cd2015-04-02 15:25:54 +02008193 rc = hpsa_lookup_board_id(pdev, &board_id);
8194 if (rc < 0) {
8195 dev_warn(&pdev->dev, "Board ID not found\n");
8196 return rc;
8197 }
8198
8199 rc = hpsa_init_reset_devices(pdev, board_id);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008200 if (rc) {
8201 if (rc != -ENOTSUPP)
8202 return rc;
8203 /* If the reset fails in a particular way (it has no way to do
8204 * a proper hard reset, so returns -ENOTSUPP) we can try to do
8205 * a soft reset once we get the controller configured up to the
8206 * point that it can accept a command.
8207 */
8208 try_soft_reset = 1;
8209 rc = 0;
8210 }
8211
8212reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008213
Don Brace303932f2010-02-04 08:42:40 -06008214 /* Command structures must be aligned on a 32-byte boundary because
8215 * the 5 lower bits of the address are used by the hardware. and by
8216 * the driver. See comments in hpsa.h for more info.
8217 */
Don Brace303932f2010-02-04 08:42:40 -06008218 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008219 h = kzalloc(sizeof(*h), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05008220 if (!h) {
8221 dev_err(&pdev->dev, "Failed to allocate controller head\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008222 return -ENOMEM;
Robert Elliott105a3db2015-04-23 09:33:48 -05008223 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008224
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008225 h->pdev = pdev;
Robert Elliott105a3db2015-04-23 09:33:48 -05008226
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06008227 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron98465902014-02-21 16:25:00 -06008228 INIT_LIST_HEAD(&h->offline_device_list);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008229 spin_lock_init(&h->lock);
Stephen M. Cameron98465902014-02-21 16:25:00 -06008230 spin_lock_init(&h->offline_device_lock);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06008231 spin_lock_init(&h->scan_lock);
Don Brace34f0c622015-01-23 16:43:46 -06008232 atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05008233 atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008234
8235 /* Allocate and clear per-cpu variable lockup_detected */
8236 h->lockup_detected = alloc_percpu(u32);
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008237 if (!h->lockup_detected) {
Robert Elliott105a3db2015-04-23 09:33:48 -05008238 dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008239 rc = -ENOMEM;
Robert Elliott2efa5922015-04-23 09:34:53 -05008240 goto clean1; /* aer/h */
Stephen M. Cameron2a5ac322014-07-03 10:18:08 -05008241 }
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008242 set_lockup_detected_for_all_cpus(h, 0);
8243
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05008244 rc = hpsa_pci_init(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05008245 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008246 goto clean2; /* lu, aer/h */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008247
Robert Elliott2946e822015-04-23 09:35:09 -05008248 /* relies on h-> settings made by hpsa_pci_init, including
8249 * interrupt_mode h->intr */
8250 rc = hpsa_scsi_host_alloc(h);
8251 if (rc)
8252 goto clean2_5; /* pci, lu, aer/h */
8253
8254 sprintf(h->devname, HPSA "%d", h->scsi_host->host_no);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008255 h->ctlr = number_of_controllers;
8256 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008257
8258 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008259 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
8260 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008261 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008262 } else {
8263 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8264 if (rc == 0) {
8265 dac = 0;
8266 } else {
8267 dev_err(&pdev->dev, "no suitable DMA available\n");
Robert Elliott2946e822015-04-23 09:35:09 -05008268 goto clean3; /* shost, pci, lu, aer/h */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008269 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008270 }
8271
8272 /* make sure the board interrupts are off */
8273 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05008274
Robert Elliott105a3db2015-04-23 09:33:48 -05008275 rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
8276 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008277 goto clean3; /* shost, pci, lu, aer/h */
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008278 rc = hpsa_alloc_cmd_pool(h);
Robert Elliott8947fd12015-01-23 16:42:54 -06008279 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008280 goto clean4; /* irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008281 rc = hpsa_alloc_sg_chain_blocks(h);
8282 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008283 goto clean5; /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Camerona08a8472010-02-04 08:43:16 -06008284 init_waitqueue_head(&h->scan_wait_queue);
Stephen Cameron9b5c48c2015-04-23 09:32:06 -05008285 init_waitqueue_head(&h->abort_cmd_wait_queue);
Webb Scalesd604f532015-04-23 09:35:22 -05008286 init_waitqueue_head(&h->event_sync_wait_queue);
8287 mutex_init(&h->reset_mutex);
Stephen M. Camerona08a8472010-02-04 08:43:16 -06008288 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008289
8290 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008291 h->ndevices = 0;
Robert Elliott2946e822015-04-23 09:35:09 -05008292
Stephen M. Cameron9a413382011-05-03 14:59:41 -05008293 spin_lock_init(&h->devlock);
Robert Elliott105a3db2015-04-23 09:33:48 -05008294 rc = hpsa_put_ctlr_into_performant_mode(h);
8295 if (rc)
Robert Elliott2946e822015-04-23 09:35:09 -05008296 goto clean6; /* sg, cmd, irq, shost, pci, lu, aer/h */
8297
8298 /* hook into SCSI subsystem */
8299 rc = hpsa_scsi_add_host(h);
8300 if (rc)
8301 goto clean7; /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
Robert Elliott2efa5922015-04-23 09:34:53 -05008302
8303 /* create the resubmit workqueue */
8304 h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
8305 if (!h->rescan_ctlr_wq) {
8306 rc = -ENOMEM;
8307 goto clean7;
8308 }
8309
8310 h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
8311 if (!h->resubmit_wq) {
8312 rc = -ENOMEM;
8313 goto clean7; /* aer/h */
8314 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008315
Robert Elliott105a3db2015-04-23 09:33:48 -05008316 /*
8317 * At this point, the controller is ready to take commands.
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008318 * Now, if reset_devices and the hard reset didn't work, try
8319 * the soft reset and see if that works.
8320 */
8321 if (try_soft_reset) {
8322
8323 /* This is kind of gross. We may or may not get a completion
8324 * from the soft reset command, and if we do, then the value
8325 * from the fifo may or may not be valid. So, we wait 10 secs
8326 * after the reset throwing away any completions we get during
8327 * that time. Unregister the interrupt handler and register
8328 * fake ones to scoop up any residual completions.
8329 */
8330 spin_lock_irqsave(&h->lock, flags);
8331 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8332 spin_unlock_irqrestore(&h->lock, flags);
Robert Elliottec501a12015-01-23 16:41:40 -06008333 hpsa_free_irqs(h);
Robert Elliott9ee61792015-01-23 16:42:32 -06008334 rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008335 hpsa_intx_discard_completions);
8336 if (rc) {
Robert Elliott9ee61792015-01-23 16:42:32 -06008337 dev_warn(&h->pdev->dev,
8338 "Failed to request_irq after soft reset.\n");
Robert Elliottd4987572015-04-23 09:34:37 -05008339 /*
Robert Elliottb2ef4802015-04-23 09:34:48 -05008340 * cannot goto clean7 or free_irqs will be called
8341 * again. Instead, do its work
8342 */
8343 hpsa_free_performant_mode(h); /* clean7 */
8344 hpsa_free_sg_chain_blocks(h); /* clean6 */
8345 hpsa_free_cmd_pool(h); /* clean5 */
8346 /*
8347 * skip hpsa_free_irqs(h) clean4 since that
8348 * was just called before request_irqs failed
Robert Elliottd4987572015-04-23 09:34:37 -05008349 */
8350 goto clean3;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008351 }
8352
8353 rc = hpsa_kdump_soft_reset(h);
8354 if (rc)
8355 /* Neither hard nor soft reset worked, we're hosed. */
Don Brace7ef73232015-07-18 11:12:33 -05008356 goto clean7;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008357
8358 dev_info(&h->pdev->dev, "Board READY.\n");
8359 dev_info(&h->pdev->dev,
8360 "Waiting for stale completions to drain.\n");
8361 h->access.set_intr_mask(h, HPSA_INTR_ON);
8362 msleep(10000);
8363 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8364
8365 rc = controller_reset_failed(h->cfgtable);
8366 if (rc)
8367 dev_info(&h->pdev->dev,
8368 "Soft reset appears to have failed.\n");
8369
8370 /* since the controller's reset, we have to go back and re-init
8371 * everything. Easiest to just forget what we've done and do it
8372 * all over again.
8373 */
8374 hpsa_undo_allocations_after_kdump_soft_reset(h);
8375 try_soft_reset = 0;
8376 if (rc)
Robert Elliottb2ef4802015-04-23 09:34:48 -05008377 /* don't goto clean, we already unallocated */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05008378 return -ENODEV;
8379
8380 goto reinit_after_soft_reset;
8381 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008382
Robert Elliott105a3db2015-04-23 09:33:48 -05008383 /* Enable Accelerated IO path at driver layer */
8384 h->acciopath_status = 1;
Scott Teel34592252015-11-04 15:52:09 -06008385 /* Disable discovery polling.*/
8386 h->discovery_polling = 0;
Scott Teelda0697b2014-02-18 13:57:00 -06008387
Scott Teele863d682014-02-18 13:57:05 -06008388
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008389 /* Turn the interrupts on so we can service requests */
8390 h->access.set_intr_mask(h, HPSA_INTR_ON);
8391
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06008392 hpsa_hba_inquiry(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008393
Scott Teel34592252015-11-04 15:52:09 -06008394 h->lastlogicals = kzalloc(sizeof(*(h->lastlogicals)), GFP_KERNEL);
8395 if (!h->lastlogicals)
8396 dev_info(&h->pdev->dev,
8397 "Can't track change to report lun data\n");
8398
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008399 /* Monitor the controller for firmware lockups */
8400 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
8401 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
8402 schedule_delayed_work(&h->monitor_ctlr_work,
8403 h->heartbeat_sample_interval);
Don Brace6636e7f2015-01-23 16:45:17 -06008404 INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
8405 queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
8406 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05008407 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008408
Robert Elliott2946e822015-04-23 09:35:09 -05008409clean7: /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008410 hpsa_free_performant_mode(h);
8411 h->access.set_intr_mask(h, HPSA_INTR_OFF);
8412clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06008413 hpsa_free_sg_chain_blocks(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008414clean5: /* cmd, irq, shost, pci, lu, aer/h */
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05008415 hpsa_free_cmd_pool(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008416clean4: /* irq, shost, pci, lu, aer/h */
Robert Elliottec501a12015-01-23 16:41:40 -06008417 hpsa_free_irqs(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008418clean3: /* shost, pci, lu, aer/h */
8419 scsi_host_put(h->scsi_host);
8420 h->scsi_host = NULL;
8421clean2_5: /* pci, lu, aer/h */
Robert Elliott195f2c62015-04-23 09:33:17 -05008422 hpsa_free_pci_init(h);
Robert Elliott2946e822015-04-23 09:35:09 -05008423clean2: /* lu, aer/h */
Robert Elliott105a3db2015-04-23 09:33:48 -05008424 if (h->lockup_detected) {
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008425 free_percpu(h->lockup_detected);
Robert Elliott105a3db2015-04-23 09:33:48 -05008426 h->lockup_detected = NULL;
8427 }
8428clean1: /* wq/aer/h */
8429 if (h->resubmit_wq) {
8430 destroy_workqueue(h->resubmit_wq);
8431 h->resubmit_wq = NULL;
8432 }
8433 if (h->rescan_ctlr_wq) {
8434 destroy_workqueue(h->rescan_ctlr_wq);
8435 h->rescan_ctlr_wq = NULL;
8436 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008437 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06008438 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008439}
8440
8441static void hpsa_flush_cache(struct ctlr_info *h)
8442{
8443 char *flush_buf;
8444 struct CommandList *c;
Webb Scales25163bd2015-04-23 09:32:00 -05008445 int rc;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008446
Stephen M. Cameron094963d2014-05-29 10:53:18 -05008447 if (unlikely(lockup_detected(h)))
Stephen M. Cameron702890e2013-09-23 13:33:30 -05008448 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008449 flush_buf = kzalloc(4, GFP_KERNEL);
8450 if (!flush_buf)
8451 return;
8452
Stephen Cameron45fcb862015-01-23 16:43:04 -06008453 c = cmd_alloc(h);
Robert Elliottbf43caf2015-04-23 09:33:38 -05008454
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008455 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
8456 RAID_CTLR_LUNID, TYPE_CMD)) {
8457 goto out;
8458 }
Webb Scales25163bd2015-04-23 09:32:00 -05008459 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
8460 PCI_DMA_TODEVICE, NO_TIMEOUT);
8461 if (rc)
8462 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008463 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06008464out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008465 dev_warn(&h->pdev->dev,
8466 "error flushing cache on controller\n");
Stephen Cameron45fcb862015-01-23 16:43:04 -06008467 cmd_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008468 kfree(flush_buf);
8469}
8470
Scott Teelc2adae42015-11-04 15:52:16 -06008471/* Make controller gather fresh report lun data each time we
8472 * send down a report luns request
8473 */
8474static void hpsa_disable_rld_caching(struct ctlr_info *h)
8475{
8476 u32 *options;
8477 struct CommandList *c;
8478 int rc;
8479
8480 /* Don't bother trying to set diag options if locked up */
8481 if (unlikely(h->lockup_detected))
8482 return;
8483
8484 options = kzalloc(sizeof(*options), GFP_KERNEL);
8485 if (!options) {
8486 dev_err(&h->pdev->dev,
8487 "Error: failed to disable rld caching, during alloc.\n");
8488 return;
8489 }
8490
8491 c = cmd_alloc(h);
8492
8493 /* first, get the current diag options settings */
8494 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
8495 RAID_CTLR_LUNID, TYPE_CMD))
8496 goto errout;
8497
8498 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
8499 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
8500 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8501 goto errout;
8502
8503 /* Now, set the bit for disabling the RLD caching */
8504 *options |= HPSA_DIAG_OPTS_DISABLE_RLD_CACHING;
8505
8506 if (fill_cmd(c, BMIC_SET_DIAG_OPTIONS, h, options, 4, 0,
8507 RAID_CTLR_LUNID, TYPE_CMD))
8508 goto errout;
8509
8510 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
8511 PCI_DMA_TODEVICE, NO_TIMEOUT);
8512 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8513 goto errout;
8514
8515 /* Now verify that it got set: */
8516 if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
8517 RAID_CTLR_LUNID, TYPE_CMD))
8518 goto errout;
8519
8520 rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
8521 PCI_DMA_FROMDEVICE, NO_TIMEOUT);
8522 if ((rc != 0) || (c->err_info->CommandStatus != 0))
8523 goto errout;
8524
8525 if (*options && HPSA_DIAG_OPTS_DISABLE_RLD_CACHING)
8526 goto out;
8527
8528errout:
8529 dev_err(&h->pdev->dev,
8530 "Error: failed to disable report lun data caching.\n");
8531out:
8532 cmd_free(h, c);
8533 kfree(options);
8534}
8535
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008536static void hpsa_shutdown(struct pci_dev *pdev)
8537{
8538 struct ctlr_info *h;
8539
8540 h = pci_get_drvdata(pdev);
8541 /* Turn board interrupts off and send the flush cache command
8542 * sendcmd will turn off interrupt, and send the flush...
8543 * To write all data in the battery backed cache to disks
8544 */
8545 hpsa_flush_cache(h);
8546 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Robert Elliott105a3db2015-04-23 09:33:48 -05008547 hpsa_free_irqs(h); /* init_one 4 */
Robert Elliottcc64c812015-04-23 09:33:12 -05008548 hpsa_disable_interrupt_mode(h); /* pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008549}
8550
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008551static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008552{
8553 int i;
8554
Robert Elliott105a3db2015-04-23 09:33:48 -05008555 for (i = 0; i < h->ndevices; i++) {
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008556 kfree(h->dev[i]);
Robert Elliott105a3db2015-04-23 09:33:48 -05008557 h->dev[i] = NULL;
8558 }
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06008559}
8560
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008561static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008562{
8563 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008564 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008565
8566 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05008567 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008568 return;
8569 }
8570 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008571
8572 /* Get rid of any controller monitoring work items */
8573 spin_lock_irqsave(&h->lock, flags);
8574 h->remove_in_progress = 1;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06008575 spin_unlock_irqrestore(&h->lock, flags);
Don Brace6636e7f2015-01-23 16:45:17 -06008576 cancel_delayed_work_sync(&h->monitor_ctlr_work);
8577 cancel_delayed_work_sync(&h->rescan_ctlr_work);
8578 destroy_workqueue(h->rescan_ctlr_wq);
8579 destroy_workqueue(h->resubmit_wq);
Robert Elliottcc64c812015-04-23 09:33:12 -05008580
Don Brace2d041302015-07-18 11:13:15 -05008581 /*
8582 * Call before disabling interrupts.
8583 * scsi_remove_host can trigger I/O operations especially
8584 * when multipath is enabled. There can be SYNCHRONIZE CACHE
8585 * operations which cannot complete and will hang the system.
8586 */
8587 if (h->scsi_host)
8588 scsi_remove_host(h->scsi_host); /* init_one 8 */
Robert Elliott105a3db2015-04-23 09:33:48 -05008589 /* includes hpsa_free_irqs - init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008590 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008591 hpsa_shutdown(pdev);
Robert Elliottcc64c812015-04-23 09:33:12 -05008592
Robert Elliott105a3db2015-04-23 09:33:48 -05008593 hpsa_free_device_info(h); /* scan */
8594
Robert Elliott2946e822015-04-23 09:35:09 -05008595 kfree(h->hba_inquiry_data); /* init_one 10 */
8596 h->hba_inquiry_data = NULL; /* init_one 10 */
Robert Elliott2946e822015-04-23 09:35:09 -05008597 hpsa_free_ioaccel2_sg_chain_blocks(h);
Robert Elliott105a3db2015-04-23 09:33:48 -05008598 hpsa_free_performant_mode(h); /* init_one 7 */
8599 hpsa_free_sg_chain_blocks(h); /* init_one 6 */
8600 hpsa_free_cmd_pool(h); /* init_one 5 */
Scott Teel34592252015-11-04 15:52:09 -06008601 kfree(h->lastlogicals);
Robert Elliott105a3db2015-04-23 09:33:48 -05008602
8603 /* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008604
Robert Elliott2946e822015-04-23 09:35:09 -05008605 scsi_host_put(h->scsi_host); /* init_one 3 */
8606 h->scsi_host = NULL; /* init_one 3 */
8607
Robert Elliott195f2c62015-04-23 09:33:17 -05008608 /* includes hpsa_disable_interrupt_mode - pci_init 2 */
Robert Elliott2946e822015-04-23 09:35:09 -05008609 hpsa_free_pci_init(h); /* init_one 2.5 */
Robert Elliott195f2c62015-04-23 09:33:17 -05008610
Robert Elliott105a3db2015-04-23 09:33:48 -05008611 free_percpu(h->lockup_detected); /* init_one 2 */
8612 h->lockup_detected = NULL; /* init_one 2 */
8613 /* (void) pci_disable_pcie_error_reporting(pdev); */ /* init_one 1 */
8614 kfree(h); /* init_one 1 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008615}
8616
8617static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
8618 __attribute__((unused)) pm_message_t state)
8619{
8620 return -ENOSYS;
8621}
8622
8623static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
8624{
8625 return -ENOSYS;
8626}
8627
8628static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06008629 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008630 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008631 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08008632 .id_table = hpsa_pci_device_id, /* id_table */
8633 .shutdown = hpsa_shutdown,
8634 .suspend = hpsa_suspend,
8635 .resume = hpsa_resume,
8636};
8637
Don Brace303932f2010-02-04 08:42:40 -06008638/* Fill in bucket_map[], given nsgs (the max number of
8639 * scatter gather elements supported) and bucket[],
8640 * which is an array of 8 integers. The bucket[] array
8641 * contains 8 different DMA transfer sizes (in 16
8642 * byte increments) which the controller uses to fetch
8643 * commands. This function fills in bucket_map[], which
8644 * maps a given number of scatter gather elements to one of
8645 * the 8 DMA transfer sizes. The point of it is to allow the
8646 * controller to only do as much DMA as needed to fetch the
8647 * command, with the DMA transfer size encoded in the lower
8648 * bits of the command address.
8649 */
8650static void calc_bucket_map(int bucket[], int num_buckets,
Don Brace2b08b3e2015-01-23 16:41:09 -06008651 int nsgs, int min_blocks, u32 *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06008652{
8653 int i, j, b, size;
8654
Don Brace303932f2010-02-04 08:42:40 -06008655 /* Note, bucket_map must have nsgs+1 entries. */
8656 for (i = 0; i <= nsgs; i++) {
8657 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06008658 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06008659 b = num_buckets; /* Assume the biggest bucket */
8660 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06008661 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06008662 if (bucket[j] >= size) {
8663 b = j;
8664 break;
8665 }
8666 }
8667 /* for a command with i SG entries, use bucket b. */
8668 bucket_map[i] = b;
8669 }
8670}
8671
Robert Elliott105a3db2015-04-23 09:33:48 -05008672/*
8673 * return -ENODEV on err, 0 on success (or no action)
8674 * allocates numerous items that must be freed later
8675 */
Robert Elliottc706a792015-01-23 16:45:01 -06008676static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06008677{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05008678 int i;
8679 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06008680 unsigned long transMethod = CFGTBL_Trans_Performant |
8681 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008682 CFGTBL_Trans_enable_directed_msix |
8683 (trans_support & (CFGTBL_Trans_io_accel1 |
8684 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06008685 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05008686
8687 /* This is a bit complicated. There are 8 registers on
8688 * the controller which we write to to tell it 8 different
8689 * sizes of commands which there may be. It's a way of
8690 * reducing the DMA done to fetch each command. Encoded into
8691 * each command's tag are 3 bits which communicate to the controller
8692 * which of the eight sizes that command fits within. The size of
8693 * each command depends on how many scatter gather entries there are.
8694 * Each SG entry requires 16 bytes. The eight registers are programmed
8695 * with the number of 16-byte blocks a command of that size requires.
8696 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008697 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05008698 * blocks. Note, this only extends to the SG entries contained
8699 * within the command block, and does not extend to chained blocks
8700 * of SG elements. bft[] contains the eight values we write to
8701 * the registers. They are not evenly distributed, but have more
8702 * sizes for small commands, and fewer sizes for larger commands.
8703 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008704 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008705#define MIN_IOACCEL2_BFT_ENTRY 5
8706#define HPSA_IOACCEL2_HEADER_SZ 4
8707 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
8708 13, 14, 15, 16, 17, 18, 19,
8709 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
8710 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
8711 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
8712 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
8713 16 * MIN_IOACCEL2_BFT_ENTRY);
8714 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008715 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06008716 /* 5 = 1 s/g entry or 4k
8717 * 6 = 2 s/g entry or 8k
8718 * 8 = 4 s/g entry or 16k
8719 * 10 = 6 s/g entry or 24k
8720 */
Don Brace303932f2010-02-04 08:42:40 -06008721
Stephen M. Cameronb3a52e72014-05-29 10:53:23 -05008722 /* If the controller supports either ioaccel method then
8723 * we can also use the RAID stack submit path that does not
8724 * perform the superfluous readl() after each command submission.
8725 */
8726 if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
8727 access = SA5_performant_access_no_read;
8728
Don Brace303932f2010-02-04 08:42:40 -06008729 /* Controller spec: zero out this buffer. */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008730 for (i = 0; i < h->nreply_queues; i++)
8731 memset(h->reply_queue[i].head, 0, h->reply_queue_size);
Don Brace303932f2010-02-04 08:42:40 -06008732
Stephen M. Camerond66ae082012-01-19 14:00:48 -06008733 bft[7] = SG_ENTRIES_IN_CMD + 4;
8734 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06008735 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06008736 for (i = 0; i < 8; i++)
8737 writel(bft[i], &h->transtable->BlockFetch[i]);
8738
8739 /* size of controller ring buffer */
8740 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05008741 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06008742 writel(0, &h->transtable->RepQCtrAddrLow32);
8743 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05008744
8745 for (i = 0; i < h->nreply_queues; i++) {
8746 writel(0, &h->transtable->RepQAddr[i].upper);
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008747 writel(h->reply_queue[i].busaddr,
Matt Gates254f7962012-05-01 11:43:06 -05008748 &h->transtable->RepQAddr[i].lower);
8749 }
8750
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008751 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06008752 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
8753 /*
8754 * enable outbound interrupt coalescing in accelerator mode;
8755 */
8756 if (trans_support & CFGTBL_Trans_io_accel1) {
8757 access = SA5_ioaccel_mode1_access;
8758 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8759 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Scott Teelc3497752014-02-18 13:56:34 -06008760 } else {
8761 if (trans_support & CFGTBL_Trans_io_accel2) {
8762 access = SA5_ioaccel_mode2_access;
8763 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8764 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
8765 }
Matt Gatese1f7de02014-02-18 13:55:17 -06008766 }
Don Brace303932f2010-02-04 08:42:40 -06008767 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06008768 if (hpsa_wait_for_mode_change_ack(h)) {
8769 dev_err(&h->pdev->dev,
8770 "performant mode problem - doorbell timeout\n");
8771 return -ENODEV;
8772 }
Don Brace303932f2010-02-04 08:42:40 -06008773 register_value = readl(&(h->cfgtable->TransportActive));
8774 if (!(register_value & CFGTBL_Trans_Performant)) {
Stephen Cameron050f7142015-01-23 16:42:22 -06008775 dev_err(&h->pdev->dev,
8776 "performant mode problem - transport not active\n");
Robert Elliottc706a792015-01-23 16:45:01 -06008777 return -ENODEV;
Don Brace303932f2010-02-04 08:42:40 -06008778 }
Stephen M. Cameron960a30e72011-02-15 15:33:03 -06008779 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06008780 h->access = access;
8781 h->transMethod = transMethod;
8782
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008783 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
8784 (trans_support & CFGTBL_Trans_io_accel2)))
Robert Elliottc706a792015-01-23 16:45:01 -06008785 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06008786
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008787 if (trans_support & CFGTBL_Trans_io_accel1) {
8788 /* Set up I/O accelerator mode */
8789 for (i = 0; i < h->nreply_queues; i++) {
8790 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
8791 h->reply_queue[i].current_entry =
8792 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
8793 }
8794 bft[7] = h->ioaccel_maxsg + 8;
8795 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
8796 h->ioaccel1_blockFetchTable);
8797
8798 /* initialize all reply queue entries to unused */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05008799 for (i = 0; i < h->nreply_queues; i++)
8800 memset(h->reply_queue[i].head,
8801 (u8) IOACCEL_MODE1_REPLY_UNUSED,
8802 h->reply_queue_size);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008803
8804 /* set all the constant fields in the accelerator command
8805 * frames once at init time to save CPU cycles later.
8806 */
8807 for (i = 0; i < h->nr_cmds; i++) {
8808 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
8809
8810 cp->function = IOACCEL1_FUNCTION_SCSIIO;
8811 cp->err_info = (u32) (h->errinfo_pool_dhandle +
8812 (i * sizeof(struct ErrorInfo)));
8813 cp->err_info_len = sizeof(struct ErrorInfo);
8814 cp->sgl_offset = IOACCEL1_SGLOFFSET;
Don Brace2b08b3e2015-01-23 16:41:09 -06008815 cp->host_context_flags =
8816 cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008817 cp->timeout_sec = 0;
8818 cp->ReplyQueue = 0;
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06008819 cp->tag =
Don Bracef2405db2015-01-23 16:43:09 -06008820 cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06008821 cp->host_addr =
8822 cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008823 (i * sizeof(struct io_accel1_cmd)));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008824 }
8825 } else if (trans_support & CFGTBL_Trans_io_accel2) {
8826 u64 cfg_offset, cfg_base_addr_index;
8827 u32 bft2_offset, cfg_base_addr;
8828 int rc;
8829
8830 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
8831 &cfg_base_addr_index, &cfg_offset);
8832 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
8833 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
8834 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
8835 4, h->ioaccel2_blockFetchTable);
8836 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
8837 BUILD_BUG_ON(offsetof(struct CfgTable,
8838 io_accel_request_size_offset) != 0xb8);
8839 h->ioaccel2_bft2_regs =
8840 remap_pci_mem(pci_resource_start(h->pdev,
8841 cfg_base_addr_index) +
8842 cfg_offset + bft2_offset,
8843 ARRAY_SIZE(bft2) *
8844 sizeof(*h->ioaccel2_bft2_regs));
8845 for (i = 0; i < ARRAY_SIZE(bft2); i++)
8846 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06008847 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06008848 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Robert Elliottc706a792015-01-23 16:45:01 -06008849 if (hpsa_wait_for_mode_change_ack(h)) {
8850 dev_err(&h->pdev->dev,
8851 "performant mode problem - enabling ioaccel mode\n");
8852 return -ENODEV;
8853 }
8854 return 0;
Matt Gatese1f7de02014-02-18 13:55:17 -06008855}
8856
Robert Elliott1fb7c982015-04-23 09:33:22 -05008857/* Free ioaccel1 mode command blocks and block fetch table */
8858static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
8859{
Robert Elliott105a3db2015-04-23 09:33:48 -05008860 if (h->ioaccel_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05008861 pci_free_consistent(h->pdev,
8862 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
8863 h->ioaccel_cmd_pool,
8864 h->ioaccel_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05008865 h->ioaccel_cmd_pool = NULL;
8866 h->ioaccel_cmd_pool_dhandle = 0;
8867 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05008868 kfree(h->ioaccel1_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05008869 h->ioaccel1_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05008870}
8871
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008872/* Allocate ioaccel1 mode command blocks and block fetch table */
8873static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
Matt Gatese1f7de02014-02-18 13:55:17 -06008874{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06008875 h->ioaccel_maxsg =
8876 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8877 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
8878 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
8879
Matt Gatese1f7de02014-02-18 13:55:17 -06008880 /* Command structures must be aligned on a 128-byte boundary
8881 * because the 7 lower bits of the address are used by the
8882 * hardware.
8883 */
Matt Gatese1f7de02014-02-18 13:55:17 -06008884 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
8885 IOACCEL1_COMMANDLIST_ALIGNMENT);
8886 h->ioaccel_cmd_pool =
8887 pci_alloc_consistent(h->pdev,
8888 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
8889 &(h->ioaccel_cmd_pool_dhandle));
8890
8891 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06008892 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06008893 sizeof(u32)), GFP_KERNEL);
8894
8895 if ((h->ioaccel_cmd_pool == NULL) ||
8896 (h->ioaccel1_blockFetchTable == NULL))
8897 goto clean_up;
8898
8899 memset(h->ioaccel_cmd_pool, 0,
8900 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
8901 return 0;
8902
8903clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05008904 hpsa_free_ioaccel1_cmd_and_bft(h);
Robert Elliott2dd02d72015-04-23 09:33:43 -05008905 return -ENOMEM;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05008906}
8907
Robert Elliott1fb7c982015-04-23 09:33:22 -05008908/* Free ioaccel2 mode command blocks and block fetch table */
8909static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
8910{
Webb Scalesd9a729f2015-04-23 09:33:27 -05008911 hpsa_free_ioaccel2_sg_chain_blocks(h);
8912
Robert Elliott105a3db2015-04-23 09:33:48 -05008913 if (h->ioaccel2_cmd_pool) {
Robert Elliott1fb7c982015-04-23 09:33:22 -05008914 pci_free_consistent(h->pdev,
8915 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
8916 h->ioaccel2_cmd_pool,
8917 h->ioaccel2_cmd_pool_dhandle);
Robert Elliott105a3db2015-04-23 09:33:48 -05008918 h->ioaccel2_cmd_pool = NULL;
8919 h->ioaccel2_cmd_pool_dhandle = 0;
8920 }
Robert Elliott1fb7c982015-04-23 09:33:22 -05008921 kfree(h->ioaccel2_blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05008922 h->ioaccel2_blockFetchTable = NULL;
Robert Elliott1fb7c982015-04-23 09:33:22 -05008923}
8924
Robert Elliottd37ffbe2015-04-23 09:32:27 -05008925/* Allocate ioaccel2 mode command blocks and block fetch table */
8926static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06008927{
Webb Scalesd9a729f2015-04-23 09:33:27 -05008928 int rc;
8929
Stephen M. Cameronaca90122014-02-18 13:56:14 -06008930 /* Allocate ioaccel2 mode command blocks and block fetch table */
8931
8932 h->ioaccel_maxsg =
8933 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8934 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
8935 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
8936
Stephen M. Cameronaca90122014-02-18 13:56:14 -06008937 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
8938 IOACCEL2_COMMANDLIST_ALIGNMENT);
8939 h->ioaccel2_cmd_pool =
8940 pci_alloc_consistent(h->pdev,
8941 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
8942 &(h->ioaccel2_cmd_pool_dhandle));
8943
8944 h->ioaccel2_blockFetchTable =
8945 kmalloc(((h->ioaccel_maxsg + 1) *
8946 sizeof(u32)), GFP_KERNEL);
8947
8948 if ((h->ioaccel2_cmd_pool == NULL) ||
Webb Scalesd9a729f2015-04-23 09:33:27 -05008949 (h->ioaccel2_blockFetchTable == NULL)) {
8950 rc = -ENOMEM;
8951 goto clean_up;
8952 }
8953
8954 rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
8955 if (rc)
Stephen M. Cameronaca90122014-02-18 13:56:14 -06008956 goto clean_up;
8957
8958 memset(h->ioaccel2_cmd_pool, 0,
8959 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
8960 return 0;
8961
8962clean_up:
Robert Elliott1fb7c982015-04-23 09:33:22 -05008963 hpsa_free_ioaccel2_cmd_and_bft(h);
Webb Scalesd9a729f2015-04-23 09:33:27 -05008964 return rc;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06008965}
8966
Robert Elliott105a3db2015-04-23 09:33:48 -05008967/* Free items allocated by hpsa_put_ctlr_into_performant_mode */
8968static void hpsa_free_performant_mode(struct ctlr_info *h)
8969{
8970 kfree(h->blockFetchTable);
8971 h->blockFetchTable = NULL;
8972 hpsa_free_reply_queues(h);
8973 hpsa_free_ioaccel1_cmd_and_bft(h);
8974 hpsa_free_ioaccel2_cmd_and_bft(h);
8975}
8976
8977/* return -ENODEV on error, 0 on success (or no action)
8978 * allocates numerous items that must be freed later
8979 */
8980static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05008981{
8982 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06008983 unsigned long transMethod = CFGTBL_Trans_Performant |
8984 CFGTBL_Trans_use_short_tags;
Robert Elliott105a3db2015-04-23 09:33:48 -05008985 int i, rc;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05008986
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06008987 if (hpsa_simple_mode)
Robert Elliott105a3db2015-04-23 09:33:48 -05008988 return 0;
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06008989
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05008990 trans_support = readl(&(h->cfgtable->TransportSupport));
8991 if (!(trans_support & PERFORMANT_MODE))
Robert Elliott105a3db2015-04-23 09:33:48 -05008992 return 0;
scameron@beardog.cce.hp.com67c99a72014-04-14 14:01:09 -05008993
Matt Gatese1f7de02014-02-18 13:55:17 -06008994 /* Check for I/O accelerator mode support */
8995 if (trans_support & CFGTBL_Trans_io_accel1) {
8996 transMethod |= CFGTBL_Trans_io_accel1 |
8997 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05008998 rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
8999 if (rc)
9000 return rc;
9001 } else if (trans_support & CFGTBL_Trans_io_accel2) {
9002 transMethod |= CFGTBL_Trans_io_accel2 |
Stephen M. Cameronaca90122014-02-18 13:56:14 -06009003 CFGTBL_Trans_enable_directed_msix;
Robert Elliott105a3db2015-04-23 09:33:48 -05009004 rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
9005 if (rc)
9006 return rc;
Matt Gatese1f7de02014-02-18 13:55:17 -06009007 }
9008
Hannes Reineckeeee0f032014-01-15 13:30:53 +01009009 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05009010 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009011 /* Performant mode ring buffer and supporting data structures */
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009012 h->reply_queue_size = h->max_commands * sizeof(u64);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009013
Matt Gates254f7962012-05-01 11:43:06 -05009014 for (i = 0; i < h->nreply_queues; i++) {
Stephen M. Cameron072b0512014-05-29 10:53:07 -05009015 h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
9016 h->reply_queue_size,
9017 &(h->reply_queue[i].busaddr));
Robert Elliott105a3db2015-04-23 09:33:48 -05009018 if (!h->reply_queue[i].head) {
9019 rc = -ENOMEM;
9020 goto clean1; /* rq, ioaccel */
9021 }
Matt Gates254f7962012-05-01 11:43:06 -05009022 h->reply_queue[i].size = h->max_commands;
9023 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
9024 h->reply_queue[i].current_entry = 0;
9025 }
9026
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009027 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06009028 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009029 sizeof(u32)), GFP_KERNEL);
Robert Elliott105a3db2015-04-23 09:33:48 -05009030 if (!h->blockFetchTable) {
9031 rc = -ENOMEM;
9032 goto clean1; /* rq, ioaccel */
9033 }
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05009034
Robert Elliott105a3db2015-04-23 09:33:48 -05009035 rc = hpsa_enter_performant_mode(h, trans_support);
9036 if (rc)
9037 goto clean2; /* bft, rq, ioaccel */
9038 return 0;
Don Brace303932f2010-02-04 08:42:40 -06009039
Robert Elliott105a3db2015-04-23 09:33:48 -05009040clean2: /* bft, rq, ioaccel */
Don Brace303932f2010-02-04 08:42:40 -06009041 kfree(h->blockFetchTable);
Robert Elliott105a3db2015-04-23 09:33:48 -05009042 h->blockFetchTable = NULL;
9043clean1: /* rq, ioaccel */
9044 hpsa_free_reply_queues(h);
9045 hpsa_free_ioaccel1_cmd_and_bft(h);
9046 hpsa_free_ioaccel2_cmd_and_bft(h);
9047 return rc;
Don Brace303932f2010-02-04 08:42:40 -06009048}
9049
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009050static int is_accelerated_cmd(struct CommandList *c)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009051{
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009052 return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
9053}
9054
9055static void hpsa_drain_accel_commands(struct ctlr_info *h)
9056{
9057 struct CommandList *c = NULL;
Don Bracef2405db2015-01-23 16:43:09 -06009058 int i, accel_cmds_out;
Webb Scales281a7fd2015-01-23 16:43:35 -06009059 int refcount;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009060
Don Bracef2405db2015-01-23 16:43:09 -06009061 do { /* wait for all outstanding ioaccel commands to drain out */
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009062 accel_cmds_out = 0;
Don Bracef2405db2015-01-23 16:43:09 -06009063 for (i = 0; i < h->nr_cmds; i++) {
Don Bracef2405db2015-01-23 16:43:09 -06009064 c = h->cmd_pool + i;
Webb Scales281a7fd2015-01-23 16:43:35 -06009065 refcount = atomic_inc_return(&c->refcount);
9066 if (refcount > 1) /* Command is allocated */
9067 accel_cmds_out += is_accelerated_cmd(c);
9068 cmd_free(h, c);
Don Bracef2405db2015-01-23 16:43:09 -06009069 }
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06009070 if (accel_cmds_out <= 0)
Webb Scales281a7fd2015-01-23 16:43:35 -06009071 break;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06009072 msleep(100);
9073 } while (1);
9074}
9075
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009076/*
9077 * This is it. Register the PCI driver information for the cards we control
9078 * the OS will call our registered routines when it finds one of our cards.
9079 */
9080static int __init hpsa_init(void)
9081{
Mike Miller31468402010-02-25 14:03:12 -06009082 return pci_register_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009083}
9084
9085static void __exit hpsa_cleanup(void)
9086{
9087 pci_unregister_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009088}
9089
Matt Gatese1f7de02014-02-18 13:55:17 -06009090static void __attribute__((unused)) verify_offsets(void)
9091{
9092#define VERIFY_OFFSET(member, offset) \
Scott Teeldd0e19f2014-02-18 13:57:31 -06009093 BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
9094
9095 VERIFY_OFFSET(structure_size, 0);
9096 VERIFY_OFFSET(volume_blk_size, 4);
9097 VERIFY_OFFSET(volume_blk_cnt, 8);
9098 VERIFY_OFFSET(phys_blk_shift, 16);
9099 VERIFY_OFFSET(parity_rotation_shift, 17);
9100 VERIFY_OFFSET(strip_size, 18);
9101 VERIFY_OFFSET(disk_starting_blk, 20);
9102 VERIFY_OFFSET(disk_blk_cnt, 28);
9103 VERIFY_OFFSET(data_disks_per_row, 36);
9104 VERIFY_OFFSET(metadata_disks_per_row, 38);
9105 VERIFY_OFFSET(row_cnt, 40);
9106 VERIFY_OFFSET(layout_map_count, 42);
9107 VERIFY_OFFSET(flags, 44);
9108 VERIFY_OFFSET(dekindex, 46);
9109 /* VERIFY_OFFSET(reserved, 48 */
9110 VERIFY_OFFSET(data, 64);
9111
9112#undef VERIFY_OFFSET
9113
9114#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06009115 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
9116
9117 VERIFY_OFFSET(IU_type, 0);
9118 VERIFY_OFFSET(direction, 1);
9119 VERIFY_OFFSET(reply_queue, 2);
9120 /* VERIFY_OFFSET(reserved1, 3); */
9121 VERIFY_OFFSET(scsi_nexus, 4);
9122 VERIFY_OFFSET(Tag, 8);
9123 VERIFY_OFFSET(cdb, 16);
9124 VERIFY_OFFSET(cciss_lun, 32);
9125 VERIFY_OFFSET(data_len, 40);
9126 VERIFY_OFFSET(cmd_priority_task_attr, 44);
9127 VERIFY_OFFSET(sg_count, 45);
9128 /* VERIFY_OFFSET(reserved3 */
9129 VERIFY_OFFSET(err_ptr, 48);
9130 VERIFY_OFFSET(err_len, 56);
9131 /* VERIFY_OFFSET(reserved4 */
9132 VERIFY_OFFSET(sg, 64);
9133
9134#undef VERIFY_OFFSET
9135
9136#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06009137 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
9138
9139 VERIFY_OFFSET(dev_handle, 0x00);
9140 VERIFY_OFFSET(reserved1, 0x02);
9141 VERIFY_OFFSET(function, 0x03);
9142 VERIFY_OFFSET(reserved2, 0x04);
9143 VERIFY_OFFSET(err_info, 0x0C);
9144 VERIFY_OFFSET(reserved3, 0x10);
9145 VERIFY_OFFSET(err_info_len, 0x12);
9146 VERIFY_OFFSET(reserved4, 0x13);
9147 VERIFY_OFFSET(sgl_offset, 0x14);
9148 VERIFY_OFFSET(reserved5, 0x15);
9149 VERIFY_OFFSET(transfer_len, 0x1C);
9150 VERIFY_OFFSET(reserved6, 0x20);
9151 VERIFY_OFFSET(io_flags, 0x24);
9152 VERIFY_OFFSET(reserved7, 0x26);
9153 VERIFY_OFFSET(LUN, 0x34);
9154 VERIFY_OFFSET(control, 0x3C);
9155 VERIFY_OFFSET(CDB, 0x40);
9156 VERIFY_OFFSET(reserved8, 0x50);
9157 VERIFY_OFFSET(host_context_flags, 0x60);
9158 VERIFY_OFFSET(timeout_sec, 0x62);
9159 VERIFY_OFFSET(ReplyQueue, 0x64);
9160 VERIFY_OFFSET(reserved9, 0x65);
Stephen M. Cameron50a0dec2014-11-14 17:26:59 -06009161 VERIFY_OFFSET(tag, 0x68);
Matt Gatese1f7de02014-02-18 13:55:17 -06009162 VERIFY_OFFSET(host_addr, 0x70);
9163 VERIFY_OFFSET(CISS_LUN, 0x78);
9164 VERIFY_OFFSET(SG, 0x78 + 8);
9165#undef VERIFY_OFFSET
9166}
9167
Stephen M. Cameronedd16362009-12-08 14:09:11 -08009168module_init(hpsa_init);
9169module_exit(hpsa_cleanup);