]>
Commit | Line | Data |
---|---|---|
edd16368 SC |
1 | /* |
2 | * Disk Array driver for HP Smart Array SAS controllers | |
3 | * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; version 2 of the License. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | |
12 | * NON INFRINGEMENT. See the GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | * | |
18 | * Questions/Comments/Bugfixes to iss_storagedev@hp.com | |
19 | * | |
20 | */ | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/interrupt.h> | |
24 | #include <linux/types.h> | |
25 | #include <linux/pci.h> | |
e5a44df8 | 26 | #include <linux/pci-aspm.h> |
edd16368 SC |
27 | #include <linux/kernel.h> |
28 | #include <linux/slab.h> | |
29 | #include <linux/delay.h> | |
30 | #include <linux/fs.h> | |
31 | #include <linux/timer.h> | |
edd16368 SC |
32 | #include <linux/init.h> |
33 | #include <linux/spinlock.h> | |
edd16368 SC |
34 | #include <linux/compat.h> |
35 | #include <linux/blktrace_api.h> | |
36 | #include <linux/uaccess.h> | |
37 | #include <linux/io.h> | |
38 | #include <linux/dma-mapping.h> | |
39 | #include <linux/completion.h> | |
40 | #include <linux/moduleparam.h> | |
41 | #include <scsi/scsi.h> | |
42 | #include <scsi/scsi_cmnd.h> | |
43 | #include <scsi/scsi_device.h> | |
44 | #include <scsi/scsi_host.h> | |
667e23d4 | 45 | #include <scsi/scsi_tcq.h> |
edd16368 SC |
46 | #include <linux/cciss_ioctl.h> |
47 | #include <linux/string.h> | |
48 | #include <linux/bitmap.h> | |
60063497 | 49 | #include <linux/atomic.h> |
edd16368 | 50 | #include <linux/kthread.h> |
a0c12413 | 51 | #include <linux/jiffies.h> |
283b4a9b | 52 | #include <asm/div64.h> |
edd16368 SC |
53 | #include "hpsa_cmd.h" |
54 | #include "hpsa.h" | |
55 | ||
56 | /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */ | |
e481cce8 | 57 | #define HPSA_DRIVER_VERSION "3.4.0-1" |
edd16368 | 58 | #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")" |
f79cfec6 | 59 | #define HPSA "hpsa" |
edd16368 SC |
60 | |
61 | /* How long to wait (in milliseconds) for board to go into simple mode */ | |
62 | #define MAX_CONFIG_WAIT 30000 | |
63 | #define MAX_IOCTL_CONFIG_WAIT 1000 | |
64 | ||
65 | /*define how many times we will try a command because of bus resets */ | |
66 | #define MAX_CMD_RETRIES 3 | |
67 | ||
68 | /* Embedded module documentation macros - see modules.h */ | |
69 | MODULE_AUTHOR("Hewlett-Packard Company"); | |
70 | MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \ | |
71 | HPSA_DRIVER_VERSION); | |
72 | MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers"); | |
73 | MODULE_VERSION(HPSA_DRIVER_VERSION); | |
74 | MODULE_LICENSE("GPL"); | |
75 | ||
76 | static int hpsa_allow_any; | |
77 | module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR); | |
78 | MODULE_PARM_DESC(hpsa_allow_any, | |
79 | "Allow hpsa driver to access unknown HP Smart Array hardware"); | |
02ec19c8 SC |
80 | static int hpsa_simple_mode; |
81 | module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR); | |
82 | MODULE_PARM_DESC(hpsa_simple_mode, | |
83 | "Use 'simple mode' rather than 'performant mode'"); | |
edd16368 SC |
84 | |
85 | /* define the PCI info for the cards we can control */ | |
86 | static const struct pci_device_id hpsa_pci_device_id[] = { | |
edd16368 SC |
87 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241}, |
88 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243}, | |
89 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245}, | |
90 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247}, | |
91 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249}, | |
163dbcd8 MM |
92 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A}, |
93 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B}, | |
f8b01eb9 | 94 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233}, |
9143a961 | 95 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350}, |
96 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351}, | |
97 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352}, | |
98 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353}, | |
99 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354}, | |
100 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355}, | |
101 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356}, | |
fe0c9610 MM |
102 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1921}, |
103 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1922}, | |
104 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1923}, | |
105 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1924}, | |
106 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1925}, | |
107 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1926}, | |
108 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1928}, | |
97b9f53d MM |
109 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1929}, |
110 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BD}, | |
111 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BE}, | |
112 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BF}, | |
113 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C0}, | |
114 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C1}, | |
115 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C2}, | |
116 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C3}, | |
117 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C4}, | |
118 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C5}, | |
119 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C7}, | |
120 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C8}, | |
121 | {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C9}, | |
7c03b870 | 122 | {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, |
6798cc0a | 123 | PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0}, |
edd16368 SC |
124 | {0,} |
125 | }; | |
126 | ||
127 | MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id); | |
128 | ||
129 | /* board_id = Subsystem Device ID & Vendor ID | |
130 | * product = Marketing Name for the board | |
131 | * access = Address of the struct of function pointers | |
132 | */ | |
133 | static struct board_type products[] = { | |
edd16368 SC |
134 | {0x3241103C, "Smart Array P212", &SA5_access}, |
135 | {0x3243103C, "Smart Array P410", &SA5_access}, | |
136 | {0x3245103C, "Smart Array P410i", &SA5_access}, | |
137 | {0x3247103C, "Smart Array P411", &SA5_access}, | |
138 | {0x3249103C, "Smart Array P812", &SA5_access}, | |
163dbcd8 MM |
139 | {0x324A103C, "Smart Array P712m", &SA5_access}, |
140 | {0x324B103C, "Smart Array P711m", &SA5_access}, | |
fe0c9610 MM |
141 | {0x3350103C, "Smart Array P222", &SA5_access}, |
142 | {0x3351103C, "Smart Array P420", &SA5_access}, | |
143 | {0x3352103C, "Smart Array P421", &SA5_access}, | |
144 | {0x3353103C, "Smart Array P822", &SA5_access}, | |
145 | {0x3354103C, "Smart Array P420i", &SA5_access}, | |
146 | {0x3355103C, "Smart Array P220i", &SA5_access}, | |
147 | {0x3356103C, "Smart Array P721m", &SA5_access}, | |
1fd6c8e3 MM |
148 | {0x1921103C, "Smart Array P830i", &SA5_access}, |
149 | {0x1922103C, "Smart Array P430", &SA5_access}, | |
150 | {0x1923103C, "Smart Array P431", &SA5_access}, | |
151 | {0x1924103C, "Smart Array P830", &SA5_access}, | |
152 | {0x1926103C, "Smart Array P731m", &SA5_access}, | |
153 | {0x1928103C, "Smart Array P230i", &SA5_access}, | |
154 | {0x1929103C, "Smart Array P530", &SA5_access}, | |
97b9f53d MM |
155 | {0x21BD103C, "Smart Array", &SA5_access}, |
156 | {0x21BE103C, "Smart Array", &SA5_access}, | |
157 | {0x21BF103C, "Smart Array", &SA5_access}, | |
158 | {0x21C0103C, "Smart Array", &SA5_access}, | |
159 | {0x21C1103C, "Smart Array", &SA5_access}, | |
160 | {0x21C2103C, "Smart Array", &SA5_access}, | |
161 | {0x21C3103C, "Smart Array", &SA5_access}, | |
162 | {0x21C4103C, "Smart Array", &SA5_access}, | |
163 | {0x21C5103C, "Smart Array", &SA5_access}, | |
164 | {0x21C7103C, "Smart Array", &SA5_access}, | |
165 | {0x21C8103C, "Smart Array", &SA5_access}, | |
166 | {0x21C9103C, "Smart Array", &SA5_access}, | |
edd16368 SC |
167 | {0xFFFF103C, "Unknown Smart Array", &SA5_access}, |
168 | }; | |
169 | ||
170 | static int number_of_controllers; | |
171 | ||
10f66018 SC |
172 | static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id); |
173 | static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id); | |
edd16368 SC |
174 | static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg); |
175 | static void start_io(struct ctlr_info *h); | |
176 | ||
177 | #ifdef CONFIG_COMPAT | |
178 | static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg); | |
179 | #endif | |
180 | ||
181 | static void cmd_free(struct ctlr_info *h, struct CommandList *c); | |
182 | static void cmd_special_free(struct ctlr_info *h, struct CommandList *c); | |
183 | static struct CommandList *cmd_alloc(struct ctlr_info *h); | |
184 | static struct CommandList *cmd_special_alloc(struct ctlr_info *h); | |
a2dac136 | 185 | static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h, |
01a02ffc | 186 | void *buff, size_t size, u8 page_code, unsigned char *scsi3addr, |
edd16368 SC |
187 | int cmd_type); |
188 | ||
f281233d | 189 | static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd); |
a08a8471 SC |
190 | static void hpsa_scan_start(struct Scsi_Host *); |
191 | static int hpsa_scan_finished(struct Scsi_Host *sh, | |
192 | unsigned long elapsed_time); | |
667e23d4 SC |
193 | static int hpsa_change_queue_depth(struct scsi_device *sdev, |
194 | int qdepth, int reason); | |
edd16368 SC |
195 | |
196 | static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd); | |
75167d2c | 197 | static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd); |
edd16368 SC |
198 | static int hpsa_slave_alloc(struct scsi_device *sdev); |
199 | static void hpsa_slave_destroy(struct scsi_device *sdev); | |
200 | ||
edd16368 | 201 | static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno); |
edd16368 SC |
202 | static int check_for_unit_attention(struct ctlr_info *h, |
203 | struct CommandList *c); | |
204 | static void check_ioctl_unit_attention(struct ctlr_info *h, | |
205 | struct CommandList *c); | |
303932fd DB |
206 | /* performant mode helper functions */ |
207 | static void calc_bucket_map(int *bucket, int num_buckets, | |
e1f7de0c | 208 | int nsgs, int min_blocks, int *bucket_map); |
6f039790 | 209 | static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h); |
254f796b | 210 | static inline u32 next_command(struct ctlr_info *h, u8 q); |
6f039790 GKH |
211 | static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr, |
212 | u32 *cfg_base_addr, u64 *cfg_base_addr_index, | |
213 | u64 *cfg_offset); | |
214 | static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev, | |
215 | unsigned long *memory_bar); | |
216 | static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id); | |
217 | static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr, | |
218 | int wait_for_ready); | |
75167d2c | 219 | static inline void finish_cmd(struct CommandList *c); |
283b4a9b | 220 | static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h); |
fe5389c8 SC |
221 | #define BOARD_NOT_READY 0 |
222 | #define BOARD_READY 1 | |
76438d08 SC |
223 | static void hpsa_drain_commands(struct ctlr_info *h); |
224 | static void hpsa_flush_cache(struct ctlr_info *h); | |
edd16368 | 225 | |
edd16368 SC |
226 | static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev) |
227 | { | |
228 | unsigned long *priv = shost_priv(sdev->host); | |
229 | return (struct ctlr_info *) *priv; | |
230 | } | |
231 | ||
a23513e8 SC |
232 | static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh) |
233 | { | |
234 | unsigned long *priv = shost_priv(sh); | |
235 | return (struct ctlr_info *) *priv; | |
236 | } | |
237 | ||
edd16368 SC |
238 | static int check_for_unit_attention(struct ctlr_info *h, |
239 | struct CommandList *c) | |
240 | { | |
241 | if (c->err_info->SenseInfo[2] != UNIT_ATTENTION) | |
242 | return 0; | |
243 | ||
244 | switch (c->err_info->SenseInfo[12]) { | |
245 | case STATE_CHANGED: | |
f79cfec6 | 246 | dev_warn(&h->pdev->dev, HPSA "%d: a state change " |
edd16368 SC |
247 | "detected, command retried\n", h->ctlr); |
248 | break; | |
249 | case LUN_FAILED: | |
f79cfec6 | 250 | dev_warn(&h->pdev->dev, HPSA "%d: LUN failure " |
edd16368 SC |
251 | "detected, action required\n", h->ctlr); |
252 | break; | |
253 | case REPORT_LUNS_CHANGED: | |
f79cfec6 | 254 | dev_warn(&h->pdev->dev, HPSA "%d: report LUN data " |
31468401 | 255 | "changed, action required\n", h->ctlr); |
edd16368 | 256 | /* |
4f4eb9f1 ST |
257 | * Note: this REPORT_LUNS_CHANGED condition only occurs on the external |
258 | * target (array) devices. | |
edd16368 SC |
259 | */ |
260 | break; | |
261 | case POWER_OR_RESET: | |
f79cfec6 | 262 | dev_warn(&h->pdev->dev, HPSA "%d: a power on " |
edd16368 SC |
263 | "or device reset detected\n", h->ctlr); |
264 | break; | |
265 | case UNIT_ATTENTION_CLEARED: | |
f79cfec6 | 266 | dev_warn(&h->pdev->dev, HPSA "%d: unit attention " |
edd16368 SC |
267 | "cleared by another initiator\n", h->ctlr); |
268 | break; | |
269 | default: | |
f79cfec6 | 270 | dev_warn(&h->pdev->dev, HPSA "%d: unknown " |
edd16368 SC |
271 | "unit attention detected\n", h->ctlr); |
272 | break; | |
273 | } | |
274 | return 1; | |
275 | } | |
276 | ||
852af20a MB |
277 | static int check_for_busy(struct ctlr_info *h, struct CommandList *c) |
278 | { | |
279 | if (c->err_info->CommandStatus != CMD_TARGET_STATUS || | |
280 | (c->err_info->ScsiStatus != SAM_STAT_BUSY && | |
281 | c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL)) | |
282 | return 0; | |
283 | dev_warn(&h->pdev->dev, HPSA "device busy"); | |
284 | return 1; | |
285 | } | |
286 | ||
edd16368 SC |
287 | static ssize_t host_store_rescan(struct device *dev, |
288 | struct device_attribute *attr, | |
289 | const char *buf, size_t count) | |
290 | { | |
291 | struct ctlr_info *h; | |
292 | struct Scsi_Host *shost = class_to_shost(dev); | |
a23513e8 | 293 | h = shost_to_hba(shost); |
31468401 | 294 | hpsa_scan_start(h->scsi_host); |
edd16368 SC |
295 | return count; |
296 | } | |
297 | ||
d28ce020 SC |
298 | static ssize_t host_show_firmware_revision(struct device *dev, |
299 | struct device_attribute *attr, char *buf) | |
300 | { | |
301 | struct ctlr_info *h; | |
302 | struct Scsi_Host *shost = class_to_shost(dev); | |
303 | unsigned char *fwrev; | |
304 | ||
305 | h = shost_to_hba(shost); | |
306 | if (!h->hba_inquiry_data) | |
307 | return 0; | |
308 | fwrev = &h->hba_inquiry_data[32]; | |
309 | return snprintf(buf, 20, "%c%c%c%c\n", | |
310 | fwrev[0], fwrev[1], fwrev[2], fwrev[3]); | |
311 | } | |
312 | ||
94a13649 SC |
313 | static ssize_t host_show_commands_outstanding(struct device *dev, |
314 | struct device_attribute *attr, char *buf) | |
315 | { | |
316 | struct Scsi_Host *shost = class_to_shost(dev); | |
317 | struct ctlr_info *h = shost_to_hba(shost); | |
318 | ||
319 | return snprintf(buf, 20, "%d\n", h->commands_outstanding); | |
320 | } | |
321 | ||
745a7a25 SC |
322 | static ssize_t host_show_transport_mode(struct device *dev, |
323 | struct device_attribute *attr, char *buf) | |
324 | { | |
325 | struct ctlr_info *h; | |
326 | struct Scsi_Host *shost = class_to_shost(dev); | |
327 | ||
328 | h = shost_to_hba(shost); | |
329 | return snprintf(buf, 20, "%s\n", | |
960a30e7 | 330 | h->transMethod & CFGTBL_Trans_Performant ? |
745a7a25 SC |
331 | "performant" : "simple"); |
332 | } | |
333 | ||
46380786 | 334 | /* List of controllers which cannot be hard reset on kexec with reset_devices */ |
941b1cda SC |
335 | static u32 unresettable_controller[] = { |
336 | 0x324a103C, /* Smart Array P712m */ | |
337 | 0x324b103C, /* SmartArray P711m */ | |
338 | 0x3223103C, /* Smart Array P800 */ | |
339 | 0x3234103C, /* Smart Array P400 */ | |
340 | 0x3235103C, /* Smart Array P400i */ | |
341 | 0x3211103C, /* Smart Array E200i */ | |
342 | 0x3212103C, /* Smart Array E200 */ | |
343 | 0x3213103C, /* Smart Array E200i */ | |
344 | 0x3214103C, /* Smart Array E200i */ | |
345 | 0x3215103C, /* Smart Array E200i */ | |
346 | 0x3237103C, /* Smart Array E500 */ | |
347 | 0x323D103C, /* Smart Array P700m */ | |
7af0abbc | 348 | 0x40800E11, /* Smart Array 5i */ |
941b1cda SC |
349 | 0x409C0E11, /* Smart Array 6400 */ |
350 | 0x409D0E11, /* Smart Array 6400 EM */ | |
5a4f934e TH |
351 | 0x40700E11, /* Smart Array 5300 */ |
352 | 0x40820E11, /* Smart Array 532 */ | |
353 | 0x40830E11, /* Smart Array 5312 */ | |
354 | 0x409A0E11, /* Smart Array 641 */ | |
355 | 0x409B0E11, /* Smart Array 642 */ | |
356 | 0x40910E11, /* Smart Array 6i */ | |
941b1cda SC |
357 | }; |
358 | ||
46380786 SC |
359 | /* List of controllers which cannot even be soft reset */ |
360 | static u32 soft_unresettable_controller[] = { | |
7af0abbc | 361 | 0x40800E11, /* Smart Array 5i */ |
5a4f934e TH |
362 | 0x40700E11, /* Smart Array 5300 */ |
363 | 0x40820E11, /* Smart Array 532 */ | |
364 | 0x40830E11, /* Smart Array 5312 */ | |
365 | 0x409A0E11, /* Smart Array 641 */ | |
366 | 0x409B0E11, /* Smart Array 642 */ | |
367 | 0x40910E11, /* Smart Array 6i */ | |
46380786 SC |
368 | /* Exclude 640x boards. These are two pci devices in one slot |
369 | * which share a battery backed cache module. One controls the | |
370 | * cache, the other accesses the cache through the one that controls | |
371 | * it. If we reset the one controlling the cache, the other will | |
372 | * likely not be happy. Just forbid resetting this conjoined mess. | |
373 | * The 640x isn't really supported by hpsa anyway. | |
374 | */ | |
375 | 0x409C0E11, /* Smart Array 6400 */ | |
376 | 0x409D0E11, /* Smart Array 6400 EM */ | |
377 | }; | |
378 | ||
379 | static int ctlr_is_hard_resettable(u32 board_id) | |
941b1cda SC |
380 | { |
381 | int i; | |
382 | ||
383 | for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++) | |
46380786 SC |
384 | if (unresettable_controller[i] == board_id) |
385 | return 0; | |
386 | return 1; | |
387 | } | |
388 | ||
389 | static int ctlr_is_soft_resettable(u32 board_id) | |
390 | { | |
391 | int i; | |
392 | ||
393 | for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++) | |
394 | if (soft_unresettable_controller[i] == board_id) | |
941b1cda SC |
395 | return 0; |
396 | return 1; | |
397 | } | |
398 | ||
46380786 SC |
399 | static int ctlr_is_resettable(u32 board_id) |
400 | { | |
401 | return ctlr_is_hard_resettable(board_id) || | |
402 | ctlr_is_soft_resettable(board_id); | |
403 | } | |
404 | ||
941b1cda SC |
405 | static ssize_t host_show_resettable(struct device *dev, |
406 | struct device_attribute *attr, char *buf) | |
407 | { | |
408 | struct ctlr_info *h; | |
409 | struct Scsi_Host *shost = class_to_shost(dev); | |
410 | ||
411 | h = shost_to_hba(shost); | |
46380786 | 412 | return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id)); |
941b1cda SC |
413 | } |
414 | ||
edd16368 SC |
415 | static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[]) |
416 | { | |
417 | return (scsi3addr[3] & 0xC0) == 0x40; | |
418 | } | |
419 | ||
420 | static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG", | |
d82357ea | 421 | "1(ADM)", "UNKNOWN" |
edd16368 SC |
422 | }; |
423 | #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1) | |
424 | ||
425 | static ssize_t raid_level_show(struct device *dev, | |
426 | struct device_attribute *attr, char *buf) | |
427 | { | |
428 | ssize_t l = 0; | |
82a72c0a | 429 | unsigned char rlevel; |
edd16368 SC |
430 | struct ctlr_info *h; |
431 | struct scsi_device *sdev; | |
432 | struct hpsa_scsi_dev_t *hdev; | |
433 | unsigned long flags; | |
434 | ||
435 | sdev = to_scsi_device(dev); | |
436 | h = sdev_to_hba(sdev); | |
437 | spin_lock_irqsave(&h->lock, flags); | |
438 | hdev = sdev->hostdata; | |
439 | if (!hdev) { | |
440 | spin_unlock_irqrestore(&h->lock, flags); | |
441 | return -ENODEV; | |
442 | } | |
443 | ||
444 | /* Is this even a logical drive? */ | |
445 | if (!is_logical_dev_addr_mode(hdev->scsi3addr)) { | |
446 | spin_unlock_irqrestore(&h->lock, flags); | |
447 | l = snprintf(buf, PAGE_SIZE, "N/A\n"); | |
448 | return l; | |
449 | } | |
450 | ||
451 | rlevel = hdev->raid_level; | |
452 | spin_unlock_irqrestore(&h->lock, flags); | |
82a72c0a | 453 | if (rlevel > RAID_UNKNOWN) |
edd16368 SC |
454 | rlevel = RAID_UNKNOWN; |
455 | l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]); | |
456 | return l; | |
457 | } | |
458 | ||
459 | static ssize_t lunid_show(struct device *dev, | |
460 | struct device_attribute *attr, char *buf) | |
461 | { | |
462 | struct ctlr_info *h; | |
463 | struct scsi_device *sdev; | |
464 | struct hpsa_scsi_dev_t *hdev; | |
465 | unsigned long flags; | |
466 | unsigned char lunid[8]; | |
467 | ||
468 | sdev = to_scsi_device(dev); | |
469 | h = sdev_to_hba(sdev); | |
470 | spin_lock_irqsave(&h->lock, flags); | |
471 | hdev = sdev->hostdata; | |
472 | if (!hdev) { | |
473 | spin_unlock_irqrestore(&h->lock, flags); | |
474 | return -ENODEV; | |
475 | } | |
476 | memcpy(lunid, hdev->scsi3addr, sizeof(lunid)); | |
477 | spin_unlock_irqrestore(&h->lock, flags); | |
478 | return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n", | |
479 | lunid[0], lunid[1], lunid[2], lunid[3], | |
480 | lunid[4], lunid[5], lunid[6], lunid[7]); | |
481 | } | |
482 | ||
483 | static ssize_t unique_id_show(struct device *dev, | |
484 | struct device_attribute *attr, char *buf) | |
485 | { | |
486 | struct ctlr_info *h; | |
487 | struct scsi_device *sdev; | |
488 | struct hpsa_scsi_dev_t *hdev; | |
489 | unsigned long flags; | |
490 | unsigned char sn[16]; | |
491 | ||
492 | sdev = to_scsi_device(dev); | |
493 | h = sdev_to_hba(sdev); | |
494 | spin_lock_irqsave(&h->lock, flags); | |
495 | hdev = sdev->hostdata; | |
496 | if (!hdev) { | |
497 | spin_unlock_irqrestore(&h->lock, flags); | |
498 | return -ENODEV; | |
499 | } | |
500 | memcpy(sn, hdev->device_id, sizeof(sn)); | |
501 | spin_unlock_irqrestore(&h->lock, flags); | |
502 | return snprintf(buf, 16 * 2 + 2, | |
503 | "%02X%02X%02X%02X%02X%02X%02X%02X" | |
504 | "%02X%02X%02X%02X%02X%02X%02X%02X\n", | |
505 | sn[0], sn[1], sn[2], sn[3], | |
506 | sn[4], sn[5], sn[6], sn[7], | |
507 | sn[8], sn[9], sn[10], sn[11], | |
508 | sn[12], sn[13], sn[14], sn[15]); | |
509 | } | |
510 | ||
c1988684 ST |
511 | static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev, |
512 | struct device_attribute *attr, char *buf) | |
513 | { | |
514 | struct ctlr_info *h; | |
515 | struct scsi_device *sdev; | |
516 | struct hpsa_scsi_dev_t *hdev; | |
517 | unsigned long flags; | |
518 | int offload_enabled; | |
519 | ||
520 | sdev = to_scsi_device(dev); | |
521 | h = sdev_to_hba(sdev); | |
522 | spin_lock_irqsave(&h->lock, flags); | |
523 | hdev = sdev->hostdata; | |
524 | if (!hdev) { | |
525 | spin_unlock_irqrestore(&h->lock, flags); | |
526 | return -ENODEV; | |
527 | } | |
528 | offload_enabled = hdev->offload_enabled; | |
529 | spin_unlock_irqrestore(&h->lock, flags); | |
530 | return snprintf(buf, 20, "%d\n", offload_enabled); | |
531 | } | |
532 | ||
3f5eac3a SC |
533 | static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL); |
534 | static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL); | |
535 | static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL); | |
536 | static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan); | |
c1988684 ST |
537 | static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO, |
538 | host_show_hp_ssd_smart_path_enabled, NULL); | |
3f5eac3a SC |
539 | static DEVICE_ATTR(firmware_revision, S_IRUGO, |
540 | host_show_firmware_revision, NULL); | |
541 | static DEVICE_ATTR(commands_outstanding, S_IRUGO, | |
542 | host_show_commands_outstanding, NULL); | |
543 | static DEVICE_ATTR(transport_mode, S_IRUGO, | |
544 | host_show_transport_mode, NULL); | |
941b1cda SC |
545 | static DEVICE_ATTR(resettable, S_IRUGO, |
546 | host_show_resettable, NULL); | |
3f5eac3a SC |
547 | |
548 | static struct device_attribute *hpsa_sdev_attrs[] = { | |
549 | &dev_attr_raid_level, | |
550 | &dev_attr_lunid, | |
551 | &dev_attr_unique_id, | |
c1988684 | 552 | &dev_attr_hp_ssd_smart_path_enabled, |
3f5eac3a SC |
553 | NULL, |
554 | }; | |
555 | ||
556 | static struct device_attribute *hpsa_shost_attrs[] = { | |
557 | &dev_attr_rescan, | |
558 | &dev_attr_firmware_revision, | |
559 | &dev_attr_commands_outstanding, | |
560 | &dev_attr_transport_mode, | |
941b1cda | 561 | &dev_attr_resettable, |
3f5eac3a SC |
562 | NULL, |
563 | }; | |
564 | ||
565 | static struct scsi_host_template hpsa_driver_template = { | |
566 | .module = THIS_MODULE, | |
f79cfec6 SC |
567 | .name = HPSA, |
568 | .proc_name = HPSA, | |
3f5eac3a SC |
569 | .queuecommand = hpsa_scsi_queue_command, |
570 | .scan_start = hpsa_scan_start, | |
571 | .scan_finished = hpsa_scan_finished, | |
572 | .change_queue_depth = hpsa_change_queue_depth, | |
573 | .this_id = -1, | |
574 | .use_clustering = ENABLE_CLUSTERING, | |
75167d2c | 575 | .eh_abort_handler = hpsa_eh_abort_handler, |
3f5eac3a SC |
576 | .eh_device_reset_handler = hpsa_eh_device_reset_handler, |
577 | .ioctl = hpsa_ioctl, | |
578 | .slave_alloc = hpsa_slave_alloc, | |
579 | .slave_destroy = hpsa_slave_destroy, | |
580 | #ifdef CONFIG_COMPAT | |
581 | .compat_ioctl = hpsa_compat_ioctl, | |
582 | #endif | |
583 | .sdev_attrs = hpsa_sdev_attrs, | |
584 | .shost_attrs = hpsa_shost_attrs, | |
c0d6a4d1 | 585 | .max_sectors = 8192, |
54b2b50c | 586 | .no_write_same = 1, |
3f5eac3a SC |
587 | }; |
588 | ||
589 | ||
590 | /* Enqueuing and dequeuing functions for cmdlists. */ | |
591 | static inline void addQ(struct list_head *list, struct CommandList *c) | |
592 | { | |
593 | list_add_tail(&c->list, list); | |
594 | } | |
595 | ||
254f796b | 596 | static inline u32 next_command(struct ctlr_info *h, u8 q) |
3f5eac3a SC |
597 | { |
598 | u32 a; | |
254f796b | 599 | struct reply_pool *rq = &h->reply_queue[q]; |
e16a33ad | 600 | unsigned long flags; |
3f5eac3a | 601 | |
e1f7de0c MG |
602 | if (h->transMethod & CFGTBL_Trans_io_accel1) |
603 | return h->access.command_completed(h, q); | |
604 | ||
3f5eac3a | 605 | if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant))) |
254f796b | 606 | return h->access.command_completed(h, q); |
3f5eac3a | 607 | |
254f796b MG |
608 | if ((rq->head[rq->current_entry] & 1) == rq->wraparound) { |
609 | a = rq->head[rq->current_entry]; | |
610 | rq->current_entry++; | |
e16a33ad | 611 | spin_lock_irqsave(&h->lock, flags); |
3f5eac3a | 612 | h->commands_outstanding--; |
e16a33ad | 613 | spin_unlock_irqrestore(&h->lock, flags); |
3f5eac3a SC |
614 | } else { |
615 | a = FIFO_EMPTY; | |
616 | } | |
617 | /* Check for wraparound */ | |
254f796b MG |
618 | if (rq->current_entry == h->max_commands) { |
619 | rq->current_entry = 0; | |
620 | rq->wraparound ^= 1; | |
3f5eac3a SC |
621 | } |
622 | return a; | |
623 | } | |
624 | ||
625 | /* set_performant_mode: Modify the tag for cciss performant | |
626 | * set bit 0 for pull model, bits 3-1 for block fetch | |
627 | * register number | |
628 | */ | |
629 | static void set_performant_mode(struct ctlr_info *h, struct CommandList *c) | |
630 | { | |
254f796b | 631 | if (likely(h->transMethod & CFGTBL_Trans_Performant)) { |
3f5eac3a | 632 | c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1); |
eee0f03a | 633 | if (likely(h->msix_vector > 0)) |
254f796b | 634 | c->Header.ReplyQueue = |
804a5cb5 | 635 | raw_smp_processor_id() % h->nreply_queues; |
254f796b | 636 | } |
3f5eac3a SC |
637 | } |
638 | ||
e85c5974 SC |
639 | static int is_firmware_flash_cmd(u8 *cdb) |
640 | { | |
641 | return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE; | |
642 | } | |
643 | ||
644 | /* | |
645 | * During firmware flash, the heartbeat register may not update as frequently | |
646 | * as it should. So we dial down lockup detection during firmware flash. and | |
647 | * dial it back up when firmware flash completes. | |
648 | */ | |
649 | #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ) | |
650 | #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ) | |
651 | static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h, | |
652 | struct CommandList *c) | |
653 | { | |
654 | if (!is_firmware_flash_cmd(c->Request.CDB)) | |
655 | return; | |
656 | atomic_inc(&h->firmware_flash_in_progress); | |
657 | h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH; | |
658 | } | |
659 | ||
660 | static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h, | |
661 | struct CommandList *c) | |
662 | { | |
663 | if (is_firmware_flash_cmd(c->Request.CDB) && | |
664 | atomic_dec_and_test(&h->firmware_flash_in_progress)) | |
665 | h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL; | |
666 | } | |
667 | ||
3f5eac3a SC |
668 | static void enqueue_cmd_and_start_io(struct ctlr_info *h, |
669 | struct CommandList *c) | |
670 | { | |
671 | unsigned long flags; | |
672 | ||
673 | set_performant_mode(h, c); | |
e85c5974 | 674 | dial_down_lockup_detection_during_fw_flash(h, c); |
3f5eac3a SC |
675 | spin_lock_irqsave(&h->lock, flags); |
676 | addQ(&h->reqQ, c); | |
677 | h->Qdepth++; | |
3f5eac3a | 678 | spin_unlock_irqrestore(&h->lock, flags); |
e16a33ad | 679 | start_io(h); |
3f5eac3a SC |
680 | } |
681 | ||
682 | static inline void removeQ(struct CommandList *c) | |
683 | { | |
684 | if (WARN_ON(list_empty(&c->list))) | |
685 | return; | |
686 | list_del_init(&c->list); | |
687 | } | |
688 | ||
689 | static inline int is_hba_lunid(unsigned char scsi3addr[]) | |
690 | { | |
691 | return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0; | |
692 | } | |
693 | ||
694 | static inline int is_scsi_rev_5(struct ctlr_info *h) | |
695 | { | |
696 | if (!h->hba_inquiry_data) | |
697 | return 0; | |
698 | if ((h->hba_inquiry_data[2] & 0x07) == 5) | |
699 | return 1; | |
700 | return 0; | |
701 | } | |
702 | ||
edd16368 SC |
703 | static int hpsa_find_target_lun(struct ctlr_info *h, |
704 | unsigned char scsi3addr[], int bus, int *target, int *lun) | |
705 | { | |
706 | /* finds an unused bus, target, lun for a new physical device | |
707 | * assumes h->devlock is held | |
708 | */ | |
709 | int i, found = 0; | |
cfe5badc | 710 | DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES); |
edd16368 | 711 | |
263d9401 | 712 | bitmap_zero(lun_taken, HPSA_MAX_DEVICES); |
edd16368 SC |
713 | |
714 | for (i = 0; i < h->ndevices; i++) { | |
715 | if (h->dev[i]->bus == bus && h->dev[i]->target != -1) | |
263d9401 | 716 | __set_bit(h->dev[i]->target, lun_taken); |
edd16368 SC |
717 | } |
718 | ||
263d9401 AM |
719 | i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES); |
720 | if (i < HPSA_MAX_DEVICES) { | |
721 | /* *bus = 1; */ | |
722 | *target = i; | |
723 | *lun = 0; | |
724 | found = 1; | |
edd16368 SC |
725 | } |
726 | return !found; | |
727 | } | |
728 | ||
729 | /* Add an entry into h->dev[] array. */ | |
730 | static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno, | |
731 | struct hpsa_scsi_dev_t *device, | |
732 | struct hpsa_scsi_dev_t *added[], int *nadded) | |
733 | { | |
734 | /* assumes h->devlock is held */ | |
735 | int n = h->ndevices; | |
736 | int i; | |
737 | unsigned char addr1[8], addr2[8]; | |
738 | struct hpsa_scsi_dev_t *sd; | |
739 | ||
cfe5badc | 740 | if (n >= HPSA_MAX_DEVICES) { |
edd16368 SC |
741 | dev_err(&h->pdev->dev, "too many devices, some will be " |
742 | "inaccessible.\n"); | |
743 | return -1; | |
744 | } | |
745 | ||
746 | /* physical devices do not have lun or target assigned until now. */ | |
747 | if (device->lun != -1) | |
748 | /* Logical device, lun is already assigned. */ | |
749 | goto lun_assigned; | |
750 | ||
751 | /* If this device a non-zero lun of a multi-lun device | |
752 | * byte 4 of the 8-byte LUN addr will contain the logical | |
753 | * unit no, zero otherise. | |
754 | */ | |
755 | if (device->scsi3addr[4] == 0) { | |
756 | /* This is not a non-zero lun of a multi-lun device */ | |
757 | if (hpsa_find_target_lun(h, device->scsi3addr, | |
758 | device->bus, &device->target, &device->lun) != 0) | |
759 | return -1; | |
760 | goto lun_assigned; | |
761 | } | |
762 | ||
763 | /* This is a non-zero lun of a multi-lun device. | |
764 | * Search through our list and find the device which | |
765 | * has the same 8 byte LUN address, excepting byte 4. | |
766 | * Assign the same bus and target for this new LUN. | |
767 | * Use the logical unit number from the firmware. | |
768 | */ | |
769 | memcpy(addr1, device->scsi3addr, 8); | |
770 | addr1[4] = 0; | |
771 | for (i = 0; i < n; i++) { | |
772 | sd = h->dev[i]; | |
773 | memcpy(addr2, sd->scsi3addr, 8); | |
774 | addr2[4] = 0; | |
775 | /* differ only in byte 4? */ | |
776 | if (memcmp(addr1, addr2, 8) == 0) { | |
777 | device->bus = sd->bus; | |
778 | device->target = sd->target; | |
779 | device->lun = device->scsi3addr[4]; | |
780 | break; | |
781 | } | |
782 | } | |
783 | if (device->lun == -1) { | |
784 | dev_warn(&h->pdev->dev, "physical device with no LUN=0," | |
785 | " suspect firmware bug or unsupported hardware " | |
786 | "configuration.\n"); | |
787 | return -1; | |
788 | } | |
789 | ||
790 | lun_assigned: | |
791 | ||
792 | h->dev[n] = device; | |
793 | h->ndevices++; | |
794 | added[*nadded] = device; | |
795 | (*nadded)++; | |
796 | ||
797 | /* initially, (before registering with scsi layer) we don't | |
798 | * know our hostno and we don't want to print anything first | |
799 | * time anyway (the scsi layer's inquiries will show that info) | |
800 | */ | |
801 | /* if (hostno != -1) */ | |
802 | dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n", | |
803 | scsi_device_type(device->devtype), hostno, | |
804 | device->bus, device->target, device->lun); | |
805 | return 0; | |
806 | } | |
807 | ||
bd9244f7 ST |
808 | /* Update an entry in h->dev[] array. */ |
809 | static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno, | |
810 | int entry, struct hpsa_scsi_dev_t *new_entry) | |
811 | { | |
812 | /* assumes h->devlock is held */ | |
813 | BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES); | |
814 | ||
815 | /* Raid level changed. */ | |
816 | h->dev[entry]->raid_level = new_entry->raid_level; | |
250fb125 SC |
817 | |
818 | /* Raid offload parameters changed. */ | |
819 | h->dev[entry]->offload_config = new_entry->offload_config; | |
820 | h->dev[entry]->offload_enabled = new_entry->offload_enabled; | |
821 | ||
bd9244f7 ST |
822 | dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n", |
823 | scsi_device_type(new_entry->devtype), hostno, new_entry->bus, | |
824 | new_entry->target, new_entry->lun); | |
825 | } | |
826 | ||
2a8ccf31 SC |
827 | /* Replace an entry from h->dev[] array. */ |
828 | static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno, | |
829 | int entry, struct hpsa_scsi_dev_t *new_entry, | |
830 | struct hpsa_scsi_dev_t *added[], int *nadded, | |
831 | struct hpsa_scsi_dev_t *removed[], int *nremoved) | |
832 | { | |
833 | /* assumes h->devlock is held */ | |
cfe5badc | 834 | BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES); |
2a8ccf31 SC |
835 | removed[*nremoved] = h->dev[entry]; |
836 | (*nremoved)++; | |
01350d05 SC |
837 | |
838 | /* | |
839 | * New physical devices won't have target/lun assigned yet | |
840 | * so we need to preserve the values in the slot we are replacing. | |
841 | */ | |
842 | if (new_entry->target == -1) { | |
843 | new_entry->target = h->dev[entry]->target; | |
844 | new_entry->lun = h->dev[entry]->lun; | |
845 | } | |
846 | ||
2a8ccf31 SC |
847 | h->dev[entry] = new_entry; |
848 | added[*nadded] = new_entry; | |
849 | (*nadded)++; | |
850 | dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n", | |
851 | scsi_device_type(new_entry->devtype), hostno, new_entry->bus, | |
852 | new_entry->target, new_entry->lun); | |
853 | } | |
854 | ||
edd16368 SC |
855 | /* Remove an entry from h->dev[] array. */ |
856 | static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry, | |
857 | struct hpsa_scsi_dev_t *removed[], int *nremoved) | |
858 | { | |
859 | /* assumes h->devlock is held */ | |
860 | int i; | |
861 | struct hpsa_scsi_dev_t *sd; | |
862 | ||
cfe5badc | 863 | BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES); |
edd16368 SC |
864 | |
865 | sd = h->dev[entry]; | |
866 | removed[*nremoved] = h->dev[entry]; | |
867 | (*nremoved)++; | |
868 | ||
869 | for (i = entry; i < h->ndevices-1; i++) | |
870 | h->dev[i] = h->dev[i+1]; | |
871 | h->ndevices--; | |
872 | dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n", | |
873 | scsi_device_type(sd->devtype), hostno, sd->bus, sd->target, | |
874 | sd->lun); | |
875 | } | |
876 | ||
877 | #define SCSI3ADDR_EQ(a, b) ( \ | |
878 | (a)[7] == (b)[7] && \ | |
879 | (a)[6] == (b)[6] && \ | |
880 | (a)[5] == (b)[5] && \ | |
881 | (a)[4] == (b)[4] && \ | |
882 | (a)[3] == (b)[3] && \ | |
883 | (a)[2] == (b)[2] && \ | |
884 | (a)[1] == (b)[1] && \ | |
885 | (a)[0] == (b)[0]) | |
886 | ||
887 | static void fixup_botched_add(struct ctlr_info *h, | |
888 | struct hpsa_scsi_dev_t *added) | |
889 | { | |
890 | /* called when scsi_add_device fails in order to re-adjust | |
891 | * h->dev[] to match the mid layer's view. | |
892 | */ | |
893 | unsigned long flags; | |
894 | int i, j; | |
895 | ||
896 | spin_lock_irqsave(&h->lock, flags); | |
897 | for (i = 0; i < h->ndevices; i++) { | |
898 | if (h->dev[i] == added) { | |
899 | for (j = i; j < h->ndevices-1; j++) | |
900 | h->dev[j] = h->dev[j+1]; | |
901 | h->ndevices--; | |
902 | break; | |
903 | } | |
904 | } | |
905 | spin_unlock_irqrestore(&h->lock, flags); | |
906 | kfree(added); | |
907 | } | |
908 | ||
909 | static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1, | |
910 | struct hpsa_scsi_dev_t *dev2) | |
911 | { | |
edd16368 SC |
912 | /* we compare everything except lun and target as these |
913 | * are not yet assigned. Compare parts likely | |
914 | * to differ first | |
915 | */ | |
916 | if (memcmp(dev1->scsi3addr, dev2->scsi3addr, | |
917 | sizeof(dev1->scsi3addr)) != 0) | |
918 | return 0; | |
919 | if (memcmp(dev1->device_id, dev2->device_id, | |
920 | sizeof(dev1->device_id)) != 0) | |
921 | return 0; | |
922 | if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0) | |
923 | return 0; | |
924 | if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0) | |
925 | return 0; | |
edd16368 SC |
926 | if (dev1->devtype != dev2->devtype) |
927 | return 0; | |
edd16368 SC |
928 | if (dev1->bus != dev2->bus) |
929 | return 0; | |
930 | return 1; | |
931 | } | |
932 | ||
bd9244f7 ST |
933 | static inline int device_updated(struct hpsa_scsi_dev_t *dev1, |
934 | struct hpsa_scsi_dev_t *dev2) | |
935 | { | |
936 | /* Device attributes that can change, but don't mean | |
937 | * that the device is a different device, nor that the OS | |
938 | * needs to be told anything about the change. | |
939 | */ | |
940 | if (dev1->raid_level != dev2->raid_level) | |
941 | return 1; | |
250fb125 SC |
942 | if (dev1->offload_config != dev2->offload_config) |
943 | return 1; | |
944 | if (dev1->offload_enabled != dev2->offload_enabled) | |
945 | return 1; | |
bd9244f7 ST |
946 | return 0; |
947 | } | |
948 | ||
edd16368 SC |
949 | /* Find needle in haystack. If exact match found, return DEVICE_SAME, |
950 | * and return needle location in *index. If scsi3addr matches, but not | |
951 | * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle | |
bd9244f7 ST |
952 | * location in *index. |
953 | * In the case of a minor device attribute change, such as RAID level, just | |
954 | * return DEVICE_UPDATED, along with the updated device's location in index. | |
955 | * If needle not found, return DEVICE_NOT_FOUND. | |
edd16368 SC |
956 | */ |
957 | static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle, | |
958 | struct hpsa_scsi_dev_t *haystack[], int haystack_size, | |
959 | int *index) | |
960 | { | |
961 | int i; | |
962 | #define DEVICE_NOT_FOUND 0 | |
963 | #define DEVICE_CHANGED 1 | |
964 | #define DEVICE_SAME 2 | |
bd9244f7 | 965 | #define DEVICE_UPDATED 3 |
edd16368 | 966 | for (i = 0; i < haystack_size; i++) { |
23231048 SC |
967 | if (haystack[i] == NULL) /* previously removed. */ |
968 | continue; | |
edd16368 SC |
969 | if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) { |
970 | *index = i; | |
bd9244f7 ST |
971 | if (device_is_the_same(needle, haystack[i])) { |
972 | if (device_updated(needle, haystack[i])) | |
973 | return DEVICE_UPDATED; | |
edd16368 | 974 | return DEVICE_SAME; |
bd9244f7 | 975 | } else { |
edd16368 | 976 | return DEVICE_CHANGED; |
bd9244f7 | 977 | } |
edd16368 SC |
978 | } |
979 | } | |
980 | *index = -1; | |
981 | return DEVICE_NOT_FOUND; | |
982 | } | |
983 | ||
4967bd3e | 984 | static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno, |
edd16368 SC |
985 | struct hpsa_scsi_dev_t *sd[], int nsds) |
986 | { | |
987 | /* sd contains scsi3 addresses and devtypes, and inquiry | |
988 | * data. This function takes what's in sd to be the current | |
989 | * reality and updates h->dev[] to reflect that reality. | |
990 | */ | |
991 | int i, entry, device_change, changes = 0; | |
992 | struct hpsa_scsi_dev_t *csd; | |
993 | unsigned long flags; | |
994 | struct hpsa_scsi_dev_t **added, **removed; | |
995 | int nadded, nremoved; | |
996 | struct Scsi_Host *sh = NULL; | |
997 | ||
cfe5badc ST |
998 | added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL); |
999 | removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL); | |
edd16368 SC |
1000 | |
1001 | if (!added || !removed) { | |
1002 | dev_warn(&h->pdev->dev, "out of memory in " | |
1003 | "adjust_hpsa_scsi_table\n"); | |
1004 | goto free_and_out; | |
1005 | } | |
1006 | ||
1007 | spin_lock_irqsave(&h->devlock, flags); | |
1008 | ||
1009 | /* find any devices in h->dev[] that are not in | |
1010 | * sd[] and remove them from h->dev[], and for any | |
1011 | * devices which have changed, remove the old device | |
1012 | * info and add the new device info. | |
bd9244f7 ST |
1013 | * If minor device attributes change, just update |
1014 | * the existing device structure. | |
edd16368 SC |
1015 | */ |
1016 | i = 0; | |
1017 | nremoved = 0; | |
1018 | nadded = 0; | |
1019 | while (i < h->ndevices) { | |
1020 | csd = h->dev[i]; | |
1021 | device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry); | |
1022 | if (device_change == DEVICE_NOT_FOUND) { | |
1023 | changes++; | |
1024 | hpsa_scsi_remove_entry(h, hostno, i, | |
1025 | removed, &nremoved); | |
1026 | continue; /* remove ^^^, hence i not incremented */ | |
1027 | } else if (device_change == DEVICE_CHANGED) { | |
1028 | changes++; | |
2a8ccf31 SC |
1029 | hpsa_scsi_replace_entry(h, hostno, i, sd[entry], |
1030 | added, &nadded, removed, &nremoved); | |
c7f172dc SC |
1031 | /* Set it to NULL to prevent it from being freed |
1032 | * at the bottom of hpsa_update_scsi_devices() | |
1033 | */ | |
1034 | sd[entry] = NULL; | |
bd9244f7 ST |
1035 | } else if (device_change == DEVICE_UPDATED) { |
1036 | hpsa_scsi_update_entry(h, hostno, i, sd[entry]); | |
edd16368 SC |
1037 | } |
1038 | i++; | |
1039 | } | |
1040 | ||
1041 | /* Now, make sure every device listed in sd[] is also | |
1042 | * listed in h->dev[], adding them if they aren't found | |
1043 | */ | |
1044 | ||
1045 | for (i = 0; i < nsds; i++) { | |
1046 | if (!sd[i]) /* if already added above. */ | |
1047 | continue; | |
1048 | device_change = hpsa_scsi_find_entry(sd[i], h->dev, | |
1049 | h->ndevices, &entry); | |
1050 | if (device_change == DEVICE_NOT_FOUND) { | |
1051 | changes++; | |
1052 | if (hpsa_scsi_add_entry(h, hostno, sd[i], | |
1053 | added, &nadded) != 0) | |
1054 | break; | |
1055 | sd[i] = NULL; /* prevent from being freed later. */ | |
1056 | } else if (device_change == DEVICE_CHANGED) { | |
1057 | /* should never happen... */ | |
1058 | changes++; | |
1059 | dev_warn(&h->pdev->dev, | |
1060 | "device unexpectedly changed.\n"); | |
1061 | /* but if it does happen, we just ignore that device */ | |
1062 | } | |
1063 | } | |
1064 | spin_unlock_irqrestore(&h->devlock, flags); | |
1065 | ||
1066 | /* Don't notify scsi mid layer of any changes the first time through | |
1067 | * (or if there are no changes) scsi_scan_host will do it later the | |
1068 | * first time through. | |
1069 | */ | |
1070 | if (hostno == -1 || !changes) | |
1071 | goto free_and_out; | |
1072 | ||
1073 | sh = h->scsi_host; | |
1074 | /* Notify scsi mid layer of any removed devices */ | |
1075 | for (i = 0; i < nremoved; i++) { | |
1076 | struct scsi_device *sdev = | |
1077 | scsi_device_lookup(sh, removed[i]->bus, | |
1078 | removed[i]->target, removed[i]->lun); | |
1079 | if (sdev != NULL) { | |
1080 | scsi_remove_device(sdev); | |
1081 | scsi_device_put(sdev); | |
1082 | } else { | |
1083 | /* We don't expect to get here. | |
1084 | * future cmds to this device will get selection | |
1085 | * timeout as if the device was gone. | |
1086 | */ | |
1087 | dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d " | |
1088 | " for removal.", hostno, removed[i]->bus, | |
1089 | removed[i]->target, removed[i]->lun); | |
1090 | } | |
1091 | kfree(removed[i]); | |
1092 | removed[i] = NULL; | |
1093 | } | |
1094 | ||
1095 | /* Notify scsi mid layer of any added devices */ | |
1096 | for (i = 0; i < nadded; i++) { | |
1097 | if (scsi_add_device(sh, added[i]->bus, | |
1098 | added[i]->target, added[i]->lun) == 0) | |
1099 | continue; | |
1100 | dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, " | |
1101 | "device not added.\n", hostno, added[i]->bus, | |
1102 | added[i]->target, added[i]->lun); | |
1103 | /* now we have to remove it from h->dev, | |
1104 | * since it didn't get added to scsi mid layer | |
1105 | */ | |
1106 | fixup_botched_add(h, added[i]); | |
1107 | } | |
1108 | ||
1109 | free_and_out: | |
1110 | kfree(added); | |
1111 | kfree(removed); | |
edd16368 SC |
1112 | } |
1113 | ||
1114 | /* | |
9e03aa2f | 1115 | * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t * |
edd16368 SC |
1116 | * Assume's h->devlock is held. |
1117 | */ | |
1118 | static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h, | |
1119 | int bus, int target, int lun) | |
1120 | { | |
1121 | int i; | |
1122 | struct hpsa_scsi_dev_t *sd; | |
1123 | ||
1124 | for (i = 0; i < h->ndevices; i++) { | |
1125 | sd = h->dev[i]; | |
1126 | if (sd->bus == bus && sd->target == target && sd->lun == lun) | |
1127 | return sd; | |
1128 | } | |
1129 | return NULL; | |
1130 | } | |
1131 | ||
1132 | /* link sdev->hostdata to our per-device structure. */ | |
1133 | static int hpsa_slave_alloc(struct scsi_device *sdev) | |
1134 | { | |
1135 | struct hpsa_scsi_dev_t *sd; | |
1136 | unsigned long flags; | |
1137 | struct ctlr_info *h; | |
1138 | ||
1139 | h = sdev_to_hba(sdev); | |
1140 | spin_lock_irqsave(&h->devlock, flags); | |
1141 | sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev), | |
1142 | sdev_id(sdev), sdev->lun); | |
1143 | if (sd != NULL) | |
1144 | sdev->hostdata = sd; | |
1145 | spin_unlock_irqrestore(&h->devlock, flags); | |
1146 | return 0; | |
1147 | } | |
1148 | ||
1149 | static void hpsa_slave_destroy(struct scsi_device *sdev) | |
1150 | { | |
bcc44255 | 1151 | /* nothing to do. */ |
edd16368 SC |
1152 | } |
1153 | ||
33a2ffce SC |
1154 | static void hpsa_free_sg_chain_blocks(struct ctlr_info *h) |
1155 | { | |
1156 | int i; | |
1157 | ||
1158 | if (!h->cmd_sg_list) | |
1159 | return; | |
1160 | for (i = 0; i < h->nr_cmds; i++) { | |
1161 | kfree(h->cmd_sg_list[i]); | |
1162 | h->cmd_sg_list[i] = NULL; | |
1163 | } | |
1164 | kfree(h->cmd_sg_list); | |
1165 | h->cmd_sg_list = NULL; | |
1166 | } | |
1167 | ||
1168 | static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h) | |
1169 | { | |
1170 | int i; | |
1171 | ||
1172 | if (h->chainsize <= 0) | |
1173 | return 0; | |
1174 | ||
1175 | h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds, | |
1176 | GFP_KERNEL); | |
1177 | if (!h->cmd_sg_list) | |
1178 | return -ENOMEM; | |
1179 | for (i = 0; i < h->nr_cmds; i++) { | |
1180 | h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) * | |
1181 | h->chainsize, GFP_KERNEL); | |
1182 | if (!h->cmd_sg_list[i]) | |
1183 | goto clean; | |
1184 | } | |
1185 | return 0; | |
1186 | ||
1187 | clean: | |
1188 | hpsa_free_sg_chain_blocks(h); | |
1189 | return -ENOMEM; | |
1190 | } | |
1191 | ||
e2bea6df | 1192 | static int hpsa_map_sg_chain_block(struct ctlr_info *h, |
33a2ffce SC |
1193 | struct CommandList *c) |
1194 | { | |
1195 | struct SGDescriptor *chain_sg, *chain_block; | |
1196 | u64 temp64; | |
1197 | ||
1198 | chain_sg = &c->SG[h->max_cmd_sg_entries - 1]; | |
1199 | chain_block = h->cmd_sg_list[c->cmdindex]; | |
1200 | chain_sg->Ext = HPSA_SG_CHAIN; | |
1201 | chain_sg->Len = sizeof(*chain_sg) * | |
1202 | (c->Header.SGTotal - h->max_cmd_sg_entries); | |
1203 | temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len, | |
1204 | PCI_DMA_TODEVICE); | |
e2bea6df SC |
1205 | if (dma_mapping_error(&h->pdev->dev, temp64)) { |
1206 | /* prevent subsequent unmapping */ | |
1207 | chain_sg->Addr.lower = 0; | |
1208 | chain_sg->Addr.upper = 0; | |
1209 | return -1; | |
1210 | } | |
33a2ffce SC |
1211 | chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL); |
1212 | chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL); | |
e2bea6df | 1213 | return 0; |
33a2ffce SC |
1214 | } |
1215 | ||
1216 | static void hpsa_unmap_sg_chain_block(struct ctlr_info *h, | |
1217 | struct CommandList *c) | |
1218 | { | |
1219 | struct SGDescriptor *chain_sg; | |
1220 | union u64bit temp64; | |
1221 | ||
1222 | if (c->Header.SGTotal <= h->max_cmd_sg_entries) | |
1223 | return; | |
1224 | ||
1225 | chain_sg = &c->SG[h->max_cmd_sg_entries - 1]; | |
1226 | temp64.val32.lower = chain_sg->Addr.lower; | |
1227 | temp64.val32.upper = chain_sg->Addr.upper; | |
1228 | pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE); | |
1229 | } | |
1230 | ||
1fb011fb | 1231 | static void complete_scsi_command(struct CommandList *cp) |
edd16368 SC |
1232 | { |
1233 | struct scsi_cmnd *cmd; | |
1234 | struct ctlr_info *h; | |
1235 | struct ErrorInfo *ei; | |
283b4a9b | 1236 | struct hpsa_scsi_dev_t *dev; |
edd16368 SC |
1237 | |
1238 | unsigned char sense_key; | |
1239 | unsigned char asc; /* additional sense code */ | |
1240 | unsigned char ascq; /* additional sense code qualifier */ | |
db111e18 | 1241 | unsigned long sense_data_size; |
edd16368 SC |
1242 | |
1243 | ei = cp->err_info; | |
1244 | cmd = (struct scsi_cmnd *) cp->scsi_cmd; | |
1245 | h = cp->h; | |
283b4a9b | 1246 | dev = cmd->device->hostdata; |
edd16368 SC |
1247 | |
1248 | scsi_dma_unmap(cmd); /* undo the DMA mappings */ | |
e1f7de0c MG |
1249 | if ((cp->cmd_type == CMD_SCSI) && |
1250 | (cp->Header.SGTotal > h->max_cmd_sg_entries)) | |
33a2ffce | 1251 | hpsa_unmap_sg_chain_block(h, cp); |
edd16368 SC |
1252 | |
1253 | cmd->result = (DID_OK << 16); /* host byte */ | |
1254 | cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */ | |
5512672f | 1255 | cmd->result |= ei->ScsiStatus; |
edd16368 SC |
1256 | |
1257 | /* copy the sense data whether we need to or not. */ | |
db111e18 SC |
1258 | if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo)) |
1259 | sense_data_size = SCSI_SENSE_BUFFERSIZE; | |
1260 | else | |
1261 | sense_data_size = sizeof(ei->SenseInfo); | |
1262 | if (ei->SenseLen < sense_data_size) | |
1263 | sense_data_size = ei->SenseLen; | |
1264 | ||
1265 | memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size); | |
edd16368 SC |
1266 | scsi_set_resid(cmd, ei->ResidualCnt); |
1267 | ||
1268 | if (ei->CommandStatus == 0) { | |
edd16368 | 1269 | cmd_free(h, cp); |
2cc5bfaf | 1270 | cmd->scsi_done(cmd); |
edd16368 SC |
1271 | return; |
1272 | } | |
1273 | ||
e1f7de0c MG |
1274 | /* For I/O accelerator commands, copy over some fields to the normal |
1275 | * CISS header used below for error handling. | |
1276 | */ | |
1277 | if (cp->cmd_type == CMD_IOACCEL1) { | |
1278 | struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex]; | |
1279 | cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd); | |
1280 | cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK; | |
1281 | cp->Header.Tag.lower = c->Tag.lower; | |
1282 | cp->Header.Tag.upper = c->Tag.upper; | |
1283 | memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8); | |
1284 | memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen); | |
283b4a9b SC |
1285 | |
1286 | /* Any RAID offload error results in retry which will use | |
1287 | * the normal I/O path so the controller can handle whatever's | |
1288 | * wrong. | |
1289 | */ | |
1290 | if (is_logical_dev_addr_mode(dev->scsi3addr)) { | |
1291 | if (ei->CommandStatus == CMD_IOACCEL_DISABLED) | |
1292 | dev->offload_enabled = 0; | |
1293 | cmd->result = DID_SOFT_ERROR << 16; | |
1294 | cmd_free(h, cp); | |
1295 | cmd->scsi_done(cmd); | |
1296 | return; | |
1297 | } | |
e1f7de0c MG |
1298 | } |
1299 | ||
edd16368 SC |
1300 | /* an error has occurred */ |
1301 | switch (ei->CommandStatus) { | |
1302 | ||
1303 | case CMD_TARGET_STATUS: | |
1304 | if (ei->ScsiStatus) { | |
1305 | /* Get sense key */ | |
1306 | sense_key = 0xf & ei->SenseInfo[2]; | |
1307 | /* Get additional sense code */ | |
1308 | asc = ei->SenseInfo[12]; | |
1309 | /* Get addition sense code qualifier */ | |
1310 | ascq = ei->SenseInfo[13]; | |
1311 | } | |
1312 | ||
1313 | if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) { | |
3ce438df | 1314 | if (check_for_unit_attention(h, cp)) |
edd16368 | 1315 | break; |
edd16368 SC |
1316 | if (sense_key == ILLEGAL_REQUEST) { |
1317 | /* | |
1318 | * SCSI REPORT_LUNS is commonly unsupported on | |
1319 | * Smart Array. Suppress noisy complaint. | |
1320 | */ | |
1321 | if (cp->Request.CDB[0] == REPORT_LUNS) | |
1322 | break; | |
1323 | ||
1324 | /* If ASC/ASCQ indicate Logical Unit | |
1325 | * Not Supported condition, | |
1326 | */ | |
1327 | if ((asc == 0x25) && (ascq == 0x0)) { | |
1328 | dev_warn(&h->pdev->dev, "cp %p " | |
1329 | "has check condition\n", cp); | |
1330 | break; | |
1331 | } | |
1332 | } | |
1333 | ||
1334 | if (sense_key == NOT_READY) { | |
1335 | /* If Sense is Not Ready, Logical Unit | |
1336 | * Not ready, Manual Intervention | |
1337 | * required | |
1338 | */ | |
1339 | if ((asc == 0x04) && (ascq == 0x03)) { | |
edd16368 SC |
1340 | dev_warn(&h->pdev->dev, "cp %p " |
1341 | "has check condition: unit " | |
1342 | "not ready, manual " | |
1343 | "intervention required\n", cp); | |
1344 | break; | |
1345 | } | |
1346 | } | |
1d3b3609 MG |
1347 | if (sense_key == ABORTED_COMMAND) { |
1348 | /* Aborted command is retryable */ | |
1349 | dev_warn(&h->pdev->dev, "cp %p " | |
1350 | "has check condition: aborted command: " | |
1351 | "ASC: 0x%x, ASCQ: 0x%x\n", | |
1352 | cp, asc, ascq); | |
2e311fba | 1353 | cmd->result |= DID_SOFT_ERROR << 16; |
1d3b3609 MG |
1354 | break; |
1355 | } | |
edd16368 | 1356 | /* Must be some other type of check condition */ |
21b8e4ef | 1357 | dev_dbg(&h->pdev->dev, "cp %p has check condition: " |
edd16368 SC |
1358 | "unknown type: " |
1359 | "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, " | |
1360 | "Returning result: 0x%x, " | |
1361 | "cmd=[%02x %02x %02x %02x %02x " | |
807be732 | 1362 | "%02x %02x %02x %02x %02x %02x " |
edd16368 SC |
1363 | "%02x %02x %02x %02x %02x]\n", |
1364 | cp, sense_key, asc, ascq, | |
1365 | cmd->result, | |
1366 | cmd->cmnd[0], cmd->cmnd[1], | |
1367 | cmd->cmnd[2], cmd->cmnd[3], | |
1368 | cmd->cmnd[4], cmd->cmnd[5], | |
1369 | cmd->cmnd[6], cmd->cmnd[7], | |
807be732 MM |
1370 | cmd->cmnd[8], cmd->cmnd[9], |
1371 | cmd->cmnd[10], cmd->cmnd[11], | |
1372 | cmd->cmnd[12], cmd->cmnd[13], | |
1373 | cmd->cmnd[14], cmd->cmnd[15]); | |
edd16368 SC |
1374 | break; |
1375 | } | |
1376 | ||
1377 | ||
1378 | /* Problem was not a check condition | |
1379 | * Pass it up to the upper layers... | |
1380 | */ | |
1381 | if (ei->ScsiStatus) { | |
1382 | dev_warn(&h->pdev->dev, "cp %p has status 0x%x " | |
1383 | "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, " | |
1384 | "Returning result: 0x%x\n", | |
1385 | cp, ei->ScsiStatus, | |
1386 | sense_key, asc, ascq, | |
1387 | cmd->result); | |
1388 | } else { /* scsi status is zero??? How??? */ | |
1389 | dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. " | |
1390 | "Returning no connection.\n", cp), | |
1391 | ||
1392 | /* Ordinarily, this case should never happen, | |
1393 | * but there is a bug in some released firmware | |
1394 | * revisions that allows it to happen if, for | |
1395 | * example, a 4100 backplane loses power and | |
1396 | * the tape drive is in it. We assume that | |
1397 | * it's a fatal error of some kind because we | |
1398 | * can't show that it wasn't. We will make it | |
1399 | * look like selection timeout since that is | |
1400 | * the most common reason for this to occur, | |
1401 | * and it's severe enough. | |
1402 | */ | |
1403 | ||
1404 | cmd->result = DID_NO_CONNECT << 16; | |
1405 | } | |
1406 | break; | |
1407 | ||
1408 | case CMD_DATA_UNDERRUN: /* let mid layer handle it. */ | |
1409 | break; | |
1410 | case CMD_DATA_OVERRUN: | |
1411 | dev_warn(&h->pdev->dev, "cp %p has" | |
1412 | " completed with data overrun " | |
1413 | "reported\n", cp); | |
1414 | break; | |
1415 | case CMD_INVALID: { | |
1416 | /* print_bytes(cp, sizeof(*cp), 1, 0); | |
1417 | print_cmd(cp); */ | |
1418 | /* We get CMD_INVALID if you address a non-existent device | |
1419 | * instead of a selection timeout (no response). You will | |
1420 | * see this if you yank out a drive, then try to access it. | |
1421 | * This is kind of a shame because it means that any other | |
1422 | * CMD_INVALID (e.g. driver bug) will get interpreted as a | |
1423 | * missing target. */ | |
1424 | cmd->result = DID_NO_CONNECT << 16; | |
1425 | } | |
1426 | break; | |
1427 | case CMD_PROTOCOL_ERR: | |
256d0eaa | 1428 | cmd->result = DID_ERROR << 16; |
edd16368 | 1429 | dev_warn(&h->pdev->dev, "cp %p has " |
256d0eaa | 1430 | "protocol error\n", cp); |
edd16368 SC |
1431 | break; |
1432 | case CMD_HARDWARE_ERR: | |
1433 | cmd->result = DID_ERROR << 16; | |
1434 | dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp); | |
1435 | break; | |
1436 | case CMD_CONNECTION_LOST: | |
1437 | cmd->result = DID_ERROR << 16; | |
1438 | dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp); | |
1439 | break; | |
1440 | case CMD_ABORTED: | |
1441 | cmd->result = DID_ABORT << 16; | |
1442 | dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n", | |
1443 | cp, ei->ScsiStatus); | |
1444 | break; | |
1445 | case CMD_ABORT_FAILED: | |
1446 | cmd->result = DID_ERROR << 16; | |
1447 | dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp); | |
1448 | break; | |
1449 | case CMD_UNSOLICITED_ABORT: | |
f6e76055 SC |
1450 | cmd->result = DID_SOFT_ERROR << 16; /* retry the command */ |
1451 | dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited " | |
edd16368 SC |
1452 | "abort\n", cp); |
1453 | break; | |
1454 | case CMD_TIMEOUT: | |
1455 | cmd->result = DID_TIME_OUT << 16; | |
1456 | dev_warn(&h->pdev->dev, "cp %p timedout\n", cp); | |
1457 | break; | |
1d5e2ed0 SC |
1458 | case CMD_UNABORTABLE: |
1459 | cmd->result = DID_ERROR << 16; | |
1460 | dev_warn(&h->pdev->dev, "Command unabortable\n"); | |
1461 | break; | |
283b4a9b SC |
1462 | case CMD_IOACCEL_DISABLED: |
1463 | /* This only handles the direct pass-through case since RAID | |
1464 | * offload is handled above. Just attempt a retry. | |
1465 | */ | |
1466 | cmd->result = DID_SOFT_ERROR << 16; | |
1467 | dev_warn(&h->pdev->dev, | |
1468 | "cp %p had HP SSD Smart Path error\n", cp); | |
1469 | break; | |
edd16368 SC |
1470 | default: |
1471 | cmd->result = DID_ERROR << 16; | |
1472 | dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n", | |
1473 | cp, ei->CommandStatus); | |
1474 | } | |
edd16368 | 1475 | cmd_free(h, cp); |
2cc5bfaf | 1476 | cmd->scsi_done(cmd); |
edd16368 SC |
1477 | } |
1478 | ||
edd16368 SC |
1479 | static void hpsa_pci_unmap(struct pci_dev *pdev, |
1480 | struct CommandList *c, int sg_used, int data_direction) | |
1481 | { | |
1482 | int i; | |
1483 | union u64bit addr64; | |
1484 | ||
1485 | for (i = 0; i < sg_used; i++) { | |
1486 | addr64.val32.lower = c->SG[i].Addr.lower; | |
1487 | addr64.val32.upper = c->SG[i].Addr.upper; | |
1488 | pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len, | |
1489 | data_direction); | |
1490 | } | |
1491 | } | |
1492 | ||
a2dac136 | 1493 | static int hpsa_map_one(struct pci_dev *pdev, |
edd16368 SC |
1494 | struct CommandList *cp, |
1495 | unsigned char *buf, | |
1496 | size_t buflen, | |
1497 | int data_direction) | |
1498 | { | |
01a02ffc | 1499 | u64 addr64; |
edd16368 SC |
1500 | |
1501 | if (buflen == 0 || data_direction == PCI_DMA_NONE) { | |
1502 | cp->Header.SGList = 0; | |
1503 | cp->Header.SGTotal = 0; | |
a2dac136 | 1504 | return 0; |
edd16368 SC |
1505 | } |
1506 | ||
01a02ffc | 1507 | addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction); |
eceaae18 | 1508 | if (dma_mapping_error(&pdev->dev, addr64)) { |
a2dac136 | 1509 | /* Prevent subsequent unmap of something never mapped */ |
eceaae18 SK |
1510 | cp->Header.SGList = 0; |
1511 | cp->Header.SGTotal = 0; | |
a2dac136 | 1512 | return -1; |
eceaae18 | 1513 | } |
edd16368 | 1514 | cp->SG[0].Addr.lower = |
01a02ffc | 1515 | (u32) (addr64 & (u64) 0x00000000FFFFFFFF); |
edd16368 | 1516 | cp->SG[0].Addr.upper = |
01a02ffc | 1517 | (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF); |
edd16368 | 1518 | cp->SG[0].Len = buflen; |
e1d9cbfa | 1519 | cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */ |
01a02ffc SC |
1520 | cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */ |
1521 | cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */ | |
a2dac136 | 1522 | return 0; |
edd16368 SC |
1523 | } |
1524 | ||
1525 | static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h, | |
1526 | struct CommandList *c) | |
1527 | { | |
1528 | DECLARE_COMPLETION_ONSTACK(wait); | |
1529 | ||
1530 | c->waiting = &wait; | |
1531 | enqueue_cmd_and_start_io(h, c); | |
1532 | wait_for_completion(&wait); | |
1533 | } | |
1534 | ||
a0c12413 SC |
1535 | static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h, |
1536 | struct CommandList *c) | |
1537 | { | |
1538 | unsigned long flags; | |
1539 | ||
1540 | /* If controller lockup detected, fake a hardware error. */ | |
1541 | spin_lock_irqsave(&h->lock, flags); | |
1542 | if (unlikely(h->lockup_detected)) { | |
1543 | spin_unlock_irqrestore(&h->lock, flags); | |
1544 | c->err_info->CommandStatus = CMD_HARDWARE_ERR; | |
1545 | } else { | |
1546 | spin_unlock_irqrestore(&h->lock, flags); | |
1547 | hpsa_scsi_do_simple_cmd_core(h, c); | |
1548 | } | |
1549 | } | |
1550 | ||
9c2fc160 | 1551 | #define MAX_DRIVER_CMD_RETRIES 25 |
edd16368 SC |
1552 | static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h, |
1553 | struct CommandList *c, int data_direction) | |
1554 | { | |
9c2fc160 | 1555 | int backoff_time = 10, retry_count = 0; |
edd16368 SC |
1556 | |
1557 | do { | |
7630abd0 | 1558 | memset(c->err_info, 0, sizeof(*c->err_info)); |
edd16368 SC |
1559 | hpsa_scsi_do_simple_cmd_core(h, c); |
1560 | retry_count++; | |
9c2fc160 SC |
1561 | if (retry_count > 3) { |
1562 | msleep(backoff_time); | |
1563 | if (backoff_time < 1000) | |
1564 | backoff_time *= 2; | |
1565 | } | |
852af20a | 1566 | } while ((check_for_unit_attention(h, c) || |
9c2fc160 SC |
1567 | check_for_busy(h, c)) && |
1568 | retry_count <= MAX_DRIVER_CMD_RETRIES); | |
edd16368 SC |
1569 | hpsa_pci_unmap(h->pdev, c, 1, data_direction); |
1570 | } | |
1571 | ||
1572 | static void hpsa_scsi_interpret_error(struct CommandList *cp) | |
1573 | { | |
1574 | struct ErrorInfo *ei; | |
1575 | struct device *d = &cp->h->pdev->dev; | |
1576 | ||
1577 | ei = cp->err_info; | |
1578 | switch (ei->CommandStatus) { | |
1579 | case CMD_TARGET_STATUS: | |
1580 | dev_warn(d, "cmd %p has completed with errors\n", cp); | |
1581 | dev_warn(d, "cmd %p has SCSI Status = %x\n", cp, | |
1582 | ei->ScsiStatus); | |
1583 | if (ei->ScsiStatus == 0) | |
1584 | dev_warn(d, "SCSI status is abnormally zero. " | |
1585 | "(probably indicates selection timeout " | |
1586 | "reported incorrectly due to a known " | |
1587 | "firmware bug, circa July, 2001.)\n"); | |
1588 | break; | |
1589 | case CMD_DATA_UNDERRUN: /* let mid layer handle it. */ | |
1590 | dev_info(d, "UNDERRUN\n"); | |
1591 | break; | |
1592 | case CMD_DATA_OVERRUN: | |
1593 | dev_warn(d, "cp %p has completed with data overrun\n", cp); | |
1594 | break; | |
1595 | case CMD_INVALID: { | |
1596 | /* controller unfortunately reports SCSI passthru's | |
1597 | * to non-existent targets as invalid commands. | |
1598 | */ | |
1599 | dev_warn(d, "cp %p is reported invalid (probably means " | |
1600 | "target device no longer present)\n", cp); | |
1601 | /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0); | |
1602 | print_cmd(cp); */ | |
1603 | } | |
1604 | break; | |
1605 | case CMD_PROTOCOL_ERR: | |
1606 | dev_warn(d, "cp %p has protocol error \n", cp); | |
1607 | break; | |
1608 | case CMD_HARDWARE_ERR: | |
1609 | /* cmd->result = DID_ERROR << 16; */ | |
1610 | dev_warn(d, "cp %p had hardware error\n", cp); | |
1611 | break; | |
1612 | case CMD_CONNECTION_LOST: | |
1613 | dev_warn(d, "cp %p had connection lost\n", cp); | |
1614 | break; | |
1615 | case CMD_ABORTED: | |
1616 | dev_warn(d, "cp %p was aborted\n", cp); | |
1617 | break; | |
1618 | case CMD_ABORT_FAILED: | |
1619 | dev_warn(d, "cp %p reports abort failed\n", cp); | |
1620 | break; | |
1621 | case CMD_UNSOLICITED_ABORT: | |
1622 | dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp); | |
1623 | break; | |
1624 | case CMD_TIMEOUT: | |
1625 | dev_warn(d, "cp %p timed out\n", cp); | |
1626 | break; | |
1d5e2ed0 SC |
1627 | case CMD_UNABORTABLE: |
1628 | dev_warn(d, "Command unabortable\n"); | |
1629 | break; | |
edd16368 SC |
1630 | default: |
1631 | dev_warn(d, "cp %p returned unknown status %x\n", cp, | |
1632 | ei->CommandStatus); | |
1633 | } | |
1634 | } | |
1635 | ||
1636 | static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr, | |
1637 | unsigned char page, unsigned char *buf, | |
1638 | unsigned char bufsize) | |
1639 | { | |
1640 | int rc = IO_OK; | |
1641 | struct CommandList *c; | |
1642 | struct ErrorInfo *ei; | |
1643 | ||
1644 | c = cmd_special_alloc(h); | |
1645 | ||
1646 | if (c == NULL) { /* trouble... */ | |
1647 | dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); | |
ecd9aad4 | 1648 | return -ENOMEM; |
edd16368 SC |
1649 | } |
1650 | ||
a2dac136 SC |
1651 | if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, |
1652 | page, scsi3addr, TYPE_CMD)) { | |
1653 | rc = -1; | |
1654 | goto out; | |
1655 | } | |
edd16368 SC |
1656 | hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE); |
1657 | ei = c->err_info; | |
1658 | if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) { | |
1659 | hpsa_scsi_interpret_error(c); | |
1660 | rc = -1; | |
1661 | } | |
a2dac136 | 1662 | out: |
edd16368 SC |
1663 | cmd_special_free(h, c); |
1664 | return rc; | |
1665 | } | |
1666 | ||
1667 | static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr) | |
1668 | { | |
1669 | int rc = IO_OK; | |
1670 | struct CommandList *c; | |
1671 | struct ErrorInfo *ei; | |
1672 | ||
1673 | c = cmd_special_alloc(h); | |
1674 | ||
1675 | if (c == NULL) { /* trouble... */ | |
1676 | dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); | |
e9ea04a6 | 1677 | return -ENOMEM; |
edd16368 SC |
1678 | } |
1679 | ||
a2dac136 SC |
1680 | /* fill_cmd can't fail here, no data buffer to map. */ |
1681 | (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, | |
1682 | NULL, 0, 0, scsi3addr, TYPE_MSG); | |
edd16368 SC |
1683 | hpsa_scsi_do_simple_cmd_core(h, c); |
1684 | /* no unmap needed here because no data xfer. */ | |
1685 | ||
1686 | ei = c->err_info; | |
1687 | if (ei->CommandStatus != 0) { | |
1688 | hpsa_scsi_interpret_error(c); | |
1689 | rc = -1; | |
1690 | } | |
1691 | cmd_special_free(h, c); | |
1692 | return rc; | |
1693 | } | |
1694 | ||
1695 | static void hpsa_get_raid_level(struct ctlr_info *h, | |
1696 | unsigned char *scsi3addr, unsigned char *raid_level) | |
1697 | { | |
1698 | int rc; | |
1699 | unsigned char *buf; | |
1700 | ||
1701 | *raid_level = RAID_UNKNOWN; | |
1702 | buf = kzalloc(64, GFP_KERNEL); | |
1703 | if (!buf) | |
1704 | return; | |
1705 | rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64); | |
1706 | if (rc == 0) | |
1707 | *raid_level = buf[8]; | |
1708 | if (*raid_level > RAID_UNKNOWN) | |
1709 | *raid_level = RAID_UNKNOWN; | |
1710 | kfree(buf); | |
1711 | return; | |
1712 | } | |
1713 | ||
283b4a9b SC |
1714 | #define HPSA_MAP_DEBUG |
1715 | #ifdef HPSA_MAP_DEBUG | |
1716 | static void hpsa_debug_map_buff(struct ctlr_info *h, int rc, | |
1717 | struct raid_map_data *map_buff) | |
1718 | { | |
1719 | struct raid_map_disk_data *dd = &map_buff->data[0]; | |
1720 | int map, row, col; | |
1721 | u16 map_cnt, row_cnt, disks_per_row; | |
1722 | ||
1723 | if (rc != 0) | |
1724 | return; | |
1725 | ||
1726 | dev_info(&h->pdev->dev, "structure_size = %u\n", | |
1727 | le32_to_cpu(map_buff->structure_size)); | |
1728 | dev_info(&h->pdev->dev, "volume_blk_size = %u\n", | |
1729 | le32_to_cpu(map_buff->volume_blk_size)); | |
1730 | dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n", | |
1731 | le64_to_cpu(map_buff->volume_blk_cnt)); | |
1732 | dev_info(&h->pdev->dev, "physicalBlockShift = %u\n", | |
1733 | map_buff->phys_blk_shift); | |
1734 | dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n", | |
1735 | map_buff->parity_rotation_shift); | |
1736 | dev_info(&h->pdev->dev, "strip_size = %u\n", | |
1737 | le16_to_cpu(map_buff->strip_size)); | |
1738 | dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n", | |
1739 | le64_to_cpu(map_buff->disk_starting_blk)); | |
1740 | dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n", | |
1741 | le64_to_cpu(map_buff->disk_blk_cnt)); | |
1742 | dev_info(&h->pdev->dev, "data_disks_per_row = %u\n", | |
1743 | le16_to_cpu(map_buff->data_disks_per_row)); | |
1744 | dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n", | |
1745 | le16_to_cpu(map_buff->metadata_disks_per_row)); | |
1746 | dev_info(&h->pdev->dev, "row_cnt = %u\n", | |
1747 | le16_to_cpu(map_buff->row_cnt)); | |
1748 | dev_info(&h->pdev->dev, "layout_map_count = %u\n", | |
1749 | le16_to_cpu(map_buff->layout_map_count)); | |
1750 | ||
1751 | map_cnt = le16_to_cpu(map_buff->layout_map_count); | |
1752 | for (map = 0; map < map_cnt; map++) { | |
1753 | dev_info(&h->pdev->dev, "Map%u:\n", map); | |
1754 | row_cnt = le16_to_cpu(map_buff->row_cnt); | |
1755 | for (row = 0; row < row_cnt; row++) { | |
1756 | dev_info(&h->pdev->dev, " Row%u:\n", row); | |
1757 | disks_per_row = | |
1758 | le16_to_cpu(map_buff->data_disks_per_row); | |
1759 | for (col = 0; col < disks_per_row; col++, dd++) | |
1760 | dev_info(&h->pdev->dev, | |
1761 | " D%02u: h=0x%04x xor=%u,%u\n", | |
1762 | col, dd->ioaccel_handle, | |
1763 | dd->xor_mult[0], dd->xor_mult[1]); | |
1764 | disks_per_row = | |
1765 | le16_to_cpu(map_buff->metadata_disks_per_row); | |
1766 | for (col = 0; col < disks_per_row; col++, dd++) | |
1767 | dev_info(&h->pdev->dev, | |
1768 | " M%02u: h=0x%04x xor=%u,%u\n", | |
1769 | col, dd->ioaccel_handle, | |
1770 | dd->xor_mult[0], dd->xor_mult[1]); | |
1771 | } | |
1772 | } | |
1773 | } | |
1774 | #else | |
1775 | static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h, | |
1776 | __attribute__((unused)) int rc, | |
1777 | __attribute__((unused)) struct raid_map_data *map_buff) | |
1778 | { | |
1779 | } | |
1780 | #endif | |
1781 | ||
1782 | static int hpsa_get_raid_map(struct ctlr_info *h, | |
1783 | unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device) | |
1784 | { | |
1785 | int rc = 0; | |
1786 | struct CommandList *c; | |
1787 | struct ErrorInfo *ei; | |
1788 | ||
1789 | c = cmd_special_alloc(h); | |
1790 | if (c == NULL) { | |
1791 | dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); | |
1792 | return -ENOMEM; | |
1793 | } | |
1794 | if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map, | |
1795 | sizeof(this_device->raid_map), 0, | |
1796 | scsi3addr, TYPE_CMD)) { | |
1797 | dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n"); | |
1798 | cmd_special_free(h, c); | |
1799 | return -ENOMEM; | |
1800 | } | |
1801 | hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE); | |
1802 | ei = c->err_info; | |
1803 | if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) { | |
1804 | hpsa_scsi_interpret_error(c); | |
1805 | cmd_special_free(h, c); | |
1806 | return -1; | |
1807 | } | |
1808 | cmd_special_free(h, c); | |
1809 | ||
1810 | /* @todo in the future, dynamically allocate RAID map memory */ | |
1811 | if (le32_to_cpu(this_device->raid_map.structure_size) > | |
1812 | sizeof(this_device->raid_map)) { | |
1813 | dev_warn(&h->pdev->dev, "RAID map size is too large!\n"); | |
1814 | rc = -1; | |
1815 | } | |
1816 | hpsa_debug_map_buff(h, rc, &this_device->raid_map); | |
1817 | return rc; | |
1818 | } | |
1819 | ||
1820 | static void hpsa_get_ioaccel_status(struct ctlr_info *h, | |
1821 | unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device) | |
1822 | { | |
1823 | int rc; | |
1824 | unsigned char *buf; | |
1825 | u8 ioaccel_status; | |
1826 | ||
1827 | this_device->offload_config = 0; | |
1828 | this_device->offload_enabled = 0; | |
1829 | ||
1830 | buf = kzalloc(64, GFP_KERNEL); | |
1831 | if (!buf) | |
1832 | return; | |
1833 | rc = hpsa_scsi_do_inquiry(h, scsi3addr, | |
1834 | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64); | |
1835 | if (rc != 0) | |
1836 | goto out; | |
1837 | ||
1838 | #define IOACCEL_STATUS_BYTE 4 | |
1839 | #define OFFLOAD_CONFIGURED_BIT 0x01 | |
1840 | #define OFFLOAD_ENABLED_BIT 0x02 | |
1841 | ioaccel_status = buf[IOACCEL_STATUS_BYTE]; | |
1842 | this_device->offload_config = | |
1843 | !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT); | |
1844 | if (this_device->offload_config) { | |
1845 | this_device->offload_enabled = | |
1846 | !!(ioaccel_status & OFFLOAD_ENABLED_BIT); | |
1847 | if (hpsa_get_raid_map(h, scsi3addr, this_device)) | |
1848 | this_device->offload_enabled = 0; | |
1849 | } | |
1850 | out: | |
1851 | kfree(buf); | |
1852 | return; | |
1853 | } | |
1854 | ||
edd16368 SC |
1855 | /* Get the device id from inquiry page 0x83 */ |
1856 | static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr, | |
1857 | unsigned char *device_id, int buflen) | |
1858 | { | |
1859 | int rc; | |
1860 | unsigned char *buf; | |
1861 | ||
1862 | if (buflen > 16) | |
1863 | buflen = 16; | |
1864 | buf = kzalloc(64, GFP_KERNEL); | |
1865 | if (!buf) | |
1866 | return -1; | |
1867 | rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64); | |
1868 | if (rc == 0) | |
1869 | memcpy(device_id, &buf[8], buflen); | |
1870 | kfree(buf); | |
1871 | return rc != 0; | |
1872 | } | |
1873 | ||
1874 | static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical, | |
1875 | struct ReportLUNdata *buf, int bufsize, | |
1876 | int extended_response) | |
1877 | { | |
1878 | int rc = IO_OK; | |
1879 | struct CommandList *c; | |
1880 | unsigned char scsi3addr[8]; | |
1881 | struct ErrorInfo *ei; | |
1882 | ||
1883 | c = cmd_special_alloc(h); | |
1884 | if (c == NULL) { /* trouble... */ | |
1885 | dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); | |
1886 | return -1; | |
1887 | } | |
e89c0ae7 SC |
1888 | /* address the controller */ |
1889 | memset(scsi3addr, 0, sizeof(scsi3addr)); | |
a2dac136 SC |
1890 | if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h, |
1891 | buf, bufsize, 0, scsi3addr, TYPE_CMD)) { | |
1892 | rc = -1; | |
1893 | goto out; | |
1894 | } | |
edd16368 SC |
1895 | if (extended_response) |
1896 | c->Request.CDB[1] = extended_response; | |
1897 | hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE); | |
1898 | ei = c->err_info; | |
1899 | if (ei->CommandStatus != 0 && | |
1900 | ei->CommandStatus != CMD_DATA_UNDERRUN) { | |
1901 | hpsa_scsi_interpret_error(c); | |
1902 | rc = -1; | |
283b4a9b SC |
1903 | } else { |
1904 | if (buf->extended_response_flag != extended_response) { | |
1905 | dev_err(&h->pdev->dev, | |
1906 | "report luns requested format %u, got %u\n", | |
1907 | extended_response, | |
1908 | buf->extended_response_flag); | |
1909 | rc = -1; | |
1910 | } | |
edd16368 | 1911 | } |
a2dac136 | 1912 | out: |
edd16368 SC |
1913 | cmd_special_free(h, c); |
1914 | return rc; | |
1915 | } | |
1916 | ||
1917 | static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h, | |
1918 | struct ReportLUNdata *buf, | |
1919 | int bufsize, int extended_response) | |
1920 | { | |
1921 | return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response); | |
1922 | } | |
1923 | ||
1924 | static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h, | |
1925 | struct ReportLUNdata *buf, int bufsize) | |
1926 | { | |
1927 | return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0); | |
1928 | } | |
1929 | ||
1930 | static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device, | |
1931 | int bus, int target, int lun) | |
1932 | { | |
1933 | device->bus = bus; | |
1934 | device->target = target; | |
1935 | device->lun = lun; | |
1936 | } | |
1937 | ||
1938 | static int hpsa_update_device_info(struct ctlr_info *h, | |
0b0e1d6c SC |
1939 | unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device, |
1940 | unsigned char *is_OBDR_device) | |
edd16368 | 1941 | { |
0b0e1d6c SC |
1942 | |
1943 | #define OBDR_SIG_OFFSET 43 | |
1944 | #define OBDR_TAPE_SIG "$DR-10" | |
1945 | #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1) | |
1946 | #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN) | |
1947 | ||
ea6d3bc3 | 1948 | unsigned char *inq_buff; |
0b0e1d6c | 1949 | unsigned char *obdr_sig; |
edd16368 | 1950 | |
ea6d3bc3 | 1951 | inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL); |
edd16368 SC |
1952 | if (!inq_buff) |
1953 | goto bail_out; | |
1954 | ||
edd16368 SC |
1955 | /* Do an inquiry to the device to see what it is. */ |
1956 | if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff, | |
1957 | (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) { | |
1958 | /* Inquiry failed (msg printed already) */ | |
1959 | dev_err(&h->pdev->dev, | |
1960 | "hpsa_update_device_info: inquiry failed\n"); | |
1961 | goto bail_out; | |
1962 | } | |
1963 | ||
edd16368 SC |
1964 | this_device->devtype = (inq_buff[0] & 0x1f); |
1965 | memcpy(this_device->scsi3addr, scsi3addr, 8); | |
1966 | memcpy(this_device->vendor, &inq_buff[8], | |
1967 | sizeof(this_device->vendor)); | |
1968 | memcpy(this_device->model, &inq_buff[16], | |
1969 | sizeof(this_device->model)); | |
edd16368 SC |
1970 | memset(this_device->device_id, 0, |
1971 | sizeof(this_device->device_id)); | |
1972 | hpsa_get_device_id(h, scsi3addr, this_device->device_id, | |
1973 | sizeof(this_device->device_id)); | |
1974 | ||
1975 | if (this_device->devtype == TYPE_DISK && | |
283b4a9b | 1976 | is_logical_dev_addr_mode(scsi3addr)) { |
edd16368 | 1977 | hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level); |
283b4a9b SC |
1978 | if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC) |
1979 | hpsa_get_ioaccel_status(h, scsi3addr, this_device); | |
1980 | } else { | |
edd16368 | 1981 | this_device->raid_level = RAID_UNKNOWN; |
283b4a9b SC |
1982 | this_device->offload_config = 0; |
1983 | this_device->offload_enabled = 0; | |
1984 | } | |
edd16368 | 1985 | |
0b0e1d6c SC |
1986 | if (is_OBDR_device) { |
1987 | /* See if this is a One-Button-Disaster-Recovery device | |
1988 | * by looking for "$DR-10" at offset 43 in inquiry data. | |
1989 | */ | |
1990 | obdr_sig = &inq_buff[OBDR_SIG_OFFSET]; | |
1991 | *is_OBDR_device = (this_device->devtype == TYPE_ROM && | |
1992 | strncmp(obdr_sig, OBDR_TAPE_SIG, | |
1993 | OBDR_SIG_LEN) == 0); | |
1994 | } | |
1995 | ||
edd16368 SC |
1996 | kfree(inq_buff); |
1997 | return 0; | |
1998 | ||
1999 | bail_out: | |
2000 | kfree(inq_buff); | |
2001 | return 1; | |
2002 | } | |
2003 | ||
4f4eb9f1 | 2004 | static unsigned char *ext_target_model[] = { |
edd16368 SC |
2005 | "MSA2012", |
2006 | "MSA2024", | |
2007 | "MSA2312", | |
2008 | "MSA2324", | |
fda38518 | 2009 | "P2000 G3 SAS", |
e06c8e5c | 2010 | "MSA 2040 SAS", |
edd16368 SC |
2011 | NULL, |
2012 | }; | |
2013 | ||
4f4eb9f1 | 2014 | static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device) |
edd16368 SC |
2015 | { |
2016 | int i; | |
2017 | ||
4f4eb9f1 ST |
2018 | for (i = 0; ext_target_model[i]; i++) |
2019 | if (strncmp(device->model, ext_target_model[i], | |
2020 | strlen(ext_target_model[i])) == 0) | |
edd16368 SC |
2021 | return 1; |
2022 | return 0; | |
2023 | } | |
2024 | ||
2025 | /* Helper function to assign bus, target, lun mapping of devices. | |
4f4eb9f1 | 2026 | * Puts non-external target logical volumes on bus 0, external target logical |
edd16368 SC |
2027 | * volumes on bus 1, physical devices on bus 2. and the hba on bus 3. |
2028 | * Logical drive target and lun are assigned at this time, but | |
2029 | * physical device lun and target assignment are deferred (assigned | |
2030 | * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.) | |
2031 | */ | |
2032 | static void figure_bus_target_lun(struct ctlr_info *h, | |
1f310bde | 2033 | u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device) |
edd16368 | 2034 | { |
1f310bde SC |
2035 | u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes)); |
2036 | ||
2037 | if (!is_logical_dev_addr_mode(lunaddrbytes)) { | |
2038 | /* physical device, target and lun filled in later */ | |
edd16368 | 2039 | if (is_hba_lunid(lunaddrbytes)) |
1f310bde | 2040 | hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff); |
edd16368 | 2041 | else |
1f310bde SC |
2042 | /* defer target, lun assignment for physical devices */ |
2043 | hpsa_set_bus_target_lun(device, 2, -1, -1); | |
2044 | return; | |
2045 | } | |
2046 | /* It's a logical device */ | |
4f4eb9f1 ST |
2047 | if (is_ext_target(h, device)) { |
2048 | /* external target way, put logicals on bus 1 | |
1f310bde SC |
2049 | * and match target/lun numbers box |
2050 | * reports, other smart array, bus 0, target 0, match lunid | |
2051 | */ | |
2052 | hpsa_set_bus_target_lun(device, | |
2053 | 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff); | |
2054 | return; | |
edd16368 | 2055 | } |
1f310bde | 2056 | hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff); |
edd16368 SC |
2057 | } |
2058 | ||
2059 | /* | |
2060 | * If there is no lun 0 on a target, linux won't find any devices. | |
4f4eb9f1 | 2061 | * For the external targets (arrays), we have to manually detect the enclosure |
edd16368 SC |
2062 | * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report |
2063 | * it for some reason. *tmpdevice is the target we're adding, | |
2064 | * this_device is a pointer into the current element of currentsd[] | |
2065 | * that we're building up in update_scsi_devices(), below. | |
2066 | * lunzerobits is a bitmap that tracks which targets already have a | |
2067 | * lun 0 assigned. | |
2068 | * Returns 1 if an enclosure was added, 0 if not. | |
2069 | */ | |
4f4eb9f1 | 2070 | static int add_ext_target_dev(struct ctlr_info *h, |
edd16368 | 2071 | struct hpsa_scsi_dev_t *tmpdevice, |
01a02ffc | 2072 | struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes, |
4f4eb9f1 | 2073 | unsigned long lunzerobits[], int *n_ext_target_devs) |
edd16368 SC |
2074 | { |
2075 | unsigned char scsi3addr[8]; | |
2076 | ||
1f310bde | 2077 | if (test_bit(tmpdevice->target, lunzerobits)) |
edd16368 SC |
2078 | return 0; /* There is already a lun 0 on this target. */ |
2079 | ||
2080 | if (!is_logical_dev_addr_mode(lunaddrbytes)) | |
2081 | return 0; /* It's the logical targets that may lack lun 0. */ | |
2082 | ||
4f4eb9f1 ST |
2083 | if (!is_ext_target(h, tmpdevice)) |
2084 | return 0; /* Only external target devices have this problem. */ | |
edd16368 | 2085 | |
1f310bde | 2086 | if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */ |
edd16368 SC |
2087 | return 0; |
2088 | ||
c4f8a299 | 2089 | memset(scsi3addr, 0, 8); |
1f310bde | 2090 | scsi3addr[3] = tmpdevice->target; |
edd16368 SC |
2091 | if (is_hba_lunid(scsi3addr)) |
2092 | return 0; /* Don't add the RAID controller here. */ | |
2093 | ||
339b2b14 SC |
2094 | if (is_scsi_rev_5(h)) |
2095 | return 0; /* p1210m doesn't need to do this. */ | |
2096 | ||
4f4eb9f1 | 2097 | if (*n_ext_target_devs >= MAX_EXT_TARGETS) { |
aca4a520 ST |
2098 | dev_warn(&h->pdev->dev, "Maximum number of external " |
2099 | "target devices exceeded. Check your hardware " | |
edd16368 SC |
2100 | "configuration."); |
2101 | return 0; | |
2102 | } | |
2103 | ||
0b0e1d6c | 2104 | if (hpsa_update_device_info(h, scsi3addr, this_device, NULL)) |
edd16368 | 2105 | return 0; |
4f4eb9f1 | 2106 | (*n_ext_target_devs)++; |
1f310bde SC |
2107 | hpsa_set_bus_target_lun(this_device, |
2108 | tmpdevice->bus, tmpdevice->target, 0); | |
2109 | set_bit(tmpdevice->target, lunzerobits); | |
edd16368 SC |
2110 | return 1; |
2111 | } | |
2112 | ||
2113 | /* | |
2114 | * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev, | |
2115 | * logdev. The number of luns in physdev and logdev are returned in | |
2116 | * *nphysicals and *nlogicals, respectively. | |
2117 | * Returns 0 on success, -1 otherwise. | |
2118 | */ | |
2119 | static int hpsa_gather_lun_info(struct ctlr_info *h, | |
2120 | int reportlunsize, | |
283b4a9b | 2121 | struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode, |
01a02ffc | 2122 | struct ReportLUNdata *logdev, u32 *nlogicals) |
edd16368 | 2123 | { |
283b4a9b SC |
2124 | int physical_entry_size = 8; |
2125 | ||
2126 | *physical_mode = 0; | |
2127 | ||
2128 | /* For I/O accelerator mode we need to read physical device handles */ | |
2129 | if (h->transMethod & CFGTBL_Trans_io_accel1) { | |
2130 | *physical_mode = HPSA_REPORT_PHYS_EXTENDED; | |
2131 | physical_entry_size = 24; | |
2132 | } | |
a93aa1fe | 2133 | if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, |
283b4a9b | 2134 | *physical_mode)) { |
edd16368 SC |
2135 | dev_err(&h->pdev->dev, "report physical LUNs failed.\n"); |
2136 | return -1; | |
2137 | } | |
283b4a9b SC |
2138 | *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / |
2139 | physical_entry_size; | |
edd16368 SC |
2140 | if (*nphysicals > HPSA_MAX_PHYS_LUN) { |
2141 | dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded." | |
2142 | " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN, | |
2143 | *nphysicals - HPSA_MAX_PHYS_LUN); | |
2144 | *nphysicals = HPSA_MAX_PHYS_LUN; | |
2145 | } | |
2146 | if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) { | |
2147 | dev_err(&h->pdev->dev, "report logical LUNs failed.\n"); | |
2148 | return -1; | |
2149 | } | |
6df1e954 | 2150 | *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8; |
edd16368 SC |
2151 | /* Reject Logicals in excess of our max capability. */ |
2152 | if (*nlogicals > HPSA_MAX_LUN) { | |
2153 | dev_warn(&h->pdev->dev, | |
2154 | "maximum logical LUNs (%d) exceeded. " | |
2155 | "%d LUNs ignored.\n", HPSA_MAX_LUN, | |
2156 | *nlogicals - HPSA_MAX_LUN); | |
2157 | *nlogicals = HPSA_MAX_LUN; | |
2158 | } | |
2159 | if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) { | |
2160 | dev_warn(&h->pdev->dev, | |
2161 | "maximum logical + physical LUNs (%d) exceeded. " | |
2162 | "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN, | |
2163 | *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN); | |
2164 | *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals; | |
2165 | } | |
2166 | return 0; | |
2167 | } | |
2168 | ||
339b2b14 | 2169 | u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i, |
a93aa1fe MG |
2170 | int nphysicals, int nlogicals, |
2171 | struct ReportExtendedLUNdata *physdev_list, | |
339b2b14 SC |
2172 | struct ReportLUNdata *logdev_list) |
2173 | { | |
2174 | /* Helper function, figure out where the LUN ID info is coming from | |
2175 | * given index i, lists of physical and logical devices, where in | |
2176 | * the list the raid controller is supposed to appear (first or last) | |
2177 | */ | |
2178 | ||
2179 | int logicals_start = nphysicals + (raid_ctlr_position == 0); | |
2180 | int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0); | |
2181 | ||
2182 | if (i == raid_ctlr_position) | |
2183 | return RAID_CTLR_LUNID; | |
2184 | ||
2185 | if (i < logicals_start) | |
2186 | return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0]; | |
2187 | ||
2188 | if (i < last_device) | |
2189 | return &logdev_list->LUN[i - nphysicals - | |
2190 | (raid_ctlr_position == 0)][0]; | |
2191 | BUG(); | |
2192 | return NULL; | |
2193 | } | |
2194 | ||
edd16368 SC |
2195 | static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno) |
2196 | { | |
2197 | /* the idea here is we could get notified | |
2198 | * that some devices have changed, so we do a report | |
2199 | * physical luns and report logical luns cmd, and adjust | |
2200 | * our list of devices accordingly. | |
2201 | * | |
2202 | * The scsi3addr's of devices won't change so long as the | |
2203 | * adapter is not reset. That means we can rescan and | |
2204 | * tell which devices we already know about, vs. new | |
2205 | * devices, vs. disappearing devices. | |
2206 | */ | |
a93aa1fe | 2207 | struct ReportExtendedLUNdata *physdev_list = NULL; |
edd16368 | 2208 | struct ReportLUNdata *logdev_list = NULL; |
01a02ffc SC |
2209 | u32 nphysicals = 0; |
2210 | u32 nlogicals = 0; | |
283b4a9b | 2211 | int physical_mode = 0; |
01a02ffc | 2212 | u32 ndev_allocated = 0; |
edd16368 SC |
2213 | struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice; |
2214 | int ncurrent = 0; | |
283b4a9b | 2215 | int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24; |
4f4eb9f1 | 2216 | int i, n_ext_target_devs, ndevs_to_allocate; |
339b2b14 | 2217 | int raid_ctlr_position; |
aca4a520 | 2218 | DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS); |
edd16368 | 2219 | |
cfe5badc | 2220 | currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL); |
edd16368 SC |
2221 | physdev_list = kzalloc(reportlunsize, GFP_KERNEL); |
2222 | logdev_list = kzalloc(reportlunsize, GFP_KERNEL); | |
edd16368 SC |
2223 | tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL); |
2224 | ||
0b0e1d6c | 2225 | if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) { |
edd16368 SC |
2226 | dev_err(&h->pdev->dev, "out of memory\n"); |
2227 | goto out; | |
2228 | } | |
2229 | memset(lunzerobits, 0, sizeof(lunzerobits)); | |
2230 | ||
a93aa1fe MG |
2231 | if (hpsa_gather_lun_info(h, reportlunsize, |
2232 | (struct ReportLUNdata *) physdev_list, &nphysicals, | |
283b4a9b | 2233 | &physical_mode, logdev_list, &nlogicals)) |
edd16368 SC |
2234 | goto out; |
2235 | ||
aca4a520 ST |
2236 | /* We might see up to the maximum number of logical and physical disks |
2237 | * plus external target devices, and a device for the local RAID | |
2238 | * controller. | |
edd16368 | 2239 | */ |
aca4a520 | 2240 | ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1; |
edd16368 SC |
2241 | |
2242 | /* Allocate the per device structures */ | |
2243 | for (i = 0; i < ndevs_to_allocate; i++) { | |
b7ec021f ST |
2244 | if (i >= HPSA_MAX_DEVICES) { |
2245 | dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded." | |
2246 | " %d devices ignored.\n", HPSA_MAX_DEVICES, | |
2247 | ndevs_to_allocate - HPSA_MAX_DEVICES); | |
2248 | break; | |
2249 | } | |
2250 | ||
edd16368 SC |
2251 | currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL); |
2252 | if (!currentsd[i]) { | |
2253 | dev_warn(&h->pdev->dev, "out of memory at %s:%d\n", | |
2254 | __FILE__, __LINE__); | |
2255 | goto out; | |
2256 | } | |
2257 | ndev_allocated++; | |
2258 | } | |
2259 | ||
339b2b14 SC |
2260 | if (unlikely(is_scsi_rev_5(h))) |
2261 | raid_ctlr_position = 0; | |
2262 | else | |
2263 | raid_ctlr_position = nphysicals + nlogicals; | |
2264 | ||
edd16368 | 2265 | /* adjust our table of devices */ |
4f4eb9f1 | 2266 | n_ext_target_devs = 0; |
edd16368 | 2267 | for (i = 0; i < nphysicals + nlogicals + 1; i++) { |
0b0e1d6c | 2268 | u8 *lunaddrbytes, is_OBDR = 0; |
edd16368 SC |
2269 | |
2270 | /* Figure out where the LUN ID info is coming from */ | |
339b2b14 SC |
2271 | lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position, |
2272 | i, nphysicals, nlogicals, physdev_list, logdev_list); | |
edd16368 | 2273 | /* skip masked physical devices. */ |
339b2b14 SC |
2274 | if (lunaddrbytes[3] & 0xC0 && |
2275 | i < nphysicals + (raid_ctlr_position == 0)) | |
edd16368 SC |
2276 | continue; |
2277 | ||
2278 | /* Get device type, vendor, model, device id */ | |
0b0e1d6c SC |
2279 | if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice, |
2280 | &is_OBDR)) | |
edd16368 | 2281 | continue; /* skip it if we can't talk to it. */ |
1f310bde | 2282 | figure_bus_target_lun(h, lunaddrbytes, tmpdevice); |
edd16368 SC |
2283 | this_device = currentsd[ncurrent]; |
2284 | ||
2285 | /* | |
4f4eb9f1 | 2286 | * For external target devices, we have to insert a LUN 0 which |
edd16368 SC |
2287 | * doesn't show up in CCISS_REPORT_PHYSICAL data, but there |
2288 | * is nonetheless an enclosure device there. We have to | |
2289 | * present that otherwise linux won't find anything if | |
2290 | * there is no lun 0. | |
2291 | */ | |
4f4eb9f1 | 2292 | if (add_ext_target_dev(h, tmpdevice, this_device, |
1f310bde | 2293 | lunaddrbytes, lunzerobits, |
4f4eb9f1 | 2294 | &n_ext_target_devs)) { |
edd16368 SC |
2295 | ncurrent++; |
2296 | this_device = currentsd[ncurrent]; | |
2297 | } | |
2298 | ||
2299 | *this_device = *tmpdevice; | |
edd16368 SC |
2300 | |
2301 | switch (this_device->devtype) { | |
0b0e1d6c | 2302 | case TYPE_ROM: |
edd16368 SC |
2303 | /* We don't *really* support actual CD-ROM devices, |
2304 | * just "One Button Disaster Recovery" tape drive | |
2305 | * which temporarily pretends to be a CD-ROM drive. | |
2306 | * So we check that the device is really an OBDR tape | |
2307 | * device by checking for "$DR-10" in bytes 43-48 of | |
2308 | * the inquiry data. | |
2309 | */ | |
0b0e1d6c SC |
2310 | if (is_OBDR) |
2311 | ncurrent++; | |
edd16368 SC |
2312 | break; |
2313 | case TYPE_DISK: | |
283b4a9b SC |
2314 | if (i >= nphysicals) { |
2315 | ncurrent++; | |
edd16368 | 2316 | break; |
283b4a9b SC |
2317 | } |
2318 | if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) { | |
2319 | memcpy(&this_device->ioaccel_handle, | |
2320 | &lunaddrbytes[20], | |
2321 | sizeof(this_device->ioaccel_handle)); | |
2322 | ncurrent++; | |
2323 | } | |
edd16368 SC |
2324 | break; |
2325 | case TYPE_TAPE: | |
2326 | case TYPE_MEDIUM_CHANGER: | |
2327 | ncurrent++; | |
2328 | break; | |
2329 | case TYPE_RAID: | |
2330 | /* Only present the Smartarray HBA as a RAID controller. | |
2331 | * If it's a RAID controller other than the HBA itself | |
2332 | * (an external RAID controller, MSA500 or similar) | |
2333 | * don't present it. | |
2334 | */ | |
2335 | if (!is_hba_lunid(lunaddrbytes)) | |
2336 | break; | |
2337 | ncurrent++; | |
2338 | break; | |
2339 | default: | |
2340 | break; | |
2341 | } | |
cfe5badc | 2342 | if (ncurrent >= HPSA_MAX_DEVICES) |
edd16368 SC |
2343 | break; |
2344 | } | |
2345 | adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent); | |
2346 | out: | |
2347 | kfree(tmpdevice); | |
2348 | for (i = 0; i < ndev_allocated; i++) | |
2349 | kfree(currentsd[i]); | |
2350 | kfree(currentsd); | |
edd16368 SC |
2351 | kfree(physdev_list); |
2352 | kfree(logdev_list); | |
edd16368 SC |
2353 | } |
2354 | ||
2355 | /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci | |
2356 | * dma mapping and fills in the scatter gather entries of the | |
2357 | * hpsa command, cp. | |
2358 | */ | |
33a2ffce | 2359 | static int hpsa_scatter_gather(struct ctlr_info *h, |
edd16368 SC |
2360 | struct CommandList *cp, |
2361 | struct scsi_cmnd *cmd) | |
2362 | { | |
2363 | unsigned int len; | |
2364 | struct scatterlist *sg; | |
01a02ffc | 2365 | u64 addr64; |
33a2ffce SC |
2366 | int use_sg, i, sg_index, chained; |
2367 | struct SGDescriptor *curr_sg; | |
edd16368 | 2368 | |
33a2ffce | 2369 | BUG_ON(scsi_sg_count(cmd) > h->maxsgentries); |
edd16368 SC |
2370 | |
2371 | use_sg = scsi_dma_map(cmd); | |
2372 | if (use_sg < 0) | |
2373 | return use_sg; | |
2374 | ||
2375 | if (!use_sg) | |
2376 | goto sglist_finished; | |
2377 | ||
33a2ffce SC |
2378 | curr_sg = cp->SG; |
2379 | chained = 0; | |
2380 | sg_index = 0; | |
edd16368 | 2381 | scsi_for_each_sg(cmd, sg, use_sg, i) { |
33a2ffce SC |
2382 | if (i == h->max_cmd_sg_entries - 1 && |
2383 | use_sg > h->max_cmd_sg_entries) { | |
2384 | chained = 1; | |
2385 | curr_sg = h->cmd_sg_list[cp->cmdindex]; | |
2386 | sg_index = 0; | |
2387 | } | |
01a02ffc | 2388 | addr64 = (u64) sg_dma_address(sg); |
edd16368 | 2389 | len = sg_dma_len(sg); |
33a2ffce SC |
2390 | curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL); |
2391 | curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL); | |
2392 | curr_sg->Len = len; | |
e1d9cbfa | 2393 | curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST; |
33a2ffce SC |
2394 | curr_sg++; |
2395 | } | |
2396 | ||
2397 | if (use_sg + chained > h->maxSG) | |
2398 | h->maxSG = use_sg + chained; | |
2399 | ||
2400 | if (chained) { | |
2401 | cp->Header.SGList = h->max_cmd_sg_entries; | |
2402 | cp->Header.SGTotal = (u16) (use_sg + 1); | |
e2bea6df SC |
2403 | if (hpsa_map_sg_chain_block(h, cp)) { |
2404 | scsi_dma_unmap(cmd); | |
2405 | return -1; | |
2406 | } | |
33a2ffce | 2407 | return 0; |
edd16368 SC |
2408 | } |
2409 | ||
2410 | sglist_finished: | |
2411 | ||
01a02ffc SC |
2412 | cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */ |
2413 | cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */ | |
edd16368 SC |
2414 | return 0; |
2415 | } | |
2416 | ||
283b4a9b SC |
2417 | #define IO_ACCEL_INELIGIBLE (1) |
2418 | static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len) | |
2419 | { | |
2420 | int is_write = 0; | |
2421 | u32 block; | |
2422 | u32 block_cnt; | |
2423 | ||
2424 | /* Perform some CDB fixups if needed using 10 byte reads/writes only */ | |
2425 | switch (cdb[0]) { | |
2426 | case WRITE_6: | |
2427 | case WRITE_12: | |
2428 | is_write = 1; | |
2429 | case READ_6: | |
2430 | case READ_12: | |
2431 | if (*cdb_len == 6) { | |
2432 | block = (((u32) cdb[2]) << 8) | cdb[3]; | |
2433 | block_cnt = cdb[4]; | |
2434 | } else { | |
2435 | BUG_ON(*cdb_len != 12); | |
2436 | block = (((u32) cdb[2]) << 24) | | |
2437 | (((u32) cdb[3]) << 16) | | |
2438 | (((u32) cdb[4]) << 8) | | |
2439 | cdb[5]; | |
2440 | block_cnt = | |
2441 | (((u32) cdb[6]) << 24) | | |
2442 | (((u32) cdb[7]) << 16) | | |
2443 | (((u32) cdb[8]) << 8) | | |
2444 | cdb[9]; | |
2445 | } | |
2446 | if (block_cnt > 0xffff) | |
2447 | return IO_ACCEL_INELIGIBLE; | |
2448 | ||
2449 | cdb[0] = is_write ? WRITE_10 : READ_10; | |
2450 | cdb[1] = 0; | |
2451 | cdb[2] = (u8) (block >> 24); | |
2452 | cdb[3] = (u8) (block >> 16); | |
2453 | cdb[4] = (u8) (block >> 8); | |
2454 | cdb[5] = (u8) (block); | |
2455 | cdb[6] = 0; | |
2456 | cdb[7] = (u8) (block_cnt >> 8); | |
2457 | cdb[8] = (u8) (block_cnt); | |
2458 | cdb[9] = 0; | |
2459 | *cdb_len = 10; | |
2460 | break; | |
2461 | } | |
2462 | return 0; | |
2463 | } | |
2464 | ||
e1f7de0c MG |
2465 | /* |
2466 | * Queue a command to the I/O accelerator path. | |
e1f7de0c MG |
2467 | */ |
2468 | static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h, | |
283b4a9b SC |
2469 | struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len, |
2470 | u8 *scsi3addr) | |
e1f7de0c MG |
2471 | { |
2472 | struct scsi_cmnd *cmd = c->scsi_cmd; | |
e1f7de0c MG |
2473 | struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex]; |
2474 | unsigned int len; | |
2475 | unsigned int total_len = 0; | |
2476 | struct scatterlist *sg; | |
2477 | u64 addr64; | |
2478 | int use_sg, i; | |
2479 | struct SGDescriptor *curr_sg; | |
2480 | u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE; | |
2481 | ||
283b4a9b SC |
2482 | /* TODO: implement chaining support */ |
2483 | if (scsi_sg_count(cmd) > h->ioaccel_maxsg) | |
2484 | return IO_ACCEL_INELIGIBLE; | |
2485 | ||
e1f7de0c MG |
2486 | BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX); |
2487 | ||
283b4a9b SC |
2488 | if (fixup_ioaccel_cdb(cdb, &cdb_len)) |
2489 | return IO_ACCEL_INELIGIBLE; | |
2490 | ||
e1f7de0c MG |
2491 | c->cmd_type = CMD_IOACCEL1; |
2492 | ||
2493 | /* Adjust the DMA address to point to the accelerated command buffer */ | |
2494 | c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle + | |
2495 | (c->cmdindex * sizeof(*cp)); | |
2496 | BUG_ON(c->busaddr & 0x0000007F); | |
2497 | ||
2498 | use_sg = scsi_dma_map(cmd); | |
2499 | if (use_sg < 0) | |
2500 | return use_sg; | |
2501 | ||
2502 | if (use_sg) { | |
2503 | curr_sg = cp->SG; | |
2504 | scsi_for_each_sg(cmd, sg, use_sg, i) { | |
2505 | addr64 = (u64) sg_dma_address(sg); | |
2506 | len = sg_dma_len(sg); | |
2507 | total_len += len; | |
2508 | curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL); | |
2509 | curr_sg->Addr.upper = | |
2510 | (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL); | |
2511 | curr_sg->Len = len; | |
2512 | ||
2513 | if (i == (scsi_sg_count(cmd) - 1)) | |
2514 | curr_sg->Ext = HPSA_SG_LAST; | |
2515 | else | |
2516 | curr_sg->Ext = 0; /* we are not chaining */ | |
2517 | curr_sg++; | |
2518 | } | |
2519 | ||
2520 | switch (cmd->sc_data_direction) { | |
2521 | case DMA_TO_DEVICE: | |
2522 | control |= IOACCEL1_CONTROL_DATA_OUT; | |
2523 | break; | |
2524 | case DMA_FROM_DEVICE: | |
2525 | control |= IOACCEL1_CONTROL_DATA_IN; | |
2526 | break; | |
2527 | case DMA_NONE: | |
2528 | control |= IOACCEL1_CONTROL_NODATAXFER; | |
2529 | break; | |
2530 | default: | |
2531 | dev_err(&h->pdev->dev, "unknown data direction: %d\n", | |
2532 | cmd->sc_data_direction); | |
2533 | BUG(); | |
2534 | break; | |
2535 | } | |
2536 | } else { | |
2537 | control |= IOACCEL1_CONTROL_NODATAXFER; | |
2538 | } | |
2539 | ||
2540 | /* Fill out the command structure to submit */ | |
283b4a9b | 2541 | cp->dev_handle = ioaccel_handle & 0xFFFF; |
e1f7de0c MG |
2542 | cp->transfer_len = total_len; |
2543 | cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ | | |
283b4a9b | 2544 | (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK); |
e1f7de0c | 2545 | cp->control = control; |
283b4a9b SC |
2546 | memcpy(cp->CDB, cdb, cdb_len); |
2547 | memcpy(cp->CISS_LUN, scsi3addr, 8); | |
e1f7de0c MG |
2548 | |
2549 | /* Tell the controller to post the reply to the queue for this | |
2550 | * processor. This seems to give the best I/O throughput. | |
2551 | */ | |
2552 | cp->ReplyQueue = smp_processor_id() % h->nreply_queues; | |
2553 | ||
2554 | /* Set the bits in the address sent down to include: | |
2555 | * - performant mode bit (bit 0) | |
2556 | * - pull count (bits 1-3) | |
2557 | * - command type (bits 4-6) | |
2558 | */ | |
2559 | c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[use_sg] << 1) | | |
2560 | IOACCEL1_BUSADDR_CMDTYPE; | |
283b4a9b | 2561 | enqueue_cmd_and_start_io(h, c); |
e1f7de0c MG |
2562 | return 0; |
2563 | } | |
edd16368 | 2564 | |
283b4a9b SC |
2565 | /* |
2566 | * Queue a command directly to a device behind the controller using the | |
2567 | * I/O accelerator path. | |
2568 | */ | |
2569 | static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h, | |
2570 | struct CommandList *c) | |
2571 | { | |
2572 | struct scsi_cmnd *cmd = c->scsi_cmd; | |
2573 | struct hpsa_scsi_dev_t *dev = cmd->device->hostdata; | |
2574 | ||
2575 | return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle, | |
2576 | cmd->cmnd, cmd->cmd_len, dev->scsi3addr); | |
2577 | } | |
2578 | ||
2579 | /* | |
2580 | * Attempt to perform offload RAID mapping for a logical volume I/O. | |
2581 | */ | |
2582 | static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h, | |
2583 | struct CommandList *c) | |
2584 | { | |
2585 | struct scsi_cmnd *cmd = c->scsi_cmd; | |
2586 | struct hpsa_scsi_dev_t *dev = cmd->device->hostdata; | |
2587 | struct raid_map_data *map = &dev->raid_map; | |
2588 | struct raid_map_disk_data *dd = &map->data[0]; | |
2589 | int is_write = 0; | |
2590 | u32 map_index; | |
2591 | u64 first_block, last_block; | |
2592 | u32 block_cnt; | |
2593 | u32 blocks_per_row; | |
2594 | u64 first_row, last_row; | |
2595 | u32 first_row_offset, last_row_offset; | |
2596 | u32 first_column, last_column; | |
2597 | u32 map_row; | |
2598 | u32 disk_handle; | |
2599 | u64 disk_block; | |
2600 | u32 disk_block_cnt; | |
2601 | u8 cdb[16]; | |
2602 | u8 cdb_len; | |
2603 | #if BITS_PER_LONG == 32 | |
2604 | u64 tmpdiv; | |
2605 | #endif | |
2606 | ||
2607 | BUG_ON(!(dev->offload_config && dev->offload_enabled)); | |
2608 | ||
2609 | /* check for valid opcode, get LBA and block count */ | |
2610 | switch (cmd->cmnd[0]) { | |
2611 | case WRITE_6: | |
2612 | is_write = 1; | |
2613 | case READ_6: | |
2614 | first_block = | |
2615 | (((u64) cmd->cmnd[2]) << 8) | | |
2616 | cmd->cmnd[3]; | |
2617 | block_cnt = cmd->cmnd[4]; | |
2618 | break; | |
2619 | case WRITE_10: | |
2620 | is_write = 1; | |
2621 | case READ_10: | |
2622 | first_block = | |
2623 | (((u64) cmd->cmnd[2]) << 24) | | |
2624 | (((u64) cmd->cmnd[3]) << 16) | | |
2625 | (((u64) cmd->cmnd[4]) << 8) | | |
2626 | cmd->cmnd[5]; | |
2627 | block_cnt = | |
2628 | (((u32) cmd->cmnd[7]) << 8) | | |
2629 | cmd->cmnd[8]; | |
2630 | break; | |
2631 | case WRITE_12: | |
2632 | is_write = 1; | |
2633 | case READ_12: | |
2634 | first_block = | |
2635 | (((u64) cmd->cmnd[2]) << 24) | | |
2636 | (((u64) cmd->cmnd[3]) << 16) | | |
2637 | (((u64) cmd->cmnd[4]) << 8) | | |
2638 | cmd->cmnd[5]; | |
2639 | block_cnt = | |
2640 | (((u32) cmd->cmnd[6]) << 24) | | |
2641 | (((u32) cmd->cmnd[7]) << 16) | | |
2642 | (((u32) cmd->cmnd[8]) << 8) | | |
2643 | cmd->cmnd[9]; | |
2644 | break; | |
2645 | case WRITE_16: | |
2646 | is_write = 1; | |
2647 | case READ_16: | |
2648 | first_block = | |
2649 | (((u64) cmd->cmnd[2]) << 56) | | |
2650 | (((u64) cmd->cmnd[3]) << 48) | | |
2651 | (((u64) cmd->cmnd[4]) << 40) | | |
2652 | (((u64) cmd->cmnd[5]) << 32) | | |
2653 | (((u64) cmd->cmnd[6]) << 24) | | |
2654 | (((u64) cmd->cmnd[7]) << 16) | | |
2655 | (((u64) cmd->cmnd[8]) << 8) | | |
2656 | cmd->cmnd[9]; | |
2657 | block_cnt = | |
2658 | (((u32) cmd->cmnd[10]) << 24) | | |
2659 | (((u32) cmd->cmnd[11]) << 16) | | |
2660 | (((u32) cmd->cmnd[12]) << 8) | | |
2661 | cmd->cmnd[13]; | |
2662 | break; | |
2663 | default: | |
2664 | return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */ | |
2665 | } | |
2666 | BUG_ON(block_cnt == 0); | |
2667 | last_block = first_block + block_cnt - 1; | |
2668 | ||
2669 | /* check for write to non-RAID-0 */ | |
2670 | if (is_write && dev->raid_level != 0) | |
2671 | return IO_ACCEL_INELIGIBLE; | |
2672 | ||
2673 | /* check for invalid block or wraparound */ | |
2674 | if (last_block >= map->volume_blk_cnt || last_block < first_block) | |
2675 | return IO_ACCEL_INELIGIBLE; | |
2676 | ||
2677 | /* calculate stripe information for the request */ | |
2678 | blocks_per_row = map->data_disks_per_row * map->strip_size; | |
2679 | #if BITS_PER_LONG == 32 | |
2680 | tmpdiv = first_block; | |
2681 | (void) do_div(tmpdiv, blocks_per_row); | |
2682 | first_row = tmpdiv; | |
2683 | tmpdiv = last_block; | |
2684 | (void) do_div(tmpdiv, blocks_per_row); | |
2685 | last_row = tmpdiv; | |
2686 | first_row_offset = (u32) (first_block - (first_row * blocks_per_row)); | |
2687 | last_row_offset = (u32) (last_block - (last_row * blocks_per_row)); | |
2688 | tmpdiv = first_row_offset; | |
2689 | (void) do_div(tmpdiv, map->strip_size); | |
2690 | first_column = tmpdiv; | |
2691 | tmpdiv = last_row_offset; | |
2692 | (void) do_div(tmpdiv, map->strip_size); | |
2693 | last_column = tmpdiv; | |
2694 | #else | |
2695 | first_row = first_block / blocks_per_row; | |
2696 | last_row = last_block / blocks_per_row; | |
2697 | first_row_offset = (u32) (first_block - (first_row * blocks_per_row)); | |
2698 | last_row_offset = (u32) (last_block - (last_row * blocks_per_row)); | |
2699 | first_column = first_row_offset / map->strip_size; | |
2700 | last_column = last_row_offset / map->strip_size; | |
2701 | #endif | |
2702 | ||
2703 | /* if this isn't a single row/column then give to the controller */ | |
2704 | if ((first_row != last_row) || (first_column != last_column)) | |
2705 | return IO_ACCEL_INELIGIBLE; | |
2706 | ||
2707 | /* proceeding with driver mapping */ | |
2708 | map_row = ((u32)(first_row >> map->parity_rotation_shift)) % | |
2709 | map->row_cnt; | |
2710 | map_index = (map_row * (map->data_disks_per_row + | |
2711 | map->metadata_disks_per_row)) + first_column; | |
2712 | if (dev->raid_level == 2) { | |
2713 | /* simple round-robin balancing of RAID 1+0 reads across | |
2714 | * primary and mirror members. this is appropriate for SSD | |
2715 | * but not optimal for HDD. | |
2716 | */ | |
2717 | if (dev->offload_to_mirror) | |
2718 | map_index += map->data_disks_per_row; | |
2719 | dev->offload_to_mirror = !dev->offload_to_mirror; | |
2720 | } | |
2721 | disk_handle = dd[map_index].ioaccel_handle; | |
2722 | disk_block = map->disk_starting_blk + (first_row * map->strip_size) + | |
2723 | (first_row_offset - (first_column * map->strip_size)); | |
2724 | disk_block_cnt = block_cnt; | |
2725 | ||
2726 | /* handle differing logical/physical block sizes */ | |
2727 | if (map->phys_blk_shift) { | |
2728 | disk_block <<= map->phys_blk_shift; | |
2729 | disk_block_cnt <<= map->phys_blk_shift; | |
2730 | } | |
2731 | BUG_ON(disk_block_cnt > 0xffff); | |
2732 | ||
2733 | /* build the new CDB for the physical disk I/O */ | |
2734 | if (disk_block > 0xffffffff) { | |
2735 | cdb[0] = is_write ? WRITE_16 : READ_16; | |
2736 | cdb[1] = 0; | |
2737 | cdb[2] = (u8) (disk_block >> 56); | |
2738 | cdb[3] = (u8) (disk_block >> 48); | |
2739 | cdb[4] = (u8) (disk_block >> 40); | |
2740 | cdb[5] = (u8) (disk_block >> 32); | |
2741 | cdb[6] = (u8) (disk_block >> 24); | |
2742 | cdb[7] = (u8) (disk_block >> 16); | |
2743 | cdb[8] = (u8) (disk_block >> 8); | |
2744 | cdb[9] = (u8) (disk_block); | |
2745 | cdb[10] = (u8) (disk_block_cnt >> 24); | |
2746 | cdb[11] = (u8) (disk_block_cnt >> 16); | |
2747 | cdb[12] = (u8) (disk_block_cnt >> 8); | |
2748 | cdb[13] = (u8) (disk_block_cnt); | |
2749 | cdb[14] = 0; | |
2750 | cdb[15] = 0; | |
2751 | cdb_len = 16; | |
2752 | } else { | |
2753 | cdb[0] = is_write ? WRITE_10 : READ_10; | |
2754 | cdb[1] = 0; | |
2755 | cdb[2] = (u8) (disk_block >> 24); | |
2756 | cdb[3] = (u8) (disk_block >> 16); | |
2757 | cdb[4] = (u8) (disk_block >> 8); | |
2758 | cdb[5] = (u8) (disk_block); | |
2759 | cdb[6] = 0; | |
2760 | cdb[7] = (u8) (disk_block_cnt >> 8); | |
2761 | cdb[8] = (u8) (disk_block_cnt); | |
2762 | cdb[9] = 0; | |
2763 | cdb_len = 10; | |
2764 | } | |
2765 | return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len, | |
2766 | dev->scsi3addr); | |
2767 | } | |
2768 | ||
f281233d | 2769 | static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd, |
edd16368 SC |
2770 | void (*done)(struct scsi_cmnd *)) |
2771 | { | |
2772 | struct ctlr_info *h; | |
2773 | struct hpsa_scsi_dev_t *dev; | |
2774 | unsigned char scsi3addr[8]; | |
2775 | struct CommandList *c; | |
2776 | unsigned long flags; | |
283b4a9b | 2777 | int rc = 0; |
edd16368 SC |
2778 | |
2779 | /* Get the ptr to our adapter structure out of cmd->host. */ | |
2780 | h = sdev_to_hba(cmd->device); | |
2781 | dev = cmd->device->hostdata; | |
2782 | if (!dev) { | |
2783 | cmd->result = DID_NO_CONNECT << 16; | |
2784 | done(cmd); | |
2785 | return 0; | |
2786 | } | |
2787 | memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr)); | |
2788 | ||
edd16368 | 2789 | spin_lock_irqsave(&h->lock, flags); |
a0c12413 SC |
2790 | if (unlikely(h->lockup_detected)) { |
2791 | spin_unlock_irqrestore(&h->lock, flags); | |
2792 | cmd->result = DID_ERROR << 16; | |
2793 | done(cmd); | |
2794 | return 0; | |
2795 | } | |
edd16368 | 2796 | spin_unlock_irqrestore(&h->lock, flags); |
e16a33ad | 2797 | c = cmd_alloc(h); |
edd16368 SC |
2798 | if (c == NULL) { /* trouble... */ |
2799 | dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n"); | |
2800 | return SCSI_MLQUEUE_HOST_BUSY; | |
2801 | } | |
2802 | ||
2803 | /* Fill in the command list header */ | |
2804 | ||
2805 | cmd->scsi_done = done; /* save this for use by completion code */ | |
2806 | ||
2807 | /* save c in case we have to abort it */ | |
2808 | cmd->host_scribble = (unsigned char *) c; | |
2809 | ||
2810 | c->cmd_type = CMD_SCSI; | |
2811 | c->scsi_cmd = cmd; | |
e1f7de0c | 2812 | |
283b4a9b SC |
2813 | /* Call alternate submit routine for I/O accelerated commands. |
2814 | * Retries always go down the normal I/O path. | |
2815 | */ | |
2816 | if (likely(cmd->retries == 0 && | |
2817 | cmd->request->cmd_type == REQ_TYPE_FS)) { | |
2818 | if (dev->offload_enabled) { | |
2819 | rc = hpsa_scsi_ioaccel_raid_map(h, c); | |
2820 | if (rc == 0) | |
2821 | return 0; /* Sent on ioaccel path */ | |
2822 | if (rc < 0) { /* scsi_dma_map failed. */ | |
2823 | cmd_free(h, c); | |
2824 | return SCSI_MLQUEUE_HOST_BUSY; | |
2825 | } | |
2826 | } else if (dev->ioaccel_handle) { | |
2827 | rc = hpsa_scsi_ioaccel_direct_map(h, c); | |
2828 | if (rc == 0) | |
2829 | return 0; /* Sent on direct map path */ | |
2830 | if (rc < 0) { /* scsi_dma_map failed. */ | |
2831 | cmd_free(h, c); | |
2832 | return SCSI_MLQUEUE_HOST_BUSY; | |
2833 | } | |
2834 | } | |
2835 | } | |
e1f7de0c | 2836 | |
edd16368 SC |
2837 | c->Header.ReplyQueue = 0; /* unused in simple mode */ |
2838 | memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8); | |
303932fd DB |
2839 | c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT); |
2840 | c->Header.Tag.lower |= DIRECT_LOOKUP_BIT; | |
edd16368 SC |
2841 | |
2842 | /* Fill in the request block... */ | |
2843 | ||
2844 | c->Request.Timeout = 0; | |
2845 | memset(c->Request.CDB, 0, sizeof(c->Request.CDB)); | |
2846 | BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB)); | |
2847 | c->Request.CDBLen = cmd->cmd_len; | |
2848 | memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len); | |
2849 | c->Request.Type.Type = TYPE_CMD; | |
2850 | c->Request.Type.Attribute = ATTR_SIMPLE; | |
2851 | switch (cmd->sc_data_direction) { | |
2852 | case DMA_TO_DEVICE: | |
2853 | c->Request.Type.Direction = XFER_WRITE; | |
2854 | break; | |
2855 | case DMA_FROM_DEVICE: | |
2856 | c->Request.Type.Direction = XFER_READ; | |
2857 | break; | |
2858 | case DMA_NONE: | |
2859 | c->Request.Type.Direction = XFER_NONE; | |
2860 | break; | |
2861 | case DMA_BIDIRECTIONAL: | |
2862 | /* This can happen if a buggy application does a scsi passthru | |
2863 | * and sets both inlen and outlen to non-zero. ( see | |
2864 | * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() ) | |
2865 | */ | |
2866 | ||
2867 | c->Request.Type.Direction = XFER_RSVD; | |
2868 | /* This is technically wrong, and hpsa controllers should | |
2869 | * reject it with CMD_INVALID, which is the most correct | |
2870 | * response, but non-fibre backends appear to let it | |
2871 | * slide by, and give the same results as if this field | |
2872 | * were set correctly. Either way is acceptable for | |
2873 | * our purposes here. | |
2874 | */ | |
2875 | ||
2876 | break; | |
2877 | ||
2878 | default: | |
2879 | dev_err(&h->pdev->dev, "unknown data direction: %d\n", | |
2880 | cmd->sc_data_direction); | |
2881 | BUG(); | |
2882 | break; | |
2883 | } | |
2884 | ||
33a2ffce | 2885 | if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */ |
edd16368 SC |
2886 | cmd_free(h, c); |
2887 | return SCSI_MLQUEUE_HOST_BUSY; | |
2888 | } | |
2889 | enqueue_cmd_and_start_io(h, c); | |
2890 | /* the cmd'll come back via intr handler in complete_scsi_command() */ | |
2891 | return 0; | |
2892 | } | |
2893 | ||
f281233d JG |
2894 | static DEF_SCSI_QCMD(hpsa_scsi_queue_command) |
2895 | ||
5f389360 SC |
2896 | static int do_not_scan_if_controller_locked_up(struct ctlr_info *h) |
2897 | { | |
2898 | unsigned long flags; | |
2899 | ||
2900 | /* | |
2901 | * Don't let rescans be initiated on a controller known | |
2902 | * to be locked up. If the controller locks up *during* | |
2903 | * a rescan, that thread is probably hosed, but at least | |
2904 | * we can prevent new rescan threads from piling up on a | |
2905 | * locked up controller. | |
2906 | */ | |
2907 | spin_lock_irqsave(&h->lock, flags); | |
2908 | if (unlikely(h->lockup_detected)) { | |
2909 | spin_unlock_irqrestore(&h->lock, flags); | |
2910 | spin_lock_irqsave(&h->scan_lock, flags); | |
2911 | h->scan_finished = 1; | |
2912 | wake_up_all(&h->scan_wait_queue); | |
2913 | spin_unlock_irqrestore(&h->scan_lock, flags); | |
2914 | return 1; | |
2915 | } | |
2916 | spin_unlock_irqrestore(&h->lock, flags); | |
2917 | return 0; | |
2918 | } | |
2919 | ||
a08a8471 SC |
2920 | static void hpsa_scan_start(struct Scsi_Host *sh) |
2921 | { | |
2922 | struct ctlr_info *h = shost_to_hba(sh); | |
2923 | unsigned long flags; | |
2924 | ||
5f389360 SC |
2925 | if (do_not_scan_if_controller_locked_up(h)) |
2926 | return; | |
2927 | ||
a08a8471 SC |
2928 | /* wait until any scan already in progress is finished. */ |
2929 | while (1) { | |
2930 | spin_lock_irqsave(&h->scan_lock, flags); | |
2931 | if (h->scan_finished) | |
2932 | break; | |
2933 | spin_unlock_irqrestore(&h->scan_lock, flags); | |
2934 | wait_event(h->scan_wait_queue, h->scan_finished); | |
2935 | /* Note: We don't need to worry about a race between this | |
2936 | * thread and driver unload because the midlayer will | |
2937 | * have incremented the reference count, so unload won't | |
2938 | * happen if we're in here. | |
2939 | */ | |
2940 | } | |
2941 | h->scan_finished = 0; /* mark scan as in progress */ | |
2942 | spin_unlock_irqrestore(&h->scan_lock, flags); | |
2943 | ||
5f389360 SC |
2944 | if (do_not_scan_if_controller_locked_up(h)) |
2945 | return; | |
2946 | ||
a08a8471 SC |
2947 | hpsa_update_scsi_devices(h, h->scsi_host->host_no); |
2948 | ||
2949 | spin_lock_irqsave(&h->scan_lock, flags); | |
2950 | h->scan_finished = 1; /* mark scan as finished. */ | |
2951 | wake_up_all(&h->scan_wait_queue); | |
2952 | spin_unlock_irqrestore(&h->scan_lock, flags); | |
2953 | } | |
2954 | ||
2955 | static int hpsa_scan_finished(struct Scsi_Host *sh, | |
2956 | unsigned long elapsed_time) | |
2957 | { | |
2958 | struct ctlr_info *h = shost_to_hba(sh); | |
2959 | unsigned long flags; | |
2960 | int finished; | |
2961 | ||
2962 | spin_lock_irqsave(&h->scan_lock, flags); | |
2963 | finished = h->scan_finished; | |
2964 | spin_unlock_irqrestore(&h->scan_lock, flags); | |
2965 | return finished; | |
2966 | } | |
2967 | ||
667e23d4 SC |
2968 | static int hpsa_change_queue_depth(struct scsi_device *sdev, |
2969 | int qdepth, int reason) | |
2970 | { | |
2971 | struct ctlr_info *h = sdev_to_hba(sdev); | |
2972 | ||
2973 | if (reason != SCSI_QDEPTH_DEFAULT) | |
2974 | return -ENOTSUPP; | |
2975 | ||
2976 | if (qdepth < 1) | |
2977 | qdepth = 1; | |
2978 | else | |
2979 | if (qdepth > h->nr_cmds) | |
2980 | qdepth = h->nr_cmds; | |
2981 | scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth); | |
2982 | return sdev->queue_depth; | |
2983 | } | |
2984 | ||
edd16368 SC |
2985 | static void hpsa_unregister_scsi(struct ctlr_info *h) |
2986 | { | |
2987 | /* we are being forcibly unloaded, and may not refuse. */ | |
2988 | scsi_remove_host(h->scsi_host); | |
2989 | scsi_host_put(h->scsi_host); | |
2990 | h->scsi_host = NULL; | |
2991 | } | |
2992 | ||
2993 | static int hpsa_register_scsi(struct ctlr_info *h) | |
2994 | { | |
b705690d SC |
2995 | struct Scsi_Host *sh; |
2996 | int error; | |
edd16368 | 2997 | |
b705690d SC |
2998 | sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h)); |
2999 | if (sh == NULL) | |
3000 | goto fail; | |
3001 | ||
3002 | sh->io_port = 0; | |
3003 | sh->n_io_port = 0; | |
3004 | sh->this_id = -1; | |
3005 | sh->max_channel = 3; | |
3006 | sh->max_cmd_len = MAX_COMMAND_SIZE; | |
3007 | sh->max_lun = HPSA_MAX_LUN; | |
3008 | sh->max_id = HPSA_MAX_LUN; | |
3009 | sh->can_queue = h->nr_cmds; | |
3010 | sh->cmd_per_lun = h->nr_cmds; | |
3011 | sh->sg_tablesize = h->maxsgentries; | |
3012 | h->scsi_host = sh; | |
3013 | sh->hostdata[0] = (unsigned long) h; | |
3014 | sh->irq = h->intr[h->intr_mode]; | |
3015 | sh->unique_id = sh->irq; | |
3016 | error = scsi_add_host(sh, &h->pdev->dev); | |
3017 | if (error) | |
3018 | goto fail_host_put; | |
3019 | scsi_scan_host(sh); | |
3020 | return 0; | |
3021 | ||
3022 | fail_host_put: | |
3023 | dev_err(&h->pdev->dev, "%s: scsi_add_host" | |
3024 | " failed for controller %d\n", __func__, h->ctlr); | |
3025 | scsi_host_put(sh); | |
3026 | return error; | |
3027 | fail: | |
3028 | dev_err(&h->pdev->dev, "%s: scsi_host_alloc" | |
3029 | " failed for controller %d\n", __func__, h->ctlr); | |
3030 | return -ENOMEM; | |
edd16368 SC |
3031 | } |
3032 | ||
3033 | static int wait_for_device_to_become_ready(struct ctlr_info *h, | |
3034 | unsigned char lunaddr[]) | |
3035 | { | |
3036 | int rc = 0; | |
3037 | int count = 0; | |
3038 | int waittime = 1; /* seconds */ | |
3039 | struct CommandList *c; | |
3040 | ||
3041 | c = cmd_special_alloc(h); | |
3042 | if (!c) { | |
3043 | dev_warn(&h->pdev->dev, "out of memory in " | |
3044 | "wait_for_device_to_become_ready.\n"); | |
3045 | return IO_ERROR; | |
3046 | } | |
3047 | ||
3048 | /* Send test unit ready until device ready, or give up. */ | |
3049 | while (count < HPSA_TUR_RETRY_LIMIT) { | |
3050 | ||
3051 | /* Wait for a bit. do this first, because if we send | |
3052 | * the TUR right away, the reset will just abort it. | |
3053 | */ | |
3054 | msleep(1000 * waittime); | |
3055 | count++; | |
3056 | ||
3057 | /* Increase wait time with each try, up to a point. */ | |
3058 | if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS) | |
3059 | waittime = waittime * 2; | |
3060 | ||
a2dac136 SC |
3061 | /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */ |
3062 | (void) fill_cmd(c, TEST_UNIT_READY, h, | |
3063 | NULL, 0, 0, lunaddr, TYPE_CMD); | |
edd16368 SC |
3064 | hpsa_scsi_do_simple_cmd_core(h, c); |
3065 | /* no unmap needed here because no data xfer. */ | |
3066 | ||
3067 | if (c->err_info->CommandStatus == CMD_SUCCESS) | |
3068 | break; | |
3069 | ||
3070 | if (c->err_info->CommandStatus == CMD_TARGET_STATUS && | |
3071 | c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION && | |
3072 | (c->err_info->SenseInfo[2] == NO_SENSE || | |
3073 | c->err_info->SenseInfo[2] == UNIT_ATTENTION)) | |
3074 | break; | |
3075 | ||
3076 | dev_warn(&h->pdev->dev, "waiting %d secs " | |
3077 | "for device to become ready.\n", waittime); | |
3078 | rc = 1; /* device not ready. */ | |
3079 | } | |
3080 | ||
3081 | if (rc) | |
3082 | dev_warn(&h->pdev->dev, "giving up on device.\n"); | |
3083 | else | |
3084 | dev_warn(&h->pdev->dev, "device is ready.\n"); | |
3085 | ||
3086 | cmd_special_free(h, c); | |
3087 | return rc; | |
3088 | } | |
3089 | ||
3090 | /* Need at least one of these error handlers to keep ../scsi/hosts.c from | |
3091 | * complaining. Doing a host- or bus-reset can't do anything good here. | |
3092 | */ | |
3093 | static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd) | |
3094 | { | |
3095 | int rc; | |
3096 | struct ctlr_info *h; | |
3097 | struct hpsa_scsi_dev_t *dev; | |
3098 | ||
3099 | /* find the controller to which the command to be aborted was sent */ | |
3100 | h = sdev_to_hba(scsicmd->device); | |
3101 | if (h == NULL) /* paranoia */ | |
3102 | return FAILED; | |
edd16368 SC |
3103 | dev = scsicmd->device->hostdata; |
3104 | if (!dev) { | |
3105 | dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: " | |
3106 | "device lookup failed.\n"); | |
3107 | return FAILED; | |
3108 | } | |
d416b0c7 SC |
3109 | dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n", |
3110 | h->scsi_host->host_no, dev->bus, dev->target, dev->lun); | |
edd16368 SC |
3111 | /* send a reset to the SCSI LUN which the command was sent to */ |
3112 | rc = hpsa_send_reset(h, dev->scsi3addr); | |
3113 | if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0) | |
3114 | return SUCCESS; | |
3115 | ||
3116 | dev_warn(&h->pdev->dev, "resetting device failed.\n"); | |
3117 | return FAILED; | |
3118 | } | |
3119 | ||
6cba3f19 SC |
3120 | static void swizzle_abort_tag(u8 *tag) |
3121 | { | |
3122 | u8 original_tag[8]; | |
3123 | ||
3124 | memcpy(original_tag, tag, 8); | |
3125 | tag[0] = original_tag[3]; | |
3126 | tag[1] = original_tag[2]; | |
3127 | tag[2] = original_tag[1]; | |
3128 | tag[3] = original_tag[0]; | |
3129 | tag[4] = original_tag[7]; | |
3130 | tag[5] = original_tag[6]; | |
3131 | tag[6] = original_tag[5]; | |
3132 | tag[7] = original_tag[4]; | |
3133 | } | |
3134 | ||
17eb87d2 ST |
3135 | static void hpsa_get_tag(struct ctlr_info *h, |
3136 | struct CommandList *c, u32 *taglower, u32 *tagupper) | |
3137 | { | |
3138 | if (c->cmd_type == CMD_IOACCEL1) { | |
3139 | struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *) | |
3140 | &h->ioaccel_cmd_pool[c->cmdindex]; | |
3141 | *tagupper = cm1->Tag.upper; | |
3142 | *taglower = cm1->Tag.lower; | |
3143 | } else { | |
3144 | *tagupper = c->Header.Tag.upper; | |
3145 | *taglower = c->Header.Tag.lower; | |
3146 | } | |
3147 | } | |
3148 | ||
75167d2c | 3149 | static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr, |
6cba3f19 | 3150 | struct CommandList *abort, int swizzle) |
75167d2c SC |
3151 | { |
3152 | int rc = IO_OK; | |
3153 | struct CommandList *c; | |
3154 | struct ErrorInfo *ei; | |
17eb87d2 | 3155 | u32 tagupper, taglower; |
75167d2c SC |
3156 | |
3157 | c = cmd_special_alloc(h); | |
3158 | if (c == NULL) { /* trouble... */ | |
3159 | dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); | |
3160 | return -ENOMEM; | |
3161 | } | |
3162 | ||
a2dac136 SC |
3163 | /* fill_cmd can't fail here, no buffer to map */ |
3164 | (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort, | |
3165 | 0, 0, scsi3addr, TYPE_MSG); | |
6cba3f19 SC |
3166 | if (swizzle) |
3167 | swizzle_abort_tag(&c->Request.CDB[4]); | |
75167d2c | 3168 | hpsa_scsi_do_simple_cmd_core(h, c); |
17eb87d2 | 3169 | hpsa_get_tag(h, abort, &taglower, &tagupper); |
75167d2c | 3170 | dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n", |
17eb87d2 | 3171 | __func__, tagupper, taglower); |
75167d2c SC |
3172 | /* no unmap needed here because no data xfer. */ |
3173 | ||
3174 | ei = c->err_info; | |
3175 | switch (ei->CommandStatus) { | |
3176 | case CMD_SUCCESS: | |
3177 | break; | |
3178 | case CMD_UNABORTABLE: /* Very common, don't make noise. */ | |
3179 | rc = -1; | |
3180 | break; | |
3181 | default: | |
3182 | dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n", | |
17eb87d2 | 3183 | __func__, tagupper, taglower); |
75167d2c SC |
3184 | hpsa_scsi_interpret_error(c); |
3185 | rc = -1; | |
3186 | break; | |
3187 | } | |
3188 | cmd_special_free(h, c); | |
3189 | dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__, | |
3190 | abort->Header.Tag.upper, abort->Header.Tag.lower); | |
3191 | return rc; | |
3192 | } | |
3193 | ||
3194 | /* | |
3195 | * hpsa_find_cmd_in_queue | |
3196 | * | |
3197 | * Used to determine whether a command (find) is still present | |
3198 | * in queue_head. Optionally excludes the last element of queue_head. | |
3199 | * | |
3200 | * This is used to avoid unnecessary aborts. Commands in h->reqQ have | |
3201 | * not yet been submitted, and so can be aborted by the driver without | |
3202 | * sending an abort to the hardware. | |
3203 | * | |
3204 | * Returns pointer to command if found in queue, NULL otherwise. | |
3205 | */ | |
3206 | static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h, | |
3207 | struct scsi_cmnd *find, struct list_head *queue_head) | |
3208 | { | |
3209 | unsigned long flags; | |
3210 | struct CommandList *c = NULL; /* ptr into cmpQ */ | |
3211 | ||
3212 | if (!find) | |
3213 | return 0; | |
3214 | spin_lock_irqsave(&h->lock, flags); | |
3215 | list_for_each_entry(c, queue_head, list) { | |
3216 | if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */ | |
3217 | continue; | |
3218 | if (c->scsi_cmd == find) { | |
3219 | spin_unlock_irqrestore(&h->lock, flags); | |
3220 | return c; | |
3221 | } | |
3222 | } | |
3223 | spin_unlock_irqrestore(&h->lock, flags); | |
3224 | return NULL; | |
3225 | } | |
3226 | ||
6cba3f19 SC |
3227 | static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h, |
3228 | u8 *tag, struct list_head *queue_head) | |
3229 | { | |
3230 | unsigned long flags; | |
3231 | struct CommandList *c; | |
3232 | ||
3233 | spin_lock_irqsave(&h->lock, flags); | |
3234 | list_for_each_entry(c, queue_head, list) { | |
3235 | if (memcmp(&c->Header.Tag, tag, 8) != 0) | |
3236 | continue; | |
3237 | spin_unlock_irqrestore(&h->lock, flags); | |
3238 | return c; | |
3239 | } | |
3240 | spin_unlock_irqrestore(&h->lock, flags); | |
3241 | return NULL; | |
3242 | } | |
3243 | ||
3244 | /* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to | |
3245 | * tell which kind we're dealing with, so we send the abort both ways. There | |
3246 | * shouldn't be any collisions between swizzled and unswizzled tags due to the | |
3247 | * way we construct our tags but we check anyway in case the assumptions which | |
3248 | * make this true someday become false. | |
3249 | */ | |
3250 | static int hpsa_send_abort_both_ways(struct ctlr_info *h, | |
3251 | unsigned char *scsi3addr, struct CommandList *abort) | |
3252 | { | |
3253 | u8 swizzled_tag[8]; | |
3254 | struct CommandList *c; | |
3255 | int rc = 0, rc2 = 0; | |
3256 | ||
3257 | /* we do not expect to find the swizzled tag in our queue, but | |
3258 | * check anyway just to be sure the assumptions which make this | |
3259 | * the case haven't become wrong. | |
3260 | */ | |
3261 | memcpy(swizzled_tag, &abort->Request.CDB[4], 8); | |
3262 | swizzle_abort_tag(swizzled_tag); | |
3263 | c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ); | |
3264 | if (c != NULL) { | |
3265 | dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n"); | |
3266 | return hpsa_send_abort(h, scsi3addr, abort, 0); | |
3267 | } | |
3268 | rc = hpsa_send_abort(h, scsi3addr, abort, 0); | |
3269 | ||
3270 | /* if the command is still in our queue, we can't conclude that it was | |
3271 | * aborted (it might have just completed normally) but in any case | |
3272 | * we don't need to try to abort it another way. | |
3273 | */ | |
3274 | c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ); | |
3275 | if (c) | |
3276 | rc2 = hpsa_send_abort(h, scsi3addr, abort, 1); | |
3277 | return rc && rc2; | |
3278 | } | |
3279 | ||
75167d2c SC |
3280 | /* Send an abort for the specified command. |
3281 | * If the device and controller support it, | |
3282 | * send a task abort request. | |
3283 | */ | |
3284 | static int hpsa_eh_abort_handler(struct scsi_cmnd *sc) | |
3285 | { | |
3286 | ||
3287 | int i, rc; | |
3288 | struct ctlr_info *h; | |
3289 | struct hpsa_scsi_dev_t *dev; | |
3290 | struct CommandList *abort; /* pointer to command to be aborted */ | |
3291 | struct CommandList *found; | |
3292 | struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */ | |
3293 | char msg[256]; /* For debug messaging. */ | |
3294 | int ml = 0; | |
17eb87d2 | 3295 | u32 tagupper, taglower; |
75167d2c SC |
3296 | |
3297 | /* Find the controller of the command to be aborted */ | |
3298 | h = sdev_to_hba(sc->device); | |
3299 | if (WARN(h == NULL, | |
3300 | "ABORT REQUEST FAILED, Controller lookup failed.\n")) | |
3301 | return FAILED; | |
3302 | ||
3303 | /* Check that controller supports some kind of task abort */ | |
3304 | if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) && | |
3305 | !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags)) | |
3306 | return FAILED; | |
3307 | ||
3308 | memset(msg, 0, sizeof(msg)); | |
3309 | ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ", | |
3310 | h->scsi_host->host_no, sc->device->channel, | |
3311 | sc->device->id, sc->device->lun); | |
3312 | ||
3313 | /* Find the device of the command to be aborted */ | |
3314 | dev = sc->device->hostdata; | |
3315 | if (!dev) { | |
3316 | dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n", | |
3317 | msg); | |
3318 | return FAILED; | |
3319 | } | |
3320 | ||
3321 | /* Get SCSI command to be aborted */ | |
3322 | abort = (struct CommandList *) sc->host_scribble; | |
3323 | if (abort == NULL) { | |
3324 | dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n", | |
3325 | msg); | |
3326 | return FAILED; | |
3327 | } | |
17eb87d2 ST |
3328 | hpsa_get_tag(h, abort, &taglower, &tagupper); |
3329 | ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower); | |
75167d2c SC |
3330 | as = (struct scsi_cmnd *) abort->scsi_cmd; |
3331 | if (as != NULL) | |
3332 | ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ", | |
3333 | as->cmnd[0], as->serial_number); | |
3334 | dev_dbg(&h->pdev->dev, "%s\n", msg); | |
3335 | dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n", | |
3336 | h->scsi_host->host_no, dev->bus, dev->target, dev->lun); | |
3337 | ||
3338 | /* Search reqQ to See if command is queued but not submitted, | |
3339 | * if so, complete the command with aborted status and remove | |
3340 | * it from the reqQ. | |
3341 | */ | |
3342 | found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ); | |
3343 | if (found) { | |
3344 | found->err_info->CommandStatus = CMD_ABORTED; | |
3345 | finish_cmd(found); | |
3346 | dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n", | |
3347 | msg); | |
3348 | return SUCCESS; | |
3349 | } | |
3350 | ||
3351 | /* not in reqQ, if also not in cmpQ, must have already completed */ | |
3352 | found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ); | |
3353 | if (!found) { | |
d6ebd0f7 | 3354 | dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n", |
75167d2c SC |
3355 | msg); |
3356 | return SUCCESS; | |
3357 | } | |
3358 | ||
3359 | /* | |
3360 | * Command is in flight, or possibly already completed | |
3361 | * by the firmware (but not to the scsi mid layer) but we can't | |
3362 | * distinguish which. Send the abort down. | |
3363 | */ | |
6cba3f19 | 3364 | rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort); |
75167d2c SC |
3365 | if (rc != 0) { |
3366 | dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg); | |
3367 | dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n", | |
3368 | h->scsi_host->host_no, | |
3369 | dev->bus, dev->target, dev->lun); | |
3370 | return FAILED; | |
3371 | } | |
3372 | dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg); | |
3373 | ||
3374 | /* If the abort(s) above completed and actually aborted the | |
3375 | * command, then the command to be aborted should already be | |
3376 | * completed. If not, wait around a bit more to see if they | |
3377 | * manage to complete normally. | |
3378 | */ | |
3379 | #define ABORT_COMPLETE_WAIT_SECS 30 | |
3380 | for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) { | |
3381 | found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ); | |
3382 | if (!found) | |
3383 | return SUCCESS; | |
3384 | msleep(100); | |
3385 | } | |
3386 | dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n", | |
3387 | msg, ABORT_COMPLETE_WAIT_SECS); | |
3388 | return FAILED; | |
3389 | } | |
3390 | ||
3391 | ||
edd16368 SC |
3392 | /* |
3393 | * For operations that cannot sleep, a command block is allocated at init, | |
3394 | * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track | |
3395 | * which ones are free or in use. Lock must be held when calling this. | |
3396 | * cmd_free() is the complement. | |
3397 | */ | |
3398 | static struct CommandList *cmd_alloc(struct ctlr_info *h) | |
3399 | { | |
3400 | struct CommandList *c; | |
3401 | int i; | |
3402 | union u64bit temp64; | |
3403 | dma_addr_t cmd_dma_handle, err_dma_handle; | |
e16a33ad | 3404 | unsigned long flags; |
edd16368 | 3405 | |
e16a33ad | 3406 | spin_lock_irqsave(&h->lock, flags); |
edd16368 SC |
3407 | do { |
3408 | i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds); | |
e16a33ad MG |
3409 | if (i == h->nr_cmds) { |
3410 | spin_unlock_irqrestore(&h->lock, flags); | |
edd16368 | 3411 | return NULL; |
e16a33ad | 3412 | } |
edd16368 SC |
3413 | } while (test_and_set_bit |
3414 | (i & (BITS_PER_LONG - 1), | |
3415 | h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0); | |
e16a33ad MG |
3416 | spin_unlock_irqrestore(&h->lock, flags); |
3417 | ||
edd16368 SC |
3418 | c = h->cmd_pool + i; |
3419 | memset(c, 0, sizeof(*c)); | |
3420 | cmd_dma_handle = h->cmd_pool_dhandle | |
3421 | + i * sizeof(*c); | |
3422 | c->err_info = h->errinfo_pool + i; | |
3423 | memset(c->err_info, 0, sizeof(*c->err_info)); | |
3424 | err_dma_handle = h->errinfo_pool_dhandle | |
3425 | + i * sizeof(*c->err_info); | |
edd16368 SC |
3426 | |
3427 | c->cmdindex = i; | |
3428 | ||
9e0fc764 | 3429 | INIT_LIST_HEAD(&c->list); |
01a02ffc SC |
3430 | c->busaddr = (u32) cmd_dma_handle; |
3431 | temp64.val = (u64) err_dma_handle; | |
edd16368 SC |
3432 | c->ErrDesc.Addr.lower = temp64.val32.lower; |
3433 | c->ErrDesc.Addr.upper = temp64.val32.upper; | |
3434 | c->ErrDesc.Len = sizeof(*c->err_info); | |
3435 | ||
3436 | c->h = h; | |
3437 | return c; | |
3438 | } | |
3439 | ||
3440 | /* For operations that can wait for kmalloc to possibly sleep, | |
3441 | * this routine can be called. Lock need not be held to call | |
3442 | * cmd_special_alloc. cmd_special_free() is the complement. | |
3443 | */ | |
3444 | static struct CommandList *cmd_special_alloc(struct ctlr_info *h) | |
3445 | { | |
3446 | struct CommandList *c; | |
3447 | union u64bit temp64; | |
3448 | dma_addr_t cmd_dma_handle, err_dma_handle; | |
3449 | ||
3450 | c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle); | |
3451 | if (c == NULL) | |
3452 | return NULL; | |
3453 | memset(c, 0, sizeof(*c)); | |
3454 | ||
e1f7de0c | 3455 | c->cmd_type = CMD_SCSI; |
edd16368 SC |
3456 | c->cmdindex = -1; |
3457 | ||
3458 | c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info), | |
3459 | &err_dma_handle); | |
3460 | ||
3461 | if (c->err_info == NULL) { | |
3462 | pci_free_consistent(h->pdev, | |
3463 | sizeof(*c), c, cmd_dma_handle); | |
3464 | return NULL; | |
3465 | } | |
3466 | memset(c->err_info, 0, sizeof(*c->err_info)); | |
3467 | ||
9e0fc764 | 3468 | INIT_LIST_HEAD(&c->list); |
01a02ffc SC |
3469 | c->busaddr = (u32) cmd_dma_handle; |
3470 | temp64.val = (u64) err_dma_handle; | |
edd16368 SC |
3471 | c->ErrDesc.Addr.lower = temp64.val32.lower; |
3472 | c->ErrDesc.Addr.upper = temp64.val32.upper; | |
3473 | c->ErrDesc.Len = sizeof(*c->err_info); | |
3474 | ||
3475 | c->h = h; | |
3476 | return c; | |
3477 | } | |
3478 | ||
3479 | static void cmd_free(struct ctlr_info *h, struct CommandList *c) | |
3480 | { | |
3481 | int i; | |
e16a33ad | 3482 | unsigned long flags; |
edd16368 SC |
3483 | |
3484 | i = c - h->cmd_pool; | |
e16a33ad | 3485 | spin_lock_irqsave(&h->lock, flags); |
edd16368 SC |
3486 | clear_bit(i & (BITS_PER_LONG - 1), |
3487 | h->cmd_pool_bits + (i / BITS_PER_LONG)); | |
e16a33ad | 3488 | spin_unlock_irqrestore(&h->lock, flags); |
edd16368 SC |
3489 | } |
3490 | ||
3491 | static void cmd_special_free(struct ctlr_info *h, struct CommandList *c) | |
3492 | { | |
3493 | union u64bit temp64; | |
3494 | ||
3495 | temp64.val32.lower = c->ErrDesc.Addr.lower; | |
3496 | temp64.val32.upper = c->ErrDesc.Addr.upper; | |
3497 | pci_free_consistent(h->pdev, sizeof(*c->err_info), | |
3498 | c->err_info, (dma_addr_t) temp64.val); | |
3499 | pci_free_consistent(h->pdev, sizeof(*c), | |
d896f3f3 | 3500 | c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK)); |
edd16368 SC |
3501 | } |
3502 | ||
3503 | #ifdef CONFIG_COMPAT | |
3504 | ||
edd16368 SC |
3505 | static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg) |
3506 | { | |
3507 | IOCTL32_Command_struct __user *arg32 = | |
3508 | (IOCTL32_Command_struct __user *) arg; | |
3509 | IOCTL_Command_struct arg64; | |
3510 | IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64)); | |
3511 | int err; | |
3512 | u32 cp; | |
3513 | ||
938abd84 | 3514 | memset(&arg64, 0, sizeof(arg64)); |
edd16368 SC |
3515 | err = 0; |
3516 | err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, | |
3517 | sizeof(arg64.LUN_info)); | |
3518 | err |= copy_from_user(&arg64.Request, &arg32->Request, | |
3519 | sizeof(arg64.Request)); | |
3520 | err |= copy_from_user(&arg64.error_info, &arg32->error_info, | |
3521 | sizeof(arg64.error_info)); | |
3522 | err |= get_user(arg64.buf_size, &arg32->buf_size); | |
3523 | err |= get_user(cp, &arg32->buf); | |
3524 | arg64.buf = compat_ptr(cp); | |
3525 | err |= copy_to_user(p, &arg64, sizeof(arg64)); | |
3526 | ||
3527 | if (err) | |
3528 | return -EFAULT; | |
3529 | ||
e39eeaed | 3530 | err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p); |
edd16368 SC |
3531 | if (err) |
3532 | return err; | |
3533 | err |= copy_in_user(&arg32->error_info, &p->error_info, | |
3534 | sizeof(arg32->error_info)); | |
3535 | if (err) | |
3536 | return -EFAULT; | |
3537 | return err; | |
3538 | } | |
3539 | ||
3540 | static int hpsa_ioctl32_big_passthru(struct scsi_device *dev, | |
3541 | int cmd, void *arg) | |
3542 | { | |
3543 | BIG_IOCTL32_Command_struct __user *arg32 = | |
3544 | (BIG_IOCTL32_Command_struct __user *) arg; | |
3545 | BIG_IOCTL_Command_struct arg64; | |
3546 | BIG_IOCTL_Command_struct __user *p = | |
3547 | compat_alloc_user_space(sizeof(arg64)); | |
3548 | int err; | |
3549 | u32 cp; | |
3550 | ||
938abd84 | 3551 | memset(&arg64, 0, sizeof(arg64)); |
edd16368 SC |
3552 | err = 0; |
3553 | err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, | |
3554 | sizeof(arg64.LUN_info)); | |
3555 | err |= copy_from_user(&arg64.Request, &arg32->Request, | |
3556 | sizeof(arg64.Request)); | |
3557 | err |= copy_from_user(&arg64.error_info, &arg32->error_info, | |
3558 | sizeof(arg64.error_info)); | |
3559 | err |= get_user(arg64.buf_size, &arg32->buf_size); | |
3560 | err |= get_user(arg64.malloc_size, &arg32->malloc_size); | |
3561 | err |= get_user(cp, &arg32->buf); | |
3562 | arg64.buf = compat_ptr(cp); | |
3563 | err |= copy_to_user(p, &arg64, sizeof(arg64)); | |
3564 | ||
3565 | if (err) | |
3566 | return -EFAULT; | |
3567 | ||
e39eeaed | 3568 | err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p); |
edd16368 SC |
3569 | if (err) |
3570 | return err; | |
3571 | err |= copy_in_user(&arg32->error_info, &p->error_info, | |
3572 | sizeof(arg32->error_info)); | |
3573 | if (err) | |
3574 | return -EFAULT; | |
3575 | return err; | |
3576 | } | |
71fe75a7 SC |
3577 | |
3578 | static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg) | |
3579 | { | |
3580 | switch (cmd) { | |
3581 | case CCISS_GETPCIINFO: | |
3582 | case CCISS_GETINTINFO: | |
3583 | case CCISS_SETINTINFO: | |
3584 | case CCISS_GETNODENAME: | |
3585 | case CCISS_SETNODENAME: | |
3586 | case CCISS_GETHEARTBEAT: | |
3587 | case CCISS_GETBUSTYPES: | |
3588 | case CCISS_GETFIRMVER: | |
3589 | case CCISS_GETDRIVVER: | |
3590 | case CCISS_REVALIDVOLS: | |
3591 | case CCISS_DEREGDISK: | |
3592 | case CCISS_REGNEWDISK: | |
3593 | case CCISS_REGNEWD: | |
3594 | case CCISS_RESCANDISK: | |
3595 | case CCISS_GETLUNINFO: | |
3596 | return hpsa_ioctl(dev, cmd, arg); | |
3597 | ||
3598 | case CCISS_PASSTHRU32: | |
3599 | return hpsa_ioctl32_passthru(dev, cmd, arg); | |
3600 | case CCISS_BIG_PASSTHRU32: | |
3601 | return hpsa_ioctl32_big_passthru(dev, cmd, arg); | |
3602 | ||
3603 | default: | |
3604 | return -ENOIOCTLCMD; | |
3605 | } | |
3606 | } | |
edd16368 SC |
3607 | #endif |
3608 | ||
3609 | static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp) | |
3610 | { | |
3611 | struct hpsa_pci_info pciinfo; | |
3612 | ||
3613 | if (!argp) | |
3614 | return -EINVAL; | |
3615 | pciinfo.domain = pci_domain_nr(h->pdev->bus); | |
3616 | pciinfo.bus = h->pdev->bus->number; | |
3617 | pciinfo.dev_fn = h->pdev->devfn; | |
3618 | pciinfo.board_id = h->board_id; | |
3619 | if (copy_to_user(argp, &pciinfo, sizeof(pciinfo))) | |
3620 | return -EFAULT; | |
3621 | return 0; | |
3622 | } | |
3623 | ||
3624 | static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp) | |
3625 | { | |
3626 | DriverVer_type DriverVer; | |
3627 | unsigned char vmaj, vmin, vsubmin; | |
3628 | int rc; | |
3629 | ||
3630 | rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu", | |
3631 | &vmaj, &vmin, &vsubmin); | |
3632 | if (rc != 3) { | |
3633 | dev_info(&h->pdev->dev, "driver version string '%s' " | |
3634 | "unrecognized.", HPSA_DRIVER_VERSION); | |
3635 | vmaj = 0; | |
3636 | vmin = 0; | |
3637 | vsubmin = 0; | |
3638 | } | |
3639 | DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin; | |
3640 | if (!argp) | |
3641 | return -EINVAL; | |
3642 | if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type))) | |
3643 | return -EFAULT; | |
3644 | return 0; | |
3645 | } | |
3646 | ||
3647 | static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp) | |
3648 | { | |
3649 | IOCTL_Command_struct iocommand; | |
3650 | struct CommandList *c; | |
3651 | char *buff = NULL; | |
3652 | union u64bit temp64; | |
c1f63c8f | 3653 | int rc = 0; |
edd16368 SC |
3654 | |
3655 | if (!argp) | |
3656 | return -EINVAL; | |
3657 | if (!capable(CAP_SYS_RAWIO)) | |
3658 | return -EPERM; | |
3659 | if (copy_from_user(&iocommand, argp, sizeof(iocommand))) | |
3660 | return -EFAULT; | |
3661 | if ((iocommand.buf_size < 1) && | |
3662 | (iocommand.Request.Type.Direction != XFER_NONE)) { | |
3663 | return -EINVAL; | |
3664 | } | |
3665 | if (iocommand.buf_size > 0) { | |
3666 | buff = kmalloc(iocommand.buf_size, GFP_KERNEL); | |
3667 | if (buff == NULL) | |
3668 | return -EFAULT; | |
b03a7771 SC |
3669 | if (iocommand.Request.Type.Direction == XFER_WRITE) { |
3670 | /* Copy the data into the buffer we created */ | |
3671 | if (copy_from_user(buff, iocommand.buf, | |
3672 | iocommand.buf_size)) { | |
c1f63c8f SC |
3673 | rc = -EFAULT; |
3674 | goto out_kfree; | |
b03a7771 SC |
3675 | } |
3676 | } else { | |
3677 | memset(buff, 0, iocommand.buf_size); | |
edd16368 | 3678 | } |
b03a7771 | 3679 | } |
edd16368 SC |
3680 | c = cmd_special_alloc(h); |
3681 | if (c == NULL) { | |
c1f63c8f SC |
3682 | rc = -ENOMEM; |
3683 | goto out_kfree; | |
edd16368 SC |
3684 | } |
3685 | /* Fill in the command type */ | |
3686 | c->cmd_type = CMD_IOCTL_PEND; | |
3687 | /* Fill in Command Header */ | |
3688 | c->Header.ReplyQueue = 0; /* unused in simple mode */ | |
3689 | if (iocommand.buf_size > 0) { /* buffer to fill */ | |
3690 | c->Header.SGList = 1; | |
3691 | c->Header.SGTotal = 1; | |
3692 | } else { /* no buffers to fill */ | |
3693 | c->Header.SGList = 0; | |
3694 | c->Header.SGTotal = 0; | |
3695 | } | |
3696 | memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN)); | |
3697 | /* use the kernel address the cmd block for tag */ | |
3698 | c->Header.Tag.lower = c->busaddr; | |
3699 | ||
3700 | /* Fill in Request block */ | |
3701 | memcpy(&c->Request, &iocommand.Request, | |
3702 | sizeof(c->Request)); | |
3703 | ||
3704 | /* Fill in the scatter gather information */ | |
3705 | if (iocommand.buf_size > 0) { | |
3706 | temp64.val = pci_map_single(h->pdev, buff, | |
3707 | iocommand.buf_size, PCI_DMA_BIDIRECTIONAL); | |
bcc48ffa SC |
3708 | if (dma_mapping_error(&h->pdev->dev, temp64.val)) { |
3709 | c->SG[0].Addr.lower = 0; | |
3710 | c->SG[0].Addr.upper = 0; | |
3711 | c->SG[0].Len = 0; | |
3712 | rc = -ENOMEM; | |
3713 | goto out; | |
3714 | } | |
edd16368 SC |
3715 | c->SG[0].Addr.lower = temp64.val32.lower; |
3716 | c->SG[0].Addr.upper = temp64.val32.upper; | |
3717 | c->SG[0].Len = iocommand.buf_size; | |
e1d9cbfa | 3718 | c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/ |
edd16368 | 3719 | } |
a0c12413 | 3720 | hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c); |
c2dd32e0 SC |
3721 | if (iocommand.buf_size > 0) |
3722 | hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL); | |
edd16368 SC |
3723 | check_ioctl_unit_attention(h, c); |
3724 | ||
3725 | /* Copy the error information out */ | |
3726 | memcpy(&iocommand.error_info, c->err_info, | |
3727 | sizeof(iocommand.error_info)); | |
3728 | if (copy_to_user(argp, &iocommand, sizeof(iocommand))) { | |
c1f63c8f SC |
3729 | rc = -EFAULT; |
3730 | goto out; | |
edd16368 | 3731 | } |
b03a7771 SC |
3732 | if (iocommand.Request.Type.Direction == XFER_READ && |
3733 | iocommand.buf_size > 0) { | |
edd16368 SC |
3734 | /* Copy the data out of the buffer we created */ |
3735 | if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) { | |
c1f63c8f SC |
3736 | rc = -EFAULT; |
3737 | goto out; | |
edd16368 SC |
3738 | } |
3739 | } | |
c1f63c8f | 3740 | out: |
edd16368 | 3741 | cmd_special_free(h, c); |
c1f63c8f SC |
3742 | out_kfree: |
3743 | kfree(buff); | |
3744 | return rc; | |
edd16368 SC |
3745 | } |
3746 | ||
3747 | static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp) | |
3748 | { | |
3749 | BIG_IOCTL_Command_struct *ioc; | |
3750 | struct CommandList *c; | |
3751 | unsigned char **buff = NULL; | |
3752 | int *buff_size = NULL; | |
3753 | union u64bit temp64; | |
3754 | BYTE sg_used = 0; | |
3755 | int status = 0; | |
3756 | int i; | |
01a02ffc SC |
3757 | u32 left; |
3758 | u32 sz; | |
edd16368 SC |
3759 | BYTE __user *data_ptr; |
3760 | ||
3761 | if (!argp) | |
3762 | return -EINVAL; | |
3763 | if (!capable(CAP_SYS_RAWIO)) | |
3764 | return -EPERM; | |
3765 | ioc = (BIG_IOCTL_Command_struct *) | |
3766 | kmalloc(sizeof(*ioc), GFP_KERNEL); | |
3767 | if (!ioc) { | |
3768 | status = -ENOMEM; | |
3769 | goto cleanup1; | |
3770 | } | |
3771 | if (copy_from_user(ioc, argp, sizeof(*ioc))) { | |
3772 | status = -EFAULT; | |
3773 | goto cleanup1; | |
3774 | } | |
3775 | if ((ioc->buf_size < 1) && | |
3776 | (ioc->Request.Type.Direction != XFER_NONE)) { | |
3777 | status = -EINVAL; | |
3778 | goto cleanup1; | |
3779 | } | |
3780 | /* Check kmalloc limits using all SGs */ | |
3781 | if (ioc->malloc_size > MAX_KMALLOC_SIZE) { | |
3782 | status = -EINVAL; | |
3783 | goto cleanup1; | |
3784 | } | |
d66ae08b | 3785 | if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) { |
edd16368 SC |
3786 | status = -EINVAL; |
3787 | goto cleanup1; | |
3788 | } | |
d66ae08b | 3789 | buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL); |
edd16368 SC |
3790 | if (!buff) { |
3791 | status = -ENOMEM; | |
3792 | goto cleanup1; | |
3793 | } | |
d66ae08b | 3794 | buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL); |
edd16368 SC |
3795 | if (!buff_size) { |
3796 | status = -ENOMEM; | |
3797 | goto cleanup1; | |
3798 | } | |
3799 | left = ioc->buf_size; | |
3800 | data_ptr = ioc->buf; | |
3801 | while (left) { | |
3802 | sz = (left > ioc->malloc_size) ? ioc->malloc_size : left; | |
3803 | buff_size[sg_used] = sz; | |
3804 | buff[sg_used] = kmalloc(sz, GFP_KERNEL); | |
3805 | if (buff[sg_used] == NULL) { | |
3806 | status = -ENOMEM; | |
3807 | goto cleanup1; | |
3808 | } | |
3809 | if (ioc->Request.Type.Direction == XFER_WRITE) { | |
3810 | if (copy_from_user(buff[sg_used], data_ptr, sz)) { | |
3811 | status = -ENOMEM; | |
3812 | goto cleanup1; | |
3813 | } | |
3814 | } else | |
3815 | memset(buff[sg_used], 0, sz); | |
3816 | left -= sz; | |
3817 | data_ptr += sz; | |
3818 | sg_used++; | |
3819 | } | |
3820 | c = cmd_special_alloc(h); | |
3821 | if (c == NULL) { | |
3822 | status = -ENOMEM; | |
3823 | goto cleanup1; | |
3824 | } | |
3825 | c->cmd_type = CMD_IOCTL_PEND; | |
3826 | c->Header.ReplyQueue = 0; | |
b03a7771 | 3827 | c->Header.SGList = c->Header.SGTotal = sg_used; |
edd16368 SC |
3828 | memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN)); |
3829 | c->Header.Tag.lower = c->busaddr; | |
3830 | memcpy(&c->Request, &ioc->Request, sizeof(c->Request)); | |
3831 | if (ioc->buf_size > 0) { | |
3832 | int i; | |
3833 | for (i = 0; i < sg_used; i++) { | |
3834 | temp64.val = pci_map_single(h->pdev, buff[i], | |
3835 | buff_size[i], PCI_DMA_BIDIRECTIONAL); | |
bcc48ffa SC |
3836 | if (dma_mapping_error(&h->pdev->dev, temp64.val)) { |
3837 | c->SG[i].Addr.lower = 0; | |
3838 | c->SG[i].Addr.upper = 0; | |
3839 | c->SG[i].Len = 0; | |
3840 | hpsa_pci_unmap(h->pdev, c, i, | |
3841 | PCI_DMA_BIDIRECTIONAL); | |
3842 | status = -ENOMEM; | |
e2d4a1f6 | 3843 | goto cleanup0; |
bcc48ffa | 3844 | } |
edd16368 SC |
3845 | c->SG[i].Addr.lower = temp64.val32.lower; |
3846 | c->SG[i].Addr.upper = temp64.val32.upper; | |
3847 | c->SG[i].Len = buff_size[i]; | |
e1d9cbfa | 3848 | c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST; |
edd16368 SC |
3849 | } |
3850 | } | |
a0c12413 | 3851 | hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c); |
b03a7771 SC |
3852 | if (sg_used) |
3853 | hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL); | |
edd16368 SC |
3854 | check_ioctl_unit_attention(h, c); |
3855 | /* Copy the error information out */ | |
3856 | memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info)); | |
3857 | if (copy_to_user(argp, ioc, sizeof(*ioc))) { | |
edd16368 | 3858 | status = -EFAULT; |
e2d4a1f6 | 3859 | goto cleanup0; |
edd16368 | 3860 | } |
b03a7771 | 3861 | if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) { |
edd16368 SC |
3862 | /* Copy the data out of the buffer we created */ |
3863 | BYTE __user *ptr = ioc->buf; | |
3864 | for (i = 0; i < sg_used; i++) { | |
3865 | if (copy_to_user(ptr, buff[i], buff_size[i])) { | |
edd16368 | 3866 | status = -EFAULT; |
e2d4a1f6 | 3867 | goto cleanup0; |
edd16368 SC |
3868 | } |
3869 | ptr += buff_size[i]; | |
3870 | } | |
3871 | } | |
edd16368 | 3872 | status = 0; |
e2d4a1f6 SC |
3873 | cleanup0: |
3874 | cmd_special_free(h, c); | |
edd16368 SC |
3875 | cleanup1: |
3876 | if (buff) { | |
3877 | for (i = 0; i < sg_used; i++) | |
3878 | kfree(buff[i]); | |
3879 | kfree(buff); | |
3880 | } | |
3881 | kfree(buff_size); | |
3882 | kfree(ioc); | |
3883 | return status; | |
3884 | } | |
3885 | ||
3886 | static void check_ioctl_unit_attention(struct ctlr_info *h, | |
3887 | struct CommandList *c) | |
3888 | { | |
3889 | if (c->err_info->CommandStatus == CMD_TARGET_STATUS && | |
3890 | c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) | |
3891 | (void) check_for_unit_attention(h, c); | |
3892 | } | |
0390f0c0 SC |
3893 | |
3894 | static int increment_passthru_count(struct ctlr_info *h) | |
3895 | { | |
3896 | unsigned long flags; | |
3897 | ||
3898 | spin_lock_irqsave(&h->passthru_count_lock, flags); | |
3899 | if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) { | |
3900 | spin_unlock_irqrestore(&h->passthru_count_lock, flags); | |
3901 | return -1; | |
3902 | } | |
3903 | h->passthru_count++; | |
3904 | spin_unlock_irqrestore(&h->passthru_count_lock, flags); | |
3905 | return 0; | |
3906 | } | |
3907 | ||
3908 | static void decrement_passthru_count(struct ctlr_info *h) | |
3909 | { | |
3910 | unsigned long flags; | |
3911 | ||
3912 | spin_lock_irqsave(&h->passthru_count_lock, flags); | |
3913 | if (h->passthru_count <= 0) { | |
3914 | spin_unlock_irqrestore(&h->passthru_count_lock, flags); | |
3915 | /* not expecting to get here. */ | |
3916 | dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n"); | |
3917 | return; | |
3918 | } | |
3919 | h->passthru_count--; | |
3920 | spin_unlock_irqrestore(&h->passthru_count_lock, flags); | |
3921 | } | |
3922 | ||
edd16368 SC |
3923 | /* |
3924 | * ioctl | |
3925 | */ | |
3926 | static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg) | |
3927 | { | |
3928 | struct ctlr_info *h; | |
3929 | void __user *argp = (void __user *)arg; | |
0390f0c0 | 3930 | int rc; |
edd16368 SC |
3931 | |
3932 | h = sdev_to_hba(dev); | |
3933 | ||
3934 | switch (cmd) { | |
3935 | case CCISS_DEREGDISK: | |
3936 | case CCISS_REGNEWDISK: | |
3937 | case CCISS_REGNEWD: | |
a08a8471 | 3938 | hpsa_scan_start(h->scsi_host); |
edd16368 SC |
3939 | return 0; |
3940 | case CCISS_GETPCIINFO: | |
3941 | return hpsa_getpciinfo_ioctl(h, argp); | |
3942 | case CCISS_GETDRIVVER: | |
3943 | return hpsa_getdrivver_ioctl(h, argp); | |
3944 | case CCISS_PASSTHRU: | |
0390f0c0 SC |
3945 | if (increment_passthru_count(h)) |
3946 | return -EAGAIN; | |
3947 | rc = hpsa_passthru_ioctl(h, argp); | |
3948 | decrement_passthru_count(h); | |
3949 | return rc; | |
edd16368 | 3950 | case CCISS_BIG_PASSTHRU: |
0390f0c0 SC |
3951 | if (increment_passthru_count(h)) |
3952 | return -EAGAIN; | |
3953 | rc = hpsa_big_passthru_ioctl(h, argp); | |
3954 | decrement_passthru_count(h); | |
3955 | return rc; | |
edd16368 SC |
3956 | default: |
3957 | return -ENOTTY; | |
3958 | } | |
3959 | } | |
3960 | ||
6f039790 GKH |
3961 | static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr, |
3962 | u8 reset_type) | |
64670ac8 SC |
3963 | { |
3964 | struct CommandList *c; | |
3965 | ||
3966 | c = cmd_alloc(h); | |
3967 | if (!c) | |
3968 | return -ENOMEM; | |
a2dac136 SC |
3969 | /* fill_cmd can't fail here, no data buffer to map */ |
3970 | (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, | |
64670ac8 SC |
3971 | RAID_CTLR_LUNID, TYPE_MSG); |
3972 | c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */ | |
3973 | c->waiting = NULL; | |
3974 | enqueue_cmd_and_start_io(h, c); | |
3975 | /* Don't wait for completion, the reset won't complete. Don't free | |
3976 | * the command either. This is the last command we will send before | |
3977 | * re-initializing everything, so it doesn't matter and won't leak. | |
3978 | */ | |
3979 | return 0; | |
3980 | } | |
3981 | ||
a2dac136 | 3982 | static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h, |
01a02ffc | 3983 | void *buff, size_t size, u8 page_code, unsigned char *scsi3addr, |
edd16368 SC |
3984 | int cmd_type) |
3985 | { | |
3986 | int pci_dir = XFER_NONE; | |
75167d2c | 3987 | struct CommandList *a; /* for commands to be aborted */ |
edd16368 SC |
3988 | |
3989 | c->cmd_type = CMD_IOCTL_PEND; | |
3990 | c->Header.ReplyQueue = 0; | |
3991 | if (buff != NULL && size > 0) { | |
3992 | c->Header.SGList = 1; | |
3993 | c->Header.SGTotal = 1; | |
3994 | } else { | |
3995 | c->Header.SGList = 0; | |
3996 | c->Header.SGTotal = 0; | |
3997 | } | |
3998 | c->Header.Tag.lower = c->busaddr; | |
3999 | memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8); | |
4000 | ||
4001 | c->Request.Type.Type = cmd_type; | |
4002 | if (cmd_type == TYPE_CMD) { | |
4003 | switch (cmd) { | |
4004 | case HPSA_INQUIRY: | |
4005 | /* are we trying to read a vital product page */ | |
4006 | if (page_code != 0) { | |
4007 | c->Request.CDB[1] = 0x01; | |
4008 | c->Request.CDB[2] = page_code; | |
4009 | } | |
4010 | c->Request.CDBLen = 6; | |
4011 | c->Request.Type.Attribute = ATTR_SIMPLE; | |
4012 | c->Request.Type.Direction = XFER_READ; | |
4013 | c->Request.Timeout = 0; | |
4014 | c->Request.CDB[0] = HPSA_INQUIRY; | |
4015 | c->Request.CDB[4] = size & 0xFF; | |
4016 | break; | |
4017 | case HPSA_REPORT_LOG: | |
4018 | case HPSA_REPORT_PHYS: | |
4019 | /* Talking to controller so It's a physical command | |
4020 | mode = 00 target = 0. Nothing to write. | |
4021 | */ | |
4022 | c->Request.CDBLen = 12; | |
4023 | c->Request.Type.Attribute = ATTR_SIMPLE; | |
4024 | c->Request.Type.Direction = XFER_READ; | |
4025 | c->Request.Timeout = 0; | |
4026 | c->Request.CDB[0] = cmd; | |
4027 | c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */ | |
4028 | c->Request.CDB[7] = (size >> 16) & 0xFF; | |
4029 | c->Request.CDB[8] = (size >> 8) & 0xFF; | |
4030 | c->Request.CDB[9] = size & 0xFF; | |
4031 | break; | |
edd16368 SC |
4032 | case HPSA_CACHE_FLUSH: |
4033 | c->Request.CDBLen = 12; | |
4034 | c->Request.Type.Attribute = ATTR_SIMPLE; | |
4035 | c->Request.Type.Direction = XFER_WRITE; | |
4036 | c->Request.Timeout = 0; | |
4037 | c->Request.CDB[0] = BMIC_WRITE; | |
4038 | c->Request.CDB[6] = BMIC_CACHE_FLUSH; | |
bb158eab SC |
4039 | c->Request.CDB[7] = (size >> 8) & 0xFF; |
4040 | c->Request.CDB[8] = size & 0xFF; | |
edd16368 SC |
4041 | break; |
4042 | case TEST_UNIT_READY: | |
4043 | c->Request.CDBLen = 6; | |
4044 | c->Request.Type.Attribute = ATTR_SIMPLE; | |
4045 | c->Request.Type.Direction = XFER_NONE; | |
4046 | c->Request.Timeout = 0; | |
4047 | break; | |
283b4a9b SC |
4048 | case HPSA_GET_RAID_MAP: |
4049 | c->Request.CDBLen = 12; | |
4050 | c->Request.Type.Attribute = ATTR_SIMPLE; | |
4051 | c->Request.Type.Direction = XFER_READ; | |
4052 | c->Request.Timeout = 0; | |
4053 | c->Request.CDB[0] = HPSA_CISS_READ; | |
4054 | c->Request.CDB[1] = cmd; | |
4055 | c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */ | |
4056 | c->Request.CDB[7] = (size >> 16) & 0xFF; | |
4057 | c->Request.CDB[8] = (size >> 8) & 0xFF; | |
4058 | c->Request.CDB[9] = size & 0xFF; | |
4059 | break; | |
edd16368 SC |
4060 | default: |
4061 | dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd); | |
4062 | BUG(); | |
a2dac136 | 4063 | return -1; |
edd16368 SC |
4064 | } |
4065 | } else if (cmd_type == TYPE_MSG) { | |
4066 | switch (cmd) { | |
4067 | ||
4068 | case HPSA_DEVICE_RESET_MSG: | |
4069 | c->Request.CDBLen = 16; | |
4070 | c->Request.Type.Type = 1; /* It is a MSG not a CMD */ | |
4071 | c->Request.Type.Attribute = ATTR_SIMPLE; | |
4072 | c->Request.Type.Direction = XFER_NONE; | |
4073 | c->Request.Timeout = 0; /* Don't time out */ | |
64670ac8 SC |
4074 | memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB)); |
4075 | c->Request.CDB[0] = cmd; | |
21e89afd | 4076 | c->Request.CDB[1] = HPSA_RESET_TYPE_LUN; |
edd16368 SC |
4077 | /* If bytes 4-7 are zero, it means reset the */ |
4078 | /* LunID device */ | |
4079 | c->Request.CDB[4] = 0x00; | |
4080 | c->Request.CDB[5] = 0x00; | |
4081 | c->Request.CDB[6] = 0x00; | |
4082 | c->Request.CDB[7] = 0x00; | |
75167d2c SC |
4083 | break; |
4084 | case HPSA_ABORT_MSG: | |
4085 | a = buff; /* point to command to be aborted */ | |
4086 | dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n", | |
4087 | a->Header.Tag.upper, a->Header.Tag.lower, | |
4088 | c->Header.Tag.upper, c->Header.Tag.lower); | |
4089 | c->Request.CDBLen = 16; | |
4090 | c->Request.Type.Type = TYPE_MSG; | |
4091 | c->Request.Type.Attribute = ATTR_SIMPLE; | |
4092 | c->Request.Type.Direction = XFER_WRITE; | |
4093 | c->Request.Timeout = 0; /* Don't time out */ | |
4094 | c->Request.CDB[0] = HPSA_TASK_MANAGEMENT; | |
4095 | c->Request.CDB[1] = HPSA_TMF_ABORT_TASK; | |
4096 | c->Request.CDB[2] = 0x00; /* reserved */ | |
4097 | c->Request.CDB[3] = 0x00; /* reserved */ | |
4098 | /* Tag to abort goes in CDB[4]-CDB[11] */ | |
4099 | c->Request.CDB[4] = a->Header.Tag.lower & 0xFF; | |
4100 | c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF; | |
4101 | c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF; | |
4102 | c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF; | |
4103 | c->Request.CDB[8] = a->Header.Tag.upper & 0xFF; | |
4104 | c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF; | |
4105 | c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF; | |
4106 | c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF; | |
4107 | c->Request.CDB[12] = 0x00; /* reserved */ | |
4108 | c->Request.CDB[13] = 0x00; /* reserved */ | |
4109 | c->Request.CDB[14] = 0x00; /* reserved */ | |
4110 | c->Request.CDB[15] = 0x00; /* reserved */ | |
edd16368 | 4111 | break; |
edd16368 SC |
4112 | default: |
4113 | dev_warn(&h->pdev->dev, "unknown message type %d\n", | |
4114 | cmd); | |
4115 | BUG(); | |
4116 | } | |
4117 | } else { | |
4118 | dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type); | |
4119 | BUG(); | |
4120 | } | |
4121 | ||
4122 | switch (c->Request.Type.Direction) { | |
4123 | case XFER_READ: | |
4124 | pci_dir = PCI_DMA_FROMDEVICE; | |
4125 | break; | |
4126 | case XFER_WRITE: | |
4127 | pci_dir = PCI_DMA_TODEVICE; | |
4128 | break; | |
4129 | case XFER_NONE: | |
4130 | pci_dir = PCI_DMA_NONE; | |
4131 | break; | |
4132 | default: | |
4133 | pci_dir = PCI_DMA_BIDIRECTIONAL; | |
4134 | } | |
a2dac136 SC |
4135 | if (hpsa_map_one(h->pdev, c, buff, size, pci_dir)) |
4136 | return -1; | |
4137 | return 0; | |
edd16368 SC |
4138 | } |
4139 | ||
4140 | /* | |
4141 | * Map (physical) PCI mem into (virtual) kernel space | |
4142 | */ | |
4143 | static void __iomem *remap_pci_mem(ulong base, ulong size) | |
4144 | { | |
4145 | ulong page_base = ((ulong) base) & PAGE_MASK; | |
4146 | ulong page_offs = ((ulong) base) - page_base; | |
088ba34c SC |
4147 | void __iomem *page_remapped = ioremap_nocache(page_base, |
4148 | page_offs + size); | |
edd16368 SC |
4149 | |
4150 | return page_remapped ? (page_remapped + page_offs) : NULL; | |
4151 | } | |
4152 | ||
4153 | /* Takes cmds off the submission queue and sends them to the hardware, | |
4154 | * then puts them on the queue of cmds waiting for completion. | |
4155 | */ | |
4156 | static void start_io(struct ctlr_info *h) | |
4157 | { | |
4158 | struct CommandList *c; | |
e16a33ad | 4159 | unsigned long flags; |
edd16368 | 4160 | |
e16a33ad | 4161 | spin_lock_irqsave(&h->lock, flags); |
9e0fc764 SC |
4162 | while (!list_empty(&h->reqQ)) { |
4163 | c = list_entry(h->reqQ.next, struct CommandList, list); | |
edd16368 SC |
4164 | /* can't do anything if fifo is full */ |
4165 | if ((h->access.fifo_full(h))) { | |
396883e2 | 4166 | h->fifo_recently_full = 1; |
edd16368 SC |
4167 | dev_warn(&h->pdev->dev, "fifo full\n"); |
4168 | break; | |
4169 | } | |
396883e2 | 4170 | h->fifo_recently_full = 0; |
edd16368 SC |
4171 | |
4172 | /* Get the first entry from the Request Q */ | |
4173 | removeQ(c); | |
4174 | h->Qdepth--; | |
4175 | ||
edd16368 SC |
4176 | /* Put job onto the completed Q */ |
4177 | addQ(&h->cmpQ, c); | |
e16a33ad MG |
4178 | |
4179 | /* Must increment commands_outstanding before unlocking | |
4180 | * and submitting to avoid race checking for fifo full | |
4181 | * condition. | |
4182 | */ | |
4183 | h->commands_outstanding++; | |
4184 | if (h->commands_outstanding > h->max_outstanding) | |
4185 | h->max_outstanding = h->commands_outstanding; | |
4186 | ||
4187 | /* Tell the controller execute command */ | |
4188 | spin_unlock_irqrestore(&h->lock, flags); | |
4189 | h->access.submit_command(h, c); | |
4190 | spin_lock_irqsave(&h->lock, flags); | |
edd16368 | 4191 | } |
e16a33ad | 4192 | spin_unlock_irqrestore(&h->lock, flags); |
edd16368 SC |
4193 | } |
4194 | ||
254f796b | 4195 | static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q) |
edd16368 | 4196 | { |
254f796b | 4197 | return h->access.command_completed(h, q); |
edd16368 SC |
4198 | } |
4199 | ||
900c5440 | 4200 | static inline bool interrupt_pending(struct ctlr_info *h) |
edd16368 SC |
4201 | { |
4202 | return h->access.intr_pending(h); | |
4203 | } | |
4204 | ||
4205 | static inline long interrupt_not_for_us(struct ctlr_info *h) | |
4206 | { | |
10f66018 SC |
4207 | return (h->access.intr_pending(h) == 0) || |
4208 | (h->interrupts_enabled == 0); | |
edd16368 SC |
4209 | } |
4210 | ||
01a02ffc SC |
4211 | static inline int bad_tag(struct ctlr_info *h, u32 tag_index, |
4212 | u32 raw_tag) | |
edd16368 SC |
4213 | { |
4214 | if (unlikely(tag_index >= h->nr_cmds)) { | |
4215 | dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag); | |
4216 | return 1; | |
4217 | } | |
4218 | return 0; | |
4219 | } | |
4220 | ||
5a3d16f5 | 4221 | static inline void finish_cmd(struct CommandList *c) |
edd16368 | 4222 | { |
e16a33ad | 4223 | unsigned long flags; |
396883e2 SC |
4224 | int io_may_be_stalled = 0; |
4225 | struct ctlr_info *h = c->h; | |
e16a33ad | 4226 | |
396883e2 | 4227 | spin_lock_irqsave(&h->lock, flags); |
edd16368 | 4228 | removeQ(c); |
396883e2 SC |
4229 | |
4230 | /* | |
4231 | * Check for possibly stalled i/o. | |
4232 | * | |
4233 | * If a fifo_full condition is encountered, requests will back up | |
4234 | * in h->reqQ. This queue is only emptied out by start_io which is | |
4235 | * only called when a new i/o request comes in. If no i/o's are | |
4236 | * forthcoming, the i/o's in h->reqQ can get stuck. So we call | |
4237 | * start_io from here if we detect such a danger. | |
4238 | * | |
4239 | * Normally, we shouldn't hit this case, but pounding on the | |
4240 | * CCISS_PASSTHRU ioctl can provoke it. Only call start_io if | |
4241 | * commands_outstanding is low. We want to avoid calling | |
4242 | * start_io from in here as much as possible, and esp. don't | |
4243 | * want to get in a cycle where we call start_io every time | |
4244 | * through here. | |
4245 | */ | |
4246 | if (unlikely(h->fifo_recently_full) && | |
4247 | h->commands_outstanding < 5) | |
4248 | io_may_be_stalled = 1; | |
4249 | ||
4250 | spin_unlock_irqrestore(&h->lock, flags); | |
4251 | ||
e85c5974 | 4252 | dial_up_lockup_detection_on_fw_flash_complete(c->h, c); |
e1f7de0c | 4253 | if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI)) |
1fb011fb | 4254 | complete_scsi_command(c); |
edd16368 SC |
4255 | else if (c->cmd_type == CMD_IOCTL_PEND) |
4256 | complete(c->waiting); | |
396883e2 SC |
4257 | if (unlikely(io_may_be_stalled)) |
4258 | start_io(h); | |
edd16368 SC |
4259 | } |
4260 | ||
a104c99f SC |
4261 | static inline u32 hpsa_tag_contains_index(u32 tag) |
4262 | { | |
a104c99f SC |
4263 | return tag & DIRECT_LOOKUP_BIT; |
4264 | } | |
4265 | ||
4266 | static inline u32 hpsa_tag_to_index(u32 tag) | |
4267 | { | |
a104c99f SC |
4268 | return tag >> DIRECT_LOOKUP_SHIFT; |
4269 | } | |
4270 | ||
a9a3a273 SC |
4271 | |
4272 | static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag) | |
a104c99f | 4273 | { |
a9a3a273 SC |
4274 | #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1) |
4275 | #define HPSA_SIMPLE_ERROR_BITS 0x03 | |
960a30e7 | 4276 | if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant))) |
a9a3a273 SC |
4277 | return tag & ~HPSA_SIMPLE_ERROR_BITS; |
4278 | return tag & ~HPSA_PERF_ERROR_BITS; | |
a104c99f SC |
4279 | } |
4280 | ||
303932fd | 4281 | /* process completion of an indexed ("direct lookup") command */ |
1d94f94d | 4282 | static inline void process_indexed_cmd(struct ctlr_info *h, |
303932fd DB |
4283 | u32 raw_tag) |
4284 | { | |
4285 | u32 tag_index; | |
4286 | struct CommandList *c; | |
4287 | ||
4288 | tag_index = hpsa_tag_to_index(raw_tag); | |
1d94f94d SC |
4289 | if (!bad_tag(h, tag_index, raw_tag)) { |
4290 | c = h->cmd_pool + tag_index; | |
4291 | finish_cmd(c); | |
4292 | } | |
303932fd DB |
4293 | } |
4294 | ||
4295 | /* process completion of a non-indexed command */ | |
1d94f94d | 4296 | static inline void process_nonindexed_cmd(struct ctlr_info *h, |
303932fd DB |
4297 | u32 raw_tag) |
4298 | { | |
4299 | u32 tag; | |
4300 | struct CommandList *c = NULL; | |
e16a33ad | 4301 | unsigned long flags; |
303932fd | 4302 | |
a9a3a273 | 4303 | tag = hpsa_tag_discard_error_bits(h, raw_tag); |
e16a33ad | 4304 | spin_lock_irqsave(&h->lock, flags); |
9e0fc764 | 4305 | list_for_each_entry(c, &h->cmpQ, list) { |
303932fd | 4306 | if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) { |
e16a33ad | 4307 | spin_unlock_irqrestore(&h->lock, flags); |
5a3d16f5 | 4308 | finish_cmd(c); |
1d94f94d | 4309 | return; |
303932fd DB |
4310 | } |
4311 | } | |
e16a33ad | 4312 | spin_unlock_irqrestore(&h->lock, flags); |
303932fd | 4313 | bad_tag(h, h->nr_cmds + 1, raw_tag); |
303932fd DB |
4314 | } |
4315 | ||
64670ac8 SC |
4316 | /* Some controllers, like p400, will give us one interrupt |
4317 | * after a soft reset, even if we turned interrupts off. | |
4318 | * Only need to check for this in the hpsa_xxx_discard_completions | |
4319 | * functions. | |
4320 | */ | |
4321 | static int ignore_bogus_interrupt(struct ctlr_info *h) | |
4322 | { | |
4323 | if (likely(!reset_devices)) | |
4324 | return 0; | |
4325 | ||
4326 | if (likely(h->interrupts_enabled)) | |
4327 | return 0; | |
4328 | ||
4329 | dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled " | |
4330 | "(known firmware bug.) Ignoring.\n"); | |
4331 | ||
4332 | return 1; | |
4333 | } | |
4334 | ||
254f796b MG |
4335 | /* |
4336 | * Convert &h->q[x] (passed to interrupt handlers) back to h. | |
4337 | * Relies on (h-q[x] == x) being true for x such that | |
4338 | * 0 <= x < MAX_REPLY_QUEUES. | |
4339 | */ | |
4340 | static struct ctlr_info *queue_to_hba(u8 *queue) | |
64670ac8 | 4341 | { |
254f796b MG |
4342 | return container_of((queue - *queue), struct ctlr_info, q[0]); |
4343 | } | |
4344 | ||
4345 | static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue) | |
4346 | { | |
4347 | struct ctlr_info *h = queue_to_hba(queue); | |
4348 | u8 q = *(u8 *) queue; | |
64670ac8 SC |
4349 | u32 raw_tag; |
4350 | ||
4351 | if (ignore_bogus_interrupt(h)) | |
4352 | return IRQ_NONE; | |
4353 | ||
4354 | if (interrupt_not_for_us(h)) | |
4355 | return IRQ_NONE; | |
a0c12413 | 4356 | h->last_intr_timestamp = get_jiffies_64(); |
64670ac8 | 4357 | while (interrupt_pending(h)) { |
254f796b | 4358 | raw_tag = get_next_completion(h, q); |
64670ac8 | 4359 | while (raw_tag != FIFO_EMPTY) |
254f796b | 4360 | raw_tag = next_command(h, q); |
64670ac8 | 4361 | } |
64670ac8 SC |
4362 | return IRQ_HANDLED; |
4363 | } | |
4364 | ||
254f796b | 4365 | static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue) |
64670ac8 | 4366 | { |
254f796b | 4367 | struct ctlr_info *h = queue_to_hba(queue); |
64670ac8 | 4368 | u32 raw_tag; |
254f796b | 4369 | u8 q = *(u8 *) queue; |
64670ac8 SC |
4370 | |
4371 | if (ignore_bogus_interrupt(h)) | |
4372 | return IRQ_NONE; | |
4373 | ||
a0c12413 | 4374 | h->last_intr_timestamp = get_jiffies_64(); |
254f796b | 4375 | raw_tag = get_next_completion(h, q); |
64670ac8 | 4376 | while (raw_tag != FIFO_EMPTY) |
254f796b | 4377 | raw_tag = next_command(h, q); |
64670ac8 SC |
4378 | return IRQ_HANDLED; |
4379 | } | |
4380 | ||
254f796b | 4381 | static irqreturn_t do_hpsa_intr_intx(int irq, void *queue) |
edd16368 | 4382 | { |
254f796b | 4383 | struct ctlr_info *h = queue_to_hba((u8 *) queue); |
303932fd | 4384 | u32 raw_tag; |
254f796b | 4385 | u8 q = *(u8 *) queue; |
edd16368 SC |
4386 | |
4387 | if (interrupt_not_for_us(h)) | |
4388 | return IRQ_NONE; | |
a0c12413 | 4389 | h->last_intr_timestamp = get_jiffies_64(); |
10f66018 | 4390 | while (interrupt_pending(h)) { |
254f796b | 4391 | raw_tag = get_next_completion(h, q); |
10f66018 | 4392 | while (raw_tag != FIFO_EMPTY) { |
1d94f94d SC |
4393 | if (likely(hpsa_tag_contains_index(raw_tag))) |
4394 | process_indexed_cmd(h, raw_tag); | |
10f66018 | 4395 | else |
1d94f94d | 4396 | process_nonindexed_cmd(h, raw_tag); |
254f796b | 4397 | raw_tag = next_command(h, q); |
10f66018 SC |
4398 | } |
4399 | } | |
10f66018 SC |
4400 | return IRQ_HANDLED; |
4401 | } | |
4402 | ||
254f796b | 4403 | static irqreturn_t do_hpsa_intr_msi(int irq, void *queue) |
10f66018 | 4404 | { |
254f796b | 4405 | struct ctlr_info *h = queue_to_hba(queue); |
10f66018 | 4406 | u32 raw_tag; |
254f796b | 4407 | u8 q = *(u8 *) queue; |
10f66018 | 4408 | |
a0c12413 | 4409 | h->last_intr_timestamp = get_jiffies_64(); |
254f796b | 4410 | raw_tag = get_next_completion(h, q); |
303932fd | 4411 | while (raw_tag != FIFO_EMPTY) { |
1d94f94d SC |
4412 | if (likely(hpsa_tag_contains_index(raw_tag))) |
4413 | process_indexed_cmd(h, raw_tag); | |
303932fd | 4414 | else |
1d94f94d | 4415 | process_nonindexed_cmd(h, raw_tag); |
254f796b | 4416 | raw_tag = next_command(h, q); |
edd16368 | 4417 | } |
edd16368 SC |
4418 | return IRQ_HANDLED; |
4419 | } | |
4420 | ||
a9a3a273 SC |
4421 | /* Send a message CDB to the firmware. Careful, this only works |
4422 | * in simple mode, not performant mode due to the tag lookup. | |
4423 | * We only ever use this immediately after a controller reset. | |
4424 | */ | |
6f039790 GKH |
4425 | static int hpsa_message(struct pci_dev *pdev, unsigned char opcode, |
4426 | unsigned char type) | |
edd16368 SC |
4427 | { |
4428 | struct Command { | |
4429 | struct CommandListHeader CommandHeader; | |
4430 | struct RequestBlock Request; | |
4431 | struct ErrDescriptor ErrorDescriptor; | |
4432 | }; | |
4433 | struct Command *cmd; | |
4434 | static const size_t cmd_sz = sizeof(*cmd) + | |
4435 | sizeof(cmd->ErrorDescriptor); | |
4436 | dma_addr_t paddr64; | |
4437 | uint32_t paddr32, tag; | |
4438 | void __iomem *vaddr; | |
4439 | int i, err; | |
4440 | ||
4441 | vaddr = pci_ioremap_bar(pdev, 0); | |
4442 | if (vaddr == NULL) | |
4443 | return -ENOMEM; | |
4444 | ||
4445 | /* The Inbound Post Queue only accepts 32-bit physical addresses for the | |
4446 | * CCISS commands, so they must be allocated from the lower 4GiB of | |
4447 | * memory. | |
4448 | */ | |
4449 | err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); | |
4450 | if (err) { | |
4451 | iounmap(vaddr); | |
4452 | return -ENOMEM; | |
4453 | } | |
4454 | ||
4455 | cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64); | |
4456 | if (cmd == NULL) { | |
4457 | iounmap(vaddr); | |
4458 | return -ENOMEM; | |
4459 | } | |
4460 | ||
4461 | /* This must fit, because of the 32-bit consistent DMA mask. Also, | |
4462 | * although there's no guarantee, we assume that the address is at | |
4463 | * least 4-byte aligned (most likely, it's page-aligned). | |
4464 | */ | |
4465 | paddr32 = paddr64; | |
4466 | ||
4467 | cmd->CommandHeader.ReplyQueue = 0; | |
4468 | cmd->CommandHeader.SGList = 0; | |
4469 | cmd->CommandHeader.SGTotal = 0; | |
4470 | cmd->CommandHeader.Tag.lower = paddr32; | |
4471 | cmd->CommandHeader.Tag.upper = 0; | |
4472 | memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8); | |
4473 | ||
4474 | cmd->Request.CDBLen = 16; | |
4475 | cmd->Request.Type.Type = TYPE_MSG; | |
4476 | cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE; | |
4477 | cmd->Request.Type.Direction = XFER_NONE; | |
4478 | cmd->Request.Timeout = 0; /* Don't time out */ | |
4479 | cmd->Request.CDB[0] = opcode; | |
4480 | cmd->Request.CDB[1] = type; | |
4481 | memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */ | |
4482 | cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd); | |
4483 | cmd->ErrorDescriptor.Addr.upper = 0; | |
4484 | cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo); | |
4485 | ||
4486 | writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET); | |
4487 | ||
4488 | for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) { | |
4489 | tag = readl(vaddr + SA5_REPLY_PORT_OFFSET); | |
a9a3a273 | 4490 | if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32) |
edd16368 SC |
4491 | break; |
4492 | msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS); | |
4493 | } | |
4494 | ||
4495 | iounmap(vaddr); | |
4496 | ||
4497 | /* we leak the DMA buffer here ... no choice since the controller could | |
4498 | * still complete the command. | |
4499 | */ | |
4500 | if (i == HPSA_MSG_SEND_RETRY_LIMIT) { | |
4501 | dev_err(&pdev->dev, "controller message %02x:%02x timed out\n", | |
4502 | opcode, type); | |
4503 | return -ETIMEDOUT; | |
4504 | } | |
4505 | ||
4506 | pci_free_consistent(pdev, cmd_sz, cmd, paddr64); | |
4507 | ||
4508 | if (tag & HPSA_ERROR_BIT) { | |
4509 | dev_err(&pdev->dev, "controller message %02x:%02x failed\n", | |
4510 | opcode, type); | |
4511 | return -EIO; | |
4512 | } | |
4513 | ||
4514 | dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n", | |
4515 | opcode, type); | |
4516 | return 0; | |
4517 | } | |
4518 | ||
edd16368 SC |
4519 | #define hpsa_noop(p) hpsa_message(p, 3, 0) |
4520 | ||
1df8552a | 4521 | static int hpsa_controller_hard_reset(struct pci_dev *pdev, |
cf0b08d0 | 4522 | void * __iomem vaddr, u32 use_doorbell) |
1df8552a SC |
4523 | { |
4524 | u16 pmcsr; | |
4525 | int pos; | |
4526 | ||
4527 | if (use_doorbell) { | |
4528 | /* For everything after the P600, the PCI power state method | |
4529 | * of resetting the controller doesn't work, so we have this | |
4530 | * other way using the doorbell register. | |
4531 | */ | |
4532 | dev_info(&pdev->dev, "using doorbell to reset controller\n"); | |
cf0b08d0 | 4533 | writel(use_doorbell, vaddr + SA5_DOORBELL); |
85009239 SC |
4534 | |
4535 | /* PMC hardware guys tell us we need a 5 second delay after | |
4536 | * doorbell reset and before any attempt to talk to the board | |
4537 | * at all to ensure that this actually works and doesn't fall | |
4538 | * over in some weird corner cases. | |
4539 | */ | |
4540 | msleep(5000); | |
1df8552a SC |
4541 | } else { /* Try to do it the PCI power state way */ |
4542 | ||
4543 | /* Quoting from the Open CISS Specification: "The Power | |
4544 | * Management Control/Status Register (CSR) controls the power | |
4545 | * state of the device. The normal operating state is D0, | |
4546 | * CSR=00h. The software off state is D3, CSR=03h. To reset | |
4547 | * the controller, place the interface device in D3 then to D0, | |
4548 | * this causes a secondary PCI reset which will reset the | |
4549 | * controller." */ | |
4550 | ||
4551 | pos = pci_find_capability(pdev, PCI_CAP_ID_PM); | |
4552 | if (pos == 0) { | |
4553 | dev_err(&pdev->dev, | |
4554 | "hpsa_reset_controller: " | |
4555 | "PCI PM not supported\n"); | |
4556 | return -ENODEV; | |
4557 | } | |
4558 | dev_info(&pdev->dev, "using PCI PM to reset controller\n"); | |
4559 | /* enter the D3hot power management state */ | |
4560 | pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr); | |
4561 | pmcsr &= ~PCI_PM_CTRL_STATE_MASK; | |
4562 | pmcsr |= PCI_D3hot; | |
4563 | pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr); | |
4564 | ||
4565 | msleep(500); | |
4566 | ||
4567 | /* enter the D0 power management state */ | |
4568 | pmcsr &= ~PCI_PM_CTRL_STATE_MASK; | |
4569 | pmcsr |= PCI_D0; | |
4570 | pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr); | |
c4853efe MM |
4571 | |
4572 | /* | |
4573 | * The P600 requires a small delay when changing states. | |
4574 | * Otherwise we may think the board did not reset and we bail. | |
4575 | * This for kdump only and is particular to the P600. | |
4576 | */ | |
4577 | msleep(500); | |
1df8552a SC |
4578 | } |
4579 | return 0; | |
4580 | } | |
4581 | ||
6f039790 | 4582 | static void init_driver_version(char *driver_version, int len) |
580ada3c SC |
4583 | { |
4584 | memset(driver_version, 0, len); | |
f79cfec6 | 4585 | strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1); |
580ada3c SC |
4586 | } |
4587 | ||
6f039790 | 4588 | static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable) |
580ada3c SC |
4589 | { |
4590 | char *driver_version; | |
4591 | int i, size = sizeof(cfgtable->driver_version); | |
4592 | ||
4593 | driver_version = kmalloc(size, GFP_KERNEL); | |
4594 | if (!driver_version) | |
4595 | return -ENOMEM; | |
4596 | ||
4597 | init_driver_version(driver_version, size); | |
4598 | for (i = 0; i < size; i++) | |
4599 | writeb(driver_version[i], &cfgtable->driver_version[i]); | |
4600 | kfree(driver_version); | |
4601 | return 0; | |
4602 | } | |
4603 | ||
6f039790 GKH |
4604 | static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable, |
4605 | unsigned char *driver_ver) | |
580ada3c SC |
4606 | { |
4607 | int i; | |
4608 | ||
4609 | for (i = 0; i < sizeof(cfgtable->driver_version); i++) | |
4610 | driver_ver[i] = readb(&cfgtable->driver_version[i]); | |
4611 | } | |
4612 | ||
6f039790 | 4613 | static int controller_reset_failed(struct CfgTable __iomem *cfgtable) |
580ada3c SC |
4614 | { |
4615 | ||
4616 | char *driver_ver, *old_driver_ver; | |
4617 | int rc, size = sizeof(cfgtable->driver_version); | |
4618 | ||
4619 | old_driver_ver = kmalloc(2 * size, GFP_KERNEL); | |
4620 | if (!old_driver_ver) | |
4621 | return -ENOMEM; | |
4622 | driver_ver = old_driver_ver + size; | |
4623 | ||
4624 | /* After a reset, the 32 bytes of "driver version" in the cfgtable | |
4625 | * should have been changed, otherwise we know the reset failed. | |
4626 | */ | |
4627 | init_driver_version(old_driver_ver, size); | |
4628 | read_driver_ver_from_cfgtable(cfgtable, driver_ver); | |
4629 | rc = !memcmp(driver_ver, old_driver_ver, size); | |
4630 | kfree(old_driver_ver); | |
4631 | return rc; | |
4632 | } | |
edd16368 | 4633 | /* This does a hard reset of the controller using PCI power management |
1df8552a | 4634 | * states or the using the doorbell register. |
edd16368 | 4635 | */ |
6f039790 | 4636 | static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev) |
edd16368 | 4637 | { |
1df8552a SC |
4638 | u64 cfg_offset; |
4639 | u32 cfg_base_addr; | |
4640 | u64 cfg_base_addr_index; | |
4641 | void __iomem *vaddr; | |
4642 | unsigned long paddr; | |
580ada3c | 4643 | u32 misc_fw_support; |
270d05de | 4644 | int rc; |
1df8552a | 4645 | struct CfgTable __iomem *cfgtable; |
cf0b08d0 | 4646 | u32 use_doorbell; |
18867659 | 4647 | u32 board_id; |
270d05de | 4648 | u16 command_register; |
edd16368 | 4649 | |
1df8552a SC |
4650 | /* For controllers as old as the P600, this is very nearly |
4651 | * the same thing as | |
edd16368 SC |
4652 | * |
4653 | * pci_save_state(pci_dev); | |
4654 | * pci_set_power_state(pci_dev, PCI_D3hot); | |
4655 | * pci_set_power_state(pci_dev, PCI_D0); | |
4656 | * pci_restore_state(pci_dev); | |
4657 | * | |
1df8552a SC |
4658 | * For controllers newer than the P600, the pci power state |
4659 | * method of resetting doesn't work so we have another way | |
4660 | * using the doorbell register. | |
edd16368 | 4661 | */ |
18867659 | 4662 | |
25c1e56a | 4663 | rc = hpsa_lookup_board_id(pdev, &board_id); |
46380786 | 4664 | if (rc < 0 || !ctlr_is_resettable(board_id)) { |
25c1e56a SC |
4665 | dev_warn(&pdev->dev, "Not resetting device.\n"); |
4666 | return -ENODEV; | |
4667 | } | |
46380786 SC |
4668 | |
4669 | /* if controller is soft- but not hard resettable... */ | |
4670 | if (!ctlr_is_hard_resettable(board_id)) | |
4671 | return -ENOTSUPP; /* try soft reset later. */ | |
18867659 | 4672 | |
270d05de SC |
4673 | /* Save the PCI command register */ |
4674 | pci_read_config_word(pdev, 4, &command_register); | |
4675 | /* Turn the board off. This is so that later pci_restore_state() | |
4676 | * won't turn the board on before the rest of config space is ready. | |
4677 | */ | |
4678 | pci_disable_device(pdev); | |
4679 | pci_save_state(pdev); | |
edd16368 | 4680 | |
1df8552a SC |
4681 | /* find the first memory BAR, so we can find the cfg table */ |
4682 | rc = hpsa_pci_find_memory_BAR(pdev, &paddr); | |
4683 | if (rc) | |
4684 | return rc; | |
4685 | vaddr = remap_pci_mem(paddr, 0x250); | |
4686 | if (!vaddr) | |
4687 | return -ENOMEM; | |
edd16368 | 4688 | |
1df8552a SC |
4689 | /* find cfgtable in order to check if reset via doorbell is supported */ |
4690 | rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr, | |
4691 | &cfg_base_addr_index, &cfg_offset); | |
4692 | if (rc) | |
4693 | goto unmap_vaddr; | |
4694 | cfgtable = remap_pci_mem(pci_resource_start(pdev, | |
4695 | cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable)); | |
4696 | if (!cfgtable) { | |
4697 | rc = -ENOMEM; | |
4698 | goto unmap_vaddr; | |
4699 | } | |
580ada3c SC |
4700 | rc = write_driver_ver_to_cfgtable(cfgtable); |
4701 | if (rc) | |
4702 | goto unmap_vaddr; | |
edd16368 | 4703 | |
cf0b08d0 SC |
4704 | /* If reset via doorbell register is supported, use that. |
4705 | * There are two such methods. Favor the newest method. | |
4706 | */ | |
1df8552a | 4707 | misc_fw_support = readl(&cfgtable->misc_fw_support); |
cf0b08d0 SC |
4708 | use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2; |
4709 | if (use_doorbell) { | |
4710 | use_doorbell = DOORBELL_CTLR_RESET2; | |
4711 | } else { | |
4712 | use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET; | |
4713 | if (use_doorbell) { | |
fba63097 MM |
4714 | dev_warn(&pdev->dev, "Soft reset not supported. " |
4715 | "Firmware update is required.\n"); | |
64670ac8 | 4716 | rc = -ENOTSUPP; /* try soft reset */ |
cf0b08d0 SC |
4717 | goto unmap_cfgtable; |
4718 | } | |
4719 | } | |
edd16368 | 4720 | |
1df8552a SC |
4721 | rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell); |
4722 | if (rc) | |
4723 | goto unmap_cfgtable; | |
edd16368 | 4724 | |
270d05de SC |
4725 | pci_restore_state(pdev); |
4726 | rc = pci_enable_device(pdev); | |
4727 | if (rc) { | |
4728 | dev_warn(&pdev->dev, "failed to enable device.\n"); | |
4729 | goto unmap_cfgtable; | |
edd16368 | 4730 | } |
270d05de | 4731 | pci_write_config_word(pdev, 4, command_register); |
edd16368 | 4732 | |
1df8552a SC |
4733 | /* Some devices (notably the HP Smart Array 5i Controller) |
4734 | need a little pause here */ | |
4735 | msleep(HPSA_POST_RESET_PAUSE_MSECS); | |
4736 | ||
fe5389c8 SC |
4737 | rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY); |
4738 | if (rc) { | |
4739 | dev_warn(&pdev->dev, | |
64670ac8 SC |
4740 | "failed waiting for board to become ready " |
4741 | "after hard reset\n"); | |
fe5389c8 SC |
4742 | goto unmap_cfgtable; |
4743 | } | |
fe5389c8 | 4744 | |
580ada3c SC |
4745 | rc = controller_reset_failed(vaddr); |
4746 | if (rc < 0) | |
4747 | goto unmap_cfgtable; | |
4748 | if (rc) { | |
64670ac8 SC |
4749 | dev_warn(&pdev->dev, "Unable to successfully reset " |
4750 | "controller. Will try soft reset.\n"); | |
4751 | rc = -ENOTSUPP; | |
580ada3c | 4752 | } else { |
64670ac8 | 4753 | dev_info(&pdev->dev, "board ready after hard reset.\n"); |
1df8552a SC |
4754 | } |
4755 | ||
4756 | unmap_cfgtable: | |
4757 | iounmap(cfgtable); | |
4758 | ||
4759 | unmap_vaddr: | |
4760 | iounmap(vaddr); | |
4761 | return rc; | |
edd16368 SC |
4762 | } |
4763 | ||
4764 | /* | |
4765 | * We cannot read the structure directly, for portability we must use | |
4766 | * the io functions. | |
4767 | * This is for debug only. | |
4768 | */ | |
edd16368 SC |
4769 | static void print_cfg_table(struct device *dev, struct CfgTable *tb) |
4770 | { | |
58f8665c | 4771 | #ifdef HPSA_DEBUG |
edd16368 SC |
4772 | int i; |
4773 | char temp_name[17]; | |
4774 | ||
4775 | dev_info(dev, "Controller Configuration information\n"); | |
4776 | dev_info(dev, "------------------------------------\n"); | |
4777 | for (i = 0; i < 4; i++) | |
4778 | temp_name[i] = readb(&(tb->Signature[i])); | |
4779 | temp_name[4] = '\0'; | |
4780 | dev_info(dev, " Signature = %s\n", temp_name); | |
4781 | dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence))); | |
4782 | dev_info(dev, " Transport methods supported = 0x%x\n", | |
4783 | readl(&(tb->TransportSupport))); | |
4784 | dev_info(dev, " Transport methods active = 0x%x\n", | |
4785 | readl(&(tb->TransportActive))); | |
4786 | dev_info(dev, " Requested transport Method = 0x%x\n", | |
4787 | readl(&(tb->HostWrite.TransportRequest))); | |
4788 | dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n", | |
4789 | readl(&(tb->HostWrite.CoalIntDelay))); | |
4790 | dev_info(dev, " Coalesce Interrupt Count = 0x%x\n", | |
4791 | readl(&(tb->HostWrite.CoalIntCount))); | |
4792 | dev_info(dev, " Max outstanding commands = 0x%d\n", | |
4793 | readl(&(tb->CmdsOutMax))); | |
4794 | dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes))); | |
4795 | for (i = 0; i < 16; i++) | |
4796 | temp_name[i] = readb(&(tb->ServerName[i])); | |
4797 | temp_name[16] = '\0'; | |
4798 | dev_info(dev, " Server Name = %s\n", temp_name); | |
4799 | dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n", | |
4800 | readl(&(tb->HeartBeat))); | |
edd16368 | 4801 | #endif /* HPSA_DEBUG */ |
58f8665c | 4802 | } |
edd16368 SC |
4803 | |
4804 | static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr) | |
4805 | { | |
4806 | int i, offset, mem_type, bar_type; | |
4807 | ||
4808 | if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */ | |
4809 | return 0; | |
4810 | offset = 0; | |
4811 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { | |
4812 | bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE; | |
4813 | if (bar_type == PCI_BASE_ADDRESS_SPACE_IO) | |
4814 | offset += 4; | |
4815 | else { | |
4816 | mem_type = pci_resource_flags(pdev, i) & | |
4817 | PCI_BASE_ADDRESS_MEM_TYPE_MASK; | |
4818 | switch (mem_type) { | |
4819 | case PCI_BASE_ADDRESS_MEM_TYPE_32: | |
4820 | case PCI_BASE_ADDRESS_MEM_TYPE_1M: | |
4821 | offset += 4; /* 32 bit */ | |
4822 | break; | |
4823 | case PCI_BASE_ADDRESS_MEM_TYPE_64: | |
4824 | offset += 8; | |
4825 | break; | |
4826 | default: /* reserved in PCI 2.2 */ | |
4827 | dev_warn(&pdev->dev, | |
4828 | "base address is invalid\n"); | |
4829 | return -1; | |
4830 | break; | |
4831 | } | |
4832 | } | |
4833 | if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0) | |
4834 | return i + 1; | |
4835 | } | |
4836 | return -1; | |
4837 | } | |
4838 | ||
4839 | /* If MSI/MSI-X is supported by the kernel we will try to enable it on | |
4840 | * controllers that are capable. If not, we use IO-APIC mode. | |
4841 | */ | |
4842 | ||
6f039790 | 4843 | static void hpsa_interrupt_mode(struct ctlr_info *h) |
edd16368 SC |
4844 | { |
4845 | #ifdef CONFIG_PCI_MSI | |
254f796b MG |
4846 | int err, i; |
4847 | struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES]; | |
4848 | ||
4849 | for (i = 0; i < MAX_REPLY_QUEUES; i++) { | |
4850 | hpsa_msix_entries[i].vector = 0; | |
4851 | hpsa_msix_entries[i].entry = i; | |
4852 | } | |
edd16368 SC |
4853 | |
4854 | /* Some boards advertise MSI but don't really support it */ | |
6b3f4c52 SC |
4855 | if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) || |
4856 | (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11)) | |
edd16368 | 4857 | goto default_int_mode; |
55c06c71 SC |
4858 | if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) { |
4859 | dev_info(&h->pdev->dev, "MSIX\n"); | |
eee0f03a | 4860 | h->msix_vector = MAX_REPLY_QUEUES; |
254f796b | 4861 | err = pci_enable_msix(h->pdev, hpsa_msix_entries, |
eee0f03a | 4862 | h->msix_vector); |
edd16368 | 4863 | if (err > 0) { |
55c06c71 | 4864 | dev_warn(&h->pdev->dev, "only %d MSI-X vectors " |
edd16368 | 4865 | "available\n", err); |
eee0f03a HR |
4866 | h->msix_vector = err; |
4867 | err = pci_enable_msix(h->pdev, hpsa_msix_entries, | |
4868 | h->msix_vector); | |
4869 | } | |
4870 | if (!err) { | |
4871 | for (i = 0; i < h->msix_vector; i++) | |
4872 | h->intr[i] = hpsa_msix_entries[i].vector; | |
4873 | return; | |
edd16368 | 4874 | } else { |
55c06c71 | 4875 | dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", |
edd16368 | 4876 | err); |
eee0f03a | 4877 | h->msix_vector = 0; |
edd16368 SC |
4878 | goto default_int_mode; |
4879 | } | |
4880 | } | |
55c06c71 SC |
4881 | if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) { |
4882 | dev_info(&h->pdev->dev, "MSI\n"); | |
4883 | if (!pci_enable_msi(h->pdev)) | |
edd16368 SC |
4884 | h->msi_vector = 1; |
4885 | else | |
55c06c71 | 4886 | dev_warn(&h->pdev->dev, "MSI init failed\n"); |
edd16368 SC |
4887 | } |
4888 | default_int_mode: | |
4889 | #endif /* CONFIG_PCI_MSI */ | |
4890 | /* if we get here we're going to use the default interrupt mode */ | |
a9a3a273 | 4891 | h->intr[h->intr_mode] = h->pdev->irq; |
edd16368 SC |
4892 | } |
4893 | ||
6f039790 | 4894 | static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id) |
e5c880d1 SC |
4895 | { |
4896 | int i; | |
4897 | u32 subsystem_vendor_id, subsystem_device_id; | |
4898 | ||
4899 | subsystem_vendor_id = pdev->subsystem_vendor; | |
4900 | subsystem_device_id = pdev->subsystem_device; | |
4901 | *board_id = ((subsystem_device_id << 16) & 0xffff0000) | | |
4902 | subsystem_vendor_id; | |
4903 | ||
4904 | for (i = 0; i < ARRAY_SIZE(products); i++) | |
4905 | if (*board_id == products[i].board_id) | |
4906 | return i; | |
4907 | ||
6798cc0a SC |
4908 | if ((subsystem_vendor_id != PCI_VENDOR_ID_HP && |
4909 | subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) || | |
4910 | !hpsa_allow_any) { | |
e5c880d1 SC |
4911 | dev_warn(&pdev->dev, "unrecognized board ID: " |
4912 | "0x%08x, ignoring.\n", *board_id); | |
4913 | return -ENODEV; | |
4914 | } | |
4915 | return ARRAY_SIZE(products) - 1; /* generic unknown smart array */ | |
4916 | } | |
4917 | ||
6f039790 GKH |
4918 | static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev, |
4919 | unsigned long *memory_bar) | |
3a7774ce SC |
4920 | { |
4921 | int i; | |
4922 | ||
4923 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) | |
12d2cd47 | 4924 | if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) { |
3a7774ce | 4925 | /* addressing mode bits already removed */ |
12d2cd47 SC |
4926 | *memory_bar = pci_resource_start(pdev, i); |
4927 | dev_dbg(&pdev->dev, "memory BAR = %lx\n", | |
3a7774ce SC |
4928 | *memory_bar); |
4929 | return 0; | |
4930 | } | |
12d2cd47 | 4931 | dev_warn(&pdev->dev, "no memory BAR found\n"); |
3a7774ce SC |
4932 | return -ENODEV; |
4933 | } | |
4934 | ||
6f039790 GKH |
4935 | static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr, |
4936 | int wait_for_ready) | |
2c4c8c8b | 4937 | { |
fe5389c8 | 4938 | int i, iterations; |
2c4c8c8b | 4939 | u32 scratchpad; |
fe5389c8 SC |
4940 | if (wait_for_ready) |
4941 | iterations = HPSA_BOARD_READY_ITERATIONS; | |
4942 | else | |
4943 | iterations = HPSA_BOARD_NOT_READY_ITERATIONS; | |
2c4c8c8b | 4944 | |
fe5389c8 SC |
4945 | for (i = 0; i < iterations; i++) { |
4946 | scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET); | |
4947 | if (wait_for_ready) { | |
4948 | if (scratchpad == HPSA_FIRMWARE_READY) | |
4949 | return 0; | |
4950 | } else { | |
4951 | if (scratchpad != HPSA_FIRMWARE_READY) | |
4952 | return 0; | |
4953 | } | |
2c4c8c8b SC |
4954 | msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS); |
4955 | } | |
fe5389c8 | 4956 | dev_warn(&pdev->dev, "board not ready, timed out.\n"); |
2c4c8c8b SC |
4957 | return -ENODEV; |
4958 | } | |
4959 | ||
6f039790 GKH |
4960 | static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr, |
4961 | u32 *cfg_base_addr, u64 *cfg_base_addr_index, | |
4962 | u64 *cfg_offset) | |
a51fd47f SC |
4963 | { |
4964 | *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET); | |
4965 | *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET); | |
4966 | *cfg_base_addr &= (u32) 0x0000ffff; | |
4967 | *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr); | |
4968 | if (*cfg_base_addr_index == -1) { | |
4969 | dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n"); | |
4970 | return -ENODEV; | |
4971 | } | |
4972 | return 0; | |
4973 | } | |
4974 | ||
6f039790 | 4975 | static int hpsa_find_cfgtables(struct ctlr_info *h) |
edd16368 | 4976 | { |
01a02ffc SC |
4977 | u64 cfg_offset; |
4978 | u32 cfg_base_addr; | |
4979 | u64 cfg_base_addr_index; | |
303932fd | 4980 | u32 trans_offset; |
a51fd47f | 4981 | int rc; |
77c4495c | 4982 | |
a51fd47f SC |
4983 | rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr, |
4984 | &cfg_base_addr_index, &cfg_offset); | |
4985 | if (rc) | |
4986 | return rc; | |
77c4495c | 4987 | h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev, |
a51fd47f | 4988 | cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable)); |
77c4495c SC |
4989 | if (!h->cfgtable) |
4990 | return -ENOMEM; | |
580ada3c SC |
4991 | rc = write_driver_ver_to_cfgtable(h->cfgtable); |
4992 | if (rc) | |
4993 | return rc; | |
77c4495c | 4994 | /* Find performant mode table. */ |
a51fd47f | 4995 | trans_offset = readl(&h->cfgtable->TransMethodOffset); |
77c4495c SC |
4996 | h->transtable = remap_pci_mem(pci_resource_start(h->pdev, |
4997 | cfg_base_addr_index)+cfg_offset+trans_offset, | |
4998 | sizeof(*h->transtable)); | |
4999 | if (!h->transtable) | |
5000 | return -ENOMEM; | |
5001 | return 0; | |
5002 | } | |
5003 | ||
6f039790 | 5004 | static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h) |
cba3d38b SC |
5005 | { |
5006 | h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands)); | |
72ceeaec SC |
5007 | |
5008 | /* Limit commands in memory limited kdump scenario. */ | |
5009 | if (reset_devices && h->max_commands > 32) | |
5010 | h->max_commands = 32; | |
5011 | ||
cba3d38b SC |
5012 | if (h->max_commands < 16) { |
5013 | dev_warn(&h->pdev->dev, "Controller reports " | |
5014 | "max supported commands of %d, an obvious lie. " | |
5015 | "Using 16. Ensure that firmware is up to date.\n", | |
5016 | h->max_commands); | |
5017 | h->max_commands = 16; | |
5018 | } | |
5019 | } | |
5020 | ||
b93d7536 SC |
5021 | /* Interrogate the hardware for some limits: |
5022 | * max commands, max SG elements without chaining, and with chaining, | |
5023 | * SG chain block size, etc. | |
5024 | */ | |
6f039790 | 5025 | static void hpsa_find_board_params(struct ctlr_info *h) |
b93d7536 | 5026 | { |
cba3d38b | 5027 | hpsa_get_max_perf_mode_cmds(h); |
b93d7536 SC |
5028 | h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */ |
5029 | h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements)); | |
283b4a9b | 5030 | h->fw_support = readl(&(h->cfgtable->misc_fw_support)); |
b93d7536 SC |
5031 | /* |
5032 | * Limit in-command s/g elements to 32 save dma'able memory. | |
5033 | * Howvever spec says if 0, use 31 | |
5034 | */ | |
5035 | h->max_cmd_sg_entries = 31; | |
5036 | if (h->maxsgentries > 512) { | |
5037 | h->max_cmd_sg_entries = 32; | |
5038 | h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1; | |
5039 | h->maxsgentries--; /* save one for chain pointer */ | |
5040 | } else { | |
5041 | h->maxsgentries = 31; /* default to traditional values */ | |
5042 | h->chainsize = 0; | |
5043 | } | |
75167d2c SC |
5044 | |
5045 | /* Find out what task management functions are supported and cache */ | |
5046 | h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags)); | |
b93d7536 SC |
5047 | } |
5048 | ||
76c46e49 SC |
5049 | static inline bool hpsa_CISS_signature_present(struct ctlr_info *h) |
5050 | { | |
0fc9fd40 | 5051 | if (!check_signature(h->cfgtable->Signature, "CISS", 4)) { |
76c46e49 SC |
5052 | dev_warn(&h->pdev->dev, "not a valid CISS config table\n"); |
5053 | return false; | |
5054 | } | |
5055 | return true; | |
5056 | } | |
5057 | ||
97a5e98c | 5058 | static inline void hpsa_set_driver_support_bits(struct ctlr_info *h) |
f7c39101 | 5059 | { |
97a5e98c | 5060 | u32 driver_support; |
f7c39101 | 5061 | |
28e13446 SC |
5062 | #ifdef CONFIG_X86 |
5063 | /* Need to enable prefetch in the SCSI core for 6400 in x86 */ | |
97a5e98c SC |
5064 | driver_support = readl(&(h->cfgtable->driver_support)); |
5065 | driver_support |= ENABLE_SCSI_PREFETCH; | |
f7c39101 | 5066 | #endif |
28e13446 SC |
5067 | driver_support |= ENABLE_UNIT_ATTN; |
5068 | writel(driver_support, &(h->cfgtable->driver_support)); | |
f7c39101 SC |
5069 | } |
5070 | ||
3d0eab67 SC |
5071 | /* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result |
5072 | * in a prefetch beyond physical memory. | |
5073 | */ | |
5074 | static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h) | |
5075 | { | |
5076 | u32 dma_prefetch; | |
5077 | ||
5078 | if (h->board_id != 0x3225103C) | |
5079 | return; | |
5080 | dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG); | |
5081 | dma_prefetch |= 0x8000; | |
5082 | writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG); | |
5083 | } | |
5084 | ||
76438d08 SC |
5085 | static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h) |
5086 | { | |
5087 | int i; | |
5088 | u32 doorbell_value; | |
5089 | unsigned long flags; | |
5090 | /* wait until the clear_event_notify bit 6 is cleared by controller. */ | |
5091 | for (i = 0; i < MAX_CONFIG_WAIT; i++) { | |
5092 | spin_lock_irqsave(&h->lock, flags); | |
5093 | doorbell_value = readl(h->vaddr + SA5_DOORBELL); | |
5094 | spin_unlock_irqrestore(&h->lock, flags); | |
5095 | if (!(doorbell_value & DOORBELL_CLEAR_EVENTS)) | |
5096 | break; | |
5097 | /* delay and try again */ | |
5098 | msleep(20); | |
5099 | } | |
5100 | } | |
5101 | ||
6f039790 | 5102 | static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h) |
eb6b2ae9 SC |
5103 | { |
5104 | int i; | |
6eaf46fd SC |
5105 | u32 doorbell_value; |
5106 | unsigned long flags; | |
eb6b2ae9 SC |
5107 | |
5108 | /* under certain very rare conditions, this can take awhile. | |
5109 | * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right | |
5110 | * as we enter this code.) | |
5111 | */ | |
5112 | for (i = 0; i < MAX_CONFIG_WAIT; i++) { | |
6eaf46fd SC |
5113 | spin_lock_irqsave(&h->lock, flags); |
5114 | doorbell_value = readl(h->vaddr + SA5_DOORBELL); | |
5115 | spin_unlock_irqrestore(&h->lock, flags); | |
382be668 | 5116 | if (!(doorbell_value & CFGTBL_ChangeReq)) |
eb6b2ae9 SC |
5117 | break; |
5118 | /* delay and try again */ | |
60d3f5b0 | 5119 | usleep_range(10000, 20000); |
eb6b2ae9 | 5120 | } |
3f4336f3 SC |
5121 | } |
5122 | ||
6f039790 | 5123 | static int hpsa_enter_simple_mode(struct ctlr_info *h) |
3f4336f3 SC |
5124 | { |
5125 | u32 trans_support; | |
5126 | ||
5127 | trans_support = readl(&(h->cfgtable->TransportSupport)); | |
5128 | if (!(trans_support & SIMPLE_MODE)) | |
5129 | return -ENOTSUPP; | |
5130 | ||
5131 | h->max_commands = readl(&(h->cfgtable->CmdsOutMax)); | |
283b4a9b | 5132 | |
3f4336f3 SC |
5133 | /* Update the field, and then ring the doorbell */ |
5134 | writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest)); | |
5135 | writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL); | |
5136 | hpsa_wait_for_mode_change_ack(h); | |
eb6b2ae9 | 5137 | print_cfg_table(&h->pdev->dev, h->cfgtable); |
283b4a9b SC |
5138 | if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) |
5139 | goto error; | |
960a30e7 | 5140 | h->transMethod = CFGTBL_Trans_Simple; |
eb6b2ae9 | 5141 | return 0; |
283b4a9b SC |
5142 | error: |
5143 | dev_warn(&h->pdev->dev, "unable to get board into simple mode\n"); | |
5144 | return -ENODEV; | |
eb6b2ae9 SC |
5145 | } |
5146 | ||
6f039790 | 5147 | static int hpsa_pci_init(struct ctlr_info *h) |
77c4495c | 5148 | { |
eb6b2ae9 | 5149 | int prod_index, err; |
edd16368 | 5150 | |
e5c880d1 SC |
5151 | prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id); |
5152 | if (prod_index < 0) | |
5153 | return -ENODEV; | |
5154 | h->product_name = products[prod_index].product_name; | |
5155 | h->access = *(products[prod_index].access); | |
edd16368 | 5156 | |
e5a44df8 MG |
5157 | pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S | |
5158 | PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM); | |
5159 | ||
55c06c71 | 5160 | err = pci_enable_device(h->pdev); |
edd16368 | 5161 | if (err) { |
55c06c71 | 5162 | dev_warn(&h->pdev->dev, "unable to enable PCI device\n"); |
edd16368 SC |
5163 | return err; |
5164 | } | |
5165 | ||
5cb460a6 SC |
5166 | /* Enable bus mastering (pci_disable_device may disable this) */ |
5167 | pci_set_master(h->pdev); | |
5168 | ||
f79cfec6 | 5169 | err = pci_request_regions(h->pdev, HPSA); |
edd16368 | 5170 | if (err) { |
55c06c71 SC |
5171 | dev_err(&h->pdev->dev, |
5172 | "cannot obtain PCI resources, aborting\n"); | |
edd16368 SC |
5173 | return err; |
5174 | } | |
6b3f4c52 | 5175 | hpsa_interrupt_mode(h); |
12d2cd47 | 5176 | err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr); |
3a7774ce | 5177 | if (err) |
edd16368 | 5178 | goto err_out_free_res; |
edd16368 | 5179 | h->vaddr = remap_pci_mem(h->paddr, 0x250); |
204892e9 SC |
5180 | if (!h->vaddr) { |
5181 | err = -ENOMEM; | |
5182 | goto err_out_free_res; | |
5183 | } | |
fe5389c8 | 5184 | err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY); |
2c4c8c8b | 5185 | if (err) |
edd16368 | 5186 | goto err_out_free_res; |
77c4495c SC |
5187 | err = hpsa_find_cfgtables(h); |
5188 | if (err) | |
edd16368 | 5189 | goto err_out_free_res; |
b93d7536 | 5190 | hpsa_find_board_params(h); |
edd16368 | 5191 | |
76c46e49 | 5192 | if (!hpsa_CISS_signature_present(h)) { |
edd16368 SC |
5193 | err = -ENODEV; |
5194 | goto err_out_free_res; | |
5195 | } | |
97a5e98c | 5196 | hpsa_set_driver_support_bits(h); |
3d0eab67 | 5197 | hpsa_p600_dma_prefetch_quirk(h); |
eb6b2ae9 SC |
5198 | err = hpsa_enter_simple_mode(h); |
5199 | if (err) | |
edd16368 | 5200 | goto err_out_free_res; |
edd16368 SC |
5201 | return 0; |
5202 | ||
5203 | err_out_free_res: | |
204892e9 SC |
5204 | if (h->transtable) |
5205 | iounmap(h->transtable); | |
5206 | if (h->cfgtable) | |
5207 | iounmap(h->cfgtable); | |
5208 | if (h->vaddr) | |
5209 | iounmap(h->vaddr); | |
f0bd0b68 | 5210 | pci_disable_device(h->pdev); |
55c06c71 | 5211 | pci_release_regions(h->pdev); |
edd16368 SC |
5212 | return err; |
5213 | } | |
5214 | ||
6f039790 | 5215 | static void hpsa_hba_inquiry(struct ctlr_info *h) |
339b2b14 SC |
5216 | { |
5217 | int rc; | |
5218 | ||
5219 | #define HBA_INQUIRY_BYTE_COUNT 64 | |
5220 | h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL); | |
5221 | if (!h->hba_inquiry_data) | |
5222 | return; | |
5223 | rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0, | |
5224 | h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT); | |
5225 | if (rc != 0) { | |
5226 | kfree(h->hba_inquiry_data); | |
5227 | h->hba_inquiry_data = NULL; | |
5228 | } | |
5229 | } | |
5230 | ||
6f039790 | 5231 | static int hpsa_init_reset_devices(struct pci_dev *pdev) |
4c2a8c40 | 5232 | { |
1df8552a | 5233 | int rc, i; |
4c2a8c40 SC |
5234 | |
5235 | if (!reset_devices) | |
5236 | return 0; | |
5237 | ||
1df8552a SC |
5238 | /* Reset the controller with a PCI power-cycle or via doorbell */ |
5239 | rc = hpsa_kdump_hard_reset_controller(pdev); | |
4c2a8c40 | 5240 | |
1df8552a SC |
5241 | /* -ENOTSUPP here means we cannot reset the controller |
5242 | * but it's already (and still) up and running in | |
18867659 SC |
5243 | * "performant mode". Or, it might be 640x, which can't reset |
5244 | * due to concerns about shared bbwc between 6402/6404 pair. | |
1df8552a SC |
5245 | */ |
5246 | if (rc == -ENOTSUPP) | |
64670ac8 | 5247 | return rc; /* just try to do the kdump anyhow. */ |
1df8552a SC |
5248 | if (rc) |
5249 | return -ENODEV; | |
4c2a8c40 SC |
5250 | |
5251 | /* Now try to get the controller to respond to a no-op */ | |
2b870cb3 | 5252 | dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n"); |
4c2a8c40 SC |
5253 | for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) { |
5254 | if (hpsa_noop(pdev) == 0) | |
5255 | break; | |
5256 | else | |
5257 | dev_warn(&pdev->dev, "no-op failed%s\n", | |
5258 | (i < 11 ? "; re-trying" : "")); | |
5259 | } | |
5260 | return 0; | |
5261 | } | |
5262 | ||
6f039790 | 5263 | static int hpsa_allocate_cmd_pool(struct ctlr_info *h) |
2e9d1b36 SC |
5264 | { |
5265 | h->cmd_pool_bits = kzalloc( | |
5266 | DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) * | |
5267 | sizeof(unsigned long), GFP_KERNEL); | |
5268 | h->cmd_pool = pci_alloc_consistent(h->pdev, | |
5269 | h->nr_cmds * sizeof(*h->cmd_pool), | |
5270 | &(h->cmd_pool_dhandle)); | |
5271 | h->errinfo_pool = pci_alloc_consistent(h->pdev, | |
5272 | h->nr_cmds * sizeof(*h->errinfo_pool), | |
5273 | &(h->errinfo_pool_dhandle)); | |
5274 | if ((h->cmd_pool_bits == NULL) | |
5275 | || (h->cmd_pool == NULL) | |
5276 | || (h->errinfo_pool == NULL)) { | |
5277 | dev_err(&h->pdev->dev, "out of memory in %s", __func__); | |
5278 | return -ENOMEM; | |
5279 | } | |
5280 | return 0; | |
5281 | } | |
5282 | ||
5283 | static void hpsa_free_cmd_pool(struct ctlr_info *h) | |
5284 | { | |
5285 | kfree(h->cmd_pool_bits); | |
5286 | if (h->cmd_pool) | |
5287 | pci_free_consistent(h->pdev, | |
5288 | h->nr_cmds * sizeof(struct CommandList), | |
5289 | h->cmd_pool, h->cmd_pool_dhandle); | |
5290 | if (h->errinfo_pool) | |
5291 | pci_free_consistent(h->pdev, | |
5292 | h->nr_cmds * sizeof(struct ErrorInfo), | |
5293 | h->errinfo_pool, | |
5294 | h->errinfo_pool_dhandle); | |
e1f7de0c MG |
5295 | if (h->ioaccel_cmd_pool) |
5296 | pci_free_consistent(h->pdev, | |
5297 | h->nr_cmds * sizeof(struct io_accel1_cmd), | |
5298 | h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle); | |
2e9d1b36 SC |
5299 | } |
5300 | ||
0ae01a32 SC |
5301 | static int hpsa_request_irq(struct ctlr_info *h, |
5302 | irqreturn_t (*msixhandler)(int, void *), | |
5303 | irqreturn_t (*intxhandler)(int, void *)) | |
5304 | { | |
254f796b | 5305 | int rc, i; |
0ae01a32 | 5306 | |
254f796b MG |
5307 | /* |
5308 | * initialize h->q[x] = x so that interrupt handlers know which | |
5309 | * queue to process. | |
5310 | */ | |
5311 | for (i = 0; i < MAX_REPLY_QUEUES; i++) | |
5312 | h->q[i] = (u8) i; | |
5313 | ||
eee0f03a | 5314 | if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) { |
254f796b | 5315 | /* If performant mode and MSI-X, use multiple reply queues */ |
eee0f03a | 5316 | for (i = 0; i < h->msix_vector; i++) |
254f796b MG |
5317 | rc = request_irq(h->intr[i], msixhandler, |
5318 | 0, h->devname, | |
5319 | &h->q[i]); | |
5320 | } else { | |
5321 | /* Use single reply pool */ | |
eee0f03a | 5322 | if (h->msix_vector > 0 || h->msi_vector) { |
254f796b MG |
5323 | rc = request_irq(h->intr[h->intr_mode], |
5324 | msixhandler, 0, h->devname, | |
5325 | &h->q[h->intr_mode]); | |
5326 | } else { | |
5327 | rc = request_irq(h->intr[h->intr_mode], | |
5328 | intxhandler, IRQF_SHARED, h->devname, | |
5329 | &h->q[h->intr_mode]); | |
5330 | } | |
5331 | } | |
0ae01a32 SC |
5332 | if (rc) { |
5333 | dev_err(&h->pdev->dev, "unable to get irq %d for %s\n", | |
5334 | h->intr[h->intr_mode], h->devname); | |
5335 | return -ENODEV; | |
5336 | } | |
5337 | return 0; | |
5338 | } | |
5339 | ||
6f039790 | 5340 | static int hpsa_kdump_soft_reset(struct ctlr_info *h) |
64670ac8 SC |
5341 | { |
5342 | if (hpsa_send_host_reset(h, RAID_CTLR_LUNID, | |
5343 | HPSA_RESET_TYPE_CONTROLLER)) { | |
5344 | dev_warn(&h->pdev->dev, "Resetting array controller failed.\n"); | |
5345 | return -EIO; | |
5346 | } | |
5347 | ||
5348 | dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n"); | |
5349 | if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) { | |
5350 | dev_warn(&h->pdev->dev, "Soft reset had no effect.\n"); | |
5351 | return -1; | |
5352 | } | |
5353 | ||
5354 | dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n"); | |
5355 | if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) { | |
5356 | dev_warn(&h->pdev->dev, "Board failed to become ready " | |
5357 | "after soft reset.\n"); | |
5358 | return -1; | |
5359 | } | |
5360 | ||
5361 | return 0; | |
5362 | } | |
5363 | ||
254f796b MG |
5364 | static void free_irqs(struct ctlr_info *h) |
5365 | { | |
5366 | int i; | |
5367 | ||
5368 | if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) { | |
5369 | /* Single reply queue, only one irq to free */ | |
5370 | i = h->intr_mode; | |
5371 | free_irq(h->intr[i], &h->q[i]); | |
5372 | return; | |
5373 | } | |
5374 | ||
eee0f03a | 5375 | for (i = 0; i < h->msix_vector; i++) |
254f796b MG |
5376 | free_irq(h->intr[i], &h->q[i]); |
5377 | } | |
5378 | ||
0097f0f4 | 5379 | static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h) |
64670ac8 | 5380 | { |
254f796b | 5381 | free_irqs(h); |
64670ac8 | 5382 | #ifdef CONFIG_PCI_MSI |
0097f0f4 SC |
5383 | if (h->msix_vector) { |
5384 | if (h->pdev->msix_enabled) | |
5385 | pci_disable_msix(h->pdev); | |
5386 | } else if (h->msi_vector) { | |
5387 | if (h->pdev->msi_enabled) | |
5388 | pci_disable_msi(h->pdev); | |
5389 | } | |
64670ac8 | 5390 | #endif /* CONFIG_PCI_MSI */ |
0097f0f4 SC |
5391 | } |
5392 | ||
5393 | static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h) | |
5394 | { | |
5395 | hpsa_free_irqs_and_disable_msix(h); | |
64670ac8 SC |
5396 | hpsa_free_sg_chain_blocks(h); |
5397 | hpsa_free_cmd_pool(h); | |
e1f7de0c | 5398 | kfree(h->ioaccel1_blockFetchTable); |
64670ac8 SC |
5399 | kfree(h->blockFetchTable); |
5400 | pci_free_consistent(h->pdev, h->reply_pool_size, | |
5401 | h->reply_pool, h->reply_pool_dhandle); | |
5402 | if (h->vaddr) | |
5403 | iounmap(h->vaddr); | |
5404 | if (h->transtable) | |
5405 | iounmap(h->transtable); | |
5406 | if (h->cfgtable) | |
5407 | iounmap(h->cfgtable); | |
5408 | pci_release_regions(h->pdev); | |
5409 | kfree(h); | |
5410 | } | |
5411 | ||
a0c12413 SC |
5412 | /* Called when controller lockup detected. */ |
5413 | static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list) | |
5414 | { | |
5415 | struct CommandList *c = NULL; | |
5416 | ||
5417 | assert_spin_locked(&h->lock); | |
5418 | /* Mark all outstanding commands as failed and complete them. */ | |
5419 | while (!list_empty(list)) { | |
5420 | c = list_entry(list->next, struct CommandList, list); | |
5421 | c->err_info->CommandStatus = CMD_HARDWARE_ERR; | |
5a3d16f5 | 5422 | finish_cmd(c); |
a0c12413 SC |
5423 | } |
5424 | } | |
5425 | ||
5426 | static void controller_lockup_detected(struct ctlr_info *h) | |
5427 | { | |
5428 | unsigned long flags; | |
5429 | ||
a0c12413 SC |
5430 | h->access.set_intr_mask(h, HPSA_INTR_OFF); |
5431 | spin_lock_irqsave(&h->lock, flags); | |
5432 | h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET); | |
5433 | spin_unlock_irqrestore(&h->lock, flags); | |
5434 | dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n", | |
5435 | h->lockup_detected); | |
5436 | pci_disable_device(h->pdev); | |
5437 | spin_lock_irqsave(&h->lock, flags); | |
5438 | fail_all_cmds_on_list(h, &h->cmpQ); | |
5439 | fail_all_cmds_on_list(h, &h->reqQ); | |
5440 | spin_unlock_irqrestore(&h->lock, flags); | |
5441 | } | |
5442 | ||
a0c12413 SC |
5443 | static void detect_controller_lockup(struct ctlr_info *h) |
5444 | { | |
5445 | u64 now; | |
5446 | u32 heartbeat; | |
5447 | unsigned long flags; | |
5448 | ||
a0c12413 SC |
5449 | now = get_jiffies_64(); |
5450 | /* If we've received an interrupt recently, we're ok. */ | |
5451 | if (time_after64(h->last_intr_timestamp + | |
e85c5974 | 5452 | (h->heartbeat_sample_interval), now)) |
a0c12413 SC |
5453 | return; |
5454 | ||
5455 | /* | |
5456 | * If we've already checked the heartbeat recently, we're ok. | |
5457 | * This could happen if someone sends us a signal. We | |
5458 | * otherwise don't care about signals in this thread. | |
5459 | */ | |
5460 | if (time_after64(h->last_heartbeat_timestamp + | |
e85c5974 | 5461 | (h->heartbeat_sample_interval), now)) |
a0c12413 SC |
5462 | return; |
5463 | ||
5464 | /* If heartbeat has not changed since we last looked, we're not ok. */ | |
5465 | spin_lock_irqsave(&h->lock, flags); | |
5466 | heartbeat = readl(&h->cfgtable->HeartBeat); | |
5467 | spin_unlock_irqrestore(&h->lock, flags); | |
5468 | if (h->last_heartbeat == heartbeat) { | |
5469 | controller_lockup_detected(h); | |
5470 | return; | |
5471 | } | |
5472 | ||
5473 | /* We're ok. */ | |
5474 | h->last_heartbeat = heartbeat; | |
5475 | h->last_heartbeat_timestamp = now; | |
5476 | } | |
5477 | ||
76438d08 SC |
5478 | static int hpsa_kickoff_rescan(struct ctlr_info *h) |
5479 | { | |
5480 | int i; | |
5481 | char *event_type; | |
5482 | ||
5483 | /* Ask the controller to clear the events we're handling. */ | |
5484 | if (h->transMethod & (CFGTBL_Trans_io_accel1) && | |
5485 | (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE || | |
5486 | h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) { | |
5487 | ||
5488 | if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE) | |
5489 | event_type = "state change"; | |
5490 | if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE) | |
5491 | event_type = "configuration change"; | |
5492 | /* Stop sending new RAID offload reqs via the IO accelerator */ | |
5493 | scsi_block_requests(h->scsi_host); | |
5494 | for (i = 0; i < h->ndevices; i++) | |
5495 | h->dev[i]->offload_enabled = 0; | |
5496 | hpsa_drain_commands(h); | |
5497 | /* Set 'accelerator path config change' bit */ | |
5498 | dev_warn(&h->pdev->dev, | |
5499 | "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n", | |
5500 | h->events, event_type); | |
5501 | writel(h->events, &(h->cfgtable->clear_event_notify)); | |
5502 | /* Set the "clear event notify field update" bit 6 */ | |
5503 | writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL); | |
5504 | /* Wait until ctlr clears 'clear event notify field', bit 6 */ | |
5505 | hpsa_wait_for_clear_event_notify_ack(h); | |
5506 | scsi_unblock_requests(h->scsi_host); | |
5507 | } else { | |
5508 | /* Acknowledge controller notification events. */ | |
5509 | writel(h->events, &(h->cfgtable->clear_event_notify)); | |
5510 | writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL); | |
5511 | hpsa_wait_for_clear_event_notify_ack(h); | |
5512 | #if 0 | |
5513 | writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL); | |
5514 | hpsa_wait_for_mode_change_ack(h); | |
5515 | #endif | |
5516 | } | |
5517 | ||
5518 | /* Something in the device list may have changed to trigger | |
5519 | * the event, so do a rescan. | |
5520 | */ | |
5521 | hpsa_scan_start(h->scsi_host); | |
5522 | /* release reference taken on scsi host in check_controller_events */ | |
5523 | scsi_host_put(h->scsi_host); | |
5524 | return 0; | |
5525 | } | |
5526 | ||
5527 | /* Check a register on the controller to see if there are configuration | |
5528 | * changes (added/changed/removed logical drives, etc.) which mean that | |
5529 | * we should rescan the controller for devices. If so, add the controller | |
5530 | * to the list of controllers needing to be rescanned, and gets a | |
5531 | * reference to the associated scsi_host. | |
5532 | */ | |
5533 | static void hpsa_ctlr_needs_rescan(struct ctlr_info *h) | |
5534 | { | |
5535 | if (!(h->fw_support & MISC_FW_EVENT_NOTIFY)) | |
5536 | return; | |
5537 | ||
5538 | h->events = readl(&(h->cfgtable->event_notify)); | |
5539 | if (!h->events) | |
5540 | return; | |
5541 | ||
5542 | /* | |
5543 | * Take a reference on scsi host for the duration of the scan | |
5544 | * Release in hpsa_kickoff_rescan(). No lock needed for scan_list | |
5545 | * as only a single thread accesses this list. | |
5546 | */ | |
5547 | scsi_host_get(h->scsi_host); | |
5548 | hpsa_kickoff_rescan(h); | |
5549 | } | |
5550 | ||
8a98db73 | 5551 | static void hpsa_monitor_ctlr_worker(struct work_struct *work) |
a0c12413 SC |
5552 | { |
5553 | unsigned long flags; | |
8a98db73 SC |
5554 | struct ctlr_info *h = container_of(to_delayed_work(work), |
5555 | struct ctlr_info, monitor_ctlr_work); | |
5556 | detect_controller_lockup(h); | |
5557 | if (h->lockup_detected) | |
5558 | return; | |
76438d08 | 5559 | hpsa_ctlr_needs_rescan(h); |
8a98db73 SC |
5560 | spin_lock_irqsave(&h->lock, flags); |
5561 | if (h->remove_in_progress) { | |
5562 | spin_unlock_irqrestore(&h->lock, flags); | |
a0c12413 SC |
5563 | return; |
5564 | } | |
8a98db73 SC |
5565 | schedule_delayed_work(&h->monitor_ctlr_work, |
5566 | h->heartbeat_sample_interval); | |
5567 | spin_unlock_irqrestore(&h->lock, flags); | |
a0c12413 SC |
5568 | } |
5569 | ||
6f039790 | 5570 | static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) |
edd16368 | 5571 | { |
4c2a8c40 | 5572 | int dac, rc; |
edd16368 | 5573 | struct ctlr_info *h; |
64670ac8 SC |
5574 | int try_soft_reset = 0; |
5575 | unsigned long flags; | |
edd16368 SC |
5576 | |
5577 | if (number_of_controllers == 0) | |
5578 | printk(KERN_INFO DRIVER_NAME "\n"); | |
edd16368 | 5579 | |
4c2a8c40 | 5580 | rc = hpsa_init_reset_devices(pdev); |
64670ac8 SC |
5581 | if (rc) { |
5582 | if (rc != -ENOTSUPP) | |
5583 | return rc; | |
5584 | /* If the reset fails in a particular way (it has no way to do | |
5585 | * a proper hard reset, so returns -ENOTSUPP) we can try to do | |
5586 | * a soft reset once we get the controller configured up to the | |
5587 | * point that it can accept a command. | |
5588 | */ | |
5589 | try_soft_reset = 1; | |
5590 | rc = 0; | |
5591 | } | |
5592 | ||
5593 | reinit_after_soft_reset: | |
edd16368 | 5594 | |
303932fd DB |
5595 | /* Command structures must be aligned on a 32-byte boundary because |
5596 | * the 5 lower bits of the address are used by the hardware. and by | |
5597 | * the driver. See comments in hpsa.h for more info. | |
5598 | */ | |
283b4a9b | 5599 | #define COMMANDLIST_ALIGNMENT 128 |
303932fd | 5600 | BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT); |
edd16368 SC |
5601 | h = kzalloc(sizeof(*h), GFP_KERNEL); |
5602 | if (!h) | |
ecd9aad4 | 5603 | return -ENOMEM; |
edd16368 | 5604 | |
55c06c71 | 5605 | h->pdev = pdev; |
a9a3a273 | 5606 | h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT; |
9e0fc764 SC |
5607 | INIT_LIST_HEAD(&h->cmpQ); |
5608 | INIT_LIST_HEAD(&h->reqQ); | |
6eaf46fd SC |
5609 | spin_lock_init(&h->lock); |
5610 | spin_lock_init(&h->scan_lock); | |
0390f0c0 | 5611 | spin_lock_init(&h->passthru_count_lock); |
55c06c71 | 5612 | rc = hpsa_pci_init(h); |
ecd9aad4 | 5613 | if (rc != 0) |
edd16368 SC |
5614 | goto clean1; |
5615 | ||
f79cfec6 | 5616 | sprintf(h->devname, HPSA "%d", number_of_controllers); |
edd16368 SC |
5617 | h->ctlr = number_of_controllers; |
5618 | number_of_controllers++; | |
edd16368 SC |
5619 | |
5620 | /* configure PCI DMA stuff */ | |
ecd9aad4 SC |
5621 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); |
5622 | if (rc == 0) { | |
edd16368 | 5623 | dac = 1; |
ecd9aad4 SC |
5624 | } else { |
5625 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); | |
5626 | if (rc == 0) { | |
5627 | dac = 0; | |
5628 | } else { | |
5629 | dev_err(&pdev->dev, "no suitable DMA available\n"); | |
5630 | goto clean1; | |
5631 | } | |
edd16368 SC |
5632 | } |
5633 | ||
5634 | /* make sure the board interrupts are off */ | |
5635 | h->access.set_intr_mask(h, HPSA_INTR_OFF); | |
10f66018 | 5636 | |
0ae01a32 | 5637 | if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx)) |
edd16368 | 5638 | goto clean2; |
303932fd DB |
5639 | dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n", |
5640 | h->devname, pdev->device, | |
a9a3a273 | 5641 | h->intr[h->intr_mode], dac ? "" : " not"); |
2e9d1b36 | 5642 | if (hpsa_allocate_cmd_pool(h)) |
edd16368 | 5643 | goto clean4; |
33a2ffce SC |
5644 | if (hpsa_allocate_sg_chain_blocks(h)) |
5645 | goto clean4; | |
a08a8471 SC |
5646 | init_waitqueue_head(&h->scan_wait_queue); |
5647 | h->scan_finished = 1; /* no scan currently in progress */ | |
edd16368 SC |
5648 | |
5649 | pci_set_drvdata(pdev, h); | |
9a41338e SC |
5650 | h->ndevices = 0; |
5651 | h->scsi_host = NULL; | |
5652 | spin_lock_init(&h->devlock); | |
64670ac8 SC |
5653 | hpsa_put_ctlr_into_performant_mode(h); |
5654 | ||
5655 | /* At this point, the controller is ready to take commands. | |
5656 | * Now, if reset_devices and the hard reset didn't work, try | |
5657 | * the soft reset and see if that works. | |
5658 | */ | |
5659 | if (try_soft_reset) { | |
5660 | ||
5661 | /* This is kind of gross. We may or may not get a completion | |
5662 | * from the soft reset command, and if we do, then the value | |
5663 | * from the fifo may or may not be valid. So, we wait 10 secs | |
5664 | * after the reset throwing away any completions we get during | |
5665 | * that time. Unregister the interrupt handler and register | |
5666 | * fake ones to scoop up any residual completions. | |
5667 | */ | |
5668 | spin_lock_irqsave(&h->lock, flags); | |
5669 | h->access.set_intr_mask(h, HPSA_INTR_OFF); | |
5670 | spin_unlock_irqrestore(&h->lock, flags); | |
254f796b | 5671 | free_irqs(h); |
64670ac8 SC |
5672 | rc = hpsa_request_irq(h, hpsa_msix_discard_completions, |
5673 | hpsa_intx_discard_completions); | |
5674 | if (rc) { | |
5675 | dev_warn(&h->pdev->dev, "Failed to request_irq after " | |
5676 | "soft reset.\n"); | |
5677 | goto clean4; | |
5678 | } | |
5679 | ||
5680 | rc = hpsa_kdump_soft_reset(h); | |
5681 | if (rc) | |
5682 | /* Neither hard nor soft reset worked, we're hosed. */ | |
5683 | goto clean4; | |
5684 | ||
5685 | dev_info(&h->pdev->dev, "Board READY.\n"); | |
5686 | dev_info(&h->pdev->dev, | |
5687 | "Waiting for stale completions to drain.\n"); | |
5688 | h->access.set_intr_mask(h, HPSA_INTR_ON); | |
5689 | msleep(10000); | |
5690 | h->access.set_intr_mask(h, HPSA_INTR_OFF); | |
5691 | ||
5692 | rc = controller_reset_failed(h->cfgtable); | |
5693 | if (rc) | |
5694 | dev_info(&h->pdev->dev, | |
5695 | "Soft reset appears to have failed.\n"); | |
5696 | ||
5697 | /* since the controller's reset, we have to go back and re-init | |
5698 | * everything. Easiest to just forget what we've done and do it | |
5699 | * all over again. | |
5700 | */ | |
5701 | hpsa_undo_allocations_after_kdump_soft_reset(h); | |
5702 | try_soft_reset = 0; | |
5703 | if (rc) | |
5704 | /* don't go to clean4, we already unallocated */ | |
5705 | return -ENODEV; | |
5706 | ||
5707 | goto reinit_after_soft_reset; | |
5708 | } | |
edd16368 SC |
5709 | |
5710 | /* Turn the interrupts on so we can service requests */ | |
5711 | h->access.set_intr_mask(h, HPSA_INTR_ON); | |
5712 | ||
339b2b14 | 5713 | hpsa_hba_inquiry(h); |
edd16368 | 5714 | hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */ |
8a98db73 SC |
5715 | |
5716 | /* Monitor the controller for firmware lockups */ | |
5717 | h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL; | |
5718 | INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker); | |
5719 | schedule_delayed_work(&h->monitor_ctlr_work, | |
5720 | h->heartbeat_sample_interval); | |
88bf6d62 | 5721 | return 0; |
edd16368 SC |
5722 | |
5723 | clean4: | |
33a2ffce | 5724 | hpsa_free_sg_chain_blocks(h); |
2e9d1b36 | 5725 | hpsa_free_cmd_pool(h); |
254f796b | 5726 | free_irqs(h); |
edd16368 SC |
5727 | clean2: |
5728 | clean1: | |
edd16368 | 5729 | kfree(h); |
ecd9aad4 | 5730 | return rc; |
edd16368 SC |
5731 | } |
5732 | ||
5733 | static void hpsa_flush_cache(struct ctlr_info *h) | |
5734 | { | |
5735 | char *flush_buf; | |
5736 | struct CommandList *c; | |
702890e3 SC |
5737 | unsigned long flags; |
5738 | ||
5739 | /* Don't bother trying to flush the cache if locked up */ | |
5740 | spin_lock_irqsave(&h->lock, flags); | |
5741 | if (unlikely(h->lockup_detected)) { | |
5742 | spin_unlock_irqrestore(&h->lock, flags); | |
5743 | return; | |
5744 | } | |
5745 | spin_unlock_irqrestore(&h->lock, flags); | |
edd16368 SC |
5746 | |
5747 | flush_buf = kzalloc(4, GFP_KERNEL); | |
5748 | if (!flush_buf) | |
5749 | return; | |
5750 | ||
5751 | c = cmd_special_alloc(h); | |
5752 | if (!c) { | |
5753 | dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); | |
5754 | goto out_of_memory; | |
5755 | } | |
a2dac136 SC |
5756 | if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0, |
5757 | RAID_CTLR_LUNID, TYPE_CMD)) { | |
5758 | goto out; | |
5759 | } | |
edd16368 SC |
5760 | hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE); |
5761 | if (c->err_info->CommandStatus != 0) | |
a2dac136 | 5762 | out: |
edd16368 SC |
5763 | dev_warn(&h->pdev->dev, |
5764 | "error flushing cache on controller\n"); | |
5765 | cmd_special_free(h, c); | |
5766 | out_of_memory: | |
5767 | kfree(flush_buf); | |
5768 | } | |
5769 | ||
5770 | static void hpsa_shutdown(struct pci_dev *pdev) | |
5771 | { | |
5772 | struct ctlr_info *h; | |
5773 | ||
5774 | h = pci_get_drvdata(pdev); | |
5775 | /* Turn board interrupts off and send the flush cache command | |
5776 | * sendcmd will turn off interrupt, and send the flush... | |
5777 | * To write all data in the battery backed cache to disks | |
5778 | */ | |
5779 | hpsa_flush_cache(h); | |
5780 | h->access.set_intr_mask(h, HPSA_INTR_OFF); | |
0097f0f4 | 5781 | hpsa_free_irqs_and_disable_msix(h); |
edd16368 SC |
5782 | } |
5783 | ||
6f039790 | 5784 | static void hpsa_free_device_info(struct ctlr_info *h) |
55e14e76 SC |
5785 | { |
5786 | int i; | |
5787 | ||
5788 | for (i = 0; i < h->ndevices; i++) | |
5789 | kfree(h->dev[i]); | |
5790 | } | |
5791 | ||
6f039790 | 5792 | static void hpsa_remove_one(struct pci_dev *pdev) |
edd16368 SC |
5793 | { |
5794 | struct ctlr_info *h; | |
8a98db73 | 5795 | unsigned long flags; |
edd16368 SC |
5796 | |
5797 | if (pci_get_drvdata(pdev) == NULL) { | |
a0c12413 | 5798 | dev_err(&pdev->dev, "unable to remove device\n"); |
edd16368 SC |
5799 | return; |
5800 | } | |
5801 | h = pci_get_drvdata(pdev); | |
8a98db73 SC |
5802 | |
5803 | /* Get rid of any controller monitoring work items */ | |
5804 | spin_lock_irqsave(&h->lock, flags); | |
5805 | h->remove_in_progress = 1; | |
5806 | cancel_delayed_work(&h->monitor_ctlr_work); | |
5807 | spin_unlock_irqrestore(&h->lock, flags); | |
5808 | ||
edd16368 SC |
5809 | hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */ |
5810 | hpsa_shutdown(pdev); | |
5811 | iounmap(h->vaddr); | |
204892e9 SC |
5812 | iounmap(h->transtable); |
5813 | iounmap(h->cfgtable); | |
55e14e76 | 5814 | hpsa_free_device_info(h); |
33a2ffce | 5815 | hpsa_free_sg_chain_blocks(h); |
edd16368 SC |
5816 | pci_free_consistent(h->pdev, |
5817 | h->nr_cmds * sizeof(struct CommandList), | |
5818 | h->cmd_pool, h->cmd_pool_dhandle); | |
5819 | pci_free_consistent(h->pdev, | |
5820 | h->nr_cmds * sizeof(struct ErrorInfo), | |
5821 | h->errinfo_pool, h->errinfo_pool_dhandle); | |
303932fd DB |
5822 | pci_free_consistent(h->pdev, h->reply_pool_size, |
5823 | h->reply_pool, h->reply_pool_dhandle); | |
edd16368 | 5824 | kfree(h->cmd_pool_bits); |
303932fd | 5825 | kfree(h->blockFetchTable); |
e1f7de0c | 5826 | kfree(h->ioaccel1_blockFetchTable); |
339b2b14 | 5827 | kfree(h->hba_inquiry_data); |
f0bd0b68 | 5828 | pci_disable_device(pdev); |
edd16368 | 5829 | pci_release_regions(pdev); |
edd16368 SC |
5830 | kfree(h); |
5831 | } | |
5832 | ||
5833 | static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev, | |
5834 | __attribute__((unused)) pm_message_t state) | |
5835 | { | |
5836 | return -ENOSYS; | |
5837 | } | |
5838 | ||
5839 | static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev) | |
5840 | { | |
5841 | return -ENOSYS; | |
5842 | } | |
5843 | ||
5844 | static struct pci_driver hpsa_pci_driver = { | |
f79cfec6 | 5845 | .name = HPSA, |
edd16368 | 5846 | .probe = hpsa_init_one, |
6f039790 | 5847 | .remove = hpsa_remove_one, |
edd16368 SC |
5848 | .id_table = hpsa_pci_device_id, /* id_table */ |
5849 | .shutdown = hpsa_shutdown, | |
5850 | .suspend = hpsa_suspend, | |
5851 | .resume = hpsa_resume, | |
5852 | }; | |
5853 | ||
303932fd DB |
5854 | /* Fill in bucket_map[], given nsgs (the max number of |
5855 | * scatter gather elements supported) and bucket[], | |
5856 | * which is an array of 8 integers. The bucket[] array | |
5857 | * contains 8 different DMA transfer sizes (in 16 | |
5858 | * byte increments) which the controller uses to fetch | |
5859 | * commands. This function fills in bucket_map[], which | |
5860 | * maps a given number of scatter gather elements to one of | |
5861 | * the 8 DMA transfer sizes. The point of it is to allow the | |
5862 | * controller to only do as much DMA as needed to fetch the | |
5863 | * command, with the DMA transfer size encoded in the lower | |
5864 | * bits of the command address. | |
5865 | */ | |
5866 | static void calc_bucket_map(int bucket[], int num_buckets, | |
e1f7de0c | 5867 | int nsgs, int min_blocks, int *bucket_map) |
303932fd DB |
5868 | { |
5869 | int i, j, b, size; | |
5870 | ||
303932fd DB |
5871 | /* Note, bucket_map must have nsgs+1 entries. */ |
5872 | for (i = 0; i <= nsgs; i++) { | |
5873 | /* Compute size of a command with i SG entries */ | |
e1f7de0c | 5874 | size = i + min_blocks; |
303932fd DB |
5875 | b = num_buckets; /* Assume the biggest bucket */ |
5876 | /* Find the bucket that is just big enough */ | |
e1f7de0c | 5877 | for (j = 0; j < num_buckets; j++) { |
303932fd DB |
5878 | if (bucket[j] >= size) { |
5879 | b = j; | |
5880 | break; | |
5881 | } | |
5882 | } | |
5883 | /* for a command with i SG entries, use bucket b. */ | |
5884 | bucket_map[i] = b; | |
5885 | } | |
5886 | } | |
5887 | ||
e1f7de0c | 5888 | static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support) |
303932fd | 5889 | { |
6c311b57 SC |
5890 | int i; |
5891 | unsigned long register_value; | |
e1f7de0c MG |
5892 | unsigned long transMethod = CFGTBL_Trans_Performant | |
5893 | (trans_support & CFGTBL_Trans_use_short_tags) | | |
5894 | CFGTBL_Trans_enable_directed_msix | | |
5895 | (trans_support & CFGTBL_Trans_io_accel1); | |
5896 | ||
5897 | struct access_method access = SA5_performant_access; | |
def342bd SC |
5898 | |
5899 | /* This is a bit complicated. There are 8 registers on | |
5900 | * the controller which we write to to tell it 8 different | |
5901 | * sizes of commands which there may be. It's a way of | |
5902 | * reducing the DMA done to fetch each command. Encoded into | |
5903 | * each command's tag are 3 bits which communicate to the controller | |
5904 | * which of the eight sizes that command fits within. The size of | |
5905 | * each command depends on how many scatter gather entries there are. | |
5906 | * Each SG entry requires 16 bytes. The eight registers are programmed | |
5907 | * with the number of 16-byte blocks a command of that size requires. | |
5908 | * The smallest command possible requires 5 such 16 byte blocks. | |
d66ae08b | 5909 | * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte |
def342bd SC |
5910 | * blocks. Note, this only extends to the SG entries contained |
5911 | * within the command block, and does not extend to chained blocks | |
5912 | * of SG elements. bft[] contains the eight values we write to | |
5913 | * the registers. They are not evenly distributed, but have more | |
5914 | * sizes for small commands, and fewer sizes for larger commands. | |
5915 | */ | |
d66ae08b SC |
5916 | int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4}; |
5917 | BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4); | |
303932fd DB |
5918 | /* 5 = 1 s/g entry or 4k |
5919 | * 6 = 2 s/g entry or 8k | |
5920 | * 8 = 4 s/g entry or 16k | |
5921 | * 10 = 6 s/g entry or 24k | |
5922 | */ | |
303932fd | 5923 | |
303932fd DB |
5924 | /* Controller spec: zero out this buffer. */ |
5925 | memset(h->reply_pool, 0, h->reply_pool_size); | |
303932fd | 5926 | |
d66ae08b SC |
5927 | bft[7] = SG_ENTRIES_IN_CMD + 4; |
5928 | calc_bucket_map(bft, ARRAY_SIZE(bft), | |
e1f7de0c | 5929 | SG_ENTRIES_IN_CMD, 4, h->blockFetchTable); |
303932fd DB |
5930 | for (i = 0; i < 8; i++) |
5931 | writel(bft[i], &h->transtable->BlockFetch[i]); | |
5932 | ||
5933 | /* size of controller ring buffer */ | |
5934 | writel(h->max_commands, &h->transtable->RepQSize); | |
254f796b | 5935 | writel(h->nreply_queues, &h->transtable->RepQCount); |
303932fd DB |
5936 | writel(0, &h->transtable->RepQCtrAddrLow32); |
5937 | writel(0, &h->transtable->RepQCtrAddrHigh32); | |
254f796b MG |
5938 | |
5939 | for (i = 0; i < h->nreply_queues; i++) { | |
5940 | writel(0, &h->transtable->RepQAddr[i].upper); | |
5941 | writel(h->reply_pool_dhandle + | |
5942 | (h->max_commands * sizeof(u64) * i), | |
5943 | &h->transtable->RepQAddr[i].lower); | |
5944 | } | |
5945 | ||
e1f7de0c MG |
5946 | writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest)); |
5947 | /* | |
5948 | * enable outbound interrupt coalescing in accelerator mode; | |
5949 | */ | |
5950 | if (trans_support & CFGTBL_Trans_io_accel1) { | |
5951 | access = SA5_ioaccel_mode1_access; | |
5952 | writel(10, &h->cfgtable->HostWrite.CoalIntDelay); | |
5953 | writel(4, &h->cfgtable->HostWrite.CoalIntCount); | |
5954 | } | |
303932fd | 5955 | writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL); |
3f4336f3 | 5956 | hpsa_wait_for_mode_change_ack(h); |
303932fd DB |
5957 | register_value = readl(&(h->cfgtable->TransportActive)); |
5958 | if (!(register_value & CFGTBL_Trans_Performant)) { | |
5959 | dev_warn(&h->pdev->dev, "unable to get board into" | |
5960 | " performant mode\n"); | |
5961 | return; | |
5962 | } | |
960a30e7 | 5963 | /* Change the access methods to the performant access methods */ |
e1f7de0c MG |
5964 | h->access = access; |
5965 | h->transMethod = transMethod; | |
5966 | ||
5967 | if (!(trans_support & CFGTBL_Trans_io_accel1)) | |
5968 | return; | |
5969 | ||
5970 | /* Set up I/O accelerator mode */ | |
5971 | for (i = 0; i < h->nreply_queues; i++) { | |
5972 | writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX); | |
5973 | h->reply_queue[i].current_entry = | |
5974 | readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX); | |
5975 | } | |
283b4a9b SC |
5976 | bft[7] = h->ioaccel_maxsg + 8; |
5977 | calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8, | |
e1f7de0c MG |
5978 | h->ioaccel1_blockFetchTable); |
5979 | ||
5980 | /* initialize all reply queue entries to unused */ | |
5981 | memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED, | |
5982 | h->reply_pool_size); | |
5983 | ||
5984 | /* set all the constant fields in the accelerator command | |
5985 | * frames once at init time to save CPU cycles later. | |
5986 | */ | |
5987 | for (i = 0; i < h->nr_cmds; i++) { | |
5988 | struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i]; | |
5989 | ||
5990 | cp->function = IOACCEL1_FUNCTION_SCSIIO; | |
5991 | cp->err_info = (u32) (h->errinfo_pool_dhandle + | |
5992 | (i * sizeof(struct ErrorInfo))); | |
5993 | cp->err_info_len = sizeof(struct ErrorInfo); | |
5994 | cp->sgl_offset = IOACCEL1_SGLOFFSET; | |
5995 | cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT; | |
5996 | cp->timeout_sec = 0; | |
5997 | cp->ReplyQueue = 0; | |
5998 | cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) | DIRECT_LOOKUP_BIT; | |
5999 | cp->Tag.upper = 0; | |
6000 | cp->host_addr.lower = (u32) (h->ioaccel_cmd_pool_dhandle + | |
6001 | (i * sizeof(struct io_accel1_cmd))); | |
6002 | cp->host_addr.upper = 0; | |
6003 | } | |
6004 | } | |
6005 | ||
6006 | static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h) | |
6007 | { | |
283b4a9b SC |
6008 | h->ioaccel_maxsg = |
6009 | readl(&(h->cfgtable->io_accel_max_embedded_sg_count)); | |
6010 | if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES) | |
6011 | h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES; | |
6012 | ||
e1f7de0c MG |
6013 | /* Command structures must be aligned on a 128-byte boundary |
6014 | * because the 7 lower bits of the address are used by the | |
6015 | * hardware. | |
6016 | */ | |
6017 | #define IOACCEL1_COMMANDLIST_ALIGNMENT 128 | |
6018 | BUILD_BUG_ON(sizeof(struct io_accel1_cmd) % | |
6019 | IOACCEL1_COMMANDLIST_ALIGNMENT); | |
6020 | h->ioaccel_cmd_pool = | |
6021 | pci_alloc_consistent(h->pdev, | |
6022 | h->nr_cmds * sizeof(*h->ioaccel_cmd_pool), | |
6023 | &(h->ioaccel_cmd_pool_dhandle)); | |
6024 | ||
6025 | h->ioaccel1_blockFetchTable = | |
283b4a9b | 6026 | kmalloc(((h->ioaccel_maxsg + 1) * |
e1f7de0c MG |
6027 | sizeof(u32)), GFP_KERNEL); |
6028 | ||
6029 | if ((h->ioaccel_cmd_pool == NULL) || | |
6030 | (h->ioaccel1_blockFetchTable == NULL)) | |
6031 | goto clean_up; | |
6032 | ||
6033 | memset(h->ioaccel_cmd_pool, 0, | |
6034 | h->nr_cmds * sizeof(*h->ioaccel_cmd_pool)); | |
6035 | return 0; | |
6036 | ||
6037 | clean_up: | |
6038 | if (h->ioaccel_cmd_pool) | |
6039 | pci_free_consistent(h->pdev, | |
6040 | h->nr_cmds * sizeof(*h->ioaccel_cmd_pool), | |
6041 | h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle); | |
6042 | kfree(h->ioaccel1_blockFetchTable); | |
6043 | return 1; | |
6c311b57 SC |
6044 | } |
6045 | ||
6f039790 | 6046 | static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h) |
6c311b57 SC |
6047 | { |
6048 | u32 trans_support; | |
e1f7de0c MG |
6049 | unsigned long transMethod = CFGTBL_Trans_Performant | |
6050 | CFGTBL_Trans_use_short_tags; | |
254f796b | 6051 | int i; |
6c311b57 | 6052 | |
02ec19c8 SC |
6053 | if (hpsa_simple_mode) |
6054 | return; | |
6055 | ||
e1f7de0c MG |
6056 | /* Check for I/O accelerator mode support */ |
6057 | if (trans_support & CFGTBL_Trans_io_accel1) { | |
6058 | transMethod |= CFGTBL_Trans_io_accel1 | | |
6059 | CFGTBL_Trans_enable_directed_msix; | |
6060 | if (hpsa_alloc_ioaccel_cmd_and_bft(h)) | |
6061 | goto clean_up; | |
6062 | } | |
6063 | ||
6064 | /* TODO, check that this next line h->nreply_queues is correct */ | |
6c311b57 SC |
6065 | trans_support = readl(&(h->cfgtable->TransportSupport)); |
6066 | if (!(trans_support & PERFORMANT_MODE)) | |
6067 | return; | |
6068 | ||
eee0f03a | 6069 | h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1; |
cba3d38b | 6070 | hpsa_get_max_perf_mode_cmds(h); |
6c311b57 | 6071 | /* Performant mode ring buffer and supporting data structures */ |
254f796b | 6072 | h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues; |
6c311b57 SC |
6073 | h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size, |
6074 | &(h->reply_pool_dhandle)); | |
6075 | ||
254f796b MG |
6076 | for (i = 0; i < h->nreply_queues; i++) { |
6077 | h->reply_queue[i].head = &h->reply_pool[h->max_commands * i]; | |
6078 | h->reply_queue[i].size = h->max_commands; | |
6079 | h->reply_queue[i].wraparound = 1; /* spec: init to 1 */ | |
6080 | h->reply_queue[i].current_entry = 0; | |
6081 | } | |
6082 | ||
6c311b57 | 6083 | /* Need a block fetch table for performant mode */ |
d66ae08b | 6084 | h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) * |
6c311b57 SC |
6085 | sizeof(u32)), GFP_KERNEL); |
6086 | ||
6087 | if ((h->reply_pool == NULL) | |
6088 | || (h->blockFetchTable == NULL)) | |
6089 | goto clean_up; | |
6090 | ||
e1f7de0c | 6091 | hpsa_enter_performant_mode(h, trans_support); |
303932fd DB |
6092 | return; |
6093 | ||
6094 | clean_up: | |
6095 | if (h->reply_pool) | |
6096 | pci_free_consistent(h->pdev, h->reply_pool_size, | |
6097 | h->reply_pool, h->reply_pool_dhandle); | |
6098 | kfree(h->blockFetchTable); | |
6099 | } | |
6100 | ||
76438d08 SC |
6101 | static void hpsa_drain_commands(struct ctlr_info *h) |
6102 | { | |
6103 | int cmds_out; | |
6104 | unsigned long flags; | |
6105 | ||
6106 | do { /* wait for all outstanding commands to drain out */ | |
6107 | spin_lock_irqsave(&h->lock, flags); | |
6108 | cmds_out = h->commands_outstanding; | |
6109 | spin_unlock_irqrestore(&h->lock, flags); | |
6110 | if (cmds_out <= 0) | |
6111 | break; | |
6112 | msleep(100); | |
6113 | } while (1); | |
6114 | } | |
6115 | ||
edd16368 SC |
6116 | /* |
6117 | * This is it. Register the PCI driver information for the cards we control | |
6118 | * the OS will call our registered routines when it finds one of our cards. | |
6119 | */ | |
6120 | static int __init hpsa_init(void) | |
6121 | { | |
31468401 | 6122 | return pci_register_driver(&hpsa_pci_driver); |
edd16368 SC |
6123 | } |
6124 | ||
6125 | static void __exit hpsa_cleanup(void) | |
6126 | { | |
6127 | pci_unregister_driver(&hpsa_pci_driver); | |
edd16368 SC |
6128 | } |
6129 | ||
e1f7de0c MG |
6130 | static void __attribute__((unused)) verify_offsets(void) |
6131 | { | |
6132 | #define VERIFY_OFFSET(member, offset) \ | |
6133 | BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset) | |
6134 | ||
6135 | VERIFY_OFFSET(dev_handle, 0x00); | |
6136 | VERIFY_OFFSET(reserved1, 0x02); | |
6137 | VERIFY_OFFSET(function, 0x03); | |
6138 | VERIFY_OFFSET(reserved2, 0x04); | |
6139 | VERIFY_OFFSET(err_info, 0x0C); | |
6140 | VERIFY_OFFSET(reserved3, 0x10); | |
6141 | VERIFY_OFFSET(err_info_len, 0x12); | |
6142 | VERIFY_OFFSET(reserved4, 0x13); | |
6143 | VERIFY_OFFSET(sgl_offset, 0x14); | |
6144 | VERIFY_OFFSET(reserved5, 0x15); | |
6145 | VERIFY_OFFSET(transfer_len, 0x1C); | |
6146 | VERIFY_OFFSET(reserved6, 0x20); | |
6147 | VERIFY_OFFSET(io_flags, 0x24); | |
6148 | VERIFY_OFFSET(reserved7, 0x26); | |
6149 | VERIFY_OFFSET(LUN, 0x34); | |
6150 | VERIFY_OFFSET(control, 0x3C); | |
6151 | VERIFY_OFFSET(CDB, 0x40); | |
6152 | VERIFY_OFFSET(reserved8, 0x50); | |
6153 | VERIFY_OFFSET(host_context_flags, 0x60); | |
6154 | VERIFY_OFFSET(timeout_sec, 0x62); | |
6155 | VERIFY_OFFSET(ReplyQueue, 0x64); | |
6156 | VERIFY_OFFSET(reserved9, 0x65); | |
6157 | VERIFY_OFFSET(Tag, 0x68); | |
6158 | VERIFY_OFFSET(host_addr, 0x70); | |
6159 | VERIFY_OFFSET(CISS_LUN, 0x78); | |
6160 | VERIFY_OFFSET(SG, 0x78 + 8); | |
6161 | #undef VERIFY_OFFSET | |
6162 | } | |
6163 | ||
edd16368 SC |
6164 | module_init(hpsa_init); |
6165 | module_exit(hpsa_cleanup); |