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 <linux/mutex.h>
50 #include <linux/slab.h>
51 #include <scsi/scsicam.h>
54 #include <scsi/scsi_host.h>
58 #define MEGARAID_MODULE_VERSION "2.00.4"
60 MODULE_AUTHOR ("sju@lsil.com");
61 MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
62 MODULE_LICENSE ("GPL");
63 MODULE_VERSION(MEGARAID_MODULE_VERSION
);
65 static DEFINE_MUTEX(megadev_mutex
);
66 static unsigned int max_cmd_per_lun
= DEF_CMD_PER_LUN
;
67 module_param(max_cmd_per_lun
, uint
, 0);
68 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)");
70 static unsigned short int max_sectors_per_io
= MAX_SECTORS_PER_IO
;
71 module_param(max_sectors_per_io
, ushort
, 0);
72 MODULE_PARM_DESC(max_sectors_per_io
, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
75 static unsigned short int max_mbox_busy_wait
= MBOX_BUSY_WAIT
;
76 module_param(max_mbox_busy_wait
, ushort
, 0);
77 MODULE_PARM_DESC(max_mbox_busy_wait
, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
79 #define RDINDOOR(adapter) readl((adapter)->mmio_base + 0x20)
80 #define RDOUTDOOR(adapter) readl((adapter)->mmio_base + 0x2C)
81 #define WRINDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x20)
82 #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
89 static adapter_t
*hba_soft_state
[MAX_CONTROLLERS
];
90 static struct proc_dir_entry
*mega_proc_dir_entry
;
92 /* For controller re-ordering */
93 static struct mega_hbas mega_hbas
[MAX_CONTROLLERS
];
96 megadev_unlocked_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
);
99 * The File Operations structure for the serial/ioctl interface of the driver
101 static const struct file_operations megadev_fops
= {
102 .owner
= THIS_MODULE
,
103 .unlocked_ioctl
= megadev_unlocked_ioctl
,
104 .open
= megadev_open
,
105 .llseek
= noop_llseek
,
109 * Array to structures for storing the information about the controllers. This
110 * information is sent to the user level applications, when they do an ioctl
111 * for this information.
113 static struct mcontroller mcontroller
[MAX_CONTROLLERS
];
115 /* The current driver version */
116 static u32 driver_ver
= 0x02000000;
118 /* major number used by the device for character interface */
121 #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
125 * Debug variable to print some diagnostic messages
127 static int trace_level
;
130 * mega_setup_mailbox()
131 * @adapter - pointer to our soft state
133 * Allocates a 8 byte aligned memory for the handshake mailbox.
136 mega_setup_mailbox(adapter_t
*adapter
)
140 adapter
->una_mbox64
= pci_alloc_consistent(adapter
->dev
,
141 sizeof(mbox64_t
), &adapter
->una_mbox64_dma
);
143 if( !adapter
->una_mbox64
) return -1;
145 adapter
->mbox
= &adapter
->una_mbox64
->mbox
;
147 adapter
->mbox
= (mbox_t
*)((((unsigned long) adapter
->mbox
) + 15) &
150 adapter
->mbox64
= (mbox64_t
*)(((unsigned long)adapter
->mbox
) - 8);
152 align
= ((void *)adapter
->mbox
) - ((void *)&adapter
->una_mbox64
->mbox
);
154 adapter
->mbox_dma
= adapter
->una_mbox64_dma
+ 8 + align
;
157 * Register the mailbox if the controller is an io-mapped controller
159 if( adapter
->flag
& BOARD_IOMAP
) {
161 outb(adapter
->mbox_dma
& 0xFF,
162 adapter
->host
->io_port
+ MBOX_PORT0
);
164 outb((adapter
->mbox_dma
>> 8) & 0xFF,
165 adapter
->host
->io_port
+ MBOX_PORT1
);
167 outb((adapter
->mbox_dma
>> 16) & 0xFF,
168 adapter
->host
->io_port
+ MBOX_PORT2
);
170 outb((adapter
->mbox_dma
>> 24) & 0xFF,
171 adapter
->host
->io_port
+ MBOX_PORT3
);
173 outb(ENABLE_MBOX_BYTE
,
174 adapter
->host
->io_port
+ ENABLE_MBOX_REGION
);
186 * mega_query_adapter()
187 * @adapter - pointer to our soft state
189 * Issue the adapter inquiry commands to the controller and find out
190 * information and parameter about the devices attached
193 mega_query_adapter(adapter_t
*adapter
)
195 dma_addr_t prod_info_dma_handle
;
196 mega_inquiry3
*inquiry3
;
197 u8 raw_mbox
[sizeof(struct mbox_out
)];
201 /* Initialize adapter inquiry mailbox */
203 mbox
= (mbox_t
*)raw_mbox
;
205 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
206 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
209 * Try to issue Inquiry3 command
210 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
211 * update enquiry3 structure
213 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
215 inquiry3
= (mega_inquiry3
*)adapter
->mega_buffer
;
217 raw_mbox
[0] = FC_NEW_CONFIG
; /* i.e. mbox->cmd=0xA1 */
218 raw_mbox
[2] = NC_SUBOP_ENQUIRY3
; /* i.e. 0x0F */
219 raw_mbox
[3] = ENQ3_GET_SOLICITED_FULL
; /* i.e. 0x02 */
221 /* Issue a blocking command to the card */
222 if ((retval
= issue_scb_block(adapter
, raw_mbox
))) {
223 /* the adapter does not support 40ld */
225 mraid_ext_inquiry
*ext_inq
;
227 dma_addr_t dma_handle
;
229 ext_inq
= pci_alloc_consistent(adapter
->dev
,
230 sizeof(mraid_ext_inquiry
), &dma_handle
);
232 if( ext_inq
== NULL
) return -1;
234 inq
= &ext_inq
->raid_inq
;
236 mbox
->m_out
.xferaddr
= (u32
)dma_handle
;
238 /*issue old 0x04 command to adapter */
239 mbox
->m_out
.cmd
= MEGA_MBOXCMD_ADPEXTINQ
;
241 issue_scb_block(adapter
, raw_mbox
);
244 * update Enquiry3 and ProductInfo structures with
245 * mraid_inquiry structure
247 mega_8_to_40ld(inq
, inquiry3
,
248 (mega_product_info
*)&adapter
->product_info
);
250 pci_free_consistent(adapter
->dev
, sizeof(mraid_ext_inquiry
),
251 ext_inq
, dma_handle
);
253 } else { /*adapter supports 40ld */
254 adapter
->flag
|= BOARD_40LD
;
257 * get product_info, which is static information and will be
260 prod_info_dma_handle
= pci_map_single(adapter
->dev
, (void *)
261 &adapter
->product_info
,
262 sizeof(mega_product_info
), PCI_DMA_FROMDEVICE
);
264 mbox
->m_out
.xferaddr
= prod_info_dma_handle
;
266 raw_mbox
[0] = FC_NEW_CONFIG
; /* i.e. mbox->cmd=0xA1 */
267 raw_mbox
[2] = NC_SUBOP_PRODUCT_INFO
; /* i.e. 0x0E */
269 if ((retval
= issue_scb_block(adapter
, raw_mbox
)))
271 "megaraid: Product_info cmd failed with error: %d\n",
274 pci_unmap_single(adapter
->dev
, prod_info_dma_handle
,
275 sizeof(mega_product_info
), PCI_DMA_FROMDEVICE
);
280 * kernel scans the channels from 0 to <= max_channel
282 adapter
->host
->max_channel
=
283 adapter
->product_info
.nchannels
+ NVIRT_CHAN
-1;
285 adapter
->host
->max_id
= 16; /* max targets per channel */
287 adapter
->host
->max_lun
= 7; /* Up to 7 luns for non disk devices */
289 adapter
->host
->cmd_per_lun
= max_cmd_per_lun
;
291 adapter
->numldrv
= inquiry3
->num_ldrv
;
293 adapter
->max_cmds
= adapter
->product_info
.max_commands
;
295 if(adapter
->max_cmds
> MAX_COMMANDS
)
296 adapter
->max_cmds
= MAX_COMMANDS
;
298 adapter
->host
->can_queue
= adapter
->max_cmds
- 1;
301 * Get the maximum number of scatter-gather elements supported by this
304 mega_get_max_sgl(adapter
);
306 adapter
->host
->sg_tablesize
= adapter
->sglen
;
309 /* use HP firmware and bios version encoding */
310 if (adapter
->product_info
.subsysvid
== HP_SUBSYS_VID
) {
311 sprintf (adapter
->fw_version
, "%c%d%d.%d%d",
312 adapter
->product_info
.fw_version
[2],
313 adapter
->product_info
.fw_version
[1] >> 4,
314 adapter
->product_info
.fw_version
[1] & 0x0f,
315 adapter
->product_info
.fw_version
[0] >> 4,
316 adapter
->product_info
.fw_version
[0] & 0x0f);
317 sprintf (adapter
->bios_version
, "%c%d%d.%d%d",
318 adapter
->product_info
.bios_version
[2],
319 adapter
->product_info
.bios_version
[1] >> 4,
320 adapter
->product_info
.bios_version
[1] & 0x0f,
321 adapter
->product_info
.bios_version
[0] >> 4,
322 adapter
->product_info
.bios_version
[0] & 0x0f);
324 memcpy(adapter
->fw_version
,
325 (char *)adapter
->product_info
.fw_version
, 4);
326 adapter
->fw_version
[4] = 0;
328 memcpy(adapter
->bios_version
,
329 (char *)adapter
->product_info
.bios_version
, 4);
331 adapter
->bios_version
[4] = 0;
334 printk(KERN_NOTICE
"megaraid: [%s:%s] detected %d logical drives.\n",
335 adapter
->fw_version
, adapter
->bios_version
, adapter
->numldrv
);
338 * Do we support extended (>10 bytes) cdbs
340 adapter
->support_ext_cdb
= mega_support_ext_cdb(adapter
);
341 if (adapter
->support_ext_cdb
)
342 printk(KERN_NOTICE
"megaraid: supports extended CDBs.\n");
350 * @adapter - pointer to our soft state
352 * Runs through the list of pending requests.
355 mega_runpendq(adapter_t
*adapter
)
357 if(!list_empty(&adapter
->pending_list
))
358 __mega_runpendq(adapter
);
363 * @scmd - Issue this scsi command
364 * @done - the callback hook into the scsi mid-layer
366 * The command queuing entry point for the mid-layer.
369 megaraid_queue_lck(Scsi_Cmnd
*scmd
, void (*done
)(Scsi_Cmnd
*))
376 adapter
= (adapter_t
*)scmd
->device
->host
->hostdata
;
378 scmd
->scsi_done
= done
;
382 * Allocate and build a SCB request
383 * busy flag will be set if mega_build_cmd() command could not
384 * allocate scb. We will return non-zero status in that case.
385 * NOTE: scb can be null even though certain commands completed
386 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
387 * return 0 in that case.
390 spin_lock_irqsave(&adapter
->lock
, flags
);
391 scb
= mega_build_cmd(adapter
, scmd
, &busy
);
395 scb
->state
|= SCB_PENDQ
;
396 list_add_tail(&scb
->list
, &adapter
->pending_list
);
399 * Check if the HBA is in quiescent state, e.g., during a
400 * delete logical drive opertion. If it is, don't run
403 if (atomic_read(&adapter
->quiescent
) == 0)
404 mega_runpendq(adapter
);
408 spin_unlock_irqrestore(&adapter
->lock
, flags
);
412 static DEF_SCSI_QCMD(megaraid_queue
)
415 * mega_allocate_scb()
416 * @adapter - pointer to our soft state
417 * @cmd - scsi command from the mid-layer
419 * Allocate a SCB structure. This is the central structure for controller
422 static inline scb_t
*
423 mega_allocate_scb(adapter_t
*adapter
, Scsi_Cmnd
*cmd
)
425 struct list_head
*head
= &adapter
->free_list
;
428 /* Unlink command from Free List */
429 if( !list_empty(head
) ) {
431 scb
= list_entry(head
->next
, scb_t
, list
);
433 list_del_init(head
->next
);
435 scb
->state
= SCB_ACTIVE
;
437 scb
->dma_type
= MEGA_DMA_TYPE_NONE
;
446 * mega_get_ldrv_num()
447 * @adapter - pointer to our soft state
448 * @cmd - scsi mid layer command
449 * @channel - channel on the controller
451 * Calculate the logical drive number based on the information in scsi command
452 * and the channel number.
455 mega_get_ldrv_num(adapter_t
*adapter
, Scsi_Cmnd
*cmd
, int channel
)
460 tgt
= cmd
->device
->id
;
462 if ( tgt
> adapter
->this_id
)
463 tgt
--; /* we do not get inquires for initiator id */
465 ldrv_num
= (channel
* 15) + tgt
;
469 * If we have a logical drive with boot enabled, project it first
471 if( adapter
->boot_ldrv_enabled
) {
472 if( ldrv_num
== 0 ) {
473 ldrv_num
= adapter
->boot_ldrv
;
476 if( ldrv_num
<= adapter
->boot_ldrv
) {
483 * If "delete logical drive" feature is enabled on this controller.
484 * Do only if at least one delete logical drive operation was done.
486 * Also, after logical drive deletion, instead of logical drive number,
487 * the value returned should be 0x80+logical drive id.
489 * These is valid only for IO commands.
492 if (adapter
->support_random_del
&& adapter
->read_ldidmap
)
493 switch (cmd
->cmnd
[0]) {
494 case READ_6
: /* fall through */
495 case WRITE_6
: /* fall through */
496 case READ_10
: /* fall through */
506 * @adapter - pointer to our soft state
507 * @cmd - Prepare using this scsi command
508 * @busy - busy flag if no resources
510 * Prepares a command and scatter gather list for the controller. This routine
511 * also finds out if the commands is intended for a logical drive or a
512 * physical device and prepares the controller command accordingly.
514 * We also re-order the logical drives and physical devices based on their
518 mega_build_cmd(adapter_t
*adapter
, Scsi_Cmnd
*cmd
, int *busy
)
520 mega_ext_passthru
*epthru
;
521 mega_passthru
*pthru
;
529 int ldrv_num
= 0; /* logical drive number */
533 * filter the internal and ioctl commands
535 if((cmd
->cmnd
[0] == MEGA_INTERNAL_CMD
))
536 return (scb_t
*)cmd
->host_scribble
;
539 * We know what channels our logical drives are on - mega_find_card()
541 islogical
= adapter
->logdrv_chan
[cmd
->device
->channel
];
544 * The theory: If physical drive is chosen for boot, all the physical
545 * devices are exported before the logical drives, otherwise physical
546 * devices are pushed after logical drives, in which case - Kernel sees
547 * the physical devices on virtual channel which is obviously converted
548 * to actual channel on the HBA.
550 if( adapter
->boot_pdrv_enabled
) {
552 /* logical channel */
553 channel
= cmd
->device
->channel
-
554 adapter
->product_info
.nchannels
;
557 /* this is physical channel */
558 channel
= cmd
->device
->channel
;
559 target
= cmd
->device
->id
;
562 * boot from a physical disk, that disk needs to be
563 * exposed first IF both the channels are SCSI, then
564 * booting from the second channel is not allowed.
567 target
= adapter
->boot_pdrv_tgt
;
569 else if( target
== adapter
->boot_pdrv_tgt
) {
576 /* this is the logical channel */
577 channel
= cmd
->device
->channel
;
580 /* physical channel */
581 channel
= cmd
->device
->channel
- NVIRT_CHAN
;
582 target
= cmd
->device
->id
;
589 /* have just LUN 0 for each target on virtual channels */
590 if (cmd
->device
->lun
) {
591 cmd
->result
= (DID_BAD_TARGET
<< 16);
596 ldrv_num
= mega_get_ldrv_num(adapter
, cmd
, channel
);
599 max_ldrv_num
= (adapter
->flag
& BOARD_40LD
) ?
600 MAX_LOGICAL_DRIVES_40LD
: MAX_LOGICAL_DRIVES_8LD
;
603 * max_ldrv_num increases by 0x80 if some logical drive was
606 if(adapter
->read_ldidmap
)
607 max_ldrv_num
+= 0x80;
609 if(ldrv_num
> max_ldrv_num
) {
610 cmd
->result
= (DID_BAD_TARGET
<< 16);
617 if( cmd
->device
->lun
> 7) {
619 * Do not support lun >7 for physically accessed
622 cmd
->result
= (DID_BAD_TARGET
<< 16);
630 * Logical drive commands
634 switch (cmd
->cmnd
[0]) {
635 case TEST_UNIT_READY
:
636 #if MEGA_HAVE_CLUSTERING
638 * Do we support clustering and is the support enabled
639 * If no, return success always
641 if( !adapter
->has_cluster
) {
642 cmd
->result
= (DID_OK
<< 16);
647 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
652 scb
->raw_mbox
[0] = MEGA_CLUSTER_CMD
;
653 scb
->raw_mbox
[2] = MEGA_RESERVATION_STATUS
;
654 scb
->raw_mbox
[3] = ldrv_num
;
656 scb
->dma_direction
= PCI_DMA_NONE
;
660 cmd
->result
= (DID_OK
<< 16);
667 struct scatterlist
*sg
;
669 sg
= scsi_sglist(cmd
);
670 buf
= kmap_atomic(sg_page(sg
), KM_IRQ0
) + sg
->offset
;
672 memset(buf
, 0, cmd
->cmnd
[4]);
673 kunmap_atomic(buf
- sg
->offset
, KM_IRQ0
);
675 cmd
->result
= (DID_OK
<< 16);
683 if(!(adapter
->flag
& (1L << cmd
->device
->channel
))) {
686 "scsi%d: scanning scsi channel %d ",
687 adapter
->host
->host_no
,
688 cmd
->device
->channel
);
689 printk("for logical drives.\n");
691 adapter
->flag
|= (1L << cmd
->device
->channel
);
694 /* Allocate a SCB and initialize passthru */
695 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
701 mbox
= (mbox_t
*)scb
->raw_mbox
;
702 memset(mbox
, 0, sizeof(scb
->raw_mbox
));
703 memset(pthru
, 0, sizeof(mega_passthru
));
707 pthru
->reqsenselen
= 14;
708 pthru
->islogical
= 1;
709 pthru
->logdrv
= ldrv_num
;
710 pthru
->cdblen
= cmd
->cmd_len
;
711 memcpy(pthru
->cdb
, cmd
->cmnd
, cmd
->cmd_len
);
713 if( adapter
->has_64bit_addr
) {
714 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU64
;
717 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
720 scb
->dma_direction
= PCI_DMA_FROMDEVICE
;
722 pthru
->numsgelements
= mega_build_sglist(adapter
, scb
,
723 &pthru
->dataxferaddr
, &pthru
->dataxferlen
);
725 mbox
->m_out
.xferaddr
= scb
->pthru_dma_addr
;
736 /* Allocate a SCB and initialize mailbox */
737 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
741 mbox
= (mbox_t
*)scb
->raw_mbox
;
743 memset(mbox
, 0, sizeof(scb
->raw_mbox
));
744 mbox
->m_out
.logdrv
= ldrv_num
;
747 * A little hack: 2nd bit is zero for all scsi read
748 * commands and is set for all scsi write commands
750 if( adapter
->has_64bit_addr
) {
751 mbox
->m_out
.cmd
= (*cmd
->cmnd
& 0x02) ?
752 MEGA_MBOXCMD_LWRITE64
:
753 MEGA_MBOXCMD_LREAD64
;
756 mbox
->m_out
.cmd
= (*cmd
->cmnd
& 0x02) ?
762 * 6-byte READ(0x08) or WRITE(0x0A) cdb
764 if( cmd
->cmd_len
== 6 ) {
765 mbox
->m_out
.numsectors
= (u32
) cmd
->cmnd
[4];
767 ((u32
)cmd
->cmnd
[1] << 16) |
768 ((u32
)cmd
->cmnd
[2] << 8) |
771 mbox
->m_out
.lba
&= 0x1FFFFF;
775 * Take modulo 0x80, since the logical drive
776 * number increases by 0x80 when a logical
779 if (*cmd
->cmnd
== READ_6
) {
780 adapter
->nreads
[ldrv_num
%0x80]++;
781 adapter
->nreadblocks
[ldrv_num
%0x80] +=
782 mbox
->m_out
.numsectors
;
784 adapter
->nwrites
[ldrv_num
%0x80]++;
785 adapter
->nwriteblocks
[ldrv_num
%0x80] +=
786 mbox
->m_out
.numsectors
;
792 * 10-byte READ(0x28) or WRITE(0x2A) cdb
794 if( cmd
->cmd_len
== 10 ) {
795 mbox
->m_out
.numsectors
=
797 ((u32
)cmd
->cmnd
[7] << 8);
799 ((u32
)cmd
->cmnd
[2] << 24) |
800 ((u32
)cmd
->cmnd
[3] << 16) |
801 ((u32
)cmd
->cmnd
[4] << 8) |
805 if (*cmd
->cmnd
== READ_10
) {
806 adapter
->nreads
[ldrv_num
%0x80]++;
807 adapter
->nreadblocks
[ldrv_num
%0x80] +=
808 mbox
->m_out
.numsectors
;
810 adapter
->nwrites
[ldrv_num
%0x80]++;
811 adapter
->nwriteblocks
[ldrv_num
%0x80] +=
812 mbox
->m_out
.numsectors
;
818 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
820 if( cmd
->cmd_len
== 12 ) {
822 ((u32
)cmd
->cmnd
[2] << 24) |
823 ((u32
)cmd
->cmnd
[3] << 16) |
824 ((u32
)cmd
->cmnd
[4] << 8) |
827 mbox
->m_out
.numsectors
=
828 ((u32
)cmd
->cmnd
[6] << 24) |
829 ((u32
)cmd
->cmnd
[7] << 16) |
830 ((u32
)cmd
->cmnd
[8] << 8) |
834 if (*cmd
->cmnd
== READ_12
) {
835 adapter
->nreads
[ldrv_num
%0x80]++;
836 adapter
->nreadblocks
[ldrv_num
%0x80] +=
837 mbox
->m_out
.numsectors
;
839 adapter
->nwrites
[ldrv_num
%0x80]++;
840 adapter
->nwriteblocks
[ldrv_num
%0x80] +=
841 mbox
->m_out
.numsectors
;
847 * If it is a read command
849 if( (*cmd
->cmnd
& 0x0F) == 0x08 ) {
850 scb
->dma_direction
= PCI_DMA_FROMDEVICE
;
853 scb
->dma_direction
= PCI_DMA_TODEVICE
;
856 /* Calculate Scatter-Gather info */
857 mbox
->m_out
.numsgelements
= mega_build_sglist(adapter
, scb
,
858 (u32
*)&mbox
->m_out
.xferaddr
, (u32
*)&seg
);
862 #if MEGA_HAVE_CLUSTERING
863 case RESERVE
: /* Fall through */
867 * Do we support clustering and is the support enabled
869 if( ! adapter
->has_cluster
) {
871 cmd
->result
= (DID_BAD_TARGET
<< 16);
876 /* Allocate a SCB and initialize mailbox */
877 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
882 scb
->raw_mbox
[0] = MEGA_CLUSTER_CMD
;
883 scb
->raw_mbox
[2] = ( *cmd
->cmnd
== RESERVE
) ?
884 MEGA_RESERVE_LD
: MEGA_RELEASE_LD
;
886 scb
->raw_mbox
[3] = ldrv_num
;
888 scb
->dma_direction
= PCI_DMA_NONE
;
894 cmd
->result
= (DID_BAD_TARGET
<< 16);
901 * Passthru drive commands
904 /* Allocate a SCB and initialize passthru */
905 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
910 mbox
= (mbox_t
*)scb
->raw_mbox
;
911 memset(mbox
, 0, sizeof(scb
->raw_mbox
));
913 if( adapter
->support_ext_cdb
) {
915 epthru
= mega_prepare_extpassthru(adapter
, scb
, cmd
,
918 mbox
->m_out
.cmd
= MEGA_MBOXCMD_EXTPTHRU
;
920 mbox
->m_out
.xferaddr
= scb
->epthru_dma_addr
;
925 pthru
= mega_prepare_passthru(adapter
, scb
, cmd
,
928 /* Initialize mailbox */
929 if( adapter
->has_64bit_addr
) {
930 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU64
;
933 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
936 mbox
->m_out
.xferaddr
= scb
->pthru_dma_addr
;
946 * mega_prepare_passthru()
947 * @adapter - pointer to our soft state
948 * @scb - our scsi control block
949 * @cmd - scsi command from the mid-layer
950 * @channel - actual channel on the controller
951 * @target - actual id on the controller.
953 * prepare a command for the scsi physical devices.
955 static mega_passthru
*
956 mega_prepare_passthru(adapter_t
*adapter
, scb_t
*scb
, Scsi_Cmnd
*cmd
,
957 int channel
, int target
)
959 mega_passthru
*pthru
;
962 memset(pthru
, 0, sizeof (mega_passthru
));
964 /* 0=6sec/1=60sec/2=10min/3=3hrs */
968 pthru
->reqsenselen
= 14;
969 pthru
->islogical
= 0;
971 pthru
->channel
= (adapter
->flag
& BOARD_40LD
) ? 0 : channel
;
973 pthru
->target
= (adapter
->flag
& BOARD_40LD
) ?
974 (channel
<< 4) | target
: target
;
976 pthru
->cdblen
= cmd
->cmd_len
;
977 pthru
->logdrv
= cmd
->device
->lun
;
979 memcpy(pthru
->cdb
, cmd
->cmnd
, cmd
->cmd_len
);
981 /* Not sure about the direction */
982 scb
->dma_direction
= PCI_DMA_BIDIRECTIONAL
;
984 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
985 switch (cmd
->cmnd
[0]) {
988 if(!(adapter
->flag
& (1L << cmd
->device
->channel
))) {
991 "scsi%d: scanning scsi channel %d [P%d] ",
992 adapter
->host
->host_no
,
993 cmd
->device
->channel
, channel
);
994 printk("for physical devices.\n");
996 adapter
->flag
|= (1L << cmd
->device
->channel
);
1000 pthru
->numsgelements
= mega_build_sglist(adapter
, scb
,
1001 &pthru
->dataxferaddr
, &pthru
->dataxferlen
);
1009 * mega_prepare_extpassthru()
1010 * @adapter - pointer to our soft state
1011 * @scb - our scsi control block
1012 * @cmd - scsi command from the mid-layer
1013 * @channel - actual channel on the controller
1014 * @target - actual id on the controller.
1016 * prepare a command for the scsi physical devices. This rountine prepares
1017 * commands for devices which can take extended CDBs (>10 bytes)
1019 static mega_ext_passthru
*
1020 mega_prepare_extpassthru(adapter_t
*adapter
, scb_t
*scb
, Scsi_Cmnd
*cmd
,
1021 int channel
, int target
)
1023 mega_ext_passthru
*epthru
;
1025 epthru
= scb
->epthru
;
1026 memset(epthru
, 0, sizeof(mega_ext_passthru
));
1028 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1029 epthru
->timeout
= 2;
1032 epthru
->reqsenselen
= 14;
1033 epthru
->islogical
= 0;
1035 epthru
->channel
= (adapter
->flag
& BOARD_40LD
) ? 0 : channel
;
1036 epthru
->target
= (adapter
->flag
& BOARD_40LD
) ?
1037 (channel
<< 4) | target
: target
;
1039 epthru
->cdblen
= cmd
->cmd_len
;
1040 epthru
->logdrv
= cmd
->device
->lun
;
1042 memcpy(epthru
->cdb
, cmd
->cmnd
, cmd
->cmd_len
);
1044 /* Not sure about the direction */
1045 scb
->dma_direction
= PCI_DMA_BIDIRECTIONAL
;
1047 switch(cmd
->cmnd
[0]) {
1050 if(!(adapter
->flag
& (1L << cmd
->device
->channel
))) {
1053 "scsi%d: scanning scsi channel %d [P%d] ",
1054 adapter
->host
->host_no
,
1055 cmd
->device
->channel
, channel
);
1056 printk("for physical devices.\n");
1058 adapter
->flag
|= (1L << cmd
->device
->channel
);
1062 epthru
->numsgelements
= mega_build_sglist(adapter
, scb
,
1063 &epthru
->dataxferaddr
, &epthru
->dataxferlen
);
1071 __mega_runpendq(adapter_t
*adapter
)
1074 struct list_head
*pos
, *next
;
1076 /* Issue any pending commands to the card */
1077 list_for_each_safe(pos
, next
, &adapter
->pending_list
) {
1079 scb
= list_entry(pos
, scb_t
, list
);
1081 if( !(scb
->state
& SCB_ISSUED
) ) {
1083 if( issue_scb(adapter
, scb
) != 0 )
1094 * @adapter - pointer to our soft state
1095 * @scb - scsi control block
1097 * Post a command to the card if the mailbox is available, otherwise return
1098 * busy. We also take the scb from the pending list if the mailbox is
1102 issue_scb(adapter_t
*adapter
, scb_t
*scb
)
1104 volatile mbox64_t
*mbox64
= adapter
->mbox64
;
1105 volatile mbox_t
*mbox
= adapter
->mbox
;
1108 if(unlikely(mbox
->m_in
.busy
)) {
1112 } while( mbox
->m_in
.busy
&& (i
< max_mbox_busy_wait
) );
1114 if(mbox
->m_in
.busy
) return -1;
1117 /* Copy mailbox data into host structure */
1118 memcpy((char *)&mbox
->m_out
, (char *)scb
->raw_mbox
,
1119 sizeof(struct mbox_out
));
1121 mbox
->m_out
.cmdid
= scb
->idx
; /* Set cmdid */
1122 mbox
->m_in
.busy
= 1; /* Set busy */
1126 * Increment the pending queue counter
1128 atomic_inc(&adapter
->pend_cmds
);
1130 switch (mbox
->m_out
.cmd
) {
1131 case MEGA_MBOXCMD_LREAD64
:
1132 case MEGA_MBOXCMD_LWRITE64
:
1133 case MEGA_MBOXCMD_PASSTHRU64
:
1134 case MEGA_MBOXCMD_EXTPTHRU
:
1135 mbox64
->xfer_segment_lo
= mbox
->m_out
.xferaddr
;
1136 mbox64
->xfer_segment_hi
= 0;
1137 mbox
->m_out
.xferaddr
= 0xFFFFFFFF;
1140 mbox64
->xfer_segment_lo
= 0;
1141 mbox64
->xfer_segment_hi
= 0;
1147 scb
->state
|= SCB_ISSUED
;
1149 if( likely(adapter
->flag
& BOARD_MEMMAP
) ) {
1150 mbox
->m_in
.poll
= 0;
1152 WRINDOOR(adapter
, adapter
->mbox_dma
| 0x1);
1155 irq_enable(adapter
);
1156 issue_command(adapter
);
1163 * Wait until the controller's mailbox is available
1166 mega_busywait_mbox (adapter_t
*adapter
)
1168 if (adapter
->mbox
->m_in
.busy
)
1169 return __mega_busywait_mbox(adapter
);
1175 * @adapter - pointer to our soft state
1176 * @raw_mbox - the mailbox
1178 * Issue a scb in synchronous and non-interrupt mode
1181 issue_scb_block(adapter_t
*adapter
, u_char
*raw_mbox
)
1183 volatile mbox64_t
*mbox64
= adapter
->mbox64
;
1184 volatile mbox_t
*mbox
= adapter
->mbox
;
1187 /* Wait until mailbox is free */
1188 if(mega_busywait_mbox (adapter
))
1189 goto bug_blocked_mailbox
;
1191 /* Copy mailbox data into host structure */
1192 memcpy((char *) mbox
, raw_mbox
, sizeof(struct mbox_out
));
1193 mbox
->m_out
.cmdid
= 0xFE;
1194 mbox
->m_in
.busy
= 1;
1196 switch (raw_mbox
[0]) {
1197 case MEGA_MBOXCMD_LREAD64
:
1198 case MEGA_MBOXCMD_LWRITE64
:
1199 case MEGA_MBOXCMD_PASSTHRU64
:
1200 case MEGA_MBOXCMD_EXTPTHRU
:
1201 mbox64
->xfer_segment_lo
= mbox
->m_out
.xferaddr
;
1202 mbox64
->xfer_segment_hi
= 0;
1203 mbox
->m_out
.xferaddr
= 0xFFFFFFFF;
1206 mbox64
->xfer_segment_lo
= 0;
1207 mbox64
->xfer_segment_hi
= 0;
1210 if( likely(adapter
->flag
& BOARD_MEMMAP
) ) {
1211 mbox
->m_in
.poll
= 0;
1213 mbox
->m_in
.numstatus
= 0xFF;
1214 mbox
->m_in
.status
= 0xFF;
1215 WRINDOOR(adapter
, adapter
->mbox_dma
| 0x1);
1217 while((volatile u8
)mbox
->m_in
.numstatus
== 0xFF)
1220 mbox
->m_in
.numstatus
= 0xFF;
1222 while( (volatile u8
)mbox
->m_in
.poll
!= 0x77 )
1225 mbox
->m_in
.poll
= 0;
1226 mbox
->m_in
.ack
= 0x77;
1228 WRINDOOR(adapter
, adapter
->mbox_dma
| 0x2);
1230 while(RDINDOOR(adapter
) & 0x2)
1234 irq_disable(adapter
);
1235 issue_command(adapter
);
1237 while (!((byte
= irq_state(adapter
)) & INTR_VALID
))
1240 set_irq_state(adapter
, byte
);
1241 irq_enable(adapter
);
1245 return mbox
->m_in
.status
;
1247 bug_blocked_mailbox
:
1248 printk(KERN_WARNING
"megaraid: Blocked mailbox......!!\n");
1255 * megaraid_isr_iomapped()
1257 * @devp - pointer to our soft state
1259 * Interrupt service routine for io-mapped controllers.
1260 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1261 * and service the completed commands.
1264 megaraid_isr_iomapped(int irq
, void *devp
)
1266 adapter_t
*adapter
= devp
;
1267 unsigned long flags
;
1270 u8 completed
[MAX_FIRMWARE_STATUS
];
1276 * loop till F/W has more commands for us to complete.
1278 spin_lock_irqsave(&adapter
->lock
, flags
);
1281 /* Check if a valid interrupt is pending */
1282 byte
= irq_state(adapter
);
1283 if( (byte
& VALID_INTR_BYTE
) == 0 ) {
1285 * No more pending commands
1289 set_irq_state(adapter
, byte
);
1291 while((nstatus
= (volatile u8
)adapter
->mbox
->m_in
.numstatus
)
1294 adapter
->mbox
->m_in
.numstatus
= 0xFF;
1296 status
= adapter
->mbox
->m_in
.status
;
1299 * decrement the pending queue counter
1301 atomic_sub(nstatus
, &adapter
->pend_cmds
);
1303 memcpy(completed
, (void *)adapter
->mbox
->m_in
.completed
,
1306 /* Acknowledge interrupt */
1309 mega_cmd_done(adapter
, completed
, nstatus
, status
);
1311 mega_rundoneq(adapter
);
1315 /* Loop through any pending requests */
1316 if(atomic_read(&adapter
->quiescent
) == 0) {
1317 mega_runpendq(adapter
);
1324 spin_unlock_irqrestore(&adapter
->lock
, flags
);
1326 return IRQ_RETVAL(handled
);
1331 * megaraid_isr_memmapped()
1333 * @devp - pointer to our soft state
1335 * Interrupt service routine for memory-mapped controllers.
1336 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1337 * and service the completed commands.
1340 megaraid_isr_memmapped(int irq
, void *devp
)
1342 adapter_t
*adapter
= devp
;
1343 unsigned long flags
;
1347 u8 completed
[MAX_FIRMWARE_STATUS
];
1352 * loop till F/W has more commands for us to complete.
1354 spin_lock_irqsave(&adapter
->lock
, flags
);
1357 /* Check if a valid interrupt is pending */
1358 dword
= RDOUTDOOR(adapter
);
1359 if(dword
!= 0x10001234) {
1361 * No more pending commands
1365 WROUTDOOR(adapter
, 0x10001234);
1367 while((nstatus
= (volatile u8
)adapter
->mbox
->m_in
.numstatus
)
1371 adapter
->mbox
->m_in
.numstatus
= 0xFF;
1373 status
= adapter
->mbox
->m_in
.status
;
1376 * decrement the pending queue counter
1378 atomic_sub(nstatus
, &adapter
->pend_cmds
);
1380 memcpy(completed
, (void *)adapter
->mbox
->m_in
.completed
,
1383 /* Acknowledge interrupt */
1384 WRINDOOR(adapter
, 0x2);
1388 while( RDINDOOR(adapter
) & 0x02 )
1391 mega_cmd_done(adapter
, completed
, nstatus
, status
);
1393 mega_rundoneq(adapter
);
1395 /* Loop through any pending requests */
1396 if(atomic_read(&adapter
->quiescent
) == 0) {
1397 mega_runpendq(adapter
);
1404 spin_unlock_irqrestore(&adapter
->lock
, flags
);
1406 return IRQ_RETVAL(handled
);
1410 * @adapter - pointer to our soft state
1411 * @completed - array of ids of completed commands
1412 * @nstatus - number of completed commands
1413 * @status - status of the last command completed
1415 * Complete the commands and call the scsi mid-layer callback hooks.
1418 mega_cmd_done(adapter_t
*adapter
, u8 completed
[], int nstatus
, int status
)
1420 mega_ext_passthru
*epthru
= NULL
;
1421 struct scatterlist
*sgl
;
1422 Scsi_Cmnd
*cmd
= NULL
;
1423 mega_passthru
*pthru
= NULL
;
1424 mbox_t
*mbox
= NULL
;
1432 * for all the commands completed, call the mid-layer callback routine
1435 for( i
= 0; i
< nstatus
; i
++ ) {
1437 cmdid
= completed
[i
];
1439 if( cmdid
== CMDID_INT_CMDS
) { /* internal command */
1440 scb
= &adapter
->int_scb
;
1442 mbox
= (mbox_t
*)scb
->raw_mbox
;
1445 * Internal command interface do not fire the extended
1446 * passthru or 64-bit passthru
1452 scb
= &adapter
->scb_list
[cmdid
];
1455 * Make sure f/w has completed a valid command
1457 if( !(scb
->state
& SCB_ISSUED
) || scb
->cmd
== NULL
) {
1459 "megaraid: invalid command ");
1460 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1461 cmdid
, scb
->state
, scb
->cmd
);
1467 * Was a abort issued for this command
1469 if( scb
->state
& SCB_ABORT
) {
1472 "megaraid: aborted cmd [%x] complete.\n",
1475 scb
->cmd
->result
= (DID_ABORT
<< 16);
1477 list_add_tail(SCSI_LIST(scb
->cmd
),
1478 &adapter
->completed_list
);
1480 mega_free_scb(adapter
, scb
);
1486 * Was a reset issued for this command
1488 if( scb
->state
& SCB_RESET
) {
1491 "megaraid: reset cmd [%x] complete.\n",
1494 scb
->cmd
->result
= (DID_RESET
<< 16);
1496 list_add_tail(SCSI_LIST(scb
->cmd
),
1497 &adapter
->completed_list
);
1499 mega_free_scb (adapter
, scb
);
1506 epthru
= scb
->epthru
;
1507 mbox
= (mbox_t
*)scb
->raw_mbox
;
1512 int logdrv
= mbox
->m_out
.logdrv
;
1514 islogical
= adapter
->logdrv_chan
[cmd
->channel
];
1516 * Maintain an error counter for the logical drive.
1517 * Some application like SNMP agent need such
1520 if( status
&& islogical
&& (cmd
->cmnd
[0] == READ_6
||
1521 cmd
->cmnd
[0] == READ_10
||
1522 cmd
->cmnd
[0] == READ_12
)) {
1524 * Logical drive number increases by 0x80 when
1525 * a logical drive is deleted
1527 adapter
->rd_errors
[logdrv
%0x80]++;
1530 if( status
&& islogical
&& (cmd
->cmnd
[0] == WRITE_6
||
1531 cmd
->cmnd
[0] == WRITE_10
||
1532 cmd
->cmnd
[0] == WRITE_12
)) {
1534 * Logical drive number increases by 0x80 when
1535 * a logical drive is deleted
1537 adapter
->wr_errors
[logdrv
%0x80]++;
1545 * Do not return the presence of hard disk on the channel so,
1546 * inquiry sent, and returned data==hard disk or removable
1547 * hard disk and not logical, request should return failure! -
1550 islogical
= adapter
->logdrv_chan
[cmd
->device
->channel
];
1551 if( cmd
->cmnd
[0] == INQUIRY
&& !islogical
) {
1553 sgl
= scsi_sglist(cmd
);
1554 if( sg_page(sgl
) ) {
1555 c
= *(unsigned char *) sg_virt(&sgl
[0]);
1558 "megaraid: invalid sg.\n");
1562 if(IS_RAID_CH(adapter
, cmd
->device
->channel
) &&
1563 ((c
& 0x1F ) == TYPE_DISK
)) {
1568 /* clear result; otherwise, success returns corrupt value */
1571 /* Convert MegaRAID status to Linux error code */
1573 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1574 cmd
->result
|= (DID_OK
<< 16);
1577 case 0x02: /* ERROR_ABORTED, i.e.
1578 SCSI_STATUS_CHECK_CONDITION */
1580 /* set sense_buffer and result fields */
1581 if( mbox
->m_out
.cmd
== MEGA_MBOXCMD_PASSTHRU
||
1582 mbox
->m_out
.cmd
== MEGA_MBOXCMD_PASSTHRU64
) {
1584 memcpy(cmd
->sense_buffer
, pthru
->reqsensearea
,
1587 cmd
->result
= (DRIVER_SENSE
<< 24) |
1589 (CHECK_CONDITION
<< 1);
1592 if (mbox
->m_out
.cmd
== MEGA_MBOXCMD_EXTPTHRU
) {
1594 memcpy(cmd
->sense_buffer
,
1595 epthru
->reqsensearea
, 14);
1597 cmd
->result
= (DRIVER_SENSE
<< 24) |
1599 (CHECK_CONDITION
<< 1);
1601 cmd
->sense_buffer
[0] = 0x70;
1602 cmd
->sense_buffer
[2] = ABORTED_COMMAND
;
1603 cmd
->result
|= (CHECK_CONDITION
<< 1);
1608 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1610 cmd
->result
|= (DID_BUS_BUSY
<< 16) | status
;
1614 #if MEGA_HAVE_CLUSTERING
1616 * If TEST_UNIT_READY fails, we know
1617 * MEGA_RESERVATION_STATUS failed
1619 if( cmd
->cmnd
[0] == TEST_UNIT_READY
) {
1620 cmd
->result
|= (DID_ERROR
<< 16) |
1621 (RESERVATION_CONFLICT
<< 1);
1625 * Error code returned is 1 if Reserve or Release
1626 * failed or the input parameter is invalid
1629 (cmd
->cmnd
[0] == RESERVE
||
1630 cmd
->cmnd
[0] == RELEASE
) ) {
1632 cmd
->result
|= (DID_ERROR
<< 16) |
1633 (RESERVATION_CONFLICT
<< 1);
1637 cmd
->result
|= (DID_BAD_TARGET
<< 16)|status
;
1641 * Only free SCBs for the commands coming down from the
1642 * mid-layer, not for which were issued internally
1644 * For internal command, restore the status returned by the
1645 * firmware so that user can interpret it.
1647 if( cmdid
== CMDID_INT_CMDS
) { /* internal command */
1648 cmd
->result
= status
;
1651 * Remove the internal command from the pending list
1653 list_del_init(&scb
->list
);
1654 scb
->state
= SCB_FREE
;
1657 mega_free_scb(adapter
, scb
);
1660 /* Add Scsi_Command to end of completed queue */
1661 list_add_tail(SCSI_LIST(cmd
), &adapter
->completed_list
);
1669 * Run through the list of completed requests and finish it
1672 mega_rundoneq (adapter_t
*adapter
)
1675 struct list_head
*pos
;
1677 list_for_each(pos
, &adapter
->completed_list
) {
1679 struct scsi_pointer
* spos
= (struct scsi_pointer
*)pos
;
1681 cmd
= list_entry(spos
, Scsi_Cmnd
, SCp
);
1682 cmd
->scsi_done(cmd
);
1685 INIT_LIST_HEAD(&adapter
->completed_list
);
1690 * Free a SCB structure
1691 * Note: We assume the scsi commands associated with this scb is not free yet.
1694 mega_free_scb(adapter_t
*adapter
, scb_t
*scb
)
1696 switch( scb
->dma_type
) {
1698 case MEGA_DMA_TYPE_NONE
:
1702 scsi_dma_unmap(scb
->cmd
);
1709 * Remove from the pending list
1711 list_del_init(&scb
->list
);
1713 /* Link the scb back into free list */
1714 scb
->state
= SCB_FREE
;
1717 list_add(&scb
->list
, &adapter
->free_list
);
1722 __mega_busywait_mbox (adapter_t
*adapter
)
1724 volatile mbox_t
*mbox
= adapter
->mbox
;
1727 for (counter
= 0; counter
< 10000; counter
++) {
1728 if (!mbox
->m_in
.busy
)
1733 return -1; /* give up after 1 second */
1737 * Copies data to SGLIST
1738 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1741 mega_build_sglist(adapter_t
*adapter
, scb_t
*scb
, u32
*buf
, u32
*len
)
1743 struct scatterlist
*sg
;
1751 * Copy Scatter-Gather list info into controller structure.
1753 * The number of sg elements returned must not exceed our limit
1755 sgcnt
= scsi_dma_map(cmd
);
1757 scb
->dma_type
= MEGA_SGLIST
;
1759 BUG_ON(sgcnt
> adapter
->sglen
|| sgcnt
< 0);
1763 if (scsi_sg_count(cmd
) == 1 && !adapter
->has_64bit_addr
) {
1764 sg
= scsi_sglist(cmd
);
1765 scb
->dma_h_bulkdata
= sg_dma_address(sg
);
1766 *buf
= (u32
)scb
->dma_h_bulkdata
;
1767 *len
= sg_dma_len(sg
);
1771 scsi_for_each_sg(cmd
, sg
, sgcnt
, idx
) {
1772 if (adapter
->has_64bit_addr
) {
1773 scb
->sgl64
[idx
].address
= sg_dma_address(sg
);
1774 *len
+= scb
->sgl64
[idx
].length
= sg_dma_len(sg
);
1776 scb
->sgl
[idx
].address
= sg_dma_address(sg
);
1777 *len
+= scb
->sgl
[idx
].length
= sg_dma_len(sg
);
1781 /* Reset pointer and length fields */
1782 *buf
= scb
->sgl_dma_addr
;
1784 /* Return count of SG requests */
1792 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1793 * Enquiry3 structures for later use
1796 mega_8_to_40ld(mraid_inquiry
*inquiry
, mega_inquiry3
*enquiry3
,
1797 mega_product_info
*product_info
)
1801 product_info
->max_commands
= inquiry
->adapter_info
.max_commands
;
1802 enquiry3
->rebuild_rate
= inquiry
->adapter_info
.rebuild_rate
;
1803 product_info
->nchannels
= inquiry
->adapter_info
.nchannels
;
1805 for (i
= 0; i
< 4; i
++) {
1806 product_info
->fw_version
[i
] =
1807 inquiry
->adapter_info
.fw_version
[i
];
1809 product_info
->bios_version
[i
] =
1810 inquiry
->adapter_info
.bios_version
[i
];
1812 enquiry3
->cache_flush_interval
=
1813 inquiry
->adapter_info
.cache_flush_interval
;
1815 product_info
->dram_size
= inquiry
->adapter_info
.dram_size
;
1817 enquiry3
->num_ldrv
= inquiry
->logdrv_info
.num_ldrv
;
1819 for (i
= 0; i
< MAX_LOGICAL_DRIVES_8LD
; i
++) {
1820 enquiry3
->ldrv_size
[i
] = inquiry
->logdrv_info
.ldrv_size
[i
];
1821 enquiry3
->ldrv_prop
[i
] = inquiry
->logdrv_info
.ldrv_prop
[i
];
1822 enquiry3
->ldrv_state
[i
] = inquiry
->logdrv_info
.ldrv_state
[i
];
1825 for (i
= 0; i
< (MAX_PHYSICAL_DRIVES
); i
++)
1826 enquiry3
->pdrv_state
[i
] = inquiry
->pdrv_info
.pdrv_state
[i
];
1830 mega_free_sgl(adapter_t
*adapter
)
1835 for(i
= 0; i
< adapter
->max_cmds
; i
++) {
1837 scb
= &adapter
->scb_list
[i
];
1840 pci_free_consistent(adapter
->dev
,
1841 sizeof(mega_sgl64
) * adapter
->sglen
,
1849 pci_free_consistent(adapter
->dev
, sizeof(mega_passthru
),
1850 scb
->pthru
, scb
->pthru_dma_addr
);
1856 pci_free_consistent(adapter
->dev
,
1857 sizeof(mega_ext_passthru
),
1858 scb
->epthru
, scb
->epthru_dma_addr
);
1868 * Get information about the card/driver
1871 megaraid_info(struct Scsi_Host
*host
)
1873 static char buffer
[512];
1876 adapter
= (adapter_t
*)host
->hostdata
;
1879 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1880 adapter
->fw_version
, adapter
->product_info
.max_commands
,
1881 adapter
->host
->max_id
, adapter
->host
->max_channel
,
1882 adapter
->host
->max_lun
);
1887 * Abort a previous SCSI request. Only commands on the pending list can be
1888 * aborted. All the commands issued to the F/W must complete.
1891 megaraid_abort(Scsi_Cmnd
*cmd
)
1896 adapter
= (adapter_t
*)cmd
->device
->host
->hostdata
;
1898 rval
= megaraid_abort_and_reset(adapter
, cmd
, SCB_ABORT
);
1901 * This is required here to complete any completed requests
1902 * to be communicated over to the mid layer.
1904 mega_rundoneq(adapter
);
1911 megaraid_reset(struct scsi_cmnd
*cmd
)
1917 adapter
= (adapter_t
*)cmd
->device
->host
->hostdata
;
1919 #if MEGA_HAVE_CLUSTERING
1920 mc
.cmd
= MEGA_CLUSTER_CMD
;
1921 mc
.opcode
= MEGA_RESET_RESERVATIONS
;
1923 if( mega_internal_command(adapter
, &mc
, NULL
) != 0 ) {
1925 "megaraid: reservation reset failed.\n");
1928 printk(KERN_INFO
"megaraid: reservation reset.\n");
1932 spin_lock_irq(&adapter
->lock
);
1934 rval
= megaraid_abort_and_reset(adapter
, cmd
, SCB_RESET
);
1937 * This is required here to complete any completed requests
1938 * to be communicated over to the mid layer.
1940 mega_rundoneq(adapter
);
1941 spin_unlock_irq(&adapter
->lock
);
1947 * megaraid_abort_and_reset()
1948 * @adapter - megaraid soft state
1949 * @cmd - scsi command to be aborted or reset
1950 * @aor - abort or reset flag
1952 * Try to locate the scsi command in the pending queue. If found and is not
1953 * issued to the controller, abort/reset it. Otherwise return failure
1956 megaraid_abort_and_reset(adapter_t
*adapter
, Scsi_Cmnd
*cmd
, int aor
)
1958 struct list_head
*pos
, *next
;
1961 printk(KERN_WARNING
"megaraid: %s cmd=%x <c=%d t=%d l=%d>\n",
1962 (aor
== SCB_ABORT
)? "ABORTING":"RESET",
1963 cmd
->cmnd
[0], cmd
->device
->channel
,
1964 cmd
->device
->id
, cmd
->device
->lun
);
1966 if(list_empty(&adapter
->pending_list
))
1969 list_for_each_safe(pos
, next
, &adapter
->pending_list
) {
1971 scb
= list_entry(pos
, scb_t
, list
);
1973 if (scb
->cmd
== cmd
) { /* Found command */
1978 * Check if this command has firmware ownership. If
1979 * yes, we cannot reset this command. Whenever f/w
1980 * completes this command, we will return appropriate
1983 if( scb
->state
& SCB_ISSUED
) {
1986 "megaraid: %s[%x], fw owner.\n",
1987 (aor
==SCB_ABORT
) ? "ABORTING":"RESET",
1995 * Not yet issued! Remove from the pending
1999 "megaraid: %s-[%x], driver owner.\n",
2000 (aor
==SCB_ABORT
) ? "ABORTING":"RESET",
2003 mega_free_scb(adapter
, scb
);
2005 if( aor
== SCB_ABORT
) {
2006 cmd
->result
= (DID_ABORT
<< 16);
2009 cmd
->result
= (DID_RESET
<< 16);
2012 list_add_tail(SCSI_LIST(cmd
),
2013 &adapter
->completed_list
);
2024 make_local_pdev(adapter_t
*adapter
, struct pci_dev
**pdev
)
2026 *pdev
= alloc_pci_dev();
2028 if( *pdev
== NULL
) return -1;
2030 memcpy(*pdev
, adapter
->dev
, sizeof(struct pci_dev
));
2032 if( pci_set_dma_mask(*pdev
, DMA_BIT_MASK(32)) != 0 ) {
2041 free_local_pdev(struct pci_dev
*pdev
)
2047 * mega_allocate_inquiry()
2048 * @dma_handle - handle returned for dma address
2049 * @pdev - handle to pci device
2051 * allocates memory for inquiry structure
2053 static inline void *
2054 mega_allocate_inquiry(dma_addr_t
*dma_handle
, struct pci_dev
*pdev
)
2056 return pci_alloc_consistent(pdev
, sizeof(mega_inquiry3
), dma_handle
);
2061 mega_free_inquiry(void *inquiry
, dma_addr_t dma_handle
, struct pci_dev
*pdev
)
2063 pci_free_consistent(pdev
, sizeof(mega_inquiry3
), inquiry
, dma_handle
);
2067 #ifdef CONFIG_PROC_FS
2068 /* Following code handles /proc fs */
2070 #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
2071 S_IRUSR | S_IFREG, \
2072 controller_proc_dir_entry, \
2076 * mega_create_proc_entry()
2077 * @index - index in soft state array
2078 * @parent - parent node for this /proc entry
2080 * Creates /proc entries for our controllers.
2083 mega_create_proc_entry(int index
, struct proc_dir_entry
*parent
)
2085 struct proc_dir_entry
*controller_proc_dir_entry
= NULL
;
2086 u8 string
[64] = { 0 };
2087 adapter_t
*adapter
= hba_soft_state
[index
];
2089 sprintf(string
, "hba%d", adapter
->host
->host_no
);
2091 controller_proc_dir_entry
=
2092 adapter
->controller_proc_dir_entry
= proc_mkdir(string
, parent
);
2094 if(!controller_proc_dir_entry
) {
2095 printk(KERN_WARNING
"\nmegaraid: proc_mkdir failed\n");
2098 adapter
->proc_read
= CREATE_READ_PROC("config", proc_read_config
);
2099 adapter
->proc_stat
= CREATE_READ_PROC("stat", proc_read_stat
);
2100 adapter
->proc_mbox
= CREATE_READ_PROC("mailbox", proc_read_mbox
);
2101 #if MEGA_HAVE_ENH_PROC
2102 adapter
->proc_rr
= CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate
);
2103 adapter
->proc_battery
= CREATE_READ_PROC("battery-status",
2107 * Display each physical drive on its channel
2109 adapter
->proc_pdrvstat
[0] = CREATE_READ_PROC("diskdrives-ch0",
2111 adapter
->proc_pdrvstat
[1] = CREATE_READ_PROC("diskdrives-ch1",
2113 adapter
->proc_pdrvstat
[2] = CREATE_READ_PROC("diskdrives-ch2",
2115 adapter
->proc_pdrvstat
[3] = CREATE_READ_PROC("diskdrives-ch3",
2119 * Display a set of up to 10 logical drive through each of following
2122 adapter
->proc_rdrvstat
[0] = CREATE_READ_PROC("raiddrives-0-9",
2124 adapter
->proc_rdrvstat
[1] = CREATE_READ_PROC("raiddrives-10-19",
2126 adapter
->proc_rdrvstat
[2] = CREATE_READ_PROC("raiddrives-20-29",
2128 adapter
->proc_rdrvstat
[3] = CREATE_READ_PROC("raiddrives-30-39",
2135 * proc_read_config()
2136 * @page - buffer to write the data in
2137 * @start - where the actual data has been written in page
2138 * @offset - same meaning as the read system call
2139 * @count - same meaning as the read system call
2140 * @eof - set if no more data needs to be returned
2141 * @data - pointer to our soft state
2143 * Display configuration information about the controller.
2146 proc_read_config(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2150 adapter_t
*adapter
= (adapter_t
*)data
;
2153 len
+= sprintf(page
+len
, "%s", MEGARAID_VERSION
);
2155 if(adapter
->product_info
.product_name
[0])
2156 len
+= sprintf(page
+len
, "%s\n",
2157 adapter
->product_info
.product_name
);
2159 len
+= sprintf(page
+len
, "Controller Type: ");
2161 if( adapter
->flag
& BOARD_MEMMAP
) {
2162 len
+= sprintf(page
+len
,
2163 "438/466/467/471/493/518/520/531/532\n");
2166 len
+= sprintf(page
+len
,
2170 if(adapter
->flag
& BOARD_40LD
) {
2171 len
+= sprintf(page
+len
,
2172 "Controller Supports 40 Logical Drives\n");
2175 if(adapter
->flag
& BOARD_64BIT
) {
2176 len
+= sprintf(page
+len
,
2177 "Controller capable of 64-bit memory addressing\n");
2179 if( adapter
->has_64bit_addr
) {
2180 len
+= sprintf(page
+len
,
2181 "Controller using 64-bit memory addressing\n");
2184 len
+= sprintf(page
+len
,
2185 "Controller is not using 64-bit memory addressing\n");
2188 len
+= sprintf(page
+len
, "Base = %08lx, Irq = %d, ", adapter
->base
,
2189 adapter
->host
->irq
);
2191 len
+= sprintf(page
+len
, "Logical Drives = %d, Channels = %d\n",
2192 adapter
->numldrv
, adapter
->product_info
.nchannels
);
2194 len
+= sprintf(page
+len
, "Version =%s:%s, DRAM = %dMb\n",
2195 adapter
->fw_version
, adapter
->bios_version
,
2196 adapter
->product_info
.dram_size
);
2198 len
+= sprintf(page
+len
,
2199 "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2200 adapter
->product_info
.max_commands
, adapter
->max_cmds
);
2202 len
+= sprintf(page
+len
, "support_ext_cdb = %d\n",
2203 adapter
->support_ext_cdb
);
2204 len
+= sprintf(page
+len
, "support_random_del = %d\n",
2205 adapter
->support_random_del
);
2206 len
+= sprintf(page
+len
, "boot_ldrv_enabled = %d\n",
2207 adapter
->boot_ldrv_enabled
);
2208 len
+= sprintf(page
+len
, "boot_ldrv = %d\n",
2209 adapter
->boot_ldrv
);
2210 len
+= sprintf(page
+len
, "boot_pdrv_enabled = %d\n",
2211 adapter
->boot_pdrv_enabled
);
2212 len
+= sprintf(page
+len
, "boot_pdrv_ch = %d\n",
2213 adapter
->boot_pdrv_ch
);
2214 len
+= sprintf(page
+len
, "boot_pdrv_tgt = %d\n",
2215 adapter
->boot_pdrv_tgt
);
2216 len
+= sprintf(page
+len
, "quiescent = %d\n",
2217 atomic_read(&adapter
->quiescent
));
2218 len
+= sprintf(page
+len
, "has_cluster = %d\n",
2219 adapter
->has_cluster
);
2221 len
+= sprintf(page
+len
, "\nModule Parameters:\n");
2222 len
+= sprintf(page
+len
, "max_cmd_per_lun = %d\n",
2224 len
+= sprintf(page
+len
, "max_sectors_per_io = %d\n",
2225 max_sectors_per_io
);
2236 * @page - buffer to write the data in
2237 * @start - where the actual data has been written in page
2238 * @offset - same meaning as the read system call
2239 * @count - same meaning as the read system call
2240 * @eof - set if no more data needs to be returned
2241 * @data - pointer to our soft state
2243 * Diaplay statistical information about the I/O activity.
2246 proc_read_stat(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2253 i
= 0; /* avoid compilation warnings */
2255 adapter
= (adapter_t
*)data
;
2257 len
= sprintf(page
, "Statistical Information for this controller\n");
2258 len
+= sprintf(page
+len
, "pend_cmds = %d\n",
2259 atomic_read(&adapter
->pend_cmds
));
2261 for(i
= 0; i
< adapter
->numldrv
; i
++) {
2262 len
+= sprintf(page
+len
, "Logical Drive %d:\n", i
);
2264 len
+= sprintf(page
+len
,
2265 "\tReads Issued = %lu, Writes Issued = %lu\n",
2266 adapter
->nreads
[i
], adapter
->nwrites
[i
]);
2268 len
+= sprintf(page
+len
,
2269 "\tSectors Read = %lu, Sectors Written = %lu\n",
2270 adapter
->nreadblocks
[i
], adapter
->nwriteblocks
[i
]);
2272 len
+= sprintf(page
+len
,
2273 "\tRead errors = %lu, Write errors = %lu\n\n",
2274 adapter
->rd_errors
[i
], adapter
->wr_errors
[i
]);
2277 len
+= sprintf(page
+len
,
2278 "IO and error counters not compiled in driver.\n");
2289 * @page - buffer to write the data in
2290 * @start - where the actual data has been written in page
2291 * @offset - same meaning as the read system call
2292 * @count - same meaning as the read system call
2293 * @eof - set if no more data needs to be returned
2294 * @data - pointer to our soft state
2296 * Display mailbox information for the last command issued. This information
2297 * is good for debugging.
2300 proc_read_mbox(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2304 adapter_t
*adapter
= (adapter_t
*)data
;
2305 volatile mbox_t
*mbox
= adapter
->mbox
;
2308 len
= sprintf(page
, "Contents of Mail Box Structure\n");
2309 len
+= sprintf(page
+len
, " Fw Command = 0x%02x\n",
2311 len
+= sprintf(page
+len
, " Cmd Sequence = 0x%02x\n",
2313 len
+= sprintf(page
+len
, " No of Sectors= %04d\n",
2314 mbox
->m_out
.numsectors
);
2315 len
+= sprintf(page
+len
, " LBA = 0x%02x\n",
2317 len
+= sprintf(page
+len
, " DTA = 0x%08x\n",
2318 mbox
->m_out
.xferaddr
);
2319 len
+= sprintf(page
+len
, " Logical Drive= 0x%02x\n",
2320 mbox
->m_out
.logdrv
);
2321 len
+= sprintf(page
+len
, " No of SG Elmt= 0x%02x\n",
2322 mbox
->m_out
.numsgelements
);
2323 len
+= sprintf(page
+len
, " Busy = %01x\n",
2325 len
+= sprintf(page
+len
, " Status = 0x%02x\n",
2335 * proc_rebuild_rate()
2336 * @page - buffer to write the data in
2337 * @start - where the actual data has been written in page
2338 * @offset - same meaning as the read system call
2339 * @count - same meaning as the read system call
2340 * @eof - set if no more data needs to be returned
2341 * @data - pointer to our soft state
2343 * Display current rebuild rate
2346 proc_rebuild_rate(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2349 adapter_t
*adapter
= (adapter_t
*)data
;
2350 dma_addr_t dma_handle
;
2352 struct pci_dev
*pdev
;
2355 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2360 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2361 free_local_pdev(pdev
);
2366 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2368 len
= sprintf(page
, "Adapter inquiry failed.\n");
2370 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2372 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2374 free_local_pdev(pdev
);
2381 if( adapter
->flag
& BOARD_40LD
) {
2382 len
= sprintf(page
, "Rebuild Rate: [%d%%]\n",
2383 ((mega_inquiry3
*)inquiry
)->rebuild_rate
);
2386 len
= sprintf(page
, "Rebuild Rate: [%d%%]\n",
2387 ((mraid_ext_inquiry
*)
2388 inquiry
)->raid_inq
.adapter_info
.rebuild_rate
);
2392 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2394 free_local_pdev(pdev
);
2404 * @page - buffer to write the data in
2405 * @start - where the actual data has been written in page
2406 * @offset - same meaning as the read system call
2407 * @count - same meaning as the read system call
2408 * @eof - set if no more data needs to be returned
2409 * @data - pointer to our soft state
2411 * Display information about the battery module on the controller.
2414 proc_battery(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2417 adapter_t
*adapter
= (adapter_t
*)data
;
2418 dma_addr_t dma_handle
;
2420 struct pci_dev
*pdev
;
2421 u8 battery_status
= 0;
2425 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2430 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2431 free_local_pdev(pdev
);
2436 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2438 len
= sprintf(page
, "Adapter inquiry failed.\n");
2440 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2442 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2444 free_local_pdev(pdev
);
2451 if( adapter
->flag
& BOARD_40LD
) {
2452 battery_status
= ((mega_inquiry3
*)inquiry
)->battery_status
;
2455 battery_status
= ((mraid_ext_inquiry
*)inquiry
)->
2456 raid_inq
.adapter_info
.battery_status
;
2460 * Decode the battery status
2462 sprintf(str
, "Battery Status:[%d]", battery_status
);
2464 if(battery_status
== MEGA_BATT_CHARGE_DONE
)
2465 strcat(str
, " Charge Done");
2467 if(battery_status
& MEGA_BATT_MODULE_MISSING
)
2468 strcat(str
, " Module Missing");
2470 if(battery_status
& MEGA_BATT_LOW_VOLTAGE
)
2471 strcat(str
, " Low Voltage");
2473 if(battery_status
& MEGA_BATT_TEMP_HIGH
)
2474 strcat(str
, " Temperature High");
2476 if(battery_status
& MEGA_BATT_PACK_MISSING
)
2477 strcat(str
, " Pack Missing");
2479 if(battery_status
& MEGA_BATT_CHARGE_INPROG
)
2480 strcat(str
, " Charge In-progress");
2482 if(battery_status
& MEGA_BATT_CHARGE_FAIL
)
2483 strcat(str
, " Charge Fail");
2485 if(battery_status
& MEGA_BATT_CYCLES_EXCEEDED
)
2486 strcat(str
, " Cycles Exceeded");
2488 len
= sprintf(page
, "%s\n", str
);
2491 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2493 free_local_pdev(pdev
);
2503 * @page - buffer to write the data in
2504 * @start - where the actual data has been written in page
2505 * @offset - same meaning as the read system call
2506 * @count - same meaning as the read system call
2507 * @eof - set if no more data needs to be returned
2508 * @data - pointer to our soft state
2510 * Display information about the physical drives on physical channel 0.
2513 proc_pdrv_ch0(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2516 adapter_t
*adapter
= (adapter_t
*)data
;
2520 return (proc_pdrv(adapter
, page
, 0));
2526 * @page - buffer to write the data in
2527 * @start - where the actual data has been written in page
2528 * @offset - same meaning as the read system call
2529 * @count - same meaning as the read system call
2530 * @eof - set if no more data needs to be returned
2531 * @data - pointer to our soft state
2533 * Display information about the physical drives on physical channel 1.
2536 proc_pdrv_ch1(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2539 adapter_t
*adapter
= (adapter_t
*)data
;
2543 return (proc_pdrv(adapter
, page
, 1));
2549 * @page - buffer to write the data in
2550 * @start - where the actual data has been written in page
2551 * @offset - same meaning as the read system call
2552 * @count - same meaning as the read system call
2553 * @eof - set if no more data needs to be returned
2554 * @data - pointer to our soft state
2556 * Display information about the physical drives on physical channel 2.
2559 proc_pdrv_ch2(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2562 adapter_t
*adapter
= (adapter_t
*)data
;
2566 return (proc_pdrv(adapter
, page
, 2));
2572 * @page - buffer to write the data in
2573 * @start - where the actual data has been written in page
2574 * @offset - same meaning as the read system call
2575 * @count - same meaning as the read system call
2576 * @eof - set if no more data needs to be returned
2577 * @data - pointer to our soft state
2579 * Display information about the physical drives on physical channel 3.
2582 proc_pdrv_ch3(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2585 adapter_t
*adapter
= (adapter_t
*)data
;
2589 return (proc_pdrv(adapter
, page
, 3));
2595 * @page - buffer to write the data in
2596 * @adapter - pointer to our soft state
2598 * Display information about the physical drives.
2601 proc_pdrv(adapter_t
*adapter
, char *page
, int channel
)
2603 dma_addr_t dma_handle
;
2605 dma_addr_t scsi_inq_dma_handle
;
2607 struct pci_dev
*pdev
;
2616 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2620 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2624 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2625 len
= sprintf(page
, "Adapter inquiry failed.\n");
2627 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2633 scsi_inq
= pci_alloc_consistent(pdev
, 256, &scsi_inq_dma_handle
);
2635 if( scsi_inq
== NULL
) {
2636 len
= sprintf(page
, "memory not available for scsi inq.\n");
2641 if( adapter
->flag
& BOARD_40LD
) {
2642 pdrv_state
= ((mega_inquiry3
*)inquiry
)->pdrv_state
;
2645 pdrv_state
= ((mraid_ext_inquiry
*)inquiry
)->
2646 raid_inq
.pdrv_info
.pdrv_state
;
2649 max_channels
= adapter
->product_info
.nchannels
;
2651 if( channel
>= max_channels
) {
2655 for( tgt
= 0; tgt
<= MAX_TARGET
; tgt
++ ) {
2657 i
= channel
*16 + tgt
;
2659 state
= *(pdrv_state
+ i
);
2661 switch( state
& 0x0F ) {
2665 "Channel:%2d Id:%2d State: Online",
2671 "Channel:%2d Id:%2d State: Failed",
2677 "Channel:%2d Id:%2d State: Rebuild",
2683 "Channel:%2d Id:%2d State: Hot spare",
2689 "Channel:%2d Id:%2d State: Un-configured",
2696 * This interface displays inquiries for disk drives
2697 * only. Inquries for logical drives and non-disk
2698 * devices are available through /proc/scsi/scsi
2700 memset(scsi_inq
, 0, 256);
2701 if( mega_internal_dev_inquiry(adapter
, channel
, tgt
,
2702 scsi_inq_dma_handle
) ||
2703 (scsi_inq
[0] & 0x1F) != TYPE_DISK
) {
2708 * Check for overflow. We print less than 240
2709 * characters for inquiry
2711 if( (len
+ 240) >= PAGE_SIZE
) break;
2713 len
+= sprintf(page
+len
, "%s.\n", str
);
2715 len
+= mega_print_inquiry(page
+len
, scsi_inq
);
2719 pci_free_consistent(pdev
, 256, scsi_inq
, scsi_inq_dma_handle
);
2721 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2723 free_local_pdev(pdev
);
2730 * Display scsi inquiry
2733 mega_print_inquiry(char *page
, char *scsi_inq
)
2738 len
= sprintf(page
, " Vendor: ");
2739 for( i
= 8; i
< 16; i
++ ) {
2740 len
+= sprintf(page
+len
, "%c", scsi_inq
[i
]);
2743 len
+= sprintf(page
+len
, " Model: ");
2745 for( i
= 16; i
< 32; i
++ ) {
2746 len
+= sprintf(page
+len
, "%c", scsi_inq
[i
]);
2749 len
+= sprintf(page
+len
, " Rev: ");
2751 for( i
= 32; i
< 36; i
++ ) {
2752 len
+= sprintf(page
+len
, "%c", scsi_inq
[i
]);
2755 len
+= sprintf(page
+len
, "\n");
2757 i
= scsi_inq
[0] & 0x1f;
2759 len
+= sprintf(page
+len
, " Type: %s ", scsi_device_type(i
));
2761 len
+= sprintf(page
+len
,
2762 " ANSI SCSI revision: %02x", scsi_inq
[2] & 0x07);
2764 if( (scsi_inq
[2] & 0x07) == 1 && (scsi_inq
[3] & 0x0f) == 1 )
2765 len
+= sprintf(page
+len
, " CCS\n");
2767 len
+= sprintf(page
+len
, "\n");
2775 * @page - buffer to write the data in
2776 * @start - where the actual data has been written in page
2777 * @offset - same meaning as the read system call
2778 * @count - same meaning as the read system call
2779 * @eof - set if no more data needs to be returned
2780 * @data - pointer to our soft state
2782 * Display real time information about the logical drives 0 through 9.
2785 proc_rdrv_10(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2788 adapter_t
*adapter
= (adapter_t
*)data
;
2792 return (proc_rdrv(adapter
, page
, 0, 9));
2798 * @page - buffer to write the data in
2799 * @start - where the actual data has been written in page
2800 * @offset - same meaning as the read system call
2801 * @count - same meaning as the read system call
2802 * @eof - set if no more data needs to be returned
2803 * @data - pointer to our soft state
2805 * Display real time information about the logical drives 0 through 9.
2808 proc_rdrv_20(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2811 adapter_t
*adapter
= (adapter_t
*)data
;
2815 return (proc_rdrv(adapter
, page
, 10, 19));
2821 * @page - buffer to write the data in
2822 * @start - where the actual data has been written in page
2823 * @offset - same meaning as the read system call
2824 * @count - same meaning as the read system call
2825 * @eof - set if no more data needs to be returned
2826 * @data - pointer to our soft state
2828 * Display real time information about the logical drives 0 through 9.
2831 proc_rdrv_30(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2834 adapter_t
*adapter
= (adapter_t
*)data
;
2838 return (proc_rdrv(adapter
, page
, 20, 29));
2844 * @page - buffer to write the data in
2845 * @start - where the actual data has been written in page
2846 * @offset - same meaning as the read system call
2847 * @count - same meaning as the read system call
2848 * @eof - set if no more data needs to be returned
2849 * @data - pointer to our soft state
2851 * Display real time information about the logical drives 0 through 9.
2854 proc_rdrv_40(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2857 adapter_t
*adapter
= (adapter_t
*)data
;
2861 return (proc_rdrv(adapter
, page
, 30, 39));
2867 * @page - buffer to write the data in
2868 * @adapter - pointer to our soft state
2869 * @start - starting logical drive to display
2870 * @end - ending logical drive to display
2872 * We do not print the inquiry information since its already available through
2873 * /proc/scsi/scsi interface
2876 proc_rdrv(adapter_t
*adapter
, char *page
, int start
, int end
)
2878 dma_addr_t dma_handle
;
2879 logdrv_param
*lparam
;
2882 dma_addr_t disk_array_dma_handle
;
2884 struct pci_dev
*pdev
;
2891 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2895 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2896 free_local_pdev(pdev
);
2900 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2902 len
= sprintf(page
, "Adapter inquiry failed.\n");
2904 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2906 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2908 free_local_pdev(pdev
);
2913 memset(&mc
, 0, sizeof(megacmd_t
));
2915 if( adapter
->flag
& BOARD_40LD
) {
2916 array_sz
= sizeof(disk_array_40ld
);
2918 rdrv_state
= ((mega_inquiry3
*)inquiry
)->ldrv_state
;
2920 num_ldrv
= ((mega_inquiry3
*)inquiry
)->num_ldrv
;
2923 array_sz
= sizeof(disk_array_8ld
);
2925 rdrv_state
= ((mraid_ext_inquiry
*)inquiry
)->
2926 raid_inq
.logdrv_info
.ldrv_state
;
2928 num_ldrv
= ((mraid_ext_inquiry
*)inquiry
)->
2929 raid_inq
.logdrv_info
.num_ldrv
;
2932 disk_array
= pci_alloc_consistent(pdev
, array_sz
,
2933 &disk_array_dma_handle
);
2935 if( disk_array
== NULL
) {
2936 len
= sprintf(page
, "memory not available.\n");
2938 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2940 free_local_pdev(pdev
);
2945 mc
.xferaddr
= (u32
)disk_array_dma_handle
;
2947 if( adapter
->flag
& BOARD_40LD
) {
2948 mc
.cmd
= FC_NEW_CONFIG
;
2949 mc
.opcode
= OP_DCMD_READ_CONFIG
;
2951 if( mega_internal_command(adapter
, &mc
, NULL
) ) {
2953 len
= sprintf(page
, "40LD read config failed.\n");
2955 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2957 pci_free_consistent(pdev
, array_sz
, disk_array
,
2958 disk_array_dma_handle
);
2960 free_local_pdev(pdev
);
2967 mc
.cmd
= NEW_READ_CONFIG_8LD
;
2969 if( mega_internal_command(adapter
, &mc
, NULL
) ) {
2971 mc
.cmd
= READ_CONFIG_8LD
;
2973 if( mega_internal_command(adapter
, &mc
,
2977 "8LD read config failed.\n");
2979 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2981 pci_free_consistent(pdev
, array_sz
,
2983 disk_array_dma_handle
);
2985 free_local_pdev(pdev
);
2992 for( i
= start
; i
< ( (end
+1 < num_ldrv
) ? end
+1 : num_ldrv
); i
++ ) {
2994 if( adapter
->flag
& BOARD_40LD
) {
2996 &((disk_array_40ld
*)disk_array
)->ldrv
[i
].lparam
;
3000 &((disk_array_8ld
*)disk_array
)->ldrv
[i
].lparam
;
3004 * Check for overflow. We print less than 240 characters for
3005 * information about each logical drive.
3007 if( (len
+ 240) >= PAGE_SIZE
) break;
3009 len
+= sprintf(page
+len
, "Logical drive:%2d:, ", i
);
3011 switch( rdrv_state
[i
] & 0x0F ) {
3013 len
+= sprintf(page
+len
, "state: offline");
3017 len
+= sprintf(page
+len
, "state: degraded");
3021 len
+= sprintf(page
+len
, "state: optimal");
3025 len
+= sprintf(page
+len
, "state: deleted");
3029 len
+= sprintf(page
+len
, "state: unknown");
3034 * Check if check consistency or initialization is going on
3035 * for this logical drive.
3037 if( (rdrv_state
[i
] & 0xF0) == 0x20 ) {
3038 len
+= sprintf(page
+len
,
3039 ", check-consistency in progress");
3041 else if( (rdrv_state
[i
] & 0xF0) == 0x10 ) {
3042 len
+= sprintf(page
+len
,
3043 ", initialization in progress");
3046 len
+= sprintf(page
+len
, "\n");
3048 len
+= sprintf(page
+len
, "Span depth:%3d, ",
3049 lparam
->span_depth
);
3051 len
+= sprintf(page
+len
, "RAID level:%3d, ",
3054 len
+= sprintf(page
+len
, "Stripe size:%3d, ",
3055 lparam
->stripe_sz
? lparam
->stripe_sz
/2: 128);
3057 len
+= sprintf(page
+len
, "Row size:%3d\n",
3061 len
+= sprintf(page
+len
, "Read Policy: ");
3063 switch(lparam
->read_ahead
) {
3066 len
+= sprintf(page
+len
, "No read ahead, ");
3070 len
+= sprintf(page
+len
, "Read ahead, ");
3073 case ADAP_READ_AHEAD
:
3074 len
+= sprintf(page
+len
, "Adaptive, ");
3079 len
+= sprintf(page
+len
, "Write Policy: ");
3081 switch(lparam
->write_mode
) {
3083 case WRMODE_WRITE_THRU
:
3084 len
+= sprintf(page
+len
, "Write thru, ");
3087 case WRMODE_WRITE_BACK
:
3088 len
+= sprintf(page
+len
, "Write back, ");
3092 len
+= sprintf(page
+len
, "Cache Policy: ");
3094 switch(lparam
->direct_io
) {
3097 len
+= sprintf(page
+len
, "Cached IO\n\n");
3101 len
+= sprintf(page
+len
, "Direct IO\n\n");
3106 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
3108 pci_free_consistent(pdev
, array_sz
, disk_array
,
3109 disk_array_dma_handle
);
3111 free_local_pdev(pdev
);
3116 static inline void mega_create_proc_entry(int index
, struct proc_dir_entry
*parent
)
3123 * megaraid_biosparam()
3125 * Return the disk geometry for a particular disk
3128 megaraid_biosparam(struct scsi_device
*sdev
, struct block_device
*bdev
,
3129 sector_t capacity
, int geom
[])
3138 /* Get pointer to host config structure */
3139 adapter
= (adapter_t
*)sdev
->host
->hostdata
;
3141 if (IS_RAID_CH(adapter
, sdev
->channel
)) {
3142 /* Default heads (64) & sectors (32) */
3145 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3148 * Handle extended translation size for logical drives
3151 if ((ulong
)capacity
>= 0x200000) {
3154 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3160 geom
[2] = cylinders
;
3163 bh
= scsi_bios_ptable(bdev
);
3166 rval
= scsi_partsize(bh
, capacity
,
3167 &geom
[2], &geom
[0], &geom
[1]);
3174 "megaraid: invalid partition on this disk on channel %d\n",
3177 /* Default heads (64) & sectors (32) */
3180 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3182 /* Handle extended translation size for logical drives > 1Gb */
3183 if ((ulong
)capacity
>= 0x200000) {
3186 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3192 geom
[2] = cylinders
;
3200 * @adapter - pointer to our soft state
3202 * Allocate memory for the various pointers in the scb structures:
3203 * scatter-gather list pointer, passthru and extended passthru structure
3207 mega_init_scb(adapter_t
*adapter
)
3212 for( i
= 0; i
< adapter
->max_cmds
; i
++ ) {
3214 scb
= &adapter
->scb_list
[i
];
3222 for( i
= 0; i
< adapter
->max_cmds
; i
++ ) {
3224 scb
= &adapter
->scb_list
[i
];
3228 scb
->sgl64
= pci_alloc_consistent(adapter
->dev
,
3229 sizeof(mega_sgl64
) * adapter
->sglen
,
3230 &scb
->sgl_dma_addr
);
3232 scb
->sgl
= (mega_sglist
*)scb
->sgl64
;
3235 printk(KERN_WARNING
"RAID: Can't allocate sglist.\n");
3236 mega_free_sgl(adapter
);
3240 scb
->pthru
= pci_alloc_consistent(adapter
->dev
,
3241 sizeof(mega_passthru
),
3242 &scb
->pthru_dma_addr
);
3245 printk(KERN_WARNING
"RAID: Can't allocate passthru.\n");
3246 mega_free_sgl(adapter
);
3250 scb
->epthru
= pci_alloc_consistent(adapter
->dev
,
3251 sizeof(mega_ext_passthru
),
3252 &scb
->epthru_dma_addr
);
3254 if( !scb
->epthru
) {
3256 "Can't allocate extended passthru.\n");
3257 mega_free_sgl(adapter
);
3262 scb
->dma_type
= MEGA_DMA_TYPE_NONE
;
3266 * lock not required since we are loading the driver, so no
3267 * commands possible right now.
3269 scb
->state
= SCB_FREE
;
3271 list_add(&scb
->list
, &adapter
->free_list
);
3283 * Routines for the character/ioctl interface to the driver. Find out if this
3287 megadev_open (struct inode
*inode
, struct file
*filep
)
3290 * Only allow superuser to access private ioctl interface
3292 if( !capable(CAP_SYS_ADMIN
) ) return -EACCES
;
3300 * @inode - Our device inode
3302 * @cmd - ioctl command
3303 * @arg - user buffer
3305 * ioctl entry point for our private ioctl interface. We move the data in from
3306 * the user space, prepare the command (if necessary, convert the old MIMD
3307 * ioctl to new ioctl command), and issue a synchronous command to the
3311 megadev_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
3317 mega_passthru __user
*upthru
; /* user address for passthru */
3318 mega_passthru
*pthru
; /* copy user passthru here */
3319 dma_addr_t pthru_dma_hndl
;
3320 void *data
= NULL
; /* data to be transferred */
3321 dma_addr_t data_dma_hndl
; /* dma handle for data xfer area */
3323 megastat_t __user
*ustats
;
3326 struct pci_dev
*pdev
;
3328 ustats
= NULL
; /* avoid compilation warnings */
3332 * Make sure only USCSICMD are issued through this interface.
3333 * MIMD application would still fire different command.
3335 if( (_IOC_TYPE(cmd
) != MEGAIOC_MAGIC
) && (cmd
!= USCSICMD
) ) {
3340 * Check and convert a possible MIMD command to NIT command.
3341 * mega_m_to_n() copies the data from the user space, so we do not
3342 * have to do it here.
3343 * NOTE: We will need some user address to copyout the data, therefore
3344 * the inteface layer will also provide us with the required user
3347 memset(&uioc
, 0, sizeof(nitioctl_t
));
3348 if( (rval
= mega_m_to_n( (void __user
*)arg
, &uioc
)) != 0 )
3352 switch( uioc
.opcode
) {
3354 case GET_DRIVER_VER
:
3355 if( put_user(driver_ver
, (u32 __user
*)uioc
.uioc_uaddr
) )
3361 if( put_user(hba_count
, (u32 __user
*)uioc
.uioc_uaddr
) )
3365 * Shucks. MIMD interface returns a positive value for number
3366 * of adapters. TODO: Change it to return 0 when there is no
3367 * applicatio using mimd interface.
3376 if( (adapno
= GETADAP(uioc
.adapno
)) >= hba_count
)
3379 if( copy_to_user(uioc
.uioc_uaddr
, mcontroller
+adapno
,
3380 sizeof(struct mcontroller
)) )
3390 if( (adapno
= GETADAP(uioc
.adapno
)) >= hba_count
)
3393 adapter
= hba_soft_state
[adapno
];
3395 ustats
= uioc
.uioc_uaddr
;
3397 if( copy_from_user(&num_ldrv
, &ustats
->num_ldrv
, sizeof(int)) )
3401 * Check for the validity of the logical drive number
3403 if( num_ldrv
>= MAX_LOGICAL_DRIVES_40LD
) return -EINVAL
;
3405 if( copy_to_user(ustats
->nreads
, adapter
->nreads
,
3406 num_ldrv
*sizeof(u32
)) )
3409 if( copy_to_user(ustats
->nreadblocks
, adapter
->nreadblocks
,
3410 num_ldrv
*sizeof(u32
)) )
3413 if( copy_to_user(ustats
->nwrites
, adapter
->nwrites
,
3414 num_ldrv
*sizeof(u32
)) )
3417 if( copy_to_user(ustats
->nwriteblocks
, adapter
->nwriteblocks
,
3418 num_ldrv
*sizeof(u32
)) )
3421 if( copy_to_user(ustats
->rd_errors
, adapter
->rd_errors
,
3422 num_ldrv
*sizeof(u32
)) )
3425 if( copy_to_user(ustats
->wr_errors
, adapter
->wr_errors
,
3426 num_ldrv
*sizeof(u32
)) )
3437 if( (adapno
= GETADAP(uioc
.adapno
)) >= hba_count
)
3440 adapter
= hba_soft_state
[adapno
];
3443 * Deletion of logical drive is a special case. The adapter
3444 * should be quiescent before this command is issued.
3446 if( uioc
.uioc_rmbox
[0] == FC_DEL_LOGDRV
&&
3447 uioc
.uioc_rmbox
[2] == OP_DEL_LOGDRV
) {
3450 * Do we support this feature
3452 if( !adapter
->support_random_del
) {
3453 printk(KERN_WARNING
"megaraid: logdrv ");
3454 printk("delete on non-supporting F/W.\n");
3459 rval
= mega_del_logdrv( adapter
, uioc
.uioc_rmbox
[3] );
3462 memset(&mc
, 0, sizeof(megacmd_t
));
3466 rval
= mega_n_to_m((void __user
*)arg
, &mc
);
3472 * This interface only support the regular passthru commands.
3473 * Reject extended passthru and 64-bit passthru
3475 if( uioc
.uioc_rmbox
[0] == MEGA_MBOXCMD_PASSTHRU64
||
3476 uioc
.uioc_rmbox
[0] == MEGA_MBOXCMD_EXTPTHRU
) {
3478 printk(KERN_WARNING
"megaraid: rejected passthru.\n");
3484 * For all internal commands, the buffer must be allocated in
3485 * <4GB address range
3487 if( make_local_pdev(adapter
, &pdev
) != 0 )
3490 /* Is it a passthru command or a DCMD */
3491 if( uioc
.uioc_rmbox
[0] == MEGA_MBOXCMD_PASSTHRU
) {
3492 /* Passthru commands */
3494 pthru
= pci_alloc_consistent(pdev
,
3495 sizeof(mega_passthru
),
3498 if( pthru
== NULL
) {
3499 free_local_pdev(pdev
);
3504 * The user passthru structure
3506 upthru
= (mega_passthru __user
*)(unsigned long)MBOX(uioc
)->xferaddr
;
3509 * Copy in the user passthru here.
3511 if( copy_from_user(pthru
, upthru
,
3512 sizeof(mega_passthru
)) ) {
3514 pci_free_consistent(pdev
,
3515 sizeof(mega_passthru
), pthru
,
3518 free_local_pdev(pdev
);
3524 * Is there a data transfer
3526 if( pthru
->dataxferlen
) {
3527 data
= pci_alloc_consistent(pdev
,
3531 if( data
== NULL
) {
3532 pci_free_consistent(pdev
,
3533 sizeof(mega_passthru
),
3537 free_local_pdev(pdev
);
3543 * Save the user address and point the kernel
3544 * address at just allocated memory
3546 uxferaddr
= pthru
->dataxferaddr
;
3547 pthru
->dataxferaddr
= data_dma_hndl
;
3552 * Is data coming down-stream
3554 if( pthru
->dataxferlen
&& (uioc
.flags
& UIOC_WR
) ) {
3558 if( copy_from_user(data
, (char __user
*)(unsigned long) uxferaddr
,
3559 pthru
->dataxferlen
) ) {
3561 goto freemem_and_return
;
3565 memset(&mc
, 0, sizeof(megacmd_t
));
3567 mc
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
3568 mc
.xferaddr
= (u32
)pthru_dma_hndl
;
3573 mega_internal_command(adapter
, &mc
, pthru
);
3575 rval
= mega_n_to_m((void __user
*)arg
, &mc
);
3577 if( rval
) goto freemem_and_return
;
3581 * Is data going up-stream
3583 if( pthru
->dataxferlen
&& (uioc
.flags
& UIOC_RD
) ) {
3584 if( copy_to_user((char __user
*)(unsigned long) uxferaddr
, data
,
3585 pthru
->dataxferlen
) ) {
3591 * Send the request sense data also, irrespective of
3592 * whether the user has asked for it or not.
3594 if (copy_to_user(upthru
->reqsensearea
,
3595 pthru
->reqsensearea
, 14))
3599 if( pthru
->dataxferlen
) {
3600 pci_free_consistent(pdev
,
3601 pthru
->dataxferlen
, data
,
3605 pci_free_consistent(pdev
, sizeof(mega_passthru
),
3606 pthru
, pthru_dma_hndl
);
3608 free_local_pdev(pdev
);
3616 * Is there a data transfer
3618 if( uioc
.xferlen
) {
3619 data
= pci_alloc_consistent(pdev
,
3620 uioc
.xferlen
, &data_dma_hndl
);
3622 if( data
== NULL
) {
3623 free_local_pdev(pdev
);
3627 uxferaddr
= MBOX(uioc
)->xferaddr
;
3631 * Is data coming down-stream
3633 if( uioc
.xferlen
&& (uioc
.flags
& UIOC_WR
) ) {
3637 if( copy_from_user(data
, (char __user
*)(unsigned long) uxferaddr
,
3640 pci_free_consistent(pdev
,
3642 data
, data_dma_hndl
);
3644 free_local_pdev(pdev
);
3650 memcpy(&mc
, MBOX(uioc
), sizeof(megacmd_t
));
3652 mc
.xferaddr
= (u32
)data_dma_hndl
;
3657 mega_internal_command(adapter
, &mc
, NULL
);
3659 rval
= mega_n_to_m((void __user
*)arg
, &mc
);
3662 if( uioc
.xferlen
) {
3663 pci_free_consistent(pdev
,
3668 free_local_pdev(pdev
);
3674 * Is data going up-stream
3676 if( uioc
.xferlen
&& (uioc
.flags
& UIOC_RD
) ) {
3677 if( copy_to_user((char __user
*)(unsigned long) uxferaddr
, data
,
3684 if( uioc
.xferlen
) {
3685 pci_free_consistent(pdev
,
3690 free_local_pdev(pdev
);
3703 megadev_unlocked_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
3707 mutex_lock(&megadev_mutex
);
3708 ret
= megadev_ioctl(filep
, cmd
, arg
);
3709 mutex_unlock(&megadev_mutex
);
3716 * @arg - user address
3717 * @uioc - new ioctl structure
3719 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3722 * Converts the older mimd ioctl structure to newer NIT structure
3725 mega_m_to_n(void __user
*arg
, nitioctl_t
*uioc
)
3727 struct uioctl_t uioc_mimd
;
3728 char signature
[8] = {0};
3734 * check is the application conforms to NIT. We do not have to do much
3736 * We exploit the fact that the signature is stored in the very
3737 * beginning of the structure.
3740 if( copy_from_user(signature
, arg
, 7) )
3743 if( memcmp(signature
, "MEGANIT", 7) == 0 ) {
3746 * NOTE NOTE: The nit ioctl is still under flux because of
3747 * change of mailbox definition, in HPE. No applications yet
3748 * use this interface and let's not have applications use this
3749 * interface till the new specifitions are in place.
3753 if( copy_from_user(uioc
, arg
, sizeof(nitioctl_t
)) )
3760 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3762 * Get the user ioctl structure
3764 if( copy_from_user(&uioc_mimd
, arg
, sizeof(struct uioctl_t
)) )
3769 * Get the opcode and subopcode for the commands
3771 opcode
= uioc_mimd
.ui
.fcs
.opcode
;
3772 subopcode
= uioc_mimd
.ui
.fcs
.subopcode
;
3777 switch (subopcode
) {
3779 case MEGAIOC_QDRVRVER
: /* Query driver version */
3780 uioc
->opcode
= GET_DRIVER_VER
;
3781 uioc
->uioc_uaddr
= uioc_mimd
.data
;
3784 case MEGAIOC_QNADAP
: /* Get # of adapters */
3785 uioc
->opcode
= GET_N_ADAP
;
3786 uioc
->uioc_uaddr
= uioc_mimd
.data
;
3789 case MEGAIOC_QADAPINFO
: /* Get adapter information */
3790 uioc
->opcode
= GET_ADAP_INFO
;
3791 uioc
->adapno
= uioc_mimd
.ui
.fcs
.adapno
;
3792 uioc
->uioc_uaddr
= uioc_mimd
.data
;
3804 uioc
->opcode
= MBOX_CMD
;
3805 uioc
->adapno
= uioc_mimd
.ui
.fcs
.adapno
;
3807 memcpy(uioc
->uioc_rmbox
, uioc_mimd
.mbox
, 18);
3809 uioc
->xferlen
= uioc_mimd
.ui
.fcs
.length
;
3811 if( uioc_mimd
.outlen
) uioc
->flags
= UIOC_RD
;
3812 if( uioc_mimd
.inlen
) uioc
->flags
|= UIOC_WR
;
3818 uioc
->opcode
= MBOX_CMD
;
3819 uioc
->adapno
= uioc_mimd
.ui
.fcs
.adapno
;
3821 memcpy(uioc
->uioc_rmbox
, uioc_mimd
.mbox
, 18);
3824 * Choose the xferlen bigger of input and output data
3826 uioc
->xferlen
= uioc_mimd
.outlen
> uioc_mimd
.inlen
?
3827 uioc_mimd
.outlen
: uioc_mimd
.inlen
;
3829 if( uioc_mimd
.outlen
) uioc
->flags
= UIOC_RD
;
3830 if( uioc_mimd
.inlen
) uioc
->flags
|= UIOC_WR
;
3844 * @arg - user address
3845 * @mc - mailbox command
3847 * Updates the status information to the application, depending on application
3848 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3851 mega_n_to_m(void __user
*arg
, megacmd_t
*mc
)
3853 nitioctl_t __user
*uiocp
;
3854 megacmd_t __user
*umc
;
3855 mega_passthru __user
*upthru
;
3856 struct uioctl_t __user
*uioc_mimd
;
3857 char signature
[8] = {0};
3860 * check is the application conforms to NIT.
3862 if( copy_from_user(signature
, arg
, 7) )
3865 if( memcmp(signature
, "MEGANIT", 7) == 0 ) {
3869 if( put_user(mc
->status
, (u8 __user
*)&MBOX_P(uiocp
)->status
) )
3872 if( mc
->cmd
== MEGA_MBOXCMD_PASSTHRU
) {
3874 umc
= MBOX_P(uiocp
);
3876 if (get_user(upthru
, (mega_passthru __user
* __user
*)&umc
->xferaddr
))
3879 if( put_user(mc
->status
, (u8 __user
*)&upthru
->scsistatus
))
3886 if( put_user(mc
->status
, (u8 __user
*)&uioc_mimd
->mbox
[17]) )
3889 if( mc
->cmd
== MEGA_MBOXCMD_PASSTHRU
) {
3891 umc
= (megacmd_t __user
*)uioc_mimd
->mbox
;
3893 if (get_user(upthru
, (mega_passthru __user
* __user
*)&umc
->xferaddr
))
3896 if( put_user(mc
->status
, (u8 __user
*)&upthru
->scsistatus
) )
3906 * MEGARAID 'FW' commands.
3910 * mega_is_bios_enabled()
3911 * @adapter - pointer to our soft state
3913 * issue command to find out if the BIOS is enabled for this controller
3916 mega_is_bios_enabled(adapter_t
*adapter
)
3918 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
3922 mbox
= (mbox_t
*)raw_mbox
;
3924 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
3926 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
3928 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
3930 raw_mbox
[0] = IS_BIOS_ENABLED
;
3931 raw_mbox
[2] = GET_BIOS
;
3934 ret
= issue_scb_block(adapter
, raw_mbox
);
3936 return *(char *)adapter
->mega_buffer
;
3941 * mega_enum_raid_scsi()
3942 * @adapter - pointer to our soft state
3944 * Find out what channels are RAID/SCSI. This information is used to
3945 * differentiate the virtual channels and physical channels and to support
3946 * ROMB feature and non-disk devices.
3949 mega_enum_raid_scsi(adapter_t
*adapter
)
3951 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
3955 mbox
= (mbox_t
*)raw_mbox
;
3957 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
3960 * issue command to find out what channels are raid/scsi
3962 raw_mbox
[0] = CHNL_CLASS
;
3963 raw_mbox
[2] = GET_CHNL_CLASS
;
3965 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
3967 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
3970 * Non-ROMB firmware fail this command, so all channels
3971 * must be shown RAID
3973 adapter
->mega_ch_class
= 0xFF;
3975 if(!issue_scb_block(adapter
, raw_mbox
)) {
3976 adapter
->mega_ch_class
= *((char *)adapter
->mega_buffer
);
3980 for( i
= 0; i
< adapter
->product_info
.nchannels
; i
++ ) {
3981 if( (adapter
->mega_ch_class
>> i
) & 0x01 ) {
3982 printk(KERN_INFO
"megaraid: channel[%d] is raid.\n",
3986 printk(KERN_INFO
"megaraid: channel[%d] is scsi.\n",
3996 * mega_get_boot_drv()
3997 * @adapter - pointer to our soft state
3999 * Find out which device is the boot device. Note, any logical drive or any
4000 * phyical device (e.g., a CDROM) can be designated as a boot device.
4003 mega_get_boot_drv(adapter_t
*adapter
)
4005 struct private_bios_data
*prv_bios_data
;
4006 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4013 mbox
= (mbox_t
*)raw_mbox
;
4015 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4017 raw_mbox
[0] = BIOS_PVT_DATA
;
4018 raw_mbox
[2] = GET_BIOS_PVT_DATA
;
4020 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
4022 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
4024 adapter
->boot_ldrv_enabled
= 0;
4025 adapter
->boot_ldrv
= 0;
4027 adapter
->boot_pdrv_enabled
= 0;
4028 adapter
->boot_pdrv_ch
= 0;
4029 adapter
->boot_pdrv_tgt
= 0;
4031 if(issue_scb_block(adapter
, raw_mbox
) == 0) {
4033 (struct private_bios_data
*)adapter
->mega_buffer
;
4036 cksum_p
= (char *)prv_bios_data
;
4037 for (i
= 0; i
< 14; i
++ ) {
4038 cksum
+= (u16
)(*cksum_p
++);
4041 if (prv_bios_data
->cksum
== (u16
)(0-cksum
) ) {
4044 * If MSB is set, a physical drive is set as boot
4047 if( prv_bios_data
->boot_drv
& 0x80 ) {
4048 adapter
->boot_pdrv_enabled
= 1;
4049 boot_pdrv
= prv_bios_data
->boot_drv
& 0x7F;
4050 adapter
->boot_pdrv_ch
= boot_pdrv
/ 16;
4051 adapter
->boot_pdrv_tgt
= boot_pdrv
% 16;
4054 adapter
->boot_ldrv_enabled
= 1;
4055 adapter
->boot_ldrv
= prv_bios_data
->boot_drv
;
4063 * mega_support_random_del()
4064 * @adapter - pointer to our soft state
4066 * Find out if this controller supports random deletion and addition of
4070 mega_support_random_del(adapter_t
*adapter
)
4072 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4076 mbox
= (mbox_t
*)raw_mbox
;
4078 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4083 raw_mbox
[0] = FC_DEL_LOGDRV
;
4084 raw_mbox
[2] = OP_SUP_DEL_LOGDRV
;
4086 rval
= issue_scb_block(adapter
, raw_mbox
);
4093 * mega_support_ext_cdb()
4094 * @adapter - pointer to our soft state
4096 * Find out if this firmware support cdblen > 10
4099 mega_support_ext_cdb(adapter_t
*adapter
)
4101 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4105 mbox
= (mbox_t
*)raw_mbox
;
4107 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4109 * issue command to find out if controller supports extended CDBs.
4114 rval
= issue_scb_block(adapter
, raw_mbox
);
4122 * @adapter - pointer to our soft state
4123 * @logdrv - logical drive to be deleted
4125 * Delete the specified logical drive. It is the responsibility of the user
4126 * app to let the OS know about this operation.
4129 mega_del_logdrv(adapter_t
*adapter
, int logdrv
)
4131 unsigned long flags
;
4136 * Stop sending commands to the controller, queue them internally.
4137 * When deletion is complete, ISR will flush the queue.
4139 atomic_set(&adapter
->quiescent
, 1);
4142 * Wait till all the issued commands are complete and there are no
4143 * commands in the pending queue
4145 while (atomic_read(&adapter
->pend_cmds
) > 0 ||
4146 !list_empty(&adapter
->pending_list
))
4147 msleep(1000); /* sleep for 1s */
4149 rval
= mega_do_del_logdrv(adapter
, logdrv
);
4151 spin_lock_irqsave(&adapter
->lock
, flags
);
4154 * If delete operation was successful, add 0x80 to the logical drive
4155 * ids for commands in the pending queue.
4157 if (adapter
->read_ldidmap
) {
4158 struct list_head
*pos
;
4159 list_for_each(pos
, &adapter
->pending_list
) {
4160 scb
= list_entry(pos
, scb_t
, list
);
4161 if (scb
->pthru
->logdrv
< 0x80 )
4162 scb
->pthru
->logdrv
+= 0x80;
4166 atomic_set(&adapter
->quiescent
, 0);
4168 mega_runpendq(adapter
);
4170 spin_unlock_irqrestore(&adapter
->lock
, flags
);
4177 mega_do_del_logdrv(adapter_t
*adapter
, int logdrv
)
4182 memset( &mc
, 0, sizeof(megacmd_t
));
4184 mc
.cmd
= FC_DEL_LOGDRV
;
4185 mc
.opcode
= OP_DEL_LOGDRV
;
4186 mc
.subopcode
= logdrv
;
4188 rval
= mega_internal_command(adapter
, &mc
, NULL
);
4190 /* log this event */
4192 printk(KERN_WARNING
"megaraid: Delete LD-%d failed.", logdrv
);
4197 * After deleting first logical drive, the logical drives must be
4198 * addressed by adding 0x80 to the logical drive id.
4200 adapter
->read_ldidmap
= 1;
4207 * mega_get_max_sgl()
4208 * @adapter - pointer to our soft state
4210 * Find out the maximum number of scatter-gather elements supported by this
4211 * version of the firmware
4214 mega_get_max_sgl(adapter_t
*adapter
)
4216 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4219 mbox
= (mbox_t
*)raw_mbox
;
4221 memset(mbox
, 0, sizeof(raw_mbox
));
4223 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
4225 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
4227 raw_mbox
[0] = MAIN_MISC_OPCODE
;
4228 raw_mbox
[2] = GET_MAX_SG_SUPPORT
;
4231 if( issue_scb_block(adapter
, raw_mbox
) ) {
4233 * f/w does not support this command. Choose the default value
4235 adapter
->sglen
= MIN_SGLIST
;
4238 adapter
->sglen
= *((char *)adapter
->mega_buffer
);
4241 * Make sure this is not more than the resources we are
4242 * planning to allocate
4244 if ( adapter
->sglen
> MAX_SGLIST
)
4245 adapter
->sglen
= MAX_SGLIST
;
4253 * mega_support_cluster()
4254 * @adapter - pointer to our soft state
4256 * Find out if this firmware support cluster calls.
4259 mega_support_cluster(adapter_t
*adapter
)
4261 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4264 mbox
= (mbox_t
*)raw_mbox
;
4266 memset(mbox
, 0, sizeof(raw_mbox
));
4268 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
4270 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
4273 * Try to get the initiator id. This command will succeed iff the
4274 * clustering is available on this HBA.
4276 raw_mbox
[0] = MEGA_GET_TARGET_ID
;
4278 if( issue_scb_block(adapter
, raw_mbox
) == 0 ) {
4281 * Cluster support available. Get the initiator target id.
4282 * Tell our id to mid-layer too.
4284 adapter
->this_id
= *(u32
*)adapter
->mega_buffer
;
4285 adapter
->host
->this_id
= adapter
->this_id
;
4293 #ifdef CONFIG_PROC_FS
4296 * @adapter - pointer to our soft state
4297 * @dma_handle - DMA address of the buffer
4299 * Issue internal commands while interrupts are available.
4300 * We only issue direct mailbox commands from within the driver. ioctl()
4301 * interface using these routines can issue passthru commands.
4304 mega_adapinq(adapter_t
*adapter
, dma_addr_t dma_handle
)
4308 memset(&mc
, 0, sizeof(megacmd_t
));
4310 if( adapter
->flag
& BOARD_40LD
) {
4311 mc
.cmd
= FC_NEW_CONFIG
;
4312 mc
.opcode
= NC_SUBOP_ENQUIRY3
;
4313 mc
.subopcode
= ENQ3_GET_SOLICITED_FULL
;
4316 mc
.cmd
= MEGA_MBOXCMD_ADPEXTINQ
;
4319 mc
.xferaddr
= (u32
)dma_handle
;
4321 if ( mega_internal_command(adapter
, &mc
, NULL
) != 0 ) {
4329 /** mega_internal_dev_inquiry()
4330 * @adapter - pointer to our soft state
4331 * @ch - channel for this device
4332 * @tgt - ID of this device
4333 * @buf_dma_handle - DMA address of the buffer
4335 * Issue the scsi inquiry for the specified device.
4338 mega_internal_dev_inquiry(adapter_t
*adapter
, u8 ch
, u8 tgt
,
4339 dma_addr_t buf_dma_handle
)
4341 mega_passthru
*pthru
;
4342 dma_addr_t pthru_dma_handle
;
4345 struct pci_dev
*pdev
;
4349 * For all internal commands, the buffer must be allocated in <4GB
4352 if( make_local_pdev(adapter
, &pdev
) != 0 ) return -1;
4354 pthru
= pci_alloc_consistent(pdev
, sizeof(mega_passthru
),
4357 if( pthru
== NULL
) {
4358 free_local_pdev(pdev
);
4364 pthru
->reqsenselen
= 14;
4365 pthru
->islogical
= 0;
4367 pthru
->channel
= (adapter
->flag
& BOARD_40LD
) ? 0 : ch
;
4369 pthru
->target
= (adapter
->flag
& BOARD_40LD
) ? (ch
<< 4)|tgt
: tgt
;
4373 pthru
->cdb
[0] = INQUIRY
;
4377 pthru
->cdb
[4] = 255;
4381 pthru
->dataxferaddr
= (u32
)buf_dma_handle
;
4382 pthru
->dataxferlen
= 256;
4384 memset(&mc
, 0, sizeof(megacmd_t
));
4386 mc
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
4387 mc
.xferaddr
= (u32
)pthru_dma_handle
;
4389 rval
= mega_internal_command(adapter
, &mc
, pthru
);
4391 pci_free_consistent(pdev
, sizeof(mega_passthru
), pthru
,
4394 free_local_pdev(pdev
);
4401 * mega_internal_command()
4402 * @adapter - pointer to our soft state
4403 * @mc - the mailbox command
4404 * @pthru - Passthru structure for DCDB commands
4406 * Issue the internal commands in interrupt mode.
4407 * The last argument is the address of the passthru structure if the command
4408 * to be fired is a passthru command
4410 * lockscope specifies whether the caller has already acquired the lock. Of
4411 * course, the caller must know which lock we are talking about.
4413 * Note: parameter 'pthru' is null for non-passthru commands.
4416 mega_internal_command(adapter_t
*adapter
, megacmd_t
*mc
, mega_passthru
*pthru
)
4419 struct scsi_device
*sdev
;
4423 scmd
= scsi_allocate_command(GFP_KERNEL
);
4428 * The internal commands share one command id and hence are
4429 * serialized. This is so because we want to reserve maximum number of
4430 * available command ids for the I/O commands.
4432 mutex_lock(&adapter
->int_mtx
);
4434 scb
= &adapter
->int_scb
;
4435 memset(scb
, 0, sizeof(scb_t
));
4437 sdev
= kzalloc(sizeof(struct scsi_device
), GFP_KERNEL
);
4438 scmd
->device
= sdev
;
4440 memset(adapter
->int_cdb
, 0, sizeof(adapter
->int_cdb
));
4441 scmd
->cmnd
= adapter
->int_cdb
;
4442 scmd
->device
->host
= adapter
->host
;
4443 scmd
->host_scribble
= (void *)scb
;
4444 scmd
->cmnd
[0] = MEGA_INTERNAL_CMD
;
4446 scb
->state
|= SCB_ACTIVE
;
4449 memcpy(scb
->raw_mbox
, mc
, sizeof(megacmd_t
));
4452 * Is it a passthru command
4454 if( mc
->cmd
== MEGA_MBOXCMD_PASSTHRU
) {
4459 scb
->idx
= CMDID_INT_CMDS
;
4461 megaraid_queue_lck(scmd
, mega_internal_done
);
4463 wait_for_completion(&adapter
->int_waitq
);
4465 rval
= scmd
->result
;
4466 mc
->status
= scmd
->result
;
4470 * Print a debug message for all failed commands. Applications can use
4473 if( scmd
->result
&& trace_level
) {
4474 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4475 mc
->cmd
, mc
->opcode
, mc
->subopcode
, scmd
->result
);
4478 mutex_unlock(&adapter
->int_mtx
);
4480 scsi_free_command(GFP_KERNEL
, scmd
);
4487 * mega_internal_done()
4488 * @scmd - internal scsi command
4490 * Callback routine for internal commands.
4493 mega_internal_done(Scsi_Cmnd
*scmd
)
4497 adapter
= (adapter_t
*)scmd
->device
->host
->hostdata
;
4499 complete(&adapter
->int_waitq
);
4504 static struct scsi_host_template megaraid_template
= {
4505 .module
= THIS_MODULE
,
4507 .proc_name
= "megaraid_legacy",
4508 .info
= megaraid_info
,
4509 .queuecommand
= megaraid_queue
,
4510 .bios_param
= megaraid_biosparam
,
4511 .max_sectors
= MAX_SECTORS_PER_IO
,
4512 .can_queue
= MAX_COMMANDS
,
4513 .this_id
= DEFAULT_INITIATOR_ID
,
4514 .sg_tablesize
= MAX_SGLIST
,
4515 .cmd_per_lun
= DEF_CMD_PER_LUN
,
4516 .use_clustering
= ENABLE_CLUSTERING
,
4517 .eh_abort_handler
= megaraid_abort
,
4518 .eh_device_reset_handler
= megaraid_reset
,
4519 .eh_bus_reset_handler
= megaraid_reset
,
4520 .eh_host_reset_handler
= megaraid_reset
,
4523 static int __devinit
4524 megaraid_probe_one(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
4526 struct Scsi_Host
*host
;
4528 unsigned long mega_baseport
, tbase
, flag
= 0;
4529 u16 subsysid
, subsysvid
;
4530 u8 pci_bus
, pci_dev_func
;
4532 int error
= -ENODEV
;
4534 if (pci_enable_device(pdev
))
4536 pci_set_master(pdev
);
4538 pci_bus
= pdev
->bus
->number
;
4539 pci_dev_func
= pdev
->devfn
;
4542 * The megaraid3 stuff reports the ID of the Intel part which is not
4543 * remotely specific to the megaraid
4545 if (pdev
->vendor
== PCI_VENDOR_ID_INTEL
) {
4548 * Don't fall over the Compaq management cards using the same
4551 if (pdev
->subsystem_vendor
== PCI_VENDOR_ID_COMPAQ
&&
4552 pdev
->subsystem_device
== 0xC000)
4554 /* Now check the magic signature byte */
4555 pci_read_config_word(pdev
, PCI_CONF_AMISIG
, &magic
);
4556 if (magic
!= HBA_SIGNATURE_471
&& magic
!= HBA_SIGNATURE
)
4558 /* Ok it is probably a megaraid */
4562 * For these vendor and device ids, signature offsets are not
4563 * valid and 64 bit is implicit
4565 if (id
->driver_data
& BOARD_64BIT
)
4566 flag
|= BOARD_64BIT
;
4570 pci_read_config_dword(pdev
, PCI_CONF_AMISIG64
, &magic64
);
4571 if (magic64
== HBA_SIGNATURE_64BIT
)
4572 flag
|= BOARD_64BIT
;
4575 subsysvid
= pdev
->subsystem_vendor
;
4576 subsysid
= pdev
->subsystem_device
;
4578 printk(KERN_NOTICE
"megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4579 id
->vendor
, id
->device
, pci_bus
);
4581 printk("slot %d:func %d\n",
4582 PCI_SLOT(pci_dev_func
), PCI_FUNC(pci_dev_func
));
4584 /* Read the base port and IRQ from PCI */
4585 mega_baseport
= pci_resource_start(pdev
, 0);
4588 tbase
= mega_baseport
;
4589 if (pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
) {
4590 flag
|= BOARD_MEMMAP
;
4592 if (!request_mem_region(mega_baseport
, 128, "megaraid")) {
4593 printk(KERN_WARNING
"megaraid: mem region busy!\n");
4594 goto out_disable_device
;
4597 mega_baseport
= (unsigned long)ioremap(mega_baseport
, 128);
4598 if (!mega_baseport
) {
4600 "megaraid: could not map hba memory\n");
4601 goto out_release_region
;
4604 flag
|= BOARD_IOMAP
;
4605 mega_baseport
+= 0x10;
4607 if (!request_region(mega_baseport
, 16, "megaraid"))
4608 goto out_disable_device
;
4611 /* Initialize SCSI Host structure */
4612 host
= scsi_host_alloc(&megaraid_template
, sizeof(adapter_t
));
4616 adapter
= (adapter_t
*)host
->hostdata
;
4617 memset(adapter
, 0, sizeof(adapter_t
));
4620 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4621 host
->host_no
, mega_baseport
, irq
);
4623 adapter
->base
= mega_baseport
;
4624 if (flag
& BOARD_MEMMAP
)
4625 adapter
->mmio_base
= (void __iomem
*) mega_baseport
;
4627 INIT_LIST_HEAD(&adapter
->free_list
);
4628 INIT_LIST_HEAD(&adapter
->pending_list
);
4629 INIT_LIST_HEAD(&adapter
->completed_list
);
4631 adapter
->flag
= flag
;
4632 spin_lock_init(&adapter
->lock
);
4634 host
->cmd_per_lun
= max_cmd_per_lun
;
4635 host
->max_sectors
= max_sectors_per_io
;
4637 adapter
->dev
= pdev
;
4638 adapter
->host
= host
;
4640 adapter
->host
->irq
= irq
;
4642 if (flag
& BOARD_MEMMAP
)
4643 adapter
->host
->base
= tbase
;
4645 adapter
->host
->io_port
= tbase
;
4646 adapter
->host
->n_io_port
= 16;
4649 adapter
->host
->unique_id
= (pci_bus
<< 8) | pci_dev_func
;
4652 * Allocate buffer to issue internal commands.
4654 adapter
->mega_buffer
= pci_alloc_consistent(adapter
->dev
,
4655 MEGA_BUFFER_SIZE
, &adapter
->buf_dma_handle
);
4656 if (!adapter
->mega_buffer
) {
4657 printk(KERN_WARNING
"megaraid: out of RAM.\n");
4661 adapter
->scb_list
= kmalloc(sizeof(scb_t
) * MAX_COMMANDS
, GFP_KERNEL
);
4662 if (!adapter
->scb_list
) {
4663 printk(KERN_WARNING
"megaraid: out of RAM.\n");
4664 goto out_free_cmd_buffer
;
4667 if (request_irq(irq
, (adapter
->flag
& BOARD_MEMMAP
) ?
4668 megaraid_isr_memmapped
: megaraid_isr_iomapped
,
4669 IRQF_SHARED
, "megaraid", adapter
)) {
4671 "megaraid: Couldn't register IRQ %d!\n", irq
);
4672 goto out_free_scb_list
;
4675 if (mega_setup_mailbox(adapter
))
4678 if (mega_query_adapter(adapter
))
4682 * Have checks for some buggy f/w
4684 if ((subsysid
== 0x1111) && (subsysvid
== 0x1111)) {
4688 if (!strcmp(adapter
->fw_version
, "3.00") ||
4689 !strcmp(adapter
->fw_version
, "3.01")) {
4691 printk( KERN_WARNING
4692 "megaraid: Your card is a Dell PERC "
4693 "2/SC RAID controller with "
4694 "firmware\nmegaraid: 3.00 or 3.01. "
4695 "This driver is known to have "
4696 "corruption issues\nmegaraid: with "
4697 "those firmware versions on this "
4698 "specific card. In order\nmegaraid: "
4699 "to protect your data, please upgrade "
4700 "your firmware to version\nmegaraid: "
4701 "3.10 or later, available from the "
4702 "Dell Technical Support web\n"
4703 "megaraid: site at\nhttp://support."
4704 "dell.com/us/en/filelib/download/"
4705 "index.asp?fileid=2940\n"
4711 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4712 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4713 * support, since this firmware cannot handle 64 bit
4716 if ((subsysvid
== HP_SUBSYS_VID
) &&
4717 ((subsysid
== 0x60E7) || (subsysid
== 0x60E8))) {
4721 if (!strcmp(adapter
->fw_version
, "H01.07") ||
4722 !strcmp(adapter
->fw_version
, "H01.08") ||
4723 !strcmp(adapter
->fw_version
, "H01.09") ) {
4725 "megaraid: Firmware H.01.07, "
4726 "H.01.08, and H.01.09 on 1M/2M "
4728 "megaraid: do not support 64 bit "
4729 "addressing.\nmegaraid: DISABLING "
4730 "64 bit support.\n");
4731 adapter
->flag
&= ~BOARD_64BIT
;
4735 if (mega_is_bios_enabled(adapter
))
4736 mega_hbas
[hba_count
].is_bios_enabled
= 1;
4737 mega_hbas
[hba_count
].hostdata_addr
= adapter
;
4740 * Find out which channel is raid and which is scsi. This is
4743 mega_enum_raid_scsi(adapter
);
4746 * Find out if a logical drive is set as the boot drive. If
4747 * there is one, will make that as the first logical drive.
4748 * ROMB: Do we have to boot from a physical drive. Then all
4749 * the physical drives would appear before the logical disks.
4750 * Else, all the physical drives would be exported to the mid
4751 * layer after logical drives.
4753 mega_get_boot_drv(adapter
);
4755 if (adapter
->boot_pdrv_enabled
) {
4756 j
= adapter
->product_info
.nchannels
;
4757 for( i
= 0; i
< j
; i
++ )
4758 adapter
->logdrv_chan
[i
] = 0;
4759 for( i
= j
; i
< NVIRT_CHAN
+ j
; i
++ )
4760 adapter
->logdrv_chan
[i
] = 1;
4762 for (i
= 0; i
< NVIRT_CHAN
; i
++)
4763 adapter
->logdrv_chan
[i
] = 1;
4764 for (i
= NVIRT_CHAN
; i
< MAX_CHANNELS
+NVIRT_CHAN
; i
++)
4765 adapter
->logdrv_chan
[i
] = 0;
4766 adapter
->mega_ch_class
<<= NVIRT_CHAN
;
4770 * Do we support random deletion and addition of logical
4773 adapter
->read_ldidmap
= 0; /* set it after first logdrv
4775 adapter
->support_random_del
= mega_support_random_del(adapter
);
4777 /* Initialize SCBs */
4778 if (mega_init_scb(adapter
))
4782 * Reset the pending commands counter
4784 atomic_set(&adapter
->pend_cmds
, 0);
4787 * Reset the adapter quiescent flag
4789 atomic_set(&adapter
->quiescent
, 0);
4791 hba_soft_state
[hba_count
] = adapter
;
4794 * Fill in the structure which needs to be passed back to the
4795 * application when it does an ioctl() for controller related
4800 mcontroller
[i
].base
= mega_baseport
;
4801 mcontroller
[i
].irq
= irq
;
4802 mcontroller
[i
].numldrv
= adapter
->numldrv
;
4803 mcontroller
[i
].pcibus
= pci_bus
;
4804 mcontroller
[i
].pcidev
= id
->device
;
4805 mcontroller
[i
].pcifun
= PCI_FUNC (pci_dev_func
);
4806 mcontroller
[i
].pciid
= -1;
4807 mcontroller
[i
].pcivendor
= id
->vendor
;
4808 mcontroller
[i
].pcislot
= PCI_SLOT(pci_dev_func
);
4809 mcontroller
[i
].uid
= (pci_bus
<< 8) | pci_dev_func
;
4812 /* Set the Mode of addressing to 64 bit if we can */
4813 if ((adapter
->flag
& BOARD_64BIT
) && (sizeof(dma_addr_t
) == 8)) {
4814 pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
4815 adapter
->has_64bit_addr
= 1;
4817 pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
4818 adapter
->has_64bit_addr
= 0;
4821 mutex_init(&adapter
->int_mtx
);
4822 init_completion(&adapter
->int_waitq
);
4824 adapter
->this_id
= DEFAULT_INITIATOR_ID
;
4825 adapter
->host
->this_id
= DEFAULT_INITIATOR_ID
;
4827 #if MEGA_HAVE_CLUSTERING
4829 * Is cluster support enabled on this controller
4830 * Note: In a cluster the HBAs ( the initiators ) will have
4831 * different target IDs and we cannot assume it to be 7. Call
4832 * to mega_support_cluster() will get the target ids also if
4833 * the cluster support is available
4835 adapter
->has_cluster
= mega_support_cluster(adapter
);
4836 if (adapter
->has_cluster
) {
4838 "megaraid: Cluster driver, initiator id:%d\n",
4843 pci_set_drvdata(pdev
, host
);
4845 mega_create_proc_entry(hba_count
, mega_proc_dir_entry
);
4847 error
= scsi_add_host(host
, &pdev
->dev
);
4851 scsi_scan_host(host
);
4856 pci_free_consistent(adapter
->dev
, sizeof(mbox64_t
),
4857 adapter
->una_mbox64
, adapter
->una_mbox64_dma
);
4859 free_irq(adapter
->host
->irq
, adapter
);
4861 kfree(adapter
->scb_list
);
4862 out_free_cmd_buffer
:
4863 pci_free_consistent(adapter
->dev
, MEGA_BUFFER_SIZE
,
4864 adapter
->mega_buffer
, adapter
->buf_dma_handle
);
4866 scsi_host_put(host
);
4868 if (flag
& BOARD_MEMMAP
)
4869 iounmap((void *)mega_baseport
);
4871 if (flag
& BOARD_MEMMAP
)
4872 release_mem_region(tbase
, 128);
4874 release_region(mega_baseport
, 16);
4876 pci_disable_device(pdev
);
4882 __megaraid_shutdown(adapter_t
*adapter
)
4884 u_char raw_mbox
[sizeof(struct mbox_out
)];
4885 mbox_t
*mbox
= (mbox_t
*)raw_mbox
;
4888 /* Flush adapter cache */
4889 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4890 raw_mbox
[0] = FLUSH_ADAPTER
;
4892 free_irq(adapter
->host
->irq
, adapter
);
4894 /* Issue a blocking (interrupts disabled) command to the card */
4895 issue_scb_block(adapter
, raw_mbox
);
4897 /* Flush disks cache */
4898 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4899 raw_mbox
[0] = FLUSH_SYSTEM
;
4901 /* Issue a blocking (interrupts disabled) command to the card */
4902 issue_scb_block(adapter
, raw_mbox
);
4904 if (atomic_read(&adapter
->pend_cmds
) > 0)
4905 printk(KERN_WARNING
"megaraid: pending commands!!\n");
4908 * Have a delibrate delay to make sure all the caches are
4911 for (i
= 0; i
<= 10; i
++)
4915 static void __devexit
4916 megaraid_remove_one(struct pci_dev
*pdev
)
4918 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
4919 adapter_t
*adapter
= (adapter_t
*)host
->hostdata
;
4921 scsi_remove_host(host
);
4923 __megaraid_shutdown(adapter
);
4925 /* Free our resources */
4926 if (adapter
->flag
& BOARD_MEMMAP
) {
4927 iounmap((void *)adapter
->base
);
4928 release_mem_region(adapter
->host
->base
, 128);
4930 release_region(adapter
->base
, 16);
4932 mega_free_sgl(adapter
);
4934 #ifdef CONFIG_PROC_FS
4935 if (adapter
->controller_proc_dir_entry
) {
4936 remove_proc_entry("stat", adapter
->controller_proc_dir_entry
);
4937 remove_proc_entry("config",
4938 adapter
->controller_proc_dir_entry
);
4939 remove_proc_entry("mailbox",
4940 adapter
->controller_proc_dir_entry
);
4941 #if MEGA_HAVE_ENH_PROC
4942 remove_proc_entry("rebuild-rate",
4943 adapter
->controller_proc_dir_entry
);
4944 remove_proc_entry("battery-status",
4945 adapter
->controller_proc_dir_entry
);
4947 remove_proc_entry("diskdrives-ch0",
4948 adapter
->controller_proc_dir_entry
);
4949 remove_proc_entry("diskdrives-ch1",
4950 adapter
->controller_proc_dir_entry
);
4951 remove_proc_entry("diskdrives-ch2",
4952 adapter
->controller_proc_dir_entry
);
4953 remove_proc_entry("diskdrives-ch3",
4954 adapter
->controller_proc_dir_entry
);
4956 remove_proc_entry("raiddrives-0-9",
4957 adapter
->controller_proc_dir_entry
);
4958 remove_proc_entry("raiddrives-10-19",
4959 adapter
->controller_proc_dir_entry
);
4960 remove_proc_entry("raiddrives-20-29",
4961 adapter
->controller_proc_dir_entry
);
4962 remove_proc_entry("raiddrives-30-39",
4963 adapter
->controller_proc_dir_entry
);
4966 char buf
[12] = { 0 };
4967 sprintf(buf
, "hba%d", adapter
->host
->host_no
);
4968 remove_proc_entry(buf
, mega_proc_dir_entry
);
4973 pci_free_consistent(adapter
->dev
, MEGA_BUFFER_SIZE
,
4974 adapter
->mega_buffer
, adapter
->buf_dma_handle
);
4975 kfree(adapter
->scb_list
);
4976 pci_free_consistent(adapter
->dev
, sizeof(mbox64_t
),
4977 adapter
->una_mbox64
, adapter
->una_mbox64_dma
);
4979 scsi_host_put(host
);
4980 pci_disable_device(pdev
);
4986 megaraid_shutdown(struct pci_dev
*pdev
)
4988 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
4989 adapter_t
*adapter
= (adapter_t
*)host
->hostdata
;
4991 __megaraid_shutdown(adapter
);
4994 static struct pci_device_id megaraid_pci_tbl
[] = {
4995 {PCI_VENDOR_ID_AMI
, PCI_DEVICE_ID_AMI_MEGARAID
,
4996 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
4997 {PCI_VENDOR_ID_AMI
, PCI_DEVICE_ID_AMI_MEGARAID2
,
4998 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
4999 {PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_AMI_MEGARAID3
,
5000 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
5003 MODULE_DEVICE_TABLE(pci
, megaraid_pci_tbl
);
5005 static struct pci_driver megaraid_pci_driver
= {
5006 .name
= "megaraid_legacy",
5007 .id_table
= megaraid_pci_tbl
,
5008 .probe
= megaraid_probe_one
,
5009 .remove
= __devexit_p(megaraid_remove_one
),
5010 .shutdown
= megaraid_shutdown
,
5013 static int __init
megaraid_init(void)
5017 if ((max_cmd_per_lun
<= 0) || (max_cmd_per_lun
> MAX_CMD_PER_LUN
))
5018 max_cmd_per_lun
= MAX_CMD_PER_LUN
;
5019 if (max_mbox_busy_wait
> MBOX_BUSY_WAIT
)
5020 max_mbox_busy_wait
= MBOX_BUSY_WAIT
;
5022 #ifdef CONFIG_PROC_FS
5023 mega_proc_dir_entry
= proc_mkdir("megaraid", NULL
);
5024 if (!mega_proc_dir_entry
) {
5026 "megaraid: failed to create megaraid root\n");
5029 error
= pci_register_driver(&megaraid_pci_driver
);
5031 #ifdef CONFIG_PROC_FS
5032 remove_proc_entry("megaraid", NULL
);
5038 * Register the driver as a character device, for applications
5039 * to access it for ioctls.
5040 * First argument (major) to register_chrdev implies a dynamic
5041 * major number allocation.
5043 major
= register_chrdev(0, "megadev_legacy", &megadev_fops
);
5046 "megaraid: failed to register char device\n");
5052 static void __exit
megaraid_exit(void)
5055 * Unregister the character device interface to the driver.
5057 unregister_chrdev(major
, "megadev_legacy");
5059 pci_unregister_driver(&megaraid_pci_driver
);
5061 #ifdef CONFIG_PROC_FS
5062 remove_proc_entry("megaraid", NULL
);
5066 module_init(megaraid_init
);
5067 module_exit(megaraid_exit
);
5069 /* vi: set ts=8 sw=8 tw=78: */