Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / scsi / megaraid.c
blob264ae2fe04c6659f58f513f03e845394c4e571ce
1 /*===================================================================
3 * Linux MegaRAID device driver
5 * Copyright 1999 American Megatrends Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Version : 1.07b
14 * Description: Linux device driver for AMI MegaRAID controller
16 * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 490
18 * History:
20 * Version 0.90:
21 * Original source contributed by Dell; integrated it into the kernel and
22 * cleaned up some things. Added support for 438/466 controllers.
24 * Version 0.91:
25 * Aligned mailbox area on 16-byte boundry.
26 * Added schedule() at the end to properly clean up.
27 * Made improvements for conformity to linux driver standards.
29 * Version 0.92:
30 * Added support for 2.1 kernels.
31 * Reads from pci_dev struct, so it's not dependent on pcibios.
32 * Added some missing virt_to_bus() translations.
33 * Added support for SMP.
34 * Changed global cli()'s to spinlocks for 2.1, and simulated
35 * spinlocks for 2.0.
36 * Removed setting of SA_INTERRUPT flag when requesting Irq.
38 * Version 0.92ac:
39 * Small changes to the comments/formatting. Plus a couple of
40 * added notes. Returned to the authors. No actual code changes
41 * save printk levels.
42 * 8 Oct 98 Alan Cox <alan.cox@linux.org>
44 * Merged with 2.1.131 source tree.
45 * 12 Dec 98 K. Baranowski <kgb@knm.org.pl>
47 * Version 0.93:
48 * Added support for vendor specific ioctl commands (0x80+xxh)
49 * Changed some fields in MEGARAID struct to better values.
50 * Added signature check for Rp controllers under 2.0 kernels
51 * Changed busy-wait loop to be time-based
52 * Fixed SMP race condition in isr
53 * Added kfree (sgList) on release
54 * Added #include linux/version.h to megaraid.h for hosts.h
55 * Changed max_id to represent max logical drives instead of targets.
57 * Version 0.94:
58 * Got rid of some excess locking/unlocking
59 * Fixed slight memory corruption problem while memcpy'ing into mailbox
60 * Changed logical drives to be reported as luns rather than targets
61 * Changed max_id to 16 since it is now max targets/chan again.
62 * Improved ioctl interface for upcoming megamgr
64 * Version 0.95:
65 * Fixed problem of queueing multiple commands to adapter;
66 * still has some strange problems on some setups, so still
67 * defaults to single. To enable parallel commands change
68 * #define MULTI_IO in megaraid.h
69 * Changed kmalloc allocation to be done in beginning.
70 * Got rid of C++ style comments
72 * Version 0.96:
73 * 762 fully supported.
75 * Version 0.97:
76 * Changed megaraid_command to use wait_queue.
77 * Fixed bug of undesirably detecting HP onboard controllers which
78 * are disabled.
80 * Version 1.00:
81 * Checks to see if an irq ocurred while in isr, and runs through
82 * routine again.
83 * Copies mailbox to temp area before processing in isr
84 * Added barrier() in busy wait to fix volatility bug
85 * Uses separate list for freed Scbs, keeps track of cmd state
86 * Put spinlocks around entire queue function for now...
87 * Full multi-io commands working stablely without previous problems
88 * Added skipXX LILO option for Madrona motherboard support
90 * Version 1.01:
91 * Fixed bug in mega_cmd_done() for megamgr control commands,
92 * the host_byte in the result code from the scsi request to
93 * scsi midlayer is set to DID_BAD_TARGET when adapter's
94 * returned codes are 0xF0 and 0xF4.
96 * Version 1.02:
97 * Fixed the tape drive bug by extending the adapter timeout value
98 * for passthrough command to 60 seconds in mega_build_cmd().
100 * Version 1.03:
101 * Fixed Madrona support.
102 * Changed the adapter timeout value from 60 sec in 1.02 to 10 min
103 * for bigger and slower tape drive.
104 * Added driver version printout at driver loadup time
106 * Version 1.04
107 * Added code for 40 ld FW support.
108 * Added new ioctl command 0x81 to support NEW_READ/WRITE_CONFIG with
109 * data area greater than 4 KB, which is the upper bound for data
110 * tranfer through scsi_ioctl interface.
111 * The addtional 32 bit field for 64bit address in the newly defined
112 * mailbox64 structure is set to 0 at this point.
114 * Version 1.05
115 * Changed the queing implementation for handling SCBs and completed
116 * commands.
117 * Added spinlocks in the interrupt service routine to enable the dirver
118 * function in the SMP environment.
119 * Fixed the problem of unnecessary aborts in the abort entry point, which
120 * also enables the driver to handle large amount of I/O requests for
121 * long duration of time.
123 * Version 1.07
124 * Removed the usage of uaccess.h file for kernel versions less than
125 * 2.0.36, as this file is not present in those versions.
127 * Version 1.07b
128 * The MegaRAID 466 cards with 3.00 firmware lockup and seem to very
129 * occasionally hang. We check such cards and report them. You can
130 * get firmware upgrades to flash the board to 3.10 for free.
132 * BUGS:
133 * Some older 2.1 kernels (eg. 2.1.90) have a bug in pci.c that
134 * fails to detect the controller as a pci device on the system.
136 * Timeout period for upper scsi layer, i.e. SD_TIMEOUT in
137 * /drivers/scsi/sd.c, is too short for this controller. SD_TIMEOUT
138 * value must be increased to (30 * HZ) otherwise false timeouts
139 * will occur in the upper layer.
141 *===================================================================*/
143 #define CRLFSTR "\n"
144 #define IOCTL_CMD_NEW 0x81
146 #define MEGARAID_VERSION "v107 (December 22, 1999)"
149 #include <linux/version.h>
151 #ifdef MODULE
152 #include <linux/modversions.h>
153 #include <linux/module.h>
155 char kernel_version[] = UTS_RELEASE;
157 MODULE_AUTHOR ("American Megatrends Inc.");
158 MODULE_DESCRIPTION ("AMI MegaRAID driver");
159 #endif
161 #include <linux/init.h>
162 #include <linux/types.h>
163 #include <linux/errno.h>
164 #include <linux/kernel.h>
165 #include <linux/ioport.h>
166 #include <linux/fcntl.h>
167 #include <linux/delay.h>
168 #include <linux/pci.h>
169 #include <linux/proc_fs.h>
170 #include <linux/blk.h>
171 #include <linux/wait.h>
172 #include <linux/tqueue.h>
173 #include <linux/interrupt.h>
175 #include <linux/stat.h>
176 #include <linux/spinlock.h>
178 #include <asm/io.h>
179 #include <asm/irq.h>
180 #if LINUX_VERSION_CODE > 0x020024
181 #include <asm/uaccess.h>
182 #endif
184 #include "sd.h"
185 #include "scsi.h"
186 #include "hosts.h"
188 #include "megaraid.h"
190 /*================================================================
192 * #Defines
194 *================================================================
197 #define MAX_SERBUF 160
198 #define COM_BASE 0x2f8
201 u32 RDINDOOR (mega_host_config * megaCfg)
203 return readl (megaCfg->base + 0x20);
206 void WRINDOOR (mega_host_config * megaCfg, u32 value)
208 writel (value, megaCfg->base + 0x20);
211 u32 RDOUTDOOR (mega_host_config * megaCfg)
213 return readl (megaCfg->base + 0x2C);
216 void WROUTDOOR (mega_host_config * megaCfg, u32 value)
218 writel (value, megaCfg->base + 0x2C);
221 /*================================================================
223 * Function prototypes
225 *================================================================
227 static int __init megaraid_setup(char *);
229 static int megaIssueCmd (mega_host_config * megaCfg,
230 u_char * mboxData,
231 mega_scb * scb,
232 int intr);
233 static int mega_build_sglist (mega_host_config * megaCfg, mega_scb * scb,
234 u32 * buffer, u32 * length);
236 static int mega_busyWaitMbox(mega_host_config *);
237 static void mega_runpendq (mega_host_config *);
238 static void mega_rundoneq (mega_host_config *);
239 static void mega_cmd_done (mega_host_config *, mega_scb *, int);
240 static mega_scb *mega_ioctl (mega_host_config * megaCfg, Scsi_Cmnd * SCpnt);
241 static inline void mega_freeSgList(mega_host_config *megaCfg);
242 static void mega_Convert8ldTo40ld( mega_RAIDINQ *inquiry,
243 mega_Enquiry3 *enquiry3,
244 megaRaidProductInfo *productInfo );
247 #include <linux/smp.h>
250 #define cpuid smp_processor_id()
251 #define DRIVER_LOCK_T
252 #define DRIVER_LOCK_INIT(p)
253 #define DRIVER_LOCK(p)
254 #define DRIVER_UNLOCK(p)
255 #define IO_LOCK_T unsigned long io_flags = 0;
256 #define IO_LOCK spin_lock_irqsave(&io_request_lock,io_flags);
257 #define IO_UNLOCK spin_unlock_irqrestore(&io_request_lock,io_flags);
259 /* set SERDEBUG to 1 to enable serial debugging */
260 #define SERDEBUG 0
261 #if SERDEBUG
262 static void ser_init (void);
263 static void ser_puts (char *str);
264 static void ser_putc (char c);
265 static int ser_printk (const char *fmt,...);
266 #endif
268 /*================================================================
270 * Global variables
272 *================================================================
275 /* Use "megaraid=skipXX" as LILO option to prohibit driver from scanning
276 XX scsi id on each channel. Used for Madrona motherboard, where SAF_TE
277 processor id cannot be scanned */
278 #ifdef MODULE
279 static char *megaraid = NULL;
280 MODULE_PARM(megaraid, "s");
281 #endif
282 static int skip_id;
284 static int numCtlrs = 0;
285 static mega_host_config *megaCtlrs[FC_MAX_CHANNELS] = {0};
287 #if DEBUG
288 static u32 maxCmdTime = 0;
289 #endif
291 static mega_scb *pLastScb = NULL;
294 #if SERDEBUG
295 static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
296 #endif
298 #if SERDEBUG
299 static char strbuf[MAX_SERBUF + 1];
301 static void ser_init ()
303 unsigned port = COM_BASE;
305 outb (0x80, port + 3);
306 outb (0, port + 1);
307 /* 9600 Baud, if 19200: outb(6,port) */
308 outb (12, port);
309 outb (3, port + 3);
310 outb (0, port + 1);
313 static void ser_puts (char *str)
315 char *ptr;
317 ser_init ();
318 for (ptr = str; *ptr; ++ptr)
319 ser_putc (*ptr);
322 static void ser_putc (char c)
324 unsigned port = COM_BASE;
326 while ((inb (port + 5) & 0x20) == 0);
327 outb (c, port);
328 if (c == 0x0a) {
329 while ((inb (port + 5) & 0x20) == 0);
330 outb (0x0d, port);
334 static int ser_printk (const char *fmt,...)
336 va_list args;
337 int i;
338 long flags;
340 spin_lock_irqsave(&serial_lock,flags);
341 va_start (args, fmt);
342 i = vsprintf (strbuf, fmt, args);
343 ser_puts (strbuf);
344 va_end (args);
345 spin_unlock_irqrestore(&serial_lock,flags);
347 return i;
350 #define TRACE(a) { ser_printk a;}
352 #else
353 #define TRACE(A)
354 #endif
356 static void callDone (Scsi_Cmnd * SCpnt)
358 if (SCpnt->result) {
359 TRACE (("*** %.08lx %.02x <%d.%d.%d> = %x\n", SCpnt->serial_number,
360 SCpnt->cmnd[0], SCpnt->channel, SCpnt->target, SCpnt->lun,
361 SCpnt->result));
363 SCpnt->scsi_done (SCpnt);
366 /*-------------------------------------------------------------------------
368 * Local functions
370 *-------------------------------------------------------------------------*/
372 /*=======================
373 * Free a SCB structure
374 *=======================
376 static void mega_freeSCB (mega_host_config *megaCfg, mega_scb * pScb)
379 mega_scb *pScbtmp;
381 if ((pScb == NULL) || (pScb->idx >= 0xFE)) {
382 return ;
385 /* Unlink from pending queue */
387 if(pScb == megaCfg->qPendingH) {
388 if(megaCfg->qPendingH == megaCfg->qPendingT )
389 megaCfg->qPendingH = megaCfg->qPendingT = NULL;
390 else {
391 megaCfg->qPendingH = megaCfg->qPendingH->next;
393 megaCfg->qPcnt--;
395 else {
396 for(pScbtmp=megaCfg->qPendingH; pScbtmp; pScbtmp=pScbtmp->next) {
397 if (pScbtmp->next == pScb) {
398 pScbtmp->next = pScb->next;
399 if(pScb == megaCfg->qPendingT) {
400 megaCfg->qPendingT = pScbtmp;
402 megaCfg->qPcnt--;
403 break;
408 /* Link back into free list */
409 pScb->state = SCB_FREE;
410 pScb->SCpnt = NULL;
412 if(megaCfg->qFreeH == (mega_scb *) NULL ) {
413 megaCfg->qFreeH = megaCfg->qFreeT = pScb;
415 else {
416 megaCfg->qFreeT->next = pScb;
417 megaCfg->qFreeT = pScb;
419 megaCfg->qFreeT->next = NULL;
420 megaCfg->qFcnt++;
424 /*===========================
425 * Allocate a SCB structure
426 *===========================
428 static mega_scb * mega_allocateSCB (mega_host_config * megaCfg, Scsi_Cmnd * SCpnt)
430 mega_scb *pScb;
432 /* Unlink command from Free List */
433 if ((pScb = megaCfg->qFreeH) != NULL) {
434 megaCfg->qFreeH = pScb->next;
435 megaCfg->qFcnt--;
437 pScb->isrcount = jiffies;
438 pScb->next = NULL;
439 pScb->state = SCB_ACTIVE;
440 pScb->SCpnt = SCpnt;
442 return pScb;
445 printk (KERN_WARNING "Megaraid: Could not allocate free SCB!!!\n");
447 return NULL;
450 /*================================================
451 * Initialize SCB structures
452 *================================================
454 static int mega_initSCB (mega_host_config * megaCfg)
456 int idx;
458 megaCfg->qFreeH = NULL;
459 megaCfg->qFcnt = 0;
460 #if DEBUG
461 if(megaCfg->max_cmds >= MAX_COMMANDS) {
462 printk("megaraid:ctlr max cmds = %x : MAX_CMDS = %x", megaCfg->max_cmds, MAX_COMMANDS);
464 #endif
466 for (idx = megaCfg->max_cmds-1; idx >= 0; idx--) {
467 megaCfg->scbList[idx].idx = idx;
468 megaCfg->scbList[idx].sgList = kmalloc(sizeof(mega_sglist) * MAX_SGLIST,
469 GFP_ATOMIC | GFP_DMA);
470 if (megaCfg->scbList[idx].sgList == NULL) {
471 printk(KERN_WARNING "Can't allocate sglist for id %d\n",idx);
472 mega_freeSgList(megaCfg);
473 return -1;
476 if (idx < MAX_COMMANDS) {
477 /* Link to free list */
478 mega_freeSCB(megaCfg, &megaCfg->scbList[idx]);
481 return 0;
484 /* Run through the list of completed requests */
485 static void mega_rundoneq (mega_host_config *megaCfg)
487 Scsi_Cmnd *SCpnt;
489 while ((SCpnt = megaCfg->qCompletedH) != NULL) {
490 megaCfg->qCompletedH = (Scsi_Cmnd *)SCpnt->host_scribble;
491 megaCfg->qCcnt--;
493 SCpnt->host_scribble = (unsigned char *) NULL ; // XC : sep 14
494 /* Callback */
495 callDone (SCpnt);
497 megaCfg->qCompletedH = megaCfg->qCompletedT = NULL;
501 * Runs through the list of pending requests
502 * Assumes that mega_lock spin_lock has been acquired.
504 static void mega_runpendq(mega_host_config *megaCfg)
506 mega_scb *pScb;
508 /* Issue any pending commands to the card */
509 for(pScb=megaCfg->qPendingH; pScb; pScb=pScb->next) {
510 if (pScb->state == SCB_ACTIVE) {
511 if(megaIssueCmd(megaCfg, pScb->mboxData, pScb, 1))
512 return;
517 /* Add command to the list of completed requests */
518 static void
519 mega_cmd_done (mega_host_config * megaCfg, mega_scb * pScb,
520 int status)
522 int islogical;
523 Scsi_Cmnd *SCpnt;
524 mega_passthru *pthru;
525 mega_mailbox *mbox;
527 if (pScb == NULL) {
528 TRACE(("NULL pScb in mega_cmd_done!"));
529 printk("NULL pScb in mega_cmd_done!");
532 SCpnt = pScb->SCpnt;
533 pthru = &pScb->pthru;
534 mbox = (mega_mailbox *) &pScb->mboxData;
536 if (SCpnt == NULL) {
537 TRACE(("NULL SCpnt in mega_cmd_done!"));
538 TRACE(("pScb->idx = ",pScb->idx));
539 TRACE(("pScb->state = ",pScb->state));
540 TRACE(("pScb->state = ",pScb->state));
541 printk("megaraid:Problem...!\n");
542 while(1);
545 islogical = (SCpnt->channel == megaCfg->host->max_channel);
547 if (SCpnt->cmnd[0] == INQUIRY &&
548 ((((u_char *) SCpnt->request_buffer)[0] & 0x1F) == TYPE_DISK) &&
549 !islogical) {
550 status = 0xF0;
553 /* clear result; otherwise, success returns corrupt value */
554 SCpnt->result = 0;
556 if ((SCpnt->cmnd[0] & 0x80) ) {/* i.e. ioctl cmd such as 0x80, 0x81 of megamgr*/
557 switch (status) {
558 case 0xF0:
559 case 0xF4:
560 SCpnt->result=(DID_BAD_TARGET<<16)|status;
561 break;
562 default:
563 SCpnt->result|=status;
564 }/*end of switch*/
566 else{
567 /* Convert MegaRAID status to Linux error code */
568 switch (status) {
569 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD*/
570 SCpnt->result |= (DID_OK << 16);
571 break;
572 case 0x02: /* ERROR_ABORTED, i.e. SCSI_STATUS_CHECK_CONDITION */
573 /*set sense_buffer and result fields*/
574 if( mbox->cmd==MEGA_MBOXCMD_PASSTHRU ){
575 memcpy( SCpnt->sense_buffer , pthru->reqsensearea, 14);
576 SCpnt->result = (DRIVER_SENSE<<24)|(DID_ERROR << 16)|status;
578 else{
579 SCpnt->sense_buffer[0]=0x70;
580 SCpnt->sense_buffer[2]=ABORTED_COMMAND;
581 SCpnt->result |= (CHECK_CONDITION << 1);
583 break;
584 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e. SCSI_STATUS_BUSY */
585 SCpnt->result |= (DID_BUS_BUSY << 16)|status;
586 break;
587 default:
588 SCpnt->result |= (DID_BAD_TARGET << 16)|status;
589 break;
592 if ( SCpnt->cmnd[0]!=IOCTL_CMD_NEW )
593 /* not IOCTL_CMD_NEW SCB, freeSCB()*/
594 /* For IOCTL_CMD_NEW SCB, delay freeSCB() in megaraid_queue()
595 * after copy data back to user space*/
596 mega_freeSCB(megaCfg, pScb);
598 /* Add Scsi_Command to end of completed queue */
599 if( megaCfg->qCompletedH == NULL ) {
600 megaCfg->qCompletedH = megaCfg->qCompletedT = SCpnt;
602 else {
603 megaCfg->qCompletedT->host_scribble = (unsigned char *) SCpnt;
604 megaCfg->qCompletedT = SCpnt;
606 megaCfg->qCompletedT->host_scribble = (unsigned char *) NULL;
607 megaCfg->qCcnt++;
610 /*-------------------------------------------------------------------
612 * Build a SCB from a Scsi_Cmnd
614 * Returns a SCB pointer, or NULL
615 * If NULL is returned, the scsi_done function MUST have been called
617 *-------------------------------------------------------------------*/
618 static mega_scb * mega_build_cmd (mega_host_config * megaCfg,
619 Scsi_Cmnd * SCpnt)
621 mega_scb *pScb;
622 mega_mailbox *mbox;
623 mega_passthru *pthru;
624 long seg;
625 char islogical;
626 char lun = SCpnt->lun;
628 if ((SCpnt->cmnd[0] == 0x80) || (SCpnt->cmnd[0] == IOCTL_CMD_NEW) ) /* ioctl */
629 return mega_ioctl (megaCfg, SCpnt);
631 islogical = (SCpnt->channel == megaCfg->host->max_channel);
633 if (!islogical && lun != 0) {
634 SCpnt->result = (DID_BAD_TARGET << 16);
635 callDone (SCpnt);
636 return NULL;
639 if (!islogical && SCpnt->target == skip_id) {
640 SCpnt->result = (DID_BAD_TARGET << 16);
641 callDone (SCpnt);
642 return NULL;
645 if ( islogical ) {
646 lun = (SCpnt->target * 8) + lun;
647 if ( lun > FC_MAX_LOGICAL_DRIVES ){
648 SCpnt->result = (DID_BAD_TARGET << 16);
649 callDone (SCpnt);
650 return NULL;
653 /*-----------------------------------------------------
655 * Logical drive commands
657 *-----------------------------------------------------*/
658 if (islogical) {
659 switch (SCpnt->cmnd[0]) {
660 case TEST_UNIT_READY:
661 memset (SCpnt->request_buffer, 0, SCpnt->request_bufflen);
662 SCpnt->result = (DID_OK << 16);
663 callDone (SCpnt);
664 return NULL;
666 case MODE_SENSE:
667 memset (SCpnt->request_buffer, 0, SCpnt->cmnd[4]);
668 SCpnt->result = (DID_OK << 16);
669 callDone (SCpnt);
670 return NULL;
672 case READ_CAPACITY:
673 case INQUIRY:
674 /* Allocate a SCB and initialize passthru */
675 if ((pScb = mega_allocateSCB (megaCfg, SCpnt)) == NULL) {
676 SCpnt->result = (DID_ERROR << 16);
677 callDone (SCpnt);
678 return NULL;
680 pthru = &pScb->pthru;
681 mbox = (mega_mailbox *) & pScb->mboxData;
683 memset (mbox, 0, sizeof (pScb->mboxData));
684 memset (pthru, 0, sizeof (mega_passthru));
685 pthru->timeout = 0;
686 pthru->ars = 1;
687 pthru->reqsenselen = 14;
688 pthru->islogical = 1;
689 pthru->logdrv = lun;
690 pthru->cdblen = SCpnt->cmd_len;
691 pthru->dataxferaddr = virt_to_bus (SCpnt->request_buffer);
692 pthru->dataxferlen = SCpnt->request_bufflen;
693 memcpy (pthru->cdb, SCpnt->cmnd, SCpnt->cmd_len);
695 /* Initialize mailbox area */
696 mbox->cmd = MEGA_MBOXCMD_PASSTHRU;
697 mbox->xferaddr = virt_to_bus (pthru);
699 return pScb;
701 case READ_6:
702 case WRITE_6:
703 case READ_10:
704 case WRITE_10:
705 /* Allocate a SCB and initialize mailbox */
706 if ((pScb = mega_allocateSCB (megaCfg, SCpnt)) == NULL) {
707 SCpnt->result = (DID_ERROR << 16);
708 callDone (SCpnt);
709 return NULL;
711 mbox = (mega_mailbox *) & pScb->mboxData;
713 memset (mbox, 0, sizeof (pScb->mboxData));
714 mbox->logdrv = lun;
715 mbox->cmd = (*SCpnt->cmnd == READ_6 || *SCpnt->cmnd == READ_10) ?
716 MEGA_MBOXCMD_LREAD : MEGA_MBOXCMD_LWRITE;
718 /* 6-byte */
719 if (*SCpnt->cmnd == READ_6 || *SCpnt->cmnd == WRITE_6) {
720 mbox->numsectors =
721 (u32) SCpnt->cmnd[4];
722 mbox->lba =
723 ((u32) SCpnt->cmnd[1] << 16) |
724 ((u32) SCpnt->cmnd[2] << 8) |
725 (u32) SCpnt->cmnd[3];
726 mbox->lba &= 0x1FFFFF;
729 /* 10-byte */
730 if (*SCpnt->cmnd == READ_10 || *SCpnt->cmnd == WRITE_10) {
731 mbox->numsectors =
732 (u32) SCpnt->cmnd[8] |
733 ((u32) SCpnt->cmnd[7] << 8);
734 mbox->lba =
735 ((u32) SCpnt->cmnd[2] << 24) |
736 ((u32) SCpnt->cmnd[3] << 16) |
737 ((u32) SCpnt->cmnd[4] << 8) |
738 (u32) SCpnt->cmnd[5];
741 /* Calculate Scatter-Gather info */
742 mbox->numsgelements = mega_build_sglist (megaCfg, pScb,
743 (u32 *) & mbox->xferaddr,
744 (u32 *) & seg);
746 return pScb;
748 default:
749 SCpnt->result = (DID_BAD_TARGET << 16);
750 callDone (SCpnt);
751 return NULL;
754 /*-----------------------------------------------------
756 * Passthru drive commands
758 *-----------------------------------------------------*/
759 else {
760 /* Allocate a SCB and initialize passthru */
761 if ((pScb = mega_allocateSCB (megaCfg, SCpnt)) == NULL) {
762 SCpnt->result = (DID_ERROR << 16);
763 callDone (SCpnt);
764 return NULL;
766 pthru = &pScb->pthru;
767 mbox = (mega_mailbox *) pScb->mboxData;
769 memset (mbox, 0, sizeof (pScb->mboxData));
770 memset (pthru, 0, sizeof (mega_passthru));
771 pthru->timeout = 2; /*set adapter timeout value to 10 min. for tape drive*/
772 /* 0=6sec/1=60sec/2=10min/3=3hrs */
773 pthru->ars = 1;
774 pthru->reqsenselen = 14;
775 pthru->islogical = 0;
776 pthru->channel = (megaCfg->flag & BOARD_40LD) ? 0 : SCpnt->channel;
777 pthru->target = (megaCfg->flag & BOARD_40LD) ? /*BOARD_40LD*/
778 (SCpnt->channel<<4)|SCpnt->target : SCpnt->target;
779 pthru->cdblen = SCpnt->cmd_len;
780 memcpy (pthru->cdb, SCpnt->cmnd, SCpnt->cmd_len);
782 pthru->numsgelements = mega_build_sglist (megaCfg, pScb,
783 (u32 *) & pthru->dataxferaddr,
784 (u32 *) & pthru->dataxferlen);
786 /* Initialize mailbox */
787 mbox->cmd = MEGA_MBOXCMD_PASSTHRU;
788 mbox->xferaddr = virt_to_bus (pthru);
790 return pScb;
792 return NULL;
795 /*--------------------------------------------------------------------
796 * build RAID commands for controller, passed down through ioctl()
797 *--------------------------------------------------------------------*/
798 static mega_scb * mega_ioctl (mega_host_config * megaCfg, Scsi_Cmnd * SCpnt)
800 mega_scb *pScb;
801 mega_ioctl_mbox *mbox;
802 mega_mailbox *mailbox;
803 mega_passthru *pthru;
804 u8 *mboxdata;
805 long seg;
806 unsigned char *data = (unsigned char *)SCpnt->request_buffer;
807 int i;
809 if ((pScb = mega_allocateSCB (megaCfg, SCpnt)) == NULL) {
810 SCpnt->result = (DID_ERROR << 16);
811 callDone (SCpnt);
812 return NULL;
815 mboxdata = (u8 *) & pScb->mboxData;
816 mbox = (mega_ioctl_mbox *) & pScb->mboxData;
817 mailbox = (mega_mailbox *) & pScb->mboxData;
818 memset (mailbox, 0, sizeof (pScb->mboxData));
820 if (data[0] == 0x03) { /* passthrough command */
821 unsigned char cdblen = data[2];
822 pthru = &pScb->pthru;
823 memset (pthru, 0, sizeof (mega_passthru));
824 pthru->islogical = (data[cdblen+3] & 0x80) ? 1:0;
825 pthru->timeout = data[cdblen+3] & 0x07;
826 pthru->reqsenselen = 14;
827 pthru->ars = (data[cdblen+3] & 0x08) ? 1:0;
828 pthru->logdrv = data[cdblen+4];
829 pthru->channel = data[cdblen+5];
830 pthru->target = data[cdblen+6];
831 pthru->cdblen = cdblen;
832 memcpy (pthru->cdb, &data[3], cdblen);
834 mailbox->cmd = MEGA_MBOXCMD_PASSTHRU;
835 mailbox->xferaddr = virt_to_bus (pthru);
837 pthru->numsgelements = mega_build_sglist (megaCfg, pScb,
838 (u32 *) & pthru->dataxferaddr,
839 (u32 *) & pthru->dataxferlen);
841 for (i=0;i<(SCpnt->request_bufflen-cdblen-7);i++) {
842 data[i] = data[i+cdblen+7];
845 return pScb;
847 /* else normal (nonpassthru) command */
849 #if LINUX_VERSION_CODE > 0x020024
851 * usage of the function copy from user is used in case of data more than
852 * 4KB. This is used only with adapters which supports more than 8 logical
853 * drives. This feature is disabled on kernels earlier or same as 2.0.36
854 * as the uaccess.h file is not available with those kernels.
857 if (SCpnt->cmnd[0] == IOCTL_CMD_NEW) {
858 /* use external data area for large xfers */
859 /* If cmnd[0] is set to IOCTL_CMD_NEW then *
860 * cmnd[4..7] = external user buffer *
861 * cmnd[8..11] = length of buffer *
862 * */
863 char *kern_area;
864 char *user_area = *((char **)&SCpnt->cmnd[4]);
865 u32 xfer_size = *((u32 *)&SCpnt->cmnd[8]);
866 if (verify_area(VERIFY_READ, user_area, xfer_size)) {
867 printk("megaraid: Got bad user address.\n");
868 SCpnt->result = (DID_ERROR << 16);
869 callDone (SCpnt);
870 return NULL;
872 kern_area = kmalloc(xfer_size, GFP_ATOMIC | GFP_DMA);
873 if (kern_area == NULL) {
874 printk("megaraid: Couldn't allocate kernel mem.\n");
875 SCpnt->result = (DID_ERROR << 16);
876 callDone (SCpnt);
877 return NULL;
879 copy_from_user(kern_area,user_area,xfer_size);
880 pScb->kern_area = kern_area;
882 #endif
884 mbox->cmd = data[0];
885 mbox->channel = data[1];
886 mbox->param = data[2];
887 mbox->pad[0] = data[3];
888 mbox->logdrv = data[4];
890 if(SCpnt->cmnd[0] == IOCTL_CMD_NEW) {
891 if(data[0]==DCMD_FC_CMD){ /*i.e. 0xA1, then override some mbox data */
892 *(mboxdata+0) = data[0]; /*mailbox byte 0: DCMD_FC_CMD*/
893 *(mboxdata+2) = data[2]; /*sub command*/
894 *(mboxdata+3) = 0; /*number of elements in SG list*/
895 mbox->xferaddr /*i.e. mboxdata byte 0x8 to 0xb*/
896 = virt_to_bus(pScb->kern_area);
898 else{
899 mbox->xferaddr = virt_to_bus(pScb->kern_area);
900 mbox->numsgelements = 0;
903 else {
905 mbox->numsgelements = mega_build_sglist (megaCfg, pScb,
906 (u32 *) & mbox->xferaddr,
907 (u32 *) & seg);
909 for (i=0;i<(SCpnt->request_bufflen-6);i++) {
910 data[i] = data[i+6];
914 return (pScb);
917 #if DEBUG
918 static void showMbox(mega_scb *pScb)
920 mega_mailbox *mbox;
922 if (pScb == NULL) return;
924 mbox = (mega_mailbox *)pScb->mboxData;
925 printk("%u cmd:%x id:%x #scts:%x lba:%x addr:%x logdrv:%x #sg:%x\n",
926 pScb->SCpnt->pid,
927 mbox->cmd, mbox->cmdid, mbox->numsectors,
928 mbox->lba, mbox->xferaddr, mbox->logdrv,
929 mbox->numsgelements);
931 #endif
933 #if DEBUG
934 static unsigned int cum_time = 0;
935 static unsigned int cum_time_cnt = 0;
936 #endif
938 /*--------------------------------------------------------------------
939 * Interrupt service routine
940 *--------------------------------------------------------------------*/
941 static void megaraid_isr (int irq, void *devp, struct pt_regs *regs)
943 #if LINUX_VERSION_CODE >= 0x20100
944 IO_LOCK_T
945 #endif
946 mega_host_config *megaCfg;
947 u_char byte, idx, sIdx, tmpBox[MAILBOX_SIZE];
948 u32 dword=0;
949 mega_mailbox *mbox;
950 mega_scb *pScb;
951 u_char qCnt, qStatus;
952 u_char completed[MAX_FIRMWARE_STATUS];
953 Scsi_Cmnd *SCpnt;
955 megaCfg = (mega_host_config *) devp;
956 mbox = (mega_mailbox *)tmpBox;
958 if (megaCfg->host->irq == irq) {
959 if (megaCfg->flag & IN_ISR) {
960 printk(KERN_ERR "ISR called reentrantly!!\n");
963 megaCfg->flag |= IN_ISR;
965 if (mega_busyWaitMbox(megaCfg)) {
966 printk(KERN_WARNING "Error: mailbox busy in isr!\n");
969 /* Check if a valid interrupt is pending */
970 if (megaCfg->flag & BOARD_QUARTZ) {
971 dword = RDOUTDOOR (megaCfg);
972 if (dword != 0x10001234) {
973 /* Spurious interrupt */
974 megaCfg->flag &= ~IN_ISR;
975 return;
978 else {
979 byte = READ_PORT (megaCfg->host->io_port, INTR_PORT);
980 if ((byte & VALID_INTR_BYTE) == 0) {
981 /* Spurious interrupt */
982 megaCfg->flag &= ~IN_ISR;
983 return;
985 WRITE_PORT (megaCfg->host->io_port, INTR_PORT, byte);
988 for(idx=0;idx<MAX_FIRMWARE_STATUS;idx++ ) completed[idx] = 0;
990 IO_LOCK;
992 qCnt = 0xff;
993 while ((qCnt = megaCfg->mbox->numstatus) == 0xFF)
996 qStatus = 0xff;
997 while ((qStatus = megaCfg->mbox->status) == 0xFF)
1000 /* Get list of completed requests */
1001 for (idx = 0; idx<qCnt; idx++) {
1002 while ((sIdx = megaCfg->mbox->completed[idx]) == 0xFF) {
1003 printk("p");
1005 completed[idx] = sIdx;
1006 sIdx = 0xFF;
1009 if (megaCfg->flag & BOARD_QUARTZ) {
1010 WROUTDOOR (megaCfg, dword);
1011 /* Acknowledge interrupt */
1012 WRINDOOR (megaCfg, virt_to_bus (megaCfg->mbox) | 0x2);
1013 while (RDINDOOR (megaCfg) & 0x02);
1015 else {
1016 CLEAR_INTR (megaCfg->host->io_port);
1019 #if DEBUG
1020 if(qCnt >= MAX_FIRMWARE_STATUS) {
1021 printk("megaraid_isr: cmplt=%d ", qCnt);
1023 #endif
1025 for (idx = 0; idx < qCnt; idx++) {
1026 sIdx = completed[idx];
1027 if ((sIdx > 0) && (sIdx <= MAX_COMMANDS)) {
1028 pScb = &megaCfg->scbList[sIdx - 1];
1030 /* ASSERT(pScb->state == SCB_ISSUED); */
1032 #if DEBUG
1033 if (((jiffies) - pScb->isrcount) > maxCmdTime) {
1034 maxCmdTime = (jiffies) - pScb->isrcount;
1035 printk("megaraid_isr : cmd time = %u\n", maxCmdTime);
1037 #endif
1039 * Assuming that the scsi command, for which an abort request was received
1040 * earlier has completed.
1042 if (pScb->state == SCB_ABORTED) {
1043 SCpnt = pScb->SCpnt;
1045 if (pScb->state == SCB_RESET) {
1046 SCpnt = pScb->SCpnt;
1047 mega_freeSCB (megaCfg, pScb);
1048 SCpnt->result = (DID_RESET << 16) ;
1049 if( megaCfg->qCompletedH == NULL ) {
1050 megaCfg->qCompletedH = megaCfg->qCompletedT = SCpnt;
1052 else {
1053 megaCfg->qCompletedT->host_scribble = (unsigned char *) SCpnt;
1054 megaCfg->qCompletedT = SCpnt;
1056 megaCfg->qCompletedT->host_scribble = (unsigned char *) NULL;
1057 megaCfg->qCcnt++;
1058 continue;
1061 if (*(pScb->SCpnt->cmnd)==IOCTL_CMD_NEW)
1062 { /* external user buffer */
1063 up(&pScb->sem);
1065 /* Mark command as completed */
1066 mega_cmd_done(megaCfg, pScb, qStatus);
1069 else {
1070 printk(KERN_ERR "megaraid: wrong cmd id completed from firmware:id=%x\n",sIdx);
1074 mega_rundoneq(megaCfg);
1076 megaCfg->flag &= ~IN_ISR;
1078 /* Loop through any pending requests */
1079 mega_runpendq(megaCfg);
1080 #if LINUX_VERSION_CODE >= 0x20100
1081 IO_UNLOCK;
1082 #endif
1087 /*==================================================*/
1088 /* Wait until the controller's mailbox is available */
1089 /*==================================================*/
1090 static int mega_busyWaitMbox (mega_host_config * megaCfg)
1092 mega_mailbox *mbox = (mega_mailbox *) megaCfg->mbox;
1093 long counter;
1095 for (counter = 0; counter < 10000; counter++) {
1096 if (!mbox->busy) {
1097 return 0;
1099 udelay (100);
1100 barrier();
1102 return -1; /* give up after 1 second */
1105 /*=====================================================
1106 * Post a command to the card
1108 * Arguments:
1109 * mega_host_config *megaCfg - Controller structure
1110 * u_char *mboxData - Mailbox area, 16 bytes
1111 * mega_scb *pScb - SCB posting (or NULL if N/A)
1112 * int intr - if 1, interrupt, 0 is blocking
1113 * Return Value: (added on 7/26 for 40ld/64bit)
1114 * -1: the command was not actually issued out
1115 * othercases:
1116 * intr==0, return ScsiStatus, i.e. mbox->status
1117 * intr==1, return 0
1118 *=====================================================
1120 static int megaIssueCmd (mega_host_config * megaCfg,
1121 u_char * mboxData,
1122 mega_scb * pScb,
1123 int intr)
1125 mega_mailbox *mbox = (mega_mailbox *) megaCfg->mbox;
1126 u_char byte;
1127 u32 cmdDone;
1128 u32 phys_mbox;
1129 u8 retval=-1;
1131 mboxData[0x1] = (pScb ? pScb->idx + 1: 0x0); /* Set cmdid */
1132 mboxData[0xF] = 1; /* Set busy */
1134 phys_mbox = virt_to_bus (megaCfg->mbox);
1136 #if DEBUG
1137 showMbox(pScb);
1138 #endif
1140 /* Wait until mailbox is free */
1141 if (mega_busyWaitMbox (megaCfg)) {
1142 printk("Blocked mailbox......!!\n");
1143 udelay(1000);
1145 #if DEBUG
1146 showMbox(pLastScb);
1147 #endif
1149 /* Abort command */
1150 if (pScb == NULL) {
1151 TRACE(("NULL pScb in megaIssue\n"));
1152 printk("NULL pScb in megaIssue\n");
1154 mega_cmd_done (megaCfg, pScb, 0x08);
1155 return -1;
1158 pLastScb = pScb;
1160 /* Copy mailbox data into host structure */
1161 megaCfg->mbox64->xferSegment = 0;
1162 memcpy (mbox, mboxData, 16);
1164 /* Kick IO */
1165 if (intr) {
1167 /* Issue interrupt (non-blocking) command */
1168 if (megaCfg->flag & BOARD_QUARTZ) {
1169 mbox->mraid_poll = 0;
1170 mbox->mraid_ack = 0;
1171 WRINDOOR (megaCfg, phys_mbox | 0x1);
1173 else {
1174 ENABLE_INTR (megaCfg->host->io_port);
1175 ISSUE_COMMAND (megaCfg->host->io_port);
1177 pScb->state = SCB_ISSUED;
1179 retval=0;
1181 else { /* Issue non-ISR (blocking) command */
1182 disable_irq(megaCfg->host->irq);
1183 if (megaCfg->flag & BOARD_QUARTZ) {
1184 mbox->mraid_poll = 0;
1185 mbox->mraid_ack = 0;
1186 WRINDOOR (megaCfg, phys_mbox | 0x1);
1188 while ((cmdDone = RDOUTDOOR (megaCfg)) != 0x10001234);
1189 WROUTDOOR (megaCfg, cmdDone);
1191 if (pScb) {
1192 mega_cmd_done (megaCfg, pScb, mbox->status);
1195 WRINDOOR (megaCfg, phys_mbox | 0x2);
1196 while (RDINDOOR (megaCfg) & 0x2);
1199 else {
1200 DISABLE_INTR (megaCfg->host->io_port);
1201 ISSUE_COMMAND (megaCfg->host->io_port);
1203 while (!((byte = READ_PORT (megaCfg->host->io_port, INTR_PORT)) & INTR_VALID));
1204 WRITE_PORT (megaCfg->host->io_port, INTR_PORT, byte);
1206 ENABLE_INTR (megaCfg->host->io_port);
1207 CLEAR_INTR (megaCfg->host->io_port);
1209 if (pScb) {
1210 mega_cmd_done (megaCfg, pScb, mbox->status);
1212 else {
1213 TRACE (("Error: NULL pScb!\n"));
1216 enable_irq(megaCfg->host->irq);
1217 retval=mbox->status;
1219 #if DEBUG
1220 while (mega_busyWaitMbox (megaCfg)) {
1221 printk("Blocked mailbox on exit......!\n");
1222 udelay(1000);
1224 #endif
1226 return retval;
1229 /*-------------------------------------------------------------------
1230 * Copies data to SGLIST
1231 *-------------------------------------------------------------------*/
1232 static int mega_build_sglist (mega_host_config * megaCfg, mega_scb * scb,
1233 u32 * buffer, u32 * length)
1235 struct scatterlist *sgList;
1236 int idx;
1238 /* Scatter-gather not used */
1239 if (scb->SCpnt->use_sg == 0) {
1240 *buffer = virt_to_bus (scb->SCpnt->request_buffer);
1241 *length = (u32) scb->SCpnt->request_bufflen;
1242 return 0;
1245 sgList = (struct scatterlist *) scb->SCpnt->request_buffer;
1246 if (scb->SCpnt->use_sg == 1) {
1247 *buffer = virt_to_bus (sgList[0].address);
1248 *length = (u32) sgList[0].length;
1249 return 0;
1252 /* Copy Scatter-Gather list info into controller structure */
1253 for (idx = 0; idx < scb->SCpnt->use_sg; idx++) {
1254 scb->sgList[idx].address = virt_to_bus (sgList[idx].address);
1255 scb->sgList[idx].length = (u32) sgList[idx].length;
1258 /* Reset pointer and length fields */
1259 *buffer = virt_to_bus (scb->sgList);
1260 *length = 0;
1262 /* Return count of SG requests */
1263 return scb->SCpnt->use_sg;
1266 /*--------------------------------------------------------------------
1267 * Initializes the adress of the controller's mailbox register
1268 * The mailbox register is used to issue commands to the card.
1269 * Format of the mailbox area:
1270 * 00 01 command
1271 * 01 01 command id
1272 * 02 02 # of sectors
1273 * 04 04 logical bus address
1274 * 08 04 physical buffer address
1275 * 0C 01 logical drive #
1276 * 0D 01 length of scatter/gather list
1277 * 0E 01 reserved
1278 * 0F 01 mailbox busy
1279 * 10 01 numstatus byte
1280 * 11 01 status byte
1281 *--------------------------------------------------------------------*/
1282 static int mega_register_mailbox (mega_host_config * megaCfg, u32 paddr)
1284 /* align on 16-byte boundry */
1285 megaCfg->mbox = &megaCfg->mailbox64.mailbox;
1286 megaCfg->mbox = (mega_mailbox *) ((((u32) megaCfg->mbox) + 16) & 0xfffffff0);
1287 megaCfg->mbox64 = (mega_mailbox64 *) (megaCfg->mbox - 4);
1288 paddr = (paddr + 4 + 16) & 0xfffffff0;
1290 /* Register mailbox area with the firmware */
1291 if (!(megaCfg->flag & BOARD_QUARTZ)) {
1292 WRITE_PORT (megaCfg->host->io_port, MBOX_PORT0, paddr & 0xFF);
1293 WRITE_PORT (megaCfg->host->io_port, MBOX_PORT1, (paddr >> 8) & 0xFF);
1294 WRITE_PORT (megaCfg->host->io_port, MBOX_PORT2, (paddr >> 16) & 0xFF);
1295 WRITE_PORT (megaCfg->host->io_port, MBOX_PORT3, (paddr >> 24) & 0xFF);
1296 WRITE_PORT (megaCfg->host->io_port, ENABLE_MBOX_REGION, ENABLE_MBOX_BYTE);
1298 CLEAR_INTR (megaCfg->host->io_port);
1299 ENABLE_INTR (megaCfg->host->io_port);
1301 return 0;
1305 /*---------------------------------------------------------------------------
1306 * mega_Convert8ldTo40ld() -- takes all info in AdapterInquiry structure and
1307 * puts it into ProductInfo and Enquiry3 structures for later use
1308 *---------------------------------------------------------------------------*/
1309 static void mega_Convert8ldTo40ld( mega_RAIDINQ *inquiry,
1310 mega_Enquiry3 *enquiry3,
1311 megaRaidProductInfo *productInfo )
1313 int i;
1315 productInfo->MaxConcCmds = inquiry->AdpInfo.MaxConcCmds;
1316 enquiry3->rbldRate = inquiry->AdpInfo.RbldRate;
1317 productInfo->SCSIChanPresent = inquiry->AdpInfo.ChanPresent;
1318 for (i=0;i<4;i++) {
1319 productInfo->FwVer[i] = inquiry->AdpInfo.FwVer[i];
1320 productInfo->BiosVer[i] = inquiry->AdpInfo.BiosVer[i];
1322 enquiry3->cacheFlushInterval = inquiry->AdpInfo.CacheFlushInterval;
1323 productInfo->DramSize = inquiry->AdpInfo.DramSize;
1325 enquiry3->numLDrv = inquiry->LogdrvInfo.NumLDrv;
1326 for (i=0;i<MAX_LOGICAL_DRIVES;i++) {
1327 enquiry3->lDrvSize[i] = inquiry->LogdrvInfo.LDrvSize[i];
1328 enquiry3->lDrvProp[i] = inquiry->LogdrvInfo.LDrvProp[i];
1329 enquiry3->lDrvState[i] = inquiry->LogdrvInfo.LDrvState[i];
1332 for (i=0;i<(MAX_PHYSICAL_DRIVES);i++) {
1333 enquiry3->pDrvState[i] = inquiry->PhysdrvInfo.PDrvState[i];
1338 /*-------------------------------------------------------------------
1339 * Issue an adapter info query to the controller
1340 *-------------------------------------------------------------------*/
1341 static int mega_i_query_adapter (mega_host_config * megaCfg)
1343 mega_Enquiry3 *enquiry3Pnt;
1344 mega_mailbox *mbox;
1345 u_char mboxData[16];
1346 u32 paddr;
1347 u8 retval;
1349 /* Initialize adapter inquiry mailbox*/
1350 paddr = virt_to_bus (megaCfg->mega_buffer);
1351 mbox = (mega_mailbox *) mboxData;
1353 memset ((void *) megaCfg->mega_buffer, 0, sizeof (megaCfg->mega_buffer));
1354 memset (mbox, 0, 16);
1357 * Try to issue Enquiry3 command
1358 * if not suceeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
1359 * update enquiry3 structure
1361 mbox->xferaddr = virt_to_bus ( (void*) megaCfg->mega_buffer);
1362 /* Initialize mailbox databuffer addr */
1363 enquiry3Pnt = (mega_Enquiry3 *) megaCfg->mega_buffer;
1364 /* point mega_Enguiry3 to the data buf */
1366 mboxData[0]=FC_NEW_CONFIG ; /* i.e. mbox->cmd=0xA1 */
1367 mboxData[2]=NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
1368 mboxData[3]=ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
1370 /* Issue a blocking command to the card */
1371 if ( (retval=megaIssueCmd(megaCfg, mboxData, NULL, 0)) != 0 )
1372 { /* the adapter does not support 40ld*/
1374 mega_RAIDINQ adapterInquiryData;
1375 mega_RAIDINQ *adapterInquiryPnt = &adapterInquiryData;
1377 mbox->xferaddr = virt_to_bus ( (void*) adapterInquiryPnt);
1379 mbox->cmd = MEGA_MBOXCMD_ADAPTERINQ; /*issue old 0x05 command to adapter*/
1380 /* Issue a blocking command to the card */;
1381 retval=megaIssueCmd (megaCfg, mboxData, NULL, 0);
1383 /*update Enquiry3 and ProductInfo structures with mega_RAIDINQ structure*/
1384 mega_Convert8ldTo40ld( adapterInquiryPnt,
1385 enquiry3Pnt,
1386 (megaRaidProductInfo * ) &megaCfg->productInfo );
1389 else{ /* adapter supports 40ld */
1390 megaCfg->flag |= BOARD_40LD;
1392 /*get productInfo, which is static information and will be unchanged*/
1393 mbox->xferaddr = virt_to_bus ( (void*) &megaCfg->productInfo );
1395 mboxData[0]=FC_NEW_CONFIG ; /* i.e. mbox->cmd=0xA1 */
1396 mboxData[2]=NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
1398 if( (retval=megaIssueCmd(megaCfg, mboxData, NULL, 0)) != 0 )
1399 printk("ami:Product_info (0x0E) cmd failed with error: %d\n", retval);
1403 megaCfg->host->max_channel = megaCfg->productInfo.SCSIChanPresent;
1404 megaCfg->host->max_id = 16; /* max targets per channel */
1405 /*(megaCfg->flag & BOARD_40LD)?FC_MAX_TARGETS_PER_CHANNEL:MAX_TARGET+1;*/
1406 megaCfg->host->max_lun = /* max lun */
1407 (megaCfg->flag & BOARD_40LD) ? FC_MAX_LOGICAL_DRIVES : MAX_LOGICAL_DRIVES;
1408 megaCfg->host->cmd_per_lun = MAX_CMD_PER_LUN;
1410 megaCfg->numldrv = enquiry3Pnt->numLDrv;
1411 megaCfg->max_cmds = megaCfg->productInfo.MaxConcCmds;
1412 if(megaCfg->max_cmds > MAX_COMMANDS) megaCfg->max_cmds = MAX_COMMANDS - 1;
1414 megaCfg->host->can_queue = megaCfg->max_cmds;
1416 if (megaCfg->host->can_queue >= MAX_COMMANDS) {
1417 megaCfg->host->can_queue = MAX_COMMANDS-1;
1420 #ifdef HP /* use HP firmware and bios version encoding */
1421 sprintf (megaCfg->fwVer, "%c%d%d.%d%d",
1422 megaCfg->productInfo.FwVer[2],
1423 megaCfg->productInfo.FwVer[1] >> 8,
1424 megaCfg->productInfo.FwVer[1] & 0x0f,
1425 megaCfg->productInfo.FwVer[2] >> 8,
1426 megaCfg->productInfo.FwVer[2] & 0x0f);
1427 sprintf (megaCfg->biosVer, "%c%d%d.%d%d",
1428 megaCfg->productInfo.BiosVer[2],
1429 megaCfg->productInfo.BiosVer[1] >> 8,
1430 megaCfg->productInfo.BiosVer[1] & 0x0f,
1431 megaCfg->productInfo.BiosVer[2] >> 8,
1432 megaCfg->productInfo.BiosVer[2] & 0x0f);
1433 #else
1434 memcpy (megaCfg->fwVer, (void *)megaCfg->productInfo.FwVer, 4);
1435 megaCfg->fwVer[4] = 0;
1437 memcpy (megaCfg->biosVer, (void *)megaCfg->productInfo.BiosVer, 4);
1438 megaCfg->biosVer[4] = 0;
1439 #endif
1441 printk ("megaraid: [%s:%s] detected %d logical drives" CRLFSTR,
1442 megaCfg->fwVer,
1443 megaCfg->biosVer,
1444 megaCfg->numldrv);
1446 return 0;
1449 /*-------------------------------------------------------------------------
1451 * Driver interface functions
1453 *-------------------------------------------------------------------------*/
1455 /*----------------------------------------------------------
1456 * Returns data to be displayed in /proc/scsi/megaraid/X
1457 *----------------------------------------------------------*/
1458 int megaraid_proc_info (char *buffer, char **start, off_t offset,
1459 int length, int host_no, int inout)
1461 *start = buffer;
1462 return 0;
1465 int mega_findCard (Scsi_Host_Template * pHostTmpl,
1466 u16 pciVendor, u16 pciDev,
1467 long flag)
1469 mega_host_config *megaCfg;
1470 struct Scsi_Host *host;
1471 u_char megaIrq;
1472 u32 megaBase;
1473 u16 numFound = 0;
1475 struct pci_dev *pdev = NULL;
1477 while ((pdev = pci_find_device (pciVendor, pciDev, pdev))) {
1478 if (pci_enable_device(pdev))
1479 continue;
1480 if ((flag & BOARD_QUARTZ) && (skip_id == -1)) {
1481 u16 magic;
1482 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
1483 if ((magic != AMI_SIGNATURE) && (magic != AMI_SIGNATURE_471))
1484 continue; /* not an AMI board */
1486 printk (KERN_INFO "megaraid: found 0x%4.04x:0x%4.04x: in %s\n",
1487 pciVendor,
1488 pciDev,
1489 pdev->slot_name);
1491 /* Read the base port and IRQ from PCI */
1492 megaBase = pci_resource_start (pdev, 0);
1493 megaIrq = pdev->irq;
1495 if (flag & BOARD_QUARTZ)
1496 megaBase = (long) ioremap (megaBase, 128);
1497 else
1498 megaBase += 0x10;
1500 /* Initialize SCSI Host structure */
1501 host = scsi_register (pHostTmpl, sizeof (mega_host_config));
1502 if(host == NULL)
1503 continue;
1504 megaCfg = (mega_host_config *) host->hostdata;
1505 memset (megaCfg, 0, sizeof (mega_host_config));
1507 printk ("scsi%d : Found a MegaRAID controller at 0x%x, IRQ: %d" CRLFSTR,
1508 host->host_no, (u_int) megaBase, megaIrq);
1510 /* Copy resource info into structure */
1511 megaCfg->qCompletedH = NULL;
1512 megaCfg->qCompletedT = NULL;
1513 megaCfg->qPendingH = NULL;
1514 megaCfg->qPendingT = NULL;
1515 megaCfg->qFreeH = NULL;
1516 megaCfg->qFreeT = NULL;
1517 megaCfg->qFcnt = 0;
1518 megaCfg->qPcnt = 0;
1519 megaCfg->qCcnt = 0;
1520 megaCfg->flag = flag;
1521 megaCfg->host = host;
1522 megaCfg->base = megaBase;
1523 megaCfg->host->irq = megaIrq;
1524 megaCfg->host->io_port = megaBase;
1525 megaCfg->host->n_io_port = 16;
1526 megaCfg->host->unique_id = (pdev->bus->number << 8) | pdev->devfn;
1527 megaCtlrs[numCtlrs++] = megaCfg;
1528 if (flag != BOARD_QUARTZ) {
1529 /* Request our IO Range */
1530 if (request_region (megaBase, 16, "megaraid")) {
1531 printk (KERN_WARNING "megaraid: Couldn't register I/O range!" CRLFSTR);
1532 scsi_unregister (host);
1533 continue;
1537 /* Request our IRQ */
1538 if (request_irq (megaIrq, megaraid_isr, SA_SHIRQ,
1539 "megaraid", megaCfg)) {
1540 printk (KERN_WARNING "megaraid: Couldn't register IRQ %d!" CRLFSTR,
1541 megaIrq);
1542 scsi_unregister (host);
1543 continue;
1546 mega_register_mailbox (megaCfg, virt_to_bus ((void *) &megaCfg->mailbox64));
1547 mega_i_query_adapter (megaCfg);
1549 if (flag == BOARD_QUARTZ) {
1550 /* Check to see if this is a Dell PERC RAID controller model 466 */
1551 u16 subsysid, subsysvid;
1552 #if LINUX_VERSION_CODE < 0x20100
1553 pcibios_read_config_word (pciBus, pciDevFun,
1554 PCI_SUBSYSTEM_VENDOR_ID,
1555 &subsysvid);
1556 pcibios_read_config_word (pciBus, pciDevFun,
1557 PCI_SUBSYSTEM_ID,
1558 &subsysid);
1559 #else
1560 pci_read_config_word (pdev, PCI_SUBSYSTEM_VENDOR_ID, &subsysvid);
1561 pci_read_config_word (pdev, PCI_SUBSYSTEM_ID, &subsysid);
1562 #endif
1563 if ( (subsysid == 0x1111) && (subsysvid == 0x1111) &&
1564 (!strcmp(megaCfg->fwVer,"3.00") || !strcmp(megaCfg->fwVer,"3.01"))) {
1565 printk(KERN_WARNING
1566 "megaraid: Your card is a Dell PERC 2/SC RAID controller with firmware\n"
1567 "megaraid: 3.00 or 3.01. This driver is known to have corruption issues\n"
1568 "megaraid: with those firmware versions on this specific card. In order\n"
1569 "megaraid: to protect your data, please upgrade your firmware to version\n"
1570 "megaraid: 3.10 or later, available from the Dell Technical Support web\n"
1571 "megaraid: site at\n"
1572 "http://support.dell.com/us/en/filelib/download/index.asp?fileid=2940\n");
1573 megaraid_release (host);
1574 #ifdef MODULE
1575 continue;
1576 #else
1577 while(1) schedule_timeout(1 * HZ);
1578 #endif
1582 /* Initialize SCBs */
1583 if (mega_initSCB (megaCfg)) {
1584 megaraid_release (host);
1585 continue;
1588 numFound++;
1590 return numFound;
1593 /*---------------------------------------------------------
1594 * Detects if a megaraid controller exists in this system
1595 *---------------------------------------------------------*/
1596 int megaraid_detect (Scsi_Host_Template * pHostTmpl)
1598 int count = 0;
1600 #ifdef MODULE
1601 if (megaraid)
1602 megaraid_setup(megaraid);
1603 #endif
1605 pHostTmpl->proc_name = "megaraid";
1607 printk ("megaraid: " MEGARAID_VERSION CRLFSTR);
1609 count += mega_findCard (pHostTmpl, 0x101E, 0x9010, 0);
1610 count += mega_findCard (pHostTmpl, 0x101E, 0x9060, 0);
1611 count += mega_findCard (pHostTmpl, 0x8086, 0x1960, BOARD_QUARTZ);
1613 return count;
1616 /*---------------------------------------------------------------------
1617 * Release the controller's resources
1618 *---------------------------------------------------------------------*/
1619 int megaraid_release (struct Scsi_Host *pSHost)
1621 mega_host_config *megaCfg;
1622 mega_mailbox *mbox;
1623 u_char mboxData[16];
1625 megaCfg = (mega_host_config *) pSHost->hostdata;
1626 mbox = (mega_mailbox *) mboxData;
1628 /* Flush cache to disk */
1629 memset (mbox, 0, 16);
1630 mboxData[0] = 0xA;
1632 free_irq (megaCfg->host->irq, megaCfg);/* Must be freed first, otherwise
1633 extra interrupt is generated */
1635 /* Issue a blocking (interrupts disabled) command to the card */
1636 megaIssueCmd (megaCfg, mboxData, NULL, 0);
1638 /* Free our resources */
1639 if (megaCfg->flag & BOARD_QUARTZ) {
1640 iounmap ((void *) megaCfg->base);
1642 else {
1643 release_region (megaCfg->host->io_port, 16);
1646 mega_freeSgList(megaCfg);
1647 scsi_unregister (pSHost);
1649 return 0;
1652 static inline void mega_freeSgList(mega_host_config *megaCfg)
1654 int i;
1656 for (i = 0; i < megaCfg->max_cmds; i++) {
1657 if (megaCfg->scbList[i].sgList)
1658 kfree (megaCfg->scbList[i].sgList); /* free sgList */
1662 /*----------------------------------------------
1663 * Get information about the card/driver
1664 *----------------------------------------------*/
1665 const char * megaraid_info (struct Scsi_Host *pSHost)
1667 static char buffer[512];
1668 mega_host_config *megaCfg;
1670 megaCfg = (mega_host_config *) pSHost->hostdata;
1672 sprintf (buffer, "AMI MegaRAID %s %d commands %d targs %d chans %d luns",
1673 megaCfg->fwVer,
1674 megaCfg->productInfo.MaxConcCmds,
1675 megaCfg->host->max_id,
1676 megaCfg->host->max_channel,
1677 megaCfg->host->max_lun);
1678 return buffer;
1681 /*-----------------------------------------------------------------
1682 * Perform a SCSI command
1683 * Mailbox area:
1684 * 00 01 command
1685 * 01 01 command id
1686 * 02 02 # of sectors
1687 * 04 04 logical bus address
1688 * 08 04 physical buffer address
1689 * 0C 01 logical drive #
1690 * 0D 01 length of scatter/gather list
1691 * 0E 01 reserved
1692 * 0F 01 mailbox busy
1693 * 10 01 numstatus byte
1694 * 11 01 status byte
1695 *-----------------------------------------------------------------*/
1696 int megaraid_queue (Scsi_Cmnd * SCpnt, void (*pktComp) (Scsi_Cmnd *))
1698 DRIVER_LOCK_T
1699 mega_host_config *megaCfg;
1700 mega_scb *pScb;
1702 megaCfg = (mega_host_config *) SCpnt->host->hostdata;
1703 DRIVER_LOCK(megaCfg);
1705 if (!(megaCfg->flag & (1L << SCpnt->channel))) {
1706 if (SCpnt->channel < SCpnt->host->max_channel)
1707 printk (/*KERN_INFO*/ "scsi%d: scanning channel %c for devices.\n",
1708 megaCfg->host->host_no,
1709 SCpnt->channel + '1');
1710 else
1711 printk(/*KERN_INFO*/ "scsi%d: scanning virtual channel for logical drives.\n", megaCfg->host->host_no);
1713 megaCfg->flag |= (1L << SCpnt->channel);
1716 SCpnt->scsi_done = pktComp;
1718 /* If driver in abort or reset.. cancel this command */
1719 if (megaCfg->flag & IN_ABORT) {
1720 SCpnt->result = (DID_ABORT << 16);
1721 /* Add Scsi_Command to end of completed queue */
1722 if( megaCfg->qCompletedH == NULL ) {
1723 megaCfg->qCompletedH = megaCfg->qCompletedT = SCpnt;
1725 else {
1726 megaCfg->qCompletedT->host_scribble = (unsigned char *) SCpnt;
1727 megaCfg->qCompletedT = SCpnt;
1729 megaCfg->qCompletedT->host_scribble = (unsigned char *) NULL;
1730 megaCfg->qCcnt++;
1732 DRIVER_UNLOCK(megaCfg);
1733 return 0;
1735 else if (megaCfg->flag & IN_RESET) {
1736 SCpnt->result = (DID_RESET << 16);
1737 /* Add Scsi_Command to end of completed queue */
1738 if( megaCfg->qCompletedH == NULL ) {
1739 megaCfg->qCompletedH = megaCfg->qCompletedT = SCpnt;
1741 else {
1742 megaCfg->qCompletedT->host_scribble = (unsigned char *) SCpnt;
1743 megaCfg->qCompletedT = SCpnt;
1745 megaCfg->qCompletedT->host_scribble = (unsigned char *) NULL;
1746 megaCfg->qCcnt++;
1748 DRIVER_UNLOCK(megaCfg);
1749 return 0;
1752 megaCfg->flag |= IN_QUEUE;
1753 /* Allocate and build a SCB request */
1754 if ((pScb = mega_build_cmd (megaCfg, SCpnt)) != NULL) {
1755 /*build SCpnt for IOCTL_CMD_NEW cmd in mega_ioctl()*/
1756 /* Add SCB to the head of the pending queue */
1757 /* Add SCB to the head of the pending queue */
1758 if( megaCfg->qPendingH == NULL ) {
1759 megaCfg->qPendingH = megaCfg->qPendingT = pScb;
1761 else {
1762 megaCfg->qPendingT->next = pScb;
1763 megaCfg->qPendingT = pScb;
1765 megaCfg->qPendingT->next = NULL;
1766 megaCfg->qPcnt++;
1768 mega_runpendq(megaCfg);
1770 #if LINUX_VERSION_CODE > 0x020024
1771 if ( SCpnt->cmnd[0]==IOCTL_CMD_NEW )
1772 { /* user data from external user buffer */
1773 char *user_area;
1774 u32 xfer_size;
1776 init_MUTEX_LOCKED(&pScb->sem);
1777 down(&pScb->sem);
1779 user_area = *((char **)&pScb->SCpnt->cmnd[4]);
1780 xfer_size = *((u32 *)&pScb->SCpnt->cmnd[8]);
1782 copy_to_user(user_area,pScb->kern_area,xfer_size);
1784 kfree(pScb->kern_area);
1786 mega_freeSCB(megaCfg, pScb);
1788 #endif
1791 megaCfg->flag &= ~IN_QUEUE;
1792 DRIVER_UNLOCK(megaCfg);
1794 return 0;
1797 /*----------------------------------------------------------------------
1798 * Issue a blocking command to the controller
1799 *----------------------------------------------------------------------*/
1800 volatile static int internal_done_flag = 0;
1801 volatile static int internal_done_errcode = 0;
1802 static DECLARE_WAIT_QUEUE_HEAD(internal_wait);
1804 static void internal_done (Scsi_Cmnd * SCpnt)
1806 internal_done_errcode = SCpnt->result;
1807 internal_done_flag++;
1808 wake_up(&internal_wait);
1811 /* shouldn't be used, but included for completeness */
1813 int megaraid_command (Scsi_Cmnd * SCpnt)
1815 internal_done_flag = 0;
1817 /* Queue command, and wait until it has completed */
1818 megaraid_queue (SCpnt, internal_done);
1820 while (!internal_done_flag) {
1821 interruptible_sleep_on(&internal_wait);
1824 return internal_done_errcode;
1827 /*---------------------------------------------------------------------
1828 * Abort a previous SCSI request
1829 *---------------------------------------------------------------------*/
1831 megaraid_abort (Scsi_Cmnd * SCpnt)
1833 mega_host_config *megaCfg;
1834 int rc; //, idx;
1835 mega_scb *pScb;
1837 rc = SCSI_ABORT_NOT_RUNNING;
1839 megaCfg = (mega_host_config *) SCpnt->host->hostdata;
1841 megaCfg->flag |= IN_ABORT;
1843 for(pScb=megaCfg->qPendingH; pScb; pScb=pScb->next) {
1844 if (pScb->SCpnt == SCpnt) {
1845 /* Found an aborting command */
1846 #if DEBUG
1847 showMbox(pScb);
1848 #endif
1851 * If the command is queued to be issued to the firmware, abort the scsi cmd,
1852 * If the command is already aborted in a previous call to the _abort entry
1853 * point, return SCSI_ABORT_SNOOZE, suggesting a reset.
1854 * If the command is issued to the firmware, which might complete after
1855 * some time, we will mark the scb as aborted, and return to the mid layer,
1856 * that abort could not be done.
1857 * In the ISR, when this command actually completes, we will perform a normal
1858 * completion.
1860 * Oct 27, 1999
1863 switch(pScb->state) {
1864 case SCB_ABORTED: /* Already aborted */
1865 rc = SCSI_ABORT_SNOOZE;
1866 break;
1867 case SCB_ISSUED: /* Waiting on ISR result */
1868 rc = SCSI_ABORT_NOT_RUNNING;
1869 pScb->state = SCB_ABORTED;
1870 break;
1871 case SCB_ACTIVE: /* still on the pending queue */
1872 mega_freeSCB (megaCfg, pScb);
1873 SCpnt->result = (DID_ABORT << 16) ;
1874 if( megaCfg->qCompletedH == NULL ) {
1875 megaCfg->qCompletedH = megaCfg->qCompletedT = SCpnt;
1877 else {
1878 megaCfg->qCompletedT->host_scribble = (unsigned char *) SCpnt;
1879 megaCfg->qCompletedT = SCpnt;
1881 megaCfg->qCompletedT->host_scribble = (unsigned char *) NULL;
1882 megaCfg->qCcnt++;
1883 rc = SCSI_ABORT_SUCCESS;
1884 break;
1885 default:
1886 printk("megaraid_abort: unknown command state!!\n");
1887 rc = SCSI_ABORT_NOT_RUNNING;
1888 break;
1890 break;
1894 megaCfg->flag &= ~IN_ABORT;
1896 #if DEBUG
1897 if(megaCfg->flag & IN_QUEUE) printk("ma:flag is in queue\n");
1898 if(megaCfg->qCompletedH == NULL) printk("ma:qchead == null\n");
1899 #endif
1902 * This is required here to complete any completed requests to be communicated
1903 * over to the mid layer.
1904 * Calling just mega_rundoneq() did not work.
1906 if(megaCfg->qCompletedH) {
1907 SCpnt = megaCfg->qCompletedH;
1908 megaCfg->qCompletedH = (Scsi_Cmnd *)SCpnt->host_scribble;
1909 megaCfg->qCcnt--;
1911 SCpnt->host_scribble = (unsigned char *) NULL ;
1912 /* Callback */
1913 callDone (SCpnt);
1915 mega_rundoneq(megaCfg);
1917 return rc;
1920 /*---------------------------------------------------------------------
1921 * Reset a previous SCSI request
1922 *---------------------------------------------------------------------*/
1923 int megaraid_reset (Scsi_Cmnd * SCpnt, unsigned int rstflags)
1925 mega_host_config *megaCfg;
1926 int idx;
1927 int rc;
1928 mega_scb *pScb;
1930 rc = SCSI_RESET_NOT_RUNNING;
1931 megaCfg = (mega_host_config *) SCpnt->host->hostdata;
1933 megaCfg->flag |= IN_RESET;
1935 printk ("megaraid_RESET: %.08lx cmd=%.02x <c=%d.t=%d.l=%d>, flag = %x\n",
1936 SCpnt->serial_number, SCpnt->cmnd[0], SCpnt->channel, SCpnt->target,
1937 SCpnt->lun, rstflags);
1939 TRACE (("RESET: %.08lx %.02x <%d.%d.%d>\n",
1940 SCpnt->serial_number, SCpnt->cmnd[0], SCpnt->channel, SCpnt->target,
1941 SCpnt->lun));
1944 * Walk list of SCBs for any that are still outstanding
1946 for (idx = 0; idx < megaCfg->max_cmds; idx++) {
1947 if (megaCfg->scbList[idx].state != SCB_FREE) {
1948 SCpnt = megaCfg->scbList[idx].SCpnt;
1949 pScb = &megaCfg->scbList[idx];
1950 if (SCpnt != NULL) {
1951 pScb->state = SCB_RESET;
1952 break;
1957 megaCfg->flag &= ~IN_RESET;
1959 mega_rundoneq(megaCfg);
1960 return rc;
1963 /*-------------------------------------------------------------
1964 * Return the disk geometry for a particular disk
1965 * Input:
1966 * Disk *disk - Disk geometry
1967 * kdev_t dev - Device node
1968 * int *geom - Returns geometry fields
1969 * geom[0] = heads
1970 * geom[1] = sectors
1971 * geom[2] = cylinders
1972 *-------------------------------------------------------------*/
1973 int megaraid_biosparam (Disk * disk, kdev_t dev, int *geom)
1975 int heads, sectors, cylinders;
1976 mega_host_config *megaCfg;
1978 /* Get pointer to host config structure */
1979 megaCfg = (mega_host_config *) disk->device->host->hostdata;
1981 /* Default heads (64) & sectors (32) */
1982 heads = 64;
1983 sectors = 32;
1984 cylinders = disk->capacity / (heads * sectors);
1986 /* Handle extended translation size for logical drives > 1Gb */
1987 if (disk->capacity >= 0x200000) {
1988 heads = 255;
1989 sectors = 63;
1990 cylinders = disk->capacity / (heads * sectors);
1993 /* return result */
1994 geom[0] = heads;
1995 geom[1] = sectors;
1996 geom[2] = cylinders;
1998 return 0;
2001 static int __init megaraid_setup(char *str)
2003 skip_id = -1;
2004 if (str && !strncmp(str, "skip", strlen("skip"))) {
2005 if (str[4] != '\0') {
2006 skip_id = str[4] - '0';
2007 if (str[5] != '\0') {
2008 skip_id = (skip_id * 10) + (str[5] - '0');
2011 skip_id = (skip_id > 15) ? -1 : skip_id;
2013 return 1;
2016 __setup("megaraid=", megaraid_setup);
2018 static Scsi_Host_Template driver_template = MEGARAID;
2020 #include "scsi_module.c"