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#include <linux/mm.h>
35#include <linux/fs.h>
36#include <linux/blkdev.h>
37#include <asm/uaccess.h>
38#include <asm/io.h>
39#include <linux/completion.h>
40#include <linux/delay.h>
41#include <linux/proc_fs.h>
42#include <linux/seq_file.h>
43#include <linux/reboot.h>
44#include <linux/module.h>
45#include <linux/list.h>
46#include <linux/interrupt.h>
47#include <linux/pci.h>
48#include <linux/init.h>
49#include <linux/dma-mapping.h>
50#include <linux/mutex.h>
51#include <linux/slab.h>
52#include <scsi/scsicam.h>
53
54#include "scsi.h"
55#include <scsi/scsi_host.h>
56
57#include "megaraid.h"
58
59#define MEGARAID_MODULE_VERSION "2.00.4"
60
61MODULE_AUTHOR ("sju@lsil.com");
62MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
63MODULE_LICENSE ("GPL");
64MODULE_VERSION(MEGARAID_MODULE_VERSION);
65
66static DEFINE_MUTEX(megadev_mutex);
67static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
68module_param(max_cmd_per_lun, uint, 0);
69MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
70
71static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
72module_param(max_sectors_per_io, ushort, 0);
73MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
74
75
76static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
77module_param(max_mbox_busy_wait, ushort, 0);
78MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
79
80#define RDINDOOR(adapter) readl((adapter)->mmio_base + 0x20)
81#define RDOUTDOOR(adapter) readl((adapter)->mmio_base + 0x2C)
82#define WRINDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x20)
83#define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
84
85
86
87
88
89static int hba_count;
90static adapter_t *hba_soft_state[MAX_CONTROLLERS];
91static struct proc_dir_entry *mega_proc_dir_entry;
92
93
94static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
95
96static long
97megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
98
99
100
101
102static const struct file_operations megadev_fops = {
103 .owner = THIS_MODULE,
104 .unlocked_ioctl = megadev_unlocked_ioctl,
105 .open = megadev_open,
106 .llseek = noop_llseek,
107};
108
109
110
111
112
113
114static struct mcontroller mcontroller[MAX_CONTROLLERS];
115
116
117static u32 driver_ver = 0x02000000;
118
119
120static int major;
121
122#define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
123
124
125
126
127
128static int trace_level;
129
130
131
132
133
134
135
136static int
137mega_setup_mailbox(adapter_t *adapter)
138{
139 unsigned long align;
140
141 adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
142 sizeof(mbox64_t), &adapter->una_mbox64_dma);
143
144 if( !adapter->una_mbox64 ) return -1;
145
146 adapter->mbox = &adapter->una_mbox64->mbox;
147
148 adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
149 (~0UL ^ 0xFUL));
150
151 adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
152
153 align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
154
155 adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
156
157
158
159
160 if( adapter->flag & BOARD_IOMAP ) {
161
162 outb(adapter->mbox_dma & 0xFF,
163 adapter->host->io_port + MBOX_PORT0);
164
165 outb((adapter->mbox_dma >> 8) & 0xFF,
166 adapter->host->io_port + MBOX_PORT1);
167
168 outb((adapter->mbox_dma >> 16) & 0xFF,
169 adapter->host->io_port + MBOX_PORT2);
170
171 outb((adapter->mbox_dma >> 24) & 0xFF,
172 adapter->host->io_port + MBOX_PORT3);
173
174 outb(ENABLE_MBOX_BYTE,
175 adapter->host->io_port + ENABLE_MBOX_REGION);
176
177 irq_ack(adapter);
178
179 irq_enable(adapter);
180 }
181
182 return 0;
183}
184
185
186
187
188
189
190
191
192
193static int
194mega_query_adapter(adapter_t *adapter)
195{
196 dma_addr_t prod_info_dma_handle;
197 mega_inquiry3 *inquiry3;
198 u8 raw_mbox[sizeof(struct mbox_out)];
199 mbox_t *mbox;
200 int retval;
201
202
203
204 mbox = (mbox_t *)raw_mbox;
205
206 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
207 memset(&mbox->m_out, 0, sizeof(raw_mbox));
208
209
210
211
212
213
214 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
215
216 inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
217
218 raw_mbox[0] = FC_NEW_CONFIG;
219 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
220 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
221
222
223 if ((retval = issue_scb_block(adapter, raw_mbox))) {
224
225
226 mraid_ext_inquiry *ext_inq;
227 mraid_inquiry *inq;
228 dma_addr_t dma_handle;
229
230 ext_inq = pci_alloc_consistent(adapter->dev,
231 sizeof(mraid_ext_inquiry), &dma_handle);
232
233 if( ext_inq == NULL ) return -1;
234
235 inq = &ext_inq->raid_inq;
236
237 mbox->m_out.xferaddr = (u32)dma_handle;
238
239
240 mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
241
242 issue_scb_block(adapter, raw_mbox);
243
244
245
246
247
248 mega_8_to_40ld(inq, inquiry3,
249 (mega_product_info *)&adapter->product_info);
250
251 pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
252 ext_inq, dma_handle);
253
254 } else {
255 adapter->flag |= BOARD_40LD;
256
257
258
259
260
261 prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
262 &adapter->product_info,
263 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
264
265 mbox->m_out.xferaddr = prod_info_dma_handle;
266
267 raw_mbox[0] = FC_NEW_CONFIG;
268 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
269
270 if ((retval = issue_scb_block(adapter, raw_mbox)))
271 printk(KERN_WARNING
272 "megaraid: Product_info cmd failed with error: %d\n",
273 retval);
274
275 pci_unmap_single(adapter->dev, prod_info_dma_handle,
276 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
277 }
278
279
280
281
282
283 adapter->host->max_channel =
284 adapter->product_info.nchannels + NVIRT_CHAN -1;
285
286 adapter->host->max_id = 16;
287
288 adapter->host->max_lun = 7;
289
290 adapter->host->cmd_per_lun = max_cmd_per_lun;
291
292 adapter->numldrv = inquiry3->num_ldrv;
293
294 adapter->max_cmds = adapter->product_info.max_commands;
295
296 if(adapter->max_cmds > MAX_COMMANDS)
297 adapter->max_cmds = MAX_COMMANDS;
298
299 adapter->host->can_queue = adapter->max_cmds - 1;
300
301
302
303
304
305 mega_get_max_sgl(adapter);
306
307 adapter->host->sg_tablesize = adapter->sglen;
308
309
310
311
312
313 if (adapter->product_info.subsysvid == PCI_VENDOR_ID_HP) {
314 sprintf (adapter->fw_version, "%c%d%d.%d%d",
315 adapter->product_info.fw_version[2],
316 0,
317 adapter->product_info.fw_version[1] & 0x0f,
318 0,
319 adapter->product_info.fw_version[0] & 0x0f);
320 sprintf (adapter->bios_version, "%c%d%d.%d%d",
321 adapter->product_info.bios_version[2],
322 0,
323 adapter->product_info.bios_version[1] & 0x0f,
324 0,
325 adapter->product_info.bios_version[0] & 0x0f);
326 } else {
327 memcpy(adapter->fw_version,
328 (char *)adapter->product_info.fw_version, 4);
329 adapter->fw_version[4] = 0;
330
331 memcpy(adapter->bios_version,
332 (char *)adapter->product_info.bios_version, 4);
333
334 adapter->bios_version[4] = 0;
335 }
336
337 printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
338 adapter->fw_version, adapter->bios_version, adapter->numldrv);
339
340
341
342
343 adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
344 if (adapter->support_ext_cdb)
345 printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
346
347
348 return 0;
349}
350
351
352
353
354
355
356
357static inline void
358mega_runpendq(adapter_t *adapter)
359{
360 if(!list_empty(&adapter->pending_list))
361 __mega_runpendq(adapter);
362}
363
364
365
366
367
368
369
370
371static int
372megaraid_queue_lck(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
373{
374 adapter_t *adapter;
375 scb_t *scb;
376 int busy=0;
377 unsigned long flags;
378
379 adapter = (adapter_t *)scmd->device->host->hostdata;
380
381 scmd->scsi_done = done;
382
383
384
385
386
387
388
389
390
391
392
393 spin_lock_irqsave(&adapter->lock, flags);
394 scb = mega_build_cmd(adapter, scmd, &busy);
395 if (!scb)
396 goto out;
397
398 scb->state |= SCB_PENDQ;
399 list_add_tail(&scb->list, &adapter->pending_list);
400
401
402
403
404
405
406 if (atomic_read(&adapter->quiescent) == 0)
407 mega_runpendq(adapter);
408
409 busy = 0;
410 out:
411 spin_unlock_irqrestore(&adapter->lock, flags);
412 return busy;
413}
414
415static DEF_SCSI_QCMD(megaraid_queue)
416
417
418
419
420
421
422
423
424
425static inline scb_t *
426mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
427{
428 struct list_head *head = &adapter->free_list;
429 scb_t *scb;
430
431
432 if( !list_empty(head) ) {
433
434 scb = list_entry(head->next, scb_t, list);
435
436 list_del_init(head->next);
437
438 scb->state = SCB_ACTIVE;
439 scb->cmd = cmd;
440 scb->dma_type = MEGA_DMA_TYPE_NONE;
441
442 return scb;
443 }
444
445 return NULL;
446}
447
448
449
450
451
452
453
454
455
456
457static inline int
458mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
459{
460 int tgt;
461 int ldrv_num;
462
463 tgt = cmd->device->id;
464
465 if ( tgt > adapter->this_id )
466 tgt--;
467
468 ldrv_num = (channel * 15) + tgt;
469
470
471
472
473
474 if( adapter->boot_ldrv_enabled ) {
475 if( ldrv_num == 0 ) {
476 ldrv_num = adapter->boot_ldrv;
477 }
478 else {
479 if( ldrv_num <= adapter->boot_ldrv ) {
480 ldrv_num--;
481 }
482 }
483 }
484
485
486
487
488
489
490
491
492
493
494
495 if (adapter->support_random_del && adapter->read_ldidmap )
496 switch (cmd->cmnd[0]) {
497 case READ_6:
498 case WRITE_6:
499 case READ_10:
500 case WRITE_10:
501 ldrv_num += 0x80;
502 }
503
504 return ldrv_num;
505}
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520static scb_t *
521mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
522{
523 mega_ext_passthru *epthru;
524 mega_passthru *pthru;
525 scb_t *scb;
526 mbox_t *mbox;
527 u32 seg;
528 char islogical;
529 int max_ldrv_num;
530 int channel = 0;
531 int target = 0;
532 int ldrv_num = 0;
533
534
535
536
537
538 if((cmd->cmnd[0] == MEGA_INTERNAL_CMD))
539 return (scb_t *)cmd->host_scribble;
540
541
542
543
544 islogical = adapter->logdrv_chan[cmd->device->channel];
545
546
547
548
549
550
551
552
553 if( adapter->boot_pdrv_enabled ) {
554 if( islogical ) {
555
556 channel = cmd->device->channel -
557 adapter->product_info.nchannels;
558 }
559 else {
560
561 channel = cmd->device->channel;
562 target = cmd->device->id;
563
564
565
566
567
568
569 if( target == 0 ) {
570 target = adapter->boot_pdrv_tgt;
571 }
572 else if( target == adapter->boot_pdrv_tgt ) {
573 target = 0;
574 }
575 }
576 }
577 else {
578 if( islogical ) {
579
580 channel = cmd->device->channel;
581 }
582 else {
583
584 channel = cmd->device->channel - NVIRT_CHAN;
585 target = cmd->device->id;
586 }
587 }
588
589
590 if(islogical) {
591
592
593 if (cmd->device->lun) {
594 cmd->result = (DID_BAD_TARGET << 16);
595 cmd->scsi_done(cmd);
596 return NULL;
597 }
598
599 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
600
601
602 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
603 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
604
605
606
607
608
609 if(adapter->read_ldidmap)
610 max_ldrv_num += 0x80;
611
612 if(ldrv_num > max_ldrv_num ) {
613 cmd->result = (DID_BAD_TARGET << 16);
614 cmd->scsi_done(cmd);
615 return NULL;
616 }
617
618 }
619 else {
620 if( cmd->device->lun > 7) {
621
622
623
624
625 cmd->result = (DID_BAD_TARGET << 16);
626 cmd->scsi_done(cmd);
627 return NULL;
628 }
629 }
630
631
632
633
634
635
636 if(islogical) {
637 switch (cmd->cmnd[0]) {
638 case TEST_UNIT_READY:
639#if MEGA_HAVE_CLUSTERING
640
641
642
643
644 if( !adapter->has_cluster ) {
645 cmd->result = (DID_OK << 16);
646 cmd->scsi_done(cmd);
647 return NULL;
648 }
649
650 if(!(scb = mega_allocate_scb(adapter, cmd))) {
651 *busy = 1;
652 return NULL;
653 }
654
655 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
656 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
657 scb->raw_mbox[3] = ldrv_num;
658
659 scb->dma_direction = PCI_DMA_NONE;
660
661 return scb;
662#else
663 cmd->result = (DID_OK << 16);
664 cmd->scsi_done(cmd);
665 return NULL;
666#endif
667
668 case MODE_SENSE: {
669 char *buf;
670 struct scatterlist *sg;
671
672 sg = scsi_sglist(cmd);
673 buf = kmap_atomic(sg_page(sg)) + sg->offset;
674
675 memset(buf, 0, cmd->cmnd[4]);
676 kunmap_atomic(buf - sg->offset);
677
678 cmd->result = (DID_OK << 16);
679 cmd->scsi_done(cmd);
680 return NULL;
681 }
682
683 case READ_CAPACITY:
684 case INQUIRY:
685
686 if(!(adapter->flag & (1L << cmd->device->channel))) {
687
688 printk(KERN_NOTICE
689 "scsi%d: scanning scsi channel %d ",
690 adapter->host->host_no,
691 cmd->device->channel);
692 printk("for logical drives.\n");
693
694 adapter->flag |= (1L << cmd->device->channel);
695 }
696
697
698 if(!(scb = mega_allocate_scb(adapter, cmd))) {
699 *busy = 1;
700 return NULL;
701 }
702 pthru = scb->pthru;
703
704 mbox = (mbox_t *)scb->raw_mbox;
705 memset(mbox, 0, sizeof(scb->raw_mbox));
706 memset(pthru, 0, sizeof(mega_passthru));
707
708 pthru->timeout = 0;
709 pthru->ars = 1;
710 pthru->reqsenselen = 14;
711 pthru->islogical = 1;
712 pthru->logdrv = ldrv_num;
713 pthru->cdblen = cmd->cmd_len;
714 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
715
716 if( adapter->has_64bit_addr ) {
717 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
718 }
719 else {
720 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
721 }
722
723 scb->dma_direction = PCI_DMA_FROMDEVICE;
724
725 pthru->numsgelements = mega_build_sglist(adapter, scb,
726 &pthru->dataxferaddr, &pthru->dataxferlen);
727
728 mbox->m_out.xferaddr = scb->pthru_dma_addr;
729
730 return scb;
731
732 case READ_6:
733 case WRITE_6:
734 case READ_10:
735 case WRITE_10:
736 case READ_12:
737 case WRITE_12:
738
739
740 if(!(scb = mega_allocate_scb(adapter, cmd))) {
741 *busy = 1;
742 return NULL;
743 }
744 mbox = (mbox_t *)scb->raw_mbox;
745
746 memset(mbox, 0, sizeof(scb->raw_mbox));
747 mbox->m_out.logdrv = ldrv_num;
748
749
750
751
752
753 if( adapter->has_64bit_addr ) {
754 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
755 MEGA_MBOXCMD_LWRITE64:
756 MEGA_MBOXCMD_LREAD64 ;
757 }
758 else {
759 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
760 MEGA_MBOXCMD_LWRITE:
761 MEGA_MBOXCMD_LREAD ;
762 }
763
764
765
766
767 if( cmd->cmd_len == 6 ) {
768 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
769 mbox->m_out.lba =
770 ((u32)cmd->cmnd[1] << 16) |
771 ((u32)cmd->cmnd[2] << 8) |
772 (u32)cmd->cmnd[3];
773
774 mbox->m_out.lba &= 0x1FFFFF;
775
776#if MEGA_HAVE_STATS
777
778
779
780
781
782 if (*cmd->cmnd == READ_6) {
783 adapter->nreads[ldrv_num%0x80]++;
784 adapter->nreadblocks[ldrv_num%0x80] +=
785 mbox->m_out.numsectors;
786 } else {
787 adapter->nwrites[ldrv_num%0x80]++;
788 adapter->nwriteblocks[ldrv_num%0x80] +=
789 mbox->m_out.numsectors;
790 }
791#endif
792 }
793
794
795
796
797 if( cmd->cmd_len == 10 ) {
798 mbox->m_out.numsectors =
799 (u32)cmd->cmnd[8] |
800 ((u32)cmd->cmnd[7] << 8);
801 mbox->m_out.lba =
802 ((u32)cmd->cmnd[2] << 24) |
803 ((u32)cmd->cmnd[3] << 16) |
804 ((u32)cmd->cmnd[4] << 8) |
805 (u32)cmd->cmnd[5];
806
807#if MEGA_HAVE_STATS
808 if (*cmd->cmnd == READ_10) {
809 adapter->nreads[ldrv_num%0x80]++;
810 adapter->nreadblocks[ldrv_num%0x80] +=
811 mbox->m_out.numsectors;
812 } else {
813 adapter->nwrites[ldrv_num%0x80]++;
814 adapter->nwriteblocks[ldrv_num%0x80] +=
815 mbox->m_out.numsectors;
816 }
817#endif
818 }
819
820
821
822
823 if( cmd->cmd_len == 12 ) {
824 mbox->m_out.lba =
825 ((u32)cmd->cmnd[2] << 24) |
826 ((u32)cmd->cmnd[3] << 16) |
827 ((u32)cmd->cmnd[4] << 8) |
828 (u32)cmd->cmnd[5];
829
830 mbox->m_out.numsectors =
831 ((u32)cmd->cmnd[6] << 24) |
832 ((u32)cmd->cmnd[7] << 16) |
833 ((u32)cmd->cmnd[8] << 8) |
834 (u32)cmd->cmnd[9];
835
836#if MEGA_HAVE_STATS
837 if (*cmd->cmnd == READ_12) {
838 adapter->nreads[ldrv_num%0x80]++;
839 adapter->nreadblocks[ldrv_num%0x80] +=
840 mbox->m_out.numsectors;
841 } else {
842 adapter->nwrites[ldrv_num%0x80]++;
843 adapter->nwriteblocks[ldrv_num%0x80] +=
844 mbox->m_out.numsectors;
845 }
846#endif
847 }
848
849
850
851
852 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
853 scb->dma_direction = PCI_DMA_FROMDEVICE;
854 }
855 else {
856 scb->dma_direction = PCI_DMA_TODEVICE;
857 }
858
859
860 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
861 (u32 *)&mbox->m_out.xferaddr, &seg);
862
863 return scb;
864
865#if MEGA_HAVE_CLUSTERING
866 case RESERVE:
867 case RELEASE:
868
869
870
871
872 if( ! adapter->has_cluster ) {
873
874 cmd->result = (DID_BAD_TARGET << 16);
875 cmd->scsi_done(cmd);
876 return NULL;
877 }
878
879
880 if(!(scb = mega_allocate_scb(adapter, cmd))) {
881 *busy = 1;
882 return NULL;
883 }
884
885 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
886 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
887 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
888
889 scb->raw_mbox[3] = ldrv_num;
890
891 scb->dma_direction = PCI_DMA_NONE;
892
893 return scb;
894#endif
895
896 default:
897 cmd->result = (DID_BAD_TARGET << 16);
898 cmd->scsi_done(cmd);
899 return NULL;
900 }
901 }
902
903
904
905
906 else {
907
908 if(!(scb = mega_allocate_scb(adapter, cmd))) {
909 *busy = 1;
910 return NULL;
911 }
912
913 mbox = (mbox_t *)scb->raw_mbox;
914 memset(mbox, 0, sizeof(scb->raw_mbox));
915
916 if( adapter->support_ext_cdb ) {
917
918 epthru = mega_prepare_extpassthru(adapter, scb, cmd,
919 channel, target);
920
921 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
922
923 mbox->m_out.xferaddr = scb->epthru_dma_addr;
924
925 }
926 else {
927
928 pthru = mega_prepare_passthru(adapter, scb, cmd,
929 channel, target);
930
931
932 if( adapter->has_64bit_addr ) {
933 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
934 }
935 else {
936 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
937 }
938
939 mbox->m_out.xferaddr = scb->pthru_dma_addr;
940
941 }
942 return scb;
943 }
944 return NULL;
945}
946
947
948
949
950
951
952
953
954
955
956
957
958static mega_passthru *
959mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
960 int channel, int target)
961{
962 mega_passthru *pthru;
963
964 pthru = scb->pthru;
965 memset(pthru, 0, sizeof (mega_passthru));
966
967
968 pthru->timeout = 2;
969
970 pthru->ars = 1;
971 pthru->reqsenselen = 14;
972 pthru->islogical = 0;
973
974 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
975
976 pthru->target = (adapter->flag & BOARD_40LD) ?
977 (channel << 4) | target : target;
978
979 pthru->cdblen = cmd->cmd_len;
980 pthru->logdrv = cmd->device->lun;
981
982 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
983
984
985 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
986
987
988 switch (cmd->cmnd[0]) {
989 case INQUIRY:
990 case READ_CAPACITY:
991 if(!(adapter->flag & (1L << cmd->device->channel))) {
992
993 printk(KERN_NOTICE
994 "scsi%d: scanning scsi channel %d [P%d] ",
995 adapter->host->host_no,
996 cmd->device->channel, channel);
997 printk("for physical devices.\n");
998
999 adapter->flag |= (1L << cmd->device->channel);
1000 }
1001
1002 default:
1003 pthru->numsgelements = mega_build_sglist(adapter, scb,
1004 &pthru->dataxferaddr, &pthru->dataxferlen);
1005 break;
1006 }
1007 return pthru;
1008}
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022static mega_ext_passthru *
1023mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1024 int channel, int target)
1025{
1026 mega_ext_passthru *epthru;
1027
1028 epthru = scb->epthru;
1029 memset(epthru, 0, sizeof(mega_ext_passthru));
1030
1031
1032 epthru->timeout = 2;
1033
1034 epthru->ars = 1;
1035 epthru->reqsenselen = 14;
1036 epthru->islogical = 0;
1037
1038 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1039 epthru->target = (adapter->flag & BOARD_40LD) ?
1040 (channel << 4) | target : target;
1041
1042 epthru->cdblen = cmd->cmd_len;
1043 epthru->logdrv = cmd->device->lun;
1044
1045 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1046
1047
1048 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1049
1050 switch(cmd->cmnd[0]) {
1051 case INQUIRY:
1052 case READ_CAPACITY:
1053 if(!(adapter->flag & (1L << cmd->device->channel))) {
1054
1055 printk(KERN_NOTICE
1056 "scsi%d: scanning scsi channel %d [P%d] ",
1057 adapter->host->host_no,
1058 cmd->device->channel, channel);
1059 printk("for physical devices.\n");
1060
1061 adapter->flag |= (1L << cmd->device->channel);
1062 }
1063
1064 default:
1065 epthru->numsgelements = mega_build_sglist(adapter, scb,
1066 &epthru->dataxferaddr, &epthru->dataxferlen);
1067 break;
1068 }
1069
1070 return epthru;
1071}
1072
1073static void
1074__mega_runpendq(adapter_t *adapter)
1075{
1076 scb_t *scb;
1077 struct list_head *pos, *next;
1078
1079
1080 list_for_each_safe(pos, next, &adapter->pending_list) {
1081
1082 scb = list_entry(pos, scb_t, list);
1083
1084 if( !(scb->state & SCB_ISSUED) ) {
1085
1086 if( issue_scb(adapter, scb) != 0 )
1087 return;
1088 }
1089 }
1090
1091 return;
1092}
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104static int
1105issue_scb(adapter_t *adapter, scb_t *scb)
1106{
1107 volatile mbox64_t *mbox64 = adapter->mbox64;
1108 volatile mbox_t *mbox = adapter->mbox;
1109 unsigned int i = 0;
1110
1111 if(unlikely(mbox->m_in.busy)) {
1112 do {
1113 udelay(1);
1114 i++;
1115 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1116
1117 if(mbox->m_in.busy) return -1;
1118 }
1119
1120
1121 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1122 sizeof(struct mbox_out));
1123
1124 mbox->m_out.cmdid = scb->idx;
1125 mbox->m_in.busy = 1;
1126
1127
1128
1129
1130
1131 atomic_inc(&adapter->pend_cmds);
1132
1133 switch (mbox->m_out.cmd) {
1134 case MEGA_MBOXCMD_LREAD64:
1135 case MEGA_MBOXCMD_LWRITE64:
1136 case MEGA_MBOXCMD_PASSTHRU64:
1137 case MEGA_MBOXCMD_EXTPTHRU:
1138 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1139 mbox64->xfer_segment_hi = 0;
1140 mbox->m_out.xferaddr = 0xFFFFFFFF;
1141 break;
1142 default:
1143 mbox64->xfer_segment_lo = 0;
1144 mbox64->xfer_segment_hi = 0;
1145 }
1146
1147
1148
1149
1150 scb->state |= SCB_ISSUED;
1151
1152 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1153 mbox->m_in.poll = 0;
1154 mbox->m_in.ack = 0;
1155 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1156 }
1157 else {
1158 irq_enable(adapter);
1159 issue_command(adapter);
1160 }
1161
1162 return 0;
1163}
1164
1165
1166
1167
1168static inline int
1169mega_busywait_mbox (adapter_t *adapter)
1170{
1171 if (adapter->mbox->m_in.busy)
1172 return __mega_busywait_mbox(adapter);
1173 return 0;
1174}
1175
1176
1177
1178
1179
1180
1181
1182
1183static int
1184issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1185{
1186 volatile mbox64_t *mbox64 = adapter->mbox64;
1187 volatile mbox_t *mbox = adapter->mbox;
1188 u8 byte;
1189
1190
1191 if(mega_busywait_mbox (adapter))
1192 goto bug_blocked_mailbox;
1193
1194
1195 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1196 mbox->m_out.cmdid = 0xFE;
1197 mbox->m_in.busy = 1;
1198
1199 switch (raw_mbox[0]) {
1200 case MEGA_MBOXCMD_LREAD64:
1201 case MEGA_MBOXCMD_LWRITE64:
1202 case MEGA_MBOXCMD_PASSTHRU64:
1203 case MEGA_MBOXCMD_EXTPTHRU:
1204 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1205 mbox64->xfer_segment_hi = 0;
1206 mbox->m_out.xferaddr = 0xFFFFFFFF;
1207 break;
1208 default:
1209 mbox64->xfer_segment_lo = 0;
1210 mbox64->xfer_segment_hi = 0;
1211 }
1212
1213 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1214 mbox->m_in.poll = 0;
1215 mbox->m_in.ack = 0;
1216 mbox->m_in.numstatus = 0xFF;
1217 mbox->m_in.status = 0xFF;
1218 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1219
1220 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1221 cpu_relax();
1222
1223 mbox->m_in.numstatus = 0xFF;
1224
1225 while( (volatile u8)mbox->m_in.poll != 0x77 )
1226 cpu_relax();
1227
1228 mbox->m_in.poll = 0;
1229 mbox->m_in.ack = 0x77;
1230
1231 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1232
1233 while(RDINDOOR(adapter) & 0x2)
1234 cpu_relax();
1235 }
1236 else {
1237 irq_disable(adapter);
1238 issue_command(adapter);
1239
1240 while (!((byte = irq_state(adapter)) & INTR_VALID))
1241 cpu_relax();
1242
1243 set_irq_state(adapter, byte);
1244 irq_enable(adapter);
1245 irq_ack(adapter);
1246 }
1247
1248 return mbox->m_in.status;
1249
1250bug_blocked_mailbox:
1251 printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1252 udelay (1000);
1253 return -1;
1254}
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266static irqreturn_t
1267megaraid_isr_iomapped(int irq, void *devp)
1268{
1269 adapter_t *adapter = devp;
1270 unsigned long flags;
1271 u8 status;
1272 u8 nstatus;
1273 u8 completed[MAX_FIRMWARE_STATUS];
1274 u8 byte;
1275 int handled = 0;
1276
1277
1278
1279
1280
1281 spin_lock_irqsave(&adapter->lock, flags);
1282
1283 do {
1284
1285 byte = irq_state(adapter);
1286 if( (byte & VALID_INTR_BYTE) == 0 ) {
1287
1288
1289
1290 goto out_unlock;
1291 }
1292 set_irq_state(adapter, byte);
1293
1294 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1295 == 0xFF)
1296 cpu_relax();
1297 adapter->mbox->m_in.numstatus = 0xFF;
1298
1299 status = adapter->mbox->m_in.status;
1300
1301
1302
1303
1304 atomic_sub(nstatus, &adapter->pend_cmds);
1305
1306 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1307 nstatus);
1308
1309
1310 irq_ack(adapter);
1311
1312 mega_cmd_done(adapter, completed, nstatus, status);
1313
1314 mega_rundoneq(adapter);
1315
1316 handled = 1;
1317
1318
1319 if(atomic_read(&adapter->quiescent) == 0) {
1320 mega_runpendq(adapter);
1321 }
1322
1323 } while(1);
1324
1325 out_unlock:
1326
1327 spin_unlock_irqrestore(&adapter->lock, flags);
1328
1329 return IRQ_RETVAL(handled);
1330}
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342static irqreturn_t
1343megaraid_isr_memmapped(int irq, void *devp)
1344{
1345 adapter_t *adapter = devp;
1346 unsigned long flags;
1347 u8 status;
1348 u32 dword = 0;
1349 u8 nstatus;
1350 u8 completed[MAX_FIRMWARE_STATUS];
1351 int handled = 0;
1352
1353
1354
1355
1356
1357 spin_lock_irqsave(&adapter->lock, flags);
1358
1359 do {
1360
1361 dword = RDOUTDOOR(adapter);
1362 if(dword != 0x10001234) {
1363
1364
1365
1366 goto out_unlock;
1367 }
1368 WROUTDOOR(adapter, 0x10001234);
1369
1370 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1371 == 0xFF) {
1372 cpu_relax();
1373 }
1374 adapter->mbox->m_in.numstatus = 0xFF;
1375
1376 status = adapter->mbox->m_in.status;
1377
1378
1379
1380
1381 atomic_sub(nstatus, &adapter->pend_cmds);
1382
1383 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1384 nstatus);
1385
1386
1387 WRINDOOR(adapter, 0x2);
1388
1389 handled = 1;
1390
1391 while( RDINDOOR(adapter) & 0x02 )
1392 cpu_relax();
1393
1394 mega_cmd_done(adapter, completed, nstatus, status);
1395
1396 mega_rundoneq(adapter);
1397
1398
1399 if(atomic_read(&adapter->quiescent) == 0) {
1400 mega_runpendq(adapter);
1401 }
1402
1403 } while(1);
1404
1405 out_unlock:
1406
1407 spin_unlock_irqrestore(&adapter->lock, flags);
1408
1409 return IRQ_RETVAL(handled);
1410}
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420static void
1421mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1422{
1423 mega_ext_passthru *epthru = NULL;
1424 struct scatterlist *sgl;
1425 Scsi_Cmnd *cmd = NULL;
1426 mega_passthru *pthru = NULL;
1427 mbox_t *mbox = NULL;
1428 u8 c;
1429 scb_t *scb;
1430 int islogical;
1431 int cmdid;
1432 int i;
1433
1434
1435
1436
1437
1438 for( i = 0; i < nstatus; i++ ) {
1439
1440 cmdid = completed[i];
1441
1442 if( cmdid == CMDID_INT_CMDS ) {
1443 scb = &adapter->int_scb;
1444 cmd = scb->cmd;
1445 mbox = (mbox_t *)scb->raw_mbox;
1446
1447
1448
1449
1450
1451 pthru = scb->pthru;
1452
1453 }
1454 else {
1455 scb = &adapter->scb_list[cmdid];
1456
1457
1458
1459
1460 if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1461 printk(KERN_CRIT
1462 "megaraid: invalid command ");
1463 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1464 cmdid, scb->state, scb->cmd);
1465
1466 continue;
1467 }
1468
1469
1470
1471
1472 if( scb->state & SCB_ABORT ) {
1473
1474 printk(KERN_WARNING
1475 "megaraid: aborted cmd [%x] complete.\n",
1476 scb->idx);
1477
1478 scb->cmd->result = (DID_ABORT << 16);
1479
1480 list_add_tail(SCSI_LIST(scb->cmd),
1481 &adapter->completed_list);
1482
1483 mega_free_scb(adapter, scb);
1484
1485 continue;
1486 }
1487
1488
1489
1490
1491 if( scb->state & SCB_RESET ) {
1492
1493 printk(KERN_WARNING
1494 "megaraid: reset cmd [%x] complete.\n",
1495 scb->idx);
1496
1497 scb->cmd->result = (DID_RESET << 16);
1498
1499 list_add_tail(SCSI_LIST(scb->cmd),
1500 &adapter->completed_list);
1501
1502 mega_free_scb (adapter, scb);
1503
1504 continue;
1505 }
1506
1507 cmd = scb->cmd;
1508 pthru = scb->pthru;
1509 epthru = scb->epthru;
1510 mbox = (mbox_t *)scb->raw_mbox;
1511
1512#if MEGA_HAVE_STATS
1513 {
1514
1515 int logdrv = mbox->m_out.logdrv;
1516
1517 islogical = adapter->logdrv_chan[cmd->channel];
1518
1519
1520
1521
1522
1523 if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1524 cmd->cmnd[0] == READ_10 ||
1525 cmd->cmnd[0] == READ_12)) {
1526
1527
1528
1529
1530 adapter->rd_errors[logdrv%0x80]++;
1531 }
1532
1533 if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1534 cmd->cmnd[0] == WRITE_10 ||
1535 cmd->cmnd[0] == WRITE_12)) {
1536
1537
1538
1539
1540 adapter->wr_errors[logdrv%0x80]++;
1541 }
1542
1543 }
1544#endif
1545 }
1546
1547
1548
1549
1550
1551
1552
1553 islogical = adapter->logdrv_chan[cmd->device->channel];
1554 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1555
1556 sgl = scsi_sglist(cmd);
1557 if( sg_page(sgl) ) {
1558 c = *(unsigned char *) sg_virt(&sgl[0]);
1559 } else {
1560 printk(KERN_WARNING
1561 "megaraid: invalid sg.\n");
1562 c = 0;
1563 }
1564
1565 if(IS_RAID_CH(adapter, cmd->device->channel) &&
1566 ((c & 0x1F ) == TYPE_DISK)) {
1567 status = 0xF0;
1568 }
1569 }
1570
1571
1572 cmd->result = 0;
1573
1574
1575 switch (status) {
1576 case 0x00:
1577 cmd->result |= (DID_OK << 16);
1578 break;
1579
1580 case 0x02:
1581
1582
1583
1584 if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1585 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1586
1587 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1588 14);
1589
1590 cmd->result = (DRIVER_SENSE << 24) |
1591 (DID_OK << 16) |
1592 (CHECK_CONDITION << 1);
1593 }
1594 else {
1595 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1596
1597 memcpy(cmd->sense_buffer,
1598 epthru->reqsensearea, 14);
1599
1600 cmd->result = (DRIVER_SENSE << 24) |
1601 (DID_OK << 16) |
1602 (CHECK_CONDITION << 1);
1603 } else {
1604 cmd->sense_buffer[0] = 0x70;
1605 cmd->sense_buffer[2] = ABORTED_COMMAND;
1606 cmd->result |= (CHECK_CONDITION << 1);
1607 }
1608 }
1609 break;
1610
1611 case 0x08:
1612
1613 cmd->result |= (DID_BUS_BUSY << 16) | status;
1614 break;
1615
1616 default:
1617#if MEGA_HAVE_CLUSTERING
1618
1619
1620
1621
1622 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1623 cmd->result |= (DID_ERROR << 16) |
1624 (RESERVATION_CONFLICT << 1);
1625 }
1626 else
1627
1628
1629
1630
1631 if( status == 1 &&
1632 (cmd->cmnd[0] == RESERVE ||
1633 cmd->cmnd[0] == RELEASE) ) {
1634
1635 cmd->result |= (DID_ERROR << 16) |
1636 (RESERVATION_CONFLICT << 1);
1637 }
1638 else
1639#endif
1640 cmd->result |= (DID_BAD_TARGET << 16)|status;
1641 }
1642
1643
1644
1645
1646
1647
1648
1649
1650 if( cmdid == CMDID_INT_CMDS ) {
1651 cmd->result = status;
1652
1653
1654
1655
1656 list_del_init(&scb->list);
1657 scb->state = SCB_FREE;
1658 }
1659 else {
1660 mega_free_scb(adapter, scb);
1661 }
1662
1663
1664 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1665 }
1666}
1667
1668
1669
1670
1671
1672
1673
1674static void
1675mega_rundoneq (adapter_t *adapter)
1676{
1677 Scsi_Cmnd *cmd;
1678 struct list_head *pos;
1679
1680 list_for_each(pos, &adapter->completed_list) {
1681
1682 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1683
1684 cmd = list_entry(spos, Scsi_Cmnd, SCp);
1685 cmd->scsi_done(cmd);
1686 }
1687
1688 INIT_LIST_HEAD(&adapter->completed_list);
1689}
1690
1691
1692
1693
1694
1695
1696static void
1697mega_free_scb(adapter_t *adapter, scb_t *scb)
1698{
1699 switch( scb->dma_type ) {
1700
1701 case MEGA_DMA_TYPE_NONE:
1702 break;
1703
1704 case MEGA_SGLIST:
1705 scsi_dma_unmap(scb->cmd);
1706 break;
1707 default:
1708 break;
1709 }
1710
1711
1712
1713
1714 list_del_init(&scb->list);
1715
1716
1717 scb->state = SCB_FREE;
1718 scb->cmd = NULL;
1719
1720 list_add(&scb->list, &adapter->free_list);
1721}
1722
1723
1724static int
1725__mega_busywait_mbox (adapter_t *adapter)
1726{
1727 volatile mbox_t *mbox = adapter->mbox;
1728 long counter;
1729
1730 for (counter = 0; counter < 10000; counter++) {
1731 if (!mbox->m_in.busy)
1732 return 0;
1733 udelay(100);
1734 cond_resched();
1735 }
1736 return -1;
1737}
1738
1739
1740
1741
1742
1743static int
1744mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1745{
1746 struct scatterlist *sg;
1747 Scsi_Cmnd *cmd;
1748 int sgcnt;
1749 int idx;
1750
1751 cmd = scb->cmd;
1752
1753
1754
1755
1756
1757
1758 sgcnt = scsi_dma_map(cmd);
1759
1760 scb->dma_type = MEGA_SGLIST;
1761
1762 BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1763
1764 *len = 0;
1765
1766 if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1767 sg = scsi_sglist(cmd);
1768 scb->dma_h_bulkdata = sg_dma_address(sg);
1769 *buf = (u32)scb->dma_h_bulkdata;
1770 *len = sg_dma_len(sg);
1771 return 0;
1772 }
1773
1774 scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1775 if (adapter->has_64bit_addr) {
1776 scb->sgl64[idx].address = sg_dma_address(sg);
1777 *len += scb->sgl64[idx].length = sg_dma_len(sg);
1778 } else {
1779 scb->sgl[idx].address = sg_dma_address(sg);
1780 *len += scb->sgl[idx].length = sg_dma_len(sg);
1781 }
1782 }
1783
1784
1785 *buf = scb->sgl_dma_addr;
1786
1787
1788 return sgcnt;
1789}
1790
1791
1792
1793
1794
1795
1796
1797
1798static void
1799mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1800 mega_product_info *product_info)
1801{
1802 int i;
1803
1804 product_info->max_commands = inquiry->adapter_info.max_commands;
1805 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1806 product_info->nchannels = inquiry->adapter_info.nchannels;
1807
1808 for (i = 0; i < 4; i++) {
1809 product_info->fw_version[i] =
1810 inquiry->adapter_info.fw_version[i];
1811
1812 product_info->bios_version[i] =
1813 inquiry->adapter_info.bios_version[i];
1814 }
1815 enquiry3->cache_flush_interval =
1816 inquiry->adapter_info.cache_flush_interval;
1817
1818 product_info->dram_size = inquiry->adapter_info.dram_size;
1819
1820 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1821
1822 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1823 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1824 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1825 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1826 }
1827
1828 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1829 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1830}
1831
1832static inline void
1833mega_free_sgl(adapter_t *adapter)
1834{
1835 scb_t *scb;
1836 int i;
1837
1838 for(i = 0; i < adapter->max_cmds; i++) {
1839
1840 scb = &adapter->scb_list[i];
1841
1842 if( scb->sgl64 ) {
1843 pci_free_consistent(adapter->dev,
1844 sizeof(mega_sgl64) * adapter->sglen,
1845 scb->sgl64,
1846 scb->sgl_dma_addr);
1847
1848 scb->sgl64 = NULL;
1849 }
1850
1851 if( scb->pthru ) {
1852 pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1853 scb->pthru, scb->pthru_dma_addr);
1854
1855 scb->pthru = NULL;
1856 }
1857
1858 if( scb->epthru ) {
1859 pci_free_consistent(adapter->dev,
1860 sizeof(mega_ext_passthru),
1861 scb->epthru, scb->epthru_dma_addr);
1862
1863 scb->epthru = NULL;
1864 }
1865
1866 }
1867}
1868
1869
1870
1871
1872
1873const char *
1874megaraid_info(struct Scsi_Host *host)
1875{
1876 static char buffer[512];
1877 adapter_t *adapter;
1878
1879 adapter = (adapter_t *)host->hostdata;
1880
1881 sprintf (buffer,
1882 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1883 adapter->fw_version, adapter->product_info.max_commands,
1884 adapter->host->max_id, adapter->host->max_channel,
1885 adapter->host->max_lun);
1886 return buffer;
1887}
1888
1889
1890
1891
1892
1893static int
1894megaraid_abort(Scsi_Cmnd *cmd)
1895{
1896 adapter_t *adapter;
1897 int rval;
1898
1899 adapter = (adapter_t *)cmd->device->host->hostdata;
1900
1901 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1902
1903
1904
1905
1906
1907 mega_rundoneq(adapter);
1908
1909 return rval;
1910}
1911
1912
1913static int
1914megaraid_reset(struct scsi_cmnd *cmd)
1915{
1916 adapter_t *adapter;
1917 megacmd_t mc;
1918 int rval;
1919
1920 adapter = (adapter_t *)cmd->device->host->hostdata;
1921
1922#if MEGA_HAVE_CLUSTERING
1923 mc.cmd = MEGA_CLUSTER_CMD;
1924 mc.opcode = MEGA_RESET_RESERVATIONS;
1925
1926 if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1927 printk(KERN_WARNING
1928 "megaraid: reservation reset failed.\n");
1929 }
1930 else {
1931 printk(KERN_INFO "megaraid: reservation reset.\n");
1932 }
1933#endif
1934
1935 spin_lock_irq(&adapter->lock);
1936
1937 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1938
1939
1940
1941
1942
1943 mega_rundoneq(adapter);
1944 spin_unlock_irq(&adapter->lock);
1945
1946 return rval;
1947}
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958static int
1959megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
1960{
1961 struct list_head *pos, *next;
1962 scb_t *scb;
1963
1964 printk(KERN_WARNING "megaraid: %s cmd=%x <c=%d t=%d l=%d>\n",
1965 (aor == SCB_ABORT)? "ABORTING":"RESET",
1966 cmd->cmnd[0], cmd->device->channel,
1967 cmd->device->id, cmd->device->lun);
1968
1969 if(list_empty(&adapter->pending_list))
1970 return FALSE;
1971
1972 list_for_each_safe(pos, next, &adapter->pending_list) {
1973
1974 scb = list_entry(pos, scb_t, list);
1975
1976 if (scb->cmd == cmd) {
1977
1978 scb->state |= aor;
1979
1980
1981
1982
1983
1984
1985
1986 if( scb->state & SCB_ISSUED ) {
1987
1988 printk(KERN_WARNING
1989 "megaraid: %s[%x], fw owner.\n",
1990 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1991 scb->idx);
1992
1993 return FALSE;
1994 }
1995 else {
1996
1997
1998
1999
2000
2001 printk(KERN_WARNING
2002 "megaraid: %s-[%x], driver owner.\n",
2003 (aor==SCB_ABORT) ? "ABORTING":"RESET",
2004 scb->idx);
2005
2006 mega_free_scb(adapter, scb);
2007
2008 if( aor == SCB_ABORT ) {
2009 cmd->result = (DID_ABORT << 16);
2010 }
2011 else {
2012 cmd->result = (DID_RESET << 16);
2013 }
2014
2015 list_add_tail(SCSI_LIST(cmd),
2016 &adapter->completed_list);
2017
2018 return TRUE;
2019 }
2020 }
2021 }
2022
2023 return FALSE;
2024}
2025
2026static inline int
2027make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2028{
2029 *pdev = pci_alloc_dev(NULL);
2030
2031 if( *pdev == NULL ) return -1;
2032
2033 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2034
2035 if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
2036 kfree(*pdev);
2037 return -1;
2038 }
2039
2040 return 0;
2041}
2042
2043static inline void
2044free_local_pdev(struct pci_dev *pdev)
2045{
2046 kfree(pdev);
2047}
2048
2049
2050
2051
2052
2053
2054
2055
2056static inline void *
2057mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2058{
2059 return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2060}
2061
2062
2063static inline void
2064mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2065{
2066 pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2067}
2068
2069
2070#ifdef CONFIG_PROC_FS
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080static int
2081proc_show_config(struct seq_file *m, void *v)
2082{
2083
2084 adapter_t *adapter = m->private;
2085
2086 seq_puts(m, MEGARAID_VERSION);
2087 if(adapter->product_info.product_name[0])
2088 seq_printf(m, "%s\n", adapter->product_info.product_name);
2089
2090 seq_puts(m, "Controller Type: ");
2091
2092 if( adapter->flag & BOARD_MEMMAP )
2093 seq_puts(m, "438/466/467/471/493/518/520/531/532\n");
2094 else
2095 seq_puts(m, "418/428/434\n");
2096
2097 if(adapter->flag & BOARD_40LD)
2098 seq_puts(m, "Controller Supports 40 Logical Drives\n");
2099
2100 if(adapter->flag & BOARD_64BIT)
2101 seq_puts(m, "Controller capable of 64-bit memory addressing\n");
2102 if( adapter->has_64bit_addr )
2103 seq_puts(m, "Controller using 64-bit memory addressing\n");
2104 else
2105 seq_puts(m, "Controller is not using 64-bit memory addressing\n");
2106
2107 seq_printf(m, "Base = %08lx, Irq = %d, ",
2108 adapter->base, adapter->host->irq);
2109
2110 seq_printf(m, "Logical Drives = %d, Channels = %d\n",
2111 adapter->numldrv, adapter->product_info.nchannels);
2112
2113 seq_printf(m, "Version =%s:%s, DRAM = %dMb\n",
2114 adapter->fw_version, adapter->bios_version,
2115 adapter->product_info.dram_size);
2116
2117 seq_printf(m, "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2118 adapter->product_info.max_commands, adapter->max_cmds);
2119
2120 seq_printf(m, "support_ext_cdb = %d\n", adapter->support_ext_cdb);
2121 seq_printf(m, "support_random_del = %d\n", adapter->support_random_del);
2122 seq_printf(m, "boot_ldrv_enabled = %d\n", adapter->boot_ldrv_enabled);
2123 seq_printf(m, "boot_ldrv = %d\n", adapter->boot_ldrv);
2124 seq_printf(m, "boot_pdrv_enabled = %d\n", adapter->boot_pdrv_enabled);
2125 seq_printf(m, "boot_pdrv_ch = %d\n", adapter->boot_pdrv_ch);
2126 seq_printf(m, "boot_pdrv_tgt = %d\n", adapter->boot_pdrv_tgt);
2127 seq_printf(m, "quiescent = %d\n",
2128 atomic_read(&adapter->quiescent));
2129 seq_printf(m, "has_cluster = %d\n", adapter->has_cluster);
2130
2131 seq_puts(m, "\nModule Parameters:\n");
2132 seq_printf(m, "max_cmd_per_lun = %d\n", max_cmd_per_lun);
2133 seq_printf(m, "max_sectors_per_io = %d\n", max_sectors_per_io);
2134 return 0;
2135}
2136
2137
2138
2139
2140
2141
2142
2143
2144static int
2145proc_show_stat(struct seq_file *m, void *v)
2146{
2147 adapter_t *adapter = m->private;
2148#if MEGA_HAVE_STATS
2149 int i;
2150#endif
2151
2152 seq_puts(m, "Statistical Information for this controller\n");
2153 seq_printf(m, "pend_cmds = %d\n", atomic_read(&adapter->pend_cmds));
2154#if MEGA_HAVE_STATS
2155 for(i = 0; i < adapter->numldrv; i++) {
2156 seq_printf(m, "Logical Drive %d:\n", i);
2157 seq_printf(m, "\tReads Issued = %lu, Writes Issued = %lu\n",
2158 adapter->nreads[i], adapter->nwrites[i]);
2159 seq_printf(m, "\tSectors Read = %lu, Sectors Written = %lu\n",
2160 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2161 seq_printf(m, "\tRead errors = %lu, Write errors = %lu\n\n",
2162 adapter->rd_errors[i], adapter->wr_errors[i]);
2163 }
2164#else
2165 seq_puts(m, "IO and error counters not compiled in driver.\n");
2166#endif
2167 return 0;
2168}
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179static int
2180proc_show_mbox(struct seq_file *m, void *v)
2181{
2182 adapter_t *adapter = m->private;
2183 volatile mbox_t *mbox = adapter->mbox;
2184
2185 seq_puts(m, "Contents of Mail Box Structure\n");
2186 seq_printf(m, " Fw Command = 0x%02x\n", mbox->m_out.cmd);
2187 seq_printf(m, " Cmd Sequence = 0x%02x\n", mbox->m_out.cmdid);
2188 seq_printf(m, " No of Sectors= %04d\n", mbox->m_out.numsectors);
2189 seq_printf(m, " LBA = 0x%02x\n", mbox->m_out.lba);
2190 seq_printf(m, " DTA = 0x%08x\n", mbox->m_out.xferaddr);
2191 seq_printf(m, " Logical Drive= 0x%02x\n", mbox->m_out.logdrv);
2192 seq_printf(m, " No of SG Elmt= 0x%02x\n", mbox->m_out.numsgelements);
2193 seq_printf(m, " Busy = %01x\n", mbox->m_in.busy);
2194 seq_printf(m, " Status = 0x%02x\n", mbox->m_in.status);
2195 return 0;
2196}
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206static int
2207proc_show_rebuild_rate(struct seq_file *m, void *v)
2208{
2209 adapter_t *adapter = m->private;
2210 dma_addr_t dma_handle;
2211 caddr_t inquiry;
2212 struct pci_dev *pdev;
2213
2214 if( make_local_pdev(adapter, &pdev) != 0 )
2215 return 0;
2216
2217 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2218 goto free_pdev;
2219
2220 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2221 seq_puts(m, "Adapter inquiry failed.\n");
2222 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2223 goto free_inquiry;
2224 }
2225
2226 if( adapter->flag & BOARD_40LD )
2227 seq_printf(m, "Rebuild Rate: [%d%%]\n",
2228 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2229 else
2230 seq_printf(m, "Rebuild Rate: [%d%%]\n",
2231 ((mraid_ext_inquiry *)
2232 inquiry)->raid_inq.adapter_info.rebuild_rate);
2233
2234free_inquiry:
2235 mega_free_inquiry(inquiry, dma_handle, pdev);
2236free_pdev:
2237 free_local_pdev(pdev);
2238 return 0;
2239}
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249static int
2250proc_show_battery(struct seq_file *m, void *v)
2251{
2252 adapter_t *adapter = m->private;
2253 dma_addr_t dma_handle;
2254 caddr_t inquiry;
2255 struct pci_dev *pdev;
2256 u8 battery_status;
2257
2258 if( make_local_pdev(adapter, &pdev) != 0 )
2259 return 0;
2260
2261 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2262 goto free_pdev;
2263
2264 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2265 seq_printf(m, "Adapter inquiry failed.\n");
2266 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2267 goto free_inquiry;
2268 }
2269
2270 if( adapter->flag & BOARD_40LD ) {
2271 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2272 }
2273 else {
2274 battery_status = ((mraid_ext_inquiry *)inquiry)->
2275 raid_inq.adapter_info.battery_status;
2276 }
2277
2278
2279
2280
2281 seq_printf(m, "Battery Status:[%d]", battery_status);
2282
2283 if(battery_status == MEGA_BATT_CHARGE_DONE)
2284 seq_puts(m, " Charge Done");
2285
2286 if(battery_status & MEGA_BATT_MODULE_MISSING)
2287 seq_puts(m, " Module Missing");
2288
2289 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2290 seq_puts(m, " Low Voltage");
2291
2292 if(battery_status & MEGA_BATT_TEMP_HIGH)
2293 seq_puts(m, " Temperature High");
2294
2295 if(battery_status & MEGA_BATT_PACK_MISSING)
2296 seq_puts(m, " Pack Missing");
2297
2298 if(battery_status & MEGA_BATT_CHARGE_INPROG)
2299 seq_puts(m, " Charge In-progress");
2300
2301 if(battery_status & MEGA_BATT_CHARGE_FAIL)
2302 seq_puts(m, " Charge Fail");
2303
2304 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2305 seq_puts(m, " Cycles Exceeded");
2306
2307 seq_putc(m, '\n');
2308
2309free_inquiry:
2310 mega_free_inquiry(inquiry, dma_handle, pdev);
2311free_pdev:
2312 free_local_pdev(pdev);
2313 return 0;
2314}
2315
2316
2317
2318
2319
2320static void
2321mega_print_inquiry(struct seq_file *m, char *scsi_inq)
2322{
2323 int i;
2324
2325 seq_puts(m, " Vendor: ");
2326 seq_write(m, scsi_inq + 8, 8);
2327 seq_puts(m, " Model: ");
2328 seq_write(m, scsi_inq + 16, 16);
2329 seq_puts(m, " Rev: ");
2330 seq_write(m, scsi_inq + 32, 4);
2331 seq_putc(m, '\n');
2332
2333 i = scsi_inq[0] & 0x1f;
2334 seq_printf(m, " Type: %s ", scsi_device_type(i));
2335
2336 seq_printf(m, " ANSI SCSI revision: %02x",
2337 scsi_inq[2] & 0x07);
2338
2339 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2340 seq_puts(m, " CCS\n");
2341 else
2342 seq_putc(m, '\n');
2343}
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353static int
2354proc_show_pdrv(struct seq_file *m, adapter_t *adapter, int channel)
2355{
2356 dma_addr_t dma_handle;
2357 char *scsi_inq;
2358 dma_addr_t scsi_inq_dma_handle;
2359 caddr_t inquiry;
2360 struct pci_dev *pdev;
2361 u8 *pdrv_state;
2362 u8 state;
2363 int tgt;
2364 int max_channels;
2365 int i;
2366
2367 if( make_local_pdev(adapter, &pdev) != 0 )
2368 return 0;
2369
2370 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2371 goto free_pdev;
2372
2373 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2374 seq_puts(m, "Adapter inquiry failed.\n");
2375 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2376 goto free_inquiry;
2377 }
2378
2379
2380 scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2381 if( scsi_inq == NULL ) {
2382 seq_puts(m, "memory not available for scsi inq.\n");
2383 goto free_inquiry;
2384 }
2385
2386 if( adapter->flag & BOARD_40LD ) {
2387 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2388 }
2389 else {
2390 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2391 raid_inq.pdrv_info.pdrv_state;
2392 }
2393
2394 max_channels = adapter->product_info.nchannels;
2395
2396 if( channel >= max_channels ) {
2397 goto free_pci;
2398 }
2399
2400 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2401
2402 i = channel*16 + tgt;
2403
2404 state = *(pdrv_state + i);
2405 switch( state & 0x0F ) {
2406 case PDRV_ONLINE:
2407 seq_printf(m, "Channel:%2d Id:%2d State: Online",
2408 channel, tgt);
2409 break;
2410
2411 case PDRV_FAILED:
2412 seq_printf(m, "Channel:%2d Id:%2d State: Failed",
2413 channel, tgt);
2414 break;
2415
2416 case PDRV_RBLD:
2417 seq_printf(m, "Channel:%2d Id:%2d State: Rebuild",
2418 channel, tgt);
2419 break;
2420
2421 case PDRV_HOTSPARE:
2422 seq_printf(m, "Channel:%2d Id:%2d State: Hot spare",
2423 channel, tgt);
2424 break;
2425
2426 default:
2427 seq_printf(m, "Channel:%2d Id:%2d State: Un-configured",
2428 channel, tgt);
2429 break;
2430 }
2431
2432
2433
2434
2435
2436
2437 memset(scsi_inq, 0, 256);
2438 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2439 scsi_inq_dma_handle) ||
2440 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2441 continue;
2442 }
2443
2444
2445
2446
2447
2448 seq_puts(m, ".\n");
2449 mega_print_inquiry(m, scsi_inq);
2450 }
2451
2452free_pci:
2453 pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2454free_inquiry:
2455 mega_free_inquiry(inquiry, dma_handle, pdev);
2456free_pdev:
2457 free_local_pdev(pdev);
2458 return 0;
2459}
2460
2461
2462
2463
2464
2465
2466
2467
2468static int
2469proc_show_pdrv_ch0(struct seq_file *m, void *v)
2470{
2471 return proc_show_pdrv(m, m->private, 0);
2472}
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482static int
2483proc_show_pdrv_ch1(struct seq_file *m, void *v)
2484{
2485 return proc_show_pdrv(m, m->private, 1);
2486}
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496static int
2497proc_show_pdrv_ch2(struct seq_file *m, void *v)
2498{
2499 return proc_show_pdrv(m, m->private, 2);
2500}
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510static int
2511proc_show_pdrv_ch3(struct seq_file *m, void *v)
2512{
2513 return proc_show_pdrv(m, m->private, 3);
2514}
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527static int
2528proc_show_rdrv(struct seq_file *m, adapter_t *adapter, int start, int end )
2529{
2530 dma_addr_t dma_handle;
2531 logdrv_param *lparam;
2532 megacmd_t mc;
2533 char *disk_array;
2534 dma_addr_t disk_array_dma_handle;
2535 caddr_t inquiry;
2536 struct pci_dev *pdev;
2537 u8 *rdrv_state;
2538 int num_ldrv;
2539 u32 array_sz;
2540 int i;
2541
2542 if( make_local_pdev(adapter, &pdev) != 0 )
2543 return 0;
2544
2545 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2546 goto free_pdev;
2547
2548 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2549 seq_puts(m, "Adapter inquiry failed.\n");
2550 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2551 goto free_inquiry;
2552 }
2553
2554 memset(&mc, 0, sizeof(megacmd_t));
2555
2556 if( adapter->flag & BOARD_40LD ) {
2557 array_sz = sizeof(disk_array_40ld);
2558
2559 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2560
2561 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2562 }
2563 else {
2564 array_sz = sizeof(disk_array_8ld);
2565
2566 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2567 raid_inq.logdrv_info.ldrv_state;
2568
2569 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2570 raid_inq.logdrv_info.num_ldrv;
2571 }
2572
2573 disk_array = pci_alloc_consistent(pdev, array_sz,
2574 &disk_array_dma_handle);
2575
2576 if( disk_array == NULL ) {
2577 seq_puts(m, "memory not available.\n");
2578 goto free_inquiry;
2579 }
2580
2581 mc.xferaddr = (u32)disk_array_dma_handle;
2582
2583 if( adapter->flag & BOARD_40LD ) {
2584 mc.cmd = FC_NEW_CONFIG;
2585 mc.opcode = OP_DCMD_READ_CONFIG;
2586
2587 if( mega_internal_command(adapter, &mc, NULL) ) {
2588 seq_puts(m, "40LD read config failed.\n");
2589 goto free_pci;
2590 }
2591
2592 }
2593 else {
2594 mc.cmd = NEW_READ_CONFIG_8LD;
2595
2596 if( mega_internal_command(adapter, &mc, NULL) ) {
2597 mc.cmd = READ_CONFIG_8LD;
2598 if( mega_internal_command(adapter, &mc, NULL) ) {
2599 seq_puts(m, "8LD read config failed.\n");
2600 goto free_pci;
2601 }
2602 }
2603 }
2604
2605 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2606
2607 if( adapter->flag & BOARD_40LD ) {
2608 lparam =
2609 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
2610 }
2611 else {
2612 lparam =
2613 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
2614 }
2615
2616
2617
2618
2619
2620 seq_printf(m, "Logical drive:%2d:, ", i);
2621
2622 switch( rdrv_state[i] & 0x0F ) {
2623 case RDRV_OFFLINE:
2624 seq_puts(m, "state: offline");
2625 break;
2626 case RDRV_DEGRADED:
2627 seq_puts(m, "state: degraded");
2628 break;
2629 case RDRV_OPTIMAL:
2630 seq_puts(m, "state: optimal");
2631 break;
2632 case RDRV_DELETED:
2633 seq_puts(m, "state: deleted");
2634 break;
2635 default:
2636 seq_puts(m, "state: unknown");
2637 break;
2638 }
2639
2640
2641
2642
2643
2644 if( (rdrv_state[i] & 0xF0) == 0x20 )
2645 seq_puts(m, ", check-consistency in progress");
2646 else if( (rdrv_state[i] & 0xF0) == 0x10 )
2647 seq_puts(m, ", initialization in progress");
2648
2649 seq_putc(m, '\n');
2650
2651 seq_printf(m, "Span depth:%3d, ", lparam->span_depth);
2652 seq_printf(m, "RAID level:%3d, ", lparam->level);
2653 seq_printf(m, "Stripe size:%3d, ",
2654 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
2655 seq_printf(m, "Row size:%3d\n", lparam->row_size);
2656
2657 seq_puts(m, "Read Policy: ");
2658 switch(lparam->read_ahead) {
2659 case NO_READ_AHEAD:
2660 seq_puts(m, "No read ahead, ");
2661 break;
2662 case READ_AHEAD:
2663 seq_puts(m, "Read ahead, ");
2664 break;
2665 case ADAP_READ_AHEAD:
2666 seq_puts(m, "Adaptive, ");
2667 break;
2668
2669 }
2670
2671 seq_puts(m, "Write Policy: ");
2672 switch(lparam->write_mode) {
2673 case WRMODE_WRITE_THRU:
2674 seq_puts(m, "Write thru, ");
2675 break;
2676 case WRMODE_WRITE_BACK:
2677 seq_puts(m, "Write back, ");
2678 break;
2679 }
2680
2681 seq_puts(m, "Cache Policy: ");
2682 switch(lparam->direct_io) {
2683 case CACHED_IO:
2684 seq_puts(m, "Cached IO\n\n");
2685 break;
2686 case DIRECT_IO:
2687 seq_puts(m, "Direct IO\n\n");
2688 break;
2689 }
2690 }
2691
2692free_pci:
2693 pci_free_consistent(pdev, array_sz, disk_array,
2694 disk_array_dma_handle);
2695free_inquiry:
2696 mega_free_inquiry(inquiry, dma_handle, pdev);
2697free_pdev:
2698 free_local_pdev(pdev);
2699 return 0;
2700}
2701
2702
2703
2704
2705
2706
2707
2708
2709static int
2710proc_show_rdrv_10(struct seq_file *m, void *v)
2711{
2712 return proc_show_rdrv(m, m->private, 0, 9);
2713}
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723static int
2724proc_show_rdrv_20(struct seq_file *m, void *v)
2725{
2726 return proc_show_rdrv(m, m->private, 10, 19);
2727}
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737static int
2738proc_show_rdrv_30(struct seq_file *m, void *v)
2739{
2740 return proc_show_rdrv(m, m->private, 20, 29);
2741}
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751static int
2752proc_show_rdrv_40(struct seq_file *m, void *v)
2753{
2754 return proc_show_rdrv(m, m->private, 30, 39);
2755}
2756
2757
2758
2759
2760
2761static int mega_proc_open(struct inode *inode, struct file *file)
2762{
2763 adapter_t *adapter = proc_get_parent_data(inode);
2764 int (*show)(struct seq_file *, void *) = PDE_DATA(inode);
2765
2766 return single_open(file, show, adapter);
2767}
2768
2769static const struct file_operations mega_proc_fops = {
2770 .open = mega_proc_open,
2771 .read = seq_read,
2772 .llseek = seq_lseek,
2773 .release = single_release,
2774};
2775
2776
2777
2778
2779struct mega_proc_file {
2780 const char *name;
2781 unsigned short ptr_offset;
2782 int (*show) (struct seq_file *m, void *v);
2783};
2784
2785static const struct mega_proc_file mega_proc_files[] = {
2786 { "config", offsetof(adapter_t, proc_read), proc_show_config },
2787 { "stat", offsetof(adapter_t, proc_stat), proc_show_stat },
2788 { "mailbox", offsetof(adapter_t, proc_mbox), proc_show_mbox },
2789#if MEGA_HAVE_ENH_PROC
2790 { "rebuild-rate", offsetof(adapter_t, proc_rr), proc_show_rebuild_rate },
2791 { "battery-status", offsetof(adapter_t, proc_battery), proc_show_battery },
2792 { "diskdrives-ch0", offsetof(adapter_t, proc_pdrvstat[0]), proc_show_pdrv_ch0 },
2793 { "diskdrives-ch1", offsetof(adapter_t, proc_pdrvstat[1]), proc_show_pdrv_ch1 },
2794 { "diskdrives-ch2", offsetof(adapter_t, proc_pdrvstat[2]), proc_show_pdrv_ch2 },
2795 { "diskdrives-ch3", offsetof(adapter_t, proc_pdrvstat[3]), proc_show_pdrv_ch3 },
2796 { "raiddrives-0-9", offsetof(adapter_t, proc_rdrvstat[0]), proc_show_rdrv_10 },
2797 { "raiddrives-10-19", offsetof(adapter_t, proc_rdrvstat[1]), proc_show_rdrv_20 },
2798 { "raiddrives-20-29", offsetof(adapter_t, proc_rdrvstat[2]), proc_show_rdrv_30 },
2799 { "raiddrives-30-39", offsetof(adapter_t, proc_rdrvstat[3]), proc_show_rdrv_40 },
2800#endif
2801 { NULL }
2802};
2803
2804
2805
2806
2807
2808
2809
2810
2811static void
2812mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2813{
2814 const struct mega_proc_file *f;
2815 adapter_t *adapter = hba_soft_state[index];
2816 struct proc_dir_entry *dir, *de, **ppde;
2817 u8 string[16];
2818
2819 sprintf(string, "hba%d", adapter->host->host_no);
2820
2821 dir = adapter->controller_proc_dir_entry =
2822 proc_mkdir_data(string, 0, parent, adapter);
2823 if(!dir) {
2824 printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2825 return;
2826 }
2827
2828 for (f = mega_proc_files; f->name; f++) {
2829 de = proc_create_data(f->name, S_IRUSR, dir, &mega_proc_fops,
2830 f->show);
2831 if (!de) {
2832 printk(KERN_WARNING "\nmegaraid: proc_create failed\n");
2833 return;
2834 }
2835
2836 ppde = (void *)adapter + f->ptr_offset;
2837 *ppde = de;
2838 }
2839}
2840
2841#else
2842static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2843{
2844}
2845#endif
2846
2847
2848
2849
2850
2851
2852
2853static int
2854megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
2855 sector_t capacity, int geom[])
2856{
2857 adapter_t *adapter;
2858 unsigned char *bh;
2859 int heads;
2860 int sectors;
2861 int cylinders;
2862 int rval;
2863
2864
2865 adapter = (adapter_t *)sdev->host->hostdata;
2866
2867 if (IS_RAID_CH(adapter, sdev->channel)) {
2868
2869 heads = 64;
2870 sectors = 32;
2871 cylinders = (ulong)capacity / (heads * sectors);
2872
2873
2874
2875
2876
2877 if ((ulong)capacity >= 0x200000) {
2878 heads = 255;
2879 sectors = 63;
2880 cylinders = (ulong)capacity / (heads * sectors);
2881 }
2882
2883
2884 geom[0] = heads;
2885 geom[1] = sectors;
2886 geom[2] = cylinders;
2887 }
2888 else {
2889 bh = scsi_bios_ptable(bdev);
2890
2891 if( bh ) {
2892 rval = scsi_partsize(bh, capacity,
2893 &geom[2], &geom[0], &geom[1]);
2894 kfree(bh);
2895 if( rval != -1 )
2896 return rval;
2897 }
2898
2899 printk(KERN_INFO
2900 "megaraid: invalid partition on this disk on channel %d\n",
2901 sdev->channel);
2902
2903
2904 heads = 64;
2905 sectors = 32;
2906 cylinders = (ulong)capacity / (heads * sectors);
2907
2908
2909 if ((ulong)capacity >= 0x200000) {
2910 heads = 255;
2911 sectors = 63;
2912 cylinders = (ulong)capacity / (heads * sectors);
2913 }
2914
2915
2916 geom[0] = heads;
2917 geom[1] = sectors;
2918 geom[2] = cylinders;
2919 }
2920
2921 return 0;
2922}
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932static int
2933mega_init_scb(adapter_t *adapter)
2934{
2935 scb_t *scb;
2936 int i;
2937
2938 for( i = 0; i < adapter->max_cmds; i++ ) {
2939
2940 scb = &adapter->scb_list[i];
2941
2942 scb->sgl64 = NULL;
2943 scb->sgl = NULL;
2944 scb->pthru = NULL;
2945 scb->epthru = NULL;
2946 }
2947
2948 for( i = 0; i < adapter->max_cmds; i++ ) {
2949
2950 scb = &adapter->scb_list[i];
2951
2952 scb->idx = i;
2953
2954 scb->sgl64 = pci_alloc_consistent(adapter->dev,
2955 sizeof(mega_sgl64) * adapter->sglen,
2956 &scb->sgl_dma_addr);
2957
2958 scb->sgl = (mega_sglist *)scb->sgl64;
2959
2960 if( !scb->sgl ) {
2961 printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
2962 mega_free_sgl(adapter);
2963 return -1;
2964 }
2965
2966 scb->pthru = pci_alloc_consistent(adapter->dev,
2967 sizeof(mega_passthru),
2968 &scb->pthru_dma_addr);
2969
2970 if( !scb->pthru ) {
2971 printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
2972 mega_free_sgl(adapter);
2973 return -1;
2974 }
2975
2976 scb->epthru = pci_alloc_consistent(adapter->dev,
2977 sizeof(mega_ext_passthru),
2978 &scb->epthru_dma_addr);
2979
2980 if( !scb->epthru ) {
2981 printk(KERN_WARNING
2982 "Can't allocate extended passthru.\n");
2983 mega_free_sgl(adapter);
2984 return -1;
2985 }
2986
2987
2988 scb->dma_type = MEGA_DMA_TYPE_NONE;
2989
2990
2991
2992
2993
2994
2995 scb->state = SCB_FREE;
2996 scb->cmd = NULL;
2997 list_add(&scb->list, &adapter->free_list);
2998 }
2999
3000 return 0;
3001}
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012static int
3013megadev_open (struct inode *inode, struct file *filep)
3014{
3015
3016
3017
3018 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
3019
3020 return 0;
3021}
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036static int
3037megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3038{
3039 adapter_t *adapter;
3040 nitioctl_t uioc;
3041 int adapno;
3042 int rval;
3043 mega_passthru __user *upthru;
3044 mega_passthru *pthru;
3045 dma_addr_t pthru_dma_hndl;
3046 void *data = NULL;
3047 dma_addr_t data_dma_hndl;
3048 megacmd_t mc;
3049 megastat_t __user *ustats;
3050 int num_ldrv;
3051 u32 uxferaddr = 0;
3052 struct pci_dev *pdev;
3053
3054 ustats = NULL;
3055 num_ldrv = 0;
3056
3057
3058
3059
3060
3061 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3062 return -EINVAL;
3063 }
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073 memset(&uioc, 0, sizeof(nitioctl_t));
3074 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3075 return rval;
3076
3077
3078 switch( uioc.opcode ) {
3079
3080 case GET_DRIVER_VER:
3081 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3082 return (-EFAULT);
3083
3084 break;
3085
3086 case GET_N_ADAP:
3087 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3088 return (-EFAULT);
3089
3090
3091
3092
3093
3094
3095 return hba_count;
3096
3097 case GET_ADAP_INFO:
3098
3099
3100
3101
3102 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3103 return (-ENODEV);
3104
3105 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3106 sizeof(struct mcontroller)) )
3107 return (-EFAULT);
3108 break;
3109
3110#if MEGA_HAVE_STATS
3111
3112 case GET_STATS:
3113
3114
3115
3116 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3117 return (-ENODEV);
3118
3119 adapter = hba_soft_state[adapno];
3120
3121 ustats = uioc.uioc_uaddr;
3122
3123 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3124 return (-EFAULT);
3125
3126
3127
3128
3129 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3130
3131 if( copy_to_user(ustats->nreads, adapter->nreads,
3132 num_ldrv*sizeof(u32)) )
3133 return -EFAULT;
3134
3135 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3136 num_ldrv*sizeof(u32)) )
3137 return -EFAULT;
3138
3139 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3140 num_ldrv*sizeof(u32)) )
3141 return -EFAULT;
3142
3143 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3144 num_ldrv*sizeof(u32)) )
3145 return -EFAULT;
3146
3147 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3148 num_ldrv*sizeof(u32)) )
3149 return -EFAULT;
3150
3151 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3152 num_ldrv*sizeof(u32)) )
3153 return -EFAULT;
3154
3155 return 0;
3156
3157#endif
3158 case MBOX_CMD:
3159
3160
3161
3162
3163 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3164 return (-ENODEV);
3165
3166 adapter = hba_soft_state[adapno];
3167
3168
3169
3170
3171
3172 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3173 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3174
3175
3176
3177
3178 if( !adapter->support_random_del ) {
3179 printk(KERN_WARNING "megaraid: logdrv ");
3180 printk("delete on non-supporting F/W.\n");
3181
3182 return (-EINVAL);
3183 }
3184
3185 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3186
3187 if( rval == 0 ) {
3188 memset(&mc, 0, sizeof(megacmd_t));
3189
3190 mc.status = rval;
3191
3192 rval = mega_n_to_m((void __user *)arg, &mc);
3193 }
3194
3195 return rval;
3196 }
3197
3198
3199
3200
3201 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3202 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3203
3204 printk(KERN_WARNING "megaraid: rejected passthru.\n");
3205
3206 return (-EINVAL);
3207 }
3208
3209
3210
3211
3212
3213 if( make_local_pdev(adapter, &pdev) != 0 )
3214 return -EIO;
3215
3216
3217 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3218
3219
3220 pthru = pci_alloc_consistent(pdev,
3221 sizeof(mega_passthru),
3222 &pthru_dma_hndl);
3223
3224 if( pthru == NULL ) {
3225 free_local_pdev(pdev);
3226 return (-ENOMEM);
3227 }
3228
3229
3230
3231
3232 upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
3233
3234
3235
3236
3237 if( copy_from_user(pthru, upthru,
3238 sizeof(mega_passthru)) ) {
3239
3240 pci_free_consistent(pdev,
3241 sizeof(mega_passthru), pthru,
3242 pthru_dma_hndl);
3243
3244 free_local_pdev(pdev);
3245
3246 return (-EFAULT);
3247 }
3248
3249
3250
3251
3252 if( pthru->dataxferlen ) {
3253 data = pci_alloc_consistent(pdev,
3254 pthru->dataxferlen,
3255 &data_dma_hndl);
3256
3257 if( data == NULL ) {
3258 pci_free_consistent(pdev,
3259 sizeof(mega_passthru),
3260 pthru,
3261 pthru_dma_hndl);
3262
3263 free_local_pdev(pdev);
3264
3265 return (-ENOMEM);
3266 }
3267
3268
3269
3270
3271
3272 uxferaddr = pthru->dataxferaddr;
3273 pthru->dataxferaddr = data_dma_hndl;
3274 }
3275
3276
3277
3278
3279
3280 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3281
3282
3283
3284 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3285 pthru->dataxferlen) ) {
3286 rval = (-EFAULT);
3287 goto freemem_and_return;
3288 }
3289 }
3290
3291 memset(&mc, 0, sizeof(megacmd_t));
3292
3293 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3294 mc.xferaddr = (u32)pthru_dma_hndl;
3295
3296
3297
3298
3299 mega_internal_command(adapter, &mc, pthru);
3300
3301 rval = mega_n_to_m((void __user *)arg, &mc);
3302
3303 if( rval ) goto freemem_and_return;
3304
3305
3306
3307
3308
3309 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3310 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3311 pthru->dataxferlen) ) {
3312 rval = (-EFAULT);
3313 }
3314 }
3315
3316
3317
3318
3319
3320 if (copy_to_user(upthru->reqsensearea,
3321 pthru->reqsensearea, 14))
3322 rval = -EFAULT;
3323
3324freemem_and_return:
3325 if( pthru->dataxferlen ) {
3326 pci_free_consistent(pdev,
3327 pthru->dataxferlen, data,
3328 data_dma_hndl);
3329 }
3330
3331 pci_free_consistent(pdev, sizeof(mega_passthru),
3332 pthru, pthru_dma_hndl);
3333
3334 free_local_pdev(pdev);
3335
3336 return rval;
3337 }
3338 else {
3339
3340
3341
3342
3343
3344 if( uioc.xferlen ) {
3345 data = pci_alloc_consistent(pdev,
3346 uioc.xferlen, &data_dma_hndl);
3347
3348 if( data == NULL ) {
3349 free_local_pdev(pdev);
3350 return (-ENOMEM);
3351 }
3352
3353 uxferaddr = MBOX(uioc)->xferaddr;
3354 }
3355
3356
3357
3358
3359 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3360
3361
3362
3363 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3364 uioc.xferlen) ) {
3365
3366 pci_free_consistent(pdev,
3367 uioc.xferlen,
3368 data, data_dma_hndl);
3369
3370 free_local_pdev(pdev);
3371
3372 return (-EFAULT);
3373 }
3374 }
3375
3376 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3377
3378 mc.xferaddr = (u32)data_dma_hndl;
3379
3380
3381
3382
3383 mega_internal_command(adapter, &mc, NULL);
3384
3385 rval = mega_n_to_m((void __user *)arg, &mc);
3386
3387 if( rval ) {
3388 if( uioc.xferlen ) {
3389 pci_free_consistent(pdev,
3390 uioc.xferlen, data,
3391 data_dma_hndl);
3392 }
3393
3394 free_local_pdev(pdev);
3395
3396 return rval;
3397 }
3398
3399
3400
3401
3402 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3403 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3404 uioc.xferlen) ) {
3405
3406 rval = (-EFAULT);
3407 }
3408 }
3409
3410 if( uioc.xferlen ) {
3411 pci_free_consistent(pdev,
3412 uioc.xferlen, data,
3413 data_dma_hndl);
3414 }
3415
3416 free_local_pdev(pdev);
3417
3418 return rval;
3419 }
3420
3421 default:
3422 return (-EINVAL);
3423 }
3424
3425 return 0;
3426}
3427
3428static long
3429megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3430{
3431 int ret;
3432
3433 mutex_lock(&megadev_mutex);
3434 ret = megadev_ioctl(filep, cmd, arg);
3435 mutex_unlock(&megadev_mutex);
3436
3437 return ret;
3438}
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450static int
3451mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3452{
3453 struct uioctl_t uioc_mimd;
3454 char signature[8] = {0};
3455 u8 opcode;
3456 u8 subopcode;
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466 if( copy_from_user(signature, arg, 7) )
3467 return (-EFAULT);
3468
3469 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3470
3471
3472
3473
3474
3475
3476
3477 return -EINVAL;
3478#if 0
3479 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3480 return (-EFAULT);
3481 return 0;
3482#endif
3483 }
3484
3485
3486
3487
3488
3489
3490 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3491 return (-EFAULT);
3492
3493
3494
3495
3496
3497 opcode = uioc_mimd.ui.fcs.opcode;
3498 subopcode = uioc_mimd.ui.fcs.subopcode;
3499
3500 switch (opcode) {
3501 case 0x82:
3502
3503 switch (subopcode) {
3504
3505 case MEGAIOC_QDRVRVER:
3506 uioc->opcode = GET_DRIVER_VER;
3507 uioc->uioc_uaddr = uioc_mimd.data;
3508 break;
3509
3510 case MEGAIOC_QNADAP:
3511 uioc->opcode = GET_N_ADAP;
3512 uioc->uioc_uaddr = uioc_mimd.data;
3513 break;
3514
3515 case MEGAIOC_QADAPINFO:
3516 uioc->opcode = GET_ADAP_INFO;
3517 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3518 uioc->uioc_uaddr = uioc_mimd.data;
3519 break;
3520
3521 default:
3522 return(-EINVAL);
3523 }
3524
3525 break;
3526
3527
3528 case 0x81:
3529
3530 uioc->opcode = MBOX_CMD;
3531 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3532
3533 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3534
3535 uioc->xferlen = uioc_mimd.ui.fcs.length;
3536
3537 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3538 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3539
3540 break;
3541
3542 case 0x80:
3543
3544 uioc->opcode = MBOX_CMD;
3545 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3546
3547 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3548
3549
3550
3551
3552 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3553 uioc_mimd.outlen : uioc_mimd.inlen;
3554
3555 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3556 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3557
3558 break;
3559
3560 default:
3561 return (-EINVAL);
3562
3563 }
3564
3565 return 0;
3566}
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576static int
3577mega_n_to_m(void __user *arg, megacmd_t *mc)
3578{
3579 nitioctl_t __user *uiocp;
3580 megacmd_t __user *umc;
3581 mega_passthru __user *upthru;
3582 struct uioctl_t __user *uioc_mimd;
3583 char signature[8] = {0};
3584
3585
3586
3587
3588 if( copy_from_user(signature, arg, 7) )
3589 return -EFAULT;
3590
3591 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3592
3593 uiocp = arg;
3594
3595 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3596 return (-EFAULT);
3597
3598 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3599
3600 umc = MBOX_P(uiocp);
3601
3602 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3603 return -EFAULT;
3604
3605 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3606 return (-EFAULT);
3607 }
3608 }
3609 else {
3610 uioc_mimd = arg;
3611
3612 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3613 return (-EFAULT);
3614
3615 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3616
3617 umc = (megacmd_t __user *)uioc_mimd->mbox;
3618
3619 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3620 return (-EFAULT);
3621
3622 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3623 return (-EFAULT);
3624 }
3625 }
3626
3627 return 0;
3628}
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641static int
3642mega_is_bios_enabled(adapter_t *adapter)
3643{
3644 unsigned char raw_mbox[sizeof(struct mbox_out)];
3645 mbox_t *mbox;
3646 int ret;
3647
3648 mbox = (mbox_t *)raw_mbox;
3649
3650 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3651
3652 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3653
3654 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3655
3656 raw_mbox[0] = IS_BIOS_ENABLED;
3657 raw_mbox[2] = GET_BIOS;
3658
3659
3660 ret = issue_scb_block(adapter, raw_mbox);
3661
3662 return *(char *)adapter->mega_buffer;
3663}
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674static void
3675mega_enum_raid_scsi(adapter_t *adapter)
3676{
3677 unsigned char raw_mbox[sizeof(struct mbox_out)];
3678 mbox_t *mbox;
3679 int i;
3680
3681 mbox = (mbox_t *)raw_mbox;
3682
3683 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3684
3685
3686
3687
3688 raw_mbox[0] = CHNL_CLASS;
3689 raw_mbox[2] = GET_CHNL_CLASS;
3690
3691 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3692
3693 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3694
3695
3696
3697
3698
3699 adapter->mega_ch_class = 0xFF;
3700
3701 if(!issue_scb_block(adapter, raw_mbox)) {
3702 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3703
3704 }
3705
3706 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
3707 if( (adapter->mega_ch_class >> i) & 0x01 ) {
3708 printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
3709 i);
3710 }
3711 else {
3712 printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
3713 i);
3714 }
3715 }
3716
3717 return;
3718}
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728static void
3729mega_get_boot_drv(adapter_t *adapter)
3730{
3731 struct private_bios_data *prv_bios_data;
3732 unsigned char raw_mbox[sizeof(struct mbox_out)];
3733 mbox_t *mbox;
3734 u16 cksum = 0;
3735 u8 *cksum_p;
3736 u8 boot_pdrv;
3737 int i;
3738
3739 mbox = (mbox_t *)raw_mbox;
3740
3741 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3742
3743 raw_mbox[0] = BIOS_PVT_DATA;
3744 raw_mbox[2] = GET_BIOS_PVT_DATA;
3745
3746 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3747
3748 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3749
3750 adapter->boot_ldrv_enabled = 0;
3751 adapter->boot_ldrv = 0;
3752
3753 adapter->boot_pdrv_enabled = 0;
3754 adapter->boot_pdrv_ch = 0;
3755 adapter->boot_pdrv_tgt = 0;
3756
3757 if(issue_scb_block(adapter, raw_mbox) == 0) {
3758 prv_bios_data =
3759 (struct private_bios_data *)adapter->mega_buffer;
3760
3761 cksum = 0;
3762 cksum_p = (char *)prv_bios_data;
3763 for (i = 0; i < 14; i++ ) {
3764 cksum += (u16)(*cksum_p++);
3765 }
3766
3767 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
3768
3769
3770
3771
3772
3773 if( prv_bios_data->boot_drv & 0x80 ) {
3774 adapter->boot_pdrv_enabled = 1;
3775 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
3776 adapter->boot_pdrv_ch = boot_pdrv / 16;
3777 adapter->boot_pdrv_tgt = boot_pdrv % 16;
3778 }
3779 else {
3780 adapter->boot_ldrv_enabled = 1;
3781 adapter->boot_ldrv = prv_bios_data->boot_drv;
3782 }
3783 }
3784 }
3785
3786}
3787
3788
3789
3790
3791
3792
3793
3794
3795static int
3796mega_support_random_del(adapter_t *adapter)
3797{
3798 unsigned char raw_mbox[sizeof(struct mbox_out)];
3799 mbox_t *mbox;
3800 int rval;
3801
3802 mbox = (mbox_t *)raw_mbox;
3803
3804 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3805
3806
3807
3808
3809 raw_mbox[0] = FC_DEL_LOGDRV;
3810 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3811
3812 rval = issue_scb_block(adapter, raw_mbox);
3813
3814 return !rval;
3815}
3816
3817
3818
3819
3820
3821
3822
3823
3824static int
3825mega_support_ext_cdb(adapter_t *adapter)
3826{
3827 unsigned char raw_mbox[sizeof(struct mbox_out)];
3828 mbox_t *mbox;
3829 int rval;
3830
3831 mbox = (mbox_t *)raw_mbox;
3832
3833 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3834
3835
3836
3837 raw_mbox[0] = 0xA4;
3838 raw_mbox[2] = 0x16;
3839
3840 rval = issue_scb_block(adapter, raw_mbox);
3841
3842 return !rval;
3843}
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854static int
3855mega_del_logdrv(adapter_t *adapter, int logdrv)
3856{
3857 unsigned long flags;
3858 scb_t *scb;
3859 int rval;
3860
3861
3862
3863
3864
3865 atomic_set(&adapter->quiescent, 1);
3866
3867
3868
3869
3870
3871 while (atomic_read(&adapter->pend_cmds) > 0 ||
3872 !list_empty(&adapter->pending_list))
3873 msleep(1000);
3874
3875 rval = mega_do_del_logdrv(adapter, logdrv);
3876
3877 spin_lock_irqsave(&adapter->lock, flags);
3878
3879
3880
3881
3882
3883 if (adapter->read_ldidmap) {
3884 struct list_head *pos;
3885 list_for_each(pos, &adapter->pending_list) {
3886 scb = list_entry(pos, scb_t, list);
3887 if (scb->pthru->logdrv < 0x80 )
3888 scb->pthru->logdrv += 0x80;
3889 }
3890 }
3891
3892 atomic_set(&adapter->quiescent, 0);
3893
3894 mega_runpendq(adapter);
3895
3896 spin_unlock_irqrestore(&adapter->lock, flags);
3897
3898 return rval;
3899}
3900
3901
3902static int
3903mega_do_del_logdrv(adapter_t *adapter, int logdrv)
3904{
3905 megacmd_t mc;
3906 int rval;
3907
3908 memset( &mc, 0, sizeof(megacmd_t));
3909
3910 mc.cmd = FC_DEL_LOGDRV;
3911 mc.opcode = OP_DEL_LOGDRV;
3912 mc.subopcode = logdrv;
3913
3914 rval = mega_internal_command(adapter, &mc, NULL);
3915
3916
3917 if(rval) {
3918 printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
3919 return rval;
3920 }
3921
3922
3923
3924
3925
3926 adapter->read_ldidmap = 1;
3927
3928 return rval;
3929}
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939static void
3940mega_get_max_sgl(adapter_t *adapter)
3941{
3942 unsigned char raw_mbox[sizeof(struct mbox_out)];
3943 mbox_t *mbox;
3944
3945 mbox = (mbox_t *)raw_mbox;
3946
3947 memset(mbox, 0, sizeof(raw_mbox));
3948
3949 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3950
3951 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3952
3953 raw_mbox[0] = MAIN_MISC_OPCODE;
3954 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3955
3956
3957 if( issue_scb_block(adapter, raw_mbox) ) {
3958
3959
3960
3961 adapter->sglen = MIN_SGLIST;
3962 }
3963 else {
3964 adapter->sglen = *((char *)adapter->mega_buffer);
3965
3966
3967
3968
3969
3970 if ( adapter->sglen > MAX_SGLIST )
3971 adapter->sglen = MAX_SGLIST;
3972 }
3973
3974 return;
3975}
3976
3977
3978
3979
3980
3981
3982
3983
3984static int
3985mega_support_cluster(adapter_t *adapter)
3986{
3987 unsigned char raw_mbox[sizeof(struct mbox_out)];
3988 mbox_t *mbox;
3989
3990 mbox = (mbox_t *)raw_mbox;
3991
3992 memset(mbox, 0, sizeof(raw_mbox));
3993
3994 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3995
3996 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3997
3998
3999
4000
4001
4002 raw_mbox[0] = MEGA_GET_TARGET_ID;
4003
4004 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
4005
4006
4007
4008
4009
4010 adapter->this_id = *(u32 *)adapter->mega_buffer;
4011 adapter->host->this_id = adapter->this_id;
4012
4013 return 1;
4014 }
4015
4016 return 0;
4017}
4018
4019#ifdef CONFIG_PROC_FS
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029static int
4030mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
4031{
4032 megacmd_t mc;
4033
4034 memset(&mc, 0, sizeof(megacmd_t));
4035
4036 if( adapter->flag & BOARD_40LD ) {
4037 mc.cmd = FC_NEW_CONFIG;
4038 mc.opcode = NC_SUBOP_ENQUIRY3;
4039 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
4040 }
4041 else {
4042 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
4043 }
4044
4045 mc.xferaddr = (u32)dma_handle;
4046
4047 if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
4048 return -1;
4049 }
4050
4051 return 0;
4052}
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063static int
4064mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4065 dma_addr_t buf_dma_handle)
4066{
4067 mega_passthru *pthru;
4068 dma_addr_t pthru_dma_handle;
4069 megacmd_t mc;
4070 int rval;
4071 struct pci_dev *pdev;
4072
4073
4074
4075
4076
4077
4078 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4079
4080 pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4081 &pthru_dma_handle);
4082
4083 if( pthru == NULL ) {
4084 free_local_pdev(pdev);
4085 return -1;
4086 }
4087
4088 pthru->timeout = 2;
4089 pthru->ars = 1;
4090 pthru->reqsenselen = 14;
4091 pthru->islogical = 0;
4092
4093 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4094
4095 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4096
4097 pthru->cdblen = 6;
4098
4099 pthru->cdb[0] = INQUIRY;
4100 pthru->cdb[1] = 0;
4101 pthru->cdb[2] = 0;
4102 pthru->cdb[3] = 0;
4103 pthru->cdb[4] = 255;
4104 pthru->cdb[5] = 0;
4105
4106
4107 pthru->dataxferaddr = (u32)buf_dma_handle;
4108 pthru->dataxferlen = 256;
4109
4110 memset(&mc, 0, sizeof(megacmd_t));
4111
4112 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4113 mc.xferaddr = (u32)pthru_dma_handle;
4114
4115 rval = mega_internal_command(adapter, &mc, pthru);
4116
4117 pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4118 pthru_dma_handle);
4119
4120 free_local_pdev(pdev);
4121
4122 return rval;
4123}
4124#endif
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141static int
4142mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4143{
4144 Scsi_Cmnd *scmd;
4145 struct scsi_device *sdev;
4146 scb_t *scb;
4147 int rval;
4148
4149 scmd = scsi_allocate_command(GFP_KERNEL);
4150 if (!scmd)
4151 return -ENOMEM;
4152
4153
4154
4155
4156
4157
4158 mutex_lock(&adapter->int_mtx);
4159
4160 scb = &adapter->int_scb;
4161 memset(scb, 0, sizeof(scb_t));
4162
4163 sdev = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
4164 scmd->device = sdev;
4165
4166 memset(adapter->int_cdb, 0, sizeof(adapter->int_cdb));
4167 scmd->cmnd = adapter->int_cdb;
4168 scmd->device->host = adapter->host;
4169 scmd->host_scribble = (void *)scb;
4170 scmd->cmnd[0] = MEGA_INTERNAL_CMD;
4171
4172 scb->state |= SCB_ACTIVE;
4173 scb->cmd = scmd;
4174
4175 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4176
4177
4178
4179
4180 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4181
4182 scb->pthru = pthru;
4183 }
4184
4185 scb->idx = CMDID_INT_CMDS;
4186
4187 megaraid_queue_lck(scmd, mega_internal_done);
4188
4189 wait_for_completion(&adapter->int_waitq);
4190
4191 rval = scmd->result;
4192 mc->status = scmd->result;
4193 kfree(sdev);
4194
4195
4196
4197
4198
4199 if( scmd->result && trace_level ) {
4200 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4201 mc->cmd, mc->opcode, mc->subopcode, scmd->result);
4202 }
4203
4204 mutex_unlock(&adapter->int_mtx);
4205
4206 scsi_free_command(GFP_KERNEL, scmd);
4207
4208 return rval;
4209}
4210
4211
4212
4213
4214
4215
4216
4217
4218static void
4219mega_internal_done(Scsi_Cmnd *scmd)
4220{
4221 adapter_t *adapter;
4222
4223 adapter = (adapter_t *)scmd->device->host->hostdata;
4224
4225 complete(&adapter->int_waitq);
4226
4227}
4228
4229
4230static struct scsi_host_template megaraid_template = {
4231 .module = THIS_MODULE,
4232 .name = "MegaRAID",
4233 .proc_name = "megaraid_legacy",
4234 .info = megaraid_info,
4235 .queuecommand = megaraid_queue,
4236 .bios_param = megaraid_biosparam,
4237 .max_sectors = MAX_SECTORS_PER_IO,
4238 .can_queue = MAX_COMMANDS,
4239 .this_id = DEFAULT_INITIATOR_ID,
4240 .sg_tablesize = MAX_SGLIST,
4241 .cmd_per_lun = DEF_CMD_PER_LUN,
4242 .use_clustering = ENABLE_CLUSTERING,
4243 .eh_abort_handler = megaraid_abort,
4244 .eh_device_reset_handler = megaraid_reset,
4245 .eh_bus_reset_handler = megaraid_reset,
4246 .eh_host_reset_handler = megaraid_reset,
4247};
4248
4249static int
4250megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4251{
4252 struct Scsi_Host *host;
4253 adapter_t *adapter;
4254 unsigned long mega_baseport, tbase, flag = 0;
4255 u16 subsysid, subsysvid;
4256 u8 pci_bus, pci_dev_func;
4257 int irq, i, j;
4258 int error = -ENODEV;
4259
4260 if (pci_enable_device(pdev))
4261 goto out;
4262 pci_set_master(pdev);
4263
4264 pci_bus = pdev->bus->number;
4265 pci_dev_func = pdev->devfn;
4266
4267
4268
4269
4270
4271 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4272 u16 magic;
4273
4274
4275
4276
4277 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4278 pdev->subsystem_device == 0xC000)
4279 return -ENODEV;
4280
4281 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4282 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4283 return -ENODEV;
4284
4285 }
4286
4287
4288
4289
4290
4291 if (id->driver_data & BOARD_64BIT)
4292 flag |= BOARD_64BIT;
4293 else {
4294 u32 magic64;
4295
4296 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4297 if (magic64 == HBA_SIGNATURE_64BIT)
4298 flag |= BOARD_64BIT;
4299 }
4300
4301 subsysvid = pdev->subsystem_vendor;
4302 subsysid = pdev->subsystem_device;
4303
4304 printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4305 id->vendor, id->device, pci_bus);
4306
4307 printk("slot %d:func %d\n",
4308 PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
4309
4310
4311 mega_baseport = pci_resource_start(pdev, 0);
4312 irq = pdev->irq;
4313
4314 tbase = mega_baseport;
4315 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4316 flag |= BOARD_MEMMAP;
4317
4318 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4319 printk(KERN_WARNING "megaraid: mem region busy!\n");
4320 goto out_disable_device;
4321 }
4322
4323 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4324 if (!mega_baseport) {
4325 printk(KERN_WARNING
4326 "megaraid: could not map hba memory\n");
4327 goto out_release_region;
4328 }
4329 } else {
4330 flag |= BOARD_IOMAP;
4331 mega_baseport += 0x10;
4332
4333 if (!request_region(mega_baseport, 16, "megaraid"))
4334 goto out_disable_device;
4335 }
4336
4337
4338 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4339 if (!host)
4340 goto out_iounmap;
4341
4342 adapter = (adapter_t *)host->hostdata;
4343 memset(adapter, 0, sizeof(adapter_t));
4344
4345 printk(KERN_NOTICE
4346 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4347 host->host_no, mega_baseport, irq);
4348
4349 adapter->base = mega_baseport;
4350 if (flag & BOARD_MEMMAP)
4351 adapter->mmio_base = (void __iomem *) mega_baseport;
4352
4353 INIT_LIST_HEAD(&adapter->free_list);
4354 INIT_LIST_HEAD(&adapter->pending_list);
4355 INIT_LIST_HEAD(&adapter->completed_list);
4356
4357 adapter->flag = flag;
4358 spin_lock_init(&adapter->lock);
4359
4360 host->cmd_per_lun = max_cmd_per_lun;
4361 host->max_sectors = max_sectors_per_io;
4362
4363 adapter->dev = pdev;
4364 adapter->host = host;
4365
4366 adapter->host->irq = irq;
4367
4368 if (flag & BOARD_MEMMAP)
4369 adapter->host->base = tbase;
4370 else {
4371 adapter->host->io_port = tbase;
4372 adapter->host->n_io_port = 16;
4373 }
4374
4375 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4376
4377
4378
4379
4380 adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4381 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4382 if (!adapter->mega_buffer) {
4383 printk(KERN_WARNING "megaraid: out of RAM.\n");
4384 goto out_host_put;
4385 }
4386
4387 adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4388 if (!adapter->scb_list) {
4389 printk(KERN_WARNING "megaraid: out of RAM.\n");
4390 goto out_free_cmd_buffer;
4391 }
4392
4393 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4394 megaraid_isr_memmapped : megaraid_isr_iomapped,
4395 IRQF_SHARED, "megaraid", adapter)) {
4396 printk(KERN_WARNING
4397 "megaraid: Couldn't register IRQ %d!\n", irq);
4398 goto out_free_scb_list;
4399 }
4400
4401 if (mega_setup_mailbox(adapter))
4402 goto out_free_irq;
4403
4404 if (mega_query_adapter(adapter))
4405 goto out_free_mbox;
4406
4407
4408
4409
4410 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4411
4412
4413
4414 if (!strcmp(adapter->fw_version, "3.00") ||
4415 !strcmp(adapter->fw_version, "3.01")) {
4416
4417 printk( KERN_WARNING
4418 "megaraid: Your card is a Dell PERC "
4419 "2/SC RAID controller with "
4420 "firmware\nmegaraid: 3.00 or 3.01. "
4421 "This driver is known to have "
4422 "corruption issues\nmegaraid: with "
4423 "those firmware versions on this "
4424 "specific card. In order\nmegaraid: "
4425 "to protect your data, please upgrade "
4426 "your firmware to version\nmegaraid: "
4427 "3.10 or later, available from the "
4428 "Dell Technical Support web\n"
4429 "megaraid: site at\nhttp://support."
4430 "dell.com/us/en/filelib/download/"
4431 "index.asp?fileid=2940\n"
4432 );
4433 }
4434 }
4435
4436
4437
4438
4439
4440
4441
4442 if ((subsysvid == PCI_VENDOR_ID_HP) &&
4443 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4444
4445
4446
4447 if (!strcmp(adapter->fw_version, "H01.07") ||
4448 !strcmp(adapter->fw_version, "H01.08") ||
4449 !strcmp(adapter->fw_version, "H01.09") ) {
4450 printk(KERN_WARNING
4451 "megaraid: Firmware H.01.07, "
4452 "H.01.08, and H.01.09 on 1M/2M "
4453 "controllers\n"
4454 "megaraid: do not support 64 bit "
4455 "addressing.\nmegaraid: DISABLING "
4456 "64 bit support.\n");
4457 adapter->flag &= ~BOARD_64BIT;
4458 }
4459 }
4460
4461 if (mega_is_bios_enabled(adapter))
4462 mega_hbas[hba_count].is_bios_enabled = 1;
4463 mega_hbas[hba_count].hostdata_addr = adapter;
4464
4465
4466
4467
4468
4469 mega_enum_raid_scsi(adapter);
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479 mega_get_boot_drv(adapter);
4480
4481 if (adapter->boot_pdrv_enabled) {
4482 j = adapter->product_info.nchannels;
4483 for( i = 0; i < j; i++ )
4484 adapter->logdrv_chan[i] = 0;
4485 for( i = j; i < NVIRT_CHAN + j; i++ )
4486 adapter->logdrv_chan[i] = 1;
4487 } else {
4488 for (i = 0; i < NVIRT_CHAN; i++)
4489 adapter->logdrv_chan[i] = 1;
4490 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4491 adapter->logdrv_chan[i] = 0;
4492 adapter->mega_ch_class <<= NVIRT_CHAN;
4493 }
4494
4495
4496
4497
4498
4499 adapter->read_ldidmap = 0;
4500
4501 adapter->support_random_del = mega_support_random_del(adapter);
4502
4503
4504 if (mega_init_scb(adapter))
4505 goto out_free_mbox;
4506
4507
4508
4509
4510 atomic_set(&adapter->pend_cmds, 0);
4511
4512
4513
4514
4515 atomic_set(&adapter->quiescent, 0);
4516
4517 hba_soft_state[hba_count] = adapter;
4518
4519
4520
4521
4522
4523
4524 i = hba_count;
4525
4526 mcontroller[i].base = mega_baseport;
4527 mcontroller[i].irq = irq;
4528 mcontroller[i].numldrv = adapter->numldrv;
4529 mcontroller[i].pcibus = pci_bus;
4530 mcontroller[i].pcidev = id->device;
4531 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4532 mcontroller[i].pciid = -1;
4533 mcontroller[i].pcivendor = id->vendor;
4534 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4535 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4536
4537
4538
4539 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4540 pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4541 adapter->has_64bit_addr = 1;
4542 } else {
4543 pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4544 adapter->has_64bit_addr = 0;
4545 }
4546
4547 mutex_init(&adapter->int_mtx);
4548 init_completion(&adapter->int_waitq);
4549
4550 adapter->this_id = DEFAULT_INITIATOR_ID;
4551 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4552
4553#if MEGA_HAVE_CLUSTERING
4554
4555
4556
4557
4558
4559
4560
4561 adapter->has_cluster = mega_support_cluster(adapter);
4562 if (adapter->has_cluster) {
4563 printk(KERN_NOTICE
4564 "megaraid: Cluster driver, initiator id:%d\n",
4565 adapter->this_id);
4566 }
4567#endif
4568
4569 pci_set_drvdata(pdev, host);
4570
4571 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4572
4573 error = scsi_add_host(host, &pdev->dev);
4574 if (error)
4575 goto out_free_mbox;
4576
4577 scsi_scan_host(host);
4578 hba_count++;
4579 return 0;
4580
4581 out_free_mbox:
4582 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4583 adapter->una_mbox64, adapter->una_mbox64_dma);
4584 out_free_irq:
4585 free_irq(adapter->host->irq, adapter);
4586 out_free_scb_list:
4587 kfree(adapter->scb_list);
4588 out_free_cmd_buffer:
4589 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4590 adapter->mega_buffer, adapter->buf_dma_handle);
4591 out_host_put:
4592 scsi_host_put(host);
4593 out_iounmap:
4594 if (flag & BOARD_MEMMAP)
4595 iounmap((void *)mega_baseport);
4596 out_release_region:
4597 if (flag & BOARD_MEMMAP)
4598 release_mem_region(tbase, 128);
4599 else
4600 release_region(mega_baseport, 16);
4601 out_disable_device:
4602 pci_disable_device(pdev);
4603 out:
4604 return error;
4605}
4606
4607static void
4608__megaraid_shutdown(adapter_t *adapter)
4609{
4610 u_char raw_mbox[sizeof(struct mbox_out)];
4611 mbox_t *mbox = (mbox_t *)raw_mbox;
4612 int i;
4613
4614
4615 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4616 raw_mbox[0] = FLUSH_ADAPTER;
4617
4618 free_irq(adapter->host->irq, adapter);
4619
4620
4621 issue_scb_block(adapter, raw_mbox);
4622
4623
4624 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4625 raw_mbox[0] = FLUSH_SYSTEM;
4626
4627
4628 issue_scb_block(adapter, raw_mbox);
4629
4630 if (atomic_read(&adapter->pend_cmds) > 0)
4631 printk(KERN_WARNING "megaraid: pending commands!!\n");
4632
4633
4634
4635
4636
4637 for (i = 0; i <= 10; i++)
4638 mdelay(1000);
4639}
4640
4641static void
4642megaraid_remove_one(struct pci_dev *pdev)
4643{
4644 struct Scsi_Host *host = pci_get_drvdata(pdev);
4645 adapter_t *adapter = (adapter_t *)host->hostdata;
4646
4647 scsi_remove_host(host);
4648
4649 __megaraid_shutdown(adapter);
4650
4651
4652 if (adapter->flag & BOARD_MEMMAP) {
4653 iounmap((void *)adapter->base);
4654 release_mem_region(adapter->host->base, 128);
4655 } else
4656 release_region(adapter->base, 16);
4657
4658 mega_free_sgl(adapter);
4659
4660#ifdef CONFIG_PROC_FS
4661 if (adapter->controller_proc_dir_entry) {
4662 remove_proc_entry("stat", adapter->controller_proc_dir_entry);
4663 remove_proc_entry("config",
4664 adapter->controller_proc_dir_entry);
4665 remove_proc_entry("mailbox",
4666 adapter->controller_proc_dir_entry);
4667#if MEGA_HAVE_ENH_PROC
4668 remove_proc_entry("rebuild-rate",
4669 adapter->controller_proc_dir_entry);
4670 remove_proc_entry("battery-status",
4671 adapter->controller_proc_dir_entry);
4672
4673 remove_proc_entry("diskdrives-ch0",
4674 adapter->controller_proc_dir_entry);
4675 remove_proc_entry("diskdrives-ch1",
4676 adapter->controller_proc_dir_entry);
4677 remove_proc_entry("diskdrives-ch2",
4678 adapter->controller_proc_dir_entry);
4679 remove_proc_entry("diskdrives-ch3",
4680 adapter->controller_proc_dir_entry);
4681
4682 remove_proc_entry("raiddrives-0-9",
4683 adapter->controller_proc_dir_entry);
4684 remove_proc_entry("raiddrives-10-19",
4685 adapter->controller_proc_dir_entry);
4686 remove_proc_entry("raiddrives-20-29",
4687 adapter->controller_proc_dir_entry);
4688 remove_proc_entry("raiddrives-30-39",
4689 adapter->controller_proc_dir_entry);
4690#endif
4691 {
4692 char buf[12] = { 0 };
4693 sprintf(buf, "hba%d", adapter->host->host_no);
4694 remove_proc_entry(buf, mega_proc_dir_entry);
4695 }
4696 }
4697#endif
4698
4699 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4700 adapter->mega_buffer, adapter->buf_dma_handle);
4701 kfree(adapter->scb_list);
4702 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4703 adapter->una_mbox64, adapter->una_mbox64_dma);
4704
4705 scsi_host_put(host);
4706 pci_disable_device(pdev);
4707
4708 hba_count--;
4709}
4710
4711static void
4712megaraid_shutdown(struct pci_dev *pdev)
4713{
4714 struct Scsi_Host *host = pci_get_drvdata(pdev);
4715 adapter_t *adapter = (adapter_t *)host->hostdata;
4716
4717 __megaraid_shutdown(adapter);
4718}
4719
4720static struct pci_device_id megaraid_pci_tbl[] = {
4721 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4722 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4723 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
4724 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4725 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
4726 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4727 {0,}
4728};
4729MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
4730
4731static struct pci_driver megaraid_pci_driver = {
4732 .name = "megaraid_legacy",
4733 .id_table = megaraid_pci_tbl,
4734 .probe = megaraid_probe_one,
4735 .remove = megaraid_remove_one,
4736 .shutdown = megaraid_shutdown,
4737};
4738
4739static int __init megaraid_init(void)
4740{
4741 int error;
4742
4743 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
4744 max_cmd_per_lun = MAX_CMD_PER_LUN;
4745 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
4746 max_mbox_busy_wait = MBOX_BUSY_WAIT;
4747
4748#ifdef CONFIG_PROC_FS
4749 mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
4750 if (!mega_proc_dir_entry) {
4751 printk(KERN_WARNING
4752 "megaraid: failed to create megaraid root\n");
4753 }
4754#endif
4755 error = pci_register_driver(&megaraid_pci_driver);
4756 if (error) {
4757#ifdef CONFIG_PROC_FS
4758 remove_proc_entry("megaraid", NULL);
4759#endif
4760 return error;
4761 }
4762
4763
4764
4765
4766
4767
4768
4769 major = register_chrdev(0, "megadev_legacy", &megadev_fops);
4770 if (!major) {
4771 printk(KERN_WARNING
4772 "megaraid: failed to register char device\n");
4773 }
4774
4775 return 0;
4776}
4777
4778static void __exit megaraid_exit(void)
4779{
4780
4781
4782
4783 unregister_chrdev(major, "megadev_legacy");
4784
4785 pci_unregister_driver(&megaraid_pci_driver);
4786
4787#ifdef CONFIG_PROC_FS
4788 remove_proc_entry("megaraid", NULL);
4789#endif
4790}
4791
4792module_init(megaraid_init);
4793module_exit(megaraid_exit);
4794
4795
4796