[POWERPC] MPC832x_RDB: Update dts to use SPI1 in QE, register mmc_spi stub
[pv_ops_mirror.git] / drivers / scsi / megaraid.c
blob3907f6718ede11272e797bc7b6f7d7a1bfc34210
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 <scsi/scsicam.h>
51 #include "scsi.h"
52 #include <scsi/scsi_host.h>
54 #include "megaraid.h"
56 #define MEGARAID_MODULE_VERSION "2.00.4"
58 MODULE_AUTHOR ("sju@lsil.com");
59 MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
60 MODULE_LICENSE ("GPL");
61 MODULE_VERSION(MEGARAID_MODULE_VERSION);
63 static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
64 module_param(max_cmd_per_lun, uint, 0);
65 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)");
67 static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
68 module_param(max_sectors_per_io, ushort, 0);
69 MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
72 static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
73 module_param(max_mbox_busy_wait, ushort, 0);
74 MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
76 #define RDINDOOR(adapter) readl((adapter)->mmio_base + 0x20)
77 #define RDOUTDOOR(adapter) readl((adapter)->mmio_base + 0x2C)
78 #define WRINDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x20)
79 #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
82 * Global variables
85 static int hba_count;
86 static adapter_t *hba_soft_state[MAX_CONTROLLERS];
87 static struct proc_dir_entry *mega_proc_dir_entry;
89 /* For controller re-ordering */
90 static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
93 * The File Operations structure for the serial/ioctl interface of the driver
95 static const struct file_operations megadev_fops = {
96 .owner = THIS_MODULE,
97 .ioctl = megadev_ioctl,
98 .open = megadev_open,
102 * Array to structures for storing the information about the controllers. This
103 * information is sent to the user level applications, when they do an ioctl
104 * for this information.
106 static struct mcontroller mcontroller[MAX_CONTROLLERS];
108 /* The current driver version */
109 static u32 driver_ver = 0x02000000;
111 /* major number used by the device for character interface */
112 static int major;
114 #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
118 * Debug variable to print some diagnostic messages
120 static int trace_level;
123 * mega_setup_mailbox()
124 * @adapter - pointer to our soft state
126 * Allocates a 8 byte aligned memory for the handshake mailbox.
128 static int
129 mega_setup_mailbox(adapter_t *adapter)
131 unsigned long align;
133 adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
134 sizeof(mbox64_t), &adapter->una_mbox64_dma);
136 if( !adapter->una_mbox64 ) return -1;
138 adapter->mbox = &adapter->una_mbox64->mbox;
140 adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
141 (~0UL ^ 0xFUL));
143 adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
145 align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
147 adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
150 * Register the mailbox if the controller is an io-mapped controller
152 if( adapter->flag & BOARD_IOMAP ) {
154 outb_p(adapter->mbox_dma & 0xFF,
155 adapter->host->io_port + MBOX_PORT0);
157 outb_p((adapter->mbox_dma >> 8) & 0xFF,
158 adapter->host->io_port + MBOX_PORT1);
160 outb_p((adapter->mbox_dma >> 16) & 0xFF,
161 adapter->host->io_port + MBOX_PORT2);
163 outb_p((adapter->mbox_dma >> 24) & 0xFF,
164 adapter->host->io_port + MBOX_PORT3);
166 outb_p(ENABLE_MBOX_BYTE,
167 adapter->host->io_port + ENABLE_MBOX_REGION);
169 irq_ack(adapter);
171 irq_enable(adapter);
174 return 0;
179 * mega_query_adapter()
180 * @adapter - pointer to our soft state
182 * Issue the adapter inquiry commands to the controller and find out
183 * information and parameter about the devices attached
185 static int
186 mega_query_adapter(adapter_t *adapter)
188 dma_addr_t prod_info_dma_handle;
189 mega_inquiry3 *inquiry3;
190 u8 raw_mbox[sizeof(struct mbox_out)];
191 mbox_t *mbox;
192 int retval;
194 /* Initialize adapter inquiry mailbox */
196 mbox = (mbox_t *)raw_mbox;
198 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
199 memset(&mbox->m_out, 0, sizeof(raw_mbox));
202 * Try to issue Inquiry3 command
203 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
204 * update enquiry3 structure
206 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
208 inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
210 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
211 raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
212 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
214 /* Issue a blocking command to the card */
215 if ((retval = issue_scb_block(adapter, raw_mbox))) {
216 /* the adapter does not support 40ld */
218 mraid_ext_inquiry *ext_inq;
219 mraid_inquiry *inq;
220 dma_addr_t dma_handle;
222 ext_inq = pci_alloc_consistent(adapter->dev,
223 sizeof(mraid_ext_inquiry), &dma_handle);
225 if( ext_inq == NULL ) return -1;
227 inq = &ext_inq->raid_inq;
229 mbox->m_out.xferaddr = (u32)dma_handle;
231 /*issue old 0x04 command to adapter */
232 mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
234 issue_scb_block(adapter, raw_mbox);
237 * update Enquiry3 and ProductInfo structures with
238 * mraid_inquiry structure
240 mega_8_to_40ld(inq, inquiry3,
241 (mega_product_info *)&adapter->product_info);
243 pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
244 ext_inq, dma_handle);
246 } else { /*adapter supports 40ld */
247 adapter->flag |= BOARD_40LD;
250 * get product_info, which is static information and will be
251 * unchanged
253 prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
254 &adapter->product_info,
255 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
257 mbox->m_out.xferaddr = prod_info_dma_handle;
259 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
260 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
262 if ((retval = issue_scb_block(adapter, raw_mbox)))
263 printk(KERN_WARNING
264 "megaraid: Product_info cmd failed with error: %d\n",
265 retval);
267 pci_unmap_single(adapter->dev, prod_info_dma_handle,
268 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
273 * kernel scans the channels from 0 to <= max_channel
275 adapter->host->max_channel =
276 adapter->product_info.nchannels + NVIRT_CHAN -1;
278 adapter->host->max_id = 16; /* max targets per channel */
280 adapter->host->max_lun = 7; /* Upto 7 luns for non disk devices */
282 adapter->host->cmd_per_lun = max_cmd_per_lun;
284 adapter->numldrv = inquiry3->num_ldrv;
286 adapter->max_cmds = adapter->product_info.max_commands;
288 if(adapter->max_cmds > MAX_COMMANDS)
289 adapter->max_cmds = MAX_COMMANDS;
291 adapter->host->can_queue = adapter->max_cmds - 1;
294 * Get the maximum number of scatter-gather elements supported by this
295 * firmware
297 mega_get_max_sgl(adapter);
299 adapter->host->sg_tablesize = adapter->sglen;
302 /* use HP firmware and bios version encoding */
303 if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
304 sprintf (adapter->fw_version, "%c%d%d.%d%d",
305 adapter->product_info.fw_version[2],
306 adapter->product_info.fw_version[1] >> 8,
307 adapter->product_info.fw_version[1] & 0x0f,
308 adapter->product_info.fw_version[0] >> 8,
309 adapter->product_info.fw_version[0] & 0x0f);
310 sprintf (adapter->bios_version, "%c%d%d.%d%d",
311 adapter->product_info.bios_version[2],
312 adapter->product_info.bios_version[1] >> 8,
313 adapter->product_info.bios_version[1] & 0x0f,
314 adapter->product_info.bios_version[0] >> 8,
315 adapter->product_info.bios_version[0] & 0x0f);
316 } else {
317 memcpy(adapter->fw_version,
318 (char *)adapter->product_info.fw_version, 4);
319 adapter->fw_version[4] = 0;
321 memcpy(adapter->bios_version,
322 (char *)adapter->product_info.bios_version, 4);
324 adapter->bios_version[4] = 0;
327 printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
328 adapter->fw_version, adapter->bios_version, adapter->numldrv);
331 * Do we support extended (>10 bytes) cdbs
333 adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
334 if (adapter->support_ext_cdb)
335 printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
338 return 0;
342 * mega_runpendq()
343 * @adapter - pointer to our soft state
345 * Runs through the list of pending requests.
347 static inline void
348 mega_runpendq(adapter_t *adapter)
350 if(!list_empty(&adapter->pending_list))
351 __mega_runpendq(adapter);
355 * megaraid_queue()
356 * @scmd - Issue this scsi command
357 * @done - the callback hook into the scsi mid-layer
359 * The command queuing entry point for the mid-layer.
361 static int
362 megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
364 adapter_t *adapter;
365 scb_t *scb;
366 int busy=0;
367 unsigned long flags;
369 adapter = (adapter_t *)scmd->device->host->hostdata;
371 scmd->scsi_done = done;
375 * Allocate and build a SCB request
376 * busy flag will be set if mega_build_cmd() command could not
377 * allocate scb. We will return non-zero status in that case.
378 * NOTE: scb can be null even though certain commands completed
379 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
380 * return 0 in that case.
383 spin_lock_irqsave(&adapter->lock, flags);
384 scb = mega_build_cmd(adapter, scmd, &busy);
385 if (!scb)
386 goto out;
388 scb->state |= SCB_PENDQ;
389 list_add_tail(&scb->list, &adapter->pending_list);
392 * Check if the HBA is in quiescent state, e.g., during a
393 * delete logical drive opertion. If it is, don't run
394 * the pending_list.
396 if (atomic_read(&adapter->quiescent) == 0)
397 mega_runpendq(adapter);
399 busy = 0;
400 out:
401 spin_unlock_irqrestore(&adapter->lock, flags);
402 return busy;
406 * mega_allocate_scb()
407 * @adapter - pointer to our soft state
408 * @cmd - scsi command from the mid-layer
410 * Allocate a SCB structure. This is the central structure for controller
411 * commands.
413 static inline scb_t *
414 mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
416 struct list_head *head = &adapter->free_list;
417 scb_t *scb;
419 /* Unlink command from Free List */
420 if( !list_empty(head) ) {
422 scb = list_entry(head->next, scb_t, list);
424 list_del_init(head->next);
426 scb->state = SCB_ACTIVE;
427 scb->cmd = cmd;
428 scb->dma_type = MEGA_DMA_TYPE_NONE;
430 return scb;
433 return NULL;
437 * mega_get_ldrv_num()
438 * @adapter - pointer to our soft state
439 * @cmd - scsi mid layer command
440 * @channel - channel on the controller
442 * Calculate the logical drive number based on the information in scsi command
443 * and the channel number.
445 static inline int
446 mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
448 int tgt;
449 int ldrv_num;
451 tgt = cmd->device->id;
453 if ( tgt > adapter->this_id )
454 tgt--; /* we do not get inquires for initiator id */
456 ldrv_num = (channel * 15) + tgt;
460 * If we have a logical drive with boot enabled, project it first
462 if( adapter->boot_ldrv_enabled ) {
463 if( ldrv_num == 0 ) {
464 ldrv_num = adapter->boot_ldrv;
466 else {
467 if( ldrv_num <= adapter->boot_ldrv ) {
468 ldrv_num--;
474 * If "delete logical drive" feature is enabled on this controller.
475 * Do only if at least one delete logical drive operation was done.
477 * Also, after logical drive deletion, instead of logical drive number,
478 * the value returned should be 0x80+logical drive id.
480 * These is valid only for IO commands.
483 if (adapter->support_random_del && adapter->read_ldidmap )
484 switch (cmd->cmnd[0]) {
485 case READ_6: /* fall through */
486 case WRITE_6: /* fall through */
487 case READ_10: /* fall through */
488 case WRITE_10:
489 ldrv_num += 0x80;
492 return ldrv_num;
496 * mega_build_cmd()
497 * @adapter - pointer to our soft state
498 * @cmd - Prepare using this scsi command
499 * @busy - busy flag if no resources
501 * Prepares a command and scatter gather list for the controller. This routine
502 * also finds out if the commands is intended for a logical drive or a
503 * physical device and prepares the controller command accordingly.
505 * We also re-order the logical drives and physical devices based on their
506 * boot settings.
508 static scb_t *
509 mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
511 mega_ext_passthru *epthru;
512 mega_passthru *pthru;
513 scb_t *scb;
514 mbox_t *mbox;
515 long seg;
516 char islogical;
517 int max_ldrv_num;
518 int channel = 0;
519 int target = 0;
520 int ldrv_num = 0; /* logical drive number */
524 * filter the internal and ioctl commands
526 if((cmd->cmnd[0] == MEGA_INTERNAL_CMD))
527 return (scb_t *)cmd->host_scribble;
530 * We know what channels our logical drives are on - mega_find_card()
532 islogical = adapter->logdrv_chan[cmd->device->channel];
535 * The theory: If physical drive is chosen for boot, all the physical
536 * devices are exported before the logical drives, otherwise physical
537 * devices are pushed after logical drives, in which case - Kernel sees
538 * the physical devices on virtual channel which is obviously converted
539 * to actual channel on the HBA.
541 if( adapter->boot_pdrv_enabled ) {
542 if( islogical ) {
543 /* logical channel */
544 channel = cmd->device->channel -
545 adapter->product_info.nchannels;
547 else {
548 /* this is physical channel */
549 channel = cmd->device->channel;
550 target = cmd->device->id;
553 * boot from a physical disk, that disk needs to be
554 * exposed first IF both the channels are SCSI, then
555 * booting from the second channel is not allowed.
557 if( target == 0 ) {
558 target = adapter->boot_pdrv_tgt;
560 else if( target == adapter->boot_pdrv_tgt ) {
561 target = 0;
565 else {
566 if( islogical ) {
567 /* this is the logical channel */
568 channel = cmd->device->channel;
570 else {
571 /* physical channel */
572 channel = cmd->device->channel - NVIRT_CHAN;
573 target = cmd->device->id;
578 if(islogical) {
580 /* have just LUN 0 for each target on virtual channels */
581 if (cmd->device->lun) {
582 cmd->result = (DID_BAD_TARGET << 16);
583 cmd->scsi_done(cmd);
584 return NULL;
587 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
590 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
591 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
594 * max_ldrv_num increases by 0x80 if some logical drive was
595 * deleted.
597 if(adapter->read_ldidmap)
598 max_ldrv_num += 0x80;
600 if(ldrv_num > max_ldrv_num ) {
601 cmd->result = (DID_BAD_TARGET << 16);
602 cmd->scsi_done(cmd);
603 return NULL;
607 else {
608 if( cmd->device->lun > 7) {
610 * Do not support lun >7 for physically accessed
611 * devices
613 cmd->result = (DID_BAD_TARGET << 16);
614 cmd->scsi_done(cmd);
615 return NULL;
621 * Logical drive commands
624 if(islogical) {
625 switch (cmd->cmnd[0]) {
626 case TEST_UNIT_READY:
627 #if MEGA_HAVE_CLUSTERING
629 * Do we support clustering and is the support enabled
630 * If no, return success always
632 if( !adapter->has_cluster ) {
633 cmd->result = (DID_OK << 16);
634 cmd->scsi_done(cmd);
635 return NULL;
638 if(!(scb = mega_allocate_scb(adapter, cmd))) {
639 *busy = 1;
640 return NULL;
643 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
644 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
645 scb->raw_mbox[3] = ldrv_num;
647 scb->dma_direction = PCI_DMA_NONE;
649 return scb;
650 #else
651 cmd->result = (DID_OK << 16);
652 cmd->scsi_done(cmd);
653 return NULL;
654 #endif
656 case MODE_SENSE: {
657 char *buf;
658 struct scatterlist *sg;
660 sg = scsi_sglist(cmd);
661 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
663 memset(buf, 0, cmd->cmnd[4]);
664 kunmap_atomic(buf - sg->offset, KM_IRQ0);
666 cmd->result = (DID_OK << 16);
667 cmd->scsi_done(cmd);
668 return NULL;
671 case READ_CAPACITY:
672 case INQUIRY:
674 if(!(adapter->flag & (1L << cmd->device->channel))) {
676 printk(KERN_NOTICE
677 "scsi%d: scanning scsi channel %d ",
678 adapter->host->host_no,
679 cmd->device->channel);
680 printk("for logical drives.\n");
682 adapter->flag |= (1L << cmd->device->channel);
685 /* Allocate a SCB and initialize passthru */
686 if(!(scb = mega_allocate_scb(adapter, cmd))) {
687 *busy = 1;
688 return NULL;
690 pthru = scb->pthru;
692 mbox = (mbox_t *)scb->raw_mbox;
693 memset(mbox, 0, sizeof(scb->raw_mbox));
694 memset(pthru, 0, sizeof(mega_passthru));
696 pthru->timeout = 0;
697 pthru->ars = 1;
698 pthru->reqsenselen = 14;
699 pthru->islogical = 1;
700 pthru->logdrv = ldrv_num;
701 pthru->cdblen = cmd->cmd_len;
702 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
704 if( adapter->has_64bit_addr ) {
705 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
707 else {
708 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
711 scb->dma_direction = PCI_DMA_FROMDEVICE;
713 pthru->numsgelements = mega_build_sglist(adapter, scb,
714 &pthru->dataxferaddr, &pthru->dataxferlen);
716 mbox->m_out.xferaddr = scb->pthru_dma_addr;
718 return scb;
720 case READ_6:
721 case WRITE_6:
722 case READ_10:
723 case WRITE_10:
724 case READ_12:
725 case WRITE_12:
727 /* Allocate a SCB and initialize mailbox */
728 if(!(scb = mega_allocate_scb(adapter, cmd))) {
729 *busy = 1;
730 return NULL;
732 mbox = (mbox_t *)scb->raw_mbox;
734 memset(mbox, 0, sizeof(scb->raw_mbox));
735 mbox->m_out.logdrv = ldrv_num;
738 * A little hack: 2nd bit is zero for all scsi read
739 * commands and is set for all scsi write commands
741 if( adapter->has_64bit_addr ) {
742 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
743 MEGA_MBOXCMD_LWRITE64:
744 MEGA_MBOXCMD_LREAD64 ;
746 else {
747 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
748 MEGA_MBOXCMD_LWRITE:
749 MEGA_MBOXCMD_LREAD ;
753 * 6-byte READ(0x08) or WRITE(0x0A) cdb
755 if( cmd->cmd_len == 6 ) {
756 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
757 mbox->m_out.lba =
758 ((u32)cmd->cmnd[1] << 16) |
759 ((u32)cmd->cmnd[2] << 8) |
760 (u32)cmd->cmnd[3];
762 mbox->m_out.lba &= 0x1FFFFF;
764 #if MEGA_HAVE_STATS
766 * Take modulo 0x80, since the logical drive
767 * number increases by 0x80 when a logical
768 * drive was deleted
770 if (*cmd->cmnd == READ_6) {
771 adapter->nreads[ldrv_num%0x80]++;
772 adapter->nreadblocks[ldrv_num%0x80] +=
773 mbox->m_out.numsectors;
774 } else {
775 adapter->nwrites[ldrv_num%0x80]++;
776 adapter->nwriteblocks[ldrv_num%0x80] +=
777 mbox->m_out.numsectors;
779 #endif
783 * 10-byte READ(0x28) or WRITE(0x2A) cdb
785 if( cmd->cmd_len == 10 ) {
786 mbox->m_out.numsectors =
787 (u32)cmd->cmnd[8] |
788 ((u32)cmd->cmnd[7] << 8);
789 mbox->m_out.lba =
790 ((u32)cmd->cmnd[2] << 24) |
791 ((u32)cmd->cmnd[3] << 16) |
792 ((u32)cmd->cmnd[4] << 8) |
793 (u32)cmd->cmnd[5];
795 #if MEGA_HAVE_STATS
796 if (*cmd->cmnd == READ_10) {
797 adapter->nreads[ldrv_num%0x80]++;
798 adapter->nreadblocks[ldrv_num%0x80] +=
799 mbox->m_out.numsectors;
800 } else {
801 adapter->nwrites[ldrv_num%0x80]++;
802 adapter->nwriteblocks[ldrv_num%0x80] +=
803 mbox->m_out.numsectors;
805 #endif
809 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
811 if( cmd->cmd_len == 12 ) {
812 mbox->m_out.lba =
813 ((u32)cmd->cmnd[2] << 24) |
814 ((u32)cmd->cmnd[3] << 16) |
815 ((u32)cmd->cmnd[4] << 8) |
816 (u32)cmd->cmnd[5];
818 mbox->m_out.numsectors =
819 ((u32)cmd->cmnd[6] << 24) |
820 ((u32)cmd->cmnd[7] << 16) |
821 ((u32)cmd->cmnd[8] << 8) |
822 (u32)cmd->cmnd[9];
824 #if MEGA_HAVE_STATS
825 if (*cmd->cmnd == READ_12) {
826 adapter->nreads[ldrv_num%0x80]++;
827 adapter->nreadblocks[ldrv_num%0x80] +=
828 mbox->m_out.numsectors;
829 } else {
830 adapter->nwrites[ldrv_num%0x80]++;
831 adapter->nwriteblocks[ldrv_num%0x80] +=
832 mbox->m_out.numsectors;
834 #endif
838 * If it is a read command
840 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
841 scb->dma_direction = PCI_DMA_FROMDEVICE;
843 else {
844 scb->dma_direction = PCI_DMA_TODEVICE;
847 /* Calculate Scatter-Gather info */
848 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
849 (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
851 return scb;
853 #if MEGA_HAVE_CLUSTERING
854 case RESERVE: /* Fall through */
855 case RELEASE:
858 * Do we support clustering and is the support enabled
860 if( ! adapter->has_cluster ) {
862 cmd->result = (DID_BAD_TARGET << 16);
863 cmd->scsi_done(cmd);
864 return NULL;
867 /* Allocate a SCB and initialize mailbox */
868 if(!(scb = mega_allocate_scb(adapter, cmd))) {
869 *busy = 1;
870 return NULL;
873 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
874 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
875 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
877 scb->raw_mbox[3] = ldrv_num;
879 scb->dma_direction = PCI_DMA_NONE;
881 return scb;
882 #endif
884 default:
885 cmd->result = (DID_BAD_TARGET << 16);
886 cmd->scsi_done(cmd);
887 return NULL;
892 * Passthru drive commands
894 else {
895 /* Allocate a SCB and initialize passthru */
896 if(!(scb = mega_allocate_scb(adapter, cmd))) {
897 *busy = 1;
898 return NULL;
901 mbox = (mbox_t *)scb->raw_mbox;
902 memset(mbox, 0, sizeof(scb->raw_mbox));
904 if( adapter->support_ext_cdb ) {
906 epthru = mega_prepare_extpassthru(adapter, scb, cmd,
907 channel, target);
909 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
911 mbox->m_out.xferaddr = scb->epthru_dma_addr;
914 else {
916 pthru = mega_prepare_passthru(adapter, scb, cmd,
917 channel, target);
919 /* Initialize mailbox */
920 if( adapter->has_64bit_addr ) {
921 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
923 else {
924 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
927 mbox->m_out.xferaddr = scb->pthru_dma_addr;
930 return scb;
932 return NULL;
937 * mega_prepare_passthru()
938 * @adapter - pointer to our soft state
939 * @scb - our scsi control block
940 * @cmd - scsi command from the mid-layer
941 * @channel - actual channel on the controller
942 * @target - actual id on the controller.
944 * prepare a command for the scsi physical devices.
946 static mega_passthru *
947 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
948 int channel, int target)
950 mega_passthru *pthru;
952 pthru = scb->pthru;
953 memset(pthru, 0, sizeof (mega_passthru));
955 /* 0=6sec/1=60sec/2=10min/3=3hrs */
956 pthru->timeout = 2;
958 pthru->ars = 1;
959 pthru->reqsenselen = 14;
960 pthru->islogical = 0;
962 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
964 pthru->target = (adapter->flag & BOARD_40LD) ?
965 (channel << 4) | target : target;
967 pthru->cdblen = cmd->cmd_len;
968 pthru->logdrv = cmd->device->lun;
970 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
972 /* Not sure about the direction */
973 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
975 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
976 switch (cmd->cmnd[0]) {
977 case INQUIRY:
978 case READ_CAPACITY:
979 if(!(adapter->flag & (1L << cmd->device->channel))) {
981 printk(KERN_NOTICE
982 "scsi%d: scanning scsi channel %d [P%d] ",
983 adapter->host->host_no,
984 cmd->device->channel, channel);
985 printk("for physical devices.\n");
987 adapter->flag |= (1L << cmd->device->channel);
989 /* Fall through */
990 default:
991 pthru->numsgelements = mega_build_sglist(adapter, scb,
992 &pthru->dataxferaddr, &pthru->dataxferlen);
993 break;
995 return pthru;
1000 * mega_prepare_extpassthru()
1001 * @adapter - pointer to our soft state
1002 * @scb - our scsi control block
1003 * @cmd - scsi command from the mid-layer
1004 * @channel - actual channel on the controller
1005 * @target - actual id on the controller.
1007 * prepare a command for the scsi physical devices. This rountine prepares
1008 * commands for devices which can take extended CDBs (>10 bytes)
1010 static mega_ext_passthru *
1011 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1012 int channel, int target)
1014 mega_ext_passthru *epthru;
1016 epthru = scb->epthru;
1017 memset(epthru, 0, sizeof(mega_ext_passthru));
1019 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1020 epthru->timeout = 2;
1022 epthru->ars = 1;
1023 epthru->reqsenselen = 14;
1024 epthru->islogical = 0;
1026 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1027 epthru->target = (adapter->flag & BOARD_40LD) ?
1028 (channel << 4) | target : target;
1030 epthru->cdblen = cmd->cmd_len;
1031 epthru->logdrv = cmd->device->lun;
1033 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1035 /* Not sure about the direction */
1036 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1038 switch(cmd->cmnd[0]) {
1039 case INQUIRY:
1040 case READ_CAPACITY:
1041 if(!(adapter->flag & (1L << cmd->device->channel))) {
1043 printk(KERN_NOTICE
1044 "scsi%d: scanning scsi channel %d [P%d] ",
1045 adapter->host->host_no,
1046 cmd->device->channel, channel);
1047 printk("for physical devices.\n");
1049 adapter->flag |= (1L << cmd->device->channel);
1051 /* Fall through */
1052 default:
1053 epthru->numsgelements = mega_build_sglist(adapter, scb,
1054 &epthru->dataxferaddr, &epthru->dataxferlen);
1055 break;
1058 return epthru;
1061 static void
1062 __mega_runpendq(adapter_t *adapter)
1064 scb_t *scb;
1065 struct list_head *pos, *next;
1067 /* Issue any pending commands to the card */
1068 list_for_each_safe(pos, next, &adapter->pending_list) {
1070 scb = list_entry(pos, scb_t, list);
1072 if( !(scb->state & SCB_ISSUED) ) {
1074 if( issue_scb(adapter, scb) != 0 )
1075 return;
1079 return;
1084 * issue_scb()
1085 * @adapter - pointer to our soft state
1086 * @scb - scsi control block
1088 * Post a command to the card if the mailbox is available, otherwise return
1089 * busy. We also take the scb from the pending list if the mailbox is
1090 * available.
1092 static int
1093 issue_scb(adapter_t *adapter, scb_t *scb)
1095 volatile mbox64_t *mbox64 = adapter->mbox64;
1096 volatile mbox_t *mbox = adapter->mbox;
1097 unsigned int i = 0;
1099 if(unlikely(mbox->m_in.busy)) {
1100 do {
1101 udelay(1);
1102 i++;
1103 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1105 if(mbox->m_in.busy) return -1;
1108 /* Copy mailbox data into host structure */
1109 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1110 sizeof(struct mbox_out));
1112 mbox->m_out.cmdid = scb->idx; /* Set cmdid */
1113 mbox->m_in.busy = 1; /* Set busy */
1117 * Increment the pending queue counter
1119 atomic_inc(&adapter->pend_cmds);
1121 switch (mbox->m_out.cmd) {
1122 case MEGA_MBOXCMD_LREAD64:
1123 case MEGA_MBOXCMD_LWRITE64:
1124 case MEGA_MBOXCMD_PASSTHRU64:
1125 case MEGA_MBOXCMD_EXTPTHRU:
1126 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1127 mbox64->xfer_segment_hi = 0;
1128 mbox->m_out.xferaddr = 0xFFFFFFFF;
1129 break;
1130 default:
1131 mbox64->xfer_segment_lo = 0;
1132 mbox64->xfer_segment_hi = 0;
1136 * post the command
1138 scb->state |= SCB_ISSUED;
1140 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1141 mbox->m_in.poll = 0;
1142 mbox->m_in.ack = 0;
1143 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1145 else {
1146 irq_enable(adapter);
1147 issue_command(adapter);
1150 return 0;
1154 * Wait until the controller's mailbox is available
1156 static inline int
1157 mega_busywait_mbox (adapter_t *adapter)
1159 if (adapter->mbox->m_in.busy)
1160 return __mega_busywait_mbox(adapter);
1161 return 0;
1165 * issue_scb_block()
1166 * @adapter - pointer to our soft state
1167 * @raw_mbox - the mailbox
1169 * Issue a scb in synchronous and non-interrupt mode
1171 static int
1172 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1174 volatile mbox64_t *mbox64 = adapter->mbox64;
1175 volatile mbox_t *mbox = adapter->mbox;
1176 u8 byte;
1178 /* Wait until mailbox is free */
1179 if(mega_busywait_mbox (adapter))
1180 goto bug_blocked_mailbox;
1182 /* Copy mailbox data into host structure */
1183 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1184 mbox->m_out.cmdid = 0xFE;
1185 mbox->m_in.busy = 1;
1187 switch (raw_mbox[0]) {
1188 case MEGA_MBOXCMD_LREAD64:
1189 case MEGA_MBOXCMD_LWRITE64:
1190 case MEGA_MBOXCMD_PASSTHRU64:
1191 case MEGA_MBOXCMD_EXTPTHRU:
1192 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1193 mbox64->xfer_segment_hi = 0;
1194 mbox->m_out.xferaddr = 0xFFFFFFFF;
1195 break;
1196 default:
1197 mbox64->xfer_segment_lo = 0;
1198 mbox64->xfer_segment_hi = 0;
1201 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1202 mbox->m_in.poll = 0;
1203 mbox->m_in.ack = 0;
1204 mbox->m_in.numstatus = 0xFF;
1205 mbox->m_in.status = 0xFF;
1206 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1208 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1209 cpu_relax();
1211 mbox->m_in.numstatus = 0xFF;
1213 while( (volatile u8)mbox->m_in.poll != 0x77 )
1214 cpu_relax();
1216 mbox->m_in.poll = 0;
1217 mbox->m_in.ack = 0x77;
1219 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1221 while(RDINDOOR(adapter) & 0x2)
1222 cpu_relax();
1224 else {
1225 irq_disable(adapter);
1226 issue_command(adapter);
1228 while (!((byte = irq_state(adapter)) & INTR_VALID))
1229 cpu_relax();
1231 set_irq_state(adapter, byte);
1232 irq_enable(adapter);
1233 irq_ack(adapter);
1236 return mbox->m_in.status;
1238 bug_blocked_mailbox:
1239 printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1240 udelay (1000);
1241 return -1;
1246 * megaraid_isr_iomapped()
1247 * @irq - irq
1248 * @devp - pointer to our soft state
1250 * Interrupt service routine for io-mapped controllers.
1251 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1252 * and service the completed commands.
1254 static irqreturn_t
1255 megaraid_isr_iomapped(int irq, void *devp)
1257 adapter_t *adapter = devp;
1258 unsigned long flags;
1259 u8 status;
1260 u8 nstatus;
1261 u8 completed[MAX_FIRMWARE_STATUS];
1262 u8 byte;
1263 int handled = 0;
1267 * loop till F/W has more commands for us to complete.
1269 spin_lock_irqsave(&adapter->lock, flags);
1271 do {
1272 /* Check if a valid interrupt is pending */
1273 byte = irq_state(adapter);
1274 if( (byte & VALID_INTR_BYTE) == 0 ) {
1276 * No more pending commands
1278 goto out_unlock;
1280 set_irq_state(adapter, byte);
1282 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1283 == 0xFF)
1284 cpu_relax();
1285 adapter->mbox->m_in.numstatus = 0xFF;
1287 status = adapter->mbox->m_in.status;
1290 * decrement the pending queue counter
1292 atomic_sub(nstatus, &adapter->pend_cmds);
1294 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1295 nstatus);
1297 /* Acknowledge interrupt */
1298 irq_ack(adapter);
1300 mega_cmd_done(adapter, completed, nstatus, status);
1302 mega_rundoneq(adapter);
1304 handled = 1;
1306 /* Loop through any pending requests */
1307 if(atomic_read(&adapter->quiescent) == 0) {
1308 mega_runpendq(adapter);
1311 } while(1);
1313 out_unlock:
1315 spin_unlock_irqrestore(&adapter->lock, flags);
1317 return IRQ_RETVAL(handled);
1322 * megaraid_isr_memmapped()
1323 * @irq - irq
1324 * @devp - pointer to our soft state
1326 * Interrupt service routine for memory-mapped controllers.
1327 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1328 * and service the completed commands.
1330 static irqreturn_t
1331 megaraid_isr_memmapped(int irq, void *devp)
1333 adapter_t *adapter = devp;
1334 unsigned long flags;
1335 u8 status;
1336 u32 dword = 0;
1337 u8 nstatus;
1338 u8 completed[MAX_FIRMWARE_STATUS];
1339 int handled = 0;
1343 * loop till F/W has more commands for us to complete.
1345 spin_lock_irqsave(&adapter->lock, flags);
1347 do {
1348 /* Check if a valid interrupt is pending */
1349 dword = RDOUTDOOR(adapter);
1350 if(dword != 0x10001234) {
1352 * No more pending commands
1354 goto out_unlock;
1356 WROUTDOOR(adapter, 0x10001234);
1358 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1359 == 0xFF) {
1360 cpu_relax();
1362 adapter->mbox->m_in.numstatus = 0xFF;
1364 status = adapter->mbox->m_in.status;
1367 * decrement the pending queue counter
1369 atomic_sub(nstatus, &adapter->pend_cmds);
1371 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1372 nstatus);
1374 /* Acknowledge interrupt */
1375 WRINDOOR(adapter, 0x2);
1377 handled = 1;
1379 while( RDINDOOR(adapter) & 0x02 )
1380 cpu_relax();
1382 mega_cmd_done(adapter, completed, nstatus, status);
1384 mega_rundoneq(adapter);
1386 /* Loop through any pending requests */
1387 if(atomic_read(&adapter->quiescent) == 0) {
1388 mega_runpendq(adapter);
1391 } while(1);
1393 out_unlock:
1395 spin_unlock_irqrestore(&adapter->lock, flags);
1397 return IRQ_RETVAL(handled);
1400 * mega_cmd_done()
1401 * @adapter - pointer to our soft state
1402 * @completed - array of ids of completed commands
1403 * @nstatus - number of completed commands
1404 * @status - status of the last command completed
1406 * Complete the comamnds and call the scsi mid-layer callback hooks.
1408 static void
1409 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1411 mega_ext_passthru *epthru = NULL;
1412 struct scatterlist *sgl;
1413 Scsi_Cmnd *cmd = NULL;
1414 mega_passthru *pthru = NULL;
1415 mbox_t *mbox = NULL;
1416 u8 c;
1417 scb_t *scb;
1418 int islogical;
1419 int cmdid;
1420 int i;
1423 * for all the commands completed, call the mid-layer callback routine
1424 * and free the scb.
1426 for( i = 0; i < nstatus; i++ ) {
1428 cmdid = completed[i];
1430 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1431 scb = &adapter->int_scb;
1432 cmd = scb->cmd;
1433 mbox = (mbox_t *)scb->raw_mbox;
1436 * Internal command interface do not fire the extended
1437 * passthru or 64-bit passthru
1439 pthru = scb->pthru;
1442 else {
1443 scb = &adapter->scb_list[cmdid];
1446 * Make sure f/w has completed a valid command
1448 if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1449 printk(KERN_CRIT
1450 "megaraid: invalid command ");
1451 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1452 cmdid, scb->state, scb->cmd);
1454 continue;
1458 * Was a abort issued for this command
1460 if( scb->state & SCB_ABORT ) {
1462 printk(KERN_WARNING
1463 "megaraid: aborted cmd %lx[%x] complete.\n",
1464 scb->cmd->serial_number, scb->idx);
1466 scb->cmd->result = (DID_ABORT << 16);
1468 list_add_tail(SCSI_LIST(scb->cmd),
1469 &adapter->completed_list);
1471 mega_free_scb(adapter, scb);
1473 continue;
1477 * Was a reset issued for this command
1479 if( scb->state & SCB_RESET ) {
1481 printk(KERN_WARNING
1482 "megaraid: reset cmd %lx[%x] complete.\n",
1483 scb->cmd->serial_number, scb->idx);
1485 scb->cmd->result = (DID_RESET << 16);
1487 list_add_tail(SCSI_LIST(scb->cmd),
1488 &adapter->completed_list);
1490 mega_free_scb (adapter, scb);
1492 continue;
1495 cmd = scb->cmd;
1496 pthru = scb->pthru;
1497 epthru = scb->epthru;
1498 mbox = (mbox_t *)scb->raw_mbox;
1500 #if MEGA_HAVE_STATS
1503 int logdrv = mbox->m_out.logdrv;
1505 islogical = adapter->logdrv_chan[cmd->channel];
1507 * Maintain an error counter for the logical drive.
1508 * Some application like SNMP agent need such
1509 * statistics
1511 if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1512 cmd->cmnd[0] == READ_10 ||
1513 cmd->cmnd[0] == READ_12)) {
1515 * Logical drive number increases by 0x80 when
1516 * a logical drive is deleted
1518 adapter->rd_errors[logdrv%0x80]++;
1521 if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1522 cmd->cmnd[0] == WRITE_10 ||
1523 cmd->cmnd[0] == WRITE_12)) {
1525 * Logical drive number increases by 0x80 when
1526 * a logical drive is deleted
1528 adapter->wr_errors[logdrv%0x80]++;
1532 #endif
1536 * Do not return the presence of hard disk on the channel so,
1537 * inquiry sent, and returned data==hard disk or removable
1538 * hard disk and not logical, request should return failure! -
1539 * PJ
1541 islogical = adapter->logdrv_chan[cmd->device->channel];
1542 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1544 sgl = scsi_sglist(cmd);
1545 if( sgl->page ) {
1546 c = *(unsigned char *)
1547 page_address((&sgl[0])->page) +
1548 (&sgl[0])->offset;
1549 } else {
1550 printk(KERN_WARNING
1551 "megaraid: invalid sg.\n");
1552 c = 0;
1555 if(IS_RAID_CH(adapter, cmd->device->channel) &&
1556 ((c & 0x1F ) == TYPE_DISK)) {
1557 status = 0xF0;
1561 /* clear result; otherwise, success returns corrupt value */
1562 cmd->result = 0;
1564 /* Convert MegaRAID status to Linux error code */
1565 switch (status) {
1566 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1567 cmd->result |= (DID_OK << 16);
1568 break;
1570 case 0x02: /* ERROR_ABORTED, i.e.
1571 SCSI_STATUS_CHECK_CONDITION */
1573 /* set sense_buffer and result fields */
1574 if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1575 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1577 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1578 14);
1580 cmd->result = (DRIVER_SENSE << 24) |
1581 (DID_OK << 16) |
1582 (CHECK_CONDITION << 1);
1584 else {
1585 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1587 memcpy(cmd->sense_buffer,
1588 epthru->reqsensearea, 14);
1590 cmd->result = (DRIVER_SENSE << 24) |
1591 (DID_OK << 16) |
1592 (CHECK_CONDITION << 1);
1593 } else {
1594 cmd->sense_buffer[0] = 0x70;
1595 cmd->sense_buffer[2] = ABORTED_COMMAND;
1596 cmd->result |= (CHECK_CONDITION << 1);
1599 break;
1601 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1602 SCSI_STATUS_BUSY */
1603 cmd->result |= (DID_BUS_BUSY << 16) | status;
1604 break;
1606 default:
1607 #if MEGA_HAVE_CLUSTERING
1609 * If TEST_UNIT_READY fails, we know
1610 * MEGA_RESERVATION_STATUS failed
1612 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1613 cmd->result |= (DID_ERROR << 16) |
1614 (RESERVATION_CONFLICT << 1);
1616 else
1618 * Error code returned is 1 if Reserve or Release
1619 * failed or the input parameter is invalid
1621 if( status == 1 &&
1622 (cmd->cmnd[0] == RESERVE ||
1623 cmd->cmnd[0] == RELEASE) ) {
1625 cmd->result |= (DID_ERROR << 16) |
1626 (RESERVATION_CONFLICT << 1);
1628 else
1629 #endif
1630 cmd->result |= (DID_BAD_TARGET << 16)|status;
1634 * Only free SCBs for the commands coming down from the
1635 * mid-layer, not for which were issued internally
1637 * For internal command, restore the status returned by the
1638 * firmware so that user can interpret it.
1640 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1641 cmd->result = status;
1644 * Remove the internal command from the pending list
1646 list_del_init(&scb->list);
1647 scb->state = SCB_FREE;
1649 else {
1650 mega_free_scb(adapter, scb);
1653 /* Add Scsi_Command to end of completed queue */
1654 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1660 * mega_runpendq()
1662 * Run through the list of completed requests and finish it
1664 static void
1665 mega_rundoneq (adapter_t *adapter)
1667 Scsi_Cmnd *cmd;
1668 struct list_head *pos;
1670 list_for_each(pos, &adapter->completed_list) {
1672 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1674 cmd = list_entry(spos, Scsi_Cmnd, SCp);
1675 cmd->scsi_done(cmd);
1678 INIT_LIST_HEAD(&adapter->completed_list);
1683 * Free a SCB structure
1684 * Note: We assume the scsi commands associated with this scb is not free yet.
1686 static void
1687 mega_free_scb(adapter_t *adapter, scb_t *scb)
1689 switch( scb->dma_type ) {
1691 case MEGA_DMA_TYPE_NONE:
1692 break;
1694 case MEGA_SGLIST:
1695 scsi_dma_unmap(scb->cmd);
1696 break;
1697 default:
1698 break;
1702 * Remove from the pending list
1704 list_del_init(&scb->list);
1706 /* Link the scb back into free list */
1707 scb->state = SCB_FREE;
1708 scb->cmd = NULL;
1710 list_add(&scb->list, &adapter->free_list);
1714 static int
1715 __mega_busywait_mbox (adapter_t *adapter)
1717 volatile mbox_t *mbox = adapter->mbox;
1718 long counter;
1720 for (counter = 0; counter < 10000; counter++) {
1721 if (!mbox->m_in.busy)
1722 return 0;
1723 udelay(100);
1724 cond_resched();
1726 return -1; /* give up after 1 second */
1730 * Copies data to SGLIST
1731 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1733 static int
1734 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1736 struct scatterlist *sg;
1737 Scsi_Cmnd *cmd;
1738 int sgcnt;
1739 int idx;
1741 cmd = scb->cmd;
1744 * Copy Scatter-Gather list info into controller structure.
1746 * The number of sg elements returned must not exceed our limit
1748 sgcnt = scsi_dma_map(cmd);
1750 scb->dma_type = MEGA_SGLIST;
1752 BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1754 *len = 0;
1756 scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1757 if (adapter->has_64bit_addr) {
1758 scb->sgl64[idx].address = sg_dma_address(sg);
1759 *len += scb->sgl64[idx].length = sg_dma_len(sg);
1760 } else {
1761 scb->sgl[idx].address = sg_dma_address(sg);
1762 *len += scb->sgl[idx].length = sg_dma_len(sg);
1766 /* Reset pointer and length fields */
1767 *buf = scb->sgl_dma_addr;
1769 /* Return count of SG requests */
1770 return sgcnt;
1775 * mega_8_to_40ld()
1777 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1778 * Enquiry3 structures for later use
1780 static void
1781 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1782 mega_product_info *product_info)
1784 int i;
1786 product_info->max_commands = inquiry->adapter_info.max_commands;
1787 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1788 product_info->nchannels = inquiry->adapter_info.nchannels;
1790 for (i = 0; i < 4; i++) {
1791 product_info->fw_version[i] =
1792 inquiry->adapter_info.fw_version[i];
1794 product_info->bios_version[i] =
1795 inquiry->adapter_info.bios_version[i];
1797 enquiry3->cache_flush_interval =
1798 inquiry->adapter_info.cache_flush_interval;
1800 product_info->dram_size = inquiry->adapter_info.dram_size;
1802 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1804 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1805 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1806 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1807 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1810 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1811 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1814 static inline void
1815 mega_free_sgl(adapter_t *adapter)
1817 scb_t *scb;
1818 int i;
1820 for(i = 0; i < adapter->max_cmds; i++) {
1822 scb = &adapter->scb_list[i];
1824 if( scb->sgl64 ) {
1825 pci_free_consistent(adapter->dev,
1826 sizeof(mega_sgl64) * adapter->sglen,
1827 scb->sgl64,
1828 scb->sgl_dma_addr);
1830 scb->sgl64 = NULL;
1833 if( scb->pthru ) {
1834 pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1835 scb->pthru, scb->pthru_dma_addr);
1837 scb->pthru = NULL;
1840 if( scb->epthru ) {
1841 pci_free_consistent(adapter->dev,
1842 sizeof(mega_ext_passthru),
1843 scb->epthru, scb->epthru_dma_addr);
1845 scb->epthru = NULL;
1853 * Get information about the card/driver
1855 const char *
1856 megaraid_info(struct Scsi_Host *host)
1858 static char buffer[512];
1859 adapter_t *adapter;
1861 adapter = (adapter_t *)host->hostdata;
1863 sprintf (buffer,
1864 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1865 adapter->fw_version, adapter->product_info.max_commands,
1866 adapter->host->max_id, adapter->host->max_channel,
1867 adapter->host->max_lun);
1868 return buffer;
1872 * Abort a previous SCSI request. Only commands on the pending list can be
1873 * aborted. All the commands issued to the F/W must complete.
1875 static int
1876 megaraid_abort(Scsi_Cmnd *cmd)
1878 adapter_t *adapter;
1879 int rval;
1881 adapter = (adapter_t *)cmd->device->host->hostdata;
1883 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1886 * This is required here to complete any completed requests
1887 * to be communicated over to the mid layer.
1889 mega_rundoneq(adapter);
1891 return rval;
1895 static int
1896 megaraid_reset(struct scsi_cmnd *cmd)
1898 adapter_t *adapter;
1899 megacmd_t mc;
1900 int rval;
1902 adapter = (adapter_t *)cmd->device->host->hostdata;
1904 #if MEGA_HAVE_CLUSTERING
1905 mc.cmd = MEGA_CLUSTER_CMD;
1906 mc.opcode = MEGA_RESET_RESERVATIONS;
1908 if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1909 printk(KERN_WARNING
1910 "megaraid: reservation reset failed.\n");
1912 else {
1913 printk(KERN_INFO "megaraid: reservation reset.\n");
1915 #endif
1917 spin_lock_irq(&adapter->lock);
1919 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1922 * This is required here to complete any completed requests
1923 * to be communicated over to the mid layer.
1925 mega_rundoneq(adapter);
1926 spin_unlock_irq(&adapter->lock);
1928 return rval;
1932 * megaraid_abort_and_reset()
1933 * @adapter - megaraid soft state
1934 * @cmd - scsi command to be aborted or reset
1935 * @aor - abort or reset flag
1937 * Try to locate the scsi command in the pending queue. If found and is not
1938 * issued to the controller, abort/reset it. Otherwise return failure
1940 static int
1941 megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
1943 struct list_head *pos, *next;
1944 scb_t *scb;
1946 printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
1947 (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
1948 cmd->cmnd[0], cmd->device->channel,
1949 cmd->device->id, cmd->device->lun);
1951 if(list_empty(&adapter->pending_list))
1952 return FALSE;
1954 list_for_each_safe(pos, next, &adapter->pending_list) {
1956 scb = list_entry(pos, scb_t, list);
1958 if (scb->cmd == cmd) { /* Found command */
1960 scb->state |= aor;
1963 * Check if this command has firmare owenership. If
1964 * yes, we cannot reset this command. Whenever, f/w
1965 * completes this command, we will return appropriate
1966 * status from ISR.
1968 if( scb->state & SCB_ISSUED ) {
1970 printk(KERN_WARNING
1971 "megaraid: %s-%lx[%x], fw owner.\n",
1972 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1973 cmd->serial_number, scb->idx);
1975 return FALSE;
1977 else {
1980 * Not yet issued! Remove from the pending
1981 * list
1983 printk(KERN_WARNING
1984 "megaraid: %s-%lx[%x], driver owner.\n",
1985 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1986 cmd->serial_number, scb->idx);
1988 mega_free_scb(adapter, scb);
1990 if( aor == SCB_ABORT ) {
1991 cmd->result = (DID_ABORT << 16);
1993 else {
1994 cmd->result = (DID_RESET << 16);
1997 list_add_tail(SCSI_LIST(cmd),
1998 &adapter->completed_list);
2000 return TRUE;
2005 return FALSE;
2008 static inline int
2009 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2011 *pdev = alloc_pci_dev();
2013 if( *pdev == NULL ) return -1;
2015 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2017 if( pci_set_dma_mask(*pdev, DMA_32BIT_MASK) != 0 ) {
2018 kfree(*pdev);
2019 return -1;
2022 return 0;
2025 static inline void
2026 free_local_pdev(struct pci_dev *pdev)
2028 kfree(pdev);
2032 * mega_allocate_inquiry()
2033 * @dma_handle - handle returned for dma address
2034 * @pdev - handle to pci device
2036 * allocates memory for inquiry structure
2038 static inline void *
2039 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2041 return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2045 static inline void
2046 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2048 pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2052 #ifdef CONFIG_PROC_FS
2053 /* Following code handles /proc fs */
2055 #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
2056 S_IRUSR | S_IFREG, \
2057 controller_proc_dir_entry, \
2058 func, adapter)
2061 * mega_create_proc_entry()
2062 * @index - index in soft state array
2063 * @parent - parent node for this /proc entry
2065 * Creates /proc entries for our controllers.
2067 static void
2068 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2070 struct proc_dir_entry *controller_proc_dir_entry = NULL;
2071 u8 string[64] = { 0 };
2072 adapter_t *adapter = hba_soft_state[index];
2074 sprintf(string, "hba%d", adapter->host->host_no);
2076 controller_proc_dir_entry =
2077 adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
2079 if(!controller_proc_dir_entry) {
2080 printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2081 return;
2083 adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
2084 adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
2085 adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
2086 #if MEGA_HAVE_ENH_PROC
2087 adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
2088 adapter->proc_battery = CREATE_READ_PROC("battery-status",
2089 proc_battery);
2092 * Display each physical drive on its channel
2094 adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
2095 proc_pdrv_ch0);
2096 adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
2097 proc_pdrv_ch1);
2098 adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
2099 proc_pdrv_ch2);
2100 adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
2101 proc_pdrv_ch3);
2104 * Display a set of up to 10 logical drive through each of following
2105 * /proc entries
2107 adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
2108 proc_rdrv_10);
2109 adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
2110 proc_rdrv_20);
2111 adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
2112 proc_rdrv_30);
2113 adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
2114 proc_rdrv_40);
2115 #endif
2120 * proc_read_config()
2121 * @page - buffer to write the data in
2122 * @start - where the actual data has been written in page
2123 * @offset - same meaning as the read system call
2124 * @count - same meaning as the read system call
2125 * @eof - set if no more data needs to be returned
2126 * @data - pointer to our soft state
2128 * Display configuration information about the controller.
2130 static int
2131 proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
2132 void *data)
2135 adapter_t *adapter = (adapter_t *)data;
2136 int len = 0;
2138 len += sprintf(page+len, "%s", MEGARAID_VERSION);
2140 if(adapter->product_info.product_name[0])
2141 len += sprintf(page+len, "%s\n",
2142 adapter->product_info.product_name);
2144 len += sprintf(page+len, "Controller Type: ");
2146 if( adapter->flag & BOARD_MEMMAP ) {
2147 len += sprintf(page+len,
2148 "438/466/467/471/493/518/520/531/532\n");
2150 else {
2151 len += sprintf(page+len,
2152 "418/428/434\n");
2155 if(adapter->flag & BOARD_40LD) {
2156 len += sprintf(page+len,
2157 "Controller Supports 40 Logical Drives\n");
2160 if(adapter->flag & BOARD_64BIT) {
2161 len += sprintf(page+len,
2162 "Controller capable of 64-bit memory addressing\n");
2164 if( adapter->has_64bit_addr ) {
2165 len += sprintf(page+len,
2166 "Controller using 64-bit memory addressing\n");
2168 else {
2169 len += sprintf(page+len,
2170 "Controller is not using 64-bit memory addressing\n");
2173 len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
2174 adapter->host->irq);
2176 len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
2177 adapter->numldrv, adapter->product_info.nchannels);
2179 len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
2180 adapter->fw_version, adapter->bios_version,
2181 adapter->product_info.dram_size);
2183 len += sprintf(page+len,
2184 "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2185 adapter->product_info.max_commands, adapter->max_cmds);
2187 len += sprintf(page+len, "support_ext_cdb = %d\n",
2188 adapter->support_ext_cdb);
2189 len += sprintf(page+len, "support_random_del = %d\n",
2190 adapter->support_random_del);
2191 len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
2192 adapter->boot_ldrv_enabled);
2193 len += sprintf(page+len, "boot_ldrv = %d\n",
2194 adapter->boot_ldrv);
2195 len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
2196 adapter->boot_pdrv_enabled);
2197 len += sprintf(page+len, "boot_pdrv_ch = %d\n",
2198 adapter->boot_pdrv_ch);
2199 len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
2200 adapter->boot_pdrv_tgt);
2201 len += sprintf(page+len, "quiescent = %d\n",
2202 atomic_read(&adapter->quiescent));
2203 len += sprintf(page+len, "has_cluster = %d\n",
2204 adapter->has_cluster);
2206 len += sprintf(page+len, "\nModule Parameters:\n");
2207 len += sprintf(page+len, "max_cmd_per_lun = %d\n",
2208 max_cmd_per_lun);
2209 len += sprintf(page+len, "max_sectors_per_io = %d\n",
2210 max_sectors_per_io);
2212 *eof = 1;
2214 return len;
2220 * proc_read_stat()
2221 * @page - buffer to write the data in
2222 * @start - where the actual data has been written in page
2223 * @offset - same meaning as the read system call
2224 * @count - same meaning as the read system call
2225 * @eof - set if no more data needs to be returned
2226 * @data - pointer to our soft state
2228 * Diaplay statistical information about the I/O activity.
2230 static int
2231 proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
2232 void *data)
2234 adapter_t *adapter;
2235 int len;
2236 int i;
2238 i = 0; /* avoid compilation warnings */
2239 len = 0;
2240 adapter = (adapter_t *)data;
2242 len = sprintf(page, "Statistical Information for this controller\n");
2243 len += sprintf(page+len, "pend_cmds = %d\n",
2244 atomic_read(&adapter->pend_cmds));
2245 #if MEGA_HAVE_STATS
2246 for(i = 0; i < adapter->numldrv; i++) {
2247 len += sprintf(page+len, "Logical Drive %d:\n", i);
2249 len += sprintf(page+len,
2250 "\tReads Issued = %lu, Writes Issued = %lu\n",
2251 adapter->nreads[i], adapter->nwrites[i]);
2253 len += sprintf(page+len,
2254 "\tSectors Read = %lu, Sectors Written = %lu\n",
2255 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2257 len += sprintf(page+len,
2258 "\tRead errors = %lu, Write errors = %lu\n\n",
2259 adapter->rd_errors[i], adapter->wr_errors[i]);
2261 #else
2262 len += sprintf(page+len,
2263 "IO and error counters not compiled in driver.\n");
2264 #endif
2266 *eof = 1;
2268 return len;
2273 * proc_read_mbox()
2274 * @page - buffer to write the data in
2275 * @start - where the actual data has been written in page
2276 * @offset - same meaning as the read system call
2277 * @count - same meaning as the read system call
2278 * @eof - set if no more data needs to be returned
2279 * @data - pointer to our soft state
2281 * Display mailbox information for the last command issued. This information
2282 * is good for debugging.
2284 static int
2285 proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
2286 void *data)
2289 adapter_t *adapter = (adapter_t *)data;
2290 volatile mbox_t *mbox = adapter->mbox;
2291 int len = 0;
2293 len = sprintf(page, "Contents of Mail Box Structure\n");
2294 len += sprintf(page+len, " Fw Command = 0x%02x\n",
2295 mbox->m_out.cmd);
2296 len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
2297 mbox->m_out.cmdid);
2298 len += sprintf(page+len, " No of Sectors= %04d\n",
2299 mbox->m_out.numsectors);
2300 len += sprintf(page+len, " LBA = 0x%02x\n",
2301 mbox->m_out.lba);
2302 len += sprintf(page+len, " DTA = 0x%08x\n",
2303 mbox->m_out.xferaddr);
2304 len += sprintf(page+len, " Logical Drive= 0x%02x\n",
2305 mbox->m_out.logdrv);
2306 len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
2307 mbox->m_out.numsgelements);
2308 len += sprintf(page+len, " Busy = %01x\n",
2309 mbox->m_in.busy);
2310 len += sprintf(page+len, " Status = 0x%02x\n",
2311 mbox->m_in.status);
2313 *eof = 1;
2315 return len;
2320 * proc_rebuild_rate()
2321 * @page - buffer to write the data in
2322 * @start - where the actual data has been written in page
2323 * @offset - same meaning as the read system call
2324 * @count - same meaning as the read system call
2325 * @eof - set if no more data needs to be returned
2326 * @data - pointer to our soft state
2328 * Display current rebuild rate
2330 static int
2331 proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
2332 void *data)
2334 adapter_t *adapter = (adapter_t *)data;
2335 dma_addr_t dma_handle;
2336 caddr_t inquiry;
2337 struct pci_dev *pdev;
2338 int len = 0;
2340 if( make_local_pdev(adapter, &pdev) != 0 ) {
2341 *eof = 1;
2342 return len;
2345 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2346 free_local_pdev(pdev);
2347 *eof = 1;
2348 return len;
2351 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2353 len = sprintf(page, "Adapter inquiry failed.\n");
2355 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2357 mega_free_inquiry(inquiry, dma_handle, pdev);
2359 free_local_pdev(pdev);
2361 *eof = 1;
2363 return len;
2366 if( adapter->flag & BOARD_40LD ) {
2367 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2368 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2370 else {
2371 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2372 ((mraid_ext_inquiry *)
2373 inquiry)->raid_inq.adapter_info.rebuild_rate);
2377 mega_free_inquiry(inquiry, dma_handle, pdev);
2379 free_local_pdev(pdev);
2381 *eof = 1;
2383 return len;
2388 * proc_battery()
2389 * @page - buffer to write the data in
2390 * @start - where the actual data has been written in page
2391 * @offset - same meaning as the read system call
2392 * @count - same meaning as the read system call
2393 * @eof - set if no more data needs to be returned
2394 * @data - pointer to our soft state
2396 * Display information about the battery module on the controller.
2398 static int
2399 proc_battery(char *page, char **start, off_t offset, int count, int *eof,
2400 void *data)
2402 adapter_t *adapter = (adapter_t *)data;
2403 dma_addr_t dma_handle;
2404 caddr_t inquiry;
2405 struct pci_dev *pdev;
2406 u8 battery_status = 0;
2407 char str[256];
2408 int len = 0;
2410 if( make_local_pdev(adapter, &pdev) != 0 ) {
2411 *eof = 1;
2412 return len;
2415 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2416 free_local_pdev(pdev);
2417 *eof = 1;
2418 return len;
2421 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2423 len = sprintf(page, "Adapter inquiry failed.\n");
2425 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2427 mega_free_inquiry(inquiry, dma_handle, pdev);
2429 free_local_pdev(pdev);
2431 *eof = 1;
2433 return len;
2436 if( adapter->flag & BOARD_40LD ) {
2437 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2439 else {
2440 battery_status = ((mraid_ext_inquiry *)inquiry)->
2441 raid_inq.adapter_info.battery_status;
2445 * Decode the battery status
2447 sprintf(str, "Battery Status:[%d]", battery_status);
2449 if(battery_status == MEGA_BATT_CHARGE_DONE)
2450 strcat(str, " Charge Done");
2452 if(battery_status & MEGA_BATT_MODULE_MISSING)
2453 strcat(str, " Module Missing");
2455 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2456 strcat(str, " Low Voltage");
2458 if(battery_status & MEGA_BATT_TEMP_HIGH)
2459 strcat(str, " Temperature High");
2461 if(battery_status & MEGA_BATT_PACK_MISSING)
2462 strcat(str, " Pack Missing");
2464 if(battery_status & MEGA_BATT_CHARGE_INPROG)
2465 strcat(str, " Charge In-progress");
2467 if(battery_status & MEGA_BATT_CHARGE_FAIL)
2468 strcat(str, " Charge Fail");
2470 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2471 strcat(str, " Cycles Exceeded");
2473 len = sprintf(page, "%s\n", str);
2476 mega_free_inquiry(inquiry, dma_handle, pdev);
2478 free_local_pdev(pdev);
2480 *eof = 1;
2482 return len;
2487 * proc_pdrv_ch0()
2488 * @page - buffer to write the data in
2489 * @start - where the actual data has been written in page
2490 * @offset - same meaning as the read system call
2491 * @count - same meaning as the read system call
2492 * @eof - set if no more data needs to be returned
2493 * @data - pointer to our soft state
2495 * Display information about the physical drives on physical channel 0.
2497 static int
2498 proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
2499 void *data)
2501 adapter_t *adapter = (adapter_t *)data;
2503 *eof = 1;
2505 return (proc_pdrv(adapter, page, 0));
2510 * proc_pdrv_ch1()
2511 * @page - buffer to write the data in
2512 * @start - where the actual data has been written in page
2513 * @offset - same meaning as the read system call
2514 * @count - same meaning as the read system call
2515 * @eof - set if no more data needs to be returned
2516 * @data - pointer to our soft state
2518 * Display information about the physical drives on physical channel 1.
2520 static int
2521 proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
2522 void *data)
2524 adapter_t *adapter = (adapter_t *)data;
2526 *eof = 1;
2528 return (proc_pdrv(adapter, page, 1));
2533 * proc_pdrv_ch2()
2534 * @page - buffer to write the data in
2535 * @start - where the actual data has been written in page
2536 * @offset - same meaning as the read system call
2537 * @count - same meaning as the read system call
2538 * @eof - set if no more data needs to be returned
2539 * @data - pointer to our soft state
2541 * Display information about the physical drives on physical channel 2.
2543 static int
2544 proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
2545 void *data)
2547 adapter_t *adapter = (adapter_t *)data;
2549 *eof = 1;
2551 return (proc_pdrv(adapter, page, 2));
2556 * proc_pdrv_ch3()
2557 * @page - buffer to write the data in
2558 * @start - where the actual data has been written in page
2559 * @offset - same meaning as the read system call
2560 * @count - same meaning as the read system call
2561 * @eof - set if no more data needs to be returned
2562 * @data - pointer to our soft state
2564 * Display information about the physical drives on physical channel 3.
2566 static int
2567 proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
2568 void *data)
2570 adapter_t *adapter = (adapter_t *)data;
2572 *eof = 1;
2574 return (proc_pdrv(adapter, page, 3));
2579 * proc_pdrv()
2580 * @page - buffer to write the data in
2581 * @adapter - pointer to our soft state
2583 * Display information about the physical drives.
2585 static int
2586 proc_pdrv(adapter_t *adapter, char *page, int channel)
2588 dma_addr_t dma_handle;
2589 char *scsi_inq;
2590 dma_addr_t scsi_inq_dma_handle;
2591 caddr_t inquiry;
2592 struct pci_dev *pdev;
2593 u8 *pdrv_state;
2594 u8 state;
2595 int tgt;
2596 int max_channels;
2597 int len = 0;
2598 char str[80];
2599 int i;
2601 if( make_local_pdev(adapter, &pdev) != 0 ) {
2602 return len;
2605 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2606 goto free_pdev;
2609 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2610 len = sprintf(page, "Adapter inquiry failed.\n");
2612 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2614 goto free_inquiry;
2618 scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2620 if( scsi_inq == NULL ) {
2621 len = sprintf(page, "memory not available for scsi inq.\n");
2623 goto free_inquiry;
2626 if( adapter->flag & BOARD_40LD ) {
2627 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2629 else {
2630 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2631 raid_inq.pdrv_info.pdrv_state;
2634 max_channels = adapter->product_info.nchannels;
2636 if( channel >= max_channels ) {
2637 goto free_pci;
2640 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2642 i = channel*16 + tgt;
2644 state = *(pdrv_state + i);
2646 switch( state & 0x0F ) {
2648 case PDRV_ONLINE:
2649 sprintf(str,
2650 "Channel:%2d Id:%2d State: Online",
2651 channel, tgt);
2652 break;
2654 case PDRV_FAILED:
2655 sprintf(str,
2656 "Channel:%2d Id:%2d State: Failed",
2657 channel, tgt);
2658 break;
2660 case PDRV_RBLD:
2661 sprintf(str,
2662 "Channel:%2d Id:%2d State: Rebuild",
2663 channel, tgt);
2664 break;
2666 case PDRV_HOTSPARE:
2667 sprintf(str,
2668 "Channel:%2d Id:%2d State: Hot spare",
2669 channel, tgt);
2670 break;
2672 default:
2673 sprintf(str,
2674 "Channel:%2d Id:%2d State: Un-configured",
2675 channel, tgt);
2676 break;
2681 * This interface displays inquiries for disk drives
2682 * only. Inquries for logical drives and non-disk
2683 * devices are available through /proc/scsi/scsi
2685 memset(scsi_inq, 0, 256);
2686 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2687 scsi_inq_dma_handle) ||
2688 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2689 continue;
2693 * Check for overflow. We print less than 240
2694 * characters for inquiry
2696 if( (len + 240) >= PAGE_SIZE ) break;
2698 len += sprintf(page+len, "%s.\n", str);
2700 len += mega_print_inquiry(page+len, scsi_inq);
2703 free_pci:
2704 pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2705 free_inquiry:
2706 mega_free_inquiry(inquiry, dma_handle, pdev);
2707 free_pdev:
2708 free_local_pdev(pdev);
2710 return len;
2715 * Display scsi inquiry
2717 static int
2718 mega_print_inquiry(char *page, char *scsi_inq)
2720 int len = 0;
2721 int i;
2723 len = sprintf(page, " Vendor: ");
2724 for( i = 8; i < 16; i++ ) {
2725 len += sprintf(page+len, "%c", scsi_inq[i]);
2728 len += sprintf(page+len, " Model: ");
2730 for( i = 16; i < 32; i++ ) {
2731 len += sprintf(page+len, "%c", scsi_inq[i]);
2734 len += sprintf(page+len, " Rev: ");
2736 for( i = 32; i < 36; i++ ) {
2737 len += sprintf(page+len, "%c", scsi_inq[i]);
2740 len += sprintf(page+len, "\n");
2742 i = scsi_inq[0] & 0x1f;
2744 len += sprintf(page+len, " Type: %s ", scsi_device_type(i));
2746 len += sprintf(page+len,
2747 " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
2749 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2750 len += sprintf(page+len, " CCS\n");
2751 else
2752 len += sprintf(page+len, "\n");
2754 return len;
2759 * proc_rdrv_10()
2760 * @page - buffer to write the data in
2761 * @start - where the actual data has been written in page
2762 * @offset - same meaning as the read system call
2763 * @count - same meaning as the read system call
2764 * @eof - set if no more data needs to be returned
2765 * @data - pointer to our soft state
2767 * Display real time information about the logical drives 0 through 9.
2769 static int
2770 proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
2771 void *data)
2773 adapter_t *adapter = (adapter_t *)data;
2775 *eof = 1;
2777 return (proc_rdrv(adapter, page, 0, 9));
2782 * proc_rdrv_20()
2783 * @page - buffer to write the data in
2784 * @start - where the actual data has been written in page
2785 * @offset - same meaning as the read system call
2786 * @count - same meaning as the read system call
2787 * @eof - set if no more data needs to be returned
2788 * @data - pointer to our soft state
2790 * Display real time information about the logical drives 0 through 9.
2792 static int
2793 proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
2794 void *data)
2796 adapter_t *adapter = (adapter_t *)data;
2798 *eof = 1;
2800 return (proc_rdrv(adapter, page, 10, 19));
2805 * proc_rdrv_30()
2806 * @page - buffer to write the data in
2807 * @start - where the actual data has been written in page
2808 * @offset - same meaning as the read system call
2809 * @count - same meaning as the read system call
2810 * @eof - set if no more data needs to be returned
2811 * @data - pointer to our soft state
2813 * Display real time information about the logical drives 0 through 9.
2815 static int
2816 proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
2817 void *data)
2819 adapter_t *adapter = (adapter_t *)data;
2821 *eof = 1;
2823 return (proc_rdrv(adapter, page, 20, 29));
2828 * proc_rdrv_40()
2829 * @page - buffer to write the data in
2830 * @start - where the actual data has been written in page
2831 * @offset - same meaning as the read system call
2832 * @count - same meaning as the read system call
2833 * @eof - set if no more data needs to be returned
2834 * @data - pointer to our soft state
2836 * Display real time information about the logical drives 0 through 9.
2838 static int
2839 proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
2840 void *data)
2842 adapter_t *adapter = (adapter_t *)data;
2844 *eof = 1;
2846 return (proc_rdrv(adapter, page, 30, 39));
2851 * proc_rdrv()
2852 * @page - buffer to write the data in
2853 * @adapter - pointer to our soft state
2854 * @start - starting logical drive to display
2855 * @end - ending logical drive to display
2857 * We do not print the inquiry information since its already available through
2858 * /proc/scsi/scsi interface
2860 static int
2861 proc_rdrv(adapter_t *adapter, char *page, int start, int end )
2863 dma_addr_t dma_handle;
2864 logdrv_param *lparam;
2865 megacmd_t mc;
2866 char *disk_array;
2867 dma_addr_t disk_array_dma_handle;
2868 caddr_t inquiry;
2869 struct pci_dev *pdev;
2870 u8 *rdrv_state;
2871 int num_ldrv;
2872 u32 array_sz;
2873 int len = 0;
2874 int i;
2876 if( make_local_pdev(adapter, &pdev) != 0 ) {
2877 return len;
2880 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2881 free_local_pdev(pdev);
2882 return len;
2885 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2887 len = sprintf(page, "Adapter inquiry failed.\n");
2889 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2891 mega_free_inquiry(inquiry, dma_handle, pdev);
2893 free_local_pdev(pdev);
2895 return len;
2898 memset(&mc, 0, sizeof(megacmd_t));
2900 if( adapter->flag & BOARD_40LD ) {
2901 array_sz = sizeof(disk_array_40ld);
2903 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2905 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2907 else {
2908 array_sz = sizeof(disk_array_8ld);
2910 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2911 raid_inq.logdrv_info.ldrv_state;
2913 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2914 raid_inq.logdrv_info.num_ldrv;
2917 disk_array = pci_alloc_consistent(pdev, array_sz,
2918 &disk_array_dma_handle);
2920 if( disk_array == NULL ) {
2921 len = sprintf(page, "memory not available.\n");
2923 mega_free_inquiry(inquiry, dma_handle, pdev);
2925 free_local_pdev(pdev);
2927 return len;
2930 mc.xferaddr = (u32)disk_array_dma_handle;
2932 if( adapter->flag & BOARD_40LD ) {
2933 mc.cmd = FC_NEW_CONFIG;
2934 mc.opcode = OP_DCMD_READ_CONFIG;
2936 if( mega_internal_command(adapter, &mc, NULL) ) {
2938 len = sprintf(page, "40LD read config failed.\n");
2940 mega_free_inquiry(inquiry, dma_handle, pdev);
2942 pci_free_consistent(pdev, array_sz, disk_array,
2943 disk_array_dma_handle);
2945 free_local_pdev(pdev);
2947 return len;
2951 else {
2952 mc.cmd = NEW_READ_CONFIG_8LD;
2954 if( mega_internal_command(adapter, &mc, NULL) ) {
2956 mc.cmd = READ_CONFIG_8LD;
2958 if( mega_internal_command(adapter, &mc,
2959 NULL) ){
2961 len = sprintf(page,
2962 "8LD read config failed.\n");
2964 mega_free_inquiry(inquiry, dma_handle, pdev);
2966 pci_free_consistent(pdev, array_sz,
2967 disk_array,
2968 disk_array_dma_handle);
2970 free_local_pdev(pdev);
2972 return len;
2977 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2979 if( adapter->flag & BOARD_40LD ) {
2980 lparam =
2981 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
2983 else {
2984 lparam =
2985 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
2989 * Check for overflow. We print less than 240 characters for
2990 * information about each logical drive.
2992 if( (len + 240) >= PAGE_SIZE ) break;
2994 len += sprintf(page+len, "Logical drive:%2d:, ", i);
2996 switch( rdrv_state[i] & 0x0F ) {
2997 case RDRV_OFFLINE:
2998 len += sprintf(page+len, "state: offline");
2999 break;
3001 case RDRV_DEGRADED:
3002 len += sprintf(page+len, "state: degraded");
3003 break;
3005 case RDRV_OPTIMAL:
3006 len += sprintf(page+len, "state: optimal");
3007 break;
3009 case RDRV_DELETED:
3010 len += sprintf(page+len, "state: deleted");
3011 break;
3013 default:
3014 len += sprintf(page+len, "state: unknown");
3015 break;
3019 * Check if check consistency or initialization is going on
3020 * for this logical drive.
3022 if( (rdrv_state[i] & 0xF0) == 0x20 ) {
3023 len += sprintf(page+len,
3024 ", check-consistency in progress");
3026 else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
3027 len += sprintf(page+len,
3028 ", initialization in progress");
3031 len += sprintf(page+len, "\n");
3033 len += sprintf(page+len, "Span depth:%3d, ",
3034 lparam->span_depth);
3036 len += sprintf(page+len, "RAID level:%3d, ",
3037 lparam->level);
3039 len += sprintf(page+len, "Stripe size:%3d, ",
3040 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
3042 len += sprintf(page+len, "Row size:%3d\n",
3043 lparam->row_size);
3046 len += sprintf(page+len, "Read Policy: ");
3048 switch(lparam->read_ahead) {
3050 case NO_READ_AHEAD:
3051 len += sprintf(page+len, "No read ahead, ");
3052 break;
3054 case READ_AHEAD:
3055 len += sprintf(page+len, "Read ahead, ");
3056 break;
3058 case ADAP_READ_AHEAD:
3059 len += sprintf(page+len, "Adaptive, ");
3060 break;
3064 len += sprintf(page+len, "Write Policy: ");
3066 switch(lparam->write_mode) {
3068 case WRMODE_WRITE_THRU:
3069 len += sprintf(page+len, "Write thru, ");
3070 break;
3072 case WRMODE_WRITE_BACK:
3073 len += sprintf(page+len, "Write back, ");
3074 break;
3077 len += sprintf(page+len, "Cache Policy: ");
3079 switch(lparam->direct_io) {
3081 case CACHED_IO:
3082 len += sprintf(page+len, "Cached IO\n\n");
3083 break;
3085 case DIRECT_IO:
3086 len += sprintf(page+len, "Direct IO\n\n");
3087 break;
3091 mega_free_inquiry(inquiry, dma_handle, pdev);
3093 pci_free_consistent(pdev, array_sz, disk_array,
3094 disk_array_dma_handle);
3096 free_local_pdev(pdev);
3098 return len;
3100 #else
3101 static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
3104 #endif
3108 * megaraid_biosparam()
3110 * Return the disk geometry for a particular disk
3112 static int
3113 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
3114 sector_t capacity, int geom[])
3116 adapter_t *adapter;
3117 unsigned char *bh;
3118 int heads;
3119 int sectors;
3120 int cylinders;
3121 int rval;
3123 /* Get pointer to host config structure */
3124 adapter = (adapter_t *)sdev->host->hostdata;
3126 if (IS_RAID_CH(adapter, sdev->channel)) {
3127 /* Default heads (64) & sectors (32) */
3128 heads = 64;
3129 sectors = 32;
3130 cylinders = (ulong)capacity / (heads * sectors);
3133 * Handle extended translation size for logical drives
3134 * > 1Gb
3136 if ((ulong)capacity >= 0x200000) {
3137 heads = 255;
3138 sectors = 63;
3139 cylinders = (ulong)capacity / (heads * sectors);
3142 /* return result */
3143 geom[0] = heads;
3144 geom[1] = sectors;
3145 geom[2] = cylinders;
3147 else {
3148 bh = scsi_bios_ptable(bdev);
3150 if( bh ) {
3151 rval = scsi_partsize(bh, capacity,
3152 &geom[2], &geom[0], &geom[1]);
3153 kfree(bh);
3154 if( rval != -1 )
3155 return rval;
3158 printk(KERN_INFO
3159 "megaraid: invalid partition on this disk on channel %d\n",
3160 sdev->channel);
3162 /* Default heads (64) & sectors (32) */
3163 heads = 64;
3164 sectors = 32;
3165 cylinders = (ulong)capacity / (heads * sectors);
3167 /* Handle extended translation size for logical drives > 1Gb */
3168 if ((ulong)capacity >= 0x200000) {
3169 heads = 255;
3170 sectors = 63;
3171 cylinders = (ulong)capacity / (heads * sectors);
3174 /* return result */
3175 geom[0] = heads;
3176 geom[1] = sectors;
3177 geom[2] = cylinders;
3180 return 0;
3184 * mega_init_scb()
3185 * @adapter - pointer to our soft state
3187 * Allocate memory for the various pointers in the scb structures:
3188 * scatter-gather list pointer, passthru and extended passthru structure
3189 * pointers.
3191 static int
3192 mega_init_scb(adapter_t *adapter)
3194 scb_t *scb;
3195 int i;
3197 for( i = 0; i < adapter->max_cmds; i++ ) {
3199 scb = &adapter->scb_list[i];
3201 scb->sgl64 = NULL;
3202 scb->sgl = NULL;
3203 scb->pthru = NULL;
3204 scb->epthru = NULL;
3207 for( i = 0; i < adapter->max_cmds; i++ ) {
3209 scb = &adapter->scb_list[i];
3211 scb->idx = i;
3213 scb->sgl64 = pci_alloc_consistent(adapter->dev,
3214 sizeof(mega_sgl64) * adapter->sglen,
3215 &scb->sgl_dma_addr);
3217 scb->sgl = (mega_sglist *)scb->sgl64;
3219 if( !scb->sgl ) {
3220 printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
3221 mega_free_sgl(adapter);
3222 return -1;
3225 scb->pthru = pci_alloc_consistent(adapter->dev,
3226 sizeof(mega_passthru),
3227 &scb->pthru_dma_addr);
3229 if( !scb->pthru ) {
3230 printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
3231 mega_free_sgl(adapter);
3232 return -1;
3235 scb->epthru = pci_alloc_consistent(adapter->dev,
3236 sizeof(mega_ext_passthru),
3237 &scb->epthru_dma_addr);
3239 if( !scb->epthru ) {
3240 printk(KERN_WARNING
3241 "Can't allocate extended passthru.\n");
3242 mega_free_sgl(adapter);
3243 return -1;
3247 scb->dma_type = MEGA_DMA_TYPE_NONE;
3250 * Link to free list
3251 * lock not required since we are loading the driver, so no
3252 * commands possible right now.
3254 scb->state = SCB_FREE;
3255 scb->cmd = NULL;
3256 list_add(&scb->list, &adapter->free_list);
3259 return 0;
3264 * megadev_open()
3265 * @inode - unused
3266 * @filep - unused
3268 * Routines for the character/ioctl interface to the driver. Find out if this
3269 * is a valid open. If yes, increment the module use count so that it cannot
3270 * be unloaded.
3272 static int
3273 megadev_open (struct inode *inode, struct file *filep)
3276 * Only allow superuser to access private ioctl interface
3278 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
3280 return 0;
3285 * megadev_ioctl()
3286 * @inode - Our device inode
3287 * @filep - unused
3288 * @cmd - ioctl command
3289 * @arg - user buffer
3291 * ioctl entry point for our private ioctl interface. We move the data in from
3292 * the user space, prepare the command (if necessary, convert the old MIMD
3293 * ioctl to new ioctl command), and issue a synchronous command to the
3294 * controller.
3296 static int
3297 megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
3298 unsigned long arg)
3300 adapter_t *adapter;
3301 nitioctl_t uioc;
3302 int adapno;
3303 int rval;
3304 mega_passthru __user *upthru; /* user address for passthru */
3305 mega_passthru *pthru; /* copy user passthru here */
3306 dma_addr_t pthru_dma_hndl;
3307 void *data = NULL; /* data to be transferred */
3308 dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
3309 megacmd_t mc;
3310 megastat_t __user *ustats;
3311 int num_ldrv;
3312 u32 uxferaddr = 0;
3313 struct pci_dev *pdev;
3315 ustats = NULL; /* avoid compilation warnings */
3316 num_ldrv = 0;
3319 * Make sure only USCSICMD are issued through this interface.
3320 * MIMD application would still fire different command.
3322 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3323 return -EINVAL;
3327 * Check and convert a possible MIMD command to NIT command.
3328 * mega_m_to_n() copies the data from the user space, so we do not
3329 * have to do it here.
3330 * NOTE: We will need some user address to copyout the data, therefore
3331 * the inteface layer will also provide us with the required user
3332 * addresses.
3334 memset(&uioc, 0, sizeof(nitioctl_t));
3335 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3336 return rval;
3339 switch( uioc.opcode ) {
3341 case GET_DRIVER_VER:
3342 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3343 return (-EFAULT);
3345 break;
3347 case GET_N_ADAP:
3348 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3349 return (-EFAULT);
3352 * Shucks. MIMD interface returns a positive value for number
3353 * of adapters. TODO: Change it to return 0 when there is no
3354 * applicatio using mimd interface.
3356 return hba_count;
3358 case GET_ADAP_INFO:
3361 * Which adapter
3363 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3364 return (-ENODEV);
3366 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3367 sizeof(struct mcontroller)) )
3368 return (-EFAULT);
3369 break;
3371 #if MEGA_HAVE_STATS
3373 case GET_STATS:
3375 * Which adapter
3377 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3378 return (-ENODEV);
3380 adapter = hba_soft_state[adapno];
3382 ustats = uioc.uioc_uaddr;
3384 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3385 return (-EFAULT);
3388 * Check for the validity of the logical drive number
3390 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3392 if( copy_to_user(ustats->nreads, adapter->nreads,
3393 num_ldrv*sizeof(u32)) )
3394 return -EFAULT;
3396 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3397 num_ldrv*sizeof(u32)) )
3398 return -EFAULT;
3400 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3401 num_ldrv*sizeof(u32)) )
3402 return -EFAULT;
3404 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3405 num_ldrv*sizeof(u32)) )
3406 return -EFAULT;
3408 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3409 num_ldrv*sizeof(u32)) )
3410 return -EFAULT;
3412 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3413 num_ldrv*sizeof(u32)) )
3414 return -EFAULT;
3416 return 0;
3418 #endif
3419 case MBOX_CMD:
3422 * Which adapter
3424 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3425 return (-ENODEV);
3427 adapter = hba_soft_state[adapno];
3430 * Deletion of logical drive is a special case. The adapter
3431 * should be quiescent before this command is issued.
3433 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3434 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3437 * Do we support this feature
3439 if( !adapter->support_random_del ) {
3440 printk(KERN_WARNING "megaraid: logdrv ");
3441 printk("delete on non-supporting F/W.\n");
3443 return (-EINVAL);
3446 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3448 if( rval == 0 ) {
3449 memset(&mc, 0, sizeof(megacmd_t));
3451 mc.status = rval;
3453 rval = mega_n_to_m((void __user *)arg, &mc);
3456 return rval;
3459 * This interface only support the regular passthru commands.
3460 * Reject extended passthru and 64-bit passthru
3462 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3463 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3465 printk(KERN_WARNING "megaraid: rejected passthru.\n");
3467 return (-EINVAL);
3471 * For all internal commands, the buffer must be allocated in
3472 * <4GB address range
3474 if( make_local_pdev(adapter, &pdev) != 0 )
3475 return -EIO;
3477 /* Is it a passthru command or a DCMD */
3478 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3479 /* Passthru commands */
3481 pthru = pci_alloc_consistent(pdev,
3482 sizeof(mega_passthru),
3483 &pthru_dma_hndl);
3485 if( pthru == NULL ) {
3486 free_local_pdev(pdev);
3487 return (-ENOMEM);
3491 * The user passthru structure
3493 upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
3496 * Copy in the user passthru here.
3498 if( copy_from_user(pthru, upthru,
3499 sizeof(mega_passthru)) ) {
3501 pci_free_consistent(pdev,
3502 sizeof(mega_passthru), pthru,
3503 pthru_dma_hndl);
3505 free_local_pdev(pdev);
3507 return (-EFAULT);
3511 * Is there a data transfer
3513 if( pthru->dataxferlen ) {
3514 data = pci_alloc_consistent(pdev,
3515 pthru->dataxferlen,
3516 &data_dma_hndl);
3518 if( data == NULL ) {
3519 pci_free_consistent(pdev,
3520 sizeof(mega_passthru),
3521 pthru,
3522 pthru_dma_hndl);
3524 free_local_pdev(pdev);
3526 return (-ENOMEM);
3530 * Save the user address and point the kernel
3531 * address at just allocated memory
3533 uxferaddr = pthru->dataxferaddr;
3534 pthru->dataxferaddr = data_dma_hndl;
3539 * Is data coming down-stream
3541 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3543 * Get the user data
3545 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3546 pthru->dataxferlen) ) {
3547 rval = (-EFAULT);
3548 goto freemem_and_return;
3552 memset(&mc, 0, sizeof(megacmd_t));
3554 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3555 mc.xferaddr = (u32)pthru_dma_hndl;
3558 * Issue the command
3560 mega_internal_command(adapter, &mc, pthru);
3562 rval = mega_n_to_m((void __user *)arg, &mc);
3564 if( rval ) goto freemem_and_return;
3568 * Is data going up-stream
3570 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3571 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3572 pthru->dataxferlen) ) {
3573 rval = (-EFAULT);
3578 * Send the request sense data also, irrespective of
3579 * whether the user has asked for it or not.
3581 if (copy_to_user(upthru->reqsensearea,
3582 pthru->reqsensearea, 14))
3583 rval = -EFAULT;
3585 freemem_and_return:
3586 if( pthru->dataxferlen ) {
3587 pci_free_consistent(pdev,
3588 pthru->dataxferlen, data,
3589 data_dma_hndl);
3592 pci_free_consistent(pdev, sizeof(mega_passthru),
3593 pthru, pthru_dma_hndl);
3595 free_local_pdev(pdev);
3597 return rval;
3599 else {
3600 /* DCMD commands */
3603 * Is there a data transfer
3605 if( uioc.xferlen ) {
3606 data = pci_alloc_consistent(pdev,
3607 uioc.xferlen, &data_dma_hndl);
3609 if( data == NULL ) {
3610 free_local_pdev(pdev);
3611 return (-ENOMEM);
3614 uxferaddr = MBOX(uioc)->xferaddr;
3618 * Is data coming down-stream
3620 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3622 * Get the user data
3624 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3625 uioc.xferlen) ) {
3627 pci_free_consistent(pdev,
3628 uioc.xferlen,
3629 data, data_dma_hndl);
3631 free_local_pdev(pdev);
3633 return (-EFAULT);
3637 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3639 mc.xferaddr = (u32)data_dma_hndl;
3642 * Issue the command
3644 mega_internal_command(adapter, &mc, NULL);
3646 rval = mega_n_to_m((void __user *)arg, &mc);
3648 if( rval ) {
3649 if( uioc.xferlen ) {
3650 pci_free_consistent(pdev,
3651 uioc.xferlen, data,
3652 data_dma_hndl);
3655 free_local_pdev(pdev);
3657 return rval;
3661 * Is data going up-stream
3663 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3664 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3665 uioc.xferlen) ) {
3667 rval = (-EFAULT);
3671 if( uioc.xferlen ) {
3672 pci_free_consistent(pdev,
3673 uioc.xferlen, data,
3674 data_dma_hndl);
3677 free_local_pdev(pdev);
3679 return rval;
3682 default:
3683 return (-EINVAL);
3686 return 0;
3690 * mega_m_to_n()
3691 * @arg - user address
3692 * @uioc - new ioctl structure
3694 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3695 * structure
3697 * Converts the older mimd ioctl structure to newer NIT structure
3699 static int
3700 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3702 struct uioctl_t uioc_mimd;
3703 char signature[8] = {0};
3704 u8 opcode;
3705 u8 subopcode;
3709 * check is the application conforms to NIT. We do not have to do much
3710 * in that case.
3711 * We exploit the fact that the signature is stored in the very
3712 * begining of the structure.
3715 if( copy_from_user(signature, arg, 7) )
3716 return (-EFAULT);
3718 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3721 * NOTE NOTE: The nit ioctl is still under flux because of
3722 * change of mailbox definition, in HPE. No applications yet
3723 * use this interface and let's not have applications use this
3724 * interface till the new specifitions are in place.
3726 return -EINVAL;
3727 #if 0
3728 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3729 return (-EFAULT);
3730 return 0;
3731 #endif
3735 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3737 * Get the user ioctl structure
3739 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3740 return (-EFAULT);
3744 * Get the opcode and subopcode for the commands
3746 opcode = uioc_mimd.ui.fcs.opcode;
3747 subopcode = uioc_mimd.ui.fcs.subopcode;
3749 switch (opcode) {
3750 case 0x82:
3752 switch (subopcode) {
3754 case MEGAIOC_QDRVRVER: /* Query driver version */
3755 uioc->opcode = GET_DRIVER_VER;
3756 uioc->uioc_uaddr = uioc_mimd.data;
3757 break;
3759 case MEGAIOC_QNADAP: /* Get # of adapters */
3760 uioc->opcode = GET_N_ADAP;
3761 uioc->uioc_uaddr = uioc_mimd.data;
3762 break;
3764 case MEGAIOC_QADAPINFO: /* Get adapter information */
3765 uioc->opcode = GET_ADAP_INFO;
3766 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3767 uioc->uioc_uaddr = uioc_mimd.data;
3768 break;
3770 default:
3771 return(-EINVAL);
3774 break;
3777 case 0x81:
3779 uioc->opcode = MBOX_CMD;
3780 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3782 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3784 uioc->xferlen = uioc_mimd.ui.fcs.length;
3786 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3787 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3789 break;
3791 case 0x80:
3793 uioc->opcode = MBOX_CMD;
3794 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3796 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3799 * Choose the xferlen bigger of input and output data
3801 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3802 uioc_mimd.outlen : uioc_mimd.inlen;
3804 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3805 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3807 break;
3809 default:
3810 return (-EINVAL);
3814 return 0;
3818 * mega_n_to_m()
3819 * @arg - user address
3820 * @mc - mailbox command
3822 * Updates the status information to the application, depending on application
3823 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3825 static int
3826 mega_n_to_m(void __user *arg, megacmd_t *mc)
3828 nitioctl_t __user *uiocp;
3829 megacmd_t __user *umc;
3830 mega_passthru __user *upthru;
3831 struct uioctl_t __user *uioc_mimd;
3832 char signature[8] = {0};
3835 * check is the application conforms to NIT.
3837 if( copy_from_user(signature, arg, 7) )
3838 return -EFAULT;
3840 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3842 uiocp = arg;
3844 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3845 return (-EFAULT);
3847 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3849 umc = MBOX_P(uiocp);
3851 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3852 return -EFAULT;
3854 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3855 return (-EFAULT);
3858 else {
3859 uioc_mimd = arg;
3861 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3862 return (-EFAULT);
3864 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3866 umc = (megacmd_t __user *)uioc_mimd->mbox;
3868 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3869 return (-EFAULT);
3871 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3872 return (-EFAULT);
3876 return 0;
3881 * MEGARAID 'FW' commands.
3885 * mega_is_bios_enabled()
3886 * @adapter - pointer to our soft state
3888 * issue command to find out if the BIOS is enabled for this controller
3890 static int
3891 mega_is_bios_enabled(adapter_t *adapter)
3893 unsigned char raw_mbox[sizeof(struct mbox_out)];
3894 mbox_t *mbox;
3895 int ret;
3897 mbox = (mbox_t *)raw_mbox;
3899 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3901 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3903 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3905 raw_mbox[0] = IS_BIOS_ENABLED;
3906 raw_mbox[2] = GET_BIOS;
3909 ret = issue_scb_block(adapter, raw_mbox);
3911 return *(char *)adapter->mega_buffer;
3916 * mega_enum_raid_scsi()
3917 * @adapter - pointer to our soft state
3919 * Find out what channels are RAID/SCSI. This information is used to
3920 * differentiate the virtual channels and physical channels and to support
3921 * ROMB feature and non-disk devices.
3923 static void
3924 mega_enum_raid_scsi(adapter_t *adapter)
3926 unsigned char raw_mbox[sizeof(struct mbox_out)];
3927 mbox_t *mbox;
3928 int i;
3930 mbox = (mbox_t *)raw_mbox;
3932 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3935 * issue command to find out what channels are raid/scsi
3937 raw_mbox[0] = CHNL_CLASS;
3938 raw_mbox[2] = GET_CHNL_CLASS;
3940 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3942 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3945 * Non-ROMB firmware fail this command, so all channels
3946 * must be shown RAID
3948 adapter->mega_ch_class = 0xFF;
3950 if(!issue_scb_block(adapter, raw_mbox)) {
3951 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3955 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
3956 if( (adapter->mega_ch_class >> i) & 0x01 ) {
3957 printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
3960 else {
3961 printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
3966 return;
3971 * mega_get_boot_drv()
3972 * @adapter - pointer to our soft state
3974 * Find out which device is the boot device. Note, any logical drive or any
3975 * phyical device (e.g., a CDROM) can be designated as a boot device.
3977 static void
3978 mega_get_boot_drv(adapter_t *adapter)
3980 struct private_bios_data *prv_bios_data;
3981 unsigned char raw_mbox[sizeof(struct mbox_out)];
3982 mbox_t *mbox;
3983 u16 cksum = 0;
3984 u8 *cksum_p;
3985 u8 boot_pdrv;
3986 int i;
3988 mbox = (mbox_t *)raw_mbox;
3990 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3992 raw_mbox[0] = BIOS_PVT_DATA;
3993 raw_mbox[2] = GET_BIOS_PVT_DATA;
3995 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3997 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3999 adapter->boot_ldrv_enabled = 0;
4000 adapter->boot_ldrv = 0;
4002 adapter->boot_pdrv_enabled = 0;
4003 adapter->boot_pdrv_ch = 0;
4004 adapter->boot_pdrv_tgt = 0;
4006 if(issue_scb_block(adapter, raw_mbox) == 0) {
4007 prv_bios_data =
4008 (struct private_bios_data *)adapter->mega_buffer;
4010 cksum = 0;
4011 cksum_p = (char *)prv_bios_data;
4012 for (i = 0; i < 14; i++ ) {
4013 cksum += (u16)(*cksum_p++);
4016 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
4019 * If MSB is set, a physical drive is set as boot
4020 * device
4022 if( prv_bios_data->boot_drv & 0x80 ) {
4023 adapter->boot_pdrv_enabled = 1;
4024 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
4025 adapter->boot_pdrv_ch = boot_pdrv / 16;
4026 adapter->boot_pdrv_tgt = boot_pdrv % 16;
4028 else {
4029 adapter->boot_ldrv_enabled = 1;
4030 adapter->boot_ldrv = prv_bios_data->boot_drv;
4038 * mega_support_random_del()
4039 * @adapter - pointer to our soft state
4041 * Find out if this controller supports random deletion and addition of
4042 * logical drives
4044 static int
4045 mega_support_random_del(adapter_t *adapter)
4047 unsigned char raw_mbox[sizeof(struct mbox_out)];
4048 mbox_t *mbox;
4049 int rval;
4051 mbox = (mbox_t *)raw_mbox;
4053 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4056 * issue command
4058 raw_mbox[0] = FC_DEL_LOGDRV;
4059 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
4061 rval = issue_scb_block(adapter, raw_mbox);
4063 return !rval;
4068 * mega_support_ext_cdb()
4069 * @adapter - pointer to our soft state
4071 * Find out if this firmware support cdblen > 10
4073 static int
4074 mega_support_ext_cdb(adapter_t *adapter)
4076 unsigned char raw_mbox[sizeof(struct mbox_out)];
4077 mbox_t *mbox;
4078 int rval;
4080 mbox = (mbox_t *)raw_mbox;
4082 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4084 * issue command to find out if controller supports extended CDBs.
4086 raw_mbox[0] = 0xA4;
4087 raw_mbox[2] = 0x16;
4089 rval = issue_scb_block(adapter, raw_mbox);
4091 return !rval;
4096 * mega_del_logdrv()
4097 * @adapter - pointer to our soft state
4098 * @logdrv - logical drive to be deleted
4100 * Delete the specified logical drive. It is the responsibility of the user
4101 * app to let the OS know about this operation.
4103 static int
4104 mega_del_logdrv(adapter_t *adapter, int logdrv)
4106 unsigned long flags;
4107 scb_t *scb;
4108 int rval;
4111 * Stop sending commands to the controller, queue them internally.
4112 * When deletion is complete, ISR will flush the queue.
4114 atomic_set(&adapter->quiescent, 1);
4117 * Wait till all the issued commands are complete and there are no
4118 * commands in the pending queue
4120 while (atomic_read(&adapter->pend_cmds) > 0 ||
4121 !list_empty(&adapter->pending_list))
4122 msleep(1000); /* sleep for 1s */
4124 rval = mega_do_del_logdrv(adapter, logdrv);
4126 spin_lock_irqsave(&adapter->lock, flags);
4129 * If delete operation was successful, add 0x80 to the logical drive
4130 * ids for commands in the pending queue.
4132 if (adapter->read_ldidmap) {
4133 struct list_head *pos;
4134 list_for_each(pos, &adapter->pending_list) {
4135 scb = list_entry(pos, scb_t, list);
4136 if (scb->pthru->logdrv < 0x80 )
4137 scb->pthru->logdrv += 0x80;
4141 atomic_set(&adapter->quiescent, 0);
4143 mega_runpendq(adapter);
4145 spin_unlock_irqrestore(&adapter->lock, flags);
4147 return rval;
4151 static int
4152 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
4154 megacmd_t mc;
4155 int rval;
4157 memset( &mc, 0, sizeof(megacmd_t));
4159 mc.cmd = FC_DEL_LOGDRV;
4160 mc.opcode = OP_DEL_LOGDRV;
4161 mc.subopcode = logdrv;
4163 rval = mega_internal_command(adapter, &mc, NULL);
4165 /* log this event */
4166 if(rval) {
4167 printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
4168 return rval;
4172 * After deleting first logical drive, the logical drives must be
4173 * addressed by adding 0x80 to the logical drive id.
4175 adapter->read_ldidmap = 1;
4177 return rval;
4182 * mega_get_max_sgl()
4183 * @adapter - pointer to our soft state
4185 * Find out the maximum number of scatter-gather elements supported by this
4186 * version of the firmware
4188 static void
4189 mega_get_max_sgl(adapter_t *adapter)
4191 unsigned char raw_mbox[sizeof(struct mbox_out)];
4192 mbox_t *mbox;
4194 mbox = (mbox_t *)raw_mbox;
4196 memset(mbox, 0, sizeof(raw_mbox));
4198 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4200 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4202 raw_mbox[0] = MAIN_MISC_OPCODE;
4203 raw_mbox[2] = GET_MAX_SG_SUPPORT;
4206 if( issue_scb_block(adapter, raw_mbox) ) {
4208 * f/w does not support this command. Choose the default value
4210 adapter->sglen = MIN_SGLIST;
4212 else {
4213 adapter->sglen = *((char *)adapter->mega_buffer);
4216 * Make sure this is not more than the resources we are
4217 * planning to allocate
4219 if ( adapter->sglen > MAX_SGLIST )
4220 adapter->sglen = MAX_SGLIST;
4223 return;
4228 * mega_support_cluster()
4229 * @adapter - pointer to our soft state
4231 * Find out if this firmware support cluster calls.
4233 static int
4234 mega_support_cluster(adapter_t *adapter)
4236 unsigned char raw_mbox[sizeof(struct mbox_out)];
4237 mbox_t *mbox;
4239 mbox = (mbox_t *)raw_mbox;
4241 memset(mbox, 0, sizeof(raw_mbox));
4243 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4245 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4248 * Try to get the initiator id. This command will succeed iff the
4249 * clustering is available on this HBA.
4251 raw_mbox[0] = MEGA_GET_TARGET_ID;
4253 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
4256 * Cluster support available. Get the initiator target id.
4257 * Tell our id to mid-layer too.
4259 adapter->this_id = *(u32 *)adapter->mega_buffer;
4260 adapter->host->this_id = adapter->this_id;
4262 return 1;
4265 return 0;
4268 #ifdef CONFIG_PROC_FS
4270 * mega_adapinq()
4271 * @adapter - pointer to our soft state
4272 * @dma_handle - DMA address of the buffer
4274 * Issue internal comamnds while interrupts are available.
4275 * We only issue direct mailbox commands from within the driver. ioctl()
4276 * interface using these routines can issue passthru commands.
4278 static int
4279 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
4281 megacmd_t mc;
4283 memset(&mc, 0, sizeof(megacmd_t));
4285 if( adapter->flag & BOARD_40LD ) {
4286 mc.cmd = FC_NEW_CONFIG;
4287 mc.opcode = NC_SUBOP_ENQUIRY3;
4288 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
4290 else {
4291 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
4294 mc.xferaddr = (u32)dma_handle;
4296 if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
4297 return -1;
4300 return 0;
4304 /** mega_internal_dev_inquiry()
4305 * @adapter - pointer to our soft state
4306 * @ch - channel for this device
4307 * @tgt - ID of this device
4308 * @buf_dma_handle - DMA address of the buffer
4310 * Issue the scsi inquiry for the specified device.
4312 static int
4313 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4314 dma_addr_t buf_dma_handle)
4316 mega_passthru *pthru;
4317 dma_addr_t pthru_dma_handle;
4318 megacmd_t mc;
4319 int rval;
4320 struct pci_dev *pdev;
4324 * For all internal commands, the buffer must be allocated in <4GB
4325 * address range
4327 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4329 pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4330 &pthru_dma_handle);
4332 if( pthru == NULL ) {
4333 free_local_pdev(pdev);
4334 return -1;
4337 pthru->timeout = 2;
4338 pthru->ars = 1;
4339 pthru->reqsenselen = 14;
4340 pthru->islogical = 0;
4342 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4344 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4346 pthru->cdblen = 6;
4348 pthru->cdb[0] = INQUIRY;
4349 pthru->cdb[1] = 0;
4350 pthru->cdb[2] = 0;
4351 pthru->cdb[3] = 0;
4352 pthru->cdb[4] = 255;
4353 pthru->cdb[5] = 0;
4356 pthru->dataxferaddr = (u32)buf_dma_handle;
4357 pthru->dataxferlen = 256;
4359 memset(&mc, 0, sizeof(megacmd_t));
4361 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4362 mc.xferaddr = (u32)pthru_dma_handle;
4364 rval = mega_internal_command(adapter, &mc, pthru);
4366 pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4367 pthru_dma_handle);
4369 free_local_pdev(pdev);
4371 return rval;
4373 #endif
4376 * mega_internal_command()
4377 * @adapter - pointer to our soft state
4378 * @mc - the mailbox command
4379 * @pthru - Passthru structure for DCDB commands
4381 * Issue the internal commands in interrupt mode.
4382 * The last argument is the address of the passthru structure if the command
4383 * to be fired is a passthru command
4385 * lockscope specifies whether the caller has already acquired the lock. Of
4386 * course, the caller must know which lock we are talking about.
4388 * Note: parameter 'pthru' is null for non-passthru commands.
4390 static int
4391 mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4393 Scsi_Cmnd *scmd;
4394 struct scsi_device *sdev;
4395 scb_t *scb;
4396 int rval;
4399 * The internal commands share one command id and hence are
4400 * serialized. This is so because we want to reserve maximum number of
4401 * available command ids for the I/O commands.
4403 mutex_lock(&adapter->int_mtx);
4405 scb = &adapter->int_scb;
4406 memset(scb, 0, sizeof(scb_t));
4408 scmd = &adapter->int_scmd;
4409 memset(scmd, 0, sizeof(Scsi_Cmnd));
4411 sdev = kmalloc(sizeof(struct scsi_device), GFP_KERNEL);
4412 memset(sdev, 0, sizeof(struct scsi_device));
4413 scmd->device = sdev;
4415 scmd->device->host = adapter->host;
4416 scmd->host_scribble = (void *)scb;
4417 scmd->cmnd[0] = MEGA_INTERNAL_CMD;
4419 scb->state |= SCB_ACTIVE;
4420 scb->cmd = scmd;
4422 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4425 * Is it a passthru command
4427 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4429 scb->pthru = pthru;
4432 scb->idx = CMDID_INT_CMDS;
4434 megaraid_queue(scmd, mega_internal_done);
4436 wait_for_completion(&adapter->int_waitq);
4438 rval = scmd->result;
4439 mc->status = scmd->result;
4440 kfree(sdev);
4443 * Print a debug message for all failed commands. Applications can use
4444 * this information.
4446 if( scmd->result && trace_level ) {
4447 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4448 mc->cmd, mc->opcode, mc->subopcode, scmd->result);
4451 mutex_unlock(&adapter->int_mtx);
4453 return rval;
4458 * mega_internal_done()
4459 * @scmd - internal scsi command
4461 * Callback routine for internal commands.
4463 static void
4464 mega_internal_done(Scsi_Cmnd *scmd)
4466 adapter_t *adapter;
4468 adapter = (adapter_t *)scmd->device->host->hostdata;
4470 complete(&adapter->int_waitq);
4475 static struct scsi_host_template megaraid_template = {
4476 .module = THIS_MODULE,
4477 .name = "MegaRAID",
4478 .proc_name = "megaraid_legacy",
4479 .info = megaraid_info,
4480 .queuecommand = megaraid_queue,
4481 .bios_param = megaraid_biosparam,
4482 .max_sectors = MAX_SECTORS_PER_IO,
4483 .can_queue = MAX_COMMANDS,
4484 .this_id = DEFAULT_INITIATOR_ID,
4485 .sg_tablesize = MAX_SGLIST,
4486 .cmd_per_lun = DEF_CMD_PER_LUN,
4487 .use_clustering = ENABLE_CLUSTERING,
4488 .eh_abort_handler = megaraid_abort,
4489 .eh_device_reset_handler = megaraid_reset,
4490 .eh_bus_reset_handler = megaraid_reset,
4491 .eh_host_reset_handler = megaraid_reset,
4494 static int __devinit
4495 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4497 struct Scsi_Host *host;
4498 adapter_t *adapter;
4499 unsigned long mega_baseport, tbase, flag = 0;
4500 u16 subsysid, subsysvid;
4501 u8 pci_bus, pci_dev_func;
4502 int irq, i, j;
4503 int error = -ENODEV;
4505 if (pci_enable_device(pdev))
4506 goto out;
4507 pci_set_master(pdev);
4509 pci_bus = pdev->bus->number;
4510 pci_dev_func = pdev->devfn;
4513 * The megaraid3 stuff reports the ID of the Intel part which is not
4514 * remotely specific to the megaraid
4516 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4517 u16 magic;
4519 * Don't fall over the Compaq management cards using the same
4520 * PCI identifier
4522 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4523 pdev->subsystem_device == 0xC000)
4524 return -ENODEV;
4525 /* Now check the magic signature byte */
4526 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4527 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4528 return -ENODEV;
4529 /* Ok it is probably a megaraid */
4533 * For these vendor and device ids, signature offsets are not
4534 * valid and 64 bit is implicit
4536 if (id->driver_data & BOARD_64BIT)
4537 flag |= BOARD_64BIT;
4538 else {
4539 u32 magic64;
4541 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4542 if (magic64 == HBA_SIGNATURE_64BIT)
4543 flag |= BOARD_64BIT;
4546 subsysvid = pdev->subsystem_vendor;
4547 subsysid = pdev->subsystem_device;
4549 printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4550 id->vendor, id->device, pci_bus);
4552 printk("slot %d:func %d\n",
4553 PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
4555 /* Read the base port and IRQ from PCI */
4556 mega_baseport = pci_resource_start(pdev, 0);
4557 irq = pdev->irq;
4559 tbase = mega_baseport;
4560 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4561 flag |= BOARD_MEMMAP;
4563 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4564 printk(KERN_WARNING "megaraid: mem region busy!\n");
4565 goto out_disable_device;
4568 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4569 if (!mega_baseport) {
4570 printk(KERN_WARNING
4571 "megaraid: could not map hba memory\n");
4572 goto out_release_region;
4574 } else {
4575 flag |= BOARD_IOMAP;
4576 mega_baseport += 0x10;
4578 if (!request_region(mega_baseport, 16, "megaraid"))
4579 goto out_disable_device;
4582 /* Initialize SCSI Host structure */
4583 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4584 if (!host)
4585 goto out_iounmap;
4587 adapter = (adapter_t *)host->hostdata;
4588 memset(adapter, 0, sizeof(adapter_t));
4590 printk(KERN_NOTICE
4591 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4592 host->host_no, mega_baseport, irq);
4594 adapter->base = mega_baseport;
4595 if (flag & BOARD_MEMMAP)
4596 adapter->mmio_base = (void __iomem *) mega_baseport;
4598 INIT_LIST_HEAD(&adapter->free_list);
4599 INIT_LIST_HEAD(&adapter->pending_list);
4600 INIT_LIST_HEAD(&adapter->completed_list);
4602 adapter->flag = flag;
4603 spin_lock_init(&adapter->lock);
4605 host->cmd_per_lun = max_cmd_per_lun;
4606 host->max_sectors = max_sectors_per_io;
4608 adapter->dev = pdev;
4609 adapter->host = host;
4611 adapter->host->irq = irq;
4613 if (flag & BOARD_MEMMAP)
4614 adapter->host->base = tbase;
4615 else {
4616 adapter->host->io_port = tbase;
4617 adapter->host->n_io_port = 16;
4620 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4623 * Allocate buffer to issue internal commands.
4625 adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4626 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4627 if (!adapter->mega_buffer) {
4628 printk(KERN_WARNING "megaraid: out of RAM.\n");
4629 goto out_host_put;
4632 adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4633 if (!adapter->scb_list) {
4634 printk(KERN_WARNING "megaraid: out of RAM.\n");
4635 goto out_free_cmd_buffer;
4638 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4639 megaraid_isr_memmapped : megaraid_isr_iomapped,
4640 IRQF_SHARED, "megaraid", adapter)) {
4641 printk(KERN_WARNING
4642 "megaraid: Couldn't register IRQ %d!\n", irq);
4643 goto out_free_scb_list;
4646 if (mega_setup_mailbox(adapter))
4647 goto out_free_irq;
4649 if (mega_query_adapter(adapter))
4650 goto out_free_mbox;
4653 * Have checks for some buggy f/w
4655 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4657 * Which firmware
4659 if (!strcmp(adapter->fw_version, "3.00") ||
4660 !strcmp(adapter->fw_version, "3.01")) {
4662 printk( KERN_WARNING
4663 "megaraid: Your card is a Dell PERC "
4664 "2/SC RAID controller with "
4665 "firmware\nmegaraid: 3.00 or 3.01. "
4666 "This driver is known to have "
4667 "corruption issues\nmegaraid: with "
4668 "those firmware versions on this "
4669 "specific card. In order\nmegaraid: "
4670 "to protect your data, please upgrade "
4671 "your firmware to version\nmegaraid: "
4672 "3.10 or later, available from the "
4673 "Dell Technical Support web\n"
4674 "megaraid: site at\nhttp://support."
4675 "dell.com/us/en/filelib/download/"
4676 "index.asp?fileid=2940\n"
4682 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4683 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4684 * support, since this firmware cannot handle 64 bit
4685 * addressing
4687 if ((subsysvid == HP_SUBSYS_VID) &&
4688 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4690 * which firmware
4692 if (!strcmp(adapter->fw_version, "H01.07") ||
4693 !strcmp(adapter->fw_version, "H01.08") ||
4694 !strcmp(adapter->fw_version, "H01.09") ) {
4695 printk(KERN_WARNING
4696 "megaraid: Firmware H.01.07, "
4697 "H.01.08, and H.01.09 on 1M/2M "
4698 "controllers\n"
4699 "megaraid: do not support 64 bit "
4700 "addressing.\nmegaraid: DISABLING "
4701 "64 bit support.\n");
4702 adapter->flag &= ~BOARD_64BIT;
4706 if (mega_is_bios_enabled(adapter))
4707 mega_hbas[hba_count].is_bios_enabled = 1;
4708 mega_hbas[hba_count].hostdata_addr = adapter;
4711 * Find out which channel is raid and which is scsi. This is
4712 * for ROMB support.
4714 mega_enum_raid_scsi(adapter);
4717 * Find out if a logical drive is set as the boot drive. If
4718 * there is one, will make that as the first logical drive.
4719 * ROMB: Do we have to boot from a physical drive. Then all
4720 * the physical drives would appear before the logical disks.
4721 * Else, all the physical drives would be exported to the mid
4722 * layer after logical drives.
4724 mega_get_boot_drv(adapter);
4726 if (adapter->boot_pdrv_enabled) {
4727 j = adapter->product_info.nchannels;
4728 for( i = 0; i < j; i++ )
4729 adapter->logdrv_chan[i] = 0;
4730 for( i = j; i < NVIRT_CHAN + j; i++ )
4731 adapter->logdrv_chan[i] = 1;
4732 } else {
4733 for (i = 0; i < NVIRT_CHAN; i++)
4734 adapter->logdrv_chan[i] = 1;
4735 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4736 adapter->logdrv_chan[i] = 0;
4737 adapter->mega_ch_class <<= NVIRT_CHAN;
4741 * Do we support random deletion and addition of logical
4742 * drives
4744 adapter->read_ldidmap = 0; /* set it after first logdrv
4745 delete cmd */
4746 adapter->support_random_del = mega_support_random_del(adapter);
4748 /* Initialize SCBs */
4749 if (mega_init_scb(adapter))
4750 goto out_free_mbox;
4753 * Reset the pending commands counter
4755 atomic_set(&adapter->pend_cmds, 0);
4758 * Reset the adapter quiescent flag
4760 atomic_set(&adapter->quiescent, 0);
4762 hba_soft_state[hba_count] = adapter;
4765 * Fill in the structure which needs to be passed back to the
4766 * application when it does an ioctl() for controller related
4767 * information.
4769 i = hba_count;
4771 mcontroller[i].base = mega_baseport;
4772 mcontroller[i].irq = irq;
4773 mcontroller[i].numldrv = adapter->numldrv;
4774 mcontroller[i].pcibus = pci_bus;
4775 mcontroller[i].pcidev = id->device;
4776 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4777 mcontroller[i].pciid = -1;
4778 mcontroller[i].pcivendor = id->vendor;
4779 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4780 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4783 /* Set the Mode of addressing to 64 bit if we can */
4784 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4785 pci_set_dma_mask(pdev, DMA_64BIT_MASK);
4786 adapter->has_64bit_addr = 1;
4787 } else {
4788 pci_set_dma_mask(pdev, DMA_32BIT_MASK);
4789 adapter->has_64bit_addr = 0;
4792 mutex_init(&adapter->int_mtx);
4793 init_completion(&adapter->int_waitq);
4795 adapter->this_id = DEFAULT_INITIATOR_ID;
4796 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4798 #if MEGA_HAVE_CLUSTERING
4800 * Is cluster support enabled on this controller
4801 * Note: In a cluster the HBAs ( the initiators ) will have
4802 * different target IDs and we cannot assume it to be 7. Call
4803 * to mega_support_cluster() will get the target ids also if
4804 * the cluster support is available
4806 adapter->has_cluster = mega_support_cluster(adapter);
4807 if (adapter->has_cluster) {
4808 printk(KERN_NOTICE
4809 "megaraid: Cluster driver, initiator id:%d\n",
4810 adapter->this_id);
4812 #endif
4814 pci_set_drvdata(pdev, host);
4816 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4818 error = scsi_add_host(host, &pdev->dev);
4819 if (error)
4820 goto out_free_mbox;
4822 scsi_scan_host(host);
4823 hba_count++;
4824 return 0;
4826 out_free_mbox:
4827 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4828 adapter->una_mbox64, adapter->una_mbox64_dma);
4829 out_free_irq:
4830 free_irq(adapter->host->irq, adapter);
4831 out_free_scb_list:
4832 kfree(adapter->scb_list);
4833 out_free_cmd_buffer:
4834 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4835 adapter->mega_buffer, adapter->buf_dma_handle);
4836 out_host_put:
4837 scsi_host_put(host);
4838 out_iounmap:
4839 if (flag & BOARD_MEMMAP)
4840 iounmap((void *)mega_baseport);
4841 out_release_region:
4842 if (flag & BOARD_MEMMAP)
4843 release_mem_region(tbase, 128);
4844 else
4845 release_region(mega_baseport, 16);
4846 out_disable_device:
4847 pci_disable_device(pdev);
4848 out:
4849 return error;
4852 static void
4853 __megaraid_shutdown(adapter_t *adapter)
4855 u_char raw_mbox[sizeof(struct mbox_out)];
4856 mbox_t *mbox = (mbox_t *)raw_mbox;
4857 int i;
4859 /* Flush adapter cache */
4860 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4861 raw_mbox[0] = FLUSH_ADAPTER;
4863 free_irq(adapter->host->irq, adapter);
4865 /* Issue a blocking (interrupts disabled) command to the card */
4866 issue_scb_block(adapter, raw_mbox);
4868 /* Flush disks cache */
4869 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4870 raw_mbox[0] = FLUSH_SYSTEM;
4872 /* Issue a blocking (interrupts disabled) command to the card */
4873 issue_scb_block(adapter, raw_mbox);
4875 if (atomic_read(&adapter->pend_cmds) > 0)
4876 printk(KERN_WARNING "megaraid: pending commands!!\n");
4879 * Have a delibrate delay to make sure all the caches are
4880 * actually flushed.
4882 for (i = 0; i <= 10; i++)
4883 mdelay(1000);
4886 static void
4887 megaraid_remove_one(struct pci_dev *pdev)
4889 struct Scsi_Host *host = pci_get_drvdata(pdev);
4890 adapter_t *adapter = (adapter_t *)host->hostdata;
4892 scsi_remove_host(host);
4894 __megaraid_shutdown(adapter);
4896 /* Free our resources */
4897 if (adapter->flag & BOARD_MEMMAP) {
4898 iounmap((void *)adapter->base);
4899 release_mem_region(adapter->host->base, 128);
4900 } else
4901 release_region(adapter->base, 16);
4903 mega_free_sgl(adapter);
4905 #ifdef CONFIG_PROC_FS
4906 if (adapter->controller_proc_dir_entry) {
4907 remove_proc_entry("stat", adapter->controller_proc_dir_entry);
4908 remove_proc_entry("config",
4909 adapter->controller_proc_dir_entry);
4910 remove_proc_entry("mailbox",
4911 adapter->controller_proc_dir_entry);
4912 #if MEGA_HAVE_ENH_PROC
4913 remove_proc_entry("rebuild-rate",
4914 adapter->controller_proc_dir_entry);
4915 remove_proc_entry("battery-status",
4916 adapter->controller_proc_dir_entry);
4918 remove_proc_entry("diskdrives-ch0",
4919 adapter->controller_proc_dir_entry);
4920 remove_proc_entry("diskdrives-ch1",
4921 adapter->controller_proc_dir_entry);
4922 remove_proc_entry("diskdrives-ch2",
4923 adapter->controller_proc_dir_entry);
4924 remove_proc_entry("diskdrives-ch3",
4925 adapter->controller_proc_dir_entry);
4927 remove_proc_entry("raiddrives-0-9",
4928 adapter->controller_proc_dir_entry);
4929 remove_proc_entry("raiddrives-10-19",
4930 adapter->controller_proc_dir_entry);
4931 remove_proc_entry("raiddrives-20-29",
4932 adapter->controller_proc_dir_entry);
4933 remove_proc_entry("raiddrives-30-39",
4934 adapter->controller_proc_dir_entry);
4935 #endif
4937 char buf[12] = { 0 };
4938 sprintf(buf, "hba%d", adapter->host->host_no);
4939 remove_proc_entry(buf, mega_proc_dir_entry);
4942 #endif
4944 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4945 adapter->mega_buffer, adapter->buf_dma_handle);
4946 kfree(adapter->scb_list);
4947 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4948 adapter->una_mbox64, adapter->una_mbox64_dma);
4950 scsi_host_put(host);
4951 pci_disable_device(pdev);
4953 hba_count--;
4956 static void
4957 megaraid_shutdown(struct pci_dev *pdev)
4959 struct Scsi_Host *host = pci_get_drvdata(pdev);
4960 adapter_t *adapter = (adapter_t *)host->hostdata;
4962 __megaraid_shutdown(adapter);
4965 static struct pci_device_id megaraid_pci_tbl[] = {
4966 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4967 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4968 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
4969 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4970 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
4971 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4972 {0,}
4974 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
4976 static struct pci_driver megaraid_pci_driver = {
4977 .name = "megaraid_legacy",
4978 .id_table = megaraid_pci_tbl,
4979 .probe = megaraid_probe_one,
4980 .remove = __devexit_p(megaraid_remove_one),
4981 .shutdown = megaraid_shutdown,
4984 static int __init megaraid_init(void)
4986 int error;
4988 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
4989 max_cmd_per_lun = MAX_CMD_PER_LUN;
4990 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
4991 max_mbox_busy_wait = MBOX_BUSY_WAIT;
4993 #ifdef CONFIG_PROC_FS
4994 mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
4995 if (!mega_proc_dir_entry) {
4996 printk(KERN_WARNING
4997 "megaraid: failed to create megaraid root\n");
4999 #endif
5000 error = pci_register_driver(&megaraid_pci_driver);
5001 if (error) {
5002 #ifdef CONFIG_PROC_FS
5003 remove_proc_entry("megaraid", &proc_root);
5004 #endif
5005 return error;
5009 * Register the driver as a character device, for applications
5010 * to access it for ioctls.
5011 * First argument (major) to register_chrdev implies a dynamic
5012 * major number allocation.
5014 major = register_chrdev(0, "megadev_legacy", &megadev_fops);
5015 if (!major) {
5016 printk(KERN_WARNING
5017 "megaraid: failed to register char device\n");
5020 return 0;
5023 static void __exit megaraid_exit(void)
5026 * Unregister the character device interface to the driver.
5028 unregister_chrdev(major, "megadev_legacy");
5030 pci_unregister_driver(&megaraid_pci_driver);
5032 #ifdef CONFIG_PROC_FS
5033 remove_proc_entry("megaraid", &proc_root);
5034 #endif
5037 module_init(megaraid_init);
5038 module_exit(megaraid_exit);
5040 /* vi: set ts=8 sw=8 tw=78: */