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