Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / scsi / megaraid.c
blob15eefa1d61fd8dfbd25d696bb519a28570005454
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/mutex.h>
50 #include <linux/slab.h>
51 #include <scsi/scsicam.h>
53 #include "scsi.h"
54 #include <scsi/scsi_host.h>
56 #include "megaraid.h"
58 #define MEGARAID_MODULE_VERSION "2.00.4"
60 MODULE_AUTHOR ("sju@lsil.com");
61 MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
62 MODULE_LICENSE ("GPL");
63 MODULE_VERSION(MEGARAID_MODULE_VERSION);
65 static DEFINE_MUTEX(megadev_mutex);
66 static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
67 module_param(max_cmd_per_lun, uint, 0);
68 MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
70 static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
71 module_param(max_sectors_per_io, ushort, 0);
72 MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
75 static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
76 module_param(max_mbox_busy_wait, ushort, 0);
77 MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
79 #define RDINDOOR(adapter) readl((adapter)->mmio_base + 0x20)
80 #define RDOUTDOOR(adapter) readl((adapter)->mmio_base + 0x2C)
81 #define WRINDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x20)
82 #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
85 * Global variables
88 static int hba_count;
89 static adapter_t *hba_soft_state[MAX_CONTROLLERS];
90 static struct proc_dir_entry *mega_proc_dir_entry;
92 /* For controller re-ordering */
93 static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
95 static long
96 megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
99 * The File Operations structure for the serial/ioctl interface of the driver
101 static const struct file_operations megadev_fops = {
102 .owner = THIS_MODULE,
103 .unlocked_ioctl = megadev_unlocked_ioctl,
104 .open = megadev_open,
105 .llseek = noop_llseek,
109 * Array to structures for storing the information about the controllers. This
110 * information is sent to the user level applications, when they do an ioctl
111 * for this information.
113 static struct mcontroller mcontroller[MAX_CONTROLLERS];
115 /* The current driver version */
116 static u32 driver_ver = 0x02000000;
118 /* major number used by the device for character interface */
119 static int major;
121 #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
125 * Debug variable to print some diagnostic messages
127 static int trace_level;
130 * mega_setup_mailbox()
131 * @adapter - pointer to our soft state
133 * Allocates a 8 byte aligned memory for the handshake mailbox.
135 static int
136 mega_setup_mailbox(adapter_t *adapter)
138 unsigned long align;
140 adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
141 sizeof(mbox64_t), &adapter->una_mbox64_dma);
143 if( !adapter->una_mbox64 ) return -1;
145 adapter->mbox = &adapter->una_mbox64->mbox;
147 adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
148 (~0UL ^ 0xFUL));
150 adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
152 align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
154 adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
157 * Register the mailbox if the controller is an io-mapped controller
159 if( adapter->flag & BOARD_IOMAP ) {
161 outb(adapter->mbox_dma & 0xFF,
162 adapter->host->io_port + MBOX_PORT0);
164 outb((adapter->mbox_dma >> 8) & 0xFF,
165 adapter->host->io_port + MBOX_PORT1);
167 outb((adapter->mbox_dma >> 16) & 0xFF,
168 adapter->host->io_port + MBOX_PORT2);
170 outb((adapter->mbox_dma >> 24) & 0xFF,
171 adapter->host->io_port + MBOX_PORT3);
173 outb(ENABLE_MBOX_BYTE,
174 adapter->host->io_port + ENABLE_MBOX_REGION);
176 irq_ack(adapter);
178 irq_enable(adapter);
181 return 0;
186 * mega_query_adapter()
187 * @adapter - pointer to our soft state
189 * Issue the adapter inquiry commands to the controller and find out
190 * information and parameter about the devices attached
192 static int
193 mega_query_adapter(adapter_t *adapter)
195 dma_addr_t prod_info_dma_handle;
196 mega_inquiry3 *inquiry3;
197 u8 raw_mbox[sizeof(struct mbox_out)];
198 mbox_t *mbox;
199 int retval;
201 /* Initialize adapter inquiry mailbox */
203 mbox = (mbox_t *)raw_mbox;
205 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
206 memset(&mbox->m_out, 0, sizeof(raw_mbox));
209 * Try to issue Inquiry3 command
210 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
211 * update enquiry3 structure
213 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
215 inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
217 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
218 raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
219 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
221 /* Issue a blocking command to the card */
222 if ((retval = issue_scb_block(adapter, raw_mbox))) {
223 /* the adapter does not support 40ld */
225 mraid_ext_inquiry *ext_inq;
226 mraid_inquiry *inq;
227 dma_addr_t dma_handle;
229 ext_inq = pci_alloc_consistent(adapter->dev,
230 sizeof(mraid_ext_inquiry), &dma_handle);
232 if( ext_inq == NULL ) return -1;
234 inq = &ext_inq->raid_inq;
236 mbox->m_out.xferaddr = (u32)dma_handle;
238 /*issue old 0x04 command to adapter */
239 mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
241 issue_scb_block(adapter, raw_mbox);
244 * update Enquiry3 and ProductInfo structures with
245 * mraid_inquiry structure
247 mega_8_to_40ld(inq, inquiry3,
248 (mega_product_info *)&adapter->product_info);
250 pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
251 ext_inq, dma_handle);
253 } else { /*adapter supports 40ld */
254 adapter->flag |= BOARD_40LD;
257 * get product_info, which is static information and will be
258 * unchanged
260 prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
261 &adapter->product_info,
262 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
264 mbox->m_out.xferaddr = prod_info_dma_handle;
266 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
267 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
269 if ((retval = issue_scb_block(adapter, raw_mbox)))
270 printk(KERN_WARNING
271 "megaraid: Product_info cmd failed with error: %d\n",
272 retval);
274 pci_unmap_single(adapter->dev, prod_info_dma_handle,
275 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
280 * kernel scans the channels from 0 to <= max_channel
282 adapter->host->max_channel =
283 adapter->product_info.nchannels + NVIRT_CHAN -1;
285 adapter->host->max_id = 16; /* max targets per channel */
287 adapter->host->max_lun = 7; /* Up to 7 luns for non disk devices */
289 adapter->host->cmd_per_lun = max_cmd_per_lun;
291 adapter->numldrv = inquiry3->num_ldrv;
293 adapter->max_cmds = adapter->product_info.max_commands;
295 if(adapter->max_cmds > MAX_COMMANDS)
296 adapter->max_cmds = MAX_COMMANDS;
298 adapter->host->can_queue = adapter->max_cmds - 1;
301 * Get the maximum number of scatter-gather elements supported by this
302 * firmware
304 mega_get_max_sgl(adapter);
306 adapter->host->sg_tablesize = adapter->sglen;
309 /* use HP firmware and bios version encoding
310 Note: fw_version[0|1] and bios_version[0|1] were originally shifted
311 right 8 bits making them zero. This 0 value was hardcoded to fix
312 sparse warnings. */
313 if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
314 sprintf (adapter->fw_version, "%c%d%d.%d%d",
315 adapter->product_info.fw_version[2],
317 adapter->product_info.fw_version[1] & 0x0f,
319 adapter->product_info.fw_version[0] & 0x0f);
320 sprintf (adapter->bios_version, "%c%d%d.%d%d",
321 adapter->product_info.bios_version[2],
323 adapter->product_info.bios_version[1] & 0x0f,
325 adapter->product_info.bios_version[0] & 0x0f);
326 } else {
327 memcpy(adapter->fw_version,
328 (char *)adapter->product_info.fw_version, 4);
329 adapter->fw_version[4] = 0;
331 memcpy(adapter->bios_version,
332 (char *)adapter->product_info.bios_version, 4);
334 adapter->bios_version[4] = 0;
337 printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
338 adapter->fw_version, adapter->bios_version, adapter->numldrv);
341 * Do we support extended (>10 bytes) cdbs
343 adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
344 if (adapter->support_ext_cdb)
345 printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
348 return 0;
352 * mega_runpendq()
353 * @adapter - pointer to our soft state
355 * Runs through the list of pending requests.
357 static inline void
358 mega_runpendq(adapter_t *adapter)
360 if(!list_empty(&adapter->pending_list))
361 __mega_runpendq(adapter);
365 * megaraid_queue()
366 * @scmd - Issue this scsi command
367 * @done - the callback hook into the scsi mid-layer
369 * The command queuing entry point for the mid-layer.
371 static int
372 megaraid_queue_lck(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
374 adapter_t *adapter;
375 scb_t *scb;
376 int busy=0;
377 unsigned long flags;
379 adapter = (adapter_t *)scmd->device->host->hostdata;
381 scmd->scsi_done = done;
385 * Allocate and build a SCB request
386 * busy flag will be set if mega_build_cmd() command could not
387 * allocate scb. We will return non-zero status in that case.
388 * NOTE: scb can be null even though certain commands completed
389 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
390 * return 0 in that case.
393 spin_lock_irqsave(&adapter->lock, flags);
394 scb = mega_build_cmd(adapter, scmd, &busy);
395 if (!scb)
396 goto out;
398 scb->state |= SCB_PENDQ;
399 list_add_tail(&scb->list, &adapter->pending_list);
402 * Check if the HBA is in quiescent state, e.g., during a
403 * delete logical drive opertion. If it is, don't run
404 * the pending_list.
406 if (atomic_read(&adapter->quiescent) == 0)
407 mega_runpendq(adapter);
409 busy = 0;
410 out:
411 spin_unlock_irqrestore(&adapter->lock, flags);
412 return busy;
415 static DEF_SCSI_QCMD(megaraid_queue)
418 * mega_allocate_scb()
419 * @adapter - pointer to our soft state
420 * @cmd - scsi command from the mid-layer
422 * Allocate a SCB structure. This is the central structure for controller
423 * commands.
425 static inline scb_t *
426 mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
428 struct list_head *head = &adapter->free_list;
429 scb_t *scb;
431 /* Unlink command from Free List */
432 if( !list_empty(head) ) {
434 scb = list_entry(head->next, scb_t, list);
436 list_del_init(head->next);
438 scb->state = SCB_ACTIVE;
439 scb->cmd = cmd;
440 scb->dma_type = MEGA_DMA_TYPE_NONE;
442 return scb;
445 return NULL;
449 * mega_get_ldrv_num()
450 * @adapter - pointer to our soft state
451 * @cmd - scsi mid layer command
452 * @channel - channel on the controller
454 * Calculate the logical drive number based on the information in scsi command
455 * and the channel number.
457 static inline int
458 mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
460 int tgt;
461 int ldrv_num;
463 tgt = cmd->device->id;
465 if ( tgt > adapter->this_id )
466 tgt--; /* we do not get inquires for initiator id */
468 ldrv_num = (channel * 15) + tgt;
472 * If we have a logical drive with boot enabled, project it first
474 if( adapter->boot_ldrv_enabled ) {
475 if( ldrv_num == 0 ) {
476 ldrv_num = adapter->boot_ldrv;
478 else {
479 if( ldrv_num <= adapter->boot_ldrv ) {
480 ldrv_num--;
486 * If "delete logical drive" feature is enabled on this controller.
487 * Do only if at least one delete logical drive operation was done.
489 * Also, after logical drive deletion, instead of logical drive number,
490 * the value returned should be 0x80+logical drive id.
492 * These is valid only for IO commands.
495 if (adapter->support_random_del && adapter->read_ldidmap )
496 switch (cmd->cmnd[0]) {
497 case READ_6: /* fall through */
498 case WRITE_6: /* fall through */
499 case READ_10: /* fall through */
500 case WRITE_10:
501 ldrv_num += 0x80;
504 return ldrv_num;
508 * mega_build_cmd()
509 * @adapter - pointer to our soft state
510 * @cmd - Prepare using this scsi command
511 * @busy - busy flag if no resources
513 * Prepares a command and scatter gather list for the controller. This routine
514 * also finds out if the commands is intended for a logical drive or a
515 * physical device and prepares the controller command accordingly.
517 * We also re-order the logical drives and physical devices based on their
518 * boot settings.
520 static scb_t *
521 mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
523 mega_ext_passthru *epthru;
524 mega_passthru *pthru;
525 scb_t *scb;
526 mbox_t *mbox;
527 long seg;
528 char islogical;
529 int max_ldrv_num;
530 int channel = 0;
531 int target = 0;
532 int ldrv_num = 0; /* logical drive number */
536 * filter the internal and ioctl commands
538 if((cmd->cmnd[0] == MEGA_INTERNAL_CMD))
539 return (scb_t *)cmd->host_scribble;
542 * We know what channels our logical drives are on - mega_find_card()
544 islogical = adapter->logdrv_chan[cmd->device->channel];
547 * The theory: If physical drive is chosen for boot, all the physical
548 * devices are exported before the logical drives, otherwise physical
549 * devices are pushed after logical drives, in which case - Kernel sees
550 * the physical devices on virtual channel which is obviously converted
551 * to actual channel on the HBA.
553 if( adapter->boot_pdrv_enabled ) {
554 if( islogical ) {
555 /* logical channel */
556 channel = cmd->device->channel -
557 adapter->product_info.nchannels;
559 else {
560 /* this is physical channel */
561 channel = cmd->device->channel;
562 target = cmd->device->id;
565 * boot from a physical disk, that disk needs to be
566 * exposed first IF both the channels are SCSI, then
567 * booting from the second channel is not allowed.
569 if( target == 0 ) {
570 target = adapter->boot_pdrv_tgt;
572 else if( target == adapter->boot_pdrv_tgt ) {
573 target = 0;
577 else {
578 if( islogical ) {
579 /* this is the logical channel */
580 channel = cmd->device->channel;
582 else {
583 /* physical channel */
584 channel = cmd->device->channel - NVIRT_CHAN;
585 target = cmd->device->id;
590 if(islogical) {
592 /* have just LUN 0 for each target on virtual channels */
593 if (cmd->device->lun) {
594 cmd->result = (DID_BAD_TARGET << 16);
595 cmd->scsi_done(cmd);
596 return NULL;
599 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
602 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
603 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
606 * max_ldrv_num increases by 0x80 if some logical drive was
607 * deleted.
609 if(adapter->read_ldidmap)
610 max_ldrv_num += 0x80;
612 if(ldrv_num > max_ldrv_num ) {
613 cmd->result = (DID_BAD_TARGET << 16);
614 cmd->scsi_done(cmd);
615 return NULL;
619 else {
620 if( cmd->device->lun > 7) {
622 * Do not support lun >7 for physically accessed
623 * devices
625 cmd->result = (DID_BAD_TARGET << 16);
626 cmd->scsi_done(cmd);
627 return NULL;
633 * Logical drive commands
636 if(islogical) {
637 switch (cmd->cmnd[0]) {
638 case TEST_UNIT_READY:
639 #if MEGA_HAVE_CLUSTERING
641 * Do we support clustering and is the support enabled
642 * If no, return success always
644 if( !adapter->has_cluster ) {
645 cmd->result = (DID_OK << 16);
646 cmd->scsi_done(cmd);
647 return NULL;
650 if(!(scb = mega_allocate_scb(adapter, cmd))) {
651 *busy = 1;
652 return NULL;
655 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
656 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
657 scb->raw_mbox[3] = ldrv_num;
659 scb->dma_direction = PCI_DMA_NONE;
661 return scb;
662 #else
663 cmd->result = (DID_OK << 16);
664 cmd->scsi_done(cmd);
665 return NULL;
666 #endif
668 case MODE_SENSE: {
669 char *buf;
670 struct scatterlist *sg;
672 sg = scsi_sglist(cmd);
673 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
675 memset(buf, 0, cmd->cmnd[4]);
676 kunmap_atomic(buf - sg->offset, KM_IRQ0);
678 cmd->result = (DID_OK << 16);
679 cmd->scsi_done(cmd);
680 return NULL;
683 case READ_CAPACITY:
684 case INQUIRY:
686 if(!(adapter->flag & (1L << cmd->device->channel))) {
688 printk(KERN_NOTICE
689 "scsi%d: scanning scsi channel %d ",
690 adapter->host->host_no,
691 cmd->device->channel);
692 printk("for logical drives.\n");
694 adapter->flag |= (1L << cmd->device->channel);
697 /* Allocate a SCB and initialize passthru */
698 if(!(scb = mega_allocate_scb(adapter, cmd))) {
699 *busy = 1;
700 return NULL;
702 pthru = scb->pthru;
704 mbox = (mbox_t *)scb->raw_mbox;
705 memset(mbox, 0, sizeof(scb->raw_mbox));
706 memset(pthru, 0, sizeof(mega_passthru));
708 pthru->timeout = 0;
709 pthru->ars = 1;
710 pthru->reqsenselen = 14;
711 pthru->islogical = 1;
712 pthru->logdrv = ldrv_num;
713 pthru->cdblen = cmd->cmd_len;
714 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
716 if( adapter->has_64bit_addr ) {
717 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
719 else {
720 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
723 scb->dma_direction = PCI_DMA_FROMDEVICE;
725 pthru->numsgelements = mega_build_sglist(adapter, scb,
726 &pthru->dataxferaddr, &pthru->dataxferlen);
728 mbox->m_out.xferaddr = scb->pthru_dma_addr;
730 return scb;
732 case READ_6:
733 case WRITE_6:
734 case READ_10:
735 case WRITE_10:
736 case READ_12:
737 case WRITE_12:
739 /* Allocate a SCB and initialize mailbox */
740 if(!(scb = mega_allocate_scb(adapter, cmd))) {
741 *busy = 1;
742 return NULL;
744 mbox = (mbox_t *)scb->raw_mbox;
746 memset(mbox, 0, sizeof(scb->raw_mbox));
747 mbox->m_out.logdrv = ldrv_num;
750 * A little hack: 2nd bit is zero for all scsi read
751 * commands and is set for all scsi write commands
753 if( adapter->has_64bit_addr ) {
754 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
755 MEGA_MBOXCMD_LWRITE64:
756 MEGA_MBOXCMD_LREAD64 ;
758 else {
759 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
760 MEGA_MBOXCMD_LWRITE:
761 MEGA_MBOXCMD_LREAD ;
765 * 6-byte READ(0x08) or WRITE(0x0A) cdb
767 if( cmd->cmd_len == 6 ) {
768 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
769 mbox->m_out.lba =
770 ((u32)cmd->cmnd[1] << 16) |
771 ((u32)cmd->cmnd[2] << 8) |
772 (u32)cmd->cmnd[3];
774 mbox->m_out.lba &= 0x1FFFFF;
776 #if MEGA_HAVE_STATS
778 * Take modulo 0x80, since the logical drive
779 * number increases by 0x80 when a logical
780 * drive was deleted
782 if (*cmd->cmnd == READ_6) {
783 adapter->nreads[ldrv_num%0x80]++;
784 adapter->nreadblocks[ldrv_num%0x80] +=
785 mbox->m_out.numsectors;
786 } else {
787 adapter->nwrites[ldrv_num%0x80]++;
788 adapter->nwriteblocks[ldrv_num%0x80] +=
789 mbox->m_out.numsectors;
791 #endif
795 * 10-byte READ(0x28) or WRITE(0x2A) cdb
797 if( cmd->cmd_len == 10 ) {
798 mbox->m_out.numsectors =
799 (u32)cmd->cmnd[8] |
800 ((u32)cmd->cmnd[7] << 8);
801 mbox->m_out.lba =
802 ((u32)cmd->cmnd[2] << 24) |
803 ((u32)cmd->cmnd[3] << 16) |
804 ((u32)cmd->cmnd[4] << 8) |
805 (u32)cmd->cmnd[5];
807 #if MEGA_HAVE_STATS
808 if (*cmd->cmnd == READ_10) {
809 adapter->nreads[ldrv_num%0x80]++;
810 adapter->nreadblocks[ldrv_num%0x80] +=
811 mbox->m_out.numsectors;
812 } else {
813 adapter->nwrites[ldrv_num%0x80]++;
814 adapter->nwriteblocks[ldrv_num%0x80] +=
815 mbox->m_out.numsectors;
817 #endif
821 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
823 if( cmd->cmd_len == 12 ) {
824 mbox->m_out.lba =
825 ((u32)cmd->cmnd[2] << 24) |
826 ((u32)cmd->cmnd[3] << 16) |
827 ((u32)cmd->cmnd[4] << 8) |
828 (u32)cmd->cmnd[5];
830 mbox->m_out.numsectors =
831 ((u32)cmd->cmnd[6] << 24) |
832 ((u32)cmd->cmnd[7] << 16) |
833 ((u32)cmd->cmnd[8] << 8) |
834 (u32)cmd->cmnd[9];
836 #if MEGA_HAVE_STATS
837 if (*cmd->cmnd == READ_12) {
838 adapter->nreads[ldrv_num%0x80]++;
839 adapter->nreadblocks[ldrv_num%0x80] +=
840 mbox->m_out.numsectors;
841 } else {
842 adapter->nwrites[ldrv_num%0x80]++;
843 adapter->nwriteblocks[ldrv_num%0x80] +=
844 mbox->m_out.numsectors;
846 #endif
850 * If it is a read command
852 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
853 scb->dma_direction = PCI_DMA_FROMDEVICE;
855 else {
856 scb->dma_direction = PCI_DMA_TODEVICE;
859 /* Calculate Scatter-Gather info */
860 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
861 (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
863 return scb;
865 #if MEGA_HAVE_CLUSTERING
866 case RESERVE: /* Fall through */
867 case RELEASE:
870 * Do we support clustering and is the support enabled
872 if( ! adapter->has_cluster ) {
874 cmd->result = (DID_BAD_TARGET << 16);
875 cmd->scsi_done(cmd);
876 return NULL;
879 /* Allocate a SCB and initialize mailbox */
880 if(!(scb = mega_allocate_scb(adapter, cmd))) {
881 *busy = 1;
882 return NULL;
885 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
886 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
887 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
889 scb->raw_mbox[3] = ldrv_num;
891 scb->dma_direction = PCI_DMA_NONE;
893 return scb;
894 #endif
896 default:
897 cmd->result = (DID_BAD_TARGET << 16);
898 cmd->scsi_done(cmd);
899 return NULL;
904 * Passthru drive commands
906 else {
907 /* Allocate a SCB and initialize passthru */
908 if(!(scb = mega_allocate_scb(adapter, cmd))) {
909 *busy = 1;
910 return NULL;
913 mbox = (mbox_t *)scb->raw_mbox;
914 memset(mbox, 0, sizeof(scb->raw_mbox));
916 if( adapter->support_ext_cdb ) {
918 epthru = mega_prepare_extpassthru(adapter, scb, cmd,
919 channel, target);
921 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
923 mbox->m_out.xferaddr = scb->epthru_dma_addr;
926 else {
928 pthru = mega_prepare_passthru(adapter, scb, cmd,
929 channel, target);
931 /* Initialize mailbox */
932 if( adapter->has_64bit_addr ) {
933 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
935 else {
936 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
939 mbox->m_out.xferaddr = scb->pthru_dma_addr;
942 return scb;
944 return NULL;
949 * mega_prepare_passthru()
950 * @adapter - pointer to our soft state
951 * @scb - our scsi control block
952 * @cmd - scsi command from the mid-layer
953 * @channel - actual channel on the controller
954 * @target - actual id on the controller.
956 * prepare a command for the scsi physical devices.
958 static mega_passthru *
959 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
960 int channel, int target)
962 mega_passthru *pthru;
964 pthru = scb->pthru;
965 memset(pthru, 0, sizeof (mega_passthru));
967 /* 0=6sec/1=60sec/2=10min/3=3hrs */
968 pthru->timeout = 2;
970 pthru->ars = 1;
971 pthru->reqsenselen = 14;
972 pthru->islogical = 0;
974 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
976 pthru->target = (adapter->flag & BOARD_40LD) ?
977 (channel << 4) | target : target;
979 pthru->cdblen = cmd->cmd_len;
980 pthru->logdrv = cmd->device->lun;
982 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
984 /* Not sure about the direction */
985 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
987 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
988 switch (cmd->cmnd[0]) {
989 case INQUIRY:
990 case READ_CAPACITY:
991 if(!(adapter->flag & (1L << cmd->device->channel))) {
993 printk(KERN_NOTICE
994 "scsi%d: scanning scsi channel %d [P%d] ",
995 adapter->host->host_no,
996 cmd->device->channel, channel);
997 printk("for physical devices.\n");
999 adapter->flag |= (1L << cmd->device->channel);
1001 /* Fall through */
1002 default:
1003 pthru->numsgelements = mega_build_sglist(adapter, scb,
1004 &pthru->dataxferaddr, &pthru->dataxferlen);
1005 break;
1007 return pthru;
1012 * mega_prepare_extpassthru()
1013 * @adapter - pointer to our soft state
1014 * @scb - our scsi control block
1015 * @cmd - scsi command from the mid-layer
1016 * @channel - actual channel on the controller
1017 * @target - actual id on the controller.
1019 * prepare a command for the scsi physical devices. This rountine prepares
1020 * commands for devices which can take extended CDBs (>10 bytes)
1022 static mega_ext_passthru *
1023 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1024 int channel, int target)
1026 mega_ext_passthru *epthru;
1028 epthru = scb->epthru;
1029 memset(epthru, 0, sizeof(mega_ext_passthru));
1031 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1032 epthru->timeout = 2;
1034 epthru->ars = 1;
1035 epthru->reqsenselen = 14;
1036 epthru->islogical = 0;
1038 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1039 epthru->target = (adapter->flag & BOARD_40LD) ?
1040 (channel << 4) | target : target;
1042 epthru->cdblen = cmd->cmd_len;
1043 epthru->logdrv = cmd->device->lun;
1045 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1047 /* Not sure about the direction */
1048 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1050 switch(cmd->cmnd[0]) {
1051 case INQUIRY:
1052 case READ_CAPACITY:
1053 if(!(adapter->flag & (1L << cmd->device->channel))) {
1055 printk(KERN_NOTICE
1056 "scsi%d: scanning scsi channel %d [P%d] ",
1057 adapter->host->host_no,
1058 cmd->device->channel, channel);
1059 printk("for physical devices.\n");
1061 adapter->flag |= (1L << cmd->device->channel);
1063 /* Fall through */
1064 default:
1065 epthru->numsgelements = mega_build_sglist(adapter, scb,
1066 &epthru->dataxferaddr, &epthru->dataxferlen);
1067 break;
1070 return epthru;
1073 static void
1074 __mega_runpendq(adapter_t *adapter)
1076 scb_t *scb;
1077 struct list_head *pos, *next;
1079 /* Issue any pending commands to the card */
1080 list_for_each_safe(pos, next, &adapter->pending_list) {
1082 scb = list_entry(pos, scb_t, list);
1084 if( !(scb->state & SCB_ISSUED) ) {
1086 if( issue_scb(adapter, scb) != 0 )
1087 return;
1091 return;
1096 * issue_scb()
1097 * @adapter - pointer to our soft state
1098 * @scb - scsi control block
1100 * Post a command to the card if the mailbox is available, otherwise return
1101 * busy. We also take the scb from the pending list if the mailbox is
1102 * available.
1104 static int
1105 issue_scb(adapter_t *adapter, scb_t *scb)
1107 volatile mbox64_t *mbox64 = adapter->mbox64;
1108 volatile mbox_t *mbox = adapter->mbox;
1109 unsigned int i = 0;
1111 if(unlikely(mbox->m_in.busy)) {
1112 do {
1113 udelay(1);
1114 i++;
1115 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1117 if(mbox->m_in.busy) return -1;
1120 /* Copy mailbox data into host structure */
1121 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1122 sizeof(struct mbox_out));
1124 mbox->m_out.cmdid = scb->idx; /* Set cmdid */
1125 mbox->m_in.busy = 1; /* Set busy */
1129 * Increment the pending queue counter
1131 atomic_inc(&adapter->pend_cmds);
1133 switch (mbox->m_out.cmd) {
1134 case MEGA_MBOXCMD_LREAD64:
1135 case MEGA_MBOXCMD_LWRITE64:
1136 case MEGA_MBOXCMD_PASSTHRU64:
1137 case MEGA_MBOXCMD_EXTPTHRU:
1138 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1139 mbox64->xfer_segment_hi = 0;
1140 mbox->m_out.xferaddr = 0xFFFFFFFF;
1141 break;
1142 default:
1143 mbox64->xfer_segment_lo = 0;
1144 mbox64->xfer_segment_hi = 0;
1148 * post the command
1150 scb->state |= SCB_ISSUED;
1152 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1153 mbox->m_in.poll = 0;
1154 mbox->m_in.ack = 0;
1155 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1157 else {
1158 irq_enable(adapter);
1159 issue_command(adapter);
1162 return 0;
1166 * Wait until the controller's mailbox is available
1168 static inline int
1169 mega_busywait_mbox (adapter_t *adapter)
1171 if (adapter->mbox->m_in.busy)
1172 return __mega_busywait_mbox(adapter);
1173 return 0;
1177 * issue_scb_block()
1178 * @adapter - pointer to our soft state
1179 * @raw_mbox - the mailbox
1181 * Issue a scb in synchronous and non-interrupt mode
1183 static int
1184 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1186 volatile mbox64_t *mbox64 = adapter->mbox64;
1187 volatile mbox_t *mbox = adapter->mbox;
1188 u8 byte;
1190 /* Wait until mailbox is free */
1191 if(mega_busywait_mbox (adapter))
1192 goto bug_blocked_mailbox;
1194 /* Copy mailbox data into host structure */
1195 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1196 mbox->m_out.cmdid = 0xFE;
1197 mbox->m_in.busy = 1;
1199 switch (raw_mbox[0]) {
1200 case MEGA_MBOXCMD_LREAD64:
1201 case MEGA_MBOXCMD_LWRITE64:
1202 case MEGA_MBOXCMD_PASSTHRU64:
1203 case MEGA_MBOXCMD_EXTPTHRU:
1204 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1205 mbox64->xfer_segment_hi = 0;
1206 mbox->m_out.xferaddr = 0xFFFFFFFF;
1207 break;
1208 default:
1209 mbox64->xfer_segment_lo = 0;
1210 mbox64->xfer_segment_hi = 0;
1213 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1214 mbox->m_in.poll = 0;
1215 mbox->m_in.ack = 0;
1216 mbox->m_in.numstatus = 0xFF;
1217 mbox->m_in.status = 0xFF;
1218 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1220 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1221 cpu_relax();
1223 mbox->m_in.numstatus = 0xFF;
1225 while( (volatile u8)mbox->m_in.poll != 0x77 )
1226 cpu_relax();
1228 mbox->m_in.poll = 0;
1229 mbox->m_in.ack = 0x77;
1231 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1233 while(RDINDOOR(adapter) & 0x2)
1234 cpu_relax();
1236 else {
1237 irq_disable(adapter);
1238 issue_command(adapter);
1240 while (!((byte = irq_state(adapter)) & INTR_VALID))
1241 cpu_relax();
1243 set_irq_state(adapter, byte);
1244 irq_enable(adapter);
1245 irq_ack(adapter);
1248 return mbox->m_in.status;
1250 bug_blocked_mailbox:
1251 printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1252 udelay (1000);
1253 return -1;
1258 * megaraid_isr_iomapped()
1259 * @irq - irq
1260 * @devp - pointer to our soft state
1262 * Interrupt service routine for io-mapped controllers.
1263 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1264 * and service the completed commands.
1266 static irqreturn_t
1267 megaraid_isr_iomapped(int irq, void *devp)
1269 adapter_t *adapter = devp;
1270 unsigned long flags;
1271 u8 status;
1272 u8 nstatus;
1273 u8 completed[MAX_FIRMWARE_STATUS];
1274 u8 byte;
1275 int handled = 0;
1279 * loop till F/W has more commands for us to complete.
1281 spin_lock_irqsave(&adapter->lock, flags);
1283 do {
1284 /* Check if a valid interrupt is pending */
1285 byte = irq_state(adapter);
1286 if( (byte & VALID_INTR_BYTE) == 0 ) {
1288 * No more pending commands
1290 goto out_unlock;
1292 set_irq_state(adapter, byte);
1294 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1295 == 0xFF)
1296 cpu_relax();
1297 adapter->mbox->m_in.numstatus = 0xFF;
1299 status = adapter->mbox->m_in.status;
1302 * decrement the pending queue counter
1304 atomic_sub(nstatus, &adapter->pend_cmds);
1306 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1307 nstatus);
1309 /* Acknowledge interrupt */
1310 irq_ack(adapter);
1312 mega_cmd_done(adapter, completed, nstatus, status);
1314 mega_rundoneq(adapter);
1316 handled = 1;
1318 /* Loop through any pending requests */
1319 if(atomic_read(&adapter->quiescent) == 0) {
1320 mega_runpendq(adapter);
1323 } while(1);
1325 out_unlock:
1327 spin_unlock_irqrestore(&adapter->lock, flags);
1329 return IRQ_RETVAL(handled);
1334 * megaraid_isr_memmapped()
1335 * @irq - irq
1336 * @devp - pointer to our soft state
1338 * Interrupt service routine for memory-mapped controllers.
1339 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1340 * and service the completed commands.
1342 static irqreturn_t
1343 megaraid_isr_memmapped(int irq, void *devp)
1345 adapter_t *adapter = devp;
1346 unsigned long flags;
1347 u8 status;
1348 u32 dword = 0;
1349 u8 nstatus;
1350 u8 completed[MAX_FIRMWARE_STATUS];
1351 int handled = 0;
1355 * loop till F/W has more commands for us to complete.
1357 spin_lock_irqsave(&adapter->lock, flags);
1359 do {
1360 /* Check if a valid interrupt is pending */
1361 dword = RDOUTDOOR(adapter);
1362 if(dword != 0x10001234) {
1364 * No more pending commands
1366 goto out_unlock;
1368 WROUTDOOR(adapter, 0x10001234);
1370 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1371 == 0xFF) {
1372 cpu_relax();
1374 adapter->mbox->m_in.numstatus = 0xFF;
1376 status = adapter->mbox->m_in.status;
1379 * decrement the pending queue counter
1381 atomic_sub(nstatus, &adapter->pend_cmds);
1383 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1384 nstatus);
1386 /* Acknowledge interrupt */
1387 WRINDOOR(adapter, 0x2);
1389 handled = 1;
1391 while( RDINDOOR(adapter) & 0x02 )
1392 cpu_relax();
1394 mega_cmd_done(adapter, completed, nstatus, status);
1396 mega_rundoneq(adapter);
1398 /* Loop through any pending requests */
1399 if(atomic_read(&adapter->quiescent) == 0) {
1400 mega_runpendq(adapter);
1403 } while(1);
1405 out_unlock:
1407 spin_unlock_irqrestore(&adapter->lock, flags);
1409 return IRQ_RETVAL(handled);
1412 * mega_cmd_done()
1413 * @adapter - pointer to our soft state
1414 * @completed - array of ids of completed commands
1415 * @nstatus - number of completed commands
1416 * @status - status of the last command completed
1418 * Complete the commands and call the scsi mid-layer callback hooks.
1420 static void
1421 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1423 mega_ext_passthru *epthru = NULL;
1424 struct scatterlist *sgl;
1425 Scsi_Cmnd *cmd = NULL;
1426 mega_passthru *pthru = NULL;
1427 mbox_t *mbox = NULL;
1428 u8 c;
1429 scb_t *scb;
1430 int islogical;
1431 int cmdid;
1432 int i;
1435 * for all the commands completed, call the mid-layer callback routine
1436 * and free the scb.
1438 for( i = 0; i < nstatus; i++ ) {
1440 cmdid = completed[i];
1442 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1443 scb = &adapter->int_scb;
1444 cmd = scb->cmd;
1445 mbox = (mbox_t *)scb->raw_mbox;
1448 * Internal command interface do not fire the extended
1449 * passthru or 64-bit passthru
1451 pthru = scb->pthru;
1454 else {
1455 scb = &adapter->scb_list[cmdid];
1458 * Make sure f/w has completed a valid command
1460 if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1461 printk(KERN_CRIT
1462 "megaraid: invalid command ");
1463 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1464 cmdid, scb->state, scb->cmd);
1466 continue;
1470 * Was a abort issued for this command
1472 if( scb->state & SCB_ABORT ) {
1474 printk(KERN_WARNING
1475 "megaraid: aborted cmd [%x] complete.\n",
1476 scb->idx);
1478 scb->cmd->result = (DID_ABORT << 16);
1480 list_add_tail(SCSI_LIST(scb->cmd),
1481 &adapter->completed_list);
1483 mega_free_scb(adapter, scb);
1485 continue;
1489 * Was a reset issued for this command
1491 if( scb->state & SCB_RESET ) {
1493 printk(KERN_WARNING
1494 "megaraid: reset cmd [%x] complete.\n",
1495 scb->idx);
1497 scb->cmd->result = (DID_RESET << 16);
1499 list_add_tail(SCSI_LIST(scb->cmd),
1500 &adapter->completed_list);
1502 mega_free_scb (adapter, scb);
1504 continue;
1507 cmd = scb->cmd;
1508 pthru = scb->pthru;
1509 epthru = scb->epthru;
1510 mbox = (mbox_t *)scb->raw_mbox;
1512 #if MEGA_HAVE_STATS
1515 int logdrv = mbox->m_out.logdrv;
1517 islogical = adapter->logdrv_chan[cmd->channel];
1519 * Maintain an error counter for the logical drive.
1520 * Some application like SNMP agent need such
1521 * statistics
1523 if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1524 cmd->cmnd[0] == READ_10 ||
1525 cmd->cmnd[0] == READ_12)) {
1527 * Logical drive number increases by 0x80 when
1528 * a logical drive is deleted
1530 adapter->rd_errors[logdrv%0x80]++;
1533 if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1534 cmd->cmnd[0] == WRITE_10 ||
1535 cmd->cmnd[0] == WRITE_12)) {
1537 * Logical drive number increases by 0x80 when
1538 * a logical drive is deleted
1540 adapter->wr_errors[logdrv%0x80]++;
1544 #endif
1548 * Do not return the presence of hard disk on the channel so,
1549 * inquiry sent, and returned data==hard disk or removable
1550 * hard disk and not logical, request should return failure! -
1551 * PJ
1553 islogical = adapter->logdrv_chan[cmd->device->channel];
1554 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1556 sgl = scsi_sglist(cmd);
1557 if( sg_page(sgl) ) {
1558 c = *(unsigned char *) sg_virt(&sgl[0]);
1559 } else {
1560 printk(KERN_WARNING
1561 "megaraid: invalid sg.\n");
1562 c = 0;
1565 if(IS_RAID_CH(adapter, cmd->device->channel) &&
1566 ((c & 0x1F ) == TYPE_DISK)) {
1567 status = 0xF0;
1571 /* clear result; otherwise, success returns corrupt value */
1572 cmd->result = 0;
1574 /* Convert MegaRAID status to Linux error code */
1575 switch (status) {
1576 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1577 cmd->result |= (DID_OK << 16);
1578 break;
1580 case 0x02: /* ERROR_ABORTED, i.e.
1581 SCSI_STATUS_CHECK_CONDITION */
1583 /* set sense_buffer and result fields */
1584 if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1585 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1587 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1588 14);
1590 cmd->result = (DRIVER_SENSE << 24) |
1591 (DID_OK << 16) |
1592 (CHECK_CONDITION << 1);
1594 else {
1595 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1597 memcpy(cmd->sense_buffer,
1598 epthru->reqsensearea, 14);
1600 cmd->result = (DRIVER_SENSE << 24) |
1601 (DID_OK << 16) |
1602 (CHECK_CONDITION << 1);
1603 } else {
1604 cmd->sense_buffer[0] = 0x70;
1605 cmd->sense_buffer[2] = ABORTED_COMMAND;
1606 cmd->result |= (CHECK_CONDITION << 1);
1609 break;
1611 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1612 SCSI_STATUS_BUSY */
1613 cmd->result |= (DID_BUS_BUSY << 16) | status;
1614 break;
1616 default:
1617 #if MEGA_HAVE_CLUSTERING
1619 * If TEST_UNIT_READY fails, we know
1620 * MEGA_RESERVATION_STATUS failed
1622 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1623 cmd->result |= (DID_ERROR << 16) |
1624 (RESERVATION_CONFLICT << 1);
1626 else
1628 * Error code returned is 1 if Reserve or Release
1629 * failed or the input parameter is invalid
1631 if( status == 1 &&
1632 (cmd->cmnd[0] == RESERVE ||
1633 cmd->cmnd[0] == RELEASE) ) {
1635 cmd->result |= (DID_ERROR << 16) |
1636 (RESERVATION_CONFLICT << 1);
1638 else
1639 #endif
1640 cmd->result |= (DID_BAD_TARGET << 16)|status;
1644 * Only free SCBs for the commands coming down from the
1645 * mid-layer, not for which were issued internally
1647 * For internal command, restore the status returned by the
1648 * firmware so that user can interpret it.
1650 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1651 cmd->result = status;
1654 * Remove the internal command from the pending list
1656 list_del_init(&scb->list);
1657 scb->state = SCB_FREE;
1659 else {
1660 mega_free_scb(adapter, scb);
1663 /* Add Scsi_Command to end of completed queue */
1664 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1670 * mega_runpendq()
1672 * Run through the list of completed requests and finish it
1674 static void
1675 mega_rundoneq (adapter_t *adapter)
1677 Scsi_Cmnd *cmd;
1678 struct list_head *pos;
1680 list_for_each(pos, &adapter->completed_list) {
1682 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1684 cmd = list_entry(spos, Scsi_Cmnd, SCp);
1685 cmd->scsi_done(cmd);
1688 INIT_LIST_HEAD(&adapter->completed_list);
1693 * Free a SCB structure
1694 * Note: We assume the scsi commands associated with this scb is not free yet.
1696 static void
1697 mega_free_scb(adapter_t *adapter, scb_t *scb)
1699 switch( scb->dma_type ) {
1701 case MEGA_DMA_TYPE_NONE:
1702 break;
1704 case MEGA_SGLIST:
1705 scsi_dma_unmap(scb->cmd);
1706 break;
1707 default:
1708 break;
1712 * Remove from the pending list
1714 list_del_init(&scb->list);
1716 /* Link the scb back into free list */
1717 scb->state = SCB_FREE;
1718 scb->cmd = NULL;
1720 list_add(&scb->list, &adapter->free_list);
1724 static int
1725 __mega_busywait_mbox (adapter_t *adapter)
1727 volatile mbox_t *mbox = adapter->mbox;
1728 long counter;
1730 for (counter = 0; counter < 10000; counter++) {
1731 if (!mbox->m_in.busy)
1732 return 0;
1733 udelay(100);
1734 cond_resched();
1736 return -1; /* give up after 1 second */
1740 * Copies data to SGLIST
1741 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1743 static int
1744 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1746 struct scatterlist *sg;
1747 Scsi_Cmnd *cmd;
1748 int sgcnt;
1749 int idx;
1751 cmd = scb->cmd;
1754 * Copy Scatter-Gather list info into controller structure.
1756 * The number of sg elements returned must not exceed our limit
1758 sgcnt = scsi_dma_map(cmd);
1760 scb->dma_type = MEGA_SGLIST;
1762 BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1764 *len = 0;
1766 if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1767 sg = scsi_sglist(cmd);
1768 scb->dma_h_bulkdata = sg_dma_address(sg);
1769 *buf = (u32)scb->dma_h_bulkdata;
1770 *len = sg_dma_len(sg);
1771 return 0;
1774 scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1775 if (adapter->has_64bit_addr) {
1776 scb->sgl64[idx].address = sg_dma_address(sg);
1777 *len += scb->sgl64[idx].length = sg_dma_len(sg);
1778 } else {
1779 scb->sgl[idx].address = sg_dma_address(sg);
1780 *len += scb->sgl[idx].length = sg_dma_len(sg);
1784 /* Reset pointer and length fields */
1785 *buf = scb->sgl_dma_addr;
1787 /* Return count of SG requests */
1788 return sgcnt;
1793 * mega_8_to_40ld()
1795 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1796 * Enquiry3 structures for later use
1798 static void
1799 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1800 mega_product_info *product_info)
1802 int i;
1804 product_info->max_commands = inquiry->adapter_info.max_commands;
1805 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1806 product_info->nchannels = inquiry->adapter_info.nchannels;
1808 for (i = 0; i < 4; i++) {
1809 product_info->fw_version[i] =
1810 inquiry->adapter_info.fw_version[i];
1812 product_info->bios_version[i] =
1813 inquiry->adapter_info.bios_version[i];
1815 enquiry3->cache_flush_interval =
1816 inquiry->adapter_info.cache_flush_interval;
1818 product_info->dram_size = inquiry->adapter_info.dram_size;
1820 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1822 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1823 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1824 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1825 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1828 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1829 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1832 static inline void
1833 mega_free_sgl(adapter_t *adapter)
1835 scb_t *scb;
1836 int i;
1838 for(i = 0; i < adapter->max_cmds; i++) {
1840 scb = &adapter->scb_list[i];
1842 if( scb->sgl64 ) {
1843 pci_free_consistent(adapter->dev,
1844 sizeof(mega_sgl64) * adapter->sglen,
1845 scb->sgl64,
1846 scb->sgl_dma_addr);
1848 scb->sgl64 = NULL;
1851 if( scb->pthru ) {
1852 pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1853 scb->pthru, scb->pthru_dma_addr);
1855 scb->pthru = NULL;
1858 if( scb->epthru ) {
1859 pci_free_consistent(adapter->dev,
1860 sizeof(mega_ext_passthru),
1861 scb->epthru, scb->epthru_dma_addr);
1863 scb->epthru = NULL;
1871 * Get information about the card/driver
1873 const char *
1874 megaraid_info(struct Scsi_Host *host)
1876 static char buffer[512];
1877 adapter_t *adapter;
1879 adapter = (adapter_t *)host->hostdata;
1881 sprintf (buffer,
1882 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1883 adapter->fw_version, adapter->product_info.max_commands,
1884 adapter->host->max_id, adapter->host->max_channel,
1885 adapter->host->max_lun);
1886 return buffer;
1890 * Abort a previous SCSI request. Only commands on the pending list can be
1891 * aborted. All the commands issued to the F/W must complete.
1893 static int
1894 megaraid_abort(Scsi_Cmnd *cmd)
1896 adapter_t *adapter;
1897 int rval;
1899 adapter = (adapter_t *)cmd->device->host->hostdata;
1901 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1904 * This is required here to complete any completed requests
1905 * to be communicated over to the mid layer.
1907 mega_rundoneq(adapter);
1909 return rval;
1913 static int
1914 megaraid_reset(struct scsi_cmnd *cmd)
1916 adapter_t *adapter;
1917 megacmd_t mc;
1918 int rval;
1920 adapter = (adapter_t *)cmd->device->host->hostdata;
1922 #if MEGA_HAVE_CLUSTERING
1923 mc.cmd = MEGA_CLUSTER_CMD;
1924 mc.opcode = MEGA_RESET_RESERVATIONS;
1926 if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1927 printk(KERN_WARNING
1928 "megaraid: reservation reset failed.\n");
1930 else {
1931 printk(KERN_INFO "megaraid: reservation reset.\n");
1933 #endif
1935 spin_lock_irq(&adapter->lock);
1937 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1940 * This is required here to complete any completed requests
1941 * to be communicated over to the mid layer.
1943 mega_rundoneq(adapter);
1944 spin_unlock_irq(&adapter->lock);
1946 return rval;
1950 * megaraid_abort_and_reset()
1951 * @adapter - megaraid soft state
1952 * @cmd - scsi command to be aborted or reset
1953 * @aor - abort or reset flag
1955 * Try to locate the scsi command in the pending queue. If found and is not
1956 * issued to the controller, abort/reset it. Otherwise return failure
1958 static int
1959 megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
1961 struct list_head *pos, *next;
1962 scb_t *scb;
1964 printk(KERN_WARNING "megaraid: %s cmd=%x <c=%d t=%d l=%d>\n",
1965 (aor == SCB_ABORT)? "ABORTING":"RESET",
1966 cmd->cmnd[0], cmd->device->channel,
1967 cmd->device->id, cmd->device->lun);
1969 if(list_empty(&adapter->pending_list))
1970 return FALSE;
1972 list_for_each_safe(pos, next, &adapter->pending_list) {
1974 scb = list_entry(pos, scb_t, list);
1976 if (scb->cmd == cmd) { /* Found command */
1978 scb->state |= aor;
1981 * Check if this command has firmware ownership. If
1982 * yes, we cannot reset this command. Whenever f/w
1983 * completes this command, we will return appropriate
1984 * status from ISR.
1986 if( scb->state & SCB_ISSUED ) {
1988 printk(KERN_WARNING
1989 "megaraid: %s[%x], fw owner.\n",
1990 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1991 scb->idx);
1993 return FALSE;
1995 else {
1998 * Not yet issued! Remove from the pending
1999 * list
2001 printk(KERN_WARNING
2002 "megaraid: %s-[%x], driver owner.\n",
2003 (aor==SCB_ABORT) ? "ABORTING":"RESET",
2004 scb->idx);
2006 mega_free_scb(adapter, scb);
2008 if( aor == SCB_ABORT ) {
2009 cmd->result = (DID_ABORT << 16);
2011 else {
2012 cmd->result = (DID_RESET << 16);
2015 list_add_tail(SCSI_LIST(cmd),
2016 &adapter->completed_list);
2018 return TRUE;
2023 return FALSE;
2026 static inline int
2027 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2029 *pdev = alloc_pci_dev();
2031 if( *pdev == NULL ) return -1;
2033 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2035 if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
2036 kfree(*pdev);
2037 return -1;
2040 return 0;
2043 static inline void
2044 free_local_pdev(struct pci_dev *pdev)
2046 kfree(pdev);
2050 * mega_allocate_inquiry()
2051 * @dma_handle - handle returned for dma address
2052 * @pdev - handle to pci device
2054 * allocates memory for inquiry structure
2056 static inline void *
2057 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2059 return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2063 static inline void
2064 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2066 pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2070 #ifdef CONFIG_PROC_FS
2071 /* Following code handles /proc fs */
2073 #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
2074 S_IRUSR | S_IFREG, \
2075 controller_proc_dir_entry, \
2076 func, adapter)
2079 * mega_create_proc_entry()
2080 * @index - index in soft state array
2081 * @parent - parent node for this /proc entry
2083 * Creates /proc entries for our controllers.
2085 static void
2086 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2088 struct proc_dir_entry *controller_proc_dir_entry = NULL;
2089 u8 string[64] = { 0 };
2090 adapter_t *adapter = hba_soft_state[index];
2092 sprintf(string, "hba%d", adapter->host->host_no);
2094 controller_proc_dir_entry =
2095 adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
2097 if(!controller_proc_dir_entry) {
2098 printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2099 return;
2101 adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
2102 adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
2103 adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
2104 #if MEGA_HAVE_ENH_PROC
2105 adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
2106 adapter->proc_battery = CREATE_READ_PROC("battery-status",
2107 proc_battery);
2110 * Display each physical drive on its channel
2112 adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
2113 proc_pdrv_ch0);
2114 adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
2115 proc_pdrv_ch1);
2116 adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
2117 proc_pdrv_ch2);
2118 adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
2119 proc_pdrv_ch3);
2122 * Display a set of up to 10 logical drive through each of following
2123 * /proc entries
2125 adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
2126 proc_rdrv_10);
2127 adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
2128 proc_rdrv_20);
2129 adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
2130 proc_rdrv_30);
2131 adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
2132 proc_rdrv_40);
2133 #endif
2138 * proc_read_config()
2139 * @page - buffer to write the data in
2140 * @start - where the actual data has been written in page
2141 * @offset - same meaning as the read system call
2142 * @count - same meaning as the read system call
2143 * @eof - set if no more data needs to be returned
2144 * @data - pointer to our soft state
2146 * Display configuration information about the controller.
2148 static int
2149 proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
2150 void *data)
2153 adapter_t *adapter = (adapter_t *)data;
2154 int len = 0;
2156 len += sprintf(page+len, "%s", MEGARAID_VERSION);
2158 if(adapter->product_info.product_name[0])
2159 len += sprintf(page+len, "%s\n",
2160 adapter->product_info.product_name);
2162 len += sprintf(page+len, "Controller Type: ");
2164 if( adapter->flag & BOARD_MEMMAP ) {
2165 len += sprintf(page+len,
2166 "438/466/467/471/493/518/520/531/532\n");
2168 else {
2169 len += sprintf(page+len,
2170 "418/428/434\n");
2173 if(adapter->flag & BOARD_40LD) {
2174 len += sprintf(page+len,
2175 "Controller Supports 40 Logical Drives\n");
2178 if(adapter->flag & BOARD_64BIT) {
2179 len += sprintf(page+len,
2180 "Controller capable of 64-bit memory addressing\n");
2182 if( adapter->has_64bit_addr ) {
2183 len += sprintf(page+len,
2184 "Controller using 64-bit memory addressing\n");
2186 else {
2187 len += sprintf(page+len,
2188 "Controller is not using 64-bit memory addressing\n");
2191 len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
2192 adapter->host->irq);
2194 len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
2195 adapter->numldrv, adapter->product_info.nchannels);
2197 len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
2198 adapter->fw_version, adapter->bios_version,
2199 adapter->product_info.dram_size);
2201 len += sprintf(page+len,
2202 "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2203 adapter->product_info.max_commands, adapter->max_cmds);
2205 len += sprintf(page+len, "support_ext_cdb = %d\n",
2206 adapter->support_ext_cdb);
2207 len += sprintf(page+len, "support_random_del = %d\n",
2208 adapter->support_random_del);
2209 len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
2210 adapter->boot_ldrv_enabled);
2211 len += sprintf(page+len, "boot_ldrv = %d\n",
2212 adapter->boot_ldrv);
2213 len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
2214 adapter->boot_pdrv_enabled);
2215 len += sprintf(page+len, "boot_pdrv_ch = %d\n",
2216 adapter->boot_pdrv_ch);
2217 len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
2218 adapter->boot_pdrv_tgt);
2219 len += sprintf(page+len, "quiescent = %d\n",
2220 atomic_read(&adapter->quiescent));
2221 len += sprintf(page+len, "has_cluster = %d\n",
2222 adapter->has_cluster);
2224 len += sprintf(page+len, "\nModule Parameters:\n");
2225 len += sprintf(page+len, "max_cmd_per_lun = %d\n",
2226 max_cmd_per_lun);
2227 len += sprintf(page+len, "max_sectors_per_io = %d\n",
2228 max_sectors_per_io);
2230 *eof = 1;
2232 return len;
2238 * proc_read_stat()
2239 * @page - buffer to write the data in
2240 * @start - where the actual data has been written in page
2241 * @offset - same meaning as the read system call
2242 * @count - same meaning as the read system call
2243 * @eof - set if no more data needs to be returned
2244 * @data - pointer to our soft state
2246 * Diaplay statistical information about the I/O activity.
2248 static int
2249 proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
2250 void *data)
2252 adapter_t *adapter;
2253 int len;
2254 int i;
2256 i = 0; /* avoid compilation warnings */
2257 len = 0;
2258 adapter = (adapter_t *)data;
2260 len = sprintf(page, "Statistical Information for this controller\n");
2261 len += sprintf(page+len, "pend_cmds = %d\n",
2262 atomic_read(&adapter->pend_cmds));
2263 #if MEGA_HAVE_STATS
2264 for(i = 0; i < adapter->numldrv; i++) {
2265 len += sprintf(page+len, "Logical Drive %d:\n", i);
2267 len += sprintf(page+len,
2268 "\tReads Issued = %lu, Writes Issued = %lu\n",
2269 adapter->nreads[i], adapter->nwrites[i]);
2271 len += sprintf(page+len,
2272 "\tSectors Read = %lu, Sectors Written = %lu\n",
2273 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2275 len += sprintf(page+len,
2276 "\tRead errors = %lu, Write errors = %lu\n\n",
2277 adapter->rd_errors[i], adapter->wr_errors[i]);
2279 #else
2280 len += sprintf(page+len,
2281 "IO and error counters not compiled in driver.\n");
2282 #endif
2284 *eof = 1;
2286 return len;
2291 * proc_read_mbox()
2292 * @page - buffer to write the data in
2293 * @start - where the actual data has been written in page
2294 * @offset - same meaning as the read system call
2295 * @count - same meaning as the read system call
2296 * @eof - set if no more data needs to be returned
2297 * @data - pointer to our soft state
2299 * Display mailbox information for the last command issued. This information
2300 * is good for debugging.
2302 static int
2303 proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
2304 void *data)
2307 adapter_t *adapter = (adapter_t *)data;
2308 volatile mbox_t *mbox = adapter->mbox;
2309 int len = 0;
2311 len = sprintf(page, "Contents of Mail Box Structure\n");
2312 len += sprintf(page+len, " Fw Command = 0x%02x\n",
2313 mbox->m_out.cmd);
2314 len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
2315 mbox->m_out.cmdid);
2316 len += sprintf(page+len, " No of Sectors= %04d\n",
2317 mbox->m_out.numsectors);
2318 len += sprintf(page+len, " LBA = 0x%02x\n",
2319 mbox->m_out.lba);
2320 len += sprintf(page+len, " DTA = 0x%08x\n",
2321 mbox->m_out.xferaddr);
2322 len += sprintf(page+len, " Logical Drive= 0x%02x\n",
2323 mbox->m_out.logdrv);
2324 len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
2325 mbox->m_out.numsgelements);
2326 len += sprintf(page+len, " Busy = %01x\n",
2327 mbox->m_in.busy);
2328 len += sprintf(page+len, " Status = 0x%02x\n",
2329 mbox->m_in.status);
2331 *eof = 1;
2333 return len;
2338 * proc_rebuild_rate()
2339 * @page - buffer to write the data in
2340 * @start - where the actual data has been written in page
2341 * @offset - same meaning as the read system call
2342 * @count - same meaning as the read system call
2343 * @eof - set if no more data needs to be returned
2344 * @data - pointer to our soft state
2346 * Display current rebuild rate
2348 static int
2349 proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
2350 void *data)
2352 adapter_t *adapter = (adapter_t *)data;
2353 dma_addr_t dma_handle;
2354 caddr_t inquiry;
2355 struct pci_dev *pdev;
2356 int len = 0;
2358 if( make_local_pdev(adapter, &pdev) != 0 ) {
2359 *eof = 1;
2360 return len;
2363 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2364 free_local_pdev(pdev);
2365 *eof = 1;
2366 return len;
2369 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2371 len = sprintf(page, "Adapter inquiry failed.\n");
2373 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2375 mega_free_inquiry(inquiry, dma_handle, pdev);
2377 free_local_pdev(pdev);
2379 *eof = 1;
2381 return len;
2384 if( adapter->flag & BOARD_40LD ) {
2385 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2386 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2388 else {
2389 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2390 ((mraid_ext_inquiry *)
2391 inquiry)->raid_inq.adapter_info.rebuild_rate);
2395 mega_free_inquiry(inquiry, dma_handle, pdev);
2397 free_local_pdev(pdev);
2399 *eof = 1;
2401 return len;
2406 * proc_battery()
2407 * @page - buffer to write the data in
2408 * @start - where the actual data has been written in page
2409 * @offset - same meaning as the read system call
2410 * @count - same meaning as the read system call
2411 * @eof - set if no more data needs to be returned
2412 * @data - pointer to our soft state
2414 * Display information about the battery module on the controller.
2416 static int
2417 proc_battery(char *page, char **start, off_t offset, int count, int *eof,
2418 void *data)
2420 adapter_t *adapter = (adapter_t *)data;
2421 dma_addr_t dma_handle;
2422 caddr_t inquiry;
2423 struct pci_dev *pdev;
2424 u8 battery_status = 0;
2425 char str[256];
2426 int len = 0;
2428 if( make_local_pdev(adapter, &pdev) != 0 ) {
2429 *eof = 1;
2430 return len;
2433 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2434 free_local_pdev(pdev);
2435 *eof = 1;
2436 return len;
2439 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2441 len = sprintf(page, "Adapter inquiry failed.\n");
2443 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2445 mega_free_inquiry(inquiry, dma_handle, pdev);
2447 free_local_pdev(pdev);
2449 *eof = 1;
2451 return len;
2454 if( adapter->flag & BOARD_40LD ) {
2455 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2457 else {
2458 battery_status = ((mraid_ext_inquiry *)inquiry)->
2459 raid_inq.adapter_info.battery_status;
2463 * Decode the battery status
2465 sprintf(str, "Battery Status:[%d]", battery_status);
2467 if(battery_status == MEGA_BATT_CHARGE_DONE)
2468 strcat(str, " Charge Done");
2470 if(battery_status & MEGA_BATT_MODULE_MISSING)
2471 strcat(str, " Module Missing");
2473 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2474 strcat(str, " Low Voltage");
2476 if(battery_status & MEGA_BATT_TEMP_HIGH)
2477 strcat(str, " Temperature High");
2479 if(battery_status & MEGA_BATT_PACK_MISSING)
2480 strcat(str, " Pack Missing");
2482 if(battery_status & MEGA_BATT_CHARGE_INPROG)
2483 strcat(str, " Charge In-progress");
2485 if(battery_status & MEGA_BATT_CHARGE_FAIL)
2486 strcat(str, " Charge Fail");
2488 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2489 strcat(str, " Cycles Exceeded");
2491 len = sprintf(page, "%s\n", str);
2494 mega_free_inquiry(inquiry, dma_handle, pdev);
2496 free_local_pdev(pdev);
2498 *eof = 1;
2500 return len;
2505 * proc_pdrv_ch0()
2506 * @page - buffer to write the data in
2507 * @start - where the actual data has been written in page
2508 * @offset - same meaning as the read system call
2509 * @count - same meaning as the read system call
2510 * @eof - set if no more data needs to be returned
2511 * @data - pointer to our soft state
2513 * Display information about the physical drives on physical channel 0.
2515 static int
2516 proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
2517 void *data)
2519 adapter_t *adapter = (adapter_t *)data;
2521 *eof = 1;
2523 return (proc_pdrv(adapter, page, 0));
2528 * proc_pdrv_ch1()
2529 * @page - buffer to write the data in
2530 * @start - where the actual data has been written in page
2531 * @offset - same meaning as the read system call
2532 * @count - same meaning as the read system call
2533 * @eof - set if no more data needs to be returned
2534 * @data - pointer to our soft state
2536 * Display information about the physical drives on physical channel 1.
2538 static int
2539 proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
2540 void *data)
2542 adapter_t *adapter = (adapter_t *)data;
2544 *eof = 1;
2546 return (proc_pdrv(adapter, page, 1));
2551 * proc_pdrv_ch2()
2552 * @page - buffer to write the data in
2553 * @start - where the actual data has been written in page
2554 * @offset - same meaning as the read system call
2555 * @count - same meaning as the read system call
2556 * @eof - set if no more data needs to be returned
2557 * @data - pointer to our soft state
2559 * Display information about the physical drives on physical channel 2.
2561 static int
2562 proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
2563 void *data)
2565 adapter_t *adapter = (adapter_t *)data;
2567 *eof = 1;
2569 return (proc_pdrv(adapter, page, 2));
2574 * proc_pdrv_ch3()
2575 * @page - buffer to write the data in
2576 * @start - where the actual data has been written in page
2577 * @offset - same meaning as the read system call
2578 * @count - same meaning as the read system call
2579 * @eof - set if no more data needs to be returned
2580 * @data - pointer to our soft state
2582 * Display information about the physical drives on physical channel 3.
2584 static int
2585 proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
2586 void *data)
2588 adapter_t *adapter = (adapter_t *)data;
2590 *eof = 1;
2592 return (proc_pdrv(adapter, page, 3));
2597 * proc_pdrv()
2598 * @page - buffer to write the data in
2599 * @adapter - pointer to our soft state
2601 * Display information about the physical drives.
2603 static int
2604 proc_pdrv(adapter_t *adapter, char *page, int channel)
2606 dma_addr_t dma_handle;
2607 char *scsi_inq;
2608 dma_addr_t scsi_inq_dma_handle;
2609 caddr_t inquiry;
2610 struct pci_dev *pdev;
2611 u8 *pdrv_state;
2612 u8 state;
2613 int tgt;
2614 int max_channels;
2615 int len = 0;
2616 char str[80];
2617 int i;
2619 if( make_local_pdev(adapter, &pdev) != 0 ) {
2620 return len;
2623 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2624 goto free_pdev;
2627 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2628 len = sprintf(page, "Adapter inquiry failed.\n");
2630 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2632 goto free_inquiry;
2636 scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2638 if( scsi_inq == NULL ) {
2639 len = sprintf(page, "memory not available for scsi inq.\n");
2641 goto free_inquiry;
2644 if( adapter->flag & BOARD_40LD ) {
2645 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2647 else {
2648 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2649 raid_inq.pdrv_info.pdrv_state;
2652 max_channels = adapter->product_info.nchannels;
2654 if( channel >= max_channels ) {
2655 goto free_pci;
2658 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2660 i = channel*16 + tgt;
2662 state = *(pdrv_state + i);
2664 switch( state & 0x0F ) {
2666 case PDRV_ONLINE:
2667 sprintf(str,
2668 "Channel:%2d Id:%2d State: Online",
2669 channel, tgt);
2670 break;
2672 case PDRV_FAILED:
2673 sprintf(str,
2674 "Channel:%2d Id:%2d State: Failed",
2675 channel, tgt);
2676 break;
2678 case PDRV_RBLD:
2679 sprintf(str,
2680 "Channel:%2d Id:%2d State: Rebuild",
2681 channel, tgt);
2682 break;
2684 case PDRV_HOTSPARE:
2685 sprintf(str,
2686 "Channel:%2d Id:%2d State: Hot spare",
2687 channel, tgt);
2688 break;
2690 default:
2691 sprintf(str,
2692 "Channel:%2d Id:%2d State: Un-configured",
2693 channel, tgt);
2694 break;
2699 * This interface displays inquiries for disk drives
2700 * only. Inquries for logical drives and non-disk
2701 * devices are available through /proc/scsi/scsi
2703 memset(scsi_inq, 0, 256);
2704 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2705 scsi_inq_dma_handle) ||
2706 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2707 continue;
2711 * Check for overflow. We print less than 240
2712 * characters for inquiry
2714 if( (len + 240) >= PAGE_SIZE ) break;
2716 len += sprintf(page+len, "%s.\n", str);
2718 len += mega_print_inquiry(page+len, scsi_inq);
2721 free_pci:
2722 pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2723 free_inquiry:
2724 mega_free_inquiry(inquiry, dma_handle, pdev);
2725 free_pdev:
2726 free_local_pdev(pdev);
2728 return len;
2733 * Display scsi inquiry
2735 static int
2736 mega_print_inquiry(char *page, char *scsi_inq)
2738 int len = 0;
2739 int i;
2741 len = sprintf(page, " Vendor: ");
2742 for( i = 8; i < 16; i++ ) {
2743 len += sprintf(page+len, "%c", scsi_inq[i]);
2746 len += sprintf(page+len, " Model: ");
2748 for( i = 16; i < 32; i++ ) {
2749 len += sprintf(page+len, "%c", scsi_inq[i]);
2752 len += sprintf(page+len, " Rev: ");
2754 for( i = 32; i < 36; i++ ) {
2755 len += sprintf(page+len, "%c", scsi_inq[i]);
2758 len += sprintf(page+len, "\n");
2760 i = scsi_inq[0] & 0x1f;
2762 len += sprintf(page+len, " Type: %s ", scsi_device_type(i));
2764 len += sprintf(page+len,
2765 " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
2767 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2768 len += sprintf(page+len, " CCS\n");
2769 else
2770 len += sprintf(page+len, "\n");
2772 return len;
2777 * proc_rdrv_10()
2778 * @page - buffer to write the data in
2779 * @start - where the actual data has been written in page
2780 * @offset - same meaning as the read system call
2781 * @count - same meaning as the read system call
2782 * @eof - set if no more data needs to be returned
2783 * @data - pointer to our soft state
2785 * Display real time information about the logical drives 0 through 9.
2787 static int
2788 proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
2789 void *data)
2791 adapter_t *adapter = (adapter_t *)data;
2793 *eof = 1;
2795 return (proc_rdrv(adapter, page, 0, 9));
2800 * proc_rdrv_20()
2801 * @page - buffer to write the data in
2802 * @start - where the actual data has been written in page
2803 * @offset - same meaning as the read system call
2804 * @count - same meaning as the read system call
2805 * @eof - set if no more data needs to be returned
2806 * @data - pointer to our soft state
2808 * Display real time information about the logical drives 0 through 9.
2810 static int
2811 proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
2812 void *data)
2814 adapter_t *adapter = (adapter_t *)data;
2816 *eof = 1;
2818 return (proc_rdrv(adapter, page, 10, 19));
2823 * proc_rdrv_30()
2824 * @page - buffer to write the data in
2825 * @start - where the actual data has been written in page
2826 * @offset - same meaning as the read system call
2827 * @count - same meaning as the read system call
2828 * @eof - set if no more data needs to be returned
2829 * @data - pointer to our soft state
2831 * Display real time information about the logical drives 0 through 9.
2833 static int
2834 proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
2835 void *data)
2837 adapter_t *adapter = (adapter_t *)data;
2839 *eof = 1;
2841 return (proc_rdrv(adapter, page, 20, 29));
2846 * proc_rdrv_40()
2847 * @page - buffer to write the data in
2848 * @start - where the actual data has been written in page
2849 * @offset - same meaning as the read system call
2850 * @count - same meaning as the read system call
2851 * @eof - set if no more data needs to be returned
2852 * @data - pointer to our soft state
2854 * Display real time information about the logical drives 0 through 9.
2856 static int
2857 proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
2858 void *data)
2860 adapter_t *adapter = (adapter_t *)data;
2862 *eof = 1;
2864 return (proc_rdrv(adapter, page, 30, 39));
2869 * proc_rdrv()
2870 * @page - buffer to write the data in
2871 * @adapter - pointer to our soft state
2872 * @start - starting logical drive to display
2873 * @end - ending logical drive to display
2875 * We do not print the inquiry information since its already available through
2876 * /proc/scsi/scsi interface
2878 static int
2879 proc_rdrv(adapter_t *adapter, char *page, int start, int end )
2881 dma_addr_t dma_handle;
2882 logdrv_param *lparam;
2883 megacmd_t mc;
2884 char *disk_array;
2885 dma_addr_t disk_array_dma_handle;
2886 caddr_t inquiry;
2887 struct pci_dev *pdev;
2888 u8 *rdrv_state;
2889 int num_ldrv;
2890 u32 array_sz;
2891 int len = 0;
2892 int i;
2894 if( make_local_pdev(adapter, &pdev) != 0 ) {
2895 return len;
2898 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2899 free_local_pdev(pdev);
2900 return len;
2903 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2905 len = sprintf(page, "Adapter inquiry failed.\n");
2907 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2909 mega_free_inquiry(inquiry, dma_handle, pdev);
2911 free_local_pdev(pdev);
2913 return len;
2916 memset(&mc, 0, sizeof(megacmd_t));
2918 if( adapter->flag & BOARD_40LD ) {
2919 array_sz = sizeof(disk_array_40ld);
2921 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2923 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2925 else {
2926 array_sz = sizeof(disk_array_8ld);
2928 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2929 raid_inq.logdrv_info.ldrv_state;
2931 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2932 raid_inq.logdrv_info.num_ldrv;
2935 disk_array = pci_alloc_consistent(pdev, array_sz,
2936 &disk_array_dma_handle);
2938 if( disk_array == NULL ) {
2939 len = sprintf(page, "memory not available.\n");
2941 mega_free_inquiry(inquiry, dma_handle, pdev);
2943 free_local_pdev(pdev);
2945 return len;
2948 mc.xferaddr = (u32)disk_array_dma_handle;
2950 if( adapter->flag & BOARD_40LD ) {
2951 mc.cmd = FC_NEW_CONFIG;
2952 mc.opcode = OP_DCMD_READ_CONFIG;
2954 if( mega_internal_command(adapter, &mc, NULL) ) {
2956 len = sprintf(page, "40LD read config failed.\n");
2958 mega_free_inquiry(inquiry, dma_handle, pdev);
2960 pci_free_consistent(pdev, array_sz, disk_array,
2961 disk_array_dma_handle);
2963 free_local_pdev(pdev);
2965 return len;
2969 else {
2970 mc.cmd = NEW_READ_CONFIG_8LD;
2972 if( mega_internal_command(adapter, &mc, NULL) ) {
2974 mc.cmd = READ_CONFIG_8LD;
2976 if( mega_internal_command(adapter, &mc,
2977 NULL) ){
2979 len = sprintf(page,
2980 "8LD read config failed.\n");
2982 mega_free_inquiry(inquiry, dma_handle, pdev);
2984 pci_free_consistent(pdev, array_sz,
2985 disk_array,
2986 disk_array_dma_handle);
2988 free_local_pdev(pdev);
2990 return len;
2995 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2997 if( adapter->flag & BOARD_40LD ) {
2998 lparam =
2999 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
3001 else {
3002 lparam =
3003 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
3007 * Check for overflow. We print less than 240 characters for
3008 * information about each logical drive.
3010 if( (len + 240) >= PAGE_SIZE ) break;
3012 len += sprintf(page+len, "Logical drive:%2d:, ", i);
3014 switch( rdrv_state[i] & 0x0F ) {
3015 case RDRV_OFFLINE:
3016 len += sprintf(page+len, "state: offline");
3017 break;
3019 case RDRV_DEGRADED:
3020 len += sprintf(page+len, "state: degraded");
3021 break;
3023 case RDRV_OPTIMAL:
3024 len += sprintf(page+len, "state: optimal");
3025 break;
3027 case RDRV_DELETED:
3028 len += sprintf(page+len, "state: deleted");
3029 break;
3031 default:
3032 len += sprintf(page+len, "state: unknown");
3033 break;
3037 * Check if check consistency or initialization is going on
3038 * for this logical drive.
3040 if( (rdrv_state[i] & 0xF0) == 0x20 ) {
3041 len += sprintf(page+len,
3042 ", check-consistency in progress");
3044 else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
3045 len += sprintf(page+len,
3046 ", initialization in progress");
3049 len += sprintf(page+len, "\n");
3051 len += sprintf(page+len, "Span depth:%3d, ",
3052 lparam->span_depth);
3054 len += sprintf(page+len, "RAID level:%3d, ",
3055 lparam->level);
3057 len += sprintf(page+len, "Stripe size:%3d, ",
3058 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
3060 len += sprintf(page+len, "Row size:%3d\n",
3061 lparam->row_size);
3064 len += sprintf(page+len, "Read Policy: ");
3066 switch(lparam->read_ahead) {
3068 case NO_READ_AHEAD:
3069 len += sprintf(page+len, "No read ahead, ");
3070 break;
3072 case READ_AHEAD:
3073 len += sprintf(page+len, "Read ahead, ");
3074 break;
3076 case ADAP_READ_AHEAD:
3077 len += sprintf(page+len, "Adaptive, ");
3078 break;
3082 len += sprintf(page+len, "Write Policy: ");
3084 switch(lparam->write_mode) {
3086 case WRMODE_WRITE_THRU:
3087 len += sprintf(page+len, "Write thru, ");
3088 break;
3090 case WRMODE_WRITE_BACK:
3091 len += sprintf(page+len, "Write back, ");
3092 break;
3095 len += sprintf(page+len, "Cache Policy: ");
3097 switch(lparam->direct_io) {
3099 case CACHED_IO:
3100 len += sprintf(page+len, "Cached IO\n\n");
3101 break;
3103 case DIRECT_IO:
3104 len += sprintf(page+len, "Direct IO\n\n");
3105 break;
3109 mega_free_inquiry(inquiry, dma_handle, pdev);
3111 pci_free_consistent(pdev, array_sz, disk_array,
3112 disk_array_dma_handle);
3114 free_local_pdev(pdev);
3116 return len;
3118 #else
3119 static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
3122 #endif
3126 * megaraid_biosparam()
3128 * Return the disk geometry for a particular disk
3130 static int
3131 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
3132 sector_t capacity, int geom[])
3134 adapter_t *adapter;
3135 unsigned char *bh;
3136 int heads;
3137 int sectors;
3138 int cylinders;
3139 int rval;
3141 /* Get pointer to host config structure */
3142 adapter = (adapter_t *)sdev->host->hostdata;
3144 if (IS_RAID_CH(adapter, sdev->channel)) {
3145 /* Default heads (64) & sectors (32) */
3146 heads = 64;
3147 sectors = 32;
3148 cylinders = (ulong)capacity / (heads * sectors);
3151 * Handle extended translation size for logical drives
3152 * > 1Gb
3154 if ((ulong)capacity >= 0x200000) {
3155 heads = 255;
3156 sectors = 63;
3157 cylinders = (ulong)capacity / (heads * sectors);
3160 /* return result */
3161 geom[0] = heads;
3162 geom[1] = sectors;
3163 geom[2] = cylinders;
3165 else {
3166 bh = scsi_bios_ptable(bdev);
3168 if( bh ) {
3169 rval = scsi_partsize(bh, capacity,
3170 &geom[2], &geom[0], &geom[1]);
3171 kfree(bh);
3172 if( rval != -1 )
3173 return rval;
3176 printk(KERN_INFO
3177 "megaraid: invalid partition on this disk on channel %d\n",
3178 sdev->channel);
3180 /* Default heads (64) & sectors (32) */
3181 heads = 64;
3182 sectors = 32;
3183 cylinders = (ulong)capacity / (heads * sectors);
3185 /* Handle extended translation size for logical drives > 1Gb */
3186 if ((ulong)capacity >= 0x200000) {
3187 heads = 255;
3188 sectors = 63;
3189 cylinders = (ulong)capacity / (heads * sectors);
3192 /* return result */
3193 geom[0] = heads;
3194 geom[1] = sectors;
3195 geom[2] = cylinders;
3198 return 0;
3202 * mega_init_scb()
3203 * @adapter - pointer to our soft state
3205 * Allocate memory for the various pointers in the scb structures:
3206 * scatter-gather list pointer, passthru and extended passthru structure
3207 * pointers.
3209 static int
3210 mega_init_scb(adapter_t *adapter)
3212 scb_t *scb;
3213 int i;
3215 for( i = 0; i < adapter->max_cmds; i++ ) {
3217 scb = &adapter->scb_list[i];
3219 scb->sgl64 = NULL;
3220 scb->sgl = NULL;
3221 scb->pthru = NULL;
3222 scb->epthru = NULL;
3225 for( i = 0; i < adapter->max_cmds; i++ ) {
3227 scb = &adapter->scb_list[i];
3229 scb->idx = i;
3231 scb->sgl64 = pci_alloc_consistent(adapter->dev,
3232 sizeof(mega_sgl64) * adapter->sglen,
3233 &scb->sgl_dma_addr);
3235 scb->sgl = (mega_sglist *)scb->sgl64;
3237 if( !scb->sgl ) {
3238 printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
3239 mega_free_sgl(adapter);
3240 return -1;
3243 scb->pthru = pci_alloc_consistent(adapter->dev,
3244 sizeof(mega_passthru),
3245 &scb->pthru_dma_addr);
3247 if( !scb->pthru ) {
3248 printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
3249 mega_free_sgl(adapter);
3250 return -1;
3253 scb->epthru = pci_alloc_consistent(adapter->dev,
3254 sizeof(mega_ext_passthru),
3255 &scb->epthru_dma_addr);
3257 if( !scb->epthru ) {
3258 printk(KERN_WARNING
3259 "Can't allocate extended passthru.\n");
3260 mega_free_sgl(adapter);
3261 return -1;
3265 scb->dma_type = MEGA_DMA_TYPE_NONE;
3268 * Link to free list
3269 * lock not required since we are loading the driver, so no
3270 * commands possible right now.
3272 scb->state = SCB_FREE;
3273 scb->cmd = NULL;
3274 list_add(&scb->list, &adapter->free_list);
3277 return 0;
3282 * megadev_open()
3283 * @inode - unused
3284 * @filep - unused
3286 * Routines for the character/ioctl interface to the driver. Find out if this
3287 * is a valid open.
3289 static int
3290 megadev_open (struct inode *inode, struct file *filep)
3293 * Only allow superuser to access private ioctl interface
3295 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
3297 return 0;
3302 * megadev_ioctl()
3303 * @inode - Our device inode
3304 * @filep - unused
3305 * @cmd - ioctl command
3306 * @arg - user buffer
3308 * ioctl entry point for our private ioctl interface. We move the data in from
3309 * the user space, prepare the command (if necessary, convert the old MIMD
3310 * ioctl to new ioctl command), and issue a synchronous command to the
3311 * controller.
3313 static int
3314 megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3316 adapter_t *adapter;
3317 nitioctl_t uioc;
3318 int adapno;
3319 int rval;
3320 mega_passthru __user *upthru; /* user address for passthru */
3321 mega_passthru *pthru; /* copy user passthru here */
3322 dma_addr_t pthru_dma_hndl;
3323 void *data = NULL; /* data to be transferred */
3324 dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
3325 megacmd_t mc;
3326 megastat_t __user *ustats;
3327 int num_ldrv;
3328 u32 uxferaddr = 0;
3329 struct pci_dev *pdev;
3331 ustats = NULL; /* avoid compilation warnings */
3332 num_ldrv = 0;
3335 * Make sure only USCSICMD are issued through this interface.
3336 * MIMD application would still fire different command.
3338 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3339 return -EINVAL;
3343 * Check and convert a possible MIMD command to NIT command.
3344 * mega_m_to_n() copies the data from the user space, so we do not
3345 * have to do it here.
3346 * NOTE: We will need some user address to copyout the data, therefore
3347 * the inteface layer will also provide us with the required user
3348 * addresses.
3350 memset(&uioc, 0, sizeof(nitioctl_t));
3351 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3352 return rval;
3355 switch( uioc.opcode ) {
3357 case GET_DRIVER_VER:
3358 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3359 return (-EFAULT);
3361 break;
3363 case GET_N_ADAP:
3364 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3365 return (-EFAULT);
3368 * Shucks. MIMD interface returns a positive value for number
3369 * of adapters. TODO: Change it to return 0 when there is no
3370 * applicatio using mimd interface.
3372 return hba_count;
3374 case GET_ADAP_INFO:
3377 * Which adapter
3379 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3380 return (-ENODEV);
3382 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3383 sizeof(struct mcontroller)) )
3384 return (-EFAULT);
3385 break;
3387 #if MEGA_HAVE_STATS
3389 case GET_STATS:
3391 * Which adapter
3393 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3394 return (-ENODEV);
3396 adapter = hba_soft_state[adapno];
3398 ustats = uioc.uioc_uaddr;
3400 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3401 return (-EFAULT);
3404 * Check for the validity of the logical drive number
3406 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3408 if( copy_to_user(ustats->nreads, adapter->nreads,
3409 num_ldrv*sizeof(u32)) )
3410 return -EFAULT;
3412 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3413 num_ldrv*sizeof(u32)) )
3414 return -EFAULT;
3416 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3417 num_ldrv*sizeof(u32)) )
3418 return -EFAULT;
3420 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3421 num_ldrv*sizeof(u32)) )
3422 return -EFAULT;
3424 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3425 num_ldrv*sizeof(u32)) )
3426 return -EFAULT;
3428 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3429 num_ldrv*sizeof(u32)) )
3430 return -EFAULT;
3432 return 0;
3434 #endif
3435 case MBOX_CMD:
3438 * Which adapter
3440 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3441 return (-ENODEV);
3443 adapter = hba_soft_state[adapno];
3446 * Deletion of logical drive is a special case. The adapter
3447 * should be quiescent before this command is issued.
3449 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3450 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3453 * Do we support this feature
3455 if( !adapter->support_random_del ) {
3456 printk(KERN_WARNING "megaraid: logdrv ");
3457 printk("delete on non-supporting F/W.\n");
3459 return (-EINVAL);
3462 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3464 if( rval == 0 ) {
3465 memset(&mc, 0, sizeof(megacmd_t));
3467 mc.status = rval;
3469 rval = mega_n_to_m((void __user *)arg, &mc);
3472 return rval;
3475 * This interface only support the regular passthru commands.
3476 * Reject extended passthru and 64-bit passthru
3478 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3479 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3481 printk(KERN_WARNING "megaraid: rejected passthru.\n");
3483 return (-EINVAL);
3487 * For all internal commands, the buffer must be allocated in
3488 * <4GB address range
3490 if( make_local_pdev(adapter, &pdev) != 0 )
3491 return -EIO;
3493 /* Is it a passthru command or a DCMD */
3494 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3495 /* Passthru commands */
3497 pthru = pci_alloc_consistent(pdev,
3498 sizeof(mega_passthru),
3499 &pthru_dma_hndl);
3501 if( pthru == NULL ) {
3502 free_local_pdev(pdev);
3503 return (-ENOMEM);
3507 * The user passthru structure
3509 upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
3512 * Copy in the user passthru here.
3514 if( copy_from_user(pthru, upthru,
3515 sizeof(mega_passthru)) ) {
3517 pci_free_consistent(pdev,
3518 sizeof(mega_passthru), pthru,
3519 pthru_dma_hndl);
3521 free_local_pdev(pdev);
3523 return (-EFAULT);
3527 * Is there a data transfer
3529 if( pthru->dataxferlen ) {
3530 data = pci_alloc_consistent(pdev,
3531 pthru->dataxferlen,
3532 &data_dma_hndl);
3534 if( data == NULL ) {
3535 pci_free_consistent(pdev,
3536 sizeof(mega_passthru),
3537 pthru,
3538 pthru_dma_hndl);
3540 free_local_pdev(pdev);
3542 return (-ENOMEM);
3546 * Save the user address and point the kernel
3547 * address at just allocated memory
3549 uxferaddr = pthru->dataxferaddr;
3550 pthru->dataxferaddr = data_dma_hndl;
3555 * Is data coming down-stream
3557 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3559 * Get the user data
3561 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3562 pthru->dataxferlen) ) {
3563 rval = (-EFAULT);
3564 goto freemem_and_return;
3568 memset(&mc, 0, sizeof(megacmd_t));
3570 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3571 mc.xferaddr = (u32)pthru_dma_hndl;
3574 * Issue the command
3576 mega_internal_command(adapter, &mc, pthru);
3578 rval = mega_n_to_m((void __user *)arg, &mc);
3580 if( rval ) goto freemem_and_return;
3584 * Is data going up-stream
3586 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3587 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3588 pthru->dataxferlen) ) {
3589 rval = (-EFAULT);
3594 * Send the request sense data also, irrespective of
3595 * whether the user has asked for it or not.
3597 if (copy_to_user(upthru->reqsensearea,
3598 pthru->reqsensearea, 14))
3599 rval = -EFAULT;
3601 freemem_and_return:
3602 if( pthru->dataxferlen ) {
3603 pci_free_consistent(pdev,
3604 pthru->dataxferlen, data,
3605 data_dma_hndl);
3608 pci_free_consistent(pdev, sizeof(mega_passthru),
3609 pthru, pthru_dma_hndl);
3611 free_local_pdev(pdev);
3613 return rval;
3615 else {
3616 /* DCMD commands */
3619 * Is there a data transfer
3621 if( uioc.xferlen ) {
3622 data = pci_alloc_consistent(pdev,
3623 uioc.xferlen, &data_dma_hndl);
3625 if( data == NULL ) {
3626 free_local_pdev(pdev);
3627 return (-ENOMEM);
3630 uxferaddr = MBOX(uioc)->xferaddr;
3634 * Is data coming down-stream
3636 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3638 * Get the user data
3640 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3641 uioc.xferlen) ) {
3643 pci_free_consistent(pdev,
3644 uioc.xferlen,
3645 data, data_dma_hndl);
3647 free_local_pdev(pdev);
3649 return (-EFAULT);
3653 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3655 mc.xferaddr = (u32)data_dma_hndl;
3658 * Issue the command
3660 mega_internal_command(adapter, &mc, NULL);
3662 rval = mega_n_to_m((void __user *)arg, &mc);
3664 if( rval ) {
3665 if( uioc.xferlen ) {
3666 pci_free_consistent(pdev,
3667 uioc.xferlen, data,
3668 data_dma_hndl);
3671 free_local_pdev(pdev);
3673 return rval;
3677 * Is data going up-stream
3679 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3680 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3681 uioc.xferlen) ) {
3683 rval = (-EFAULT);
3687 if( uioc.xferlen ) {
3688 pci_free_consistent(pdev,
3689 uioc.xferlen, data,
3690 data_dma_hndl);
3693 free_local_pdev(pdev);
3695 return rval;
3698 default:
3699 return (-EINVAL);
3702 return 0;
3705 static long
3706 megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3708 int ret;
3710 mutex_lock(&megadev_mutex);
3711 ret = megadev_ioctl(filep, cmd, arg);
3712 mutex_unlock(&megadev_mutex);
3714 return ret;
3718 * mega_m_to_n()
3719 * @arg - user address
3720 * @uioc - new ioctl structure
3722 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3723 * structure
3725 * Converts the older mimd ioctl structure to newer NIT structure
3727 static int
3728 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3730 struct uioctl_t uioc_mimd;
3731 char signature[8] = {0};
3732 u8 opcode;
3733 u8 subopcode;
3737 * check is the application conforms to NIT. We do not have to do much
3738 * in that case.
3739 * We exploit the fact that the signature is stored in the very
3740 * beginning of the structure.
3743 if( copy_from_user(signature, arg, 7) )
3744 return (-EFAULT);
3746 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3749 * NOTE NOTE: The nit ioctl is still under flux because of
3750 * change of mailbox definition, in HPE. No applications yet
3751 * use this interface and let's not have applications use this
3752 * interface till the new specifitions are in place.
3754 return -EINVAL;
3755 #if 0
3756 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3757 return (-EFAULT);
3758 return 0;
3759 #endif
3763 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3765 * Get the user ioctl structure
3767 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3768 return (-EFAULT);
3772 * Get the opcode and subopcode for the commands
3774 opcode = uioc_mimd.ui.fcs.opcode;
3775 subopcode = uioc_mimd.ui.fcs.subopcode;
3777 switch (opcode) {
3778 case 0x82:
3780 switch (subopcode) {
3782 case MEGAIOC_QDRVRVER: /* Query driver version */
3783 uioc->opcode = GET_DRIVER_VER;
3784 uioc->uioc_uaddr = uioc_mimd.data;
3785 break;
3787 case MEGAIOC_QNADAP: /* Get # of adapters */
3788 uioc->opcode = GET_N_ADAP;
3789 uioc->uioc_uaddr = uioc_mimd.data;
3790 break;
3792 case MEGAIOC_QADAPINFO: /* Get adapter information */
3793 uioc->opcode = GET_ADAP_INFO;
3794 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3795 uioc->uioc_uaddr = uioc_mimd.data;
3796 break;
3798 default:
3799 return(-EINVAL);
3802 break;
3805 case 0x81:
3807 uioc->opcode = MBOX_CMD;
3808 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3810 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3812 uioc->xferlen = uioc_mimd.ui.fcs.length;
3814 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3815 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3817 break;
3819 case 0x80:
3821 uioc->opcode = MBOX_CMD;
3822 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3824 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3827 * Choose the xferlen bigger of input and output data
3829 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3830 uioc_mimd.outlen : uioc_mimd.inlen;
3832 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3833 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3835 break;
3837 default:
3838 return (-EINVAL);
3842 return 0;
3846 * mega_n_to_m()
3847 * @arg - user address
3848 * @mc - mailbox command
3850 * Updates the status information to the application, depending on application
3851 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3853 static int
3854 mega_n_to_m(void __user *arg, megacmd_t *mc)
3856 nitioctl_t __user *uiocp;
3857 megacmd_t __user *umc;
3858 mega_passthru __user *upthru;
3859 struct uioctl_t __user *uioc_mimd;
3860 char signature[8] = {0};
3863 * check is the application conforms to NIT.
3865 if( copy_from_user(signature, arg, 7) )
3866 return -EFAULT;
3868 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3870 uiocp = arg;
3872 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3873 return (-EFAULT);
3875 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3877 umc = MBOX_P(uiocp);
3879 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3880 return -EFAULT;
3882 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3883 return (-EFAULT);
3886 else {
3887 uioc_mimd = arg;
3889 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3890 return (-EFAULT);
3892 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3894 umc = (megacmd_t __user *)uioc_mimd->mbox;
3896 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3897 return (-EFAULT);
3899 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3900 return (-EFAULT);
3904 return 0;
3909 * MEGARAID 'FW' commands.
3913 * mega_is_bios_enabled()
3914 * @adapter - pointer to our soft state
3916 * issue command to find out if the BIOS is enabled for this controller
3918 static int
3919 mega_is_bios_enabled(adapter_t *adapter)
3921 unsigned char raw_mbox[sizeof(struct mbox_out)];
3922 mbox_t *mbox;
3923 int ret;
3925 mbox = (mbox_t *)raw_mbox;
3927 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3929 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3931 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3933 raw_mbox[0] = IS_BIOS_ENABLED;
3934 raw_mbox[2] = GET_BIOS;
3937 ret = issue_scb_block(adapter, raw_mbox);
3939 return *(char *)adapter->mega_buffer;
3944 * mega_enum_raid_scsi()
3945 * @adapter - pointer to our soft state
3947 * Find out what channels are RAID/SCSI. This information is used to
3948 * differentiate the virtual channels and physical channels and to support
3949 * ROMB feature and non-disk devices.
3951 static void
3952 mega_enum_raid_scsi(adapter_t *adapter)
3954 unsigned char raw_mbox[sizeof(struct mbox_out)];
3955 mbox_t *mbox;
3956 int i;
3958 mbox = (mbox_t *)raw_mbox;
3960 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3963 * issue command to find out what channels are raid/scsi
3965 raw_mbox[0] = CHNL_CLASS;
3966 raw_mbox[2] = GET_CHNL_CLASS;
3968 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3970 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3973 * Non-ROMB firmware fail this command, so all channels
3974 * must be shown RAID
3976 adapter->mega_ch_class = 0xFF;
3978 if(!issue_scb_block(adapter, raw_mbox)) {
3979 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3983 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
3984 if( (adapter->mega_ch_class >> i) & 0x01 ) {
3985 printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
3988 else {
3989 printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
3994 return;
3999 * mega_get_boot_drv()
4000 * @adapter - pointer to our soft state
4002 * Find out which device is the boot device. Note, any logical drive or any
4003 * phyical device (e.g., a CDROM) can be designated as a boot device.
4005 static void
4006 mega_get_boot_drv(adapter_t *adapter)
4008 struct private_bios_data *prv_bios_data;
4009 unsigned char raw_mbox[sizeof(struct mbox_out)];
4010 mbox_t *mbox;
4011 u16 cksum = 0;
4012 u8 *cksum_p;
4013 u8 boot_pdrv;
4014 int i;
4016 mbox = (mbox_t *)raw_mbox;
4018 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4020 raw_mbox[0] = BIOS_PVT_DATA;
4021 raw_mbox[2] = GET_BIOS_PVT_DATA;
4023 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4025 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4027 adapter->boot_ldrv_enabled = 0;
4028 adapter->boot_ldrv = 0;
4030 adapter->boot_pdrv_enabled = 0;
4031 adapter->boot_pdrv_ch = 0;
4032 adapter->boot_pdrv_tgt = 0;
4034 if(issue_scb_block(adapter, raw_mbox) == 0) {
4035 prv_bios_data =
4036 (struct private_bios_data *)adapter->mega_buffer;
4038 cksum = 0;
4039 cksum_p = (char *)prv_bios_data;
4040 for (i = 0; i < 14; i++ ) {
4041 cksum += (u16)(*cksum_p++);
4044 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
4047 * If MSB is set, a physical drive is set as boot
4048 * device
4050 if( prv_bios_data->boot_drv & 0x80 ) {
4051 adapter->boot_pdrv_enabled = 1;
4052 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
4053 adapter->boot_pdrv_ch = boot_pdrv / 16;
4054 adapter->boot_pdrv_tgt = boot_pdrv % 16;
4056 else {
4057 adapter->boot_ldrv_enabled = 1;
4058 adapter->boot_ldrv = prv_bios_data->boot_drv;
4066 * mega_support_random_del()
4067 * @adapter - pointer to our soft state
4069 * Find out if this controller supports random deletion and addition of
4070 * logical drives
4072 static int
4073 mega_support_random_del(adapter_t *adapter)
4075 unsigned char raw_mbox[sizeof(struct mbox_out)];
4076 mbox_t *mbox;
4077 int rval;
4079 mbox = (mbox_t *)raw_mbox;
4081 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4084 * issue command
4086 raw_mbox[0] = FC_DEL_LOGDRV;
4087 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
4089 rval = issue_scb_block(adapter, raw_mbox);
4091 return !rval;
4096 * mega_support_ext_cdb()
4097 * @adapter - pointer to our soft state
4099 * Find out if this firmware support cdblen > 10
4101 static int
4102 mega_support_ext_cdb(adapter_t *adapter)
4104 unsigned char raw_mbox[sizeof(struct mbox_out)];
4105 mbox_t *mbox;
4106 int rval;
4108 mbox = (mbox_t *)raw_mbox;
4110 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4112 * issue command to find out if controller supports extended CDBs.
4114 raw_mbox[0] = 0xA4;
4115 raw_mbox[2] = 0x16;
4117 rval = issue_scb_block(adapter, raw_mbox);
4119 return !rval;
4124 * mega_del_logdrv()
4125 * @adapter - pointer to our soft state
4126 * @logdrv - logical drive to be deleted
4128 * Delete the specified logical drive. It is the responsibility of the user
4129 * app to let the OS know about this operation.
4131 static int
4132 mega_del_logdrv(adapter_t *adapter, int logdrv)
4134 unsigned long flags;
4135 scb_t *scb;
4136 int rval;
4139 * Stop sending commands to the controller, queue them internally.
4140 * When deletion is complete, ISR will flush the queue.
4142 atomic_set(&adapter->quiescent, 1);
4145 * Wait till all the issued commands are complete and there are no
4146 * commands in the pending queue
4148 while (atomic_read(&adapter->pend_cmds) > 0 ||
4149 !list_empty(&adapter->pending_list))
4150 msleep(1000); /* sleep for 1s */
4152 rval = mega_do_del_logdrv(adapter, logdrv);
4154 spin_lock_irqsave(&adapter->lock, flags);
4157 * If delete operation was successful, add 0x80 to the logical drive
4158 * ids for commands in the pending queue.
4160 if (adapter->read_ldidmap) {
4161 struct list_head *pos;
4162 list_for_each(pos, &adapter->pending_list) {
4163 scb = list_entry(pos, scb_t, list);
4164 if (scb->pthru->logdrv < 0x80 )
4165 scb->pthru->logdrv += 0x80;
4169 atomic_set(&adapter->quiescent, 0);
4171 mega_runpendq(adapter);
4173 spin_unlock_irqrestore(&adapter->lock, flags);
4175 return rval;
4179 static int
4180 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
4182 megacmd_t mc;
4183 int rval;
4185 memset( &mc, 0, sizeof(megacmd_t));
4187 mc.cmd = FC_DEL_LOGDRV;
4188 mc.opcode = OP_DEL_LOGDRV;
4189 mc.subopcode = logdrv;
4191 rval = mega_internal_command(adapter, &mc, NULL);
4193 /* log this event */
4194 if(rval) {
4195 printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
4196 return rval;
4200 * After deleting first logical drive, the logical drives must be
4201 * addressed by adding 0x80 to the logical drive id.
4203 adapter->read_ldidmap = 1;
4205 return rval;
4210 * mega_get_max_sgl()
4211 * @adapter - pointer to our soft state
4213 * Find out the maximum number of scatter-gather elements supported by this
4214 * version of the firmware
4216 static void
4217 mega_get_max_sgl(adapter_t *adapter)
4219 unsigned char raw_mbox[sizeof(struct mbox_out)];
4220 mbox_t *mbox;
4222 mbox = (mbox_t *)raw_mbox;
4224 memset(mbox, 0, sizeof(raw_mbox));
4226 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4228 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4230 raw_mbox[0] = MAIN_MISC_OPCODE;
4231 raw_mbox[2] = GET_MAX_SG_SUPPORT;
4234 if( issue_scb_block(adapter, raw_mbox) ) {
4236 * f/w does not support this command. Choose the default value
4238 adapter->sglen = MIN_SGLIST;
4240 else {
4241 adapter->sglen = *((char *)adapter->mega_buffer);
4244 * Make sure this is not more than the resources we are
4245 * planning to allocate
4247 if ( adapter->sglen > MAX_SGLIST )
4248 adapter->sglen = MAX_SGLIST;
4251 return;
4256 * mega_support_cluster()
4257 * @adapter - pointer to our soft state
4259 * Find out if this firmware support cluster calls.
4261 static int
4262 mega_support_cluster(adapter_t *adapter)
4264 unsigned char raw_mbox[sizeof(struct mbox_out)];
4265 mbox_t *mbox;
4267 mbox = (mbox_t *)raw_mbox;
4269 memset(mbox, 0, sizeof(raw_mbox));
4271 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4273 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4276 * Try to get the initiator id. This command will succeed iff the
4277 * clustering is available on this HBA.
4279 raw_mbox[0] = MEGA_GET_TARGET_ID;
4281 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
4284 * Cluster support available. Get the initiator target id.
4285 * Tell our id to mid-layer too.
4287 adapter->this_id = *(u32 *)adapter->mega_buffer;
4288 adapter->host->this_id = adapter->this_id;
4290 return 1;
4293 return 0;
4296 #ifdef CONFIG_PROC_FS
4298 * mega_adapinq()
4299 * @adapter - pointer to our soft state
4300 * @dma_handle - DMA address of the buffer
4302 * Issue internal commands while interrupts are available.
4303 * We only issue direct mailbox commands from within the driver. ioctl()
4304 * interface using these routines can issue passthru commands.
4306 static int
4307 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
4309 megacmd_t mc;
4311 memset(&mc, 0, sizeof(megacmd_t));
4313 if( adapter->flag & BOARD_40LD ) {
4314 mc.cmd = FC_NEW_CONFIG;
4315 mc.opcode = NC_SUBOP_ENQUIRY3;
4316 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
4318 else {
4319 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
4322 mc.xferaddr = (u32)dma_handle;
4324 if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
4325 return -1;
4328 return 0;
4332 /** mega_internal_dev_inquiry()
4333 * @adapter - pointer to our soft state
4334 * @ch - channel for this device
4335 * @tgt - ID of this device
4336 * @buf_dma_handle - DMA address of the buffer
4338 * Issue the scsi inquiry for the specified device.
4340 static int
4341 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4342 dma_addr_t buf_dma_handle)
4344 mega_passthru *pthru;
4345 dma_addr_t pthru_dma_handle;
4346 megacmd_t mc;
4347 int rval;
4348 struct pci_dev *pdev;
4352 * For all internal commands, the buffer must be allocated in <4GB
4353 * address range
4355 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4357 pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4358 &pthru_dma_handle);
4360 if( pthru == NULL ) {
4361 free_local_pdev(pdev);
4362 return -1;
4365 pthru->timeout = 2;
4366 pthru->ars = 1;
4367 pthru->reqsenselen = 14;
4368 pthru->islogical = 0;
4370 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4372 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4374 pthru->cdblen = 6;
4376 pthru->cdb[0] = INQUIRY;
4377 pthru->cdb[1] = 0;
4378 pthru->cdb[2] = 0;
4379 pthru->cdb[3] = 0;
4380 pthru->cdb[4] = 255;
4381 pthru->cdb[5] = 0;
4384 pthru->dataxferaddr = (u32)buf_dma_handle;
4385 pthru->dataxferlen = 256;
4387 memset(&mc, 0, sizeof(megacmd_t));
4389 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4390 mc.xferaddr = (u32)pthru_dma_handle;
4392 rval = mega_internal_command(adapter, &mc, pthru);
4394 pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4395 pthru_dma_handle);
4397 free_local_pdev(pdev);
4399 return rval;
4401 #endif
4404 * mega_internal_command()
4405 * @adapter - pointer to our soft state
4406 * @mc - the mailbox command
4407 * @pthru - Passthru structure for DCDB commands
4409 * Issue the internal commands in interrupt mode.
4410 * The last argument is the address of the passthru structure if the command
4411 * to be fired is a passthru command
4413 * lockscope specifies whether the caller has already acquired the lock. Of
4414 * course, the caller must know which lock we are talking about.
4416 * Note: parameter 'pthru' is null for non-passthru commands.
4418 static int
4419 mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4421 Scsi_Cmnd *scmd;
4422 struct scsi_device *sdev;
4423 scb_t *scb;
4424 int rval;
4426 scmd = scsi_allocate_command(GFP_KERNEL);
4427 if (!scmd)
4428 return -ENOMEM;
4431 * The internal commands share one command id and hence are
4432 * serialized. This is so because we want to reserve maximum number of
4433 * available command ids for the I/O commands.
4435 mutex_lock(&adapter->int_mtx);
4437 scb = &adapter->int_scb;
4438 memset(scb, 0, sizeof(scb_t));
4440 sdev = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
4441 scmd->device = sdev;
4443 memset(adapter->int_cdb, 0, sizeof(adapter->int_cdb));
4444 scmd->cmnd = adapter->int_cdb;
4445 scmd->device->host = adapter->host;
4446 scmd->host_scribble = (void *)scb;
4447 scmd->cmnd[0] = MEGA_INTERNAL_CMD;
4449 scb->state |= SCB_ACTIVE;
4450 scb->cmd = scmd;
4452 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4455 * Is it a passthru command
4457 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4459 scb->pthru = pthru;
4462 scb->idx = CMDID_INT_CMDS;
4464 megaraid_queue_lck(scmd, mega_internal_done);
4466 wait_for_completion(&adapter->int_waitq);
4468 rval = scmd->result;
4469 mc->status = scmd->result;
4470 kfree(sdev);
4473 * Print a debug message for all failed commands. Applications can use
4474 * this information.
4476 if( scmd->result && trace_level ) {
4477 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4478 mc->cmd, mc->opcode, mc->subopcode, scmd->result);
4481 mutex_unlock(&adapter->int_mtx);
4483 scsi_free_command(GFP_KERNEL, scmd);
4485 return rval;
4490 * mega_internal_done()
4491 * @scmd - internal scsi command
4493 * Callback routine for internal commands.
4495 static void
4496 mega_internal_done(Scsi_Cmnd *scmd)
4498 adapter_t *adapter;
4500 adapter = (adapter_t *)scmd->device->host->hostdata;
4502 complete(&adapter->int_waitq);
4507 static struct scsi_host_template megaraid_template = {
4508 .module = THIS_MODULE,
4509 .name = "MegaRAID",
4510 .proc_name = "megaraid_legacy",
4511 .info = megaraid_info,
4512 .queuecommand = megaraid_queue,
4513 .bios_param = megaraid_biosparam,
4514 .max_sectors = MAX_SECTORS_PER_IO,
4515 .can_queue = MAX_COMMANDS,
4516 .this_id = DEFAULT_INITIATOR_ID,
4517 .sg_tablesize = MAX_SGLIST,
4518 .cmd_per_lun = DEF_CMD_PER_LUN,
4519 .use_clustering = ENABLE_CLUSTERING,
4520 .eh_abort_handler = megaraid_abort,
4521 .eh_device_reset_handler = megaraid_reset,
4522 .eh_bus_reset_handler = megaraid_reset,
4523 .eh_host_reset_handler = megaraid_reset,
4526 static int __devinit
4527 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4529 struct Scsi_Host *host;
4530 adapter_t *adapter;
4531 unsigned long mega_baseport, tbase, flag = 0;
4532 u16 subsysid, subsysvid;
4533 u8 pci_bus, pci_dev_func;
4534 int irq, i, j;
4535 int error = -ENODEV;
4537 if (pci_enable_device(pdev))
4538 goto out;
4539 pci_set_master(pdev);
4541 pci_bus = pdev->bus->number;
4542 pci_dev_func = pdev->devfn;
4545 * The megaraid3 stuff reports the ID of the Intel part which is not
4546 * remotely specific to the megaraid
4548 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4549 u16 magic;
4551 * Don't fall over the Compaq management cards using the same
4552 * PCI identifier
4554 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4555 pdev->subsystem_device == 0xC000)
4556 return -ENODEV;
4557 /* Now check the magic signature byte */
4558 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4559 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4560 return -ENODEV;
4561 /* Ok it is probably a megaraid */
4565 * For these vendor and device ids, signature offsets are not
4566 * valid and 64 bit is implicit
4568 if (id->driver_data & BOARD_64BIT)
4569 flag |= BOARD_64BIT;
4570 else {
4571 u32 magic64;
4573 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4574 if (magic64 == HBA_SIGNATURE_64BIT)
4575 flag |= BOARD_64BIT;
4578 subsysvid = pdev->subsystem_vendor;
4579 subsysid = pdev->subsystem_device;
4581 printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4582 id->vendor, id->device, pci_bus);
4584 printk("slot %d:func %d\n",
4585 PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
4587 /* Read the base port and IRQ from PCI */
4588 mega_baseport = pci_resource_start(pdev, 0);
4589 irq = pdev->irq;
4591 tbase = mega_baseport;
4592 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4593 flag |= BOARD_MEMMAP;
4595 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4596 printk(KERN_WARNING "megaraid: mem region busy!\n");
4597 goto out_disable_device;
4600 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4601 if (!mega_baseport) {
4602 printk(KERN_WARNING
4603 "megaraid: could not map hba memory\n");
4604 goto out_release_region;
4606 } else {
4607 flag |= BOARD_IOMAP;
4608 mega_baseport += 0x10;
4610 if (!request_region(mega_baseport, 16, "megaraid"))
4611 goto out_disable_device;
4614 /* Initialize SCSI Host structure */
4615 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4616 if (!host)
4617 goto out_iounmap;
4619 adapter = (adapter_t *)host->hostdata;
4620 memset(adapter, 0, sizeof(adapter_t));
4622 printk(KERN_NOTICE
4623 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4624 host->host_no, mega_baseport, irq);
4626 adapter->base = mega_baseport;
4627 if (flag & BOARD_MEMMAP)
4628 adapter->mmio_base = (void __iomem *) mega_baseport;
4630 INIT_LIST_HEAD(&adapter->free_list);
4631 INIT_LIST_HEAD(&adapter->pending_list);
4632 INIT_LIST_HEAD(&adapter->completed_list);
4634 adapter->flag = flag;
4635 spin_lock_init(&adapter->lock);
4637 host->cmd_per_lun = max_cmd_per_lun;
4638 host->max_sectors = max_sectors_per_io;
4640 adapter->dev = pdev;
4641 adapter->host = host;
4643 adapter->host->irq = irq;
4645 if (flag & BOARD_MEMMAP)
4646 adapter->host->base = tbase;
4647 else {
4648 adapter->host->io_port = tbase;
4649 adapter->host->n_io_port = 16;
4652 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4655 * Allocate buffer to issue internal commands.
4657 adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4658 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4659 if (!adapter->mega_buffer) {
4660 printk(KERN_WARNING "megaraid: out of RAM.\n");
4661 goto out_host_put;
4664 adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4665 if (!adapter->scb_list) {
4666 printk(KERN_WARNING "megaraid: out of RAM.\n");
4667 goto out_free_cmd_buffer;
4670 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4671 megaraid_isr_memmapped : megaraid_isr_iomapped,
4672 IRQF_SHARED, "megaraid", adapter)) {
4673 printk(KERN_WARNING
4674 "megaraid: Couldn't register IRQ %d!\n", irq);
4675 goto out_free_scb_list;
4678 if (mega_setup_mailbox(adapter))
4679 goto out_free_irq;
4681 if (mega_query_adapter(adapter))
4682 goto out_free_mbox;
4685 * Have checks for some buggy f/w
4687 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4689 * Which firmware
4691 if (!strcmp(adapter->fw_version, "3.00") ||
4692 !strcmp(adapter->fw_version, "3.01")) {
4694 printk( KERN_WARNING
4695 "megaraid: Your card is a Dell PERC "
4696 "2/SC RAID controller with "
4697 "firmware\nmegaraid: 3.00 or 3.01. "
4698 "This driver is known to have "
4699 "corruption issues\nmegaraid: with "
4700 "those firmware versions on this "
4701 "specific card. In order\nmegaraid: "
4702 "to protect your data, please upgrade "
4703 "your firmware to version\nmegaraid: "
4704 "3.10 or later, available from the "
4705 "Dell Technical Support web\n"
4706 "megaraid: site at\nhttp://support."
4707 "dell.com/us/en/filelib/download/"
4708 "index.asp?fileid=2940\n"
4714 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4715 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4716 * support, since this firmware cannot handle 64 bit
4717 * addressing
4719 if ((subsysvid == HP_SUBSYS_VID) &&
4720 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4722 * which firmware
4724 if (!strcmp(adapter->fw_version, "H01.07") ||
4725 !strcmp(adapter->fw_version, "H01.08") ||
4726 !strcmp(adapter->fw_version, "H01.09") ) {
4727 printk(KERN_WARNING
4728 "megaraid: Firmware H.01.07, "
4729 "H.01.08, and H.01.09 on 1M/2M "
4730 "controllers\n"
4731 "megaraid: do not support 64 bit "
4732 "addressing.\nmegaraid: DISABLING "
4733 "64 bit support.\n");
4734 adapter->flag &= ~BOARD_64BIT;
4738 if (mega_is_bios_enabled(adapter))
4739 mega_hbas[hba_count].is_bios_enabled = 1;
4740 mega_hbas[hba_count].hostdata_addr = adapter;
4743 * Find out which channel is raid and which is scsi. This is
4744 * for ROMB support.
4746 mega_enum_raid_scsi(adapter);
4749 * Find out if a logical drive is set as the boot drive. If
4750 * there is one, will make that as the first logical drive.
4751 * ROMB: Do we have to boot from a physical drive. Then all
4752 * the physical drives would appear before the logical disks.
4753 * Else, all the physical drives would be exported to the mid
4754 * layer after logical drives.
4756 mega_get_boot_drv(adapter);
4758 if (adapter->boot_pdrv_enabled) {
4759 j = adapter->product_info.nchannels;
4760 for( i = 0; i < j; i++ )
4761 adapter->logdrv_chan[i] = 0;
4762 for( i = j; i < NVIRT_CHAN + j; i++ )
4763 adapter->logdrv_chan[i] = 1;
4764 } else {
4765 for (i = 0; i < NVIRT_CHAN; i++)
4766 adapter->logdrv_chan[i] = 1;
4767 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4768 adapter->logdrv_chan[i] = 0;
4769 adapter->mega_ch_class <<= NVIRT_CHAN;
4773 * Do we support random deletion and addition of logical
4774 * drives
4776 adapter->read_ldidmap = 0; /* set it after first logdrv
4777 delete cmd */
4778 adapter->support_random_del = mega_support_random_del(adapter);
4780 /* Initialize SCBs */
4781 if (mega_init_scb(adapter))
4782 goto out_free_mbox;
4785 * Reset the pending commands counter
4787 atomic_set(&adapter->pend_cmds, 0);
4790 * Reset the adapter quiescent flag
4792 atomic_set(&adapter->quiescent, 0);
4794 hba_soft_state[hba_count] = adapter;
4797 * Fill in the structure which needs to be passed back to the
4798 * application when it does an ioctl() for controller related
4799 * information.
4801 i = hba_count;
4803 mcontroller[i].base = mega_baseport;
4804 mcontroller[i].irq = irq;
4805 mcontroller[i].numldrv = adapter->numldrv;
4806 mcontroller[i].pcibus = pci_bus;
4807 mcontroller[i].pcidev = id->device;
4808 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4809 mcontroller[i].pciid = -1;
4810 mcontroller[i].pcivendor = id->vendor;
4811 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4812 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4815 /* Set the Mode of addressing to 64 bit if we can */
4816 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4817 pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4818 adapter->has_64bit_addr = 1;
4819 } else {
4820 pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4821 adapter->has_64bit_addr = 0;
4824 mutex_init(&adapter->int_mtx);
4825 init_completion(&adapter->int_waitq);
4827 adapter->this_id = DEFAULT_INITIATOR_ID;
4828 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4830 #if MEGA_HAVE_CLUSTERING
4832 * Is cluster support enabled on this controller
4833 * Note: In a cluster the HBAs ( the initiators ) will have
4834 * different target IDs and we cannot assume it to be 7. Call
4835 * to mega_support_cluster() will get the target ids also if
4836 * the cluster support is available
4838 adapter->has_cluster = mega_support_cluster(adapter);
4839 if (adapter->has_cluster) {
4840 printk(KERN_NOTICE
4841 "megaraid: Cluster driver, initiator id:%d\n",
4842 adapter->this_id);
4844 #endif
4846 pci_set_drvdata(pdev, host);
4848 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4850 error = scsi_add_host(host, &pdev->dev);
4851 if (error)
4852 goto out_free_mbox;
4854 scsi_scan_host(host);
4855 hba_count++;
4856 return 0;
4858 out_free_mbox:
4859 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4860 adapter->una_mbox64, adapter->una_mbox64_dma);
4861 out_free_irq:
4862 free_irq(adapter->host->irq, adapter);
4863 out_free_scb_list:
4864 kfree(adapter->scb_list);
4865 out_free_cmd_buffer:
4866 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4867 adapter->mega_buffer, adapter->buf_dma_handle);
4868 out_host_put:
4869 scsi_host_put(host);
4870 out_iounmap:
4871 if (flag & BOARD_MEMMAP)
4872 iounmap((void *)mega_baseport);
4873 out_release_region:
4874 if (flag & BOARD_MEMMAP)
4875 release_mem_region(tbase, 128);
4876 else
4877 release_region(mega_baseport, 16);
4878 out_disable_device:
4879 pci_disable_device(pdev);
4880 out:
4881 return error;
4884 static void
4885 __megaraid_shutdown(adapter_t *adapter)
4887 u_char raw_mbox[sizeof(struct mbox_out)];
4888 mbox_t *mbox = (mbox_t *)raw_mbox;
4889 int i;
4891 /* Flush adapter cache */
4892 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4893 raw_mbox[0] = FLUSH_ADAPTER;
4895 free_irq(adapter->host->irq, adapter);
4897 /* Issue a blocking (interrupts disabled) command to the card */
4898 issue_scb_block(adapter, raw_mbox);
4900 /* Flush disks cache */
4901 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4902 raw_mbox[0] = FLUSH_SYSTEM;
4904 /* Issue a blocking (interrupts disabled) command to the card */
4905 issue_scb_block(adapter, raw_mbox);
4907 if (atomic_read(&adapter->pend_cmds) > 0)
4908 printk(KERN_WARNING "megaraid: pending commands!!\n");
4911 * Have a delibrate delay to make sure all the caches are
4912 * actually flushed.
4914 for (i = 0; i <= 10; i++)
4915 mdelay(1000);
4918 static void __devexit
4919 megaraid_remove_one(struct pci_dev *pdev)
4921 struct Scsi_Host *host = pci_get_drvdata(pdev);
4922 adapter_t *adapter = (adapter_t *)host->hostdata;
4924 scsi_remove_host(host);
4926 __megaraid_shutdown(adapter);
4928 /* Free our resources */
4929 if (adapter->flag & BOARD_MEMMAP) {
4930 iounmap((void *)adapter->base);
4931 release_mem_region(adapter->host->base, 128);
4932 } else
4933 release_region(adapter->base, 16);
4935 mega_free_sgl(adapter);
4937 #ifdef CONFIG_PROC_FS
4938 if (adapter->controller_proc_dir_entry) {
4939 remove_proc_entry("stat", adapter->controller_proc_dir_entry);
4940 remove_proc_entry("config",
4941 adapter->controller_proc_dir_entry);
4942 remove_proc_entry("mailbox",
4943 adapter->controller_proc_dir_entry);
4944 #if MEGA_HAVE_ENH_PROC
4945 remove_proc_entry("rebuild-rate",
4946 adapter->controller_proc_dir_entry);
4947 remove_proc_entry("battery-status",
4948 adapter->controller_proc_dir_entry);
4950 remove_proc_entry("diskdrives-ch0",
4951 adapter->controller_proc_dir_entry);
4952 remove_proc_entry("diskdrives-ch1",
4953 adapter->controller_proc_dir_entry);
4954 remove_proc_entry("diskdrives-ch2",
4955 adapter->controller_proc_dir_entry);
4956 remove_proc_entry("diskdrives-ch3",
4957 adapter->controller_proc_dir_entry);
4959 remove_proc_entry("raiddrives-0-9",
4960 adapter->controller_proc_dir_entry);
4961 remove_proc_entry("raiddrives-10-19",
4962 adapter->controller_proc_dir_entry);
4963 remove_proc_entry("raiddrives-20-29",
4964 adapter->controller_proc_dir_entry);
4965 remove_proc_entry("raiddrives-30-39",
4966 adapter->controller_proc_dir_entry);
4967 #endif
4969 char buf[12] = { 0 };
4970 sprintf(buf, "hba%d", adapter->host->host_no);
4971 remove_proc_entry(buf, mega_proc_dir_entry);
4974 #endif
4976 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4977 adapter->mega_buffer, adapter->buf_dma_handle);
4978 kfree(adapter->scb_list);
4979 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4980 adapter->una_mbox64, adapter->una_mbox64_dma);
4982 scsi_host_put(host);
4983 pci_disable_device(pdev);
4985 hba_count--;
4988 static void
4989 megaraid_shutdown(struct pci_dev *pdev)
4991 struct Scsi_Host *host = pci_get_drvdata(pdev);
4992 adapter_t *adapter = (adapter_t *)host->hostdata;
4994 __megaraid_shutdown(adapter);
4997 static struct pci_device_id megaraid_pci_tbl[] = {
4998 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4999 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5000 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
5001 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5002 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
5003 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5004 {0,}
5006 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
5008 static struct pci_driver megaraid_pci_driver = {
5009 .name = "megaraid_legacy",
5010 .id_table = megaraid_pci_tbl,
5011 .probe = megaraid_probe_one,
5012 .remove = __devexit_p(megaraid_remove_one),
5013 .shutdown = megaraid_shutdown,
5016 static int __init megaraid_init(void)
5018 int error;
5020 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
5021 max_cmd_per_lun = MAX_CMD_PER_LUN;
5022 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
5023 max_mbox_busy_wait = MBOX_BUSY_WAIT;
5025 #ifdef CONFIG_PROC_FS
5026 mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
5027 if (!mega_proc_dir_entry) {
5028 printk(KERN_WARNING
5029 "megaraid: failed to create megaraid root\n");
5031 #endif
5032 error = pci_register_driver(&megaraid_pci_driver);
5033 if (error) {
5034 #ifdef CONFIG_PROC_FS
5035 remove_proc_entry("megaraid", NULL);
5036 #endif
5037 return error;
5041 * Register the driver as a character device, for applications
5042 * to access it for ioctls.
5043 * First argument (major) to register_chrdev implies a dynamic
5044 * major number allocation.
5046 major = register_chrdev(0, "megadev_legacy", &megadev_fops);
5047 if (!major) {
5048 printk(KERN_WARNING
5049 "megaraid: failed to register char device\n");
5052 return 0;
5055 static void __exit megaraid_exit(void)
5058 * Unregister the character device interface to the driver.
5060 unregister_chrdev(major, "megadev_legacy");
5062 pci_unregister_driver(&megaraid_pci_driver);
5064 #ifdef CONFIG_PROC_FS
5065 remove_proc_entry("megaraid", NULL);
5066 #endif
5069 module_init(megaraid_init);
5070 module_exit(megaraid_exit);
5072 /* vi: set ts=8 sw=8 tw=78: */