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