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
;
308 /* use HP firmware and bios version encoding
309 Note: fw_version[0|1] and bios_version[0|1] were originally shifted
310 right 8 bits making them zero. This 0 value was hardcoded to fix
312 if (adapter
->product_info
.subsysvid
== PCI_VENDOR_ID_HP
) {
313 sprintf (adapter
->fw_version
, "%c%d%d.%d%d",
314 adapter
->product_info
.fw_version
[2],
316 adapter
->product_info
.fw_version
[1] & 0x0f,
318 adapter
->product_info
.fw_version
[0] & 0x0f);
319 sprintf (adapter
->bios_version
, "%c%d%d.%d%d",
320 adapter
->product_info
.bios_version
[2],
322 adapter
->product_info
.bios_version
[1] & 0x0f,
324 adapter
->product_info
.bios_version
[0] & 0x0f);
326 memcpy(adapter
->fw_version
,
327 (char *)adapter
->product_info
.fw_version
, 4);
328 adapter
->fw_version
[4] = 0;
330 memcpy(adapter
->bios_version
,
331 (char *)adapter
->product_info
.bios_version
, 4);
333 adapter
->bios_version
[4] = 0;
336 printk(KERN_NOTICE
"megaraid: [%s:%s] detected %d logical drives.\n",
337 adapter
->fw_version
, adapter
->bios_version
, adapter
->numldrv
);
340 * Do we support extended (>10 bytes) cdbs
342 adapter
->support_ext_cdb
= mega_support_ext_cdb(adapter
);
343 if (adapter
->support_ext_cdb
)
344 printk(KERN_NOTICE
"megaraid: supports extended CDBs.\n");
352 * @adapter - pointer to our soft state
354 * Runs through the list of pending requests.
357 mega_runpendq(adapter_t
*adapter
)
359 if(!list_empty(&adapter
->pending_list
))
360 __mega_runpendq(adapter
);
365 * @scmd - Issue this scsi command
366 * @done - the callback hook into the scsi mid-layer
368 * The command queuing entry point for the mid-layer.
371 megaraid_queue_lck(Scsi_Cmnd
*scmd
, void (*done
)(Scsi_Cmnd
*))
378 adapter
= (adapter_t
*)scmd
->device
->host
->hostdata
;
380 scmd
->scsi_done
= done
;
384 * Allocate and build a SCB request
385 * busy flag will be set if mega_build_cmd() command could not
386 * allocate scb. We will return non-zero status in that case.
387 * NOTE: scb can be null even though certain commands completed
388 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
389 * return 0 in that case.
392 spin_lock_irqsave(&adapter
->lock
, flags
);
393 scb
= mega_build_cmd(adapter
, scmd
, &busy
);
397 scb
->state
|= SCB_PENDQ
;
398 list_add_tail(&scb
->list
, &adapter
->pending_list
);
401 * Check if the HBA is in quiescent state, e.g., during a
402 * delete logical drive opertion. If it is, don't run
405 if (atomic_read(&adapter
->quiescent
) == 0)
406 mega_runpendq(adapter
);
410 spin_unlock_irqrestore(&adapter
->lock
, flags
);
414 static DEF_SCSI_QCMD(megaraid_queue
)
417 * mega_allocate_scb()
418 * @adapter - pointer to our soft state
419 * @cmd - scsi command from the mid-layer
421 * Allocate a SCB structure. This is the central structure for controller
424 static inline scb_t
*
425 mega_allocate_scb(adapter_t
*adapter
, Scsi_Cmnd
*cmd
)
427 struct list_head
*head
= &adapter
->free_list
;
430 /* Unlink command from Free List */
431 if( !list_empty(head
) ) {
433 scb
= list_entry(head
->next
, scb_t
, list
);
435 list_del_init(head
->next
);
437 scb
->state
= SCB_ACTIVE
;
439 scb
->dma_type
= MEGA_DMA_TYPE_NONE
;
448 * mega_get_ldrv_num()
449 * @adapter - pointer to our soft state
450 * @cmd - scsi mid layer command
451 * @channel - channel on the controller
453 * Calculate the logical drive number based on the information in scsi command
454 * and the channel number.
457 mega_get_ldrv_num(adapter_t
*adapter
, Scsi_Cmnd
*cmd
, int channel
)
462 tgt
= cmd
->device
->id
;
464 if ( tgt
> adapter
->this_id
)
465 tgt
--; /* we do not get inquires for initiator id */
467 ldrv_num
= (channel
* 15) + tgt
;
471 * If we have a logical drive with boot enabled, project it first
473 if( adapter
->boot_ldrv_enabled
) {
474 if( ldrv_num
== 0 ) {
475 ldrv_num
= adapter
->boot_ldrv
;
478 if( ldrv_num
<= adapter
->boot_ldrv
) {
485 * If "delete logical drive" feature is enabled on this controller.
486 * Do only if at least one delete logical drive operation was done.
488 * Also, after logical drive deletion, instead of logical drive number,
489 * the value returned should be 0x80+logical drive id.
491 * These is valid only for IO commands.
494 if (adapter
->support_random_del
&& adapter
->read_ldidmap
)
495 switch (cmd
->cmnd
[0]) {
496 case READ_6
: /* fall through */
497 case WRITE_6
: /* fall through */
498 case READ_10
: /* fall through */
508 * @adapter - pointer to our soft state
509 * @cmd - Prepare using this scsi command
510 * @busy - busy flag if no resources
512 * Prepares a command and scatter gather list for the controller. This routine
513 * also finds out if the commands is intended for a logical drive or a
514 * physical device and prepares the controller command accordingly.
516 * We also re-order the logical drives and physical devices based on their
520 mega_build_cmd(adapter_t
*adapter
, Scsi_Cmnd
*cmd
, int *busy
)
522 mega_ext_passthru
*epthru
;
523 mega_passthru
*pthru
;
531 int ldrv_num
= 0; /* logical drive number */
535 * filter the internal and ioctl commands
537 if((cmd
->cmnd
[0] == MEGA_INTERNAL_CMD
))
538 return (scb_t
*)cmd
->host_scribble
;
541 * We know what channels our logical drives are on - mega_find_card()
543 islogical
= adapter
->logdrv_chan
[cmd
->device
->channel
];
546 * The theory: If physical drive is chosen for boot, all the physical
547 * devices are exported before the logical drives, otherwise physical
548 * devices are pushed after logical drives, in which case - Kernel sees
549 * the physical devices on virtual channel which is obviously converted
550 * to actual channel on the HBA.
552 if( adapter
->boot_pdrv_enabled
) {
554 /* logical channel */
555 channel
= cmd
->device
->channel
-
556 adapter
->product_info
.nchannels
;
559 /* this is physical channel */
560 channel
= cmd
->device
->channel
;
561 target
= cmd
->device
->id
;
564 * boot from a physical disk, that disk needs to be
565 * exposed first IF both the channels are SCSI, then
566 * booting from the second channel is not allowed.
569 target
= adapter
->boot_pdrv_tgt
;
571 else if( target
== adapter
->boot_pdrv_tgt
) {
578 /* this is the logical channel */
579 channel
= cmd
->device
->channel
;
582 /* physical channel */
583 channel
= cmd
->device
->channel
- NVIRT_CHAN
;
584 target
= cmd
->device
->id
;
591 /* have just LUN 0 for each target on virtual channels */
592 if (cmd
->device
->lun
) {
593 cmd
->result
= (DID_BAD_TARGET
<< 16);
598 ldrv_num
= mega_get_ldrv_num(adapter
, cmd
, channel
);
601 max_ldrv_num
= (adapter
->flag
& BOARD_40LD
) ?
602 MAX_LOGICAL_DRIVES_40LD
: MAX_LOGICAL_DRIVES_8LD
;
605 * max_ldrv_num increases by 0x80 if some logical drive was
608 if(adapter
->read_ldidmap
)
609 max_ldrv_num
+= 0x80;
611 if(ldrv_num
> max_ldrv_num
) {
612 cmd
->result
= (DID_BAD_TARGET
<< 16);
619 if( cmd
->device
->lun
> 7) {
621 * Do not support lun >7 for physically accessed
624 cmd
->result
= (DID_BAD_TARGET
<< 16);
632 * Logical drive commands
636 switch (cmd
->cmnd
[0]) {
637 case TEST_UNIT_READY
:
638 #if MEGA_HAVE_CLUSTERING
640 * Do we support clustering and is the support enabled
641 * If no, return success always
643 if( !adapter
->has_cluster
) {
644 cmd
->result
= (DID_OK
<< 16);
649 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
654 scb
->raw_mbox
[0] = MEGA_CLUSTER_CMD
;
655 scb
->raw_mbox
[2] = MEGA_RESERVATION_STATUS
;
656 scb
->raw_mbox
[3] = ldrv_num
;
658 scb
->dma_direction
= PCI_DMA_NONE
;
662 cmd
->result
= (DID_OK
<< 16);
669 struct scatterlist
*sg
;
671 sg
= scsi_sglist(cmd
);
672 buf
= kmap_atomic(sg_page(sg
)) + sg
->offset
;
674 memset(buf
, 0, cmd
->cmnd
[4]);
675 kunmap_atomic(buf
- sg
->offset
);
677 cmd
->result
= (DID_OK
<< 16);
685 if(!(adapter
->flag
& (1L << cmd
->device
->channel
))) {
688 "scsi%d: scanning scsi channel %d ",
689 adapter
->host
->host_no
,
690 cmd
->device
->channel
);
691 printk("for logical drives.\n");
693 adapter
->flag
|= (1L << cmd
->device
->channel
);
696 /* Allocate a SCB and initialize passthru */
697 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
703 mbox
= (mbox_t
*)scb
->raw_mbox
;
704 memset(mbox
, 0, sizeof(scb
->raw_mbox
));
705 memset(pthru
, 0, sizeof(mega_passthru
));
709 pthru
->reqsenselen
= 14;
710 pthru
->islogical
= 1;
711 pthru
->logdrv
= ldrv_num
;
712 pthru
->cdblen
= cmd
->cmd_len
;
713 memcpy(pthru
->cdb
, cmd
->cmnd
, cmd
->cmd_len
);
715 if( adapter
->has_64bit_addr
) {
716 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU64
;
719 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
722 scb
->dma_direction
= PCI_DMA_FROMDEVICE
;
724 pthru
->numsgelements
= mega_build_sglist(adapter
, scb
,
725 &pthru
->dataxferaddr
, &pthru
->dataxferlen
);
727 mbox
->m_out
.xferaddr
= scb
->pthru_dma_addr
;
738 /* Allocate a SCB and initialize mailbox */
739 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
743 mbox
= (mbox_t
*)scb
->raw_mbox
;
745 memset(mbox
, 0, sizeof(scb
->raw_mbox
));
746 mbox
->m_out
.logdrv
= ldrv_num
;
749 * A little hack: 2nd bit is zero for all scsi read
750 * commands and is set for all scsi write commands
752 if( adapter
->has_64bit_addr
) {
753 mbox
->m_out
.cmd
= (*cmd
->cmnd
& 0x02) ?
754 MEGA_MBOXCMD_LWRITE64
:
755 MEGA_MBOXCMD_LREAD64
;
758 mbox
->m_out
.cmd
= (*cmd
->cmnd
& 0x02) ?
764 * 6-byte READ(0x08) or WRITE(0x0A) cdb
766 if( cmd
->cmd_len
== 6 ) {
767 mbox
->m_out
.numsectors
= (u32
) cmd
->cmnd
[4];
769 ((u32
)cmd
->cmnd
[1] << 16) |
770 ((u32
)cmd
->cmnd
[2] << 8) |
773 mbox
->m_out
.lba
&= 0x1FFFFF;
777 * Take modulo 0x80, since the logical drive
778 * number increases by 0x80 when a logical
781 if (*cmd
->cmnd
== READ_6
) {
782 adapter
->nreads
[ldrv_num
%0x80]++;
783 adapter
->nreadblocks
[ldrv_num
%0x80] +=
784 mbox
->m_out
.numsectors
;
786 adapter
->nwrites
[ldrv_num
%0x80]++;
787 adapter
->nwriteblocks
[ldrv_num
%0x80] +=
788 mbox
->m_out
.numsectors
;
794 * 10-byte READ(0x28) or WRITE(0x2A) cdb
796 if( cmd
->cmd_len
== 10 ) {
797 mbox
->m_out
.numsectors
=
799 ((u32
)cmd
->cmnd
[7] << 8);
801 ((u32
)cmd
->cmnd
[2] << 24) |
802 ((u32
)cmd
->cmnd
[3] << 16) |
803 ((u32
)cmd
->cmnd
[4] << 8) |
807 if (*cmd
->cmnd
== READ_10
) {
808 adapter
->nreads
[ldrv_num
%0x80]++;
809 adapter
->nreadblocks
[ldrv_num
%0x80] +=
810 mbox
->m_out
.numsectors
;
812 adapter
->nwrites
[ldrv_num
%0x80]++;
813 adapter
->nwriteblocks
[ldrv_num
%0x80] +=
814 mbox
->m_out
.numsectors
;
820 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
822 if( cmd
->cmd_len
== 12 ) {
824 ((u32
)cmd
->cmnd
[2] << 24) |
825 ((u32
)cmd
->cmnd
[3] << 16) |
826 ((u32
)cmd
->cmnd
[4] << 8) |
829 mbox
->m_out
.numsectors
=
830 ((u32
)cmd
->cmnd
[6] << 24) |
831 ((u32
)cmd
->cmnd
[7] << 16) |
832 ((u32
)cmd
->cmnd
[8] << 8) |
836 if (*cmd
->cmnd
== READ_12
) {
837 adapter
->nreads
[ldrv_num
%0x80]++;
838 adapter
->nreadblocks
[ldrv_num
%0x80] +=
839 mbox
->m_out
.numsectors
;
841 adapter
->nwrites
[ldrv_num
%0x80]++;
842 adapter
->nwriteblocks
[ldrv_num
%0x80] +=
843 mbox
->m_out
.numsectors
;
849 * If it is a read command
851 if( (*cmd
->cmnd
& 0x0F) == 0x08 ) {
852 scb
->dma_direction
= PCI_DMA_FROMDEVICE
;
855 scb
->dma_direction
= PCI_DMA_TODEVICE
;
858 /* Calculate Scatter-Gather info */
859 mbox
->m_out
.numsgelements
= mega_build_sglist(adapter
, scb
,
860 (u32
*)&mbox
->m_out
.xferaddr
, &seg
);
864 #if MEGA_HAVE_CLUSTERING
865 case RESERVE
: /* Fall through */
869 * Do we support clustering and is the support enabled
871 if( ! adapter
->has_cluster
) {
873 cmd
->result
= (DID_BAD_TARGET
<< 16);
878 /* Allocate a SCB and initialize mailbox */
879 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
884 scb
->raw_mbox
[0] = MEGA_CLUSTER_CMD
;
885 scb
->raw_mbox
[2] = ( *cmd
->cmnd
== RESERVE
) ?
886 MEGA_RESERVE_LD
: MEGA_RELEASE_LD
;
888 scb
->raw_mbox
[3] = ldrv_num
;
890 scb
->dma_direction
= PCI_DMA_NONE
;
896 cmd
->result
= (DID_BAD_TARGET
<< 16);
903 * Passthru drive commands
906 /* Allocate a SCB and initialize passthru */
907 if(!(scb
= mega_allocate_scb(adapter
, cmd
))) {
912 mbox
= (mbox_t
*)scb
->raw_mbox
;
913 memset(mbox
, 0, sizeof(scb
->raw_mbox
));
915 if( adapter
->support_ext_cdb
) {
917 epthru
= mega_prepare_extpassthru(adapter
, scb
, cmd
,
920 mbox
->m_out
.cmd
= MEGA_MBOXCMD_EXTPTHRU
;
922 mbox
->m_out
.xferaddr
= scb
->epthru_dma_addr
;
927 pthru
= mega_prepare_passthru(adapter
, scb
, cmd
,
930 /* Initialize mailbox */
931 if( adapter
->has_64bit_addr
) {
932 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU64
;
935 mbox
->m_out
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
938 mbox
->m_out
.xferaddr
= scb
->pthru_dma_addr
;
948 * mega_prepare_passthru()
949 * @adapter - pointer to our soft state
950 * @scb - our scsi control block
951 * @cmd - scsi command from the mid-layer
952 * @channel - actual channel on the controller
953 * @target - actual id on the controller.
955 * prepare a command for the scsi physical devices.
957 static mega_passthru
*
958 mega_prepare_passthru(adapter_t
*adapter
, scb_t
*scb
, Scsi_Cmnd
*cmd
,
959 int channel
, int target
)
961 mega_passthru
*pthru
;
964 memset(pthru
, 0, sizeof (mega_passthru
));
966 /* 0=6sec/1=60sec/2=10min/3=3hrs */
970 pthru
->reqsenselen
= 14;
971 pthru
->islogical
= 0;
973 pthru
->channel
= (adapter
->flag
& BOARD_40LD
) ? 0 : channel
;
975 pthru
->target
= (adapter
->flag
& BOARD_40LD
) ?
976 (channel
<< 4) | target
: target
;
978 pthru
->cdblen
= cmd
->cmd_len
;
979 pthru
->logdrv
= cmd
->device
->lun
;
981 memcpy(pthru
->cdb
, cmd
->cmnd
, cmd
->cmd_len
);
983 /* Not sure about the direction */
984 scb
->dma_direction
= PCI_DMA_BIDIRECTIONAL
;
986 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
987 switch (cmd
->cmnd
[0]) {
990 if(!(adapter
->flag
& (1L << cmd
->device
->channel
))) {
993 "scsi%d: scanning scsi channel %d [P%d] ",
994 adapter
->host
->host_no
,
995 cmd
->device
->channel
, channel
);
996 printk("for physical devices.\n");
998 adapter
->flag
|= (1L << cmd
->device
->channel
);
1002 pthru
->numsgelements
= mega_build_sglist(adapter
, scb
,
1003 &pthru
->dataxferaddr
, &pthru
->dataxferlen
);
1011 * mega_prepare_extpassthru()
1012 * @adapter - pointer to our soft state
1013 * @scb - our scsi control block
1014 * @cmd - scsi command from the mid-layer
1015 * @channel - actual channel on the controller
1016 * @target - actual id on the controller.
1018 * prepare a command for the scsi physical devices. This rountine prepares
1019 * commands for devices which can take extended CDBs (>10 bytes)
1021 static mega_ext_passthru
*
1022 mega_prepare_extpassthru(adapter_t
*adapter
, scb_t
*scb
, Scsi_Cmnd
*cmd
,
1023 int channel
, int target
)
1025 mega_ext_passthru
*epthru
;
1027 epthru
= scb
->epthru
;
1028 memset(epthru
, 0, sizeof(mega_ext_passthru
));
1030 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1031 epthru
->timeout
= 2;
1034 epthru
->reqsenselen
= 14;
1035 epthru
->islogical
= 0;
1037 epthru
->channel
= (adapter
->flag
& BOARD_40LD
) ? 0 : channel
;
1038 epthru
->target
= (adapter
->flag
& BOARD_40LD
) ?
1039 (channel
<< 4) | target
: target
;
1041 epthru
->cdblen
= cmd
->cmd_len
;
1042 epthru
->logdrv
= cmd
->device
->lun
;
1044 memcpy(epthru
->cdb
, cmd
->cmnd
, cmd
->cmd_len
);
1046 /* Not sure about the direction */
1047 scb
->dma_direction
= PCI_DMA_BIDIRECTIONAL
;
1049 switch(cmd
->cmnd
[0]) {
1052 if(!(adapter
->flag
& (1L << cmd
->device
->channel
))) {
1055 "scsi%d: scanning scsi channel %d [P%d] ",
1056 adapter
->host
->host_no
,
1057 cmd
->device
->channel
, channel
);
1058 printk("for physical devices.\n");
1060 adapter
->flag
|= (1L << cmd
->device
->channel
);
1064 epthru
->numsgelements
= mega_build_sglist(adapter
, scb
,
1065 &epthru
->dataxferaddr
, &epthru
->dataxferlen
);
1073 __mega_runpendq(adapter_t
*adapter
)
1076 struct list_head
*pos
, *next
;
1078 /* Issue any pending commands to the card */
1079 list_for_each_safe(pos
, next
, &adapter
->pending_list
) {
1081 scb
= list_entry(pos
, scb_t
, list
);
1083 if( !(scb
->state
& SCB_ISSUED
) ) {
1085 if( issue_scb(adapter
, scb
) != 0 )
1096 * @adapter - pointer to our soft state
1097 * @scb - scsi control block
1099 * Post a command to the card if the mailbox is available, otherwise return
1100 * busy. We also take the scb from the pending list if the mailbox is
1104 issue_scb(adapter_t
*adapter
, scb_t
*scb
)
1106 volatile mbox64_t
*mbox64
= adapter
->mbox64
;
1107 volatile mbox_t
*mbox
= adapter
->mbox
;
1110 if(unlikely(mbox
->m_in
.busy
)) {
1114 } while( mbox
->m_in
.busy
&& (i
< max_mbox_busy_wait
) );
1116 if(mbox
->m_in
.busy
) return -1;
1119 /* Copy mailbox data into host structure */
1120 memcpy((char *)&mbox
->m_out
, (char *)scb
->raw_mbox
,
1121 sizeof(struct mbox_out
));
1123 mbox
->m_out
.cmdid
= scb
->idx
; /* Set cmdid */
1124 mbox
->m_in
.busy
= 1; /* Set busy */
1128 * Increment the pending queue counter
1130 atomic_inc(&adapter
->pend_cmds
);
1132 switch (mbox
->m_out
.cmd
) {
1133 case MEGA_MBOXCMD_LREAD64
:
1134 case MEGA_MBOXCMD_LWRITE64
:
1135 case MEGA_MBOXCMD_PASSTHRU64
:
1136 case MEGA_MBOXCMD_EXTPTHRU
:
1137 mbox64
->xfer_segment_lo
= mbox
->m_out
.xferaddr
;
1138 mbox64
->xfer_segment_hi
= 0;
1139 mbox
->m_out
.xferaddr
= 0xFFFFFFFF;
1142 mbox64
->xfer_segment_lo
= 0;
1143 mbox64
->xfer_segment_hi
= 0;
1149 scb
->state
|= SCB_ISSUED
;
1151 if( likely(adapter
->flag
& BOARD_MEMMAP
) ) {
1152 mbox
->m_in
.poll
= 0;
1154 WRINDOOR(adapter
, adapter
->mbox_dma
| 0x1);
1157 irq_enable(adapter
);
1158 issue_command(adapter
);
1165 * Wait until the controller's mailbox is available
1168 mega_busywait_mbox (adapter_t
*adapter
)
1170 if (adapter
->mbox
->m_in
.busy
)
1171 return __mega_busywait_mbox(adapter
);
1177 * @adapter - pointer to our soft state
1178 * @raw_mbox - the mailbox
1180 * Issue a scb in synchronous and non-interrupt mode
1183 issue_scb_block(adapter_t
*adapter
, u_char
*raw_mbox
)
1185 volatile mbox64_t
*mbox64
= adapter
->mbox64
;
1186 volatile mbox_t
*mbox
= adapter
->mbox
;
1189 /* Wait until mailbox is free */
1190 if(mega_busywait_mbox (adapter
))
1191 goto bug_blocked_mailbox
;
1193 /* Copy mailbox data into host structure */
1194 memcpy((char *) mbox
, raw_mbox
, sizeof(struct mbox_out
));
1195 mbox
->m_out
.cmdid
= 0xFE;
1196 mbox
->m_in
.busy
= 1;
1198 switch (raw_mbox
[0]) {
1199 case MEGA_MBOXCMD_LREAD64
:
1200 case MEGA_MBOXCMD_LWRITE64
:
1201 case MEGA_MBOXCMD_PASSTHRU64
:
1202 case MEGA_MBOXCMD_EXTPTHRU
:
1203 mbox64
->xfer_segment_lo
= mbox
->m_out
.xferaddr
;
1204 mbox64
->xfer_segment_hi
= 0;
1205 mbox
->m_out
.xferaddr
= 0xFFFFFFFF;
1208 mbox64
->xfer_segment_lo
= 0;
1209 mbox64
->xfer_segment_hi
= 0;
1212 if( likely(adapter
->flag
& BOARD_MEMMAP
) ) {
1213 mbox
->m_in
.poll
= 0;
1215 mbox
->m_in
.numstatus
= 0xFF;
1216 mbox
->m_in
.status
= 0xFF;
1217 WRINDOOR(adapter
, adapter
->mbox_dma
| 0x1);
1219 while((volatile u8
)mbox
->m_in
.numstatus
== 0xFF)
1222 mbox
->m_in
.numstatus
= 0xFF;
1224 while( (volatile u8
)mbox
->m_in
.poll
!= 0x77 )
1227 mbox
->m_in
.poll
= 0;
1228 mbox
->m_in
.ack
= 0x77;
1230 WRINDOOR(adapter
, adapter
->mbox_dma
| 0x2);
1232 while(RDINDOOR(adapter
) & 0x2)
1236 irq_disable(adapter
);
1237 issue_command(adapter
);
1239 while (!((byte
= irq_state(adapter
)) & INTR_VALID
))
1242 set_irq_state(adapter
, byte
);
1243 irq_enable(adapter
);
1247 return mbox
->m_in
.status
;
1249 bug_blocked_mailbox
:
1250 printk(KERN_WARNING
"megaraid: Blocked mailbox......!!\n");
1257 * megaraid_isr_iomapped()
1259 * @devp - pointer to our soft state
1261 * Interrupt service routine for io-mapped controllers.
1262 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1263 * and service the completed commands.
1266 megaraid_isr_iomapped(int irq
, void *devp
)
1268 adapter_t
*adapter
= devp
;
1269 unsigned long flags
;
1272 u8 completed
[MAX_FIRMWARE_STATUS
];
1278 * loop till F/W has more commands for us to complete.
1280 spin_lock_irqsave(&adapter
->lock
, flags
);
1283 /* Check if a valid interrupt is pending */
1284 byte
= irq_state(adapter
);
1285 if( (byte
& VALID_INTR_BYTE
) == 0 ) {
1287 * No more pending commands
1291 set_irq_state(adapter
, byte
);
1293 while((nstatus
= (volatile u8
)adapter
->mbox
->m_in
.numstatus
)
1296 adapter
->mbox
->m_in
.numstatus
= 0xFF;
1298 status
= adapter
->mbox
->m_in
.status
;
1301 * decrement the pending queue counter
1303 atomic_sub(nstatus
, &adapter
->pend_cmds
);
1305 memcpy(completed
, (void *)adapter
->mbox
->m_in
.completed
,
1308 /* Acknowledge interrupt */
1311 mega_cmd_done(adapter
, completed
, nstatus
, status
);
1313 mega_rundoneq(adapter
);
1317 /* Loop through any pending requests */
1318 if(atomic_read(&adapter
->quiescent
) == 0) {
1319 mega_runpendq(adapter
);
1326 spin_unlock_irqrestore(&adapter
->lock
, flags
);
1328 return IRQ_RETVAL(handled
);
1333 * megaraid_isr_memmapped()
1335 * @devp - pointer to our soft state
1337 * Interrupt service routine for memory-mapped controllers.
1338 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1339 * and service the completed commands.
1342 megaraid_isr_memmapped(int irq
, void *devp
)
1344 adapter_t
*adapter
= devp
;
1345 unsigned long flags
;
1349 u8 completed
[MAX_FIRMWARE_STATUS
];
1354 * loop till F/W has more commands for us to complete.
1356 spin_lock_irqsave(&adapter
->lock
, flags
);
1359 /* Check if a valid interrupt is pending */
1360 dword
= RDOUTDOOR(adapter
);
1361 if(dword
!= 0x10001234) {
1363 * No more pending commands
1367 WROUTDOOR(adapter
, 0x10001234);
1369 while((nstatus
= (volatile u8
)adapter
->mbox
->m_in
.numstatus
)
1373 adapter
->mbox
->m_in
.numstatus
= 0xFF;
1375 status
= adapter
->mbox
->m_in
.status
;
1378 * decrement the pending queue counter
1380 atomic_sub(nstatus
, &adapter
->pend_cmds
);
1382 memcpy(completed
, (void *)adapter
->mbox
->m_in
.completed
,
1385 /* Acknowledge interrupt */
1386 WRINDOOR(adapter
, 0x2);
1390 while( RDINDOOR(adapter
) & 0x02 )
1393 mega_cmd_done(adapter
, completed
, nstatus
, status
);
1395 mega_rundoneq(adapter
);
1397 /* Loop through any pending requests */
1398 if(atomic_read(&adapter
->quiescent
) == 0) {
1399 mega_runpendq(adapter
);
1406 spin_unlock_irqrestore(&adapter
->lock
, flags
);
1408 return IRQ_RETVAL(handled
);
1412 * @adapter - pointer to our soft state
1413 * @completed - array of ids of completed commands
1414 * @nstatus - number of completed commands
1415 * @status - status of the last command completed
1417 * Complete the commands and call the scsi mid-layer callback hooks.
1420 mega_cmd_done(adapter_t
*adapter
, u8 completed
[], int nstatus
, int status
)
1422 mega_ext_passthru
*epthru
= NULL
;
1423 struct scatterlist
*sgl
;
1424 Scsi_Cmnd
*cmd
= NULL
;
1425 mega_passthru
*pthru
= NULL
;
1426 mbox_t
*mbox
= NULL
;
1434 * for all the commands completed, call the mid-layer callback routine
1437 for( i
= 0; i
< nstatus
; i
++ ) {
1439 cmdid
= completed
[i
];
1441 if( cmdid
== CMDID_INT_CMDS
) { /* internal command */
1442 scb
= &adapter
->int_scb
;
1444 mbox
= (mbox_t
*)scb
->raw_mbox
;
1447 * Internal command interface do not fire the extended
1448 * passthru or 64-bit passthru
1454 scb
= &adapter
->scb_list
[cmdid
];
1457 * Make sure f/w has completed a valid command
1459 if( !(scb
->state
& SCB_ISSUED
) || scb
->cmd
== NULL
) {
1461 "megaraid: invalid command ");
1462 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1463 cmdid
, scb
->state
, scb
->cmd
);
1469 * Was a abort issued for this command
1471 if( scb
->state
& SCB_ABORT
) {
1474 "megaraid: aborted cmd [%x] complete.\n",
1477 scb
->cmd
->result
= (DID_ABORT
<< 16);
1479 list_add_tail(SCSI_LIST(scb
->cmd
),
1480 &adapter
->completed_list
);
1482 mega_free_scb(adapter
, scb
);
1488 * Was a reset issued for this command
1490 if( scb
->state
& SCB_RESET
) {
1493 "megaraid: reset cmd [%x] complete.\n",
1496 scb
->cmd
->result
= (DID_RESET
<< 16);
1498 list_add_tail(SCSI_LIST(scb
->cmd
),
1499 &adapter
->completed_list
);
1501 mega_free_scb (adapter
, scb
);
1508 epthru
= scb
->epthru
;
1509 mbox
= (mbox_t
*)scb
->raw_mbox
;
1514 int logdrv
= mbox
->m_out
.logdrv
;
1516 islogical
= adapter
->logdrv_chan
[cmd
->channel
];
1518 * Maintain an error counter for the logical drive.
1519 * Some application like SNMP agent need such
1522 if( status
&& islogical
&& (cmd
->cmnd
[0] == READ_6
||
1523 cmd
->cmnd
[0] == READ_10
||
1524 cmd
->cmnd
[0] == READ_12
)) {
1526 * Logical drive number increases by 0x80 when
1527 * a logical drive is deleted
1529 adapter
->rd_errors
[logdrv
%0x80]++;
1532 if( status
&& islogical
&& (cmd
->cmnd
[0] == WRITE_6
||
1533 cmd
->cmnd
[0] == WRITE_10
||
1534 cmd
->cmnd
[0] == WRITE_12
)) {
1536 * Logical drive number increases by 0x80 when
1537 * a logical drive is deleted
1539 adapter
->wr_errors
[logdrv
%0x80]++;
1547 * Do not return the presence of hard disk on the channel so,
1548 * inquiry sent, and returned data==hard disk or removable
1549 * hard disk and not logical, request should return failure! -
1552 islogical
= adapter
->logdrv_chan
[cmd
->device
->channel
];
1553 if( cmd
->cmnd
[0] == INQUIRY
&& !islogical
) {
1555 sgl
= scsi_sglist(cmd
);
1556 if( sg_page(sgl
) ) {
1557 c
= *(unsigned char *) sg_virt(&sgl
[0]);
1560 "megaraid: invalid sg.\n");
1564 if(IS_RAID_CH(adapter
, cmd
->device
->channel
) &&
1565 ((c
& 0x1F ) == TYPE_DISK
)) {
1570 /* clear result; otherwise, success returns corrupt value */
1573 /* Convert MegaRAID status to Linux error code */
1575 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1576 cmd
->result
|= (DID_OK
<< 16);
1579 case 0x02: /* ERROR_ABORTED, i.e.
1580 SCSI_STATUS_CHECK_CONDITION */
1582 /* set sense_buffer and result fields */
1583 if( mbox
->m_out
.cmd
== MEGA_MBOXCMD_PASSTHRU
||
1584 mbox
->m_out
.cmd
== MEGA_MBOXCMD_PASSTHRU64
) {
1586 memcpy(cmd
->sense_buffer
, pthru
->reqsensearea
,
1589 cmd
->result
= (DRIVER_SENSE
<< 24) |
1591 (CHECK_CONDITION
<< 1);
1594 if (mbox
->m_out
.cmd
== MEGA_MBOXCMD_EXTPTHRU
) {
1596 memcpy(cmd
->sense_buffer
,
1597 epthru
->reqsensearea
, 14);
1599 cmd
->result
= (DRIVER_SENSE
<< 24) |
1601 (CHECK_CONDITION
<< 1);
1603 cmd
->sense_buffer
[0] = 0x70;
1604 cmd
->sense_buffer
[2] = ABORTED_COMMAND
;
1605 cmd
->result
|= (CHECK_CONDITION
<< 1);
1610 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1612 cmd
->result
|= (DID_BUS_BUSY
<< 16) | status
;
1616 #if MEGA_HAVE_CLUSTERING
1618 * If TEST_UNIT_READY fails, we know
1619 * MEGA_RESERVATION_STATUS failed
1621 if( cmd
->cmnd
[0] == TEST_UNIT_READY
) {
1622 cmd
->result
|= (DID_ERROR
<< 16) |
1623 (RESERVATION_CONFLICT
<< 1);
1627 * Error code returned is 1 if Reserve or Release
1628 * failed or the input parameter is invalid
1631 (cmd
->cmnd
[0] == RESERVE
||
1632 cmd
->cmnd
[0] == RELEASE
) ) {
1634 cmd
->result
|= (DID_ERROR
<< 16) |
1635 (RESERVATION_CONFLICT
<< 1);
1639 cmd
->result
|= (DID_BAD_TARGET
<< 16)|status
;
1643 * Only free SCBs for the commands coming down from the
1644 * mid-layer, not for which were issued internally
1646 * For internal command, restore the status returned by the
1647 * firmware so that user can interpret it.
1649 if( cmdid
== CMDID_INT_CMDS
) { /* internal command */
1650 cmd
->result
= status
;
1653 * Remove the internal command from the pending list
1655 list_del_init(&scb
->list
);
1656 scb
->state
= SCB_FREE
;
1659 mega_free_scb(adapter
, scb
);
1662 /* Add Scsi_Command to end of completed queue */
1663 list_add_tail(SCSI_LIST(cmd
), &adapter
->completed_list
);
1671 * Run through the list of completed requests and finish it
1674 mega_rundoneq (adapter_t
*adapter
)
1677 struct list_head
*pos
;
1679 list_for_each(pos
, &adapter
->completed_list
) {
1681 struct scsi_pointer
* spos
= (struct scsi_pointer
*)pos
;
1683 cmd
= list_entry(spos
, Scsi_Cmnd
, SCp
);
1684 cmd
->scsi_done(cmd
);
1687 INIT_LIST_HEAD(&adapter
->completed_list
);
1692 * Free a SCB structure
1693 * Note: We assume the scsi commands associated with this scb is not free yet.
1696 mega_free_scb(adapter_t
*adapter
, scb_t
*scb
)
1698 switch( scb
->dma_type
) {
1700 case MEGA_DMA_TYPE_NONE
:
1704 scsi_dma_unmap(scb
->cmd
);
1711 * Remove from the pending list
1713 list_del_init(&scb
->list
);
1715 /* Link the scb back into free list */
1716 scb
->state
= SCB_FREE
;
1719 list_add(&scb
->list
, &adapter
->free_list
);
1724 __mega_busywait_mbox (adapter_t
*adapter
)
1726 volatile mbox_t
*mbox
= adapter
->mbox
;
1729 for (counter
= 0; counter
< 10000; counter
++) {
1730 if (!mbox
->m_in
.busy
)
1735 return -1; /* give up after 1 second */
1739 * Copies data to SGLIST
1740 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1743 mega_build_sglist(adapter_t
*adapter
, scb_t
*scb
, u32
*buf
, u32
*len
)
1745 struct scatterlist
*sg
;
1753 * Copy Scatter-Gather list info into controller structure.
1755 * The number of sg elements returned must not exceed our limit
1757 sgcnt
= scsi_dma_map(cmd
);
1759 scb
->dma_type
= MEGA_SGLIST
;
1761 BUG_ON(sgcnt
> adapter
->sglen
|| sgcnt
< 0);
1765 if (scsi_sg_count(cmd
) == 1 && !adapter
->has_64bit_addr
) {
1766 sg
= scsi_sglist(cmd
);
1767 scb
->dma_h_bulkdata
= sg_dma_address(sg
);
1768 *buf
= (u32
)scb
->dma_h_bulkdata
;
1769 *len
= sg_dma_len(sg
);
1773 scsi_for_each_sg(cmd
, sg
, sgcnt
, idx
) {
1774 if (adapter
->has_64bit_addr
) {
1775 scb
->sgl64
[idx
].address
= sg_dma_address(sg
);
1776 *len
+= scb
->sgl64
[idx
].length
= sg_dma_len(sg
);
1778 scb
->sgl
[idx
].address
= sg_dma_address(sg
);
1779 *len
+= scb
->sgl
[idx
].length
= sg_dma_len(sg
);
1783 /* Reset pointer and length fields */
1784 *buf
= scb
->sgl_dma_addr
;
1786 /* Return count of SG requests */
1794 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1795 * Enquiry3 structures for later use
1798 mega_8_to_40ld(mraid_inquiry
*inquiry
, mega_inquiry3
*enquiry3
,
1799 mega_product_info
*product_info
)
1803 product_info
->max_commands
= inquiry
->adapter_info
.max_commands
;
1804 enquiry3
->rebuild_rate
= inquiry
->adapter_info
.rebuild_rate
;
1805 product_info
->nchannels
= inquiry
->adapter_info
.nchannels
;
1807 for (i
= 0; i
< 4; i
++) {
1808 product_info
->fw_version
[i
] =
1809 inquiry
->adapter_info
.fw_version
[i
];
1811 product_info
->bios_version
[i
] =
1812 inquiry
->adapter_info
.bios_version
[i
];
1814 enquiry3
->cache_flush_interval
=
1815 inquiry
->adapter_info
.cache_flush_interval
;
1817 product_info
->dram_size
= inquiry
->adapter_info
.dram_size
;
1819 enquiry3
->num_ldrv
= inquiry
->logdrv_info
.num_ldrv
;
1821 for (i
= 0; i
< MAX_LOGICAL_DRIVES_8LD
; i
++) {
1822 enquiry3
->ldrv_size
[i
] = inquiry
->logdrv_info
.ldrv_size
[i
];
1823 enquiry3
->ldrv_prop
[i
] = inquiry
->logdrv_info
.ldrv_prop
[i
];
1824 enquiry3
->ldrv_state
[i
] = inquiry
->logdrv_info
.ldrv_state
[i
];
1827 for (i
= 0; i
< (MAX_PHYSICAL_DRIVES
); i
++)
1828 enquiry3
->pdrv_state
[i
] = inquiry
->pdrv_info
.pdrv_state
[i
];
1832 mega_free_sgl(adapter_t
*adapter
)
1837 for(i
= 0; i
< adapter
->max_cmds
; i
++) {
1839 scb
= &adapter
->scb_list
[i
];
1842 pci_free_consistent(adapter
->dev
,
1843 sizeof(mega_sgl64
) * adapter
->sglen
,
1851 pci_free_consistent(adapter
->dev
, sizeof(mega_passthru
),
1852 scb
->pthru
, scb
->pthru_dma_addr
);
1858 pci_free_consistent(adapter
->dev
,
1859 sizeof(mega_ext_passthru
),
1860 scb
->epthru
, scb
->epthru_dma_addr
);
1870 * Get information about the card/driver
1873 megaraid_info(struct Scsi_Host
*host
)
1875 static char buffer
[512];
1878 adapter
= (adapter_t
*)host
->hostdata
;
1881 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1882 adapter
->fw_version
, adapter
->product_info
.max_commands
,
1883 adapter
->host
->max_id
, adapter
->host
->max_channel
,
1884 adapter
->host
->max_lun
);
1889 * Abort a previous SCSI request. Only commands on the pending list can be
1890 * aborted. All the commands issued to the F/W must complete.
1893 megaraid_abort(Scsi_Cmnd
*cmd
)
1898 adapter
= (adapter_t
*)cmd
->device
->host
->hostdata
;
1900 rval
= megaraid_abort_and_reset(adapter
, cmd
, SCB_ABORT
);
1903 * This is required here to complete any completed requests
1904 * to be communicated over to the mid layer.
1906 mega_rundoneq(adapter
);
1913 megaraid_reset(struct scsi_cmnd
*cmd
)
1919 adapter
= (adapter_t
*)cmd
->device
->host
->hostdata
;
1921 #if MEGA_HAVE_CLUSTERING
1922 mc
.cmd
= MEGA_CLUSTER_CMD
;
1923 mc
.opcode
= MEGA_RESET_RESERVATIONS
;
1925 if( mega_internal_command(adapter
, &mc
, NULL
) != 0 ) {
1927 "megaraid: reservation reset failed.\n");
1930 printk(KERN_INFO
"megaraid: reservation reset.\n");
1934 spin_lock_irq(&adapter
->lock
);
1936 rval
= megaraid_abort_and_reset(adapter
, cmd
, SCB_RESET
);
1939 * This is required here to complete any completed requests
1940 * to be communicated over to the mid layer.
1942 mega_rundoneq(adapter
);
1943 spin_unlock_irq(&adapter
->lock
);
1949 * megaraid_abort_and_reset()
1950 * @adapter - megaraid soft state
1951 * @cmd - scsi command to be aborted or reset
1952 * @aor - abort or reset flag
1954 * Try to locate the scsi command in the pending queue. If found and is not
1955 * issued to the controller, abort/reset it. Otherwise return failure
1958 megaraid_abort_and_reset(adapter_t
*adapter
, Scsi_Cmnd
*cmd
, int aor
)
1960 struct list_head
*pos
, *next
;
1963 printk(KERN_WARNING
"megaraid: %s cmd=%x <c=%d t=%d l=%d>\n",
1964 (aor
== SCB_ABORT
)? "ABORTING":"RESET",
1965 cmd
->cmnd
[0], cmd
->device
->channel
,
1966 cmd
->device
->id
, cmd
->device
->lun
);
1968 if(list_empty(&adapter
->pending_list
))
1971 list_for_each_safe(pos
, next
, &adapter
->pending_list
) {
1973 scb
= list_entry(pos
, scb_t
, list
);
1975 if (scb
->cmd
== cmd
) { /* Found command */
1980 * Check if this command has firmware ownership. If
1981 * yes, we cannot reset this command. Whenever f/w
1982 * completes this command, we will return appropriate
1985 if( scb
->state
& SCB_ISSUED
) {
1988 "megaraid: %s[%x], fw owner.\n",
1989 (aor
==SCB_ABORT
) ? "ABORTING":"RESET",
1997 * Not yet issued! Remove from the pending
2001 "megaraid: %s-[%x], driver owner.\n",
2002 (aor
==SCB_ABORT
) ? "ABORTING":"RESET",
2005 mega_free_scb(adapter
, scb
);
2007 if( aor
== SCB_ABORT
) {
2008 cmd
->result
= (DID_ABORT
<< 16);
2011 cmd
->result
= (DID_RESET
<< 16);
2014 list_add_tail(SCSI_LIST(cmd
),
2015 &adapter
->completed_list
);
2026 make_local_pdev(adapter_t
*adapter
, struct pci_dev
**pdev
)
2028 *pdev
= alloc_pci_dev();
2030 if( *pdev
== NULL
) return -1;
2032 memcpy(*pdev
, adapter
->dev
, sizeof(struct pci_dev
));
2034 if( pci_set_dma_mask(*pdev
, DMA_BIT_MASK(32)) != 0 ) {
2043 free_local_pdev(struct pci_dev
*pdev
)
2049 * mega_allocate_inquiry()
2050 * @dma_handle - handle returned for dma address
2051 * @pdev - handle to pci device
2053 * allocates memory for inquiry structure
2055 static inline void *
2056 mega_allocate_inquiry(dma_addr_t
*dma_handle
, struct pci_dev
*pdev
)
2058 return pci_alloc_consistent(pdev
, sizeof(mega_inquiry3
), dma_handle
);
2063 mega_free_inquiry(void *inquiry
, dma_addr_t dma_handle
, struct pci_dev
*pdev
)
2065 pci_free_consistent(pdev
, sizeof(mega_inquiry3
), inquiry
, dma_handle
);
2069 #ifdef CONFIG_PROC_FS
2070 /* Following code handles /proc fs */
2072 #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
2073 S_IRUSR | S_IFREG, \
2074 controller_proc_dir_entry, \
2078 * mega_create_proc_entry()
2079 * @index - index in soft state array
2080 * @parent - parent node for this /proc entry
2082 * Creates /proc entries for our controllers.
2085 mega_create_proc_entry(int index
, struct proc_dir_entry
*parent
)
2087 struct proc_dir_entry
*controller_proc_dir_entry
= NULL
;
2088 u8 string
[64] = { 0 };
2089 adapter_t
*adapter
= hba_soft_state
[index
];
2091 sprintf(string
, "hba%d", adapter
->host
->host_no
);
2093 controller_proc_dir_entry
=
2094 adapter
->controller_proc_dir_entry
= proc_mkdir(string
, parent
);
2096 if(!controller_proc_dir_entry
) {
2097 printk(KERN_WARNING
"\nmegaraid: proc_mkdir failed\n");
2100 adapter
->proc_read
= CREATE_READ_PROC("config", proc_read_config
);
2101 adapter
->proc_stat
= CREATE_READ_PROC("stat", proc_read_stat
);
2102 adapter
->proc_mbox
= CREATE_READ_PROC("mailbox", proc_read_mbox
);
2103 #if MEGA_HAVE_ENH_PROC
2104 adapter
->proc_rr
= CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate
);
2105 adapter
->proc_battery
= CREATE_READ_PROC("battery-status",
2109 * Display each physical drive on its channel
2111 adapter
->proc_pdrvstat
[0] = CREATE_READ_PROC("diskdrives-ch0",
2113 adapter
->proc_pdrvstat
[1] = CREATE_READ_PROC("diskdrives-ch1",
2115 adapter
->proc_pdrvstat
[2] = CREATE_READ_PROC("diskdrives-ch2",
2117 adapter
->proc_pdrvstat
[3] = CREATE_READ_PROC("diskdrives-ch3",
2121 * Display a set of up to 10 logical drive through each of following
2124 adapter
->proc_rdrvstat
[0] = CREATE_READ_PROC("raiddrives-0-9",
2126 adapter
->proc_rdrvstat
[1] = CREATE_READ_PROC("raiddrives-10-19",
2128 adapter
->proc_rdrvstat
[2] = CREATE_READ_PROC("raiddrives-20-29",
2130 adapter
->proc_rdrvstat
[3] = CREATE_READ_PROC("raiddrives-30-39",
2137 * proc_read_config()
2138 * @page - buffer to write the data in
2139 * @start - where the actual data has been written in page
2140 * @offset - same meaning as the read system call
2141 * @count - same meaning as the read system call
2142 * @eof - set if no more data needs to be returned
2143 * @data - pointer to our soft state
2145 * Display configuration information about the controller.
2148 proc_read_config(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2152 adapter_t
*adapter
= (adapter_t
*)data
;
2155 len
+= sprintf(page
+len
, "%s", MEGARAID_VERSION
);
2157 if(adapter
->product_info
.product_name
[0])
2158 len
+= sprintf(page
+len
, "%s\n",
2159 adapter
->product_info
.product_name
);
2161 len
+= sprintf(page
+len
, "Controller Type: ");
2163 if( adapter
->flag
& BOARD_MEMMAP
) {
2164 len
+= sprintf(page
+len
,
2165 "438/466/467/471/493/518/520/531/532\n");
2168 len
+= sprintf(page
+len
,
2172 if(adapter
->flag
& BOARD_40LD
) {
2173 len
+= sprintf(page
+len
,
2174 "Controller Supports 40 Logical Drives\n");
2177 if(adapter
->flag
& BOARD_64BIT
) {
2178 len
+= sprintf(page
+len
,
2179 "Controller capable of 64-bit memory addressing\n");
2181 if( adapter
->has_64bit_addr
) {
2182 len
+= sprintf(page
+len
,
2183 "Controller using 64-bit memory addressing\n");
2186 len
+= sprintf(page
+len
,
2187 "Controller is not using 64-bit memory addressing\n");
2190 len
+= sprintf(page
+len
, "Base = %08lx, Irq = %d, ", adapter
->base
,
2191 adapter
->host
->irq
);
2193 len
+= sprintf(page
+len
, "Logical Drives = %d, Channels = %d\n",
2194 adapter
->numldrv
, adapter
->product_info
.nchannels
);
2196 len
+= sprintf(page
+len
, "Version =%s:%s, DRAM = %dMb\n",
2197 adapter
->fw_version
, adapter
->bios_version
,
2198 adapter
->product_info
.dram_size
);
2200 len
+= sprintf(page
+len
,
2201 "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2202 adapter
->product_info
.max_commands
, adapter
->max_cmds
);
2204 len
+= sprintf(page
+len
, "support_ext_cdb = %d\n",
2205 adapter
->support_ext_cdb
);
2206 len
+= sprintf(page
+len
, "support_random_del = %d\n",
2207 adapter
->support_random_del
);
2208 len
+= sprintf(page
+len
, "boot_ldrv_enabled = %d\n",
2209 adapter
->boot_ldrv_enabled
);
2210 len
+= sprintf(page
+len
, "boot_ldrv = %d\n",
2211 adapter
->boot_ldrv
);
2212 len
+= sprintf(page
+len
, "boot_pdrv_enabled = %d\n",
2213 adapter
->boot_pdrv_enabled
);
2214 len
+= sprintf(page
+len
, "boot_pdrv_ch = %d\n",
2215 adapter
->boot_pdrv_ch
);
2216 len
+= sprintf(page
+len
, "boot_pdrv_tgt = %d\n",
2217 adapter
->boot_pdrv_tgt
);
2218 len
+= sprintf(page
+len
, "quiescent = %d\n",
2219 atomic_read(&adapter
->quiescent
));
2220 len
+= sprintf(page
+len
, "has_cluster = %d\n",
2221 adapter
->has_cluster
);
2223 len
+= sprintf(page
+len
, "\nModule Parameters:\n");
2224 len
+= sprintf(page
+len
, "max_cmd_per_lun = %d\n",
2226 len
+= sprintf(page
+len
, "max_sectors_per_io = %d\n",
2227 max_sectors_per_io
);
2238 * @page - buffer to write the data in
2239 * @start - where the actual data has been written in page
2240 * @offset - same meaning as the read system call
2241 * @count - same meaning as the read system call
2242 * @eof - set if no more data needs to be returned
2243 * @data - pointer to our soft state
2245 * Diaplay statistical information about the I/O activity.
2248 proc_read_stat(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2255 i
= 0; /* avoid compilation warnings */
2257 adapter
= (adapter_t
*)data
;
2259 len
= sprintf(page
, "Statistical Information for this controller\n");
2260 len
+= sprintf(page
+len
, "pend_cmds = %d\n",
2261 atomic_read(&adapter
->pend_cmds
));
2263 for(i
= 0; i
< adapter
->numldrv
; i
++) {
2264 len
+= sprintf(page
+len
, "Logical Drive %d:\n", i
);
2266 len
+= sprintf(page
+len
,
2267 "\tReads Issued = %lu, Writes Issued = %lu\n",
2268 adapter
->nreads
[i
], adapter
->nwrites
[i
]);
2270 len
+= sprintf(page
+len
,
2271 "\tSectors Read = %lu, Sectors Written = %lu\n",
2272 adapter
->nreadblocks
[i
], adapter
->nwriteblocks
[i
]);
2274 len
+= sprintf(page
+len
,
2275 "\tRead errors = %lu, Write errors = %lu\n\n",
2276 adapter
->rd_errors
[i
], adapter
->wr_errors
[i
]);
2279 len
+= sprintf(page
+len
,
2280 "IO and error counters not compiled in driver.\n");
2291 * @page - buffer to write the data in
2292 * @start - where the actual data has been written in page
2293 * @offset - same meaning as the read system call
2294 * @count - same meaning as the read system call
2295 * @eof - set if no more data needs to be returned
2296 * @data - pointer to our soft state
2298 * Display mailbox information for the last command issued. This information
2299 * is good for debugging.
2302 proc_read_mbox(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2306 adapter_t
*adapter
= (adapter_t
*)data
;
2307 volatile mbox_t
*mbox
= adapter
->mbox
;
2310 len
= sprintf(page
, "Contents of Mail Box Structure\n");
2311 len
+= sprintf(page
+len
, " Fw Command = 0x%02x\n",
2313 len
+= sprintf(page
+len
, " Cmd Sequence = 0x%02x\n",
2315 len
+= sprintf(page
+len
, " No of Sectors= %04d\n",
2316 mbox
->m_out
.numsectors
);
2317 len
+= sprintf(page
+len
, " LBA = 0x%02x\n",
2319 len
+= sprintf(page
+len
, " DTA = 0x%08x\n",
2320 mbox
->m_out
.xferaddr
);
2321 len
+= sprintf(page
+len
, " Logical Drive= 0x%02x\n",
2322 mbox
->m_out
.logdrv
);
2323 len
+= sprintf(page
+len
, " No of SG Elmt= 0x%02x\n",
2324 mbox
->m_out
.numsgelements
);
2325 len
+= sprintf(page
+len
, " Busy = %01x\n",
2327 len
+= sprintf(page
+len
, " Status = 0x%02x\n",
2337 * proc_rebuild_rate()
2338 * @page - buffer to write the data in
2339 * @start - where the actual data has been written in page
2340 * @offset - same meaning as the read system call
2341 * @count - same meaning as the read system call
2342 * @eof - set if no more data needs to be returned
2343 * @data - pointer to our soft state
2345 * Display current rebuild rate
2348 proc_rebuild_rate(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2351 adapter_t
*adapter
= (adapter_t
*)data
;
2352 dma_addr_t dma_handle
;
2354 struct pci_dev
*pdev
;
2357 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2362 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2363 free_local_pdev(pdev
);
2368 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2370 len
= sprintf(page
, "Adapter inquiry failed.\n");
2372 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2374 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2376 free_local_pdev(pdev
);
2383 if( adapter
->flag
& BOARD_40LD
) {
2384 len
= sprintf(page
, "Rebuild Rate: [%d%%]\n",
2385 ((mega_inquiry3
*)inquiry
)->rebuild_rate
);
2388 len
= sprintf(page
, "Rebuild Rate: [%d%%]\n",
2389 ((mraid_ext_inquiry
*)
2390 inquiry
)->raid_inq
.adapter_info
.rebuild_rate
);
2394 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2396 free_local_pdev(pdev
);
2406 * @page - buffer to write the data in
2407 * @start - where the actual data has been written in page
2408 * @offset - same meaning as the read system call
2409 * @count - same meaning as the read system call
2410 * @eof - set if no more data needs to be returned
2411 * @data - pointer to our soft state
2413 * Display information about the battery module on the controller.
2416 proc_battery(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2419 adapter_t
*adapter
= (adapter_t
*)data
;
2420 dma_addr_t dma_handle
;
2422 struct pci_dev
*pdev
;
2423 u8 battery_status
= 0;
2427 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2432 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2433 free_local_pdev(pdev
);
2438 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2440 len
= sprintf(page
, "Adapter inquiry failed.\n");
2442 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2444 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2446 free_local_pdev(pdev
);
2453 if( adapter
->flag
& BOARD_40LD
) {
2454 battery_status
= ((mega_inquiry3
*)inquiry
)->battery_status
;
2457 battery_status
= ((mraid_ext_inquiry
*)inquiry
)->
2458 raid_inq
.adapter_info
.battery_status
;
2462 * Decode the battery status
2464 sprintf(str
, "Battery Status:[%d]", battery_status
);
2466 if(battery_status
== MEGA_BATT_CHARGE_DONE
)
2467 strcat(str
, " Charge Done");
2469 if(battery_status
& MEGA_BATT_MODULE_MISSING
)
2470 strcat(str
, " Module Missing");
2472 if(battery_status
& MEGA_BATT_LOW_VOLTAGE
)
2473 strcat(str
, " Low Voltage");
2475 if(battery_status
& MEGA_BATT_TEMP_HIGH
)
2476 strcat(str
, " Temperature High");
2478 if(battery_status
& MEGA_BATT_PACK_MISSING
)
2479 strcat(str
, " Pack Missing");
2481 if(battery_status
& MEGA_BATT_CHARGE_INPROG
)
2482 strcat(str
, " Charge In-progress");
2484 if(battery_status
& MEGA_BATT_CHARGE_FAIL
)
2485 strcat(str
, " Charge Fail");
2487 if(battery_status
& MEGA_BATT_CYCLES_EXCEEDED
)
2488 strcat(str
, " Cycles Exceeded");
2490 len
= sprintf(page
, "%s\n", str
);
2493 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2495 free_local_pdev(pdev
);
2505 * @page - buffer to write the data in
2506 * @start - where the actual data has been written in page
2507 * @offset - same meaning as the read system call
2508 * @count - same meaning as the read system call
2509 * @eof - set if no more data needs to be returned
2510 * @data - pointer to our soft state
2512 * Display information about the physical drives on physical channel 0.
2515 proc_pdrv_ch0(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2518 adapter_t
*adapter
= (adapter_t
*)data
;
2522 return (proc_pdrv(adapter
, page
, 0));
2528 * @page - buffer to write the data in
2529 * @start - where the actual data has been written in page
2530 * @offset - same meaning as the read system call
2531 * @count - same meaning as the read system call
2532 * @eof - set if no more data needs to be returned
2533 * @data - pointer to our soft state
2535 * Display information about the physical drives on physical channel 1.
2538 proc_pdrv_ch1(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2541 adapter_t
*adapter
= (adapter_t
*)data
;
2545 return (proc_pdrv(adapter
, page
, 1));
2551 * @page - buffer to write the data in
2552 * @start - where the actual data has been written in page
2553 * @offset - same meaning as the read system call
2554 * @count - same meaning as the read system call
2555 * @eof - set if no more data needs to be returned
2556 * @data - pointer to our soft state
2558 * Display information about the physical drives on physical channel 2.
2561 proc_pdrv_ch2(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2564 adapter_t
*adapter
= (adapter_t
*)data
;
2568 return (proc_pdrv(adapter
, page
, 2));
2574 * @page - buffer to write the data in
2575 * @start - where the actual data has been written in page
2576 * @offset - same meaning as the read system call
2577 * @count - same meaning as the read system call
2578 * @eof - set if no more data needs to be returned
2579 * @data - pointer to our soft state
2581 * Display information about the physical drives on physical channel 3.
2584 proc_pdrv_ch3(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2587 adapter_t
*adapter
= (adapter_t
*)data
;
2591 return (proc_pdrv(adapter
, page
, 3));
2597 * @page - buffer to write the data in
2598 * @adapter - pointer to our soft state
2600 * Display information about the physical drives.
2603 proc_pdrv(adapter_t
*adapter
, char *page
, int channel
)
2605 dma_addr_t dma_handle
;
2607 dma_addr_t scsi_inq_dma_handle
;
2609 struct pci_dev
*pdev
;
2618 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2622 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2626 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2627 len
= sprintf(page
, "Adapter inquiry failed.\n");
2629 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2635 scsi_inq
= pci_alloc_consistent(pdev
, 256, &scsi_inq_dma_handle
);
2637 if( scsi_inq
== NULL
) {
2638 len
= sprintf(page
, "memory not available for scsi inq.\n");
2643 if( adapter
->flag
& BOARD_40LD
) {
2644 pdrv_state
= ((mega_inquiry3
*)inquiry
)->pdrv_state
;
2647 pdrv_state
= ((mraid_ext_inquiry
*)inquiry
)->
2648 raid_inq
.pdrv_info
.pdrv_state
;
2651 max_channels
= adapter
->product_info
.nchannels
;
2653 if( channel
>= max_channels
) {
2657 for( tgt
= 0; tgt
<= MAX_TARGET
; tgt
++ ) {
2659 i
= channel
*16 + tgt
;
2661 state
= *(pdrv_state
+ i
);
2663 switch( state
& 0x0F ) {
2667 "Channel:%2d Id:%2d State: Online",
2673 "Channel:%2d Id:%2d State: Failed",
2679 "Channel:%2d Id:%2d State: Rebuild",
2685 "Channel:%2d Id:%2d State: Hot spare",
2691 "Channel:%2d Id:%2d State: Un-configured",
2698 * This interface displays inquiries for disk drives
2699 * only. Inquries for logical drives and non-disk
2700 * devices are available through /proc/scsi/scsi
2702 memset(scsi_inq
, 0, 256);
2703 if( mega_internal_dev_inquiry(adapter
, channel
, tgt
,
2704 scsi_inq_dma_handle
) ||
2705 (scsi_inq
[0] & 0x1F) != TYPE_DISK
) {
2710 * Check for overflow. We print less than 240
2711 * characters for inquiry
2713 if( (len
+ 240) >= PAGE_SIZE
) break;
2715 len
+= sprintf(page
+len
, "%s.\n", str
);
2717 len
+= mega_print_inquiry(page
+len
, scsi_inq
);
2721 pci_free_consistent(pdev
, 256, scsi_inq
, scsi_inq_dma_handle
);
2723 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2725 free_local_pdev(pdev
);
2732 * Display scsi inquiry
2735 mega_print_inquiry(char *page
, char *scsi_inq
)
2740 len
= sprintf(page
, " Vendor: ");
2741 for( i
= 8; i
< 16; i
++ ) {
2742 len
+= sprintf(page
+len
, "%c", scsi_inq
[i
]);
2745 len
+= sprintf(page
+len
, " Model: ");
2747 for( i
= 16; i
< 32; i
++ ) {
2748 len
+= sprintf(page
+len
, "%c", scsi_inq
[i
]);
2751 len
+= sprintf(page
+len
, " Rev: ");
2753 for( i
= 32; i
< 36; i
++ ) {
2754 len
+= sprintf(page
+len
, "%c", scsi_inq
[i
]);
2757 len
+= sprintf(page
+len
, "\n");
2759 i
= scsi_inq
[0] & 0x1f;
2761 len
+= sprintf(page
+len
, " Type: %s ", scsi_device_type(i
));
2763 len
+= sprintf(page
+len
,
2764 " ANSI SCSI revision: %02x", scsi_inq
[2] & 0x07);
2766 if( (scsi_inq
[2] & 0x07) == 1 && (scsi_inq
[3] & 0x0f) == 1 )
2767 len
+= sprintf(page
+len
, " CCS\n");
2769 len
+= sprintf(page
+len
, "\n");
2777 * @page - buffer to write the data in
2778 * @start - where the actual data has been written in page
2779 * @offset - same meaning as the read system call
2780 * @count - same meaning as the read system call
2781 * @eof - set if no more data needs to be returned
2782 * @data - pointer to our soft state
2784 * Display real time information about the logical drives 0 through 9.
2787 proc_rdrv_10(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2790 adapter_t
*adapter
= (adapter_t
*)data
;
2794 return (proc_rdrv(adapter
, page
, 0, 9));
2800 * @page - buffer to write the data in
2801 * @start - where the actual data has been written in page
2802 * @offset - same meaning as the read system call
2803 * @count - same meaning as the read system call
2804 * @eof - set if no more data needs to be returned
2805 * @data - pointer to our soft state
2807 * Display real time information about the logical drives 0 through 9.
2810 proc_rdrv_20(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2813 adapter_t
*adapter
= (adapter_t
*)data
;
2817 return (proc_rdrv(adapter
, page
, 10, 19));
2823 * @page - buffer to write the data in
2824 * @start - where the actual data has been written in page
2825 * @offset - same meaning as the read system call
2826 * @count - same meaning as the read system call
2827 * @eof - set if no more data needs to be returned
2828 * @data - pointer to our soft state
2830 * Display real time information about the logical drives 0 through 9.
2833 proc_rdrv_30(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2836 adapter_t
*adapter
= (adapter_t
*)data
;
2840 return (proc_rdrv(adapter
, page
, 20, 29));
2846 * @page - buffer to write the data in
2847 * @start - where the actual data has been written in page
2848 * @offset - same meaning as the read system call
2849 * @count - same meaning as the read system call
2850 * @eof - set if no more data needs to be returned
2851 * @data - pointer to our soft state
2853 * Display real time information about the logical drives 0 through 9.
2856 proc_rdrv_40(char *page
, char **start
, off_t offset
, int count
, int *eof
,
2859 adapter_t
*adapter
= (adapter_t
*)data
;
2863 return (proc_rdrv(adapter
, page
, 30, 39));
2869 * @page - buffer to write the data in
2870 * @adapter - pointer to our soft state
2871 * @start - starting logical drive to display
2872 * @end - ending logical drive to display
2874 * We do not print the inquiry information since its already available through
2875 * /proc/scsi/scsi interface
2878 proc_rdrv(adapter_t
*adapter
, char *page
, int start
, int end
)
2880 dma_addr_t dma_handle
;
2881 logdrv_param
*lparam
;
2884 dma_addr_t disk_array_dma_handle
;
2886 struct pci_dev
*pdev
;
2893 if( make_local_pdev(adapter
, &pdev
) != 0 ) {
2897 if( (inquiry
= mega_allocate_inquiry(&dma_handle
, pdev
)) == NULL
) {
2898 free_local_pdev(pdev
);
2902 if( mega_adapinq(adapter
, dma_handle
) != 0 ) {
2904 len
= sprintf(page
, "Adapter inquiry failed.\n");
2906 printk(KERN_WARNING
"megaraid: inquiry failed.\n");
2908 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2910 free_local_pdev(pdev
);
2915 memset(&mc
, 0, sizeof(megacmd_t
));
2917 if( adapter
->flag
& BOARD_40LD
) {
2918 array_sz
= sizeof(disk_array_40ld
);
2920 rdrv_state
= ((mega_inquiry3
*)inquiry
)->ldrv_state
;
2922 num_ldrv
= ((mega_inquiry3
*)inquiry
)->num_ldrv
;
2925 array_sz
= sizeof(disk_array_8ld
);
2927 rdrv_state
= ((mraid_ext_inquiry
*)inquiry
)->
2928 raid_inq
.logdrv_info
.ldrv_state
;
2930 num_ldrv
= ((mraid_ext_inquiry
*)inquiry
)->
2931 raid_inq
.logdrv_info
.num_ldrv
;
2934 disk_array
= pci_alloc_consistent(pdev
, array_sz
,
2935 &disk_array_dma_handle
);
2937 if( disk_array
== NULL
) {
2938 len
= sprintf(page
, "memory not available.\n");
2940 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2942 free_local_pdev(pdev
);
2947 mc
.xferaddr
= (u32
)disk_array_dma_handle
;
2949 if( adapter
->flag
& BOARD_40LD
) {
2950 mc
.cmd
= FC_NEW_CONFIG
;
2951 mc
.opcode
= OP_DCMD_READ_CONFIG
;
2953 if( mega_internal_command(adapter
, &mc
, NULL
) ) {
2955 len
= sprintf(page
, "40LD read config failed.\n");
2957 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2959 pci_free_consistent(pdev
, array_sz
, disk_array
,
2960 disk_array_dma_handle
);
2962 free_local_pdev(pdev
);
2969 mc
.cmd
= NEW_READ_CONFIG_8LD
;
2971 if( mega_internal_command(adapter
, &mc
, NULL
) ) {
2973 mc
.cmd
= READ_CONFIG_8LD
;
2975 if( mega_internal_command(adapter
, &mc
,
2979 "8LD read config failed.\n");
2981 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
2983 pci_free_consistent(pdev
, array_sz
,
2985 disk_array_dma_handle
);
2987 free_local_pdev(pdev
);
2994 for( i
= start
; i
< ( (end
+1 < num_ldrv
) ? end
+1 : num_ldrv
); i
++ ) {
2996 if( adapter
->flag
& BOARD_40LD
) {
2998 &((disk_array_40ld
*)disk_array
)->ldrv
[i
].lparam
;
3002 &((disk_array_8ld
*)disk_array
)->ldrv
[i
].lparam
;
3006 * Check for overflow. We print less than 240 characters for
3007 * information about each logical drive.
3009 if( (len
+ 240) >= PAGE_SIZE
) break;
3011 len
+= sprintf(page
+len
, "Logical drive:%2d:, ", i
);
3013 switch( rdrv_state
[i
] & 0x0F ) {
3015 len
+= sprintf(page
+len
, "state: offline");
3019 len
+= sprintf(page
+len
, "state: degraded");
3023 len
+= sprintf(page
+len
, "state: optimal");
3027 len
+= sprintf(page
+len
, "state: deleted");
3031 len
+= sprintf(page
+len
, "state: unknown");
3036 * Check if check consistency or initialization is going on
3037 * for this logical drive.
3039 if( (rdrv_state
[i
] & 0xF0) == 0x20 ) {
3040 len
+= sprintf(page
+len
,
3041 ", check-consistency in progress");
3043 else if( (rdrv_state
[i
] & 0xF0) == 0x10 ) {
3044 len
+= sprintf(page
+len
,
3045 ", initialization in progress");
3048 len
+= sprintf(page
+len
, "\n");
3050 len
+= sprintf(page
+len
, "Span depth:%3d, ",
3051 lparam
->span_depth
);
3053 len
+= sprintf(page
+len
, "RAID level:%3d, ",
3056 len
+= sprintf(page
+len
, "Stripe size:%3d, ",
3057 lparam
->stripe_sz
? lparam
->stripe_sz
/2: 128);
3059 len
+= sprintf(page
+len
, "Row size:%3d\n",
3063 len
+= sprintf(page
+len
, "Read Policy: ");
3065 switch(lparam
->read_ahead
) {
3068 len
+= sprintf(page
+len
, "No read ahead, ");
3072 len
+= sprintf(page
+len
, "Read ahead, ");
3075 case ADAP_READ_AHEAD
:
3076 len
+= sprintf(page
+len
, "Adaptive, ");
3081 len
+= sprintf(page
+len
, "Write Policy: ");
3083 switch(lparam
->write_mode
) {
3085 case WRMODE_WRITE_THRU
:
3086 len
+= sprintf(page
+len
, "Write thru, ");
3089 case WRMODE_WRITE_BACK
:
3090 len
+= sprintf(page
+len
, "Write back, ");
3094 len
+= sprintf(page
+len
, "Cache Policy: ");
3096 switch(lparam
->direct_io
) {
3099 len
+= sprintf(page
+len
, "Cached IO\n\n");
3103 len
+= sprintf(page
+len
, "Direct IO\n\n");
3108 mega_free_inquiry(inquiry
, dma_handle
, pdev
);
3110 pci_free_consistent(pdev
, array_sz
, disk_array
,
3111 disk_array_dma_handle
);
3113 free_local_pdev(pdev
);
3118 static inline void mega_create_proc_entry(int index
, struct proc_dir_entry
*parent
)
3125 * megaraid_biosparam()
3127 * Return the disk geometry for a particular disk
3130 megaraid_biosparam(struct scsi_device
*sdev
, struct block_device
*bdev
,
3131 sector_t capacity
, int geom
[])
3140 /* Get pointer to host config structure */
3141 adapter
= (adapter_t
*)sdev
->host
->hostdata
;
3143 if (IS_RAID_CH(adapter
, sdev
->channel
)) {
3144 /* Default heads (64) & sectors (32) */
3147 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3150 * Handle extended translation size for logical drives
3153 if ((ulong
)capacity
>= 0x200000) {
3156 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3162 geom
[2] = cylinders
;
3165 bh
= scsi_bios_ptable(bdev
);
3168 rval
= scsi_partsize(bh
, capacity
,
3169 &geom
[2], &geom
[0], &geom
[1]);
3176 "megaraid: invalid partition on this disk on channel %d\n",
3179 /* Default heads (64) & sectors (32) */
3182 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3184 /* Handle extended translation size for logical drives > 1Gb */
3185 if ((ulong
)capacity
>= 0x200000) {
3188 cylinders
= (ulong
)capacity
/ (heads
* sectors
);
3194 geom
[2] = cylinders
;
3202 * @adapter - pointer to our soft state
3204 * Allocate memory for the various pointers in the scb structures:
3205 * scatter-gather list pointer, passthru and extended passthru structure
3209 mega_init_scb(adapter_t
*adapter
)
3214 for( i
= 0; i
< adapter
->max_cmds
; i
++ ) {
3216 scb
= &adapter
->scb_list
[i
];
3224 for( i
= 0; i
< adapter
->max_cmds
; i
++ ) {
3226 scb
= &adapter
->scb_list
[i
];
3230 scb
->sgl64
= pci_alloc_consistent(adapter
->dev
,
3231 sizeof(mega_sgl64
) * adapter
->sglen
,
3232 &scb
->sgl_dma_addr
);
3234 scb
->sgl
= (mega_sglist
*)scb
->sgl64
;
3237 printk(KERN_WARNING
"RAID: Can't allocate sglist.\n");
3238 mega_free_sgl(adapter
);
3242 scb
->pthru
= pci_alloc_consistent(adapter
->dev
,
3243 sizeof(mega_passthru
),
3244 &scb
->pthru_dma_addr
);
3247 printk(KERN_WARNING
"RAID: Can't allocate passthru.\n");
3248 mega_free_sgl(adapter
);
3252 scb
->epthru
= pci_alloc_consistent(adapter
->dev
,
3253 sizeof(mega_ext_passthru
),
3254 &scb
->epthru_dma_addr
);
3256 if( !scb
->epthru
) {
3258 "Can't allocate extended passthru.\n");
3259 mega_free_sgl(adapter
);
3264 scb
->dma_type
= MEGA_DMA_TYPE_NONE
;
3268 * lock not required since we are loading the driver, so no
3269 * commands possible right now.
3271 scb
->state
= SCB_FREE
;
3273 list_add(&scb
->list
, &adapter
->free_list
);
3285 * Routines for the character/ioctl interface to the driver. Find out if this
3289 megadev_open (struct inode
*inode
, struct file
*filep
)
3292 * Only allow superuser to access private ioctl interface
3294 if( !capable(CAP_SYS_ADMIN
) ) return -EACCES
;
3302 * @inode - Our device inode
3304 * @cmd - ioctl command
3305 * @arg - user buffer
3307 * ioctl entry point for our private ioctl interface. We move the data in from
3308 * the user space, prepare the command (if necessary, convert the old MIMD
3309 * ioctl to new ioctl command), and issue a synchronous command to the
3313 megadev_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
3319 mega_passthru __user
*upthru
; /* user address for passthru */
3320 mega_passthru
*pthru
; /* copy user passthru here */
3321 dma_addr_t pthru_dma_hndl
;
3322 void *data
= NULL
; /* data to be transferred */
3323 dma_addr_t data_dma_hndl
; /* dma handle for data xfer area */
3325 megastat_t __user
*ustats
;
3328 struct pci_dev
*pdev
;
3330 ustats
= NULL
; /* avoid compilation warnings */
3334 * Make sure only USCSICMD are issued through this interface.
3335 * MIMD application would still fire different command.
3337 if( (_IOC_TYPE(cmd
) != MEGAIOC_MAGIC
) && (cmd
!= USCSICMD
) ) {
3342 * Check and convert a possible MIMD command to NIT command.
3343 * mega_m_to_n() copies the data from the user space, so we do not
3344 * have to do it here.
3345 * NOTE: We will need some user address to copyout the data, therefore
3346 * the inteface layer will also provide us with the required user
3349 memset(&uioc
, 0, sizeof(nitioctl_t
));
3350 if( (rval
= mega_m_to_n( (void __user
*)arg
, &uioc
)) != 0 )
3354 switch( uioc
.opcode
) {
3356 case GET_DRIVER_VER
:
3357 if( put_user(driver_ver
, (u32 __user
*)uioc
.uioc_uaddr
) )
3363 if( put_user(hba_count
, (u32 __user
*)uioc
.uioc_uaddr
) )
3367 * Shucks. MIMD interface returns a positive value for number
3368 * of adapters. TODO: Change it to return 0 when there is no
3369 * applicatio using mimd interface.
3378 if( (adapno
= GETADAP(uioc
.adapno
)) >= hba_count
)
3381 if( copy_to_user(uioc
.uioc_uaddr
, mcontroller
+adapno
,
3382 sizeof(struct mcontroller
)) )
3392 if( (adapno
= GETADAP(uioc
.adapno
)) >= hba_count
)
3395 adapter
= hba_soft_state
[adapno
];
3397 ustats
= uioc
.uioc_uaddr
;
3399 if( copy_from_user(&num_ldrv
, &ustats
->num_ldrv
, sizeof(int)) )
3403 * Check for the validity of the logical drive number
3405 if( num_ldrv
>= MAX_LOGICAL_DRIVES_40LD
) return -EINVAL
;
3407 if( copy_to_user(ustats
->nreads
, adapter
->nreads
,
3408 num_ldrv
*sizeof(u32
)) )
3411 if( copy_to_user(ustats
->nreadblocks
, adapter
->nreadblocks
,
3412 num_ldrv
*sizeof(u32
)) )
3415 if( copy_to_user(ustats
->nwrites
, adapter
->nwrites
,
3416 num_ldrv
*sizeof(u32
)) )
3419 if( copy_to_user(ustats
->nwriteblocks
, adapter
->nwriteblocks
,
3420 num_ldrv
*sizeof(u32
)) )
3423 if( copy_to_user(ustats
->rd_errors
, adapter
->rd_errors
,
3424 num_ldrv
*sizeof(u32
)) )
3427 if( copy_to_user(ustats
->wr_errors
, adapter
->wr_errors
,
3428 num_ldrv
*sizeof(u32
)) )
3439 if( (adapno
= GETADAP(uioc
.adapno
)) >= hba_count
)
3442 adapter
= hba_soft_state
[adapno
];
3445 * Deletion of logical drive is a special case. The adapter
3446 * should be quiescent before this command is issued.
3448 if( uioc
.uioc_rmbox
[0] == FC_DEL_LOGDRV
&&
3449 uioc
.uioc_rmbox
[2] == OP_DEL_LOGDRV
) {
3452 * Do we support this feature
3454 if( !adapter
->support_random_del
) {
3455 printk(KERN_WARNING
"megaraid: logdrv ");
3456 printk("delete on non-supporting F/W.\n");
3461 rval
= mega_del_logdrv( adapter
, uioc
.uioc_rmbox
[3] );
3464 memset(&mc
, 0, sizeof(megacmd_t
));
3468 rval
= mega_n_to_m((void __user
*)arg
, &mc
);
3474 * This interface only support the regular passthru commands.
3475 * Reject extended passthru and 64-bit passthru
3477 if( uioc
.uioc_rmbox
[0] == MEGA_MBOXCMD_PASSTHRU64
||
3478 uioc
.uioc_rmbox
[0] == MEGA_MBOXCMD_EXTPTHRU
) {
3480 printk(KERN_WARNING
"megaraid: rejected passthru.\n");
3486 * For all internal commands, the buffer must be allocated in
3487 * <4GB address range
3489 if( make_local_pdev(adapter
, &pdev
) != 0 )
3492 /* Is it a passthru command or a DCMD */
3493 if( uioc
.uioc_rmbox
[0] == MEGA_MBOXCMD_PASSTHRU
) {
3494 /* Passthru commands */
3496 pthru
= pci_alloc_consistent(pdev
,
3497 sizeof(mega_passthru
),
3500 if( pthru
== NULL
) {
3501 free_local_pdev(pdev
);
3506 * The user passthru structure
3508 upthru
= (mega_passthru __user
*)(unsigned long)MBOX(uioc
)->xferaddr
;
3511 * Copy in the user passthru here.
3513 if( copy_from_user(pthru
, upthru
,
3514 sizeof(mega_passthru
)) ) {
3516 pci_free_consistent(pdev
,
3517 sizeof(mega_passthru
), pthru
,
3520 free_local_pdev(pdev
);
3526 * Is there a data transfer
3528 if( pthru
->dataxferlen
) {
3529 data
= pci_alloc_consistent(pdev
,
3533 if( data
== NULL
) {
3534 pci_free_consistent(pdev
,
3535 sizeof(mega_passthru
),
3539 free_local_pdev(pdev
);
3545 * Save the user address and point the kernel
3546 * address at just allocated memory
3548 uxferaddr
= pthru
->dataxferaddr
;
3549 pthru
->dataxferaddr
= data_dma_hndl
;
3554 * Is data coming down-stream
3556 if( pthru
->dataxferlen
&& (uioc
.flags
& UIOC_WR
) ) {
3560 if( copy_from_user(data
, (char __user
*)(unsigned long) uxferaddr
,
3561 pthru
->dataxferlen
) ) {
3563 goto freemem_and_return
;
3567 memset(&mc
, 0, sizeof(megacmd_t
));
3569 mc
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
3570 mc
.xferaddr
= (u32
)pthru_dma_hndl
;
3575 mega_internal_command(adapter
, &mc
, pthru
);
3577 rval
= mega_n_to_m((void __user
*)arg
, &mc
);
3579 if( rval
) goto freemem_and_return
;
3583 * Is data going up-stream
3585 if( pthru
->dataxferlen
&& (uioc
.flags
& UIOC_RD
) ) {
3586 if( copy_to_user((char __user
*)(unsigned long) uxferaddr
, data
,
3587 pthru
->dataxferlen
) ) {
3593 * Send the request sense data also, irrespective of
3594 * whether the user has asked for it or not.
3596 if (copy_to_user(upthru
->reqsensearea
,
3597 pthru
->reqsensearea
, 14))
3601 if( pthru
->dataxferlen
) {
3602 pci_free_consistent(pdev
,
3603 pthru
->dataxferlen
, data
,
3607 pci_free_consistent(pdev
, sizeof(mega_passthru
),
3608 pthru
, pthru_dma_hndl
);
3610 free_local_pdev(pdev
);
3618 * Is there a data transfer
3620 if( uioc
.xferlen
) {
3621 data
= pci_alloc_consistent(pdev
,
3622 uioc
.xferlen
, &data_dma_hndl
);
3624 if( data
== NULL
) {
3625 free_local_pdev(pdev
);
3629 uxferaddr
= MBOX(uioc
)->xferaddr
;
3633 * Is data coming down-stream
3635 if( uioc
.xferlen
&& (uioc
.flags
& UIOC_WR
) ) {
3639 if( copy_from_user(data
, (char __user
*)(unsigned long) uxferaddr
,
3642 pci_free_consistent(pdev
,
3644 data
, data_dma_hndl
);
3646 free_local_pdev(pdev
);
3652 memcpy(&mc
, MBOX(uioc
), sizeof(megacmd_t
));
3654 mc
.xferaddr
= (u32
)data_dma_hndl
;
3659 mega_internal_command(adapter
, &mc
, NULL
);
3661 rval
= mega_n_to_m((void __user
*)arg
, &mc
);
3664 if( uioc
.xferlen
) {
3665 pci_free_consistent(pdev
,
3670 free_local_pdev(pdev
);
3676 * Is data going up-stream
3678 if( uioc
.xferlen
&& (uioc
.flags
& UIOC_RD
) ) {
3679 if( copy_to_user((char __user
*)(unsigned long) uxferaddr
, data
,
3686 if( uioc
.xferlen
) {
3687 pci_free_consistent(pdev
,
3692 free_local_pdev(pdev
);
3705 megadev_unlocked_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
3709 mutex_lock(&megadev_mutex
);
3710 ret
= megadev_ioctl(filep
, cmd
, arg
);
3711 mutex_unlock(&megadev_mutex
);
3718 * @arg - user address
3719 * @uioc - new ioctl structure
3721 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3724 * Converts the older mimd ioctl structure to newer NIT structure
3727 mega_m_to_n(void __user
*arg
, nitioctl_t
*uioc
)
3729 struct uioctl_t uioc_mimd
;
3730 char signature
[8] = {0};
3736 * check is the application conforms to NIT. We do not have to do much
3738 * We exploit the fact that the signature is stored in the very
3739 * beginning of the structure.
3742 if( copy_from_user(signature
, arg
, 7) )
3745 if( memcmp(signature
, "MEGANIT", 7) == 0 ) {
3748 * NOTE NOTE: The nit ioctl is still under flux because of
3749 * change of mailbox definition, in HPE. No applications yet
3750 * use this interface and let's not have applications use this
3751 * interface till the new specifitions are in place.
3755 if( copy_from_user(uioc
, arg
, sizeof(nitioctl_t
)) )
3762 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3764 * Get the user ioctl structure
3766 if( copy_from_user(&uioc_mimd
, arg
, sizeof(struct uioctl_t
)) )
3771 * Get the opcode and subopcode for the commands
3773 opcode
= uioc_mimd
.ui
.fcs
.opcode
;
3774 subopcode
= uioc_mimd
.ui
.fcs
.subopcode
;
3779 switch (subopcode
) {
3781 case MEGAIOC_QDRVRVER
: /* Query driver version */
3782 uioc
->opcode
= GET_DRIVER_VER
;
3783 uioc
->uioc_uaddr
= uioc_mimd
.data
;
3786 case MEGAIOC_QNADAP
: /* Get # of adapters */
3787 uioc
->opcode
= GET_N_ADAP
;
3788 uioc
->uioc_uaddr
= uioc_mimd
.data
;
3791 case MEGAIOC_QADAPINFO
: /* Get adapter information */
3792 uioc
->opcode
= GET_ADAP_INFO
;
3793 uioc
->adapno
= uioc_mimd
.ui
.fcs
.adapno
;
3794 uioc
->uioc_uaddr
= uioc_mimd
.data
;
3806 uioc
->opcode
= MBOX_CMD
;
3807 uioc
->adapno
= uioc_mimd
.ui
.fcs
.adapno
;
3809 memcpy(uioc
->uioc_rmbox
, uioc_mimd
.mbox
, 18);
3811 uioc
->xferlen
= uioc_mimd
.ui
.fcs
.length
;
3813 if( uioc_mimd
.outlen
) uioc
->flags
= UIOC_RD
;
3814 if( uioc_mimd
.inlen
) uioc
->flags
|= UIOC_WR
;
3820 uioc
->opcode
= MBOX_CMD
;
3821 uioc
->adapno
= uioc_mimd
.ui
.fcs
.adapno
;
3823 memcpy(uioc
->uioc_rmbox
, uioc_mimd
.mbox
, 18);
3826 * Choose the xferlen bigger of input and output data
3828 uioc
->xferlen
= uioc_mimd
.outlen
> uioc_mimd
.inlen
?
3829 uioc_mimd
.outlen
: uioc_mimd
.inlen
;
3831 if( uioc_mimd
.outlen
) uioc
->flags
= UIOC_RD
;
3832 if( uioc_mimd
.inlen
) uioc
->flags
|= UIOC_WR
;
3846 * @arg - user address
3847 * @mc - mailbox command
3849 * Updates the status information to the application, depending on application
3850 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3853 mega_n_to_m(void __user
*arg
, megacmd_t
*mc
)
3855 nitioctl_t __user
*uiocp
;
3856 megacmd_t __user
*umc
;
3857 mega_passthru __user
*upthru
;
3858 struct uioctl_t __user
*uioc_mimd
;
3859 char signature
[8] = {0};
3862 * check is the application conforms to NIT.
3864 if( copy_from_user(signature
, arg
, 7) )
3867 if( memcmp(signature
, "MEGANIT", 7) == 0 ) {
3871 if( put_user(mc
->status
, (u8 __user
*)&MBOX_P(uiocp
)->status
) )
3874 if( mc
->cmd
== MEGA_MBOXCMD_PASSTHRU
) {
3876 umc
= MBOX_P(uiocp
);
3878 if (get_user(upthru
, (mega_passthru __user
* __user
*)&umc
->xferaddr
))
3881 if( put_user(mc
->status
, (u8 __user
*)&upthru
->scsistatus
))
3888 if( put_user(mc
->status
, (u8 __user
*)&uioc_mimd
->mbox
[17]) )
3891 if( mc
->cmd
== MEGA_MBOXCMD_PASSTHRU
) {
3893 umc
= (megacmd_t __user
*)uioc_mimd
->mbox
;
3895 if (get_user(upthru
, (mega_passthru __user
* __user
*)&umc
->xferaddr
))
3898 if( put_user(mc
->status
, (u8 __user
*)&upthru
->scsistatus
) )
3908 * MEGARAID 'FW' commands.
3912 * mega_is_bios_enabled()
3913 * @adapter - pointer to our soft state
3915 * issue command to find out if the BIOS is enabled for this controller
3918 mega_is_bios_enabled(adapter_t
*adapter
)
3920 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
3924 mbox
= (mbox_t
*)raw_mbox
;
3926 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
3928 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
3930 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
3932 raw_mbox
[0] = IS_BIOS_ENABLED
;
3933 raw_mbox
[2] = GET_BIOS
;
3936 ret
= issue_scb_block(adapter
, raw_mbox
);
3938 return *(char *)adapter
->mega_buffer
;
3943 * mega_enum_raid_scsi()
3944 * @adapter - pointer to our soft state
3946 * Find out what channels are RAID/SCSI. This information is used to
3947 * differentiate the virtual channels and physical channels and to support
3948 * ROMB feature and non-disk devices.
3951 mega_enum_raid_scsi(adapter_t
*adapter
)
3953 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
3957 mbox
= (mbox_t
*)raw_mbox
;
3959 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
3962 * issue command to find out what channels are raid/scsi
3964 raw_mbox
[0] = CHNL_CLASS
;
3965 raw_mbox
[2] = GET_CHNL_CLASS
;
3967 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
3969 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
3972 * Non-ROMB firmware fail this command, so all channels
3973 * must be shown RAID
3975 adapter
->mega_ch_class
= 0xFF;
3977 if(!issue_scb_block(adapter
, raw_mbox
)) {
3978 adapter
->mega_ch_class
= *((char *)adapter
->mega_buffer
);
3982 for( i
= 0; i
< adapter
->product_info
.nchannels
; i
++ ) {
3983 if( (adapter
->mega_ch_class
>> i
) & 0x01 ) {
3984 printk(KERN_INFO
"megaraid: channel[%d] is raid.\n",
3988 printk(KERN_INFO
"megaraid: channel[%d] is scsi.\n",
3998 * mega_get_boot_drv()
3999 * @adapter - pointer to our soft state
4001 * Find out which device is the boot device. Note, any logical drive or any
4002 * phyical device (e.g., a CDROM) can be designated as a boot device.
4005 mega_get_boot_drv(adapter_t
*adapter
)
4007 struct private_bios_data
*prv_bios_data
;
4008 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4015 mbox
= (mbox_t
*)raw_mbox
;
4017 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4019 raw_mbox
[0] = BIOS_PVT_DATA
;
4020 raw_mbox
[2] = GET_BIOS_PVT_DATA
;
4022 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
4024 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
4026 adapter
->boot_ldrv_enabled
= 0;
4027 adapter
->boot_ldrv
= 0;
4029 adapter
->boot_pdrv_enabled
= 0;
4030 adapter
->boot_pdrv_ch
= 0;
4031 adapter
->boot_pdrv_tgt
= 0;
4033 if(issue_scb_block(adapter
, raw_mbox
) == 0) {
4035 (struct private_bios_data
*)adapter
->mega_buffer
;
4038 cksum_p
= (char *)prv_bios_data
;
4039 for (i
= 0; i
< 14; i
++ ) {
4040 cksum
+= (u16
)(*cksum_p
++);
4043 if (prv_bios_data
->cksum
== (u16
)(0-cksum
) ) {
4046 * If MSB is set, a physical drive is set as boot
4049 if( prv_bios_data
->boot_drv
& 0x80 ) {
4050 adapter
->boot_pdrv_enabled
= 1;
4051 boot_pdrv
= prv_bios_data
->boot_drv
& 0x7F;
4052 adapter
->boot_pdrv_ch
= boot_pdrv
/ 16;
4053 adapter
->boot_pdrv_tgt
= boot_pdrv
% 16;
4056 adapter
->boot_ldrv_enabled
= 1;
4057 adapter
->boot_ldrv
= prv_bios_data
->boot_drv
;
4065 * mega_support_random_del()
4066 * @adapter - pointer to our soft state
4068 * Find out if this controller supports random deletion and addition of
4072 mega_support_random_del(adapter_t
*adapter
)
4074 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4078 mbox
= (mbox_t
*)raw_mbox
;
4080 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4085 raw_mbox
[0] = FC_DEL_LOGDRV
;
4086 raw_mbox
[2] = OP_SUP_DEL_LOGDRV
;
4088 rval
= issue_scb_block(adapter
, raw_mbox
);
4095 * mega_support_ext_cdb()
4096 * @adapter - pointer to our soft state
4098 * Find out if this firmware support cdblen > 10
4101 mega_support_ext_cdb(adapter_t
*adapter
)
4103 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4107 mbox
= (mbox_t
*)raw_mbox
;
4109 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4111 * issue command to find out if controller supports extended CDBs.
4116 rval
= issue_scb_block(adapter
, raw_mbox
);
4124 * @adapter - pointer to our soft state
4125 * @logdrv - logical drive to be deleted
4127 * Delete the specified logical drive. It is the responsibility of the user
4128 * app to let the OS know about this operation.
4131 mega_del_logdrv(adapter_t
*adapter
, int logdrv
)
4133 unsigned long flags
;
4138 * Stop sending commands to the controller, queue them internally.
4139 * When deletion is complete, ISR will flush the queue.
4141 atomic_set(&adapter
->quiescent
, 1);
4144 * Wait till all the issued commands are complete and there are no
4145 * commands in the pending queue
4147 while (atomic_read(&adapter
->pend_cmds
) > 0 ||
4148 !list_empty(&adapter
->pending_list
))
4149 msleep(1000); /* sleep for 1s */
4151 rval
= mega_do_del_logdrv(adapter
, logdrv
);
4153 spin_lock_irqsave(&adapter
->lock
, flags
);
4156 * If delete operation was successful, add 0x80 to the logical drive
4157 * ids for commands in the pending queue.
4159 if (adapter
->read_ldidmap
) {
4160 struct list_head
*pos
;
4161 list_for_each(pos
, &adapter
->pending_list
) {
4162 scb
= list_entry(pos
, scb_t
, list
);
4163 if (scb
->pthru
->logdrv
< 0x80 )
4164 scb
->pthru
->logdrv
+= 0x80;
4168 atomic_set(&adapter
->quiescent
, 0);
4170 mega_runpendq(adapter
);
4172 spin_unlock_irqrestore(&adapter
->lock
, flags
);
4179 mega_do_del_logdrv(adapter_t
*adapter
, int logdrv
)
4184 memset( &mc
, 0, sizeof(megacmd_t
));
4186 mc
.cmd
= FC_DEL_LOGDRV
;
4187 mc
.opcode
= OP_DEL_LOGDRV
;
4188 mc
.subopcode
= logdrv
;
4190 rval
= mega_internal_command(adapter
, &mc
, NULL
);
4192 /* log this event */
4194 printk(KERN_WARNING
"megaraid: Delete LD-%d failed.", logdrv
);
4199 * After deleting first logical drive, the logical drives must be
4200 * addressed by adding 0x80 to the logical drive id.
4202 adapter
->read_ldidmap
= 1;
4209 * mega_get_max_sgl()
4210 * @adapter - pointer to our soft state
4212 * Find out the maximum number of scatter-gather elements supported by this
4213 * version of the firmware
4216 mega_get_max_sgl(adapter_t
*adapter
)
4218 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4221 mbox
= (mbox_t
*)raw_mbox
;
4223 memset(mbox
, 0, sizeof(raw_mbox
));
4225 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
4227 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
4229 raw_mbox
[0] = MAIN_MISC_OPCODE
;
4230 raw_mbox
[2] = GET_MAX_SG_SUPPORT
;
4233 if( issue_scb_block(adapter
, raw_mbox
) ) {
4235 * f/w does not support this command. Choose the default value
4237 adapter
->sglen
= MIN_SGLIST
;
4240 adapter
->sglen
= *((char *)adapter
->mega_buffer
);
4243 * Make sure this is not more than the resources we are
4244 * planning to allocate
4246 if ( adapter
->sglen
> MAX_SGLIST
)
4247 adapter
->sglen
= MAX_SGLIST
;
4255 * mega_support_cluster()
4256 * @adapter - pointer to our soft state
4258 * Find out if this firmware support cluster calls.
4261 mega_support_cluster(adapter_t
*adapter
)
4263 unsigned char raw_mbox
[sizeof(struct mbox_out
)];
4266 mbox
= (mbox_t
*)raw_mbox
;
4268 memset(mbox
, 0, sizeof(raw_mbox
));
4270 memset((void *)adapter
->mega_buffer
, 0, MEGA_BUFFER_SIZE
);
4272 mbox
->m_out
.xferaddr
= (u32
)adapter
->buf_dma_handle
;
4275 * Try to get the initiator id. This command will succeed iff the
4276 * clustering is available on this HBA.
4278 raw_mbox
[0] = MEGA_GET_TARGET_ID
;
4280 if( issue_scb_block(adapter
, raw_mbox
) == 0 ) {
4283 * Cluster support available. Get the initiator target id.
4284 * Tell our id to mid-layer too.
4286 adapter
->this_id
= *(u32
*)adapter
->mega_buffer
;
4287 adapter
->host
->this_id
= adapter
->this_id
;
4295 #ifdef CONFIG_PROC_FS
4298 * @adapter - pointer to our soft state
4299 * @dma_handle - DMA address of the buffer
4301 * Issue internal commands while interrupts are available.
4302 * We only issue direct mailbox commands from within the driver. ioctl()
4303 * interface using these routines can issue passthru commands.
4306 mega_adapinq(adapter_t
*adapter
, dma_addr_t dma_handle
)
4310 memset(&mc
, 0, sizeof(megacmd_t
));
4312 if( adapter
->flag
& BOARD_40LD
) {
4313 mc
.cmd
= FC_NEW_CONFIG
;
4314 mc
.opcode
= NC_SUBOP_ENQUIRY3
;
4315 mc
.subopcode
= ENQ3_GET_SOLICITED_FULL
;
4318 mc
.cmd
= MEGA_MBOXCMD_ADPEXTINQ
;
4321 mc
.xferaddr
= (u32
)dma_handle
;
4323 if ( mega_internal_command(adapter
, &mc
, NULL
) != 0 ) {
4331 /** mega_internal_dev_inquiry()
4332 * @adapter - pointer to our soft state
4333 * @ch - channel for this device
4334 * @tgt - ID of this device
4335 * @buf_dma_handle - DMA address of the buffer
4337 * Issue the scsi inquiry for the specified device.
4340 mega_internal_dev_inquiry(adapter_t
*adapter
, u8 ch
, u8 tgt
,
4341 dma_addr_t buf_dma_handle
)
4343 mega_passthru
*pthru
;
4344 dma_addr_t pthru_dma_handle
;
4347 struct pci_dev
*pdev
;
4351 * For all internal commands, the buffer must be allocated in <4GB
4354 if( make_local_pdev(adapter
, &pdev
) != 0 ) return -1;
4356 pthru
= pci_alloc_consistent(pdev
, sizeof(mega_passthru
),
4359 if( pthru
== NULL
) {
4360 free_local_pdev(pdev
);
4366 pthru
->reqsenselen
= 14;
4367 pthru
->islogical
= 0;
4369 pthru
->channel
= (adapter
->flag
& BOARD_40LD
) ? 0 : ch
;
4371 pthru
->target
= (adapter
->flag
& BOARD_40LD
) ? (ch
<< 4)|tgt
: tgt
;
4375 pthru
->cdb
[0] = INQUIRY
;
4379 pthru
->cdb
[4] = 255;
4383 pthru
->dataxferaddr
= (u32
)buf_dma_handle
;
4384 pthru
->dataxferlen
= 256;
4386 memset(&mc
, 0, sizeof(megacmd_t
));
4388 mc
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
4389 mc
.xferaddr
= (u32
)pthru_dma_handle
;
4391 rval
= mega_internal_command(adapter
, &mc
, pthru
);
4393 pci_free_consistent(pdev
, sizeof(mega_passthru
), pthru
,
4396 free_local_pdev(pdev
);
4403 * mega_internal_command()
4404 * @adapter - pointer to our soft state
4405 * @mc - the mailbox command
4406 * @pthru - Passthru structure for DCDB commands
4408 * Issue the internal commands in interrupt mode.
4409 * The last argument is the address of the passthru structure if the command
4410 * to be fired is a passthru command
4412 * lockscope specifies whether the caller has already acquired the lock. Of
4413 * course, the caller must know which lock we are talking about.
4415 * Note: parameter 'pthru' is null for non-passthru commands.
4418 mega_internal_command(adapter_t
*adapter
, megacmd_t
*mc
, mega_passthru
*pthru
)
4421 struct scsi_device
*sdev
;
4425 scmd
= scsi_allocate_command(GFP_KERNEL
);
4430 * The internal commands share one command id and hence are
4431 * serialized. This is so because we want to reserve maximum number of
4432 * available command ids for the I/O commands.
4434 mutex_lock(&adapter
->int_mtx
);
4436 scb
= &adapter
->int_scb
;
4437 memset(scb
, 0, sizeof(scb_t
));
4439 sdev
= kzalloc(sizeof(struct scsi_device
), GFP_KERNEL
);
4440 scmd
->device
= sdev
;
4442 memset(adapter
->int_cdb
, 0, sizeof(adapter
->int_cdb
));
4443 scmd
->cmnd
= adapter
->int_cdb
;
4444 scmd
->device
->host
= adapter
->host
;
4445 scmd
->host_scribble
= (void *)scb
;
4446 scmd
->cmnd
[0] = MEGA_INTERNAL_CMD
;
4448 scb
->state
|= SCB_ACTIVE
;
4451 memcpy(scb
->raw_mbox
, mc
, sizeof(megacmd_t
));
4454 * Is it a passthru command
4456 if( mc
->cmd
== MEGA_MBOXCMD_PASSTHRU
) {
4461 scb
->idx
= CMDID_INT_CMDS
;
4463 megaraid_queue_lck(scmd
, mega_internal_done
);
4465 wait_for_completion(&adapter
->int_waitq
);
4467 rval
= scmd
->result
;
4468 mc
->status
= scmd
->result
;
4472 * Print a debug message for all failed commands. Applications can use
4475 if( scmd
->result
&& trace_level
) {
4476 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4477 mc
->cmd
, mc
->opcode
, mc
->subopcode
, scmd
->result
);
4480 mutex_unlock(&adapter
->int_mtx
);
4482 scsi_free_command(GFP_KERNEL
, scmd
);
4489 * mega_internal_done()
4490 * @scmd - internal scsi command
4492 * Callback routine for internal commands.
4495 mega_internal_done(Scsi_Cmnd
*scmd
)
4499 adapter
= (adapter_t
*)scmd
->device
->host
->hostdata
;
4501 complete(&adapter
->int_waitq
);
4506 static struct scsi_host_template megaraid_template
= {
4507 .module
= THIS_MODULE
,
4509 .proc_name
= "megaraid_legacy",
4510 .info
= megaraid_info
,
4511 .queuecommand
= megaraid_queue
,
4512 .bios_param
= megaraid_biosparam
,
4513 .max_sectors
= MAX_SECTORS_PER_IO
,
4514 .can_queue
= MAX_COMMANDS
,
4515 .this_id
= DEFAULT_INITIATOR_ID
,
4516 .sg_tablesize
= MAX_SGLIST
,
4517 .cmd_per_lun
= DEF_CMD_PER_LUN
,
4518 .use_clustering
= ENABLE_CLUSTERING
,
4519 .eh_abort_handler
= megaraid_abort
,
4520 .eh_device_reset_handler
= megaraid_reset
,
4521 .eh_bus_reset_handler
= megaraid_reset
,
4522 .eh_host_reset_handler
= megaraid_reset
,
4525 static int __devinit
4526 megaraid_probe_one(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
4528 struct Scsi_Host
*host
;
4530 unsigned long mega_baseport
, tbase
, flag
= 0;
4531 u16 subsysid
, subsysvid
;
4532 u8 pci_bus
, pci_dev_func
;
4534 int error
= -ENODEV
;
4536 if (pci_enable_device(pdev
))
4538 pci_set_master(pdev
);
4540 pci_bus
= pdev
->bus
->number
;
4541 pci_dev_func
= pdev
->devfn
;
4544 * The megaraid3 stuff reports the ID of the Intel part which is not
4545 * remotely specific to the megaraid
4547 if (pdev
->vendor
== PCI_VENDOR_ID_INTEL
) {
4550 * Don't fall over the Compaq management cards using the same
4553 if (pdev
->subsystem_vendor
== PCI_VENDOR_ID_COMPAQ
&&
4554 pdev
->subsystem_device
== 0xC000)
4556 /* Now check the magic signature byte */
4557 pci_read_config_word(pdev
, PCI_CONF_AMISIG
, &magic
);
4558 if (magic
!= HBA_SIGNATURE_471
&& magic
!= HBA_SIGNATURE
)
4560 /* Ok it is probably a megaraid */
4564 * For these vendor and device ids, signature offsets are not
4565 * valid and 64 bit is implicit
4567 if (id
->driver_data
& BOARD_64BIT
)
4568 flag
|= BOARD_64BIT
;
4572 pci_read_config_dword(pdev
, PCI_CONF_AMISIG64
, &magic64
);
4573 if (magic64
== HBA_SIGNATURE_64BIT
)
4574 flag
|= BOARD_64BIT
;
4577 subsysvid
= pdev
->subsystem_vendor
;
4578 subsysid
= pdev
->subsystem_device
;
4580 printk(KERN_NOTICE
"megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4581 id
->vendor
, id
->device
, pci_bus
);
4583 printk("slot %d:func %d\n",
4584 PCI_SLOT(pci_dev_func
), PCI_FUNC(pci_dev_func
));
4586 /* Read the base port and IRQ from PCI */
4587 mega_baseport
= pci_resource_start(pdev
, 0);
4590 tbase
= mega_baseport
;
4591 if (pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
) {
4592 flag
|= BOARD_MEMMAP
;
4594 if (!request_mem_region(mega_baseport
, 128, "megaraid")) {
4595 printk(KERN_WARNING
"megaraid: mem region busy!\n");
4596 goto out_disable_device
;
4599 mega_baseport
= (unsigned long)ioremap(mega_baseport
, 128);
4600 if (!mega_baseport
) {
4602 "megaraid: could not map hba memory\n");
4603 goto out_release_region
;
4606 flag
|= BOARD_IOMAP
;
4607 mega_baseport
+= 0x10;
4609 if (!request_region(mega_baseport
, 16, "megaraid"))
4610 goto out_disable_device
;
4613 /* Initialize SCSI Host structure */
4614 host
= scsi_host_alloc(&megaraid_template
, sizeof(adapter_t
));
4618 adapter
= (adapter_t
*)host
->hostdata
;
4619 memset(adapter
, 0, sizeof(adapter_t
));
4622 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4623 host
->host_no
, mega_baseport
, irq
);
4625 adapter
->base
= mega_baseport
;
4626 if (flag
& BOARD_MEMMAP
)
4627 adapter
->mmio_base
= (void __iomem
*) mega_baseport
;
4629 INIT_LIST_HEAD(&adapter
->free_list
);
4630 INIT_LIST_HEAD(&adapter
->pending_list
);
4631 INIT_LIST_HEAD(&adapter
->completed_list
);
4633 adapter
->flag
= flag
;
4634 spin_lock_init(&adapter
->lock
);
4636 host
->cmd_per_lun
= max_cmd_per_lun
;
4637 host
->max_sectors
= max_sectors_per_io
;
4639 adapter
->dev
= pdev
;
4640 adapter
->host
= host
;
4642 adapter
->host
->irq
= irq
;
4644 if (flag
& BOARD_MEMMAP
)
4645 adapter
->host
->base
= tbase
;
4647 adapter
->host
->io_port
= tbase
;
4648 adapter
->host
->n_io_port
= 16;
4651 adapter
->host
->unique_id
= (pci_bus
<< 8) | pci_dev_func
;
4654 * Allocate buffer to issue internal commands.
4656 adapter
->mega_buffer
= pci_alloc_consistent(adapter
->dev
,
4657 MEGA_BUFFER_SIZE
, &adapter
->buf_dma_handle
);
4658 if (!adapter
->mega_buffer
) {
4659 printk(KERN_WARNING
"megaraid: out of RAM.\n");
4663 adapter
->scb_list
= kmalloc(sizeof(scb_t
) * MAX_COMMANDS
, GFP_KERNEL
);
4664 if (!adapter
->scb_list
) {
4665 printk(KERN_WARNING
"megaraid: out of RAM.\n");
4666 goto out_free_cmd_buffer
;
4669 if (request_irq(irq
, (adapter
->flag
& BOARD_MEMMAP
) ?
4670 megaraid_isr_memmapped
: megaraid_isr_iomapped
,
4671 IRQF_SHARED
, "megaraid", adapter
)) {
4673 "megaraid: Couldn't register IRQ %d!\n", irq
);
4674 goto out_free_scb_list
;
4677 if (mega_setup_mailbox(adapter
))
4680 if (mega_query_adapter(adapter
))
4684 * Have checks for some buggy f/w
4686 if ((subsysid
== 0x1111) && (subsysvid
== 0x1111)) {
4690 if (!strcmp(adapter
->fw_version
, "3.00") ||
4691 !strcmp(adapter
->fw_version
, "3.01")) {
4693 printk( KERN_WARNING
4694 "megaraid: Your card is a Dell PERC "
4695 "2/SC RAID controller with "
4696 "firmware\nmegaraid: 3.00 or 3.01. "
4697 "This driver is known to have "
4698 "corruption issues\nmegaraid: with "
4699 "those firmware versions on this "
4700 "specific card. In order\nmegaraid: "
4701 "to protect your data, please upgrade "
4702 "your firmware to version\nmegaraid: "
4703 "3.10 or later, available from the "
4704 "Dell Technical Support web\n"
4705 "megaraid: site at\nhttp://support."
4706 "dell.com/us/en/filelib/download/"
4707 "index.asp?fileid=2940\n"
4713 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4714 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4715 * support, since this firmware cannot handle 64 bit
4718 if ((subsysvid
== PCI_VENDOR_ID_HP
) &&
4719 ((subsysid
== 0x60E7) || (subsysid
== 0x60E8))) {
4723 if (!strcmp(adapter
->fw_version
, "H01.07") ||
4724 !strcmp(adapter
->fw_version
, "H01.08") ||
4725 !strcmp(adapter
->fw_version
, "H01.09") ) {
4727 "megaraid: Firmware H.01.07, "
4728 "H.01.08, and H.01.09 on 1M/2M "
4730 "megaraid: do not support 64 bit "
4731 "addressing.\nmegaraid: DISABLING "
4732 "64 bit support.\n");
4733 adapter
->flag
&= ~BOARD_64BIT
;
4737 if (mega_is_bios_enabled(adapter
))
4738 mega_hbas
[hba_count
].is_bios_enabled
= 1;
4739 mega_hbas
[hba_count
].hostdata_addr
= adapter
;
4742 * Find out which channel is raid and which is scsi. This is
4745 mega_enum_raid_scsi(adapter
);
4748 * Find out if a logical drive is set as the boot drive. If
4749 * there is one, will make that as the first logical drive.
4750 * ROMB: Do we have to boot from a physical drive. Then all
4751 * the physical drives would appear before the logical disks.
4752 * Else, all the physical drives would be exported to the mid
4753 * layer after logical drives.
4755 mega_get_boot_drv(adapter
);
4757 if (adapter
->boot_pdrv_enabled
) {
4758 j
= adapter
->product_info
.nchannels
;
4759 for( i
= 0; i
< j
; i
++ )
4760 adapter
->logdrv_chan
[i
] = 0;
4761 for( i
= j
; i
< NVIRT_CHAN
+ j
; i
++ )
4762 adapter
->logdrv_chan
[i
] = 1;
4764 for (i
= 0; i
< NVIRT_CHAN
; i
++)
4765 adapter
->logdrv_chan
[i
] = 1;
4766 for (i
= NVIRT_CHAN
; i
< MAX_CHANNELS
+NVIRT_CHAN
; i
++)
4767 adapter
->logdrv_chan
[i
] = 0;
4768 adapter
->mega_ch_class
<<= NVIRT_CHAN
;
4772 * Do we support random deletion and addition of logical
4775 adapter
->read_ldidmap
= 0; /* set it after first logdrv
4777 adapter
->support_random_del
= mega_support_random_del(adapter
);
4779 /* Initialize SCBs */
4780 if (mega_init_scb(adapter
))
4784 * Reset the pending commands counter
4786 atomic_set(&adapter
->pend_cmds
, 0);
4789 * Reset the adapter quiescent flag
4791 atomic_set(&adapter
->quiescent
, 0);
4793 hba_soft_state
[hba_count
] = adapter
;
4796 * Fill in the structure which needs to be passed back to the
4797 * application when it does an ioctl() for controller related
4802 mcontroller
[i
].base
= mega_baseport
;
4803 mcontroller
[i
].irq
= irq
;
4804 mcontroller
[i
].numldrv
= adapter
->numldrv
;
4805 mcontroller
[i
].pcibus
= pci_bus
;
4806 mcontroller
[i
].pcidev
= id
->device
;
4807 mcontroller
[i
].pcifun
= PCI_FUNC (pci_dev_func
);
4808 mcontroller
[i
].pciid
= -1;
4809 mcontroller
[i
].pcivendor
= id
->vendor
;
4810 mcontroller
[i
].pcislot
= PCI_SLOT(pci_dev_func
);
4811 mcontroller
[i
].uid
= (pci_bus
<< 8) | pci_dev_func
;
4814 /* Set the Mode of addressing to 64 bit if we can */
4815 if ((adapter
->flag
& BOARD_64BIT
) && (sizeof(dma_addr_t
) == 8)) {
4816 pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
4817 adapter
->has_64bit_addr
= 1;
4819 pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
4820 adapter
->has_64bit_addr
= 0;
4823 mutex_init(&adapter
->int_mtx
);
4824 init_completion(&adapter
->int_waitq
);
4826 adapter
->this_id
= DEFAULT_INITIATOR_ID
;
4827 adapter
->host
->this_id
= DEFAULT_INITIATOR_ID
;
4829 #if MEGA_HAVE_CLUSTERING
4831 * Is cluster support enabled on this controller
4832 * Note: In a cluster the HBAs ( the initiators ) will have
4833 * different target IDs and we cannot assume it to be 7. Call
4834 * to mega_support_cluster() will get the target ids also if
4835 * the cluster support is available
4837 adapter
->has_cluster
= mega_support_cluster(adapter
);
4838 if (adapter
->has_cluster
) {
4840 "megaraid: Cluster driver, initiator id:%d\n",
4845 pci_set_drvdata(pdev
, host
);
4847 mega_create_proc_entry(hba_count
, mega_proc_dir_entry
);
4849 error
= scsi_add_host(host
, &pdev
->dev
);
4853 scsi_scan_host(host
);
4858 pci_free_consistent(adapter
->dev
, sizeof(mbox64_t
),
4859 adapter
->una_mbox64
, adapter
->una_mbox64_dma
);
4861 free_irq(adapter
->host
->irq
, adapter
);
4863 kfree(adapter
->scb_list
);
4864 out_free_cmd_buffer
:
4865 pci_free_consistent(adapter
->dev
, MEGA_BUFFER_SIZE
,
4866 adapter
->mega_buffer
, adapter
->buf_dma_handle
);
4868 scsi_host_put(host
);
4870 if (flag
& BOARD_MEMMAP
)
4871 iounmap((void *)mega_baseport
);
4873 if (flag
& BOARD_MEMMAP
)
4874 release_mem_region(tbase
, 128);
4876 release_region(mega_baseport
, 16);
4878 pci_disable_device(pdev
);
4884 __megaraid_shutdown(adapter_t
*adapter
)
4886 u_char raw_mbox
[sizeof(struct mbox_out
)];
4887 mbox_t
*mbox
= (mbox_t
*)raw_mbox
;
4890 /* Flush adapter cache */
4891 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4892 raw_mbox
[0] = FLUSH_ADAPTER
;
4894 free_irq(adapter
->host
->irq
, adapter
);
4896 /* Issue a blocking (interrupts disabled) command to the card */
4897 issue_scb_block(adapter
, raw_mbox
);
4899 /* Flush disks cache */
4900 memset(&mbox
->m_out
, 0, sizeof(raw_mbox
));
4901 raw_mbox
[0] = FLUSH_SYSTEM
;
4903 /* Issue a blocking (interrupts disabled) command to the card */
4904 issue_scb_block(adapter
, raw_mbox
);
4906 if (atomic_read(&adapter
->pend_cmds
) > 0)
4907 printk(KERN_WARNING
"megaraid: pending commands!!\n");
4910 * Have a delibrate delay to make sure all the caches are
4913 for (i
= 0; i
<= 10; i
++)
4917 static void __devexit
4918 megaraid_remove_one(struct pci_dev
*pdev
)
4920 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
4921 adapter_t
*adapter
= (adapter_t
*)host
->hostdata
;
4923 scsi_remove_host(host
);
4925 __megaraid_shutdown(adapter
);
4927 /* Free our resources */
4928 if (adapter
->flag
& BOARD_MEMMAP
) {
4929 iounmap((void *)adapter
->base
);
4930 release_mem_region(adapter
->host
->base
, 128);
4932 release_region(adapter
->base
, 16);
4934 mega_free_sgl(adapter
);
4936 #ifdef CONFIG_PROC_FS
4937 if (adapter
->controller_proc_dir_entry
) {
4938 remove_proc_entry("stat", adapter
->controller_proc_dir_entry
);
4939 remove_proc_entry("config",
4940 adapter
->controller_proc_dir_entry
);
4941 remove_proc_entry("mailbox",
4942 adapter
->controller_proc_dir_entry
);
4943 #if MEGA_HAVE_ENH_PROC
4944 remove_proc_entry("rebuild-rate",
4945 adapter
->controller_proc_dir_entry
);
4946 remove_proc_entry("battery-status",
4947 adapter
->controller_proc_dir_entry
);
4949 remove_proc_entry("diskdrives-ch0",
4950 adapter
->controller_proc_dir_entry
);
4951 remove_proc_entry("diskdrives-ch1",
4952 adapter
->controller_proc_dir_entry
);
4953 remove_proc_entry("diskdrives-ch2",
4954 adapter
->controller_proc_dir_entry
);
4955 remove_proc_entry("diskdrives-ch3",
4956 adapter
->controller_proc_dir_entry
);
4958 remove_proc_entry("raiddrives-0-9",
4959 adapter
->controller_proc_dir_entry
);
4960 remove_proc_entry("raiddrives-10-19",
4961 adapter
->controller_proc_dir_entry
);
4962 remove_proc_entry("raiddrives-20-29",
4963 adapter
->controller_proc_dir_entry
);
4964 remove_proc_entry("raiddrives-30-39",
4965 adapter
->controller_proc_dir_entry
);
4968 char buf
[12] = { 0 };
4969 sprintf(buf
, "hba%d", adapter
->host
->host_no
);
4970 remove_proc_entry(buf
, mega_proc_dir_entry
);
4975 pci_free_consistent(adapter
->dev
, MEGA_BUFFER_SIZE
,
4976 adapter
->mega_buffer
, adapter
->buf_dma_handle
);
4977 kfree(adapter
->scb_list
);
4978 pci_free_consistent(adapter
->dev
, sizeof(mbox64_t
),
4979 adapter
->una_mbox64
, adapter
->una_mbox64_dma
);
4981 scsi_host_put(host
);
4982 pci_disable_device(pdev
);
4988 megaraid_shutdown(struct pci_dev
*pdev
)
4990 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
4991 adapter_t
*adapter
= (adapter_t
*)host
->hostdata
;
4993 __megaraid_shutdown(adapter
);
4996 static struct pci_device_id megaraid_pci_tbl
[] = {
4997 {PCI_VENDOR_ID_AMI
, PCI_DEVICE_ID_AMI_MEGARAID
,
4998 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
4999 {PCI_VENDOR_ID_AMI
, PCI_DEVICE_ID_AMI_MEGARAID2
,
5000 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
5001 {PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_AMI_MEGARAID3
,
5002 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
5005 MODULE_DEVICE_TABLE(pci
, megaraid_pci_tbl
);
5007 static struct pci_driver megaraid_pci_driver
= {
5008 .name
= "megaraid_legacy",
5009 .id_table
= megaraid_pci_tbl
,
5010 .probe
= megaraid_probe_one
,
5011 .remove
= __devexit_p(megaraid_remove_one
),
5012 .shutdown
= megaraid_shutdown
,
5015 static int __init
megaraid_init(void)
5019 if ((max_cmd_per_lun
<= 0) || (max_cmd_per_lun
> MAX_CMD_PER_LUN
))
5020 max_cmd_per_lun
= MAX_CMD_PER_LUN
;
5021 if (max_mbox_busy_wait
> MBOX_BUSY_WAIT
)
5022 max_mbox_busy_wait
= MBOX_BUSY_WAIT
;
5024 #ifdef CONFIG_PROC_FS
5025 mega_proc_dir_entry
= proc_mkdir("megaraid", NULL
);
5026 if (!mega_proc_dir_entry
) {
5028 "megaraid: failed to create megaraid root\n");
5031 error
= pci_register_driver(&megaraid_pci_driver
);
5033 #ifdef CONFIG_PROC_FS
5034 remove_proc_entry("megaraid", NULL
);
5040 * Register the driver as a character device, for applications
5041 * to access it for ioctls.
5042 * First argument (major) to register_chrdev implies a dynamic
5043 * major number allocation.
5045 major
= register_chrdev(0, "megadev_legacy", &megadev_fops
);
5048 "megaraid: failed to register char device\n");
5054 static void __exit
megaraid_exit(void)
5057 * Unregister the character device interface to the driver.
5059 unregister_chrdev(major
, "megadev_legacy");
5061 pci_unregister_driver(&megaraid_pci_driver
);
5063 #ifdef CONFIG_PROC_FS
5064 remove_proc_entry("megaraid", NULL
);
5068 module_init(megaraid_init
);
5069 module_exit(megaraid_exit
);
5071 /* vi: set ts=8 sw=8 tw=78: */