[SUNLANCE]: Mark sparc_lance_probe_one as __devinit.
[linux-2.6/verdex.git] / drivers / scsi / megaraid.c
blobb87bef69ba0fc28b9261350cae27795df3b72f9c
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)->base + 0x20)
77 #define RDOUTDOOR(adapter) readl((adapter)->base + 0x2C)
78 #define WRINDOOR(adapter,value) writel(value, (adapter)->base + 0x20)
79 #define WROUTDOOR(adapter,value) writel(value, (adapter)->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 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 cmd->request_buffer;
532 * We know what channels our logical drives are on - mega_find_card()
534 islogical = adapter->logdrv_chan[cmd->device->channel];
537 * The theory: If physical drive is chosen for boot, all the physical
538 * devices are exported before the logical drives, otherwise physical
539 * devices are pushed after logical drives, in which case - Kernel sees
540 * the physical devices on virtual channel which is obviously converted
541 * to actual channel on the HBA.
543 if( adapter->boot_pdrv_enabled ) {
544 if( islogical ) {
545 /* logical channel */
546 channel = cmd->device->channel -
547 adapter->product_info.nchannels;
549 else {
550 /* this is physical channel */
551 channel = cmd->device->channel;
552 target = cmd->device->id;
555 * boot from a physical disk, that disk needs to be
556 * exposed first IF both the channels are SCSI, then
557 * booting from the second channel is not allowed.
559 if( target == 0 ) {
560 target = adapter->boot_pdrv_tgt;
562 else if( target == adapter->boot_pdrv_tgt ) {
563 target = 0;
567 else {
568 if( islogical ) {
569 /* this is the logical channel */
570 channel = cmd->device->channel;
572 else {
573 /* physical channel */
574 channel = cmd->device->channel - NVIRT_CHAN;
575 target = cmd->device->id;
580 if(islogical) {
582 /* have just LUN 0 for each target on virtual channels */
583 if (cmd->device->lun) {
584 cmd->result = (DID_BAD_TARGET << 16);
585 cmd->scsi_done(cmd);
586 return NULL;
589 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
592 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
593 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
596 * max_ldrv_num increases by 0x80 if some logical drive was
597 * deleted.
599 if(adapter->read_ldidmap)
600 max_ldrv_num += 0x80;
602 if(ldrv_num > max_ldrv_num ) {
603 cmd->result = (DID_BAD_TARGET << 16);
604 cmd->scsi_done(cmd);
605 return NULL;
609 else {
610 if( cmd->device->lun > 7) {
612 * Do not support lun >7 for physically accessed
613 * devices
615 cmd->result = (DID_BAD_TARGET << 16);
616 cmd->scsi_done(cmd);
617 return NULL;
623 * Logical drive commands
626 if(islogical) {
627 switch (cmd->cmnd[0]) {
628 case TEST_UNIT_READY:
629 #if MEGA_HAVE_CLUSTERING
631 * Do we support clustering and is the support enabled
632 * If no, return success always
634 if( !adapter->has_cluster ) {
635 cmd->result = (DID_OK << 16);
636 cmd->scsi_done(cmd);
637 return NULL;
640 if(!(scb = mega_allocate_scb(adapter, cmd))) {
641 *busy = 1;
642 return NULL;
645 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
646 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
647 scb->raw_mbox[3] = ldrv_num;
649 scb->dma_direction = PCI_DMA_NONE;
651 return scb;
652 #else
653 cmd->result = (DID_OK << 16);
654 cmd->scsi_done(cmd);
655 return NULL;
656 #endif
658 case MODE_SENSE: {
659 char *buf;
661 if (cmd->use_sg) {
662 struct scatterlist *sg;
664 sg = (struct scatterlist *)cmd->request_buffer;
665 buf = kmap_atomic(sg->page, KM_IRQ0) +
666 sg->offset;
667 } else
668 buf = cmd->request_buffer;
669 memset(buf, 0, cmd->cmnd[4]);
670 if (cmd->use_sg) {
671 struct scatterlist *sg;
673 sg = (struct scatterlist *)cmd->request_buffer;
674 kunmap_atomic(buf - sg->offset, KM_IRQ0);
676 cmd->result = (DID_OK << 16);
677 cmd->scsi_done(cmd);
678 return NULL;
681 case READ_CAPACITY:
682 case INQUIRY:
684 if(!(adapter->flag & (1L << cmd->device->channel))) {
686 printk(KERN_NOTICE
687 "scsi%d: scanning scsi channel %d ",
688 adapter->host->host_no,
689 cmd->device->channel);
690 printk("for logical drives.\n");
692 adapter->flag |= (1L << cmd->device->channel);
695 /* Allocate a SCB and initialize passthru */
696 if(!(scb = mega_allocate_scb(adapter, cmd))) {
697 *busy = 1;
698 return NULL;
700 pthru = scb->pthru;
702 mbox = (mbox_t *)scb->raw_mbox;
703 memset(mbox, 0, sizeof(scb->raw_mbox));
704 memset(pthru, 0, sizeof(mega_passthru));
706 pthru->timeout = 0;
707 pthru->ars = 1;
708 pthru->reqsenselen = 14;
709 pthru->islogical = 1;
710 pthru->logdrv = ldrv_num;
711 pthru->cdblen = cmd->cmd_len;
712 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
714 if( adapter->has_64bit_addr ) {
715 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
717 else {
718 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
721 scb->dma_direction = PCI_DMA_FROMDEVICE;
723 pthru->numsgelements = mega_build_sglist(adapter, scb,
724 &pthru->dataxferaddr, &pthru->dataxferlen);
726 mbox->m_out.xferaddr = scb->pthru_dma_addr;
728 return scb;
730 case READ_6:
731 case WRITE_6:
732 case READ_10:
733 case WRITE_10:
734 case READ_12:
735 case WRITE_12:
737 /* Allocate a SCB and initialize mailbox */
738 if(!(scb = mega_allocate_scb(adapter, cmd))) {
739 *busy = 1;
740 return NULL;
742 mbox = (mbox_t *)scb->raw_mbox;
744 memset(mbox, 0, sizeof(scb->raw_mbox));
745 mbox->m_out.logdrv = ldrv_num;
748 * A little hack: 2nd bit is zero for all scsi read
749 * commands and is set for all scsi write commands
751 if( adapter->has_64bit_addr ) {
752 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
753 MEGA_MBOXCMD_LWRITE64:
754 MEGA_MBOXCMD_LREAD64 ;
756 else {
757 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
758 MEGA_MBOXCMD_LWRITE:
759 MEGA_MBOXCMD_LREAD ;
763 * 6-byte READ(0x08) or WRITE(0x0A) cdb
765 if( cmd->cmd_len == 6 ) {
766 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
767 mbox->m_out.lba =
768 ((u32)cmd->cmnd[1] << 16) |
769 ((u32)cmd->cmnd[2] << 8) |
770 (u32)cmd->cmnd[3];
772 mbox->m_out.lba &= 0x1FFFFF;
774 #if MEGA_HAVE_STATS
776 * Take modulo 0x80, since the logical drive
777 * number increases by 0x80 when a logical
778 * drive was deleted
780 if (*cmd->cmnd == READ_6) {
781 adapter->nreads[ldrv_num%0x80]++;
782 adapter->nreadblocks[ldrv_num%0x80] +=
783 mbox->m_out.numsectors;
784 } else {
785 adapter->nwrites[ldrv_num%0x80]++;
786 adapter->nwriteblocks[ldrv_num%0x80] +=
787 mbox->m_out.numsectors;
789 #endif
793 * 10-byte READ(0x28) or WRITE(0x2A) cdb
795 if( cmd->cmd_len == 10 ) {
796 mbox->m_out.numsectors =
797 (u32)cmd->cmnd[8] |
798 ((u32)cmd->cmnd[7] << 8);
799 mbox->m_out.lba =
800 ((u32)cmd->cmnd[2] << 24) |
801 ((u32)cmd->cmnd[3] << 16) |
802 ((u32)cmd->cmnd[4] << 8) |
803 (u32)cmd->cmnd[5];
805 #if MEGA_HAVE_STATS
806 if (*cmd->cmnd == READ_10) {
807 adapter->nreads[ldrv_num%0x80]++;
808 adapter->nreadblocks[ldrv_num%0x80] +=
809 mbox->m_out.numsectors;
810 } else {
811 adapter->nwrites[ldrv_num%0x80]++;
812 adapter->nwriteblocks[ldrv_num%0x80] +=
813 mbox->m_out.numsectors;
815 #endif
819 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
821 if( cmd->cmd_len == 12 ) {
822 mbox->m_out.lba =
823 ((u32)cmd->cmnd[2] << 24) |
824 ((u32)cmd->cmnd[3] << 16) |
825 ((u32)cmd->cmnd[4] << 8) |
826 (u32)cmd->cmnd[5];
828 mbox->m_out.numsectors =
829 ((u32)cmd->cmnd[6] << 24) |
830 ((u32)cmd->cmnd[7] << 16) |
831 ((u32)cmd->cmnd[8] << 8) |
832 (u32)cmd->cmnd[9];
834 #if MEGA_HAVE_STATS
835 if (*cmd->cmnd == READ_12) {
836 adapter->nreads[ldrv_num%0x80]++;
837 adapter->nreadblocks[ldrv_num%0x80] +=
838 mbox->m_out.numsectors;
839 } else {
840 adapter->nwrites[ldrv_num%0x80]++;
841 adapter->nwriteblocks[ldrv_num%0x80] +=
842 mbox->m_out.numsectors;
844 #endif
848 * If it is a read command
850 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
851 scb->dma_direction = PCI_DMA_FROMDEVICE;
853 else {
854 scb->dma_direction = PCI_DMA_TODEVICE;
857 /* Calculate Scatter-Gather info */
858 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
859 (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
861 return scb;
863 #if MEGA_HAVE_CLUSTERING
864 case RESERVE: /* Fall through */
865 case RELEASE:
868 * Do we support clustering and is the support enabled
870 if( ! adapter->has_cluster ) {
872 cmd->result = (DID_BAD_TARGET << 16);
873 cmd->scsi_done(cmd);
874 return NULL;
877 /* Allocate a SCB and initialize mailbox */
878 if(!(scb = mega_allocate_scb(adapter, cmd))) {
879 *busy = 1;
880 return NULL;
883 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
884 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
885 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
887 scb->raw_mbox[3] = ldrv_num;
889 scb->dma_direction = PCI_DMA_NONE;
891 return scb;
892 #endif
894 default:
895 cmd->result = (DID_BAD_TARGET << 16);
896 cmd->scsi_done(cmd);
897 return NULL;
902 * Passthru drive commands
904 else {
905 /* Allocate a SCB and initialize passthru */
906 if(!(scb = mega_allocate_scb(adapter, cmd))) {
907 *busy = 1;
908 return NULL;
911 mbox = (mbox_t *)scb->raw_mbox;
912 memset(mbox, 0, sizeof(scb->raw_mbox));
914 if( adapter->support_ext_cdb ) {
916 epthru = mega_prepare_extpassthru(adapter, scb, cmd,
917 channel, target);
919 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
921 mbox->m_out.xferaddr = scb->epthru_dma_addr;
924 else {
926 pthru = mega_prepare_passthru(adapter, scb, cmd,
927 channel, target);
929 /* Initialize mailbox */
930 if( adapter->has_64bit_addr ) {
931 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
933 else {
934 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
937 mbox->m_out.xferaddr = scb->pthru_dma_addr;
940 return scb;
942 return NULL;
947 * mega_prepare_passthru()
948 * @adapter - pointer to our soft state
949 * @scb - our scsi control block
950 * @cmd - scsi command from the mid-layer
951 * @channel - actual channel on the controller
952 * @target - actual id on the controller.
954 * prepare a command for the scsi physical devices.
956 static mega_passthru *
957 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
958 int channel, int target)
960 mega_passthru *pthru;
962 pthru = scb->pthru;
963 memset(pthru, 0, sizeof (mega_passthru));
965 /* 0=6sec/1=60sec/2=10min/3=3hrs */
966 pthru->timeout = 2;
968 pthru->ars = 1;
969 pthru->reqsenselen = 14;
970 pthru->islogical = 0;
972 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
974 pthru->target = (adapter->flag & BOARD_40LD) ?
975 (channel << 4) | target : target;
977 pthru->cdblen = cmd->cmd_len;
978 pthru->logdrv = cmd->device->lun;
980 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
982 /* Not sure about the direction */
983 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
985 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
986 switch (cmd->cmnd[0]) {
987 case INQUIRY:
988 case READ_CAPACITY:
989 if(!(adapter->flag & (1L << cmd->device->channel))) {
991 printk(KERN_NOTICE
992 "scsi%d: scanning scsi channel %d [P%d] ",
993 adapter->host->host_no,
994 cmd->device->channel, channel);
995 printk("for physical devices.\n");
997 adapter->flag |= (1L << cmd->device->channel);
999 /* Fall through */
1000 default:
1001 pthru->numsgelements = mega_build_sglist(adapter, scb,
1002 &pthru->dataxferaddr, &pthru->dataxferlen);
1003 break;
1005 return pthru;
1010 * mega_prepare_extpassthru()
1011 * @adapter - pointer to our soft state
1012 * @scb - our scsi control block
1013 * @cmd - scsi command from the mid-layer
1014 * @channel - actual channel on the controller
1015 * @target - actual id on the controller.
1017 * prepare a command for the scsi physical devices. This rountine prepares
1018 * commands for devices which can take extended CDBs (>10 bytes)
1020 static mega_ext_passthru *
1021 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1022 int channel, int target)
1024 mega_ext_passthru *epthru;
1026 epthru = scb->epthru;
1027 memset(epthru, 0, sizeof(mega_ext_passthru));
1029 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1030 epthru->timeout = 2;
1032 epthru->ars = 1;
1033 epthru->reqsenselen = 14;
1034 epthru->islogical = 0;
1036 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1037 epthru->target = (adapter->flag & BOARD_40LD) ?
1038 (channel << 4) | target : target;
1040 epthru->cdblen = cmd->cmd_len;
1041 epthru->logdrv = cmd->device->lun;
1043 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1045 /* Not sure about the direction */
1046 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1048 switch(cmd->cmnd[0]) {
1049 case INQUIRY:
1050 case READ_CAPACITY:
1051 if(!(adapter->flag & (1L << cmd->device->channel))) {
1053 printk(KERN_NOTICE
1054 "scsi%d: scanning scsi channel %d [P%d] ",
1055 adapter->host->host_no,
1056 cmd->device->channel, channel);
1057 printk("for physical devices.\n");
1059 adapter->flag |= (1L << cmd->device->channel);
1061 /* Fall through */
1062 default:
1063 epthru->numsgelements = mega_build_sglist(adapter, scb,
1064 &epthru->dataxferaddr, &epthru->dataxferlen);
1065 break;
1068 return epthru;
1071 static void
1072 __mega_runpendq(adapter_t *adapter)
1074 scb_t *scb;
1075 struct list_head *pos, *next;
1077 /* Issue any pending commands to the card */
1078 list_for_each_safe(pos, next, &adapter->pending_list) {
1080 scb = list_entry(pos, scb_t, list);
1082 if( !(scb->state & SCB_ISSUED) ) {
1084 if( issue_scb(adapter, scb) != 0 )
1085 return;
1089 return;
1094 * issue_scb()
1095 * @adapter - pointer to our soft state
1096 * @scb - scsi control block
1098 * Post a command to the card if the mailbox is available, otherwise return
1099 * busy. We also take the scb from the pending list if the mailbox is
1100 * available.
1102 static int
1103 issue_scb(adapter_t *adapter, scb_t *scb)
1105 volatile mbox64_t *mbox64 = adapter->mbox64;
1106 volatile mbox_t *mbox = adapter->mbox;
1107 unsigned int i = 0;
1109 if(unlikely(mbox->m_in.busy)) {
1110 do {
1111 udelay(1);
1112 i++;
1113 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1115 if(mbox->m_in.busy) return -1;
1118 /* Copy mailbox data into host structure */
1119 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1120 sizeof(struct mbox_out));
1122 mbox->m_out.cmdid = scb->idx; /* Set cmdid */
1123 mbox->m_in.busy = 1; /* Set busy */
1127 * Increment the pending queue counter
1129 atomic_inc(&adapter->pend_cmds);
1131 switch (mbox->m_out.cmd) {
1132 case MEGA_MBOXCMD_LREAD64:
1133 case MEGA_MBOXCMD_LWRITE64:
1134 case MEGA_MBOXCMD_PASSTHRU64:
1135 case MEGA_MBOXCMD_EXTPTHRU:
1136 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1137 mbox64->xfer_segment_hi = 0;
1138 mbox->m_out.xferaddr = 0xFFFFFFFF;
1139 break;
1140 default:
1141 mbox64->xfer_segment_lo = 0;
1142 mbox64->xfer_segment_hi = 0;
1146 * post the command
1148 scb->state |= SCB_ISSUED;
1150 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1151 mbox->m_in.poll = 0;
1152 mbox->m_in.ack = 0;
1153 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1155 else {
1156 irq_enable(adapter);
1157 issue_command(adapter);
1160 return 0;
1164 * Wait until the controller's mailbox is available
1166 static inline int
1167 mega_busywait_mbox (adapter_t *adapter)
1169 if (adapter->mbox->m_in.busy)
1170 return __mega_busywait_mbox(adapter);
1171 return 0;
1175 * issue_scb_block()
1176 * @adapter - pointer to our soft state
1177 * @raw_mbox - the mailbox
1179 * Issue a scb in synchronous and non-interrupt mode
1181 static int
1182 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1184 volatile mbox64_t *mbox64 = adapter->mbox64;
1185 volatile mbox_t *mbox = adapter->mbox;
1186 u8 byte;
1188 /* Wait until mailbox is free */
1189 if(mega_busywait_mbox (adapter))
1190 goto bug_blocked_mailbox;
1192 /* Copy mailbox data into host structure */
1193 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1194 mbox->m_out.cmdid = 0xFE;
1195 mbox->m_in.busy = 1;
1197 switch (raw_mbox[0]) {
1198 case MEGA_MBOXCMD_LREAD64:
1199 case MEGA_MBOXCMD_LWRITE64:
1200 case MEGA_MBOXCMD_PASSTHRU64:
1201 case MEGA_MBOXCMD_EXTPTHRU:
1202 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1203 mbox64->xfer_segment_hi = 0;
1204 mbox->m_out.xferaddr = 0xFFFFFFFF;
1205 break;
1206 default:
1207 mbox64->xfer_segment_lo = 0;
1208 mbox64->xfer_segment_hi = 0;
1211 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1212 mbox->m_in.poll = 0;
1213 mbox->m_in.ack = 0;
1214 mbox->m_in.numstatus = 0xFF;
1215 mbox->m_in.status = 0xFF;
1216 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1218 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1219 cpu_relax();
1221 mbox->m_in.numstatus = 0xFF;
1223 while( (volatile u8)mbox->m_in.poll != 0x77 )
1224 cpu_relax();
1226 mbox->m_in.poll = 0;
1227 mbox->m_in.ack = 0x77;
1229 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1231 while(RDINDOOR(adapter) & 0x2)
1232 cpu_relax();
1234 else {
1235 irq_disable(adapter);
1236 issue_command(adapter);
1238 while (!((byte = irq_state(adapter)) & INTR_VALID))
1239 cpu_relax();
1241 set_irq_state(adapter, byte);
1242 irq_enable(adapter);
1243 irq_ack(adapter);
1246 return mbox->m_in.status;
1248 bug_blocked_mailbox:
1249 printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1250 udelay (1000);
1251 return -1;
1256 * megaraid_isr_iomapped()
1257 * @irq - irq
1258 * @devp - pointer to our soft state
1259 * @regs - unused
1261 * Interrupt service routine for io-mapped controllers.
1262 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1263 * and service the completed commands.
1265 static irqreturn_t
1266 megaraid_isr_iomapped(int irq, void *devp, struct pt_regs *regs)
1268 adapter_t *adapter = devp;
1269 unsigned long flags;
1270 u8 status;
1271 u8 nstatus;
1272 u8 completed[MAX_FIRMWARE_STATUS];
1273 u8 byte;
1274 int handled = 0;
1278 * loop till F/W has more commands for us to complete.
1280 spin_lock_irqsave(&adapter->lock, flags);
1282 do {
1283 /* Check if a valid interrupt is pending */
1284 byte = irq_state(adapter);
1285 if( (byte & VALID_INTR_BYTE) == 0 ) {
1287 * No more pending commands
1289 goto out_unlock;
1291 set_irq_state(adapter, byte);
1293 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1294 == 0xFF)
1295 cpu_relax();
1296 adapter->mbox->m_in.numstatus = 0xFF;
1298 status = adapter->mbox->m_in.status;
1301 * decrement the pending queue counter
1303 atomic_sub(nstatus, &adapter->pend_cmds);
1305 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1306 nstatus);
1308 /* Acknowledge interrupt */
1309 irq_ack(adapter);
1311 mega_cmd_done(adapter, completed, nstatus, status);
1313 mega_rundoneq(adapter);
1315 handled = 1;
1317 /* Loop through any pending requests */
1318 if(atomic_read(&adapter->quiescent) == 0) {
1319 mega_runpendq(adapter);
1322 } while(1);
1324 out_unlock:
1326 spin_unlock_irqrestore(&adapter->lock, flags);
1328 return IRQ_RETVAL(handled);
1333 * megaraid_isr_memmapped()
1334 * @irq - irq
1335 * @devp - pointer to our soft state
1336 * @regs - unused
1338 * Interrupt service routine for memory-mapped controllers.
1339 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1340 * and service the completed commands.
1342 static irqreturn_t
1343 megaraid_isr_memmapped(int irq, void *devp, struct pt_regs *regs)
1345 adapter_t *adapter = devp;
1346 unsigned long flags;
1347 u8 status;
1348 u32 dword = 0;
1349 u8 nstatus;
1350 u8 completed[MAX_FIRMWARE_STATUS];
1351 int handled = 0;
1355 * loop till F/W has more commands for us to complete.
1357 spin_lock_irqsave(&adapter->lock, flags);
1359 do {
1360 /* Check if a valid interrupt is pending */
1361 dword = RDOUTDOOR(adapter);
1362 if(dword != 0x10001234) {
1364 * No more pending commands
1366 goto out_unlock;
1368 WROUTDOOR(adapter, 0x10001234);
1370 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1371 == 0xFF) {
1372 cpu_relax();
1374 adapter->mbox->m_in.numstatus = 0xFF;
1376 status = adapter->mbox->m_in.status;
1379 * decrement the pending queue counter
1381 atomic_sub(nstatus, &adapter->pend_cmds);
1383 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1384 nstatus);
1386 /* Acknowledge interrupt */
1387 WRINDOOR(adapter, 0x2);
1389 handled = 1;
1391 while( RDINDOOR(adapter) & 0x02 ) cpu_relax();
1393 mega_cmd_done(adapter, completed, nstatus, status);
1395 mega_rundoneq(adapter);
1397 /* Loop through any pending requests */
1398 if(atomic_read(&adapter->quiescent) == 0) {
1399 mega_runpendq(adapter);
1402 } while(1);
1404 out_unlock:
1406 spin_unlock_irqrestore(&adapter->lock, flags);
1408 return IRQ_RETVAL(handled);
1411 * mega_cmd_done()
1412 * @adapter - pointer to our soft state
1413 * @completed - array of ids of completed commands
1414 * @nstatus - number of completed commands
1415 * @status - status of the last command completed
1417 * Complete the comamnds and call the scsi mid-layer callback hooks.
1419 static void
1420 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1422 mega_ext_passthru *epthru = NULL;
1423 struct scatterlist *sgl;
1424 Scsi_Cmnd *cmd = NULL;
1425 mega_passthru *pthru = NULL;
1426 mbox_t *mbox = NULL;
1427 u8 c;
1428 scb_t *scb;
1429 int islogical;
1430 int cmdid;
1431 int i;
1434 * for all the commands completed, call the mid-layer callback routine
1435 * and free the scb.
1437 for( i = 0; i < nstatus; i++ ) {
1439 cmdid = completed[i];
1441 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1442 scb = &adapter->int_scb;
1443 cmd = scb->cmd;
1444 mbox = (mbox_t *)scb->raw_mbox;
1447 * Internal command interface do not fire the extended
1448 * passthru or 64-bit passthru
1450 pthru = scb->pthru;
1453 else {
1454 scb = &adapter->scb_list[cmdid];
1457 * Make sure f/w has completed a valid command
1459 if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1460 printk(KERN_CRIT
1461 "megaraid: invalid command ");
1462 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1463 cmdid, scb->state, scb->cmd);
1465 continue;
1469 * Was a abort issued for this command
1471 if( scb->state & SCB_ABORT ) {
1473 printk(KERN_WARNING
1474 "megaraid: aborted cmd %lx[%x] complete.\n",
1475 scb->cmd->serial_number, scb->idx);
1477 scb->cmd->result = (DID_ABORT << 16);
1479 list_add_tail(SCSI_LIST(scb->cmd),
1480 &adapter->completed_list);
1482 mega_free_scb(adapter, scb);
1484 continue;
1488 * Was a reset issued for this command
1490 if( scb->state & SCB_RESET ) {
1492 printk(KERN_WARNING
1493 "megaraid: reset cmd %lx[%x] complete.\n",
1494 scb->cmd->serial_number, scb->idx);
1496 scb->cmd->result = (DID_RESET << 16);
1498 list_add_tail(SCSI_LIST(scb->cmd),
1499 &adapter->completed_list);
1501 mega_free_scb (adapter, scb);
1503 continue;
1506 cmd = scb->cmd;
1507 pthru = scb->pthru;
1508 epthru = scb->epthru;
1509 mbox = (mbox_t *)scb->raw_mbox;
1511 #if MEGA_HAVE_STATS
1514 int logdrv = mbox->m_out.logdrv;
1516 islogical = adapter->logdrv_chan[cmd->channel];
1518 * Maintain an error counter for the logical drive.
1519 * Some application like SNMP agent need such
1520 * statistics
1522 if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1523 cmd->cmnd[0] == READ_10 ||
1524 cmd->cmnd[0] == READ_12)) {
1526 * Logical drive number increases by 0x80 when
1527 * a logical drive is deleted
1529 adapter->rd_errors[logdrv%0x80]++;
1532 if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1533 cmd->cmnd[0] == WRITE_10 ||
1534 cmd->cmnd[0] == WRITE_12)) {
1536 * Logical drive number increases by 0x80 when
1537 * a logical drive is deleted
1539 adapter->wr_errors[logdrv%0x80]++;
1543 #endif
1547 * Do not return the presence of hard disk on the channel so,
1548 * inquiry sent, and returned data==hard disk or removable
1549 * hard disk and not logical, request should return failure! -
1550 * PJ
1552 islogical = adapter->logdrv_chan[cmd->device->channel];
1553 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1555 if( cmd->use_sg ) {
1556 sgl = (struct scatterlist *)
1557 cmd->request_buffer;
1559 if( sgl->page ) {
1560 c = *(unsigned char *)
1561 page_address((&sgl[0])->page) +
1562 (&sgl[0])->offset;
1564 else {
1565 printk(KERN_WARNING
1566 "megaraid: invalid sg.\n");
1567 c = 0;
1570 else {
1571 c = *(u8 *)cmd->request_buffer;
1574 if(IS_RAID_CH(adapter, cmd->device->channel) &&
1575 ((c & 0x1F ) == TYPE_DISK)) {
1576 status = 0xF0;
1580 /* clear result; otherwise, success returns corrupt value */
1581 cmd->result = 0;
1583 /* Convert MegaRAID status to Linux error code */
1584 switch (status) {
1585 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1586 cmd->result |= (DID_OK << 16);
1587 break;
1589 case 0x02: /* ERROR_ABORTED, i.e.
1590 SCSI_STATUS_CHECK_CONDITION */
1592 /* set sense_buffer and result fields */
1593 if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1594 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1596 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1597 14);
1599 cmd->result = (DRIVER_SENSE << 24) |
1600 (DID_OK << 16) |
1601 (CHECK_CONDITION << 1);
1603 else {
1604 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1606 memcpy(cmd->sense_buffer,
1607 epthru->reqsensearea, 14);
1609 cmd->result = (DRIVER_SENSE << 24) |
1610 (DID_OK << 16) |
1611 (CHECK_CONDITION << 1);
1612 } else {
1613 cmd->sense_buffer[0] = 0x70;
1614 cmd->sense_buffer[2] = ABORTED_COMMAND;
1615 cmd->result |= (CHECK_CONDITION << 1);
1618 break;
1620 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1621 SCSI_STATUS_BUSY */
1622 cmd->result |= (DID_BUS_BUSY << 16) | status;
1623 break;
1625 default:
1626 #if MEGA_HAVE_CLUSTERING
1628 * If TEST_UNIT_READY fails, we know
1629 * MEGA_RESERVATION_STATUS failed
1631 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1632 cmd->result |= (DID_ERROR << 16) |
1633 (RESERVATION_CONFLICT << 1);
1635 else
1637 * Error code returned is 1 if Reserve or Release
1638 * failed or the input parameter is invalid
1640 if( status == 1 &&
1641 (cmd->cmnd[0] == RESERVE ||
1642 cmd->cmnd[0] == RELEASE) ) {
1644 cmd->result |= (DID_ERROR << 16) |
1645 (RESERVATION_CONFLICT << 1);
1647 else
1648 #endif
1649 cmd->result |= (DID_BAD_TARGET << 16)|status;
1653 * Only free SCBs for the commands coming down from the
1654 * mid-layer, not for which were issued internally
1656 * For internal command, restore the status returned by the
1657 * firmware so that user can interpret it.
1659 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1660 cmd->result = status;
1663 * Remove the internal command from the pending list
1665 list_del_init(&scb->list);
1666 scb->state = SCB_FREE;
1668 else {
1669 mega_free_scb(adapter, scb);
1672 /* Add Scsi_Command to end of completed queue */
1673 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1679 * mega_runpendq()
1681 * Run through the list of completed requests and finish it
1683 static void
1684 mega_rundoneq (adapter_t *adapter)
1686 Scsi_Cmnd *cmd;
1687 struct list_head *pos;
1689 list_for_each(pos, &adapter->completed_list) {
1691 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1693 cmd = list_entry(spos, Scsi_Cmnd, SCp);
1694 cmd->scsi_done(cmd);
1697 INIT_LIST_HEAD(&adapter->completed_list);
1702 * Free a SCB structure
1703 * Note: We assume the scsi commands associated with this scb is not free yet.
1705 static void
1706 mega_free_scb(adapter_t *adapter, scb_t *scb)
1708 unsigned long length;
1710 switch( scb->dma_type ) {
1712 case MEGA_DMA_TYPE_NONE:
1713 break;
1715 case MEGA_BULK_DATA:
1716 if (scb->cmd->use_sg == 0)
1717 length = scb->cmd->request_bufflen;
1718 else {
1719 struct scatterlist *sgl =
1720 (struct scatterlist *)scb->cmd->request_buffer;
1721 length = sgl->length;
1723 pci_unmap_page(adapter->dev, scb->dma_h_bulkdata,
1724 length, scb->dma_direction);
1725 break;
1727 case MEGA_SGLIST:
1728 pci_unmap_sg(adapter->dev, scb->cmd->request_buffer,
1729 scb->cmd->use_sg, scb->dma_direction);
1730 break;
1732 default:
1733 break;
1737 * Remove from the pending list
1739 list_del_init(&scb->list);
1741 /* Link the scb back into free list */
1742 scb->state = SCB_FREE;
1743 scb->cmd = NULL;
1745 list_add(&scb->list, &adapter->free_list);
1749 static int
1750 __mega_busywait_mbox (adapter_t *adapter)
1752 volatile mbox_t *mbox = adapter->mbox;
1753 long counter;
1755 for (counter = 0; counter < 10000; counter++) {
1756 if (!mbox->m_in.busy)
1757 return 0;
1758 udelay(100); yield();
1760 return -1; /* give up after 1 second */
1764 * Copies data to SGLIST
1765 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1767 static int
1768 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1770 struct scatterlist *sgl;
1771 struct page *page;
1772 unsigned long offset;
1773 unsigned int length;
1774 Scsi_Cmnd *cmd;
1775 int sgcnt;
1776 int idx;
1778 cmd = scb->cmd;
1780 /* Scatter-gather not used */
1781 if( cmd->use_sg == 0 || (cmd->use_sg == 1 &&
1782 !adapter->has_64bit_addr)) {
1784 if (cmd->use_sg == 0) {
1785 page = virt_to_page(cmd->request_buffer);
1786 offset = offset_in_page(cmd->request_buffer);
1787 length = cmd->request_bufflen;
1788 } else {
1789 sgl = (struct scatterlist *)cmd->request_buffer;
1790 page = sgl->page;
1791 offset = sgl->offset;
1792 length = sgl->length;
1795 scb->dma_h_bulkdata = pci_map_page(adapter->dev,
1796 page, offset,
1797 length,
1798 scb->dma_direction);
1799 scb->dma_type = MEGA_BULK_DATA;
1802 * We need to handle special 64-bit commands that need a
1803 * minimum of 1 SG
1805 if( adapter->has_64bit_addr ) {
1806 scb->sgl64[0].address = scb->dma_h_bulkdata;
1807 scb->sgl64[0].length = length;
1808 *buf = (u32)scb->sgl_dma_addr;
1809 *len = (u32)length;
1810 return 1;
1812 else {
1813 *buf = (u32)scb->dma_h_bulkdata;
1814 *len = (u32)length;
1816 return 0;
1819 sgl = (struct scatterlist *)cmd->request_buffer;
1822 * Copy Scatter-Gather list info into controller structure.
1824 * The number of sg elements returned must not exceed our limit
1826 sgcnt = pci_map_sg(adapter->dev, sgl, cmd->use_sg,
1827 scb->dma_direction);
1829 scb->dma_type = MEGA_SGLIST;
1831 BUG_ON(sgcnt > adapter->sglen);
1833 *len = 0;
1835 for( idx = 0; idx < sgcnt; idx++, sgl++ ) {
1837 if( adapter->has_64bit_addr ) {
1838 scb->sgl64[idx].address = sg_dma_address(sgl);
1839 *len += scb->sgl64[idx].length = sg_dma_len(sgl);
1841 else {
1842 scb->sgl[idx].address = sg_dma_address(sgl);
1843 *len += scb->sgl[idx].length = sg_dma_len(sgl);
1847 /* Reset pointer and length fields */
1848 *buf = scb->sgl_dma_addr;
1850 /* Return count of SG requests */
1851 return sgcnt;
1856 * mega_8_to_40ld()
1858 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1859 * Enquiry3 structures for later use
1861 static void
1862 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1863 mega_product_info *product_info)
1865 int i;
1867 product_info->max_commands = inquiry->adapter_info.max_commands;
1868 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1869 product_info->nchannels = inquiry->adapter_info.nchannels;
1871 for (i = 0; i < 4; i++) {
1872 product_info->fw_version[i] =
1873 inquiry->adapter_info.fw_version[i];
1875 product_info->bios_version[i] =
1876 inquiry->adapter_info.bios_version[i];
1878 enquiry3->cache_flush_interval =
1879 inquiry->adapter_info.cache_flush_interval;
1881 product_info->dram_size = inquiry->adapter_info.dram_size;
1883 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1885 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1886 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1887 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1888 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1891 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1892 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1895 static inline void
1896 mega_free_sgl(adapter_t *adapter)
1898 scb_t *scb;
1899 int i;
1901 for(i = 0; i < adapter->max_cmds; i++) {
1903 scb = &adapter->scb_list[i];
1905 if( scb->sgl64 ) {
1906 pci_free_consistent(adapter->dev,
1907 sizeof(mega_sgl64) * adapter->sglen,
1908 scb->sgl64,
1909 scb->sgl_dma_addr);
1911 scb->sgl64 = NULL;
1914 if( scb->pthru ) {
1915 pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1916 scb->pthru, scb->pthru_dma_addr);
1918 scb->pthru = NULL;
1921 if( scb->epthru ) {
1922 pci_free_consistent(adapter->dev,
1923 sizeof(mega_ext_passthru),
1924 scb->epthru, scb->epthru_dma_addr);
1926 scb->epthru = NULL;
1934 * Get information about the card/driver
1936 const char *
1937 megaraid_info(struct Scsi_Host *host)
1939 static char buffer[512];
1940 adapter_t *adapter;
1942 adapter = (adapter_t *)host->hostdata;
1944 sprintf (buffer,
1945 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1946 adapter->fw_version, adapter->product_info.max_commands,
1947 adapter->host->max_id, adapter->host->max_channel,
1948 adapter->host->max_lun);
1949 return buffer;
1953 * Abort a previous SCSI request. Only commands on the pending list can be
1954 * aborted. All the commands issued to the F/W must complete.
1956 static int
1957 megaraid_abort(Scsi_Cmnd *cmd)
1959 adapter_t *adapter;
1960 int rval;
1962 adapter = (adapter_t *)cmd->device->host->hostdata;
1964 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1967 * This is required here to complete any completed requests
1968 * to be communicated over to the mid layer.
1970 mega_rundoneq(adapter);
1972 return rval;
1976 static int
1977 megaraid_reset(struct scsi_cmnd *cmd)
1979 adapter_t *adapter;
1980 megacmd_t mc;
1981 int rval;
1983 adapter = (adapter_t *)cmd->device->host->hostdata;
1985 #if MEGA_HAVE_CLUSTERING
1986 mc.cmd = MEGA_CLUSTER_CMD;
1987 mc.opcode = MEGA_RESET_RESERVATIONS;
1989 if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1990 printk(KERN_WARNING
1991 "megaraid: reservation reset failed.\n");
1993 else {
1994 printk(KERN_INFO "megaraid: reservation reset.\n");
1996 #endif
1998 spin_lock_irq(&adapter->lock);
2000 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
2003 * This is required here to complete any completed requests
2004 * to be communicated over to the mid layer.
2006 mega_rundoneq(adapter);
2007 spin_unlock_irq(&adapter->lock);
2009 return rval;
2013 * megaraid_abort_and_reset()
2014 * @adapter - megaraid soft state
2015 * @cmd - scsi command to be aborted or reset
2016 * @aor - abort or reset flag
2018 * Try to locate the scsi command in the pending queue. If found and is not
2019 * issued to the controller, abort/reset it. Otherwise return failure
2021 static int
2022 megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
2024 struct list_head *pos, *next;
2025 scb_t *scb;
2027 printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
2028 (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
2029 cmd->cmnd[0], cmd->device->channel,
2030 cmd->device->id, cmd->device->lun);
2032 if(list_empty(&adapter->pending_list))
2033 return FALSE;
2035 list_for_each_safe(pos, next, &adapter->pending_list) {
2037 scb = list_entry(pos, scb_t, list);
2039 if (scb->cmd == cmd) { /* Found command */
2041 scb->state |= aor;
2044 * Check if this command has firmare owenership. If
2045 * yes, we cannot reset this command. Whenever, f/w
2046 * completes this command, we will return appropriate
2047 * status from ISR.
2049 if( scb->state & SCB_ISSUED ) {
2051 printk(KERN_WARNING
2052 "megaraid: %s-%lx[%x], fw owner.\n",
2053 (aor==SCB_ABORT) ? "ABORTING":"RESET",
2054 cmd->serial_number, scb->idx);
2056 return FALSE;
2058 else {
2061 * Not yet issued! Remove from the pending
2062 * list
2064 printk(KERN_WARNING
2065 "megaraid: %s-%lx[%x], driver owner.\n",
2066 (aor==SCB_ABORT) ? "ABORTING":"RESET",
2067 cmd->serial_number, scb->idx);
2069 mega_free_scb(adapter, scb);
2071 if( aor == SCB_ABORT ) {
2072 cmd->result = (DID_ABORT << 16);
2074 else {
2075 cmd->result = (DID_RESET << 16);
2078 list_add_tail(SCSI_LIST(cmd),
2079 &adapter->completed_list);
2081 return TRUE;
2086 return FALSE;
2089 static inline int
2090 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2092 *pdev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
2094 if( *pdev == NULL ) return -1;
2096 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2098 if( pci_set_dma_mask(*pdev, DMA_32BIT_MASK) != 0 ) {
2099 kfree(*pdev);
2100 return -1;
2103 return 0;
2106 static inline void
2107 free_local_pdev(struct pci_dev *pdev)
2109 kfree(pdev);
2113 * mega_allocate_inquiry()
2114 * @dma_handle - handle returned for dma address
2115 * @pdev - handle to pci device
2117 * allocates memory for inquiry structure
2119 static inline void *
2120 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2122 return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2126 static inline void
2127 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2129 pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2133 #ifdef CONFIG_PROC_FS
2134 /* Following code handles /proc fs */
2136 #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
2137 S_IRUSR | S_IFREG, \
2138 controller_proc_dir_entry, \
2139 func, adapter)
2142 * mega_create_proc_entry()
2143 * @index - index in soft state array
2144 * @parent - parent node for this /proc entry
2146 * Creates /proc entries for our controllers.
2148 static void
2149 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2151 struct proc_dir_entry *controller_proc_dir_entry = NULL;
2152 u8 string[64] = { 0 };
2153 adapter_t *adapter = hba_soft_state[index];
2155 sprintf(string, "hba%d", adapter->host->host_no);
2157 controller_proc_dir_entry =
2158 adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
2160 if(!controller_proc_dir_entry) {
2161 printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2162 return;
2164 adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
2165 adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
2166 adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
2167 #if MEGA_HAVE_ENH_PROC
2168 adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
2169 adapter->proc_battery = CREATE_READ_PROC("battery-status",
2170 proc_battery);
2173 * Display each physical drive on its channel
2175 adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
2176 proc_pdrv_ch0);
2177 adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
2178 proc_pdrv_ch1);
2179 adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
2180 proc_pdrv_ch2);
2181 adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
2182 proc_pdrv_ch3);
2185 * Display a set of up to 10 logical drive through each of following
2186 * /proc entries
2188 adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
2189 proc_rdrv_10);
2190 adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
2191 proc_rdrv_20);
2192 adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
2193 proc_rdrv_30);
2194 adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
2195 proc_rdrv_40);
2196 #endif
2201 * proc_read_config()
2202 * @page - buffer to write the data in
2203 * @start - where the actual data has been written in page
2204 * @offset - same meaning as the read system call
2205 * @count - same meaning as the read system call
2206 * @eof - set if no more data needs to be returned
2207 * @data - pointer to our soft state
2209 * Display configuration information about the controller.
2211 static int
2212 proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
2213 void *data)
2216 adapter_t *adapter = (adapter_t *)data;
2217 int len = 0;
2219 len += sprintf(page+len, "%s", MEGARAID_VERSION);
2221 if(adapter->product_info.product_name[0])
2222 len += sprintf(page+len, "%s\n",
2223 adapter->product_info.product_name);
2225 len += sprintf(page+len, "Controller Type: ");
2227 if( adapter->flag & BOARD_MEMMAP ) {
2228 len += sprintf(page+len,
2229 "438/466/467/471/493/518/520/531/532\n");
2231 else {
2232 len += sprintf(page+len,
2233 "418/428/434\n");
2236 if(adapter->flag & BOARD_40LD) {
2237 len += sprintf(page+len,
2238 "Controller Supports 40 Logical Drives\n");
2241 if(adapter->flag & BOARD_64BIT) {
2242 len += sprintf(page+len,
2243 "Controller capable of 64-bit memory addressing\n");
2245 if( adapter->has_64bit_addr ) {
2246 len += sprintf(page+len,
2247 "Controller using 64-bit memory addressing\n");
2249 else {
2250 len += sprintf(page+len,
2251 "Controller is not using 64-bit memory addressing\n");
2254 len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
2255 adapter->host->irq);
2257 len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
2258 adapter->numldrv, adapter->product_info.nchannels);
2260 len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
2261 adapter->fw_version, adapter->bios_version,
2262 adapter->product_info.dram_size);
2264 len += sprintf(page+len,
2265 "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2266 adapter->product_info.max_commands, adapter->max_cmds);
2268 len += sprintf(page+len, "support_ext_cdb = %d\n",
2269 adapter->support_ext_cdb);
2270 len += sprintf(page+len, "support_random_del = %d\n",
2271 adapter->support_random_del);
2272 len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
2273 adapter->boot_ldrv_enabled);
2274 len += sprintf(page+len, "boot_ldrv = %d\n",
2275 adapter->boot_ldrv);
2276 len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
2277 adapter->boot_pdrv_enabled);
2278 len += sprintf(page+len, "boot_pdrv_ch = %d\n",
2279 adapter->boot_pdrv_ch);
2280 len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
2281 adapter->boot_pdrv_tgt);
2282 len += sprintf(page+len, "quiescent = %d\n",
2283 atomic_read(&adapter->quiescent));
2284 len += sprintf(page+len, "has_cluster = %d\n",
2285 adapter->has_cluster);
2287 len += sprintf(page+len, "\nModule Parameters:\n");
2288 len += sprintf(page+len, "max_cmd_per_lun = %d\n",
2289 max_cmd_per_lun);
2290 len += sprintf(page+len, "max_sectors_per_io = %d\n",
2291 max_sectors_per_io);
2293 *eof = 1;
2295 return len;
2301 * proc_read_stat()
2302 * @page - buffer to write the data in
2303 * @start - where the actual data has been written in page
2304 * @offset - same meaning as the read system call
2305 * @count - same meaning as the read system call
2306 * @eof - set if no more data needs to be returned
2307 * @data - pointer to our soft state
2309 * Diaplay statistical information about the I/O activity.
2311 static int
2312 proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
2313 void *data)
2315 adapter_t *adapter;
2316 int len;
2317 int i;
2319 i = 0; /* avoid compilation warnings */
2320 len = 0;
2321 adapter = (adapter_t *)data;
2323 len = sprintf(page, "Statistical Information for this controller\n");
2324 len += sprintf(page+len, "pend_cmds = %d\n",
2325 atomic_read(&adapter->pend_cmds));
2326 #if MEGA_HAVE_STATS
2327 for(i = 0; i < adapter->numldrv; i++) {
2328 len += sprintf(page+len, "Logical Drive %d:\n", i);
2330 len += sprintf(page+len,
2331 "\tReads Issued = %lu, Writes Issued = %lu\n",
2332 adapter->nreads[i], adapter->nwrites[i]);
2334 len += sprintf(page+len,
2335 "\tSectors Read = %lu, Sectors Written = %lu\n",
2336 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2338 len += sprintf(page+len,
2339 "\tRead errors = %lu, Write errors = %lu\n\n",
2340 adapter->rd_errors[i], adapter->wr_errors[i]);
2342 #else
2343 len += sprintf(page+len,
2344 "IO and error counters not compiled in driver.\n");
2345 #endif
2347 *eof = 1;
2349 return len;
2354 * proc_read_mbox()
2355 * @page - buffer to write the data in
2356 * @start - where the actual data has been written in page
2357 * @offset - same meaning as the read system call
2358 * @count - same meaning as the read system call
2359 * @eof - set if no more data needs to be returned
2360 * @data - pointer to our soft state
2362 * Display mailbox information for the last command issued. This information
2363 * is good for debugging.
2365 static int
2366 proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
2367 void *data)
2370 adapter_t *adapter = (adapter_t *)data;
2371 volatile mbox_t *mbox = adapter->mbox;
2372 int len = 0;
2374 len = sprintf(page, "Contents of Mail Box Structure\n");
2375 len += sprintf(page+len, " Fw Command = 0x%02x\n",
2376 mbox->m_out.cmd);
2377 len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
2378 mbox->m_out.cmdid);
2379 len += sprintf(page+len, " No of Sectors= %04d\n",
2380 mbox->m_out.numsectors);
2381 len += sprintf(page+len, " LBA = 0x%02x\n",
2382 mbox->m_out.lba);
2383 len += sprintf(page+len, " DTA = 0x%08x\n",
2384 mbox->m_out.xferaddr);
2385 len += sprintf(page+len, " Logical Drive= 0x%02x\n",
2386 mbox->m_out.logdrv);
2387 len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
2388 mbox->m_out.numsgelements);
2389 len += sprintf(page+len, " Busy = %01x\n",
2390 mbox->m_in.busy);
2391 len += sprintf(page+len, " Status = 0x%02x\n",
2392 mbox->m_in.status);
2394 *eof = 1;
2396 return len;
2401 * proc_rebuild_rate()
2402 * @page - buffer to write the data in
2403 * @start - where the actual data has been written in page
2404 * @offset - same meaning as the read system call
2405 * @count - same meaning as the read system call
2406 * @eof - set if no more data needs to be returned
2407 * @data - pointer to our soft state
2409 * Display current rebuild rate
2411 static int
2412 proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
2413 void *data)
2415 adapter_t *adapter = (adapter_t *)data;
2416 dma_addr_t dma_handle;
2417 caddr_t inquiry;
2418 struct pci_dev *pdev;
2419 int len = 0;
2421 if( make_local_pdev(adapter, &pdev) != 0 ) {
2422 *eof = 1;
2423 return len;
2426 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2427 free_local_pdev(pdev);
2428 *eof = 1;
2429 return len;
2432 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2434 len = sprintf(page, "Adapter inquiry failed.\n");
2436 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2438 mega_free_inquiry(inquiry, dma_handle, pdev);
2440 free_local_pdev(pdev);
2442 *eof = 1;
2444 return len;
2447 if( adapter->flag & BOARD_40LD ) {
2448 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2449 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2451 else {
2452 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2453 ((mraid_ext_inquiry *)
2454 inquiry)->raid_inq.adapter_info.rebuild_rate);
2458 mega_free_inquiry(inquiry, dma_handle, pdev);
2460 free_local_pdev(pdev);
2462 *eof = 1;
2464 return len;
2469 * proc_battery()
2470 * @page - buffer to write the data in
2471 * @start - where the actual data has been written in page
2472 * @offset - same meaning as the read system call
2473 * @count - same meaning as the read system call
2474 * @eof - set if no more data needs to be returned
2475 * @data - pointer to our soft state
2477 * Display information about the battery module on the controller.
2479 static int
2480 proc_battery(char *page, char **start, off_t offset, int count, int *eof,
2481 void *data)
2483 adapter_t *adapter = (adapter_t *)data;
2484 dma_addr_t dma_handle;
2485 caddr_t inquiry;
2486 struct pci_dev *pdev;
2487 u8 battery_status = 0;
2488 char str[256];
2489 int len = 0;
2491 if( make_local_pdev(adapter, &pdev) != 0 ) {
2492 *eof = 1;
2493 return len;
2496 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2497 free_local_pdev(pdev);
2498 *eof = 1;
2499 return len;
2502 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2504 len = sprintf(page, "Adapter inquiry failed.\n");
2506 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2508 mega_free_inquiry(inquiry, dma_handle, pdev);
2510 free_local_pdev(pdev);
2512 *eof = 1;
2514 return len;
2517 if( adapter->flag & BOARD_40LD ) {
2518 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2520 else {
2521 battery_status = ((mraid_ext_inquiry *)inquiry)->
2522 raid_inq.adapter_info.battery_status;
2526 * Decode the battery status
2528 sprintf(str, "Battery Status:[%d]", battery_status);
2530 if(battery_status == MEGA_BATT_CHARGE_DONE)
2531 strcat(str, " Charge Done");
2533 if(battery_status & MEGA_BATT_MODULE_MISSING)
2534 strcat(str, " Module Missing");
2536 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2537 strcat(str, " Low Voltage");
2539 if(battery_status & MEGA_BATT_TEMP_HIGH)
2540 strcat(str, " Temperature High");
2542 if(battery_status & MEGA_BATT_PACK_MISSING)
2543 strcat(str, " Pack Missing");
2545 if(battery_status & MEGA_BATT_CHARGE_INPROG)
2546 strcat(str, " Charge In-progress");
2548 if(battery_status & MEGA_BATT_CHARGE_FAIL)
2549 strcat(str, " Charge Fail");
2551 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2552 strcat(str, " Cycles Exceeded");
2554 len = sprintf(page, "%s\n", str);
2557 mega_free_inquiry(inquiry, dma_handle, pdev);
2559 free_local_pdev(pdev);
2561 *eof = 1;
2563 return len;
2568 * proc_pdrv_ch0()
2569 * @page - buffer to write the data in
2570 * @start - where the actual data has been written in page
2571 * @offset - same meaning as the read system call
2572 * @count - same meaning as the read system call
2573 * @eof - set if no more data needs to be returned
2574 * @data - pointer to our soft state
2576 * Display information about the physical drives on physical channel 0.
2578 static int
2579 proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
2580 void *data)
2582 adapter_t *adapter = (adapter_t *)data;
2584 *eof = 1;
2586 return (proc_pdrv(adapter, page, 0));
2591 * proc_pdrv_ch1()
2592 * @page - buffer to write the data in
2593 * @start - where the actual data has been written in page
2594 * @offset - same meaning as the read system call
2595 * @count - same meaning as the read system call
2596 * @eof - set if no more data needs to be returned
2597 * @data - pointer to our soft state
2599 * Display information about the physical drives on physical channel 1.
2601 static int
2602 proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
2603 void *data)
2605 adapter_t *adapter = (adapter_t *)data;
2607 *eof = 1;
2609 return (proc_pdrv(adapter, page, 1));
2614 * proc_pdrv_ch2()
2615 * @page - buffer to write the data in
2616 * @start - where the actual data has been written in page
2617 * @offset - same meaning as the read system call
2618 * @count - same meaning as the read system call
2619 * @eof - set if no more data needs to be returned
2620 * @data - pointer to our soft state
2622 * Display information about the physical drives on physical channel 2.
2624 static int
2625 proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
2626 void *data)
2628 adapter_t *adapter = (adapter_t *)data;
2630 *eof = 1;
2632 return (proc_pdrv(adapter, page, 2));
2637 * proc_pdrv_ch3()
2638 * @page - buffer to write the data in
2639 * @start - where the actual data has been written in page
2640 * @offset - same meaning as the read system call
2641 * @count - same meaning as the read system call
2642 * @eof - set if no more data needs to be returned
2643 * @data - pointer to our soft state
2645 * Display information about the physical drives on physical channel 3.
2647 static int
2648 proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
2649 void *data)
2651 adapter_t *adapter = (adapter_t *)data;
2653 *eof = 1;
2655 return (proc_pdrv(adapter, page, 3));
2660 * proc_pdrv()
2661 * @page - buffer to write the data in
2662 * @adapter - pointer to our soft state
2664 * Display information about the physical drives.
2666 static int
2667 proc_pdrv(adapter_t *adapter, char *page, int channel)
2669 dma_addr_t dma_handle;
2670 char *scsi_inq;
2671 dma_addr_t scsi_inq_dma_handle;
2672 caddr_t inquiry;
2673 struct pci_dev *pdev;
2674 u8 *pdrv_state;
2675 u8 state;
2676 int tgt;
2677 int max_channels;
2678 int len = 0;
2679 char str[80];
2680 int i;
2682 if( make_local_pdev(adapter, &pdev) != 0 ) {
2683 return len;
2686 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2687 goto free_pdev;
2690 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2691 len = sprintf(page, "Adapter inquiry failed.\n");
2693 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2695 goto free_inquiry;
2699 scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2701 if( scsi_inq == NULL ) {
2702 len = sprintf(page, "memory not available for scsi inq.\n");
2704 goto free_inquiry;
2707 if( adapter->flag & BOARD_40LD ) {
2708 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2710 else {
2711 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2712 raid_inq.pdrv_info.pdrv_state;
2715 max_channels = adapter->product_info.nchannels;
2717 if( channel >= max_channels ) {
2718 goto free_pci;
2721 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2723 i = channel*16 + tgt;
2725 state = *(pdrv_state + i);
2727 switch( state & 0x0F ) {
2729 case PDRV_ONLINE:
2730 sprintf(str,
2731 "Channel:%2d Id:%2d State: Online",
2732 channel, tgt);
2733 break;
2735 case PDRV_FAILED:
2736 sprintf(str,
2737 "Channel:%2d Id:%2d State: Failed",
2738 channel, tgt);
2739 break;
2741 case PDRV_RBLD:
2742 sprintf(str,
2743 "Channel:%2d Id:%2d State: Rebuild",
2744 channel, tgt);
2745 break;
2747 case PDRV_HOTSPARE:
2748 sprintf(str,
2749 "Channel:%2d Id:%2d State: Hot spare",
2750 channel, tgt);
2751 break;
2753 default:
2754 sprintf(str,
2755 "Channel:%2d Id:%2d State: Un-configured",
2756 channel, tgt);
2757 break;
2762 * This interface displays inquiries for disk drives
2763 * only. Inquries for logical drives and non-disk
2764 * devices are available through /proc/scsi/scsi
2766 memset(scsi_inq, 0, 256);
2767 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2768 scsi_inq_dma_handle) ||
2769 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2770 continue;
2774 * Check for overflow. We print less than 240
2775 * characters for inquiry
2777 if( (len + 240) >= PAGE_SIZE ) break;
2779 len += sprintf(page+len, "%s.\n", str);
2781 len += mega_print_inquiry(page+len, scsi_inq);
2784 free_pci:
2785 pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2786 free_inquiry:
2787 mega_free_inquiry(inquiry, dma_handle, pdev);
2788 free_pdev:
2789 free_local_pdev(pdev);
2791 return len;
2796 * Display scsi inquiry
2798 static int
2799 mega_print_inquiry(char *page, char *scsi_inq)
2801 int len = 0;
2802 int i;
2804 len = sprintf(page, " Vendor: ");
2805 for( i = 8; i < 16; i++ ) {
2806 len += sprintf(page+len, "%c", scsi_inq[i]);
2809 len += sprintf(page+len, " Model: ");
2811 for( i = 16; i < 32; i++ ) {
2812 len += sprintf(page+len, "%c", scsi_inq[i]);
2815 len += sprintf(page+len, " Rev: ");
2817 for( i = 32; i < 36; i++ ) {
2818 len += sprintf(page+len, "%c", scsi_inq[i]);
2821 len += sprintf(page+len, "\n");
2823 i = scsi_inq[0] & 0x1f;
2825 len += sprintf(page+len, " Type: %s ", scsi_device_type(i));
2827 len += sprintf(page+len,
2828 " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
2830 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2831 len += sprintf(page+len, " CCS\n");
2832 else
2833 len += sprintf(page+len, "\n");
2835 return len;
2840 * proc_rdrv_10()
2841 * @page - buffer to write the data in
2842 * @start - where the actual data has been written in page
2843 * @offset - same meaning as the read system call
2844 * @count - same meaning as the read system call
2845 * @eof - set if no more data needs to be returned
2846 * @data - pointer to our soft state
2848 * Display real time information about the logical drives 0 through 9.
2850 static int
2851 proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
2852 void *data)
2854 adapter_t *adapter = (adapter_t *)data;
2856 *eof = 1;
2858 return (proc_rdrv(adapter, page, 0, 9));
2863 * proc_rdrv_20()
2864 * @page - buffer to write the data in
2865 * @start - where the actual data has been written in page
2866 * @offset - same meaning as the read system call
2867 * @count - same meaning as the read system call
2868 * @eof - set if no more data needs to be returned
2869 * @data - pointer to our soft state
2871 * Display real time information about the logical drives 0 through 9.
2873 static int
2874 proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
2875 void *data)
2877 adapter_t *adapter = (adapter_t *)data;
2879 *eof = 1;
2881 return (proc_rdrv(adapter, page, 10, 19));
2886 * proc_rdrv_30()
2887 * @page - buffer to write the data in
2888 * @start - where the actual data has been written in page
2889 * @offset - same meaning as the read system call
2890 * @count - same meaning as the read system call
2891 * @eof - set if no more data needs to be returned
2892 * @data - pointer to our soft state
2894 * Display real time information about the logical drives 0 through 9.
2896 static int
2897 proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
2898 void *data)
2900 adapter_t *adapter = (adapter_t *)data;
2902 *eof = 1;
2904 return (proc_rdrv(adapter, page, 20, 29));
2909 * proc_rdrv_40()
2910 * @page - buffer to write the data in
2911 * @start - where the actual data has been written in page
2912 * @offset - same meaning as the read system call
2913 * @count - same meaning as the read system call
2914 * @eof - set if no more data needs to be returned
2915 * @data - pointer to our soft state
2917 * Display real time information about the logical drives 0 through 9.
2919 static int
2920 proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
2921 void *data)
2923 adapter_t *adapter = (adapter_t *)data;
2925 *eof = 1;
2927 return (proc_rdrv(adapter, page, 30, 39));
2932 * proc_rdrv()
2933 * @page - buffer to write the data in
2934 * @adapter - pointer to our soft state
2935 * @start - starting logical drive to display
2936 * @end - ending logical drive to display
2938 * We do not print the inquiry information since its already available through
2939 * /proc/scsi/scsi interface
2941 static int
2942 proc_rdrv(adapter_t *adapter, char *page, int start, int end )
2944 dma_addr_t dma_handle;
2945 logdrv_param *lparam;
2946 megacmd_t mc;
2947 char *disk_array;
2948 dma_addr_t disk_array_dma_handle;
2949 caddr_t inquiry;
2950 struct pci_dev *pdev;
2951 u8 *rdrv_state;
2952 int num_ldrv;
2953 u32 array_sz;
2954 int len = 0;
2955 int i;
2957 if( make_local_pdev(adapter, &pdev) != 0 ) {
2958 return len;
2961 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2962 free_local_pdev(pdev);
2963 return len;
2966 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2968 len = sprintf(page, "Adapter inquiry failed.\n");
2970 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2972 mega_free_inquiry(inquiry, dma_handle, pdev);
2974 free_local_pdev(pdev);
2976 return len;
2979 memset(&mc, 0, sizeof(megacmd_t));
2981 if( adapter->flag & BOARD_40LD ) {
2982 array_sz = sizeof(disk_array_40ld);
2984 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2986 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2988 else {
2989 array_sz = sizeof(disk_array_8ld);
2991 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2992 raid_inq.logdrv_info.ldrv_state;
2994 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2995 raid_inq.logdrv_info.num_ldrv;
2998 disk_array = pci_alloc_consistent(pdev, array_sz,
2999 &disk_array_dma_handle);
3001 if( disk_array == NULL ) {
3002 len = sprintf(page, "memory not available.\n");
3004 mega_free_inquiry(inquiry, dma_handle, pdev);
3006 free_local_pdev(pdev);
3008 return len;
3011 mc.xferaddr = (u32)disk_array_dma_handle;
3013 if( adapter->flag & BOARD_40LD ) {
3014 mc.cmd = FC_NEW_CONFIG;
3015 mc.opcode = OP_DCMD_READ_CONFIG;
3017 if( mega_internal_command(adapter, &mc, NULL) ) {
3019 len = sprintf(page, "40LD read config failed.\n");
3021 mega_free_inquiry(inquiry, dma_handle, pdev);
3023 pci_free_consistent(pdev, array_sz, disk_array,
3024 disk_array_dma_handle);
3026 free_local_pdev(pdev);
3028 return len;
3032 else {
3033 mc.cmd = NEW_READ_CONFIG_8LD;
3035 if( mega_internal_command(adapter, &mc, NULL) ) {
3037 mc.cmd = READ_CONFIG_8LD;
3039 if( mega_internal_command(adapter, &mc,
3040 NULL) ){
3042 len = sprintf(page,
3043 "8LD read config failed.\n");
3045 mega_free_inquiry(inquiry, dma_handle, pdev);
3047 pci_free_consistent(pdev, array_sz,
3048 disk_array,
3049 disk_array_dma_handle);
3051 free_local_pdev(pdev);
3053 return len;
3058 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
3060 if( adapter->flag & BOARD_40LD ) {
3061 lparam =
3062 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
3064 else {
3065 lparam =
3066 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
3070 * Check for overflow. We print less than 240 characters for
3071 * information about each logical drive.
3073 if( (len + 240) >= PAGE_SIZE ) break;
3075 len += sprintf(page+len, "Logical drive:%2d:, ", i);
3077 switch( rdrv_state[i] & 0x0F ) {
3078 case RDRV_OFFLINE:
3079 len += sprintf(page+len, "state: offline");
3080 break;
3082 case RDRV_DEGRADED:
3083 len += sprintf(page+len, "state: degraded");
3084 break;
3086 case RDRV_OPTIMAL:
3087 len += sprintf(page+len, "state: optimal");
3088 break;
3090 case RDRV_DELETED:
3091 len += sprintf(page+len, "state: deleted");
3092 break;
3094 default:
3095 len += sprintf(page+len, "state: unknown");
3096 break;
3100 * Check if check consistency or initialization is going on
3101 * for this logical drive.
3103 if( (rdrv_state[i] & 0xF0) == 0x20 ) {
3104 len += sprintf(page+len,
3105 ", check-consistency in progress");
3107 else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
3108 len += sprintf(page+len,
3109 ", initialization in progress");
3112 len += sprintf(page+len, "\n");
3114 len += sprintf(page+len, "Span depth:%3d, ",
3115 lparam->span_depth);
3117 len += sprintf(page+len, "RAID level:%3d, ",
3118 lparam->level);
3120 len += sprintf(page+len, "Stripe size:%3d, ",
3121 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
3123 len += sprintf(page+len, "Row size:%3d\n",
3124 lparam->row_size);
3127 len += sprintf(page+len, "Read Policy: ");
3129 switch(lparam->read_ahead) {
3131 case NO_READ_AHEAD:
3132 len += sprintf(page+len, "No read ahead, ");
3133 break;
3135 case READ_AHEAD:
3136 len += sprintf(page+len, "Read ahead, ");
3137 break;
3139 case ADAP_READ_AHEAD:
3140 len += sprintf(page+len, "Adaptive, ");
3141 break;
3145 len += sprintf(page+len, "Write Policy: ");
3147 switch(lparam->write_mode) {
3149 case WRMODE_WRITE_THRU:
3150 len += sprintf(page+len, "Write thru, ");
3151 break;
3153 case WRMODE_WRITE_BACK:
3154 len += sprintf(page+len, "Write back, ");
3155 break;
3158 len += sprintf(page+len, "Cache Policy: ");
3160 switch(lparam->direct_io) {
3162 case CACHED_IO:
3163 len += sprintf(page+len, "Cached IO\n\n");
3164 break;
3166 case DIRECT_IO:
3167 len += sprintf(page+len, "Direct IO\n\n");
3168 break;
3172 mega_free_inquiry(inquiry, dma_handle, pdev);
3174 pci_free_consistent(pdev, array_sz, disk_array,
3175 disk_array_dma_handle);
3177 free_local_pdev(pdev);
3179 return len;
3182 #endif
3186 * megaraid_biosparam()
3188 * Return the disk geometry for a particular disk
3190 static int
3191 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
3192 sector_t capacity, int geom[])
3194 adapter_t *adapter;
3195 unsigned char *bh;
3196 int heads;
3197 int sectors;
3198 int cylinders;
3199 int rval;
3201 /* Get pointer to host config structure */
3202 adapter = (adapter_t *)sdev->host->hostdata;
3204 if (IS_RAID_CH(adapter, sdev->channel)) {
3205 /* Default heads (64) & sectors (32) */
3206 heads = 64;
3207 sectors = 32;
3208 cylinders = (ulong)capacity / (heads * sectors);
3211 * Handle extended translation size for logical drives
3212 * > 1Gb
3214 if ((ulong)capacity >= 0x200000) {
3215 heads = 255;
3216 sectors = 63;
3217 cylinders = (ulong)capacity / (heads * sectors);
3220 /* return result */
3221 geom[0] = heads;
3222 geom[1] = sectors;
3223 geom[2] = cylinders;
3225 else {
3226 bh = scsi_bios_ptable(bdev);
3228 if( bh ) {
3229 rval = scsi_partsize(bh, capacity,
3230 &geom[2], &geom[0], &geom[1]);
3231 kfree(bh);
3232 if( rval != -1 )
3233 return rval;
3236 printk(KERN_INFO
3237 "megaraid: invalid partition on this disk on channel %d\n",
3238 sdev->channel);
3240 /* Default heads (64) & sectors (32) */
3241 heads = 64;
3242 sectors = 32;
3243 cylinders = (ulong)capacity / (heads * sectors);
3245 /* Handle extended translation size for logical drives > 1Gb */
3246 if ((ulong)capacity >= 0x200000) {
3247 heads = 255;
3248 sectors = 63;
3249 cylinders = (ulong)capacity / (heads * sectors);
3252 /* return result */
3253 geom[0] = heads;
3254 geom[1] = sectors;
3255 geom[2] = cylinders;
3258 return 0;
3262 * mega_init_scb()
3263 * @adapter - pointer to our soft state
3265 * Allocate memory for the various pointers in the scb structures:
3266 * scatter-gather list pointer, passthru and extended passthru structure
3267 * pointers.
3269 static int
3270 mega_init_scb(adapter_t *adapter)
3272 scb_t *scb;
3273 int i;
3275 for( i = 0; i < adapter->max_cmds; i++ ) {
3277 scb = &adapter->scb_list[i];
3279 scb->sgl64 = NULL;
3280 scb->sgl = NULL;
3281 scb->pthru = NULL;
3282 scb->epthru = NULL;
3285 for( i = 0; i < adapter->max_cmds; i++ ) {
3287 scb = &adapter->scb_list[i];
3289 scb->idx = i;
3291 scb->sgl64 = pci_alloc_consistent(adapter->dev,
3292 sizeof(mega_sgl64) * adapter->sglen,
3293 &scb->sgl_dma_addr);
3295 scb->sgl = (mega_sglist *)scb->sgl64;
3297 if( !scb->sgl ) {
3298 printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
3299 mega_free_sgl(adapter);
3300 return -1;
3303 scb->pthru = pci_alloc_consistent(adapter->dev,
3304 sizeof(mega_passthru),
3305 &scb->pthru_dma_addr);
3307 if( !scb->pthru ) {
3308 printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
3309 mega_free_sgl(adapter);
3310 return -1;
3313 scb->epthru = pci_alloc_consistent(adapter->dev,
3314 sizeof(mega_ext_passthru),
3315 &scb->epthru_dma_addr);
3317 if( !scb->epthru ) {
3318 printk(KERN_WARNING
3319 "Can't allocate extended passthru.\n");
3320 mega_free_sgl(adapter);
3321 return -1;
3325 scb->dma_type = MEGA_DMA_TYPE_NONE;
3328 * Link to free list
3329 * lock not required since we are loading the driver, so no
3330 * commands possible right now.
3332 scb->state = SCB_FREE;
3333 scb->cmd = NULL;
3334 list_add(&scb->list, &adapter->free_list);
3337 return 0;
3342 * megadev_open()
3343 * @inode - unused
3344 * @filep - unused
3346 * Routines for the character/ioctl interface to the driver. Find out if this
3347 * is a valid open. If yes, increment the module use count so that it cannot
3348 * be unloaded.
3350 static int
3351 megadev_open (struct inode *inode, struct file *filep)
3354 * Only allow superuser to access private ioctl interface
3356 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
3358 return 0;
3363 * megadev_ioctl()
3364 * @inode - Our device inode
3365 * @filep - unused
3366 * @cmd - ioctl command
3367 * @arg - user buffer
3369 * ioctl entry point for our private ioctl interface. We move the data in from
3370 * the user space, prepare the command (if necessary, convert the old MIMD
3371 * ioctl to new ioctl command), and issue a synchronous command to the
3372 * controller.
3374 static int
3375 megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
3376 unsigned long arg)
3378 adapter_t *adapter;
3379 nitioctl_t uioc;
3380 int adapno;
3381 int rval;
3382 mega_passthru __user *upthru; /* user address for passthru */
3383 mega_passthru *pthru; /* copy user passthru here */
3384 dma_addr_t pthru_dma_hndl;
3385 void *data = NULL; /* data to be transferred */
3386 dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
3387 megacmd_t mc;
3388 megastat_t __user *ustats;
3389 int num_ldrv;
3390 u32 uxferaddr = 0;
3391 struct pci_dev *pdev;
3393 ustats = NULL; /* avoid compilation warnings */
3394 num_ldrv = 0;
3397 * Make sure only USCSICMD are issued through this interface.
3398 * MIMD application would still fire different command.
3400 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3401 return -EINVAL;
3405 * Check and convert a possible MIMD command to NIT command.
3406 * mega_m_to_n() copies the data from the user space, so we do not
3407 * have to do it here.
3408 * NOTE: We will need some user address to copyout the data, therefore
3409 * the inteface layer will also provide us with the required user
3410 * addresses.
3412 memset(&uioc, 0, sizeof(nitioctl_t));
3413 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3414 return rval;
3417 switch( uioc.opcode ) {
3419 case GET_DRIVER_VER:
3420 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3421 return (-EFAULT);
3423 break;
3425 case GET_N_ADAP:
3426 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3427 return (-EFAULT);
3430 * Shucks. MIMD interface returns a positive value for number
3431 * of adapters. TODO: Change it to return 0 when there is no
3432 * applicatio using mimd interface.
3434 return hba_count;
3436 case GET_ADAP_INFO:
3439 * Which adapter
3441 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3442 return (-ENODEV);
3444 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3445 sizeof(struct mcontroller)) )
3446 return (-EFAULT);
3447 break;
3449 #if MEGA_HAVE_STATS
3451 case GET_STATS:
3453 * Which adapter
3455 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3456 return (-ENODEV);
3458 adapter = hba_soft_state[adapno];
3460 ustats = uioc.uioc_uaddr;
3462 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3463 return (-EFAULT);
3466 * Check for the validity of the logical drive number
3468 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3470 if( copy_to_user(ustats->nreads, adapter->nreads,
3471 num_ldrv*sizeof(u32)) )
3472 return -EFAULT;
3474 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3475 num_ldrv*sizeof(u32)) )
3476 return -EFAULT;
3478 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3479 num_ldrv*sizeof(u32)) )
3480 return -EFAULT;
3482 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3483 num_ldrv*sizeof(u32)) )
3484 return -EFAULT;
3486 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3487 num_ldrv*sizeof(u32)) )
3488 return -EFAULT;
3490 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3491 num_ldrv*sizeof(u32)) )
3492 return -EFAULT;
3494 return 0;
3496 #endif
3497 case MBOX_CMD:
3500 * Which adapter
3502 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3503 return (-ENODEV);
3505 adapter = hba_soft_state[adapno];
3508 * Deletion of logical drive is a special case. The adapter
3509 * should be quiescent before this command is issued.
3511 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3512 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3515 * Do we support this feature
3517 if( !adapter->support_random_del ) {
3518 printk(KERN_WARNING "megaraid: logdrv ");
3519 printk("delete on non-supporting F/W.\n");
3521 return (-EINVAL);
3524 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3526 if( rval == 0 ) {
3527 memset(&mc, 0, sizeof(megacmd_t));
3529 mc.status = rval;
3531 rval = mega_n_to_m((void __user *)arg, &mc);
3534 return rval;
3537 * This interface only support the regular passthru commands.
3538 * Reject extended passthru and 64-bit passthru
3540 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3541 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3543 printk(KERN_WARNING "megaraid: rejected passthru.\n");
3545 return (-EINVAL);
3549 * For all internal commands, the buffer must be allocated in
3550 * <4GB address range
3552 if( make_local_pdev(adapter, &pdev) != 0 )
3553 return -EIO;
3555 /* Is it a passthru command or a DCMD */
3556 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3557 /* Passthru commands */
3559 pthru = pci_alloc_consistent(pdev,
3560 sizeof(mega_passthru),
3561 &pthru_dma_hndl);
3563 if( pthru == NULL ) {
3564 free_local_pdev(pdev);
3565 return (-ENOMEM);
3569 * The user passthru structure
3571 upthru = (mega_passthru __user *)MBOX(uioc)->xferaddr;
3574 * Copy in the user passthru here.
3576 if( copy_from_user(pthru, upthru,
3577 sizeof(mega_passthru)) ) {
3579 pci_free_consistent(pdev,
3580 sizeof(mega_passthru), pthru,
3581 pthru_dma_hndl);
3583 free_local_pdev(pdev);
3585 return (-EFAULT);
3589 * Is there a data transfer
3591 if( pthru->dataxferlen ) {
3592 data = pci_alloc_consistent(pdev,
3593 pthru->dataxferlen,
3594 &data_dma_hndl);
3596 if( data == NULL ) {
3597 pci_free_consistent(pdev,
3598 sizeof(mega_passthru),
3599 pthru,
3600 pthru_dma_hndl);
3602 free_local_pdev(pdev);
3604 return (-ENOMEM);
3608 * Save the user address and point the kernel
3609 * address at just allocated memory
3611 uxferaddr = pthru->dataxferaddr;
3612 pthru->dataxferaddr = data_dma_hndl;
3617 * Is data coming down-stream
3619 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3621 * Get the user data
3623 if( copy_from_user(data, (char __user *)uxferaddr,
3624 pthru->dataxferlen) ) {
3625 rval = (-EFAULT);
3626 goto freemem_and_return;
3630 memset(&mc, 0, sizeof(megacmd_t));
3632 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3633 mc.xferaddr = (u32)pthru_dma_hndl;
3636 * Issue the command
3638 mega_internal_command(adapter, &mc, pthru);
3640 rval = mega_n_to_m((void __user *)arg, &mc);
3642 if( rval ) goto freemem_and_return;
3646 * Is data going up-stream
3648 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3649 if( copy_to_user((char __user *)uxferaddr, data,
3650 pthru->dataxferlen) ) {
3651 rval = (-EFAULT);
3656 * Send the request sense data also, irrespective of
3657 * whether the user has asked for it or not.
3659 if (copy_to_user(upthru->reqsensearea,
3660 pthru->reqsensearea, 14))
3661 rval = -EFAULT;
3663 freemem_and_return:
3664 if( pthru->dataxferlen ) {
3665 pci_free_consistent(pdev,
3666 pthru->dataxferlen, data,
3667 data_dma_hndl);
3670 pci_free_consistent(pdev, sizeof(mega_passthru),
3671 pthru, pthru_dma_hndl);
3673 free_local_pdev(pdev);
3675 return rval;
3677 else {
3678 /* DCMD commands */
3681 * Is there a data transfer
3683 if( uioc.xferlen ) {
3684 data = pci_alloc_consistent(pdev,
3685 uioc.xferlen, &data_dma_hndl);
3687 if( data == NULL ) {
3688 free_local_pdev(pdev);
3689 return (-ENOMEM);
3692 uxferaddr = MBOX(uioc)->xferaddr;
3696 * Is data coming down-stream
3698 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3700 * Get the user data
3702 if( copy_from_user(data, (char __user *)uxferaddr,
3703 uioc.xferlen) ) {
3705 pci_free_consistent(pdev,
3706 uioc.xferlen,
3707 data, data_dma_hndl);
3709 free_local_pdev(pdev);
3711 return (-EFAULT);
3715 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3717 mc.xferaddr = (u32)data_dma_hndl;
3720 * Issue the command
3722 mega_internal_command(adapter, &mc, NULL);
3724 rval = mega_n_to_m((void __user *)arg, &mc);
3726 if( rval ) {
3727 if( uioc.xferlen ) {
3728 pci_free_consistent(pdev,
3729 uioc.xferlen, data,
3730 data_dma_hndl);
3733 free_local_pdev(pdev);
3735 return rval;
3739 * Is data going up-stream
3741 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3742 if( copy_to_user((char __user *)uxferaddr, data,
3743 uioc.xferlen) ) {
3745 rval = (-EFAULT);
3749 if( uioc.xferlen ) {
3750 pci_free_consistent(pdev,
3751 uioc.xferlen, data,
3752 data_dma_hndl);
3755 free_local_pdev(pdev);
3757 return rval;
3760 default:
3761 return (-EINVAL);
3764 return 0;
3768 * mega_m_to_n()
3769 * @arg - user address
3770 * @uioc - new ioctl structure
3772 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3773 * structure
3775 * Converts the older mimd ioctl structure to newer NIT structure
3777 static int
3778 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3780 struct uioctl_t uioc_mimd;
3781 char signature[8] = {0};
3782 u8 opcode;
3783 u8 subopcode;
3787 * check is the application conforms to NIT. We do not have to do much
3788 * in that case.
3789 * We exploit the fact that the signature is stored in the very
3790 * begining of the structure.
3793 if( copy_from_user(signature, arg, 7) )
3794 return (-EFAULT);
3796 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3799 * NOTE NOTE: The nit ioctl is still under flux because of
3800 * change of mailbox definition, in HPE. No applications yet
3801 * use this interface and let's not have applications use this
3802 * interface till the new specifitions are in place.
3804 return -EINVAL;
3805 #if 0
3806 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3807 return (-EFAULT);
3808 return 0;
3809 #endif
3813 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3815 * Get the user ioctl structure
3817 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3818 return (-EFAULT);
3822 * Get the opcode and subopcode for the commands
3824 opcode = uioc_mimd.ui.fcs.opcode;
3825 subopcode = uioc_mimd.ui.fcs.subopcode;
3827 switch (opcode) {
3828 case 0x82:
3830 switch (subopcode) {
3832 case MEGAIOC_QDRVRVER: /* Query driver version */
3833 uioc->opcode = GET_DRIVER_VER;
3834 uioc->uioc_uaddr = uioc_mimd.data;
3835 break;
3837 case MEGAIOC_QNADAP: /* Get # of adapters */
3838 uioc->opcode = GET_N_ADAP;
3839 uioc->uioc_uaddr = uioc_mimd.data;
3840 break;
3842 case MEGAIOC_QADAPINFO: /* Get adapter information */
3843 uioc->opcode = GET_ADAP_INFO;
3844 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3845 uioc->uioc_uaddr = uioc_mimd.data;
3846 break;
3848 default:
3849 return(-EINVAL);
3852 break;
3855 case 0x81:
3857 uioc->opcode = MBOX_CMD;
3858 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3860 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3862 uioc->xferlen = uioc_mimd.ui.fcs.length;
3864 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3865 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3867 break;
3869 case 0x80:
3871 uioc->opcode = MBOX_CMD;
3872 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3874 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3877 * Choose the xferlen bigger of input and output data
3879 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3880 uioc_mimd.outlen : uioc_mimd.inlen;
3882 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3883 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3885 break;
3887 default:
3888 return (-EINVAL);
3892 return 0;
3896 * mega_n_to_m()
3897 * @arg - user address
3898 * @mc - mailbox command
3900 * Updates the status information to the application, depending on application
3901 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3903 static int
3904 mega_n_to_m(void __user *arg, megacmd_t *mc)
3906 nitioctl_t __user *uiocp;
3907 megacmd_t __user *umc;
3908 mega_passthru __user *upthru;
3909 struct uioctl_t __user *uioc_mimd;
3910 char signature[8] = {0};
3913 * check is the application conforms to NIT.
3915 if( copy_from_user(signature, arg, 7) )
3916 return -EFAULT;
3918 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3920 uiocp = arg;
3922 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3923 return (-EFAULT);
3925 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3927 umc = MBOX_P(uiocp);
3929 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3930 return -EFAULT;
3932 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3933 return (-EFAULT);
3936 else {
3937 uioc_mimd = arg;
3939 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3940 return (-EFAULT);
3942 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3944 umc = (megacmd_t __user *)uioc_mimd->mbox;
3946 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3947 return (-EFAULT);
3949 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3950 return (-EFAULT);
3954 return 0;
3959 * MEGARAID 'FW' commands.
3963 * mega_is_bios_enabled()
3964 * @adapter - pointer to our soft state
3966 * issue command to find out if the BIOS is enabled for this controller
3968 static int
3969 mega_is_bios_enabled(adapter_t *adapter)
3971 unsigned char raw_mbox[sizeof(struct mbox_out)];
3972 mbox_t *mbox;
3973 int ret;
3975 mbox = (mbox_t *)raw_mbox;
3977 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3979 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3981 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3983 raw_mbox[0] = IS_BIOS_ENABLED;
3984 raw_mbox[2] = GET_BIOS;
3987 ret = issue_scb_block(adapter, raw_mbox);
3989 return *(char *)adapter->mega_buffer;
3994 * mega_enum_raid_scsi()
3995 * @adapter - pointer to our soft state
3997 * Find out what channels are RAID/SCSI. This information is used to
3998 * differentiate the virtual channels and physical channels and to support
3999 * ROMB feature and non-disk devices.
4001 static void
4002 mega_enum_raid_scsi(adapter_t *adapter)
4004 unsigned char raw_mbox[sizeof(struct mbox_out)];
4005 mbox_t *mbox;
4006 int i;
4008 mbox = (mbox_t *)raw_mbox;
4010 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4013 * issue command to find out what channels are raid/scsi
4015 raw_mbox[0] = CHNL_CLASS;
4016 raw_mbox[2] = GET_CHNL_CLASS;
4018 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4020 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4023 * Non-ROMB firmware fail this command, so all channels
4024 * must be shown RAID
4026 adapter->mega_ch_class = 0xFF;
4028 if(!issue_scb_block(adapter, raw_mbox)) {
4029 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
4033 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
4034 if( (adapter->mega_ch_class >> i) & 0x01 ) {
4035 printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
4038 else {
4039 printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
4044 return;
4049 * mega_get_boot_drv()
4050 * @adapter - pointer to our soft state
4052 * Find out which device is the boot device. Note, any logical drive or any
4053 * phyical device (e.g., a CDROM) can be designated as a boot device.
4055 static void
4056 mega_get_boot_drv(adapter_t *adapter)
4058 struct private_bios_data *prv_bios_data;
4059 unsigned char raw_mbox[sizeof(struct mbox_out)];
4060 mbox_t *mbox;
4061 u16 cksum = 0;
4062 u8 *cksum_p;
4063 u8 boot_pdrv;
4064 int i;
4066 mbox = (mbox_t *)raw_mbox;
4068 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4070 raw_mbox[0] = BIOS_PVT_DATA;
4071 raw_mbox[2] = GET_BIOS_PVT_DATA;
4073 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4075 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4077 adapter->boot_ldrv_enabled = 0;
4078 adapter->boot_ldrv = 0;
4080 adapter->boot_pdrv_enabled = 0;
4081 adapter->boot_pdrv_ch = 0;
4082 adapter->boot_pdrv_tgt = 0;
4084 if(issue_scb_block(adapter, raw_mbox) == 0) {
4085 prv_bios_data =
4086 (struct private_bios_data *)adapter->mega_buffer;
4088 cksum = 0;
4089 cksum_p = (char *)prv_bios_data;
4090 for (i = 0; i < 14; i++ ) {
4091 cksum += (u16)(*cksum_p++);
4094 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
4097 * If MSB is set, a physical drive is set as boot
4098 * device
4100 if( prv_bios_data->boot_drv & 0x80 ) {
4101 adapter->boot_pdrv_enabled = 1;
4102 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
4103 adapter->boot_pdrv_ch = boot_pdrv / 16;
4104 adapter->boot_pdrv_tgt = boot_pdrv % 16;
4106 else {
4107 adapter->boot_ldrv_enabled = 1;
4108 adapter->boot_ldrv = prv_bios_data->boot_drv;
4116 * mega_support_random_del()
4117 * @adapter - pointer to our soft state
4119 * Find out if this controller supports random deletion and addition of
4120 * logical drives
4122 static int
4123 mega_support_random_del(adapter_t *adapter)
4125 unsigned char raw_mbox[sizeof(struct mbox_out)];
4126 mbox_t *mbox;
4127 int rval;
4129 mbox = (mbox_t *)raw_mbox;
4131 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4134 * issue command
4136 raw_mbox[0] = FC_DEL_LOGDRV;
4137 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
4139 rval = issue_scb_block(adapter, raw_mbox);
4141 return !rval;
4146 * mega_support_ext_cdb()
4147 * @adapter - pointer to our soft state
4149 * Find out if this firmware support cdblen > 10
4151 static int
4152 mega_support_ext_cdb(adapter_t *adapter)
4154 unsigned char raw_mbox[sizeof(struct mbox_out)];
4155 mbox_t *mbox;
4156 int rval;
4158 mbox = (mbox_t *)raw_mbox;
4160 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4162 * issue command to find out if controller supports extended CDBs.
4164 raw_mbox[0] = 0xA4;
4165 raw_mbox[2] = 0x16;
4167 rval = issue_scb_block(adapter, raw_mbox);
4169 return !rval;
4174 * mega_del_logdrv()
4175 * @adapter - pointer to our soft state
4176 * @logdrv - logical drive to be deleted
4178 * Delete the specified logical drive. It is the responsibility of the user
4179 * app to let the OS know about this operation.
4181 static int
4182 mega_del_logdrv(adapter_t *adapter, int logdrv)
4184 unsigned long flags;
4185 scb_t *scb;
4186 int rval;
4189 * Stop sending commands to the controller, queue them internally.
4190 * When deletion is complete, ISR will flush the queue.
4192 atomic_set(&adapter->quiescent, 1);
4195 * Wait till all the issued commands are complete and there are no
4196 * commands in the pending queue
4198 while (atomic_read(&adapter->pend_cmds) > 0 ||
4199 !list_empty(&adapter->pending_list))
4200 msleep(1000); /* sleep for 1s */
4202 rval = mega_do_del_logdrv(adapter, logdrv);
4204 spin_lock_irqsave(&adapter->lock, flags);
4207 * If delete operation was successful, add 0x80 to the logical drive
4208 * ids for commands in the pending queue.
4210 if (adapter->read_ldidmap) {
4211 struct list_head *pos;
4212 list_for_each(pos, &adapter->pending_list) {
4213 scb = list_entry(pos, scb_t, list);
4214 if (scb->pthru->logdrv < 0x80 )
4215 scb->pthru->logdrv += 0x80;
4219 atomic_set(&adapter->quiescent, 0);
4221 mega_runpendq(adapter);
4223 spin_unlock_irqrestore(&adapter->lock, flags);
4225 return rval;
4229 static int
4230 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
4232 megacmd_t mc;
4233 int rval;
4235 memset( &mc, 0, sizeof(megacmd_t));
4237 mc.cmd = FC_DEL_LOGDRV;
4238 mc.opcode = OP_DEL_LOGDRV;
4239 mc.subopcode = logdrv;
4241 rval = mega_internal_command(adapter, &mc, NULL);
4243 /* log this event */
4244 if(rval) {
4245 printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
4246 return rval;
4250 * After deleting first logical drive, the logical drives must be
4251 * addressed by adding 0x80 to the logical drive id.
4253 adapter->read_ldidmap = 1;
4255 return rval;
4260 * mega_get_max_sgl()
4261 * @adapter - pointer to our soft state
4263 * Find out the maximum number of scatter-gather elements supported by this
4264 * version of the firmware
4266 static void
4267 mega_get_max_sgl(adapter_t *adapter)
4269 unsigned char raw_mbox[sizeof(struct mbox_out)];
4270 mbox_t *mbox;
4272 mbox = (mbox_t *)raw_mbox;
4274 memset(mbox, 0, sizeof(raw_mbox));
4276 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4278 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4280 raw_mbox[0] = MAIN_MISC_OPCODE;
4281 raw_mbox[2] = GET_MAX_SG_SUPPORT;
4284 if( issue_scb_block(adapter, raw_mbox) ) {
4286 * f/w does not support this command. Choose the default value
4288 adapter->sglen = MIN_SGLIST;
4290 else {
4291 adapter->sglen = *((char *)adapter->mega_buffer);
4294 * Make sure this is not more than the resources we are
4295 * planning to allocate
4297 if ( adapter->sglen > MAX_SGLIST )
4298 adapter->sglen = MAX_SGLIST;
4301 return;
4306 * mega_support_cluster()
4307 * @adapter - pointer to our soft state
4309 * Find out if this firmware support cluster calls.
4311 static int
4312 mega_support_cluster(adapter_t *adapter)
4314 unsigned char raw_mbox[sizeof(struct mbox_out)];
4315 mbox_t *mbox;
4317 mbox = (mbox_t *)raw_mbox;
4319 memset(mbox, 0, sizeof(raw_mbox));
4321 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4323 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4326 * Try to get the initiator id. This command will succeed iff the
4327 * clustering is available on this HBA.
4329 raw_mbox[0] = MEGA_GET_TARGET_ID;
4331 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
4334 * Cluster support available. Get the initiator target id.
4335 * Tell our id to mid-layer too.
4337 adapter->this_id = *(u32 *)adapter->mega_buffer;
4338 adapter->host->this_id = adapter->this_id;
4340 return 1;
4343 return 0;
4348 * mega_adapinq()
4349 * @adapter - pointer to our soft state
4350 * @dma_handle - DMA address of the buffer
4352 * Issue internal comamnds while interrupts are available.
4353 * We only issue direct mailbox commands from within the driver. ioctl()
4354 * interface using these routines can issue passthru commands.
4356 static int
4357 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
4359 megacmd_t mc;
4361 memset(&mc, 0, sizeof(megacmd_t));
4363 if( adapter->flag & BOARD_40LD ) {
4364 mc.cmd = FC_NEW_CONFIG;
4365 mc.opcode = NC_SUBOP_ENQUIRY3;
4366 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
4368 else {
4369 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
4372 mc.xferaddr = (u32)dma_handle;
4374 if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
4375 return -1;
4378 return 0;
4382 /** mega_internal_dev_inquiry()
4383 * @adapter - pointer to our soft state
4384 * @ch - channel for this device
4385 * @tgt - ID of this device
4386 * @buf_dma_handle - DMA address of the buffer
4388 * Issue the scsi inquiry for the specified device.
4390 static int
4391 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4392 dma_addr_t buf_dma_handle)
4394 mega_passthru *pthru;
4395 dma_addr_t pthru_dma_handle;
4396 megacmd_t mc;
4397 int rval;
4398 struct pci_dev *pdev;
4402 * For all internal commands, the buffer must be allocated in <4GB
4403 * address range
4405 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4407 pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4408 &pthru_dma_handle);
4410 if( pthru == NULL ) {
4411 free_local_pdev(pdev);
4412 return -1;
4415 pthru->timeout = 2;
4416 pthru->ars = 1;
4417 pthru->reqsenselen = 14;
4418 pthru->islogical = 0;
4420 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4422 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4424 pthru->cdblen = 6;
4426 pthru->cdb[0] = INQUIRY;
4427 pthru->cdb[1] = 0;
4428 pthru->cdb[2] = 0;
4429 pthru->cdb[3] = 0;
4430 pthru->cdb[4] = 255;
4431 pthru->cdb[5] = 0;
4434 pthru->dataxferaddr = (u32)buf_dma_handle;
4435 pthru->dataxferlen = 256;
4437 memset(&mc, 0, sizeof(megacmd_t));
4439 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4440 mc.xferaddr = (u32)pthru_dma_handle;
4442 rval = mega_internal_command(adapter, &mc, pthru);
4444 pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4445 pthru_dma_handle);
4447 free_local_pdev(pdev);
4449 return rval;
4454 * mega_internal_command()
4455 * @adapter - pointer to our soft state
4456 * @mc - the mailbox command
4457 * @pthru - Passthru structure for DCDB commands
4459 * Issue the internal commands in interrupt mode.
4460 * The last argument is the address of the passthru structure if the command
4461 * to be fired is a passthru command
4463 * lockscope specifies whether the caller has already acquired the lock. Of
4464 * course, the caller must know which lock we are talking about.
4466 * Note: parameter 'pthru' is null for non-passthru commands.
4468 static int
4469 mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4471 Scsi_Cmnd *scmd;
4472 struct scsi_device *sdev;
4473 scb_t *scb;
4474 int rval;
4477 * The internal commands share one command id and hence are
4478 * serialized. This is so because we want to reserve maximum number of
4479 * available command ids for the I/O commands.
4481 mutex_lock(&adapter->int_mtx);
4483 scb = &adapter->int_scb;
4484 memset(scb, 0, sizeof(scb_t));
4486 scmd = &adapter->int_scmd;
4487 memset(scmd, 0, sizeof(Scsi_Cmnd));
4489 sdev = kmalloc(sizeof(struct scsi_device), GFP_KERNEL);
4490 memset(sdev, 0, sizeof(struct scsi_device));
4491 scmd->device = sdev;
4493 scmd->device->host = adapter->host;
4494 scmd->request_buffer = (void *)scb;
4495 scmd->cmnd[0] = MEGA_INTERNAL_CMD;
4497 scb->state |= SCB_ACTIVE;
4498 scb->cmd = scmd;
4500 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4503 * Is it a passthru command
4505 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4507 scb->pthru = pthru;
4510 scb->idx = CMDID_INT_CMDS;
4512 megaraid_queue(scmd, mega_internal_done);
4514 wait_for_completion(&adapter->int_waitq);
4516 rval = scmd->result;
4517 mc->status = scmd->result;
4518 kfree(sdev);
4521 * Print a debug message for all failed commands. Applications can use
4522 * this information.
4524 if( scmd->result && trace_level ) {
4525 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4526 mc->cmd, mc->opcode, mc->subopcode, scmd->result);
4529 mutex_unlock(&adapter->int_mtx);
4531 return rval;
4536 * mega_internal_done()
4537 * @scmd - internal scsi command
4539 * Callback routine for internal commands.
4541 static void
4542 mega_internal_done(Scsi_Cmnd *scmd)
4544 adapter_t *adapter;
4546 adapter = (adapter_t *)scmd->device->host->hostdata;
4548 complete(&adapter->int_waitq);
4553 static struct scsi_host_template megaraid_template = {
4554 .module = THIS_MODULE,
4555 .name = "MegaRAID",
4556 .proc_name = "megaraid_legacy",
4557 .info = megaraid_info,
4558 .queuecommand = megaraid_queue,
4559 .bios_param = megaraid_biosparam,
4560 .max_sectors = MAX_SECTORS_PER_IO,
4561 .can_queue = MAX_COMMANDS,
4562 .this_id = DEFAULT_INITIATOR_ID,
4563 .sg_tablesize = MAX_SGLIST,
4564 .cmd_per_lun = DEF_CMD_PER_LUN,
4565 .use_clustering = ENABLE_CLUSTERING,
4566 .eh_abort_handler = megaraid_abort,
4567 .eh_device_reset_handler = megaraid_reset,
4568 .eh_bus_reset_handler = megaraid_reset,
4569 .eh_host_reset_handler = megaraid_reset,
4572 static int __devinit
4573 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4575 struct Scsi_Host *host;
4576 adapter_t *adapter;
4577 unsigned long mega_baseport, tbase, flag = 0;
4578 u16 subsysid, subsysvid;
4579 u8 pci_bus, pci_dev_func;
4580 int irq, i, j;
4581 int error = -ENODEV;
4583 if (pci_enable_device(pdev))
4584 goto out;
4585 pci_set_master(pdev);
4587 pci_bus = pdev->bus->number;
4588 pci_dev_func = pdev->devfn;
4591 * The megaraid3 stuff reports the ID of the Intel part which is not
4592 * remotely specific to the megaraid
4594 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4595 u16 magic;
4597 * Don't fall over the Compaq management cards using the same
4598 * PCI identifier
4600 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4601 pdev->subsystem_device == 0xC000)
4602 return -ENODEV;
4603 /* Now check the magic signature byte */
4604 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4605 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4606 return -ENODEV;
4607 /* Ok it is probably a megaraid */
4611 * For these vendor and device ids, signature offsets are not
4612 * valid and 64 bit is implicit
4614 if (id->driver_data & BOARD_64BIT)
4615 flag |= BOARD_64BIT;
4616 else {
4617 u32 magic64;
4619 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4620 if (magic64 == HBA_SIGNATURE_64BIT)
4621 flag |= BOARD_64BIT;
4624 subsysvid = pdev->subsystem_vendor;
4625 subsysid = pdev->subsystem_device;
4627 printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4628 id->vendor, id->device, pci_bus);
4630 printk("slot %d:func %d\n",
4631 PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
4633 /* Read the base port and IRQ from PCI */
4634 mega_baseport = pci_resource_start(pdev, 0);
4635 irq = pdev->irq;
4637 tbase = mega_baseport;
4638 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4639 flag |= BOARD_MEMMAP;
4641 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4642 printk(KERN_WARNING "megaraid: mem region busy!\n");
4643 goto out_disable_device;
4646 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4647 if (!mega_baseport) {
4648 printk(KERN_WARNING
4649 "megaraid: could not map hba memory\n");
4650 goto out_release_region;
4652 } else {
4653 flag |= BOARD_IOMAP;
4654 mega_baseport += 0x10;
4656 if (!request_region(mega_baseport, 16, "megaraid"))
4657 goto out_disable_device;
4660 /* Initialize SCSI Host structure */
4661 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4662 if (!host)
4663 goto out_iounmap;
4665 adapter = (adapter_t *)host->hostdata;
4666 memset(adapter, 0, sizeof(adapter_t));
4668 printk(KERN_NOTICE
4669 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4670 host->host_no, mega_baseport, irq);
4672 adapter->base = mega_baseport;
4674 INIT_LIST_HEAD(&adapter->free_list);
4675 INIT_LIST_HEAD(&adapter->pending_list);
4676 INIT_LIST_HEAD(&adapter->completed_list);
4678 adapter->flag = flag;
4679 spin_lock_init(&adapter->lock);
4681 host->cmd_per_lun = max_cmd_per_lun;
4682 host->max_sectors = max_sectors_per_io;
4684 adapter->dev = pdev;
4685 adapter->host = host;
4687 adapter->host->irq = irq;
4689 if (flag & BOARD_MEMMAP)
4690 adapter->host->base = tbase;
4691 else {
4692 adapter->host->io_port = tbase;
4693 adapter->host->n_io_port = 16;
4696 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4699 * Allocate buffer to issue internal commands.
4701 adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4702 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4703 if (!adapter->mega_buffer) {
4704 printk(KERN_WARNING "megaraid: out of RAM.\n");
4705 goto out_host_put;
4708 adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4709 if (!adapter->scb_list) {
4710 printk(KERN_WARNING "megaraid: out of RAM.\n");
4711 goto out_free_cmd_buffer;
4714 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4715 megaraid_isr_memmapped : megaraid_isr_iomapped,
4716 IRQF_SHARED, "megaraid", adapter)) {
4717 printk(KERN_WARNING
4718 "megaraid: Couldn't register IRQ %d!\n", irq);
4719 goto out_free_scb_list;
4722 if (mega_setup_mailbox(adapter))
4723 goto out_free_irq;
4725 if (mega_query_adapter(adapter))
4726 goto out_free_mbox;
4729 * Have checks for some buggy f/w
4731 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4733 * Which firmware
4735 if (!strcmp(adapter->fw_version, "3.00") ||
4736 !strcmp(adapter->fw_version, "3.01")) {
4738 printk( KERN_WARNING
4739 "megaraid: Your card is a Dell PERC "
4740 "2/SC RAID controller with "
4741 "firmware\nmegaraid: 3.00 or 3.01. "
4742 "This driver is known to have "
4743 "corruption issues\nmegaraid: with "
4744 "those firmware versions on this "
4745 "specific card. In order\nmegaraid: "
4746 "to protect your data, please upgrade "
4747 "your firmware to version\nmegaraid: "
4748 "3.10 or later, available from the "
4749 "Dell Technical Support web\n"
4750 "megaraid: site at\nhttp://support."
4751 "dell.com/us/en/filelib/download/"
4752 "index.asp?fileid=2940\n"
4758 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4759 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4760 * support, since this firmware cannot handle 64 bit
4761 * addressing
4763 if ((subsysvid == HP_SUBSYS_VID) &&
4764 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4766 * which firmware
4768 if (!strcmp(adapter->fw_version, "H01.07") ||
4769 !strcmp(adapter->fw_version, "H01.08") ||
4770 !strcmp(adapter->fw_version, "H01.09") ) {
4771 printk(KERN_WARNING
4772 "megaraid: Firmware H.01.07, "
4773 "H.01.08, and H.01.09 on 1M/2M "
4774 "controllers\n"
4775 "megaraid: do not support 64 bit "
4776 "addressing.\nmegaraid: DISABLING "
4777 "64 bit support.\n");
4778 adapter->flag &= ~BOARD_64BIT;
4782 if (mega_is_bios_enabled(adapter))
4783 mega_hbas[hba_count].is_bios_enabled = 1;
4784 mega_hbas[hba_count].hostdata_addr = adapter;
4787 * Find out which channel is raid and which is scsi. This is
4788 * for ROMB support.
4790 mega_enum_raid_scsi(adapter);
4793 * Find out if a logical drive is set as the boot drive. If
4794 * there is one, will make that as the first logical drive.
4795 * ROMB: Do we have to boot from a physical drive. Then all
4796 * the physical drives would appear before the logical disks.
4797 * Else, all the physical drives would be exported to the mid
4798 * layer after logical drives.
4800 mega_get_boot_drv(adapter);
4802 if (adapter->boot_pdrv_enabled) {
4803 j = adapter->product_info.nchannels;
4804 for( i = 0; i < j; i++ )
4805 adapter->logdrv_chan[i] = 0;
4806 for( i = j; i < NVIRT_CHAN + j; i++ )
4807 adapter->logdrv_chan[i] = 1;
4808 } else {
4809 for (i = 0; i < NVIRT_CHAN; i++)
4810 adapter->logdrv_chan[i] = 1;
4811 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4812 adapter->logdrv_chan[i] = 0;
4813 adapter->mega_ch_class <<= NVIRT_CHAN;
4817 * Do we support random deletion and addition of logical
4818 * drives
4820 adapter->read_ldidmap = 0; /* set it after first logdrv
4821 delete cmd */
4822 adapter->support_random_del = mega_support_random_del(adapter);
4824 /* Initialize SCBs */
4825 if (mega_init_scb(adapter))
4826 goto out_free_mbox;
4829 * Reset the pending commands counter
4831 atomic_set(&adapter->pend_cmds, 0);
4834 * Reset the adapter quiescent flag
4836 atomic_set(&adapter->quiescent, 0);
4838 hba_soft_state[hba_count] = adapter;
4841 * Fill in the structure which needs to be passed back to the
4842 * application when it does an ioctl() for controller related
4843 * information.
4845 i = hba_count;
4847 mcontroller[i].base = mega_baseport;
4848 mcontroller[i].irq = irq;
4849 mcontroller[i].numldrv = adapter->numldrv;
4850 mcontroller[i].pcibus = pci_bus;
4851 mcontroller[i].pcidev = id->device;
4852 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4853 mcontroller[i].pciid = -1;
4854 mcontroller[i].pcivendor = id->vendor;
4855 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4856 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4859 /* Set the Mode of addressing to 64 bit if we can */
4860 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4861 pci_set_dma_mask(pdev, DMA_64BIT_MASK);
4862 adapter->has_64bit_addr = 1;
4863 } else {
4864 pci_set_dma_mask(pdev, DMA_32BIT_MASK);
4865 adapter->has_64bit_addr = 0;
4868 mutex_init(&adapter->int_mtx);
4869 init_completion(&adapter->int_waitq);
4871 adapter->this_id = DEFAULT_INITIATOR_ID;
4872 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4874 #if MEGA_HAVE_CLUSTERING
4876 * Is cluster support enabled on this controller
4877 * Note: In a cluster the HBAs ( the initiators ) will have
4878 * different target IDs and we cannot assume it to be 7. Call
4879 * to mega_support_cluster() will get the target ids also if
4880 * the cluster support is available
4882 adapter->has_cluster = mega_support_cluster(adapter);
4883 if (adapter->has_cluster) {
4884 printk(KERN_NOTICE
4885 "megaraid: Cluster driver, initiator id:%d\n",
4886 adapter->this_id);
4888 #endif
4890 pci_set_drvdata(pdev, host);
4892 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4894 error = scsi_add_host(host, &pdev->dev);
4895 if (error)
4896 goto out_free_mbox;
4898 scsi_scan_host(host);
4899 hba_count++;
4900 return 0;
4902 out_free_mbox:
4903 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4904 adapter->una_mbox64, adapter->una_mbox64_dma);
4905 out_free_irq:
4906 free_irq(adapter->host->irq, adapter);
4907 out_free_scb_list:
4908 kfree(adapter->scb_list);
4909 out_free_cmd_buffer:
4910 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4911 adapter->mega_buffer, adapter->buf_dma_handle);
4912 out_host_put:
4913 scsi_host_put(host);
4914 out_iounmap:
4915 if (flag & BOARD_MEMMAP)
4916 iounmap((void *)mega_baseport);
4917 out_release_region:
4918 if (flag & BOARD_MEMMAP)
4919 release_mem_region(tbase, 128);
4920 else
4921 release_region(mega_baseport, 16);
4922 out_disable_device:
4923 pci_disable_device(pdev);
4924 out:
4925 return error;
4928 static void
4929 __megaraid_shutdown(adapter_t *adapter)
4931 u_char raw_mbox[sizeof(struct mbox_out)];
4932 mbox_t *mbox = (mbox_t *)raw_mbox;
4933 int i;
4935 /* Flush adapter cache */
4936 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4937 raw_mbox[0] = FLUSH_ADAPTER;
4939 free_irq(adapter->host->irq, adapter);
4941 /* Issue a blocking (interrupts disabled) command to the card */
4942 issue_scb_block(adapter, raw_mbox);
4944 /* Flush disks cache */
4945 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4946 raw_mbox[0] = FLUSH_SYSTEM;
4948 /* Issue a blocking (interrupts disabled) command to the card */
4949 issue_scb_block(adapter, raw_mbox);
4951 if (atomic_read(&adapter->pend_cmds) > 0)
4952 printk(KERN_WARNING "megaraid: pending commands!!\n");
4955 * Have a delibrate delay to make sure all the caches are
4956 * actually flushed.
4958 for (i = 0; i <= 10; i++)
4959 mdelay(1000);
4962 static void
4963 megaraid_remove_one(struct pci_dev *pdev)
4965 struct Scsi_Host *host = pci_get_drvdata(pdev);
4966 adapter_t *adapter = (adapter_t *)host->hostdata;
4967 char buf[12] = { 0 };
4969 scsi_remove_host(host);
4971 __megaraid_shutdown(adapter);
4973 /* Free our resources */
4974 if (adapter->flag & BOARD_MEMMAP) {
4975 iounmap((void *)adapter->base);
4976 release_mem_region(adapter->host->base, 128);
4977 } else
4978 release_region(adapter->base, 16);
4980 mega_free_sgl(adapter);
4982 #ifdef CONFIG_PROC_FS
4983 if (adapter->controller_proc_dir_entry) {
4984 remove_proc_entry("stat", adapter->controller_proc_dir_entry);
4985 remove_proc_entry("config",
4986 adapter->controller_proc_dir_entry);
4987 remove_proc_entry("mailbox",
4988 adapter->controller_proc_dir_entry);
4989 #if MEGA_HAVE_ENH_PROC
4990 remove_proc_entry("rebuild-rate",
4991 adapter->controller_proc_dir_entry);
4992 remove_proc_entry("battery-status",
4993 adapter->controller_proc_dir_entry);
4995 remove_proc_entry("diskdrives-ch0",
4996 adapter->controller_proc_dir_entry);
4997 remove_proc_entry("diskdrives-ch1",
4998 adapter->controller_proc_dir_entry);
4999 remove_proc_entry("diskdrives-ch2",
5000 adapter->controller_proc_dir_entry);
5001 remove_proc_entry("diskdrives-ch3",
5002 adapter->controller_proc_dir_entry);
5004 remove_proc_entry("raiddrives-0-9",
5005 adapter->controller_proc_dir_entry);
5006 remove_proc_entry("raiddrives-10-19",
5007 adapter->controller_proc_dir_entry);
5008 remove_proc_entry("raiddrives-20-29",
5009 adapter->controller_proc_dir_entry);
5010 remove_proc_entry("raiddrives-30-39",
5011 adapter->controller_proc_dir_entry);
5012 #endif
5013 sprintf(buf, "hba%d", adapter->host->host_no);
5014 remove_proc_entry(buf, mega_proc_dir_entry);
5016 #endif
5018 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
5019 adapter->mega_buffer, adapter->buf_dma_handle);
5020 kfree(adapter->scb_list);
5021 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
5022 adapter->una_mbox64, adapter->una_mbox64_dma);
5024 scsi_host_put(host);
5025 pci_disable_device(pdev);
5027 hba_count--;
5030 static void
5031 megaraid_shutdown(struct pci_dev *pdev)
5033 struct Scsi_Host *host = pci_get_drvdata(pdev);
5034 adapter_t *adapter = (adapter_t *)host->hostdata;
5036 __megaraid_shutdown(adapter);
5039 static struct pci_device_id megaraid_pci_tbl[] = {
5040 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
5041 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5042 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
5043 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5044 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
5045 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
5046 {0,}
5048 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
5050 static struct pci_driver megaraid_pci_driver = {
5051 .name = "megaraid_legacy",
5052 .id_table = megaraid_pci_tbl,
5053 .probe = megaraid_probe_one,
5054 .remove = __devexit_p(megaraid_remove_one),
5055 .shutdown = megaraid_shutdown,
5058 static int __init megaraid_init(void)
5060 int error;
5062 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
5063 max_cmd_per_lun = MAX_CMD_PER_LUN;
5064 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
5065 max_mbox_busy_wait = MBOX_BUSY_WAIT;
5067 #ifdef CONFIG_PROC_FS
5068 mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
5069 if (!mega_proc_dir_entry) {
5070 printk(KERN_WARNING
5071 "megaraid: failed to create megaraid root\n");
5073 #endif
5074 error = pci_module_init(&megaraid_pci_driver);
5075 if (error) {
5076 #ifdef CONFIG_PROC_FS
5077 remove_proc_entry("megaraid", &proc_root);
5078 #endif
5079 return error;
5083 * Register the driver as a character device, for applications
5084 * to access it for ioctls.
5085 * First argument (major) to register_chrdev implies a dynamic
5086 * major number allocation.
5088 major = register_chrdev(0, "megadev_legacy", &megadev_fops);
5089 if (!major) {
5090 printk(KERN_WARNING
5091 "megaraid: failed to register char device\n");
5094 return 0;
5097 static void __exit megaraid_exit(void)
5100 * Unregister the character device interface to the driver.
5102 unregister_chrdev(major, "megadev_legacy");
5104 pci_unregister_driver(&megaraid_pci_driver);
5106 #ifdef CONFIG_PROC_FS
5107 remove_proc_entry("megaraid", &proc_root);
5108 #endif
5111 module_init(megaraid_init);
5112 module_exit(megaraid_exit);
5114 /* vi: set ts=8 sw=8 tw=78: */