]>
Commit | Line | Data |
---|---|---|
26780d9e BG |
1 | /* |
2 | * linux/drivers/scsi/esas2r/esas2r_main.c | |
3 | * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers | |
4 | * | |
5 | * Copyright (c) 2001-2013 ATTO Technology, Inc. | |
6 | * (mailto:linuxdrivers@attotech.com) | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * as published by the Free Software Foundation; either version 2 | |
11 | * of the License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * NO WARRANTY | |
19 | * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR | |
20 | * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT | |
21 | * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, | |
22 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is | |
23 | * solely responsible for determining the appropriateness of using and | |
24 | * distributing the Program and assumes all risks associated with its | |
25 | * exercise of rights under this Agreement, including but not limited to | |
26 | * the risks and costs of program errors, damage to or loss of data, | |
27 | * programs or equipment, and unavailability or interruption of operations. | |
28 | * | |
29 | * DISCLAIMER OF LIABILITY | |
30 | * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY | |
31 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
32 | * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND | |
33 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | |
35 | * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED | |
36 | * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES | |
37 | * | |
38 | * You should have received a copy of the GNU General Public License | |
39 | * along with this program; if not, write to the Free Software | |
40 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | |
41 | * USA. | |
42 | */ | |
43 | ||
44 | #include "esas2r.h" | |
45 | ||
46 | MODULE_DESCRIPTION(ESAS2R_DRVR_NAME ": " ESAS2R_LONGNAME " driver"); | |
47 | MODULE_AUTHOR("ATTO Technology, Inc."); | |
48 | MODULE_LICENSE("GPL"); | |
49 | MODULE_VERSION(ESAS2R_VERSION_STR); | |
50 | ||
51 | /* global definitions */ | |
52 | ||
53 | static int found_adapters; | |
54 | struct esas2r_adapter *esas2r_adapters[MAX_ADAPTERS]; | |
55 | ||
56 | #define ESAS2R_VDA_EVENT_PORT1 54414 | |
57 | #define ESAS2R_VDA_EVENT_PORT2 54415 | |
58 | #define ESAS2R_VDA_EVENT_SOCK_COUNT 2 | |
59 | ||
60 | static struct esas2r_adapter *esas2r_adapter_from_kobj(struct kobject *kobj) | |
61 | { | |
62 | struct device *dev = container_of(kobj, struct device, kobj); | |
63 | struct Scsi_Host *host = class_to_shost(dev); | |
64 | ||
65 | return (struct esas2r_adapter *)host->hostdata; | |
66 | } | |
67 | ||
68 | static ssize_t read_fw(struct file *file, struct kobject *kobj, | |
69 | struct bin_attribute *attr, | |
70 | char *buf, loff_t off, size_t count) | |
71 | { | |
72 | struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj); | |
73 | ||
74 | return esas2r_read_fw(a, buf, off, count); | |
75 | } | |
76 | ||
77 | static ssize_t write_fw(struct file *file, struct kobject *kobj, | |
78 | struct bin_attribute *attr, | |
79 | char *buf, loff_t off, size_t count) | |
80 | { | |
81 | struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj); | |
82 | ||
83 | return esas2r_write_fw(a, buf, off, count); | |
84 | } | |
85 | ||
86 | static ssize_t read_fs(struct file *file, struct kobject *kobj, | |
87 | struct bin_attribute *attr, | |
88 | char *buf, loff_t off, size_t count) | |
89 | { | |
90 | struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj); | |
91 | ||
92 | return esas2r_read_fs(a, buf, off, count); | |
93 | } | |
94 | ||
95 | static ssize_t write_fs(struct file *file, struct kobject *kobj, | |
96 | struct bin_attribute *attr, | |
97 | char *buf, loff_t off, size_t count) | |
98 | { | |
99 | struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj); | |
100 | int length = min(sizeof(struct esas2r_ioctl_fs), count); | |
101 | int result = 0; | |
102 | ||
103 | result = esas2r_write_fs(a, buf, off, count); | |
104 | ||
105 | if (result < 0) | |
106 | result = 0; | |
107 | ||
108 | return length; | |
109 | } | |
110 | ||
111 | static ssize_t read_vda(struct file *file, struct kobject *kobj, | |
112 | struct bin_attribute *attr, | |
113 | char *buf, loff_t off, size_t count) | |
114 | { | |
115 | struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj); | |
116 | ||
117 | return esas2r_read_vda(a, buf, off, count); | |
118 | } | |
119 | ||
120 | static ssize_t write_vda(struct file *file, struct kobject *kobj, | |
121 | struct bin_attribute *attr, | |
122 | char *buf, loff_t off, size_t count) | |
123 | { | |
124 | struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj); | |
125 | ||
126 | return esas2r_write_vda(a, buf, off, count); | |
127 | } | |
128 | ||
129 | static ssize_t read_live_nvram(struct file *file, struct kobject *kobj, | |
130 | struct bin_attribute *attr, | |
131 | char *buf, loff_t off, size_t count) | |
132 | { | |
133 | struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj); | |
134 | int length = min_t(size_t, sizeof(struct esas2r_sas_nvram), PAGE_SIZE); | |
135 | ||
136 | memcpy(buf, a->nvram, length); | |
137 | return length; | |
138 | } | |
139 | ||
140 | static ssize_t write_live_nvram(struct file *file, struct kobject *kobj, | |
141 | struct bin_attribute *attr, | |
142 | char *buf, loff_t off, size_t count) | |
143 | { | |
144 | struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj); | |
145 | struct esas2r_request *rq; | |
146 | int result = -EFAULT; | |
147 | ||
148 | rq = esas2r_alloc_request(a); | |
149 | if (rq == NULL) | |
150 | return -ENOMEM; | |
151 | ||
152 | if (esas2r_write_params(a, rq, (struct esas2r_sas_nvram *)buf)) | |
153 | result = count; | |
154 | ||
155 | esas2r_free_request(a, rq); | |
156 | ||
157 | return result; | |
158 | } | |
159 | ||
160 | static ssize_t read_default_nvram(struct file *file, struct kobject *kobj, | |
161 | struct bin_attribute *attr, | |
162 | char *buf, loff_t off, size_t count) | |
163 | { | |
164 | struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj); | |
165 | ||
166 | esas2r_nvram_get_defaults(a, (struct esas2r_sas_nvram *)buf); | |
167 | ||
168 | return sizeof(struct esas2r_sas_nvram); | |
169 | } | |
170 | ||
171 | static ssize_t read_hw(struct file *file, struct kobject *kobj, | |
172 | struct bin_attribute *attr, | |
173 | char *buf, loff_t off, size_t count) | |
174 | { | |
175 | struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj); | |
176 | int length = min_t(size_t, sizeof(struct atto_ioctl), PAGE_SIZE); | |
177 | ||
178 | if (!a->local_atto_ioctl) | |
179 | return -ENOMEM; | |
180 | ||
181 | if (handle_hba_ioctl(a, a->local_atto_ioctl) != IOCTL_SUCCESS) | |
182 | return -ENOMEM; | |
183 | ||
184 | memcpy(buf, a->local_atto_ioctl, length); | |
185 | ||
186 | return length; | |
187 | } | |
188 | ||
189 | static ssize_t write_hw(struct file *file, struct kobject *kobj, | |
190 | struct bin_attribute *attr, | |
191 | char *buf, loff_t off, size_t count) | |
192 | { | |
193 | struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj); | |
194 | int length = min(sizeof(struct atto_ioctl), count); | |
195 | ||
196 | if (!a->local_atto_ioctl) { | |
197 | a->local_atto_ioctl = kzalloc(sizeof(struct atto_ioctl), | |
198 | GFP_KERNEL); | |
199 | if (a->local_atto_ioctl == NULL) { | |
200 | esas2r_log(ESAS2R_LOG_WARN, | |
201 | "write_hw kzalloc failed for %d bytes", | |
202 | sizeof(struct atto_ioctl)); | |
203 | return -ENOMEM; | |
204 | } | |
205 | } | |
206 | ||
207 | memset(a->local_atto_ioctl, 0, sizeof(struct atto_ioctl)); | |
208 | memcpy(a->local_atto_ioctl, buf, length); | |
209 | ||
210 | return length; | |
211 | } | |
212 | ||
213 | #define ESAS2R_RW_BIN_ATTR(_name) \ | |
214 | struct bin_attribute bin_attr_ ## _name = { \ | |
215 | .attr = \ | |
216 | { .name = __stringify(_name), .mode = S_IRUSR | S_IWUSR }, \ | |
217 | .size = 0, \ | |
218 | .read = read_ ## _name, \ | |
219 | .write = write_ ## _name } | |
220 | ||
221 | ESAS2R_RW_BIN_ATTR(fw); | |
222 | ESAS2R_RW_BIN_ATTR(fs); | |
223 | ESAS2R_RW_BIN_ATTR(vda); | |
224 | ESAS2R_RW_BIN_ATTR(hw); | |
225 | ESAS2R_RW_BIN_ATTR(live_nvram); | |
226 | ||
227 | struct bin_attribute bin_attr_default_nvram = { | |
228 | .attr = { .name = "default_nvram", .mode = S_IRUGO }, | |
229 | .size = 0, | |
230 | .read = read_default_nvram, | |
231 | .write = NULL | |
232 | }; | |
233 | ||
234 | static struct scsi_host_template driver_template = { | |
235 | .module = THIS_MODULE, | |
236 | .show_info = esas2r_show_info, | |
237 | .name = ESAS2R_LONGNAME, | |
238 | .release = esas2r_release, | |
239 | .info = esas2r_info, | |
240 | .ioctl = esas2r_ioctl, | |
241 | .queuecommand = esas2r_queuecommand, | |
242 | .eh_abort_handler = esas2r_eh_abort, | |
243 | .eh_device_reset_handler = esas2r_device_reset, | |
244 | .eh_bus_reset_handler = esas2r_bus_reset, | |
245 | .eh_host_reset_handler = esas2r_host_reset, | |
246 | .eh_target_reset_handler = esas2r_target_reset, | |
247 | .can_queue = 128, | |
248 | .this_id = -1, | |
249 | .sg_tablesize = SCSI_MAX_SG_SEGMENTS, | |
250 | .cmd_per_lun = | |
251 | ESAS2R_DEFAULT_CMD_PER_LUN, | |
252 | .present = 0, | |
253 | .unchecked_isa_dma = 0, | |
254 | .use_clustering = ENABLE_CLUSTERING, | |
255 | .emulated = 0, | |
256 | .proc_name = ESAS2R_DRVR_NAME, | |
26780d9e | 257 | .change_queue_depth = esas2r_change_queue_depth, |
a62182f3 | 258 | .change_queue_type = scsi_change_queue_type, |
26780d9e | 259 | .max_sectors = 0xFFFF, |
2ecb204d | 260 | .use_blk_tags = 1, |
26780d9e BG |
261 | }; |
262 | ||
263 | int sgl_page_size = 512; | |
264 | module_param(sgl_page_size, int, 0); | |
265 | MODULE_PARM_DESC(sgl_page_size, | |
266 | "Scatter/gather list (SGL) page size in number of S/G " | |
267 | "entries. If your application is doing a lot of very large " | |
268 | "transfers, you may want to increase the SGL page size. " | |
269 | "Default 512."); | |
270 | ||
271 | int num_sg_lists = 1024; | |
272 | module_param(num_sg_lists, int, 0); | |
273 | MODULE_PARM_DESC(num_sg_lists, | |
274 | "Number of scatter/gather lists. Default 1024."); | |
275 | ||
276 | int sg_tablesize = SCSI_MAX_SG_SEGMENTS; | |
277 | module_param(sg_tablesize, int, 0); | |
278 | MODULE_PARM_DESC(sg_tablesize, | |
279 | "Maximum number of entries in a scatter/gather table."); | |
280 | ||
281 | int num_requests = 256; | |
282 | module_param(num_requests, int, 0); | |
283 | MODULE_PARM_DESC(num_requests, | |
284 | "Number of requests. Default 256."); | |
285 | ||
286 | int num_ae_requests = 4; | |
287 | module_param(num_ae_requests, int, 0); | |
288 | MODULE_PARM_DESC(num_ae_requests, | |
289 | "Number of VDA asynchromous event requests. Default 4."); | |
290 | ||
291 | int cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN; | |
292 | module_param(cmd_per_lun, int, 0); | |
293 | MODULE_PARM_DESC(cmd_per_lun, | |
294 | "Maximum number of commands per LUN. Default " | |
295 | DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN) "."); | |
296 | ||
297 | int can_queue = 128; | |
298 | module_param(can_queue, int, 0); | |
299 | MODULE_PARM_DESC(can_queue, | |
300 | "Maximum number of commands per adapter. Default 128."); | |
301 | ||
302 | int esas2r_max_sectors = 0xFFFF; | |
303 | module_param(esas2r_max_sectors, int, 0); | |
304 | MODULE_PARM_DESC(esas2r_max_sectors, | |
305 | "Maximum number of disk sectors in a single data transfer. " | |
306 | "Default 65535 (largest possible setting)."); | |
307 | ||
308 | int interrupt_mode = 1; | |
309 | module_param(interrupt_mode, int, 0); | |
310 | MODULE_PARM_DESC(interrupt_mode, | |
311 | "Defines the interrupt mode to use. 0 for legacy" | |
312 | ", 1 for MSI. Default is MSI (1)."); | |
313 | ||
314 | static struct pci_device_id | |
315 | esas2r_pci_table[] = { | |
316 | { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x0049, | |
317 | 0, | |
318 | 0, 0 }, | |
319 | { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004A, | |
320 | 0, | |
321 | 0, 0 }, | |
322 | { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004B, | |
323 | 0, | |
324 | 0, 0 }, | |
325 | { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004C, | |
326 | 0, | |
327 | 0, 0 }, | |
328 | { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004D, | |
329 | 0, | |
330 | 0, 0 }, | |
331 | { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004E, | |
332 | 0, | |
333 | 0, 0 }, | |
334 | { 0, 0, 0, 0, | |
335 | 0, | |
336 | 0, 0 } | |
337 | }; | |
338 | ||
339 | MODULE_DEVICE_TABLE(pci, esas2r_pci_table); | |
340 | ||
341 | static int | |
342 | esas2r_probe(struct pci_dev *pcid, const struct pci_device_id *id); | |
343 | ||
344 | static void | |
345 | esas2r_remove(struct pci_dev *pcid); | |
346 | ||
347 | static struct pci_driver | |
348 | esas2r_pci_driver = { | |
349 | .name = ESAS2R_DRVR_NAME, | |
350 | .id_table = esas2r_pci_table, | |
351 | .probe = esas2r_probe, | |
352 | .remove = esas2r_remove, | |
353 | .suspend = esas2r_suspend, | |
354 | .resume = esas2r_resume, | |
355 | }; | |
356 | ||
357 | static int esas2r_probe(struct pci_dev *pcid, | |
358 | const struct pci_device_id *id) | |
359 | { | |
360 | struct Scsi_Host *host = NULL; | |
361 | struct esas2r_adapter *a; | |
362 | int err; | |
363 | ||
364 | size_t host_alloc_size = sizeof(struct esas2r_adapter) | |
365 | + ((num_requests) + | |
366 | 1) * sizeof(struct esas2r_request); | |
367 | ||
368 | esas2r_log_dev(ESAS2R_LOG_DEBG, &(pcid->dev), | |
369 | "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x", | |
370 | pcid->vendor, | |
371 | pcid->device, | |
372 | pcid->subsystem_vendor, | |
373 | pcid->subsystem_device); | |
374 | ||
375 | esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev), | |
376 | "before pci_enable_device() " | |
377 | "enable_cnt: %d", | |
378 | pcid->enable_cnt.counter); | |
379 | ||
380 | err = pci_enable_device(pcid); | |
381 | if (err != 0) { | |
382 | esas2r_log_dev(ESAS2R_LOG_CRIT, &(pcid->dev), | |
383 | "pci_enable_device() FAIL (%d)", | |
384 | err); | |
385 | return -ENODEV; | |
386 | } | |
387 | ||
388 | esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev), | |
389 | "pci_enable_device() OK"); | |
390 | esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev), | |
806188a4 | 391 | "after pci_enable_device() enable_cnt: %d", |
26780d9e BG |
392 | pcid->enable_cnt.counter); |
393 | ||
394 | host = scsi_host_alloc(&driver_template, host_alloc_size); | |
395 | if (host == NULL) { | |
396 | esas2r_log(ESAS2R_LOG_CRIT, "scsi_host_alloc() FAIL"); | |
397 | return -ENODEV; | |
398 | } | |
399 | ||
400 | memset(host->hostdata, 0, host_alloc_size); | |
401 | ||
402 | a = (struct esas2r_adapter *)host->hostdata; | |
403 | ||
404 | esas2r_log(ESAS2R_LOG_INFO, "scsi_host_alloc() OK host: %p", host); | |
405 | ||
406 | /* override max LUN and max target id */ | |
407 | ||
408 | host->max_id = ESAS2R_MAX_ID + 1; | |
409 | host->max_lun = 255; | |
410 | ||
411 | /* we can handle 16-byte CDbs */ | |
412 | ||
413 | host->max_cmd_len = 16; | |
414 | ||
415 | host->can_queue = can_queue; | |
416 | host->cmd_per_lun = cmd_per_lun; | |
417 | host->this_id = host->max_id + 1; | |
418 | host->max_channel = 0; | |
419 | host->unique_id = found_adapters; | |
420 | host->sg_tablesize = sg_tablesize; | |
421 | host->max_sectors = esas2r_max_sectors; | |
422 | ||
423 | /* set to bus master for BIOses that don't do it for us */ | |
424 | ||
425 | esas2r_log(ESAS2R_LOG_INFO, "pci_set_master() called"); | |
426 | ||
427 | pci_set_master(pcid); | |
428 | ||
429 | if (!esas2r_init_adapter(host, pcid, found_adapters)) { | |
430 | esas2r_log(ESAS2R_LOG_CRIT, | |
431 | "unable to initialize device at PCI bus %x:%x", | |
432 | pcid->bus->number, | |
433 | pcid->devfn); | |
434 | ||
435 | esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev), | |
436 | "scsi_host_put() called"); | |
437 | ||
438 | scsi_host_put(host); | |
439 | ||
440 | return 0; | |
441 | ||
442 | } | |
443 | ||
444 | esas2r_log(ESAS2R_LOG_INFO, "pci_set_drvdata(%p, %p) called", pcid, | |
445 | host->hostdata); | |
446 | ||
447 | pci_set_drvdata(pcid, host); | |
448 | ||
449 | esas2r_log(ESAS2R_LOG_INFO, "scsi_add_host() called"); | |
450 | ||
451 | err = scsi_add_host(host, &pcid->dev); | |
452 | ||
453 | if (err) { | |
454 | esas2r_log(ESAS2R_LOG_CRIT, "scsi_add_host returned %d", err); | |
455 | esas2r_log_dev(ESAS2R_LOG_CRIT, &(host->shost_gendev), | |
456 | "scsi_add_host() FAIL"); | |
457 | ||
458 | esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev), | |
459 | "scsi_host_put() called"); | |
460 | ||
461 | scsi_host_put(host); | |
462 | ||
463 | esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev), | |
464 | "pci_set_drvdata(%p, NULL) called", | |
465 | pcid); | |
466 | ||
467 | pci_set_drvdata(pcid, NULL); | |
468 | ||
469 | return -ENODEV; | |
470 | } | |
471 | ||
472 | ||
473 | esas2r_fw_event_on(a); | |
474 | ||
475 | esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev), | |
476 | "scsi_scan_host() called"); | |
477 | ||
478 | scsi_scan_host(host); | |
479 | ||
480 | /* Add sysfs binary files */ | |
481 | if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fw)) | |
482 | esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev), | |
483 | "Failed to create sysfs binary file: fw"); | |
484 | else | |
485 | a->sysfs_fw_created = 1; | |
486 | ||
487 | if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fs)) | |
488 | esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev), | |
489 | "Failed to create sysfs binary file: fs"); | |
490 | else | |
491 | a->sysfs_fs_created = 1; | |
492 | ||
493 | if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_vda)) | |
494 | esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev), | |
495 | "Failed to create sysfs binary file: vda"); | |
496 | else | |
497 | a->sysfs_vda_created = 1; | |
498 | ||
499 | if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_hw)) | |
500 | esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev), | |
501 | "Failed to create sysfs binary file: hw"); | |
502 | else | |
503 | a->sysfs_hw_created = 1; | |
504 | ||
505 | if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_live_nvram)) | |
506 | esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev), | |
507 | "Failed to create sysfs binary file: live_nvram"); | |
508 | else | |
509 | a->sysfs_live_nvram_created = 1; | |
510 | ||
511 | if (sysfs_create_bin_file(&host->shost_dev.kobj, | |
512 | &bin_attr_default_nvram)) | |
513 | esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev), | |
514 | "Failed to create sysfs binary file: default_nvram"); | |
515 | else | |
516 | a->sysfs_default_nvram_created = 1; | |
517 | ||
518 | found_adapters++; | |
519 | ||
520 | return 0; | |
521 | } | |
522 | ||
523 | static void esas2r_remove(struct pci_dev *pdev) | |
524 | { | |
525 | struct Scsi_Host *host; | |
526 | int index; | |
527 | ||
528 | if (pdev == NULL) { | |
529 | esas2r_log(ESAS2R_LOG_WARN, "esas2r_remove pdev==NULL"); | |
530 | return; | |
531 | } | |
532 | ||
533 | host = pci_get_drvdata(pdev); | |
534 | ||
535 | if (host == NULL) { | |
536 | /* | |
537 | * this can happen if pci_set_drvdata was already called | |
538 | * to clear the host pointer. if this is the case, we | |
539 | * are okay; this channel has already been cleaned up. | |
540 | */ | |
541 | ||
542 | return; | |
543 | } | |
544 | ||
545 | esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev), | |
546 | "esas2r_remove(%p) called; " | |
547 | "host:%p", pdev, | |
548 | host); | |
549 | ||
550 | index = esas2r_cleanup(host); | |
551 | ||
552 | if (index < 0) | |
553 | esas2r_log_dev(ESAS2R_LOG_WARN, &(pdev->dev), | |
554 | "unknown host in %s", | |
555 | __func__); | |
556 | ||
557 | found_adapters--; | |
558 | ||
559 | /* if this was the last adapter, clean up the rest of the driver */ | |
560 | ||
561 | if (found_adapters == 0) | |
562 | esas2r_cleanup(NULL); | |
563 | } | |
564 | ||
565 | static int __init esas2r_init(void) | |
566 | { | |
567 | int i; | |
568 | ||
569 | esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__); | |
570 | ||
571 | /* verify valid parameters */ | |
572 | ||
573 | if (can_queue < 1) { | |
574 | esas2r_log(ESAS2R_LOG_WARN, | |
575 | "warning: can_queue must be at least 1, value " | |
576 | "forced."); | |
577 | can_queue = 1; | |
578 | } else if (can_queue > 2048) { | |
579 | esas2r_log(ESAS2R_LOG_WARN, | |
580 | "warning: can_queue must be no larger than 2048, " | |
581 | "value forced."); | |
582 | can_queue = 2048; | |
583 | } | |
584 | ||
585 | if (cmd_per_lun < 1) { | |
586 | esas2r_log(ESAS2R_LOG_WARN, | |
587 | "warning: cmd_per_lun must be at least 1, value " | |
588 | "forced."); | |
589 | cmd_per_lun = 1; | |
590 | } else if (cmd_per_lun > 2048) { | |
591 | esas2r_log(ESAS2R_LOG_WARN, | |
592 | "warning: cmd_per_lun must be no larger than " | |
593 | "2048, value forced."); | |
594 | cmd_per_lun = 2048; | |
595 | } | |
596 | ||
597 | if (sg_tablesize < 32) { | |
598 | esas2r_log(ESAS2R_LOG_WARN, | |
599 | "warning: sg_tablesize must be at least 32, " | |
600 | "value forced."); | |
601 | sg_tablesize = 32; | |
602 | } | |
603 | ||
604 | if (esas2r_max_sectors < 1) { | |
605 | esas2r_log(ESAS2R_LOG_WARN, | |
606 | "warning: esas2r_max_sectors must be at least " | |
607 | "1, value forced."); | |
608 | esas2r_max_sectors = 1; | |
609 | } else if (esas2r_max_sectors > 0xffff) { | |
610 | esas2r_log(ESAS2R_LOG_WARN, | |
611 | "warning: esas2r_max_sectors must be no larger " | |
612 | "than 0xffff, value forced."); | |
613 | esas2r_max_sectors = 0xffff; | |
614 | } | |
615 | ||
616 | sgl_page_size &= ~(ESAS2R_SGL_ALIGN - 1); | |
617 | ||
618 | if (sgl_page_size < SGL_PG_SZ_MIN) | |
619 | sgl_page_size = SGL_PG_SZ_MIN; | |
620 | else if (sgl_page_size > SGL_PG_SZ_MAX) | |
621 | sgl_page_size = SGL_PG_SZ_MAX; | |
622 | ||
623 | if (num_sg_lists < NUM_SGL_MIN) | |
624 | num_sg_lists = NUM_SGL_MIN; | |
625 | else if (num_sg_lists > NUM_SGL_MAX) | |
626 | num_sg_lists = NUM_SGL_MAX; | |
627 | ||
628 | if (num_requests < NUM_REQ_MIN) | |
629 | num_requests = NUM_REQ_MIN; | |
630 | else if (num_requests > NUM_REQ_MAX) | |
631 | num_requests = NUM_REQ_MAX; | |
632 | ||
633 | if (num_ae_requests < NUM_AE_MIN) | |
634 | num_ae_requests = NUM_AE_MIN; | |
635 | else if (num_ae_requests > NUM_AE_MAX) | |
636 | num_ae_requests = NUM_AE_MAX; | |
637 | ||
638 | /* set up other globals */ | |
639 | ||
640 | for (i = 0; i < MAX_ADAPTERS; i++) | |
641 | esas2r_adapters[i] = NULL; | |
642 | ||
643 | /* initialize */ | |
644 | ||
645 | driver_template.module = THIS_MODULE; | |
646 | ||
647 | if (pci_register_driver(&esas2r_pci_driver) != 0) | |
648 | esas2r_log(ESAS2R_LOG_CRIT, "pci_register_driver FAILED"); | |
649 | else | |
650 | esas2r_log(ESAS2R_LOG_INFO, "pci_register_driver() OK"); | |
651 | ||
652 | if (!found_adapters) { | |
653 | pci_unregister_driver(&esas2r_pci_driver); | |
654 | esas2r_cleanup(NULL); | |
655 | ||
656 | esas2r_log(ESAS2R_LOG_CRIT, | |
657 | "driver will not be loaded because no ATTO " | |
658 | "%s devices were found", | |
659 | ESAS2R_DRVR_NAME); | |
660 | return -1; | |
661 | } else { | |
662 | esas2r_log(ESAS2R_LOG_INFO, "found %d adapters", | |
663 | found_adapters); | |
664 | } | |
665 | ||
666 | return 0; | |
667 | } | |
668 | ||
669 | /* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */ | |
670 | static const struct file_operations esas2r_proc_fops = { | |
671 | .compat_ioctl = esas2r_proc_ioctl, | |
672 | .unlocked_ioctl = esas2r_proc_ioctl, | |
673 | }; | |
674 | ||
675 | static struct Scsi_Host *esas2r_proc_host; | |
676 | static int esas2r_proc_major; | |
677 | ||
678 | long esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) | |
679 | { | |
680 | return esas2r_ioctl_handler(esas2r_proc_host->hostdata, | |
681 | (int)cmd, (void __user *)arg); | |
682 | } | |
683 | ||
684 | static void __exit esas2r_exit(void) | |
685 | { | |
686 | esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__); | |
687 | ||
688 | if (esas2r_proc_major > 0) { | |
689 | esas2r_log(ESAS2R_LOG_INFO, "unregister proc"); | |
690 | ||
691 | remove_proc_entry(ATTONODE_NAME, | |
692 | esas2r_proc_host->hostt->proc_dir); | |
693 | unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME); | |
694 | ||
695 | esas2r_proc_major = 0; | |
696 | } | |
697 | ||
698 | esas2r_log(ESAS2R_LOG_INFO, "pci_unregister_driver() called"); | |
699 | ||
700 | pci_unregister_driver(&esas2r_pci_driver); | |
701 | } | |
702 | ||
703 | int esas2r_show_info(struct seq_file *m, struct Scsi_Host *sh) | |
704 | { | |
705 | struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata; | |
706 | ||
707 | struct esas2r_target *t; | |
708 | int dev_count = 0; | |
709 | ||
710 | esas2r_log(ESAS2R_LOG_DEBG, "esas2r_show_info (%p,%d)", m, sh->host_no); | |
711 | ||
712 | seq_printf(m, ESAS2R_LONGNAME "\n" | |
713 | "Driver version: "ESAS2R_VERSION_STR "\n" | |
714 | "Flash version: %s\n" | |
715 | "Firmware version: %s\n" | |
716 | "Copyright "ESAS2R_COPYRIGHT_YEARS "\n" | |
717 | "http://www.attotech.com\n" | |
718 | "\n", | |
719 | a->flash_rev, | |
720 | a->fw_rev[0] ? a->fw_rev : "(none)"); | |
721 | ||
722 | ||
723 | seq_printf(m, "Adapter information:\n" | |
724 | "--------------------\n" | |
725 | "Model: %s\n" | |
726 | "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n", | |
727 | esas2r_get_model_name(a), | |
728 | a->nvram->sas_addr[0], | |
729 | a->nvram->sas_addr[1], | |
730 | a->nvram->sas_addr[2], | |
731 | a->nvram->sas_addr[3], | |
732 | a->nvram->sas_addr[4], | |
733 | a->nvram->sas_addr[5], | |
734 | a->nvram->sas_addr[6], | |
735 | a->nvram->sas_addr[7]); | |
736 | ||
737 | seq_puts(m, "\n" | |
738 | "Discovered devices:\n" | |
739 | "\n" | |
740 | " # Target ID\n" | |
741 | "---------------\n"); | |
742 | ||
743 | for (t = a->targetdb; t < a->targetdb_end; t++) | |
744 | if (t->buffered_target_state == TS_PRESENT) { | |
745 | seq_printf(m, " %3d %3d\n", | |
746 | ++dev_count, | |
747 | (u16)(uintptr_t)(t - a->targetdb)); | |
748 | } | |
749 | ||
750 | if (dev_count == 0) | |
751 | seq_puts(m, "none\n"); | |
752 | ||
753 | seq_puts(m, "\n"); | |
754 | return 0; | |
755 | ||
756 | } | |
757 | ||
758 | int esas2r_release(struct Scsi_Host *sh) | |
759 | { | |
760 | esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev), | |
761 | "esas2r_release() called"); | |
762 | ||
763 | esas2r_cleanup(sh); | |
764 | if (sh->irq) | |
765 | free_irq(sh->irq, NULL); | |
766 | scsi_unregister(sh); | |
767 | return 0; | |
768 | } | |
769 | ||
770 | const char *esas2r_info(struct Scsi_Host *sh) | |
771 | { | |
772 | struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata; | |
773 | static char esas2r_info_str[512]; | |
774 | ||
775 | esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev), | |
776 | "esas2r_info() called"); | |
777 | ||
778 | /* | |
779 | * if we haven't done so already, register as a char driver | |
780 | * and stick a node under "/proc/scsi/esas2r/ATTOnode" | |
781 | */ | |
782 | ||
783 | if (esas2r_proc_major <= 0) { | |
784 | esas2r_proc_host = sh; | |
785 | ||
786 | esas2r_proc_major = register_chrdev(0, ESAS2R_DRVR_NAME, | |
787 | &esas2r_proc_fops); | |
788 | ||
789 | esas2r_log_dev(ESAS2R_LOG_DEBG, &(sh->shost_gendev), | |
790 | "register_chrdev (major %d)", | |
791 | esas2r_proc_major); | |
792 | ||
793 | if (esas2r_proc_major > 0) { | |
794 | struct proc_dir_entry *pde; | |
795 | ||
796 | pde = proc_create(ATTONODE_NAME, 0, | |
797 | sh->hostt->proc_dir, | |
798 | &esas2r_proc_fops); | |
799 | ||
800 | if (!pde) { | |
801 | esas2r_log_dev(ESAS2R_LOG_WARN, | |
802 | &(sh->shost_gendev), | |
803 | "failed to create_proc_entry"); | |
804 | esas2r_proc_major = -1; | |
805 | } | |
806 | } | |
807 | } | |
808 | ||
809 | sprintf(esas2r_info_str, | |
810 | ESAS2R_LONGNAME " (bus 0x%02X, device 0x%02X, IRQ 0x%02X)" | |
811 | " driver version: "ESAS2R_VERSION_STR " firmware version: " | |
812 | "%s\n", | |
813 | a->pcid->bus->number, a->pcid->devfn, a->pcid->irq, | |
814 | a->fw_rev[0] ? a->fw_rev : "(none)"); | |
815 | ||
816 | return esas2r_info_str; | |
817 | } | |
818 | ||
819 | /* Callback for building a request scatter/gather list */ | |
820 | static u32 get_physaddr_from_sgc(struct esas2r_sg_context *sgc, u64 *addr) | |
821 | { | |
822 | u32 len; | |
823 | ||
824 | if (likely(sgc->cur_offset == sgc->exp_offset)) { | |
825 | /* | |
826 | * the normal case: caller used all bytes from previous call, so | |
827 | * expected offset is the same as the current offset. | |
828 | */ | |
829 | ||
830 | if (sgc->sgel_count < sgc->num_sgel) { | |
831 | /* retrieve next segment, except for first time */ | |
832 | if (sgc->exp_offset > (u8 *)0) { | |
833 | /* advance current segment */ | |
834 | sgc->cur_sgel = sg_next(sgc->cur_sgel); | |
835 | ++(sgc->sgel_count); | |
836 | } | |
837 | ||
838 | ||
839 | len = sg_dma_len(sgc->cur_sgel); | |
840 | (*addr) = sg_dma_address(sgc->cur_sgel); | |
841 | ||
842 | /* save the total # bytes returned to caller so far */ | |
843 | sgc->exp_offset += len; | |
844 | ||
845 | } else { | |
846 | len = 0; | |
847 | } | |
848 | } else if (sgc->cur_offset < sgc->exp_offset) { | |
849 | /* | |
850 | * caller did not use all bytes from previous call. need to | |
851 | * compute the address based on current segment. | |
852 | */ | |
853 | ||
854 | len = sg_dma_len(sgc->cur_sgel); | |
855 | (*addr) = sg_dma_address(sgc->cur_sgel); | |
856 | ||
857 | sgc->exp_offset -= len; | |
858 | ||
859 | /* calculate PA based on prev segment address and offsets */ | |
860 | *addr = *addr + | |
861 | (sgc->cur_offset - sgc->exp_offset); | |
862 | ||
863 | sgc->exp_offset += len; | |
864 | ||
865 | /* re-calculate length based on offset */ | |
866 | len = lower_32_bits( | |
867 | sgc->exp_offset - sgc->cur_offset); | |
868 | } else { /* if ( sgc->cur_offset > sgc->exp_offset ) */ | |
869 | /* | |
870 | * we don't expect the caller to skip ahead. | |
871 | * cur_offset will never exceed the len we return | |
872 | */ | |
873 | len = 0; | |
874 | } | |
875 | ||
876 | return len; | |
877 | } | |
878 | ||
879 | int esas2r_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) | |
880 | { | |
881 | struct esas2r_adapter *a = | |
882 | (struct esas2r_adapter *)cmd->device->host->hostdata; | |
883 | struct esas2r_request *rq; | |
884 | struct esas2r_sg_context sgc; | |
885 | unsigned bufflen; | |
886 | ||
887 | /* Assume success, if it fails we will fix the result later. */ | |
888 | cmd->result = DID_OK << 16; | |
889 | ||
9588d24e | 890 | if (unlikely(test_bit(AF_DEGRADED_MODE, &a->flags))) { |
26780d9e BG |
891 | cmd->result = DID_NO_CONNECT << 16; |
892 | cmd->scsi_done(cmd); | |
893 | return 0; | |
894 | } | |
895 | ||
896 | rq = esas2r_alloc_request(a); | |
897 | if (unlikely(rq == NULL)) { | |
898 | esas2r_debug("esas2r_alloc_request failed"); | |
899 | return SCSI_MLQUEUE_HOST_BUSY; | |
900 | } | |
901 | ||
902 | rq->cmd = cmd; | |
903 | bufflen = scsi_bufflen(cmd); | |
904 | ||
905 | if (likely(bufflen != 0)) { | |
906 | if (cmd->sc_data_direction == DMA_TO_DEVICE) | |
907 | rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD); | |
908 | else if (cmd->sc_data_direction == DMA_FROM_DEVICE) | |
909 | rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD); | |
910 | } | |
911 | ||
912 | memcpy(rq->vrq->scsi.cdb, cmd->cmnd, cmd->cmd_len); | |
913 | rq->vrq->scsi.length = cpu_to_le32(bufflen); | |
914 | rq->target_id = cmd->device->id; | |
915 | rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun); | |
916 | rq->sense_buf = cmd->sense_buffer; | |
917 | rq->sense_len = SCSI_SENSE_BUFFERSIZE; | |
918 | ||
919 | esas2r_sgc_init(&sgc, a, rq, NULL); | |
920 | ||
921 | sgc.length = bufflen; | |
922 | sgc.cur_offset = NULL; | |
923 | ||
924 | sgc.cur_sgel = scsi_sglist(cmd); | |
925 | sgc.exp_offset = NULL; | |
926 | sgc.num_sgel = scsi_dma_map(cmd); | |
927 | sgc.sgel_count = 0; | |
928 | ||
929 | if (unlikely(sgc.num_sgel < 0)) { | |
930 | esas2r_free_request(a, rq); | |
931 | return SCSI_MLQUEUE_HOST_BUSY; | |
932 | } | |
933 | ||
934 | sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_from_sgc; | |
935 | ||
936 | if (unlikely(!esas2r_build_sg_list(a, rq, &sgc))) { | |
937 | scsi_dma_unmap(cmd); | |
938 | esas2r_free_request(a, rq); | |
939 | return SCSI_MLQUEUE_HOST_BUSY; | |
940 | } | |
941 | ||
942 | esas2r_debug("start request %p to %d:%d\n", rq, (int)cmd->device->id, | |
943 | (int)cmd->device->lun); | |
944 | ||
945 | esas2r_start_request(a, rq); | |
946 | ||
947 | return 0; | |
948 | } | |
949 | ||
950 | static void complete_task_management_request(struct esas2r_adapter *a, | |
951 | struct esas2r_request *rq) | |
952 | { | |
953 | (*rq->task_management_status_ptr) = rq->req_stat; | |
954 | esas2r_free_request(a, rq); | |
955 | } | |
956 | ||
957 | /** | |
958 | * Searches the specified queue for the specified queue for the command | |
959 | * to abort. | |
960 | * | |
961 | * @param [in] a | |
962 | * @param [in] abort_request | |
963 | * @param [in] cmd | |
964 | * t | |
965 | * @return 0 on failure, 1 if command was not found, 2 if command was found | |
966 | */ | |
967 | static int esas2r_check_active_queue(struct esas2r_adapter *a, | |
968 | struct esas2r_request **abort_request, | |
969 | struct scsi_cmnd *cmd, | |
970 | struct list_head *queue) | |
971 | { | |
972 | bool found = false; | |
973 | struct esas2r_request *ar = *abort_request; | |
974 | struct esas2r_request *rq; | |
975 | struct list_head *element, *next; | |
976 | ||
977 | list_for_each_safe(element, next, queue) { | |
978 | ||
979 | rq = list_entry(element, struct esas2r_request, req_list); | |
980 | ||
981 | if (rq->cmd == cmd) { | |
982 | ||
983 | /* Found the request. See what to do with it. */ | |
984 | if (queue == &a->active_list) { | |
985 | /* | |
986 | * We are searching the active queue, which | |
987 | * means that we need to send an abort request | |
988 | * to the firmware. | |
989 | */ | |
990 | ar = esas2r_alloc_request(a); | |
991 | if (ar == NULL) { | |
992 | esas2r_log_dev(ESAS2R_LOG_WARN, | |
993 | &(a->host->shost_gendev), | |
994 | "unable to allocate an abort request for cmd %p", | |
995 | cmd); | |
996 | return 0; /* Failure */ | |
997 | } | |
998 | ||
999 | /* | |
1000 | * Task management request must be formatted | |
1001 | * with a lock held. | |
1002 | */ | |
1003 | ar->sense_len = 0; | |
1004 | ar->vrq->scsi.length = 0; | |
1005 | ar->target_id = rq->target_id; | |
1006 | ar->vrq->scsi.flags |= cpu_to_le32( | |
1007 | (u8)le32_to_cpu(rq->vrq->scsi.flags)); | |
1008 | ||
1009 | memset(ar->vrq->scsi.cdb, 0, | |
1010 | sizeof(ar->vrq->scsi.cdb)); | |
1011 | ||
1012 | ar->vrq->scsi.flags |= cpu_to_le32( | |
1013 | FCP_CMND_TRM); | |
1014 | ar->vrq->scsi.u.abort_handle = | |
1015 | rq->vrq->scsi.handle; | |
1016 | } else { | |
1017 | /* | |
1018 | * The request is pending but not active on | |
1019 | * the firmware. Just free it now and we'll | |
1020 | * report the successful abort below. | |
1021 | */ | |
1022 | list_del_init(&rq->req_list); | |
1023 | esas2r_free_request(a, rq); | |
1024 | } | |
1025 | ||
1026 | found = true; | |
1027 | break; | |
1028 | } | |
1029 | ||
1030 | } | |
1031 | ||
1032 | if (!found) | |
1033 | return 1; /* Not found */ | |
1034 | ||
1035 | return 2; /* found */ | |
1036 | ||
1037 | ||
1038 | } | |
1039 | ||
1040 | int esas2r_eh_abort(struct scsi_cmnd *cmd) | |
1041 | { | |
1042 | struct esas2r_adapter *a = | |
1043 | (struct esas2r_adapter *)cmd->device->host->hostdata; | |
1044 | struct esas2r_request *abort_request = NULL; | |
1045 | unsigned long flags; | |
1046 | struct list_head *queue; | |
1047 | int result; | |
1048 | ||
1049 | esas2r_log(ESAS2R_LOG_INFO, "eh_abort (%p)", cmd); | |
1050 | ||
9588d24e | 1051 | if (test_bit(AF_DEGRADED_MODE, &a->flags)) { |
26780d9e BG |
1052 | cmd->result = DID_ABORT << 16; |
1053 | ||
1054 | scsi_set_resid(cmd, 0); | |
1055 | ||
1056 | cmd->scsi_done(cmd); | |
1057 | ||
b6c92b7e | 1058 | return SUCCESS; |
26780d9e BG |
1059 | } |
1060 | ||
1061 | spin_lock_irqsave(&a->queue_lock, flags); | |
1062 | ||
1063 | /* | |
1064 | * Run through the defer and active queues looking for the request | |
1065 | * to abort. | |
1066 | */ | |
1067 | ||
1068 | queue = &a->defer_list; | |
1069 | ||
1070 | check_active_queue: | |
1071 | ||
1072 | result = esas2r_check_active_queue(a, &abort_request, cmd, queue); | |
1073 | ||
1074 | if (!result) { | |
1075 | spin_unlock_irqrestore(&a->queue_lock, flags); | |
1076 | return FAILED; | |
1077 | } else if (result == 2 && (queue == &a->defer_list)) { | |
1078 | queue = &a->active_list; | |
1079 | goto check_active_queue; | |
1080 | } | |
1081 | ||
1082 | spin_unlock_irqrestore(&a->queue_lock, flags); | |
1083 | ||
1084 | if (abort_request) { | |
1085 | u8 task_management_status = RS_PENDING; | |
1086 | ||
1087 | /* | |
1088 | * the request is already active, so we need to tell | |
1089 | * the firmware to abort it and wait for the response. | |
1090 | */ | |
1091 | ||
1092 | abort_request->comp_cb = complete_task_management_request; | |
1093 | abort_request->task_management_status_ptr = | |
1094 | &task_management_status; | |
1095 | ||
1096 | esas2r_start_request(a, abort_request); | |
1097 | ||
1098 | if (atomic_read(&a->disable_cnt) == 0) | |
1099 | esas2r_do_deferred_processes(a); | |
1100 | ||
1101 | while (task_management_status == RS_PENDING) | |
1102 | msleep(10); | |
1103 | ||
1104 | /* | |
1105 | * Once we get here, the original request will have been | |
1106 | * completed by the firmware and the abort request will have | |
1107 | * been cleaned up. we're done! | |
1108 | */ | |
1109 | ||
1110 | return SUCCESS; | |
1111 | } | |
1112 | ||
1113 | /* | |
1114 | * If we get here, either we found the inactive request and | |
1115 | * freed it, or we didn't find it at all. Either way, success! | |
1116 | */ | |
1117 | ||
1118 | cmd->result = DID_ABORT << 16; | |
1119 | ||
1120 | scsi_set_resid(cmd, 0); | |
1121 | ||
1122 | cmd->scsi_done(cmd); | |
1123 | ||
1124 | return SUCCESS; | |
1125 | } | |
1126 | ||
1127 | static int esas2r_host_bus_reset(struct scsi_cmnd *cmd, bool host_reset) | |
1128 | { | |
1129 | struct esas2r_adapter *a = | |
1130 | (struct esas2r_adapter *)cmd->device->host->hostdata; | |
1131 | ||
9588d24e | 1132 | if (test_bit(AF_DEGRADED_MODE, &a->flags)) |
26780d9e BG |
1133 | return FAILED; |
1134 | ||
1135 | if (host_reset) | |
1136 | esas2r_reset_adapter(a); | |
1137 | else | |
1138 | esas2r_reset_bus(a); | |
1139 | ||
1140 | /* above call sets the AF_OS_RESET flag. wait for it to clear. */ | |
1141 | ||
9588d24e | 1142 | while (test_bit(AF_OS_RESET, &a->flags)) { |
26780d9e BG |
1143 | msleep(10); |
1144 | ||
9588d24e | 1145 | if (test_bit(AF_DEGRADED_MODE, &a->flags)) |
26780d9e BG |
1146 | return FAILED; |
1147 | } | |
1148 | ||
9588d24e | 1149 | if (test_bit(AF_DEGRADED_MODE, &a->flags)) |
26780d9e BG |
1150 | return FAILED; |
1151 | ||
1152 | return SUCCESS; | |
1153 | } | |
1154 | ||
1155 | int esas2r_host_reset(struct scsi_cmnd *cmd) | |
1156 | { | |
1157 | esas2r_log(ESAS2R_LOG_INFO, "host_reset (%p)", cmd); | |
1158 | ||
1159 | return esas2r_host_bus_reset(cmd, true); | |
1160 | } | |
1161 | ||
1162 | int esas2r_bus_reset(struct scsi_cmnd *cmd) | |
1163 | { | |
1164 | esas2r_log(ESAS2R_LOG_INFO, "bus_reset (%p)", cmd); | |
1165 | ||
1166 | return esas2r_host_bus_reset(cmd, false); | |
1167 | } | |
1168 | ||
1169 | static int esas2r_dev_targ_reset(struct scsi_cmnd *cmd, bool target_reset) | |
1170 | { | |
1171 | struct esas2r_adapter *a = | |
1172 | (struct esas2r_adapter *)cmd->device->host->hostdata; | |
1173 | struct esas2r_request *rq; | |
1174 | u8 task_management_status = RS_PENDING; | |
1175 | bool completed; | |
1176 | ||
9588d24e | 1177 | if (test_bit(AF_DEGRADED_MODE, &a->flags)) |
26780d9e BG |
1178 | return FAILED; |
1179 | ||
1180 | retry: | |
1181 | rq = esas2r_alloc_request(a); | |
1182 | if (rq == NULL) { | |
1183 | if (target_reset) { | |
1184 | esas2r_log(ESAS2R_LOG_CRIT, | |
1185 | "unable to allocate a request for a " | |
1186 | "target reset (%d)!", | |
1187 | cmd->device->id); | |
1188 | } else { | |
1189 | esas2r_log(ESAS2R_LOG_CRIT, | |
1190 | "unable to allocate a request for a " | |
1191 | "device reset (%d:%d)!", | |
1192 | cmd->device->id, | |
1193 | cmd->device->lun); | |
1194 | } | |
1195 | ||
1196 | ||
1197 | return FAILED; | |
1198 | } | |
1199 | ||
1200 | rq->target_id = cmd->device->id; | |
1201 | rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun); | |
1202 | rq->req_stat = RS_PENDING; | |
1203 | ||
1204 | rq->comp_cb = complete_task_management_request; | |
1205 | rq->task_management_status_ptr = &task_management_status; | |
1206 | ||
1207 | if (target_reset) { | |
1208 | esas2r_debug("issuing target reset (%p) to id %d", rq, | |
1209 | cmd->device->id); | |
1210 | completed = esas2r_send_task_mgmt(a, rq, 0x20); | |
1211 | } else { | |
1212 | esas2r_debug("issuing device reset (%p) to id %d lun %d", rq, | |
1213 | cmd->device->id, cmd->device->lun); | |
1214 | completed = esas2r_send_task_mgmt(a, rq, 0x10); | |
1215 | } | |
1216 | ||
1217 | if (completed) { | |
1218 | /* Task management cmd completed right away, need to free it. */ | |
1219 | ||
1220 | esas2r_free_request(a, rq); | |
1221 | } else { | |
1222 | /* | |
1223 | * Wait for firmware to complete the request. Completion | |
1224 | * callback will free it. | |
1225 | */ | |
1226 | while (task_management_status == RS_PENDING) | |
1227 | msleep(10); | |
1228 | } | |
1229 | ||
9588d24e | 1230 | if (test_bit(AF_DEGRADED_MODE, &a->flags)) |
26780d9e BG |
1231 | return FAILED; |
1232 | ||
1233 | if (task_management_status == RS_BUSY) { | |
1234 | /* | |
1235 | * Busy, probably because we are flashing. Wait a bit and | |
1236 | * try again. | |
1237 | */ | |
1238 | msleep(100); | |
1239 | goto retry; | |
1240 | } | |
1241 | ||
1242 | return SUCCESS; | |
1243 | } | |
1244 | ||
1245 | int esas2r_device_reset(struct scsi_cmnd *cmd) | |
1246 | { | |
1247 | esas2r_log(ESAS2R_LOG_INFO, "device_reset (%p)", cmd); | |
1248 | ||
1249 | return esas2r_dev_targ_reset(cmd, false); | |
1250 | ||
1251 | } | |
1252 | ||
1253 | int esas2r_target_reset(struct scsi_cmnd *cmd) | |
1254 | { | |
1255 | esas2r_log(ESAS2R_LOG_INFO, "target_reset (%p)", cmd); | |
1256 | ||
1257 | return esas2r_dev_targ_reset(cmd, true); | |
1258 | } | |
1259 | ||
1260 | int esas2r_change_queue_depth(struct scsi_device *dev, int depth, int reason) | |
1261 | { | |
1262 | esas2r_log(ESAS2R_LOG_INFO, "change_queue_depth %p, %d", dev, depth); | |
1263 | ||
c8b09f6f | 1264 | scsi_adjust_queue_depth(dev, depth); |
26780d9e BG |
1265 | |
1266 | return dev->queue_depth; | |
1267 | } | |
1268 | ||
26780d9e BG |
1269 | void esas2r_log_request_failure(struct esas2r_adapter *a, |
1270 | struct esas2r_request *rq) | |
1271 | { | |
1272 | u8 reqstatus = rq->req_stat; | |
1273 | ||
1274 | if (reqstatus == RS_SUCCESS) | |
1275 | return; | |
1276 | ||
1277 | if (rq->vrq->scsi.function == VDA_FUNC_SCSI) { | |
1278 | if (reqstatus == RS_SCSI_ERROR) { | |
1279 | if (rq->func_rsp.scsi_rsp.sense_len >= 13) { | |
1280 | esas2r_log(ESAS2R_LOG_WARN, | |
1281 | "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x", | |
1282 | rq->sense_buf[2], rq->sense_buf[12], | |
1283 | rq->sense_buf[13], | |
1284 | rq->vrq->scsi.cdb[0]); | |
1285 | } else { | |
1286 | esas2r_log(ESAS2R_LOG_WARN, | |
1287 | "request failure - SCSI error CDB:%x\n", | |
1288 | rq->vrq->scsi.cdb[0]); | |
1289 | } | |
1290 | } else if ((rq->vrq->scsi.cdb[0] != INQUIRY | |
1291 | && rq->vrq->scsi.cdb[0] != REPORT_LUNS) | |
1292 | || (reqstatus != RS_SEL | |
1293 | && reqstatus != RS_SEL2)) { | |
1294 | if ((reqstatus == RS_UNDERRUN) && | |
1295 | (rq->vrq->scsi.cdb[0] == INQUIRY)) { | |
1296 | /* Don't log inquiry underruns */ | |
1297 | } else { | |
1298 | esas2r_log(ESAS2R_LOG_WARN, | |
1299 | "request failure - cdb:%x reqstatus:%d target:%d", | |
1300 | rq->vrq->scsi.cdb[0], reqstatus, | |
1301 | rq->target_id); | |
1302 | } | |
1303 | } | |
1304 | } | |
1305 | } | |
1306 | ||
1307 | void esas2r_wait_request(struct esas2r_adapter *a, struct esas2r_request *rq) | |
1308 | { | |
1309 | u32 starttime; | |
1310 | u32 timeout; | |
1311 | ||
1312 | starttime = jiffies_to_msecs(jiffies); | |
1313 | timeout = rq->timeout ? rq->timeout : 5000; | |
1314 | ||
1315 | while (true) { | |
1316 | esas2r_polled_interrupt(a); | |
1317 | ||
1318 | if (rq->req_stat != RS_STARTED) | |
1319 | break; | |
1320 | ||
1321 | schedule_timeout_interruptible(msecs_to_jiffies(100)); | |
1322 | ||
1323 | if ((jiffies_to_msecs(jiffies) - starttime) > timeout) { | |
1324 | esas2r_hdebug("request TMO"); | |
1325 | esas2r_bugon(); | |
1326 | ||
1327 | rq->req_stat = RS_TIMEOUT; | |
1328 | ||
1329 | esas2r_local_reset_adapter(a); | |
1330 | return; | |
1331 | } | |
1332 | } | |
1333 | } | |
1334 | ||
1335 | u32 esas2r_map_data_window(struct esas2r_adapter *a, u32 addr_lo) | |
1336 | { | |
1337 | u32 offset = addr_lo & (MW_DATA_WINDOW_SIZE - 1); | |
1338 | u32 base = addr_lo & -(signed int)MW_DATA_WINDOW_SIZE; | |
1339 | ||
1340 | if (a->window_base != base) { | |
1341 | esas2r_write_register_dword(a, MVR_PCI_WIN1_REMAP, | |
1342 | base | MVRPW1R_ENABLE); | |
1343 | esas2r_flush_register_dword(a, MVR_PCI_WIN1_REMAP); | |
1344 | a->window_base = base; | |
1345 | } | |
1346 | ||
1347 | return offset; | |
1348 | } | |
1349 | ||
1350 | /* Read a block of data from chip memory */ | |
1351 | bool esas2r_read_mem_block(struct esas2r_adapter *a, | |
1352 | void *to, | |
1353 | u32 from, | |
1354 | u32 size) | |
1355 | { | |
1356 | u8 *end = (u8 *)to; | |
1357 | ||
1358 | while (size) { | |
1359 | u32 len; | |
1360 | u32 offset; | |
1361 | u32 iatvr; | |
1362 | ||
1363 | iatvr = (from & -(signed int)MW_DATA_WINDOW_SIZE); | |
1364 | ||
1365 | esas2r_map_data_window(a, iatvr); | |
1366 | ||
1367 | offset = from & (MW_DATA_WINDOW_SIZE - 1); | |
1368 | len = size; | |
1369 | ||
1370 | if (len > MW_DATA_WINDOW_SIZE - offset) | |
1371 | len = MW_DATA_WINDOW_SIZE - offset; | |
1372 | ||
1373 | from += len; | |
1374 | size -= len; | |
1375 | ||
1376 | while (len--) { | |
1377 | *end++ = esas2r_read_data_byte(a, offset); | |
1378 | offset++; | |
1379 | } | |
1380 | } | |
1381 | ||
1382 | return true; | |
1383 | } | |
1384 | ||
1385 | void esas2r_nuxi_mgt_data(u8 function, void *data) | |
1386 | { | |
1387 | struct atto_vda_grp_info *g; | |
1388 | struct atto_vda_devinfo *d; | |
1389 | struct atto_vdapart_info *p; | |
1390 | struct atto_vda_dh_info *h; | |
1391 | struct atto_vda_metrics_info *m; | |
1392 | struct atto_vda_schedule_info *s; | |
1393 | struct atto_vda_buzzer_info *b; | |
1394 | u8 i; | |
1395 | ||
1396 | switch (function) { | |
1397 | case VDAMGT_BUZZER_INFO: | |
1398 | case VDAMGT_BUZZER_SET: | |
1399 | ||
1400 | b = (struct atto_vda_buzzer_info *)data; | |
1401 | ||
1402 | b->duration = le32_to_cpu(b->duration); | |
1403 | break; | |
1404 | ||
1405 | case VDAMGT_SCHEDULE_INFO: | |
1406 | case VDAMGT_SCHEDULE_EVENT: | |
1407 | ||
1408 | s = (struct atto_vda_schedule_info *)data; | |
1409 | ||
1410 | s->id = le32_to_cpu(s->id); | |
1411 | ||
1412 | break; | |
1413 | ||
1414 | case VDAMGT_DEV_INFO: | |
1415 | case VDAMGT_DEV_CLEAN: | |
1416 | case VDAMGT_DEV_PT_INFO: | |
1417 | case VDAMGT_DEV_FEATURES: | |
1418 | case VDAMGT_DEV_PT_FEATURES: | |
1419 | case VDAMGT_DEV_OPERATION: | |
1420 | ||
1421 | d = (struct atto_vda_devinfo *)data; | |
1422 | ||
1423 | d->capacity = le64_to_cpu(d->capacity); | |
1424 | d->block_size = le32_to_cpu(d->block_size); | |
1425 | d->ses_dev_index = le16_to_cpu(d->ses_dev_index); | |
1426 | d->target_id = le16_to_cpu(d->target_id); | |
1427 | d->lun = le16_to_cpu(d->lun); | |
1428 | d->features = le16_to_cpu(d->features); | |
1429 | break; | |
1430 | ||
1431 | case VDAMGT_GRP_INFO: | |
1432 | case VDAMGT_GRP_CREATE: | |
1433 | case VDAMGT_GRP_DELETE: | |
1434 | case VDAMGT_ADD_STORAGE: | |
1435 | case VDAMGT_MEMBER_ADD: | |
1436 | case VDAMGT_GRP_COMMIT: | |
1437 | case VDAMGT_GRP_REBUILD: | |
1438 | case VDAMGT_GRP_COMMIT_INIT: | |
1439 | case VDAMGT_QUICK_RAID: | |
1440 | case VDAMGT_GRP_FEATURES: | |
1441 | case VDAMGT_GRP_COMMIT_INIT_AUTOMAP: | |
1442 | case VDAMGT_QUICK_RAID_INIT_AUTOMAP: | |
1443 | case VDAMGT_SPARE_LIST: | |
1444 | case VDAMGT_SPARE_ADD: | |
1445 | case VDAMGT_SPARE_REMOVE: | |
1446 | case VDAMGT_LOCAL_SPARE_ADD: | |
1447 | case VDAMGT_GRP_OPERATION: | |
1448 | ||
1449 | g = (struct atto_vda_grp_info *)data; | |
1450 | ||
1451 | g->capacity = le64_to_cpu(g->capacity); | |
1452 | g->block_size = le32_to_cpu(g->block_size); | |
1453 | g->interleave = le32_to_cpu(g->interleave); | |
1454 | g->features = le16_to_cpu(g->features); | |
1455 | ||
1456 | for (i = 0; i < 32; i++) | |
1457 | g->members[i] = le16_to_cpu(g->members[i]); | |
1458 | ||
1459 | break; | |
1460 | ||
1461 | case VDAMGT_PART_INFO: | |
1462 | case VDAMGT_PART_MAP: | |
1463 | case VDAMGT_PART_UNMAP: | |
1464 | case VDAMGT_PART_AUTOMAP: | |
1465 | case VDAMGT_PART_SPLIT: | |
1466 | case VDAMGT_PART_MERGE: | |
1467 | ||
1468 | p = (struct atto_vdapart_info *)data; | |
1469 | ||
1470 | p->part_size = le64_to_cpu(p->part_size); | |
1471 | p->start_lba = le32_to_cpu(p->start_lba); | |
1472 | p->block_size = le32_to_cpu(p->block_size); | |
1473 | p->target_id = le16_to_cpu(p->target_id); | |
1474 | break; | |
1475 | ||
1476 | case VDAMGT_DEV_HEALTH_REQ: | |
1477 | ||
1478 | h = (struct atto_vda_dh_info *)data; | |
1479 | ||
1480 | h->med_defect_cnt = le32_to_cpu(h->med_defect_cnt); | |
1481 | h->info_exc_cnt = le32_to_cpu(h->info_exc_cnt); | |
1482 | break; | |
1483 | ||
1484 | case VDAMGT_DEV_METRICS: | |
1485 | ||
1486 | m = (struct atto_vda_metrics_info *)data; | |
1487 | ||
1488 | for (i = 0; i < 32; i++) | |
1489 | m->dev_indexes[i] = le16_to_cpu(m->dev_indexes[i]); | |
1490 | ||
1491 | break; | |
1492 | ||
1493 | default: | |
1494 | break; | |
1495 | } | |
1496 | } | |
1497 | ||
1498 | void esas2r_nuxi_cfg_data(u8 function, void *data) | |
1499 | { | |
1500 | struct atto_vda_cfg_init *ci; | |
1501 | ||
1502 | switch (function) { | |
1503 | case VDA_CFG_INIT: | |
1504 | case VDA_CFG_GET_INIT: | |
1505 | case VDA_CFG_GET_INIT2: | |
1506 | ||
1507 | ci = (struct atto_vda_cfg_init *)data; | |
1508 | ||
1509 | ci->date_time.year = le16_to_cpu(ci->date_time.year); | |
1510 | ci->sgl_page_size = le32_to_cpu(ci->sgl_page_size); | |
1511 | ci->vda_version = le32_to_cpu(ci->vda_version); | |
1512 | ci->epoch_time = le32_to_cpu(ci->epoch_time); | |
1513 | ci->ioctl_tunnel = le32_to_cpu(ci->ioctl_tunnel); | |
1514 | ci->num_targets_backend = le32_to_cpu(ci->num_targets_backend); | |
1515 | break; | |
1516 | ||
1517 | default: | |
1518 | break; | |
1519 | } | |
1520 | } | |
1521 | ||
1522 | void esas2r_nuxi_ae_data(union atto_vda_ae *ae) | |
1523 | { | |
1524 | struct atto_vda_ae_raid *r = &ae->raid; | |
1525 | struct atto_vda_ae_lu *l = &ae->lu; | |
1526 | ||
1527 | switch (ae->hdr.bytype) { | |
1528 | case VDAAE_HDR_TYPE_RAID: | |
1529 | ||
1530 | r->dwflags = le32_to_cpu(r->dwflags); | |
1531 | break; | |
1532 | ||
1533 | case VDAAE_HDR_TYPE_LU: | |
1534 | ||
1535 | l->dwevent = le32_to_cpu(l->dwevent); | |
1536 | l->wphys_target_id = le16_to_cpu(l->wphys_target_id); | |
1537 | l->id.tgtlun.wtarget_id = le16_to_cpu(l->id.tgtlun.wtarget_id); | |
1538 | ||
1539 | if (l->hdr.bylength >= offsetof(struct atto_vda_ae_lu, id) | |
1540 | + sizeof(struct atto_vda_ae_lu_tgt_lun_raid)) { | |
1541 | l->id.tgtlun_raid.dwinterleave | |
1542 | = le32_to_cpu(l->id.tgtlun_raid.dwinterleave); | |
1543 | l->id.tgtlun_raid.dwblock_size | |
1544 | = le32_to_cpu(l->id.tgtlun_raid.dwblock_size); | |
1545 | } | |
1546 | ||
1547 | break; | |
1548 | ||
1549 | case VDAAE_HDR_TYPE_DISK: | |
1550 | default: | |
1551 | break; | |
1552 | } | |
1553 | } | |
1554 | ||
1555 | void esas2r_free_request(struct esas2r_adapter *a, struct esas2r_request *rq) | |
1556 | { | |
1557 | unsigned long flags; | |
1558 | ||
1559 | esas2r_rq_destroy_request(rq, a); | |
1560 | spin_lock_irqsave(&a->request_lock, flags); | |
1561 | list_add(&rq->comp_list, &a->avail_request); | |
1562 | spin_unlock_irqrestore(&a->request_lock, flags); | |
1563 | } | |
1564 | ||
1565 | struct esas2r_request *esas2r_alloc_request(struct esas2r_adapter *a) | |
1566 | { | |
1567 | struct esas2r_request *rq; | |
1568 | unsigned long flags; | |
1569 | ||
1570 | spin_lock_irqsave(&a->request_lock, flags); | |
1571 | ||
1572 | if (unlikely(list_empty(&a->avail_request))) { | |
1573 | spin_unlock_irqrestore(&a->request_lock, flags); | |
1574 | return NULL; | |
1575 | } | |
1576 | ||
1577 | rq = list_first_entry(&a->avail_request, struct esas2r_request, | |
1578 | comp_list); | |
1579 | list_del(&rq->comp_list); | |
1580 | spin_unlock_irqrestore(&a->request_lock, flags); | |
1581 | esas2r_rq_init_request(rq, a); | |
1582 | ||
1583 | return rq; | |
1584 | ||
1585 | } | |
1586 | ||
1587 | void esas2r_complete_request_cb(struct esas2r_adapter *a, | |
1588 | struct esas2r_request *rq) | |
1589 | { | |
1590 | esas2r_debug("completing request %p\n", rq); | |
1591 | ||
1592 | scsi_dma_unmap(rq->cmd); | |
1593 | ||
1594 | if (unlikely(rq->req_stat != RS_SUCCESS)) { | |
1595 | esas2r_debug("[%x STATUS %x:%x (%x)]", rq->target_id, | |
1596 | rq->req_stat, | |
1597 | rq->func_rsp.scsi_rsp.scsi_stat, | |
1598 | rq->cmd); | |
1599 | ||
1600 | rq->cmd->result = | |
1601 | ((esas2r_req_status_to_error(rq->req_stat) << 16) | |
1602 | | (rq->func_rsp.scsi_rsp.scsi_stat & STATUS_MASK)); | |
1603 | ||
1604 | if (rq->req_stat == RS_UNDERRUN) | |
1605 | scsi_set_resid(rq->cmd, | |
1606 | le32_to_cpu(rq->func_rsp.scsi_rsp. | |
1607 | residual_length)); | |
1608 | else | |
1609 | scsi_set_resid(rq->cmd, 0); | |
1610 | } | |
1611 | ||
1612 | rq->cmd->scsi_done(rq->cmd); | |
1613 | ||
1614 | esas2r_free_request(a, rq); | |
1615 | } | |
1616 | ||
1617 | /* Run tasklet to handle stuff outside of interrupt context. */ | |
1618 | void esas2r_adapter_tasklet(unsigned long context) | |
1619 | { | |
1620 | struct esas2r_adapter *a = (struct esas2r_adapter *)context; | |
1621 | ||
9588d24e BG |
1622 | if (unlikely(test_bit(AF2_TIMER_TICK, &a->flags2))) { |
1623 | clear_bit(AF2_TIMER_TICK, &a->flags2); | |
26780d9e BG |
1624 | esas2r_timer_tick(a); |
1625 | } | |
1626 | ||
9588d24e BG |
1627 | if (likely(test_bit(AF2_INT_PENDING, &a->flags2))) { |
1628 | clear_bit(AF2_INT_PENDING, &a->flags2); | |
26780d9e BG |
1629 | esas2r_adapter_interrupt(a); |
1630 | } | |
1631 | ||
1632 | if (esas2r_is_tasklet_pending(a)) | |
1633 | esas2r_do_tasklet_tasks(a); | |
1634 | ||
1635 | if (esas2r_is_tasklet_pending(a) | |
9588d24e BG |
1636 | || (test_bit(AF2_INT_PENDING, &a->flags2)) |
1637 | || (test_bit(AF2_TIMER_TICK, &a->flags2))) { | |
1638 | clear_bit(AF_TASKLET_SCHEDULED, &a->flags); | |
26780d9e BG |
1639 | esas2r_schedule_tasklet(a); |
1640 | } else { | |
9588d24e | 1641 | clear_bit(AF_TASKLET_SCHEDULED, &a->flags); |
26780d9e BG |
1642 | } |
1643 | } | |
1644 | ||
1645 | static void esas2r_timer_callback(unsigned long context); | |
1646 | ||
1647 | void esas2r_kickoff_timer(struct esas2r_adapter *a) | |
1648 | { | |
1649 | init_timer(&a->timer); | |
1650 | ||
1651 | a->timer.function = esas2r_timer_callback; | |
1652 | a->timer.data = (unsigned long)a; | |
1653 | a->timer.expires = jiffies + | |
1654 | msecs_to_jiffies(100); | |
1655 | ||
1656 | add_timer(&a->timer); | |
1657 | } | |
1658 | ||
1659 | static void esas2r_timer_callback(unsigned long context) | |
1660 | { | |
1661 | struct esas2r_adapter *a = (struct esas2r_adapter *)context; | |
1662 | ||
9588d24e | 1663 | set_bit(AF2_TIMER_TICK, &a->flags2); |
26780d9e BG |
1664 | |
1665 | esas2r_schedule_tasklet(a); | |
1666 | ||
1667 | esas2r_kickoff_timer(a); | |
1668 | } | |
1669 | ||
1670 | /* | |
1671 | * Firmware events need to be handled outside of interrupt context | |
1672 | * so we schedule a delayed_work to handle them. | |
1673 | */ | |
1674 | ||
1675 | static void | |
1676 | esas2r_free_fw_event(struct esas2r_fw_event_work *fw_event) | |
1677 | { | |
1678 | unsigned long flags; | |
1679 | struct esas2r_adapter *a = fw_event->a; | |
1680 | ||
1681 | spin_lock_irqsave(&a->fw_event_lock, flags); | |
1682 | list_del(&fw_event->list); | |
1683 | kfree(fw_event); | |
1684 | spin_unlock_irqrestore(&a->fw_event_lock, flags); | |
1685 | } | |
1686 | ||
1687 | void | |
1688 | esas2r_fw_event_off(struct esas2r_adapter *a) | |
1689 | { | |
1690 | unsigned long flags; | |
1691 | ||
1692 | spin_lock_irqsave(&a->fw_event_lock, flags); | |
1693 | a->fw_events_off = 1; | |
1694 | spin_unlock_irqrestore(&a->fw_event_lock, flags); | |
1695 | } | |
1696 | ||
1697 | void | |
1698 | esas2r_fw_event_on(struct esas2r_adapter *a) | |
1699 | { | |
1700 | unsigned long flags; | |
1701 | ||
1702 | spin_lock_irqsave(&a->fw_event_lock, flags); | |
1703 | a->fw_events_off = 0; | |
1704 | spin_unlock_irqrestore(&a->fw_event_lock, flags); | |
1705 | } | |
1706 | ||
1707 | static void esas2r_add_device(struct esas2r_adapter *a, u16 target_id) | |
1708 | { | |
1709 | int ret; | |
1710 | struct scsi_device *scsi_dev; | |
1711 | ||
1712 | scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0); | |
1713 | ||
1714 | if (scsi_dev) { | |
1715 | esas2r_log_dev( | |
1716 | ESAS2R_LOG_WARN, | |
1717 | &(scsi_dev-> | |
1718 | sdev_gendev), | |
1719 | "scsi device already exists at id %d", target_id); | |
1720 | ||
1721 | scsi_device_put(scsi_dev); | |
1722 | } else { | |
1723 | esas2r_log_dev( | |
1724 | ESAS2R_LOG_INFO, | |
1725 | &(a->host-> | |
1726 | shost_gendev), | |
1727 | "scsi_add_device() called for 0:%d:0", | |
1728 | target_id); | |
1729 | ||
1730 | ret = scsi_add_device(a->host, 0, target_id, 0); | |
1731 | if (ret) { | |
1732 | esas2r_log_dev( | |
1733 | ESAS2R_LOG_CRIT, | |
1734 | &(a->host-> | |
1735 | shost_gendev), | |
1736 | "scsi_add_device failed with %d for id %d", | |
1737 | ret, target_id); | |
1738 | } | |
1739 | } | |
1740 | } | |
1741 | ||
1742 | static void esas2r_remove_device(struct esas2r_adapter *a, u16 target_id) | |
1743 | { | |
1744 | struct scsi_device *scsi_dev; | |
1745 | ||
1746 | scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0); | |
1747 | ||
1748 | if (scsi_dev) { | |
1749 | scsi_device_set_state(scsi_dev, SDEV_OFFLINE); | |
1750 | ||
1751 | esas2r_log_dev( | |
1752 | ESAS2R_LOG_INFO, | |
1753 | &(scsi_dev-> | |
1754 | sdev_gendev), | |
1755 | "scsi_remove_device() called for 0:%d:0", | |
1756 | target_id); | |
1757 | ||
1758 | scsi_remove_device(scsi_dev); | |
1759 | ||
1760 | esas2r_log_dev( | |
1761 | ESAS2R_LOG_INFO, | |
1762 | &(scsi_dev-> | |
1763 | sdev_gendev), | |
1764 | "scsi_device_put() called"); | |
1765 | ||
1766 | scsi_device_put(scsi_dev); | |
1767 | } else { | |
1768 | esas2r_log_dev( | |
1769 | ESAS2R_LOG_WARN, | |
1770 | &(a->host->shost_gendev), | |
1771 | "no target found at id %d", | |
1772 | target_id); | |
1773 | } | |
1774 | } | |
1775 | ||
1776 | /* | |
1777 | * Sends a firmware asynchronous event to anyone who happens to be | |
1778 | * listening on the defined ATTO VDA event ports. | |
1779 | */ | |
1780 | static void esas2r_send_ae_event(struct esas2r_fw_event_work *fw_event) | |
1781 | { | |
1782 | struct esas2r_vda_ae *ae = (struct esas2r_vda_ae *)fw_event->data; | |
1783 | char *type; | |
1784 | ||
1785 | switch (ae->vda_ae.hdr.bytype) { | |
1786 | case VDAAE_HDR_TYPE_RAID: | |
1787 | type = "RAID group state change"; | |
1788 | break; | |
1789 | ||
1790 | case VDAAE_HDR_TYPE_LU: | |
1791 | type = "Mapped destination LU change"; | |
1792 | break; | |
1793 | ||
1794 | case VDAAE_HDR_TYPE_DISK: | |
1795 | type = "Physical disk inventory change"; | |
1796 | break; | |
1797 | ||
1798 | case VDAAE_HDR_TYPE_RESET: | |
1799 | type = "Firmware reset"; | |
1800 | break; | |
1801 | ||
1802 | case VDAAE_HDR_TYPE_LOG_INFO: | |
1803 | type = "Event Log message (INFO level)"; | |
1804 | break; | |
1805 | ||
1806 | case VDAAE_HDR_TYPE_LOG_WARN: | |
1807 | type = "Event Log message (WARN level)"; | |
1808 | break; | |
1809 | ||
1810 | case VDAAE_HDR_TYPE_LOG_CRIT: | |
1811 | type = "Event Log message (CRIT level)"; | |
1812 | break; | |
1813 | ||
1814 | case VDAAE_HDR_TYPE_LOG_FAIL: | |
1815 | type = "Event Log message (FAIL level)"; | |
1816 | break; | |
1817 | ||
1818 | case VDAAE_HDR_TYPE_NVC: | |
1819 | type = "NVCache change"; | |
1820 | break; | |
1821 | ||
1822 | case VDAAE_HDR_TYPE_TLG_INFO: | |
1823 | type = "Time stamped log message (INFO level)"; | |
1824 | break; | |
1825 | ||
1826 | case VDAAE_HDR_TYPE_TLG_WARN: | |
1827 | type = "Time stamped log message (WARN level)"; | |
1828 | break; | |
1829 | ||
1830 | case VDAAE_HDR_TYPE_TLG_CRIT: | |
1831 | type = "Time stamped log message (CRIT level)"; | |
1832 | break; | |
1833 | ||
1834 | case VDAAE_HDR_TYPE_PWRMGT: | |
1835 | type = "Power management"; | |
1836 | break; | |
1837 | ||
1838 | case VDAAE_HDR_TYPE_MUTE: | |
1839 | type = "Mute button pressed"; | |
1840 | break; | |
1841 | ||
1842 | case VDAAE_HDR_TYPE_DEV: | |
1843 | type = "Device attribute change"; | |
1844 | break; | |
1845 | ||
1846 | default: | |
1847 | type = "Unknown"; | |
1848 | break; | |
1849 | } | |
1850 | ||
1851 | esas2r_log(ESAS2R_LOG_WARN, | |
1852 | "An async event of type \"%s\" was received from the firmware. The event contents are:", | |
1853 | type); | |
1854 | esas2r_log_hexdump(ESAS2R_LOG_WARN, &ae->vda_ae, | |
1855 | ae->vda_ae.hdr.bylength); | |
1856 | ||
1857 | } | |
1858 | ||
1859 | static void | |
1860 | esas2r_firmware_event_work(struct work_struct *work) | |
1861 | { | |
1862 | struct esas2r_fw_event_work *fw_event = | |
1863 | container_of(work, struct esas2r_fw_event_work, work.work); | |
1864 | ||
1865 | struct esas2r_adapter *a = fw_event->a; | |
1866 | ||
1867 | u16 target_id = *(u16 *)&fw_event->data[0]; | |
1868 | ||
1869 | if (a->fw_events_off) | |
1870 | goto done; | |
1871 | ||
1872 | switch (fw_event->type) { | |
1873 | case fw_event_null: | |
1874 | break; /* do nothing */ | |
1875 | ||
1876 | case fw_event_lun_change: | |
1877 | esas2r_remove_device(a, target_id); | |
1878 | esas2r_add_device(a, target_id); | |
1879 | break; | |
1880 | ||
1881 | case fw_event_present: | |
1882 | esas2r_add_device(a, target_id); | |
1883 | break; | |
1884 | ||
1885 | case fw_event_not_present: | |
1886 | esas2r_remove_device(a, target_id); | |
1887 | break; | |
1888 | ||
1889 | case fw_event_vda_ae: | |
1890 | esas2r_send_ae_event(fw_event); | |
1891 | break; | |
1892 | } | |
1893 | ||
1894 | done: | |
1895 | esas2r_free_fw_event(fw_event); | |
1896 | } | |
1897 | ||
1898 | void esas2r_queue_fw_event(struct esas2r_adapter *a, | |
1899 | enum fw_event_type type, | |
1900 | void *data, | |
1901 | int data_sz) | |
1902 | { | |
1903 | struct esas2r_fw_event_work *fw_event; | |
1904 | unsigned long flags; | |
1905 | ||
1906 | fw_event = kzalloc(sizeof(struct esas2r_fw_event_work), GFP_ATOMIC); | |
1907 | if (!fw_event) { | |
1908 | esas2r_log(ESAS2R_LOG_WARN, | |
1909 | "esas2r_queue_fw_event failed to alloc"); | |
1910 | return; | |
1911 | } | |
1912 | ||
1913 | if (type == fw_event_vda_ae) { | |
1914 | struct esas2r_vda_ae *ae = | |
1915 | (struct esas2r_vda_ae *)fw_event->data; | |
1916 | ||
1917 | ae->signature = ESAS2R_VDA_EVENT_SIG; | |
1918 | ae->bus_number = a->pcid->bus->number; | |
1919 | ae->devfn = a->pcid->devfn; | |
1920 | memcpy(&ae->vda_ae, data, sizeof(ae->vda_ae)); | |
1921 | } else { | |
1922 | memcpy(fw_event->data, data, data_sz); | |
1923 | } | |
1924 | ||
1925 | fw_event->type = type; | |
1926 | fw_event->a = a; | |
1927 | ||
1928 | spin_lock_irqsave(&a->fw_event_lock, flags); | |
1929 | list_add_tail(&fw_event->list, &a->fw_event_list); | |
1930 | INIT_DELAYED_WORK(&fw_event->work, esas2r_firmware_event_work); | |
1931 | queue_delayed_work_on( | |
1932 | smp_processor_id(), a->fw_event_q, &fw_event->work, | |
1933 | msecs_to_jiffies(1)); | |
1934 | spin_unlock_irqrestore(&a->fw_event_lock, flags); | |
1935 | } | |
1936 | ||
1937 | void esas2r_target_state_changed(struct esas2r_adapter *a, u16 targ_id, | |
1938 | u8 state) | |
1939 | { | |
1940 | if (state == TS_LUN_CHANGE) | |
1941 | esas2r_queue_fw_event(a, fw_event_lun_change, &targ_id, | |
1942 | sizeof(targ_id)); | |
1943 | else if (state == TS_PRESENT) | |
1944 | esas2r_queue_fw_event(a, fw_event_present, &targ_id, | |
1945 | sizeof(targ_id)); | |
1946 | else if (state == TS_NOT_PRESENT) | |
1947 | esas2r_queue_fw_event(a, fw_event_not_present, &targ_id, | |
1948 | sizeof(targ_id)); | |
1949 | } | |
1950 | ||
1951 | /* Translate status to a Linux SCSI mid-layer error code */ | |
1952 | int esas2r_req_status_to_error(u8 req_stat) | |
1953 | { | |
1954 | switch (req_stat) { | |
1955 | case RS_OVERRUN: | |
1956 | case RS_UNDERRUN: | |
1957 | case RS_SUCCESS: | |
1958 | /* | |
1959 | * NOTE: SCSI mid-layer wants a good status for a SCSI error, because | |
1960 | * it will check the scsi_stat value in the completion anyway. | |
1961 | */ | |
1962 | case RS_SCSI_ERROR: | |
1963 | return DID_OK; | |
1964 | ||
1965 | case RS_SEL: | |
1966 | case RS_SEL2: | |
1967 | return DID_NO_CONNECT; | |
1968 | ||
1969 | case RS_RESET: | |
1970 | return DID_RESET; | |
1971 | ||
1972 | case RS_ABORTED: | |
1973 | return DID_ABORT; | |
1974 | ||
1975 | case RS_BUSY: | |
1976 | return DID_BUS_BUSY; | |
1977 | } | |
1978 | ||
1979 | /* everything else is just an error. */ | |
1980 | ||
1981 | return DID_ERROR; | |
1982 | } | |
1983 | ||
1984 | module_init(esas2r_init); | |
1985 | module_exit(esas2r_exit); |