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.
14 * Description: Linux device driver for AMI MegaRAID controller
16 * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 490
21 * Original source contributed by Dell; integrated it into the kernel and
22 * cleaned up some things. Added support for 438/466 controllers.
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.
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
36 * Removed setting of SA_INTERRUPT flag when requesting Irq.
39 * Small changes to the comments/formatting. Plus a couple of
40 * added notes. Returned to the authors. No actual code changes
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>
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.
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
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
73 * 762 fully supported.
76 * Changed megaraid_command to use wait_queue.
77 * Fixed bug of undesirably detecting HP onboard controllers which
81 * Checks to see if an irq ocurred while in isr, and runs through
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
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.
97 * Fixed the tape drive bug by extending the adapter timeout value
98 * for passthrough command to 60 seconds in mega_build_cmd().
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
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.
115 * Changed the queing implementation for handling SCBs and completed
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.
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.
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.
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 *===================================================================*/
144 #define IOCTL_CMD_NEW 0x81
146 #define MEGARAID_VERSION "v107 (December 22, 1999)"
149 #include <linux/version.h>
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");
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>
180 #if LINUX_VERSION_CODE > 0x020024
181 #include <asm/uaccess.h>
188 #include "megaraid.h"
190 /*================================================================
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
,
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 */
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
,...);
268 /*================================================================
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 */
279 static char *megaraid
= NULL
;
280 MODULE_PARM(megaraid
, "s");
284 static int numCtlrs
= 0;
285 static mega_host_config
*megaCtlrs
[FC_MAX_CHANNELS
] = {0};
288 static u32 maxCmdTime
= 0;
291 static mega_scb
*pLastScb
= NULL
;
295 static spinlock_t serial_lock
= SPIN_LOCK_UNLOCKED
;
299 static char strbuf
[MAX_SERBUF
+ 1];
301 static void ser_init ()
303 unsigned port
= COM_BASE
;
305 outb (0x80, port
+ 3);
307 /* 9600 Baud, if 19200: outb(6,port) */
313 static void ser_puts (char *str
)
318 for (ptr
= str
; *ptr
; ++ptr
)
322 static void ser_putc (char c
)
324 unsigned port
= COM_BASE
;
326 while ((inb (port
+ 5) & 0x20) == 0);
329 while ((inb (port
+ 5) & 0x20) == 0);
334 static int ser_printk (const char *fmt
,...)
340 spin_lock_irqsave(&serial_lock
,flags
);
341 va_start (args
, fmt
);
342 i
= vsprintf (strbuf
, fmt
, args
);
345 spin_unlock_irqrestore(&serial_lock
,flags
);
350 #define TRACE(a) { ser_printk a;}
356 static void callDone (Scsi_Cmnd
* SCpnt
)
359 TRACE (("*** %.08lx %.02x <%d.%d.%d> = %x\n", SCpnt
->serial_number
,
360 SCpnt
->cmnd
[0], SCpnt
->channel
, SCpnt
->target
, SCpnt
->lun
,
363 SCpnt
->scsi_done (SCpnt
);
366 /*-------------------------------------------------------------------------
370 *-------------------------------------------------------------------------*/
372 /*=======================
373 * Free a SCB structure
374 *=======================
376 static void mega_freeSCB (mega_host_config
*megaCfg
, mega_scb
* pScb
)
381 if ((pScb
== NULL
) || (pScb
->idx
>= 0xFE)) {
385 /* Unlink from pending queue */
387 if(pScb
== megaCfg
->qPendingH
) {
388 if(megaCfg
->qPendingH
== megaCfg
->qPendingT
)
389 megaCfg
->qPendingH
= megaCfg
->qPendingT
= NULL
;
391 megaCfg
->qPendingH
= megaCfg
->qPendingH
->next
;
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
;
408 /* Link back into free list */
409 pScb
->state
= SCB_FREE
;
412 if(megaCfg
->qFreeH
== (mega_scb
*) NULL
) {
413 megaCfg
->qFreeH
= megaCfg
->qFreeT
= pScb
;
416 megaCfg
->qFreeT
->next
= pScb
;
417 megaCfg
->qFreeT
= pScb
;
419 megaCfg
->qFreeT
->next
= NULL
;
424 /*===========================
425 * Allocate a SCB structure
426 *===========================
428 static mega_scb
* mega_allocateSCB (mega_host_config
* megaCfg
, Scsi_Cmnd
* SCpnt
)
432 /* Unlink command from Free List */
433 if ((pScb
= megaCfg
->qFreeH
) != NULL
) {
434 megaCfg
->qFreeH
= pScb
->next
;
437 pScb
->isrcount
= jiffies
;
439 pScb
->state
= SCB_ACTIVE
;
445 printk (KERN_WARNING
"Megaraid: Could not allocate free SCB!!!\n");
450 /*================================================
451 * Initialize SCB structures
452 *================================================
454 static int mega_initSCB (mega_host_config
* megaCfg
)
458 megaCfg
->qFreeH
= NULL
;
461 if(megaCfg
->max_cmds
>= MAX_COMMANDS
) {
462 printk("megaraid:ctlr max cmds = %x : MAX_CMDS = %x", megaCfg
->max_cmds
, MAX_COMMANDS
);
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
);
476 if (idx
< MAX_COMMANDS
) {
477 /* Link to free list */
478 mega_freeSCB(megaCfg
, &megaCfg
->scbList
[idx
]);
484 /* Run through the list of completed requests */
485 static void mega_rundoneq (mega_host_config
*megaCfg
)
489 while ((SCpnt
= megaCfg
->qCompletedH
) != NULL
) {
490 megaCfg
->qCompletedH
= (Scsi_Cmnd
*)SCpnt
->host_scribble
;
493 SCpnt
->host_scribble
= (unsigned char *) NULL
; // XC : sep 14
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
)
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))
517 /* Add command to the list of completed requests */
519 mega_cmd_done (mega_host_config
* megaCfg
, mega_scb
* pScb
,
524 mega_passthru
*pthru
;
528 TRACE(("NULL pScb in mega_cmd_done!"));
529 printk("NULL pScb in mega_cmd_done!");
533 pthru
= &pScb
->pthru
;
534 mbox
= (mega_mailbox
*) &pScb
->mboxData
;
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");
545 islogical
= (SCpnt
->channel
== megaCfg
->host
->max_channel
);
547 if (SCpnt
->cmnd
[0] == INQUIRY
&&
548 ((((u_char
*) SCpnt
->request_buffer
)[0] & 0x1F) == TYPE_DISK
) &&
553 /* clear result; otherwise, success returns corrupt value */
556 if ((SCpnt
->cmnd
[0] & 0x80) ) {/* i.e. ioctl cmd such as 0x80, 0x81 of megamgr*/
560 SCpnt
->result
=(DID_BAD_TARGET
<<16)|status
;
563 SCpnt
->result
|=status
;
567 /* Convert MegaRAID status to Linux error code */
569 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD*/
570 SCpnt
->result
|= (DID_OK
<< 16);
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
;
579 SCpnt
->sense_buffer
[0]=0x70;
580 SCpnt
->sense_buffer
[2]=ABORTED_COMMAND
;
581 SCpnt
->result
|= (CHECK_CONDITION
<< 1);
584 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e. SCSI_STATUS_BUSY */
585 SCpnt
->result
|= (DID_BUS_BUSY
<< 16)|status
;
588 SCpnt
->result
|= (DID_BAD_TARGET
<< 16)|status
;
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
;
603 megaCfg
->qCompletedT
->host_scribble
= (unsigned char *) SCpnt
;
604 megaCfg
->qCompletedT
= SCpnt
;
606 megaCfg
->qCompletedT
->host_scribble
= (unsigned char *) NULL
;
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
,
623 mega_passthru
*pthru
;
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);
639 if (!islogical
&& SCpnt
->target
== skip_id
) {
640 SCpnt
->result
= (DID_BAD_TARGET
<< 16);
646 lun
= (SCpnt
->target
* 8) + lun
;
647 if ( lun
> FC_MAX_LOGICAL_DRIVES
){
648 SCpnt
->result
= (DID_BAD_TARGET
<< 16);
653 /*-----------------------------------------------------
655 * Logical drive commands
657 *-----------------------------------------------------*/
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);
667 memset (SCpnt
->request_buffer
, 0, SCpnt
->cmnd
[4]);
668 SCpnt
->result
= (DID_OK
<< 16);
674 /* Allocate a SCB and initialize passthru */
675 if ((pScb
= mega_allocateSCB (megaCfg
, SCpnt
)) == NULL
) {
676 SCpnt
->result
= (DID_ERROR
<< 16);
680 pthru
= &pScb
->pthru
;
681 mbox
= (mega_mailbox
*) & pScb
->mboxData
;
683 memset (mbox
, 0, sizeof (pScb
->mboxData
));
684 memset (pthru
, 0, sizeof (mega_passthru
));
687 pthru
->reqsenselen
= 14;
688 pthru
->islogical
= 1;
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
);
705 /* Allocate a SCB and initialize mailbox */
706 if ((pScb
= mega_allocateSCB (megaCfg
, SCpnt
)) == NULL
) {
707 SCpnt
->result
= (DID_ERROR
<< 16);
711 mbox
= (mega_mailbox
*) & pScb
->mboxData
;
713 memset (mbox
, 0, sizeof (pScb
->mboxData
));
715 mbox
->cmd
= (*SCpnt
->cmnd
== READ_6
|| *SCpnt
->cmnd
== READ_10
) ?
716 MEGA_MBOXCMD_LREAD
: MEGA_MBOXCMD_LWRITE
;
719 if (*SCpnt
->cmnd
== READ_6
|| *SCpnt
->cmnd
== WRITE_6
) {
721 (u32
) SCpnt
->cmnd
[4];
723 ((u32
) SCpnt
->cmnd
[1] << 16) |
724 ((u32
) SCpnt
->cmnd
[2] << 8) |
725 (u32
) SCpnt
->cmnd
[3];
726 mbox
->lba
&= 0x1FFFFF;
730 if (*SCpnt
->cmnd
== READ_10
|| *SCpnt
->cmnd
== WRITE_10
) {
732 (u32
) SCpnt
->cmnd
[8] |
733 ((u32
) SCpnt
->cmnd
[7] << 8);
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
,
749 SCpnt
->result
= (DID_BAD_TARGET
<< 16);
754 /*-----------------------------------------------------
756 * Passthru drive commands
758 *-----------------------------------------------------*/
760 /* Allocate a SCB and initialize passthru */
761 if ((pScb
= mega_allocateSCB (megaCfg
, SCpnt
)) == NULL
) {
762 SCpnt
->result
= (DID_ERROR
<< 16);
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 */
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
);
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
)
801 mega_ioctl_mbox
*mbox
;
802 mega_mailbox
*mailbox
;
803 mega_passthru
*pthru
;
806 unsigned char *data
= (unsigned char *)SCpnt
->request_buffer
;
809 if ((pScb
= mega_allocateSCB (megaCfg
, SCpnt
)) == NULL
) {
810 SCpnt
->result
= (DID_ERROR
<< 16);
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];
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 *
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);
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);
879 copy_from_user(kern_area
,user_area
,xfer_size
);
880 pScb
->kern_area
= kern_area
;
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
);
899 mbox
->xferaddr
= virt_to_bus(pScb
->kern_area
);
900 mbox
->numsgelements
= 0;
905 mbox
->numsgelements
= mega_build_sglist (megaCfg
, pScb
,
906 (u32
*) & mbox
->xferaddr
,
909 for (i
=0;i
<(SCpnt
->request_bufflen
-6);i
++) {
918 static void showMbox(mega_scb
*pScb
)
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",
927 mbox
->cmd
, mbox
->cmdid
, mbox
->numsectors
,
928 mbox
->lba
, mbox
->xferaddr
, mbox
->logdrv
,
929 mbox
->numsgelements
);
934 static unsigned int cum_time
= 0;
935 static unsigned int cum_time_cnt
= 0;
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
946 mega_host_config
*megaCfg
;
947 u_char byte
, idx
, sIdx
, tmpBox
[MAILBOX_SIZE
];
951 u_char qCnt
, qStatus
;
952 u_char completed
[MAX_FIRMWARE_STATUS
];
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
;
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
;
985 WRITE_PORT (megaCfg
->host
->io_port
, INTR_PORT
, byte
);
988 for(idx
=0;idx
<MAX_FIRMWARE_STATUS
;idx
++ ) completed
[idx
] = 0;
993 while ((qCnt
= megaCfg
->mbox
->numstatus
) == 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) {
1005 completed
[idx
] = sIdx
;
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);
1016 CLEAR_INTR (megaCfg
->host
->io_port
);
1020 if(qCnt
>= MAX_FIRMWARE_STATUS
) {
1021 printk("megaraid_isr: cmplt=%d ", qCnt
);
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); */
1033 if (((jiffies
) - pScb
->isrcount
) > maxCmdTime
) {
1034 maxCmdTime
= (jiffies
) - pScb
->isrcount
;
1035 printk("megaraid_isr : cmd time = %u\n", maxCmdTime
);
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
;
1053 megaCfg
->qCompletedT
->host_scribble
= (unsigned char *) SCpnt
;
1054 megaCfg
->qCompletedT
= SCpnt
;
1056 megaCfg
->qCompletedT
->host_scribble
= (unsigned char *) NULL
;
1061 if (*(pScb
->SCpnt
->cmnd
)==IOCTL_CMD_NEW
)
1062 { /* external user buffer */
1065 /* Mark command as completed */
1066 mega_cmd_done(megaCfg
, pScb
, qStatus
);
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
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
;
1095 for (counter
= 0; counter
< 10000; counter
++) {
1102 return -1; /* give up after 1 second */
1105 /*=====================================================
1106 * Post a command to the card
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
1116 * intr==0, return ScsiStatus, i.e. mbox->status
1118 *=====================================================
1120 static int megaIssueCmd (mega_host_config
* megaCfg
,
1125 mega_mailbox
*mbox
= (mega_mailbox
*) megaCfg
->mbox
;
1131 mboxData
[0x1] = (pScb
? pScb
->idx
+ 1: 0x0); /* Set cmdid */
1132 mboxData
[0xF] = 1; /* Set busy */
1134 phys_mbox
= virt_to_bus (megaCfg
->mbox
);
1140 /* Wait until mailbox is free */
1141 if (mega_busyWaitMbox (megaCfg
)) {
1142 printk("Blocked mailbox......!!\n");
1151 TRACE(("NULL pScb in megaIssue\n"));
1152 printk("NULL pScb in megaIssue\n");
1154 mega_cmd_done (megaCfg
, pScb
, 0x08);
1160 /* Copy mailbox data into host structure */
1161 megaCfg
->mbox64
->xferSegment
= 0;
1162 memcpy (mbox
, mboxData
, 16);
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);
1174 ENABLE_INTR (megaCfg
->host
->io_port
);
1175 ISSUE_COMMAND (megaCfg
->host
->io_port
);
1177 pScb
->state
= SCB_ISSUED
;
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
);
1192 mega_cmd_done (megaCfg
, pScb
, mbox
->status
);
1195 WRINDOOR (megaCfg
, phys_mbox
| 0x2);
1196 while (RDINDOOR (megaCfg
) & 0x2);
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
);
1210 mega_cmd_done (megaCfg
, pScb
, mbox
->status
);
1213 TRACE (("Error: NULL pScb!\n"));
1216 enable_irq(megaCfg
->host
->irq
);
1217 retval
=mbox
->status
;
1220 while (mega_busyWaitMbox (megaCfg
)) {
1221 printk("Blocked mailbox on exit......!\n");
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
;
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
;
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
;
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
);
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:
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
1278 * 0F 01 mailbox busy
1279 * 10 01 numstatus 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
);
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
)
1315 productInfo
->MaxConcCmds
= inquiry
->AdpInfo
.MaxConcCmds
;
1316 enquiry3
->rbldRate
= inquiry
->AdpInfo
.RbldRate
;
1317 productInfo
->SCSIChanPresent
= inquiry
->AdpInfo
.ChanPresent
;
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
;
1345 u_char mboxData
[16];
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
,
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);
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;
1441 printk ("megaraid: [%s:%s] detected %d logical drives" CRLFSTR
,
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
)
1465 int mega_findCard (Scsi_Host_Template
* pHostTmpl
,
1466 u16 pciVendor
, u16 pciDev
,
1469 mega_host_config
*megaCfg
;
1470 struct Scsi_Host
*host
;
1475 struct pci_dev
*pdev
= NULL
;
1477 while ((pdev
= pci_find_device (pciVendor
, pciDev
, pdev
))) {
1478 if (pci_enable_device(pdev
))
1480 if ((flag
& BOARD_QUARTZ
) && (skip_id
== -1)) {
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",
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);
1500 /* Initialize SCSI Host structure */
1501 host
= scsi_register (pHostTmpl
, sizeof (mega_host_config
));
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
;
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
);
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
,
1542 scsi_unregister (host
);
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
,
1556 pcibios_read_config_word (pciBus
, pciDevFun
,
1560 pci_read_config_word (pdev
, PCI_SUBSYSTEM_VENDOR_ID
, &subsysvid
);
1561 pci_read_config_word (pdev
, PCI_SUBSYSTEM_ID
, &subsysid
);
1563 if ( (subsysid
== 0x1111) && (subsysvid
== 0x1111) &&
1564 (!strcmp(megaCfg
->fwVer
,"3.00") || !strcmp(megaCfg
->fwVer
,"3.01"))) {
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
);
1577 while(1) schedule_timeout(1 * HZ
);
1582 /* Initialize SCBs */
1583 if (mega_initSCB (megaCfg
)) {
1584 megaraid_release (host
);
1593 /*---------------------------------------------------------
1594 * Detects if a megaraid controller exists in this system
1595 *---------------------------------------------------------*/
1596 int megaraid_detect (Scsi_Host_Template
* pHostTmpl
)
1602 megaraid_setup(megaraid
);
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
);
1616 /*---------------------------------------------------------------------
1617 * Release the controller's resources
1618 *---------------------------------------------------------------------*/
1619 int megaraid_release (struct Scsi_Host
*pSHost
)
1621 mega_host_config
*megaCfg
;
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);
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
);
1643 release_region (megaCfg
->host
->io_port
, 16);
1646 mega_freeSgList(megaCfg
);
1647 scsi_unregister (pSHost
);
1652 static inline void mega_freeSgList(mega_host_config
*megaCfg
)
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",
1674 megaCfg
->productInfo
.MaxConcCmds
,
1675 megaCfg
->host
->max_id
,
1676 megaCfg
->host
->max_channel
,
1677 megaCfg
->host
->max_lun
);
1681 /*-----------------------------------------------------------------
1682 * Perform a SCSI command
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
1692 * 0F 01 mailbox busy
1693 * 10 01 numstatus byte
1695 *-----------------------------------------------------------------*/
1696 int megaraid_queue (Scsi_Cmnd
* SCpnt
, void (*pktComp
) (Scsi_Cmnd
*))
1699 mega_host_config
*megaCfg
;
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');
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
;
1726 megaCfg
->qCompletedT
->host_scribble
= (unsigned char *) SCpnt
;
1727 megaCfg
->qCompletedT
= SCpnt
;
1729 megaCfg
->qCompletedT
->host_scribble
= (unsigned char *) NULL
;
1732 DRIVER_UNLOCK(megaCfg
);
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
;
1742 megaCfg
->qCompletedT
->host_scribble
= (unsigned char *) SCpnt
;
1743 megaCfg
->qCompletedT
= SCpnt
;
1745 megaCfg
->qCompletedT
->host_scribble
= (unsigned char *) NULL
;
1748 DRIVER_UNLOCK(megaCfg
);
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
;
1762 megaCfg
->qPendingT
->next
= pScb
;
1763 megaCfg
->qPendingT
= pScb
;
1765 megaCfg
->qPendingT
->next
= NULL
;
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 */
1776 init_MUTEX_LOCKED(&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
);
1791 megaCfg
->flag
&= ~IN_QUEUE
;
1792 DRIVER_UNLOCK(megaCfg
);
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
;
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 */
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
1863 switch(pScb
->state
) {
1864 case SCB_ABORTED
: /* Already aborted */
1865 rc
= SCSI_ABORT_SNOOZE
;
1867 case SCB_ISSUED
: /* Waiting on ISR result */
1868 rc
= SCSI_ABORT_NOT_RUNNING
;
1869 pScb
->state
= SCB_ABORTED
;
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
;
1878 megaCfg
->qCompletedT
->host_scribble
= (unsigned char *) SCpnt
;
1879 megaCfg
->qCompletedT
= SCpnt
;
1881 megaCfg
->qCompletedT
->host_scribble
= (unsigned char *) NULL
;
1883 rc
= SCSI_ABORT_SUCCESS
;
1886 printk("megaraid_abort: unknown command state!!\n");
1887 rc
= SCSI_ABORT_NOT_RUNNING
;
1894 megaCfg
->flag
&= ~IN_ABORT
;
1897 if(megaCfg
->flag
& IN_QUEUE
) printk("ma:flag is in queue\n");
1898 if(megaCfg
->qCompletedH
== NULL
) printk("ma:qchead == null\n");
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
;
1911 SCpnt
->host_scribble
= (unsigned char *) NULL
;
1915 mega_rundoneq(megaCfg
);
1920 /*---------------------------------------------------------------------
1921 * Reset a previous SCSI request
1922 *---------------------------------------------------------------------*/
1923 int megaraid_reset (Scsi_Cmnd
* SCpnt
, unsigned int rstflags
)
1925 mega_host_config
*megaCfg
;
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
,
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
;
1957 megaCfg
->flag
&= ~IN_RESET
;
1959 mega_rundoneq(megaCfg
);
1963 /*-------------------------------------------------------------
1964 * Return the disk geometry for a particular disk
1966 * Disk *disk - Disk geometry
1967 * kdev_t dev - Device node
1968 * int *geom - Returns geometry fields
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) */
1984 cylinders
= disk
->capacity
/ (heads
* sectors
);
1986 /* Handle extended translation size for logical drives > 1Gb */
1987 if (disk
->capacity
>= 0x200000) {
1990 cylinders
= disk
->capacity
/ (heads
* sectors
);
1996 geom
[2] = cylinders
;
2001 static int __init
megaraid_setup(char *str
)
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
;
2016 __setup("megaraid=", megaraid_setup
);
2018 static Scsi_Host_Template driver_template
= MEGARAID
;
2020 #include "scsi_module.c"