3 * Linux MegaRAID device driver
5 * Copyright © 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.3 (Feb 19, 2003) - Atul Mukker <Atul.Mukker@lsil.com>
22 * Description: Linux device driver for LSI Logic MegaRAID controller
24 * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
27 * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
28 * and others. Please send updates to the mailing list
29 * linux-scsi@vger.kernel.org .
35 #include <linux/blkdev.h>
36 #include <asm/uaccess.h>
38 #include <linux/completion.h>
39 #include <linux/delay.h>
40 #include <linux/proc_fs.h>
41 #include <linux/reboot.h>
42 #include <linux/module.h>
43 #include <linux/list.h>
44 #include <linux/interrupt.h>
45 #include <linux/pci.h>
46 #include <linux/init.h>
47 #include <scsi/scsicam.h>
50 #include <scsi/scsi_host.h>
54 #define MEGARAID_MODULE_VERSION "2.00.3"
56 MODULE_AUTHOR ("LSI Logic Corporation");
57 MODULE_DESCRIPTION ("LSI Logic MegaRAID driver");
58 MODULE_LICENSE ("GPL");
59 MODULE_VERSION(MEGARAID_MODULE_VERSION);
61 static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
62 module_param(max_cmd_per_lun, uint, 0);
63 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)");
65 static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
66 module_param(max_sectors_per_io, ushort, 0);
67 MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
70 static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
71 module_param(max_mbox_busy_wait, ushort, 0);
72 MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
74 #define RDINDOOR(adapter) readl((adapter)->base + 0x20)
75 #define RDOUTDOOR(adapter) readl((adapter)->base + 0x2C)
76 #define WRINDOOR(adapter,value) writel(value, (adapter)->base + 0x20)
77 #define WROUTDOOR(adapter,value) writel(value, (adapter)->base + 0x2C)
84 static adapter_t *hba_soft_state[MAX_CONTROLLERS];
85 static struct proc_dir_entry *mega_proc_dir_entry;
87 /* For controller re-ordering */
88 static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
91 * The File Operations structure for the serial/ioctl interface of the driver
93 static struct file_operations megadev_fops = {
95 .ioctl = megadev_ioctl,
100 * Array to structures for storing the information about the controllers. This
101 * information is sent to the user level applications, when they do an ioctl
102 * for this information.
104 static struct mcontroller mcontroller[MAX_CONTROLLERS];
106 /* The current driver version */
107 static u32 driver_ver = 0x02000000;
109 /* major number used by the device for character interface */
112 #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
116 * Debug variable to print some diagnostic messages
118 static int trace_level;
121 * mega_setup_mailbox()
122 * @adapter - pointer to our soft state
124 * Allocates a 8 byte aligned memory for the handshake mailbox.
127 mega_setup_mailbox(adapter_t *adapter)
131 adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
132 sizeof(mbox64_t), &adapter->una_mbox64_dma);
134 if( !adapter->una_mbox64 ) return -1;
136 adapter->mbox = &adapter->una_mbox64->mbox;
138 adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
141 adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
143 align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
145 adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
148 * Register the mailbox if the controller is an io-mapped controller
150 if( adapter->flag & BOARD_IOMAP ) {
152 outb_p(adapter->mbox_dma & 0xFF,
153 adapter->host->io_port + MBOX_PORT0);
155 outb_p((adapter->mbox_dma >> 8) & 0xFF,
156 adapter->host->io_port + MBOX_PORT1);
158 outb_p((adapter->mbox_dma >> 16) & 0xFF,
159 adapter->host->io_port + MBOX_PORT2);
161 outb_p((adapter->mbox_dma >> 24) & 0xFF,
162 adapter->host->io_port + MBOX_PORT3);
164 outb_p(ENABLE_MBOX_BYTE,
165 adapter->host->io_port + ENABLE_MBOX_REGION);
177 * mega_query_adapter()
178 * @adapter - pointer to our soft state
180 * Issue the adapter inquiry commands to the controller and find out
181 * information and parameter about the devices attached
184 mega_query_adapter(adapter_t *adapter)
186 dma_addr_t prod_info_dma_handle;
187 mega_inquiry3 *inquiry3;
188 u8 raw_mbox[sizeof(struct mbox_out)];
192 /* Initialize adapter inquiry mailbox */
194 mbox = (mbox_t *)raw_mbox;
196 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
197 memset(&mbox->m_out, 0, sizeof(raw_mbox));
200 * Try to issue Inquiry3 command
201 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
202 * update enquiry3 structure
204 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
206 inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
208 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
209 raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
210 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
212 /* Issue a blocking command to the card */
213 if ((retval = issue_scb_block(adapter, raw_mbox))) {
214 /* the adapter does not support 40ld */
216 mraid_ext_inquiry *ext_inq;
218 dma_addr_t dma_handle;
220 ext_inq = pci_alloc_consistent(adapter->dev,
221 sizeof(mraid_ext_inquiry), &dma_handle);
223 if( ext_inq == NULL ) return -1;
225 inq = &ext_inq->raid_inq;
227 mbox->m_out.xferaddr = (u32)dma_handle;
229 /*issue old 0x04 command to adapter */
230 mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
232 issue_scb_block(adapter, raw_mbox);
235 * update Enquiry3 and ProductInfo structures with
236 * mraid_inquiry structure
238 mega_8_to_40ld(inq, inquiry3,
239 (mega_product_info *)&adapter->product_info);
241 pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
242 ext_inq, dma_handle);
244 } else { /*adapter supports 40ld */
245 adapter->flag |= BOARD_40LD;
248 * get product_info, which is static information and will be
251 prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
252 &adapter->product_info,
253 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
255 mbox->m_out.xferaddr = prod_info_dma_handle;
257 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
258 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
260 if ((retval = issue_scb_block(adapter, raw_mbox)))
262 "megaraid: Product_info cmd failed with error: %d\n",
265 pci_unmap_single(adapter->dev, prod_info_dma_handle,
266 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
271 * kernel scans the channels from 0 to <= max_channel
273 adapter->host->max_channel =
274 adapter->product_info.nchannels + NVIRT_CHAN -1;
276 adapter->host->max_id = 16; /* max targets per channel */
278 adapter->host->max_lun = 7; /* Upto 7 luns for non disk devices */
280 adapter->host->cmd_per_lun = max_cmd_per_lun;
282 adapter->numldrv = inquiry3->num_ldrv;
284 adapter->max_cmds = adapter->product_info.max_commands;
286 if(adapter->max_cmds > MAX_COMMANDS)
287 adapter->max_cmds = MAX_COMMANDS;
289 adapter->host->can_queue = adapter->max_cmds - 1;
292 * Get the maximum number of scatter-gather elements supported by this
295 mega_get_max_sgl(adapter);
297 adapter->host->sg_tablesize = adapter->sglen;
300 /* use HP firmware and bios version encoding */
301 if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
302 sprintf (adapter->fw_version, "%c%d%d.%d%d",
303 adapter->product_info.fw_version[2],
304 adapter->product_info.fw_version[1] >> 8,
305 adapter->product_info.fw_version[1] & 0x0f,
306 adapter->product_info.fw_version[0] >> 8,
307 adapter->product_info.fw_version[0] & 0x0f);
308 sprintf (adapter->bios_version, "%c%d%d.%d%d",
309 adapter->product_info.bios_version[2],
310 adapter->product_info.bios_version[1] >> 8,
311 adapter->product_info.bios_version[1] & 0x0f,
312 adapter->product_info.bios_version[0] >> 8,
313 adapter->product_info.bios_version[0] & 0x0f);
315 memcpy(adapter->fw_version,
316 (char *)adapter->product_info.fw_version, 4);
317 adapter->fw_version[4] = 0;
319 memcpy(adapter->bios_version,
320 (char *)adapter->product_info.bios_version, 4);
322 adapter->bios_version[4] = 0;
325 printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
326 adapter->fw_version, adapter->bios_version, adapter->numldrv);
329 * Do we support extended (>10 bytes) cdbs
331 adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
332 if (adapter->support_ext_cdb)
333 printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
341 * @adapter - pointer to our soft state
343 * Runs through the list of pending requests.
346 mega_runpendq(adapter_t *adapter)
348 if(!list_empty(&adapter->pending_list))
349 __mega_runpendq(adapter);
354 * @scmd - Issue this scsi command
355 * @done - the callback hook into the scsi mid-layer
357 * The command queuing entry point for the mid-layer.
360 megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
366 adapter = (adapter_t *)scmd->device->host->hostdata;
368 scmd->scsi_done = done;
372 * Allocate and build a SCB request
373 * busy flag will be set if mega_build_cmd() command could not
374 * allocate scb. We will return non-zero status in that case.
375 * NOTE: scb can be null even though certain commands completed
376 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
377 * return 0 in that case.
380 scb = mega_build_cmd(adapter, scmd, &busy);
383 scb->state |= SCB_PENDQ;
384 list_add_tail(&scb->list, &adapter->pending_list);
387 * Check if the HBA is in quiescent state, e.g., during a
388 * delete logical drive opertion. If it is, don't run
391 if(atomic_read(&adapter->quiescent) == 0) {
392 mega_runpendq(adapter);
401 * mega_allocate_scb()
402 * @adapter - pointer to our soft state
403 * @cmd - scsi command from the mid-layer
405 * Allocate a SCB structure. This is the central structure for controller
408 static inline scb_t *
409 mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
411 struct list_head *head = &adapter->free_list;
414 /* Unlink command from Free List */
415 if( !list_empty(head) ) {
417 scb = list_entry(head->next, scb_t, list);
419 list_del_init(head->next);
421 scb->state = SCB_ACTIVE;
423 scb->dma_type = MEGA_DMA_TYPE_NONE;
432 * mega_get_ldrv_num()
433 * @adapter - pointer to our soft state
434 * @cmd - scsi mid layer command
435 * @channel - channel on the controller
437 * Calculate the logical drive number based on the information in scsi command
438 * and the channel number.
441 mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
446 tgt = cmd->device->id;
448 if ( tgt > adapter->this_id )
449 tgt--; /* we do not get inquires for initiator id */
451 ldrv_num = (channel * 15) + tgt;
455 * If we have a logical drive with boot enabled, project it first
457 if( adapter->boot_ldrv_enabled ) {
458 if( ldrv_num == 0 ) {
459 ldrv_num = adapter->boot_ldrv;
462 if( ldrv_num <= adapter->boot_ldrv ) {
469 * If "delete logical drive" feature is enabled on this controller.
470 * Do only if at least one delete logical drive operation was done.
472 * Also, after logical drive deletion, instead of logical drive number,
473 * the value returned should be 0x80+logical drive id.
475 * These is valid only for IO commands.
478 if (adapter->support_random_del && adapter->read_ldidmap )
479 switch (cmd->cmnd[0]) {
480 case READ_6: /* fall through */
481 case WRITE_6: /* fall through */
482 case READ_10: /* fall through */
492 * @adapter - pointer to our soft state
493 * @cmd - Prepare using this scsi command
494 * @busy - busy flag if no resources
496 * Prepares a command and scatter gather list for the controller. This routine
497 * also finds out if the commands is intended for a logical drive or a
498 * physical device and prepares the controller command accordingly.
500 * We also re-order the logical drives and physical devices based on their
504 mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
506 mega_ext_passthru *epthru;
507 mega_passthru *pthru;
515 int ldrv_num = 0; /* logical drive number */
519 * filter the internal and ioctl commands
521 if((cmd->cmnd[0] == MEGA_INTERNAL_CMD)) {
527 * We know what channels our logical drives are on - mega_find_card()
529 islogical = adapter->logdrv_chan[cmd->device->channel];
532 * The theory: If physical drive is chosen for boot, all the physical
533 * devices are exported before the logical drives, otherwise physical
534 * devices are pushed after logical drives, in which case - Kernel sees
535 * the physical devices on virtual channel which is obviously converted
536 * to actual channel on the HBA.
538 if( adapter->boot_pdrv_enabled ) {
540 /* logical channel */
541 channel = cmd->device->channel -
542 adapter->product_info.nchannels;
545 /* this is physical channel */
546 channel = cmd->device->channel;
547 target = cmd->device->id;
550 * boot from a physical disk, that disk needs to be
551 * exposed first IF both the channels are SCSI, then
552 * booting from the second channel is not allowed.
555 target = adapter->boot_pdrv_tgt;
557 else if( target == adapter->boot_pdrv_tgt ) {
564 /* this is the logical channel */
565 channel = cmd->device->channel;
568 /* physical channel */
569 channel = cmd->device->channel - NVIRT_CHAN;
570 target = cmd->device->id;
577 /* have just LUN 0 for each target on virtual channels */
578 if (cmd->device->lun) {
579 cmd->result = (DID_BAD_TARGET << 16);
584 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
587 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
588 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
591 * max_ldrv_num increases by 0x80 if some logical drive was
594 if(adapter->read_ldidmap)
595 max_ldrv_num += 0x80;
597 if(ldrv_num > max_ldrv_num ) {
598 cmd->result = (DID_BAD_TARGET << 16);
605 if( cmd->device->lun > 7) {
607 * Do not support lun >7 for physically accessed
610 cmd->result = (DID_BAD_TARGET << 16);
618 * Logical drive commands
622 switch (cmd->cmnd[0]) {
623 case TEST_UNIT_READY:
624 #if MEGA_HAVE_CLUSTERING
626 * Do we support clustering and is the support enabled
627 * If no, return success always
629 if( !adapter->has_cluster ) {
630 cmd->result = (DID_OK << 16);
635 if(!(scb = mega_allocate_scb(adapter, cmd))) {
640 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
641 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
642 scb->raw_mbox[3] = ldrv_num;
644 scb->dma_direction = PCI_DMA_NONE;
648 cmd->result = (DID_OK << 16);
657 struct scatterlist *sg;
659 sg = (struct scatterlist *)cmd->request_buffer;
660 buf = kmap_atomic(sg->page, KM_IRQ0) +
663 buf = cmd->request_buffer;
664 memset(cmd->request_buffer, 0, cmd->cmnd[4]);
666 struct scatterlist *sg;
668 sg = (struct scatterlist *)cmd->request_buffer;
669 kunmap_atomic(buf - sg->offset, KM_IRQ0);
671 cmd->result = (DID_OK << 16);
679 if(!(adapter->flag & (1L << cmd->device->channel))) {
682 "scsi%d: scanning scsi channel %d ",
683 adapter->host->host_no,
684 cmd->device->channel);
685 printk("for logical drives.\n");
687 adapter->flag |= (1L << cmd->device->channel);
690 /* Allocate a SCB and initialize passthru */
691 if(!(scb = mega_allocate_scb(adapter, cmd))) {
697 mbox = (mbox_t *)scb->raw_mbox;
698 memset(mbox, 0, sizeof(scb->raw_mbox));
699 memset(pthru, 0, sizeof(mega_passthru));
703 pthru->reqsenselen = 14;
704 pthru->islogical = 1;
705 pthru->logdrv = ldrv_num;
706 pthru->cdblen = cmd->cmd_len;
707 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
709 if( adapter->has_64bit_addr ) {
710 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
713 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
716 scb->dma_direction = PCI_DMA_FROMDEVICE;
718 pthru->numsgelements = mega_build_sglist(adapter, scb,
719 &pthru->dataxferaddr, &pthru->dataxferlen);
721 mbox->m_out.xferaddr = scb->pthru_dma_addr;
732 /* Allocate a SCB and initialize mailbox */
733 if(!(scb = mega_allocate_scb(adapter, cmd))) {
737 mbox = (mbox_t *)scb->raw_mbox;
739 memset(mbox, 0, sizeof(scb->raw_mbox));
740 mbox->m_out.logdrv = ldrv_num;
743 * A little hack: 2nd bit is zero for all scsi read
744 * commands and is set for all scsi write commands
746 if( adapter->has_64bit_addr ) {
747 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
748 MEGA_MBOXCMD_LWRITE64:
749 MEGA_MBOXCMD_LREAD64 ;
752 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
758 * 6-byte READ(0x08) or WRITE(0x0A) cdb
760 if( cmd->cmd_len == 6 ) {
761 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
763 ((u32)cmd->cmnd[1] << 16) |
764 ((u32)cmd->cmnd[2] << 8) |
767 mbox->m_out.lba &= 0x1FFFFF;
771 * Take modulo 0x80, since the logical drive
772 * number increases by 0x80 when a logical
775 if (*cmd->cmnd == READ_6) {
776 adapter->nreads[ldrv_num%0x80]++;
777 adapter->nreadblocks[ldrv_num%0x80] +=
778 mbox->m_out.numsectors;
780 adapter->nwrites[ldrv_num%0x80]++;
781 adapter->nwriteblocks[ldrv_num%0x80] +=
782 mbox->m_out.numsectors;
788 * 10-byte READ(0x28) or WRITE(0x2A) cdb
790 if( cmd->cmd_len == 10 ) {
791 mbox->m_out.numsectors =
793 ((u32)cmd->cmnd[7] << 8);
795 ((u32)cmd->cmnd[2] << 24) |
796 ((u32)cmd->cmnd[3] << 16) |
797 ((u32)cmd->cmnd[4] << 8) |
801 if (*cmd->cmnd == READ_10) {
802 adapter->nreads[ldrv_num%0x80]++;
803 adapter->nreadblocks[ldrv_num%0x80] +=
804 mbox->m_out.numsectors;
806 adapter->nwrites[ldrv_num%0x80]++;
807 adapter->nwriteblocks[ldrv_num%0x80] +=
808 mbox->m_out.numsectors;
814 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
816 if( cmd->cmd_len == 12 ) {
818 ((u32)cmd->cmnd[2] << 24) |
819 ((u32)cmd->cmnd[3] << 16) |
820 ((u32)cmd->cmnd[4] << 8) |
823 mbox->m_out.numsectors =
824 ((u32)cmd->cmnd[6] << 24) |
825 ((u32)cmd->cmnd[7] << 16) |
826 ((u32)cmd->cmnd[8] << 8) |
830 if (*cmd->cmnd == READ_12) {
831 adapter->nreads[ldrv_num%0x80]++;
832 adapter->nreadblocks[ldrv_num%0x80] +=
833 mbox->m_out.numsectors;
835 adapter->nwrites[ldrv_num%0x80]++;
836 adapter->nwriteblocks[ldrv_num%0x80] +=
837 mbox->m_out.numsectors;
843 * If it is a read command
845 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
846 scb->dma_direction = PCI_DMA_FROMDEVICE;
849 scb->dma_direction = PCI_DMA_TODEVICE;
852 /* Calculate Scatter-Gather info */
853 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
854 (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
858 #if MEGA_HAVE_CLUSTERING
859 case RESERVE: /* Fall through */
863 * Do we support clustering and is the support enabled
865 if( ! adapter->has_cluster ) {
867 cmd->result = (DID_BAD_TARGET << 16);
872 /* Allocate a SCB and initialize mailbox */
873 if(!(scb = mega_allocate_scb(adapter, cmd))) {
878 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
879 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
880 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
882 scb->raw_mbox[3] = ldrv_num;
884 scb->dma_direction = PCI_DMA_NONE;
890 cmd->result = (DID_BAD_TARGET << 16);
897 * Passthru drive commands
900 /* Allocate a SCB and initialize passthru */
901 if(!(scb = mega_allocate_scb(adapter, cmd))) {
906 mbox = (mbox_t *)scb->raw_mbox;
907 memset(mbox, 0, sizeof(scb->raw_mbox));
909 if( adapter->support_ext_cdb ) {
911 epthru = mega_prepare_extpassthru(adapter, scb, cmd,
914 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
916 mbox->m_out.xferaddr = scb->epthru_dma_addr;
921 pthru = mega_prepare_passthru(adapter, scb, cmd,
924 /* Initialize mailbox */
925 if( adapter->has_64bit_addr ) {
926 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
929 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
932 mbox->m_out.xferaddr = scb->pthru_dma_addr;
942 * mega_prepare_passthru()
943 * @adapter - pointer to our soft state
944 * @scb - our scsi control block
945 * @cmd - scsi command from the mid-layer
946 * @channel - actual channel on the controller
947 * @target - actual id on the controller.
949 * prepare a command for the scsi physical devices.
951 static mega_passthru *
952 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
953 int channel, int target)
955 mega_passthru *pthru;
958 memset(pthru, 0, sizeof (mega_passthru));
960 /* 0=6sec/1=60sec/2=10min/3=3hrs */
964 pthru->reqsenselen = 14;
965 pthru->islogical = 0;
967 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
969 pthru->target = (adapter->flag & BOARD_40LD) ?
970 (channel << 4) | target : target;
972 pthru->cdblen = cmd->cmd_len;
973 pthru->logdrv = cmd->device->lun;
975 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
977 /* Not sure about the direction */
978 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
980 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
981 switch (cmd->cmnd[0]) {
984 if(!(adapter->flag & (1L << cmd->device->channel))) {
987 "scsi%d: scanning scsi channel %d [P%d] ",
988 adapter->host->host_no,
989 cmd->device->channel, channel);
990 printk("for physical devices.\n");
992 adapter->flag |= (1L << cmd->device->channel);
996 pthru->numsgelements = mega_build_sglist(adapter, scb,
997 &pthru->dataxferaddr, &pthru->dataxferlen);
1005 * mega_prepare_extpassthru()
1006 * @adapter - pointer to our soft state
1007 * @scb - our scsi control block
1008 * @cmd - scsi command from the mid-layer
1009 * @channel - actual channel on the controller
1010 * @target - actual id on the controller.
1012 * prepare a command for the scsi physical devices. This rountine prepares
1013 * commands for devices which can take extended CDBs (>10 bytes)
1015 static mega_ext_passthru *
1016 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1017 int channel, int target)
1019 mega_ext_passthru *epthru;
1021 epthru = scb->epthru;
1022 memset(epthru, 0, sizeof(mega_ext_passthru));
1024 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1025 epthru->timeout = 2;
1028 epthru->reqsenselen = 14;
1029 epthru->islogical = 0;
1031 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1032 epthru->target = (adapter->flag & BOARD_40LD) ?
1033 (channel << 4) | target : target;
1035 epthru->cdblen = cmd->cmd_len;
1036 epthru->logdrv = cmd->device->lun;
1038 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1040 /* Not sure about the direction */
1041 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1043 switch(cmd->cmnd[0]) {
1046 if(!(adapter->flag & (1L << cmd->device->channel))) {
1049 "scsi%d: scanning scsi channel %d [P%d] ",
1050 adapter->host->host_no,
1051 cmd->device->channel, channel);
1052 printk("for physical devices.\n");
1054 adapter->flag |= (1L << cmd->device->channel);
1058 epthru->numsgelements = mega_build_sglist(adapter, scb,
1059 &epthru->dataxferaddr, &epthru->dataxferlen);
1067 __mega_runpendq(adapter_t *adapter)
1070 struct list_head *pos, *next;
1072 /* Issue any pending commands to the card */
1073 list_for_each_safe(pos, next, &adapter->pending_list) {
1075 scb = list_entry(pos, scb_t, list);
1077 if( !(scb->state & SCB_ISSUED) ) {
1079 if( issue_scb(adapter, scb) != 0 )
1090 * @adapter - pointer to our soft state
1091 * @scb - scsi control block
1093 * Post a command to the card if the mailbox is available, otherwise return
1094 * busy. We also take the scb from the pending list if the mailbox is
1098 issue_scb(adapter_t *adapter, scb_t *scb)
1100 volatile mbox64_t *mbox64 = adapter->mbox64;
1101 volatile mbox_t *mbox = adapter->mbox;
1104 if(unlikely(mbox->m_in.busy)) {
1108 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1110 if(mbox->m_in.busy) return -1;
1113 /* Copy mailbox data into host structure */
1114 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1115 sizeof(struct mbox_out));
1117 mbox->m_out.cmdid = scb->idx; /* Set cmdid */
1118 mbox->m_in.busy = 1; /* Set busy */
1122 * Increment the pending queue counter
1124 atomic_inc(&adapter->pend_cmds);
1126 switch (mbox->m_out.cmd) {
1127 case MEGA_MBOXCMD_LREAD64:
1128 case MEGA_MBOXCMD_LWRITE64:
1129 case MEGA_MBOXCMD_PASSTHRU64:
1130 case MEGA_MBOXCMD_EXTPTHRU:
1131 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1132 mbox64->xfer_segment_hi = 0;
1133 mbox->m_out.xferaddr = 0xFFFFFFFF;
1136 mbox64->xfer_segment_lo = 0;
1137 mbox64->xfer_segment_hi = 0;
1143 scb->state |= SCB_ISSUED;
1145 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1146 mbox->m_in.poll = 0;
1148 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1151 irq_enable(adapter);
1152 issue_command(adapter);
1159 * Wait until the controller's mailbox is available
1162 mega_busywait_mbox (adapter_t *adapter)
1164 if (adapter->mbox->m_in.busy)
1165 return __mega_busywait_mbox(adapter);
1171 * @adapter - pointer to our soft state
1172 * @raw_mbox - the mailbox
1174 * Issue a scb in synchronous and non-interrupt mode
1177 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1179 volatile mbox64_t *mbox64 = adapter->mbox64;
1180 volatile mbox_t *mbox = adapter->mbox;
1183 /* Wait until mailbox is free */
1184 if(mega_busywait_mbox (adapter))
1185 goto bug_blocked_mailbox;
1187 /* Copy mailbox data into host structure */
1188 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1189 mbox->m_out.cmdid = 0xFE;
1190 mbox->m_in.busy = 1;
1192 switch (raw_mbox[0]) {
1193 case MEGA_MBOXCMD_LREAD64:
1194 case MEGA_MBOXCMD_LWRITE64:
1195 case MEGA_MBOXCMD_PASSTHRU64:
1196 case MEGA_MBOXCMD_EXTPTHRU:
1197 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1198 mbox64->xfer_segment_hi = 0;
1199 mbox->m_out.xferaddr = 0xFFFFFFFF;
1202 mbox64->xfer_segment_lo = 0;
1203 mbox64->xfer_segment_hi = 0;
1206 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1207 mbox->m_in.poll = 0;
1209 mbox->m_in.numstatus = 0xFF;
1210 mbox->m_in.status = 0xFF;
1211 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1213 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1216 mbox->m_in.numstatus = 0xFF;
1218 while( (volatile u8)mbox->m_in.poll != 0x77 )
1221 mbox->m_in.poll = 0;
1222 mbox->m_in.ack = 0x77;
1224 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1226 while(RDINDOOR(adapter) & 0x2)
1230 irq_disable(adapter);
1231 issue_command(adapter);
1233 while (!((byte = irq_state(adapter)) & INTR_VALID))
1236 set_irq_state(adapter, byte);
1237 irq_enable(adapter);
1241 return mbox->m_in.status;
1243 bug_blocked_mailbox:
1244 printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1251 * megaraid_isr_iomapped()
1253 * @devp - pointer to our soft state
1256 * Interrupt service routine for io-mapped controllers.
1257 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1258 * and service the completed commands.
1261 megaraid_isr_iomapped(int irq, void *devp, struct pt_regs *regs)
1263 adapter_t *adapter = devp;
1264 unsigned long flags;
1267 u8 completed[MAX_FIRMWARE_STATUS];
1273 * loop till F/W has more commands for us to complete.
1275 spin_lock_irqsave(&adapter->lock, flags);
1278 /* Check if a valid interrupt is pending */
1279 byte = irq_state(adapter);
1280 if( (byte & VALID_INTR_BYTE) == 0 ) {
1282 * No more pending commands
1286 set_irq_state(adapter, byte);
1288 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1291 adapter->mbox->m_in.numstatus = 0xFF;
1293 status = adapter->mbox->m_in.status;
1296 * decrement the pending queue counter
1298 atomic_sub(nstatus, &adapter->pend_cmds);
1300 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1303 /* Acknowledge interrupt */
1306 mega_cmd_done(adapter, completed, nstatus, status);
1308 mega_rundoneq(adapter);
1312 /* Loop through any pending requests */
1313 if(atomic_read(&adapter->quiescent) == 0) {
1314 mega_runpendq(adapter);
1321 spin_unlock_irqrestore(&adapter->lock, flags);
1323 return IRQ_RETVAL(handled);
1328 * megaraid_isr_memmapped()
1330 * @devp - pointer to our soft state
1333 * Interrupt service routine for memory-mapped controllers.
1334 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1335 * and service the completed commands.
1338 megaraid_isr_memmapped(int irq, void *devp, struct pt_regs *regs)
1340 adapter_t *adapter = devp;
1341 unsigned long flags;
1345 u8 completed[MAX_FIRMWARE_STATUS];
1350 * loop till F/W has more commands for us to complete.
1352 spin_lock_irqsave(&adapter->lock, flags);
1355 /* Check if a valid interrupt is pending */
1356 dword = RDOUTDOOR(adapter);
1357 if(dword != 0x10001234) {
1359 * No more pending commands
1363 WROUTDOOR(adapter, 0x10001234);
1365 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1369 adapter->mbox->m_in.numstatus = 0xFF;
1371 status = adapter->mbox->m_in.status;
1374 * decrement the pending queue counter
1376 atomic_sub(nstatus, &adapter->pend_cmds);
1378 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1381 /* Acknowledge interrupt */
1382 WRINDOOR(adapter, 0x2);
1386 while( RDINDOOR(adapter) & 0x02 ) cpu_relax();
1388 mega_cmd_done(adapter, completed, nstatus, status);
1390 mega_rundoneq(adapter);
1392 /* Loop through any pending requests */
1393 if(atomic_read(&adapter->quiescent) == 0) {
1394 mega_runpendq(adapter);
1401 spin_unlock_irqrestore(&adapter->lock, flags);
1403 return IRQ_RETVAL(handled);
1407 * @adapter - pointer to our soft state
1408 * @completed - array of ids of completed commands
1409 * @nstatus - number of completed commands
1410 * @status - status of the last command completed
1412 * Complete the comamnds and call the scsi mid-layer callback hooks.
1415 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1417 mega_ext_passthru *epthru = NULL;
1418 struct scatterlist *sgl;
1419 Scsi_Cmnd *cmd = NULL;
1420 mega_passthru *pthru = NULL;
1421 mbox_t *mbox = NULL;
1429 * for all the commands completed, call the mid-layer callback routine
1432 for( i = 0; i < nstatus; i++ ) {
1434 cmdid = completed[i];
1436 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1437 scb = &adapter->int_scb;
1439 mbox = (mbox_t *)scb->raw_mbox;
1442 * Internal command interface do not fire the extended
1443 * passthru or 64-bit passthru
1449 scb = &adapter->scb_list[cmdid];
1452 * Make sure f/w has completed a valid command
1454 if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1456 "megaraid: invalid command ");
1457 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1458 cmdid, scb->state, scb->cmd);
1464 * Was a abort issued for this command
1466 if( scb->state & SCB_ABORT ) {
1469 "megaraid: aborted cmd %lx[%x] complete.\n",
1470 scb->cmd->serial_number, scb->idx);
1472 scb->cmd->result = (DID_ABORT << 16);
1474 list_add_tail(SCSI_LIST(scb->cmd),
1475 &adapter->completed_list);
1477 mega_free_scb(adapter, scb);
1483 * Was a reset issued for this command
1485 if( scb->state & SCB_RESET ) {
1488 "megaraid: reset cmd %lx[%x] complete.\n",
1489 scb->cmd->serial_number, scb->idx);
1491 scb->cmd->result = (DID_RESET << 16);
1493 list_add_tail(SCSI_LIST(scb->cmd),
1494 &adapter->completed_list);
1496 mega_free_scb (adapter, scb);
1503 epthru = scb->epthru;
1504 mbox = (mbox_t *)scb->raw_mbox;
1509 int logdrv = mbox->m_out.logdrv;
1511 islogical = adapter->logdrv_chan[cmd->channel];
1513 * Maintain an error counter for the logical drive.
1514 * Some application like SNMP agent need such
1517 if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1518 cmd->cmnd[0] == READ_10 ||
1519 cmd->cmnd[0] == READ_12)) {
1521 * Logical drive number increases by 0x80 when
1522 * a logical drive is deleted
1524 adapter->rd_errors[logdrv%0x80]++;
1527 if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1528 cmd->cmnd[0] == WRITE_10 ||
1529 cmd->cmnd[0] == WRITE_12)) {
1531 * Logical drive number increases by 0x80 when
1532 * a logical drive is deleted
1534 adapter->wr_errors[logdrv%0x80]++;
1542 * Do not return the presence of hard disk on the channel so,
1543 * inquiry sent, and returned data==hard disk or removable
1544 * hard disk and not logical, request should return failure! -
1547 islogical = adapter->logdrv_chan[cmd->device->channel];
1548 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1551 sgl = (struct scatterlist *)
1552 cmd->request_buffer;
1555 c = *(unsigned char *)
1556 page_address((&sgl[0])->page) +
1561 "megaraid: invalid sg.\n");
1566 c = *(u8 *)cmd->request_buffer;
1569 if(IS_RAID_CH(adapter, cmd->device->channel) &&
1570 ((c & 0x1F ) == TYPE_DISK)) {
1575 /* clear result; otherwise, success returns corrupt value */
1578 /* Convert MegaRAID status to Linux error code */
1580 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1581 cmd->result |= (DID_OK << 16);
1584 case 0x02: /* ERROR_ABORTED, i.e.
1585 SCSI_STATUS_CHECK_CONDITION */
1587 /* set sense_buffer and result fields */
1588 if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1589 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1591 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1594 cmd->result = (DRIVER_SENSE << 24) |
1596 (CHECK_CONDITION << 1);
1599 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1601 memcpy(cmd->sense_buffer,
1602 epthru->reqsensearea, 14);
1604 cmd->result = (DRIVER_SENSE << 24) |
1606 (CHECK_CONDITION << 1);
1608 cmd->sense_buffer[0] = 0x70;
1609 cmd->sense_buffer[2] = ABORTED_COMMAND;
1610 cmd->result |= (CHECK_CONDITION << 1);
1615 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1617 cmd->result |= (DID_BUS_BUSY << 16) | status;
1621 #if MEGA_HAVE_CLUSTERING
1623 * If TEST_UNIT_READY fails, we know
1624 * MEGA_RESERVATION_STATUS failed
1626 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1627 cmd->result |= (DID_ERROR << 16) |
1628 (RESERVATION_CONFLICT << 1);
1632 * Error code returned is 1 if Reserve or Release
1633 * failed or the input parameter is invalid
1636 (cmd->cmnd[0] == RESERVE ||
1637 cmd->cmnd[0] == RELEASE) ) {
1639 cmd->result |= (DID_ERROR << 16) |
1640 (RESERVATION_CONFLICT << 1);
1644 cmd->result |= (DID_BAD_TARGET << 16)|status;
1648 * Only free SCBs for the commands coming down from the
1649 * mid-layer, not for which were issued internally
1651 * For internal command, restore the status returned by the
1652 * firmware so that user can interpret it.
1654 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1655 cmd->result = status;
1658 * Remove the internal command from the pending list
1660 list_del_init(&scb->list);
1661 scb->state = SCB_FREE;
1664 mega_free_scb(adapter, scb);
1667 /* Add Scsi_Command to end of completed queue */
1668 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1676 * Run through the list of completed requests and finish it
1679 mega_rundoneq (adapter_t *adapter)
1682 struct list_head *pos;
1684 list_for_each(pos, &adapter->completed_list) {
1686 Scsi_Pointer* spos = (Scsi_Pointer *)pos;
1688 cmd = list_entry(spos, Scsi_Cmnd, SCp);
1689 cmd->scsi_done(cmd);
1692 INIT_LIST_HEAD(&adapter->completed_list);
1697 * Free a SCB structure
1698 * Note: We assume the scsi commands associated with this scb is not free yet.
1701 mega_free_scb(adapter_t *adapter, scb_t *scb)
1703 unsigned long length;
1705 switch( scb->dma_type ) {
1707 case MEGA_DMA_TYPE_NONE:
1710 case MEGA_BULK_DATA:
1711 if (scb->cmd->use_sg == 0)
1712 length = scb->cmd->request_bufflen;
1714 struct scatterlist *sgl =
1715 (struct scatterlist *)scb->cmd->request_buffer;
1716 length = sgl->length;
1718 pci_unmap_page(adapter->dev, scb->dma_h_bulkdata,
1719 length, scb->dma_direction);
1723 pci_unmap_sg(adapter->dev, scb->cmd->request_buffer,
1724 scb->cmd->use_sg, scb->dma_direction);
1732 * Remove from the pending list
1734 list_del_init(&scb->list);
1736 /* Link the scb back into free list */
1737 scb->state = SCB_FREE;
1740 list_add(&scb->list, &adapter->free_list);
1745 __mega_busywait_mbox (adapter_t *adapter)
1747 volatile mbox_t *mbox = adapter->mbox;
1750 for (counter = 0; counter < 10000; counter++) {
1751 if (!mbox->m_in.busy)
1753 udelay(100); yield();
1755 return -1; /* give up after 1 second */
1759 * Copies data to SGLIST
1760 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1763 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1765 struct scatterlist *sgl;
1767 unsigned long offset;
1768 unsigned int length;
1775 /* Scatter-gather not used */
1776 if( cmd->use_sg == 0 || (cmd->use_sg == 1 &&
1777 !adapter->has_64bit_addr)) {
1779 if (cmd->use_sg == 0) {
1780 page = virt_to_page(cmd->request_buffer);
1781 offset = offset_in_page(cmd->request_buffer);
1782 length = cmd->request_bufflen;
1784 sgl = (struct scatterlist *)cmd->request_buffer;
1786 offset = sgl->offset;
1787 length = sgl->length;
1790 scb->dma_h_bulkdata = pci_map_page(adapter->dev,
1793 scb->dma_direction);
1794 scb->dma_type = MEGA_BULK_DATA;
1797 * We need to handle special 64-bit commands that need a
1800 if( adapter->has_64bit_addr ) {
1801 scb->sgl64[0].address = scb->dma_h_bulkdata;
1802 scb->sgl64[0].length = length;
1803 *buf = (u32)scb->sgl_dma_addr;
1808 *buf = (u32)scb->dma_h_bulkdata;
1814 sgl = (struct scatterlist *)cmd->request_buffer;
1817 * Copy Scatter-Gather list info into controller structure.
1819 * The number of sg elements returned must not exceed our limit
1821 sgcnt = pci_map_sg(adapter->dev, sgl, cmd->use_sg,
1822 scb->dma_direction);
1824 scb->dma_type = MEGA_SGLIST;
1826 if( sgcnt > adapter->sglen ) BUG();
1830 for( idx = 0; idx < sgcnt; idx++, sgl++ ) {
1832 if( adapter->has_64bit_addr ) {
1833 scb->sgl64[idx].address = sg_dma_address(sgl);
1834 *len += scb->sgl64[idx].length = sg_dma_len(sgl);
1837 scb->sgl[idx].address = sg_dma_address(sgl);
1838 *len += scb->sgl[idx].length = sg_dma_len(sgl);
1842 /* Reset pointer and length fields */
1843 *buf = scb->sgl_dma_addr;
1845 /* Return count of SG requests */
1853 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1854 * Enquiry3 structures for later use
1857 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1858 mega_product_info *product_info)
1862 product_info->max_commands = inquiry->adapter_info.max_commands;
1863 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1864 product_info->nchannels = inquiry->adapter_info.nchannels;
1866 for (i = 0; i < 4; i++) {
1867 product_info->fw_version[i] =
1868 inquiry->adapter_info.fw_version[i];
1870 product_info->bios_version[i] =
1871 inquiry->adapter_info.bios_version[i];
1873 enquiry3->cache_flush_interval =
1874 inquiry->adapter_info.cache_flush_interval;
1876 product_info->dram_size = inquiry->adapter_info.dram_size;
1878 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1880 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1881 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1882 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1883 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1886 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1887 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1891 mega_free_sgl(adapter_t *adapter)
1896 for(i = 0; i < adapter->max_cmds; i++) {
1898 scb = &adapter->scb_list[i];
1901 pci_free_consistent(adapter->dev,
1902 sizeof(mega_sgl64) * adapter->sglen,
1910 pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1911 scb->pthru, scb->pthru_dma_addr);
1917 pci_free_consistent(adapter->dev,
1918 sizeof(mega_ext_passthru),
1919 scb->epthru, scb->epthru_dma_addr);
1929 * Get information about the card/driver
1932 megaraid_info(struct Scsi_Host *host)
1934 static char buffer[512];
1937 adapter = (adapter_t *)host->hostdata;
1940 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1941 adapter->fw_version, adapter->product_info.max_commands,
1942 adapter->host->max_id, adapter->host->max_channel,
1943 adapter->host->max_lun);
1948 * Abort a previous SCSI request. Only commands on the pending list can be
1949 * aborted. All the commands issued to the F/W must complete.
1952 megaraid_abort(Scsi_Cmnd *cmd)
1957 adapter = (adapter_t *)cmd->device->host->hostdata;
1959 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1962 * This is required here to complete any completed requests
1963 * to be communicated over to the mid layer.
1965 mega_rundoneq(adapter);
1972 megaraid_reset(struct scsi_cmnd *cmd)
1978 adapter = (adapter_t *)cmd->device->host->hostdata;
1980 #if MEGA_HAVE_CLUSTERING
1981 mc.cmd = MEGA_CLUSTER_CMD;
1982 mc.opcode = MEGA_RESET_RESERVATIONS;
1984 if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) != 0 ) {
1986 "megaraid: reservation reset failed.\n");
1989 printk(KERN_INFO "megaraid: reservation reset.\n");
1993 spin_lock_irq(&adapter->lock);
1995 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1998 * This is required here to complete any completed requests
1999 * to be communicated over to the mid layer.
2001 mega_rundoneq(adapter);
2002 spin_unlock_irq(&adapter->lock);
2008 * megaraid_abort_and_reset()
2009 * @adapter - megaraid soft state
2010 * @cmd - scsi command to be aborted or reset
2011 * @aor - abort or reset flag
2013 * Try to locate the scsi command in the pending queue. If found and is not
2014 * issued to the controller, abort/reset it. Otherwise return failure
2017 megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
2019 struct list_head *pos, *next;
2022 printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
2023 (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
2024 cmd->cmnd[0], cmd->device->channel,
2025 cmd->device->id, cmd->device->lun);
2027 if(list_empty(&adapter->pending_list))
2030 list_for_each_safe(pos, next, &adapter->pending_list) {
2032 scb = list_entry(pos, scb_t, list);
2034 if (scb->cmd == cmd) { /* Found command */
2039 * Check if this command has firmare owenership. If
2040 * yes, we cannot reset this command. Whenever, f/w
2041 * completes this command, we will return appropriate
2044 if( scb->state & SCB_ISSUED ) {
2047 "megaraid: %s-%lx[%x], fw owner.\n",
2048 (aor==SCB_ABORT) ? "ABORTING":"RESET",
2049 cmd->serial_number, scb->idx);
2056 * Not yet issued! Remove from the pending
2060 "megaraid: %s-%lx[%x], driver owner.\n",
2061 (aor==SCB_ABORT) ? "ABORTING":"RESET",
2062 cmd->serial_number, scb->idx);
2064 mega_free_scb(adapter, scb);
2066 if( aor == SCB_ABORT ) {
2067 cmd->result = (DID_ABORT << 16);
2070 cmd->result = (DID_RESET << 16);
2073 list_add_tail(SCSI_LIST(cmd),
2074 &adapter->completed_list);
2085 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2087 *pdev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
2089 if( *pdev == NULL ) return -1;
2091 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2093 if( pci_set_dma_mask(*pdev, 0xffffffff) != 0 ) {
2102 free_local_pdev(struct pci_dev *pdev)
2108 * mega_allocate_inquiry()
2109 * @dma_handle - handle returned for dma address
2110 * @pdev - handle to pci device
2112 * allocates memory for inquiry structure
2114 static inline void *
2115 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2117 return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2122 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2124 pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2128 #ifdef CONFIG_PROC_FS
2129 /* Following code handles /proc fs */
2131 #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
2132 S_IRUSR | S_IFREG, \
2133 controller_proc_dir_entry, \
2137 * mega_create_proc_entry()
2138 * @index - index in soft state array
2139 * @parent - parent node for this /proc entry
2141 * Creates /proc entries for our controllers.
2144 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2146 struct proc_dir_entry *controller_proc_dir_entry = NULL;
2147 u8 string[64] = { 0 };
2148 adapter_t *adapter = hba_soft_state[index];
2150 sprintf(string, "hba%d", adapter->host->host_no);
2152 controller_proc_dir_entry =
2153 adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
2155 if(!controller_proc_dir_entry) {
2156 printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2159 adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
2160 adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
2161 adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
2162 #if MEGA_HAVE_ENH_PROC
2163 adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
2164 adapter->proc_battery = CREATE_READ_PROC("battery-status",
2168 * Display each physical drive on its channel
2170 adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
2172 adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
2174 adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
2176 adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
2180 * Display a set of up to 10 logical drive through each of following
2183 adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
2185 adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
2187 adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
2189 adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
2196 * proc_read_config()
2197 * @page - buffer to write the data in
2198 * @start - where the actual data has been written in page
2199 * @offset - same meaning as the read system call
2200 * @count - same meaning as the read system call
2201 * @eof - set if no more data needs to be returned
2202 * @data - pointer to our soft state
2204 * Display configuration information about the controller.
2207 proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
2211 adapter_t *adapter = (adapter_t *)data;
2214 len += sprintf(page+len, "%s", MEGARAID_VERSION);
2216 if(adapter->product_info.product_name[0])
2217 len += sprintf(page+len, "%s\n",
2218 adapter->product_info.product_name);
2220 len += sprintf(page+len, "Controller Type: ");
2222 if( adapter->flag & BOARD_MEMMAP ) {
2223 len += sprintf(page+len,
2224 "438/466/467/471/493/518/520/531/532\n");
2227 len += sprintf(page+len,
2231 if(adapter->flag & BOARD_40LD) {
2232 len += sprintf(page+len,
2233 "Controller Supports 40 Logical Drives\n");
2236 if(adapter->flag & BOARD_64BIT) {
2237 len += sprintf(page+len,
2238 "Controller capable of 64-bit memory addressing\n");
2240 if( adapter->has_64bit_addr ) {
2241 len += sprintf(page+len,
2242 "Controller using 64-bit memory addressing\n");
2245 len += sprintf(page+len,
2246 "Controller is not using 64-bit memory addressing\n");
2249 len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
2250 adapter->host->irq);
2252 len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
2253 adapter->numldrv, adapter->product_info.nchannels);
2255 len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
2256 adapter->fw_version, adapter->bios_version,
2257 adapter->product_info.dram_size);
2259 len += sprintf(page+len,
2260 "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2261 adapter->product_info.max_commands, adapter->max_cmds);
2263 len += sprintf(page+len, "support_ext_cdb = %d\n",
2264 adapter->support_ext_cdb);
2265 len += sprintf(page+len, "support_random_del = %d\n",
2266 adapter->support_random_del);
2267 len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
2268 adapter->boot_ldrv_enabled);
2269 len += sprintf(page+len, "boot_ldrv = %d\n",
2270 adapter->boot_ldrv);
2271 len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
2272 adapter->boot_pdrv_enabled);
2273 len += sprintf(page+len, "boot_pdrv_ch = %d\n",
2274 adapter->boot_pdrv_ch);
2275 len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
2276 adapter->boot_pdrv_tgt);
2277 len += sprintf(page+len, "quiescent = %d\n",
2278 atomic_read(&adapter->quiescent));
2279 len += sprintf(page+len, "has_cluster = %d\n",
2280 adapter->has_cluster);
2282 len += sprintf(page+len, "\nModule Parameters:\n");
2283 len += sprintf(page+len, "max_cmd_per_lun = %d\n",
2285 len += sprintf(page+len, "max_sectors_per_io = %d\n",
2286 max_sectors_per_io);
2297 * @page - buffer to write the data in
2298 * @start - where the actual data has been written in page
2299 * @offset - same meaning as the read system call
2300 * @count - same meaning as the read system call
2301 * @eof - set if no more data needs to be returned
2302 * @data - pointer to our soft state
2304 * Diaplay statistical information about the I/O activity.
2307 proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
2314 i = 0; /* avoid compilation warnings */
2316 adapter = (adapter_t *)data;
2318 len = sprintf(page, "Statistical Information for this controller\n");
2319 len += sprintf(page+len, "pend_cmds = %d\n",
2320 atomic_read(&adapter->pend_cmds));
2322 for(i = 0; i < adapter->numldrv; i++) {
2323 len += sprintf(page+len, "Logical Drive %d:\n", i);
2325 len += sprintf(page+len,
2326 "\tReads Issued = %lu, Writes Issued = %lu\n",
2327 adapter->nreads[i], adapter->nwrites[i]);
2329 len += sprintf(page+len,
2330 "\tSectors Read = %lu, Sectors Written = %lu\n",
2331 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2333 len += sprintf(page+len,
2334 "\tRead errors = %lu, Write errors = %lu\n\n",
2335 adapter->rd_errors[i], adapter->wr_errors[i]);
2338 len += sprintf(page+len,
2339 "IO and error counters not compiled in driver.\n");
2350 * @page - buffer to write the data in
2351 * @start - where the actual data has been written in page
2352 * @offset - same meaning as the read system call
2353 * @count - same meaning as the read system call
2354 * @eof - set if no more data needs to be returned
2355 * @data - pointer to our soft state
2357 * Display mailbox information for the last command issued. This information
2358 * is good for debugging.
2361 proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
2365 adapter_t *adapter = (adapter_t *)data;
2366 volatile mbox_t *mbox = adapter->mbox;
2369 len = sprintf(page, "Contents of Mail Box Structure\n");
2370 len += sprintf(page+len, " Fw Command = 0x%02x\n",
2372 len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
2374 len += sprintf(page+len, " No of Sectors= %04d\n",
2375 mbox->m_out.numsectors);
2376 len += sprintf(page+len, " LBA = 0x%02x\n",
2378 len += sprintf(page+len, " DTA = 0x%08x\n",
2379 mbox->m_out.xferaddr);
2380 len += sprintf(page+len, " Logical Drive= 0x%02x\n",
2381 mbox->m_out.logdrv);
2382 len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
2383 mbox->m_out.numsgelements);
2384 len += sprintf(page+len, " Busy = %01x\n",
2386 len += sprintf(page+len, " Status = 0x%02x\n",
2396 * proc_rebuild_rate()
2397 * @page - buffer to write the data in
2398 * @start - where the actual data has been written in page
2399 * @offset - same meaning as the read system call
2400 * @count - same meaning as the read system call
2401 * @eof - set if no more data needs to be returned
2402 * @data - pointer to our soft state
2404 * Display current rebuild rate
2407 proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
2410 adapter_t *adapter = (adapter_t *)data;
2411 dma_addr_t dma_handle;
2413 struct pci_dev *pdev;
2416 if( make_local_pdev(adapter, &pdev) != 0 ) {
2421 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2422 free_local_pdev(pdev);
2427 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2429 len = sprintf(page, "Adapter inquiry failed.\n");
2431 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2433 mega_free_inquiry(inquiry, dma_handle, pdev);
2435 free_local_pdev(pdev);
2442 if( adapter->flag & BOARD_40LD ) {
2443 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2444 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2447 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2448 ((mraid_ext_inquiry *)
2449 inquiry)->raid_inq.adapter_info.rebuild_rate);
2453 mega_free_inquiry(inquiry, dma_handle, pdev);
2455 free_local_pdev(pdev);
2465 * @page - buffer to write the data in
2466 * @start - where the actual data has been written in page
2467 * @offset - same meaning as the read system call
2468 * @count - same meaning as the read system call
2469 * @eof - set if no more data needs to be returned
2470 * @data - pointer to our soft state
2472 * Display information about the battery module on the controller.
2475 proc_battery(char *page, char **start, off_t offset, int count, int *eof,
2478 adapter_t *adapter = (adapter_t *)data;
2479 dma_addr_t dma_handle;
2481 struct pci_dev *pdev;
2482 u8 battery_status = 0;
2486 if( make_local_pdev(adapter, &pdev) != 0 ) {
2491 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2492 free_local_pdev(pdev);
2497 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2499 len = sprintf(page, "Adapter inquiry failed.\n");
2501 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2503 mega_free_inquiry(inquiry, dma_handle, pdev);
2505 free_local_pdev(pdev);
2512 if( adapter->flag & BOARD_40LD ) {
2513 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2516 battery_status = ((mraid_ext_inquiry *)inquiry)->
2517 raid_inq.adapter_info.battery_status;
2521 * Decode the battery status
2523 sprintf(str, "Battery Status:[%d]", battery_status);
2525 if(battery_status == MEGA_BATT_CHARGE_DONE)
2526 strcat(str, " Charge Done");
2528 if(battery_status & MEGA_BATT_MODULE_MISSING)
2529 strcat(str, " Module Missing");
2531 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2532 strcat(str, " Low Voltage");
2534 if(battery_status & MEGA_BATT_TEMP_HIGH)
2535 strcat(str, " Temperature High");
2537 if(battery_status & MEGA_BATT_PACK_MISSING)
2538 strcat(str, " Pack Missing");
2540 if(battery_status & MEGA_BATT_CHARGE_INPROG)
2541 strcat(str, " Charge In-progress");
2543 if(battery_status & MEGA_BATT_CHARGE_FAIL)
2544 strcat(str, " Charge Fail");
2546 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2547 strcat(str, " Cycles Exceeded");
2549 len = sprintf(page, "%s\n", str);
2552 mega_free_inquiry(inquiry, dma_handle, pdev);
2554 free_local_pdev(pdev);
2564 * @page - buffer to write the data in
2565 * @start - where the actual data has been written in page
2566 * @offset - same meaning as the read system call
2567 * @count - same meaning as the read system call
2568 * @eof - set if no more data needs to be returned
2569 * @data - pointer to our soft state
2571 * Display information about the physical drives on physical channel 0.
2574 proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
2577 adapter_t *adapter = (adapter_t *)data;
2581 return (proc_pdrv(adapter, page, 0));
2587 * @page - buffer to write the data in
2588 * @start - where the actual data has been written in page
2589 * @offset - same meaning as the read system call
2590 * @count - same meaning as the read system call
2591 * @eof - set if no more data needs to be returned
2592 * @data - pointer to our soft state
2594 * Display information about the physical drives on physical channel 1.
2597 proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
2600 adapter_t *adapter = (adapter_t *)data;
2604 return (proc_pdrv(adapter, page, 1));
2610 * @page - buffer to write the data in
2611 * @start - where the actual data has been written in page
2612 * @offset - same meaning as the read system call
2613 * @count - same meaning as the read system call
2614 * @eof - set if no more data needs to be returned
2615 * @data - pointer to our soft state
2617 * Display information about the physical drives on physical channel 2.
2620 proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
2623 adapter_t *adapter = (adapter_t *)data;
2627 return (proc_pdrv(adapter, page, 2));
2633 * @page - buffer to write the data in
2634 * @start - where the actual data has been written in page
2635 * @offset - same meaning as the read system call
2636 * @count - same meaning as the read system call
2637 * @eof - set if no more data needs to be returned
2638 * @data - pointer to our soft state
2640 * Display information about the physical drives on physical channel 3.
2643 proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
2646 adapter_t *adapter = (adapter_t *)data;
2650 return (proc_pdrv(adapter, page, 3));
2656 * @page - buffer to write the data in
2657 * @adapter - pointer to our soft state
2659 * Display information about the physical drives.
2662 proc_pdrv(adapter_t *adapter, char *page, int channel)
2664 dma_addr_t dma_handle;
2666 dma_addr_t scsi_inq_dma_handle;
2668 struct pci_dev *pdev;
2677 if( make_local_pdev(adapter, &pdev) != 0 ) {
2681 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2685 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2686 len = sprintf(page, "Adapter inquiry failed.\n");
2688 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2694 scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2696 if( scsi_inq == NULL ) {
2697 len = sprintf(page, "memory not available for scsi inq.\n");
2702 if( adapter->flag & BOARD_40LD ) {
2703 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2706 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2707 raid_inq.pdrv_info.pdrv_state;
2710 max_channels = adapter->product_info.nchannels;
2712 if( channel >= max_channels ) {
2716 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2718 i = channel*16 + tgt;
2720 state = *(pdrv_state + i);
2722 switch( state & 0x0F ) {
2726 "Channel:%2d Id:%2d State: Online",
2732 "Channel:%2d Id:%2d State: Failed",
2738 "Channel:%2d Id:%2d State: Rebuild",
2744 "Channel:%2d Id:%2d State: Hot spare",
2750 "Channel:%2d Id:%2d State: Un-configured",
2757 * This interface displays inquiries for disk drives
2758 * only. Inquries for logical drives and non-disk
2759 * devices are available through /proc/scsi/scsi
2761 memset(scsi_inq, 0, 256);
2762 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2763 scsi_inq_dma_handle) ||
2764 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2769 * Check for overflow. We print less than 240
2770 * characters for inquiry
2772 if( (len + 240) >= PAGE_SIZE ) break;
2774 len += sprintf(page+len, "%s.\n", str);
2776 len += mega_print_inquiry(page+len, scsi_inq);
2780 pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2782 mega_free_inquiry(inquiry, dma_handle, pdev);
2784 free_local_pdev(pdev);
2791 * Display scsi inquiry
2794 mega_print_inquiry(char *page, char *scsi_inq)
2799 len = sprintf(page, " Vendor: ");
2800 for( i = 8; i < 16; i++ ) {
2801 len += sprintf(page+len, "%c", scsi_inq[i]);
2804 len += sprintf(page+len, " Model: ");
2806 for( i = 16; i < 32; i++ ) {
2807 len += sprintf(page+len, "%c", scsi_inq[i]);
2810 len += sprintf(page+len, " Rev: ");
2812 for( i = 32; i < 36; i++ ) {
2813 len += sprintf(page+len, "%c", scsi_inq[i]);
2816 len += sprintf(page+len, "\n");
2818 i = scsi_inq[0] & 0x1f;
2820 len += sprintf(page+len, " Type: %s ",
2821 i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
2824 len += sprintf(page+len,
2825 " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
2827 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2828 len += sprintf(page+len, " CCS\n");
2830 len += sprintf(page+len, "\n");
2838 * @page - buffer to write the data in
2839 * @start - where the actual data has been written in page
2840 * @offset - same meaning as the read system call
2841 * @count - same meaning as the read system call
2842 * @eof - set if no more data needs to be returned
2843 * @data - pointer to our soft state
2845 * Display real time information about the logical drives 0 through 9.
2848 proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
2851 adapter_t *adapter = (adapter_t *)data;
2855 return (proc_rdrv(adapter, page, 0, 9));
2861 * @page - buffer to write the data in
2862 * @start - where the actual data has been written in page
2863 * @offset - same meaning as the read system call
2864 * @count - same meaning as the read system call
2865 * @eof - set if no more data needs to be returned
2866 * @data - pointer to our soft state
2868 * Display real time information about the logical drives 0 through 9.
2871 proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
2874 adapter_t *adapter = (adapter_t *)data;
2878 return (proc_rdrv(adapter, page, 10, 19));
2884 * @page - buffer to write the data in
2885 * @start - where the actual data has been written in page
2886 * @offset - same meaning as the read system call
2887 * @count - same meaning as the read system call
2888 * @eof - set if no more data needs to be returned
2889 * @data - pointer to our soft state
2891 * Display real time information about the logical drives 0 through 9.
2894 proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
2897 adapter_t *adapter = (adapter_t *)data;
2901 return (proc_rdrv(adapter, page, 20, 29));
2907 * @page - buffer to write the data in
2908 * @start - where the actual data has been written in page
2909 * @offset - same meaning as the read system call
2910 * @count - same meaning as the read system call
2911 * @eof - set if no more data needs to be returned
2912 * @data - pointer to our soft state
2914 * Display real time information about the logical drives 0 through 9.
2917 proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
2920 adapter_t *adapter = (adapter_t *)data;
2924 return (proc_rdrv(adapter, page, 30, 39));
2930 * @page - buffer to write the data in
2931 * @adapter - pointer to our soft state
2932 * @start - starting logical drive to display
2933 * @end - ending logical drive to display
2935 * We do not print the inquiry information since its already available through
2936 * /proc/scsi/scsi interface
2939 proc_rdrv(adapter_t *adapter, char *page, int start, int end )
2941 dma_addr_t dma_handle;
2942 logdrv_param *lparam;
2945 dma_addr_t disk_array_dma_handle;
2947 struct pci_dev *pdev;
2954 if( make_local_pdev(adapter, &pdev) != 0 ) {
2958 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2959 free_local_pdev(pdev);
2963 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2965 len = sprintf(page, "Adapter inquiry failed.\n");
2967 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2969 mega_free_inquiry(inquiry, dma_handle, pdev);
2971 free_local_pdev(pdev);
2976 memset(&mc, 0, sizeof(megacmd_t));
2978 if( adapter->flag & BOARD_40LD ) {
2979 array_sz = sizeof(disk_array_40ld);
2981 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2983 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2986 array_sz = sizeof(disk_array_8ld);
2988 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2989 raid_inq.logdrv_info.ldrv_state;
2991 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2992 raid_inq.logdrv_info.num_ldrv;
2995 disk_array = pci_alloc_consistent(pdev, array_sz,
2996 &disk_array_dma_handle);
2998 if( disk_array == NULL ) {
2999 len = sprintf(page, "memory not available.\n");
3001 mega_free_inquiry(inquiry, dma_handle, pdev);
3003 free_local_pdev(pdev);
3008 mc.xferaddr = (u32)disk_array_dma_handle;
3010 if( adapter->flag & BOARD_40LD ) {
3011 mc.cmd = FC_NEW_CONFIG;
3012 mc.opcode = OP_DCMD_READ_CONFIG;
3014 if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
3016 len = sprintf(page, "40LD read config failed.\n");
3018 mega_free_inquiry(inquiry, dma_handle, pdev);
3020 pci_free_consistent(pdev, array_sz, disk_array,
3021 disk_array_dma_handle);
3023 free_local_pdev(pdev);
3030 mc.cmd = NEW_READ_CONFIG_8LD;
3032 if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
3034 mc.cmd = READ_CONFIG_8LD;
3036 if( mega_internal_command(adapter, LOCK_INT, &mc,
3040 "8LD read config failed.\n");
3042 mega_free_inquiry(inquiry, dma_handle, pdev);
3044 pci_free_consistent(pdev, array_sz,
3046 disk_array_dma_handle);
3048 free_local_pdev(pdev);
3055 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
3057 if( adapter->flag & BOARD_40LD ) {
3059 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
3063 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
3067 * Check for overflow. We print less than 240 characters for
3068 * information about each logical drive.
3070 if( (len + 240) >= PAGE_SIZE ) break;
3072 len += sprintf(page+len, "Logical drive:%2d:, ", i);
3074 switch( rdrv_state[i] & 0x0F ) {
3076 len += sprintf(page+len, "state: offline");
3080 len += sprintf(page+len, "state: degraded");
3084 len += sprintf(page+len, "state: optimal");
3088 len += sprintf(page+len, "state: deleted");
3092 len += sprintf(page+len, "state: unknown");
3097 * Check if check consistency or initialization is going on
3098 * for this logical drive.
3100 if( (rdrv_state[i] & 0xF0) == 0x20 ) {
3101 len += sprintf(page+len,
3102 ", check-consistency in progress");
3104 else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
3105 len += sprintf(page+len,
3106 ", initialization in progress");
3109 len += sprintf(page+len, "\n");
3111 len += sprintf(page+len, "Span depth:%3d, ",
3112 lparam->span_depth);
3114 len += sprintf(page+len, "RAID level:%3d, ",
3117 len += sprintf(page+len, "Stripe size:%3d, ",
3118 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
3120 len += sprintf(page+len, "Row size:%3d\n",
3124 len += sprintf(page+len, "Read Policy: ");
3126 switch(lparam->read_ahead) {
3129 len += sprintf(page+len, "No read ahead, ");
3133 len += sprintf(page+len, "Read ahead, ");
3136 case ADAP_READ_AHEAD:
3137 len += sprintf(page+len, "Adaptive, ");
3142 len += sprintf(page+len, "Write Policy: ");
3144 switch(lparam->write_mode) {
3146 case WRMODE_WRITE_THRU:
3147 len += sprintf(page+len, "Write thru, ");
3150 case WRMODE_WRITE_BACK:
3151 len += sprintf(page+len, "Write back, ");
3155 len += sprintf(page+len, "Cache Policy: ");
3157 switch(lparam->direct_io) {
3160 len += sprintf(page+len, "Cached IO\n\n");
3164 len += sprintf(page+len, "Direct IO\n\n");
3169 mega_free_inquiry(inquiry, dma_handle, pdev);
3171 pci_free_consistent(pdev, array_sz, disk_array,
3172 disk_array_dma_handle);
3174 free_local_pdev(pdev);
3183 * megaraid_biosparam()
3185 * Return the disk geometry for a particular disk
3188 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
3189 sector_t capacity, int geom[])
3198 /* Get pointer to host config structure */
3199 adapter = (adapter_t *)sdev->host->hostdata;
3201 if (IS_RAID_CH(adapter, sdev->channel)) {
3202 /* Default heads (64) & sectors (32) */
3205 cylinders = (ulong)capacity / (heads * sectors);
3208 * Handle extended translation size for logical drives
3211 if ((ulong)capacity >= 0x200000) {
3214 cylinders = (ulong)capacity / (heads * sectors);
3220 geom[2] = cylinders;
3223 bh = scsi_bios_ptable(bdev);
3226 rval = scsi_partsize(bh, capacity,
3227 &geom[2], &geom[0], &geom[1]);
3234 "megaraid: invalid partition on this disk on channel %d\n",
3237 /* Default heads (64) & sectors (32) */
3240 cylinders = (ulong)capacity / (heads * sectors);
3242 /* Handle extended translation size for logical drives > 1Gb */
3243 if ((ulong)capacity >= 0x200000) {
3246 cylinders = (ulong)capacity / (heads * sectors);
3252 geom[2] = cylinders;
3260 * @adapter - pointer to our soft state
3262 * Allocate memory for the various pointers in the scb structures:
3263 * scatter-gather list pointer, passthru and extended passthru structure
3267 mega_init_scb(adapter_t *adapter)
3272 for( i = 0; i < adapter->max_cmds; i++ ) {
3274 scb = &adapter->scb_list[i];
3282 for( i = 0; i < adapter->max_cmds; i++ ) {
3284 scb = &adapter->scb_list[i];
3288 scb->sgl64 = pci_alloc_consistent(adapter->dev,
3289 sizeof(mega_sgl64) * adapter->sglen,
3290 &scb->sgl_dma_addr);
3292 scb->sgl = (mega_sglist *)scb->sgl64;
3295 printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
3296 mega_free_sgl(adapter);
3300 scb->pthru = pci_alloc_consistent(adapter->dev,
3301 sizeof(mega_passthru),
3302 &scb->pthru_dma_addr);
3305 printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
3306 mega_free_sgl(adapter);
3310 scb->epthru = pci_alloc_consistent(adapter->dev,
3311 sizeof(mega_ext_passthru),
3312 &scb->epthru_dma_addr);
3314 if( !scb->epthru ) {
3316 "Can't allocate extended passthru.\n");
3317 mega_free_sgl(adapter);
3322 scb->dma_type = MEGA_DMA_TYPE_NONE;
3326 * lock not required since we are loading the driver, so no
3327 * commands possible right now.
3329 scb->state = SCB_FREE;
3331 list_add(&scb->list, &adapter->free_list);
3343 * Routines for the character/ioctl interface to the driver. Find out if this
3344 * is a valid open. If yes, increment the module use count so that it cannot
3348 megadev_open (struct inode *inode, struct file *filep)
3351 * Only allow superuser to access private ioctl interface
3353 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
3361 * @inode - Our device inode
3363 * @cmd - ioctl command
3364 * @arg - user buffer
3366 * ioctl entry point for our private ioctl interface. We move the data in from
3367 * the user space, prepare the command (if necessary, convert the old MIMD
3368 * ioctl to new ioctl command), and issue a synchronous command to the
3372 megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
3379 mega_passthru __user *upthru; /* user address for passthru */
3380 mega_passthru *pthru; /* copy user passthru here */
3381 dma_addr_t pthru_dma_hndl;
3382 void *data = NULL; /* data to be transferred */
3383 dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
3385 megastat_t __user *ustats;
3388 struct pci_dev *pdev;
3390 ustats = NULL; /* avoid compilation warnings */
3394 * Make sure only USCSICMD are issued through this interface.
3395 * MIMD application would still fire different command.
3397 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3402 * Check and convert a possible MIMD command to NIT command.
3403 * mega_m_to_n() copies the data from the user space, so we do not
3404 * have to do it here.
3405 * NOTE: We will need some user address to copyout the data, therefore
3406 * the inteface layer will also provide us with the required user
3409 memset(&uioc, 0, sizeof(nitioctl_t));
3410 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3414 switch( uioc.opcode ) {
3416 case GET_DRIVER_VER:
3417 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3423 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3427 * Shucks. MIMD interface returns a positive value for number
3428 * of adapters. TODO: Change it to return 0 when there is no
3429 * applicatio using mimd interface.
3438 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3441 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3442 sizeof(struct mcontroller)) )
3452 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3455 adapter = hba_soft_state[adapno];
3457 ustats = uioc.uioc_uaddr;
3459 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3463 * Check for the validity of the logical drive number
3465 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3467 if( copy_to_user(ustats->nreads, adapter->nreads,
3468 num_ldrv*sizeof(u32)) )
3471 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3472 num_ldrv*sizeof(u32)) )
3475 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3476 num_ldrv*sizeof(u32)) )
3479 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3480 num_ldrv*sizeof(u32)) )
3483 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3484 num_ldrv*sizeof(u32)) )
3487 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3488 num_ldrv*sizeof(u32)) )
3499 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3502 adapter = hba_soft_state[adapno];
3505 * Deletion of logical drive is a special case. The adapter
3506 * should be quiescent before this command is issued.
3508 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3509 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3512 * Do we support this feature
3514 if( !adapter->support_random_del ) {
3515 printk(KERN_WARNING "megaraid: logdrv ");
3516 printk("delete on non-supporting F/W.\n");
3521 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3524 memset(&mc, 0, sizeof(megacmd_t));
3528 rval = mega_n_to_m((void __user *)arg, &mc);
3534 * This interface only support the regular passthru commands.
3535 * Reject extended passthru and 64-bit passthru
3537 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3538 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3540 printk(KERN_WARNING "megaraid: rejected passthru.\n");
3546 * For all internal commands, the buffer must be allocated in
3547 * <4GB address range
3549 if( make_local_pdev(adapter, &pdev) != 0 )
3552 /* Is it a passthru command or a DCMD */
3553 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3554 /* Passthru commands */
3556 pthru = pci_alloc_consistent(pdev,
3557 sizeof(mega_passthru),
3560 if( pthru == NULL ) {
3561 free_local_pdev(pdev);
3566 * The user passthru structure
3568 upthru = (mega_passthru __user *)MBOX(uioc)->xferaddr;
3571 * Copy in the user passthru here.
3573 if( copy_from_user(pthru, upthru,
3574 sizeof(mega_passthru)) ) {
3576 pci_free_consistent(pdev,
3577 sizeof(mega_passthru), pthru,
3580 free_local_pdev(pdev);
3586 * Is there a data transfer
3588 if( pthru->dataxferlen ) {
3589 data = pci_alloc_consistent(pdev,
3593 if( data == NULL ) {
3594 pci_free_consistent(pdev,
3595 sizeof(mega_passthru),
3599 free_local_pdev(pdev);
3605 * Save the user address and point the kernel
3606 * address at just allocated memory
3608 uxferaddr = pthru->dataxferaddr;
3609 pthru->dataxferaddr = data_dma_hndl;
3614 * Is data coming down-stream
3616 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3620 if( copy_from_user(data, (char __user *)uxferaddr,
3621 pthru->dataxferlen) ) {
3623 goto freemem_and_return;
3627 memset(&mc, 0, sizeof(megacmd_t));
3629 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3630 mc.xferaddr = (u32)pthru_dma_hndl;
3635 mega_internal_command(adapter, LOCK_INT, &mc, pthru);
3637 rval = mega_n_to_m((void __user *)arg, &mc);
3639 if( rval ) goto freemem_and_return;
3643 * Is data going up-stream
3645 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3646 if( copy_to_user((char __user *)uxferaddr, data,
3647 pthru->dataxferlen) ) {
3653 * Send the request sense data also, irrespective of
3654 * whether the user has asked for it or not.
3656 copy_to_user(upthru->reqsensearea,
3657 pthru->reqsensearea, 14);
3660 if( pthru->dataxferlen ) {
3661 pci_free_consistent(pdev,
3662 pthru->dataxferlen, data,
3666 pci_free_consistent(pdev, sizeof(mega_passthru),
3667 pthru, pthru_dma_hndl);
3669 free_local_pdev(pdev);
3677 * Is there a data transfer
3679 if( uioc.xferlen ) {
3680 data = pci_alloc_consistent(pdev,
3681 uioc.xferlen, &data_dma_hndl);
3683 if( data == NULL ) {
3684 free_local_pdev(pdev);
3688 uxferaddr = MBOX(uioc)->xferaddr;
3692 * Is data coming down-stream
3694 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3698 if( copy_from_user(data, (char __user *)uxferaddr,
3701 pci_free_consistent(pdev,
3703 data, data_dma_hndl);
3705 free_local_pdev(pdev);
3711 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3713 mc.xferaddr = (u32)data_dma_hndl;
3718 mega_internal_command(adapter, LOCK_INT, &mc, NULL);
3720 rval = mega_n_to_m((void __user *)arg, &mc);
3723 if( uioc.xferlen ) {
3724 pci_free_consistent(pdev,
3729 free_local_pdev(pdev);
3735 * Is data going up-stream
3737 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3738 if( copy_to_user((char __user *)uxferaddr, data,
3745 if( uioc.xferlen ) {
3746 pci_free_consistent(pdev,
3751 free_local_pdev(pdev);
3765 * @arg - user address
3766 * @uioc - new ioctl structure
3768 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3771 * Converts the older mimd ioctl structure to newer NIT structure
3774 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3776 struct uioctl_t uioc_mimd;
3777 char signature[8] = {0};
3783 * check is the application conforms to NIT. We do not have to do much
3785 * We exploit the fact that the signature is stored in the very
3786 * begining of the structure.
3789 if( copy_from_user(signature, arg, 7) )
3792 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3795 * NOTE NOTE: The nit ioctl is still under flux because of
3796 * change of mailbox definition, in HPE. No applications yet
3797 * use this interface and let's not have applications use this
3798 * interface till the new specifitions are in place.
3802 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3809 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3811 * Get the user ioctl structure
3813 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3818 * Get the opcode and subopcode for the commands
3820 opcode = uioc_mimd.ui.fcs.opcode;
3821 subopcode = uioc_mimd.ui.fcs.subopcode;
3826 switch (subopcode) {
3828 case MEGAIOC_QDRVRVER: /* Query driver version */
3829 uioc->opcode = GET_DRIVER_VER;
3830 uioc->uioc_uaddr = uioc_mimd.data;
3833 case MEGAIOC_QNADAP: /* Get # of adapters */
3834 uioc->opcode = GET_N_ADAP;
3835 uioc->uioc_uaddr = uioc_mimd.data;
3838 case MEGAIOC_QADAPINFO: /* Get adapter information */
3839 uioc->opcode = GET_ADAP_INFO;
3840 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3841 uioc->uioc_uaddr = uioc_mimd.data;
3853 uioc->opcode = MBOX_CMD;
3854 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3856 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3858 uioc->xferlen = uioc_mimd.ui.fcs.length;
3860 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3861 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3867 uioc->opcode = MBOX_CMD;
3868 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3870 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3873 * Choose the xferlen bigger of input and output data
3875 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3876 uioc_mimd.outlen : uioc_mimd.inlen;
3878 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3879 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3893 * @arg - user address
3894 * @mc - mailbox command
3896 * Updates the status information to the application, depending on application
3897 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3900 mega_n_to_m(void __user *arg, megacmd_t *mc)
3902 nitioctl_t __user *uiocp;
3903 megacmd_t __user *umc;
3904 mega_passthru __user *upthru;
3905 struct uioctl_t __user *uioc_mimd;
3906 char signature[8] = {0};
3909 * check is the application conforms to NIT.
3911 if( copy_from_user(signature, arg, 7) )
3914 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3918 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3921 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3923 umc = MBOX_P(uiocp);
3925 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3928 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3935 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3938 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3940 umc = (megacmd_t __user *)uioc_mimd->mbox;
3942 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3945 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3955 * MEGARAID 'FW' commands.
3959 * mega_is_bios_enabled()
3960 * @adapter - pointer to our soft state
3962 * issue command to find out if the BIOS is enabled for this controller
3965 mega_is_bios_enabled(adapter_t *adapter)
3967 unsigned char raw_mbox[sizeof(struct mbox_out)];
3971 mbox = (mbox_t *)raw_mbox;
3973 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3975 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3977 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3979 raw_mbox[0] = IS_BIOS_ENABLED;
3980 raw_mbox[2] = GET_BIOS;
3983 ret = issue_scb_block(adapter, raw_mbox);
3985 return *(char *)adapter->mega_buffer;
3990 * mega_enum_raid_scsi()
3991 * @adapter - pointer to our soft state
3993 * Find out what channels are RAID/SCSI. This information is used to
3994 * differentiate the virtual channels and physical channels and to support
3995 * ROMB feature and non-disk devices.
3998 mega_enum_raid_scsi(adapter_t *adapter)
4000 unsigned char raw_mbox[sizeof(struct mbox_out)];
4004 mbox = (mbox_t *)raw_mbox;
4006 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4009 * issue command to find out what channels are raid/scsi
4011 raw_mbox[0] = CHNL_CLASS;
4012 raw_mbox[2] = GET_CHNL_CLASS;
4014 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4016 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4019 * Non-ROMB firmware fail this command, so all channels
4020 * must be shown RAID
4022 adapter->mega_ch_class = 0xFF;
4024 if(!issue_scb_block(adapter, raw_mbox)) {
4025 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
4029 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
4030 if( (adapter->mega_ch_class >> i) & 0x01 ) {
4031 printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
4035 printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
4045 * mega_get_boot_drv()
4046 * @adapter - pointer to our soft state
4048 * Find out which device is the boot device. Note, any logical drive or any
4049 * phyical device (e.g., a CDROM) can be designated as a boot device.
4052 mega_get_boot_drv(adapter_t *adapter)
4054 struct private_bios_data *prv_bios_data;
4055 unsigned char raw_mbox[sizeof(struct mbox_out)];
4062 mbox = (mbox_t *)raw_mbox;
4064 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4066 raw_mbox[0] = BIOS_PVT_DATA;
4067 raw_mbox[2] = GET_BIOS_PVT_DATA;
4069 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4071 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4073 adapter->boot_ldrv_enabled = 0;
4074 adapter->boot_ldrv = 0;
4076 adapter->boot_pdrv_enabled = 0;
4077 adapter->boot_pdrv_ch = 0;
4078 adapter->boot_pdrv_tgt = 0;
4080 if(issue_scb_block(adapter, raw_mbox) == 0) {
4082 (struct private_bios_data *)adapter->mega_buffer;
4085 cksum_p = (char *)prv_bios_data;
4086 for (i = 0; i < 14; i++ ) {
4087 cksum += (u16)(*cksum_p++);
4090 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
4093 * If MSB is set, a physical drive is set as boot
4096 if( prv_bios_data->boot_drv & 0x80 ) {
4097 adapter->boot_pdrv_enabled = 1;
4098 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
4099 adapter->boot_pdrv_ch = boot_pdrv / 16;
4100 adapter->boot_pdrv_tgt = boot_pdrv % 16;
4103 adapter->boot_ldrv_enabled = 1;
4104 adapter->boot_ldrv = prv_bios_data->boot_drv;
4112 * mega_support_random_del()
4113 * @adapter - pointer to our soft state
4115 * Find out if this controller supports random deletion and addition of
4119 mega_support_random_del(adapter_t *adapter)
4121 unsigned char raw_mbox[sizeof(struct mbox_out)];
4125 mbox = (mbox_t *)raw_mbox;
4127 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4132 raw_mbox[0] = FC_DEL_LOGDRV;
4133 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
4135 rval = issue_scb_block(adapter, raw_mbox);
4142 * mega_support_ext_cdb()
4143 * @adapter - pointer to our soft state
4145 * Find out if this firmware support cdblen > 10
4148 mega_support_ext_cdb(adapter_t *adapter)
4150 unsigned char raw_mbox[sizeof(struct mbox_out)];
4154 mbox = (mbox_t *)raw_mbox;
4156 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4158 * issue command to find out if controller supports extended CDBs.
4163 rval = issue_scb_block(adapter, raw_mbox);
4171 * @adapter - pointer to our soft state
4172 * @logdrv - logical drive to be deleted
4174 * Delete the specified logical drive. It is the responsibility of the user
4175 * app to let the OS know about this operation.
4178 mega_del_logdrv(adapter_t *adapter, int logdrv)
4180 unsigned long flags;
4185 * Stop sending commands to the controller, queue them internally.
4186 * When deletion is complete, ISR will flush the queue.
4188 atomic_set(&adapter->quiescent, 1);
4191 * Wait till all the issued commands are complete and there are no
4192 * commands in the pending queue
4194 while (atomic_read(&adapter->pend_cmds) > 0 ||
4195 !list_empty(&adapter->pending_list))
4196 msleep(1000); /* sleep for 1s */
4198 rval = mega_do_del_logdrv(adapter, logdrv);
4200 spin_lock_irqsave(&adapter->lock, flags);
4203 * If delete operation was successful, add 0x80 to the logical drive
4204 * ids for commands in the pending queue.
4206 if (adapter->read_ldidmap) {
4207 struct list_head *pos;
4208 list_for_each(pos, &adapter->pending_list) {
4209 scb = list_entry(pos, scb_t, list);
4210 if (scb->pthru->logdrv < 0x80 )
4211 scb->pthru->logdrv += 0x80;
4215 atomic_set(&adapter->quiescent, 0);
4217 mega_runpendq(adapter);
4219 spin_unlock_irqrestore(&adapter->lock, flags);
4226 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
4231 memset( &mc, 0, sizeof(megacmd_t));
4233 mc.cmd = FC_DEL_LOGDRV;
4234 mc.opcode = OP_DEL_LOGDRV;
4235 mc.subopcode = logdrv;
4237 rval = mega_internal_command(adapter, LOCK_INT, &mc, NULL);
4239 /* log this event */
4241 printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
4246 * After deleting first logical drive, the logical drives must be
4247 * addressed by adding 0x80 to the logical drive id.
4249 adapter->read_ldidmap = 1;
4256 * mega_get_max_sgl()
4257 * @adapter - pointer to our soft state
4259 * Find out the maximum number of scatter-gather elements supported by this
4260 * version of the firmware
4263 mega_get_max_sgl(adapter_t *adapter)
4265 unsigned char raw_mbox[sizeof(struct mbox_out)];
4268 mbox = (mbox_t *)raw_mbox;
4270 memset(mbox, 0, sizeof(raw_mbox));
4272 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4274 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4276 raw_mbox[0] = MAIN_MISC_OPCODE;
4277 raw_mbox[2] = GET_MAX_SG_SUPPORT;
4280 if( issue_scb_block(adapter, raw_mbox) ) {
4282 * f/w does not support this command. Choose the default value
4284 adapter->sglen = MIN_SGLIST;
4287 adapter->sglen = *((char *)adapter->mega_buffer);
4290 * Make sure this is not more than the resources we are
4291 * planning to allocate
4293 if ( adapter->sglen > MAX_SGLIST )
4294 adapter->sglen = MAX_SGLIST;
4302 * mega_support_cluster()
4303 * @adapter - pointer to our soft state
4305 * Find out if this firmware support cluster calls.
4308 mega_support_cluster(adapter_t *adapter)
4310 unsigned char raw_mbox[sizeof(struct mbox_out)];
4313 mbox = (mbox_t *)raw_mbox;
4315 memset(mbox, 0, sizeof(raw_mbox));
4317 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4319 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4322 * Try to get the initiator id. This command will succeed iff the
4323 * clustering is available on this HBA.
4325 raw_mbox[0] = MEGA_GET_TARGET_ID;
4327 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
4330 * Cluster support available. Get the initiator target id.
4331 * Tell our id to mid-layer too.
4333 adapter->this_id = *(u32 *)adapter->mega_buffer;
4334 adapter->host->this_id = adapter->this_id;
4345 * @adapter - pointer to our soft state
4346 * @dma_handle - DMA address of the buffer
4348 * Issue internal comamnds while interrupts are available.
4349 * We only issue direct mailbox commands from within the driver. ioctl()
4350 * interface using these routines can issue passthru commands.
4353 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
4357 memset(&mc, 0, sizeof(megacmd_t));
4359 if( adapter->flag & BOARD_40LD ) {
4360 mc.cmd = FC_NEW_CONFIG;
4361 mc.opcode = NC_SUBOP_ENQUIRY3;
4362 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
4365 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
4368 mc.xferaddr = (u32)dma_handle;
4370 if ( mega_internal_command(adapter, LOCK_INT, &mc, NULL) != 0 ) {
4378 /** mega_internal_dev_inquiry()
4379 * @adapter - pointer to our soft state
4380 * @ch - channel for this device
4381 * @tgt - ID of this device
4382 * @buf_dma_handle - DMA address of the buffer
4384 * Issue the scsi inquiry for the specified device.
4387 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4388 dma_addr_t buf_dma_handle)
4390 mega_passthru *pthru;
4391 dma_addr_t pthru_dma_handle;
4394 struct pci_dev *pdev;
4398 * For all internal commands, the buffer must be allocated in <4GB
4401 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4403 pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4406 if( pthru == NULL ) {
4407 free_local_pdev(pdev);
4413 pthru->reqsenselen = 14;
4414 pthru->islogical = 0;
4416 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4418 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4422 pthru->cdb[0] = INQUIRY;
4426 pthru->cdb[4] = 255;
4430 pthru->dataxferaddr = (u32)buf_dma_handle;
4431 pthru->dataxferlen = 256;
4433 memset(&mc, 0, sizeof(megacmd_t));
4435 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4436 mc.xferaddr = (u32)pthru_dma_handle;
4438 rval = mega_internal_command(adapter, LOCK_INT, &mc, pthru);
4440 pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4443 free_local_pdev(pdev);
4450 * mega_internal_command()
4451 * @adapter - pointer to our soft state
4452 * @ls - the scope of the exclusion lock.
4453 * @mc - the mailbox command
4454 * @pthru - Passthru structure for DCDB commands
4456 * Issue the internal commands in interrupt mode.
4457 * The last argument is the address of the passthru structure if the command
4458 * to be fired is a passthru command
4460 * lockscope specifies whether the caller has already acquired the lock. Of
4461 * course, the caller must know which lock we are talking about.
4463 * Note: parameter 'pthru' is null for non-passthru commands.
4466 mega_internal_command(adapter_t *adapter, lockscope_t ls, megacmd_t *mc,
4467 mega_passthru *pthru )
4470 struct scsi_device *sdev;
4471 unsigned long flags = 0;
4476 * The internal commands share one command id and hence are
4477 * serialized. This is so because we want to reserve maximum number of
4478 * available command ids for the I/O commands.
4480 down(&adapter->int_mtx);
4482 scb = &adapter->int_scb;
4483 memset(scb, 0, sizeof(scb_t));
4485 scmd = &adapter->int_scmd;
4486 memset(scmd, 0, sizeof(Scsi_Cmnd));
4488 sdev = kmalloc(sizeof(struct scsi_device), GFP_KERNEL);
4489 memset(sdev, 0, sizeof(struct scsi_device));
4490 scmd->device = sdev;
4492 scmd->device->host = adapter->host;
4493 scmd->buffer = (void *)scb;
4494 scmd->cmnd[0] = MEGA_INTERNAL_CMD;
4496 scb->state |= SCB_ACTIVE;
4499 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4502 * Is it a passthru command
4504 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4509 scb->idx = CMDID_INT_CMDS;
4512 * Get the lock only if the caller has not acquired it already
4514 if( ls == LOCK_INT ) spin_lock_irqsave(&adapter->lock, flags);
4516 megaraid_queue(scmd, mega_internal_done);
4518 if( ls == LOCK_INT ) spin_unlock_irqrestore(&adapter->lock, flags);
4520 wait_for_completion(&adapter->int_waitq);
4522 rval = scmd->result;
4523 mc->status = scmd->result;
4527 * Print a debug message for all failed commands. Applications can use
4530 if( scmd->result && trace_level ) {
4531 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4532 mc->cmd, mc->opcode, mc->subopcode, scmd->result);
4535 up(&adapter->int_mtx);
4542 * mega_internal_done()
4543 * @scmd - internal scsi command
4545 * Callback routine for internal commands.
4548 mega_internal_done(Scsi_Cmnd *scmd)
4552 adapter = (adapter_t *)scmd->device->host->hostdata;
4554 complete(&adapter->int_waitq);
4559 static struct scsi_host_template megaraid_template = {
4560 .module = THIS_MODULE,
4562 .proc_name = "megaraid",
4563 .info = megaraid_info,
4564 .queuecommand = megaraid_queue,
4565 .bios_param = megaraid_biosparam,
4566 .max_sectors = MAX_SECTORS_PER_IO,
4567 .can_queue = MAX_COMMANDS,
4568 .this_id = DEFAULT_INITIATOR_ID,
4569 .sg_tablesize = MAX_SGLIST,
4570 .cmd_per_lun = DEF_CMD_PER_LUN,
4571 .use_clustering = ENABLE_CLUSTERING,
4572 .eh_abort_handler = megaraid_abort,
4573 .eh_device_reset_handler = megaraid_reset,
4574 .eh_bus_reset_handler = megaraid_reset,
4575 .eh_host_reset_handler = megaraid_reset,
4578 static int __devinit
4579 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4581 struct Scsi_Host *host;
4583 unsigned long mega_baseport, tbase, flag = 0;
4584 u16 subsysid, subsysvid;
4585 u8 pci_bus, pci_dev_func;
4587 int error = -ENODEV;
4589 if (pci_enable_device(pdev))
4591 pci_set_master(pdev);
4593 pci_bus = pdev->bus->number;
4594 pci_dev_func = pdev->devfn;
4597 * The megaraid3 stuff reports the ID of the Intel part which is not
4598 * remotely specific to the megaraid
4600 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4603 * Don't fall over the Compaq management cards using the same
4606 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4607 pdev->subsystem_device == 0xC000)
4609 /* Now check the magic signature byte */
4610 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4611 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4613 /* Ok it is probably a megaraid */
4617 * For these vendor and device ids, signature offsets are not
4618 * valid and 64 bit is implicit
4620 if (id->driver_data & BOARD_64BIT)
4621 flag |= BOARD_64BIT;
4625 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4626 if (magic64 == HBA_SIGNATURE_64BIT)
4627 flag |= BOARD_64BIT;
4630 subsysvid = pdev->subsystem_vendor;
4631 subsysid = pdev->subsystem_device;
4633 printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4634 id->vendor, id->device, pci_bus);
4636 printk("slot %d:func %d\n",
4637 PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
4639 /* Read the base port and IRQ from PCI */
4640 mega_baseport = pci_resource_start(pdev, 0);
4643 tbase = mega_baseport;
4644 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4645 flag |= BOARD_MEMMAP;
4647 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4648 printk(KERN_WARNING "megaraid: mem region busy!\n");
4649 goto out_disable_device;
4652 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4653 if (!mega_baseport) {
4655 "megaraid: could not map hba memory\n");
4656 goto out_release_region;
4659 flag |= BOARD_IOMAP;
4660 mega_baseport += 0x10;
4662 if (!request_region(mega_baseport, 16, "megaraid"))
4663 goto out_disable_device;
4666 /* Initialize SCSI Host structure */
4667 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4671 adapter = (adapter_t *)host->hostdata;
4672 memset(adapter, 0, sizeof(adapter_t));
4675 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4676 host->host_no, mega_baseport, irq);
4678 adapter->base = mega_baseport;
4680 INIT_LIST_HEAD(&adapter->free_list);
4681 INIT_LIST_HEAD(&adapter->pending_list);
4682 INIT_LIST_HEAD(&adapter->completed_list);
4684 adapter->flag = flag;
4685 spin_lock_init(&adapter->lock);
4686 scsi_assign_lock(host, &adapter->lock);
4688 host->cmd_per_lun = max_cmd_per_lun;
4689 host->max_sectors = max_sectors_per_io;
4691 adapter->dev = pdev;
4692 adapter->host = host;
4694 adapter->host->irq = irq;
4696 if (flag & BOARD_MEMMAP)
4697 adapter->host->base = tbase;
4699 adapter->host->io_port = tbase;
4700 adapter->host->n_io_port = 16;
4703 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4706 * Allocate buffer to issue internal commands.
4708 adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4709 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4710 if (!adapter->mega_buffer) {
4711 printk(KERN_WARNING "megaraid: out of RAM.\n");
4715 adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4716 if (!adapter->scb_list) {
4717 printk(KERN_WARNING "megaraid: out of RAM.\n");
4718 goto out_free_cmd_buffer;
4721 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4722 megaraid_isr_memmapped : megaraid_isr_iomapped,
4723 SA_SHIRQ, "megaraid", adapter)) {
4725 "megaraid: Couldn't register IRQ %d!\n", irq);
4726 goto out_free_scb_list;
4729 if (mega_setup_mailbox(adapter))
4732 if (mega_query_adapter(adapter))
4736 * Have checks for some buggy f/w
4738 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4742 if (!strcmp(adapter->fw_version, "3.00") ||
4743 !strcmp(adapter->fw_version, "3.01")) {
4745 printk( KERN_WARNING
4746 "megaraid: Your card is a Dell PERC "
4747 "2/SC RAID controller with "
4748 "firmware\nmegaraid: 3.00 or 3.01. "
4749 "This driver is known to have "
4750 "corruption issues\nmegaraid: with "
4751 "those firmware versions on this "
4752 "specific card. In order\nmegaraid: "
4753 "to protect your data, please upgrade "
4754 "your firmware to version\nmegaraid: "
4755 "3.10 or later, available from the "
4756 "Dell Technical Support web\n"
4757 "megaraid: site at\nhttp://support."
4758 "dell.com/us/en/filelib/download/"
4759 "index.asp?fileid=2940\n"
4765 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4766 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4767 * support, since this firmware cannot handle 64 bit
4770 if ((subsysvid == HP_SUBSYS_VID) &&
4771 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4775 if (!strcmp(adapter->fw_version, "H01.07") ||
4776 !strcmp(adapter->fw_version, "H01.08") ||
4777 !strcmp(adapter->fw_version, "H01.09") ) {
4779 "megaraid: Firmware H.01.07, "
4780 "H.01.08, and H.01.09 on 1M/2M "
4782 "megaraid: do not support 64 bit "
4783 "addressing.\nmegaraid: DISABLING "
4784 "64 bit support.\n");
4785 adapter->flag &= ~BOARD_64BIT;
4789 if (mega_is_bios_enabled(adapter))
4790 mega_hbas[hba_count].is_bios_enabled = 1;
4791 mega_hbas[hba_count].hostdata_addr = adapter;
4794 * Find out which channel is raid and which is scsi. This is
4797 mega_enum_raid_scsi(adapter);
4800 * Find out if a logical drive is set as the boot drive. If
4801 * there is one, will make that as the first logical drive.
4802 * ROMB: Do we have to boot from a physical drive. Then all
4803 * the physical drives would appear before the logical disks.
4804 * Else, all the physical drives would be exported to the mid
4805 * layer after logical drives.
4807 mega_get_boot_drv(adapter);
4809 if (adapter->boot_pdrv_enabled) {
4810 j = adapter->product_info.nchannels;
4811 for( i = 0; i < j; i++ )
4812 adapter->logdrv_chan[i] = 0;
4813 for( i = j; i < NVIRT_CHAN + j; i++ )
4814 adapter->logdrv_chan[i] = 1;
4816 for (i = 0; i < NVIRT_CHAN; i++)
4817 adapter->logdrv_chan[i] = 1;
4818 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4819 adapter->logdrv_chan[i] = 0;
4820 adapter->mega_ch_class <<= NVIRT_CHAN;
4824 * Do we support random deletion and addition of logical
4827 adapter->read_ldidmap = 0; /* set it after first logdrv
4829 adapter->support_random_del = mega_support_random_del(adapter);
4831 /* Initialize SCBs */
4832 if (mega_init_scb(adapter))
4836 * Reset the pending commands counter
4838 atomic_set(&adapter->pend_cmds, 0);
4841 * Reset the adapter quiescent flag
4843 atomic_set(&adapter->quiescent, 0);
4845 hba_soft_state[hba_count] = adapter;
4848 * Fill in the structure which needs to be passed back to the
4849 * application when it does an ioctl() for controller related
4854 mcontroller[i].base = mega_baseport;
4855 mcontroller[i].irq = irq;
4856 mcontroller[i].numldrv = adapter->numldrv;
4857 mcontroller[i].pcibus = pci_bus;
4858 mcontroller[i].pcidev = id->device;
4859 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4860 mcontroller[i].pciid = -1;
4861 mcontroller[i].pcivendor = id->vendor;
4862 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4863 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4866 /* Set the Mode of addressing to 64 bit if we can */
4867 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4868 pci_set_dma_mask(pdev, 0xffffffffffffffffULL);
4869 adapter->has_64bit_addr = 1;
4871 pci_set_dma_mask(pdev, 0xffffffff);
4872 adapter->has_64bit_addr = 0;
4875 init_MUTEX(&adapter->int_mtx);
4876 init_completion(&adapter->int_waitq);
4878 adapter->this_id = DEFAULT_INITIATOR_ID;
4879 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4881 #if MEGA_HAVE_CLUSTERING
4883 * Is cluster support enabled on this controller
4884 * Note: In a cluster the HBAs ( the initiators ) will have
4885 * different target IDs and we cannot assume it to be 7. Call
4886 * to mega_support_cluster() will get the target ids also if
4887 * the cluster support is available
4889 adapter->has_cluster = mega_support_cluster(adapter);
4890 if (adapter->has_cluster) {
4892 "megaraid: Cluster driver, initiator id:%d\n",
4897 pci_set_drvdata(pdev, host);
4899 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4901 error = scsi_add_host(host, &pdev->dev);
4905 scsi_scan_host(host);
4910 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4911 adapter->una_mbox64, adapter->una_mbox64_dma);
4913 free_irq(adapter->host->irq, adapter);
4915 kfree(adapter->scb_list);
4916 out_free_cmd_buffer:
4917 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4918 adapter->mega_buffer, adapter->buf_dma_handle);
4920 scsi_host_put(host);
4922 if (flag & BOARD_MEMMAP)
4923 iounmap((void *)mega_baseport);
4925 if (flag & BOARD_MEMMAP)
4926 release_mem_region(tbase, 128);
4928 release_region(mega_baseport, 16);
4930 pci_disable_device(pdev);
4936 __megaraid_shutdown(adapter_t *adapter)
4938 u_char raw_mbox[sizeof(struct mbox_out)];
4939 mbox_t *mbox = (mbox_t *)raw_mbox;
4942 /* Flush adapter cache */
4943 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4944 raw_mbox[0] = FLUSH_ADAPTER;
4946 free_irq(adapter->host->irq, adapter);
4948 /* Issue a blocking (interrupts disabled) command to the card */
4949 issue_scb_block(adapter, raw_mbox);
4951 /* Flush disks cache */
4952 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4953 raw_mbox[0] = FLUSH_SYSTEM;
4955 /* Issue a blocking (interrupts disabled) command to the card */
4956 issue_scb_block(adapter, raw_mbox);
4958 if (atomic_read(&adapter->pend_cmds) > 0)
4959 printk(KERN_WARNING "megaraid: pending commands!!\n");
4962 * Have a delibrate delay to make sure all the caches are
4965 for (i = 0; i <= 10; i++)
4970 megaraid_remove_one(struct pci_dev *pdev)
4972 struct Scsi_Host *host = pci_get_drvdata(pdev);
4973 adapter_t *adapter = (adapter_t *)host->hostdata;
4974 char buf[12] = { 0 };
4976 scsi_remove_host(host);
4978 __megaraid_shutdown(adapter);
4980 /* Free our resources */
4981 if (adapter->flag & BOARD_MEMMAP) {
4982 iounmap((void *)adapter->base);
4983 release_mem_region(adapter->host->base, 128);
4985 release_region(adapter->base, 16);
4987 mega_free_sgl(adapter);
4989 #ifdef CONFIG_PROC_FS
4990 if (adapter->controller_proc_dir_entry) {
4991 remove_proc_entry("stat", adapter->controller_proc_dir_entry);
4992 remove_proc_entry("config",
4993 adapter->controller_proc_dir_entry);
4994 remove_proc_entry("mailbox",
4995 adapter->controller_proc_dir_entry);
4996 #if MEGA_HAVE_ENH_PROC
4997 remove_proc_entry("rebuild-rate",
4998 adapter->controller_proc_dir_entry);
4999 remove_proc_entry("battery-status",
5000 adapter->controller_proc_dir_entry);
5002 remove_proc_entry("diskdrives-ch0",
5003 adapter->controller_proc_dir_entry);
5004 remove_proc_entry("diskdrives-ch1",
5005 adapter->controller_proc_dir_entry);
5006 remove_proc_entry("diskdrives-ch2",
5007 adapter->controller_proc_dir_entry);
5008 remove_proc_entry("diskdrives-ch3",
5009 adapter->controller_proc_dir_entry);
5011 remove_proc_entry("raiddrives-0-9",
5012 adapter->controller_proc_dir_entry);
5013 remove_proc_entry("raiddrives-10-19",
5014 adapter->controller_proc_dir_entry);
5015 remove_proc_entry("raiddrives-20-29",
5016 adapter->controller_proc_dir_entry);
5017 remove_proc_entry("raiddrives-30-39",
5018 adapter->controller_proc_dir_entry);
5020 sprintf(buf, "hba%d", adapter->host->host_no);
5021 remove_proc_entry(buf, mega_proc_dir_entry);
5025 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
5026 adapter->mega_buffer, adapter->buf_dma_handle);
5027 kfree(adapter->scb_list);
5028 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
5029 adapter->una_mbox64, adapter->una_mbox64_dma);
5031 scsi_host_put(host);
5032 pci_disable_device(pdev);
5038 megaraid_shutdown(struct pci_dev *pdev)
5040 struct Scsi_Host *host = pci_get_drvdata(pdev);
5041 adapter_t *adapter = (adapter_t *)host->hostdata;
5043 __megaraid_shutdown(adapter);
5046 static struct pci_device_id megaraid_pci_tbl[] = {
5047 {PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DISCOVERY,
5048 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5049 {PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_PERC4_DI,
5050 PCI_ANY_ID, PCI_ANY_ID, 0, 0, BOARD_64BIT},
5051 {PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_PERC4_QC_VERDE,
5052 PCI_ANY_ID, PCI_ANY_ID, 0, 0, BOARD_64BIT},
5053 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
5054 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5055 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
5056 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5057 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID3,
5058 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5059 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
5060 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5061 {PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_AMI_MEGARAID3,
5062 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5065 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
5067 static struct pci_driver megaraid_pci_driver = {
5069 .id_table = megaraid_pci_tbl,
5070 .probe = megaraid_probe_one,
5071 .remove = __devexit_p(megaraid_remove_one),
5072 .shutdown = megaraid_shutdown,
5075 static int __init megaraid_init(void)
5079 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
5080 max_cmd_per_lun = MAX_CMD_PER_LUN;
5081 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
5082 max_mbox_busy_wait = MBOX_BUSY_WAIT;
5084 #ifdef CONFIG_PROC_FS
5085 mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
5086 if (!mega_proc_dir_entry) {
5088 "megaraid: failed to create megaraid root\n");
5091 error = pci_module_init(&megaraid_pci_driver);
5093 #ifdef CONFIG_PROC_FS
5094 remove_proc_entry("megaraid", &proc_root);
5100 * Register the driver as a character device, for applications
5101 * to access it for ioctls.
5102 * First argument (major) to register_chrdev implies a dynamic
5103 * major number allocation.
5105 major = register_chrdev(0, "megadev", &megadev_fops);
5108 "megaraid: failed to register char device\n");
5114 static void __exit megaraid_exit(void)
5117 * Unregister the character device interface to the driver.
5119 unregister_chrdev(major, "megadev");
5121 pci_unregister_driver(&megaraid_pci_driver);
5123 #ifdef CONFIG_PROC_FS
5124 remove_proc_entry("megaraid", &proc_root);
5128 module_init(megaraid_init);
5129 module_exit(megaraid_exit);
5131 /* vi: set ts=8 sw=8 tw=78: */