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