Merge tag 'linux-kselftest-5.2-rc3' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / drivers / scsi / megaraid / megaraid_mbox.c
CommitLineData
1da177e4
LT
1/*
2 *
3 * Linux MegaRAID device driver
4 *
5 * Copyright (c) 2003-2004 LSI Logic Corporation.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * FILE : megaraid_mbox.c
cd96d96f 13 * Version : v2.20.5.1 (Nov 16 2006)
1da177e4
LT
14 *
15 * Authors:
cd96d96f
SP
16 * Atul Mukker <Atul.Mukker@lsi.com>
17 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsi.com>
18 * Manoj Jose <Manoj.Jose@lsi.com>
19 * Seokmann Ju
1da177e4
LT
20 *
21 * List of supported controllers
22 *
23 * OEM Product Name VID DID SSVID SSID
24 * --- ------------ --- --- ---- ----
25 * Dell PERC3/QC 101E 1960 1028 0471
26 * Dell PERC3/DC 101E 1960 1028 0493
27 * Dell PERC3/SC 101E 1960 1028 0475
28 * Dell PERC3/Di 1028 1960 1028 0123
29 * Dell PERC4/SC 1000 1960 1028 0520
30 * Dell PERC4/DC 1000 1960 1028 0518
31 * Dell PERC4/QC 1000 0407 1028 0531
32 * Dell PERC4/Di 1028 000F 1028 014A
33 * Dell PERC 4e/Si 1028 0013 1028 016c
34 * Dell PERC 4e/Di 1028 0013 1028 016d
35 * Dell PERC 4e/Di 1028 0013 1028 016e
36 * Dell PERC 4e/Di 1028 0013 1028 016f
37 * Dell PERC 4e/Di 1028 0013 1028 0170
38 * Dell PERC 4e/DC 1000 0408 1028 0002
39 * Dell PERC 4e/SC 1000 0408 1028 0001
40 *
41 *
42 * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
43 * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
44 * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
45 * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
46 * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
47 * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
48 * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
49 * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
50 * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
51 * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
52 * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
53 * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
54 *
55 * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
56 * INTEL RAID Controller SRCS16 1000 1960 8086 0523
57 * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
58 * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
59 * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
60 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
61 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
62 * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
63 *
64 * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
65 *
66 * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
67 *
68 * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
69 *
395cf969 70 * For history of changes, see Documentation/scsi/ChangeLog.megaraid
1da177e4
LT
71 */
72
5a0e3ad6 73#include <linux/slab.h>
acf3368f 74#include <linux/module.h>
1da177e4
LT
75#include "megaraid_mbox.h"
76
77static int megaraid_init(void);
78static void megaraid_exit(void);
79
80static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
81static void megaraid_detach_one(struct pci_dev *);
5457b6a6 82static void megaraid_mbox_shutdown(struct pci_dev *);
1da177e4
LT
83
84static int megaraid_io_attach(adapter_t *);
85static void megaraid_io_detach(adapter_t *);
86
87static int megaraid_init_mbox(adapter_t *);
88static void megaraid_fini_mbox(adapter_t *);
89
90static int megaraid_alloc_cmd_packets(adapter_t *);
91static void megaraid_free_cmd_packets(adapter_t *);
92
93static int megaraid_mbox_setup_dma_pools(adapter_t *);
94static void megaraid_mbox_teardown_dma_pools(adapter_t *);
95
96static int megaraid_sysfs_alloc_resources(adapter_t *);
97static void megaraid_sysfs_free_resources(adapter_t *);
98
99static int megaraid_abort_handler(struct scsi_cmnd *);
100static int megaraid_reset_handler(struct scsi_cmnd *);
101
102static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
103static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
104static int megaraid_busywait_mbox(mraid_device_t *);
105static int megaraid_mbox_product_info(adapter_t *);
106static int megaraid_mbox_extended_cdb(adapter_t *);
107static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
108static int megaraid_mbox_support_random_del(adapter_t *);
109static int megaraid_mbox_get_max_sg(adapter_t *);
110static void megaraid_mbox_enum_raid_scsi(adapter_t *);
111static void megaraid_mbox_flush_cache(adapter_t *);
cd96d96f 112static int megaraid_mbox_fire_sync_cmd(adapter_t *);
1da177e4
LT
113
114static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
115static void megaraid_mbox_setup_device_map(adapter_t *);
116
f281233d 117static int megaraid_queue_command(struct Scsi_Host *, struct scsi_cmnd *);
1da177e4
LT
118static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
119static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
120static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
121 struct scsi_cmnd *);
122static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
123 struct scsi_cmnd *);
124
7d12e780 125static irqreturn_t megaraid_isr(int, void *);
1da177e4
LT
126
127static void megaraid_mbox_dpc(unsigned long);
128
ee959b00 129static ssize_t megaraid_sysfs_show_app_hndl(struct device *, struct device_attribute *attr, char *);
10523b3b 130static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
1da177e4
LT
131
132static int megaraid_cmm_register(adapter_t *);
133static int megaraid_cmm_unregister(adapter_t *);
134static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
135static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
136static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
137static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
138static int wait_till_fw_empty(adapter_t *);
139
140
141
cd96d96f 142MODULE_AUTHOR("megaraidlinux@lsi.com");
1da177e4
LT
143MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
144MODULE_LICENSE("GPL");
145MODULE_VERSION(MEGARAID_VERSION);
146
147/*
148 * ### modules parameters for driver ###
149 */
150
a69b74d3 151/*
1da177e4
LT
152 * Set to enable driver to expose unconfigured disk to kernel
153 */
154static int megaraid_expose_unconf_disks = 0;
155module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
156MODULE_PARM_DESC(unconf_disks,
157 "Set to expose unconfigured disks to kernel (default=0)");
158
a69b74d3 159/*
1da177e4
LT
160 * driver wait time if the adapter's mailbox is busy
161 */
162static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
163module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
164MODULE_PARM_DESC(busy_wait,
165 "Max wait for mailbox in microseconds if busy (default=10)");
166
a69b74d3 167/*
1da177e4
LT
168 * number of sectors per IO command
169 */
170static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
171module_param_named(max_sectors, megaraid_max_sectors, int, 0);
172MODULE_PARM_DESC(max_sectors,
173 "Maximum number of sectors per IO command (default=128)");
174
a69b74d3 175/*
1da177e4
LT
176 * number of commands per logical unit
177 */
178static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
179module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
180MODULE_PARM_DESC(cmd_per_lun,
181 "Maximum number of commands per logical unit (default=64)");
182
183
a69b74d3 184/*
1da177e4
LT
185 * Fast driver load option, skip scanning for physical devices during load.
186 * This would result in non-disk devices being skipped during driver load
187 * time. These can be later added though, using /proc/scsi/scsi
188 */
189static unsigned int megaraid_fast_load = 0;
190module_param_named(fast_load, megaraid_fast_load, int, 0);
191MODULE_PARM_DESC(fast_load,
192 "Faster loading of the driver, skips physical devices! (default=0)");
193
194
a69b74d3 195/*
1da177e4
LT
196 * mraid_debug level - threshold for amount of information to be displayed by
197 * the driver. This level can be changed through modules parameters, ioctl or
198 * sysfs/proc interface. By default, print the announcement messages only.
199 */
200int mraid_debug_level = CL_ANN;
201module_param_named(debug_level, mraid_debug_level, int, 0);
202MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
203
1da177e4
LT
204/*
205 * PCI table for all supported controllers.
206 */
207static struct pci_device_id pci_id_table_g[] = {
208 {
209 PCI_VENDOR_ID_DELL,
210 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
211 PCI_VENDOR_ID_DELL,
212 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
213 },
214 {
215 PCI_VENDOR_ID_LSI_LOGIC,
216 PCI_DEVICE_ID_PERC4_SC,
217 PCI_VENDOR_ID_DELL,
218 PCI_SUBSYS_ID_PERC4_SC,
219 },
220 {
221 PCI_VENDOR_ID_LSI_LOGIC,
222 PCI_DEVICE_ID_PERC4_DC,
223 PCI_VENDOR_ID_DELL,
224 PCI_SUBSYS_ID_PERC4_DC,
225 },
226 {
227 PCI_VENDOR_ID_LSI_LOGIC,
672b2d38
JS
228 PCI_DEVICE_ID_VERDE,
229 PCI_ANY_ID,
230 PCI_ANY_ID,
1da177e4
LT
231 },
232 {
233 PCI_VENDOR_ID_DELL,
234 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
235 PCI_VENDOR_ID_DELL,
236 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
237 },
238 {
239 PCI_VENDOR_ID_DELL,
240 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
241 PCI_VENDOR_ID_DELL,
242 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
243 },
244 {
245 PCI_VENDOR_ID_DELL,
246 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
247 PCI_VENDOR_ID_DELL,
248 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
249 },
250 {
251 PCI_VENDOR_ID_DELL,
252 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
253 PCI_VENDOR_ID_DELL,
254 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
255 },
256 {
257 PCI_VENDOR_ID_DELL,
258 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
259 PCI_VENDOR_ID_DELL,
260 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
261 },
262 {
263 PCI_VENDOR_ID_DELL,
264 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
265 PCI_VENDOR_ID_DELL,
266 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
267 },
268 {
269 PCI_VENDOR_ID_LSI_LOGIC,
672b2d38
JS
270 PCI_DEVICE_ID_DOBSON,
271 PCI_ANY_ID,
272 PCI_ANY_ID,
1da177e4
LT
273 },
274 {
275 PCI_VENDOR_ID_AMI,
276 PCI_DEVICE_ID_AMI_MEGARAID3,
ed7e8ef7
JS
277 PCI_ANY_ID,
278 PCI_ANY_ID,
1da177e4
LT
279 },
280 {
281 PCI_VENDOR_ID_LSI_LOGIC,
ed7e8ef7
JS
282 PCI_DEVICE_ID_AMI_MEGARAID3,
283 PCI_ANY_ID,
284 PCI_ANY_ID,
1da177e4
LT
285 },
286 {
287 PCI_VENDOR_ID_LSI_LOGIC,
672b2d38
JS
288 PCI_DEVICE_ID_LINDSAY,
289 PCI_ANY_ID,
290 PCI_ANY_ID,
1da177e4 291 },
1da177e4
LT
292 {0} /* Terminating entry */
293};
294MODULE_DEVICE_TABLE(pci, pci_id_table_g);
295
296
382db811 297static struct pci_driver megaraid_pci_driver = {
1da177e4
LT
298 .name = "megaraid",
299 .id_table = pci_id_table_g,
300 .probe = megaraid_probe_one,
6f039790 301 .remove = megaraid_detach_one,
5457b6a6 302 .shutdown = megaraid_mbox_shutdown,
1da177e4
LT
303};
304
305
306
307// definitions for the device attributes for exporting logical drive number
308// for a scsi address (Host, Channel, Id, Lun)
309
ee959b00 310DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
1da177e4
LT
311 NULL);
312
313// Host template initializer for megaraid mbox sysfs device attributes
ee959b00
TJ
314static struct device_attribute *megaraid_shost_attrs[] = {
315 &dev_attr_megaraid_mbox_app_hndl,
1da177e4
LT
316 NULL,
317};
318
319
320DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
321
322// Host template initializer for megaraid mbox sysfs device attributes
323static struct device_attribute *megaraid_sdev_attrs[] = {
324 &dev_attr_megaraid_mbox_ld,
325 NULL,
326};
327
1da177e4
LT
328/*
329 * Scsi host template for megaraid unified driver
330 */
331static struct scsi_host_template megaraid_template_g = {
332 .module = THIS_MODULE,
333 .name = "LSI Logic MegaRAID driver",
334 .proc_name = "megaraid",
335 .queuecommand = megaraid_queue_command,
336 .eh_abort_handler = megaraid_abort_handler,
1da177e4 337 .eh_host_reset_handler = megaraid_reset_handler,
db5ed4df 338 .change_queue_depth = scsi_change_queue_depth,
54b2b50c 339 .no_write_same = 1,
1da177e4
LT
340 .sdev_attrs = megaraid_sdev_attrs,
341 .shost_attrs = megaraid_shost_attrs,
342};
343
344
345/**
346 * megaraid_init - module load hook
347 *
348 * We register ourselves as hotplug enabled module and let PCI subsystem
a69b74d3
RD
349 * discover our adapters.
350 */
1da177e4
LT
351static int __init
352megaraid_init(void)
353{
354 int rval;
355
356 // Announce the driver version
357 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
358 MEGARAID_EXT_VERSION));
359
360 // check validity of module parameters
361 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
362
363 con_log(CL_ANN, (KERN_WARNING
364 "megaraid mailbox: max commands per lun reset to %d\n",
365 MBOX_MAX_SCSI_CMDS));
366
367 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
368 }
369
370
371 // register as a PCI hot-plug driver module
382db811 372 rval = pci_register_driver(&megaraid_pci_driver);
672b2d38 373 if (rval < 0) {
1da177e4
LT
374 con_log(CL_ANN, (KERN_WARNING
375 "megaraid: could not register hotplug support.\n"));
376 }
377
378 return rval;
379}
380
381
382/**
383 * megaraid_exit - driver unload entry point
384 *
a69b74d3 385 * We simply unwrap the megaraid_init routine here.
1da177e4
LT
386 */
387static void __exit
388megaraid_exit(void)
389{
390 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
391
392 // unregister as PCI hotplug driver
382db811 393 pci_unregister_driver(&megaraid_pci_driver);
1da177e4
LT
394
395 return;
396}
397
398
399/**
400 * megaraid_probe_one - PCI hotplug entry point
a69b74d3
RD
401 * @pdev : handle to this controller's PCI configuration space
402 * @id : pci device id of the class of controllers
1da177e4
LT
403 *
404 * This routine should be called whenever a new adapter is detected by the
3a4fa0a2 405 * PCI hotplug susbsystem.
a69b74d3 406 */
6f039790 407static int
1da177e4
LT
408megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
409{
410 adapter_t *adapter;
411
412
413 // detected a new controller
414 con_log(CL_ANN, (KERN_INFO
415 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
416 pdev->vendor, pdev->device, pdev->subsystem_vendor,
417 pdev->subsystem_device));
418
419 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
420 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
421
422 if (pci_enable_device(pdev)) {
423 con_log(CL_ANN, (KERN_WARNING
424 "megaraid: pci_enable_device failed\n"));
425
426 return -ENODEV;
427 }
428
429 // Enable bus-mastering on this controller
430 pci_set_master(pdev);
431
432 // Allocate the per driver initialization structure
dd00cc48 433 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
1da177e4
LT
434
435 if (adapter == NULL) {
436 con_log(CL_ANN, (KERN_WARNING
cadbd4a5 437 "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
1da177e4
LT
438
439 goto out_probe_one;
440 }
1da177e4
LT
441
442
443 // set up PCI related soft state and other pre-known parameters
444 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
445 adapter->irq = pdev->irq;
446 adapter->pdev = pdev;
447
448 atomic_set(&adapter->being_detached, 0);
449
450 // Setup the default DMA mask. This would be changed later on
451 // depending on hardware capabilities
66e3a241 452 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(32))) {
1da177e4 453 con_log(CL_ANN, (KERN_WARNING
66e3a241 454 "megaraid: dma_set_mask failed:%d\n", __LINE__));
1da177e4
LT
455
456 goto out_free_adapter;
457 }
458
459
460 // Initialize the synchronization lock for kernel and LLD
461 spin_lock_init(&adapter->lock);
1da177e4
LT
462
463 // Initialize the command queues: the list of free SCBs and the list
464 // of pending SCBs.
465 INIT_LIST_HEAD(&adapter->kscb_pool);
466 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
467
468 INIT_LIST_HEAD(&adapter->pend_list);
469 spin_lock_init(PENDING_LIST_LOCK(adapter));
470
471 INIT_LIST_HEAD(&adapter->completed_list);
472 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
473
474
475 // Start the mailbox based controller
476 if (megaraid_init_mbox(adapter) != 0) {
477 con_log(CL_ANN, (KERN_WARNING
6868aa76 478 "megaraid: mailbox adapter did not initialize\n"));
1da177e4
LT
479
480 goto out_free_adapter;
481 }
482
483 // Register with LSI Common Management Module
484 if (megaraid_cmm_register(adapter) != 0) {
485
486 con_log(CL_ANN, (KERN_WARNING
487 "megaraid: could not register with management module\n"));
488
489 goto out_fini_mbox;
490 }
491
492 // setup adapter handle in PCI soft state
493 pci_set_drvdata(pdev, adapter);
494
495 // attach with scsi mid-layer
496 if (megaraid_io_attach(adapter) != 0) {
497
498 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
499
500 goto out_cmm_unreg;
501 }
502
503 return 0;
504
505out_cmm_unreg:
1da177e4
LT
506 megaraid_cmm_unregister(adapter);
507out_fini_mbox:
508 megaraid_fini_mbox(adapter);
509out_free_adapter:
510 kfree(adapter);
511out_probe_one:
512 pci_disable_device(pdev);
513
514 return -ENODEV;
515}
516
517
518/**
a69b74d3 519 * megaraid_detach_one - release framework resources and call LLD release routine
3948ff8b 520 * @pdev : handle for our PCI configuration space
1da177e4
LT
521 *
522 * This routine is called during driver unload. We free all the allocated
523 * resources and call the corresponding LLD so that it can also release all
524 * its resources.
525 *
a69b74d3
RD
526 * This routine is also called from the PCI hotplug system.
527 */
1da177e4
LT
528static void
529megaraid_detach_one(struct pci_dev *pdev)
530{
531 adapter_t *adapter;
532 struct Scsi_Host *host;
533
534
535 // Start a rollback on this adapter
536 adapter = pci_get_drvdata(pdev);
537
538 if (!adapter) {
539 con_log(CL_ANN, (KERN_CRIT
540 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
541 pdev->vendor, pdev->device, pdev->subsystem_vendor,
542 pdev->subsystem_device));
543
544 return;
545 }
546 else {
547 con_log(CL_ANN, (KERN_NOTICE
548 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
549 pdev->vendor, pdev->device, pdev->subsystem_vendor,
550 pdev->subsystem_device));
551 }
552
553
554 host = adapter->host;
555
556 // do not allow any more requests from the management module for this
557 // adapter.
558 // FIXME: How do we account for the request which might still be
559 // pending with us?
560 atomic_set(&adapter->being_detached, 1);
561
562 // detach from the IO sub-system
563 megaraid_io_detach(adapter);
564
1da177e4
LT
565 // Unregister from common management module
566 //
567 // FIXME: this must return success or failure for conditions if there
568 // is a command pending with LLD or not.
569 megaraid_cmm_unregister(adapter);
570
571 // finalize the mailbox based controller and release all resources
572 megaraid_fini_mbox(adapter);
573
574 kfree(adapter);
575
576 scsi_host_put(host);
577
578 pci_disable_device(pdev);
579
580 return;
581}
582
583
584/**
585 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
a69b74d3 586 * @pdev : generic driver model device
1da177e4 587 *
a69b74d3 588 * Shutdown notification, perform flush cache.
1da177e4
LT
589 */
590static void
5457b6a6 591megaraid_mbox_shutdown(struct pci_dev *pdev)
1da177e4 592{
5457b6a6 593 adapter_t *adapter = pci_get_drvdata(pdev);
1da177e4
LT
594 static int counter;
595
596 if (!adapter) {
597 con_log(CL_ANN, (KERN_WARNING
598 "megaraid: null device in shutdown\n"));
599 return;
600 }
601
602 // flush caches now
603 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
604 counter++));
605
606 megaraid_mbox_flush_cache(adapter);
607
608 con_log(CL_ANN, ("done\n"));
609}
610
611
612/**
613 * megaraid_io_attach - attach a device with the IO subsystem
a69b74d3 614 * @adapter : controller's soft state
1da177e4 615 *
a69b74d3
RD
616 * Attach this device with the IO subsystem.
617 */
1da177e4
LT
618static int
619megaraid_io_attach(adapter_t *adapter)
620{
621 struct Scsi_Host *host;
622
623 // Initialize SCSI Host structure
624 host = scsi_host_alloc(&megaraid_template_g, 8);
625 if (!host) {
626 con_log(CL_ANN, (KERN_WARNING
627 "megaraid mbox: scsi_register failed\n"));
628
629 return -1;
630 }
631
632 SCSIHOST2ADAP(host) = (caddr_t)adapter;
633 adapter->host = host;
634
1da177e4
LT
635 host->irq = adapter->irq;
636 host->unique_id = adapter->unique_id;
637 host->can_queue = adapter->max_cmds;
638 host->this_id = adapter->init_id;
639 host->sg_tablesize = adapter->sglen;
640 host->max_sectors = adapter->max_sectors;
641 host->cmd_per_lun = adapter->cmd_per_lun;
642 host->max_channel = adapter->max_channel;
643 host->max_id = adapter->max_target;
644 host->max_lun = adapter->max_lun;
645
646
647 // notify mid-layer about the new controller
648 if (scsi_add_host(host, &adapter->pdev->dev)) {
649
650 con_log(CL_ANN, (KERN_WARNING
651 "megaraid mbox: scsi_add_host failed\n"));
652
653 scsi_host_put(host);
654
655 return -1;
656 }
657
658 scsi_scan_host(host);
659
660 return 0;
661}
662
663
664/**
665 * megaraid_io_detach - detach a device from the IO subsystem
a69b74d3 666 * @adapter : controller's soft state
1da177e4 667 *
a69b74d3
RD
668 * Detach this device from the IO subsystem.
669 */
1da177e4
LT
670static void
671megaraid_io_detach(adapter_t *adapter)
672{
673 struct Scsi_Host *host;
674
675 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
676
677 host = adapter->host;
678
679 scsi_remove_host(host);
680
681 return;
682}
683
684
685/*
686 * START: Mailbox Low Level Driver
687 *
688 * This is section specific to the single mailbox based controllers
689 */
690
691/**
692 * megaraid_init_mbox - initialize controller
a69b74d3 693 * @adapter : our soft state
1da177e4 694 *
a69b74d3
RD
695 * - Allocate 16-byte aligned mailbox memory for firmware handshake
696 * - Allocate controller's memory resources
697 * - Find out all initialization data
698 * - Allocate memory required for all the commands
699 * - Use internal library of FW routines, build up complete soft state
1da177e4 700 */
6f039790 701static int
1da177e4
LT
702megaraid_init_mbox(adapter_t *adapter)
703{
704 struct pci_dev *pdev;
705 mraid_device_t *raid_dev;
706 int i;
fbf60802 707 uint32_t magic64;
1da177e4
LT
708
709
710 adapter->ito = MBOX_TIMEOUT;
711 pdev = adapter->pdev;
712
713 /*
714 * Allocate and initialize the init data structure for mailbox
715 * controllers
716 */
dd00cc48 717 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
1da177e4
LT
718 if (raid_dev == NULL) return -1;
719
1da177e4
LT
720
721 /*
722 * Attach the adapter soft state to raid device soft state
723 */
724 adapter->raid_device = (caddr_t)raid_dev;
725 raid_dev->fast_load = megaraid_fast_load;
726
727
728 // our baseport
729 raid_dev->baseport = pci_resource_start(pdev, 0);
730
731 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
732
733 con_log(CL_ANN, (KERN_WARNING
734 "megaraid: mem region busy\n"));
735
736 goto out_free_raid_dev;
737 }
738
739 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
740
741 if (!raid_dev->baseaddr) {
742
743 con_log(CL_ANN, (KERN_WARNING
744 "megaraid: could not map hba memory\n") );
745
746 goto out_release_regions;
747 }
748
cd96d96f
SP
749 /* initialize the mutual exclusion lock for the mailbox */
750 spin_lock_init(&raid_dev->mailbox_lock);
751
752 /* allocate memory required for commands */
753 if (megaraid_alloc_cmd_packets(adapter) != 0)
754 goto out_iounmap;
1da177e4 755
cd96d96f
SP
756 /*
757 * Issue SYNC cmd to flush the pending cmds in the adapter
758 * and initialize its internal state
759 */
760
761 if (megaraid_mbox_fire_sync_cmd(adapter))
762 con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
763
764 /*
765 * Setup the rest of the soft state using the library of
766 * FW routines
767 */
768
769 /* request IRQ and register the interrupt service routine */
1d6f359a 770 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
1da177e4
LT
771 adapter)) {
772
773 con_log(CL_ANN, (KERN_WARNING
774 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
cd96d96f 775 goto out_alloc_cmds;
1da177e4 776
1da177e4
LT
777 }
778
779 // Product info
cd96d96f
SP
780 if (megaraid_mbox_product_info(adapter) != 0)
781 goto out_free_irq;
1da177e4
LT
782
783 // Do we support extended CDBs
784 adapter->max_cdb_sz = 10;
785 if (megaraid_mbox_extended_cdb(adapter) == 0) {
786 adapter->max_cdb_sz = 16;
787 }
788
789 /*
790 * Do we support cluster environment, if we do, what is the initiator
791 * id.
792 * NOTE: In a non-cluster aware firmware environment, the LLD should
793 * return 7 as initiator id.
794 */
795 adapter->ha = 0;
796 adapter->init_id = -1;
797 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
798 adapter->ha = 1;
799 }
800
801 /*
802 * Prepare the device ids array to have the mapping between the kernel
803 * device address and megaraid device address.
804 * We export the physical devices on their actual addresses. The
805 * logical drives are exported on a virtual SCSI channel
806 */
807 megaraid_mbox_setup_device_map(adapter);
808
809 // If the firmware supports random deletion, update the device id map
810 if (megaraid_mbox_support_random_del(adapter)) {
811
812 // Change the logical drives numbers in device_ids array one
813 // slot in device_ids is reserved for target id, that's why
814 // "<=" below
815 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
816 adapter->device_ids[adapter->max_channel][i] += 0x80;
817 }
818 adapter->device_ids[adapter->max_channel][adapter->init_id] =
819 0xFF;
820
821 raid_dev->random_del_supported = 1;
822 }
823
824 /*
825 * find out the maximum number of scatter-gather elements supported by
826 * this firmware
827 */
828 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
829
830 // enumerate RAID and SCSI channels so that all devices on SCSI
831 // channels can later be exported, including disk devices
832 megaraid_mbox_enum_raid_scsi(adapter);
833
834 /*
835 * Other parameters required by upper layer
836 *
837 * maximum number of sectors per IO command
838 */
839 adapter->max_sectors = megaraid_max_sectors;
840
841 /*
842 * number of queued commands per LUN.
843 */
844 adapter->cmd_per_lun = megaraid_cmd_per_lun;
845
846 /*
847 * Allocate resources required to issue FW calls, when sysfs is
848 * accessed
849 */
cd96d96f
SP
850 if (megaraid_sysfs_alloc_resources(adapter) != 0)
851 goto out_free_irq;
1da177e4
LT
852
853 // Set the DMA mask to 64-bit. All supported controllers as capable of
854 // DMA in this range
fbf60802
JS
855 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
856
857 if (((magic64 == HBA_SIGNATURE_64_BIT) &&
858 ((adapter->pdev->subsystem_device !=
8741ca71 859 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
fbf60802
JS
860 (adapter->pdev->subsystem_device !=
861 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
862 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
863 adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
864 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
865 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
866 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
867 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
868 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
869 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
870 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
871 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
66e3a241 872 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(64))) {
fbf60802
JS
873 con_log(CL_ANN, (KERN_WARNING
874 "megaraid: DMA mask for 64-bit failed\n"));
875
66e3a241
CH
876 if (dma_set_mask(&adapter->pdev->dev,
877 DMA_BIT_MASK(32))) {
fbf60802
JS
878 con_log(CL_ANN, (KERN_WARNING
879 "megaraid: 32-bit DMA mask failed\n"));
880 goto out_free_sysfs_res;
881 }
882 }
1da177e4
LT
883 }
884
885 // setup tasklet for DPC
886 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
887 (unsigned long)adapter);
888
889 con_log(CL_DLEVEL1, (KERN_INFO
890 "megaraid mbox hba successfully initialized\n"));
891
892 return 0;
893
894out_free_sysfs_res:
895 megaraid_sysfs_free_resources(adapter);
1da177e4
LT
896out_free_irq:
897 free_irq(adapter->irq, adapter);
cd96d96f
SP
898out_alloc_cmds:
899 megaraid_free_cmd_packets(adapter);
1da177e4
LT
900out_iounmap:
901 iounmap(raid_dev->baseaddr);
902out_release_regions:
903 pci_release_regions(pdev);
904out_free_raid_dev:
905 kfree(raid_dev);
906
907 return -1;
908}
909
910
911/**
912 * megaraid_fini_mbox - undo controller initialization
a69b74d3 913 * @adapter : our soft state
1da177e4
LT
914 */
915static void
916megaraid_fini_mbox(adapter_t *adapter)
917{
918 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
919
920 // flush all caches
921 megaraid_mbox_flush_cache(adapter);
922
923 tasklet_kill(&adapter->dpc_h);
924
925 megaraid_sysfs_free_resources(adapter);
926
927 megaraid_free_cmd_packets(adapter);
928
929 free_irq(adapter->irq, adapter);
930
931 iounmap(raid_dev->baseaddr);
932
933 pci_release_regions(adapter->pdev);
934
935 kfree(raid_dev);
936
937 return;
938}
939
940
941/**
942 * megaraid_alloc_cmd_packets - allocate shared mailbox
a69b74d3 943 * @adapter : soft state of the raid controller
1da177e4 944 *
6868aa76 945 * Allocate and align the shared mailbox. This mailbox is used to issue
3948ff8b 946 * all the commands. For IO based controllers, the mailbox is also registered
1da177e4 947 * with the FW. Allocate memory for all commands as well.
a69b74d3 948 * This is our big allocator.
1da177e4
LT
949 */
950static int
951megaraid_alloc_cmd_packets(adapter_t *adapter)
952{
953 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
954 struct pci_dev *pdev;
955 unsigned long align;
956 scb_t *scb;
957 mbox_ccb_t *ccb;
958 struct mraid_pci_blk *epthru_pci_blk;
959 struct mraid_pci_blk *sg_pci_blk;
960 struct mraid_pci_blk *mbox_pci_blk;
961 int i;
962
963 pdev = adapter->pdev;
964
965 /*
966 * Setup the mailbox
967 * Allocate the common 16-byte aligned memory for the handshake
968 * mailbox.
969 */
750afb08
LC
970 raid_dev->una_mbox64 = dma_alloc_coherent(&adapter->pdev->dev,
971 sizeof(mbox64_t),
972 &raid_dev->una_mbox64_dma,
973 GFP_KERNEL);
1da177e4
LT
974
975 if (!raid_dev->una_mbox64) {
976 con_log(CL_ANN, (KERN_WARNING
cadbd4a5 977 "megaraid: out of memory, %s %d\n", __func__,
1da177e4
LT
978 __LINE__));
979 return -1;
980 }
1da177e4
LT
981
982 /*
983 * Align the mailbox at 16-byte boundary
984 */
985 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
986
987 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
988 (~0UL ^ 0xFUL));
989
990 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
991
992 align = ((void *)raid_dev->mbox -
993 ((void *)&raid_dev->una_mbox64->mbox32));
994
995 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
996 align;
997
998 // Allocate memory for commands issued internally
750afb08
LC
999 adapter->ibuf = dma_alloc_coherent(&pdev->dev, MBOX_IBUF_SIZE,
1000 &adapter->ibuf_dma_h, GFP_KERNEL);
1da177e4
LT
1001 if (!adapter->ibuf) {
1002
1003 con_log(CL_ANN, (KERN_WARNING
cadbd4a5 1004 "megaraid: out of memory, %s %d\n", __func__,
1da177e4
LT
1005 __LINE__));
1006
1007 goto out_free_common_mbox;
1008 }
1da177e4
LT
1009
1010 // Allocate memory for our SCSI Command Blocks and their associated
1011 // memory
1012
1013 /*
1014 * Allocate memory for the base list of scb. Later allocate memory for
1015 * CCBs and embedded components of each CCB and point the pointers in
1016 * scb to the allocated components
1017 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1018 * since the calling routine does not yet know the number of available
1019 * commands.
1020 */
dd00cc48 1021 adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
1da177e4
LT
1022
1023 if (adapter->kscb_list == NULL) {
1024 con_log(CL_ANN, (KERN_WARNING
cadbd4a5 1025 "megaraid: out of memory, %s %d\n", __func__,
1da177e4
LT
1026 __LINE__));
1027 goto out_free_ibuf;
1028 }
1da177e4
LT
1029
1030 // memory allocation for our command packets
1031 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1032 con_log(CL_ANN, (KERN_WARNING
cadbd4a5 1033 "megaraid: out of memory, %s %d\n", __func__,
1da177e4
LT
1034 __LINE__));
1035 goto out_free_scb_list;
1036 }
1037
1038 // Adjust the scb pointers and link in the free pool
1039 epthru_pci_blk = raid_dev->epthru_pool;
1040 sg_pci_blk = raid_dev->sg_pool;
1041 mbox_pci_blk = raid_dev->mbox_pool;
1042
1043 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1044 scb = adapter->kscb_list + i;
1045 ccb = raid_dev->ccb_list + i;
1046
1047 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1048 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1049 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1050 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1051
1052 // make sure the mailbox is aligned properly
1053 if (ccb->mbox_dma_h & 0x0F) {
1054 con_log(CL_ANN, (KERN_CRIT
1055 "megaraid mbox: not aligned on 16-bytes\n"));
1056
1057 goto out_teardown_dma_pools;
1058 }
1059
1060 ccb->epthru = (mraid_epassthru_t *)
1061 epthru_pci_blk[i].vaddr;
1062 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1063 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1064 ccb->pthru_dma_h = ccb->epthru_dma_h;
1065
1066
1067 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1068 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1069 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1070
1071 scb->ccb = (caddr_t)ccb;
1072 scb->gp = 0;
1073
1074 scb->sno = i; // command index
1075
1076 scb->scp = NULL;
1077 scb->state = SCB_FREE;
66e3a241 1078 scb->dma_direction = DMA_NONE;
1da177e4
LT
1079 scb->dma_type = MRAID_DMA_NONE;
1080 scb->dev_channel = -1;
1081 scb->dev_target = -1;
1082
1083 // put scb in the free pool
1084 list_add_tail(&scb->list, &adapter->kscb_pool);
1085 }
1086
1087 return 0;
1088
1089out_teardown_dma_pools:
1090 megaraid_mbox_teardown_dma_pools(adapter);
1091out_free_scb_list:
1092 kfree(adapter->kscb_list);
1093out_free_ibuf:
66e3a241 1094 dma_free_coherent(&pdev->dev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1da177e4
LT
1095 adapter->ibuf_dma_h);
1096out_free_common_mbox:
66e3a241 1097 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t),
1da177e4
LT
1098 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1099
1100 return -1;
1101}
1102
1103
1104/**
1105 * megaraid_free_cmd_packets - free memory
a69b74d3 1106 * @adapter : soft state of the raid controller
1da177e4 1107 *
a69b74d3 1108 * Release memory resources allocated for commands.
1da177e4
LT
1109 */
1110static void
1111megaraid_free_cmd_packets(adapter_t *adapter)
1112{
1113 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1114
1115 megaraid_mbox_teardown_dma_pools(adapter);
1116
1117 kfree(adapter->kscb_list);
1118
66e3a241 1119 dma_free_coherent(&adapter->pdev->dev, MBOX_IBUF_SIZE,
1da177e4
LT
1120 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1121
66e3a241 1122 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t),
1da177e4
LT
1123 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1124 return;
1125}
1126
1127
1128/**
1129 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
a69b74d3 1130 * @adapter : HBA soft state
1da177e4 1131 *
a69b74d3
RD
1132 * Setup the dma pools for mailbox, passthru and extended passthru structures,
1133 * and scatter-gather lists.
1da177e4
LT
1134 */
1135static int
1136megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1137{
1138 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1139 struct mraid_pci_blk *epthru_pci_blk;
1140 struct mraid_pci_blk *sg_pci_blk;
1141 struct mraid_pci_blk *mbox_pci_blk;
1142 int i;
1143
1144
1145
1146 // Allocate memory for 16-bytes aligned mailboxes
fc69d86d
RP
1147 raid_dev->mbox_pool_handle = dma_pool_create("megaraid mbox pool",
1148 &adapter->pdev->dev,
1da177e4
LT
1149 sizeof(mbox64_t) + 16,
1150 16, 0);
1151
1152 if (raid_dev->mbox_pool_handle == NULL) {
1153 goto fail_setup_dma_pool;
1154 }
1155
1156 mbox_pci_blk = raid_dev->mbox_pool;
1157 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
fc69d86d 1158 mbox_pci_blk[i].vaddr = dma_pool_alloc(
1da177e4
LT
1159 raid_dev->mbox_pool_handle,
1160 GFP_KERNEL,
1161 &mbox_pci_blk[i].dma_addr);
1162 if (!mbox_pci_blk[i].vaddr) {
1163 goto fail_setup_dma_pool;
1164 }
1165 }
1166
1167 /*
1168 * Allocate memory for each embedded passthru strucuture pointer
1169 * Request for a 128 bytes aligned structure for each passthru command
1170 * structure
1171 * Since passthru and extended passthru commands are exclusive, they
1172 * share common memory pool. Passthru structures piggyback on memory
1173 * allocted to extended passthru since passthru is smaller of the two
1174 */
fc69d86d
RP
1175 raid_dev->epthru_pool_handle = dma_pool_create("megaraid mbox pthru",
1176 &adapter->pdev->dev, sizeof(mraid_epassthru_t), 128, 0);
1da177e4
LT
1177
1178 if (raid_dev->epthru_pool_handle == NULL) {
1179 goto fail_setup_dma_pool;
1180 }
1181
1182 epthru_pci_blk = raid_dev->epthru_pool;
1183 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
fc69d86d 1184 epthru_pci_blk[i].vaddr = dma_pool_alloc(
1da177e4
LT
1185 raid_dev->epthru_pool_handle,
1186 GFP_KERNEL,
1187 &epthru_pci_blk[i].dma_addr);
1188 if (!epthru_pci_blk[i].vaddr) {
1189 goto fail_setup_dma_pool;
1190 }
1191 }
1192
1193
1194 // Allocate memory for each scatter-gather list. Request for 512 bytes
1195 // alignment for each sg list
fc69d86d
RP
1196 raid_dev->sg_pool_handle = dma_pool_create("megaraid mbox sg",
1197 &adapter->pdev->dev,
1da177e4
LT
1198 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1199 512, 0);
1200
1201 if (raid_dev->sg_pool_handle == NULL) {
1202 goto fail_setup_dma_pool;
1203 }
1204
1205 sg_pci_blk = raid_dev->sg_pool;
1206 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
fc69d86d 1207 sg_pci_blk[i].vaddr = dma_pool_alloc(
1da177e4
LT
1208 raid_dev->sg_pool_handle,
1209 GFP_KERNEL,
1210 &sg_pci_blk[i].dma_addr);
1211 if (!sg_pci_blk[i].vaddr) {
1212 goto fail_setup_dma_pool;
1213 }
1214 }
1215
1216 return 0;
1217
1218fail_setup_dma_pool:
1219 megaraid_mbox_teardown_dma_pools(adapter);
1220 return -1;
1221}
1222
1223
1224/**
1225 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
a69b74d3 1226 * @adapter : HBA soft state
1da177e4 1227 *
a69b74d3
RD
1228 * Teardown the dma pool for mailbox, passthru and extended passthru
1229 * structures, and scatter-gather lists.
1da177e4
LT
1230 */
1231static void
1232megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1233{
1234 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1235 struct mraid_pci_blk *epthru_pci_blk;
1236 struct mraid_pci_blk *sg_pci_blk;
1237 struct mraid_pci_blk *mbox_pci_blk;
1238 int i;
1239
1240
1241 sg_pci_blk = raid_dev->sg_pool;
1242 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
fc69d86d 1243 dma_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1da177e4
LT
1244 sg_pci_blk[i].dma_addr);
1245 }
19c05072 1246 dma_pool_destroy(raid_dev->sg_pool_handle);
1da177e4
LT
1247
1248
1249 epthru_pci_blk = raid_dev->epthru_pool;
1250 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
fc69d86d 1251 dma_pool_free(raid_dev->epthru_pool_handle,
1da177e4
LT
1252 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1253 }
19c05072 1254 dma_pool_destroy(raid_dev->epthru_pool_handle);
1da177e4
LT
1255
1256
1257 mbox_pci_blk = raid_dev->mbox_pool;
1258 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
fc69d86d 1259 dma_pool_free(raid_dev->mbox_pool_handle,
1da177e4
LT
1260 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1261 }
19c05072 1262 dma_pool_destroy(raid_dev->mbox_pool_handle);
1da177e4
LT
1263
1264 return;
1265}
1266
1267
1268/**
1269 * megaraid_alloc_scb - detach and return a scb from the free list
1270 * @adapter : controller's soft state
a69b74d3 1271 * @scp : pointer to the scsi command to be executed
1da177e4 1272 *
a69b74d3
RD
1273 * Return the scb from the head of the free list. %NULL if there are none
1274 * available.
1275 */
858119e1 1276static scb_t *
1da177e4
LT
1277megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1278{
1279 struct list_head *head = &adapter->kscb_pool;
1280 scb_t *scb = NULL;
1281 unsigned long flags;
1282
1283 // detach scb from free pool
1284 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1285
1286 if (list_empty(head)) {
1287 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1288 return NULL;
1289 }
1290
1291 scb = list_entry(head->next, scb_t, list);
1292 list_del_init(&scb->list);
1293
1294 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1295
1296 scb->state = SCB_ACTIVE;
1297 scb->scp = scp;
1298 scb->dma_type = MRAID_DMA_NONE;
1299
1300 return scb;
1301}
1302
1303
1304/**
1305 * megaraid_dealloc_scb - return the scb to the free pool
1306 * @adapter : controller's soft state
1307 * @scb : scb to be freed
1308 *
a69b74d3 1309 * Return the scb back to the free list of scbs. The caller must 'flush' the
1da177e4
LT
1310 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1311 * NOTE NOTE: Make sure the scb is not on any list before calling this
1312 * routine.
a69b74d3 1313 */
1da177e4
LT
1314static inline void
1315megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1316{
1317 unsigned long flags;
1318
1319 // put scb in the free pool
1320 scb->state = SCB_FREE;
1321 scb->scp = NULL;
1322 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1323
1324 list_add(&scb->list, &adapter->kscb_pool);
1325
1326 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1327
1328 return;
1329}
1330
1331
1332/**
1333 * megaraid_mbox_mksgl - make the scatter-gather list
a69b74d3
RD
1334 * @adapter : controller's soft state
1335 * @scb : scsi control block
1da177e4 1336 *
a69b74d3 1337 * Prepare the scatter-gather list.
1da177e4 1338 */
858119e1 1339static int
1da177e4
LT
1340megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1341{
1342 struct scatterlist *sgl;
1343 mbox_ccb_t *ccb;
1da177e4
LT
1344 struct scsi_cmnd *scp;
1345 int sgcnt;
1346 int i;
1347
1348
1349 scp = scb->scp;
1350 ccb = (mbox_ccb_t *)scb->ccb;
1351
32fbac22
FT
1352 sgcnt = scsi_dma_map(scp);
1353 BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
1354
1da177e4 1355 // no mapping required if no data to be transferred
32fbac22 1356 if (!sgcnt)
1da177e4
LT
1357 return 0;
1358
1da177e4
LT
1359 scb->dma_type = MRAID_DMA_WSG;
1360
32fbac22 1361 scsi_for_each_sg(scp, sgl, sgcnt, i) {
1da177e4
LT
1362 ccb->sgl64[i].address = sg_dma_address(sgl);
1363 ccb->sgl64[i].length = sg_dma_len(sgl);
1364 }
1365
1366 // Return count of SG nodes
1367 return sgcnt;
1368}
1369
1370
1371/**
1372 * mbox_post_cmd - issue a mailbox command
a69b74d3
RD
1373 * @adapter : controller's soft state
1374 * @scb : command to be issued
1da177e4 1375 *
a69b74d3 1376 * Post the command to the controller if mailbox is available.
1da177e4 1377 */
858119e1 1378static int
1da177e4
LT
1379mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1380{
1381 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1382 mbox64_t *mbox64;
1383 mbox_t *mbox;
1384 mbox_ccb_t *ccb;
1385 unsigned long flags;
1386 unsigned int i = 0;
1387
1388
1389 ccb = (mbox_ccb_t *)scb->ccb;
1390 mbox = raid_dev->mbox;
1391 mbox64 = raid_dev->mbox64;
1392
1393 /*
1394 * Check for busy mailbox. If it is, return failure - the caller
1395 * should retry later.
1396 */
1397 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1398
1399 if (unlikely(mbox->busy)) {
1400 do {
1401 udelay(1);
1402 i++;
1403 rmb();
1404 } while(mbox->busy && (i < max_mbox_busy_wait));
1405
1406 if (mbox->busy) {
1407
1408 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1409
1410 return -1;
1411 }
1412 }
1413
1414
1415 // Copy this command's mailbox data into "adapter's" mailbox
1416 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1417 mbox->cmdid = scb->sno;
1418
1419 adapter->outstanding_cmds++;
1420
1da177e4
LT
1421 mbox->busy = 1; // Set busy
1422 mbox->poll = 0;
1423 mbox->ack = 0;
1424 wmb();
1425
1426 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1427
1428 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1429
1430 return 0;
1431}
1432
1433
1434/**
1435 * megaraid_queue_command - generic queue entry point for all LLDs
1436 * @scp : pointer to the scsi command to be executed
1437 * @done : callback routine to be called after the cmd has be completed
1438 *
1439 * Queue entry point for mailbox based controllers.
1440 */
1441static int
f281233d 1442megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
1da177e4
LT
1443{
1444 adapter_t *adapter;
1445 scb_t *scb;
1446 int if_busy;
1447
1448 adapter = SCP2ADAPTER(scp);
1449 scp->scsi_done = done;
1450 scp->result = 0;
1451
1da177e4
LT
1452 /*
1453 * Allocate and build a SCB request
1454 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1455 * not allocate scb. We will return non-zero status in that case.
1456 * NOTE: scb can be null even though certain commands completed
1457 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1458 * return 0 in that case, and we would do the callback right away.
1459 */
1460 if_busy = 0;
f2c8dc40 1461 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1da177e4
LT
1462 if (!scb) { // command already completed
1463 done(scp);
1464 return 0;
1465 }
1466
f2c8dc40 1467 megaraid_mbox_runpendq(adapter, scb);
1da177e4
LT
1468 return if_busy;
1469}
1470
f281233d
JG
1471static DEF_SCSI_QCMD(megaraid_queue_command)
1472
1da177e4 1473/**
a69b74d3
RD
1474 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
1475 * @adapter : controller's soft state
1476 * @scp : mid-layer scsi command pointer
1477 * @busy : set if request could not be completed because of lack of
1da177e4
LT
1478 * resources
1479 *
a69b74d3
RD
1480 * Transform the mid-layer scsi command to megaraid firmware lingua.
1481 * Convert the command issued by mid-layer to format understood by megaraid
1482 * firmware. We also complete certain commands without sending them to firmware.
1da177e4
LT
1483 */
1484static scb_t *
1485megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1486{
1487 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1488 int channel;
1489 int target;
1490 int islogical;
1491 mbox_ccb_t *ccb;
1492 mraid_passthru_t *pthru;
1493 mbox64_t *mbox64;
1494 mbox_t *mbox;
1495 scb_t *scb;
1496 char skip[] = "skipping";
1497 char scan[] = "scanning";
1498 char *ss;
1499
1500
1501 /*
1502 * Get the appropriate device map for the device this command is
1503 * intended for
1504 */
1505 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1506
1507 /*
1508 * Logical drive commands
1509 */
1510 if (islogical) {
1511 switch (scp->cmnd[0]) {
1512 case TEST_UNIT_READY:
1513 /*
1514 * Do we support clustering and is the support enabled
1515 * If no, return success always
1516 */
1517 if (!adapter->ha) {
1518 scp->result = (DID_OK << 16);
1519 return NULL;
1520 }
1521
1522 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1523 scp->result = (DID_ERROR << 16);
1524 *busy = 1;
1525 return NULL;
1526 }
1527
1528 scb->dma_direction = scp->sc_data_direction;
1529 scb->dev_channel = 0xFF;
1530 scb->dev_target = target;
1531 ccb = (mbox_ccb_t *)scb->ccb;
1532
1533 /*
1534 * The command id will be provided by the command
1535 * issuance routine
1536 */
1537 ccb->raw_mbox[0] = CLUSTER_CMD;
1538 ccb->raw_mbox[2] = RESERVATION_STATUS;
1539 ccb->raw_mbox[3] = target;
1540
1541 return scb;
1542
1543 case MODE_SENSE:
32fbac22
FT
1544 {
1545 struct scatterlist *sgl;
1546 caddr_t vaddr;
1da177e4 1547
32fbac22 1548 sgl = scsi_sglist(scp);
45711f1a
JA
1549 if (sg_page(sgl)) {
1550 vaddr = (caddr_t) sg_virt(&sgl[0]);
1da177e4 1551
32fbac22 1552 memset(vaddr, 0, scp->cmnd[4]);
1da177e4
LT
1553 }
1554 else {
32fbac22
FT
1555 con_log(CL_ANN, (KERN_WARNING
1556 "megaraid mailbox: invalid sg:%d\n",
1557 __LINE__));
1da177e4 1558 }
32fbac22
FT
1559 }
1560 scp->result = (DID_OK << 16);
1561 return NULL;
1da177e4
LT
1562
1563 case INQUIRY:
1564 /*
1565 * Display the channel scan for logical drives
1566 * Do not display scan for a channel if already done.
1567 */
1568 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1569
1570 con_log(CL_ANN, (KERN_INFO
1571 "scsi[%d]: scanning scsi channel %d",
1572 adapter->host->host_no,
1573 SCP2CHANNEL(scp)));
1574
1575 con_log(CL_ANN, (
1576 " [virtual] for logical drives\n"));
1577
1578 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1579 }
1580
aa677bc7
JS
1581 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
1582 scp->sense_buffer[0] = 0x70;
1583 scp->sense_buffer[2] = ILLEGAL_REQUEST;
1584 scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
1585 scp->result = CHECK_CONDITION << 1;
1586 return NULL;
1587 }
1588
1da177e4
LT
1589 /* Fall through */
1590
1591 case READ_CAPACITY:
1592 /*
1593 * Do not allow LUN > 0 for logical drives and
1594 * requests for more than 40 logical drives
1595 */
1596 if (SCP2LUN(scp)) {
1597 scp->result = (DID_BAD_TARGET << 16);
1598 return NULL;
1599 }
1600 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1601 scp->result = (DID_BAD_TARGET << 16);
1602 return NULL;
1603 }
1604
1605
1606 /* Allocate a SCB and initialize passthru */
1607 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1608 scp->result = (DID_ERROR << 16);
1609 *busy = 1;
1610 return NULL;
1611 }
1612
1613 ccb = (mbox_ccb_t *)scb->ccb;
1614 scb->dev_channel = 0xFF;
1615 scb->dev_target = target;
1616 pthru = ccb->pthru;
1617 mbox = ccb->mbox;
1618 mbox64 = ccb->mbox64;
1619
1620 pthru->timeout = 0;
1621 pthru->ars = 1;
1622 pthru->reqsenselen = 14;
1623 pthru->islogical = 1;
1624 pthru->logdrv = target;
1625 pthru->cdblen = scp->cmd_len;
1626 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1627
1628 mbox->cmd = MBOXCMD_PASSTHRU64;
1629 scb->dma_direction = scp->sc_data_direction;
1630
32fbac22 1631 pthru->dataxferlen = scsi_bufflen(scp);
1da177e4
LT
1632 pthru->dataxferaddr = ccb->sgl_dma_h;
1633 pthru->numsge = megaraid_mbox_mksgl(adapter,
1634 scb);
1635
1636 mbox->xferaddr = 0xFFFFFFFF;
1637 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1638 mbox64->xferaddr_hi = 0;
1639
1640 return scb;
1641
1642 case READ_6:
1643 case WRITE_6:
1644 case READ_10:
1645 case WRITE_10:
1646 case READ_12:
1647 case WRITE_12:
1648
1649 /*
1650 * Allocate a SCB and initialize mailbox
1651 */
1652 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1653 scp->result = (DID_ERROR << 16);
1654 *busy = 1;
1655 return NULL;
1656 }
1657 ccb = (mbox_ccb_t *)scb->ccb;
1658 scb->dev_channel = 0xFF;
1659 scb->dev_target = target;
1660 mbox = ccb->mbox;
1661 mbox64 = ccb->mbox64;
1662 mbox->logdrv = target;
1663
1664 /*
1665 * A little HACK: 2nd bit is zero for all scsi read
1666 * commands and is set for all scsi write commands
1667 */
1668 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1669 MBOXCMD_LREAD64 ;
1670
1671 /*
1672 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1673 */
1674 if (scp->cmd_len == 6) {
1675 mbox->numsectors = (uint32_t)scp->cmnd[4];
1676 mbox->lba =
1677 ((uint32_t)scp->cmnd[1] << 16) |
1678 ((uint32_t)scp->cmnd[2] << 8) |
1679 (uint32_t)scp->cmnd[3];
1680
1681 mbox->lba &= 0x1FFFFF;
1682 }
1683
1684 /*
1685 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1686 */
1687 else if (scp->cmd_len == 10) {
1688 mbox->numsectors =
1689 (uint32_t)scp->cmnd[8] |
1690 ((uint32_t)scp->cmnd[7] << 8);
1691 mbox->lba =
1692 ((uint32_t)scp->cmnd[2] << 24) |
1693 ((uint32_t)scp->cmnd[3] << 16) |
1694 ((uint32_t)scp->cmnd[4] << 8) |
1695 (uint32_t)scp->cmnd[5];
1696 }
1697
1698 /*
1699 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1700 */
1701 else if (scp->cmd_len == 12) {
1702 mbox->lba =
1703 ((uint32_t)scp->cmnd[2] << 24) |
1704 ((uint32_t)scp->cmnd[3] << 16) |
1705 ((uint32_t)scp->cmnd[4] << 8) |
1706 (uint32_t)scp->cmnd[5];
1707
1708 mbox->numsectors =
1709 ((uint32_t)scp->cmnd[6] << 24) |
1710 ((uint32_t)scp->cmnd[7] << 16) |
1711 ((uint32_t)scp->cmnd[8] << 8) |
1712 (uint32_t)scp->cmnd[9];
1713 }
1714 else {
1715 con_log(CL_ANN, (KERN_WARNING
1716 "megaraid: unsupported CDB length\n"));
1717
1718 megaraid_dealloc_scb(adapter, scb);
1719
1720 scp->result = (DID_ERROR << 16);
1721 return NULL;
1722 }
1723
1724 scb->dma_direction = scp->sc_data_direction;
1725
1726 // Calculate Scatter-Gather info
1727 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1728 mbox->numsge = megaraid_mbox_mksgl(adapter,
1729 scb);
1730 mbox->xferaddr = 0xFFFFFFFF;
1731 mbox64->xferaddr_hi = 0;
1732
1733 return scb;
1734
1735 case RESERVE:
1736 case RELEASE:
1737 /*
1738 * Do we support clustering and is the support enabled
1739 */
1740 if (!adapter->ha) {
1741 scp->result = (DID_BAD_TARGET << 16);
1742 return NULL;
1743 }
1744
1745 /*
1746 * Allocate a SCB and initialize mailbox
1747 */
1748 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1749 scp->result = (DID_ERROR << 16);
1750 *busy = 1;
1751 return NULL;
1752 }
1753
1754 ccb = (mbox_ccb_t *)scb->ccb;
1755 scb->dev_channel = 0xFF;
1756 scb->dev_target = target;
1757 ccb->raw_mbox[0] = CLUSTER_CMD;
1758 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1759 RESERVE_LD : RELEASE_LD;
1760
1761 ccb->raw_mbox[3] = target;
1762 scb->dma_direction = scp->sc_data_direction;
1763
1764 return scb;
1765
1766 default:
1767 scp->result = (DID_BAD_TARGET << 16);
1768 return NULL;
1769 }
1770 }
1771 else { // Passthru device commands
1772
1773 // Do not allow access to target id > 15 or LUN > 7
1774 if (target > 15 || SCP2LUN(scp) > 7) {
1775 scp->result = (DID_BAD_TARGET << 16);
1776 return NULL;
1777 }
1778
1779 // if fast load option was set and scan for last device is
1780 // over, reset the fast_load flag so that during a possible
1781 // next scan, devices can be made available
1782 if (rdev->fast_load && (target == 15) &&
1783 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1784
1785 con_log(CL_ANN, (KERN_INFO
1786 "megaraid[%d]: physical device scan re-enabled\n",
1787 adapter->host->host_no));
1788 rdev->fast_load = 0;
1789 }
1790
1791 /*
1792 * Display the channel scan for physical devices
1793 */
1794 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1795
1796 ss = rdev->fast_load ? skip : scan;
1797
1798 con_log(CL_ANN, (KERN_INFO
1799 "scsi[%d]: %s scsi channel %d [Phy %d]",
1800 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1801 channel));
1802
1803 con_log(CL_ANN, (
1804 " for non-raid devices\n"));
1805
1806 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1807 }
1808
1809 // disable channel sweep if fast load option given
1810 if (rdev->fast_load) {
1811 scp->result = (DID_BAD_TARGET << 16);
1812 return NULL;
1813 }
1814
1815 // Allocate a SCB and initialize passthru
1816 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1817 scp->result = (DID_ERROR << 16);
1818 *busy = 1;
1819 return NULL;
1820 }
1821
1822 ccb = (mbox_ccb_t *)scb->ccb;
1823 scb->dev_channel = channel;
1824 scb->dev_target = target;
1825 scb->dma_direction = scp->sc_data_direction;
1826 mbox = ccb->mbox;
1827 mbox64 = ccb->mbox64;
1828
1829 // Does this firmware support extended CDBs
1830 if (adapter->max_cdb_sz == 16) {
1831 mbox->cmd = MBOXCMD_EXTPTHRU;
1832
1833 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1834
1835 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1836 mbox64->xferaddr_hi = 0;
1837 mbox->xferaddr = 0xFFFFFFFF;
1838 }
1839 else {
1840 mbox->cmd = MBOXCMD_PASSTHRU64;
1841
1842 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1843
1844 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1845 mbox64->xferaddr_hi = 0;
1846 mbox->xferaddr = 0xFFFFFFFF;
1847 }
1848 return scb;
1849 }
1850
1851 // NOT REACHED
1852}
1853
1854
1855/**
1856 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1857 * @adapter : controller's soft state
a69b74d3 1858 * @scb_q : SCB to be queued in the pending list
1da177e4 1859 *
a69b74d3 1860 * Scan the pending list for commands which are not yet issued and try to
1da177e4
LT
1861 * post to the controller. The SCB can be a null pointer, which would indicate
1862 * no SCB to be queue, just try to execute the ones in the pending list.
1863 *
1864 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1865 * out from the head of the pending list. If it is successfully issued, the
1866 * next SCB is at the head now.
1867 */
1868static void
1869megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1870{
1871 scb_t *scb;
1872 unsigned long flags;
1873
1874 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1875
1876 if (scb_q) {
1877 scb_q->state = SCB_PENDQ;
1878 list_add_tail(&scb_q->list, &adapter->pend_list);
1879 }
1880
1881 // if the adapter in not in quiescent mode, post the commands to FW
1882 if (adapter->quiescent) {
1883 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1884 return;
1885 }
1886
1887 while (!list_empty(&adapter->pend_list)) {
1888
1889 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1890
1891 scb = list_entry(adapter->pend_list.next, scb_t, list);
1892
1893 // remove the scb from the pending list and try to
1894 // issue. If we are unable to issue it, put back in
1895 // the pending list and return
1896
1897 list_del_init(&scb->list);
1898
1899 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1900
1901 // if mailbox was busy, return SCB back to pending
1902 // list. Make sure to add at the head, since that's
1903 // where it would have been removed from
1904
1905 scb->state = SCB_ISSUED;
1906
1907 if (mbox_post_cmd(adapter, scb) != 0) {
1908
1909 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1910
1911 scb->state = SCB_PENDQ;
1912
1913 list_add(&scb->list, &adapter->pend_list);
1914
1915 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
1916 flags);
1917
1918 return;
1919 }
1920
1921 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1922 }
1923
1924 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1925
1926
1927 return;
1928}
1929
1930
1931/**
1932 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
a69b74d3
RD
1933 * @adapter : pointer to controller's soft state
1934 * @scb : scsi control block
1935 * @scp : scsi command from the mid-layer
1da177e4 1936 *
a69b74d3 1937 * Prepare a command for the scsi physical devices.
1da177e4
LT
1938 */
1939static void
1940megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
1941 struct scsi_cmnd *scp)
1942{
1943 mbox_ccb_t *ccb;
1944 mraid_passthru_t *pthru;
1945 uint8_t channel;
1946 uint8_t target;
1947
1948 ccb = (mbox_ccb_t *)scb->ccb;
1949 pthru = ccb->pthru;
1950 channel = scb->dev_channel;
1951 target = scb->dev_target;
1952
1953 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
1954 pthru->timeout = 4;
1955 pthru->ars = 1;
1956 pthru->islogical = 0;
1957 pthru->channel = 0;
1958 pthru->target = (channel << 4) | target;
1959 pthru->logdrv = SCP2LUN(scp);
1960 pthru->reqsenselen = 14;
1961 pthru->cdblen = scp->cmd_len;
1962
1963 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1964
32fbac22
FT
1965 if (scsi_bufflen(scp)) {
1966 pthru->dataxferlen = scsi_bufflen(scp);
1da177e4
LT
1967 pthru->dataxferaddr = ccb->sgl_dma_h;
1968 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
1969 }
1970 else {
1971 pthru->dataxferaddr = 0;
1972 pthru->dataxferlen = 0;
1973 pthru->numsge = 0;
1974 }
1975 return;
1976}
1977
1978
1979/**
1980 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
a69b74d3
RD
1981 * @adapter : pointer to controller's soft state
1982 * @scb : scsi control block
1983 * @scp : scsi command from the mid-layer
1da177e4 1984 *
3948ff8b 1985 * Prepare a command for the scsi physical devices. This routine prepares
a69b74d3 1986 * commands for devices which can take extended CDBs (>10 bytes).
1da177e4
LT
1987 */
1988static void
1989megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
1990 struct scsi_cmnd *scp)
1991{
1992 mbox_ccb_t *ccb;
1993 mraid_epassthru_t *epthru;
1994 uint8_t channel;
1995 uint8_t target;
1996
1997 ccb = (mbox_ccb_t *)scb->ccb;
1998 epthru = ccb->epthru;
1999 channel = scb->dev_channel;
2000 target = scb->dev_target;
2001
2002 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2003 epthru->timeout = 4;
2004 epthru->ars = 1;
2005 epthru->islogical = 0;
2006 epthru->channel = 0;
2007 epthru->target = (channel << 4) | target;
2008 epthru->logdrv = SCP2LUN(scp);
2009 epthru->reqsenselen = 14;
2010 epthru->cdblen = scp->cmd_len;
2011
2012 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2013
32fbac22
FT
2014 if (scsi_bufflen(scp)) {
2015 epthru->dataxferlen = scsi_bufflen(scp);
1da177e4
LT
2016 epthru->dataxferaddr = ccb->sgl_dma_h;
2017 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2018 }
2019 else {
2020 epthru->dataxferaddr = 0;
2021 epthru->dataxferlen = 0;
2022 epthru->numsge = 0;
2023 }
2024 return;
2025}
2026
2027
2028/**
2029 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
a69b74d3 2030 * @adapter : controller's soft state
1da177e4 2031 *
a69b74d3 2032 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
1da177e4
LT
2033 * completed command and put them on the completed list for later processing.
2034 *
2035 * Returns: 1 if the interrupt is valid, 0 otherwise
2036 */
858119e1 2037static int
1da177e4
LT
2038megaraid_ack_sequence(adapter_t *adapter)
2039{
2040 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2041 mbox_t *mbox;
2042 scb_t *scb;
2043 uint8_t nstatus;
2044 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2045 struct list_head clist;
2046 int handled;
2047 uint32_t dword;
2048 unsigned long flags;
2049 int i, j;
2050
2051
2052 mbox = raid_dev->mbox;
2053
2054 // move the SCBs from the firmware completed array to our local list
2055 INIT_LIST_HEAD(&clist);
2056
2057 // loop till F/W has more commands for us to complete
2058 handled = 0;
2059 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2060 do {
2061 /*
2062 * Check if a valid interrupt is pending. If found, force the
2063 * interrupt line low.
2064 */
2065 dword = RDOUTDOOR(raid_dev);
2066 if (dword != 0x10001234) break;
2067
2068 handled = 1;
2069
2070 WROUTDOOR(raid_dev, 0x10001234);
2071
2072 nstatus = 0;
2073 // wait for valid numstatus to post
2074 for (i = 0; i < 0xFFFFF; i++) {
2075 if (mbox->numstatus != 0xFF) {
2076 nstatus = mbox->numstatus;
2077 break;
2078 }
2079 rmb();
2080 }
2081 mbox->numstatus = 0xFF;
2082
2083 adapter->outstanding_cmds -= nstatus;
2084
2085 for (i = 0; i < nstatus; i++) {
2086
2087 // wait for valid command index to post
2088 for (j = 0; j < 0xFFFFF; j++) {
2089 if (mbox->completed[i] != 0xFF) break;
2090 rmb();
2091 }
2092 completed[i] = mbox->completed[i];
2093 mbox->completed[i] = 0xFF;
2094
2095 if (completed[i] == 0xFF) {
2096 con_log(CL_ANN, (KERN_CRIT
2097 "megaraid: command posting timed out\n"));
2098
2099 BUG();
2100 continue;
2101 }
2102
2103 // Get SCB associated with this command id
2104 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2105 // a cmm command
2106 scb = adapter->uscb_list + (completed[i] -
2107 MBOX_MAX_SCSI_CMDS);
2108 }
2109 else {
2110 // an os command
2111 scb = adapter->kscb_list + completed[i];
2112 }
2113
2114 scb->status = mbox->status;
2115 list_add_tail(&scb->list, &clist);
2116 }
2117
2118 // Acknowledge interrupt
2119 WRINDOOR(raid_dev, 0x02);
2120
2121 } while(1);
2122
2123 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2124
2125
2126 // put the completed commands in the completed list. DPC would
2127 // complete these commands later
2128 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2129
2130 list_splice(&clist, &adapter->completed_list);
2131
2132 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2133
2134
2135 // schedule the DPC if there is some work for it
2136 if (handled)
2137 tasklet_schedule(&adapter->dpc_h);
2138
2139 return handled;
2140}
2141
2142
2143/**
2144 * megaraid_isr - isr for memory based mailbox based controllers
a69b74d3
RD
2145 * @irq : irq
2146 * @devp : pointer to our soft state
1da177e4
LT
2147 *
2148 * Interrupt service routine for memory-mapped mailbox controllers.
2149 */
2150static irqreturn_t
7d12e780 2151megaraid_isr(int irq, void *devp)
1da177e4
LT
2152{
2153 adapter_t *adapter = devp;
2154 int handled;
2155
2156 handled = megaraid_ack_sequence(adapter);
2157
2158 /* Loop through any pending requests */
2159 if (!adapter->quiescent) {
2160 megaraid_mbox_runpendq(adapter, NULL);
2161 }
2162
2163 return IRQ_RETVAL(handled);
2164}
2165
2166
1da177e4
LT
2167/**
2168 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2169 * @devp : pointer to HBA soft state
2170 *
2171 * Pick up the commands from the completed list and send back to the owners.
2172 * This is a reentrant function and does not assume any locks are held while
2173 * it is being called.
2174 */
2175static void
2176megaraid_mbox_dpc(unsigned long devp)
2177{
2178 adapter_t *adapter = (adapter_t *)devp;
2179 mraid_device_t *raid_dev;
2180 struct list_head clist;
2181 struct scatterlist *sgl;
2182 scb_t *scb;
2183 scb_t *tmp;
2184 struct scsi_cmnd *scp;
2185 mraid_passthru_t *pthru;
2186 mraid_epassthru_t *epthru;
2187 mbox_ccb_t *ccb;
2188 int islogical;
2189 int pdev_index;
2190 int pdev_state;
2191 mbox_t *mbox;
2192 unsigned long flags;
2193 uint8_t c;
2194 int status;
c005fb4f 2195 uioc_t *kioc;
1da177e4
LT
2196
2197
2198 if (!adapter) return;
2199
2200 raid_dev = ADAP2RAIDDEV(adapter);
2201
2202 // move the SCBs from the completed list to our local list
2203 INIT_LIST_HEAD(&clist);
2204
2205 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2206
2207 list_splice_init(&adapter->completed_list, &clist);
2208
2209 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2210
2211
2212 list_for_each_entry_safe(scb, tmp, &clist, list) {
2213
2214 status = scb->status;
2215 scp = scb->scp;
2216 ccb = (mbox_ccb_t *)scb->ccb;
2217 pthru = ccb->pthru;
2218 epthru = ccb->epthru;
2219 mbox = ccb->mbox;
2220
2221 // Make sure f/w has completed a valid command
2222 if (scb->state != SCB_ISSUED) {
2223 con_log(CL_ANN, (KERN_CRIT
2224 "megaraid critical err: invalid command %d:%d:%p\n",
2225 scb->sno, scb->state, scp));
2226 BUG();
2227 continue; // Must never happen!
2228 }
2229
2230 // check for the management command and complete it right away
2231 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2232 scb->state = SCB_FREE;
2233 scb->status = status;
2234
2235 // remove from local clist
2236 list_del_init(&scb->list);
2237
c005fb4f
JS
2238 kioc = (uioc_t *)scb->gp;
2239 kioc->status = 0;
2240
1da177e4
LT
2241 megaraid_mbox_mm_done(adapter, scb);
2242
2243 continue;
2244 }
2245
2246 // Was an abort issued for this command earlier
2247 if (scb->state & SCB_ABORT) {
2248 con_log(CL_ANN, (KERN_NOTICE
5cd049a5
CH
2249 "megaraid: aborted cmd [%x] completed\n",
2250 scb->sno));
1da177e4
LT
2251 }
2252
2253 /*
2254 * If the inquiry came of a disk drive which is not part of
2255 * any RAID array, expose it to the kernel. For this to be
2256 * enabled, user must set the "megaraid_expose_unconf_disks"
2257 * flag to 1 by specifying it on module parameter list.
2258 * This would enable data migration off drives from other
2259 * configurations.
2260 */
2261 islogical = MRAID_IS_LOGICAL(adapter, scp);
2262 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2263 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2264
32fbac22 2265 sgl = scsi_sglist(scp);
45711f1a
JA
2266 if (sg_page(sgl)) {
2267 c = *(unsigned char *) sg_virt(&sgl[0]);
32fbac22
FT
2268 } else {
2269 con_log(CL_ANN, (KERN_WARNING
2270 "megaraid mailbox: invalid sg:%d\n",
2271 __LINE__));
2272 c = 0;
1da177e4
LT
2273 }
2274
2275 if ((c & 0x1F ) == TYPE_DISK) {
2276 pdev_index = (scb->dev_channel * 16) +
2277 scb->dev_target;
2278 pdev_state =
2279 raid_dev->pdrv_state[pdev_index] & 0x0F;
2280
2281 if (pdev_state == PDRV_ONLINE ||
2282 pdev_state == PDRV_FAILED ||
2283 pdev_state == PDRV_RBLD ||
2284 pdev_state == PDRV_HOTSPARE ||
2285 megaraid_expose_unconf_disks == 0) {
2286
2287 status = 0xF0;
2288 }
2289 }
2290 }
2291
2292 // Convert MegaRAID status to Linux error code
2293 switch (status) {
2294
2295 case 0x00:
2296
2297 scp->result = (DID_OK << 16);
2298 break;
2299
2300 case 0x02:
2301
2302 /* set sense_buffer and result fields */
2303 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2304 mbox->cmd == MBOXCMD_PASSTHRU64) {
2305
2306 memcpy(scp->sense_buffer, pthru->reqsensearea,
2307 14);
2308
2309 scp->result = DRIVER_SENSE << 24 |
2310 DID_OK << 16 | CHECK_CONDITION << 1;
2311 }
2312 else {
2313 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2314
2315 memcpy(scp->sense_buffer,
2316 epthru->reqsensearea, 14);
2317
2318 scp->result = DRIVER_SENSE << 24 |
2319 DID_OK << 16 |
2320 CHECK_CONDITION << 1;
2321 } else {
2322 scp->sense_buffer[0] = 0x70;
2323 scp->sense_buffer[2] = ABORTED_COMMAND;
2324 scp->result = CHECK_CONDITION << 1;
2325 }
2326 }
2327 break;
2328
2329 case 0x08:
2330
2331 scp->result = DID_BUS_BUSY << 16 | status;
2332 break;
2333
2334 default:
2335
2336 /*
2337 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2338 * failed
2339 */
2340 if (scp->cmnd[0] == TEST_UNIT_READY) {
2341 scp->result = DID_ERROR << 16 |
2342 RESERVATION_CONFLICT << 1;
2343 }
2344 else
2345 /*
2346 * Error code returned is 1 if Reserve or Release
2347 * failed or the input parameter is invalid
2348 */
2349 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2350 scp->cmnd[0] == RELEASE)) {
2351
2352 scp->result = DID_ERROR << 16 |
2353 RESERVATION_CONFLICT << 1;
2354 }
2355 else {
2356 scp->result = DID_BAD_TARGET << 16 | status;
2357 }
2358 }
2359
2360 // print a debug message for all failed commands
2361 if (status) {
2362 megaraid_mbox_display_scb(adapter, scb);
2363 }
2364
67d98f0a 2365 scsi_dma_unmap(scp);
1da177e4
LT
2366
2367 // remove from local clist
2368 list_del_init(&scb->list);
2369
2370 // put back in free list
2371 megaraid_dealloc_scb(adapter, scb);
2372
2373 // send the scsi packet back to kernel
1da177e4 2374 scp->scsi_done(scp);
1da177e4
LT
2375 }
2376
2377 return;
2378}
2379
2380
2381/**
2382 * megaraid_abort_handler - abort the scsi command
2383 * @scp : command to be aborted
2384 *
2385 * Abort a previous SCSI request. Only commands on the pending list can be
2386 * aborted. All the commands issued to the F/W must complete.
2387 **/
2388static int
f2c8dc40 2389megaraid_abort_handler(struct scsi_cmnd *scp)
1da177e4
LT
2390{
2391 adapter_t *adapter;
2392 mraid_device_t *raid_dev;
2393 scb_t *scb;
2394 scb_t *tmp;
2395 int found;
2396 unsigned long flags;
2397 int i;
2398
2399
2400 adapter = SCP2ADAPTER(scp);
2401 raid_dev = ADAP2RAIDDEV(adapter);
2402
1da177e4 2403 con_log(CL_ANN, (KERN_WARNING
5cd049a5
CH
2404 "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
2405 scp->cmnd[0], SCP2CHANNEL(scp),
1da177e4
LT
2406 SCP2TARGET(scp), SCP2LUN(scp)));
2407
2408 // If FW has stopped responding, simply return failure
2409 if (raid_dev->hw_error) {
2410 con_log(CL_ANN, (KERN_NOTICE
2411 "megaraid: hw error, not aborting\n"));
2412 return FAILED;
2413 }
2414
2415 // There might a race here, where the command was completed by the
2416 // firmware and now it is on the completed list. Before we could
2417 // complete the command to the kernel in dpc, the abort came.
2418 // Find out if this is the case to avoid the race.
2419 scb = NULL;
2420 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2421 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2422
2423 if (scb->scp == scp) { // Found command
2424
2425 list_del_init(&scb->list); // from completed list
2426
2427 con_log(CL_ANN, (KERN_WARNING
5cd049a5
CH
2428 "megaraid: %d[%d:%d], abort from completed list\n",
2429 scb->sno, scb->dev_channel, scb->dev_target));
1da177e4
LT
2430
2431 scp->result = (DID_ABORT << 16);
2432 scp->scsi_done(scp);
2433
2434 megaraid_dealloc_scb(adapter, scb);
2435
2436 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2437 flags);
2438
2439 return SUCCESS;
2440 }
2441 }
2442 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2443
2444
2445 // Find out if this command is still on the pending list. If it is and
2446 // was never issued, abort and return success. If the command is owned
2447 // by the firmware, we must wait for it to complete by the FW.
2448 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2449 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2450
2451 if (scb->scp == scp) { // Found command
2452
2453 list_del_init(&scb->list); // from pending list
2454
2455 ASSERT(!(scb->state & SCB_ISSUED));
2456
2457 con_log(CL_ANN, (KERN_WARNING
5cd049a5
CH
2458 "megaraid abort: [%d:%d], driver owner\n",
2459 scb->dev_channel, scb->dev_target));
1da177e4
LT
2460
2461 scp->result = (DID_ABORT << 16);
2462 scp->scsi_done(scp);
2463
2464 megaraid_dealloc_scb(adapter, scb);
2465
2466 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2467 flags);
2468
2469 return SUCCESS;
2470 }
2471 }
2472 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2473
2474
2475 // Check do we even own this command, in which case this would be
2476 // owned by the firmware. The only way to locate the FW scb is to
2477 // traverse through the list of all SCB, since driver does not
2478 // maintain these SCBs on any list
2479 found = 0;
f2c8dc40 2480 spin_lock_irq(&adapter->lock);
1da177e4
LT
2481 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2482 scb = adapter->kscb_list + i;
2483
2484 if (scb->scp == scp) {
2485
2486 found = 1;
2487
2488 if (!(scb->state & SCB_ISSUED)) {
2489 con_log(CL_ANN, (KERN_WARNING
5cd049a5
CH
2490 "megaraid abort: %d[%d:%d], invalid state\n",
2491 scb->sno, scb->dev_channel, scb->dev_target));
1da177e4
LT
2492 BUG();
2493 }
2494 else {
2495 con_log(CL_ANN, (KERN_WARNING
5cd049a5
CH
2496 "megaraid abort: %d[%d:%d], fw owner\n",
2497 scb->sno, scb->dev_channel, scb->dev_target));
1da177e4
LT
2498 }
2499 }
2500 }
f2c8dc40 2501 spin_unlock_irq(&adapter->lock);
1da177e4
LT
2502
2503 if (!found) {
5cd049a5 2504 con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
1da177e4
LT
2505
2506 // FIXME: Should there be a callback for this command?
2507 return SUCCESS;
2508 }
2509
2510 // We cannot actually abort a command owned by firmware, return
2511 // failure and wait for reset. In host reset handler, we will find out
2512 // if the HBA is still live
2513 return FAILED;
2514}
2515
1da177e4 2516/**
3948ff8b 2517 * megaraid_reset_handler - device reset handler for mailbox based driver
1da177e4
LT
2518 * @scp : reference command
2519 *
2520 * Reset handler for the mailbox based controller. First try to find out if
2521 * the FW is still live, in which case the outstanding commands counter mut go
2522 * down to 0. If that happens, also issue the reservation reset command to
2523 * relinquish (possible) reservations on the logical drives connected to this
a69b74d3 2524 * host.
1da177e4
LT
2525 **/
2526static int
f2c8dc40 2527megaraid_reset_handler(struct scsi_cmnd *scp)
1da177e4
LT
2528{
2529 adapter_t *adapter;
2530 scb_t *scb;
2531 scb_t *tmp;
2532 mraid_device_t *raid_dev;
2533 unsigned long flags;
2534 uint8_t raw_mbox[sizeof(mbox_t)];
2535 int rval;
2536 int recovery_window;
1da177e4 2537 int i;
c005fb4f 2538 uioc_t *kioc;
1da177e4
LT
2539
2540 adapter = SCP2ADAPTER(scp);
2541 raid_dev = ADAP2RAIDDEV(adapter);
2542
1da177e4
LT
2543 // return failure if adapter is not responding
2544 if (raid_dev->hw_error) {
2545 con_log(CL_ANN, (KERN_NOTICE
2546 "megaraid: hw error, cannot reset\n"));
2547 return FAILED;
2548 }
2549
1da177e4
LT
2550 // Under exceptional conditions, FW can take up to 3 minutes to
2551 // complete command processing. Wait for additional 2 minutes for the
2552 // pending commands counter to go down to 0. If it doesn't, let the
2553 // controller be marked offline
2554 // Also, reset all the commands currently owned by the driver
2555 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2556 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
1da177e4
LT
2557 list_del_init(&scb->list); // from pending list
2558
c005fb4f
JS
2559 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2560 con_log(CL_ANN, (KERN_WARNING
2561 "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2562 scb->sno, scb->dev_channel, scb->dev_target));
1da177e4 2563
c005fb4f 2564 scb->status = -1;
1da177e4 2565
c005fb4f
JS
2566 kioc = (uioc_t *)scb->gp;
2567 kioc->status = -EFAULT;
2568
2569 megaraid_mbox_mm_done(adapter, scb);
2570 } else {
2571 if (scb->scp == scp) { // Found command
2572 con_log(CL_ANN, (KERN_WARNING
5cd049a5
CH
2573 "megaraid: %d[%d:%d], reset from pending list\n",
2574 scb->sno, scb->dev_channel, scb->dev_target));
c005fb4f
JS
2575 } else {
2576 con_log(CL_ANN, (KERN_WARNING
2577 "megaraid: IO packet with %d[%d:%d] being reset\n",
2578 scb->sno, scb->dev_channel, scb->dev_target));
2579 }
2580
2581 scb->scp->result = (DID_RESET << 16);
2582 scb->scp->scsi_done(scb->scp);
2583
2584 megaraid_dealloc_scb(adapter, scb);
2585 }
1da177e4
LT
2586 }
2587 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2588
2589 if (adapter->outstanding_cmds) {
2590 con_log(CL_ANN, (KERN_NOTICE
2591 "megaraid: %d outstanding commands. Max wait %d sec\n",
c005fb4f
JS
2592 adapter->outstanding_cmds,
2593 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
1da177e4
LT
2594 }
2595
1da177e4
LT
2596 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2597
c005fb4f 2598 for (i = 0; i < recovery_window; i++) {
1da177e4
LT
2599
2600 megaraid_ack_sequence(adapter);
2601
2602 // print a message once every 5 seconds only
2603 if (!(i % 5)) {
2604 con_log(CL_ANN, (
2605 "megaraid mbox: Wait for %d commands to complete:%d\n",
2606 adapter->outstanding_cmds,
c005fb4f 2607 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
1da177e4
LT
2608 }
2609
25985edc 2610 // bailout if no recovery happened in reset time
c005fb4f 2611 if (adapter->outstanding_cmds == 0) {
1da177e4
LT
2612 break;
2613 }
2614
2615 msleep(1000);
2616 }
2617
f2c8dc40 2618 spin_lock(&adapter->lock);
1da177e4
LT
2619
2620 // If still outstanding commands, bail out
2621 if (adapter->outstanding_cmds) {
2622 con_log(CL_ANN, (KERN_WARNING
2623 "megaraid mbox: critical hardware error!\n"));
2624
2625 raid_dev->hw_error = 1;
2626
f2c8dc40
CH
2627 rval = FAILED;
2628 goto out;
1da177e4
LT
2629 }
2630 else {
2631 con_log(CL_ANN, (KERN_NOTICE
af901ca1 2632 "megaraid mbox: reset sequence completed successfully\n"));
1da177e4
LT
2633 }
2634
2635
2636 // If the controller supports clustering, reset reservations
f2c8dc40
CH
2637 if (!adapter->ha) {
2638 rval = SUCCESS;
2639 goto out;
2640 }
1da177e4
LT
2641
2642 // clear reservations if any
2643 raw_mbox[0] = CLUSTER_CMD;
2644 raw_mbox[2] = RESET_RESERVATIONS;
2645
2646 rval = SUCCESS;
2647 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2648 con_log(CL_ANN,
2649 (KERN_INFO "megaraid: reservation reset\n"));
2650 }
2651 else {
2652 rval = FAILED;
2653 con_log(CL_ANN, (KERN_WARNING
2654 "megaraid: reservation reset failed\n"));
2655 }
2656
f2c8dc40 2657 out:
6548b0e5 2658 spin_unlock(&adapter->lock);
1da177e4
LT
2659 return rval;
2660}
2661
1da177e4
LT
2662/*
2663 * START: internal commands library
2664 *
2665 * This section of the driver has the common routine used by the driver and
2666 * also has all the FW routines
2667 */
2668
2669/**
2670 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
a69b74d3
RD
2671 * @adapter : controller's soft state
2672 * @raw_mbox : the mailbox
1da177e4
LT
2673 *
2674 * Issue a scb in synchronous and non-interrupt mode for mailbox based
a69b74d3 2675 * controllers.
1da177e4
LT
2676 */
2677static int
2678mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2679{
2680 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1da177e4
LT
2681 mbox_t *mbox;
2682 uint8_t status;
2683 int i;
2684
1da177e4
LT
2685 mbox = raid_dev->mbox;
2686
2687 /*
2688 * Wait until mailbox is free
2689 */
2690 if (megaraid_busywait_mbox(raid_dev) != 0)
2691 goto blocked_mailbox;
2692
2693 /*
2694 * Copy mailbox data into host structure
2695 */
2696 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2697 mbox->cmdid = 0xFE;
2698 mbox->busy = 1;
2699 mbox->poll = 0;
2700 mbox->ack = 0;
2701 mbox->numstatus = 0xFF;
2702 mbox->status = 0xFF;
2703
2704 wmb();
2705 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2706
2707 // wait for maximum 1 second for status to post. If the status is not
2708 // available within 1 second, assume FW is initializing and wait
2709 // for an extended amount of time
2710 if (mbox->numstatus == 0xFF) { // status not yet available
53b3531b 2711 udelay(25);
1da177e4
LT
2712
2713 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2714 rmb();
2715 msleep(1);
2716 }
2717
2718
2719 if (i == 1000) {
2720 con_log(CL_ANN, (KERN_NOTICE
2721 "megaraid mailbox: wait for FW to boot "));
2722
2723 for (i = 0; (mbox->numstatus == 0xFF) &&
2724 (i < MBOX_RESET_WAIT); i++) {
2725 rmb();
2726 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2727 MBOX_RESET_WAIT - i));
2728 msleep(1000);
2729 }
2730
2731 if (i == MBOX_RESET_WAIT) {
2732
2733 con_log(CL_ANN, (
2734 "\nmegaraid mailbox: status not available\n"));
2735
2736 return -1;
2737 }
2738 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2739 }
2740 }
2741
2742 // wait for maximum 1 second for poll semaphore
2743 if (mbox->poll != 0x77) {
2744 udelay(25);
2745
2746 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2747 rmb();
2748 msleep(1);
2749 }
2750
2751 if (i == 1000) {
2752 con_log(CL_ANN, (KERN_WARNING
2753 "megaraid mailbox: could not get poll semaphore\n"));
2754 return -1;
2755 }
2756 }
2757
2758 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2759 wmb();
2760
2761 // wait for maximum 1 second for acknowledgement
2762 if (RDINDOOR(raid_dev) & 0x2) {
2763 udelay(25);
2764
2765 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2766 rmb();
2767 msleep(1);
2768 }
2769
2770 if (i == 1000) {
2771 con_log(CL_ANN, (KERN_WARNING
2772 "megaraid mailbox: could not acknowledge\n"));
2773 return -1;
2774 }
2775 }
2776 mbox->poll = 0;
2777 mbox->ack = 0x77;
2778
2779 status = mbox->status;
2780
2781 // invalidate the completed command id array. After command
2782 // completion, firmware would write the valid id.
2783 mbox->numstatus = 0xFF;
2784 mbox->status = 0xFF;
2785 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2786 mbox->completed[i] = 0xFF;
2787 }
2788
2789 return status;
2790
2791blocked_mailbox:
2792
2793 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2794 return -1;
2795}
2796
2797
2798/**
2799 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
a69b74d3
RD
2800 * @adapter : controller's soft state
2801 * @raw_mbox : the mailbox
1da177e4
LT
2802 *
2803 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2804 * controllers. This is a faster version of the synchronous command and
a69b74d3 2805 * therefore can be called in interrupt-context as well.
1da177e4
LT
2806 */
2807static int
2808mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2809{
2810 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2811 mbox_t *mbox;
2812 long i;
2813
2814
2815 mbox = raid_dev->mbox;
2816
2817 // return immediately if the mailbox is busy
2818 if (mbox->busy) return -1;
2819
2820 // Copy mailbox data into host structure
2821 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2822 mbox->cmdid = 0xFE;
2823 mbox->busy = 1;
2824 mbox->poll = 0;
2825 mbox->ack = 0;
2826 mbox->numstatus = 0xFF;
2827 mbox->status = 0xFF;
2828
2829 wmb();
2830 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2831
c005fb4f 2832 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
1da177e4 2833 if (mbox->numstatus != 0xFF) break;
ed7e8ef7 2834 rmb();
c005fb4f 2835 udelay(MBOX_SYNC_DELAY_200);
1da177e4
LT
2836 }
2837
c005fb4f 2838 if (i == MBOX_SYNC_WAIT_CNT) {
1da177e4
LT
2839 // We may need to re-calibrate the counter
2840 con_log(CL_ANN, (KERN_CRIT
2841 "megaraid: fast sync command timed out\n"));
2842 }
2843
2844 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2845 wmb();
2846
2847 return mbox->status;
2848}
2849
2850
2851/**
2852 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
a69b74d3 2853 * @raid_dev : RAID device (HBA) soft state
1da177e4 2854 *
a69b74d3
RD
2855 * Wait until the controller's mailbox is available to accept more commands.
2856 * Wait for at most 1 second.
1da177e4
LT
2857 */
2858static int
2859megaraid_busywait_mbox(mraid_device_t *raid_dev)
2860{
2861 mbox_t *mbox = raid_dev->mbox;
2862 int i = 0;
2863
2864 if (mbox->busy) {
2865 udelay(25);
2866 for (i = 0; mbox->busy && i < 1000; i++)
2867 msleep(1);
2868 }
2869
2870 if (i < 1000) return 0;
2871 else return -1;
2872}
2873
2874
2875/**
2876 * megaraid_mbox_product_info - some static information about the controller
a69b74d3 2877 * @adapter : our soft state
1da177e4 2878 *
a69b74d3 2879 * Issue commands to the controller to grab some parameters required by our
1da177e4
LT
2880 * caller.
2881 */
2882static int
2883megaraid_mbox_product_info(adapter_t *adapter)
2884{
2885 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2886 mbox_t *mbox;
2887 uint8_t raw_mbox[sizeof(mbox_t)];
2888 mraid_pinfo_t *pinfo;
2889 dma_addr_t pinfo_dma_h;
2890 mraid_inquiry3_t *mraid_inq3;
2891 int i;
2892
2893
2894 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2895 mbox = (mbox_t *)raw_mbox;
2896
2897 /*
2898 * Issue an ENQUIRY3 command to find out certain adapter parameters,
2899 * e.g., max channels, max commands etc.
2900 */
750afb08
LC
2901 pinfo = dma_alloc_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2902 &pinfo_dma_h, GFP_KERNEL);
1da177e4
LT
2903 if (pinfo == NULL) {
2904 con_log(CL_ANN, (KERN_WARNING
cadbd4a5 2905 "megaraid: out of memory, %s %d\n", __func__,
1da177e4
LT
2906 __LINE__));
2907
2908 return -1;
2909 }
1da177e4
LT
2910
2911 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
2912 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
2913
2914 raw_mbox[0] = FC_NEW_CONFIG;
2915 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
2916 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
2917
2918 // Issue the command
2919 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2920
2921 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
2922
66e3a241 2923 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
1da177e4
LT
2924 pinfo, pinfo_dma_h);
2925
2926 return -1;
2927 }
2928
2929 /*
2930 * Collect information about state of each physical drive
2931 * attached to the controller. We will expose all the disks
2932 * which are not part of RAID
2933 */
2934 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
2935 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
2936 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
2937 }
2938
2939 /*
2940 * Get product info for information like number of channels,
2941 * maximum commands supported.
2942 */
2943 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2944 mbox->xferaddr = (uint32_t)pinfo_dma_h;
2945
2946 raw_mbox[0] = FC_NEW_CONFIG;
2947 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
2948
2949 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2950
2951 con_log(CL_ANN, (KERN_WARNING
2952 "megaraid: product info failed\n"));
2953
66e3a241 2954 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
1da177e4
LT
2955 pinfo, pinfo_dma_h);
2956
2957 return -1;
2958 }
2959
2960 /*
2961 * Setup some parameters for host, as required by our caller
2962 */
2963 adapter->max_channel = pinfo->nchannels;
2964
2965 /*
2966 * we will export all the logical drives on a single channel.
2967 * Add 1 since inquires do not come for inititor ID
2968 */
2969 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
2970 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
2971
2972 /*
2973 * These are the maximum outstanding commands for the scsi-layer
2974 */
2975 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
2976
2977 memset(adapter->fw_version, 0, VERSION_SIZE);
2978 memset(adapter->bios_version, 0, VERSION_SIZE);
2979
2980 memcpy(adapter->fw_version, pinfo->fw_version, 4);
2981 adapter->fw_version[4] = 0;
2982
2983 memcpy(adapter->bios_version, pinfo->bios_version, 4);
2984 adapter->bios_version[4] = 0;
2985
2986 con_log(CL_ANN, (KERN_NOTICE
2987 "megaraid: fw version:[%s] bios version:[%s]\n",
2988 adapter->fw_version, adapter->bios_version));
2989
66e3a241 2990 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t), pinfo,
1da177e4
LT
2991 pinfo_dma_h);
2992
2993 return 0;
2994}
2995
2996
2997
2998/**
2999 * megaraid_mbox_extended_cdb - check for support for extended CDBs
a69b74d3 3000 * @adapter : soft state for the controller
1da177e4 3001 *
a69b74d3
RD
3002 * This routine check whether the controller in question supports extended
3003 * ( > 10 bytes ) CDBs.
1da177e4
LT
3004 */
3005static int
3006megaraid_mbox_extended_cdb(adapter_t *adapter)
3007{
3008 mbox_t *mbox;
3009 uint8_t raw_mbox[sizeof(mbox_t)];
3010 int rval;
3011
3012 mbox = (mbox_t *)raw_mbox;
3013
3014 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3015 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3016
3017 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3018
3019 raw_mbox[0] = MAIN_MISC_OPCODE;
3020 raw_mbox[2] = SUPPORT_EXT_CDB;
3021
3022 /*
3023 * Issue the command
3024 */
3025 rval = 0;
3026 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3027 rval = -1;
3028 }
3029
3030 return rval;
3031}
3032
3033
3034/**
3035 * megaraid_mbox_support_ha - Do we support clustering
a69b74d3
RD
3036 * @adapter : soft state for the controller
3037 * @init_id : ID of the initiator
1da177e4
LT
3038 *
3039 * Determine if the firmware supports clustering and the ID of the initiator.
3040 */
3041static int
3042megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3043{
3044 mbox_t *mbox;
3045 uint8_t raw_mbox[sizeof(mbox_t)];
3046 int rval;
3047
3048
3049 mbox = (mbox_t *)raw_mbox;
3050
3051 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3052
3053 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3054
3055 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3056
3057 raw_mbox[0] = GET_TARGET_ID;
3058
3059 // Issue the command
3060 *init_id = 7;
3061 rval = -1;
3062 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3063
3064 *init_id = *(uint8_t *)adapter->ibuf;
3065
3066 con_log(CL_ANN, (KERN_INFO
3067 "megaraid: cluster firmware, initiator ID: %d\n",
3068 *init_id));
3069
3070 rval = 0;
3071 }
3072
3073 return rval;
3074}
3075
3076
3077/**
3078 * megaraid_mbox_support_random_del - Do we support random deletion
a69b74d3 3079 * @adapter : soft state for the controller
1da177e4 3080 *
a69b74d3 3081 * Determine if the firmware supports random deletion.
1da177e4
LT
3082 * Return: 1 is operation supported, 0 otherwise
3083 */
3084static int
3085megaraid_mbox_support_random_del(adapter_t *adapter)
3086{
1da177e4
LT
3087 uint8_t raw_mbox[sizeof(mbox_t)];
3088 int rval;
3089
69cd39e9
HR
3090 /*
3091 * Newer firmware on Dell CERC expect a different
3092 * random deletion handling, so disable it.
3093 */
3094 if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
3095 adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
3096 adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
3097 adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
3098 (adapter->fw_version[0] > '6' ||
3099 (adapter->fw_version[0] == '6' &&
3100 adapter->fw_version[2] > '6') ||
3101 (adapter->fw_version[0] == '6'
3102 && adapter->fw_version[2] == '6'
3103 && adapter->fw_version[3] > '1'))) {
3104 con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
3105 return 0;
3106 }
1da177e4 3107
1da177e4
LT
3108 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3109
3110 raw_mbox[0] = FC_DEL_LOGDRV;
3111 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3112
3113 // Issue the command
3114 rval = 0;
3115 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3116
3117 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3118
3119 rval = 1;
3120 }
3121
3122 return rval;
3123}
3124
3125
3126/**
3127 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
a69b74d3 3128 * @adapter : soft state for the controller
1da177e4
LT
3129 *
3130 * Find out the maximum number of scatter-gather elements supported by the
a69b74d3 3131 * firmware.
1da177e4
LT
3132 */
3133static int
3134megaraid_mbox_get_max_sg(adapter_t *adapter)
3135{
3136 mbox_t *mbox;
3137 uint8_t raw_mbox[sizeof(mbox_t)];
3138 int nsg;
3139
3140
3141 mbox = (mbox_t *)raw_mbox;
3142
3143 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3144
3145 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3146
3147 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3148
3149 raw_mbox[0] = MAIN_MISC_OPCODE;
3150 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3151
3152 // Issue the command
3153 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3154 nsg = *(uint8_t *)adapter->ibuf;
3155 }
3156 else {
3157 nsg = MBOX_DEFAULT_SG_SIZE;
3158 }
3159
3160 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3161
3162 return nsg;
3163}
3164
3165
3166/**
3167 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
a69b74d3 3168 * @adapter : soft state for the controller
1da177e4 3169 *
a69b74d3
RD
3170 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
3171 * can be exported as regular SCSI channels.
1da177e4
LT
3172 */
3173static void
3174megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3175{
3176 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3177 mbox_t *mbox;
3178 uint8_t raw_mbox[sizeof(mbox_t)];
3179
3180
3181 mbox = (mbox_t *)raw_mbox;
3182
3183 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3184
3185 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3186
3187 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3188
3189 raw_mbox[0] = CHNL_CLASS;
3190 raw_mbox[2] = GET_CHNL_CLASS;
3191
3192 // Issue the command. If the command fails, all channels are RAID
3193 // channels
3194 raid_dev->channel_class = 0xFF;
3195 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3196 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3197 }
3198
3199 return;
3200}
3201
3202
3203/**
3204 * megaraid_mbox_flush_cache - flush adapter and disks cache
a69b74d3 3205 * @adapter : soft state for the controller
1da177e4 3206 *
a69b74d3 3207 * Flush adapter cache followed by disks cache.
1da177e4
LT
3208 */
3209static void
3210megaraid_mbox_flush_cache(adapter_t *adapter)
3211{
1da177e4
LT
3212 uint8_t raw_mbox[sizeof(mbox_t)];
3213
1da177e4
LT
3214 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3215
3216 raw_mbox[0] = FLUSH_ADAPTER;
3217
3218 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3219 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3220 }
3221
3222 raw_mbox[0] = FLUSH_SYSTEM;
3223
3224 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3225 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3226 }
3227
3228 return;
3229}
3230
3231
cd96d96f
SP
3232/**
3233 * megaraid_mbox_fire_sync_cmd - fire the sync cmd
a69b74d3 3234 * @adapter : soft state for the controller
cd96d96f 3235 *
a69b74d3 3236 * Clears the pending cmds in FW and reinits its RAID structs.
cd96d96f
SP
3237 */
3238static int
3239megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
3240{
3241 mbox_t *mbox;
3242 uint8_t raw_mbox[sizeof(mbox_t)];
3243 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
cd96d96f
SP
3244 int status = 0;
3245 int i;
3246 uint32_t dword;
3247
3248 mbox = (mbox_t *)raw_mbox;
3249
3250 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3251
3252 raw_mbox[0] = 0xFF;
3253
cd96d96f
SP
3254 mbox = raid_dev->mbox;
3255
3256 /* Wait until mailbox is free */
3257 if (megaraid_busywait_mbox(raid_dev) != 0) {
3258 status = 1;
3259 goto blocked_mailbox;
3260 }
3261
3262 /* Copy mailbox data into host structure */
3263 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
3264 mbox->cmdid = 0xFE;
3265 mbox->busy = 1;
3266 mbox->poll = 0;
3267 mbox->ack = 0;
3268 mbox->numstatus = 0;
3269 mbox->status = 0;
3270
3271 wmb();
3272 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3273
3274 /* Wait for maximum 1 min for status to post.
3275 * If the Firmware SUPPORTS the ABOVE COMMAND,
3276 * mbox->cmd will be set to 0
3277 * else
3278 * the firmware will reject the command with
3279 * mbox->numstatus set to 1
3280 */
3281
3282 i = 0;
3283 status = 0;
3284 while (!mbox->numstatus && mbox->cmd == 0xFF) {
3285 rmb();
3286 msleep(1);
3287 i++;
3288 if (i > 1000 * 60) {
3289 status = 1;
3290 break;
3291 }
3292 }
3293 if (mbox->numstatus == 1)
3294 status = 1; /*cmd not supported*/
3295
3296 /* Check for interrupt line */
3297 dword = RDOUTDOOR(raid_dev);
3298 WROUTDOOR(raid_dev, dword);
3299 WRINDOOR(raid_dev,2);
3300
3301 return status;
3302
3303blocked_mailbox:
3304 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
3305 return status;
3306}
3307
1da177e4
LT
3308/**
3309 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
a69b74d3
RD
3310 * @adapter : controller's soft state
3311 * @scb : SCB to be displayed
3312 * @level : debug level for console print
1da177e4
LT
3313 *
3314 * Diplay information about the given SCB iff the current debug level is
a69b74d3 3315 * verbose.
1da177e4
LT
3316 */
3317static void
3318megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3319{
3320 mbox_ccb_t *ccb;
3321 struct scsi_cmnd *scp;
3322 mbox_t *mbox;
3323 int level;
3324 int i;
3325
3326
3327 ccb = (mbox_ccb_t *)scb->ccb;
3328 scp = scb->scp;
3329 mbox = ccb->mbox;
3330
3331 level = CL_DLEVEL3;
3332
3333 con_log(level, (KERN_NOTICE
3334 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3335 mbox->cmd, scb->sno));
3336
3337 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3338 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3339 mbox->numsge));
3340
3341 if (!scp) return;
3342
3343 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3344
3345 for (i = 0; i < scp->cmd_len; i++) {
3346 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3347 }
3348
3349 con_log(level, ("\n"));
3350
3351 return;
3352}
3353
3354
3355/**
3356 * megaraid_mbox_setup_device_map - manage device ids
3357 * @adapter : Driver's soft state
3358 *
3948ff8b 3359 * Manage the device ids to have an appropriate mapping between the kernel
1da177e4
LT
3360 * scsi addresses and megaraid scsi and logical drive addresses. We export
3361 * scsi devices on their actual addresses, whereas the logical drives are
3362 * exported on a virtual scsi channel.
a69b74d3 3363 */
1da177e4
LT
3364static void
3365megaraid_mbox_setup_device_map(adapter_t *adapter)
3366{
3367 uint8_t c;
3368 uint8_t t;
3369
3370 /*
3371 * First fill the values on the logical drive channel
3372 */
3373 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3374 adapter->device_ids[adapter->max_channel][t] =
3375 (t < adapter->init_id) ? t : t - 1;
3376
3377 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3378
3379 /*
3380 * Fill the values on the physical devices channels
3381 */
3382 for (c = 0; c < adapter->max_channel; c++)
3383 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3384 adapter->device_ids[c][t] = (c << 8) | t;
3385}
3386
3387
3388/*
3389 * END: internal commands library
3390 */
3391
3392/*
3393 * START: Interface for the common management module
3394 *
b1c11812 3395 * This is the module, which interfaces with the common management module to
1da177e4
LT
3396 * provide support for ioctl and sysfs
3397 */
3398
3399/**
b1c11812 3400 * megaraid_cmm_register - register with the management module
a69b74d3 3401 * @adapter : HBA soft state
1da177e4
LT
3402 *
3403 * Register with the management module, which allows applications to issue
3404 * ioctl calls to the drivers. This interface is used by the management module
3405 * to setup sysfs support as well.
3406 */
3407static int
3408megaraid_cmm_register(adapter_t *adapter)
3409{
3410 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3411 mraid_mmadp_t adp;
3412 scb_t *scb;
3413 mbox_ccb_t *ccb;
3414 int rval;
3415 int i;
3416
3417 // Allocate memory for the base list of scb for management module.
dd00cc48 3418 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
1da177e4
LT
3419
3420 if (adapter->uscb_list == NULL) {
3421 con_log(CL_ANN, (KERN_WARNING
cadbd4a5 3422 "megaraid: out of memory, %s %d\n", __func__,
1da177e4
LT
3423 __LINE__));
3424 return -1;
3425 }
1da177e4
LT
3426
3427
3428 // Initialize the synchronization parameters for resources for
3429 // commands for management module
3430 INIT_LIST_HEAD(&adapter->uscb_pool);
3431
3432 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3433
3434
3435
3436 // link all the packets. Note, CCB for commands, coming from the
3437 // commom management module, mailbox physical address are already
3438 // setup by it. We just need placeholder for that in our local command
3439 // control blocks
3440 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3441
3442 scb = adapter->uscb_list + i;
3443 ccb = raid_dev->uccb_list + i;
3444
3445 scb->ccb = (caddr_t)ccb;
3446 ccb->mbox64 = raid_dev->umbox64 + i;
3447 ccb->mbox = &ccb->mbox64->mbox32;
3448 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3449
3450 scb->gp = 0;
3451
3452 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3453 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3454 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3455
3456 scb->scp = NULL;
3457 scb->state = SCB_FREE;
66e3a241 3458 scb->dma_direction = DMA_NONE;
1da177e4
LT
3459 scb->dma_type = MRAID_DMA_NONE;
3460 scb->dev_channel = -1;
3461 scb->dev_target = -1;
3462
3463 // put scb in the free pool
3464 list_add_tail(&scb->list, &adapter->uscb_pool);
3465 }
3466
3467 adp.unique_id = adapter->unique_id;
3468 adp.drvr_type = DRVRTYPE_MBOX;
3469 adp.drvr_data = (unsigned long)adapter;
3470 adp.pdev = adapter->pdev;
3471 adp.issue_uioc = megaraid_mbox_mm_handler;
c005fb4f 3472 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
1da177e4
LT
3473 adp.max_kioc = MBOX_MAX_USER_CMDS;
3474
3475 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3476
3477 con_log(CL_ANN, (KERN_WARNING
3478 "megaraid mbox: did not register with CMM\n"));
3479
3480 kfree(adapter->uscb_list);
3481 }
3482
3483 return rval;
3484}
3485
3486
3487/**
b1c11812 3488 * megaraid_cmm_unregister - un-register with the management module
a69b74d3 3489 * @adapter : HBA soft state
1da177e4
LT
3490 *
3491 * Un-register with the management module.
3492 * FIXME: mgmt module must return failure for unregister if it has pending
a69b74d3 3493 * commands in LLD.
1da177e4
LT
3494 */
3495static int
3496megaraid_cmm_unregister(adapter_t *adapter)
3497{
3498 kfree(adapter->uscb_list);
3499 mraid_mm_unregister_adp(adapter->unique_id);
3500 return 0;
3501}
3502
3503
3504/**
3505 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
a69b74d3
RD
3506 * @drvr_data : LLD specific data
3507 * @kioc : CMM interface packet
3508 * @action : command action
1da177e4 3509 *
b1c11812 3510 * This routine is invoked whenever the Common Management Module (CMM) has a
1da177e4
LT
3511 * command for us. The 'action' parameter specifies if this is a new command
3512 * or otherwise.
3513 */
3514static int
3515megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3516{
3517 adapter_t *adapter;
3518
3519 if (action != IOCTL_ISSUE) {
3520 con_log(CL_ANN, (KERN_WARNING
3521 "megaraid: unsupported management action:%#2x\n",
3522 action));
3523 return (-ENOTSUPP);
3524 }
3525
3526 adapter = (adapter_t *)drvr_data;
3527
3528 // make sure this adapter is not being detached right now.
3529 if (atomic_read(&adapter->being_detached)) {
3530 con_log(CL_ANN, (KERN_WARNING
3531 "megaraid: reject management request, detaching\n"));
3532 return (-ENODEV);
3533 }
3534
3535 switch (kioc->opcode) {
3536
3537 case GET_ADAP_INFO:
3538
3539 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3540 (unsigned long)kioc->buf_vaddr);
3541
3542 kioc->done(kioc);
3543
3544 return kioc->status;
3545
3546 case MBOX_CMD:
3547
3548 return megaraid_mbox_mm_command(adapter, kioc);
3549
3550 default:
3551 kioc->status = (-EINVAL);
3552 kioc->done(kioc);
3553 return (-EINVAL);
3554 }
3555
3556 return 0; // not reached
3557}
3558
3559/**
3560 * megaraid_mbox_mm_command - issues commands routed through CMM
a69b74d3
RD
3561 * @adapter : HBA soft state
3562 * @kioc : management command packet
1da177e4
LT
3563 *
3564 * Issues commands, which are routed through the management module.
3565 */
3566static int
3567megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3568{
3569 struct list_head *head = &adapter->uscb_pool;
3570 mbox64_t *mbox64;
3571 uint8_t *raw_mbox;
3572 scb_t *scb;
3573 mbox_ccb_t *ccb;
3574 unsigned long flags;
3575
3576 // detach one scb from free pool
3577 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3578
3579 if (list_empty(head)) { // should never happen because of CMM
3580
3581 con_log(CL_ANN, (KERN_WARNING
3582 "megaraid mbox: bug in cmm handler, lost resources\n"));
3583
3584 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3585
3586 return (-EINVAL);
3587 }
3588
3589 scb = list_entry(head->next, scb_t, list);
3590 list_del_init(&scb->list);
3591
3592 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3593
3594 scb->state = SCB_ACTIVE;
3595 scb->dma_type = MRAID_DMA_NONE;
66e3a241 3596 scb->dma_direction = DMA_NONE;
1da177e4
LT
3597
3598 ccb = (mbox_ccb_t *)scb->ccb;
3599 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3600 raw_mbox = (uint8_t *)&mbox64->mbox32;
3601
3602 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3603
3604 scb->gp = (unsigned long)kioc;
3605
3606 /*
3607 * If it is a logdrv random delete operation, we have to wait till
3608 * there are no outstanding cmds at the fw and then issue it directly
3609 */
3610 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3611
3612 if (wait_till_fw_empty(adapter)) {
3613 con_log(CL_ANN, (KERN_NOTICE
3614 "megaraid mbox: LD delete, timed out\n"));
3615
3616 kioc->status = -ETIME;
3617
3618 scb->status = -1;
3619
3620 megaraid_mbox_mm_done(adapter, scb);
3621
3622 return (-ETIME);
3623 }
3624
3625 INIT_LIST_HEAD(&scb->list);
3626
3627 scb->state = SCB_ISSUED;
3628 if (mbox_post_cmd(adapter, scb) != 0) {
3629
3630 con_log(CL_ANN, (KERN_NOTICE
3631 "megaraid mbox: LD delete, mailbox busy\n"));
3632
3633 kioc->status = -EBUSY;
3634
3635 scb->status = -1;
3636
3637 megaraid_mbox_mm_done(adapter, scb);
3638
3639 return (-EBUSY);
3640 }
3641
3642 return 0;
3643 }
3644
3645 // put the command on the pending list and execute
3646 megaraid_mbox_runpendq(adapter, scb);
3647
3648 return 0;
3649}
3650
3651
3652static int
3653wait_till_fw_empty(adapter_t *adapter)
3654{
3655 unsigned long flags = 0;
3656 int i;
3657
3658
3659 /*
3660 * Set the quiescent flag to stop issuing cmds to FW.
3661 */
f2c8dc40 3662 spin_lock_irqsave(&adapter->lock, flags);
1da177e4 3663 adapter->quiescent++;
f2c8dc40 3664 spin_unlock_irqrestore(&adapter->lock, flags);
1da177e4
LT
3665
3666 /*
3667 * Wait till there are no more cmds outstanding at FW. Try for at most
3668 * 60 seconds
3669 */
3670 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3671 con_log(CL_DLEVEL1, (KERN_INFO
3672 "megaraid: FW has %d pending commands\n",
3673 adapter->outstanding_cmds));
3674
3675 msleep(1000);
3676 }
3677
3678 return adapter->outstanding_cmds;
3679}
3680
3681
3682/**
3683 * megaraid_mbox_mm_done - callback for CMM commands
3684 * @adapter : HBA soft state
3685 * @scb : completed command
3686 *
3687 * Callback routine for internal commands originated from the management
3688 * module.
3689 */
3690static void
3691megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3692{
3693 uioc_t *kioc;
3694 mbox64_t *mbox64;
3695 uint8_t *raw_mbox;
3696 unsigned long flags;
3697
3698 kioc = (uioc_t *)scb->gp;
1da177e4
LT
3699 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3700 mbox64->mbox32.status = scb->status;
3701 raw_mbox = (uint8_t *)&mbox64->mbox32;
3702
3703
3704 // put scb in the free pool
3705 scb->state = SCB_FREE;
3706 scb->scp = NULL;
3707
3708 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3709
3710 list_add(&scb->list, &adapter->uscb_pool);
3711
3712 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3713
3714 // if a delete logical drive operation succeeded, restart the
3715 // controller
3716 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3717
3718 adapter->quiescent--;
3719
3720 megaraid_mbox_runpendq(adapter, NULL);
3721 }
3722
3723 kioc->done(kioc);
3724
3725 return;
3726}
3727
3728
3729/**
3730 * gather_hbainfo - HBA characteristics for the applications
a69b74d3
RD
3731 * @adapter : HBA soft state
3732 * @hinfo : pointer to the caller's host info strucuture
1da177e4
LT
3733 */
3734static int
3735gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3736{
1da177e4
LT
3737 hinfo->pci_vendor_id = adapter->pdev->vendor;
3738 hinfo->pci_device_id = adapter->pdev->device;
3739 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3740 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3741
3742 hinfo->pci_bus = adapter->pdev->bus->number;
3743 hinfo->pci_dev_fn = adapter->pdev->devfn;
3744 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3745 hinfo->irq = adapter->host->irq;
3746 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3747
3748 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3749 hinfo->host_no = adapter->host->host_no;
3750
3751 return 0;
3752}
3753
3754/*
3755 * END: Interface for the common management module
3756 */
3757
3758
3759
3760/**
3761 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
a69b74d3 3762 * @adapter : controller's soft state
1da177e4
LT
3763 *
3764 * Allocate packets required to issue FW calls whenever the sysfs attributes
3765 * are read. These attributes would require up-to-date information from the
3766 * FW. Also set up resources for mutual exclusion to share these resources and
3767 * the wait queue.
3768 *
a69b74d3
RD
3769 * Return 0 on success.
3770 * Return -ERROR_CODE on failure.
1da177e4
LT
3771 */
3772static int
3773megaraid_sysfs_alloc_resources(adapter_t *adapter)
3774{
3775 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3776 int rval = 0;
3777
3778 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3779
3780 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3781
66e3a241
CH
3782 raid_dev->sysfs_buffer = dma_alloc_coherent(&adapter->pdev->dev,
3783 PAGE_SIZE, &raid_dev->sysfs_buffer_dma, GFP_KERNEL);
1da177e4
LT
3784
3785 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3786 !raid_dev->sysfs_buffer) {
3787
3788 con_log(CL_ANN, (KERN_WARNING
cadbd4a5 3789 "megaraid: out of memory, %s %d\n", __func__,
1da177e4
LT
3790 __LINE__));
3791
3792 rval = -ENOMEM;
3793
3794 megaraid_sysfs_free_resources(adapter);
3795 }
3796
0c2cc433 3797 mutex_init(&raid_dev->sysfs_mtx);
1da177e4
LT
3798
3799 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3800
3801 return rval;
3802}
3803
3804
3805/**
3806 * megaraid_sysfs_free_resources - free sysfs related resources
a69b74d3 3807 * @adapter : controller's soft state
1da177e4
LT
3808 *
3809 * Free packets allocated for sysfs FW commands
1da177e4
LT
3810 */
3811static void
3812megaraid_sysfs_free_resources(adapter_t *adapter)
3813{
3814 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3815
c9475cb0
JJ
3816 kfree(raid_dev->sysfs_uioc);
3817 kfree(raid_dev->sysfs_mbox64);
1da177e4
LT
3818
3819 if (raid_dev->sysfs_buffer) {
66e3a241 3820 dma_free_coherent(&adapter->pdev->dev, PAGE_SIZE,
1da177e4
LT
3821 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3822 }
3823}
3824
3825
3826/**
3827 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
a69b74d3 3828 * @uioc : completed packet
1da177e4
LT
3829 *
3830 * Callback routine called in the ISR/tasklet context for get ldmap call
1da177e4
LT
3831 */
3832static void
3833megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3834{
3835 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3836 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3837
3838 uioc->status = 0;
3839
3840 wake_up(&raid_dev->sysfs_wait_q);
3841}
3842
1da177e4
LT
3843/**
3844 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
c251a7be 3845 * @t : timed out timer
1da177e4
LT
3846 *
3847 * Timeout routine to recover and return to application, in case the adapter
a69b74d3
RD
3848 * has stopped responding. A timeout of 60 seconds for this command seems like
3849 * a good value.
1da177e4
LT
3850 */
3851static void
c251a7be 3852megaraid_sysfs_get_ldmap_timeout(struct timer_list *t)
1da177e4 3853{
c251a7be
KC
3854 struct uioc_timeout *timeout = from_timer(timeout, t, timer);
3855 uioc_t *uioc = timeout->uioc;
1da177e4
LT
3856 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3857 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3858
3859 uioc->status = -ETIME;
3860
3861 wake_up(&raid_dev->sysfs_wait_q);
3862}
3863
3864
3865/**
3866 * megaraid_sysfs_get_ldmap - get update logical drive map
a69b74d3 3867 * @adapter : controller's soft state
1da177e4
LT
3868 *
3869 * This routine will be called whenever user reads the logical drive
3870 * attributes, go get the current logical drive mapping table from the
b1c11812 3871 * firmware. We use the management API's to issue commands to the controller.
1da177e4
LT
3872 *
3873 * NOTE: The commands issuance functionality is not generalized and
3874 * implemented in context of "get ld map" command only. If required, the
3875 * command issuance logical can be trivially pulled out and implemented as a
25985edc 3876 * standalone library. For now, this should suffice since there is no other
1da177e4
LT
3877 * user of this interface.
3878 *
a69b74d3
RD
3879 * Return 0 on success.
3880 * Return -1 on failure.
1da177e4
LT
3881 */
3882static int
3883megaraid_sysfs_get_ldmap(adapter_t *adapter)
3884{
3885 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3886 uioc_t *uioc;
3887 mbox64_t *mbox64;
3888 mbox_t *mbox;
3889 char *raw_mbox;
c251a7be 3890 struct uioc_timeout timeout;
1da177e4
LT
3891 caddr_t ldmap;
3892 int rval = 0;
3893
3894 /*
3895 * Allow only one read at a time to go through the sysfs attributes
3896 */
0c2cc433 3897 mutex_lock(&raid_dev->sysfs_mtx);
1da177e4
LT
3898
3899 uioc = raid_dev->sysfs_uioc;
3900 mbox64 = raid_dev->sysfs_mbox64;
3901 ldmap = raid_dev->sysfs_buffer;
3902
3903 memset(uioc, 0, sizeof(uioc_t));
3904 memset(mbox64, 0, sizeof(mbox64_t));
3905 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
3906
3907 mbox = &mbox64->mbox32;
3908 raw_mbox = (char *)mbox;
3909 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
3910 uioc->buf_vaddr = (caddr_t)adapter;
3911 uioc->status = -ENODATA;
3912 uioc->done = megaraid_sysfs_get_ldmap_done;
3913
3914 /*
3915 * Prepare the mailbox packet to get the current logical drive mapping
3916 * table
3917 */
3918 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
3919
3920 raw_mbox[0] = FC_DEL_LOGDRV;
3921 raw_mbox[2] = OP_GET_LDID_MAP;
3922
3923 /*
3924 * Setup a timer to recover from a non-responding controller
3925 */
c251a7be
KC
3926 timeout.uioc = uioc;
3927 timer_setup_on_stack(&timeout.timer,
3928 megaraid_sysfs_get_ldmap_timeout, 0);
1da177e4 3929
c251a7be
KC
3930 timeout.timer.expires = jiffies + 60 * HZ;
3931 add_timer(&timeout.timer);
1da177e4
LT
3932
3933 /*
3934 * Send the command to the firmware
3935 */
3936 rval = megaraid_mbox_mm_command(adapter, uioc);
3937
3938 if (rval == 0) { // command successfully issued
3939 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
3940
3941 /*
3942 * Check if the command timed out
3943 */
3944 if (uioc->status == -ETIME) {
3945 con_log(CL_ANN, (KERN_NOTICE
3946 "megaraid: sysfs get ld map timed out\n"));
3947
3948 rval = -ETIME;
3949 }
3950 else {
3951 rval = mbox->status;
3952 }
3953
3954 if (rval == 0) {
3955 memcpy(raid_dev->curr_ldmap, ldmap,
3956 sizeof(raid_dev->curr_ldmap));
3957 }
3958 else {
3959 con_log(CL_ANN, (KERN_NOTICE
3960 "megaraid: get ld map failed with %x\n", rval));
3961 }
3962 }
3963 else {
3964 con_log(CL_ANN, (KERN_NOTICE
3965 "megaraid: could not issue ldmap command:%x\n", rval));
3966 }
3967
3968
c251a7be
KC
3969 del_timer_sync(&timeout.timer);
3970 destroy_timer_on_stack(&timeout.timer);
1da177e4 3971
0c2cc433 3972 mutex_unlock(&raid_dev->sysfs_mtx);
1da177e4
LT
3973
3974 return rval;
3975}
3976
3977
3978/**
3979 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
a69b74d3
RD
3980 * @cdev : class device object representation for the host
3981 * @buf : buffer to send data to
1da177e4
LT
3982 *
3983 * Display the handle used by the applications while executing management
3984 * tasks on the adapter. We invoke a management module API to get the adapter
3985 * handle, since we do not interface with applications directly.
1da177e4
LT
3986 */
3987static ssize_t
ee959b00
TJ
3988megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
3989 char *buf)
1da177e4 3990{
ee959b00 3991 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3992 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
3993 uint32_t app_hndl;
3994
3995 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
3996
3997 return snprintf(buf, 8, "%u\n", app_hndl);
3998}
3999
4000
4001/**
4002 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
a69b74d3
RD
4003 * @dev : device object representation for the scsi device
4004 * @attr : device attribute to show
4005 * @buf : buffer to send data to
1da177e4
LT
4006 *
4007 * Display the logical drive number for the device in question, if it a valid
a69b74d3
RD
4008 * logical drive. For physical devices, "-1" is returned.
4009 *
4010 * The logical drive number is displayed in following format:
1da177e4
LT
4011 *
4012 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
1da177e4 4013 *
a69b74d3 4014 * <int> <int> <int> <int>
1da177e4
LT
4015 */
4016static ssize_t
10523b3b 4017megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
4018{
4019 struct scsi_device *sdev = to_scsi_device(dev);
4020 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4021 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4022 int scsi_id = -1;
4023 int logical_drv = -1;
4024 int ldid_map = -1;
4025 uint32_t app_hndl = 0;
4026 int mapped_sdev_id;
4027 int rval;
4028 int i;
4029
4030 if (raid_dev->random_del_supported &&
4031 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4032
4033 rval = megaraid_sysfs_get_ldmap(adapter);
4034 if (rval == 0) {
4035
4036 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4037
4038 mapped_sdev_id = sdev->id;
4039
4040 if (sdev->id > adapter->init_id) {
4041 mapped_sdev_id -= 1;
4042 }
4043
4044 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4045
4046 scsi_id = sdev->id;
4047
4048 logical_drv = i;
4049
4050 ldid_map = raid_dev->curr_ldmap[i];
4051
4052 app_hndl = mraid_mm_adapter_app_handle(
4053 adapter->unique_id);
4054
4055 break;
4056 }
4057 }
4058 }
4059 else {
4060 con_log(CL_ANN, (KERN_NOTICE
4061 "megaraid: sysfs get ld map failed: %x\n",
4062 rval));
4063 }
4064 }
4065
4066 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4067 ldid_map, app_hndl);
4068}
4069
4070
4071/*
4072 * END: Mailbox Low Level Driver
4073 */
4074module_init(megaraid_init);
4075module_exit(megaraid_exit);
4076
4077/* vim: set ts=8 sw=8 tw=78 ai si: */