3 * Linux MegaRAID device driver
5 * Copyright (c) 2002 LSI Logic Corporation.
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 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
14 * - speed-ups (list handling fixes, issued_list, optimizations.)
17 * Copyright (c) 2003 Christoph Hellwig <hch@lst.de>
18 * - new-style, hotplug-aware pci probing and scsi registration
20 * Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
21 * <Seokmann.Ju@lsil.com>
23 * Description: Linux device driver for LSI Logic MegaRAID controller
25 * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
28 * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
29 * and others. Please send updates to the mailing list
30 * linux-scsi@vger.kernel.org .
36 #include <linux/blkdev.h>
37 #include <asm/uaccess.h>
39 #include <linux/completion.h>
40 #include <linux/delay.h>
41 #include <linux/proc_fs.h>
42 #include <linux/reboot.h>
43 #include <linux/module.h>
44 #include <linux/list.h>
45 #include <linux/interrupt.h>
46 #include <linux/pci.h>
47 #include <linux/init.h>
48 #include <linux/dma-mapping.h>
49 #include <scsi/scsicam.h>
52 #include <scsi/scsi_host.h>
56 #define MEGARAID_MODULE_VERSION "2.00.4"
58 MODULE_AUTHOR ("sju@lsil.com");
59 MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
60 MODULE_LICENSE ("GPL");
61 MODULE_VERSION(MEGARAID_MODULE_VERSION
);
63 static unsigned int max_cmd_per_lun
= DEF_CMD_PER_LUN
;
64 module_param(max_cmd_per_lun
, uint
, 0);
65 MODULE_PARM_DESC(max_cmd_per_lun
, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
67 static unsigned short int max_sectors_per_io
= MAX_SECTORS_PER_IO
;
68 module_param(max_sectors_per_io
, ushort
, 0);
69 MODULE_PARM_DESC(max_sectors_per_io
, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
72 static unsigned short int max_mbox_busy_wait
= MBOX_BUSY_WAIT
;
73 module_param(max_mbox_busy_wait
, ushort
, 0);
74 MODULE_PARM_DESC(max_mbox_busy_wait
, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
76 #define RDINDOOR(adapter) readl((adapter)->mmio_base + 0x20)
77 #define RDOUTDOOR(adapter) readl((adapter)->mmio_base + 0x2C)
78 #define WRINDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x20)
79 #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
86 static adapter_t
*hba_soft_state
[MAX_CONTROLLERS
];
87 static struct proc_dir_entry
*mega_proc_dir_entry
;
89 /* For controller re-ordering */
90 static struct mega_hbas mega_hbas
[MAX_CONTROLLERS
];
93 * The File Operations structure for the serial/ioctl interface of the driver
95 static const struct file_operations megadev_fops
= {
97 .ioctl
= megadev_ioctl
,
102 * Array to structures for storing the information about the controllers. This
103 * information is sent to the user level applications, when they do an ioctl
104 * for this information.
106 static struct mcontroller mcontroller
[MAX_CONTROLLERS
];
108 /* The current driver version */
109 static u32 driver_ver
= 0x02000000;
111 /* major number used by the device for character interface */
114 #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
118 * Debug variable to print some diagnostic messages
120 static int trace_level
;
123 * mega_setup_mailbox()
124 * @adapter - pointer to our soft state
126 * Allocates a 8 byte aligned memory for the handshake mailbox.
129 mega_setup_mailbox(adapter_t
*adapter
)
133 adapter
->una_mbox64
= pci_alloc_consistent(adapter
->dev
,
134 sizeof(mbox64_t
), &adapter
->una_mbox64_dma
);
136 if( !adapter
->una_mbox64
) return -1;
138 adapter
->mbox
= &adapter
->una_mbox64
->mbox
;
140 adapter
->mbox
= (mbox_t
*)((((unsigned long) adapter
->mbox
) + 15) &
143 adapter
->mbox64
= (mbox64_t
*)(((unsigned long)adapter
->mbox
) - 8);
145 align
= ((void *)adapter
->mbox
) - ((void *)&adapter
->una_mbox64
->mbox
);
147 adapter
->mbox_dma
= adapter
->una_mbox64_dma
+ 8 + align
;
150 * Register the mailbox if the controller is an io-mapped controller
152 if( adapter
->flag
& BOARD_IOMAP
) {
154 <<<<<<< HEAD
:drivers
/scsi
/megaraid
.c
155 outb_p(adapter
->mbox_dma
& 0xFF,
157 outb(adapter
->mbox_dma
& 0xFF,
158 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/scsi
/megaraid
.c
159 adapter
->host
->io_port
+ MBOX_PORT0
);
161 <<<<<<< HEAD
:drivers
/scsi
/megaraid
.c
162 outb_p((adapter
->mbox_dma
>> 8) & 0xFF,
164 outb((adapter
->mbox_dma
>> 8) & 0xFF,
165 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/scsi
/megaraid
.c
166 adapter
->host
->io_port
+ MBOX_PORT1
);
168 <<<<<<< HEAD
:drivers
/scsi
/megaraid
.c
169 outb_p((adapter
->mbox_dma
>> 16) & 0xFF,
171 outb((adapter
->mbox_dma
>> 16) & 0xFF,
172 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/scsi
/megaraid
.c
173 adapter
->host
->io_port
+ MBOX_PORT2
);
175 <<<<<<< HEAD
:drivers
/scsi
/megaraid
.c
176 outb_p((adapter
->mbox_dma
>> 24) & 0xFF,
178 outb((adapter
->mbox_dma
>> 24) & 0xFF,
179 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/scsi
/megaraid
.c
180 adapter
->host
->io_port
+ MBOX_PORT3
);
182 <<<<<<< HEAD
:drivers
/scsi
/megaraid
.c
183 outb_p(ENABLE_MBOX_BYTE
,
185 outb(ENABLE_MBOX_BYTE
,
186 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/scsi
/megaraid
.c
187 adapter
->host
->io_port
+ ENABLE_MBOX_REGION
);
199 * mega_query_adapter()
200 * @adapter - pointer to our soft state
202 * Issue the adapter inquiry commands to the controller and find out
203 * information and parameter about the devices attached
206 mega_query_adapter(adapter_t
*adapter
)
208 dma_addr_t prod_info_dma_handle
;
209 mega_inquiry3
*inquiry3
;
210 u8 raw_mbox
[sizeof(struct mbox_out
)];
214 /* Initialize adapter inquiry mailbox */
216 mbox
= (mbox_t
*)raw_mbox
;
218 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
219 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
222 * Try to issue Inquiry3 command
223 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
224 * update enquiry3 structure
226 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
228 inquiry3
= (mega_inquiry3
*)adapter
->mega_buffer
;
230 raw_mbox
[0] = FC_NEW_CONFIG
; /* i.e. mbox->cmd=0xA1 */
231 raw_mbox
[2] = NC_SUBOP_ENQUIRY3
; /* i.e. 0x0F */
232 raw_mbox
[3] = ENQ3_GET_SOLICITED_FULL
; /* i.e. 0x02 */
234 /* Issue a blocking command to the card */
235 if ((retval
= issue_scb_block(adapter
, raw_mbox
))) {
236 /* the adapter does not support 40ld */
238 mraid_ext_inquiry
*ext_inq
;
240 dma_addr_t dma_handle
;
242 ext_inq
= pci_alloc_consistent(adapter
->dev
,
243 sizeof(mraid_ext_inquiry
), &dma_handle
);
245 if( ext_inq
== NULL
) return -1;
247 inq
= &ext_inq
->raid_inq
;
249 mbox
->m_out
.xferaddr
= (u32
)dma_handle
;
251 /*issue old 0x04 command to adapter */
252 mbox
->m_out
.cmd
= MEGA_MBOXCMD_ADPEXTINQ
;
254 issue_scb_block(adapter
, raw_mbox
);
257 * update Enquiry3 and ProductInfo structures with
258 * mraid_inquiry structure
260 mega_8_to_40ld(inq
, inquiry3
,
261 (mega_product_info
*)&adapter
->product_info
);
263 pci_free_consistent(adapter
->dev
, sizeof(mraid_ext_inquiry
),
264 ext_inq
, dma_handle
);
266 } else { /*adapter supports 40ld */
267 adapter
->flag
|= BOARD_40LD
;
270 * get product_info, which is static information and will be
273 prod_info_dma_handle
= pci_map_single(adapter
->dev
, (void *)
274 &adapter
->product_info
,
275 sizeof(mega_product_info
), PCI_DMA_FROMDEVICE
);
277 mbox
->m_out
.xferaddr
= prod_info_dma_handle
;
279 raw_mbox
[0] = FC_NEW_CONFIG
; /* i.e. mbox->cmd=0xA1 */
280 raw_mbox
[2] = NC_SUBOP_PRODUCT_INFO
; /* i.e. 0x0E */
282 if ((retval
= issue_scb_block(adapter
, raw_mbox
)))
284 "megaraid: Product_info cmd failed with error: %d\n",
287 pci_unmap_single(adapter
->dev
, prod_info_dma_handle
,
288 sizeof(mega_product_info
), PCI_DMA_FROMDEVICE
);
293 * kernel scans the channels from 0 to <= max_channel
295 adapter
->host
->max_channel
=
296 adapter
->product_info
.nchannels
+ NVIRT_CHAN
-1;
298 adapter
->host
->max_id
= 16; /* max targets per channel */
300 adapter
->host
->max_lun
= 7; /* Upto 7 luns for non disk devices */
302 adapter
->host
->cmd_per_lun
= max_cmd_per_lun
;
304 adapter
->numldrv
= inquiry3
->num_ldrv
;
306 adapter
->max_cmds
= adapter
->product_info
.max_commands
;
308 if(adapter
->max_cmds
> MAX_COMMANDS
)
309 adapter
->max_cmds
= MAX_COMMANDS
;
311 adapter
->host
->can_queue
= adapter
->max_cmds
- 1;
314 * Get the maximum number of scatter-gather elements supported by this
317 mega_get_max_sgl(adapter
);
319 adapter
->host
->sg_tablesize
= adapter
->sglen
;
322 /* use HP firmware and bios version encoding */
323 if (adapter
->product_info
.subsysvid
== HP_SUBSYS_VID
) {
324 sprintf (adapter
->fw_version
, "%c%d%d.%d%d",
325 adapter
->product_info
.fw_version
[2],
326 adapter
->product_info
.fw_version
[1] >> 8,
327 adapter
->product_info
.fw_version
[1] & 0x0f,
328 adapter
->product_info
.fw_version
[0] >> 8,
329 adapter
->product_info
.fw_version
[0] & 0x0f);
330 sprintf (adapter
->bios_version
, "%c%d%d.%d%d",
331 adapter
->product_info
.bios_version
[2],
332 adapter
->product_info
.bios_version
[1] >> 8,
333 adapter
->product_info
.bios_version
[1] & 0x0f,
334 adapter
->product_info
.bios_version
[0] >> 8,
335 adapter
->product_info
.bios_version
[0] & 0x0f);
337 memcpy(adapter
->fw_version
,
338 (char *)adapter
->product_info
.fw_version
, 4);
339 adapter
->fw_version
[4] = 0;
341 memcpy(adapter
->bios_version
,
342 (char *)adapter
->product_info
.bios_version
, 4);
344 adapter
->bios_version
[4] = 0;
347 printk(KERN_NOTICE
"megaraid: [%s:%s] detected %d logical drives.\n",
348 adapter
->fw_version
, adapter
->bios_version
, adapter
->numldrv
);
351 * Do we support extended (>10 bytes) cdbs
353 adapter
->support_ext_cdb
= mega_support_ext_cdb(adapter
);
354 if (adapter
->support_ext_cdb
)
355 printk(KERN_NOTICE
"megaraid: supports extended CDBs.\n");
363 * @adapter - pointer to our soft state
365 * Runs through the list of pending requests.
368 mega_runpendq(adapter_t
*adapter
)
370 if(!list_empty(&adapter
->pending_list
))
371 __mega_runpendq(adapter
);
376 * @scmd - Issue this scsi command
377 * @done - the callback hook into the scsi mid-layer
379 * The command queuing entry point for the mid-layer.
382 megaraid_queue(Scsi_Cmnd
*scmd
, void (*done
)(Scsi_Cmnd
*))
389 adapter
= (adapter_t
*)scmd
->device
->host
->hostdata
;
391 scmd
->scsi_done
= done
;
395 * Allocate and build a SCB request
396 * busy flag will be set if mega_build_cmd() command could not
397 * allocate scb. We will return non-zero status in that case.
398 * NOTE: scb can be null even though certain commands completed
399 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
400 * return 0 in that case.
403 spin_lock_irqsave(&adapter
->lock
, flags
);
404 scb
= mega_build_cmd(adapter
, scmd
, &busy
);
408 scb
->state
|= SCB_PENDQ
;
409 list_add_tail(&scb
->list
, &adapter
->pending_list
);
412 * Check if the HBA is in quiescent state, e.g., during a
413 * delete logical drive opertion. If it is, don't run
416 if (atomic_read(&adapter
->quiescent
) == 0)
417 mega_runpendq(adapter
);
421 spin_unlock_irqrestore(&adapter
->lock
, flags
);
426 * mega_allocate_scb()
427 * @adapter - pointer to our soft state
428 * @cmd - scsi command from the mid-layer
430 * Allocate a SCB structure. This is the central structure for controller
433 static inline scb_t
*
434 mega_allocate_scb(adapter_t
*adapter
, Scsi_Cmnd
*cmd
)
436 struct list_head
*head
= &adapter
->free_list
;
439 /* Unlink command from Free List */
440 if( !list_empty(head
) ) {
442 scb
= list_entry(head
->next
, scb_t
, list
);
444 list_del_init(head
->next
);
446 scb
->state
= SCB_ACTIVE
;
448 scb
->dma_type
= MEGA_DMA_TYPE_NONE
;
457 * mega_get_ldrv_num()
458 * @adapter - pointer to our soft state
459 * @cmd - scsi mid layer command
460 * @channel - channel on the controller
462 * Calculate the logical drive number based on the information in scsi command
463 * and the channel number.
466 mega_get_ldrv_num(adapter_t
*adapter
, Scsi_Cmnd
*cmd
, int channel
)
471 tgt
= cmd
->device
->id
;
473 if ( tgt
> adapter
->this_id
)
474 tgt
--; /* we do not get inquires for initiator id */
476 ldrv_num
= (channel
* 15) + tgt
;
480 * If we have a logical drive with boot enabled, project it first
482 if( adapter
->boot_ldrv_enabled
) {
483 if( ldrv_num
== 0 ) {
484 ldrv_num
= adapter
->boot_ldrv
;
487 if( ldrv_num
<= adapter
->boot_ldrv
) {
494 * If "delete logical drive" feature is enabled on this controller.
495 * Do only if at least one delete logical drive operation was done.
497 * Also, after logical drive deletion, instead of logical drive number,
498 * the value returned should be 0x80+logical drive id.
500 * These is valid only for IO commands.
503 if (adapter
->support_random_del
&& adapter
->read_ldidmap
)
504 switch (cmd
->cmnd
[0]) {
505 case READ_6
: /* fall through */
506 case WRITE_6
: /* fall through */
507 case READ_10
: /* fall through */
517 * @adapter - pointer to our soft state
518 * @cmd - Prepare using this scsi command
519 * @busy - busy flag if no resources
521 * Prepares a command and scatter gather list for the controller. This routine
522 * also finds out if the commands is intended for a logical drive or a
523 * physical device and prepares the controller command accordingly.
525 * We also re-order the logical drives and physical devices based on their
529 mega_build_cmd(adapter_t
*adapter
, Scsi_Cmnd
*cmd
, int *busy
)
531 mega_ext_passthru
*epthru
;
532 mega_passthru
*pthru
;
540 int ldrv_num
= 0; /* logical drive number */
544 * filter the internal and ioctl commands
546 if((cmd
->cmnd
[0] == MEGA_INTERNAL_CMD
))
547 return (scb_t
*)cmd
->host_scribble
;
550 * We know what channels our logical drives are on - mega_find_card()
552 islogical
= adapter
->logdrv_chan
[cmd
->device
->channel
];
555 * The theory: If physical drive is chosen for boot, all the physical
556 * devices are exported before the logical drives, otherwise physical
557 * devices are pushed after logical drives, in which case - Kernel sees
558 * the physical devices on virtual channel which is obviously converted
559 * to actual channel on the HBA.
561 if( adapter
->boot_pdrv_enabled
) {
563 /* logical channel */
564 channel
= cmd
->device
->channel
-
565 adapter
->product_info
.nchannels
;
568 /* this is physical channel */
569 channel
= cmd
->device
->channel
;
570 target
= cmd
->device
->id
;
573 * boot from a physical disk, that disk needs to be
574 * exposed first IF both the channels are SCSI, then
575 * booting from the second channel is not allowed.
578 target
= adapter
->boot_pdrv_tgt
;
580 else if( target
== adapter
->boot_pdrv_tgt
) {
587 /* this is the logical channel */
588 channel
= cmd
->device
->channel
;
591 /* physical channel */
592 channel
= cmd
->device
->channel
- NVIRT_CHAN
;
593 target
= cmd
->device
->id
;
600 /* have just LUN 0 for each target on virtual channels */
601 if (cmd
->device
->lun
) {
602 cmd
->result
= (DID_BAD_TARGET
<< 16);
607 ldrv_num
= mega_get_ldrv_num(adapter
, cmd
, channel
);
610 max_ldrv_num
= (adapter
->flag
& BOARD_40LD
) ?
611 MAX_LOGICAL_DRIVES_40LD
: MAX_LOGICAL_DRIVES_8LD
;
614 * max_ldrv_num increases by 0x80 if some logical drive was
617 if(adapter
->read_ldidmap
)
618 max_ldrv_num
+= 0x80;
620 if(ldrv_num
> max_ldrv_num
) {
621 cmd
->result
= (DID_BAD_TARGET
<< 16);
628 if( cmd
->device
->lun
> 7) {
630 * Do not support lun >7 for physically accessed
633 cmd
->result
= (DID_BAD_TARGET
<< 16);
641 * Logical drive commands
645 switch (cmd
->cmnd
[0]) {
646 case TEST_UNIT_READY
:
647 #if MEGA_HAVE_CLUSTERING
649 * Do we support clustering and is the support enabled
650 * If no, return success always
652 if( !adapter
->has_cluster
) {
653 cmd
->result
= (DID_OK
<< 16);
658 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
663 scb
->raw_mbox
[0] = MEGA_CLUSTER_CMD
;
664 scb
->raw_mbox
[2] = MEGA_RESERVATION_STATUS
;
665 scb
->raw_mbox
[3] = ldrv_num
;
667 scb
->dma_direction
= PCI_DMA_NONE
;
671 cmd
->result
= (DID_OK
<< 16);
678 struct scatterlist
*sg
;
680 sg
= scsi_sglist(cmd
);
681 buf
= kmap_atomic(sg_page(sg
), KM_IRQ0
) + sg
->offset
;
683 memset(buf
, 0, cmd
->cmnd
[4]);
684 kunmap_atomic(buf
- sg
->offset
, KM_IRQ0
);
686 cmd
->result
= (DID_OK
<< 16);
694 if(!(adapter
->flag
& (1L << cmd
->device
->channel
))) {
697 "scsi%d: scanning scsi channel %d ",
698 adapter
->host
->host_no
,
699 cmd
->device
->channel
);
700 printk("for logical drives.\n");
702 adapter
->flag
|= (1L << cmd
->device
->channel
);
705 /* Allocate a SCB and initialize passthru */
706 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
712 mbox
= (mbox_t
*)scb
->raw_mbox
;
713 memset(mbox
, 0, sizeof(scb
->raw_mbox
));
714 memset(pthru
, 0, sizeof(mega_passthru
));
718 pthru
->reqsenselen
= 14;
719 pthru
->islogical
= 1;
720 pthru
->logdrv
= ldrv_num
;
721 pthru
->cdblen
= cmd
->cmd_len
;
722 memcpy(pthru
->cdb
, cmd
->cmnd
, cmd
->cmd_len
);
724 if( adapter
->has_64bit_addr
) {
725 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU64
;
728 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
731 scb
->dma_direction
= PCI_DMA_FROMDEVICE
;
733 pthru
->numsgelements
= mega_build_sglist(adapter
, scb
,
734 &pthru
->dataxferaddr
, &pthru
->dataxferlen
);
736 mbox
->m_out
.xferaddr
= scb
->pthru_dma_addr
;
747 /* Allocate a SCB and initialize mailbox */
748 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
752 mbox
= (mbox_t
*)scb
->raw_mbox
;
754 memset(mbox
, 0, sizeof(scb
->raw_mbox
));
755 mbox
->m_out
.logdrv
= ldrv_num
;
758 * A little hack: 2nd bit is zero for all scsi read
759 * commands and is set for all scsi write commands
761 if( adapter
->has_64bit_addr
) {
762 mbox
->m_out
.cmd
= (*cmd
->cmnd
& 0x02) ?
763 MEGA_MBOXCMD_LWRITE64
:
764 MEGA_MBOXCMD_LREAD64
;
767 mbox
->m_out
.cmd
= (*cmd
->cmnd
& 0x02) ?
773 * 6-byte READ(0x08) or WRITE(0x0A) cdb
775 if( cmd
->cmd_len
== 6 ) {
776 mbox
->m_out
.numsectors
= (u32
) cmd
->cmnd
[4];
778 ((u32
)cmd
->cmnd
[1] << 16) |
779 ((u32
)cmd
->cmnd
[2] << 8) |
782 mbox
->m_out
.lba
&= 0x1FFFFF;
786 * Take modulo 0x80, since the logical drive
787 * number increases by 0x80 when a logical
790 if (*cmd
->cmnd
== READ_6
) {
791 adapter
->nreads
[ldrv_num
%0x80]++;
792 adapter
->nreadblocks
[ldrv_num
%0x80] +=
793 mbox
->m_out
.numsectors
;
795 adapter
->nwrites
[ldrv_num
%0x80]++;
796 adapter
->nwriteblocks
[ldrv_num
%0x80] +=
797 mbox
->m_out
.numsectors
;
803 * 10-byte READ(0x28) or WRITE(0x2A) cdb
805 if( cmd
->cmd_len
== 10 ) {
806 mbox
->m_out
.numsectors
=
808 ((u32
)cmd
->cmnd
[7] << 8);
810 ((u32
)cmd
->cmnd
[2] << 24) |
811 ((u32
)cmd
->cmnd
[3] << 16) |
812 ((u32
)cmd
->cmnd
[4] << 8) |
816 if (*cmd
->cmnd
== READ_10
) {
817 adapter
->nreads
[ldrv_num
%0x80]++;
818 adapter
->nreadblocks
[ldrv_num
%0x80] +=
819 mbox
->m_out
.numsectors
;
821 adapter
->nwrites
[ldrv_num
%0x80]++;
822 adapter
->nwriteblocks
[ldrv_num
%0x80] +=
823 mbox
->m_out
.numsectors
;
829 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
831 if( cmd
->cmd_len
== 12 ) {
833 ((u32
)cmd
->cmnd
[2] << 24) |
834 ((u32
)cmd
->cmnd
[3] << 16) |
835 ((u32
)cmd
->cmnd
[4] << 8) |
838 mbox
->m_out
.numsectors
=
839 ((u32
)cmd
->cmnd
[6] << 24) |
840 ((u32
)cmd
->cmnd
[7] << 16) |
841 ((u32
)cmd
->cmnd
[8] << 8) |
845 if (*cmd
->cmnd
== READ_12
) {
846 adapter
->nreads
[ldrv_num
%0x80]++;
847 adapter
->nreadblocks
[ldrv_num
%0x80] +=
848 mbox
->m_out
.numsectors
;
850 adapter
->nwrites
[ldrv_num
%0x80]++;
851 adapter
->nwriteblocks
[ldrv_num
%0x80] +=
852 mbox
->m_out
.numsectors
;
858 * If it is a read command
860 if( (*cmd
->cmnd
& 0x0F) == 0x08 ) {
861 scb
->dma_direction
= PCI_DMA_FROMDEVICE
;
864 scb
->dma_direction
= PCI_DMA_TODEVICE
;
867 /* Calculate Scatter-Gather info */
868 mbox
->m_out
.numsgelements
= mega_build_sglist(adapter
, scb
,
869 (u32
*)&mbox
->m_out
.xferaddr
, (u32
*)&seg
);
873 #if MEGA_HAVE_CLUSTERING
874 case RESERVE
: /* Fall through */
878 * Do we support clustering and is the support enabled
880 if( ! adapter
->has_cluster
) {
882 cmd
->result
= (DID_BAD_TARGET
<< 16);
887 /* Allocate a SCB and initialize mailbox */
888 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
893 scb
->raw_mbox
[0] = MEGA_CLUSTER_CMD
;
894 scb
->raw_mbox
[2] = ( *cmd
->cmnd
== RESERVE
) ?
895 MEGA_RESERVE_LD
: MEGA_RELEASE_LD
;
897 scb
->raw_mbox
[3] = ldrv_num
;
899 scb
->dma_direction
= PCI_DMA_NONE
;
905 cmd
->result
= (DID_BAD_TARGET
<< 16);
912 * Passthru drive commands
915 /* Allocate a SCB and initialize passthru */
916 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
921 mbox
= (mbox_t
*)scb
->raw_mbox
;
922 memset(mbox
, 0, sizeof(scb
->raw_mbox
));
924 if( adapter
->support_ext_cdb
) {
926 epthru
= mega_prepare_extpassthru(adapter
, scb
, cmd
,
929 mbox
->m_out
.cmd
= MEGA_MBOXCMD_EXTPTHRU
;
931 mbox
->m_out
.xferaddr
= scb
->epthru_dma_addr
;
936 pthru
= mega_prepare_passthru(adapter
, scb
, cmd
,
939 /* Initialize mailbox */
940 if( adapter
->has_64bit_addr
) {
941 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU64
;
944 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
947 mbox
->m_out
.xferaddr
= scb
->pthru_dma_addr
;
957 * mega_prepare_passthru()
958 * @adapter - pointer to our soft state
959 * @scb - our scsi control block
960 * @cmd - scsi command from the mid-layer
961 * @channel - actual channel on the controller
962 * @target - actual id on the controller.
964 * prepare a command for the scsi physical devices.
966 static mega_passthru
*
967 mega_prepare_passthru(adapter_t
*adapter
, scb_t
*scb
, Scsi_Cmnd
*cmd
,
968 int channel
, int target
)
970 mega_passthru
*pthru
;
973 memset(pthru
, 0, sizeof (mega_passthru
));
975 /* 0=6sec/1=60sec/2=10min/3=3hrs */
979 pthru
->reqsenselen
= 14;
980 pthru
->islogical
= 0;
982 pthru
->channel
= (adapter
->flag
& BOARD_40LD
) ? 0 : channel
;
984 pthru
->target
= (adapter
->flag
& BOARD_40LD
) ?
985 (channel
<< 4) | target
: target
;
987 pthru
->cdblen
= cmd
->cmd_len
;
988 pthru
->logdrv
= cmd
->device
->lun
;
990 memcpy(pthru
->cdb
, cmd
->cmnd
, cmd
->cmd_len
);
992 /* Not sure about the direction */
993 scb
->dma_direction
= PCI_DMA_BIDIRECTIONAL
;
995 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
996 switch (cmd
->cmnd
[0]) {
999 if(!(adapter
->flag
& (1L << cmd
->device
->channel
))) {
1002 "scsi%d: scanning scsi channel %d [P%d] ",
1003 adapter
->host
->host_no
,
1004 cmd
->device
->channel
, channel
);
1005 printk("for physical devices.\n");
1007 adapter
->flag
|= (1L << cmd
->device
->channel
);
1011 pthru
->numsgelements
= mega_build_sglist(adapter
, scb
,
1012 &pthru
->dataxferaddr
, &pthru
->dataxferlen
);
1020 * mega_prepare_extpassthru()
1021 * @adapter - pointer to our soft state
1022 * @scb - our scsi control block
1023 * @cmd - scsi command from the mid-layer
1024 * @channel - actual channel on the controller
1025 * @target - actual id on the controller.
1027 * prepare a command for the scsi physical devices. This rountine prepares
1028 * commands for devices which can take extended CDBs (>10 bytes)
1030 static mega_ext_passthru
*
1031 mega_prepare_extpassthru(adapter_t
*adapter
, scb_t
*scb
, Scsi_Cmnd
*cmd
,
1032 int channel
, int target
)
1034 mega_ext_passthru
*epthru
;
1036 epthru
= scb
->epthru
;
1037 memset(epthru
, 0, sizeof(mega_ext_passthru
));
1039 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1040 epthru
->timeout
= 2;
1043 epthru
->reqsenselen
= 14;
1044 epthru
->islogical
= 0;
1046 epthru
->channel
= (adapter
->flag
& BOARD_40LD
) ? 0 : channel
;
1047 epthru
->target
= (adapter
->flag
& BOARD_40LD
) ?
1048 (channel
<< 4) | target
: target
;
1050 epthru
->cdblen
= cmd
->cmd_len
;
1051 epthru
->logdrv
= cmd
->device
->lun
;
1053 memcpy(epthru
->cdb
, cmd
->cmnd
, cmd
->cmd_len
);
1055 /* Not sure about the direction */
1056 scb
->dma_direction
= PCI_DMA_BIDIRECTIONAL
;
1058 switch(cmd
->cmnd
[0]) {
1061 if(!(adapter
->flag
& (1L << cmd
->device
->channel
))) {
1064 "scsi%d: scanning scsi channel %d [P%d] ",
1065 adapter
->host
->host_no
,
1066 cmd
->device
->channel
, channel
);
1067 printk("for physical devices.\n");
1069 adapter
->flag
|= (1L << cmd
->device
->channel
);
1073 epthru
->numsgelements
= mega_build_sglist(adapter
, scb
,
1074 &epthru
->dataxferaddr
, &epthru
->dataxferlen
);
1082 __mega_runpendq(adapter_t
*adapter
)
1085 struct list_head
*pos
, *next
;
1087 /* Issue any pending commands to the card */
1088 list_for_each_safe(pos
, next
, &adapter
->pending_list
) {
1090 scb
= list_entry(pos
, scb_t
, list
);
1092 if( !(scb
->state
& SCB_ISSUED
) ) {
1094 if( issue_scb(adapter
, scb
) != 0 )
1105 * @adapter - pointer to our soft state
1106 * @scb - scsi control block
1108 * Post a command to the card if the mailbox is available, otherwise return
1109 * busy. We also take the scb from the pending list if the mailbox is
1113 issue_scb(adapter_t
*adapter
, scb_t
*scb
)
1115 volatile mbox64_t
*mbox64
= adapter
->mbox64
;
1116 volatile mbox_t
*mbox
= adapter
->mbox
;
1119 if(unlikely(mbox
->m_in
.busy
)) {
1123 } while( mbox
->m_in
.busy
&& (i
< max_mbox_busy_wait
) );
1125 if(mbox
->m_in
.busy
) return -1;
1128 /* Copy mailbox data into host structure */
1129 memcpy((char *)&mbox
->m_out
, (char *)scb
->raw_mbox
,
1130 sizeof(struct mbox_out
));
1132 mbox
->m_out
.cmdid
= scb
->idx
; /* Set cmdid */
1133 mbox
->m_in
.busy
= 1; /* Set busy */
1137 * Increment the pending queue counter
1139 atomic_inc(&adapter
->pend_cmds
);
1141 switch (mbox
->m_out
.cmd
) {
1142 case MEGA_MBOXCMD_LREAD64
:
1143 case MEGA_MBOXCMD_LWRITE64
:
1144 case MEGA_MBOXCMD_PASSTHRU64
:
1145 case MEGA_MBOXCMD_EXTPTHRU
:
1146 mbox64
->xfer_segment_lo
= mbox
->m_out
.xferaddr
;
1147 mbox64
->xfer_segment_hi
= 0;
1148 mbox
->m_out
.xferaddr
= 0xFFFFFFFF;
1151 mbox64
->xfer_segment_lo
= 0;
1152 mbox64
->xfer_segment_hi
= 0;
1158 scb
->state
|= SCB_ISSUED
;
1160 if( likely(adapter
->flag
& BOARD_MEMMAP
) ) {
1161 mbox
->m_in
.poll
= 0;
1163 WRINDOOR(adapter
, adapter
->mbox_dma
| 0x1);
1166 irq_enable(adapter
);
1167 issue_command(adapter
);
1174 * Wait until the controller's mailbox is available
1177 mega_busywait_mbox (adapter_t
*adapter
)
1179 if (adapter
->mbox
->m_in
.busy
)
1180 return __mega_busywait_mbox(adapter
);
1186 * @adapter - pointer to our soft state
1187 * @raw_mbox - the mailbox
1189 * Issue a scb in synchronous and non-interrupt mode
1192 issue_scb_block(adapter_t
*adapter
, u_char
*raw_mbox
)
1194 volatile mbox64_t
*mbox64
= adapter
->mbox64
;
1195 volatile mbox_t
*mbox
= adapter
->mbox
;
1198 /* Wait until mailbox is free */
1199 if(mega_busywait_mbox (adapter
))
1200 goto bug_blocked_mailbox
;
1202 /* Copy mailbox data into host structure */
1203 memcpy((char *) mbox
, raw_mbox
, sizeof(struct mbox_out
));
1204 mbox
->m_out
.cmdid
= 0xFE;
1205 mbox
->m_in
.busy
= 1;
1207 switch (raw_mbox
[0]) {
1208 case MEGA_MBOXCMD_LREAD64
:
1209 case MEGA_MBOXCMD_LWRITE64
:
1210 case MEGA_MBOXCMD_PASSTHRU64
:
1211 case MEGA_MBOXCMD_EXTPTHRU
:
1212 mbox64
->xfer_segment_lo
= mbox
->m_out
.xferaddr
;
1213 mbox64
->xfer_segment_hi
= 0;
1214 mbox
->m_out
.xferaddr
= 0xFFFFFFFF;
1217 mbox64
->xfer_segment_lo
= 0;
1218 mbox64
->xfer_segment_hi
= 0;
1221 if( likely(adapter
->flag
& BOARD_MEMMAP
) ) {
1222 mbox
->m_in
.poll
= 0;
1224 mbox
->m_in
.numstatus
= 0xFF;
1225 mbox
->m_in
.status
= 0xFF;
1226 WRINDOOR(adapter
, adapter
->mbox_dma
| 0x1);
1228 while((volatile u8
)mbox
->m_in
.numstatus
== 0xFF)
1231 mbox
->m_in
.numstatus
= 0xFF;
1233 while( (volatile u8
)mbox
->m_in
.poll
!= 0x77 )
1236 mbox
->m_in
.poll
= 0;
1237 mbox
->m_in
.ack
= 0x77;
1239 WRINDOOR(adapter
, adapter
->mbox_dma
| 0x2);
1241 while(RDINDOOR(adapter
) & 0x2)
1245 irq_disable(adapter
);
1246 issue_command(adapter
);
1248 while (!((byte
= irq_state(adapter
)) & INTR_VALID
))
1251 set_irq_state(adapter
, byte
);
1252 irq_enable(adapter
);
1256 return mbox
->m_in
.status
;
1258 bug_blocked_mailbox
:
1259 printk(KERN_WARNING
"megaraid: Blocked mailbox......!!\n");
1266 * megaraid_isr_iomapped()
1268 * @devp - pointer to our soft state
1270 * Interrupt service routine for io-mapped controllers.
1271 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1272 * and service the completed commands.
1275 megaraid_isr_iomapped(int irq
, void *devp
)
1277 adapter_t
*adapter
= devp
;
1278 unsigned long flags
;
1281 u8 completed
[MAX_FIRMWARE_STATUS
];
1287 * loop till F/W has more commands for us to complete.
1289 spin_lock_irqsave(&adapter
->lock
, flags
);
1292 /* Check if a valid interrupt is pending */
1293 byte
= irq_state(adapter
);
1294 if( (byte
& VALID_INTR_BYTE
) == 0 ) {
1296 * No more pending commands
1300 set_irq_state(adapter
, byte
);
1302 while((nstatus
= (volatile u8
)adapter
->mbox
->m_in
.numstatus
)
1305 adapter
->mbox
->m_in
.numstatus
= 0xFF;
1307 status
= adapter
->mbox
->m_in
.status
;
1310 * decrement the pending queue counter
1312 atomic_sub(nstatus
, &adapter
->pend_cmds
);
1314 memcpy(completed
, (void *)adapter
->mbox
->m_in
.completed
,
1317 /* Acknowledge interrupt */
1320 mega_cmd_done(adapter
, completed
, nstatus
, status
);
1322 mega_rundoneq(adapter
);
1326 /* Loop through any pending requests */
1327 if(atomic_read(&adapter
->quiescent
) == 0) {
1328 mega_runpendq(adapter
);
1335 spin_unlock_irqrestore(&adapter
->lock
, flags
);
1337 return IRQ_RETVAL(handled
);
1342 * megaraid_isr_memmapped()
1344 * @devp - pointer to our soft state
1346 * Interrupt service routine for memory-mapped controllers.
1347 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1348 * and service the completed commands.
1351 megaraid_isr_memmapped(int irq
, void *devp
)
1353 adapter_t
*adapter
= devp
;
1354 unsigned long flags
;
1358 u8 completed
[MAX_FIRMWARE_STATUS
];
1363 * loop till F/W has more commands for us to complete.
1365 spin_lock_irqsave(&adapter
->lock
, flags
);
1368 /* Check if a valid interrupt is pending */
1369 dword
= RDOUTDOOR(adapter
);
1370 if(dword
!= 0x10001234) {
1372 * No more pending commands
1376 WROUTDOOR(adapter
, 0x10001234);
1378 while((nstatus
= (volatile u8
)adapter
->mbox
->m_in
.numstatus
)
1382 adapter
->mbox
->m_in
.numstatus
= 0xFF;
1384 status
= adapter
->mbox
->m_in
.status
;
1387 * decrement the pending queue counter
1389 atomic_sub(nstatus
, &adapter
->pend_cmds
);
1391 memcpy(completed
, (void *)adapter
->mbox
->m_in
.completed
,
1394 /* Acknowledge interrupt */
1395 WRINDOOR(adapter
, 0x2);
1399 while( RDINDOOR(adapter
) & 0x02 )
1402 mega_cmd_done(adapter
, completed
, nstatus
, status
);
1404 mega_rundoneq(adapter
);
1406 /* Loop through any pending requests */
1407 if(atomic_read(&adapter
->quiescent
) == 0) {
1408 mega_runpendq(adapter
);
1415 spin_unlock_irqrestore(&adapter
->lock
, flags
);
1417 return IRQ_RETVAL(handled
);
1421 * @adapter - pointer to our soft state
1422 * @completed - array of ids of completed commands
1423 * @nstatus - number of completed commands
1424 * @status - status of the last command completed
1426 * Complete the comamnds and call the scsi mid-layer callback hooks.
1429 mega_cmd_done(adapter_t
*adapter
, u8 completed
[], int nstatus
, int status
)
1431 mega_ext_passthru
*epthru
= NULL
;
1432 struct scatterlist
*sgl
;
1433 Scsi_Cmnd
*cmd
= NULL
;
1434 mega_passthru
*pthru
= NULL
;
1435 mbox_t
*mbox
= NULL
;
1443 * for all the commands completed, call the mid-layer callback routine
1446 for( i
= 0; i
< nstatus
; i
++ ) {
1448 cmdid
= completed
[i
];
1450 if( cmdid
== CMDID_INT_CMDS
) { /* internal command */
1451 scb
= &adapter
->int_scb
;
1453 mbox
= (mbox_t
*)scb
->raw_mbox
;
1456 * Internal command interface do not fire the extended
1457 * passthru or 64-bit passthru
1463 scb
= &adapter
->scb_list
[cmdid
];
1466 * Make sure f/w has completed a valid command
1468 if( !(scb
->state
& SCB_ISSUED
) || scb
->cmd
== NULL
) {
1470 "megaraid: invalid command ");
1471 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1472 cmdid
, scb
->state
, scb
->cmd
);
1478 * Was a abort issued for this command
1480 if( scb
->state
& SCB_ABORT
) {
1483 "megaraid: aborted cmd %lx[%x] complete.\n",
1484 scb
->cmd
->serial_number
, scb
->idx
);
1486 scb
->cmd
->result
= (DID_ABORT
<< 16);
1488 list_add_tail(SCSI_LIST(scb
->cmd
),
1489 &adapter
->completed_list
);
1491 mega_free_scb(adapter
, scb
);
1497 * Was a reset issued for this command
1499 if( scb
->state
& SCB_RESET
) {
1502 "megaraid: reset cmd %lx[%x] complete.\n",
1503 scb
->cmd
->serial_number
, scb
->idx
);
1505 scb
->cmd
->result
= (DID_RESET
<< 16);
1507 list_add_tail(SCSI_LIST(scb
->cmd
),
1508 &adapter
->completed_list
);
1510 mega_free_scb (adapter
, scb
);
1517 epthru
= scb
->epthru
;
1518 mbox
= (mbox_t
*)scb
->raw_mbox
;
1523 int logdrv
= mbox
->m_out
.logdrv
;
1525 islogical
= adapter
->logdrv_chan
[cmd
->channel
];
1527 * Maintain an error counter for the logical drive.
1528 * Some application like SNMP agent need such
1531 if( status
&& islogical
&& (cmd
->cmnd
[0] == READ_6
||
1532 cmd
->cmnd
[0] == READ_10
||
1533 cmd
->cmnd
[0] == READ_12
)) {
1535 * Logical drive number increases by 0x80 when
1536 * a logical drive is deleted
1538 adapter
->rd_errors
[logdrv
%0x80]++;
1541 if( status
&& islogical
&& (cmd
->cmnd
[0] == WRITE_6
||
1542 cmd
->cmnd
[0] == WRITE_10
||
1543 cmd
->cmnd
[0] == WRITE_12
)) {
1545 * Logical drive number increases by 0x80 when
1546 * a logical drive is deleted
1548 adapter
->wr_errors
[logdrv
%0x80]++;
1556 * Do not return the presence of hard disk on the channel so,
1557 * inquiry sent, and returned data==hard disk or removable
1558 * hard disk and not logical, request should return failure! -
1561 islogical
= adapter
->logdrv_chan
[cmd
->device
->channel
];
1562 if( cmd
->cmnd
[0] == INQUIRY
&& !islogical
) {
1564 sgl
= scsi_sglist(cmd
);
1565 if( sg_page(sgl
) ) {
1566 c
= *(unsigned char *) sg_virt(&sgl
[0]);
1569 "megaraid: invalid sg.\n");
1573 if(IS_RAID_CH(adapter
, cmd
->device
->channel
) &&
1574 ((c
& 0x1F ) == TYPE_DISK
)) {
1579 /* clear result; otherwise, success returns corrupt value */
1582 /* Convert MegaRAID status to Linux error code */
1584 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1585 cmd
->result
|= (DID_OK
<< 16);
1588 case 0x02: /* ERROR_ABORTED, i.e.
1589 SCSI_STATUS_CHECK_CONDITION */
1591 /* set sense_buffer and result fields */
1592 if( mbox
->m_out
.cmd
== MEGA_MBOXCMD_PASSTHRU
||
1593 mbox
->m_out
.cmd
== MEGA_MBOXCMD_PASSTHRU64
) {
1595 memcpy(cmd
->sense_buffer
, pthru
->reqsensearea
,
1598 cmd
->result
= (DRIVER_SENSE
<< 24) |
1600 (CHECK_CONDITION
<< 1);
1603 if (mbox
->m_out
.cmd
== MEGA_MBOXCMD_EXTPTHRU
) {
1605 memcpy(cmd
->sense_buffer
,
1606 epthru
->reqsensearea
, 14);
1608 cmd
->result
= (DRIVER_SENSE
<< 24) |
1610 (CHECK_CONDITION
<< 1);
1612 cmd
->sense_buffer
[0] = 0x70;
1613 cmd
->sense_buffer
[2] = ABORTED_COMMAND
;
1614 cmd
->result
|= (CHECK_CONDITION
<< 1);
1619 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1621 cmd
->result
|= (DID_BUS_BUSY
<< 16) | status
;
1625 #if MEGA_HAVE_CLUSTERING
1627 * If TEST_UNIT_READY fails, we know
1628 * MEGA_RESERVATION_STATUS failed
1630 if( cmd
->cmnd
[0] == TEST_UNIT_READY
) {
1631 cmd
->result
|= (DID_ERROR
<< 16) |
1632 (RESERVATION_CONFLICT
<< 1);
1636 * Error code returned is 1 if Reserve or Release
1637 * failed or the input parameter is invalid
1640 (cmd
->cmnd
[0] == RESERVE
||
1641 cmd
->cmnd
[0] == RELEASE
) ) {
1643 cmd
->result
|= (DID_ERROR
<< 16) |
1644 (RESERVATION_CONFLICT
<< 1);
1648 cmd
->result
|= (DID_BAD_TARGET
<< 16)|status
;
1652 * Only free SCBs for the commands coming down from the
1653 * mid-layer, not for which were issued internally
1655 * For internal command, restore the status returned by the
1656 * firmware so that user can interpret it.
1658 if( cmdid
== CMDID_INT_CMDS
) { /* internal command */
1659 cmd
->result
= status
;
1662 * Remove the internal command from the pending list
1664 list_del_init(&scb
->list
);
1665 scb
->state
= SCB_FREE
;
1668 mega_free_scb(adapter
, scb
);
1671 /* Add Scsi_Command to end of completed queue */
1672 list_add_tail(SCSI_LIST(cmd
), &adapter
->completed_list
);
1680 * Run through the list of completed requests and finish it
1683 mega_rundoneq (adapter_t
*adapter
)
1686 struct list_head
*pos
;
1688 list_for_each(pos
, &adapter
->completed_list
) {
1690 struct scsi_pointer
* spos
= (struct scsi_pointer
*)pos
;
1692 cmd
= list_entry(spos
, Scsi_Cmnd
, SCp
);
1693 cmd
->scsi_done(cmd
);
1696 INIT_LIST_HEAD(&adapter
->completed_list
);
1701 * Free a SCB structure
1702 * Note: We assume the scsi commands associated with this scb is not free yet.
1705 mega_free_scb(adapter_t
*adapter
, scb_t
*scb
)
1707 switch( scb
->dma_type
) {
1709 case MEGA_DMA_TYPE_NONE
:
1713 scsi_dma_unmap(scb
->cmd
);
1720 * Remove from the pending list
1722 list_del_init(&scb
->list
);
1724 /* Link the scb back into free list */
1725 scb
->state
= SCB_FREE
;
1728 list_add(&scb
->list
, &adapter
->free_list
);
1733 __mega_busywait_mbox (adapter_t
*adapter
)
1735 volatile mbox_t
*mbox
= adapter
->mbox
;
1738 for (counter
= 0; counter
< 10000; counter
++) {
1739 if (!mbox
->m_in
.busy
)
1744 return -1; /* give up after 1 second */
1748 * Copies data to SGLIST
1749 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1752 mega_build_sglist(adapter_t
*adapter
, scb_t
*scb
, u32
*buf
, u32
*len
)
1754 struct scatterlist
*sg
;
1762 * Copy Scatter-Gather list info into controller structure.
1764 * The number of sg elements returned must not exceed our limit
1766 sgcnt
= scsi_dma_map(cmd
);
1768 scb
->dma_type
= MEGA_SGLIST
;
1770 BUG_ON(sgcnt
> adapter
->sglen
|| sgcnt
< 0);
1774 if (scsi_sg_count(cmd
) == 1 && !adapter
->has_64bit_addr
) {
1775 sg
= scsi_sglist(cmd
);
1776 scb
->dma_h_bulkdata
= sg_dma_address(sg
);
1777 *buf
= (u32
)scb
->dma_h_bulkdata
;
1778 *len
= sg_dma_len(sg
);
1782 scsi_for_each_sg(cmd
, sg
, sgcnt
, idx
) {
1783 if (adapter
->has_64bit_addr
) {
1784 scb
->sgl64
[idx
].address
= sg_dma_address(sg
);
1785 *len
+= scb
->sgl64
[idx
].length
= sg_dma_len(sg
);
1787 scb
->sgl
[idx
].address
= sg_dma_address(sg
);
1788 *len
+= scb
->sgl
[idx
].length
= sg_dma_len(sg
);
1792 /* Reset pointer and length fields */
1793 *buf
= scb
->sgl_dma_addr
;
1795 /* Return count of SG requests */
1803 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1804 * Enquiry3 structures for later use
1807 mega_8_to_40ld(mraid_inquiry
*inquiry
, mega_inquiry3
*enquiry3
,
1808 mega_product_info
*product_info
)
1812 product_info
->max_commands
= inquiry
->adapter_info
.max_commands
;
1813 enquiry3
->rebuild_rate
= inquiry
->adapter_info
.rebuild_rate
;
1814 product_info
->nchannels
= inquiry
->adapter_info
.nchannels
;
1816 for (i
= 0; i
< 4; i
++) {
1817 product_info
->fw_version
[i
] =
1818 inquiry
->adapter_info
.fw_version
[i
];
1820 product_info
->bios_version
[i
] =
1821 inquiry
->adapter_info
.bios_version
[i
];
1823 enquiry3
->cache_flush_interval
=
1824 inquiry
->adapter_info
.cache_flush_interval
;
1826 product_info
->dram_size
= inquiry
->adapter_info
.dram_size
;
1828 enquiry3
->num_ldrv
= inquiry
->logdrv_info
.num_ldrv
;
1830 for (i
= 0; i
< MAX_LOGICAL_DRIVES_8LD
; i
++) {
1831 enquiry3
->ldrv_size
[i
] = inquiry
->logdrv_info
.ldrv_size
[i
];
1832 enquiry3
->ldrv_prop
[i
] = inquiry
->logdrv_info
.ldrv_prop
[i
];
1833 enquiry3
->ldrv_state
[i
] = inquiry
->logdrv_info
.ldrv_state
[i
];
1836 for (i
= 0; i
< (MAX_PHYSICAL_DRIVES
); i
++)
1837 enquiry3
->pdrv_state
[i
] = inquiry
->pdrv_info
.pdrv_state
[i
];
1841 mega_free_sgl(adapter_t
*adapter
)
1846 for(i
= 0; i
< adapter
->max_cmds
; i
++) {
1848 scb
= &adapter
->scb_list
[i
];
1851 pci_free_consistent(adapter
->dev
,
1852 sizeof(mega_sgl64
) * adapter
->sglen
,
1860 pci_free_consistent(adapter
->dev
, sizeof(mega_passthru
),
1861 scb
->pthru
, scb
->pthru_dma_addr
);
1867 pci_free_consistent(adapter
->dev
,
1868 sizeof(mega_ext_passthru
),
1869 scb
->epthru
, scb
->epthru_dma_addr
);
1879 * Get information about the card/driver
1882 megaraid_info(struct Scsi_Host
*host
)
1884 static char buffer
[512];
1887 adapter
= (adapter_t
*)host
->hostdata
;
1890 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1891 adapter
->fw_version
, adapter
->product_info
.max_commands
,
1892 adapter
->host
->max_id
, adapter
->host
->max_channel
,
1893 adapter
->host
->max_lun
);
1898 * Abort a previous SCSI request. Only commands on the pending list can be
1899 * aborted. All the commands issued to the F/W must complete.
1902 megaraid_abort(Scsi_Cmnd
*cmd
)
1907 adapter
= (adapter_t
*)cmd
->device
->host
->hostdata
;
1909 rval
= megaraid_abort_and_reset(adapter
, cmd
, SCB_ABORT
);
1912 * This is required here to complete any completed requests
1913 * to be communicated over to the mid layer.
1915 mega_rundoneq(adapter
);
1922 megaraid_reset(struct scsi_cmnd
*cmd
)
1928 adapter
= (adapter_t
*)cmd
->device
->host
->hostdata
;
1930 #if MEGA_HAVE_CLUSTERING
1931 mc
.cmd
= MEGA_CLUSTER_CMD
;
1932 mc
.opcode
= MEGA_RESET_RESERVATIONS
;
1934 if( mega_internal_command(adapter
, &mc
, NULL
) != 0 ) {
1936 "megaraid: reservation reset failed.\n");
1939 printk(KERN_INFO
"megaraid: reservation reset.\n");
1943 spin_lock_irq(&adapter
->lock
);
1945 rval
= megaraid_abort_and_reset(adapter
, cmd
, SCB_RESET
);
1948 * This is required here to complete any completed requests
1949 * to be communicated over to the mid layer.
1951 mega_rundoneq(adapter
);
1952 spin_unlock_irq(&adapter
->lock
);
1958 * megaraid_abort_and_reset()
1959 * @adapter - megaraid soft state
1960 * @cmd - scsi command to be aborted or reset
1961 * @aor - abort or reset flag
1963 * Try to locate the scsi command in the pending queue. If found and is not
1964 * issued to the controller, abort/reset it. Otherwise return failure
1967 megaraid_abort_and_reset(adapter_t
*adapter
, Scsi_Cmnd
*cmd
, int aor
)
1969 struct list_head
*pos
, *next
;
1972 printk(KERN_WARNING
"megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
1973 (aor
== SCB_ABORT
)? "ABORTING":"RESET", cmd
->serial_number
,
1974 cmd
->cmnd
[0], cmd
->device
->channel
,
1975 cmd
->device
->id
, cmd
->device
->lun
);
1977 if(list_empty(&adapter
->pending_list
))
1980 list_for_each_safe(pos
, next
, &adapter
->pending_list
) {
1982 scb
= list_entry(pos
, scb_t
, list
);
1984 if (scb
->cmd
== cmd
) { /* Found command */
1989 * Check if this command has firmare owenership. If
1990 * yes, we cannot reset this command. Whenever, f/w
1991 * completes this command, we will return appropriate
1994 if( scb
->state
& SCB_ISSUED
) {
1997 "megaraid: %s-%lx[%x], fw owner.\n",
1998 (aor
==SCB_ABORT
) ? "ABORTING":"RESET",
1999 cmd
->serial_number
, scb
->idx
);
2006 * Not yet issued! Remove from the pending
2010 "megaraid: %s-%lx[%x], driver owner.\n",
2011 (aor
==SCB_ABORT
) ? "ABORTING":"RESET",
2012 cmd
->serial_number
, scb
->idx
);
2014 mega_free_scb(adapter
, scb
);
2016 if( aor
== SCB_ABORT
) {
2017 cmd
->result
= (DID_ABORT
<< 16);
2020 cmd
->result
= (DID_RESET
<< 16);
2023 list_add_tail(SCSI_LIST(cmd
),
2024 &adapter
->completed_list
);
2035 make_local_pdev(adapter_t
*adapter
, struct pci_dev
**pdev
)
2037 *pdev
= alloc_pci_dev();
2039 if( *pdev
== NULL
) return -1;
2041 memcpy(*pdev
, adapter
->dev
, sizeof(struct pci_dev
));
2043 if( pci_set_dma_mask(*pdev
, DMA_32BIT_MASK
) != 0 ) {
2052 free_local_pdev(struct pci_dev
*pdev
)
2058 * mega_allocate_inquiry()
2059 * @dma_handle - handle returned for dma address
2060 * @pdev - handle to pci device
2062 * allocates memory for inquiry structure
2064 static inline void *
2065 mega_allocate_inquiry(dma_addr_t
*dma_handle
, struct pci_dev
*pdev
)
2067 return pci_alloc_consistent(pdev
, sizeof(mega_inquiry3
), dma_handle
);
2072 mega_free_inquiry(void *inquiry
, dma_addr_t dma_handle
, struct pci_dev
*pdev
)
2074 pci_free_consistent(pdev
, sizeof(mega_inquiry3
), inquiry
, dma_handle
);
2078 #ifdef CONFIG_PROC_FS
2079 /* Following code handles /proc fs */
2081 #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
2082 S_IRUSR | S_IFREG, \
2083 controller_proc_dir_entry, \
2087 * mega_create_proc_entry()
2088 * @index - index in soft state array
2089 * @parent - parent node for this /proc entry
2091 * Creates /proc entries for our controllers.
2094 mega_create_proc_entry(int index
, struct proc_dir_entry
*parent
)
2096 struct proc_dir_entry
*controller_proc_dir_entry
= NULL
;
2097 u8 string
[64] = { 0 };
2098 adapter_t
*adapter
= hba_soft_state
[index
];
2100 sprintf(string
, "hba%d", adapter
->host
->host_no
);
2102 controller_proc_dir_entry
=
2103 adapter
->controller_proc_dir_entry
= proc_mkdir(string
, parent
);
2105 if(!controller_proc_dir_entry
) {
2106 printk(KERN_WARNING
"\nmegaraid: proc_mkdir failed\n");
2109 adapter
->proc_read
= CREATE_READ_PROC("config", proc_read_config
);
2110 adapter
->proc_stat
= CREATE_READ_PROC("stat", proc_read_stat
);
2111 adapter
->proc_mbox
= CREATE_READ_PROC("mailbox", proc_read_mbox
);
2112 #if MEGA_HAVE_ENH_PROC
2113 adapter
->proc_rr
= CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate
);
2114 adapter
->proc_battery
= CREATE_READ_PROC("battery-status",
2118 * Display each physical drive on its channel
2120 adapter
->proc_pdrvstat
[0] = CREATE_READ_PROC("diskdrives-ch0",
2122 adapter
->proc_pdrvstat
[1] = CREATE_READ_PROC("diskdrives-ch1",
2124 adapter
->proc_pdrvstat
[2] = CREATE_READ_PROC("diskdrives-ch2",
2126 adapter
->proc_pdrvstat
[3] = CREATE_READ_PROC("diskdrives-ch3",
2130 * Display a set of up to 10 logical drive through each of following
2133 adapter
->proc_rdrvstat
[0] = CREATE_READ_PROC("raiddrives-0-9",
2135 adapter
->proc_rdrvstat
[1] = CREATE_READ_PROC("raiddrives-10-19",
2137 adapter
->proc_rdrvstat
[2] = CREATE_READ_PROC("raiddrives-20-29",
2139 adapter
->proc_rdrvstat
[3] = CREATE_READ_PROC("raiddrives-30-39",
2146 * proc_read_config()
2147 * @page - buffer to write the data in
2148 * @start - where the actual data has been written in page
2149 * @offset - same meaning as the read system call
2150 * @count - same meaning as the read system call
2151 * @eof - set if no more data needs to be returned
2152 * @data - pointer to our soft state
2154 * Display configuration information about the controller.
2157 proc_read_config(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2161 adapter_t
*adapter
= (adapter_t
*)data
;
2164 len
+= sprintf(page
+len
, "%s", MEGARAID_VERSION
);
2166 if(adapter
->product_info
.product_name
[0])
2167 len
+= sprintf(page
+len
, "%s\n",
2168 adapter
->product_info
.product_name
);
2170 len
+= sprintf(page
+len
, "Controller Type: ");
2172 if( adapter
->flag
& BOARD_MEMMAP
) {
2173 len
+= sprintf(page
+len
,
2174 "438/466/467/471/493/518/520/531/532\n");
2177 len
+= sprintf(page
+len
,
2181 if(adapter
->flag
& BOARD_40LD
) {
2182 len
+= sprintf(page
+len
,
2183 "Controller Supports 40 Logical Drives\n");
2186 if(adapter
->flag
& BOARD_64BIT
) {
2187 len
+= sprintf(page
+len
,
2188 "Controller capable of 64-bit memory addressing\n");
2190 if( adapter
->has_64bit_addr
) {
2191 len
+= sprintf(page
+len
,
2192 "Controller using 64-bit memory addressing\n");
2195 len
+= sprintf(page
+len
,
2196 "Controller is not using 64-bit memory addressing\n");
2199 len
+= sprintf(page
+len
, "Base = %08lx, Irq = %d, ", adapter
->base
,
2200 adapter
->host
->irq
);
2202 len
+= sprintf(page
+len
, "Logical Drives = %d, Channels = %d\n",
2203 adapter
->numldrv
, adapter
->product_info
.nchannels
);
2205 len
+= sprintf(page
+len
, "Version =%s:%s, DRAM = %dMb\n",
2206 adapter
->fw_version
, adapter
->bios_version
,
2207 adapter
->product_info
.dram_size
);
2209 len
+= sprintf(page
+len
,
2210 "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2211 adapter
->product_info
.max_commands
, adapter
->max_cmds
);
2213 len
+= sprintf(page
+len
, "support_ext_cdb = %d\n",
2214 adapter
->support_ext_cdb
);
2215 len
+= sprintf(page
+len
, "support_random_del = %d\n",
2216 adapter
->support_random_del
);
2217 len
+= sprintf(page
+len
, "boot_ldrv_enabled = %d\n",
2218 adapter
->boot_ldrv_enabled
);
2219 len
+= sprintf(page
+len
, "boot_ldrv = %d\n",
2220 adapter
->boot_ldrv
);
2221 len
+= sprintf(page
+len
, "boot_pdrv_enabled = %d\n",
2222 adapter
->boot_pdrv_enabled
);
2223 len
+= sprintf(page
+len
, "boot_pdrv_ch = %d\n",
2224 adapter
->boot_pdrv_ch
);
2225 len
+= sprintf(page
+len
, "boot_pdrv_tgt = %d\n",
2226 adapter
->boot_pdrv_tgt
);
2227 len
+= sprintf(page
+len
, "quiescent = %d\n",
2228 atomic_read(&adapter
->quiescent
));
2229 len
+= sprintf(page
+len
, "has_cluster = %d\n",
2230 adapter
->has_cluster
);
2232 len
+= sprintf(page
+len
, "\nModule Parameters:\n");
2233 len
+= sprintf(page
+len
, "max_cmd_per_lun = %d\n",
2235 len
+= sprintf(page
+len
, "max_sectors_per_io = %d\n",
2236 max_sectors_per_io
);
2247 * @page - buffer to write the data in
2248 * @start - where the actual data has been written in page
2249 * @offset - same meaning as the read system call
2250 * @count - same meaning as the read system call
2251 * @eof - set if no more data needs to be returned
2252 * @data - pointer to our soft state
2254 * Diaplay statistical information about the I/O activity.
2257 proc_read_stat(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2264 i
= 0; /* avoid compilation warnings */
2266 adapter
= (adapter_t
*)data
;
2268 len
= sprintf(page
, "Statistical Information for this controller\n");
2269 len
+= sprintf(page
+len
, "pend_cmds = %d\n",
2270 atomic_read(&adapter
->pend_cmds
));
2272 for(i
= 0; i
< adapter
->numldrv
; i
++) {
2273 len
+= sprintf(page
+len
, "Logical Drive %d:\n", i
);
2275 len
+= sprintf(page
+len
,
2276 "\tReads Issued = %lu, Writes Issued = %lu\n",
2277 adapter
->nreads
[i
], adapter
->nwrites
[i
]);
2279 len
+= sprintf(page
+len
,
2280 "\tSectors Read = %lu, Sectors Written = %lu\n",
2281 adapter
->nreadblocks
[i
], adapter
->nwriteblocks
[i
]);
2283 len
+= sprintf(page
+len
,
2284 "\tRead errors = %lu, Write errors = %lu\n\n",
2285 adapter
->rd_errors
[i
], adapter
->wr_errors
[i
]);
2288 len
+= sprintf(page
+len
,
2289 "IO and error counters not compiled in driver.\n");
2300 * @page - buffer to write the data in
2301 * @start - where the actual data has been written in page
2302 * @offset - same meaning as the read system call
2303 * @count - same meaning as the read system call
2304 * @eof - set if no more data needs to be returned
2305 * @data - pointer to our soft state
2307 * Display mailbox information for the last command issued. This information
2308 * is good for debugging.
2311 proc_read_mbox(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2315 adapter_t
*adapter
= (adapter_t
*)data
;
2316 volatile mbox_t
*mbox
= adapter
->mbox
;
2319 len
= sprintf(page
, "Contents of Mail Box Structure\n");
2320 len
+= sprintf(page
+len
, " Fw Command = 0x%02x\n",
2322 len
+= sprintf(page
+len
, " Cmd Sequence = 0x%02x\n",
2324 len
+= sprintf(page
+len
, " No of Sectors= %04d\n",
2325 mbox
->m_out
.numsectors
);
2326 len
+= sprintf(page
+len
, " LBA = 0x%02x\n",
2328 len
+= sprintf(page
+len
, " DTA = 0x%08x\n",
2329 mbox
->m_out
.xferaddr
);
2330 len
+= sprintf(page
+len
, " Logical Drive= 0x%02x\n",
2331 mbox
->m_out
.logdrv
);
2332 len
+= sprintf(page
+len
, " No of SG Elmt= 0x%02x\n",
2333 mbox
->m_out
.numsgelements
);
2334 len
+= sprintf(page
+len
, " Busy = %01x\n",
2336 len
+= sprintf(page
+len
, " Status = 0x%02x\n",
2346 * proc_rebuild_rate()
2347 * @page - buffer to write the data in
2348 * @start - where the actual data has been written in page
2349 * @offset - same meaning as the read system call
2350 * @count - same meaning as the read system call
2351 * @eof - set if no more data needs to be returned
2352 * @data - pointer to our soft state
2354 * Display current rebuild rate
2357 proc_rebuild_rate(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2360 adapter_t
*adapter
= (adapter_t
*)data
;
2361 dma_addr_t dma_handle
;
2363 struct pci_dev
*pdev
;
2366 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2371 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2372 free_local_pdev(pdev
);
2377 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2379 len
= sprintf(page
, "Adapter inquiry failed.\n");
2381 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2383 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2385 free_local_pdev(pdev
);
2392 if( adapter
->flag
& BOARD_40LD
) {
2393 len
= sprintf(page
, "Rebuild Rate: [%d%%]\n",
2394 ((mega_inquiry3
*)inquiry
)->rebuild_rate
);
2397 len
= sprintf(page
, "Rebuild Rate: [%d%%]\n",
2398 ((mraid_ext_inquiry
*)
2399 inquiry
)->raid_inq
.adapter_info
.rebuild_rate
);
2403 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2405 free_local_pdev(pdev
);
2415 * @page - buffer to write the data in
2416 * @start - where the actual data has been written in page
2417 * @offset - same meaning as the read system call
2418 * @count - same meaning as the read system call
2419 * @eof - set if no more data needs to be returned
2420 * @data - pointer to our soft state
2422 * Display information about the battery module on the controller.
2425 proc_battery(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2428 adapter_t
*adapter
= (adapter_t
*)data
;
2429 dma_addr_t dma_handle
;
2431 struct pci_dev
*pdev
;
2432 u8 battery_status
= 0;
2436 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2441 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2442 free_local_pdev(pdev
);
2447 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2449 len
= sprintf(page
, "Adapter inquiry failed.\n");
2451 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2453 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2455 free_local_pdev(pdev
);
2462 if( adapter
->flag
& BOARD_40LD
) {
2463 battery_status
= ((mega_inquiry3
*)inquiry
)->battery_status
;
2466 battery_status
= ((mraid_ext_inquiry
*)inquiry
)->
2467 raid_inq
.adapter_info
.battery_status
;
2471 * Decode the battery status
2473 sprintf(str
, "Battery Status:[%d]", battery_status
);
2475 if(battery_status
== MEGA_BATT_CHARGE_DONE
)
2476 strcat(str
, " Charge Done");
2478 if(battery_status
& MEGA_BATT_MODULE_MISSING
)
2479 strcat(str
, " Module Missing");
2481 if(battery_status
& MEGA_BATT_LOW_VOLTAGE
)
2482 strcat(str
, " Low Voltage");
2484 if(battery_status
& MEGA_BATT_TEMP_HIGH
)
2485 strcat(str
, " Temperature High");
2487 if(battery_status
& MEGA_BATT_PACK_MISSING
)
2488 strcat(str
, " Pack Missing");
2490 if(battery_status
& MEGA_BATT_CHARGE_INPROG
)
2491 strcat(str
, " Charge In-progress");
2493 if(battery_status
& MEGA_BATT_CHARGE_FAIL
)
2494 strcat(str
, " Charge Fail");
2496 if(battery_status
& MEGA_BATT_CYCLES_EXCEEDED
)
2497 strcat(str
, " Cycles Exceeded");
2499 len
= sprintf(page
, "%s\n", str
);
2502 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2504 free_local_pdev(pdev
);
2514 * @page - buffer to write the data in
2515 * @start - where the actual data has been written in page
2516 * @offset - same meaning as the read system call
2517 * @count - same meaning as the read system call
2518 * @eof - set if no more data needs to be returned
2519 * @data - pointer to our soft state
2521 * Display information about the physical drives on physical channel 0.
2524 proc_pdrv_ch0(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2527 adapter_t
*adapter
= (adapter_t
*)data
;
2531 return (proc_pdrv(adapter
, page
, 0));
2537 * @page - buffer to write the data in
2538 * @start - where the actual data has been written in page
2539 * @offset - same meaning as the read system call
2540 * @count - same meaning as the read system call
2541 * @eof - set if no more data needs to be returned
2542 * @data - pointer to our soft state
2544 * Display information about the physical drives on physical channel 1.
2547 proc_pdrv_ch1(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2550 adapter_t
*adapter
= (adapter_t
*)data
;
2554 return (proc_pdrv(adapter
, page
, 1));
2560 * @page - buffer to write the data in
2561 * @start - where the actual data has been written in page
2562 * @offset - same meaning as the read system call
2563 * @count - same meaning as the read system call
2564 * @eof - set if no more data needs to be returned
2565 * @data - pointer to our soft state
2567 * Display information about the physical drives on physical channel 2.
2570 proc_pdrv_ch2(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2573 adapter_t
*adapter
= (adapter_t
*)data
;
2577 return (proc_pdrv(adapter
, page
, 2));
2583 * @page - buffer to write the data in
2584 * @start - where the actual data has been written in page
2585 * @offset - same meaning as the read system call
2586 * @count - same meaning as the read system call
2587 * @eof - set if no more data needs to be returned
2588 * @data - pointer to our soft state
2590 * Display information about the physical drives on physical channel 3.
2593 proc_pdrv_ch3(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2596 adapter_t
*adapter
= (adapter_t
*)data
;
2600 return (proc_pdrv(adapter
, page
, 3));
2606 * @page - buffer to write the data in
2607 * @adapter - pointer to our soft state
2609 * Display information about the physical drives.
2612 proc_pdrv(adapter_t
*adapter
, char *page
, int channel
)
2614 dma_addr_t dma_handle
;
2616 dma_addr_t scsi_inq_dma_handle
;
2618 struct pci_dev
*pdev
;
2627 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2631 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2635 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2636 len
= sprintf(page
, "Adapter inquiry failed.\n");
2638 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2644 scsi_inq
= pci_alloc_consistent(pdev
, 256, &scsi_inq_dma_handle
);
2646 if( scsi_inq
== NULL
) {
2647 len
= sprintf(page
, "memory not available for scsi inq.\n");
2652 if( adapter
->flag
& BOARD_40LD
) {
2653 pdrv_state
= ((mega_inquiry3
*)inquiry
)->pdrv_state
;
2656 pdrv_state
= ((mraid_ext_inquiry
*)inquiry
)->
2657 raid_inq
.pdrv_info
.pdrv_state
;
2660 max_channels
= adapter
->product_info
.nchannels
;
2662 if( channel
>= max_channels
) {
2666 for( tgt
= 0; tgt
<= MAX_TARGET
; tgt
++ ) {
2668 i
= channel
*16 + tgt
;
2670 state
= *(pdrv_state
+ i
);
2672 switch( state
& 0x0F ) {
2676 "Channel:%2d Id:%2d State: Online",
2682 "Channel:%2d Id:%2d State: Failed",
2688 "Channel:%2d Id:%2d State: Rebuild",
2694 "Channel:%2d Id:%2d State: Hot spare",
2700 "Channel:%2d Id:%2d State: Un-configured",
2707 * This interface displays inquiries for disk drives
2708 * only. Inquries for logical drives and non-disk
2709 * devices are available through /proc/scsi/scsi
2711 memset(scsi_inq
, 0, 256);
2712 if( mega_internal_dev_inquiry(adapter
, channel
, tgt
,
2713 scsi_inq_dma_handle
) ||
2714 (scsi_inq
[0] & 0x1F) != TYPE_DISK
) {
2719 * Check for overflow. We print less than 240
2720 * characters for inquiry
2722 if( (len
+ 240) >= PAGE_SIZE
) break;
2724 len
+= sprintf(page
+len
, "%s.\n", str
);
2726 len
+= mega_print_inquiry(page
+len
, scsi_inq
);
2730 pci_free_consistent(pdev
, 256, scsi_inq
, scsi_inq_dma_handle
);
2732 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2734 free_local_pdev(pdev
);
2741 * Display scsi inquiry
2744 mega_print_inquiry(char *page
, char *scsi_inq
)
2749 len
= sprintf(page
, " Vendor: ");
2750 for( i
= 8; i
< 16; i
++ ) {
2751 len
+= sprintf(page
+len
, "%c", scsi_inq
[i
]);
2754 len
+= sprintf(page
+len
, " Model: ");
2756 for( i
= 16; i
< 32; i
++ ) {
2757 len
+= sprintf(page
+len
, "%c", scsi_inq
[i
]);
2760 len
+= sprintf(page
+len
, " Rev: ");
2762 for( i
= 32; i
< 36; i
++ ) {
2763 len
+= sprintf(page
+len
, "%c", scsi_inq
[i
]);
2766 len
+= sprintf(page
+len
, "\n");
2768 i
= scsi_inq
[0] & 0x1f;
2770 len
+= sprintf(page
+len
, " Type: %s ", scsi_device_type(i
));
2772 len
+= sprintf(page
+len
,
2773 " ANSI SCSI revision: %02x", scsi_inq
[2] & 0x07);
2775 if( (scsi_inq
[2] & 0x07) == 1 && (scsi_inq
[3] & 0x0f) == 1 )
2776 len
+= sprintf(page
+len
, " CCS\n");
2778 len
+= sprintf(page
+len
, "\n");
2786 * @page - buffer to write the data in
2787 * @start - where the actual data has been written in page
2788 * @offset - same meaning as the read system call
2789 * @count - same meaning as the read system call
2790 * @eof - set if no more data needs to be returned
2791 * @data - pointer to our soft state
2793 * Display real time information about the logical drives 0 through 9.
2796 proc_rdrv_10(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2799 adapter_t
*adapter
= (adapter_t
*)data
;
2803 return (proc_rdrv(adapter
, page
, 0, 9));
2809 * @page - buffer to write the data in
2810 * @start - where the actual data has been written in page
2811 * @offset - same meaning as the read system call
2812 * @count - same meaning as the read system call
2813 * @eof - set if no more data needs to be returned
2814 * @data - pointer to our soft state
2816 * Display real time information about the logical drives 0 through 9.
2819 proc_rdrv_20(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2822 adapter_t
*adapter
= (adapter_t
*)data
;
2826 return (proc_rdrv(adapter
, page
, 10, 19));
2832 * @page - buffer to write the data in
2833 * @start - where the actual data has been written in page
2834 * @offset - same meaning as the read system call
2835 * @count - same meaning as the read system call
2836 * @eof - set if no more data needs to be returned
2837 * @data - pointer to our soft state
2839 * Display real time information about the logical drives 0 through 9.
2842 proc_rdrv_30(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2845 adapter_t
*adapter
= (adapter_t
*)data
;
2849 return (proc_rdrv(adapter
, page
, 20, 29));
2855 * @page - buffer to write the data in
2856 * @start - where the actual data has been written in page
2857 * @offset - same meaning as the read system call
2858 * @count - same meaning as the read system call
2859 * @eof - set if no more data needs to be returned
2860 * @data - pointer to our soft state
2862 * Display real time information about the logical drives 0 through 9.
2865 proc_rdrv_40(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2868 adapter_t
*adapter
= (adapter_t
*)data
;
2872 return (proc_rdrv(adapter
, page
, 30, 39));
2878 * @page - buffer to write the data in
2879 * @adapter - pointer to our soft state
2880 * @start - starting logical drive to display
2881 * @end - ending logical drive to display
2883 * We do not print the inquiry information since its already available through
2884 * /proc/scsi/scsi interface
2887 proc_rdrv(adapter_t
*adapter
, char *page
, int start
, int end
)
2889 dma_addr_t dma_handle
;
2890 logdrv_param
*lparam
;
2893 dma_addr_t disk_array_dma_handle
;
2895 struct pci_dev
*pdev
;
2902 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2906 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2907 free_local_pdev(pdev
);
2911 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2913 len
= sprintf(page
, "Adapter inquiry failed.\n");
2915 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2917 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2919 free_local_pdev(pdev
);
2924 memset(&mc
, 0, sizeof(megacmd_t
));
2926 if( adapter
->flag
& BOARD_40LD
) {
2927 array_sz
= sizeof(disk_array_40ld
);
2929 rdrv_state
= ((mega_inquiry3
*)inquiry
)->ldrv_state
;
2931 num_ldrv
= ((mega_inquiry3
*)inquiry
)->num_ldrv
;
2934 array_sz
= sizeof(disk_array_8ld
);
2936 rdrv_state
= ((mraid_ext_inquiry
*)inquiry
)->
2937 raid_inq
.logdrv_info
.ldrv_state
;
2939 num_ldrv
= ((mraid_ext_inquiry
*)inquiry
)->
2940 raid_inq
.logdrv_info
.num_ldrv
;
2943 disk_array
= pci_alloc_consistent(pdev
, array_sz
,
2944 &disk_array_dma_handle
);
2946 if( disk_array
== NULL
) {
2947 len
= sprintf(page
, "memory not available.\n");
2949 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2951 free_local_pdev(pdev
);
2956 mc
.xferaddr
= (u32
)disk_array_dma_handle
;
2958 if( adapter
->flag
& BOARD_40LD
) {
2959 mc
.cmd
= FC_NEW_CONFIG
;
2960 mc
.opcode
= OP_DCMD_READ_CONFIG
;
2962 if( mega_internal_command(adapter
, &mc
, NULL
) ) {
2964 len
= sprintf(page
, "40LD read config failed.\n");
2966 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2968 pci_free_consistent(pdev
, array_sz
, disk_array
,
2969 disk_array_dma_handle
);
2971 free_local_pdev(pdev
);
2978 mc
.cmd
= NEW_READ_CONFIG_8LD
;
2980 if( mega_internal_command(adapter
, &mc
, NULL
) ) {
2982 mc
.cmd
= READ_CONFIG_8LD
;
2984 if( mega_internal_command(adapter
, &mc
,
2988 "8LD read config failed.\n");
2990 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2992 pci_free_consistent(pdev
, array_sz
,
2994 disk_array_dma_handle
);
2996 free_local_pdev(pdev
);
3003 for( i
= start
; i
< ( (end
+1 < num_ldrv
) ? end
+1 : num_ldrv
); i
++ ) {
3005 if( adapter
->flag
& BOARD_40LD
) {
3007 &((disk_array_40ld
*)disk_array
)->ldrv
[i
].lparam
;
3011 &((disk_array_8ld
*)disk_array
)->ldrv
[i
].lparam
;
3015 * Check for overflow. We print less than 240 characters for
3016 * information about each logical drive.
3018 if( (len
+ 240) >= PAGE_SIZE
) break;
3020 len
+= sprintf(page
+len
, "Logical drive:%2d:, ", i
);
3022 switch( rdrv_state
[i
] & 0x0F ) {
3024 len
+= sprintf(page
+len
, "state: offline");
3028 len
+= sprintf(page
+len
, "state: degraded");
3032 len
+= sprintf(page
+len
, "state: optimal");
3036 len
+= sprintf(page
+len
, "state: deleted");
3040 len
+= sprintf(page
+len
, "state: unknown");
3045 * Check if check consistency or initialization is going on
3046 * for this logical drive.
3048 if( (rdrv_state
[i
] & 0xF0) == 0x20 ) {
3049 len
+= sprintf(page
+len
,
3050 ", check-consistency in progress");
3052 else if( (rdrv_state
[i
] & 0xF0) == 0x10 ) {
3053 len
+= sprintf(page
+len
,
3054 ", initialization in progress");
3057 len
+= sprintf(page
+len
, "\n");
3059 len
+= sprintf(page
+len
, "Span depth:%3d, ",
3060 lparam
->span_depth
);
3062 len
+= sprintf(page
+len
, "RAID level:%3d, ",
3065 len
+= sprintf(page
+len
, "Stripe size:%3d, ",
3066 lparam
->stripe_sz
? lparam
->stripe_sz
/2: 128);
3068 len
+= sprintf(page
+len
, "Row size:%3d\n",
3072 len
+= sprintf(page
+len
, "Read Policy: ");
3074 switch(lparam
->read_ahead
) {
3077 len
+= sprintf(page
+len
, "No read ahead, ");
3081 len
+= sprintf(page
+len
, "Read ahead, ");
3084 case ADAP_READ_AHEAD
:
3085 len
+= sprintf(page
+len
, "Adaptive, ");
3090 len
+= sprintf(page
+len
, "Write Policy: ");
3092 switch(lparam
->write_mode
) {
3094 case WRMODE_WRITE_THRU
:
3095 len
+= sprintf(page
+len
, "Write thru, ");
3098 case WRMODE_WRITE_BACK
:
3099 len
+= sprintf(page
+len
, "Write back, ");
3103 len
+= sprintf(page
+len
, "Cache Policy: ");
3105 switch(lparam
->direct_io
) {
3108 len
+= sprintf(page
+len
, "Cached IO\n\n");
3112 len
+= sprintf(page
+len
, "Direct IO\n\n");
3117 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
3119 pci_free_consistent(pdev
, array_sz
, disk_array
,
3120 disk_array_dma_handle
);
3122 free_local_pdev(pdev
);
3127 static inline void mega_create_proc_entry(int index
, struct proc_dir_entry
*parent
)
3134 * megaraid_biosparam()
3136 * Return the disk geometry for a particular disk
3139 megaraid_biosparam(struct scsi_device
*sdev
, struct block_device
*bdev
,
3140 sector_t capacity
, int geom
[])
3149 /* Get pointer to host config structure */
3150 adapter
= (adapter_t
*)sdev
->host
->hostdata
;
3152 if (IS_RAID_CH(adapter
, sdev
->channel
)) {
3153 /* Default heads (64) & sectors (32) */
3156 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3159 * Handle extended translation size for logical drives
3162 if ((ulong
)capacity
>= 0x200000) {
3165 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3171 geom
[2] = cylinders
;
3174 bh
= scsi_bios_ptable(bdev
);
3177 rval
= scsi_partsize(bh
, capacity
,
3178 &geom
[2], &geom
[0], &geom
[1]);
3185 "megaraid: invalid partition on this disk on channel %d\n",
3188 /* Default heads (64) & sectors (32) */
3191 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3193 /* Handle extended translation size for logical drives > 1Gb */
3194 if ((ulong
)capacity
>= 0x200000) {
3197 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3203 geom
[2] = cylinders
;
3211 * @adapter - pointer to our soft state
3213 * Allocate memory for the various pointers in the scb structures:
3214 * scatter-gather list pointer, passthru and extended passthru structure
3218 mega_init_scb(adapter_t
*adapter
)
3223 for( i
= 0; i
< adapter
->max_cmds
; i
++ ) {
3225 scb
= &adapter
->scb_list
[i
];
3233 for( i
= 0; i
< adapter
->max_cmds
; i
++ ) {
3235 scb
= &adapter
->scb_list
[i
];
3239 scb
->sgl64
= pci_alloc_consistent(adapter
->dev
,
3240 sizeof(mega_sgl64
) * adapter
->sglen
,
3241 &scb
->sgl_dma_addr
);
3243 scb
->sgl
= (mega_sglist
*)scb
->sgl64
;
3246 printk(KERN_WARNING
"RAID: Can't allocate sglist.\n");
3247 mega_free_sgl(adapter
);
3251 scb
->pthru
= pci_alloc_consistent(adapter
->dev
,
3252 sizeof(mega_passthru
),
3253 &scb
->pthru_dma_addr
);
3256 printk(KERN_WARNING
"RAID: Can't allocate passthru.\n");
3257 mega_free_sgl(adapter
);
3261 scb
->epthru
= pci_alloc_consistent(adapter
->dev
,
3262 sizeof(mega_ext_passthru
),
3263 &scb
->epthru_dma_addr
);
3265 if( !scb
->epthru
) {
3267 "Can't allocate extended passthru.\n");
3268 mega_free_sgl(adapter
);
3273 scb
->dma_type
= MEGA_DMA_TYPE_NONE
;
3277 * lock not required since we are loading the driver, so no
3278 * commands possible right now.
3280 scb
->state
= SCB_FREE
;
3282 list_add(&scb
->list
, &adapter
->free_list
);
3294 * Routines for the character/ioctl interface to the driver. Find out if this
3295 * is a valid open. If yes, increment the module use count so that it cannot
3299 megadev_open (struct inode
*inode
, struct file
*filep
)
3302 * Only allow superuser to access private ioctl interface
3304 if( !capable(CAP_SYS_ADMIN
) ) return -EACCES
;
3312 * @inode - Our device inode
3314 * @cmd - ioctl command
3315 * @arg - user buffer
3317 * ioctl entry point for our private ioctl interface. We move the data in from
3318 * the user space, prepare the command (if necessary, convert the old MIMD
3319 * ioctl to new ioctl command), and issue a synchronous command to the
3323 megadev_ioctl(struct inode
*inode
, struct file
*filep
, unsigned int cmd
,
3330 mega_passthru __user
*upthru
; /* user address for passthru */
3331 mega_passthru
*pthru
; /* copy user passthru here */
3332 dma_addr_t pthru_dma_hndl
;
3333 void *data
= NULL
; /* data to be transferred */
3334 dma_addr_t data_dma_hndl
; /* dma handle for data xfer area */
3336 megastat_t __user
*ustats
;
3339 struct pci_dev
*pdev
;
3341 ustats
= NULL
; /* avoid compilation warnings */
3345 * Make sure only USCSICMD are issued through this interface.
3346 * MIMD application would still fire different command.
3348 if( (_IOC_TYPE(cmd
) != MEGAIOC_MAGIC
) && (cmd
!= USCSICMD
) ) {
3353 * Check and convert a possible MIMD command to NIT command.
3354 * mega_m_to_n() copies the data from the user space, so we do not
3355 * have to do it here.
3356 * NOTE: We will need some user address to copyout the data, therefore
3357 * the inteface layer will also provide us with the required user
3360 memset(&uioc
, 0, sizeof(nitioctl_t
));
3361 if( (rval
= mega_m_to_n( (void __user
*)arg
, &uioc
)) != 0 )
3365 switch( uioc
.opcode
) {
3367 case GET_DRIVER_VER
:
3368 if( put_user(driver_ver
, (u32 __user
*)uioc
.uioc_uaddr
) )
3374 if( put_user(hba_count
, (u32 __user
*)uioc
.uioc_uaddr
) )
3378 * Shucks. MIMD interface returns a positive value for number
3379 * of adapters. TODO: Change it to return 0 when there is no
3380 * applicatio using mimd interface.
3389 if( (adapno
= GETADAP(uioc
.adapno
)) >= hba_count
)
3392 if( copy_to_user(uioc
.uioc_uaddr
, mcontroller
+adapno
,
3393 sizeof(struct mcontroller
)) )
3403 if( (adapno
= GETADAP(uioc
.adapno
)) >= hba_count
)
3406 adapter
= hba_soft_state
[adapno
];
3408 ustats
= uioc
.uioc_uaddr
;
3410 if( copy_from_user(&num_ldrv
, &ustats
->num_ldrv
, sizeof(int)) )
3414 * Check for the validity of the logical drive number
3416 if( num_ldrv
>= MAX_LOGICAL_DRIVES_40LD
) return -EINVAL
;
3418 if( copy_to_user(ustats
->nreads
, adapter
->nreads
,
3419 num_ldrv
*sizeof(u32
)) )
3422 if( copy_to_user(ustats
->nreadblocks
, adapter
->nreadblocks
,
3423 num_ldrv
*sizeof(u32
)) )
3426 if( copy_to_user(ustats
->nwrites
, adapter
->nwrites
,
3427 num_ldrv
*sizeof(u32
)) )
3430 if( copy_to_user(ustats
->nwriteblocks
, adapter
->nwriteblocks
,
3431 num_ldrv
*sizeof(u32
)) )
3434 if( copy_to_user(ustats
->rd_errors
, adapter
->rd_errors
,
3435 num_ldrv
*sizeof(u32
)) )
3438 if( copy_to_user(ustats
->wr_errors
, adapter
->wr_errors
,
3439 num_ldrv
*sizeof(u32
)) )
3450 if( (adapno
= GETADAP(uioc
.adapno
)) >= hba_count
)
3453 adapter
= hba_soft_state
[adapno
];
3456 * Deletion of logical drive is a special case. The adapter
3457 * should be quiescent before this command is issued.
3459 if( uioc
.uioc_rmbox
[0] == FC_DEL_LOGDRV
&&
3460 uioc
.uioc_rmbox
[2] == OP_DEL_LOGDRV
) {
3463 * Do we support this feature
3465 if( !adapter
->support_random_del
) {
3466 printk(KERN_WARNING
"megaraid: logdrv ");
3467 printk("delete on non-supporting F/W.\n");
3472 rval
= mega_del_logdrv( adapter
, uioc
.uioc_rmbox
[3] );
3475 memset(&mc
, 0, sizeof(megacmd_t
));
3479 rval
= mega_n_to_m((void __user
*)arg
, &mc
);
3485 * This interface only support the regular passthru commands.
3486 * Reject extended passthru and 64-bit passthru
3488 if( uioc
.uioc_rmbox
[0] == MEGA_MBOXCMD_PASSTHRU64
||
3489 uioc
.uioc_rmbox
[0] == MEGA_MBOXCMD_EXTPTHRU
) {
3491 printk(KERN_WARNING
"megaraid: rejected passthru.\n");
3497 * For all internal commands, the buffer must be allocated in
3498 * <4GB address range
3500 if( make_local_pdev(adapter
, &pdev
) != 0 )
3503 /* Is it a passthru command or a DCMD */
3504 if( uioc
.uioc_rmbox
[0] == MEGA_MBOXCMD_PASSTHRU
) {
3505 /* Passthru commands */
3507 pthru
= pci_alloc_consistent(pdev
,
3508 sizeof(mega_passthru
),
3511 if( pthru
== NULL
) {
3512 free_local_pdev(pdev
);
3517 * The user passthru structure
3519 upthru
= (mega_passthru __user
*)(unsigned long)MBOX(uioc
)->xferaddr
;
3522 * Copy in the user passthru here.
3524 if( copy_from_user(pthru
, upthru
,
3525 sizeof(mega_passthru
)) ) {
3527 pci_free_consistent(pdev
,
3528 sizeof(mega_passthru
), pthru
,
3531 free_local_pdev(pdev
);
3537 * Is there a data transfer
3539 if( pthru
->dataxferlen
) {
3540 data
= pci_alloc_consistent(pdev
,
3544 if( data
== NULL
) {
3545 pci_free_consistent(pdev
,
3546 sizeof(mega_passthru
),
3550 free_local_pdev(pdev
);
3556 * Save the user address and point the kernel
3557 * address at just allocated memory
3559 uxferaddr
= pthru
->dataxferaddr
;
3560 pthru
->dataxferaddr
= data_dma_hndl
;
3565 * Is data coming down-stream
3567 if( pthru
->dataxferlen
&& (uioc
.flags
& UIOC_WR
) ) {
3571 if( copy_from_user(data
, (char __user
*)(unsigned long) uxferaddr
,
3572 pthru
->dataxferlen
) ) {
3574 goto freemem_and_return
;
3578 memset(&mc
, 0, sizeof(megacmd_t
));
3580 mc
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
3581 mc
.xferaddr
= (u32
)pthru_dma_hndl
;
3586 mega_internal_command(adapter
, &mc
, pthru
);
3588 rval
= mega_n_to_m((void __user
*)arg
, &mc
);
3590 if( rval
) goto freemem_and_return
;
3594 * Is data going up-stream
3596 if( pthru
->dataxferlen
&& (uioc
.flags
& UIOC_RD
) ) {
3597 if( copy_to_user((char __user
*)(unsigned long) uxferaddr
, data
,
3598 pthru
->dataxferlen
) ) {
3604 * Send the request sense data also, irrespective of
3605 * whether the user has asked for it or not.
3607 if (copy_to_user(upthru
->reqsensearea
,
3608 pthru
->reqsensearea
, 14))
3612 if( pthru
->dataxferlen
) {
3613 pci_free_consistent(pdev
,
3614 pthru
->dataxferlen
, data
,
3618 pci_free_consistent(pdev
, sizeof(mega_passthru
),
3619 pthru
, pthru_dma_hndl
);
3621 free_local_pdev(pdev
);
3629 * Is there a data transfer
3631 if( uioc
.xferlen
) {
3632 data
= pci_alloc_consistent(pdev
,
3633 uioc
.xferlen
, &data_dma_hndl
);
3635 if( data
== NULL
) {
3636 free_local_pdev(pdev
);
3640 uxferaddr
= MBOX(uioc
)->xferaddr
;
3644 * Is data coming down-stream
3646 if( uioc
.xferlen
&& (uioc
.flags
& UIOC_WR
) ) {
3650 if( copy_from_user(data
, (char __user
*)(unsigned long) uxferaddr
,
3653 pci_free_consistent(pdev
,
3655 data
, data_dma_hndl
);
3657 free_local_pdev(pdev
);
3663 memcpy(&mc
, MBOX(uioc
), sizeof(megacmd_t
));
3665 mc
.xferaddr
= (u32
)data_dma_hndl
;
3670 mega_internal_command(adapter
, &mc
, NULL
);
3672 rval
= mega_n_to_m((void __user
*)arg
, &mc
);
3675 if( uioc
.xferlen
) {
3676 pci_free_consistent(pdev
,
3681 free_local_pdev(pdev
);
3687 * Is data going up-stream
3689 if( uioc
.xferlen
&& (uioc
.flags
& UIOC_RD
) ) {
3690 if( copy_to_user((char __user
*)(unsigned long) uxferaddr
, data
,
3697 if( uioc
.xferlen
) {
3698 pci_free_consistent(pdev
,
3703 free_local_pdev(pdev
);
3717 * @arg - user address
3718 * @uioc - new ioctl structure
3720 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3723 * Converts the older mimd ioctl structure to newer NIT structure
3726 mega_m_to_n(void __user
*arg
, nitioctl_t
*uioc
)
3728 struct uioctl_t uioc_mimd
;
3729 char signature
[8] = {0};
3735 * check is the application conforms to NIT. We do not have to do much
3737 * We exploit the fact that the signature is stored in the very
3738 * begining of the structure.
3741 if( copy_from_user(signature
, arg
, 7) )
3744 if( memcmp(signature
, "MEGANIT", 7) == 0 ) {
3747 * NOTE NOTE: The nit ioctl is still under flux because of
3748 * change of mailbox definition, in HPE. No applications yet
3749 * use this interface and let's not have applications use this
3750 * interface till the new specifitions are in place.
3754 if( copy_from_user(uioc
, arg
, sizeof(nitioctl_t
)) )
3761 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3763 * Get the user ioctl structure
3765 if( copy_from_user(&uioc_mimd
, arg
, sizeof(struct uioctl_t
)) )
3770 * Get the opcode and subopcode for the commands
3772 opcode
= uioc_mimd
.ui
.fcs
.opcode
;
3773 subopcode
= uioc_mimd
.ui
.fcs
.subopcode
;
3778 switch (subopcode
) {
3780 case MEGAIOC_QDRVRVER
: /* Query driver version */
3781 uioc
->opcode
= GET_DRIVER_VER
;
3782 uioc
->uioc_uaddr
= uioc_mimd
.data
;
3785 case MEGAIOC_QNADAP
: /* Get # of adapters */
3786 uioc
->opcode
= GET_N_ADAP
;
3787 uioc
->uioc_uaddr
= uioc_mimd
.data
;
3790 case MEGAIOC_QADAPINFO
: /* Get adapter information */
3791 uioc
->opcode
= GET_ADAP_INFO
;
3792 uioc
->adapno
= uioc_mimd
.ui
.fcs
.adapno
;
3793 uioc
->uioc_uaddr
= uioc_mimd
.data
;
3805 uioc
->opcode
= MBOX_CMD
;
3806 uioc
->adapno
= uioc_mimd
.ui
.fcs
.adapno
;
3808 memcpy(uioc
->uioc_rmbox
, uioc_mimd
.mbox
, 18);
3810 uioc
->xferlen
= uioc_mimd
.ui
.fcs
.length
;
3812 if( uioc_mimd
.outlen
) uioc
->flags
= UIOC_RD
;
3813 if( uioc_mimd
.inlen
) uioc
->flags
|= UIOC_WR
;
3819 uioc
->opcode
= MBOX_CMD
;
3820 uioc
->adapno
= uioc_mimd
.ui
.fcs
.adapno
;
3822 memcpy(uioc
->uioc_rmbox
, uioc_mimd
.mbox
, 18);
3825 * Choose the xferlen bigger of input and output data
3827 uioc
->xferlen
= uioc_mimd
.outlen
> uioc_mimd
.inlen
?
3828 uioc_mimd
.outlen
: uioc_mimd
.inlen
;
3830 if( uioc_mimd
.outlen
) uioc
->flags
= UIOC_RD
;
3831 if( uioc_mimd
.inlen
) uioc
->flags
|= UIOC_WR
;
3845 * @arg - user address
3846 * @mc - mailbox command
3848 * Updates the status information to the application, depending on application
3849 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3852 mega_n_to_m(void __user
*arg
, megacmd_t
*mc
)
3854 nitioctl_t __user
*uiocp
;
3855 megacmd_t __user
*umc
;
3856 mega_passthru __user
*upthru
;
3857 struct uioctl_t __user
*uioc_mimd
;
3858 char signature
[8] = {0};
3861 * check is the application conforms to NIT.
3863 if( copy_from_user(signature
, arg
, 7) )
3866 if( memcmp(signature
, "MEGANIT", 7) == 0 ) {
3870 if( put_user(mc
->status
, (u8 __user
*)&MBOX_P(uiocp
)->status
) )
3873 if( mc
->cmd
== MEGA_MBOXCMD_PASSTHRU
) {
3875 umc
= MBOX_P(uiocp
);
3877 if (get_user(upthru
, (mega_passthru __user
* __user
*)&umc
->xferaddr
))
3880 if( put_user(mc
->status
, (u8 __user
*)&upthru
->scsistatus
))
3887 if( put_user(mc
->status
, (u8 __user
*)&uioc_mimd
->mbox
[17]) )
3890 if( mc
->cmd
== MEGA_MBOXCMD_PASSTHRU
) {
3892 umc
= (megacmd_t __user
*)uioc_mimd
->mbox
;
3894 if (get_user(upthru
, (mega_passthru __user
* __user
*)&umc
->xferaddr
))
3897 if( put_user(mc
->status
, (u8 __user
*)&upthru
->scsistatus
) )
3907 * MEGARAID 'FW' commands.
3911 * mega_is_bios_enabled()
3912 * @adapter - pointer to our soft state
3914 * issue command to find out if the BIOS is enabled for this controller
3917 mega_is_bios_enabled(adapter_t
*adapter
)
3919 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
3923 mbox
= (mbox_t
*)raw_mbox
;
3925 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
3927 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
3929 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
3931 raw_mbox
[0] = IS_BIOS_ENABLED
;
3932 raw_mbox
[2] = GET_BIOS
;
3935 ret
= issue_scb_block(adapter
, raw_mbox
);
3937 return *(char *)adapter
->mega_buffer
;
3942 * mega_enum_raid_scsi()
3943 * @adapter - pointer to our soft state
3945 * Find out what channels are RAID/SCSI. This information is used to
3946 * differentiate the virtual channels and physical channels and to support
3947 * ROMB feature and non-disk devices.
3950 mega_enum_raid_scsi(adapter_t
*adapter
)
3952 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
3956 mbox
= (mbox_t
*)raw_mbox
;
3958 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
3961 * issue command to find out what channels are raid/scsi
3963 raw_mbox
[0] = CHNL_CLASS
;
3964 raw_mbox
[2] = GET_CHNL_CLASS
;
3966 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
3968 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
3971 * Non-ROMB firmware fail this command, so all channels
3972 * must be shown RAID
3974 adapter
->mega_ch_class
= 0xFF;
3976 if(!issue_scb_block(adapter
, raw_mbox
)) {
3977 adapter
->mega_ch_class
= *((char *)adapter
->mega_buffer
);
3981 for( i
= 0; i
< adapter
->product_info
.nchannels
; i
++ ) {
3982 if( (adapter
->mega_ch_class
>> i
) & 0x01 ) {
3983 printk(KERN_INFO
"megaraid: channel[%d] is raid.\n",
3987 printk(KERN_INFO
"megaraid: channel[%d] is scsi.\n",
3997 * mega_get_boot_drv()
3998 * @adapter - pointer to our soft state
4000 * Find out which device is the boot device. Note, any logical drive or any
4001 * phyical device (e.g., a CDROM) can be designated as a boot device.
4004 mega_get_boot_drv(adapter_t
*adapter
)
4006 struct private_bios_data
*prv_bios_data
;
4007 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4014 mbox
= (mbox_t
*)raw_mbox
;
4016 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4018 raw_mbox
[0] = BIOS_PVT_DATA
;
4019 raw_mbox
[2] = GET_BIOS_PVT_DATA
;
4021 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
4023 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
4025 adapter
->boot_ldrv_enabled
= 0;
4026 adapter
->boot_ldrv
= 0;
4028 adapter
->boot_pdrv_enabled
= 0;
4029 adapter
->boot_pdrv_ch
= 0;
4030 adapter
->boot_pdrv_tgt
= 0;
4032 if(issue_scb_block(adapter
, raw_mbox
) == 0) {
4034 (struct private_bios_data
*)adapter
->mega_buffer
;
4037 cksum_p
= (char *)prv_bios_data
;
4038 for (i
= 0; i
< 14; i
++ ) {
4039 cksum
+= (u16
)(*cksum_p
++);
4042 if (prv_bios_data
->cksum
== (u16
)(0-cksum
) ) {
4045 * If MSB is set, a physical drive is set as boot
4048 if( prv_bios_data
->boot_drv
& 0x80 ) {
4049 adapter
->boot_pdrv_enabled
= 1;
4050 boot_pdrv
= prv_bios_data
->boot_drv
& 0x7F;
4051 adapter
->boot_pdrv_ch
= boot_pdrv
/ 16;
4052 adapter
->boot_pdrv_tgt
= boot_pdrv
% 16;
4055 adapter
->boot_ldrv_enabled
= 1;
4056 adapter
->boot_ldrv
= prv_bios_data
->boot_drv
;
4064 * mega_support_random_del()
4065 * @adapter - pointer to our soft state
4067 * Find out if this controller supports random deletion and addition of
4071 mega_support_random_del(adapter_t
*adapter
)
4073 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4077 mbox
= (mbox_t
*)raw_mbox
;
4079 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4084 raw_mbox
[0] = FC_DEL_LOGDRV
;
4085 raw_mbox
[2] = OP_SUP_DEL_LOGDRV
;
4087 rval
= issue_scb_block(adapter
, raw_mbox
);
4094 * mega_support_ext_cdb()
4095 * @adapter - pointer to our soft state
4097 * Find out if this firmware support cdblen > 10
4100 mega_support_ext_cdb(adapter_t
*adapter
)
4102 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4106 mbox
= (mbox_t
*)raw_mbox
;
4108 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4110 * issue command to find out if controller supports extended CDBs.
4115 rval
= issue_scb_block(adapter
, raw_mbox
);
4123 * @adapter - pointer to our soft state
4124 * @logdrv - logical drive to be deleted
4126 * Delete the specified logical drive. It is the responsibility of the user
4127 * app to let the OS know about this operation.
4130 mega_del_logdrv(adapter_t
*adapter
, int logdrv
)
4132 unsigned long flags
;
4137 * Stop sending commands to the controller, queue them internally.
4138 * When deletion is complete, ISR will flush the queue.
4140 atomic_set(&adapter
->quiescent
, 1);
4143 * Wait till all the issued commands are complete and there are no
4144 * commands in the pending queue
4146 while (atomic_read(&adapter
->pend_cmds
) > 0 ||
4147 !list_empty(&adapter
->pending_list
))
4148 msleep(1000); /* sleep for 1s */
4150 rval
= mega_do_del_logdrv(adapter
, logdrv
);
4152 spin_lock_irqsave(&adapter
->lock
, flags
);
4155 * If delete operation was successful, add 0x80 to the logical drive
4156 * ids for commands in the pending queue.
4158 if (adapter
->read_ldidmap
) {
4159 struct list_head
*pos
;
4160 list_for_each(pos
, &adapter
->pending_list
) {
4161 scb
= list_entry(pos
, scb_t
, list
);
4162 if (scb
->pthru
->logdrv
< 0x80 )
4163 scb
->pthru
->logdrv
+= 0x80;
4167 atomic_set(&adapter
->quiescent
, 0);
4169 mega_runpendq(adapter
);
4171 spin_unlock_irqrestore(&adapter
->lock
, flags
);
4178 mega_do_del_logdrv(adapter_t
*adapter
, int logdrv
)
4183 memset( &mc
, 0, sizeof(megacmd_t
));
4185 mc
.cmd
= FC_DEL_LOGDRV
;
4186 mc
.opcode
= OP_DEL_LOGDRV
;
4187 mc
.subopcode
= logdrv
;
4189 rval
= mega_internal_command(adapter
, &mc
, NULL
);
4191 /* log this event */
4193 printk(KERN_WARNING
"megaraid: Delete LD-%d failed.", logdrv
);
4198 * After deleting first logical drive, the logical drives must be
4199 * addressed by adding 0x80 to the logical drive id.
4201 adapter
->read_ldidmap
= 1;
4208 * mega_get_max_sgl()
4209 * @adapter - pointer to our soft state
4211 * Find out the maximum number of scatter-gather elements supported by this
4212 * version of the firmware
4215 mega_get_max_sgl(adapter_t
*adapter
)
4217 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4220 mbox
= (mbox_t
*)raw_mbox
;
4222 memset(mbox
, 0, sizeof(raw_mbox
));
4224 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
4226 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
4228 raw_mbox
[0] = MAIN_MISC_OPCODE
;
4229 raw_mbox
[2] = GET_MAX_SG_SUPPORT
;
4232 if( issue_scb_block(adapter
, raw_mbox
) ) {
4234 * f/w does not support this command. Choose the default value
4236 adapter
->sglen
= MIN_SGLIST
;
4239 adapter
->sglen
= *((char *)adapter
->mega_buffer
);
4242 * Make sure this is not more than the resources we are
4243 * planning to allocate
4245 if ( adapter
->sglen
> MAX_SGLIST
)
4246 adapter
->sglen
= MAX_SGLIST
;
4254 * mega_support_cluster()
4255 * @adapter - pointer to our soft state
4257 * Find out if this firmware support cluster calls.
4260 mega_support_cluster(adapter_t
*adapter
)
4262 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4265 mbox
= (mbox_t
*)raw_mbox
;
4267 memset(mbox
, 0, sizeof(raw_mbox
));
4269 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
4271 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
4274 * Try to get the initiator id. This command will succeed iff the
4275 * clustering is available on this HBA.
4277 raw_mbox
[0] = MEGA_GET_TARGET_ID
;
4279 if( issue_scb_block(adapter
, raw_mbox
) == 0 ) {
4282 * Cluster support available. Get the initiator target id.
4283 * Tell our id to mid-layer too.
4285 adapter
->this_id
= *(u32
*)adapter
->mega_buffer
;
4286 adapter
->host
->this_id
= adapter
->this_id
;
4294 #ifdef CONFIG_PROC_FS
4297 * @adapter - pointer to our soft state
4298 * @dma_handle - DMA address of the buffer
4300 * Issue internal comamnds while interrupts are available.
4301 * We only issue direct mailbox commands from within the driver. ioctl()
4302 * interface using these routines can issue passthru commands.
4305 mega_adapinq(adapter_t
*adapter
, dma_addr_t dma_handle
)
4309 memset(&mc
, 0, sizeof(megacmd_t
));
4311 if( adapter
->flag
& BOARD_40LD
) {
4312 mc
.cmd
= FC_NEW_CONFIG
;
4313 mc
.opcode
= NC_SUBOP_ENQUIRY3
;
4314 mc
.subopcode
= ENQ3_GET_SOLICITED_FULL
;
4317 mc
.cmd
= MEGA_MBOXCMD_ADPEXTINQ
;
4320 mc
.xferaddr
= (u32
)dma_handle
;
4322 if ( mega_internal_command(adapter
, &mc
, NULL
) != 0 ) {
4330 /** mega_internal_dev_inquiry()
4331 * @adapter - pointer to our soft state
4332 * @ch - channel for this device
4333 * @tgt - ID of this device
4334 * @buf_dma_handle - DMA address of the buffer
4336 * Issue the scsi inquiry for the specified device.
4339 mega_internal_dev_inquiry(adapter_t
*adapter
, u8 ch
, u8 tgt
,
4340 dma_addr_t buf_dma_handle
)
4342 mega_passthru
*pthru
;
4343 dma_addr_t pthru_dma_handle
;
4346 struct pci_dev
*pdev
;
4350 * For all internal commands, the buffer must be allocated in <4GB
4353 if( make_local_pdev(adapter
, &pdev
) != 0 ) return -1;
4355 pthru
= pci_alloc_consistent(pdev
, sizeof(mega_passthru
),
4358 if( pthru
== NULL
) {
4359 free_local_pdev(pdev
);
4365 pthru
->reqsenselen
= 14;
4366 pthru
->islogical
= 0;
4368 pthru
->channel
= (adapter
->flag
& BOARD_40LD
) ? 0 : ch
;
4370 pthru
->target
= (adapter
->flag
& BOARD_40LD
) ? (ch
<< 4)|tgt
: tgt
;
4374 pthru
->cdb
[0] = INQUIRY
;
4378 pthru
->cdb
[4] = 255;
4382 pthru
->dataxferaddr
= (u32
)buf_dma_handle
;
4383 pthru
->dataxferlen
= 256;
4385 memset(&mc
, 0, sizeof(megacmd_t
));
4387 mc
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
4388 mc
.xferaddr
= (u32
)pthru_dma_handle
;
4390 rval
= mega_internal_command(adapter
, &mc
, pthru
);
4392 pci_free_consistent(pdev
, sizeof(mega_passthru
), pthru
,
4395 free_local_pdev(pdev
);
4402 * mega_internal_command()
4403 * @adapter - pointer to our soft state
4404 * @mc - the mailbox command
4405 * @pthru - Passthru structure for DCDB commands
4407 * Issue the internal commands in interrupt mode.
4408 * The last argument is the address of the passthru structure if the command
4409 * to be fired is a passthru command
4411 * lockscope specifies whether the caller has already acquired the lock. Of
4412 * course, the caller must know which lock we are talking about.
4414 * Note: parameter 'pthru' is null for non-passthru commands.
4417 mega_internal_command(adapter_t
*adapter
, megacmd_t
*mc
, mega_passthru
*pthru
)
4420 struct scsi_device
*sdev
;
4425 * The internal commands share one command id and hence are
4426 * serialized. This is so because we want to reserve maximum number of
4427 * available command ids for the I/O commands.
4429 mutex_lock(&adapter
->int_mtx
);
4431 scb
= &adapter
->int_scb
;
4432 memset(scb
, 0, sizeof(scb_t
));
4434 scmd
= &adapter
->int_scmd
;
4435 memset(scmd
, 0, sizeof(Scsi_Cmnd
));
4437 sdev
= kzalloc(sizeof(struct scsi_device
), GFP_KERNEL
);
4438 scmd
->device
= sdev
;
4440 scmd
->device
->host
= adapter
->host
;
4441 scmd
->host_scribble
= (void *)scb
;
4442 scmd
->cmnd
[0] = MEGA_INTERNAL_CMD
;
4444 scb
->state
|= SCB_ACTIVE
;
4447 memcpy(scb
->raw_mbox
, mc
, sizeof(megacmd_t
));
4450 * Is it a passthru command
4452 if( mc
->cmd
== MEGA_MBOXCMD_PASSTHRU
) {
4457 scb
->idx
= CMDID_INT_CMDS
;
4459 megaraid_queue(scmd
, mega_internal_done
);
4461 wait_for_completion(&adapter
->int_waitq
);
4463 rval
= scmd
->result
;
4464 mc
->status
= scmd
->result
;
4468 * Print a debug message for all failed commands. Applications can use
4471 if( scmd
->result
&& trace_level
) {
4472 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4473 mc
->cmd
, mc
->opcode
, mc
->subopcode
, scmd
->result
);
4476 mutex_unlock(&adapter
->int_mtx
);
4483 * mega_internal_done()
4484 * @scmd - internal scsi command
4486 * Callback routine for internal commands.
4489 mega_internal_done(Scsi_Cmnd
*scmd
)
4493 adapter
= (adapter_t
*)scmd
->device
->host
->hostdata
;
4495 complete(&adapter
->int_waitq
);
4500 static struct scsi_host_template megaraid_template
= {
4501 .module
= THIS_MODULE
,
4503 .proc_name
= "megaraid_legacy",
4504 .info
= megaraid_info
,
4505 .queuecommand
= megaraid_queue
,
4506 .bios_param
= megaraid_biosparam
,
4507 .max_sectors
= MAX_SECTORS_PER_IO
,
4508 .can_queue
= MAX_COMMANDS
,
4509 .this_id
= DEFAULT_INITIATOR_ID
,
4510 .sg_tablesize
= MAX_SGLIST
,
4511 .cmd_per_lun
= DEF_CMD_PER_LUN
,
4512 .use_clustering
= ENABLE_CLUSTERING
,
4513 .eh_abort_handler
= megaraid_abort
,
4514 .eh_device_reset_handler
= megaraid_reset
,
4515 .eh_bus_reset_handler
= megaraid_reset
,
4516 .eh_host_reset_handler
= megaraid_reset
,
4519 static int __devinit
4520 megaraid_probe_one(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
4522 struct Scsi_Host
*host
;
4524 unsigned long mega_baseport
, tbase
, flag
= 0;
4525 u16 subsysid
, subsysvid
;
4526 u8 pci_bus
, pci_dev_func
;
4528 int error
= -ENODEV
;
4530 if (pci_enable_device(pdev
))
4532 pci_set_master(pdev
);
4534 pci_bus
= pdev
->bus
->number
;
4535 pci_dev_func
= pdev
->devfn
;
4538 * The megaraid3 stuff reports the ID of the Intel part which is not
4539 * remotely specific to the megaraid
4541 if (pdev
->vendor
== PCI_VENDOR_ID_INTEL
) {
4544 * Don't fall over the Compaq management cards using the same
4547 if (pdev
->subsystem_vendor
== PCI_VENDOR_ID_COMPAQ
&&
4548 pdev
->subsystem_device
== 0xC000)
4550 /* Now check the magic signature byte */
4551 pci_read_config_word(pdev
, PCI_CONF_AMISIG
, &magic
);
4552 if (magic
!= HBA_SIGNATURE_471
&& magic
!= HBA_SIGNATURE
)
4554 /* Ok it is probably a megaraid */
4558 * For these vendor and device ids, signature offsets are not
4559 * valid and 64 bit is implicit
4561 if (id
->driver_data
& BOARD_64BIT
)
4562 flag
|= BOARD_64BIT
;
4566 pci_read_config_dword(pdev
, PCI_CONF_AMISIG64
, &magic64
);
4567 if (magic64
== HBA_SIGNATURE_64BIT
)
4568 flag
|= BOARD_64BIT
;
4571 subsysvid
= pdev
->subsystem_vendor
;
4572 subsysid
= pdev
->subsystem_device
;
4574 printk(KERN_NOTICE
"megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4575 id
->vendor
, id
->device
, pci_bus
);
4577 printk("slot %d:func %d\n",
4578 PCI_SLOT(pci_dev_func
), PCI_FUNC(pci_dev_func
));
4580 /* Read the base port and IRQ from PCI */
4581 mega_baseport
= pci_resource_start(pdev
, 0);
4584 tbase
= mega_baseport
;
4585 if (pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
) {
4586 flag
|= BOARD_MEMMAP
;
4588 if (!request_mem_region(mega_baseport
, 128, "megaraid")) {
4589 printk(KERN_WARNING
"megaraid: mem region busy!\n");
4590 goto out_disable_device
;
4593 mega_baseport
= (unsigned long)ioremap(mega_baseport
, 128);
4594 if (!mega_baseport
) {
4596 "megaraid: could not map hba memory\n");
4597 goto out_release_region
;
4600 flag
|= BOARD_IOMAP
;
4601 mega_baseport
+= 0x10;
4603 if (!request_region(mega_baseport
, 16, "megaraid"))
4604 goto out_disable_device
;
4607 /* Initialize SCSI Host structure */
4608 host
= scsi_host_alloc(&megaraid_template
, sizeof(adapter_t
));
4612 adapter
= (adapter_t
*)host
->hostdata
;
4613 memset(adapter
, 0, sizeof(adapter_t
));
4616 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4617 host
->host_no
, mega_baseport
, irq
);
4619 adapter
->base
= mega_baseport
;
4620 if (flag
& BOARD_MEMMAP
)
4621 adapter
->mmio_base
= (void __iomem
*) mega_baseport
;
4623 INIT_LIST_HEAD(&adapter
->free_list
);
4624 INIT_LIST_HEAD(&adapter
->pending_list
);
4625 INIT_LIST_HEAD(&adapter
->completed_list
);
4627 adapter
->flag
= flag
;
4628 spin_lock_init(&adapter
->lock
);
4630 host
->cmd_per_lun
= max_cmd_per_lun
;
4631 host
->max_sectors
= max_sectors_per_io
;
4633 adapter
->dev
= pdev
;
4634 adapter
->host
= host
;
4636 adapter
->host
->irq
= irq
;
4638 if (flag
& BOARD_MEMMAP
)
4639 adapter
->host
->base
= tbase
;
4641 adapter
->host
->io_port
= tbase
;
4642 adapter
->host
->n_io_port
= 16;
4645 adapter
->host
->unique_id
= (pci_bus
<< 8) | pci_dev_func
;
4648 * Allocate buffer to issue internal commands.
4650 adapter
->mega_buffer
= pci_alloc_consistent(adapter
->dev
,
4651 MEGA_BUFFER_SIZE
, &adapter
->buf_dma_handle
);
4652 if (!adapter
->mega_buffer
) {
4653 printk(KERN_WARNING
"megaraid: out of RAM.\n");
4657 adapter
->scb_list
= kmalloc(sizeof(scb_t
) * MAX_COMMANDS
, GFP_KERNEL
);
4658 if (!adapter
->scb_list
) {
4659 printk(KERN_WARNING
"megaraid: out of RAM.\n");
4660 goto out_free_cmd_buffer
;
4663 if (request_irq(irq
, (adapter
->flag
& BOARD_MEMMAP
) ?
4664 megaraid_isr_memmapped
: megaraid_isr_iomapped
,
4665 IRQF_SHARED
, "megaraid", adapter
)) {
4667 "megaraid: Couldn't register IRQ %d!\n", irq
);
4668 goto out_free_scb_list
;
4671 if (mega_setup_mailbox(adapter
))
4674 if (mega_query_adapter(adapter
))
4678 * Have checks for some buggy f/w
4680 if ((subsysid
== 0x1111) && (subsysvid
== 0x1111)) {
4684 if (!strcmp(adapter
->fw_version
, "3.00") ||
4685 !strcmp(adapter
->fw_version
, "3.01")) {
4687 printk( KERN_WARNING
4688 "megaraid: Your card is a Dell PERC "
4689 "2/SC RAID controller with "
4690 "firmware\nmegaraid: 3.00 or 3.01. "
4691 "This driver is known to have "
4692 "corruption issues\nmegaraid: with "
4693 "those firmware versions on this "
4694 "specific card. In order\nmegaraid: "
4695 "to protect your data, please upgrade "
4696 "your firmware to version\nmegaraid: "
4697 "3.10 or later, available from the "
4698 "Dell Technical Support web\n"
4699 "megaraid: site at\nhttp://support."
4700 "dell.com/us/en/filelib/download/"
4701 "index.asp?fileid=2940\n"
4707 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4708 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4709 * support, since this firmware cannot handle 64 bit
4712 if ((subsysvid
== HP_SUBSYS_VID
) &&
4713 ((subsysid
== 0x60E7) || (subsysid
== 0x60E8))) {
4717 if (!strcmp(adapter
->fw_version
, "H01.07") ||
4718 !strcmp(adapter
->fw_version
, "H01.08") ||
4719 !strcmp(adapter
->fw_version
, "H01.09") ) {
4721 "megaraid: Firmware H.01.07, "
4722 "H.01.08, and H.01.09 on 1M/2M "
4724 "megaraid: do not support 64 bit "
4725 "addressing.\nmegaraid: DISABLING "
4726 "64 bit support.\n");
4727 adapter
->flag
&= ~BOARD_64BIT
;
4731 if (mega_is_bios_enabled(adapter
))
4732 mega_hbas
[hba_count
].is_bios_enabled
= 1;
4733 mega_hbas
[hba_count
].hostdata_addr
= adapter
;
4736 * Find out which channel is raid and which is scsi. This is
4739 mega_enum_raid_scsi(adapter
);
4742 * Find out if a logical drive is set as the boot drive. If
4743 * there is one, will make that as the first logical drive.
4744 * ROMB: Do we have to boot from a physical drive. Then all
4745 * the physical drives would appear before the logical disks.
4746 * Else, all the physical drives would be exported to the mid
4747 * layer after logical drives.
4749 mega_get_boot_drv(adapter
);
4751 if (adapter
->boot_pdrv_enabled
) {
4752 j
= adapter
->product_info
.nchannels
;
4753 for( i
= 0; i
< j
; i
++ )
4754 adapter
->logdrv_chan
[i
] = 0;
4755 for( i
= j
; i
< NVIRT_CHAN
+ j
; i
++ )
4756 adapter
->logdrv_chan
[i
] = 1;
4758 for (i
= 0; i
< NVIRT_CHAN
; i
++)
4759 adapter
->logdrv_chan
[i
] = 1;
4760 for (i
= NVIRT_CHAN
; i
< MAX_CHANNELS
+NVIRT_CHAN
; i
++)
4761 adapter
->logdrv_chan
[i
] = 0;
4762 adapter
->mega_ch_class
<<= NVIRT_CHAN
;
4766 * Do we support random deletion and addition of logical
4769 adapter
->read_ldidmap
= 0; /* set it after first logdrv
4771 adapter
->support_random_del
= mega_support_random_del(adapter
);
4773 /* Initialize SCBs */
4774 if (mega_init_scb(adapter
))
4778 * Reset the pending commands counter
4780 atomic_set(&adapter
->pend_cmds
, 0);
4783 * Reset the adapter quiescent flag
4785 atomic_set(&adapter
->quiescent
, 0);
4787 hba_soft_state
[hba_count
] = adapter
;
4790 * Fill in the structure which needs to be passed back to the
4791 * application when it does an ioctl() for controller related
4796 mcontroller
[i
].base
= mega_baseport
;
4797 mcontroller
[i
].irq
= irq
;
4798 mcontroller
[i
].numldrv
= adapter
->numldrv
;
4799 mcontroller
[i
].pcibus
= pci_bus
;
4800 mcontroller
[i
].pcidev
= id
->device
;
4801 mcontroller
[i
].pcifun
= PCI_FUNC (pci_dev_func
);
4802 mcontroller
[i
].pciid
= -1;
4803 mcontroller
[i
].pcivendor
= id
->vendor
;
4804 mcontroller
[i
].pcislot
= PCI_SLOT(pci_dev_func
);
4805 mcontroller
[i
].uid
= (pci_bus
<< 8) | pci_dev_func
;
4808 /* Set the Mode of addressing to 64 bit if we can */
4809 if ((adapter
->flag
& BOARD_64BIT
) && (sizeof(dma_addr_t
) == 8)) {
4810 pci_set_dma_mask(pdev
, DMA_64BIT_MASK
);
4811 adapter
->has_64bit_addr
= 1;
4813 pci_set_dma_mask(pdev
, DMA_32BIT_MASK
);
4814 adapter
->has_64bit_addr
= 0;
4817 mutex_init(&adapter
->int_mtx
);
4818 init_completion(&adapter
->int_waitq
);
4820 adapter
->this_id
= DEFAULT_INITIATOR_ID
;
4821 adapter
->host
->this_id
= DEFAULT_INITIATOR_ID
;
4823 #if MEGA_HAVE_CLUSTERING
4825 * Is cluster support enabled on this controller
4826 * Note: In a cluster the HBAs ( the initiators ) will have
4827 * different target IDs and we cannot assume it to be 7. Call
4828 * to mega_support_cluster() will get the target ids also if
4829 * the cluster support is available
4831 adapter
->has_cluster
= mega_support_cluster(adapter
);
4832 if (adapter
->has_cluster
) {
4834 "megaraid: Cluster driver, initiator id:%d\n",
4839 pci_set_drvdata(pdev
, host
);
4841 mega_create_proc_entry(hba_count
, mega_proc_dir_entry
);
4843 error
= scsi_add_host(host
, &pdev
->dev
);
4847 scsi_scan_host(host
);
4852 pci_free_consistent(adapter
->dev
, sizeof(mbox64_t
),
4853 adapter
->una_mbox64
, adapter
->una_mbox64_dma
);
4855 free_irq(adapter
->host
->irq
, adapter
);
4857 kfree(adapter
->scb_list
);
4858 out_free_cmd_buffer
:
4859 pci_free_consistent(adapter
->dev
, MEGA_BUFFER_SIZE
,
4860 adapter
->mega_buffer
, adapter
->buf_dma_handle
);
4862 scsi_host_put(host
);
4864 if (flag
& BOARD_MEMMAP
)
4865 iounmap((void *)mega_baseport
);
4867 if (flag
& BOARD_MEMMAP
)
4868 release_mem_region(tbase
, 128);
4870 release_region(mega_baseport
, 16);
4872 pci_disable_device(pdev
);
4878 __megaraid_shutdown(adapter_t
*adapter
)
4880 u_char raw_mbox
[sizeof(struct mbox_out
)];
4881 mbox_t
*mbox
= (mbox_t
*)raw_mbox
;
4884 /* Flush adapter cache */
4885 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4886 raw_mbox
[0] = FLUSH_ADAPTER
;
4888 free_irq(adapter
->host
->irq
, adapter
);
4890 /* Issue a blocking (interrupts disabled) command to the card */
4891 issue_scb_block(adapter
, raw_mbox
);
4893 /* Flush disks cache */
4894 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4895 raw_mbox
[0] = FLUSH_SYSTEM
;
4897 /* Issue a blocking (interrupts disabled) command to the card */
4898 issue_scb_block(adapter
, raw_mbox
);
4900 if (atomic_read(&adapter
->pend_cmds
) > 0)
4901 printk(KERN_WARNING
"megaraid: pending commands!!\n");
4904 * Have a delibrate delay to make sure all the caches are
4907 for (i
= 0; i
<= 10; i
++)
4911 static void __devexit
4912 megaraid_remove_one(struct pci_dev
*pdev
)
4914 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
4915 adapter_t
*adapter
= (adapter_t
*)host
->hostdata
;
4917 scsi_remove_host(host
);
4919 __megaraid_shutdown(adapter
);
4921 /* Free our resources */
4922 if (adapter
->flag
& BOARD_MEMMAP
) {
4923 iounmap((void *)adapter
->base
);
4924 release_mem_region(adapter
->host
->base
, 128);
4926 release_region(adapter
->base
, 16);
4928 mega_free_sgl(adapter
);
4930 #ifdef CONFIG_PROC_FS
4931 if (adapter
->controller_proc_dir_entry
) {
4932 remove_proc_entry("stat", adapter
->controller_proc_dir_entry
);
4933 remove_proc_entry("config",
4934 adapter
->controller_proc_dir_entry
);
4935 remove_proc_entry("mailbox",
4936 adapter
->controller_proc_dir_entry
);
4937 #if MEGA_HAVE_ENH_PROC
4938 remove_proc_entry("rebuild-rate",
4939 adapter
->controller_proc_dir_entry
);
4940 remove_proc_entry("battery-status",
4941 adapter
->controller_proc_dir_entry
);
4943 remove_proc_entry("diskdrives-ch0",
4944 adapter
->controller_proc_dir_entry
);
4945 remove_proc_entry("diskdrives-ch1",
4946 adapter
->controller_proc_dir_entry
);
4947 remove_proc_entry("diskdrives-ch2",
4948 adapter
->controller_proc_dir_entry
);
4949 remove_proc_entry("diskdrives-ch3",
4950 adapter
->controller_proc_dir_entry
);
4952 remove_proc_entry("raiddrives-0-9",
4953 adapter
->controller_proc_dir_entry
);
4954 remove_proc_entry("raiddrives-10-19",
4955 adapter
->controller_proc_dir_entry
);
4956 remove_proc_entry("raiddrives-20-29",
4957 adapter
->controller_proc_dir_entry
);
4958 remove_proc_entry("raiddrives-30-39",
4959 adapter
->controller_proc_dir_entry
);
4962 char buf
[12] = { 0 };
4963 sprintf(buf
, "hba%d", adapter
->host
->host_no
);
4964 remove_proc_entry(buf
, mega_proc_dir_entry
);
4969 pci_free_consistent(adapter
->dev
, MEGA_BUFFER_SIZE
,
4970 adapter
->mega_buffer
, adapter
->buf_dma_handle
);
4971 kfree(adapter
->scb_list
);
4972 pci_free_consistent(adapter
->dev
, sizeof(mbox64_t
),
4973 adapter
->una_mbox64
, adapter
->una_mbox64_dma
);
4975 scsi_host_put(host
);
4976 pci_disable_device(pdev
);
4982 megaraid_shutdown(struct pci_dev
*pdev
)
4984 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
4985 adapter_t
*adapter
= (adapter_t
*)host
->hostdata
;
4987 __megaraid_shutdown(adapter
);
4990 static struct pci_device_id megaraid_pci_tbl
[] = {
4991 {PCI_VENDOR_ID_AMI
, PCI_DEVICE_ID_AMI_MEGARAID
,
4992 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
4993 {PCI_VENDOR_ID_AMI
, PCI_DEVICE_ID_AMI_MEGARAID2
,
4994 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
4995 {PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_AMI_MEGARAID3
,
4996 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
4999 MODULE_DEVICE_TABLE(pci
, megaraid_pci_tbl
);
5001 static struct pci_driver megaraid_pci_driver
= {
5002 .name
= "megaraid_legacy",
5003 .id_table
= megaraid_pci_tbl
,
5004 .probe
= megaraid_probe_one
,
5005 .remove
= __devexit_p(megaraid_remove_one
),
5006 .shutdown
= megaraid_shutdown
,
5009 static int __init
megaraid_init(void)
5013 if ((max_cmd_per_lun
<= 0) || (max_cmd_per_lun
> MAX_CMD_PER_LUN
))
5014 max_cmd_per_lun
= MAX_CMD_PER_LUN
;
5015 if (max_mbox_busy_wait
> MBOX_BUSY_WAIT
)
5016 max_mbox_busy_wait
= MBOX_BUSY_WAIT
;
5018 #ifdef CONFIG_PROC_FS
5019 mega_proc_dir_entry
= proc_mkdir("megaraid", &proc_root
);
5020 if (!mega_proc_dir_entry
) {
5022 "megaraid: failed to create megaraid root\n");
5025 error
= pci_register_driver(&megaraid_pci_driver
);
5027 #ifdef CONFIG_PROC_FS
5028 remove_proc_entry("megaraid", &proc_root
);
5034 * Register the driver as a character device, for applications
5035 * to access it for ioctls.
5036 * First argument (major) to register_chrdev implies a dynamic
5037 * major number allocation.
5039 major
= register_chrdev(0, "megadev_legacy", &megadev_fops
);
5042 "megaraid: failed to register char device\n");
5048 static void __exit
megaraid_exit(void)
5051 * Unregister the character device interface to the driver.
5053 unregister_chrdev(major
, "megadev_legacy");
5055 pci_unregister_driver(&megaraid_pci_driver
);
5057 #ifdef CONFIG_PROC_FS
5058 remove_proc_entry("megaraid", &proc_root
);
5062 module_init(megaraid_init
);
5063 module_exit(megaraid_exit
);
5065 /* vi: set ts=8 sw=8 tw=78: */