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