various bugfixes
[cor_2_6_31.git] / drivers / scsi / megaraid.c
blob49eb0612d5af03f10e8b2ad591ed0bbf25943557
1 /*
3 * Linux MegaRAID device driver
5 * Copyright (c) 2002 LSI Logic Corporation.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
13 * - fixes
14 * - speed-ups (list handling fixes, issued_list, optimizations.)
15 * - lots of cleanups.
17 * Copyright (c) 2003 Christoph Hellwig <hch@lst.de>
18 * - new-style, hotplug-aware pci probing and scsi registration
20 * Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
21 * <Seokmann.Ju@lsil.com>
23 * Description: Linux device driver for LSI Logic MegaRAID controller
25 * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
26 * 518, 520, 531, 532
28 * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
29 * and others. Please send updates to the mailing list
30 * linux-scsi@vger.kernel.org .
34 #include <linux/mm.h>
35 #include <linux/fs.h>
36 #include <linux/blkdev.h>
37 #include <asm/uaccess.h>
38 #include <asm/io.h>
39 #include <linux/completion.h>
40 #include <linux/delay.h>
41 #include <linux/proc_fs.h>
42 #include <linux/reboot.h>
43 #include <linux/module.h>
44 #include <linux/list.h>
45 #include <linux/interrupt.h>
46 #include <linux/pci.h>
47 #include <linux/init.h>
48 #include <linux/dma-mapping.h>
49 #include <linux/smp_lock.h>
50 #include <scsi/scsicam.h>
52 #include "scsi.h"
53 #include <scsi/scsi_host.h>
55 #include "megaraid.h"
57 #define MEGARAID_MODULE_VERSION "2.00.4"
59 MODULE_AUTHOR ("sju@lsil.com");
60 MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
61 MODULE_LICENSE ("GPL");
62 MODULE_VERSION(MEGARAID_MODULE_VERSION);
64 static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
65 module_param(max_cmd_per_lun, uint, 0);
66 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)");
68 static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
69 module_param(max_sectors_per_io, ushort, 0);
70 MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
73 static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
74 module_param(max_mbox_busy_wait, ushort, 0);
75 MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
77 #define RDINDOOR(adapter) readl((adapter)->mmio_base + 0x20)
78 #define RDOUTDOOR(adapter) readl((adapter)->mmio_base + 0x2C)
79 #define WRINDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x20)
80 #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
83 * Global variables
86 static int hba_count;
87 static adapter_t *hba_soft_state[MAX_CONTROLLERS];
88 static struct proc_dir_entry *mega_proc_dir_entry;
90 /* For controller re-ordering */
91 static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
94 * The File Operations structure for the serial/ioctl interface of the driver
96 static const struct file_operations megadev_fops = {
97 .owner = THIS_MODULE,
98 .ioctl = megadev_ioctl,
99 .open = megadev_open,
103 * Array to structures for storing the information about the controllers. This
104 * information is sent to the user level applications, when they do an ioctl
105 * for this information.
107 static struct mcontroller mcontroller[MAX_CONTROLLERS];
109 /* The current driver version */
110 static u32 driver_ver = 0x02000000;
112 /* major number used by the device for character interface */
113 static int major;
115 #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
119 * Debug variable to print some diagnostic messages
121 static int trace_level;
124 * mega_setup_mailbox()
125 * @adapter - pointer to our soft state
127 * Allocates a 8 byte aligned memory for the handshake mailbox.
129 static int
130 mega_setup_mailbox(adapter_t *adapter)
132 unsigned long align;
134 adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
135 sizeof(mbox64_t), &adapter->una_mbox64_dma);
137 if( !adapter->una_mbox64 ) return -1;
139 adapter->mbox = &adapter->una_mbox64->mbox;
141 adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
142 (~0UL ^ 0xFUL));
144 adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
146 align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
148 adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
151 * Register the mailbox if the controller is an io-mapped controller
153 if( adapter->flag & BOARD_IOMAP ) {
155 outb(adapter->mbox_dma & 0xFF,
156 adapter->host->io_port + MBOX_PORT0);
158 outb((adapter->mbox_dma >> 8) & 0xFF,
159 adapter->host->io_port + MBOX_PORT1);
161 outb((adapter->mbox_dma >> 16) & 0xFF,
162 adapter->host->io_port + MBOX_PORT2);
164 outb((adapter->mbox_dma >> 24) & 0xFF,
165 adapter->host->io_port + MBOX_PORT3);
167 outb(ENABLE_MBOX_BYTE,
168 adapter->host->io_port + ENABLE_MBOX_REGION);
170 irq_ack(adapter);
172 irq_enable(adapter);
175 return 0;
180 * mega_query_adapter()
181 * @adapter - pointer to our soft state
183 * Issue the adapter inquiry commands to the controller and find out
184 * information and parameter about the devices attached
186 static int
187 mega_query_adapter(adapter_t *adapter)
189 dma_addr_t prod_info_dma_handle;
190 mega_inquiry3 *inquiry3;
191 u8 raw_mbox[sizeof(struct mbox_out)];
192 mbox_t *mbox;
193 int retval;
195 /* Initialize adapter inquiry mailbox */
197 mbox = (mbox_t *)raw_mbox;
199 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
200 memset(&mbox->m_out, 0, sizeof(raw_mbox));
203 * Try to issue Inquiry3 command
204 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
205 * update enquiry3 structure
207 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
209 inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
211 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
212 raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
213 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
215 /* Issue a blocking command to the card */
216 if ((retval = issue_scb_block(adapter, raw_mbox))) {
217 /* the adapter does not support 40ld */
219 mraid_ext_inquiry *ext_inq;
220 mraid_inquiry *inq;
221 dma_addr_t dma_handle;
223 ext_inq = pci_alloc_consistent(adapter->dev,
224 sizeof(mraid_ext_inquiry), &dma_handle);
226 if( ext_inq == NULL ) return -1;
228 inq = &ext_inq->raid_inq;
230 mbox->m_out.xferaddr = (u32)dma_handle;
232 /*issue old 0x04 command to adapter */
233 mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
235 issue_scb_block(adapter, raw_mbox);
238 * update Enquiry3 and ProductInfo structures with
239 * mraid_inquiry structure
241 mega_8_to_40ld(inq, inquiry3,
242 (mega_product_info *)&adapter->product_info);
244 pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
245 ext_inq, dma_handle);
247 } else { /*adapter supports 40ld */
248 adapter->flag |= BOARD_40LD;
251 * get product_info, which is static information and will be
252 * unchanged
254 prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
255 &adapter->product_info,
256 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
258 mbox->m_out.xferaddr = prod_info_dma_handle;
260 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
261 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
263 if ((retval = issue_scb_block(adapter, raw_mbox)))
264 printk(KERN_WARNING
265 "megaraid: Product_info cmd failed with error: %d\n",
266 retval);
268 pci_unmap_single(adapter->dev, prod_info_dma_handle,
269 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
274 * kernel scans the channels from 0 to <= max_channel
276 adapter->host->max_channel =
277 adapter->product_info.nchannels + NVIRT_CHAN -1;
279 adapter->host->max_id = 16; /* max targets per channel */
281 adapter->host->max_lun = 7; /* Upto 7 luns for non disk devices */
283 adapter->host->cmd_per_lun = max_cmd_per_lun;
285 adapter->numldrv = inquiry3->num_ldrv;
287 adapter->max_cmds = adapter->product_info.max_commands;
289 if(adapter->max_cmds > MAX_COMMANDS)
290 adapter->max_cmds = MAX_COMMANDS;
292 adapter->host->can_queue = adapter->max_cmds - 1;
295 * Get the maximum number of scatter-gather elements supported by this
296 * firmware
298 mega_get_max_sgl(adapter);
300 adapter->host->sg_tablesize = adapter->sglen;
303 /* use HP firmware and bios version encoding */
304 if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
305 sprintf (adapter->fw_version, "%c%d%d.%d%d",
306 adapter->product_info.fw_version[2],
307 adapter->product_info.fw_version[1] >> 8,
308 adapter->product_info.fw_version[1] & 0x0f,
309 adapter->product_info.fw_version[0] >> 8,
310 adapter->product_info.fw_version[0] & 0x0f);
311 sprintf (adapter->bios_version, "%c%d%d.%d%d",
312 adapter->product_info.bios_version[2],
313 adapter->product_info.bios_version[1] >> 8,
314 adapter->product_info.bios_version[1] & 0x0f,
315 adapter->product_info.bios_version[0] >> 8,
316 adapter->product_info.bios_version[0] & 0x0f);
317 } else {
318 memcpy(adapter->fw_version,
319 (char *)adapter->product_info.fw_version, 4);
320 adapter->fw_version[4] = 0;
322 memcpy(adapter->bios_version,
323 (char *)adapter->product_info.bios_version, 4);
325 adapter->bios_version[4] = 0;
328 printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
329 adapter->fw_version, adapter->bios_version, adapter->numldrv);
332 * Do we support extended (>10 bytes) cdbs
334 adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
335 if (adapter->support_ext_cdb)
336 printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
339 return 0;
343 * mega_runpendq()
344 * @adapter - pointer to our soft state
346 * Runs through the list of pending requests.
348 static inline void
349 mega_runpendq(adapter_t *adapter)
351 if(!list_empty(&adapter->pending_list))
352 __mega_runpendq(adapter);
356 * megaraid_queue()
357 * @scmd - Issue this scsi command
358 * @done - the callback hook into the scsi mid-layer
360 * The command queuing entry point for the mid-layer.
362 static int
363 megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
365 adapter_t *adapter;
366 scb_t *scb;
367 int busy=0;
368 unsigned long flags;
370 adapter = (adapter_t *)scmd->device->host->hostdata;
372 scmd->scsi_done = done;
376 * Allocate and build a SCB request
377 * busy flag will be set if mega_build_cmd() command could not
378 * allocate scb. We will return non-zero status in that case.
379 * NOTE: scb can be null even though certain commands completed
380 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
381 * return 0 in that case.
384 spin_lock_irqsave(&adapter->lock, flags);
385 scb = mega_build_cmd(adapter, scmd, &busy);
386 if (!scb)
387 goto out;
389 scb->state |= SCB_PENDQ;
390 list_add_tail(&scb->list, &adapter->pending_list);
393 * Check if the HBA is in quiescent state, e.g., during a
394 * delete logical drive opertion. If it is, don't run
395 * the pending_list.
397 if (atomic_read(&adapter->quiescent) == 0)
398 mega_runpendq(adapter);
400 busy = 0;
401 out:
402 spin_unlock_irqrestore(&adapter->lock, flags);
403 return busy;
407 * mega_allocate_scb()
408 * @adapter - pointer to our soft state
409 * @cmd - scsi command from the mid-layer
411 * Allocate a SCB structure. This is the central structure for controller
412 * commands.
414 static inline scb_t *
415 mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
417 struct list_head *head = &adapter->free_list;
418 scb_t *scb;
420 /* Unlink command from Free List */
421 if( !list_empty(head) ) {
423 scb = list_entry(head->next, scb_t, list);
425 list_del_init(head->next);
427 scb->state = SCB_ACTIVE;
428 scb->cmd = cmd;
429 scb->dma_type = MEGA_DMA_TYPE_NONE;
431 return scb;
434 return NULL;
438 * mega_get_ldrv_num()
439 * @adapter - pointer to our soft state
440 * @cmd - scsi mid layer command
441 * @channel - channel on the controller
443 * Calculate the logical drive number based on the information in scsi command
444 * and the channel number.
446 static inline int
447 mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
449 int tgt;
450 int ldrv_num;
452 tgt = cmd->device->id;
454 if ( tgt > adapter->this_id )
455 tgt--; /* we do not get inquires for initiator id */
457 ldrv_num = (channel * 15) + tgt;
461 * If we have a logical drive with boot enabled, project it first
463 if( adapter->boot_ldrv_enabled ) {
464 if( ldrv_num == 0 ) {
465 ldrv_num = adapter->boot_ldrv;
467 else {
468 if( ldrv_num <= adapter->boot_ldrv ) {
469 ldrv_num--;
475 * If "delete logical drive" feature is enabled on this controller.
476 * Do only if at least one delete logical drive operation was done.
478 * Also, after logical drive deletion, instead of logical drive number,
479 * the value returned should be 0x80+logical drive id.
481 * These is valid only for IO commands.
484 if (adapter->support_random_del && adapter->read_ldidmap )
485 switch (cmd->cmnd[0]) {
486 case READ_6: /* fall through */
487 case WRITE_6: /* fall through */
488 case READ_10: /* fall through */
489 case WRITE_10:
490 ldrv_num += 0x80;
493 return ldrv_num;
497 * mega_build_cmd()
498 * @adapter - pointer to our soft state
499 * @cmd - Prepare using this scsi command
500 * @busy - busy flag if no resources
502 * Prepares a command and scatter gather list for the controller. This routine
503 * also finds out if the commands is intended for a logical drive or a
504 * physical device and prepares the controller command accordingly.
506 * We also re-order the logical drives and physical devices based on their
507 * boot settings.
509 static scb_t *
510 mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
512 mega_ext_passthru *epthru;
513 mega_passthru *pthru;
514 scb_t *scb;
515 mbox_t *mbox;
516 long seg;
517 char islogical;
518 int max_ldrv_num;
519 int channel = 0;
520 int target = 0;
521 int ldrv_num = 0; /* logical drive number */
525 * filter the internal and ioctl commands
527 if((cmd->cmnd[0] == MEGA_INTERNAL_CMD))
528 return (scb_t *)cmd->host_scribble;
531 * We know what channels our logical drives are on - mega_find_card()
533 islogical = adapter->logdrv_chan[cmd->device->channel];
536 * The theory: If physical drive is chosen for boot, all the physical
537 * devices are exported before the logical drives, otherwise physical
538 * devices are pushed after logical drives, in which case - Kernel sees
539 * the physical devices on virtual channel which is obviously converted
540 * to actual channel on the HBA.
542 if( adapter->boot_pdrv_enabled ) {
543 if( islogical ) {
544 /* logical channel */
545 channel = cmd->device->channel -
546 adapter->product_info.nchannels;
548 else {
549 /* this is physical channel */
550 channel = cmd->device->channel;
551 target = cmd->device->id;
554 * boot from a physical disk, that disk needs to be
555 * exposed first IF both the channels are SCSI, then
556 * booting from the second channel is not allowed.
558 if( target == 0 ) {
559 target = adapter->boot_pdrv_tgt;
561 else if( target == adapter->boot_pdrv_tgt ) {
562 target = 0;
566 else {
567 if( islogical ) {
568 /* this is the logical channel */
569 channel = cmd->device->channel;
571 else {
572 /* physical channel */
573 channel = cmd->device->channel - NVIRT_CHAN;
574 target = cmd->device->id;
579 if(islogical) {
581 /* have just LUN 0 for each target on virtual channels */
582 if (cmd->device->lun) {
583 cmd->result = (DID_BAD_TARGET << 16);
584 cmd->scsi_done(cmd);
585 return NULL;
588 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
591 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
592 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
595 * max_ldrv_num increases by 0x80 if some logical drive was
596 * deleted.
598 if(adapter->read_ldidmap)
599 max_ldrv_num += 0x80;
601 if(ldrv_num > max_ldrv_num ) {
602 cmd->result = (DID_BAD_TARGET << 16);
603 cmd->scsi_done(cmd);
604 return NULL;
608 else {
609 if( cmd->device->lun > 7) {
611 * Do not support lun >7 for physically accessed
612 * devices
614 cmd->result = (DID_BAD_TARGET << 16);
615 cmd->scsi_done(cmd);
616 return NULL;
622 * Logical drive commands
625 if(islogical) {
626 switch (cmd->cmnd[0]) {
627 case TEST_UNIT_READY:
628 #if MEGA_HAVE_CLUSTERING
630 * Do we support clustering and is the support enabled
631 * If no, return success always
633 if( !adapter->has_cluster ) {
634 cmd->result = (DID_OK << 16);
635 cmd->scsi_done(cmd);
636 return NULL;
639 if(!(scb = mega_allocate_scb(adapter, cmd))) {
640 *busy = 1;
641 return NULL;
644 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
645 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
646 scb->raw_mbox[3] = ldrv_num;
648 scb->dma_direction = PCI_DMA_NONE;
650 return scb;
651 #else
652 cmd->result = (DID_OK << 16);
653 cmd->scsi_done(cmd);
654 return NULL;
655 #endif
657 case MODE_SENSE: {
658 char *buf;
659 struct scatterlist *sg;
661 sg = scsi_sglist(cmd);
662 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
664 memset(buf, 0, cmd->cmnd[4]);
665 kunmap_atomic(buf - sg->offset, KM_IRQ0);
667 cmd->result = (DID_OK << 16);
668 cmd->scsi_done(cmd);
669 return NULL;
672 case READ_CAPACITY:
673 case INQUIRY:
675 if(!(adapter->flag & (1L << cmd->device->channel))) {
677 printk(KERN_NOTICE
678 "scsi%d: scanning scsi channel %d ",
679 adapter->host->host_no,
680 cmd->device->channel);
681 printk("for logical drives.\n");
683 adapter->flag |= (1L << cmd->device->channel);
686 /* Allocate a SCB and initialize passthru */
687 if(!(scb = mega_allocate_scb(adapter, cmd))) {
688 *busy = 1;
689 return NULL;
691 pthru = scb->pthru;
693 mbox = (mbox_t *)scb->raw_mbox;
694 memset(mbox, 0, sizeof(scb->raw_mbox));
695 memset(pthru, 0, sizeof(mega_passthru));
697 pthru->timeout = 0;
698 pthru->ars = 1;
699 pthru->reqsenselen = 14;
700 pthru->islogical = 1;
701 pthru->logdrv = ldrv_num;
702 pthru->cdblen = cmd->cmd_len;
703 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
705 if( adapter->has_64bit_addr ) {
706 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
708 else {
709 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
712 scb->dma_direction = PCI_DMA_FROMDEVICE;
714 pthru->numsgelements = mega_build_sglist(adapter, scb,
715 &pthru->dataxferaddr, &pthru->dataxferlen);
717 mbox->m_out.xferaddr = scb->pthru_dma_addr;
719 return scb;
721 case READ_6:
722 case WRITE_6:
723 case READ_10:
724 case WRITE_10:
725 case READ_12:
726 case WRITE_12:
728 /* Allocate a SCB and initialize mailbox */
729 if(!(scb = mega_allocate_scb(adapter, cmd))) {
730 *busy = 1;
731 return NULL;
733 mbox = (mbox_t *)scb->raw_mbox;
735 memset(mbox, 0, sizeof(scb->raw_mbox));
736 mbox->m_out.logdrv = ldrv_num;
739 * A little hack: 2nd bit is zero for all scsi read
740 * commands and is set for all scsi write commands
742 if( adapter->has_64bit_addr ) {
743 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
744 MEGA_MBOXCMD_LWRITE64:
745 MEGA_MBOXCMD_LREAD64 ;
747 else {
748 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
749 MEGA_MBOXCMD_LWRITE:
750 MEGA_MBOXCMD_LREAD ;
754 * 6-byte READ(0x08) or WRITE(0x0A) cdb
756 if( cmd->cmd_len == 6 ) {
757 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
758 mbox->m_out.lba =
759 ((u32)cmd->cmnd[1] << 16) |
760 ((u32)cmd->cmnd[2] << 8) |
761 (u32)cmd->cmnd[3];
763 mbox->m_out.lba &= 0x1FFFFF;
765 #if MEGA_HAVE_STATS
767 * Take modulo 0x80, since the logical drive
768 * number increases by 0x80 when a logical
769 * drive was deleted
771 if (*cmd->cmnd == READ_6) {
772 adapter->nreads[ldrv_num%0x80]++;
773 adapter->nreadblocks[ldrv_num%0x80] +=
774 mbox->m_out.numsectors;
775 } else {
776 adapter->nwrites[ldrv_num%0x80]++;
777 adapter->nwriteblocks[ldrv_num%0x80] +=
778 mbox->m_out.numsectors;
780 #endif
784 * 10-byte READ(0x28) or WRITE(0x2A) cdb
786 if( cmd->cmd_len == 10 ) {
787 mbox->m_out.numsectors =
788 (u32)cmd->cmnd[8] |
789 ((u32)cmd->cmnd[7] << 8);
790 mbox->m_out.lba =
791 ((u32)cmd->cmnd[2] << 24) |
792 ((u32)cmd->cmnd[3] << 16) |
793 ((u32)cmd->cmnd[4] << 8) |
794 (u32)cmd->cmnd[5];
796 #if MEGA_HAVE_STATS
797 if (*cmd->cmnd == READ_10) {
798 adapter->nreads[ldrv_num%0x80]++;
799 adapter->nreadblocks[ldrv_num%0x80] +=
800 mbox->m_out.numsectors;
801 } else {
802 adapter->nwrites[ldrv_num%0x80]++;
803 adapter->nwriteblocks[ldrv_num%0x80] +=
804 mbox->m_out.numsectors;
806 #endif
810 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
812 if( cmd->cmd_len == 12 ) {
813 mbox->m_out.lba =
814 ((u32)cmd->cmnd[2] << 24) |
815 ((u32)cmd->cmnd[3] << 16) |
816 ((u32)cmd->cmnd[4] << 8) |
817 (u32)cmd->cmnd[5];
819 mbox->m_out.numsectors =
820 ((u32)cmd->cmnd[6] << 24) |
821 ((u32)cmd->cmnd[7] << 16) |
822 ((u32)cmd->cmnd[8] << 8) |
823 (u32)cmd->cmnd[9];
825 #if MEGA_HAVE_STATS
826 if (*cmd->cmnd == READ_12) {
827 adapter->nreads[ldrv_num%0x80]++;
828 adapter->nreadblocks[ldrv_num%0x80] +=
829 mbox->m_out.numsectors;
830 } else {
831 adapter->nwrites[ldrv_num%0x80]++;
832 adapter->nwriteblocks[ldrv_num%0x80] +=
833 mbox->m_out.numsectors;
835 #endif
839 * If it is a read command
841 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
842 scb->dma_direction = PCI_DMA_FROMDEVICE;
844 else {
845 scb->dma_direction = PCI_DMA_TODEVICE;
848 /* Calculate Scatter-Gather info */
849 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
850 (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
852 return scb;
854 #if MEGA_HAVE_CLUSTERING
855 case RESERVE: /* Fall through */
856 case RELEASE:
859 * Do we support clustering and is the support enabled
861 if( ! adapter->has_cluster ) {
863 cmd->result = (DID_BAD_TARGET << 16);
864 cmd->scsi_done(cmd);
865 return NULL;
868 /* Allocate a SCB and initialize mailbox */
869 if(!(scb = mega_allocate_scb(adapter, cmd))) {
870 *busy = 1;
871 return NULL;
874 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
875 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
876 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
878 scb->raw_mbox[3] = ldrv_num;
880 scb->dma_direction = PCI_DMA_NONE;
882 return scb;
883 #endif
885 default:
886 cmd->result = (DID_BAD_TARGET << 16);
887 cmd->scsi_done(cmd);
888 return NULL;
893 * Passthru drive commands
895 else {
896 /* Allocate a SCB and initialize passthru */
897 if(!(scb = mega_allocate_scb(adapter, cmd))) {
898 *busy = 1;
899 return NULL;
902 mbox = (mbox_t *)scb->raw_mbox;
903 memset(mbox, 0, sizeof(scb->raw_mbox));
905 if( adapter->support_ext_cdb ) {
907 epthru = mega_prepare_extpassthru(adapter, scb, cmd,
908 channel, target);
910 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
912 mbox->m_out.xferaddr = scb->epthru_dma_addr;
915 else {
917 pthru = mega_prepare_passthru(adapter, scb, cmd,
918 channel, target);
920 /* Initialize mailbox */
921 if( adapter->has_64bit_addr ) {
922 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
924 else {
925 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
928 mbox->m_out.xferaddr = scb->pthru_dma_addr;
931 return scb;
933 return NULL;
938 * mega_prepare_passthru()
939 * @adapter - pointer to our soft state
940 * @scb - our scsi control block
941 * @cmd - scsi command from the mid-layer
942 * @channel - actual channel on the controller
943 * @target - actual id on the controller.
945 * prepare a command for the scsi physical devices.
947 static mega_passthru *
948 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
949 int channel, int target)
951 mega_passthru *pthru;
953 pthru = scb->pthru;
954 memset(pthru, 0, sizeof (mega_passthru));
956 /* 0=6sec/1=60sec/2=10min/3=3hrs */
957 pthru->timeout = 2;
959 pthru->ars = 1;
960 pthru->reqsenselen = 14;
961 pthru->islogical = 0;
963 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
965 pthru->target = (adapter->flag & BOARD_40LD) ?
966 (channel << 4) | target : target;
968 pthru->cdblen = cmd->cmd_len;
969 pthru->logdrv = cmd->device->lun;
971 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
973 /* Not sure about the direction */
974 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
976 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
977 switch (cmd->cmnd[0]) {
978 case INQUIRY:
979 case READ_CAPACITY:
980 if(!(adapter->flag & (1L << cmd->device->channel))) {
982 printk(KERN_NOTICE
983 "scsi%d: scanning scsi channel %d [P%d] ",
984 adapter->host->host_no,
985 cmd->device->channel, channel);
986 printk("for physical devices.\n");
988 adapter->flag |= (1L << cmd->device->channel);
990 /* Fall through */
991 default:
992 pthru->numsgelements = mega_build_sglist(adapter, scb,
993 &pthru->dataxferaddr, &pthru->dataxferlen);
994 break;
996 return pthru;
1001 * mega_prepare_extpassthru()
1002 * @adapter - pointer to our soft state
1003 * @scb - our scsi control block
1004 * @cmd - scsi command from the mid-layer
1005 * @channel - actual channel on the controller
1006 * @target - actual id on the controller.
1008 * prepare a command for the scsi physical devices. This rountine prepares
1009 * commands for devices which can take extended CDBs (>10 bytes)
1011 static mega_ext_passthru *
1012 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1013 int channel, int target)
1015 mega_ext_passthru *epthru;
1017 epthru = scb->epthru;
1018 memset(epthru, 0, sizeof(mega_ext_passthru));
1020 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1021 epthru->timeout = 2;
1023 epthru->ars = 1;
1024 epthru->reqsenselen = 14;
1025 epthru->islogical = 0;
1027 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1028 epthru->target = (adapter->flag & BOARD_40LD) ?
1029 (channel << 4) | target : target;
1031 epthru->cdblen = cmd->cmd_len;
1032 epthru->logdrv = cmd->device->lun;
1034 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1036 /* Not sure about the direction */
1037 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1039 switch(cmd->cmnd[0]) {
1040 case INQUIRY:
1041 case READ_CAPACITY:
1042 if(!(adapter->flag & (1L << cmd->device->channel))) {
1044 printk(KERN_NOTICE
1045 "scsi%d: scanning scsi channel %d [P%d] ",
1046 adapter->host->host_no,
1047 cmd->device->channel, channel);
1048 printk("for physical devices.\n");
1050 adapter->flag |= (1L << cmd->device->channel);
1052 /* Fall through */
1053 default:
1054 epthru->numsgelements = mega_build_sglist(adapter, scb,
1055 &epthru->dataxferaddr, &epthru->dataxferlen);
1056 break;
1059 return epthru;
1062 static void
1063 __mega_runpendq(adapter_t *adapter)
1065 scb_t *scb;
1066 struct list_head *pos, *next;
1068 /* Issue any pending commands to the card */
1069 list_for_each_safe(pos, next, &adapter->pending_list) {
1071 scb = list_entry(pos, scb_t, list);
1073 if( !(scb->state & SCB_ISSUED) ) {
1075 if( issue_scb(adapter, scb) != 0 )
1076 return;
1080 return;
1085 * issue_scb()
1086 * @adapter - pointer to our soft state
1087 * @scb - scsi control block
1089 * Post a command to the card if the mailbox is available, otherwise return
1090 * busy. We also take the scb from the pending list if the mailbox is
1091 * available.
1093 static int
1094 issue_scb(adapter_t *adapter, scb_t *scb)
1096 volatile mbox64_t *mbox64 = adapter->mbox64;
1097 volatile mbox_t *mbox = adapter->mbox;
1098 unsigned int i = 0;
1100 if(unlikely(mbox->m_in.busy)) {
1101 do {
1102 udelay(1);
1103 i++;
1104 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1106 if(mbox->m_in.busy) return -1;
1109 /* Copy mailbox data into host structure */
1110 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1111 sizeof(struct mbox_out));
1113 mbox->m_out.cmdid = scb->idx; /* Set cmdid */
1114 mbox->m_in.busy = 1; /* Set busy */
1118 * Increment the pending queue counter
1120 atomic_inc(&adapter->pend_cmds);
1122 switch (mbox->m_out.cmd) {
1123 case MEGA_MBOXCMD_LREAD64:
1124 case MEGA_MBOXCMD_LWRITE64:
1125 case MEGA_MBOXCMD_PASSTHRU64:
1126 case MEGA_MBOXCMD_EXTPTHRU:
1127 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1128 mbox64->xfer_segment_hi = 0;
1129 mbox->m_out.xferaddr = 0xFFFFFFFF;
1130 break;
1131 default:
1132 mbox64->xfer_segment_lo = 0;
1133 mbox64->xfer_segment_hi = 0;
1137 * post the command
1139 scb->state |= SCB_ISSUED;
1141 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1142 mbox->m_in.poll = 0;
1143 mbox->m_in.ack = 0;
1144 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1146 else {
1147 irq_enable(adapter);
1148 issue_command(adapter);
1151 return 0;
1155 * Wait until the controller's mailbox is available
1157 static inline int
1158 mega_busywait_mbox (adapter_t *adapter)
1160 if (adapter->mbox->m_in.busy)
1161 return __mega_busywait_mbox(adapter);
1162 return 0;
1166 * issue_scb_block()
1167 * @adapter - pointer to our soft state
1168 * @raw_mbox - the mailbox
1170 * Issue a scb in synchronous and non-interrupt mode
1172 static int
1173 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1175 volatile mbox64_t *mbox64 = adapter->mbox64;
1176 volatile mbox_t *mbox = adapter->mbox;
1177 u8 byte;
1179 /* Wait until mailbox is free */
1180 if(mega_busywait_mbox (adapter))
1181 goto bug_blocked_mailbox;
1183 /* Copy mailbox data into host structure */
1184 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1185 mbox->m_out.cmdid = 0xFE;
1186 mbox->m_in.busy = 1;
1188 switch (raw_mbox[0]) {
1189 case MEGA_MBOXCMD_LREAD64:
1190 case MEGA_MBOXCMD_LWRITE64:
1191 case MEGA_MBOXCMD_PASSTHRU64:
1192 case MEGA_MBOXCMD_EXTPTHRU:
1193 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1194 mbox64->xfer_segment_hi = 0;
1195 mbox->m_out.xferaddr = 0xFFFFFFFF;
1196 break;
1197 default:
1198 mbox64->xfer_segment_lo = 0;
1199 mbox64->xfer_segment_hi = 0;
1202 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1203 mbox->m_in.poll = 0;
1204 mbox->m_in.ack = 0;
1205 mbox->m_in.numstatus = 0xFF;
1206 mbox->m_in.status = 0xFF;
1207 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1209 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1210 cpu_relax();
1212 mbox->m_in.numstatus = 0xFF;
1214 while( (volatile u8)mbox->m_in.poll != 0x77 )
1215 cpu_relax();
1217 mbox->m_in.poll = 0;
1218 mbox->m_in.ack = 0x77;
1220 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1222 while(RDINDOOR(adapter) & 0x2)
1223 cpu_relax();
1225 else {
1226 irq_disable(adapter);
1227 issue_command(adapter);
1229 while (!((byte = irq_state(adapter)) & INTR_VALID))
1230 cpu_relax();
1232 set_irq_state(adapter, byte);
1233 irq_enable(adapter);
1234 irq_ack(adapter);
1237 return mbox->m_in.status;
1239 bug_blocked_mailbox:
1240 printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1241 udelay (1000);
1242 return -1;
1247 * megaraid_isr_iomapped()
1248 * @irq - irq
1249 * @devp - pointer to our soft state
1251 * Interrupt service routine for io-mapped controllers.
1252 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1253 * and service the completed commands.
1255 static irqreturn_t
1256 megaraid_isr_iomapped(int irq, void *devp)
1258 adapter_t *adapter = devp;
1259 unsigned long flags;
1260 u8 status;
1261 u8 nstatus;
1262 u8 completed[MAX_FIRMWARE_STATUS];
1263 u8 byte;
1264 int handled = 0;
1268 * loop till F/W has more commands for us to complete.
1270 spin_lock_irqsave(&adapter->lock, flags);
1272 do {
1273 /* Check if a valid interrupt is pending */
1274 byte = irq_state(adapter);
1275 if( (byte & VALID_INTR_BYTE) == 0 ) {
1277 * No more pending commands
1279 goto out_unlock;
1281 set_irq_state(adapter, byte);
1283 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1284 == 0xFF)
1285 cpu_relax();
1286 adapter->mbox->m_in.numstatus = 0xFF;
1288 status = adapter->mbox->m_in.status;
1291 * decrement the pending queue counter
1293 atomic_sub(nstatus, &adapter->pend_cmds);
1295 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1296 nstatus);
1298 /* Acknowledge interrupt */
1299 irq_ack(adapter);
1301 mega_cmd_done(adapter, completed, nstatus, status);
1303 mega_rundoneq(adapter);
1305 handled = 1;
1307 /* Loop through any pending requests */
1308 if(atomic_read(&adapter->quiescent) == 0) {
1309 mega_runpendq(adapter);
1312 } while(1);
1314 out_unlock:
1316 spin_unlock_irqrestore(&adapter->lock, flags);
1318 return IRQ_RETVAL(handled);
1323 * megaraid_isr_memmapped()
1324 * @irq - irq
1325 * @devp - pointer to our soft state
1327 * Interrupt service routine for memory-mapped controllers.
1328 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1329 * and service the completed commands.
1331 static irqreturn_t
1332 megaraid_isr_memmapped(int irq, void *devp)
1334 adapter_t *adapter = devp;
1335 unsigned long flags;
1336 u8 status;
1337 u32 dword = 0;
1338 u8 nstatus;
1339 u8 completed[MAX_FIRMWARE_STATUS];
1340 int handled = 0;
1344 * loop till F/W has more commands for us to complete.
1346 spin_lock_irqsave(&adapter->lock, flags);
1348 do {
1349 /* Check if a valid interrupt is pending */
1350 dword = RDOUTDOOR(adapter);
1351 if(dword != 0x10001234) {
1353 * No more pending commands
1355 goto out_unlock;
1357 WROUTDOOR(adapter, 0x10001234);
1359 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1360 == 0xFF) {
1361 cpu_relax();
1363 adapter->mbox->m_in.numstatus = 0xFF;
1365 status = adapter->mbox->m_in.status;
1368 * decrement the pending queue counter
1370 atomic_sub(nstatus, &adapter->pend_cmds);
1372 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1373 nstatus);
1375 /* Acknowledge interrupt */
1376 WRINDOOR(adapter, 0x2);
1378 handled = 1;
1380 while( RDINDOOR(adapter) & 0x02 )
1381 cpu_relax();
1383 mega_cmd_done(adapter, completed, nstatus, status);
1385 mega_rundoneq(adapter);
1387 /* Loop through any pending requests */
1388 if(atomic_read(&adapter->quiescent) == 0) {
1389 mega_runpendq(adapter);
1392 } while(1);
1394 out_unlock:
1396 spin_unlock_irqrestore(&adapter->lock, flags);
1398 return IRQ_RETVAL(handled);
1401 * mega_cmd_done()
1402 * @adapter - pointer to our soft state
1403 * @completed - array of ids of completed commands
1404 * @nstatus - number of completed commands
1405 * @status - status of the last command completed
1407 * Complete the comamnds and call the scsi mid-layer callback hooks.
1409 static void
1410 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1412 mega_ext_passthru *epthru = NULL;
1413 struct scatterlist *sgl;
1414 Scsi_Cmnd *cmd = NULL;
1415 mega_passthru *pthru = NULL;
1416 mbox_t *mbox = NULL;
1417 u8 c;
1418 scb_t *scb;
1419 int islogical;
1420 int cmdid;
1421 int i;
1424 * for all the commands completed, call the mid-layer callback routine
1425 * and free the scb.
1427 for( i = 0; i < nstatus; i++ ) {
1429 cmdid = completed[i];
1431 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1432 scb = &adapter->int_scb;
1433 cmd = scb->cmd;
1434 mbox = (mbox_t *)scb->raw_mbox;
1437 * Internal command interface do not fire the extended
1438 * passthru or 64-bit passthru
1440 pthru = scb->pthru;
1443 else {
1444 scb = &adapter->scb_list[cmdid];
1447 * Make sure f/w has completed a valid command
1449 if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1450 printk(KERN_CRIT
1451 "megaraid: invalid command ");
1452 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1453 cmdid, scb->state, scb->cmd);
1455 continue;
1459 * Was a abort issued for this command
1461 if( scb->state & SCB_ABORT ) {
1463 printk(KERN_WARNING
1464 "megaraid: aborted cmd %lx[%x] complete.\n",
1465 scb->cmd->serial_number, scb->idx);
1467 scb->cmd->result = (DID_ABORT << 16);
1469 list_add_tail(SCSI_LIST(scb->cmd),
1470 &adapter->completed_list);
1472 mega_free_scb(adapter, scb);
1474 continue;
1478 * Was a reset issued for this command
1480 if( scb->state & SCB_RESET ) {
1482 printk(KERN_WARNING
1483 "megaraid: reset cmd %lx[%x] complete.\n",
1484 scb->cmd->serial_number, scb->idx);
1486 scb->cmd->result = (DID_RESET << 16);
1488 list_add_tail(SCSI_LIST(scb->cmd),
1489 &adapter->completed_list);
1491 mega_free_scb (adapter, scb);
1493 continue;
1496 cmd = scb->cmd;
1497 pthru = scb->pthru;
1498 epthru = scb->epthru;
1499 mbox = (mbox_t *)scb->raw_mbox;
1501 #if MEGA_HAVE_STATS
1504 int logdrv = mbox->m_out.logdrv;
1506 islogical = adapter->logdrv_chan[cmd->channel];
1508 * Maintain an error counter for the logical drive.
1509 * Some application like SNMP agent need such
1510 * statistics
1512 if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1513 cmd->cmnd[0] == READ_10 ||
1514 cmd->cmnd[0] == READ_12)) {
1516 * Logical drive number increases by 0x80 when
1517 * a logical drive is deleted
1519 adapter->rd_errors[logdrv%0x80]++;
1522 if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1523 cmd->cmnd[0] == WRITE_10 ||
1524 cmd->cmnd[0] == WRITE_12)) {
1526 * Logical drive number increases by 0x80 when
1527 * a logical drive is deleted
1529 adapter->wr_errors[logdrv%0x80]++;
1533 #endif
1537 * Do not return the presence of hard disk on the channel so,
1538 * inquiry sent, and returned data==hard disk or removable
1539 * hard disk and not logical, request should return failure! -
1540 * PJ
1542 islogical = adapter->logdrv_chan[cmd->device->channel];
1543 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1545 sgl = scsi_sglist(cmd);
1546 if( sg_page(sgl) ) {
1547 c = *(unsigned char *) sg_virt(&sgl[0]);
1548 } else {
1549 printk(KERN_WARNING
1550 "megaraid: invalid sg.\n");
1551 c = 0;
1554 if(IS_RAID_CH(adapter, cmd->device->channel) &&
1555 ((c & 0x1F ) == TYPE_DISK)) {
1556 status = 0xF0;
1560 /* clear result; otherwise, success returns corrupt value */
1561 cmd->result = 0;
1563 /* Convert MegaRAID status to Linux error code */
1564 switch (status) {
1565 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1566 cmd->result |= (DID_OK << 16);
1567 break;
1569 case 0x02: /* ERROR_ABORTED, i.e.
1570 SCSI_STATUS_CHECK_CONDITION */
1572 /* set sense_buffer and result fields */
1573 if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1574 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1576 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1577 14);
1579 cmd->result = (DRIVER_SENSE << 24) |
1580 (DID_OK << 16) |
1581 (CHECK_CONDITION << 1);
1583 else {
1584 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1586 memcpy(cmd->sense_buffer,
1587 epthru->reqsensearea, 14);
1589 cmd->result = (DRIVER_SENSE << 24) |
1590 (DID_OK << 16) |
1591 (CHECK_CONDITION << 1);
1592 } else {
1593 cmd->sense_buffer[0] = 0x70;
1594 cmd->sense_buffer[2] = ABORTED_COMMAND;
1595 cmd->result |= (CHECK_CONDITION << 1);
1598 break;
1600 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1601 SCSI_STATUS_BUSY */
1602 cmd->result |= (DID_BUS_BUSY << 16) | status;
1603 break;
1605 default:
1606 #if MEGA_HAVE_CLUSTERING
1608 * If TEST_UNIT_READY fails, we know
1609 * MEGA_RESERVATION_STATUS failed
1611 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1612 cmd->result |= (DID_ERROR << 16) |
1613 (RESERVATION_CONFLICT << 1);
1615 else
1617 * Error code returned is 1 if Reserve or Release
1618 * failed or the input parameter is invalid
1620 if( status == 1 &&
1621 (cmd->cmnd[0] == RESERVE ||
1622 cmd->cmnd[0] == RELEASE) ) {
1624 cmd->result |= (DID_ERROR << 16) |
1625 (RESERVATION_CONFLICT << 1);
1627 else
1628 #endif
1629 cmd->result |= (DID_BAD_TARGET << 16)|status;
1633 * Only free SCBs for the commands coming down from the
1634 * mid-layer, not for which were issued internally
1636 * For internal command, restore the status returned by the
1637 * firmware so that user can interpret it.
1639 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1640 cmd->result = status;
1643 * Remove the internal command from the pending list
1645 list_del_init(&scb->list);
1646 scb->state = SCB_FREE;
1648 else {
1649 mega_free_scb(adapter, scb);
1652 /* Add Scsi_Command to end of completed queue */
1653 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1659 * mega_runpendq()
1661 * Run through the list of completed requests and finish it
1663 static void
1664 mega_rundoneq (adapter_t *adapter)
1666 Scsi_Cmnd *cmd;
1667 struct list_head *pos;
1669 list_for_each(pos, &adapter->completed_list) {
1671 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1673 cmd = list_entry(spos, Scsi_Cmnd, SCp);
1674 cmd->scsi_done(cmd);
1677 INIT_LIST_HEAD(&adapter->completed_list);
1682 * Free a SCB structure
1683 * Note: We assume the scsi commands associated with this scb is not free yet.
1685 static void
1686 mega_free_scb(adapter_t *adapter, scb_t *scb)
1688 switch( scb->dma_type ) {
1690 case MEGA_DMA_TYPE_NONE:
1691 break;
1693 case MEGA_SGLIST:
1694 scsi_dma_unmap(scb->cmd);
1695 break;
1696 default:
1697 break;
1701 * Remove from the pending list
1703 list_del_init(&scb->list);
1705 /* Link the scb back into free list */
1706 scb->state = SCB_FREE;
1707 scb->cmd = NULL;
1709 list_add(&scb->list, &adapter->free_list);
1713 static int
1714 __mega_busywait_mbox (adapter_t *adapter)
1716 volatile mbox_t *mbox = adapter->mbox;
1717 long counter;
1719 for (counter = 0; counter < 10000; counter++) {
1720 if (!mbox->m_in.busy)
1721 return 0;
1722 udelay(100);
1723 cond_resched();
1725 return -1; /* give up after 1 second */
1729 * Copies data to SGLIST
1730 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1732 static int
1733 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1735 struct scatterlist *sg;
1736 Scsi_Cmnd *cmd;
1737 int sgcnt;
1738 int idx;
1740 cmd = scb->cmd;
1743 * Copy Scatter-Gather list info into controller structure.
1745 * The number of sg elements returned must not exceed our limit
1747 sgcnt = scsi_dma_map(cmd);
1749 scb->dma_type = MEGA_SGLIST;
1751 BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1753 *len = 0;
1755 if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1756 sg = scsi_sglist(cmd);
1757 scb->dma_h_bulkdata = sg_dma_address(sg);
1758 *buf = (u32)scb->dma_h_bulkdata;
1759 *len = sg_dma_len(sg);
1760 return 0;
1763 scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1764 if (adapter->has_64bit_addr) {
1765 scb->sgl64[idx].address = sg_dma_address(sg);
1766 *len += scb->sgl64[idx].length = sg_dma_len(sg);
1767 } else {
1768 scb->sgl[idx].address = sg_dma_address(sg);
1769 *len += scb->sgl[idx].length = sg_dma_len(sg);
1773 /* Reset pointer and length fields */
1774 *buf = scb->sgl_dma_addr;
1776 /* Return count of SG requests */
1777 return sgcnt;
1782 * mega_8_to_40ld()
1784 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1785 * Enquiry3 structures for later use
1787 static void
1788 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1789 mega_product_info *product_info)
1791 int i;
1793 product_info->max_commands = inquiry->adapter_info.max_commands;
1794 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1795 product_info->nchannels = inquiry->adapter_info.nchannels;
1797 for (i = 0; i < 4; i++) {
1798 product_info->fw_version[i] =
1799 inquiry->adapter_info.fw_version[i];
1801 product_info->bios_version[i] =
1802 inquiry->adapter_info.bios_version[i];
1804 enquiry3->cache_flush_interval =
1805 inquiry->adapter_info.cache_flush_interval;
1807 product_info->dram_size = inquiry->adapter_info.dram_size;
1809 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1811 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1812 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1813 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1814 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1817 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1818 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1821 static inline void
1822 mega_free_sgl(adapter_t *adapter)
1824 scb_t *scb;
1825 int i;
1827 for(i = 0; i < adapter->max_cmds; i++) {
1829 scb = &adapter->scb_list[i];
1831 if( scb->sgl64 ) {
1832 pci_free_consistent(adapter->dev,
1833 sizeof(mega_sgl64) * adapter->sglen,
1834 scb->sgl64,
1835 scb->sgl_dma_addr);
1837 scb->sgl64 = NULL;
1840 if( scb->pthru ) {
1841 pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1842 scb->pthru, scb->pthru_dma_addr);
1844 scb->pthru = NULL;
1847 if( scb->epthru ) {
1848 pci_free_consistent(adapter->dev,
1849 sizeof(mega_ext_passthru),
1850 scb->epthru, scb->epthru_dma_addr);
1852 scb->epthru = NULL;
1860 * Get information about the card/driver
1862 const char *
1863 megaraid_info(struct Scsi_Host *host)
1865 static char buffer[512];
1866 adapter_t *adapter;
1868 adapter = (adapter_t *)host->hostdata;
1870 sprintf (buffer,
1871 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1872 adapter->fw_version, adapter->product_info.max_commands,
1873 adapter->host->max_id, adapter->host->max_channel,
1874 adapter->host->max_lun);
1875 return buffer;
1879 * Abort a previous SCSI request. Only commands on the pending list can be
1880 * aborted. All the commands issued to the F/W must complete.
1882 static int
1883 megaraid_abort(Scsi_Cmnd *cmd)
1885 adapter_t *adapter;
1886 int rval;
1888 adapter = (adapter_t *)cmd->device->host->hostdata;
1890 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1893 * This is required here to complete any completed requests
1894 * to be communicated over to the mid layer.
1896 mega_rundoneq(adapter);
1898 return rval;
1902 static int
1903 megaraid_reset(struct scsi_cmnd *cmd)
1905 adapter_t *adapter;
1906 megacmd_t mc;
1907 int rval;
1909 adapter = (adapter_t *)cmd->device->host->hostdata;
1911 #if MEGA_HAVE_CLUSTERING
1912 mc.cmd = MEGA_CLUSTER_CMD;
1913 mc.opcode = MEGA_RESET_RESERVATIONS;
1915 if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1916 printk(KERN_WARNING
1917 "megaraid: reservation reset failed.\n");
1919 else {
1920 printk(KERN_INFO "megaraid: reservation reset.\n");
1922 #endif
1924 spin_lock_irq(&adapter->lock);
1926 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1929 * This is required here to complete any completed requests
1930 * to be communicated over to the mid layer.
1932 mega_rundoneq(adapter);
1933 spin_unlock_irq(&adapter->lock);
1935 return rval;
1939 * megaraid_abort_and_reset()
1940 * @adapter - megaraid soft state
1941 * @cmd - scsi command to be aborted or reset
1942 * @aor - abort or reset flag
1944 * Try to locate the scsi command in the pending queue. If found and is not
1945 * issued to the controller, abort/reset it. Otherwise return failure
1947 static int
1948 megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
1950 struct list_head *pos, *next;
1951 scb_t *scb;
1953 printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
1954 (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
1955 cmd->cmnd[0], cmd->device->channel,
1956 cmd->device->id, cmd->device->lun);
1958 if(list_empty(&adapter->pending_list))
1959 return FALSE;
1961 list_for_each_safe(pos, next, &adapter->pending_list) {
1963 scb = list_entry(pos, scb_t, list);
1965 if (scb->cmd == cmd) { /* Found command */
1967 scb->state |= aor;
1970 * Check if this command has firmware ownership. If
1971 * yes, we cannot reset this command. Whenever f/w
1972 * completes this command, we will return appropriate
1973 * status from ISR.
1975 if( scb->state & SCB_ISSUED ) {
1977 printk(KERN_WARNING
1978 "megaraid: %s-%lx[%x], fw owner.\n",
1979 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1980 cmd->serial_number, scb->idx);
1982 return FALSE;
1984 else {
1987 * Not yet issued! Remove from the pending
1988 * list
1990 printk(KERN_WARNING
1991 "megaraid: %s-%lx[%x], driver owner.\n",
1992 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1993 cmd->serial_number, scb->idx);
1995 mega_free_scb(adapter, scb);
1997 if( aor == SCB_ABORT ) {
1998 cmd->result = (DID_ABORT << 16);
2000 else {
2001 cmd->result = (DID_RESET << 16);
2004 list_add_tail(SCSI_LIST(cmd),
2005 &adapter->completed_list);
2007 return TRUE;
2012 return FALSE;
2015 static inline int
2016 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2018 *pdev = alloc_pci_dev();
2020 if( *pdev == NULL ) return -1;
2022 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2024 if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
2025 kfree(*pdev);
2026 return -1;
2029 return 0;
2032 static inline void
2033 free_local_pdev(struct pci_dev *pdev)
2035 kfree(pdev);
2039 * mega_allocate_inquiry()
2040 * @dma_handle - handle returned for dma address
2041 * @pdev - handle to pci device
2043 * allocates memory for inquiry structure
2045 static inline void *
2046 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2048 return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2052 static inline void
2053 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2055 pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2059 #ifdef CONFIG_PROC_FS
2060 /* Following code handles /proc fs */
2062 #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
2063 S_IRUSR | S_IFREG, \
2064 controller_proc_dir_entry, \
2065 func, adapter)
2068 * mega_create_proc_entry()
2069 * @index - index in soft state array
2070 * @parent - parent node for this /proc entry
2072 * Creates /proc entries for our controllers.
2074 static void
2075 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2077 struct proc_dir_entry *controller_proc_dir_entry = NULL;
2078 u8 string[64] = { 0 };
2079 adapter_t *adapter = hba_soft_state[index];
2081 sprintf(string, "hba%d", adapter->host->host_no);
2083 controller_proc_dir_entry =
2084 adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
2086 if(!controller_proc_dir_entry) {
2087 printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2088 return;
2090 adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
2091 adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
2092 adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
2093 #if MEGA_HAVE_ENH_PROC
2094 adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
2095 adapter->proc_battery = CREATE_READ_PROC("battery-status",
2096 proc_battery);
2099 * Display each physical drive on its channel
2101 adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
2102 proc_pdrv_ch0);
2103 adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
2104 proc_pdrv_ch1);
2105 adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
2106 proc_pdrv_ch2);
2107 adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
2108 proc_pdrv_ch3);
2111 * Display a set of up to 10 logical drive through each of following
2112 * /proc entries
2114 adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
2115 proc_rdrv_10);
2116 adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
2117 proc_rdrv_20);
2118 adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
2119 proc_rdrv_30);
2120 adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
2121 proc_rdrv_40);
2122 #endif
2127 * proc_read_config()
2128 * @page - buffer to write the data in
2129 * @start - where the actual data has been written in page
2130 * @offset - same meaning as the read system call
2131 * @count - same meaning as the read system call
2132 * @eof - set if no more data needs to be returned
2133 * @data - pointer to our soft state
2135 * Display configuration information about the controller.
2137 static int
2138 proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
2139 void *data)
2142 adapter_t *adapter = (adapter_t *)data;
2143 int len = 0;
2145 len += sprintf(page+len, "%s", MEGARAID_VERSION);
2147 if(adapter->product_info.product_name[0])
2148 len += sprintf(page+len, "%s\n",
2149 adapter->product_info.product_name);
2151 len += sprintf(page+len, "Controller Type: ");
2153 if( adapter->flag & BOARD_MEMMAP ) {
2154 len += sprintf(page+len,
2155 "438/466/467/471/493/518/520/531/532\n");
2157 else {
2158 len += sprintf(page+len,
2159 "418/428/434\n");
2162 if(adapter->flag & BOARD_40LD) {
2163 len += sprintf(page+len,
2164 "Controller Supports 40 Logical Drives\n");
2167 if(adapter->flag & BOARD_64BIT) {
2168 len += sprintf(page+len,
2169 "Controller capable of 64-bit memory addressing\n");
2171 if( adapter->has_64bit_addr ) {
2172 len += sprintf(page+len,
2173 "Controller using 64-bit memory addressing\n");
2175 else {
2176 len += sprintf(page+len,
2177 "Controller is not using 64-bit memory addressing\n");
2180 len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
2181 adapter->host->irq);
2183 len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
2184 adapter->numldrv, adapter->product_info.nchannels);
2186 len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
2187 adapter->fw_version, adapter->bios_version,
2188 adapter->product_info.dram_size);
2190 len += sprintf(page+len,
2191 "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2192 adapter->product_info.max_commands, adapter->max_cmds);
2194 len += sprintf(page+len, "support_ext_cdb = %d\n",
2195 adapter->support_ext_cdb);
2196 len += sprintf(page+len, "support_random_del = %d\n",
2197 adapter->support_random_del);
2198 len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
2199 adapter->boot_ldrv_enabled);
2200 len += sprintf(page+len, "boot_ldrv = %d\n",
2201 adapter->boot_ldrv);
2202 len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
2203 adapter->boot_pdrv_enabled);
2204 len += sprintf(page+len, "boot_pdrv_ch = %d\n",
2205 adapter->boot_pdrv_ch);
2206 len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
2207 adapter->boot_pdrv_tgt);
2208 len += sprintf(page+len, "quiescent = %d\n",
2209 atomic_read(&adapter->quiescent));
2210 len += sprintf(page+len, "has_cluster = %d\n",
2211 adapter->has_cluster);
2213 len += sprintf(page+len, "\nModule Parameters:\n");
2214 len += sprintf(page+len, "max_cmd_per_lun = %d\n",
2215 max_cmd_per_lun);
2216 len += sprintf(page+len, "max_sectors_per_io = %d\n",
2217 max_sectors_per_io);
2219 *eof = 1;
2221 return len;
2227 * proc_read_stat()
2228 * @page - buffer to write the data in
2229 * @start - where the actual data has been written in page
2230 * @offset - same meaning as the read system call
2231 * @count - same meaning as the read system call
2232 * @eof - set if no more data needs to be returned
2233 * @data - pointer to our soft state
2235 * Diaplay statistical information about the I/O activity.
2237 static int
2238 proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
2239 void *data)
2241 adapter_t *adapter;
2242 int len;
2243 int i;
2245 i = 0; /* avoid compilation warnings */
2246 len = 0;
2247 adapter = (adapter_t *)data;
2249 len = sprintf(page, "Statistical Information for this controller\n");
2250 len += sprintf(page+len, "pend_cmds = %d\n",
2251 atomic_read(&adapter->pend_cmds));
2252 #if MEGA_HAVE_STATS
2253 for(i = 0; i < adapter->numldrv; i++) {
2254 len += sprintf(page+len, "Logical Drive %d:\n", i);
2256 len += sprintf(page+len,
2257 "\tReads Issued = %lu, Writes Issued = %lu\n",
2258 adapter->nreads[i], adapter->nwrites[i]);
2260 len += sprintf(page+len,
2261 "\tSectors Read = %lu, Sectors Written = %lu\n",
2262 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2264 len += sprintf(page+len,
2265 "\tRead errors = %lu, Write errors = %lu\n\n",
2266 adapter->rd_errors[i], adapter->wr_errors[i]);
2268 #else
2269 len += sprintf(page+len,
2270 "IO and error counters not compiled in driver.\n");
2271 #endif
2273 *eof = 1;
2275 return len;
2280 * proc_read_mbox()
2281 * @page - buffer to write the data in
2282 * @start - where the actual data has been written in page
2283 * @offset - same meaning as the read system call
2284 * @count - same meaning as the read system call
2285 * @eof - set if no more data needs to be returned
2286 * @data - pointer to our soft state
2288 * Display mailbox information for the last command issued. This information
2289 * is good for debugging.
2291 static int
2292 proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
2293 void *data)
2296 adapter_t *adapter = (adapter_t *)data;
2297 volatile mbox_t *mbox = adapter->mbox;
2298 int len = 0;
2300 len = sprintf(page, "Contents of Mail Box Structure\n");
2301 len += sprintf(page+len, " Fw Command = 0x%02x\n",
2302 mbox->m_out.cmd);
2303 len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
2304 mbox->m_out.cmdid);
2305 len += sprintf(page+len, " No of Sectors= %04d\n",
2306 mbox->m_out.numsectors);
2307 len += sprintf(page+len, " LBA = 0x%02x\n",
2308 mbox->m_out.lba);
2309 len += sprintf(page+len, " DTA = 0x%08x\n",
2310 mbox->m_out.xferaddr);
2311 len += sprintf(page+len, " Logical Drive= 0x%02x\n",
2312 mbox->m_out.logdrv);
2313 len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
2314 mbox->m_out.numsgelements);
2315 len += sprintf(page+len, " Busy = %01x\n",
2316 mbox->m_in.busy);
2317 len += sprintf(page+len, " Status = 0x%02x\n",
2318 mbox->m_in.status);
2320 *eof = 1;
2322 return len;
2327 * proc_rebuild_rate()
2328 * @page - buffer to write the data in
2329 * @start - where the actual data has been written in page
2330 * @offset - same meaning as the read system call
2331 * @count - same meaning as the read system call
2332 * @eof - set if no more data needs to be returned
2333 * @data - pointer to our soft state
2335 * Display current rebuild rate
2337 static int
2338 proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
2339 void *data)
2341 adapter_t *adapter = (adapter_t *)data;
2342 dma_addr_t dma_handle;
2343 caddr_t inquiry;
2344 struct pci_dev *pdev;
2345 int len = 0;
2347 if( make_local_pdev(adapter, &pdev) != 0 ) {
2348 *eof = 1;
2349 return len;
2352 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2353 free_local_pdev(pdev);
2354 *eof = 1;
2355 return len;
2358 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2360 len = sprintf(page, "Adapter inquiry failed.\n");
2362 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2364 mega_free_inquiry(inquiry, dma_handle, pdev);
2366 free_local_pdev(pdev);
2368 *eof = 1;
2370 return len;
2373 if( adapter->flag & BOARD_40LD ) {
2374 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2375 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2377 else {
2378 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2379 ((mraid_ext_inquiry *)
2380 inquiry)->raid_inq.adapter_info.rebuild_rate);
2384 mega_free_inquiry(inquiry, dma_handle, pdev);
2386 free_local_pdev(pdev);
2388 *eof = 1;
2390 return len;
2395 * proc_battery()
2396 * @page - buffer to write the data in
2397 * @start - where the actual data has been written in page
2398 * @offset - same meaning as the read system call
2399 * @count - same meaning as the read system call
2400 * @eof - set if no more data needs to be returned
2401 * @data - pointer to our soft state
2403 * Display information about the battery module on the controller.
2405 static int
2406 proc_battery(char *page, char **start, off_t offset, int count, int *eof,
2407 void *data)
2409 adapter_t *adapter = (adapter_t *)data;
2410 dma_addr_t dma_handle;
2411 caddr_t inquiry;
2412 struct pci_dev *pdev;
2413 u8 battery_status = 0;
2414 char str[256];
2415 int len = 0;
2417 if( make_local_pdev(adapter, &pdev) != 0 ) {
2418 *eof = 1;
2419 return len;
2422 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2423 free_local_pdev(pdev);
2424 *eof = 1;
2425 return len;
2428 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2430 len = sprintf(page, "Adapter inquiry failed.\n");
2432 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2434 mega_free_inquiry(inquiry, dma_handle, pdev);
2436 free_local_pdev(pdev);
2438 *eof = 1;
2440 return len;
2443 if( adapter->flag & BOARD_40LD ) {
2444 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2446 else {
2447 battery_status = ((mraid_ext_inquiry *)inquiry)->
2448 raid_inq.adapter_info.battery_status;
2452 * Decode the battery status
2454 sprintf(str, "Battery Status:[%d]", battery_status);
2456 if(battery_status == MEGA_BATT_CHARGE_DONE)
2457 strcat(str, " Charge Done");
2459 if(battery_status & MEGA_BATT_MODULE_MISSING)
2460 strcat(str, " Module Missing");
2462 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2463 strcat(str, " Low Voltage");
2465 if(battery_status & MEGA_BATT_TEMP_HIGH)
2466 strcat(str, " Temperature High");
2468 if(battery_status & MEGA_BATT_PACK_MISSING)
2469 strcat(str, " Pack Missing");
2471 if(battery_status & MEGA_BATT_CHARGE_INPROG)
2472 strcat(str, " Charge In-progress");
2474 if(battery_status & MEGA_BATT_CHARGE_FAIL)
2475 strcat(str, " Charge Fail");
2477 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2478 strcat(str, " Cycles Exceeded");
2480 len = sprintf(page, "%s\n", str);
2483 mega_free_inquiry(inquiry, dma_handle, pdev);
2485 free_local_pdev(pdev);
2487 *eof = 1;
2489 return len;
2494 * proc_pdrv_ch0()
2495 * @page - buffer to write the data in
2496 * @start - where the actual data has been written in page
2497 * @offset - same meaning as the read system call
2498 * @count - same meaning as the read system call
2499 * @eof - set if no more data needs to be returned
2500 * @data - pointer to our soft state
2502 * Display information about the physical drives on physical channel 0.
2504 static int
2505 proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
2506 void *data)
2508 adapter_t *adapter = (adapter_t *)data;
2510 *eof = 1;
2512 return (proc_pdrv(adapter, page, 0));
2517 * proc_pdrv_ch1()
2518 * @page - buffer to write the data in
2519 * @start - where the actual data has been written in page
2520 * @offset - same meaning as the read system call
2521 * @count - same meaning as the read system call
2522 * @eof - set if no more data needs to be returned
2523 * @data - pointer to our soft state
2525 * Display information about the physical drives on physical channel 1.
2527 static int
2528 proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
2529 void *data)
2531 adapter_t *adapter = (adapter_t *)data;
2533 *eof = 1;
2535 return (proc_pdrv(adapter, page, 1));
2540 * proc_pdrv_ch2()
2541 * @page - buffer to write the data in
2542 * @start - where the actual data has been written in page
2543 * @offset - same meaning as the read system call
2544 * @count - same meaning as the read system call
2545 * @eof - set if no more data needs to be returned
2546 * @data - pointer to our soft state
2548 * Display information about the physical drives on physical channel 2.
2550 static int
2551 proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
2552 void *data)
2554 adapter_t *adapter = (adapter_t *)data;
2556 *eof = 1;
2558 return (proc_pdrv(adapter, page, 2));
2563 * proc_pdrv_ch3()
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 3.
2573 static int
2574 proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
2575 void *data)
2577 adapter_t *adapter = (adapter_t *)data;
2579 *eof = 1;
2581 return (proc_pdrv(adapter, page, 3));
2586 * proc_pdrv()
2587 * @page - buffer to write the data in
2588 * @adapter - pointer to our soft state
2590 * Display information about the physical drives.
2592 static int
2593 proc_pdrv(adapter_t *adapter, char *page, int channel)
2595 dma_addr_t dma_handle;
2596 char *scsi_inq;
2597 dma_addr_t scsi_inq_dma_handle;
2598 caddr_t inquiry;
2599 struct pci_dev *pdev;
2600 u8 *pdrv_state;
2601 u8 state;
2602 int tgt;
2603 int max_channels;
2604 int len = 0;
2605 char str[80];
2606 int i;
2608 if( make_local_pdev(adapter, &pdev) != 0 ) {
2609 return len;
2612 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2613 goto free_pdev;
2616 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2617 len = sprintf(page, "Adapter inquiry failed.\n");
2619 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2621 goto free_inquiry;
2625 scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2627 if( scsi_inq == NULL ) {
2628 len = sprintf(page, "memory not available for scsi inq.\n");
2630 goto free_inquiry;
2633 if( adapter->flag & BOARD_40LD ) {
2634 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2636 else {
2637 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2638 raid_inq.pdrv_info.pdrv_state;
2641 max_channels = adapter->product_info.nchannels;
2643 if( channel >= max_channels ) {
2644 goto free_pci;
2647 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2649 i = channel*16 + tgt;
2651 state = *(pdrv_state + i);
2653 switch( state & 0x0F ) {
2655 case PDRV_ONLINE:
2656 sprintf(str,
2657 "Channel:%2d Id:%2d State: Online",
2658 channel, tgt);
2659 break;
2661 case PDRV_FAILED:
2662 sprintf(str,
2663 "Channel:%2d Id:%2d State: Failed",
2664 channel, tgt);
2665 break;
2667 case PDRV_RBLD:
2668 sprintf(str,
2669 "Channel:%2d Id:%2d State: Rebuild",
2670 channel, tgt);
2671 break;
2673 case PDRV_HOTSPARE:
2674 sprintf(str,
2675 "Channel:%2d Id:%2d State: Hot spare",
2676 channel, tgt);
2677 break;
2679 default:
2680 sprintf(str,
2681 "Channel:%2d Id:%2d State: Un-configured",
2682 channel, tgt);
2683 break;
2688 * This interface displays inquiries for disk drives
2689 * only. Inquries for logical drives and non-disk
2690 * devices are available through /proc/scsi/scsi
2692 memset(scsi_inq, 0, 256);
2693 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2694 scsi_inq_dma_handle) ||
2695 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2696 continue;
2700 * Check for overflow. We print less than 240
2701 * characters for inquiry
2703 if( (len + 240) >= PAGE_SIZE ) break;
2705 len += sprintf(page+len, "%s.\n", str);
2707 len += mega_print_inquiry(page+len, scsi_inq);
2710 free_pci:
2711 pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2712 free_inquiry:
2713 mega_free_inquiry(inquiry, dma_handle, pdev);
2714 free_pdev:
2715 free_local_pdev(pdev);
2717 return len;
2722 * Display scsi inquiry
2724 static int
2725 mega_print_inquiry(char *page, char *scsi_inq)
2727 int len = 0;
2728 int i;
2730 len = sprintf(page, " Vendor: ");
2731 for( i = 8; i < 16; i++ ) {
2732 len += sprintf(page+len, "%c", scsi_inq[i]);
2735 len += sprintf(page+len, " Model: ");
2737 for( i = 16; i < 32; i++ ) {
2738 len += sprintf(page+len, "%c", scsi_inq[i]);
2741 len += sprintf(page+len, " Rev: ");
2743 for( i = 32; i < 36; i++ ) {
2744 len += sprintf(page+len, "%c", scsi_inq[i]);
2747 len += sprintf(page+len, "\n");
2749 i = scsi_inq[0] & 0x1f;
2751 len += sprintf(page+len, " Type: %s ", scsi_device_type(i));
2753 len += sprintf(page+len,
2754 " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
2756 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2757 len += sprintf(page+len, " CCS\n");
2758 else
2759 len += sprintf(page+len, "\n");
2761 return len;
2766 * proc_rdrv_10()
2767 * @page - buffer to write the data in
2768 * @start - where the actual data has been written in page
2769 * @offset - same meaning as the read system call
2770 * @count - same meaning as the read system call
2771 * @eof - set if no more data needs to be returned
2772 * @data - pointer to our soft state
2774 * Display real time information about the logical drives 0 through 9.
2776 static int
2777 proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
2778 void *data)
2780 adapter_t *adapter = (adapter_t *)data;
2782 *eof = 1;
2784 return (proc_rdrv(adapter, page, 0, 9));
2789 * proc_rdrv_20()
2790 * @page - buffer to write the data in
2791 * @start - where the actual data has been written in page
2792 * @offset - same meaning as the read system call
2793 * @count - same meaning as the read system call
2794 * @eof - set if no more data needs to be returned
2795 * @data - pointer to our soft state
2797 * Display real time information about the logical drives 0 through 9.
2799 static int
2800 proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
2801 void *data)
2803 adapter_t *adapter = (adapter_t *)data;
2805 *eof = 1;
2807 return (proc_rdrv(adapter, page, 10, 19));
2812 * proc_rdrv_30()
2813 * @page - buffer to write the data in
2814 * @start - where the actual data has been written in page
2815 * @offset - same meaning as the read system call
2816 * @count - same meaning as the read system call
2817 * @eof - set if no more data needs to be returned
2818 * @data - pointer to our soft state
2820 * Display real time information about the logical drives 0 through 9.
2822 static int
2823 proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
2824 void *data)
2826 adapter_t *adapter = (adapter_t *)data;
2828 *eof = 1;
2830 return (proc_rdrv(adapter, page, 20, 29));
2835 * proc_rdrv_40()
2836 * @page - buffer to write the data in
2837 * @start - where the actual data has been written in page
2838 * @offset - same meaning as the read system call
2839 * @count - same meaning as the read system call
2840 * @eof - set if no more data needs to be returned
2841 * @data - pointer to our soft state
2843 * Display real time information about the logical drives 0 through 9.
2845 static int
2846 proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
2847 void *data)
2849 adapter_t *adapter = (adapter_t *)data;
2851 *eof = 1;
2853 return (proc_rdrv(adapter, page, 30, 39));
2858 * proc_rdrv()
2859 * @page - buffer to write the data in
2860 * @adapter - pointer to our soft state
2861 * @start - starting logical drive to display
2862 * @end - ending logical drive to display
2864 * We do not print the inquiry information since its already available through
2865 * /proc/scsi/scsi interface
2867 static int
2868 proc_rdrv(adapter_t *adapter, char *page, int start, int end )
2870 dma_addr_t dma_handle;
2871 logdrv_param *lparam;
2872 megacmd_t mc;
2873 char *disk_array;
2874 dma_addr_t disk_array_dma_handle;
2875 caddr_t inquiry;
2876 struct pci_dev *pdev;
2877 u8 *rdrv_state;
2878 int num_ldrv;
2879 u32 array_sz;
2880 int len = 0;
2881 int i;
2883 if( make_local_pdev(adapter, &pdev) != 0 ) {
2884 return len;
2887 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2888 free_local_pdev(pdev);
2889 return len;
2892 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2894 len = sprintf(page, "Adapter inquiry failed.\n");
2896 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2898 mega_free_inquiry(inquiry, dma_handle, pdev);
2900 free_local_pdev(pdev);
2902 return len;
2905 memset(&mc, 0, sizeof(megacmd_t));
2907 if( adapter->flag & BOARD_40LD ) {
2908 array_sz = sizeof(disk_array_40ld);
2910 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2912 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2914 else {
2915 array_sz = sizeof(disk_array_8ld);
2917 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2918 raid_inq.logdrv_info.ldrv_state;
2920 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2921 raid_inq.logdrv_info.num_ldrv;
2924 disk_array = pci_alloc_consistent(pdev, array_sz,
2925 &disk_array_dma_handle);
2927 if( disk_array == NULL ) {
2928 len = sprintf(page, "memory not available.\n");
2930 mega_free_inquiry(inquiry, dma_handle, pdev);
2932 free_local_pdev(pdev);
2934 return len;
2937 mc.xferaddr = (u32)disk_array_dma_handle;
2939 if( adapter->flag & BOARD_40LD ) {
2940 mc.cmd = FC_NEW_CONFIG;
2941 mc.opcode = OP_DCMD_READ_CONFIG;
2943 if( mega_internal_command(adapter, &mc, NULL) ) {
2945 len = sprintf(page, "40LD read config failed.\n");
2947 mega_free_inquiry(inquiry, dma_handle, pdev);
2949 pci_free_consistent(pdev, array_sz, disk_array,
2950 disk_array_dma_handle);
2952 free_local_pdev(pdev);
2954 return len;
2958 else {
2959 mc.cmd = NEW_READ_CONFIG_8LD;
2961 if( mega_internal_command(adapter, &mc, NULL) ) {
2963 mc.cmd = READ_CONFIG_8LD;
2965 if( mega_internal_command(adapter, &mc,
2966 NULL) ){
2968 len = sprintf(page,
2969 "8LD read config failed.\n");
2971 mega_free_inquiry(inquiry, dma_handle, pdev);
2973 pci_free_consistent(pdev, array_sz,
2974 disk_array,
2975 disk_array_dma_handle);
2977 free_local_pdev(pdev);
2979 return len;
2984 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2986 if( adapter->flag & BOARD_40LD ) {
2987 lparam =
2988 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
2990 else {
2991 lparam =
2992 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
2996 * Check for overflow. We print less than 240 characters for
2997 * information about each logical drive.
2999 if( (len + 240) >= PAGE_SIZE ) break;
3001 len += sprintf(page+len, "Logical drive:%2d:, ", i);
3003 switch( rdrv_state[i] & 0x0F ) {
3004 case RDRV_OFFLINE:
3005 len += sprintf(page+len, "state: offline");
3006 break;
3008 case RDRV_DEGRADED:
3009 len += sprintf(page+len, "state: degraded");
3010 break;
3012 case RDRV_OPTIMAL:
3013 len += sprintf(page+len, "state: optimal");
3014 break;
3016 case RDRV_DELETED:
3017 len += sprintf(page+len, "state: deleted");
3018 break;
3020 default:
3021 len += sprintf(page+len, "state: unknown");
3022 break;
3026 * Check if check consistency or initialization is going on
3027 * for this logical drive.
3029 if( (rdrv_state[i] & 0xF0) == 0x20 ) {
3030 len += sprintf(page+len,
3031 ", check-consistency in progress");
3033 else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
3034 len += sprintf(page+len,
3035 ", initialization in progress");
3038 len += sprintf(page+len, "\n");
3040 len += sprintf(page+len, "Span depth:%3d, ",
3041 lparam->span_depth);
3043 len += sprintf(page+len, "RAID level:%3d, ",
3044 lparam->level);
3046 len += sprintf(page+len, "Stripe size:%3d, ",
3047 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
3049 len += sprintf(page+len, "Row size:%3d\n",
3050 lparam->row_size);
3053 len += sprintf(page+len, "Read Policy: ");
3055 switch(lparam->read_ahead) {
3057 case NO_READ_AHEAD:
3058 len += sprintf(page+len, "No read ahead, ");
3059 break;
3061 case READ_AHEAD:
3062 len += sprintf(page+len, "Read ahead, ");
3063 break;
3065 case ADAP_READ_AHEAD:
3066 len += sprintf(page+len, "Adaptive, ");
3067 break;
3071 len += sprintf(page+len, "Write Policy: ");
3073 switch(lparam->write_mode) {
3075 case WRMODE_WRITE_THRU:
3076 len += sprintf(page+len, "Write thru, ");
3077 break;
3079 case WRMODE_WRITE_BACK:
3080 len += sprintf(page+len, "Write back, ");
3081 break;
3084 len += sprintf(page+len, "Cache Policy: ");
3086 switch(lparam->direct_io) {
3088 case CACHED_IO:
3089 len += sprintf(page+len, "Cached IO\n\n");
3090 break;
3092 case DIRECT_IO:
3093 len += sprintf(page+len, "Direct IO\n\n");
3094 break;
3098 mega_free_inquiry(inquiry, dma_handle, pdev);
3100 pci_free_consistent(pdev, array_sz, disk_array,
3101 disk_array_dma_handle);
3103 free_local_pdev(pdev);
3105 return len;
3107 #else
3108 static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
3111 #endif
3115 * megaraid_biosparam()
3117 * Return the disk geometry for a particular disk
3119 static int
3120 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
3121 sector_t capacity, int geom[])
3123 adapter_t *adapter;
3124 unsigned char *bh;
3125 int heads;
3126 int sectors;
3127 int cylinders;
3128 int rval;
3130 /* Get pointer to host config structure */
3131 adapter = (adapter_t *)sdev->host->hostdata;
3133 if (IS_RAID_CH(adapter, sdev->channel)) {
3134 /* Default heads (64) & sectors (32) */
3135 heads = 64;
3136 sectors = 32;
3137 cylinders = (ulong)capacity / (heads * sectors);
3140 * Handle extended translation size for logical drives
3141 * > 1Gb
3143 if ((ulong)capacity >= 0x200000) {
3144 heads = 255;
3145 sectors = 63;
3146 cylinders = (ulong)capacity / (heads * sectors);
3149 /* return result */
3150 geom[0] = heads;
3151 geom[1] = sectors;
3152 geom[2] = cylinders;
3154 else {
3155 bh = scsi_bios_ptable(bdev);
3157 if( bh ) {
3158 rval = scsi_partsize(bh, capacity,
3159 &geom[2], &geom[0], &geom[1]);
3160 kfree(bh);
3161 if( rval != -1 )
3162 return rval;
3165 printk(KERN_INFO
3166 "megaraid: invalid partition on this disk on channel %d\n",
3167 sdev->channel);
3169 /* Default heads (64) & sectors (32) */
3170 heads = 64;
3171 sectors = 32;
3172 cylinders = (ulong)capacity / (heads * sectors);
3174 /* Handle extended translation size for logical drives > 1Gb */
3175 if ((ulong)capacity >= 0x200000) {
3176 heads = 255;
3177 sectors = 63;
3178 cylinders = (ulong)capacity / (heads * sectors);
3181 /* return result */
3182 geom[0] = heads;
3183 geom[1] = sectors;
3184 geom[2] = cylinders;
3187 return 0;
3191 * mega_init_scb()
3192 * @adapter - pointer to our soft state
3194 * Allocate memory for the various pointers in the scb structures:
3195 * scatter-gather list pointer, passthru and extended passthru structure
3196 * pointers.
3198 static int
3199 mega_init_scb(adapter_t *adapter)
3201 scb_t *scb;
3202 int i;
3204 for( i = 0; i < adapter->max_cmds; i++ ) {
3206 scb = &adapter->scb_list[i];
3208 scb->sgl64 = NULL;
3209 scb->sgl = NULL;
3210 scb->pthru = NULL;
3211 scb->epthru = NULL;
3214 for( i = 0; i < adapter->max_cmds; i++ ) {
3216 scb = &adapter->scb_list[i];
3218 scb->idx = i;
3220 scb->sgl64 = pci_alloc_consistent(adapter->dev,
3221 sizeof(mega_sgl64) * adapter->sglen,
3222 &scb->sgl_dma_addr);
3224 scb->sgl = (mega_sglist *)scb->sgl64;
3226 if( !scb->sgl ) {
3227 printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
3228 mega_free_sgl(adapter);
3229 return -1;
3232 scb->pthru = pci_alloc_consistent(adapter->dev,
3233 sizeof(mega_passthru),
3234 &scb->pthru_dma_addr);
3236 if( !scb->pthru ) {
3237 printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
3238 mega_free_sgl(adapter);
3239 return -1;
3242 scb->epthru = pci_alloc_consistent(adapter->dev,
3243 sizeof(mega_ext_passthru),
3244 &scb->epthru_dma_addr);
3246 if( !scb->epthru ) {
3247 printk(KERN_WARNING
3248 "Can't allocate extended passthru.\n");
3249 mega_free_sgl(adapter);
3250 return -1;
3254 scb->dma_type = MEGA_DMA_TYPE_NONE;
3257 * Link to free list
3258 * lock not required since we are loading the driver, so no
3259 * commands possible right now.
3261 scb->state = SCB_FREE;
3262 scb->cmd = NULL;
3263 list_add(&scb->list, &adapter->free_list);
3266 return 0;
3271 * megadev_open()
3272 * @inode - unused
3273 * @filep - unused
3275 * Routines for the character/ioctl interface to the driver. Find out if this
3276 * is a valid open.
3278 static int
3279 megadev_open (struct inode *inode, struct file *filep)
3281 cycle_kernel_lock();
3283 * Only allow superuser to access private ioctl interface
3285 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
3287 return 0;
3292 * megadev_ioctl()
3293 * @inode - Our device inode
3294 * @filep - unused
3295 * @cmd - ioctl command
3296 * @arg - user buffer
3298 * ioctl entry point for our private ioctl interface. We move the data in from
3299 * the user space, prepare the command (if necessary, convert the old MIMD
3300 * ioctl to new ioctl command), and issue a synchronous command to the
3301 * controller.
3303 static int
3304 megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
3305 unsigned long arg)
3307 adapter_t *adapter;
3308 nitioctl_t uioc;
3309 int adapno;
3310 int rval;
3311 mega_passthru __user *upthru; /* user address for passthru */
3312 mega_passthru *pthru; /* copy user passthru here */
3313 dma_addr_t pthru_dma_hndl;
3314 void *data = NULL; /* data to be transferred */
3315 dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
3316 megacmd_t mc;
3317 megastat_t __user *ustats;
3318 int num_ldrv;
3319 u32 uxferaddr = 0;
3320 struct pci_dev *pdev;
3322 ustats = NULL; /* avoid compilation warnings */
3323 num_ldrv = 0;
3326 * Make sure only USCSICMD are issued through this interface.
3327 * MIMD application would still fire different command.
3329 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3330 return -EINVAL;
3334 * Check and convert a possible MIMD command to NIT command.
3335 * mega_m_to_n() copies the data from the user space, so we do not
3336 * have to do it here.
3337 * NOTE: We will need some user address to copyout the data, therefore
3338 * the inteface layer will also provide us with the required user
3339 * addresses.
3341 memset(&uioc, 0, sizeof(nitioctl_t));
3342 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3343 return rval;
3346 switch( uioc.opcode ) {
3348 case GET_DRIVER_VER:
3349 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3350 return (-EFAULT);
3352 break;
3354 case GET_N_ADAP:
3355 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3356 return (-EFAULT);
3359 * Shucks. MIMD interface returns a positive value for number
3360 * of adapters. TODO: Change it to return 0 when there is no
3361 * applicatio using mimd interface.
3363 return hba_count;
3365 case GET_ADAP_INFO:
3368 * Which adapter
3370 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3371 return (-ENODEV);
3373 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3374 sizeof(struct mcontroller)) )
3375 return (-EFAULT);
3376 break;
3378 #if MEGA_HAVE_STATS
3380 case GET_STATS:
3382 * Which adapter
3384 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3385 return (-ENODEV);
3387 adapter = hba_soft_state[adapno];
3389 ustats = uioc.uioc_uaddr;
3391 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3392 return (-EFAULT);
3395 * Check for the validity of the logical drive number
3397 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3399 if( copy_to_user(ustats->nreads, adapter->nreads,
3400 num_ldrv*sizeof(u32)) )
3401 return -EFAULT;
3403 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3404 num_ldrv*sizeof(u32)) )
3405 return -EFAULT;
3407 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3408 num_ldrv*sizeof(u32)) )
3409 return -EFAULT;
3411 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3412 num_ldrv*sizeof(u32)) )
3413 return -EFAULT;
3415 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3416 num_ldrv*sizeof(u32)) )
3417 return -EFAULT;
3419 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3420 num_ldrv*sizeof(u32)) )
3421 return -EFAULT;
3423 return 0;
3425 #endif
3426 case MBOX_CMD:
3429 * Which adapter
3431 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3432 return (-ENODEV);
3434 adapter = hba_soft_state[adapno];
3437 * Deletion of logical drive is a special case. The adapter
3438 * should be quiescent before this command is issued.
3440 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3441 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3444 * Do we support this feature
3446 if( !adapter->support_random_del ) {
3447 printk(KERN_WARNING "megaraid: logdrv ");
3448 printk("delete on non-supporting F/W.\n");
3450 return (-EINVAL);
3453 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3455 if( rval == 0 ) {
3456 memset(&mc, 0, sizeof(megacmd_t));
3458 mc.status = rval;
3460 rval = mega_n_to_m((void __user *)arg, &mc);
3463 return rval;
3466 * This interface only support the regular passthru commands.
3467 * Reject extended passthru and 64-bit passthru
3469 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3470 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3472 printk(KERN_WARNING "megaraid: rejected passthru.\n");
3474 return (-EINVAL);
3478 * For all internal commands, the buffer must be allocated in
3479 * <4GB address range
3481 if( make_local_pdev(adapter, &pdev) != 0 )
3482 return -EIO;
3484 /* Is it a passthru command or a DCMD */
3485 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3486 /* Passthru commands */
3488 pthru = pci_alloc_consistent(pdev,
3489 sizeof(mega_passthru),
3490 &pthru_dma_hndl);
3492 if( pthru == NULL ) {
3493 free_local_pdev(pdev);
3494 return (-ENOMEM);
3498 * The user passthru structure
3500 upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
3503 * Copy in the user passthru here.
3505 if( copy_from_user(pthru, upthru,
3506 sizeof(mega_passthru)) ) {
3508 pci_free_consistent(pdev,
3509 sizeof(mega_passthru), pthru,
3510 pthru_dma_hndl);
3512 free_local_pdev(pdev);
3514 return (-EFAULT);
3518 * Is there a data transfer
3520 if( pthru->dataxferlen ) {
3521 data = pci_alloc_consistent(pdev,
3522 pthru->dataxferlen,
3523 &data_dma_hndl);
3525 if( data == NULL ) {
3526 pci_free_consistent(pdev,
3527 sizeof(mega_passthru),
3528 pthru,
3529 pthru_dma_hndl);
3531 free_local_pdev(pdev);
3533 return (-ENOMEM);
3537 * Save the user address and point the kernel
3538 * address at just allocated memory
3540 uxferaddr = pthru->dataxferaddr;
3541 pthru->dataxferaddr = data_dma_hndl;
3546 * Is data coming down-stream
3548 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3550 * Get the user data
3552 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3553 pthru->dataxferlen) ) {
3554 rval = (-EFAULT);
3555 goto freemem_and_return;
3559 memset(&mc, 0, sizeof(megacmd_t));
3561 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3562 mc.xferaddr = (u32)pthru_dma_hndl;
3565 * Issue the command
3567 mega_internal_command(adapter, &mc, pthru);
3569 rval = mega_n_to_m((void __user *)arg, &mc);
3571 if( rval ) goto freemem_and_return;
3575 * Is data going up-stream
3577 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3578 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3579 pthru->dataxferlen) ) {
3580 rval = (-EFAULT);
3585 * Send the request sense data also, irrespective of
3586 * whether the user has asked for it or not.
3588 if (copy_to_user(upthru->reqsensearea,
3589 pthru->reqsensearea, 14))
3590 rval = -EFAULT;
3592 freemem_and_return:
3593 if( pthru->dataxferlen ) {
3594 pci_free_consistent(pdev,
3595 pthru->dataxferlen, data,
3596 data_dma_hndl);
3599 pci_free_consistent(pdev, sizeof(mega_passthru),
3600 pthru, pthru_dma_hndl);
3602 free_local_pdev(pdev);
3604 return rval;
3606 else {
3607 /* DCMD commands */
3610 * Is there a data transfer
3612 if( uioc.xferlen ) {
3613 data = pci_alloc_consistent(pdev,
3614 uioc.xferlen, &data_dma_hndl);
3616 if( data == NULL ) {
3617 free_local_pdev(pdev);
3618 return (-ENOMEM);
3621 uxferaddr = MBOX(uioc)->xferaddr;
3625 * Is data coming down-stream
3627 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3629 * Get the user data
3631 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3632 uioc.xferlen) ) {
3634 pci_free_consistent(pdev,
3635 uioc.xferlen,
3636 data, data_dma_hndl);
3638 free_local_pdev(pdev);
3640 return (-EFAULT);
3644 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3646 mc.xferaddr = (u32)data_dma_hndl;
3649 * Issue the command
3651 mega_internal_command(adapter, &mc, NULL);
3653 rval = mega_n_to_m((void __user *)arg, &mc);
3655 if( rval ) {
3656 if( uioc.xferlen ) {
3657 pci_free_consistent(pdev,
3658 uioc.xferlen, data,
3659 data_dma_hndl);
3662 free_local_pdev(pdev);
3664 return rval;
3668 * Is data going up-stream
3670 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3671 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3672 uioc.xferlen) ) {
3674 rval = (-EFAULT);
3678 if( uioc.xferlen ) {
3679 pci_free_consistent(pdev,
3680 uioc.xferlen, data,
3681 data_dma_hndl);
3684 free_local_pdev(pdev);
3686 return rval;
3689 default:
3690 return (-EINVAL);
3693 return 0;
3697 * mega_m_to_n()
3698 * @arg - user address
3699 * @uioc - new ioctl structure
3701 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3702 * structure
3704 * Converts the older mimd ioctl structure to newer NIT structure
3706 static int
3707 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3709 struct uioctl_t uioc_mimd;
3710 char signature[8] = {0};
3711 u8 opcode;
3712 u8 subopcode;
3716 * check is the application conforms to NIT. We do not have to do much
3717 * in that case.
3718 * We exploit the fact that the signature is stored in the very
3719 * begining of the structure.
3722 if( copy_from_user(signature, arg, 7) )
3723 return (-EFAULT);
3725 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3728 * NOTE NOTE: The nit ioctl is still under flux because of
3729 * change of mailbox definition, in HPE. No applications yet
3730 * use this interface and let's not have applications use this
3731 * interface till the new specifitions are in place.
3733 return -EINVAL;
3734 #if 0
3735 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3736 return (-EFAULT);
3737 return 0;
3738 #endif
3742 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3744 * Get the user ioctl structure
3746 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3747 return (-EFAULT);
3751 * Get the opcode and subopcode for the commands
3753 opcode = uioc_mimd.ui.fcs.opcode;
3754 subopcode = uioc_mimd.ui.fcs.subopcode;
3756 switch (opcode) {
3757 case 0x82:
3759 switch (subopcode) {
3761 case MEGAIOC_QDRVRVER: /* Query driver version */
3762 uioc->opcode = GET_DRIVER_VER;
3763 uioc->uioc_uaddr = uioc_mimd.data;
3764 break;
3766 case MEGAIOC_QNADAP: /* Get # of adapters */
3767 uioc->opcode = GET_N_ADAP;
3768 uioc->uioc_uaddr = uioc_mimd.data;
3769 break;
3771 case MEGAIOC_QADAPINFO: /* Get adapter information */
3772 uioc->opcode = GET_ADAP_INFO;
3773 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3774 uioc->uioc_uaddr = uioc_mimd.data;
3775 break;
3777 default:
3778 return(-EINVAL);
3781 break;
3784 case 0x81:
3786 uioc->opcode = MBOX_CMD;
3787 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3789 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3791 uioc->xferlen = uioc_mimd.ui.fcs.length;
3793 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3794 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3796 break;
3798 case 0x80:
3800 uioc->opcode = MBOX_CMD;
3801 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3803 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3806 * Choose the xferlen bigger of input and output data
3808 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3809 uioc_mimd.outlen : uioc_mimd.inlen;
3811 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3812 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3814 break;
3816 default:
3817 return (-EINVAL);
3821 return 0;
3825 * mega_n_to_m()
3826 * @arg - user address
3827 * @mc - mailbox command
3829 * Updates the status information to the application, depending on application
3830 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3832 static int
3833 mega_n_to_m(void __user *arg, megacmd_t *mc)
3835 nitioctl_t __user *uiocp;
3836 megacmd_t __user *umc;
3837 mega_passthru __user *upthru;
3838 struct uioctl_t __user *uioc_mimd;
3839 char signature[8] = {0};
3842 * check is the application conforms to NIT.
3844 if( copy_from_user(signature, arg, 7) )
3845 return -EFAULT;
3847 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3849 uiocp = arg;
3851 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3852 return (-EFAULT);
3854 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3856 umc = MBOX_P(uiocp);
3858 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3859 return -EFAULT;
3861 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3862 return (-EFAULT);
3865 else {
3866 uioc_mimd = arg;
3868 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3869 return (-EFAULT);
3871 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3873 umc = (megacmd_t __user *)uioc_mimd->mbox;
3875 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3876 return (-EFAULT);
3878 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3879 return (-EFAULT);
3883 return 0;
3888 * MEGARAID 'FW' commands.
3892 * mega_is_bios_enabled()
3893 * @adapter - pointer to our soft state
3895 * issue command to find out if the BIOS is enabled for this controller
3897 static int
3898 mega_is_bios_enabled(adapter_t *adapter)
3900 unsigned char raw_mbox[sizeof(struct mbox_out)];
3901 mbox_t *mbox;
3902 int ret;
3904 mbox = (mbox_t *)raw_mbox;
3906 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3908 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3910 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3912 raw_mbox[0] = IS_BIOS_ENABLED;
3913 raw_mbox[2] = GET_BIOS;
3916 ret = issue_scb_block(adapter, raw_mbox);
3918 return *(char *)adapter->mega_buffer;
3923 * mega_enum_raid_scsi()
3924 * @adapter - pointer to our soft state
3926 * Find out what channels are RAID/SCSI. This information is used to
3927 * differentiate the virtual channels and physical channels and to support
3928 * ROMB feature and non-disk devices.
3930 static void
3931 mega_enum_raid_scsi(adapter_t *adapter)
3933 unsigned char raw_mbox[sizeof(struct mbox_out)];
3934 mbox_t *mbox;
3935 int i;
3937 mbox = (mbox_t *)raw_mbox;
3939 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3942 * issue command to find out what channels are raid/scsi
3944 raw_mbox[0] = CHNL_CLASS;
3945 raw_mbox[2] = GET_CHNL_CLASS;
3947 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3949 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3952 * Non-ROMB firmware fail this command, so all channels
3953 * must be shown RAID
3955 adapter->mega_ch_class = 0xFF;
3957 if(!issue_scb_block(adapter, raw_mbox)) {
3958 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3962 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
3963 if( (adapter->mega_ch_class >> i) & 0x01 ) {
3964 printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
3967 else {
3968 printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
3973 return;
3978 * mega_get_boot_drv()
3979 * @adapter - pointer to our soft state
3981 * Find out which device is the boot device. Note, any logical drive or any
3982 * phyical device (e.g., a CDROM) can be designated as a boot device.
3984 static void
3985 mega_get_boot_drv(adapter_t *adapter)
3987 struct private_bios_data *prv_bios_data;
3988 unsigned char raw_mbox[sizeof(struct mbox_out)];
3989 mbox_t *mbox;
3990 u16 cksum = 0;
3991 u8 *cksum_p;
3992 u8 boot_pdrv;
3993 int i;
3995 mbox = (mbox_t *)raw_mbox;
3997 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3999 raw_mbox[0] = BIOS_PVT_DATA;
4000 raw_mbox[2] = GET_BIOS_PVT_DATA;
4002 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4004 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4006 adapter->boot_ldrv_enabled = 0;
4007 adapter->boot_ldrv = 0;
4009 adapter->boot_pdrv_enabled = 0;
4010 adapter->boot_pdrv_ch = 0;
4011 adapter->boot_pdrv_tgt = 0;
4013 if(issue_scb_block(adapter, raw_mbox) == 0) {
4014 prv_bios_data =
4015 (struct private_bios_data *)adapter->mega_buffer;
4017 cksum = 0;
4018 cksum_p = (char *)prv_bios_data;
4019 for (i = 0; i < 14; i++ ) {
4020 cksum += (u16)(*cksum_p++);
4023 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
4026 * If MSB is set, a physical drive is set as boot
4027 * device
4029 if( prv_bios_data->boot_drv & 0x80 ) {
4030 adapter->boot_pdrv_enabled = 1;
4031 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
4032 adapter->boot_pdrv_ch = boot_pdrv / 16;
4033 adapter->boot_pdrv_tgt = boot_pdrv % 16;
4035 else {
4036 adapter->boot_ldrv_enabled = 1;
4037 adapter->boot_ldrv = prv_bios_data->boot_drv;
4045 * mega_support_random_del()
4046 * @adapter - pointer to our soft state
4048 * Find out if this controller supports random deletion and addition of
4049 * logical drives
4051 static int
4052 mega_support_random_del(adapter_t *adapter)
4054 unsigned char raw_mbox[sizeof(struct mbox_out)];
4055 mbox_t *mbox;
4056 int rval;
4058 mbox = (mbox_t *)raw_mbox;
4060 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4063 * issue command
4065 raw_mbox[0] = FC_DEL_LOGDRV;
4066 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
4068 rval = issue_scb_block(adapter, raw_mbox);
4070 return !rval;
4075 * mega_support_ext_cdb()
4076 * @adapter - pointer to our soft state
4078 * Find out if this firmware support cdblen > 10
4080 static int
4081 mega_support_ext_cdb(adapter_t *adapter)
4083 unsigned char raw_mbox[sizeof(struct mbox_out)];
4084 mbox_t *mbox;
4085 int rval;
4087 mbox = (mbox_t *)raw_mbox;
4089 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4091 * issue command to find out if controller supports extended CDBs.
4093 raw_mbox[0] = 0xA4;
4094 raw_mbox[2] = 0x16;
4096 rval = issue_scb_block(adapter, raw_mbox);
4098 return !rval;
4103 * mega_del_logdrv()
4104 * @adapter - pointer to our soft state
4105 * @logdrv - logical drive to be deleted
4107 * Delete the specified logical drive. It is the responsibility of the user
4108 * app to let the OS know about this operation.
4110 static int
4111 mega_del_logdrv(adapter_t *adapter, int logdrv)
4113 unsigned long flags;
4114 scb_t *scb;
4115 int rval;
4118 * Stop sending commands to the controller, queue them internally.
4119 * When deletion is complete, ISR will flush the queue.
4121 atomic_set(&adapter->quiescent, 1);
4124 * Wait till all the issued commands are complete and there are no
4125 * commands in the pending queue
4127 while (atomic_read(&adapter->pend_cmds) > 0 ||
4128 !list_empty(&adapter->pending_list))
4129 msleep(1000); /* sleep for 1s */
4131 rval = mega_do_del_logdrv(adapter, logdrv);
4133 spin_lock_irqsave(&adapter->lock, flags);
4136 * If delete operation was successful, add 0x80 to the logical drive
4137 * ids for commands in the pending queue.
4139 if (adapter->read_ldidmap) {
4140 struct list_head *pos;
4141 list_for_each(pos, &adapter->pending_list) {
4142 scb = list_entry(pos, scb_t, list);
4143 if (scb->pthru->logdrv < 0x80 )
4144 scb->pthru->logdrv += 0x80;
4148 atomic_set(&adapter->quiescent, 0);
4150 mega_runpendq(adapter);
4152 spin_unlock_irqrestore(&adapter->lock, flags);
4154 return rval;
4158 static int
4159 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
4161 megacmd_t mc;
4162 int rval;
4164 memset( &mc, 0, sizeof(megacmd_t));
4166 mc.cmd = FC_DEL_LOGDRV;
4167 mc.opcode = OP_DEL_LOGDRV;
4168 mc.subopcode = logdrv;
4170 rval = mega_internal_command(adapter, &mc, NULL);
4172 /* log this event */
4173 if(rval) {
4174 printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
4175 return rval;
4179 * After deleting first logical drive, the logical drives must be
4180 * addressed by adding 0x80 to the logical drive id.
4182 adapter->read_ldidmap = 1;
4184 return rval;
4189 * mega_get_max_sgl()
4190 * @adapter - pointer to our soft state
4192 * Find out the maximum number of scatter-gather elements supported by this
4193 * version of the firmware
4195 static void
4196 mega_get_max_sgl(adapter_t *adapter)
4198 unsigned char raw_mbox[sizeof(struct mbox_out)];
4199 mbox_t *mbox;
4201 mbox = (mbox_t *)raw_mbox;
4203 memset(mbox, 0, sizeof(raw_mbox));
4205 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4207 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4209 raw_mbox[0] = MAIN_MISC_OPCODE;
4210 raw_mbox[2] = GET_MAX_SG_SUPPORT;
4213 if( issue_scb_block(adapter, raw_mbox) ) {
4215 * f/w does not support this command. Choose the default value
4217 adapter->sglen = MIN_SGLIST;
4219 else {
4220 adapter->sglen = *((char *)adapter->mega_buffer);
4223 * Make sure this is not more than the resources we are
4224 * planning to allocate
4226 if ( adapter->sglen > MAX_SGLIST )
4227 adapter->sglen = MAX_SGLIST;
4230 return;
4235 * mega_support_cluster()
4236 * @adapter - pointer to our soft state
4238 * Find out if this firmware support cluster calls.
4240 static int
4241 mega_support_cluster(adapter_t *adapter)
4243 unsigned char raw_mbox[sizeof(struct mbox_out)];
4244 mbox_t *mbox;
4246 mbox = (mbox_t *)raw_mbox;
4248 memset(mbox, 0, sizeof(raw_mbox));
4250 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4252 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4255 * Try to get the initiator id. This command will succeed iff the
4256 * clustering is available on this HBA.
4258 raw_mbox[0] = MEGA_GET_TARGET_ID;
4260 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
4263 * Cluster support available. Get the initiator target id.
4264 * Tell our id to mid-layer too.
4266 adapter->this_id = *(u32 *)adapter->mega_buffer;
4267 adapter->host->this_id = adapter->this_id;
4269 return 1;
4272 return 0;
4275 #ifdef CONFIG_PROC_FS
4277 * mega_adapinq()
4278 * @adapter - pointer to our soft state
4279 * @dma_handle - DMA address of the buffer
4281 * Issue internal comamnds while interrupts are available.
4282 * We only issue direct mailbox commands from within the driver. ioctl()
4283 * interface using these routines can issue passthru commands.
4285 static int
4286 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
4288 megacmd_t mc;
4290 memset(&mc, 0, sizeof(megacmd_t));
4292 if( adapter->flag & BOARD_40LD ) {
4293 mc.cmd = FC_NEW_CONFIG;
4294 mc.opcode = NC_SUBOP_ENQUIRY3;
4295 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
4297 else {
4298 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
4301 mc.xferaddr = (u32)dma_handle;
4303 if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
4304 return -1;
4307 return 0;
4311 /** mega_internal_dev_inquiry()
4312 * @adapter - pointer to our soft state
4313 * @ch - channel for this device
4314 * @tgt - ID of this device
4315 * @buf_dma_handle - DMA address of the buffer
4317 * Issue the scsi inquiry for the specified device.
4319 static int
4320 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4321 dma_addr_t buf_dma_handle)
4323 mega_passthru *pthru;
4324 dma_addr_t pthru_dma_handle;
4325 megacmd_t mc;
4326 int rval;
4327 struct pci_dev *pdev;
4331 * For all internal commands, the buffer must be allocated in <4GB
4332 * address range
4334 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4336 pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4337 &pthru_dma_handle);
4339 if( pthru == NULL ) {
4340 free_local_pdev(pdev);
4341 return -1;
4344 pthru->timeout = 2;
4345 pthru->ars = 1;
4346 pthru->reqsenselen = 14;
4347 pthru->islogical = 0;
4349 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4351 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4353 pthru->cdblen = 6;
4355 pthru->cdb[0] = INQUIRY;
4356 pthru->cdb[1] = 0;
4357 pthru->cdb[2] = 0;
4358 pthru->cdb[3] = 0;
4359 pthru->cdb[4] = 255;
4360 pthru->cdb[5] = 0;
4363 pthru->dataxferaddr = (u32)buf_dma_handle;
4364 pthru->dataxferlen = 256;
4366 memset(&mc, 0, sizeof(megacmd_t));
4368 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4369 mc.xferaddr = (u32)pthru_dma_handle;
4371 rval = mega_internal_command(adapter, &mc, pthru);
4373 pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4374 pthru_dma_handle);
4376 free_local_pdev(pdev);
4378 return rval;
4380 #endif
4383 * mega_internal_command()
4384 * @adapter - pointer to our soft state
4385 * @mc - the mailbox command
4386 * @pthru - Passthru structure for DCDB commands
4388 * Issue the internal commands in interrupt mode.
4389 * The last argument is the address of the passthru structure if the command
4390 * to be fired is a passthru command
4392 * lockscope specifies whether the caller has already acquired the lock. Of
4393 * course, the caller must know which lock we are talking about.
4395 * Note: parameter 'pthru' is null for non-passthru commands.
4397 static int
4398 mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4400 Scsi_Cmnd *scmd;
4401 struct scsi_device *sdev;
4402 scb_t *scb;
4403 int rval;
4405 scmd = scsi_allocate_command(GFP_KERNEL);
4406 if (!scmd)
4407 return -ENOMEM;
4410 * The internal commands share one command id and hence are
4411 * serialized. This is so because we want to reserve maximum number of
4412 * available command ids for the I/O commands.
4414 mutex_lock(&adapter->int_mtx);
4416 scb = &adapter->int_scb;
4417 memset(scb, 0, sizeof(scb_t));
4419 sdev = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
4420 scmd->device = sdev;
4422 memset(adapter->int_cdb, 0, sizeof(adapter->int_cdb));
4423 scmd->cmnd = adapter->int_cdb;
4424 scmd->device->host = adapter->host;
4425 scmd->host_scribble = (void *)scb;
4426 scmd->cmnd[0] = MEGA_INTERNAL_CMD;
4428 scb->state |= SCB_ACTIVE;
4429 scb->cmd = scmd;
4431 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4434 * Is it a passthru command
4436 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4438 scb->pthru = pthru;
4441 scb->idx = CMDID_INT_CMDS;
4443 megaraid_queue(scmd, mega_internal_done);
4445 wait_for_completion(&adapter->int_waitq);
4447 rval = scmd->result;
4448 mc->status = scmd->result;
4449 kfree(sdev);
4452 * Print a debug message for all failed commands. Applications can use
4453 * this information.
4455 if( scmd->result && trace_level ) {
4456 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4457 mc->cmd, mc->opcode, mc->subopcode, scmd->result);
4460 mutex_unlock(&adapter->int_mtx);
4462 scsi_free_command(GFP_KERNEL, scmd);
4464 return rval;
4469 * mega_internal_done()
4470 * @scmd - internal scsi command
4472 * Callback routine for internal commands.
4474 static void
4475 mega_internal_done(Scsi_Cmnd *scmd)
4477 adapter_t *adapter;
4479 adapter = (adapter_t *)scmd->device->host->hostdata;
4481 complete(&adapter->int_waitq);
4486 static struct scsi_host_template megaraid_template = {
4487 .module = THIS_MODULE,
4488 .name = "MegaRAID",
4489 .proc_name = "megaraid_legacy",
4490 .info = megaraid_info,
4491 .queuecommand = megaraid_queue,
4492 .bios_param = megaraid_biosparam,
4493 .max_sectors = MAX_SECTORS_PER_IO,
4494 .can_queue = MAX_COMMANDS,
4495 .this_id = DEFAULT_INITIATOR_ID,
4496 .sg_tablesize = MAX_SGLIST,
4497 .cmd_per_lun = DEF_CMD_PER_LUN,
4498 .use_clustering = ENABLE_CLUSTERING,
4499 .eh_abort_handler = megaraid_abort,
4500 .eh_device_reset_handler = megaraid_reset,
4501 .eh_bus_reset_handler = megaraid_reset,
4502 .eh_host_reset_handler = megaraid_reset,
4505 static int __devinit
4506 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4508 struct Scsi_Host *host;
4509 adapter_t *adapter;
4510 unsigned long mega_baseport, tbase, flag = 0;
4511 u16 subsysid, subsysvid;
4512 u8 pci_bus, pci_dev_func;
4513 int irq, i, j;
4514 int error = -ENODEV;
4516 if (pci_enable_device(pdev))
4517 goto out;
4518 pci_set_master(pdev);
4520 pci_bus = pdev->bus->number;
4521 pci_dev_func = pdev->devfn;
4524 * The megaraid3 stuff reports the ID of the Intel part which is not
4525 * remotely specific to the megaraid
4527 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4528 u16 magic;
4530 * Don't fall over the Compaq management cards using the same
4531 * PCI identifier
4533 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4534 pdev->subsystem_device == 0xC000)
4535 return -ENODEV;
4536 /* Now check the magic signature byte */
4537 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4538 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4539 return -ENODEV;
4540 /* Ok it is probably a megaraid */
4544 * For these vendor and device ids, signature offsets are not
4545 * valid and 64 bit is implicit
4547 if (id->driver_data & BOARD_64BIT)
4548 flag |= BOARD_64BIT;
4549 else {
4550 u32 magic64;
4552 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4553 if (magic64 == HBA_SIGNATURE_64BIT)
4554 flag |= BOARD_64BIT;
4557 subsysvid = pdev->subsystem_vendor;
4558 subsysid = pdev->subsystem_device;
4560 printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4561 id->vendor, id->device, pci_bus);
4563 printk("slot %d:func %d\n",
4564 PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
4566 /* Read the base port and IRQ from PCI */
4567 mega_baseport = pci_resource_start(pdev, 0);
4568 irq = pdev->irq;
4570 tbase = mega_baseport;
4571 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4572 flag |= BOARD_MEMMAP;
4574 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4575 printk(KERN_WARNING "megaraid: mem region busy!\n");
4576 goto out_disable_device;
4579 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4580 if (!mega_baseport) {
4581 printk(KERN_WARNING
4582 "megaraid: could not map hba memory\n");
4583 goto out_release_region;
4585 } else {
4586 flag |= BOARD_IOMAP;
4587 mega_baseport += 0x10;
4589 if (!request_region(mega_baseport, 16, "megaraid"))
4590 goto out_disable_device;
4593 /* Initialize SCSI Host structure */
4594 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4595 if (!host)
4596 goto out_iounmap;
4598 adapter = (adapter_t *)host->hostdata;
4599 memset(adapter, 0, sizeof(adapter_t));
4601 printk(KERN_NOTICE
4602 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4603 host->host_no, mega_baseport, irq);
4605 adapter->base = mega_baseport;
4606 if (flag & BOARD_MEMMAP)
4607 adapter->mmio_base = (void __iomem *) mega_baseport;
4609 INIT_LIST_HEAD(&adapter->free_list);
4610 INIT_LIST_HEAD(&adapter->pending_list);
4611 INIT_LIST_HEAD(&adapter->completed_list);
4613 adapter->flag = flag;
4614 spin_lock_init(&adapter->lock);
4616 host->cmd_per_lun = max_cmd_per_lun;
4617 host->max_sectors = max_sectors_per_io;
4619 adapter->dev = pdev;
4620 adapter->host = host;
4622 adapter->host->irq = irq;
4624 if (flag & BOARD_MEMMAP)
4625 adapter->host->base = tbase;
4626 else {
4627 adapter->host->io_port = tbase;
4628 adapter->host->n_io_port = 16;
4631 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4634 * Allocate buffer to issue internal commands.
4636 adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4637 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4638 if (!adapter->mega_buffer) {
4639 printk(KERN_WARNING "megaraid: out of RAM.\n");
4640 goto out_host_put;
4643 adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4644 if (!adapter->scb_list) {
4645 printk(KERN_WARNING "megaraid: out of RAM.\n");
4646 goto out_free_cmd_buffer;
4649 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4650 megaraid_isr_memmapped : megaraid_isr_iomapped,
4651 IRQF_SHARED, "megaraid", adapter)) {
4652 printk(KERN_WARNING
4653 "megaraid: Couldn't register IRQ %d!\n", irq);
4654 goto out_free_scb_list;
4657 if (mega_setup_mailbox(adapter))
4658 goto out_free_irq;
4660 if (mega_query_adapter(adapter))
4661 goto out_free_mbox;
4664 * Have checks for some buggy f/w
4666 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4668 * Which firmware
4670 if (!strcmp(adapter->fw_version, "3.00") ||
4671 !strcmp(adapter->fw_version, "3.01")) {
4673 printk( KERN_WARNING
4674 "megaraid: Your card is a Dell PERC "
4675 "2/SC RAID controller with "
4676 "firmware\nmegaraid: 3.00 or 3.01. "
4677 "This driver is known to have "
4678 "corruption issues\nmegaraid: with "
4679 "those firmware versions on this "
4680 "specific card. In order\nmegaraid: "
4681 "to protect your data, please upgrade "
4682 "your firmware to version\nmegaraid: "
4683 "3.10 or later, available from the "
4684 "Dell Technical Support web\n"
4685 "megaraid: site at\nhttp://support."
4686 "dell.com/us/en/filelib/download/"
4687 "index.asp?fileid=2940\n"
4693 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4694 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4695 * support, since this firmware cannot handle 64 bit
4696 * addressing
4698 if ((subsysvid == HP_SUBSYS_VID) &&
4699 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4701 * which firmware
4703 if (!strcmp(adapter->fw_version, "H01.07") ||
4704 !strcmp(adapter->fw_version, "H01.08") ||
4705 !strcmp(adapter->fw_version, "H01.09") ) {
4706 printk(KERN_WARNING
4707 "megaraid: Firmware H.01.07, "
4708 "H.01.08, and H.01.09 on 1M/2M "
4709 "controllers\n"
4710 "megaraid: do not support 64 bit "
4711 "addressing.\nmegaraid: DISABLING "
4712 "64 bit support.\n");
4713 adapter->flag &= ~BOARD_64BIT;
4717 if (mega_is_bios_enabled(adapter))
4718 mega_hbas[hba_count].is_bios_enabled = 1;
4719 mega_hbas[hba_count].hostdata_addr = adapter;
4722 * Find out which channel is raid and which is scsi. This is
4723 * for ROMB support.
4725 mega_enum_raid_scsi(adapter);
4728 * Find out if a logical drive is set as the boot drive. If
4729 * there is one, will make that as the first logical drive.
4730 * ROMB: Do we have to boot from a physical drive. Then all
4731 * the physical drives would appear before the logical disks.
4732 * Else, all the physical drives would be exported to the mid
4733 * layer after logical drives.
4735 mega_get_boot_drv(adapter);
4737 if (adapter->boot_pdrv_enabled) {
4738 j = adapter->product_info.nchannels;
4739 for( i = 0; i < j; i++ )
4740 adapter->logdrv_chan[i] = 0;
4741 for( i = j; i < NVIRT_CHAN + j; i++ )
4742 adapter->logdrv_chan[i] = 1;
4743 } else {
4744 for (i = 0; i < NVIRT_CHAN; i++)
4745 adapter->logdrv_chan[i] = 1;
4746 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4747 adapter->logdrv_chan[i] = 0;
4748 adapter->mega_ch_class <<= NVIRT_CHAN;
4752 * Do we support random deletion and addition of logical
4753 * drives
4755 adapter->read_ldidmap = 0; /* set it after first logdrv
4756 delete cmd */
4757 adapter->support_random_del = mega_support_random_del(adapter);
4759 /* Initialize SCBs */
4760 if (mega_init_scb(adapter))
4761 goto out_free_mbox;
4764 * Reset the pending commands counter
4766 atomic_set(&adapter->pend_cmds, 0);
4769 * Reset the adapter quiescent flag
4771 atomic_set(&adapter->quiescent, 0);
4773 hba_soft_state[hba_count] = adapter;
4776 * Fill in the structure which needs to be passed back to the
4777 * application when it does an ioctl() for controller related
4778 * information.
4780 i = hba_count;
4782 mcontroller[i].base = mega_baseport;
4783 mcontroller[i].irq = irq;
4784 mcontroller[i].numldrv = adapter->numldrv;
4785 mcontroller[i].pcibus = pci_bus;
4786 mcontroller[i].pcidev = id->device;
4787 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4788 mcontroller[i].pciid = -1;
4789 mcontroller[i].pcivendor = id->vendor;
4790 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4791 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4794 /* Set the Mode of addressing to 64 bit if we can */
4795 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4796 pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4797 adapter->has_64bit_addr = 1;
4798 } else {
4799 pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4800 adapter->has_64bit_addr = 0;
4803 mutex_init(&adapter->int_mtx);
4804 init_completion(&adapter->int_waitq);
4806 adapter->this_id = DEFAULT_INITIATOR_ID;
4807 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4809 #if MEGA_HAVE_CLUSTERING
4811 * Is cluster support enabled on this controller
4812 * Note: In a cluster the HBAs ( the initiators ) will have
4813 * different target IDs and we cannot assume it to be 7. Call
4814 * to mega_support_cluster() will get the target ids also if
4815 * the cluster support is available
4817 adapter->has_cluster = mega_support_cluster(adapter);
4818 if (adapter->has_cluster) {
4819 printk(KERN_NOTICE
4820 "megaraid: Cluster driver, initiator id:%d\n",
4821 adapter->this_id);
4823 #endif
4825 pci_set_drvdata(pdev, host);
4827 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4829 error = scsi_add_host(host, &pdev->dev);
4830 if (error)
4831 goto out_free_mbox;
4833 scsi_scan_host(host);
4834 hba_count++;
4835 return 0;
4837 out_free_mbox:
4838 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4839 adapter->una_mbox64, adapter->una_mbox64_dma);
4840 out_free_irq:
4841 free_irq(adapter->host->irq, adapter);
4842 out_free_scb_list:
4843 kfree(adapter->scb_list);
4844 out_free_cmd_buffer:
4845 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4846 adapter->mega_buffer, adapter->buf_dma_handle);
4847 out_host_put:
4848 scsi_host_put(host);
4849 out_iounmap:
4850 if (flag & BOARD_MEMMAP)
4851 iounmap((void *)mega_baseport);
4852 out_release_region:
4853 if (flag & BOARD_MEMMAP)
4854 release_mem_region(tbase, 128);
4855 else
4856 release_region(mega_baseport, 16);
4857 out_disable_device:
4858 pci_disable_device(pdev);
4859 out:
4860 return error;
4863 static void
4864 __megaraid_shutdown(adapter_t *adapter)
4866 u_char raw_mbox[sizeof(struct mbox_out)];
4867 mbox_t *mbox = (mbox_t *)raw_mbox;
4868 int i;
4870 /* Flush adapter cache */
4871 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4872 raw_mbox[0] = FLUSH_ADAPTER;
4874 free_irq(adapter->host->irq, adapter);
4876 /* Issue a blocking (interrupts disabled) command to the card */
4877 issue_scb_block(adapter, raw_mbox);
4879 /* Flush disks cache */
4880 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4881 raw_mbox[0] = FLUSH_SYSTEM;
4883 /* Issue a blocking (interrupts disabled) command to the card */
4884 issue_scb_block(adapter, raw_mbox);
4886 if (atomic_read(&adapter->pend_cmds) > 0)
4887 printk(KERN_WARNING "megaraid: pending commands!!\n");
4890 * Have a delibrate delay to make sure all the caches are
4891 * actually flushed.
4893 for (i = 0; i <= 10; i++)
4894 mdelay(1000);
4897 static void __devexit
4898 megaraid_remove_one(struct pci_dev *pdev)
4900 struct Scsi_Host *host = pci_get_drvdata(pdev);
4901 adapter_t *adapter = (adapter_t *)host->hostdata;
4903 scsi_remove_host(host);
4905 __megaraid_shutdown(adapter);
4907 /* Free our resources */
4908 if (adapter->flag & BOARD_MEMMAP) {
4909 iounmap((void *)adapter->base);
4910 release_mem_region(adapter->host->base, 128);
4911 } else
4912 release_region(adapter->base, 16);
4914 mega_free_sgl(adapter);
4916 #ifdef CONFIG_PROC_FS
4917 if (adapter->controller_proc_dir_entry) {
4918 remove_proc_entry("stat", adapter->controller_proc_dir_entry);
4919 remove_proc_entry("config",
4920 adapter->controller_proc_dir_entry);
4921 remove_proc_entry("mailbox",
4922 adapter->controller_proc_dir_entry);
4923 #if MEGA_HAVE_ENH_PROC
4924 remove_proc_entry("rebuild-rate",
4925 adapter->controller_proc_dir_entry);
4926 remove_proc_entry("battery-status",
4927 adapter->controller_proc_dir_entry);
4929 remove_proc_entry("diskdrives-ch0",
4930 adapter->controller_proc_dir_entry);
4931 remove_proc_entry("diskdrives-ch1",
4932 adapter->controller_proc_dir_entry);
4933 remove_proc_entry("diskdrives-ch2",
4934 adapter->controller_proc_dir_entry);
4935 remove_proc_entry("diskdrives-ch3",
4936 adapter->controller_proc_dir_entry);
4938 remove_proc_entry("raiddrives-0-9",
4939 adapter->controller_proc_dir_entry);
4940 remove_proc_entry("raiddrives-10-19",
4941 adapter->controller_proc_dir_entry);
4942 remove_proc_entry("raiddrives-20-29",
4943 adapter->controller_proc_dir_entry);
4944 remove_proc_entry("raiddrives-30-39",
4945 adapter->controller_proc_dir_entry);
4946 #endif
4948 char buf[12] = { 0 };
4949 sprintf(buf, "hba%d", adapter->host->host_no);
4950 remove_proc_entry(buf, mega_proc_dir_entry);
4953 #endif
4955 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4956 adapter->mega_buffer, adapter->buf_dma_handle);
4957 kfree(adapter->scb_list);
4958 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4959 adapter->una_mbox64, adapter->una_mbox64_dma);
4961 scsi_host_put(host);
4962 pci_disable_device(pdev);
4964 hba_count--;
4967 static void
4968 megaraid_shutdown(struct pci_dev *pdev)
4970 struct Scsi_Host *host = pci_get_drvdata(pdev);
4971 adapter_t *adapter = (adapter_t *)host->hostdata;
4973 __megaraid_shutdown(adapter);
4976 static struct pci_device_id megaraid_pci_tbl[] = {
4977 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4978 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4979 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
4980 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4981 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
4982 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4983 {0,}
4985 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
4987 static struct pci_driver megaraid_pci_driver = {
4988 .name = "megaraid_legacy",
4989 .id_table = megaraid_pci_tbl,
4990 .probe = megaraid_probe_one,
4991 .remove = __devexit_p(megaraid_remove_one),
4992 .shutdown = megaraid_shutdown,
4995 static int __init megaraid_init(void)
4997 int error;
4999 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
5000 max_cmd_per_lun = MAX_CMD_PER_LUN;
5001 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
5002 max_mbox_busy_wait = MBOX_BUSY_WAIT;
5004 #ifdef CONFIG_PROC_FS
5005 mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
5006 if (!mega_proc_dir_entry) {
5007 printk(KERN_WARNING
5008 "megaraid: failed to create megaraid root\n");
5010 #endif
5011 error = pci_register_driver(&megaraid_pci_driver);
5012 if (error) {
5013 #ifdef CONFIG_PROC_FS
5014 remove_proc_entry("megaraid", NULL);
5015 #endif
5016 return error;
5020 * Register the driver as a character device, for applications
5021 * to access it for ioctls.
5022 * First argument (major) to register_chrdev implies a dynamic
5023 * major number allocation.
5025 major = register_chrdev(0, "megadev_legacy", &megadev_fops);
5026 if (!major) {
5027 printk(KERN_WARNING
5028 "megaraid: failed to register char device\n");
5031 return 0;
5034 static void __exit megaraid_exit(void)
5037 * Unregister the character device interface to the driver.
5039 unregister_chrdev(major, "megadev_legacy");
5041 pci_unregister_driver(&megaraid_pci_driver);
5043 #ifdef CONFIG_PROC_FS
5044 remove_proc_entry("megaraid", NULL);
5045 #endif
5048 module_init(megaraid_init);
5049 module_exit(megaraid_exit);
5051 /* vi: set ts=8 sw=8 tw=78: */