drm/nouveau/devinit: ensure legacy vga control is enabled during post
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / megaraid.c
blob76ad72d32c3f5ef8123b0b32b921de5ad7634f86
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;
308 /* use HP firmware and bios version encoding
309 Note: fw_version[0|1] and bios_version[0|1] were originally shifted
310 right 8 bits making them zero. This 0 value was hardcoded to fix
311 sparse warnings. */
312 if (adapter->product_info.subsysvid == PCI_VENDOR_ID_HP) {
313 sprintf (adapter->fw_version, "%c%d%d.%d%d",
314 adapter->product_info.fw_version[2],
316 adapter->product_info.fw_version[1] & 0x0f,
318 adapter->product_info.fw_version[0] & 0x0f);
319 sprintf (adapter->bios_version, "%c%d%d.%d%d",
320 adapter->product_info.bios_version[2],
322 adapter->product_info.bios_version[1] & 0x0f,
324 adapter->product_info.bios_version[0] & 0x0f);
325 } else {
326 memcpy(adapter->fw_version,
327 (char *)adapter->product_info.fw_version, 4);
328 adapter->fw_version[4] = 0;
330 memcpy(adapter->bios_version,
331 (char *)adapter->product_info.bios_version, 4);
333 adapter->bios_version[4] = 0;
336 printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
337 adapter->fw_version, adapter->bios_version, adapter->numldrv);
340 * Do we support extended (>10 bytes) cdbs
342 adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
343 if (adapter->support_ext_cdb)
344 printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
347 return 0;
351 * mega_runpendq()
352 * @adapter - pointer to our soft state
354 * Runs through the list of pending requests.
356 static inline void
357 mega_runpendq(adapter_t *adapter)
359 if(!list_empty(&adapter->pending_list))
360 __mega_runpendq(adapter);
364 * megaraid_queue()
365 * @scmd - Issue this scsi command
366 * @done - the callback hook into the scsi mid-layer
368 * The command queuing entry point for the mid-layer.
370 static int
371 megaraid_queue_lck(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
373 adapter_t *adapter;
374 scb_t *scb;
375 int busy=0;
376 unsigned long flags;
378 adapter = (adapter_t *)scmd->device->host->hostdata;
380 scmd->scsi_done = done;
384 * Allocate and build a SCB request
385 * busy flag will be set if mega_build_cmd() command could not
386 * allocate scb. We will return non-zero status in that case.
387 * NOTE: scb can be null even though certain commands completed
388 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
389 * return 0 in that case.
392 spin_lock_irqsave(&adapter->lock, flags);
393 scb = mega_build_cmd(adapter, scmd, &busy);
394 if (!scb)
395 goto out;
397 scb->state |= SCB_PENDQ;
398 list_add_tail(&scb->list, &adapter->pending_list);
401 * Check if the HBA is in quiescent state, e.g., during a
402 * delete logical drive opertion. If it is, don't run
403 * the pending_list.
405 if (atomic_read(&adapter->quiescent) == 0)
406 mega_runpendq(adapter);
408 busy = 0;
409 out:
410 spin_unlock_irqrestore(&adapter->lock, flags);
411 return busy;
414 static DEF_SCSI_QCMD(megaraid_queue)
417 * mega_allocate_scb()
418 * @adapter - pointer to our soft state
419 * @cmd - scsi command from the mid-layer
421 * Allocate a SCB structure. This is the central structure for controller
422 * commands.
424 static inline scb_t *
425 mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
427 struct list_head *head = &adapter->free_list;
428 scb_t *scb;
430 /* Unlink command from Free List */
431 if( !list_empty(head) ) {
433 scb = list_entry(head->next, scb_t, list);
435 list_del_init(head->next);
437 scb->state = SCB_ACTIVE;
438 scb->cmd = cmd;
439 scb->dma_type = MEGA_DMA_TYPE_NONE;
441 return scb;
444 return NULL;
448 * mega_get_ldrv_num()
449 * @adapter - pointer to our soft state
450 * @cmd - scsi mid layer command
451 * @channel - channel on the controller
453 * Calculate the logical drive number based on the information in scsi command
454 * and the channel number.
456 static inline int
457 mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
459 int tgt;
460 int ldrv_num;
462 tgt = cmd->device->id;
464 if ( tgt > adapter->this_id )
465 tgt--; /* we do not get inquires for initiator id */
467 ldrv_num = (channel * 15) + tgt;
471 * If we have a logical drive with boot enabled, project it first
473 if( adapter->boot_ldrv_enabled ) {
474 if( ldrv_num == 0 ) {
475 ldrv_num = adapter->boot_ldrv;
477 else {
478 if( ldrv_num <= adapter->boot_ldrv ) {
479 ldrv_num--;
485 * If "delete logical drive" feature is enabled on this controller.
486 * Do only if at least one delete logical drive operation was done.
488 * Also, after logical drive deletion, instead of logical drive number,
489 * the value returned should be 0x80+logical drive id.
491 * These is valid only for IO commands.
494 if (adapter->support_random_del && adapter->read_ldidmap )
495 switch (cmd->cmnd[0]) {
496 case READ_6: /* fall through */
497 case WRITE_6: /* fall through */
498 case READ_10: /* fall through */
499 case WRITE_10:
500 ldrv_num += 0x80;
503 return ldrv_num;
507 * mega_build_cmd()
508 * @adapter - pointer to our soft state
509 * @cmd - Prepare using this scsi command
510 * @busy - busy flag if no resources
512 * Prepares a command and scatter gather list for the controller. This routine
513 * also finds out if the commands is intended for a logical drive or a
514 * physical device and prepares the controller command accordingly.
516 * We also re-order the logical drives and physical devices based on their
517 * boot settings.
519 static scb_t *
520 mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
522 mega_ext_passthru *epthru;
523 mega_passthru *pthru;
524 scb_t *scb;
525 mbox_t *mbox;
526 u32 seg;
527 char islogical;
528 int max_ldrv_num;
529 int channel = 0;
530 int target = 0;
531 int ldrv_num = 0; /* logical drive number */
535 * filter the internal and ioctl commands
537 if((cmd->cmnd[0] == MEGA_INTERNAL_CMD))
538 return (scb_t *)cmd->host_scribble;
541 * We know what channels our logical drives are on - mega_find_card()
543 islogical = adapter->logdrv_chan[cmd->device->channel];
546 * The theory: If physical drive is chosen for boot, all the physical
547 * devices are exported before the logical drives, otherwise physical
548 * devices are pushed after logical drives, in which case - Kernel sees
549 * the physical devices on virtual channel which is obviously converted
550 * to actual channel on the HBA.
552 if( adapter->boot_pdrv_enabled ) {
553 if( islogical ) {
554 /* logical channel */
555 channel = cmd->device->channel -
556 adapter->product_info.nchannels;
558 else {
559 /* this is physical channel */
560 channel = cmd->device->channel;
561 target = cmd->device->id;
564 * boot from a physical disk, that disk needs to be
565 * exposed first IF both the channels are SCSI, then
566 * booting from the second channel is not allowed.
568 if( target == 0 ) {
569 target = adapter->boot_pdrv_tgt;
571 else if( target == adapter->boot_pdrv_tgt ) {
572 target = 0;
576 else {
577 if( islogical ) {
578 /* this is the logical channel */
579 channel = cmd->device->channel;
581 else {
582 /* physical channel */
583 channel = cmd->device->channel - NVIRT_CHAN;
584 target = cmd->device->id;
589 if(islogical) {
591 /* have just LUN 0 for each target on virtual channels */
592 if (cmd->device->lun) {
593 cmd->result = (DID_BAD_TARGET << 16);
594 cmd->scsi_done(cmd);
595 return NULL;
598 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
601 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
602 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
605 * max_ldrv_num increases by 0x80 if some logical drive was
606 * deleted.
608 if(adapter->read_ldidmap)
609 max_ldrv_num += 0x80;
611 if(ldrv_num > max_ldrv_num ) {
612 cmd->result = (DID_BAD_TARGET << 16);
613 cmd->scsi_done(cmd);
614 return NULL;
618 else {
619 if( cmd->device->lun > 7) {
621 * Do not support lun >7 for physically accessed
622 * devices
624 cmd->result = (DID_BAD_TARGET << 16);
625 cmd->scsi_done(cmd);
626 return NULL;
632 * Logical drive commands
635 if(islogical) {
636 switch (cmd->cmnd[0]) {
637 case TEST_UNIT_READY:
638 #if MEGA_HAVE_CLUSTERING
640 * Do we support clustering and is the support enabled
641 * If no, return success always
643 if( !adapter->has_cluster ) {
644 cmd->result = (DID_OK << 16);
645 cmd->scsi_done(cmd);
646 return NULL;
649 if(!(scb = mega_allocate_scb(adapter, cmd))) {
650 *busy = 1;
651 return NULL;
654 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
655 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
656 scb->raw_mbox[3] = ldrv_num;
658 scb->dma_direction = PCI_DMA_NONE;
660 return scb;
661 #else
662 cmd->result = (DID_OK << 16);
663 cmd->scsi_done(cmd);
664 return NULL;
665 #endif
667 case MODE_SENSE: {
668 char *buf;
669 struct scatterlist *sg;
671 sg = scsi_sglist(cmd);
672 buf = kmap_atomic(sg_page(sg)) + sg->offset;
674 memset(buf, 0, cmd->cmnd[4]);
675 kunmap_atomic(buf - sg->offset);
677 cmd->result = (DID_OK << 16);
678 cmd->scsi_done(cmd);
679 return NULL;
682 case READ_CAPACITY:
683 case INQUIRY:
685 if(!(adapter->flag & (1L << cmd->device->channel))) {
687 printk(KERN_NOTICE
688 "scsi%d: scanning scsi channel %d ",
689 adapter->host->host_no,
690 cmd->device->channel);
691 printk("for logical drives.\n");
693 adapter->flag |= (1L << cmd->device->channel);
696 /* Allocate a SCB and initialize passthru */
697 if(!(scb = mega_allocate_scb(adapter, cmd))) {
698 *busy = 1;
699 return NULL;
701 pthru = scb->pthru;
703 mbox = (mbox_t *)scb->raw_mbox;
704 memset(mbox, 0, sizeof(scb->raw_mbox));
705 memset(pthru, 0, sizeof(mega_passthru));
707 pthru->timeout = 0;
708 pthru->ars = 1;
709 pthru->reqsenselen = 14;
710 pthru->islogical = 1;
711 pthru->logdrv = ldrv_num;
712 pthru->cdblen = cmd->cmd_len;
713 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
715 if( adapter->has_64bit_addr ) {
716 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
718 else {
719 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
722 scb->dma_direction = PCI_DMA_FROMDEVICE;
724 pthru->numsgelements = mega_build_sglist(adapter, scb,
725 &pthru->dataxferaddr, &pthru->dataxferlen);
727 mbox->m_out.xferaddr = scb->pthru_dma_addr;
729 return scb;
731 case READ_6:
732 case WRITE_6:
733 case READ_10:
734 case WRITE_10:
735 case READ_12:
736 case WRITE_12:
738 /* Allocate a SCB and initialize mailbox */
739 if(!(scb = mega_allocate_scb(adapter, cmd))) {
740 *busy = 1;
741 return NULL;
743 mbox = (mbox_t *)scb->raw_mbox;
745 memset(mbox, 0, sizeof(scb->raw_mbox));
746 mbox->m_out.logdrv = ldrv_num;
749 * A little hack: 2nd bit is zero for all scsi read
750 * commands and is set for all scsi write commands
752 if( adapter->has_64bit_addr ) {
753 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
754 MEGA_MBOXCMD_LWRITE64:
755 MEGA_MBOXCMD_LREAD64 ;
757 else {
758 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
759 MEGA_MBOXCMD_LWRITE:
760 MEGA_MBOXCMD_LREAD ;
764 * 6-byte READ(0x08) or WRITE(0x0A) cdb
766 if( cmd->cmd_len == 6 ) {
767 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
768 mbox->m_out.lba =
769 ((u32)cmd->cmnd[1] << 16) |
770 ((u32)cmd->cmnd[2] << 8) |
771 (u32)cmd->cmnd[3];
773 mbox->m_out.lba &= 0x1FFFFF;
775 #if MEGA_HAVE_STATS
777 * Take modulo 0x80, since the logical drive
778 * number increases by 0x80 when a logical
779 * drive was deleted
781 if (*cmd->cmnd == READ_6) {
782 adapter->nreads[ldrv_num%0x80]++;
783 adapter->nreadblocks[ldrv_num%0x80] +=
784 mbox->m_out.numsectors;
785 } else {
786 adapter->nwrites[ldrv_num%0x80]++;
787 adapter->nwriteblocks[ldrv_num%0x80] +=
788 mbox->m_out.numsectors;
790 #endif
794 * 10-byte READ(0x28) or WRITE(0x2A) cdb
796 if( cmd->cmd_len == 10 ) {
797 mbox->m_out.numsectors =
798 (u32)cmd->cmnd[8] |
799 ((u32)cmd->cmnd[7] << 8);
800 mbox->m_out.lba =
801 ((u32)cmd->cmnd[2] << 24) |
802 ((u32)cmd->cmnd[3] << 16) |
803 ((u32)cmd->cmnd[4] << 8) |
804 (u32)cmd->cmnd[5];
806 #if MEGA_HAVE_STATS
807 if (*cmd->cmnd == READ_10) {
808 adapter->nreads[ldrv_num%0x80]++;
809 adapter->nreadblocks[ldrv_num%0x80] +=
810 mbox->m_out.numsectors;
811 } else {
812 adapter->nwrites[ldrv_num%0x80]++;
813 adapter->nwriteblocks[ldrv_num%0x80] +=
814 mbox->m_out.numsectors;
816 #endif
820 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
822 if( cmd->cmd_len == 12 ) {
823 mbox->m_out.lba =
824 ((u32)cmd->cmnd[2] << 24) |
825 ((u32)cmd->cmnd[3] << 16) |
826 ((u32)cmd->cmnd[4] << 8) |
827 (u32)cmd->cmnd[5];
829 mbox->m_out.numsectors =
830 ((u32)cmd->cmnd[6] << 24) |
831 ((u32)cmd->cmnd[7] << 16) |
832 ((u32)cmd->cmnd[8] << 8) |
833 (u32)cmd->cmnd[9];
835 #if MEGA_HAVE_STATS
836 if (*cmd->cmnd == READ_12) {
837 adapter->nreads[ldrv_num%0x80]++;
838 adapter->nreadblocks[ldrv_num%0x80] +=
839 mbox->m_out.numsectors;
840 } else {
841 adapter->nwrites[ldrv_num%0x80]++;
842 adapter->nwriteblocks[ldrv_num%0x80] +=
843 mbox->m_out.numsectors;
845 #endif
849 * If it is a read command
851 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
852 scb->dma_direction = PCI_DMA_FROMDEVICE;
854 else {
855 scb->dma_direction = PCI_DMA_TODEVICE;
858 /* Calculate Scatter-Gather info */
859 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
860 (u32 *)&mbox->m_out.xferaddr, &seg);
862 return scb;
864 #if MEGA_HAVE_CLUSTERING
865 case RESERVE: /* Fall through */
866 case RELEASE:
869 * Do we support clustering and is the support enabled
871 if( ! adapter->has_cluster ) {
873 cmd->result = (DID_BAD_TARGET << 16);
874 cmd->scsi_done(cmd);
875 return NULL;
878 /* Allocate a SCB and initialize mailbox */
879 if(!(scb = mega_allocate_scb(adapter, cmd))) {
880 *busy = 1;
881 return NULL;
884 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
885 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
886 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
888 scb->raw_mbox[3] = ldrv_num;
890 scb->dma_direction = PCI_DMA_NONE;
892 return scb;
893 #endif
895 default:
896 cmd->result = (DID_BAD_TARGET << 16);
897 cmd->scsi_done(cmd);
898 return NULL;
903 * Passthru drive commands
905 else {
906 /* Allocate a SCB and initialize passthru */
907 if(!(scb = mega_allocate_scb(adapter, cmd))) {
908 *busy = 1;
909 return NULL;
912 mbox = (mbox_t *)scb->raw_mbox;
913 memset(mbox, 0, sizeof(scb->raw_mbox));
915 if( adapter->support_ext_cdb ) {
917 epthru = mega_prepare_extpassthru(adapter, scb, cmd,
918 channel, target);
920 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
922 mbox->m_out.xferaddr = scb->epthru_dma_addr;
925 else {
927 pthru = mega_prepare_passthru(adapter, scb, cmd,
928 channel, target);
930 /* Initialize mailbox */
931 if( adapter->has_64bit_addr ) {
932 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
934 else {
935 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
938 mbox->m_out.xferaddr = scb->pthru_dma_addr;
941 return scb;
943 return NULL;
948 * mega_prepare_passthru()
949 * @adapter - pointer to our soft state
950 * @scb - our scsi control block
951 * @cmd - scsi command from the mid-layer
952 * @channel - actual channel on the controller
953 * @target - actual id on the controller.
955 * prepare a command for the scsi physical devices.
957 static mega_passthru *
958 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
959 int channel, int target)
961 mega_passthru *pthru;
963 pthru = scb->pthru;
964 memset(pthru, 0, sizeof (mega_passthru));
966 /* 0=6sec/1=60sec/2=10min/3=3hrs */
967 pthru->timeout = 2;
969 pthru->ars = 1;
970 pthru->reqsenselen = 14;
971 pthru->islogical = 0;
973 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
975 pthru->target = (adapter->flag & BOARD_40LD) ?
976 (channel << 4) | target : target;
978 pthru->cdblen = cmd->cmd_len;
979 pthru->logdrv = cmd->device->lun;
981 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
983 /* Not sure about the direction */
984 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
986 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
987 switch (cmd->cmnd[0]) {
988 case INQUIRY:
989 case READ_CAPACITY:
990 if(!(adapter->flag & (1L << cmd->device->channel))) {
992 printk(KERN_NOTICE
993 "scsi%d: scanning scsi channel %d [P%d] ",
994 adapter->host->host_no,
995 cmd->device->channel, channel);
996 printk("for physical devices.\n");
998 adapter->flag |= (1L << cmd->device->channel);
1000 /* Fall through */
1001 default:
1002 pthru->numsgelements = mega_build_sglist(adapter, scb,
1003 &pthru->dataxferaddr, &pthru->dataxferlen);
1004 break;
1006 return pthru;
1011 * mega_prepare_extpassthru()
1012 * @adapter - pointer to our soft state
1013 * @scb - our scsi control block
1014 * @cmd - scsi command from the mid-layer
1015 * @channel - actual channel on the controller
1016 * @target - actual id on the controller.
1018 * prepare a command for the scsi physical devices. This rountine prepares
1019 * commands for devices which can take extended CDBs (>10 bytes)
1021 static mega_ext_passthru *
1022 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1023 int channel, int target)
1025 mega_ext_passthru *epthru;
1027 epthru = scb->epthru;
1028 memset(epthru, 0, sizeof(mega_ext_passthru));
1030 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1031 epthru->timeout = 2;
1033 epthru->ars = 1;
1034 epthru->reqsenselen = 14;
1035 epthru->islogical = 0;
1037 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1038 epthru->target = (adapter->flag & BOARD_40LD) ?
1039 (channel << 4) | target : target;
1041 epthru->cdblen = cmd->cmd_len;
1042 epthru->logdrv = cmd->device->lun;
1044 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1046 /* Not sure about the direction */
1047 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1049 switch(cmd->cmnd[0]) {
1050 case INQUIRY:
1051 case READ_CAPACITY:
1052 if(!(adapter->flag & (1L << cmd->device->channel))) {
1054 printk(KERN_NOTICE
1055 "scsi%d: scanning scsi channel %d [P%d] ",
1056 adapter->host->host_no,
1057 cmd->device->channel, channel);
1058 printk("for physical devices.\n");
1060 adapter->flag |= (1L << cmd->device->channel);
1062 /* Fall through */
1063 default:
1064 epthru->numsgelements = mega_build_sglist(adapter, scb,
1065 &epthru->dataxferaddr, &epthru->dataxferlen);
1066 break;
1069 return epthru;
1072 static void
1073 __mega_runpendq(adapter_t *adapter)
1075 scb_t *scb;
1076 struct list_head *pos, *next;
1078 /* Issue any pending commands to the card */
1079 list_for_each_safe(pos, next, &adapter->pending_list) {
1081 scb = list_entry(pos, scb_t, list);
1083 if( !(scb->state & SCB_ISSUED) ) {
1085 if( issue_scb(adapter, scb) != 0 )
1086 return;
1090 return;
1095 * issue_scb()
1096 * @adapter - pointer to our soft state
1097 * @scb - scsi control block
1099 * Post a command to the card if the mailbox is available, otherwise return
1100 * busy. We also take the scb from the pending list if the mailbox is
1101 * available.
1103 static int
1104 issue_scb(adapter_t *adapter, scb_t *scb)
1106 volatile mbox64_t *mbox64 = adapter->mbox64;
1107 volatile mbox_t *mbox = adapter->mbox;
1108 unsigned int i = 0;
1110 if(unlikely(mbox->m_in.busy)) {
1111 do {
1112 udelay(1);
1113 i++;
1114 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1116 if(mbox->m_in.busy) return -1;
1119 /* Copy mailbox data into host structure */
1120 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1121 sizeof(struct mbox_out));
1123 mbox->m_out.cmdid = scb->idx; /* Set cmdid */
1124 mbox->m_in.busy = 1; /* Set busy */
1128 * Increment the pending queue counter
1130 atomic_inc(&adapter->pend_cmds);
1132 switch (mbox->m_out.cmd) {
1133 case MEGA_MBOXCMD_LREAD64:
1134 case MEGA_MBOXCMD_LWRITE64:
1135 case MEGA_MBOXCMD_PASSTHRU64:
1136 case MEGA_MBOXCMD_EXTPTHRU:
1137 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1138 mbox64->xfer_segment_hi = 0;
1139 mbox->m_out.xferaddr = 0xFFFFFFFF;
1140 break;
1141 default:
1142 mbox64->xfer_segment_lo = 0;
1143 mbox64->xfer_segment_hi = 0;
1147 * post the command
1149 scb->state |= SCB_ISSUED;
1151 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1152 mbox->m_in.poll = 0;
1153 mbox->m_in.ack = 0;
1154 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1156 else {
1157 irq_enable(adapter);
1158 issue_command(adapter);
1161 return 0;
1165 * Wait until the controller's mailbox is available
1167 static inline int
1168 mega_busywait_mbox (adapter_t *adapter)
1170 if (adapter->mbox->m_in.busy)
1171 return __mega_busywait_mbox(adapter);
1172 return 0;
1176 * issue_scb_block()
1177 * @adapter - pointer to our soft state
1178 * @raw_mbox - the mailbox
1180 * Issue a scb in synchronous and non-interrupt mode
1182 static int
1183 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1185 volatile mbox64_t *mbox64 = adapter->mbox64;
1186 volatile mbox_t *mbox = adapter->mbox;
1187 u8 byte;
1189 /* Wait until mailbox is free */
1190 if(mega_busywait_mbox (adapter))
1191 goto bug_blocked_mailbox;
1193 /* Copy mailbox data into host structure */
1194 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1195 mbox->m_out.cmdid = 0xFE;
1196 mbox->m_in.busy = 1;
1198 switch (raw_mbox[0]) {
1199 case MEGA_MBOXCMD_LREAD64:
1200 case MEGA_MBOXCMD_LWRITE64:
1201 case MEGA_MBOXCMD_PASSTHRU64:
1202 case MEGA_MBOXCMD_EXTPTHRU:
1203 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1204 mbox64->xfer_segment_hi = 0;
1205 mbox->m_out.xferaddr = 0xFFFFFFFF;
1206 break;
1207 default:
1208 mbox64->xfer_segment_lo = 0;
1209 mbox64->xfer_segment_hi = 0;
1212 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1213 mbox->m_in.poll = 0;
1214 mbox->m_in.ack = 0;
1215 mbox->m_in.numstatus = 0xFF;
1216 mbox->m_in.status = 0xFF;
1217 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1219 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1220 cpu_relax();
1222 mbox->m_in.numstatus = 0xFF;
1224 while( (volatile u8)mbox->m_in.poll != 0x77 )
1225 cpu_relax();
1227 mbox->m_in.poll = 0;
1228 mbox->m_in.ack = 0x77;
1230 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1232 while(RDINDOOR(adapter) & 0x2)
1233 cpu_relax();
1235 else {
1236 irq_disable(adapter);
1237 issue_command(adapter);
1239 while (!((byte = irq_state(adapter)) & INTR_VALID))
1240 cpu_relax();
1242 set_irq_state(adapter, byte);
1243 irq_enable(adapter);
1244 irq_ack(adapter);
1247 return mbox->m_in.status;
1249 bug_blocked_mailbox:
1250 printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1251 udelay (1000);
1252 return -1;
1257 * megaraid_isr_iomapped()
1258 * @irq - irq
1259 * @devp - pointer to our soft state
1261 * Interrupt service routine for io-mapped controllers.
1262 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1263 * and service the completed commands.
1265 static irqreturn_t
1266 megaraid_isr_iomapped(int irq, void *devp)
1268 adapter_t *adapter = devp;
1269 unsigned long flags;
1270 u8 status;
1271 u8 nstatus;
1272 u8 completed[MAX_FIRMWARE_STATUS];
1273 u8 byte;
1274 int handled = 0;
1278 * loop till F/W has more commands for us to complete.
1280 spin_lock_irqsave(&adapter->lock, flags);
1282 do {
1283 /* Check if a valid interrupt is pending */
1284 byte = irq_state(adapter);
1285 if( (byte & VALID_INTR_BYTE) == 0 ) {
1287 * No more pending commands
1289 goto out_unlock;
1291 set_irq_state(adapter, byte);
1293 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1294 == 0xFF)
1295 cpu_relax();
1296 adapter->mbox->m_in.numstatus = 0xFF;
1298 status = adapter->mbox->m_in.status;
1301 * decrement the pending queue counter
1303 atomic_sub(nstatus, &adapter->pend_cmds);
1305 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1306 nstatus);
1308 /* Acknowledge interrupt */
1309 irq_ack(adapter);
1311 mega_cmd_done(adapter, completed, nstatus, status);
1313 mega_rundoneq(adapter);
1315 handled = 1;
1317 /* Loop through any pending requests */
1318 if(atomic_read(&adapter->quiescent) == 0) {
1319 mega_runpendq(adapter);
1322 } while(1);
1324 out_unlock:
1326 spin_unlock_irqrestore(&adapter->lock, flags);
1328 return IRQ_RETVAL(handled);
1333 * megaraid_isr_memmapped()
1334 * @irq - irq
1335 * @devp - pointer to our soft state
1337 * Interrupt service routine for memory-mapped controllers.
1338 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1339 * and service the completed commands.
1341 static irqreturn_t
1342 megaraid_isr_memmapped(int irq, void *devp)
1344 adapter_t *adapter = devp;
1345 unsigned long flags;
1346 u8 status;
1347 u32 dword = 0;
1348 u8 nstatus;
1349 u8 completed[MAX_FIRMWARE_STATUS];
1350 int handled = 0;
1354 * loop till F/W has more commands for us to complete.
1356 spin_lock_irqsave(&adapter->lock, flags);
1358 do {
1359 /* Check if a valid interrupt is pending */
1360 dword = RDOUTDOOR(adapter);
1361 if(dword != 0x10001234) {
1363 * No more pending commands
1365 goto out_unlock;
1367 WROUTDOOR(adapter, 0x10001234);
1369 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1370 == 0xFF) {
1371 cpu_relax();
1373 adapter->mbox->m_in.numstatus = 0xFF;
1375 status = adapter->mbox->m_in.status;
1378 * decrement the pending queue counter
1380 atomic_sub(nstatus, &adapter->pend_cmds);
1382 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1383 nstatus);
1385 /* Acknowledge interrupt */
1386 WRINDOOR(adapter, 0x2);
1388 handled = 1;
1390 while( RDINDOOR(adapter) & 0x02 )
1391 cpu_relax();
1393 mega_cmd_done(adapter, completed, nstatus, status);
1395 mega_rundoneq(adapter);
1397 /* Loop through any pending requests */
1398 if(atomic_read(&adapter->quiescent) == 0) {
1399 mega_runpendq(adapter);
1402 } while(1);
1404 out_unlock:
1406 spin_unlock_irqrestore(&adapter->lock, flags);
1408 return IRQ_RETVAL(handled);
1411 * mega_cmd_done()
1412 * @adapter - pointer to our soft state
1413 * @completed - array of ids of completed commands
1414 * @nstatus - number of completed commands
1415 * @status - status of the last command completed
1417 * Complete the commands and call the scsi mid-layer callback hooks.
1419 static void
1420 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1422 mega_ext_passthru *epthru = NULL;
1423 struct scatterlist *sgl;
1424 Scsi_Cmnd *cmd = NULL;
1425 mega_passthru *pthru = NULL;
1426 mbox_t *mbox = NULL;
1427 u8 c;
1428 scb_t *scb;
1429 int islogical;
1430 int cmdid;
1431 int i;
1434 * for all the commands completed, call the mid-layer callback routine
1435 * and free the scb.
1437 for( i = 0; i < nstatus; i++ ) {
1439 cmdid = completed[i];
1441 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1442 scb = &adapter->int_scb;
1443 cmd = scb->cmd;
1444 mbox = (mbox_t *)scb->raw_mbox;
1447 * Internal command interface do not fire the extended
1448 * passthru or 64-bit passthru
1450 pthru = scb->pthru;
1453 else {
1454 scb = &adapter->scb_list[cmdid];
1457 * Make sure f/w has completed a valid command
1459 if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1460 printk(KERN_CRIT
1461 "megaraid: invalid command ");
1462 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1463 cmdid, scb->state, scb->cmd);
1465 continue;
1469 * Was a abort issued for this command
1471 if( scb->state & SCB_ABORT ) {
1473 printk(KERN_WARNING
1474 "megaraid: aborted cmd [%x] complete.\n",
1475 scb->idx);
1477 scb->cmd->result = (DID_ABORT << 16);
1479 list_add_tail(SCSI_LIST(scb->cmd),
1480 &adapter->completed_list);
1482 mega_free_scb(adapter, scb);
1484 continue;
1488 * Was a reset issued for this command
1490 if( scb->state & SCB_RESET ) {
1492 printk(KERN_WARNING
1493 "megaraid: reset cmd [%x] complete.\n",
1494 scb->idx);
1496 scb->cmd->result = (DID_RESET << 16);
1498 list_add_tail(SCSI_LIST(scb->cmd),
1499 &adapter->completed_list);
1501 mega_free_scb (adapter, scb);
1503 continue;
1506 cmd = scb->cmd;
1507 pthru = scb->pthru;
1508 epthru = scb->epthru;
1509 mbox = (mbox_t *)scb->raw_mbox;
1511 #if MEGA_HAVE_STATS
1514 int logdrv = mbox->m_out.logdrv;
1516 islogical = adapter->logdrv_chan[cmd->channel];
1518 * Maintain an error counter for the logical drive.
1519 * Some application like SNMP agent need such
1520 * statistics
1522 if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1523 cmd->cmnd[0] == READ_10 ||
1524 cmd->cmnd[0] == READ_12)) {
1526 * Logical drive number increases by 0x80 when
1527 * a logical drive is deleted
1529 adapter->rd_errors[logdrv%0x80]++;
1532 if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1533 cmd->cmnd[0] == WRITE_10 ||
1534 cmd->cmnd[0] == WRITE_12)) {
1536 * Logical drive number increases by 0x80 when
1537 * a logical drive is deleted
1539 adapter->wr_errors[logdrv%0x80]++;
1543 #endif
1547 * Do not return the presence of hard disk on the channel so,
1548 * inquiry sent, and returned data==hard disk or removable
1549 * hard disk and not logical, request should return failure! -
1550 * PJ
1552 islogical = adapter->logdrv_chan[cmd->device->channel];
1553 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1555 sgl = scsi_sglist(cmd);
1556 if( sg_page(sgl) ) {
1557 c = *(unsigned char *) sg_virt(&sgl[0]);
1558 } else {
1559 printk(KERN_WARNING
1560 "megaraid: invalid sg.\n");
1561 c = 0;
1564 if(IS_RAID_CH(adapter, cmd->device->channel) &&
1565 ((c & 0x1F ) == TYPE_DISK)) {
1566 status = 0xF0;
1570 /* clear result; otherwise, success returns corrupt value */
1571 cmd->result = 0;
1573 /* Convert MegaRAID status to Linux error code */
1574 switch (status) {
1575 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1576 cmd->result |= (DID_OK << 16);
1577 break;
1579 case 0x02: /* ERROR_ABORTED, i.e.
1580 SCSI_STATUS_CHECK_CONDITION */
1582 /* set sense_buffer and result fields */
1583 if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1584 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1586 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1587 14);
1589 cmd->result = (DRIVER_SENSE << 24) |
1590 (DID_OK << 16) |
1591 (CHECK_CONDITION << 1);
1593 else {
1594 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1596 memcpy(cmd->sense_buffer,
1597 epthru->reqsensearea, 14);
1599 cmd->result = (DRIVER_SENSE << 24) |
1600 (DID_OK << 16) |
1601 (CHECK_CONDITION << 1);
1602 } else {
1603 cmd->sense_buffer[0] = 0x70;
1604 cmd->sense_buffer[2] = ABORTED_COMMAND;
1605 cmd->result |= (CHECK_CONDITION << 1);
1608 break;
1610 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1611 SCSI_STATUS_BUSY */
1612 cmd->result |= (DID_BUS_BUSY << 16) | status;
1613 break;
1615 default:
1616 #if MEGA_HAVE_CLUSTERING
1618 * If TEST_UNIT_READY fails, we know
1619 * MEGA_RESERVATION_STATUS failed
1621 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1622 cmd->result |= (DID_ERROR << 16) |
1623 (RESERVATION_CONFLICT << 1);
1625 else
1627 * Error code returned is 1 if Reserve or Release
1628 * failed or the input parameter is invalid
1630 if( status == 1 &&
1631 (cmd->cmnd[0] == RESERVE ||
1632 cmd->cmnd[0] == RELEASE) ) {
1634 cmd->result |= (DID_ERROR << 16) |
1635 (RESERVATION_CONFLICT << 1);
1637 else
1638 #endif
1639 cmd->result |= (DID_BAD_TARGET << 16)|status;
1643 * Only free SCBs for the commands coming down from the
1644 * mid-layer, not for which were issued internally
1646 * For internal command, restore the status returned by the
1647 * firmware so that user can interpret it.
1649 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1650 cmd->result = status;
1653 * Remove the internal command from the pending list
1655 list_del_init(&scb->list);
1656 scb->state = SCB_FREE;
1658 else {
1659 mega_free_scb(adapter, scb);
1662 /* Add Scsi_Command to end of completed queue */
1663 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1669 * mega_runpendq()
1671 * Run through the list of completed requests and finish it
1673 static void
1674 mega_rundoneq (adapter_t *adapter)
1676 Scsi_Cmnd *cmd;
1677 struct list_head *pos;
1679 list_for_each(pos, &adapter->completed_list) {
1681 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1683 cmd = list_entry(spos, Scsi_Cmnd, SCp);
1684 cmd->scsi_done(cmd);
1687 INIT_LIST_HEAD(&adapter->completed_list);
1692 * Free a SCB structure
1693 * Note: We assume the scsi commands associated with this scb is not free yet.
1695 static void
1696 mega_free_scb(adapter_t *adapter, scb_t *scb)
1698 switch( scb->dma_type ) {
1700 case MEGA_DMA_TYPE_NONE:
1701 break;
1703 case MEGA_SGLIST:
1704 scsi_dma_unmap(scb->cmd);
1705 break;
1706 default:
1707 break;
1711 * Remove from the pending list
1713 list_del_init(&scb->list);
1715 /* Link the scb back into free list */
1716 scb->state = SCB_FREE;
1717 scb->cmd = NULL;
1719 list_add(&scb->list, &adapter->free_list);
1723 static int
1724 __mega_busywait_mbox (adapter_t *adapter)
1726 volatile mbox_t *mbox = adapter->mbox;
1727 long counter;
1729 for (counter = 0; counter < 10000; counter++) {
1730 if (!mbox->m_in.busy)
1731 return 0;
1732 udelay(100);
1733 cond_resched();
1735 return -1; /* give up after 1 second */
1739 * Copies data to SGLIST
1740 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1742 static int
1743 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1745 struct scatterlist *sg;
1746 Scsi_Cmnd *cmd;
1747 int sgcnt;
1748 int idx;
1750 cmd = scb->cmd;
1753 * Copy Scatter-Gather list info into controller structure.
1755 * The number of sg elements returned must not exceed our limit
1757 sgcnt = scsi_dma_map(cmd);
1759 scb->dma_type = MEGA_SGLIST;
1761 BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1763 *len = 0;
1765 if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1766 sg = scsi_sglist(cmd);
1767 scb->dma_h_bulkdata = sg_dma_address(sg);
1768 *buf = (u32)scb->dma_h_bulkdata;
1769 *len = sg_dma_len(sg);
1770 return 0;
1773 scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1774 if (adapter->has_64bit_addr) {
1775 scb->sgl64[idx].address = sg_dma_address(sg);
1776 *len += scb->sgl64[idx].length = sg_dma_len(sg);
1777 } else {
1778 scb->sgl[idx].address = sg_dma_address(sg);
1779 *len += scb->sgl[idx].length = sg_dma_len(sg);
1783 /* Reset pointer and length fields */
1784 *buf = scb->sgl_dma_addr;
1786 /* Return count of SG requests */
1787 return sgcnt;
1792 * mega_8_to_40ld()
1794 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1795 * Enquiry3 structures for later use
1797 static void
1798 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1799 mega_product_info *product_info)
1801 int i;
1803 product_info->max_commands = inquiry->adapter_info.max_commands;
1804 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1805 product_info->nchannels = inquiry->adapter_info.nchannels;
1807 for (i = 0; i < 4; i++) {
1808 product_info->fw_version[i] =
1809 inquiry->adapter_info.fw_version[i];
1811 product_info->bios_version[i] =
1812 inquiry->adapter_info.bios_version[i];
1814 enquiry3->cache_flush_interval =
1815 inquiry->adapter_info.cache_flush_interval;
1817 product_info->dram_size = inquiry->adapter_info.dram_size;
1819 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1821 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1822 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1823 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1824 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1827 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1828 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1831 static inline void
1832 mega_free_sgl(adapter_t *adapter)
1834 scb_t *scb;
1835 int i;
1837 for(i = 0; i < adapter->max_cmds; i++) {
1839 scb = &adapter->scb_list[i];
1841 if( scb->sgl64 ) {
1842 pci_free_consistent(adapter->dev,
1843 sizeof(mega_sgl64) * adapter->sglen,
1844 scb->sgl64,
1845 scb->sgl_dma_addr);
1847 scb->sgl64 = NULL;
1850 if( scb->pthru ) {
1851 pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1852 scb->pthru, scb->pthru_dma_addr);
1854 scb->pthru = NULL;
1857 if( scb->epthru ) {
1858 pci_free_consistent(adapter->dev,
1859 sizeof(mega_ext_passthru),
1860 scb->epthru, scb->epthru_dma_addr);
1862 scb->epthru = NULL;
1870 * Get information about the card/driver
1872 const char *
1873 megaraid_info(struct Scsi_Host *host)
1875 static char buffer[512];
1876 adapter_t *adapter;
1878 adapter = (adapter_t *)host->hostdata;
1880 sprintf (buffer,
1881 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1882 adapter->fw_version, adapter->product_info.max_commands,
1883 adapter->host->max_id, adapter->host->max_channel,
1884 adapter->host->max_lun);
1885 return buffer;
1889 * Abort a previous SCSI request. Only commands on the pending list can be
1890 * aborted. All the commands issued to the F/W must complete.
1892 static int
1893 megaraid_abort(Scsi_Cmnd *cmd)
1895 adapter_t *adapter;
1896 int rval;
1898 adapter = (adapter_t *)cmd->device->host->hostdata;
1900 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1903 * This is required here to complete any completed requests
1904 * to be communicated over to the mid layer.
1906 mega_rundoneq(adapter);
1908 return rval;
1912 static int
1913 megaraid_reset(struct scsi_cmnd *cmd)
1915 adapter_t *adapter;
1916 megacmd_t mc;
1917 int rval;
1919 adapter = (adapter_t *)cmd->device->host->hostdata;
1921 #if MEGA_HAVE_CLUSTERING
1922 mc.cmd = MEGA_CLUSTER_CMD;
1923 mc.opcode = MEGA_RESET_RESERVATIONS;
1925 if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1926 printk(KERN_WARNING
1927 "megaraid: reservation reset failed.\n");
1929 else {
1930 printk(KERN_INFO "megaraid: reservation reset.\n");
1932 #endif
1934 spin_lock_irq(&adapter->lock);
1936 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1939 * This is required here to complete any completed requests
1940 * to be communicated over to the mid layer.
1942 mega_rundoneq(adapter);
1943 spin_unlock_irq(&adapter->lock);
1945 return rval;
1949 * megaraid_abort_and_reset()
1950 * @adapter - megaraid soft state
1951 * @cmd - scsi command to be aborted or reset
1952 * @aor - abort or reset flag
1954 * Try to locate the scsi command in the pending queue. If found and is not
1955 * issued to the controller, abort/reset it. Otherwise return failure
1957 static int
1958 megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
1960 struct list_head *pos, *next;
1961 scb_t *scb;
1963 printk(KERN_WARNING "megaraid: %s cmd=%x <c=%d t=%d l=%d>\n",
1964 (aor == SCB_ABORT)? "ABORTING":"RESET",
1965 cmd->cmnd[0], cmd->device->channel,
1966 cmd->device->id, cmd->device->lun);
1968 if(list_empty(&adapter->pending_list))
1969 return FALSE;
1971 list_for_each_safe(pos, next, &adapter->pending_list) {
1973 scb = list_entry(pos, scb_t, list);
1975 if (scb->cmd == cmd) { /* Found command */
1977 scb->state |= aor;
1980 * Check if this command has firmware ownership. If
1981 * yes, we cannot reset this command. Whenever f/w
1982 * completes this command, we will return appropriate
1983 * status from ISR.
1985 if( scb->state & SCB_ISSUED ) {
1987 printk(KERN_WARNING
1988 "megaraid: %s[%x], fw owner.\n",
1989 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1990 scb->idx);
1992 return FALSE;
1994 else {
1997 * Not yet issued! Remove from the pending
1998 * list
2000 printk(KERN_WARNING
2001 "megaraid: %s-[%x], driver owner.\n",
2002 (aor==SCB_ABORT) ? "ABORTING":"RESET",
2003 scb->idx);
2005 mega_free_scb(adapter, scb);
2007 if( aor == SCB_ABORT ) {
2008 cmd->result = (DID_ABORT << 16);
2010 else {
2011 cmd->result = (DID_RESET << 16);
2014 list_add_tail(SCSI_LIST(cmd),
2015 &adapter->completed_list);
2017 return TRUE;
2022 return FALSE;
2025 static inline int
2026 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2028 *pdev = alloc_pci_dev();
2030 if( *pdev == NULL ) return -1;
2032 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2034 if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
2035 kfree(*pdev);
2036 return -1;
2039 return 0;
2042 static inline void
2043 free_local_pdev(struct pci_dev *pdev)
2045 kfree(pdev);
2049 * mega_allocate_inquiry()
2050 * @dma_handle - handle returned for dma address
2051 * @pdev - handle to pci device
2053 * allocates memory for inquiry structure
2055 static inline void *
2056 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2058 return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2062 static inline void
2063 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2065 pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2069 #ifdef CONFIG_PROC_FS
2070 /* Following code handles /proc fs */
2072 #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
2073 S_IRUSR | S_IFREG, \
2074 controller_proc_dir_entry, \
2075 func, adapter)
2078 * mega_create_proc_entry()
2079 * @index - index in soft state array
2080 * @parent - parent node for this /proc entry
2082 * Creates /proc entries for our controllers.
2084 static void
2085 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2087 struct proc_dir_entry *controller_proc_dir_entry = NULL;
2088 u8 string[64] = { 0 };
2089 adapter_t *adapter = hba_soft_state[index];
2091 sprintf(string, "hba%d", adapter->host->host_no);
2093 controller_proc_dir_entry =
2094 adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
2096 if(!controller_proc_dir_entry) {
2097 printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2098 return;
2100 adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
2101 adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
2102 adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
2103 #if MEGA_HAVE_ENH_PROC
2104 adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
2105 adapter->proc_battery = CREATE_READ_PROC("battery-status",
2106 proc_battery);
2109 * Display each physical drive on its channel
2111 adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
2112 proc_pdrv_ch0);
2113 adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
2114 proc_pdrv_ch1);
2115 adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
2116 proc_pdrv_ch2);
2117 adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
2118 proc_pdrv_ch3);
2121 * Display a set of up to 10 logical drive through each of following
2122 * /proc entries
2124 adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
2125 proc_rdrv_10);
2126 adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
2127 proc_rdrv_20);
2128 adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
2129 proc_rdrv_30);
2130 adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
2131 proc_rdrv_40);
2132 #endif
2137 * proc_read_config()
2138 * @page - buffer to write the data in
2139 * @start - where the actual data has been written in page
2140 * @offset - same meaning as the read system call
2141 * @count - same meaning as the read system call
2142 * @eof - set if no more data needs to be returned
2143 * @data - pointer to our soft state
2145 * Display configuration information about the controller.
2147 static int
2148 proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
2149 void *data)
2152 adapter_t *adapter = (adapter_t *)data;
2153 int len = 0;
2155 len += sprintf(page+len, "%s", MEGARAID_VERSION);
2157 if(adapter->product_info.product_name[0])
2158 len += sprintf(page+len, "%s\n",
2159 adapter->product_info.product_name);
2161 len += sprintf(page+len, "Controller Type: ");
2163 if( adapter->flag & BOARD_MEMMAP ) {
2164 len += sprintf(page+len,
2165 "438/466/467/471/493/518/520/531/532\n");
2167 else {
2168 len += sprintf(page+len,
2169 "418/428/434\n");
2172 if(adapter->flag & BOARD_40LD) {
2173 len += sprintf(page+len,
2174 "Controller Supports 40 Logical Drives\n");
2177 if(adapter->flag & BOARD_64BIT) {
2178 len += sprintf(page+len,
2179 "Controller capable of 64-bit memory addressing\n");
2181 if( adapter->has_64bit_addr ) {
2182 len += sprintf(page+len,
2183 "Controller using 64-bit memory addressing\n");
2185 else {
2186 len += sprintf(page+len,
2187 "Controller is not using 64-bit memory addressing\n");
2190 len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
2191 adapter->host->irq);
2193 len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
2194 adapter->numldrv, adapter->product_info.nchannels);
2196 len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
2197 adapter->fw_version, adapter->bios_version,
2198 adapter->product_info.dram_size);
2200 len += sprintf(page+len,
2201 "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2202 adapter->product_info.max_commands, adapter->max_cmds);
2204 len += sprintf(page+len, "support_ext_cdb = %d\n",
2205 adapter->support_ext_cdb);
2206 len += sprintf(page+len, "support_random_del = %d\n",
2207 adapter->support_random_del);
2208 len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
2209 adapter->boot_ldrv_enabled);
2210 len += sprintf(page+len, "boot_ldrv = %d\n",
2211 adapter->boot_ldrv);
2212 len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
2213 adapter->boot_pdrv_enabled);
2214 len += sprintf(page+len, "boot_pdrv_ch = %d\n",
2215 adapter->boot_pdrv_ch);
2216 len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
2217 adapter->boot_pdrv_tgt);
2218 len += sprintf(page+len, "quiescent = %d\n",
2219 atomic_read(&adapter->quiescent));
2220 len += sprintf(page+len, "has_cluster = %d\n",
2221 adapter->has_cluster);
2223 len += sprintf(page+len, "\nModule Parameters:\n");
2224 len += sprintf(page+len, "max_cmd_per_lun = %d\n",
2225 max_cmd_per_lun);
2226 len += sprintf(page+len, "max_sectors_per_io = %d\n",
2227 max_sectors_per_io);
2229 *eof = 1;
2231 return len;
2237 * proc_read_stat()
2238 * @page - buffer to write the data in
2239 * @start - where the actual data has been written in page
2240 * @offset - same meaning as the read system call
2241 * @count - same meaning as the read system call
2242 * @eof - set if no more data needs to be returned
2243 * @data - pointer to our soft state
2245 * Diaplay statistical information about the I/O activity.
2247 static int
2248 proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
2249 void *data)
2251 adapter_t *adapter;
2252 int len;
2253 int i;
2255 i = 0; /* avoid compilation warnings */
2256 len = 0;
2257 adapter = (adapter_t *)data;
2259 len = sprintf(page, "Statistical Information for this controller\n");
2260 len += sprintf(page+len, "pend_cmds = %d\n",
2261 atomic_read(&adapter->pend_cmds));
2262 #if MEGA_HAVE_STATS
2263 for(i = 0; i < adapter->numldrv; i++) {
2264 len += sprintf(page+len, "Logical Drive %d:\n", i);
2266 len += sprintf(page+len,
2267 "\tReads Issued = %lu, Writes Issued = %lu\n",
2268 adapter->nreads[i], adapter->nwrites[i]);
2270 len += sprintf(page+len,
2271 "\tSectors Read = %lu, Sectors Written = %lu\n",
2272 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2274 len += sprintf(page+len,
2275 "\tRead errors = %lu, Write errors = %lu\n\n",
2276 adapter->rd_errors[i], adapter->wr_errors[i]);
2278 #else
2279 len += sprintf(page+len,
2280 "IO and error counters not compiled in driver.\n");
2281 #endif
2283 *eof = 1;
2285 return len;
2290 * proc_read_mbox()
2291 * @page - buffer to write the data in
2292 * @start - where the actual data has been written in page
2293 * @offset - same meaning as the read system call
2294 * @count - same meaning as the read system call
2295 * @eof - set if no more data needs to be returned
2296 * @data - pointer to our soft state
2298 * Display mailbox information for the last command issued. This information
2299 * is good for debugging.
2301 static int
2302 proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
2303 void *data)
2306 adapter_t *adapter = (adapter_t *)data;
2307 volatile mbox_t *mbox = adapter->mbox;
2308 int len = 0;
2310 len = sprintf(page, "Contents of Mail Box Structure\n");
2311 len += sprintf(page+len, " Fw Command = 0x%02x\n",
2312 mbox->m_out.cmd);
2313 len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
2314 mbox->m_out.cmdid);
2315 len += sprintf(page+len, " No of Sectors= %04d\n",
2316 mbox->m_out.numsectors);
2317 len += sprintf(page+len, " LBA = 0x%02x\n",
2318 mbox->m_out.lba);
2319 len += sprintf(page+len, " DTA = 0x%08x\n",
2320 mbox->m_out.xferaddr);
2321 len += sprintf(page+len, " Logical Drive= 0x%02x\n",
2322 mbox->m_out.logdrv);
2323 len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
2324 mbox->m_out.numsgelements);
2325 len += sprintf(page+len, " Busy = %01x\n",
2326 mbox->m_in.busy);
2327 len += sprintf(page+len, " Status = 0x%02x\n",
2328 mbox->m_in.status);
2330 *eof = 1;
2332 return len;
2337 * proc_rebuild_rate()
2338 * @page - buffer to write the data in
2339 * @start - where the actual data has been written in page
2340 * @offset - same meaning as the read system call
2341 * @count - same meaning as the read system call
2342 * @eof - set if no more data needs to be returned
2343 * @data - pointer to our soft state
2345 * Display current rebuild rate
2347 static int
2348 proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
2349 void *data)
2351 adapter_t *adapter = (adapter_t *)data;
2352 dma_addr_t dma_handle;
2353 caddr_t inquiry;
2354 struct pci_dev *pdev;
2355 int len = 0;
2357 if( make_local_pdev(adapter, &pdev) != 0 ) {
2358 *eof = 1;
2359 return len;
2362 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2363 free_local_pdev(pdev);
2364 *eof = 1;
2365 return len;
2368 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2370 len = sprintf(page, "Adapter inquiry failed.\n");
2372 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2374 mega_free_inquiry(inquiry, dma_handle, pdev);
2376 free_local_pdev(pdev);
2378 *eof = 1;
2380 return len;
2383 if( adapter->flag & BOARD_40LD ) {
2384 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2385 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2387 else {
2388 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2389 ((mraid_ext_inquiry *)
2390 inquiry)->raid_inq.adapter_info.rebuild_rate);
2394 mega_free_inquiry(inquiry, dma_handle, pdev);
2396 free_local_pdev(pdev);
2398 *eof = 1;
2400 return len;
2405 * proc_battery()
2406 * @page - buffer to write the data in
2407 * @start - where the actual data has been written in page
2408 * @offset - same meaning as the read system call
2409 * @count - same meaning as the read system call
2410 * @eof - set if no more data needs to be returned
2411 * @data - pointer to our soft state
2413 * Display information about the battery module on the controller.
2415 static int
2416 proc_battery(char *page, char **start, off_t offset, int count, int *eof,
2417 void *data)
2419 adapter_t *adapter = (adapter_t *)data;
2420 dma_addr_t dma_handle;
2421 caddr_t inquiry;
2422 struct pci_dev *pdev;
2423 u8 battery_status = 0;
2424 char str[256];
2425 int len = 0;
2427 if( make_local_pdev(adapter, &pdev) != 0 ) {
2428 *eof = 1;
2429 return len;
2432 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2433 free_local_pdev(pdev);
2434 *eof = 1;
2435 return len;
2438 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2440 len = sprintf(page, "Adapter inquiry failed.\n");
2442 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2444 mega_free_inquiry(inquiry, dma_handle, pdev);
2446 free_local_pdev(pdev);
2448 *eof = 1;
2450 return len;
2453 if( adapter->flag & BOARD_40LD ) {
2454 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2456 else {
2457 battery_status = ((mraid_ext_inquiry *)inquiry)->
2458 raid_inq.adapter_info.battery_status;
2462 * Decode the battery status
2464 sprintf(str, "Battery Status:[%d]", battery_status);
2466 if(battery_status == MEGA_BATT_CHARGE_DONE)
2467 strcat(str, " Charge Done");
2469 if(battery_status & MEGA_BATT_MODULE_MISSING)
2470 strcat(str, " Module Missing");
2472 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2473 strcat(str, " Low Voltage");
2475 if(battery_status & MEGA_BATT_TEMP_HIGH)
2476 strcat(str, " Temperature High");
2478 if(battery_status & MEGA_BATT_PACK_MISSING)
2479 strcat(str, " Pack Missing");
2481 if(battery_status & MEGA_BATT_CHARGE_INPROG)
2482 strcat(str, " Charge In-progress");
2484 if(battery_status & MEGA_BATT_CHARGE_FAIL)
2485 strcat(str, " Charge Fail");
2487 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2488 strcat(str, " Cycles Exceeded");
2490 len = sprintf(page, "%s\n", str);
2493 mega_free_inquiry(inquiry, dma_handle, pdev);
2495 free_local_pdev(pdev);
2497 *eof = 1;
2499 return len;
2504 * proc_pdrv_ch0()
2505 * @page - buffer to write the data in
2506 * @start - where the actual data has been written in page
2507 * @offset - same meaning as the read system call
2508 * @count - same meaning as the read system call
2509 * @eof - set if no more data needs to be returned
2510 * @data - pointer to our soft state
2512 * Display information about the physical drives on physical channel 0.
2514 static int
2515 proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
2516 void *data)
2518 adapter_t *adapter = (adapter_t *)data;
2520 *eof = 1;
2522 return (proc_pdrv(adapter, page, 0));
2527 * proc_pdrv_ch1()
2528 * @page - buffer to write the data in
2529 * @start - where the actual data has been written in page
2530 * @offset - same meaning as the read system call
2531 * @count - same meaning as the read system call
2532 * @eof - set if no more data needs to be returned
2533 * @data - pointer to our soft state
2535 * Display information about the physical drives on physical channel 1.
2537 static int
2538 proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
2539 void *data)
2541 adapter_t *adapter = (adapter_t *)data;
2543 *eof = 1;
2545 return (proc_pdrv(adapter, page, 1));
2550 * proc_pdrv_ch2()
2551 * @page - buffer to write the data in
2552 * @start - where the actual data has been written in page
2553 * @offset - same meaning as the read system call
2554 * @count - same meaning as the read system call
2555 * @eof - set if no more data needs to be returned
2556 * @data - pointer to our soft state
2558 * Display information about the physical drives on physical channel 2.
2560 static int
2561 proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
2562 void *data)
2564 adapter_t *adapter = (adapter_t *)data;
2566 *eof = 1;
2568 return (proc_pdrv(adapter, page, 2));
2573 * proc_pdrv_ch3()
2574 * @page - buffer to write the data in
2575 * @start - where the actual data has been written in page
2576 * @offset - same meaning as the read system call
2577 * @count - same meaning as the read system call
2578 * @eof - set if no more data needs to be returned
2579 * @data - pointer to our soft state
2581 * Display information about the physical drives on physical channel 3.
2583 static int
2584 proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
2585 void *data)
2587 adapter_t *adapter = (adapter_t *)data;
2589 *eof = 1;
2591 return (proc_pdrv(adapter, page, 3));
2596 * proc_pdrv()
2597 * @page - buffer to write the data in
2598 * @adapter - pointer to our soft state
2600 * Display information about the physical drives.
2602 static int
2603 proc_pdrv(adapter_t *adapter, char *page, int channel)
2605 dma_addr_t dma_handle;
2606 char *scsi_inq;
2607 dma_addr_t scsi_inq_dma_handle;
2608 caddr_t inquiry;
2609 struct pci_dev *pdev;
2610 u8 *pdrv_state;
2611 u8 state;
2612 int tgt;
2613 int max_channels;
2614 int len = 0;
2615 char str[80];
2616 int i;
2618 if( make_local_pdev(adapter, &pdev) != 0 ) {
2619 return len;
2622 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2623 goto free_pdev;
2626 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2627 len = sprintf(page, "Adapter inquiry failed.\n");
2629 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2631 goto free_inquiry;
2635 scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2637 if( scsi_inq == NULL ) {
2638 len = sprintf(page, "memory not available for scsi inq.\n");
2640 goto free_inquiry;
2643 if( adapter->flag & BOARD_40LD ) {
2644 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2646 else {
2647 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2648 raid_inq.pdrv_info.pdrv_state;
2651 max_channels = adapter->product_info.nchannels;
2653 if( channel >= max_channels ) {
2654 goto free_pci;
2657 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2659 i = channel*16 + tgt;
2661 state = *(pdrv_state + i);
2663 switch( state & 0x0F ) {
2665 case PDRV_ONLINE:
2666 sprintf(str,
2667 "Channel:%2d Id:%2d State: Online",
2668 channel, tgt);
2669 break;
2671 case PDRV_FAILED:
2672 sprintf(str,
2673 "Channel:%2d Id:%2d State: Failed",
2674 channel, tgt);
2675 break;
2677 case PDRV_RBLD:
2678 sprintf(str,
2679 "Channel:%2d Id:%2d State: Rebuild",
2680 channel, tgt);
2681 break;
2683 case PDRV_HOTSPARE:
2684 sprintf(str,
2685 "Channel:%2d Id:%2d State: Hot spare",
2686 channel, tgt);
2687 break;
2689 default:
2690 sprintf(str,
2691 "Channel:%2d Id:%2d State: Un-configured",
2692 channel, tgt);
2693 break;
2698 * This interface displays inquiries for disk drives
2699 * only. Inquries for logical drives and non-disk
2700 * devices are available through /proc/scsi/scsi
2702 memset(scsi_inq, 0, 256);
2703 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2704 scsi_inq_dma_handle) ||
2705 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2706 continue;
2710 * Check for overflow. We print less than 240
2711 * characters for inquiry
2713 if( (len + 240) >= PAGE_SIZE ) break;
2715 len += sprintf(page+len, "%s.\n", str);
2717 len += mega_print_inquiry(page+len, scsi_inq);
2720 free_pci:
2721 pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2722 free_inquiry:
2723 mega_free_inquiry(inquiry, dma_handle, pdev);
2724 free_pdev:
2725 free_local_pdev(pdev);
2727 return len;
2732 * Display scsi inquiry
2734 static int
2735 mega_print_inquiry(char *page, char *scsi_inq)
2737 int len = 0;
2738 int i;
2740 len = sprintf(page, " Vendor: ");
2741 for( i = 8; i < 16; i++ ) {
2742 len += sprintf(page+len, "%c", scsi_inq[i]);
2745 len += sprintf(page+len, " Model: ");
2747 for( i = 16; i < 32; i++ ) {
2748 len += sprintf(page+len, "%c", scsi_inq[i]);
2751 len += sprintf(page+len, " Rev: ");
2753 for( i = 32; i < 36; i++ ) {
2754 len += sprintf(page+len, "%c", scsi_inq[i]);
2757 len += sprintf(page+len, "\n");
2759 i = scsi_inq[0] & 0x1f;
2761 len += sprintf(page+len, " Type: %s ", scsi_device_type(i));
2763 len += sprintf(page+len,
2764 " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
2766 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2767 len += sprintf(page+len, " CCS\n");
2768 else
2769 len += sprintf(page+len, "\n");
2771 return len;
2776 * proc_rdrv_10()
2777 * @page - buffer to write the data in
2778 * @start - where the actual data has been written in page
2779 * @offset - same meaning as the read system call
2780 * @count - same meaning as the read system call
2781 * @eof - set if no more data needs to be returned
2782 * @data - pointer to our soft state
2784 * Display real time information about the logical drives 0 through 9.
2786 static int
2787 proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
2788 void *data)
2790 adapter_t *adapter = (adapter_t *)data;
2792 *eof = 1;
2794 return (proc_rdrv(adapter, page, 0, 9));
2799 * proc_rdrv_20()
2800 * @page - buffer to write the data in
2801 * @start - where the actual data has been written in page
2802 * @offset - same meaning as the read system call
2803 * @count - same meaning as the read system call
2804 * @eof - set if no more data needs to be returned
2805 * @data - pointer to our soft state
2807 * Display real time information about the logical drives 0 through 9.
2809 static int
2810 proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
2811 void *data)
2813 adapter_t *adapter = (adapter_t *)data;
2815 *eof = 1;
2817 return (proc_rdrv(adapter, page, 10, 19));
2822 * proc_rdrv_30()
2823 * @page - buffer to write the data in
2824 * @start - where the actual data has been written in page
2825 * @offset - same meaning as the read system call
2826 * @count - same meaning as the read system call
2827 * @eof - set if no more data needs to be returned
2828 * @data - pointer to our soft state
2830 * Display real time information about the logical drives 0 through 9.
2832 static int
2833 proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
2834 void *data)
2836 adapter_t *adapter = (adapter_t *)data;
2838 *eof = 1;
2840 return (proc_rdrv(adapter, page, 20, 29));
2845 * proc_rdrv_40()
2846 * @page - buffer to write the data in
2847 * @start - where the actual data has been written in page
2848 * @offset - same meaning as the read system call
2849 * @count - same meaning as the read system call
2850 * @eof - set if no more data needs to be returned
2851 * @data - pointer to our soft state
2853 * Display real time information about the logical drives 0 through 9.
2855 static int
2856 proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
2857 void *data)
2859 adapter_t *adapter = (adapter_t *)data;
2861 *eof = 1;
2863 return (proc_rdrv(adapter, page, 30, 39));
2868 * proc_rdrv()
2869 * @page - buffer to write the data in
2870 * @adapter - pointer to our soft state
2871 * @start - starting logical drive to display
2872 * @end - ending logical drive to display
2874 * We do not print the inquiry information since its already available through
2875 * /proc/scsi/scsi interface
2877 static int
2878 proc_rdrv(adapter_t *adapter, char *page, int start, int end )
2880 dma_addr_t dma_handle;
2881 logdrv_param *lparam;
2882 megacmd_t mc;
2883 char *disk_array;
2884 dma_addr_t disk_array_dma_handle;
2885 caddr_t inquiry;
2886 struct pci_dev *pdev;
2887 u8 *rdrv_state;
2888 int num_ldrv;
2889 u32 array_sz;
2890 int len = 0;
2891 int i;
2893 if( make_local_pdev(adapter, &pdev) != 0 ) {
2894 return len;
2897 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2898 free_local_pdev(pdev);
2899 return len;
2902 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2904 len = sprintf(page, "Adapter inquiry failed.\n");
2906 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2908 mega_free_inquiry(inquiry, dma_handle, pdev);
2910 free_local_pdev(pdev);
2912 return len;
2915 memset(&mc, 0, sizeof(megacmd_t));
2917 if( adapter->flag & BOARD_40LD ) {
2918 array_sz = sizeof(disk_array_40ld);
2920 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2922 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2924 else {
2925 array_sz = sizeof(disk_array_8ld);
2927 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2928 raid_inq.logdrv_info.ldrv_state;
2930 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2931 raid_inq.logdrv_info.num_ldrv;
2934 disk_array = pci_alloc_consistent(pdev, array_sz,
2935 &disk_array_dma_handle);
2937 if( disk_array == NULL ) {
2938 len = sprintf(page, "memory not available.\n");
2940 mega_free_inquiry(inquiry, dma_handle, pdev);
2942 free_local_pdev(pdev);
2944 return len;
2947 mc.xferaddr = (u32)disk_array_dma_handle;
2949 if( adapter->flag & BOARD_40LD ) {
2950 mc.cmd = FC_NEW_CONFIG;
2951 mc.opcode = OP_DCMD_READ_CONFIG;
2953 if( mega_internal_command(adapter, &mc, NULL) ) {
2955 len = sprintf(page, "40LD read config failed.\n");
2957 mega_free_inquiry(inquiry, dma_handle, pdev);
2959 pci_free_consistent(pdev, array_sz, disk_array,
2960 disk_array_dma_handle);
2962 free_local_pdev(pdev);
2964 return len;
2968 else {
2969 mc.cmd = NEW_READ_CONFIG_8LD;
2971 if( mega_internal_command(adapter, &mc, NULL) ) {
2973 mc.cmd = READ_CONFIG_8LD;
2975 if( mega_internal_command(adapter, &mc,
2976 NULL) ){
2978 len = sprintf(page,
2979 "8LD read config failed.\n");
2981 mega_free_inquiry(inquiry, dma_handle, pdev);
2983 pci_free_consistent(pdev, array_sz,
2984 disk_array,
2985 disk_array_dma_handle);
2987 free_local_pdev(pdev);
2989 return len;
2994 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2996 if( adapter->flag & BOARD_40LD ) {
2997 lparam =
2998 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
3000 else {
3001 lparam =
3002 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
3006 * Check for overflow. We print less than 240 characters for
3007 * information about each logical drive.
3009 if( (len + 240) >= PAGE_SIZE ) break;
3011 len += sprintf(page+len, "Logical drive:%2d:, ", i);
3013 switch( rdrv_state[i] & 0x0F ) {
3014 case RDRV_OFFLINE:
3015 len += sprintf(page+len, "state: offline");
3016 break;
3018 case RDRV_DEGRADED:
3019 len += sprintf(page+len, "state: degraded");
3020 break;
3022 case RDRV_OPTIMAL:
3023 len += sprintf(page+len, "state: optimal");
3024 break;
3026 case RDRV_DELETED:
3027 len += sprintf(page+len, "state: deleted");
3028 break;
3030 default:
3031 len += sprintf(page+len, "state: unknown");
3032 break;
3036 * Check if check consistency or initialization is going on
3037 * for this logical drive.
3039 if( (rdrv_state[i] & 0xF0) == 0x20 ) {
3040 len += sprintf(page+len,
3041 ", check-consistency in progress");
3043 else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
3044 len += sprintf(page+len,
3045 ", initialization in progress");
3048 len += sprintf(page+len, "\n");
3050 len += sprintf(page+len, "Span depth:%3d, ",
3051 lparam->span_depth);
3053 len += sprintf(page+len, "RAID level:%3d, ",
3054 lparam->level);
3056 len += sprintf(page+len, "Stripe size:%3d, ",
3057 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
3059 len += sprintf(page+len, "Row size:%3d\n",
3060 lparam->row_size);
3063 len += sprintf(page+len, "Read Policy: ");
3065 switch(lparam->read_ahead) {
3067 case NO_READ_AHEAD:
3068 len += sprintf(page+len, "No read ahead, ");
3069 break;
3071 case READ_AHEAD:
3072 len += sprintf(page+len, "Read ahead, ");
3073 break;
3075 case ADAP_READ_AHEAD:
3076 len += sprintf(page+len, "Adaptive, ");
3077 break;
3081 len += sprintf(page+len, "Write Policy: ");
3083 switch(lparam->write_mode) {
3085 case WRMODE_WRITE_THRU:
3086 len += sprintf(page+len, "Write thru, ");
3087 break;
3089 case WRMODE_WRITE_BACK:
3090 len += sprintf(page+len, "Write back, ");
3091 break;
3094 len += sprintf(page+len, "Cache Policy: ");
3096 switch(lparam->direct_io) {
3098 case CACHED_IO:
3099 len += sprintf(page+len, "Cached IO\n\n");
3100 break;
3102 case DIRECT_IO:
3103 len += sprintf(page+len, "Direct IO\n\n");
3104 break;
3108 mega_free_inquiry(inquiry, dma_handle, pdev);
3110 pci_free_consistent(pdev, array_sz, disk_array,
3111 disk_array_dma_handle);
3113 free_local_pdev(pdev);
3115 return len;
3117 #else
3118 static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
3121 #endif
3125 * megaraid_biosparam()
3127 * Return the disk geometry for a particular disk
3129 static int
3130 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
3131 sector_t capacity, int geom[])
3133 adapter_t *adapter;
3134 unsigned char *bh;
3135 int heads;
3136 int sectors;
3137 int cylinders;
3138 int rval;
3140 /* Get pointer to host config structure */
3141 adapter = (adapter_t *)sdev->host->hostdata;
3143 if (IS_RAID_CH(adapter, sdev->channel)) {
3144 /* Default heads (64) & sectors (32) */
3145 heads = 64;
3146 sectors = 32;
3147 cylinders = (ulong)capacity / (heads * sectors);
3150 * Handle extended translation size for logical drives
3151 * > 1Gb
3153 if ((ulong)capacity >= 0x200000) {
3154 heads = 255;
3155 sectors = 63;
3156 cylinders = (ulong)capacity / (heads * sectors);
3159 /* return result */
3160 geom[0] = heads;
3161 geom[1] = sectors;
3162 geom[2] = cylinders;
3164 else {
3165 bh = scsi_bios_ptable(bdev);
3167 if( bh ) {
3168 rval = scsi_partsize(bh, capacity,
3169 &geom[2], &geom[0], &geom[1]);
3170 kfree(bh);
3171 if( rval != -1 )
3172 return rval;
3175 printk(KERN_INFO
3176 "megaraid: invalid partition on this disk on channel %d\n",
3177 sdev->channel);
3179 /* Default heads (64) & sectors (32) */
3180 heads = 64;
3181 sectors = 32;
3182 cylinders = (ulong)capacity / (heads * sectors);
3184 /* Handle extended translation size for logical drives > 1Gb */
3185 if ((ulong)capacity >= 0x200000) {
3186 heads = 255;
3187 sectors = 63;
3188 cylinders = (ulong)capacity / (heads * sectors);
3191 /* return result */
3192 geom[0] = heads;
3193 geom[1] = sectors;
3194 geom[2] = cylinders;
3197 return 0;
3201 * mega_init_scb()
3202 * @adapter - pointer to our soft state
3204 * Allocate memory for the various pointers in the scb structures:
3205 * scatter-gather list pointer, passthru and extended passthru structure
3206 * pointers.
3208 static int
3209 mega_init_scb(adapter_t *adapter)
3211 scb_t *scb;
3212 int i;
3214 for( i = 0; i < adapter->max_cmds; i++ ) {
3216 scb = &adapter->scb_list[i];
3218 scb->sgl64 = NULL;
3219 scb->sgl = NULL;
3220 scb->pthru = NULL;
3221 scb->epthru = NULL;
3224 for( i = 0; i < adapter->max_cmds; i++ ) {
3226 scb = &adapter->scb_list[i];
3228 scb->idx = i;
3230 scb->sgl64 = pci_alloc_consistent(adapter->dev,
3231 sizeof(mega_sgl64) * adapter->sglen,
3232 &scb->sgl_dma_addr);
3234 scb->sgl = (mega_sglist *)scb->sgl64;
3236 if( !scb->sgl ) {
3237 printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
3238 mega_free_sgl(adapter);
3239 return -1;
3242 scb->pthru = pci_alloc_consistent(adapter->dev,
3243 sizeof(mega_passthru),
3244 &scb->pthru_dma_addr);
3246 if( !scb->pthru ) {
3247 printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
3248 mega_free_sgl(adapter);
3249 return -1;
3252 scb->epthru = pci_alloc_consistent(adapter->dev,
3253 sizeof(mega_ext_passthru),
3254 &scb->epthru_dma_addr);
3256 if( !scb->epthru ) {
3257 printk(KERN_WARNING
3258 "Can't allocate extended passthru.\n");
3259 mega_free_sgl(adapter);
3260 return -1;
3264 scb->dma_type = MEGA_DMA_TYPE_NONE;
3267 * Link to free list
3268 * lock not required since we are loading the driver, so no
3269 * commands possible right now.
3271 scb->state = SCB_FREE;
3272 scb->cmd = NULL;
3273 list_add(&scb->list, &adapter->free_list);
3276 return 0;
3281 * megadev_open()
3282 * @inode - unused
3283 * @filep - unused
3285 * Routines for the character/ioctl interface to the driver. Find out if this
3286 * is a valid open.
3288 static int
3289 megadev_open (struct inode *inode, struct file *filep)
3292 * Only allow superuser to access private ioctl interface
3294 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
3296 return 0;
3301 * megadev_ioctl()
3302 * @inode - Our device inode
3303 * @filep - unused
3304 * @cmd - ioctl command
3305 * @arg - user buffer
3307 * ioctl entry point for our private ioctl interface. We move the data in from
3308 * the user space, prepare the command (if necessary, convert the old MIMD
3309 * ioctl to new ioctl command), and issue a synchronous command to the
3310 * controller.
3312 static int
3313 megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3315 adapter_t *adapter;
3316 nitioctl_t uioc;
3317 int adapno;
3318 int rval;
3319 mega_passthru __user *upthru; /* user address for passthru */
3320 mega_passthru *pthru; /* copy user passthru here */
3321 dma_addr_t pthru_dma_hndl;
3322 void *data = NULL; /* data to be transferred */
3323 dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
3324 megacmd_t mc;
3325 megastat_t __user *ustats;
3326 int num_ldrv;
3327 u32 uxferaddr = 0;
3328 struct pci_dev *pdev;
3330 ustats = NULL; /* avoid compilation warnings */
3331 num_ldrv = 0;
3334 * Make sure only USCSICMD are issued through this interface.
3335 * MIMD application would still fire different command.
3337 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3338 return -EINVAL;
3342 * Check and convert a possible MIMD command to NIT command.
3343 * mega_m_to_n() copies the data from the user space, so we do not
3344 * have to do it here.
3345 * NOTE: We will need some user address to copyout the data, therefore
3346 * the inteface layer will also provide us with the required user
3347 * addresses.
3349 memset(&uioc, 0, sizeof(nitioctl_t));
3350 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3351 return rval;
3354 switch( uioc.opcode ) {
3356 case GET_DRIVER_VER:
3357 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3358 return (-EFAULT);
3360 break;
3362 case GET_N_ADAP:
3363 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3364 return (-EFAULT);
3367 * Shucks. MIMD interface returns a positive value for number
3368 * of adapters. TODO: Change it to return 0 when there is no
3369 * applicatio using mimd interface.
3371 return hba_count;
3373 case GET_ADAP_INFO:
3376 * Which adapter
3378 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3379 return (-ENODEV);
3381 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3382 sizeof(struct mcontroller)) )
3383 return (-EFAULT);
3384 break;
3386 #if MEGA_HAVE_STATS
3388 case GET_STATS:
3390 * Which adapter
3392 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3393 return (-ENODEV);
3395 adapter = hba_soft_state[adapno];
3397 ustats = uioc.uioc_uaddr;
3399 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3400 return (-EFAULT);
3403 * Check for the validity of the logical drive number
3405 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3407 if( copy_to_user(ustats->nreads, adapter->nreads,
3408 num_ldrv*sizeof(u32)) )
3409 return -EFAULT;
3411 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3412 num_ldrv*sizeof(u32)) )
3413 return -EFAULT;
3415 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3416 num_ldrv*sizeof(u32)) )
3417 return -EFAULT;
3419 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3420 num_ldrv*sizeof(u32)) )
3421 return -EFAULT;
3423 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3424 num_ldrv*sizeof(u32)) )
3425 return -EFAULT;
3427 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3428 num_ldrv*sizeof(u32)) )
3429 return -EFAULT;
3431 return 0;
3433 #endif
3434 case MBOX_CMD:
3437 * Which adapter
3439 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3440 return (-ENODEV);
3442 adapter = hba_soft_state[adapno];
3445 * Deletion of logical drive is a special case. The adapter
3446 * should be quiescent before this command is issued.
3448 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3449 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3452 * Do we support this feature
3454 if( !adapter->support_random_del ) {
3455 printk(KERN_WARNING "megaraid: logdrv ");
3456 printk("delete on non-supporting F/W.\n");
3458 return (-EINVAL);
3461 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3463 if( rval == 0 ) {
3464 memset(&mc, 0, sizeof(megacmd_t));
3466 mc.status = rval;
3468 rval = mega_n_to_m((void __user *)arg, &mc);
3471 return rval;
3474 * This interface only support the regular passthru commands.
3475 * Reject extended passthru and 64-bit passthru
3477 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3478 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3480 printk(KERN_WARNING "megaraid: rejected passthru.\n");
3482 return (-EINVAL);
3486 * For all internal commands, the buffer must be allocated in
3487 * <4GB address range
3489 if( make_local_pdev(adapter, &pdev) != 0 )
3490 return -EIO;
3492 /* Is it a passthru command or a DCMD */
3493 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3494 /* Passthru commands */
3496 pthru = pci_alloc_consistent(pdev,
3497 sizeof(mega_passthru),
3498 &pthru_dma_hndl);
3500 if( pthru == NULL ) {
3501 free_local_pdev(pdev);
3502 return (-ENOMEM);
3506 * The user passthru structure
3508 upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
3511 * Copy in the user passthru here.
3513 if( copy_from_user(pthru, upthru,
3514 sizeof(mega_passthru)) ) {
3516 pci_free_consistent(pdev,
3517 sizeof(mega_passthru), pthru,
3518 pthru_dma_hndl);
3520 free_local_pdev(pdev);
3522 return (-EFAULT);
3526 * Is there a data transfer
3528 if( pthru->dataxferlen ) {
3529 data = pci_alloc_consistent(pdev,
3530 pthru->dataxferlen,
3531 &data_dma_hndl);
3533 if( data == NULL ) {
3534 pci_free_consistent(pdev,
3535 sizeof(mega_passthru),
3536 pthru,
3537 pthru_dma_hndl);
3539 free_local_pdev(pdev);
3541 return (-ENOMEM);
3545 * Save the user address and point the kernel
3546 * address at just allocated memory
3548 uxferaddr = pthru->dataxferaddr;
3549 pthru->dataxferaddr = data_dma_hndl;
3554 * Is data coming down-stream
3556 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3558 * Get the user data
3560 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3561 pthru->dataxferlen) ) {
3562 rval = (-EFAULT);
3563 goto freemem_and_return;
3567 memset(&mc, 0, sizeof(megacmd_t));
3569 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3570 mc.xferaddr = (u32)pthru_dma_hndl;
3573 * Issue the command
3575 mega_internal_command(adapter, &mc, pthru);
3577 rval = mega_n_to_m((void __user *)arg, &mc);
3579 if( rval ) goto freemem_and_return;
3583 * Is data going up-stream
3585 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3586 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3587 pthru->dataxferlen) ) {
3588 rval = (-EFAULT);
3593 * Send the request sense data also, irrespective of
3594 * whether the user has asked for it or not.
3596 if (copy_to_user(upthru->reqsensearea,
3597 pthru->reqsensearea, 14))
3598 rval = -EFAULT;
3600 freemem_and_return:
3601 if( pthru->dataxferlen ) {
3602 pci_free_consistent(pdev,
3603 pthru->dataxferlen, data,
3604 data_dma_hndl);
3607 pci_free_consistent(pdev, sizeof(mega_passthru),
3608 pthru, pthru_dma_hndl);
3610 free_local_pdev(pdev);
3612 return rval;
3614 else {
3615 /* DCMD commands */
3618 * Is there a data transfer
3620 if( uioc.xferlen ) {
3621 data = pci_alloc_consistent(pdev,
3622 uioc.xferlen, &data_dma_hndl);
3624 if( data == NULL ) {
3625 free_local_pdev(pdev);
3626 return (-ENOMEM);
3629 uxferaddr = MBOX(uioc)->xferaddr;
3633 * Is data coming down-stream
3635 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3637 * Get the user data
3639 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3640 uioc.xferlen) ) {
3642 pci_free_consistent(pdev,
3643 uioc.xferlen,
3644 data, data_dma_hndl);
3646 free_local_pdev(pdev);
3648 return (-EFAULT);
3652 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3654 mc.xferaddr = (u32)data_dma_hndl;
3657 * Issue the command
3659 mega_internal_command(adapter, &mc, NULL);
3661 rval = mega_n_to_m((void __user *)arg, &mc);
3663 if( rval ) {
3664 if( uioc.xferlen ) {
3665 pci_free_consistent(pdev,
3666 uioc.xferlen, data,
3667 data_dma_hndl);
3670 free_local_pdev(pdev);
3672 return rval;
3676 * Is data going up-stream
3678 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3679 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3680 uioc.xferlen) ) {
3682 rval = (-EFAULT);
3686 if( uioc.xferlen ) {
3687 pci_free_consistent(pdev,
3688 uioc.xferlen, data,
3689 data_dma_hndl);
3692 free_local_pdev(pdev);
3694 return rval;
3697 default:
3698 return (-EINVAL);
3701 return 0;
3704 static long
3705 megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3707 int ret;
3709 mutex_lock(&megadev_mutex);
3710 ret = megadev_ioctl(filep, cmd, arg);
3711 mutex_unlock(&megadev_mutex);
3713 return ret;
3717 * mega_m_to_n()
3718 * @arg - user address
3719 * @uioc - new ioctl structure
3721 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3722 * structure
3724 * Converts the older mimd ioctl structure to newer NIT structure
3726 static int
3727 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3729 struct uioctl_t uioc_mimd;
3730 char signature[8] = {0};
3731 u8 opcode;
3732 u8 subopcode;
3736 * check is the application conforms to NIT. We do not have to do much
3737 * in that case.
3738 * We exploit the fact that the signature is stored in the very
3739 * beginning of the structure.
3742 if( copy_from_user(signature, arg, 7) )
3743 return (-EFAULT);
3745 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3748 * NOTE NOTE: The nit ioctl is still under flux because of
3749 * change of mailbox definition, in HPE. No applications yet
3750 * use this interface and let's not have applications use this
3751 * interface till the new specifitions are in place.
3753 return -EINVAL;
3754 #if 0
3755 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3756 return (-EFAULT);
3757 return 0;
3758 #endif
3762 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3764 * Get the user ioctl structure
3766 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3767 return (-EFAULT);
3771 * Get the opcode and subopcode for the commands
3773 opcode = uioc_mimd.ui.fcs.opcode;
3774 subopcode = uioc_mimd.ui.fcs.subopcode;
3776 switch (opcode) {
3777 case 0x82:
3779 switch (subopcode) {
3781 case MEGAIOC_QDRVRVER: /* Query driver version */
3782 uioc->opcode = GET_DRIVER_VER;
3783 uioc->uioc_uaddr = uioc_mimd.data;
3784 break;
3786 case MEGAIOC_QNADAP: /* Get # of adapters */
3787 uioc->opcode = GET_N_ADAP;
3788 uioc->uioc_uaddr = uioc_mimd.data;
3789 break;
3791 case MEGAIOC_QADAPINFO: /* Get adapter information */
3792 uioc->opcode = GET_ADAP_INFO;
3793 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3794 uioc->uioc_uaddr = uioc_mimd.data;
3795 break;
3797 default:
3798 return(-EINVAL);
3801 break;
3804 case 0x81:
3806 uioc->opcode = MBOX_CMD;
3807 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3809 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3811 uioc->xferlen = uioc_mimd.ui.fcs.length;
3813 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3814 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3816 break;
3818 case 0x80:
3820 uioc->opcode = MBOX_CMD;
3821 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3823 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3826 * Choose the xferlen bigger of input and output data
3828 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3829 uioc_mimd.outlen : uioc_mimd.inlen;
3831 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3832 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3834 break;
3836 default:
3837 return (-EINVAL);
3841 return 0;
3845 * mega_n_to_m()
3846 * @arg - user address
3847 * @mc - mailbox command
3849 * Updates the status information to the application, depending on application
3850 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3852 static int
3853 mega_n_to_m(void __user *arg, megacmd_t *mc)
3855 nitioctl_t __user *uiocp;
3856 megacmd_t __user *umc;
3857 mega_passthru __user *upthru;
3858 struct uioctl_t __user *uioc_mimd;
3859 char signature[8] = {0};
3862 * check is the application conforms to NIT.
3864 if( copy_from_user(signature, arg, 7) )
3865 return -EFAULT;
3867 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3869 uiocp = arg;
3871 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3872 return (-EFAULT);
3874 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3876 umc = MBOX_P(uiocp);
3878 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3879 return -EFAULT;
3881 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3882 return (-EFAULT);
3885 else {
3886 uioc_mimd = arg;
3888 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3889 return (-EFAULT);
3891 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3893 umc = (megacmd_t __user *)uioc_mimd->mbox;
3895 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3896 return (-EFAULT);
3898 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3899 return (-EFAULT);
3903 return 0;
3908 * MEGARAID 'FW' commands.
3912 * mega_is_bios_enabled()
3913 * @adapter - pointer to our soft state
3915 * issue command to find out if the BIOS is enabled for this controller
3917 static int
3918 mega_is_bios_enabled(adapter_t *adapter)
3920 unsigned char raw_mbox[sizeof(struct mbox_out)];
3921 mbox_t *mbox;
3922 int ret;
3924 mbox = (mbox_t *)raw_mbox;
3926 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3928 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3930 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3932 raw_mbox[0] = IS_BIOS_ENABLED;
3933 raw_mbox[2] = GET_BIOS;
3936 ret = issue_scb_block(adapter, raw_mbox);
3938 return *(char *)adapter->mega_buffer;
3943 * mega_enum_raid_scsi()
3944 * @adapter - pointer to our soft state
3946 * Find out what channels are RAID/SCSI. This information is used to
3947 * differentiate the virtual channels and physical channels and to support
3948 * ROMB feature and non-disk devices.
3950 static void
3951 mega_enum_raid_scsi(adapter_t *adapter)
3953 unsigned char raw_mbox[sizeof(struct mbox_out)];
3954 mbox_t *mbox;
3955 int i;
3957 mbox = (mbox_t *)raw_mbox;
3959 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3962 * issue command to find out what channels are raid/scsi
3964 raw_mbox[0] = CHNL_CLASS;
3965 raw_mbox[2] = GET_CHNL_CLASS;
3967 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3969 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3972 * Non-ROMB firmware fail this command, so all channels
3973 * must be shown RAID
3975 adapter->mega_ch_class = 0xFF;
3977 if(!issue_scb_block(adapter, raw_mbox)) {
3978 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3982 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
3983 if( (adapter->mega_ch_class >> i) & 0x01 ) {
3984 printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
3987 else {
3988 printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
3993 return;
3998 * mega_get_boot_drv()
3999 * @adapter - pointer to our soft state
4001 * Find out which device is the boot device. Note, any logical drive or any
4002 * phyical device (e.g., a CDROM) can be designated as a boot device.
4004 static void
4005 mega_get_boot_drv(adapter_t *adapter)
4007 struct private_bios_data *prv_bios_data;
4008 unsigned char raw_mbox[sizeof(struct mbox_out)];
4009 mbox_t *mbox;
4010 u16 cksum = 0;
4011 u8 *cksum_p;
4012 u8 boot_pdrv;
4013 int i;
4015 mbox = (mbox_t *)raw_mbox;
4017 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4019 raw_mbox[0] = BIOS_PVT_DATA;
4020 raw_mbox[2] = GET_BIOS_PVT_DATA;
4022 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4024 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4026 adapter->boot_ldrv_enabled = 0;
4027 adapter->boot_ldrv = 0;
4029 adapter->boot_pdrv_enabled = 0;
4030 adapter->boot_pdrv_ch = 0;
4031 adapter->boot_pdrv_tgt = 0;
4033 if(issue_scb_block(adapter, raw_mbox) == 0) {
4034 prv_bios_data =
4035 (struct private_bios_data *)adapter->mega_buffer;
4037 cksum = 0;
4038 cksum_p = (char *)prv_bios_data;
4039 for (i = 0; i < 14; i++ ) {
4040 cksum += (u16)(*cksum_p++);
4043 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
4046 * If MSB is set, a physical drive is set as boot
4047 * device
4049 if( prv_bios_data->boot_drv & 0x80 ) {
4050 adapter->boot_pdrv_enabled = 1;
4051 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
4052 adapter->boot_pdrv_ch = boot_pdrv / 16;
4053 adapter->boot_pdrv_tgt = boot_pdrv % 16;
4055 else {
4056 adapter->boot_ldrv_enabled = 1;
4057 adapter->boot_ldrv = prv_bios_data->boot_drv;
4065 * mega_support_random_del()
4066 * @adapter - pointer to our soft state
4068 * Find out if this controller supports random deletion and addition of
4069 * logical drives
4071 static int
4072 mega_support_random_del(adapter_t *adapter)
4074 unsigned char raw_mbox[sizeof(struct mbox_out)];
4075 mbox_t *mbox;
4076 int rval;
4078 mbox = (mbox_t *)raw_mbox;
4080 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4083 * issue command
4085 raw_mbox[0] = FC_DEL_LOGDRV;
4086 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
4088 rval = issue_scb_block(adapter, raw_mbox);
4090 return !rval;
4095 * mega_support_ext_cdb()
4096 * @adapter - pointer to our soft state
4098 * Find out if this firmware support cdblen > 10
4100 static int
4101 mega_support_ext_cdb(adapter_t *adapter)
4103 unsigned char raw_mbox[sizeof(struct mbox_out)];
4104 mbox_t *mbox;
4105 int rval;
4107 mbox = (mbox_t *)raw_mbox;
4109 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4111 * issue command to find out if controller supports extended CDBs.
4113 raw_mbox[0] = 0xA4;
4114 raw_mbox[2] = 0x16;
4116 rval = issue_scb_block(adapter, raw_mbox);
4118 return !rval;
4123 * mega_del_logdrv()
4124 * @adapter - pointer to our soft state
4125 * @logdrv - logical drive to be deleted
4127 * Delete the specified logical drive. It is the responsibility of the user
4128 * app to let the OS know about this operation.
4130 static int
4131 mega_del_logdrv(adapter_t *adapter, int logdrv)
4133 unsigned long flags;
4134 scb_t *scb;
4135 int rval;
4138 * Stop sending commands to the controller, queue them internally.
4139 * When deletion is complete, ISR will flush the queue.
4141 atomic_set(&adapter->quiescent, 1);
4144 * Wait till all the issued commands are complete and there are no
4145 * commands in the pending queue
4147 while (atomic_read(&adapter->pend_cmds) > 0 ||
4148 !list_empty(&adapter->pending_list))
4149 msleep(1000); /* sleep for 1s */
4151 rval = mega_do_del_logdrv(adapter, logdrv);
4153 spin_lock_irqsave(&adapter->lock, flags);
4156 * If delete operation was successful, add 0x80 to the logical drive
4157 * ids for commands in the pending queue.
4159 if (adapter->read_ldidmap) {
4160 struct list_head *pos;
4161 list_for_each(pos, &adapter->pending_list) {
4162 scb = list_entry(pos, scb_t, list);
4163 if (scb->pthru->logdrv < 0x80 )
4164 scb->pthru->logdrv += 0x80;
4168 atomic_set(&adapter->quiescent, 0);
4170 mega_runpendq(adapter);
4172 spin_unlock_irqrestore(&adapter->lock, flags);
4174 return rval;
4178 static int
4179 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
4181 megacmd_t mc;
4182 int rval;
4184 memset( &mc, 0, sizeof(megacmd_t));
4186 mc.cmd = FC_DEL_LOGDRV;
4187 mc.opcode = OP_DEL_LOGDRV;
4188 mc.subopcode = logdrv;
4190 rval = mega_internal_command(adapter, &mc, NULL);
4192 /* log this event */
4193 if(rval) {
4194 printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
4195 return rval;
4199 * After deleting first logical drive, the logical drives must be
4200 * addressed by adding 0x80 to the logical drive id.
4202 adapter->read_ldidmap = 1;
4204 return rval;
4209 * mega_get_max_sgl()
4210 * @adapter - pointer to our soft state
4212 * Find out the maximum number of scatter-gather elements supported by this
4213 * version of the firmware
4215 static void
4216 mega_get_max_sgl(adapter_t *adapter)
4218 unsigned char raw_mbox[sizeof(struct mbox_out)];
4219 mbox_t *mbox;
4221 mbox = (mbox_t *)raw_mbox;
4223 memset(mbox, 0, sizeof(raw_mbox));
4225 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4227 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4229 raw_mbox[0] = MAIN_MISC_OPCODE;
4230 raw_mbox[2] = GET_MAX_SG_SUPPORT;
4233 if( issue_scb_block(adapter, raw_mbox) ) {
4235 * f/w does not support this command. Choose the default value
4237 adapter->sglen = MIN_SGLIST;
4239 else {
4240 adapter->sglen = *((char *)adapter->mega_buffer);
4243 * Make sure this is not more than the resources we are
4244 * planning to allocate
4246 if ( adapter->sglen > MAX_SGLIST )
4247 adapter->sglen = MAX_SGLIST;
4250 return;
4255 * mega_support_cluster()
4256 * @adapter - pointer to our soft state
4258 * Find out if this firmware support cluster calls.
4260 static int
4261 mega_support_cluster(adapter_t *adapter)
4263 unsigned char raw_mbox[sizeof(struct mbox_out)];
4264 mbox_t *mbox;
4266 mbox = (mbox_t *)raw_mbox;
4268 memset(mbox, 0, sizeof(raw_mbox));
4270 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4272 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4275 * Try to get the initiator id. This command will succeed iff the
4276 * clustering is available on this HBA.
4278 raw_mbox[0] = MEGA_GET_TARGET_ID;
4280 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
4283 * Cluster support available. Get the initiator target id.
4284 * Tell our id to mid-layer too.
4286 adapter->this_id = *(u32 *)adapter->mega_buffer;
4287 adapter->host->this_id = adapter->this_id;
4289 return 1;
4292 return 0;
4295 #ifdef CONFIG_PROC_FS
4297 * mega_adapinq()
4298 * @adapter - pointer to our soft state
4299 * @dma_handle - DMA address of the buffer
4301 * Issue internal commands while interrupts are available.
4302 * We only issue direct mailbox commands from within the driver. ioctl()
4303 * interface using these routines can issue passthru commands.
4305 static int
4306 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
4308 megacmd_t mc;
4310 memset(&mc, 0, sizeof(megacmd_t));
4312 if( adapter->flag & BOARD_40LD ) {
4313 mc.cmd = FC_NEW_CONFIG;
4314 mc.opcode = NC_SUBOP_ENQUIRY3;
4315 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
4317 else {
4318 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
4321 mc.xferaddr = (u32)dma_handle;
4323 if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
4324 return -1;
4327 return 0;
4331 /** mega_internal_dev_inquiry()
4332 * @adapter - pointer to our soft state
4333 * @ch - channel for this device
4334 * @tgt - ID of this device
4335 * @buf_dma_handle - DMA address of the buffer
4337 * Issue the scsi inquiry for the specified device.
4339 static int
4340 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4341 dma_addr_t buf_dma_handle)
4343 mega_passthru *pthru;
4344 dma_addr_t pthru_dma_handle;
4345 megacmd_t mc;
4346 int rval;
4347 struct pci_dev *pdev;
4351 * For all internal commands, the buffer must be allocated in <4GB
4352 * address range
4354 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4356 pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4357 &pthru_dma_handle);
4359 if( pthru == NULL ) {
4360 free_local_pdev(pdev);
4361 return -1;
4364 pthru->timeout = 2;
4365 pthru->ars = 1;
4366 pthru->reqsenselen = 14;
4367 pthru->islogical = 0;
4369 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4371 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4373 pthru->cdblen = 6;
4375 pthru->cdb[0] = INQUIRY;
4376 pthru->cdb[1] = 0;
4377 pthru->cdb[2] = 0;
4378 pthru->cdb[3] = 0;
4379 pthru->cdb[4] = 255;
4380 pthru->cdb[5] = 0;
4383 pthru->dataxferaddr = (u32)buf_dma_handle;
4384 pthru->dataxferlen = 256;
4386 memset(&mc, 0, sizeof(megacmd_t));
4388 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4389 mc.xferaddr = (u32)pthru_dma_handle;
4391 rval = mega_internal_command(adapter, &mc, pthru);
4393 pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4394 pthru_dma_handle);
4396 free_local_pdev(pdev);
4398 return rval;
4400 #endif
4403 * mega_internal_command()
4404 * @adapter - pointer to our soft state
4405 * @mc - the mailbox command
4406 * @pthru - Passthru structure for DCDB commands
4408 * Issue the internal commands in interrupt mode.
4409 * The last argument is the address of the passthru structure if the command
4410 * to be fired is a passthru command
4412 * lockscope specifies whether the caller has already acquired the lock. Of
4413 * course, the caller must know which lock we are talking about.
4415 * Note: parameter 'pthru' is null for non-passthru commands.
4417 static int
4418 mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4420 Scsi_Cmnd *scmd;
4421 struct scsi_device *sdev;
4422 scb_t *scb;
4423 int rval;
4425 scmd = scsi_allocate_command(GFP_KERNEL);
4426 if (!scmd)
4427 return -ENOMEM;
4430 * The internal commands share one command id and hence are
4431 * serialized. This is so because we want to reserve maximum number of
4432 * available command ids for the I/O commands.
4434 mutex_lock(&adapter->int_mtx);
4436 scb = &adapter->int_scb;
4437 memset(scb, 0, sizeof(scb_t));
4439 sdev = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
4440 scmd->device = sdev;
4442 memset(adapter->int_cdb, 0, sizeof(adapter->int_cdb));
4443 scmd->cmnd = adapter->int_cdb;
4444 scmd->device->host = adapter->host;
4445 scmd->host_scribble = (void *)scb;
4446 scmd->cmnd[0] = MEGA_INTERNAL_CMD;
4448 scb->state |= SCB_ACTIVE;
4449 scb->cmd = scmd;
4451 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4454 * Is it a passthru command
4456 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4458 scb->pthru = pthru;
4461 scb->idx = CMDID_INT_CMDS;
4463 megaraid_queue_lck(scmd, mega_internal_done);
4465 wait_for_completion(&adapter->int_waitq);
4467 rval = scmd->result;
4468 mc->status = scmd->result;
4469 kfree(sdev);
4472 * Print a debug message for all failed commands. Applications can use
4473 * this information.
4475 if( scmd->result && trace_level ) {
4476 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4477 mc->cmd, mc->opcode, mc->subopcode, scmd->result);
4480 mutex_unlock(&adapter->int_mtx);
4482 scsi_free_command(GFP_KERNEL, scmd);
4484 return rval;
4489 * mega_internal_done()
4490 * @scmd - internal scsi command
4492 * Callback routine for internal commands.
4494 static void
4495 mega_internal_done(Scsi_Cmnd *scmd)
4497 adapter_t *adapter;
4499 adapter = (adapter_t *)scmd->device->host->hostdata;
4501 complete(&adapter->int_waitq);
4506 static struct scsi_host_template megaraid_template = {
4507 .module = THIS_MODULE,
4508 .name = "MegaRAID",
4509 .proc_name = "megaraid_legacy",
4510 .info = megaraid_info,
4511 .queuecommand = megaraid_queue,
4512 .bios_param = megaraid_biosparam,
4513 .max_sectors = MAX_SECTORS_PER_IO,
4514 .can_queue = MAX_COMMANDS,
4515 .this_id = DEFAULT_INITIATOR_ID,
4516 .sg_tablesize = MAX_SGLIST,
4517 .cmd_per_lun = DEF_CMD_PER_LUN,
4518 .use_clustering = ENABLE_CLUSTERING,
4519 .eh_abort_handler = megaraid_abort,
4520 .eh_device_reset_handler = megaraid_reset,
4521 .eh_bus_reset_handler = megaraid_reset,
4522 .eh_host_reset_handler = megaraid_reset,
4525 static int __devinit
4526 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4528 struct Scsi_Host *host;
4529 adapter_t *adapter;
4530 unsigned long mega_baseport, tbase, flag = 0;
4531 u16 subsysid, subsysvid;
4532 u8 pci_bus, pci_dev_func;
4533 int irq, i, j;
4534 int error = -ENODEV;
4536 if (pci_enable_device(pdev))
4537 goto out;
4538 pci_set_master(pdev);
4540 pci_bus = pdev->bus->number;
4541 pci_dev_func = pdev->devfn;
4544 * The megaraid3 stuff reports the ID of the Intel part which is not
4545 * remotely specific to the megaraid
4547 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4548 u16 magic;
4550 * Don't fall over the Compaq management cards using the same
4551 * PCI identifier
4553 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4554 pdev->subsystem_device == 0xC000)
4555 return -ENODEV;
4556 /* Now check the magic signature byte */
4557 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4558 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4559 return -ENODEV;
4560 /* Ok it is probably a megaraid */
4564 * For these vendor and device ids, signature offsets are not
4565 * valid and 64 bit is implicit
4567 if (id->driver_data & BOARD_64BIT)
4568 flag |= BOARD_64BIT;
4569 else {
4570 u32 magic64;
4572 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4573 if (magic64 == HBA_SIGNATURE_64BIT)
4574 flag |= BOARD_64BIT;
4577 subsysvid = pdev->subsystem_vendor;
4578 subsysid = pdev->subsystem_device;
4580 printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4581 id->vendor, id->device, pci_bus);
4583 printk("slot %d:func %d\n",
4584 PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
4586 /* Read the base port and IRQ from PCI */
4587 mega_baseport = pci_resource_start(pdev, 0);
4588 irq = pdev->irq;
4590 tbase = mega_baseport;
4591 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4592 flag |= BOARD_MEMMAP;
4594 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4595 printk(KERN_WARNING "megaraid: mem region busy!\n");
4596 goto out_disable_device;
4599 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4600 if (!mega_baseport) {
4601 printk(KERN_WARNING
4602 "megaraid: could not map hba memory\n");
4603 goto out_release_region;
4605 } else {
4606 flag |= BOARD_IOMAP;
4607 mega_baseport += 0x10;
4609 if (!request_region(mega_baseport, 16, "megaraid"))
4610 goto out_disable_device;
4613 /* Initialize SCSI Host structure */
4614 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4615 if (!host)
4616 goto out_iounmap;
4618 adapter = (adapter_t *)host->hostdata;
4619 memset(adapter, 0, sizeof(adapter_t));
4621 printk(KERN_NOTICE
4622 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4623 host->host_no, mega_baseport, irq);
4625 adapter->base = mega_baseport;
4626 if (flag & BOARD_MEMMAP)
4627 adapter->mmio_base = (void __iomem *) mega_baseport;
4629 INIT_LIST_HEAD(&adapter->free_list);
4630 INIT_LIST_HEAD(&adapter->pending_list);
4631 INIT_LIST_HEAD(&adapter->completed_list);
4633 adapter->flag = flag;
4634 spin_lock_init(&adapter->lock);
4636 host->cmd_per_lun = max_cmd_per_lun;
4637 host->max_sectors = max_sectors_per_io;
4639 adapter->dev = pdev;
4640 adapter->host = host;
4642 adapter->host->irq = irq;
4644 if (flag & BOARD_MEMMAP)
4645 adapter->host->base = tbase;
4646 else {
4647 adapter->host->io_port = tbase;
4648 adapter->host->n_io_port = 16;
4651 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4654 * Allocate buffer to issue internal commands.
4656 adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4657 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4658 if (!adapter->mega_buffer) {
4659 printk(KERN_WARNING "megaraid: out of RAM.\n");
4660 goto out_host_put;
4663 adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4664 if (!adapter->scb_list) {
4665 printk(KERN_WARNING "megaraid: out of RAM.\n");
4666 goto out_free_cmd_buffer;
4669 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4670 megaraid_isr_memmapped : megaraid_isr_iomapped,
4671 IRQF_SHARED, "megaraid", adapter)) {
4672 printk(KERN_WARNING
4673 "megaraid: Couldn't register IRQ %d!\n", irq);
4674 goto out_free_scb_list;
4677 if (mega_setup_mailbox(adapter))
4678 goto out_free_irq;
4680 if (mega_query_adapter(adapter))
4681 goto out_free_mbox;
4684 * Have checks for some buggy f/w
4686 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4688 * Which firmware
4690 if (!strcmp(adapter->fw_version, "3.00") ||
4691 !strcmp(adapter->fw_version, "3.01")) {
4693 printk( KERN_WARNING
4694 "megaraid: Your card is a Dell PERC "
4695 "2/SC RAID controller with "
4696 "firmware\nmegaraid: 3.00 or 3.01. "
4697 "This driver is known to have "
4698 "corruption issues\nmegaraid: with "
4699 "those firmware versions on this "
4700 "specific card. In order\nmegaraid: "
4701 "to protect your data, please upgrade "
4702 "your firmware to version\nmegaraid: "
4703 "3.10 or later, available from the "
4704 "Dell Technical Support web\n"
4705 "megaraid: site at\nhttp://support."
4706 "dell.com/us/en/filelib/download/"
4707 "index.asp?fileid=2940\n"
4713 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4714 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4715 * support, since this firmware cannot handle 64 bit
4716 * addressing
4718 if ((subsysvid == PCI_VENDOR_ID_HP) &&
4719 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4721 * which firmware
4723 if (!strcmp(adapter->fw_version, "H01.07") ||
4724 !strcmp(adapter->fw_version, "H01.08") ||
4725 !strcmp(adapter->fw_version, "H01.09") ) {
4726 printk(KERN_WARNING
4727 "megaraid: Firmware H.01.07, "
4728 "H.01.08, and H.01.09 on 1M/2M "
4729 "controllers\n"
4730 "megaraid: do not support 64 bit "
4731 "addressing.\nmegaraid: DISABLING "
4732 "64 bit support.\n");
4733 adapter->flag &= ~BOARD_64BIT;
4737 if (mega_is_bios_enabled(adapter))
4738 mega_hbas[hba_count].is_bios_enabled = 1;
4739 mega_hbas[hba_count].hostdata_addr = adapter;
4742 * Find out which channel is raid and which is scsi. This is
4743 * for ROMB support.
4745 mega_enum_raid_scsi(adapter);
4748 * Find out if a logical drive is set as the boot drive. If
4749 * there is one, will make that as the first logical drive.
4750 * ROMB: Do we have to boot from a physical drive. Then all
4751 * the physical drives would appear before the logical disks.
4752 * Else, all the physical drives would be exported to the mid
4753 * layer after logical drives.
4755 mega_get_boot_drv(adapter);
4757 if (adapter->boot_pdrv_enabled) {
4758 j = adapter->product_info.nchannels;
4759 for( i = 0; i < j; i++ )
4760 adapter->logdrv_chan[i] = 0;
4761 for( i = j; i < NVIRT_CHAN + j; i++ )
4762 adapter->logdrv_chan[i] = 1;
4763 } else {
4764 for (i = 0; i < NVIRT_CHAN; i++)
4765 adapter->logdrv_chan[i] = 1;
4766 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4767 adapter->logdrv_chan[i] = 0;
4768 adapter->mega_ch_class <<= NVIRT_CHAN;
4772 * Do we support random deletion and addition of logical
4773 * drives
4775 adapter->read_ldidmap = 0; /* set it after first logdrv
4776 delete cmd */
4777 adapter->support_random_del = mega_support_random_del(adapter);
4779 /* Initialize SCBs */
4780 if (mega_init_scb(adapter))
4781 goto out_free_mbox;
4784 * Reset the pending commands counter
4786 atomic_set(&adapter->pend_cmds, 0);
4789 * Reset the adapter quiescent flag
4791 atomic_set(&adapter->quiescent, 0);
4793 hba_soft_state[hba_count] = adapter;
4796 * Fill in the structure which needs to be passed back to the
4797 * application when it does an ioctl() for controller related
4798 * information.
4800 i = hba_count;
4802 mcontroller[i].base = mega_baseport;
4803 mcontroller[i].irq = irq;
4804 mcontroller[i].numldrv = adapter->numldrv;
4805 mcontroller[i].pcibus = pci_bus;
4806 mcontroller[i].pcidev = id->device;
4807 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4808 mcontroller[i].pciid = -1;
4809 mcontroller[i].pcivendor = id->vendor;
4810 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4811 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4814 /* Set the Mode of addressing to 64 bit if we can */
4815 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4816 pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4817 adapter->has_64bit_addr = 1;
4818 } else {
4819 pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4820 adapter->has_64bit_addr = 0;
4823 mutex_init(&adapter->int_mtx);
4824 init_completion(&adapter->int_waitq);
4826 adapter->this_id = DEFAULT_INITIATOR_ID;
4827 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4829 #if MEGA_HAVE_CLUSTERING
4831 * Is cluster support enabled on this controller
4832 * Note: In a cluster the HBAs ( the initiators ) will have
4833 * different target IDs and we cannot assume it to be 7. Call
4834 * to mega_support_cluster() will get the target ids also if
4835 * the cluster support is available
4837 adapter->has_cluster = mega_support_cluster(adapter);
4838 if (adapter->has_cluster) {
4839 printk(KERN_NOTICE
4840 "megaraid: Cluster driver, initiator id:%d\n",
4841 adapter->this_id);
4843 #endif
4845 pci_set_drvdata(pdev, host);
4847 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4849 error = scsi_add_host(host, &pdev->dev);
4850 if (error)
4851 goto out_free_mbox;
4853 scsi_scan_host(host);
4854 hba_count++;
4855 return 0;
4857 out_free_mbox:
4858 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4859 adapter->una_mbox64, adapter->una_mbox64_dma);
4860 out_free_irq:
4861 free_irq(adapter->host->irq, adapter);
4862 out_free_scb_list:
4863 kfree(adapter->scb_list);
4864 out_free_cmd_buffer:
4865 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4866 adapter->mega_buffer, adapter->buf_dma_handle);
4867 out_host_put:
4868 scsi_host_put(host);
4869 out_iounmap:
4870 if (flag & BOARD_MEMMAP)
4871 iounmap((void *)mega_baseport);
4872 out_release_region:
4873 if (flag & BOARD_MEMMAP)
4874 release_mem_region(tbase, 128);
4875 else
4876 release_region(mega_baseport, 16);
4877 out_disable_device:
4878 pci_disable_device(pdev);
4879 out:
4880 return error;
4883 static void
4884 __megaraid_shutdown(adapter_t *adapter)
4886 u_char raw_mbox[sizeof(struct mbox_out)];
4887 mbox_t *mbox = (mbox_t *)raw_mbox;
4888 int i;
4890 /* Flush adapter cache */
4891 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4892 raw_mbox[0] = FLUSH_ADAPTER;
4894 free_irq(adapter->host->irq, adapter);
4896 /* Issue a blocking (interrupts disabled) command to the card */
4897 issue_scb_block(adapter, raw_mbox);
4899 /* Flush disks cache */
4900 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4901 raw_mbox[0] = FLUSH_SYSTEM;
4903 /* Issue a blocking (interrupts disabled) command to the card */
4904 issue_scb_block(adapter, raw_mbox);
4906 if (atomic_read(&adapter->pend_cmds) > 0)
4907 printk(KERN_WARNING "megaraid: pending commands!!\n");
4910 * Have a delibrate delay to make sure all the caches are
4911 * actually flushed.
4913 for (i = 0; i <= 10; i++)
4914 mdelay(1000);
4917 static void __devexit
4918 megaraid_remove_one(struct pci_dev *pdev)
4920 struct Scsi_Host *host = pci_get_drvdata(pdev);
4921 adapter_t *adapter = (adapter_t *)host->hostdata;
4923 scsi_remove_host(host);
4925 __megaraid_shutdown(adapter);
4927 /* Free our resources */
4928 if (adapter->flag & BOARD_MEMMAP) {
4929 iounmap((void *)adapter->base);
4930 release_mem_region(adapter->host->base, 128);
4931 } else
4932 release_region(adapter->base, 16);
4934 mega_free_sgl(adapter);
4936 #ifdef CONFIG_PROC_FS
4937 if (adapter->controller_proc_dir_entry) {
4938 remove_proc_entry("stat", adapter->controller_proc_dir_entry);
4939 remove_proc_entry("config",
4940 adapter->controller_proc_dir_entry);
4941 remove_proc_entry("mailbox",
4942 adapter->controller_proc_dir_entry);
4943 #if MEGA_HAVE_ENH_PROC
4944 remove_proc_entry("rebuild-rate",
4945 adapter->controller_proc_dir_entry);
4946 remove_proc_entry("battery-status",
4947 adapter->controller_proc_dir_entry);
4949 remove_proc_entry("diskdrives-ch0",
4950 adapter->controller_proc_dir_entry);
4951 remove_proc_entry("diskdrives-ch1",
4952 adapter->controller_proc_dir_entry);
4953 remove_proc_entry("diskdrives-ch2",
4954 adapter->controller_proc_dir_entry);
4955 remove_proc_entry("diskdrives-ch3",
4956 adapter->controller_proc_dir_entry);
4958 remove_proc_entry("raiddrives-0-9",
4959 adapter->controller_proc_dir_entry);
4960 remove_proc_entry("raiddrives-10-19",
4961 adapter->controller_proc_dir_entry);
4962 remove_proc_entry("raiddrives-20-29",
4963 adapter->controller_proc_dir_entry);
4964 remove_proc_entry("raiddrives-30-39",
4965 adapter->controller_proc_dir_entry);
4966 #endif
4968 char buf[12] = { 0 };
4969 sprintf(buf, "hba%d", adapter->host->host_no);
4970 remove_proc_entry(buf, mega_proc_dir_entry);
4973 #endif
4975 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4976 adapter->mega_buffer, adapter->buf_dma_handle);
4977 kfree(adapter->scb_list);
4978 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4979 adapter->una_mbox64, adapter->una_mbox64_dma);
4981 scsi_host_put(host);
4982 pci_disable_device(pdev);
4984 hba_count--;
4987 static void
4988 megaraid_shutdown(struct pci_dev *pdev)
4990 struct Scsi_Host *host = pci_get_drvdata(pdev);
4991 adapter_t *adapter = (adapter_t *)host->hostdata;
4993 __megaraid_shutdown(adapter);
4996 static struct pci_device_id megaraid_pci_tbl[] = {
4997 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4998 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4999 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
5000 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5001 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
5002 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5003 {0,}
5005 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
5007 static struct pci_driver megaraid_pci_driver = {
5008 .name = "megaraid_legacy",
5009 .id_table = megaraid_pci_tbl,
5010 .probe = megaraid_probe_one,
5011 .remove = __devexit_p(megaraid_remove_one),
5012 .shutdown = megaraid_shutdown,
5015 static int __init megaraid_init(void)
5017 int error;
5019 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
5020 max_cmd_per_lun = MAX_CMD_PER_LUN;
5021 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
5022 max_mbox_busy_wait = MBOX_BUSY_WAIT;
5024 #ifdef CONFIG_PROC_FS
5025 mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
5026 if (!mega_proc_dir_entry) {
5027 printk(KERN_WARNING
5028 "megaraid: failed to create megaraid root\n");
5030 #endif
5031 error = pci_register_driver(&megaraid_pci_driver);
5032 if (error) {
5033 #ifdef CONFIG_PROC_FS
5034 remove_proc_entry("megaraid", NULL);
5035 #endif
5036 return error;
5040 * Register the driver as a character device, for applications
5041 * to access it for ioctls.
5042 * First argument (major) to register_chrdev implies a dynamic
5043 * major number allocation.
5045 major = register_chrdev(0, "megadev_legacy", &megadev_fops);
5046 if (!major) {
5047 printk(KERN_WARNING
5048 "megaraid: failed to register char device\n");
5051 return 0;
5054 static void __exit megaraid_exit(void)
5057 * Unregister the character device interface to the driver.
5059 unregister_chrdev(major, "megadev_legacy");
5061 pci_unregister_driver(&megaraid_pci_driver);
5063 #ifdef CONFIG_PROC_FS
5064 remove_proc_entry("megaraid", NULL);
5065 #endif
5068 module_init(megaraid_init);
5069 module_exit(megaraid_exit);
5071 /* vi: set ts=8 sw=8 tw=78: */