Merge tag 'for-usb-next-2013-06-17' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / drivers / scsi / megaraid.c
blob846f475f62c160890835f33a234f85c6a175a222
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/seq_file.h>
43 #include <linux/reboot.h>
44 #include <linux/module.h>
45 #include <linux/list.h>
46 #include <linux/interrupt.h>
47 #include <linux/pci.h>
48 #include <linux/init.h>
49 #include <linux/dma-mapping.h>
50 #include <linux/mutex.h>
51 #include <linux/slab.h>
52 #include <scsi/scsicam.h>
54 #include "scsi.h"
55 #include <scsi/scsi_host.h>
57 #include "megaraid.h"
59 #define MEGARAID_MODULE_VERSION "2.00.4"
61 MODULE_AUTHOR ("sju@lsil.com");
62 MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
63 MODULE_LICENSE ("GPL");
64 MODULE_VERSION(MEGARAID_MODULE_VERSION);
66 static DEFINE_MUTEX(megadev_mutex);
67 static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
68 module_param(max_cmd_per_lun, uint, 0);
69 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)");
71 static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
72 module_param(max_sectors_per_io, ushort, 0);
73 MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
76 static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
77 module_param(max_mbox_busy_wait, ushort, 0);
78 MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
80 #define RDINDOOR(adapter) readl((adapter)->mmio_base + 0x20)
81 #define RDOUTDOOR(adapter) readl((adapter)->mmio_base + 0x2C)
82 #define WRINDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x20)
83 #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
86 * Global variables
89 static int hba_count;
90 static adapter_t *hba_soft_state[MAX_CONTROLLERS];
91 static struct proc_dir_entry *mega_proc_dir_entry;
93 /* For controller re-ordering */
94 static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
96 static long
97 megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
100 * The File Operations structure for the serial/ioctl interface of the driver
102 static const struct file_operations megadev_fops = {
103 .owner = THIS_MODULE,
104 .unlocked_ioctl = megadev_unlocked_ioctl,
105 .open = megadev_open,
106 .llseek = noop_llseek,
110 * Array to structures for storing the information about the controllers. This
111 * information is sent to the user level applications, when they do an ioctl
112 * for this information.
114 static struct mcontroller mcontroller[MAX_CONTROLLERS];
116 /* The current driver version */
117 static u32 driver_ver = 0x02000000;
119 /* major number used by the device for character interface */
120 static int major;
122 #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
126 * Debug variable to print some diagnostic messages
128 static int trace_level;
131 * mega_setup_mailbox()
132 * @adapter - pointer to our soft state
134 * Allocates a 8 byte aligned memory for the handshake mailbox.
136 static int
137 mega_setup_mailbox(adapter_t *adapter)
139 unsigned long align;
141 adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
142 sizeof(mbox64_t), &adapter->una_mbox64_dma);
144 if( !adapter->una_mbox64 ) return -1;
146 adapter->mbox = &adapter->una_mbox64->mbox;
148 adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
149 (~0UL ^ 0xFUL));
151 adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
153 align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
155 adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
158 * Register the mailbox if the controller is an io-mapped controller
160 if( adapter->flag & BOARD_IOMAP ) {
162 outb(adapter->mbox_dma & 0xFF,
163 adapter->host->io_port + MBOX_PORT0);
165 outb((adapter->mbox_dma >> 8) & 0xFF,
166 adapter->host->io_port + MBOX_PORT1);
168 outb((adapter->mbox_dma >> 16) & 0xFF,
169 adapter->host->io_port + MBOX_PORT2);
171 outb((adapter->mbox_dma >> 24) & 0xFF,
172 adapter->host->io_port + MBOX_PORT3);
174 outb(ENABLE_MBOX_BYTE,
175 adapter->host->io_port + ENABLE_MBOX_REGION);
177 irq_ack(adapter);
179 irq_enable(adapter);
182 return 0;
187 * mega_query_adapter()
188 * @adapter - pointer to our soft state
190 * Issue the adapter inquiry commands to the controller and find out
191 * information and parameter about the devices attached
193 static int
194 mega_query_adapter(adapter_t *adapter)
196 dma_addr_t prod_info_dma_handle;
197 mega_inquiry3 *inquiry3;
198 u8 raw_mbox[sizeof(struct mbox_out)];
199 mbox_t *mbox;
200 int retval;
202 /* Initialize adapter inquiry mailbox */
204 mbox = (mbox_t *)raw_mbox;
206 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
207 memset(&mbox->m_out, 0, sizeof(raw_mbox));
210 * Try to issue Inquiry3 command
211 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
212 * update enquiry3 structure
214 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
216 inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
218 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
219 raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
220 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
222 /* Issue a blocking command to the card */
223 if ((retval = issue_scb_block(adapter, raw_mbox))) {
224 /* the adapter does not support 40ld */
226 mraid_ext_inquiry *ext_inq;
227 mraid_inquiry *inq;
228 dma_addr_t dma_handle;
230 ext_inq = pci_alloc_consistent(adapter->dev,
231 sizeof(mraid_ext_inquiry), &dma_handle);
233 if( ext_inq == NULL ) return -1;
235 inq = &ext_inq->raid_inq;
237 mbox->m_out.xferaddr = (u32)dma_handle;
239 /*issue old 0x04 command to adapter */
240 mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
242 issue_scb_block(adapter, raw_mbox);
245 * update Enquiry3 and ProductInfo structures with
246 * mraid_inquiry structure
248 mega_8_to_40ld(inq, inquiry3,
249 (mega_product_info *)&adapter->product_info);
251 pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
252 ext_inq, dma_handle);
254 } else { /*adapter supports 40ld */
255 adapter->flag |= BOARD_40LD;
258 * get product_info, which is static information and will be
259 * unchanged
261 prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
262 &adapter->product_info,
263 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
265 mbox->m_out.xferaddr = prod_info_dma_handle;
267 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
268 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
270 if ((retval = issue_scb_block(adapter, raw_mbox)))
271 printk(KERN_WARNING
272 "megaraid: Product_info cmd failed with error: %d\n",
273 retval);
275 pci_unmap_single(adapter->dev, prod_info_dma_handle,
276 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
281 * kernel scans the channels from 0 to <= max_channel
283 adapter->host->max_channel =
284 adapter->product_info.nchannels + NVIRT_CHAN -1;
286 adapter->host->max_id = 16; /* max targets per channel */
288 adapter->host->max_lun = 7; /* Up to 7 luns for non disk devices */
290 adapter->host->cmd_per_lun = max_cmd_per_lun;
292 adapter->numldrv = inquiry3->num_ldrv;
294 adapter->max_cmds = adapter->product_info.max_commands;
296 if(adapter->max_cmds > MAX_COMMANDS)
297 adapter->max_cmds = MAX_COMMANDS;
299 adapter->host->can_queue = adapter->max_cmds - 1;
302 * Get the maximum number of scatter-gather elements supported by this
303 * firmware
305 mega_get_max_sgl(adapter);
307 adapter->host->sg_tablesize = adapter->sglen;
309 /* use HP firmware and bios version encoding
310 Note: fw_version[0|1] and bios_version[0|1] were originally shifted
311 right 8 bits making them zero. This 0 value was hardcoded to fix
312 sparse warnings. */
313 if (adapter->product_info.subsysvid == PCI_VENDOR_ID_HP) {
314 sprintf (adapter->fw_version, "%c%d%d.%d%d",
315 adapter->product_info.fw_version[2],
317 adapter->product_info.fw_version[1] & 0x0f,
319 adapter->product_info.fw_version[0] & 0x0f);
320 sprintf (adapter->bios_version, "%c%d%d.%d%d",
321 adapter->product_info.bios_version[2],
323 adapter->product_info.bios_version[1] & 0x0f,
325 adapter->product_info.bios_version[0] & 0x0f);
326 } else {
327 memcpy(adapter->fw_version,
328 (char *)adapter->product_info.fw_version, 4);
329 adapter->fw_version[4] = 0;
331 memcpy(adapter->bios_version,
332 (char *)adapter->product_info.bios_version, 4);
334 adapter->bios_version[4] = 0;
337 printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
338 adapter->fw_version, adapter->bios_version, adapter->numldrv);
341 * Do we support extended (>10 bytes) cdbs
343 adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
344 if (adapter->support_ext_cdb)
345 printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
348 return 0;
352 * mega_runpendq()
353 * @adapter - pointer to our soft state
355 * Runs through the list of pending requests.
357 static inline void
358 mega_runpendq(adapter_t *adapter)
360 if(!list_empty(&adapter->pending_list))
361 __mega_runpendq(adapter);
365 * megaraid_queue()
366 * @scmd - Issue this scsi command
367 * @done - the callback hook into the scsi mid-layer
369 * The command queuing entry point for the mid-layer.
371 static int
372 megaraid_queue_lck(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
374 adapter_t *adapter;
375 scb_t *scb;
376 int busy=0;
377 unsigned long flags;
379 adapter = (adapter_t *)scmd->device->host->hostdata;
381 scmd->scsi_done = done;
385 * Allocate and build a SCB request
386 * busy flag will be set if mega_build_cmd() command could not
387 * allocate scb. We will return non-zero status in that case.
388 * NOTE: scb can be null even though certain commands completed
389 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
390 * return 0 in that case.
393 spin_lock_irqsave(&adapter->lock, flags);
394 scb = mega_build_cmd(adapter, scmd, &busy);
395 if (!scb)
396 goto out;
398 scb->state |= SCB_PENDQ;
399 list_add_tail(&scb->list, &adapter->pending_list);
402 * Check if the HBA is in quiescent state, e.g., during a
403 * delete logical drive opertion. If it is, don't run
404 * the pending_list.
406 if (atomic_read(&adapter->quiescent) == 0)
407 mega_runpendq(adapter);
409 busy = 0;
410 out:
411 spin_unlock_irqrestore(&adapter->lock, flags);
412 return busy;
415 static DEF_SCSI_QCMD(megaraid_queue)
418 * mega_allocate_scb()
419 * @adapter - pointer to our soft state
420 * @cmd - scsi command from the mid-layer
422 * Allocate a SCB structure. This is the central structure for controller
423 * commands.
425 static inline scb_t *
426 mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
428 struct list_head *head = &adapter->free_list;
429 scb_t *scb;
431 /* Unlink command from Free List */
432 if( !list_empty(head) ) {
434 scb = list_entry(head->next, scb_t, list);
436 list_del_init(head->next);
438 scb->state = SCB_ACTIVE;
439 scb->cmd = cmd;
440 scb->dma_type = MEGA_DMA_TYPE_NONE;
442 return scb;
445 return NULL;
449 * mega_get_ldrv_num()
450 * @adapter - pointer to our soft state
451 * @cmd - scsi mid layer command
452 * @channel - channel on the controller
454 * Calculate the logical drive number based on the information in scsi command
455 * and the channel number.
457 static inline int
458 mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
460 int tgt;
461 int ldrv_num;
463 tgt = cmd->device->id;
465 if ( tgt > adapter->this_id )
466 tgt--; /* we do not get inquires for initiator id */
468 ldrv_num = (channel * 15) + tgt;
472 * If we have a logical drive with boot enabled, project it first
474 if( adapter->boot_ldrv_enabled ) {
475 if( ldrv_num == 0 ) {
476 ldrv_num = adapter->boot_ldrv;
478 else {
479 if( ldrv_num <= adapter->boot_ldrv ) {
480 ldrv_num--;
486 * If "delete logical drive" feature is enabled on this controller.
487 * Do only if at least one delete logical drive operation was done.
489 * Also, after logical drive deletion, instead of logical drive number,
490 * the value returned should be 0x80+logical drive id.
492 * These is valid only for IO commands.
495 if (adapter->support_random_del && adapter->read_ldidmap )
496 switch (cmd->cmnd[0]) {
497 case READ_6: /* fall through */
498 case WRITE_6: /* fall through */
499 case READ_10: /* fall through */
500 case WRITE_10:
501 ldrv_num += 0x80;
504 return ldrv_num;
508 * mega_build_cmd()
509 * @adapter - pointer to our soft state
510 * @cmd - Prepare using this scsi command
511 * @busy - busy flag if no resources
513 * Prepares a command and scatter gather list for the controller. This routine
514 * also finds out if the commands is intended for a logical drive or a
515 * physical device and prepares the controller command accordingly.
517 * We also re-order the logical drives and physical devices based on their
518 * boot settings.
520 static scb_t *
521 mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
523 mega_ext_passthru *epthru;
524 mega_passthru *pthru;
525 scb_t *scb;
526 mbox_t *mbox;
527 u32 seg;
528 char islogical;
529 int max_ldrv_num;
530 int channel = 0;
531 int target = 0;
532 int ldrv_num = 0; /* logical drive number */
536 * filter the internal and ioctl commands
538 if((cmd->cmnd[0] == MEGA_INTERNAL_CMD))
539 return (scb_t *)cmd->host_scribble;
542 * We know what channels our logical drives are on - mega_find_card()
544 islogical = adapter->logdrv_chan[cmd->device->channel];
547 * The theory: If physical drive is chosen for boot, all the physical
548 * devices are exported before the logical drives, otherwise physical
549 * devices are pushed after logical drives, in which case - Kernel sees
550 * the physical devices on virtual channel which is obviously converted
551 * to actual channel on the HBA.
553 if( adapter->boot_pdrv_enabled ) {
554 if( islogical ) {
555 /* logical channel */
556 channel = cmd->device->channel -
557 adapter->product_info.nchannels;
559 else {
560 /* this is physical channel */
561 channel = cmd->device->channel;
562 target = cmd->device->id;
565 * boot from a physical disk, that disk needs to be
566 * exposed first IF both the channels are SCSI, then
567 * booting from the second channel is not allowed.
569 if( target == 0 ) {
570 target = adapter->boot_pdrv_tgt;
572 else if( target == adapter->boot_pdrv_tgt ) {
573 target = 0;
577 else {
578 if( islogical ) {
579 /* this is the logical channel */
580 channel = cmd->device->channel;
582 else {
583 /* physical channel */
584 channel = cmd->device->channel - NVIRT_CHAN;
585 target = cmd->device->id;
590 if(islogical) {
592 /* have just LUN 0 for each target on virtual channels */
593 if (cmd->device->lun) {
594 cmd->result = (DID_BAD_TARGET << 16);
595 cmd->scsi_done(cmd);
596 return NULL;
599 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
602 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
603 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
606 * max_ldrv_num increases by 0x80 if some logical drive was
607 * deleted.
609 if(adapter->read_ldidmap)
610 max_ldrv_num += 0x80;
612 if(ldrv_num > max_ldrv_num ) {
613 cmd->result = (DID_BAD_TARGET << 16);
614 cmd->scsi_done(cmd);
615 return NULL;
619 else {
620 if( cmd->device->lun > 7) {
622 * Do not support lun >7 for physically accessed
623 * devices
625 cmd->result = (DID_BAD_TARGET << 16);
626 cmd->scsi_done(cmd);
627 return NULL;
633 * Logical drive commands
636 if(islogical) {
637 switch (cmd->cmnd[0]) {
638 case TEST_UNIT_READY:
639 #if MEGA_HAVE_CLUSTERING
641 * Do we support clustering and is the support enabled
642 * If no, return success always
644 if( !adapter->has_cluster ) {
645 cmd->result = (DID_OK << 16);
646 cmd->scsi_done(cmd);
647 return NULL;
650 if(!(scb = mega_allocate_scb(adapter, cmd))) {
651 *busy = 1;
652 return NULL;
655 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
656 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
657 scb->raw_mbox[3] = ldrv_num;
659 scb->dma_direction = PCI_DMA_NONE;
661 return scb;
662 #else
663 cmd->result = (DID_OK << 16);
664 cmd->scsi_done(cmd);
665 return NULL;
666 #endif
668 case MODE_SENSE: {
669 char *buf;
670 struct scatterlist *sg;
672 sg = scsi_sglist(cmd);
673 buf = kmap_atomic(sg_page(sg)) + sg->offset;
675 memset(buf, 0, cmd->cmnd[4]);
676 kunmap_atomic(buf - sg->offset);
678 cmd->result = (DID_OK << 16);
679 cmd->scsi_done(cmd);
680 return NULL;
683 case READ_CAPACITY:
684 case INQUIRY:
686 if(!(adapter->flag & (1L << cmd->device->channel))) {
688 printk(KERN_NOTICE
689 "scsi%d: scanning scsi channel %d ",
690 adapter->host->host_no,
691 cmd->device->channel);
692 printk("for logical drives.\n");
694 adapter->flag |= (1L << cmd->device->channel);
697 /* Allocate a SCB and initialize passthru */
698 if(!(scb = mega_allocate_scb(adapter, cmd))) {
699 *busy = 1;
700 return NULL;
702 pthru = scb->pthru;
704 mbox = (mbox_t *)scb->raw_mbox;
705 memset(mbox, 0, sizeof(scb->raw_mbox));
706 memset(pthru, 0, sizeof(mega_passthru));
708 pthru->timeout = 0;
709 pthru->ars = 1;
710 pthru->reqsenselen = 14;
711 pthru->islogical = 1;
712 pthru->logdrv = ldrv_num;
713 pthru->cdblen = cmd->cmd_len;
714 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
716 if( adapter->has_64bit_addr ) {
717 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
719 else {
720 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
723 scb->dma_direction = PCI_DMA_FROMDEVICE;
725 pthru->numsgelements = mega_build_sglist(adapter, scb,
726 &pthru->dataxferaddr, &pthru->dataxferlen);
728 mbox->m_out.xferaddr = scb->pthru_dma_addr;
730 return scb;
732 case READ_6:
733 case WRITE_6:
734 case READ_10:
735 case WRITE_10:
736 case READ_12:
737 case WRITE_12:
739 /* Allocate a SCB and initialize mailbox */
740 if(!(scb = mega_allocate_scb(adapter, cmd))) {
741 *busy = 1;
742 return NULL;
744 mbox = (mbox_t *)scb->raw_mbox;
746 memset(mbox, 0, sizeof(scb->raw_mbox));
747 mbox->m_out.logdrv = ldrv_num;
750 * A little hack: 2nd bit is zero for all scsi read
751 * commands and is set for all scsi write commands
753 if( adapter->has_64bit_addr ) {
754 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
755 MEGA_MBOXCMD_LWRITE64:
756 MEGA_MBOXCMD_LREAD64 ;
758 else {
759 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
760 MEGA_MBOXCMD_LWRITE:
761 MEGA_MBOXCMD_LREAD ;
765 * 6-byte READ(0x08) or WRITE(0x0A) cdb
767 if( cmd->cmd_len == 6 ) {
768 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
769 mbox->m_out.lba =
770 ((u32)cmd->cmnd[1] << 16) |
771 ((u32)cmd->cmnd[2] << 8) |
772 (u32)cmd->cmnd[3];
774 mbox->m_out.lba &= 0x1FFFFF;
776 #if MEGA_HAVE_STATS
778 * Take modulo 0x80, since the logical drive
779 * number increases by 0x80 when a logical
780 * drive was deleted
782 if (*cmd->cmnd == READ_6) {
783 adapter->nreads[ldrv_num%0x80]++;
784 adapter->nreadblocks[ldrv_num%0x80] +=
785 mbox->m_out.numsectors;
786 } else {
787 adapter->nwrites[ldrv_num%0x80]++;
788 adapter->nwriteblocks[ldrv_num%0x80] +=
789 mbox->m_out.numsectors;
791 #endif
795 * 10-byte READ(0x28) or WRITE(0x2A) cdb
797 if( cmd->cmd_len == 10 ) {
798 mbox->m_out.numsectors =
799 (u32)cmd->cmnd[8] |
800 ((u32)cmd->cmnd[7] << 8);
801 mbox->m_out.lba =
802 ((u32)cmd->cmnd[2] << 24) |
803 ((u32)cmd->cmnd[3] << 16) |
804 ((u32)cmd->cmnd[4] << 8) |
805 (u32)cmd->cmnd[5];
807 #if MEGA_HAVE_STATS
808 if (*cmd->cmnd == READ_10) {
809 adapter->nreads[ldrv_num%0x80]++;
810 adapter->nreadblocks[ldrv_num%0x80] +=
811 mbox->m_out.numsectors;
812 } else {
813 adapter->nwrites[ldrv_num%0x80]++;
814 adapter->nwriteblocks[ldrv_num%0x80] +=
815 mbox->m_out.numsectors;
817 #endif
821 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
823 if( cmd->cmd_len == 12 ) {
824 mbox->m_out.lba =
825 ((u32)cmd->cmnd[2] << 24) |
826 ((u32)cmd->cmnd[3] << 16) |
827 ((u32)cmd->cmnd[4] << 8) |
828 (u32)cmd->cmnd[5];
830 mbox->m_out.numsectors =
831 ((u32)cmd->cmnd[6] << 24) |
832 ((u32)cmd->cmnd[7] << 16) |
833 ((u32)cmd->cmnd[8] << 8) |
834 (u32)cmd->cmnd[9];
836 #if MEGA_HAVE_STATS
837 if (*cmd->cmnd == READ_12) {
838 adapter->nreads[ldrv_num%0x80]++;
839 adapter->nreadblocks[ldrv_num%0x80] +=
840 mbox->m_out.numsectors;
841 } else {
842 adapter->nwrites[ldrv_num%0x80]++;
843 adapter->nwriteblocks[ldrv_num%0x80] +=
844 mbox->m_out.numsectors;
846 #endif
850 * If it is a read command
852 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
853 scb->dma_direction = PCI_DMA_FROMDEVICE;
855 else {
856 scb->dma_direction = PCI_DMA_TODEVICE;
859 /* Calculate Scatter-Gather info */
860 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
861 (u32 *)&mbox->m_out.xferaddr, &seg);
863 return scb;
865 #if MEGA_HAVE_CLUSTERING
866 case RESERVE: /* Fall through */
867 case RELEASE:
870 * Do we support clustering and is the support enabled
872 if( ! adapter->has_cluster ) {
874 cmd->result = (DID_BAD_TARGET << 16);
875 cmd->scsi_done(cmd);
876 return NULL;
879 /* Allocate a SCB and initialize mailbox */
880 if(!(scb = mega_allocate_scb(adapter, cmd))) {
881 *busy = 1;
882 return NULL;
885 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
886 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
887 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
889 scb->raw_mbox[3] = ldrv_num;
891 scb->dma_direction = PCI_DMA_NONE;
893 return scb;
894 #endif
896 default:
897 cmd->result = (DID_BAD_TARGET << 16);
898 cmd->scsi_done(cmd);
899 return NULL;
904 * Passthru drive commands
906 else {
907 /* Allocate a SCB and initialize passthru */
908 if(!(scb = mega_allocate_scb(adapter, cmd))) {
909 *busy = 1;
910 return NULL;
913 mbox = (mbox_t *)scb->raw_mbox;
914 memset(mbox, 0, sizeof(scb->raw_mbox));
916 if( adapter->support_ext_cdb ) {
918 epthru = mega_prepare_extpassthru(adapter, scb, cmd,
919 channel, target);
921 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
923 mbox->m_out.xferaddr = scb->epthru_dma_addr;
926 else {
928 pthru = mega_prepare_passthru(adapter, scb, cmd,
929 channel, target);
931 /* Initialize mailbox */
932 if( adapter->has_64bit_addr ) {
933 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
935 else {
936 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
939 mbox->m_out.xferaddr = scb->pthru_dma_addr;
942 return scb;
944 return NULL;
949 * mega_prepare_passthru()
950 * @adapter - pointer to our soft state
951 * @scb - our scsi control block
952 * @cmd - scsi command from the mid-layer
953 * @channel - actual channel on the controller
954 * @target - actual id on the controller.
956 * prepare a command for the scsi physical devices.
958 static mega_passthru *
959 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
960 int channel, int target)
962 mega_passthru *pthru;
964 pthru = scb->pthru;
965 memset(pthru, 0, sizeof (mega_passthru));
967 /* 0=6sec/1=60sec/2=10min/3=3hrs */
968 pthru->timeout = 2;
970 pthru->ars = 1;
971 pthru->reqsenselen = 14;
972 pthru->islogical = 0;
974 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
976 pthru->target = (adapter->flag & BOARD_40LD) ?
977 (channel << 4) | target : target;
979 pthru->cdblen = cmd->cmd_len;
980 pthru->logdrv = cmd->device->lun;
982 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
984 /* Not sure about the direction */
985 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
987 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
988 switch (cmd->cmnd[0]) {
989 case INQUIRY:
990 case READ_CAPACITY:
991 if(!(adapter->flag & (1L << cmd->device->channel))) {
993 printk(KERN_NOTICE
994 "scsi%d: scanning scsi channel %d [P%d] ",
995 adapter->host->host_no,
996 cmd->device->channel, channel);
997 printk("for physical devices.\n");
999 adapter->flag |= (1L << cmd->device->channel);
1001 /* Fall through */
1002 default:
1003 pthru->numsgelements = mega_build_sglist(adapter, scb,
1004 &pthru->dataxferaddr, &pthru->dataxferlen);
1005 break;
1007 return pthru;
1012 * mega_prepare_extpassthru()
1013 * @adapter - pointer to our soft state
1014 * @scb - our scsi control block
1015 * @cmd - scsi command from the mid-layer
1016 * @channel - actual channel on the controller
1017 * @target - actual id on the controller.
1019 * prepare a command for the scsi physical devices. This rountine prepares
1020 * commands for devices which can take extended CDBs (>10 bytes)
1022 static mega_ext_passthru *
1023 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1024 int channel, int target)
1026 mega_ext_passthru *epthru;
1028 epthru = scb->epthru;
1029 memset(epthru, 0, sizeof(mega_ext_passthru));
1031 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1032 epthru->timeout = 2;
1034 epthru->ars = 1;
1035 epthru->reqsenselen = 14;
1036 epthru->islogical = 0;
1038 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1039 epthru->target = (adapter->flag & BOARD_40LD) ?
1040 (channel << 4) | target : target;
1042 epthru->cdblen = cmd->cmd_len;
1043 epthru->logdrv = cmd->device->lun;
1045 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1047 /* Not sure about the direction */
1048 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1050 switch(cmd->cmnd[0]) {
1051 case INQUIRY:
1052 case READ_CAPACITY:
1053 if(!(adapter->flag & (1L << cmd->device->channel))) {
1055 printk(KERN_NOTICE
1056 "scsi%d: scanning scsi channel %d [P%d] ",
1057 adapter->host->host_no,
1058 cmd->device->channel, channel);
1059 printk("for physical devices.\n");
1061 adapter->flag |= (1L << cmd->device->channel);
1063 /* Fall through */
1064 default:
1065 epthru->numsgelements = mega_build_sglist(adapter, scb,
1066 &epthru->dataxferaddr, &epthru->dataxferlen);
1067 break;
1070 return epthru;
1073 static void
1074 __mega_runpendq(adapter_t *adapter)
1076 scb_t *scb;
1077 struct list_head *pos, *next;
1079 /* Issue any pending commands to the card */
1080 list_for_each_safe(pos, next, &adapter->pending_list) {
1082 scb = list_entry(pos, scb_t, list);
1084 if( !(scb->state & SCB_ISSUED) ) {
1086 if( issue_scb(adapter, scb) != 0 )
1087 return;
1091 return;
1096 * issue_scb()
1097 * @adapter - pointer to our soft state
1098 * @scb - scsi control block
1100 * Post a command to the card if the mailbox is available, otherwise return
1101 * busy. We also take the scb from the pending list if the mailbox is
1102 * available.
1104 static int
1105 issue_scb(adapter_t *adapter, scb_t *scb)
1107 volatile mbox64_t *mbox64 = adapter->mbox64;
1108 volatile mbox_t *mbox = adapter->mbox;
1109 unsigned int i = 0;
1111 if(unlikely(mbox->m_in.busy)) {
1112 do {
1113 udelay(1);
1114 i++;
1115 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1117 if(mbox->m_in.busy) return -1;
1120 /* Copy mailbox data into host structure */
1121 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1122 sizeof(struct mbox_out));
1124 mbox->m_out.cmdid = scb->idx; /* Set cmdid */
1125 mbox->m_in.busy = 1; /* Set busy */
1129 * Increment the pending queue counter
1131 atomic_inc(&adapter->pend_cmds);
1133 switch (mbox->m_out.cmd) {
1134 case MEGA_MBOXCMD_LREAD64:
1135 case MEGA_MBOXCMD_LWRITE64:
1136 case MEGA_MBOXCMD_PASSTHRU64:
1137 case MEGA_MBOXCMD_EXTPTHRU:
1138 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1139 mbox64->xfer_segment_hi = 0;
1140 mbox->m_out.xferaddr = 0xFFFFFFFF;
1141 break;
1142 default:
1143 mbox64->xfer_segment_lo = 0;
1144 mbox64->xfer_segment_hi = 0;
1148 * post the command
1150 scb->state |= SCB_ISSUED;
1152 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1153 mbox->m_in.poll = 0;
1154 mbox->m_in.ack = 0;
1155 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1157 else {
1158 irq_enable(adapter);
1159 issue_command(adapter);
1162 return 0;
1166 * Wait until the controller's mailbox is available
1168 static inline int
1169 mega_busywait_mbox (adapter_t *adapter)
1171 if (adapter->mbox->m_in.busy)
1172 return __mega_busywait_mbox(adapter);
1173 return 0;
1177 * issue_scb_block()
1178 * @adapter - pointer to our soft state
1179 * @raw_mbox - the mailbox
1181 * Issue a scb in synchronous and non-interrupt mode
1183 static int
1184 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1186 volatile mbox64_t *mbox64 = adapter->mbox64;
1187 volatile mbox_t *mbox = adapter->mbox;
1188 u8 byte;
1190 /* Wait until mailbox is free */
1191 if(mega_busywait_mbox (adapter))
1192 goto bug_blocked_mailbox;
1194 /* Copy mailbox data into host structure */
1195 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1196 mbox->m_out.cmdid = 0xFE;
1197 mbox->m_in.busy = 1;
1199 switch (raw_mbox[0]) {
1200 case MEGA_MBOXCMD_LREAD64:
1201 case MEGA_MBOXCMD_LWRITE64:
1202 case MEGA_MBOXCMD_PASSTHRU64:
1203 case MEGA_MBOXCMD_EXTPTHRU:
1204 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1205 mbox64->xfer_segment_hi = 0;
1206 mbox->m_out.xferaddr = 0xFFFFFFFF;
1207 break;
1208 default:
1209 mbox64->xfer_segment_lo = 0;
1210 mbox64->xfer_segment_hi = 0;
1213 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1214 mbox->m_in.poll = 0;
1215 mbox->m_in.ack = 0;
1216 mbox->m_in.numstatus = 0xFF;
1217 mbox->m_in.status = 0xFF;
1218 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1220 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1221 cpu_relax();
1223 mbox->m_in.numstatus = 0xFF;
1225 while( (volatile u8)mbox->m_in.poll != 0x77 )
1226 cpu_relax();
1228 mbox->m_in.poll = 0;
1229 mbox->m_in.ack = 0x77;
1231 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1233 while(RDINDOOR(adapter) & 0x2)
1234 cpu_relax();
1236 else {
1237 irq_disable(adapter);
1238 issue_command(adapter);
1240 while (!((byte = irq_state(adapter)) & INTR_VALID))
1241 cpu_relax();
1243 set_irq_state(adapter, byte);
1244 irq_enable(adapter);
1245 irq_ack(adapter);
1248 return mbox->m_in.status;
1250 bug_blocked_mailbox:
1251 printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1252 udelay (1000);
1253 return -1;
1258 * megaraid_isr_iomapped()
1259 * @irq - irq
1260 * @devp - pointer to our soft state
1262 * Interrupt service routine for io-mapped controllers.
1263 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1264 * and service the completed commands.
1266 static irqreturn_t
1267 megaraid_isr_iomapped(int irq, void *devp)
1269 adapter_t *adapter = devp;
1270 unsigned long flags;
1271 u8 status;
1272 u8 nstatus;
1273 u8 completed[MAX_FIRMWARE_STATUS];
1274 u8 byte;
1275 int handled = 0;
1279 * loop till F/W has more commands for us to complete.
1281 spin_lock_irqsave(&adapter->lock, flags);
1283 do {
1284 /* Check if a valid interrupt is pending */
1285 byte = irq_state(adapter);
1286 if( (byte & VALID_INTR_BYTE) == 0 ) {
1288 * No more pending commands
1290 goto out_unlock;
1292 set_irq_state(adapter, byte);
1294 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1295 == 0xFF)
1296 cpu_relax();
1297 adapter->mbox->m_in.numstatus = 0xFF;
1299 status = adapter->mbox->m_in.status;
1302 * decrement the pending queue counter
1304 atomic_sub(nstatus, &adapter->pend_cmds);
1306 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1307 nstatus);
1309 /* Acknowledge interrupt */
1310 irq_ack(adapter);
1312 mega_cmd_done(adapter, completed, nstatus, status);
1314 mega_rundoneq(adapter);
1316 handled = 1;
1318 /* Loop through any pending requests */
1319 if(atomic_read(&adapter->quiescent) == 0) {
1320 mega_runpendq(adapter);
1323 } while(1);
1325 out_unlock:
1327 spin_unlock_irqrestore(&adapter->lock, flags);
1329 return IRQ_RETVAL(handled);
1334 * megaraid_isr_memmapped()
1335 * @irq - irq
1336 * @devp - pointer to our soft state
1338 * Interrupt service routine for memory-mapped controllers.
1339 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1340 * and service the completed commands.
1342 static irqreturn_t
1343 megaraid_isr_memmapped(int irq, void *devp)
1345 adapter_t *adapter = devp;
1346 unsigned long flags;
1347 u8 status;
1348 u32 dword = 0;
1349 u8 nstatus;
1350 u8 completed[MAX_FIRMWARE_STATUS];
1351 int handled = 0;
1355 * loop till F/W has more commands for us to complete.
1357 spin_lock_irqsave(&adapter->lock, flags);
1359 do {
1360 /* Check if a valid interrupt is pending */
1361 dword = RDOUTDOOR(adapter);
1362 if(dword != 0x10001234) {
1364 * No more pending commands
1366 goto out_unlock;
1368 WROUTDOOR(adapter, 0x10001234);
1370 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1371 == 0xFF) {
1372 cpu_relax();
1374 adapter->mbox->m_in.numstatus = 0xFF;
1376 status = adapter->mbox->m_in.status;
1379 * decrement the pending queue counter
1381 atomic_sub(nstatus, &adapter->pend_cmds);
1383 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1384 nstatus);
1386 /* Acknowledge interrupt */
1387 WRINDOOR(adapter, 0x2);
1389 handled = 1;
1391 while( RDINDOOR(adapter) & 0x02 )
1392 cpu_relax();
1394 mega_cmd_done(adapter, completed, nstatus, status);
1396 mega_rundoneq(adapter);
1398 /* Loop through any pending requests */
1399 if(atomic_read(&adapter->quiescent) == 0) {
1400 mega_runpendq(adapter);
1403 } while(1);
1405 out_unlock:
1407 spin_unlock_irqrestore(&adapter->lock, flags);
1409 return IRQ_RETVAL(handled);
1412 * mega_cmd_done()
1413 * @adapter - pointer to our soft state
1414 * @completed - array of ids of completed commands
1415 * @nstatus - number of completed commands
1416 * @status - status of the last command completed
1418 * Complete the commands and call the scsi mid-layer callback hooks.
1420 static void
1421 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1423 mega_ext_passthru *epthru = NULL;
1424 struct scatterlist *sgl;
1425 Scsi_Cmnd *cmd = NULL;
1426 mega_passthru *pthru = NULL;
1427 mbox_t *mbox = NULL;
1428 u8 c;
1429 scb_t *scb;
1430 int islogical;
1431 int cmdid;
1432 int i;
1435 * for all the commands completed, call the mid-layer callback routine
1436 * and free the scb.
1438 for( i = 0; i < nstatus; i++ ) {
1440 cmdid = completed[i];
1442 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1443 scb = &adapter->int_scb;
1444 cmd = scb->cmd;
1445 mbox = (mbox_t *)scb->raw_mbox;
1448 * Internal command interface do not fire the extended
1449 * passthru or 64-bit passthru
1451 pthru = scb->pthru;
1454 else {
1455 scb = &adapter->scb_list[cmdid];
1458 * Make sure f/w has completed a valid command
1460 if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1461 printk(KERN_CRIT
1462 "megaraid: invalid command ");
1463 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1464 cmdid, scb->state, scb->cmd);
1466 continue;
1470 * Was a abort issued for this command
1472 if( scb->state & SCB_ABORT ) {
1474 printk(KERN_WARNING
1475 "megaraid: aborted cmd [%x] complete.\n",
1476 scb->idx);
1478 scb->cmd->result = (DID_ABORT << 16);
1480 list_add_tail(SCSI_LIST(scb->cmd),
1481 &adapter->completed_list);
1483 mega_free_scb(adapter, scb);
1485 continue;
1489 * Was a reset issued for this command
1491 if( scb->state & SCB_RESET ) {
1493 printk(KERN_WARNING
1494 "megaraid: reset cmd [%x] complete.\n",
1495 scb->idx);
1497 scb->cmd->result = (DID_RESET << 16);
1499 list_add_tail(SCSI_LIST(scb->cmd),
1500 &adapter->completed_list);
1502 mega_free_scb (adapter, scb);
1504 continue;
1507 cmd = scb->cmd;
1508 pthru = scb->pthru;
1509 epthru = scb->epthru;
1510 mbox = (mbox_t *)scb->raw_mbox;
1512 #if MEGA_HAVE_STATS
1515 int logdrv = mbox->m_out.logdrv;
1517 islogical = adapter->logdrv_chan[cmd->channel];
1519 * Maintain an error counter for the logical drive.
1520 * Some application like SNMP agent need such
1521 * statistics
1523 if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1524 cmd->cmnd[0] == READ_10 ||
1525 cmd->cmnd[0] == READ_12)) {
1527 * Logical drive number increases by 0x80 when
1528 * a logical drive is deleted
1530 adapter->rd_errors[logdrv%0x80]++;
1533 if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1534 cmd->cmnd[0] == WRITE_10 ||
1535 cmd->cmnd[0] == WRITE_12)) {
1537 * Logical drive number increases by 0x80 when
1538 * a logical drive is deleted
1540 adapter->wr_errors[logdrv%0x80]++;
1544 #endif
1548 * Do not return the presence of hard disk on the channel so,
1549 * inquiry sent, and returned data==hard disk or removable
1550 * hard disk and not logical, request should return failure! -
1551 * PJ
1553 islogical = adapter->logdrv_chan[cmd->device->channel];
1554 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1556 sgl = scsi_sglist(cmd);
1557 if( sg_page(sgl) ) {
1558 c = *(unsigned char *) sg_virt(&sgl[0]);
1559 } else {
1560 printk(KERN_WARNING
1561 "megaraid: invalid sg.\n");
1562 c = 0;
1565 if(IS_RAID_CH(adapter, cmd->device->channel) &&
1566 ((c & 0x1F ) == TYPE_DISK)) {
1567 status = 0xF0;
1571 /* clear result; otherwise, success returns corrupt value */
1572 cmd->result = 0;
1574 /* Convert MegaRAID status to Linux error code */
1575 switch (status) {
1576 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1577 cmd->result |= (DID_OK << 16);
1578 break;
1580 case 0x02: /* ERROR_ABORTED, i.e.
1581 SCSI_STATUS_CHECK_CONDITION */
1583 /* set sense_buffer and result fields */
1584 if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1585 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1587 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1588 14);
1590 cmd->result = (DRIVER_SENSE << 24) |
1591 (DID_OK << 16) |
1592 (CHECK_CONDITION << 1);
1594 else {
1595 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1597 memcpy(cmd->sense_buffer,
1598 epthru->reqsensearea, 14);
1600 cmd->result = (DRIVER_SENSE << 24) |
1601 (DID_OK << 16) |
1602 (CHECK_CONDITION << 1);
1603 } else {
1604 cmd->sense_buffer[0] = 0x70;
1605 cmd->sense_buffer[2] = ABORTED_COMMAND;
1606 cmd->result |= (CHECK_CONDITION << 1);
1609 break;
1611 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1612 SCSI_STATUS_BUSY */
1613 cmd->result |= (DID_BUS_BUSY << 16) | status;
1614 break;
1616 default:
1617 #if MEGA_HAVE_CLUSTERING
1619 * If TEST_UNIT_READY fails, we know
1620 * MEGA_RESERVATION_STATUS failed
1622 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1623 cmd->result |= (DID_ERROR << 16) |
1624 (RESERVATION_CONFLICT << 1);
1626 else
1628 * Error code returned is 1 if Reserve or Release
1629 * failed or the input parameter is invalid
1631 if( status == 1 &&
1632 (cmd->cmnd[0] == RESERVE ||
1633 cmd->cmnd[0] == RELEASE) ) {
1635 cmd->result |= (DID_ERROR << 16) |
1636 (RESERVATION_CONFLICT << 1);
1638 else
1639 #endif
1640 cmd->result |= (DID_BAD_TARGET << 16)|status;
1644 * Only free SCBs for the commands coming down from the
1645 * mid-layer, not for which were issued internally
1647 * For internal command, restore the status returned by the
1648 * firmware so that user can interpret it.
1650 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1651 cmd->result = status;
1654 * Remove the internal command from the pending list
1656 list_del_init(&scb->list);
1657 scb->state = SCB_FREE;
1659 else {
1660 mega_free_scb(adapter, scb);
1663 /* Add Scsi_Command to end of completed queue */
1664 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1670 * mega_runpendq()
1672 * Run through the list of completed requests and finish it
1674 static void
1675 mega_rundoneq (adapter_t *adapter)
1677 Scsi_Cmnd *cmd;
1678 struct list_head *pos;
1680 list_for_each(pos, &adapter->completed_list) {
1682 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1684 cmd = list_entry(spos, Scsi_Cmnd, SCp);
1685 cmd->scsi_done(cmd);
1688 INIT_LIST_HEAD(&adapter->completed_list);
1693 * Free a SCB structure
1694 * Note: We assume the scsi commands associated with this scb is not free yet.
1696 static void
1697 mega_free_scb(adapter_t *adapter, scb_t *scb)
1699 switch( scb->dma_type ) {
1701 case MEGA_DMA_TYPE_NONE:
1702 break;
1704 case MEGA_SGLIST:
1705 scsi_dma_unmap(scb->cmd);
1706 break;
1707 default:
1708 break;
1712 * Remove from the pending list
1714 list_del_init(&scb->list);
1716 /* Link the scb back into free list */
1717 scb->state = SCB_FREE;
1718 scb->cmd = NULL;
1720 list_add(&scb->list, &adapter->free_list);
1724 static int
1725 __mega_busywait_mbox (adapter_t *adapter)
1727 volatile mbox_t *mbox = adapter->mbox;
1728 long counter;
1730 for (counter = 0; counter < 10000; counter++) {
1731 if (!mbox->m_in.busy)
1732 return 0;
1733 udelay(100);
1734 cond_resched();
1736 return -1; /* give up after 1 second */
1740 * Copies data to SGLIST
1741 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1743 static int
1744 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1746 struct scatterlist *sg;
1747 Scsi_Cmnd *cmd;
1748 int sgcnt;
1749 int idx;
1751 cmd = scb->cmd;
1754 * Copy Scatter-Gather list info into controller structure.
1756 * The number of sg elements returned must not exceed our limit
1758 sgcnt = scsi_dma_map(cmd);
1760 scb->dma_type = MEGA_SGLIST;
1762 BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1764 *len = 0;
1766 if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1767 sg = scsi_sglist(cmd);
1768 scb->dma_h_bulkdata = sg_dma_address(sg);
1769 *buf = (u32)scb->dma_h_bulkdata;
1770 *len = sg_dma_len(sg);
1771 return 0;
1774 scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1775 if (adapter->has_64bit_addr) {
1776 scb->sgl64[idx].address = sg_dma_address(sg);
1777 *len += scb->sgl64[idx].length = sg_dma_len(sg);
1778 } else {
1779 scb->sgl[idx].address = sg_dma_address(sg);
1780 *len += scb->sgl[idx].length = sg_dma_len(sg);
1784 /* Reset pointer and length fields */
1785 *buf = scb->sgl_dma_addr;
1787 /* Return count of SG requests */
1788 return sgcnt;
1793 * mega_8_to_40ld()
1795 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1796 * Enquiry3 structures for later use
1798 static void
1799 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1800 mega_product_info *product_info)
1802 int i;
1804 product_info->max_commands = inquiry->adapter_info.max_commands;
1805 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1806 product_info->nchannels = inquiry->adapter_info.nchannels;
1808 for (i = 0; i < 4; i++) {
1809 product_info->fw_version[i] =
1810 inquiry->adapter_info.fw_version[i];
1812 product_info->bios_version[i] =
1813 inquiry->adapter_info.bios_version[i];
1815 enquiry3->cache_flush_interval =
1816 inquiry->adapter_info.cache_flush_interval;
1818 product_info->dram_size = inquiry->adapter_info.dram_size;
1820 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1822 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1823 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1824 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1825 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1828 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1829 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1832 static inline void
1833 mega_free_sgl(adapter_t *adapter)
1835 scb_t *scb;
1836 int i;
1838 for(i = 0; i < adapter->max_cmds; i++) {
1840 scb = &adapter->scb_list[i];
1842 if( scb->sgl64 ) {
1843 pci_free_consistent(adapter->dev,
1844 sizeof(mega_sgl64) * adapter->sglen,
1845 scb->sgl64,
1846 scb->sgl_dma_addr);
1848 scb->sgl64 = NULL;
1851 if( scb->pthru ) {
1852 pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1853 scb->pthru, scb->pthru_dma_addr);
1855 scb->pthru = NULL;
1858 if( scb->epthru ) {
1859 pci_free_consistent(adapter->dev,
1860 sizeof(mega_ext_passthru),
1861 scb->epthru, scb->epthru_dma_addr);
1863 scb->epthru = NULL;
1871 * Get information about the card/driver
1873 const char *
1874 megaraid_info(struct Scsi_Host *host)
1876 static char buffer[512];
1877 adapter_t *adapter;
1879 adapter = (adapter_t *)host->hostdata;
1881 sprintf (buffer,
1882 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1883 adapter->fw_version, adapter->product_info.max_commands,
1884 adapter->host->max_id, adapter->host->max_channel,
1885 adapter->host->max_lun);
1886 return buffer;
1890 * Abort a previous SCSI request. Only commands on the pending list can be
1891 * aborted. All the commands issued to the F/W must complete.
1893 static int
1894 megaraid_abort(Scsi_Cmnd *cmd)
1896 adapter_t *adapter;
1897 int rval;
1899 adapter = (adapter_t *)cmd->device->host->hostdata;
1901 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1904 * This is required here to complete any completed requests
1905 * to be communicated over to the mid layer.
1907 mega_rundoneq(adapter);
1909 return rval;
1913 static int
1914 megaraid_reset(struct scsi_cmnd *cmd)
1916 adapter_t *adapter;
1917 megacmd_t mc;
1918 int rval;
1920 adapter = (adapter_t *)cmd->device->host->hostdata;
1922 #if MEGA_HAVE_CLUSTERING
1923 mc.cmd = MEGA_CLUSTER_CMD;
1924 mc.opcode = MEGA_RESET_RESERVATIONS;
1926 if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1927 printk(KERN_WARNING
1928 "megaraid: reservation reset failed.\n");
1930 else {
1931 printk(KERN_INFO "megaraid: reservation reset.\n");
1933 #endif
1935 spin_lock_irq(&adapter->lock);
1937 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1940 * This is required here to complete any completed requests
1941 * to be communicated over to the mid layer.
1943 mega_rundoneq(adapter);
1944 spin_unlock_irq(&adapter->lock);
1946 return rval;
1950 * megaraid_abort_and_reset()
1951 * @adapter - megaraid soft state
1952 * @cmd - scsi command to be aborted or reset
1953 * @aor - abort or reset flag
1955 * Try to locate the scsi command in the pending queue. If found and is not
1956 * issued to the controller, abort/reset it. Otherwise return failure
1958 static int
1959 megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
1961 struct list_head *pos, *next;
1962 scb_t *scb;
1964 printk(KERN_WARNING "megaraid: %s cmd=%x <c=%d t=%d l=%d>\n",
1965 (aor == SCB_ABORT)? "ABORTING":"RESET",
1966 cmd->cmnd[0], cmd->device->channel,
1967 cmd->device->id, cmd->device->lun);
1969 if(list_empty(&adapter->pending_list))
1970 return FALSE;
1972 list_for_each_safe(pos, next, &adapter->pending_list) {
1974 scb = list_entry(pos, scb_t, list);
1976 if (scb->cmd == cmd) { /* Found command */
1978 scb->state |= aor;
1981 * Check if this command has firmware ownership. If
1982 * yes, we cannot reset this command. Whenever f/w
1983 * completes this command, we will return appropriate
1984 * status from ISR.
1986 if( scb->state & SCB_ISSUED ) {
1988 printk(KERN_WARNING
1989 "megaraid: %s[%x], fw owner.\n",
1990 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1991 scb->idx);
1993 return FALSE;
1995 else {
1998 * Not yet issued! Remove from the pending
1999 * list
2001 printk(KERN_WARNING
2002 "megaraid: %s-[%x], driver owner.\n",
2003 (aor==SCB_ABORT) ? "ABORTING":"RESET",
2004 scb->idx);
2006 mega_free_scb(adapter, scb);
2008 if( aor == SCB_ABORT ) {
2009 cmd->result = (DID_ABORT << 16);
2011 else {
2012 cmd->result = (DID_RESET << 16);
2015 list_add_tail(SCSI_LIST(cmd),
2016 &adapter->completed_list);
2018 return TRUE;
2023 return FALSE;
2026 static inline int
2027 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2029 *pdev = alloc_pci_dev();
2031 if( *pdev == NULL ) return -1;
2033 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2035 if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
2036 kfree(*pdev);
2037 return -1;
2040 return 0;
2043 static inline void
2044 free_local_pdev(struct pci_dev *pdev)
2046 kfree(pdev);
2050 * mega_allocate_inquiry()
2051 * @dma_handle - handle returned for dma address
2052 * @pdev - handle to pci device
2054 * allocates memory for inquiry structure
2056 static inline void *
2057 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2059 return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2063 static inline void
2064 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2066 pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2070 #ifdef CONFIG_PROC_FS
2071 /* Following code handles /proc fs */
2074 * proc_show_config()
2075 * @m - Synthetic file construction data
2076 * @v - File iterator
2078 * Display configuration information about the controller.
2080 static int
2081 proc_show_config(struct seq_file *m, void *v)
2084 adapter_t *adapter = m->private;
2086 seq_puts(m, MEGARAID_VERSION);
2087 if(adapter->product_info.product_name[0])
2088 seq_printf(m, "%s\n", adapter->product_info.product_name);
2090 seq_puts(m, "Controller Type: ");
2092 if( adapter->flag & BOARD_MEMMAP )
2093 seq_puts(m, "438/466/467/471/493/518/520/531/532\n");
2094 else
2095 seq_puts(m, "418/428/434\n");
2097 if(adapter->flag & BOARD_40LD)
2098 seq_puts(m, "Controller Supports 40 Logical Drives\n");
2100 if(adapter->flag & BOARD_64BIT)
2101 seq_puts(m, "Controller capable of 64-bit memory addressing\n");
2102 if( adapter->has_64bit_addr )
2103 seq_puts(m, "Controller using 64-bit memory addressing\n");
2104 else
2105 seq_puts(m, "Controller is not using 64-bit memory addressing\n");
2107 seq_printf(m, "Base = %08lx, Irq = %d, ",
2108 adapter->base, adapter->host->irq);
2110 seq_printf(m, "Logical Drives = %d, Channels = %d\n",
2111 adapter->numldrv, adapter->product_info.nchannels);
2113 seq_printf(m, "Version =%s:%s, DRAM = %dMb\n",
2114 adapter->fw_version, adapter->bios_version,
2115 adapter->product_info.dram_size);
2117 seq_printf(m, "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2118 adapter->product_info.max_commands, adapter->max_cmds);
2120 seq_printf(m, "support_ext_cdb = %d\n", adapter->support_ext_cdb);
2121 seq_printf(m, "support_random_del = %d\n", adapter->support_random_del);
2122 seq_printf(m, "boot_ldrv_enabled = %d\n", adapter->boot_ldrv_enabled);
2123 seq_printf(m, "boot_ldrv = %d\n", adapter->boot_ldrv);
2124 seq_printf(m, "boot_pdrv_enabled = %d\n", adapter->boot_pdrv_enabled);
2125 seq_printf(m, "boot_pdrv_ch = %d\n", adapter->boot_pdrv_ch);
2126 seq_printf(m, "boot_pdrv_tgt = %d\n", adapter->boot_pdrv_tgt);
2127 seq_printf(m, "quiescent = %d\n",
2128 atomic_read(&adapter->quiescent));
2129 seq_printf(m, "has_cluster = %d\n", adapter->has_cluster);
2131 seq_puts(m, "\nModule Parameters:\n");
2132 seq_printf(m, "max_cmd_per_lun = %d\n", max_cmd_per_lun);
2133 seq_printf(m, "max_sectors_per_io = %d\n", max_sectors_per_io);
2134 return 0;
2138 * proc_show_stat()
2139 * @m - Synthetic file construction data
2140 * @v - File iterator
2142 * Display statistical information about the I/O activity.
2144 static int
2145 proc_show_stat(struct seq_file *m, void *v)
2147 adapter_t *adapter = m->private;
2148 #if MEGA_HAVE_STATS
2149 int i;
2150 #endif
2152 seq_puts(m, "Statistical Information for this controller\n");
2153 seq_printf(m, "pend_cmds = %d\n", atomic_read(&adapter->pend_cmds));
2154 #if MEGA_HAVE_STATS
2155 for(i = 0; i < adapter->numldrv; i++) {
2156 seq_printf(m, "Logical Drive %d:\n", i);
2157 seq_printf(m, "\tReads Issued = %lu, Writes Issued = %lu\n",
2158 adapter->nreads[i], adapter->nwrites[i]);
2159 seq_printf(m, "\tSectors Read = %lu, Sectors Written = %lu\n",
2160 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2161 seq_printf(m, "\tRead errors = %lu, Write errors = %lu\n\n",
2162 adapter->rd_errors[i], adapter->wr_errors[i]);
2164 #else
2165 seq_puts(m, "IO and error counters not compiled in driver.\n");
2166 #endif
2167 return 0;
2172 * proc_show_mbox()
2173 * @m - Synthetic file construction data
2174 * @v - File iterator
2176 * Display mailbox information for the last command issued. This information
2177 * is good for debugging.
2179 static int
2180 proc_show_mbox(struct seq_file *m, void *v)
2182 adapter_t *adapter = m->private;
2183 volatile mbox_t *mbox = adapter->mbox;
2185 seq_puts(m, "Contents of Mail Box Structure\n");
2186 seq_printf(m, " Fw Command = 0x%02x\n", mbox->m_out.cmd);
2187 seq_printf(m, " Cmd Sequence = 0x%02x\n", mbox->m_out.cmdid);
2188 seq_printf(m, " No of Sectors= %04d\n", mbox->m_out.numsectors);
2189 seq_printf(m, " LBA = 0x%02x\n", mbox->m_out.lba);
2190 seq_printf(m, " DTA = 0x%08x\n", mbox->m_out.xferaddr);
2191 seq_printf(m, " Logical Drive= 0x%02x\n", mbox->m_out.logdrv);
2192 seq_printf(m, " No of SG Elmt= 0x%02x\n", mbox->m_out.numsgelements);
2193 seq_printf(m, " Busy = %01x\n", mbox->m_in.busy);
2194 seq_printf(m, " Status = 0x%02x\n", mbox->m_in.status);
2195 return 0;
2200 * proc_show_rebuild_rate()
2201 * @m - Synthetic file construction data
2202 * @v - File iterator
2204 * Display current rebuild rate
2206 static int
2207 proc_show_rebuild_rate(struct seq_file *m, void *v)
2209 adapter_t *adapter = m->private;
2210 dma_addr_t dma_handle;
2211 caddr_t inquiry;
2212 struct pci_dev *pdev;
2214 if( make_local_pdev(adapter, &pdev) != 0 )
2215 return 0;
2217 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2218 goto free_pdev;
2220 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2221 seq_puts(m, "Adapter inquiry failed.\n");
2222 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2223 goto free_inquiry;
2226 if( adapter->flag & BOARD_40LD )
2227 seq_printf(m, "Rebuild Rate: [%d%%]\n",
2228 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2229 else
2230 seq_printf(m, "Rebuild Rate: [%d%%]\n",
2231 ((mraid_ext_inquiry *)
2232 inquiry)->raid_inq.adapter_info.rebuild_rate);
2234 free_inquiry:
2235 mega_free_inquiry(inquiry, dma_handle, pdev);
2236 free_pdev:
2237 free_local_pdev(pdev);
2238 return 0;
2243 * proc_show_battery()
2244 * @m - Synthetic file construction data
2245 * @v - File iterator
2247 * Display information about the battery module on the controller.
2249 static int
2250 proc_show_battery(struct seq_file *m, void *v)
2252 adapter_t *adapter = m->private;
2253 dma_addr_t dma_handle;
2254 caddr_t inquiry;
2255 struct pci_dev *pdev;
2256 u8 battery_status;
2258 if( make_local_pdev(adapter, &pdev) != 0 )
2259 return 0;
2261 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2262 goto free_pdev;
2264 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2265 seq_printf(m, "Adapter inquiry failed.\n");
2266 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2267 goto free_inquiry;
2270 if( adapter->flag & BOARD_40LD ) {
2271 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2273 else {
2274 battery_status = ((mraid_ext_inquiry *)inquiry)->
2275 raid_inq.adapter_info.battery_status;
2279 * Decode the battery status
2281 seq_printf(m, "Battery Status:[%d]", battery_status);
2283 if(battery_status == MEGA_BATT_CHARGE_DONE)
2284 seq_puts(m, " Charge Done");
2286 if(battery_status & MEGA_BATT_MODULE_MISSING)
2287 seq_puts(m, " Module Missing");
2289 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2290 seq_puts(m, " Low Voltage");
2292 if(battery_status & MEGA_BATT_TEMP_HIGH)
2293 seq_puts(m, " Temperature High");
2295 if(battery_status & MEGA_BATT_PACK_MISSING)
2296 seq_puts(m, " Pack Missing");
2298 if(battery_status & MEGA_BATT_CHARGE_INPROG)
2299 seq_puts(m, " Charge In-progress");
2301 if(battery_status & MEGA_BATT_CHARGE_FAIL)
2302 seq_puts(m, " Charge Fail");
2304 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2305 seq_puts(m, " Cycles Exceeded");
2307 seq_putc(m, '\n');
2309 free_inquiry:
2310 mega_free_inquiry(inquiry, dma_handle, pdev);
2311 free_pdev:
2312 free_local_pdev(pdev);
2313 return 0;
2318 * Display scsi inquiry
2320 static void
2321 mega_print_inquiry(struct seq_file *m, char *scsi_inq)
2323 int i;
2325 seq_puts(m, " Vendor: ");
2326 seq_write(m, scsi_inq + 8, 8);
2327 seq_puts(m, " Model: ");
2328 seq_write(m, scsi_inq + 16, 16);
2329 seq_puts(m, " Rev: ");
2330 seq_write(m, scsi_inq + 32, 4);
2331 seq_putc(m, '\n');
2333 i = scsi_inq[0] & 0x1f;
2334 seq_printf(m, " Type: %s ", scsi_device_type(i));
2336 seq_printf(m, " ANSI SCSI revision: %02x",
2337 scsi_inq[2] & 0x07);
2339 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2340 seq_puts(m, " CCS\n");
2341 else
2342 seq_putc(m, '\n');
2346 * proc_show_pdrv()
2347 * @m - Synthetic file construction data
2348 * @page - buffer to write the data in
2349 * @adapter - pointer to our soft state
2351 * Display information about the physical drives.
2353 static int
2354 proc_show_pdrv(struct seq_file *m, adapter_t *adapter, int channel)
2356 dma_addr_t dma_handle;
2357 char *scsi_inq;
2358 dma_addr_t scsi_inq_dma_handle;
2359 caddr_t inquiry;
2360 struct pci_dev *pdev;
2361 u8 *pdrv_state;
2362 u8 state;
2363 int tgt;
2364 int max_channels;
2365 int i;
2367 if( make_local_pdev(adapter, &pdev) != 0 )
2368 return 0;
2370 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2371 goto free_pdev;
2373 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2374 seq_puts(m, "Adapter inquiry failed.\n");
2375 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2376 goto free_inquiry;
2380 scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2381 if( scsi_inq == NULL ) {
2382 seq_puts(m, "memory not available for scsi inq.\n");
2383 goto free_inquiry;
2386 if( adapter->flag & BOARD_40LD ) {
2387 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2389 else {
2390 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2391 raid_inq.pdrv_info.pdrv_state;
2394 max_channels = adapter->product_info.nchannels;
2396 if( channel >= max_channels ) {
2397 goto free_pci;
2400 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2402 i = channel*16 + tgt;
2404 state = *(pdrv_state + i);
2405 switch( state & 0x0F ) {
2406 case PDRV_ONLINE:
2407 seq_printf(m, "Channel:%2d Id:%2d State: Online",
2408 channel, tgt);
2409 break;
2411 case PDRV_FAILED:
2412 seq_printf(m, "Channel:%2d Id:%2d State: Failed",
2413 channel, tgt);
2414 break;
2416 case PDRV_RBLD:
2417 seq_printf(m, "Channel:%2d Id:%2d State: Rebuild",
2418 channel, tgt);
2419 break;
2421 case PDRV_HOTSPARE:
2422 seq_printf(m, "Channel:%2d Id:%2d State: Hot spare",
2423 channel, tgt);
2424 break;
2426 default:
2427 seq_printf(m, "Channel:%2d Id:%2d State: Un-configured",
2428 channel, tgt);
2429 break;
2433 * This interface displays inquiries for disk drives
2434 * only. Inquries for logical drives and non-disk
2435 * devices are available through /proc/scsi/scsi
2437 memset(scsi_inq, 0, 256);
2438 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2439 scsi_inq_dma_handle) ||
2440 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2441 continue;
2445 * Check for overflow. We print less than 240
2446 * characters for inquiry
2448 seq_puts(m, ".\n");
2449 mega_print_inquiry(m, scsi_inq);
2452 free_pci:
2453 pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2454 free_inquiry:
2455 mega_free_inquiry(inquiry, dma_handle, pdev);
2456 free_pdev:
2457 free_local_pdev(pdev);
2458 return 0;
2462 * proc_show_pdrv_ch0()
2463 * @m - Synthetic file construction data
2464 * @v - File iterator
2466 * Display information about the physical drives on physical channel 0.
2468 static int
2469 proc_show_pdrv_ch0(struct seq_file *m, void *v)
2471 return proc_show_pdrv(m, m->private, 0);
2476 * proc_show_pdrv_ch1()
2477 * @m - Synthetic file construction data
2478 * @v - File iterator
2480 * Display information about the physical drives on physical channel 1.
2482 static int
2483 proc_show_pdrv_ch1(struct seq_file *m, void *v)
2485 return proc_show_pdrv(m, m->private, 1);
2490 * proc_show_pdrv_ch2()
2491 * @m - Synthetic file construction data
2492 * @v - File iterator
2494 * Display information about the physical drives on physical channel 2.
2496 static int
2497 proc_show_pdrv_ch2(struct seq_file *m, void *v)
2499 return proc_show_pdrv(m, m->private, 2);
2504 * proc_show_pdrv_ch3()
2505 * @m - Synthetic file construction data
2506 * @v - File iterator
2508 * Display information about the physical drives on physical channel 3.
2510 static int
2511 proc_show_pdrv_ch3(struct seq_file *m, void *v)
2513 return proc_show_pdrv(m, m->private, 3);
2518 * proc_show_rdrv()
2519 * @m - Synthetic file construction data
2520 * @adapter - pointer to our soft state
2521 * @start - starting logical drive to display
2522 * @end - ending logical drive to display
2524 * We do not print the inquiry information since its already available through
2525 * /proc/scsi/scsi interface
2527 static int
2528 proc_show_rdrv(struct seq_file *m, adapter_t *adapter, int start, int end )
2530 dma_addr_t dma_handle;
2531 logdrv_param *lparam;
2532 megacmd_t mc;
2533 char *disk_array;
2534 dma_addr_t disk_array_dma_handle;
2535 caddr_t inquiry;
2536 struct pci_dev *pdev;
2537 u8 *rdrv_state;
2538 int num_ldrv;
2539 u32 array_sz;
2540 int i;
2542 if( make_local_pdev(adapter, &pdev) != 0 )
2543 return 0;
2545 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2546 goto free_pdev;
2548 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2549 seq_puts(m, "Adapter inquiry failed.\n");
2550 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2551 goto free_inquiry;
2554 memset(&mc, 0, sizeof(megacmd_t));
2556 if( adapter->flag & BOARD_40LD ) {
2557 array_sz = sizeof(disk_array_40ld);
2559 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2561 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2563 else {
2564 array_sz = sizeof(disk_array_8ld);
2566 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2567 raid_inq.logdrv_info.ldrv_state;
2569 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2570 raid_inq.logdrv_info.num_ldrv;
2573 disk_array = pci_alloc_consistent(pdev, array_sz,
2574 &disk_array_dma_handle);
2576 if( disk_array == NULL ) {
2577 seq_puts(m, "memory not available.\n");
2578 goto free_inquiry;
2581 mc.xferaddr = (u32)disk_array_dma_handle;
2583 if( adapter->flag & BOARD_40LD ) {
2584 mc.cmd = FC_NEW_CONFIG;
2585 mc.opcode = OP_DCMD_READ_CONFIG;
2587 if( mega_internal_command(adapter, &mc, NULL) ) {
2588 seq_puts(m, "40LD read config failed.\n");
2589 goto free_pci;
2593 else {
2594 mc.cmd = NEW_READ_CONFIG_8LD;
2596 if( mega_internal_command(adapter, &mc, NULL) ) {
2597 mc.cmd = READ_CONFIG_8LD;
2598 if( mega_internal_command(adapter, &mc, NULL) ) {
2599 seq_puts(m, "8LD read config failed.\n");
2600 goto free_pci;
2605 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2607 if( adapter->flag & BOARD_40LD ) {
2608 lparam =
2609 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
2611 else {
2612 lparam =
2613 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
2617 * Check for overflow. We print less than 240 characters for
2618 * information about each logical drive.
2620 seq_printf(m, "Logical drive:%2d:, ", i);
2622 switch( rdrv_state[i] & 0x0F ) {
2623 case RDRV_OFFLINE:
2624 seq_puts(m, "state: offline");
2625 break;
2626 case RDRV_DEGRADED:
2627 seq_puts(m, "state: degraded");
2628 break;
2629 case RDRV_OPTIMAL:
2630 seq_puts(m, "state: optimal");
2631 break;
2632 case RDRV_DELETED:
2633 seq_puts(m, "state: deleted");
2634 break;
2635 default:
2636 seq_puts(m, "state: unknown");
2637 break;
2641 * Check if check consistency or initialization is going on
2642 * for this logical drive.
2644 if( (rdrv_state[i] & 0xF0) == 0x20 )
2645 seq_puts(m, ", check-consistency in progress");
2646 else if( (rdrv_state[i] & 0xF0) == 0x10 )
2647 seq_puts(m, ", initialization in progress");
2649 seq_putc(m, '\n');
2651 seq_printf(m, "Span depth:%3d, ", lparam->span_depth);
2652 seq_printf(m, "RAID level:%3d, ", lparam->level);
2653 seq_printf(m, "Stripe size:%3d, ",
2654 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
2655 seq_printf(m, "Row size:%3d\n", lparam->row_size);
2657 seq_puts(m, "Read Policy: ");
2658 switch(lparam->read_ahead) {
2659 case NO_READ_AHEAD:
2660 seq_puts(m, "No read ahead, ");
2661 break;
2662 case READ_AHEAD:
2663 seq_puts(m, "Read ahead, ");
2664 break;
2665 case ADAP_READ_AHEAD:
2666 seq_puts(m, "Adaptive, ");
2667 break;
2671 seq_puts(m, "Write Policy: ");
2672 switch(lparam->write_mode) {
2673 case WRMODE_WRITE_THRU:
2674 seq_puts(m, "Write thru, ");
2675 break;
2676 case WRMODE_WRITE_BACK:
2677 seq_puts(m, "Write back, ");
2678 break;
2681 seq_puts(m, "Cache Policy: ");
2682 switch(lparam->direct_io) {
2683 case CACHED_IO:
2684 seq_puts(m, "Cached IO\n\n");
2685 break;
2686 case DIRECT_IO:
2687 seq_puts(m, "Direct IO\n\n");
2688 break;
2692 free_pci:
2693 pci_free_consistent(pdev, array_sz, disk_array,
2694 disk_array_dma_handle);
2695 free_inquiry:
2696 mega_free_inquiry(inquiry, dma_handle, pdev);
2697 free_pdev:
2698 free_local_pdev(pdev);
2699 return 0;
2703 * proc_show_rdrv_10()
2704 * @m - Synthetic file construction data
2705 * @v - File iterator
2707 * Display real time information about the logical drives 0 through 9.
2709 static int
2710 proc_show_rdrv_10(struct seq_file *m, void *v)
2712 return proc_show_rdrv(m, m->private, 0, 9);
2717 * proc_show_rdrv_20()
2718 * @m - Synthetic file construction data
2719 * @v - File iterator
2721 * Display real time information about the logical drives 0 through 9.
2723 static int
2724 proc_show_rdrv_20(struct seq_file *m, void *v)
2726 return proc_show_rdrv(m, m->private, 10, 19);
2731 * proc_show_rdrv_30()
2732 * @m - Synthetic file construction data
2733 * @v - File iterator
2735 * Display real time information about the logical drives 0 through 9.
2737 static int
2738 proc_show_rdrv_30(struct seq_file *m, void *v)
2740 return proc_show_rdrv(m, m->private, 20, 29);
2745 * proc_show_rdrv_40()
2746 * @m - Synthetic file construction data
2747 * @v - File iterator
2749 * Display real time information about the logical drives 0 through 9.
2751 static int
2752 proc_show_rdrv_40(struct seq_file *m, void *v)
2754 return proc_show_rdrv(m, m->private, 30, 39);
2759 * seq_file wrappers for procfile show routines.
2761 static int mega_proc_open(struct inode *inode, struct file *file)
2763 adapter_t *adapter = proc_get_parent_data(inode);
2764 int (*show)(struct seq_file *, void *) = PDE_DATA(inode);
2766 return single_open(file, show, adapter);
2769 static const struct file_operations mega_proc_fops = {
2770 .open = mega_proc_open,
2771 .read = seq_read,
2772 .llseek = seq_lseek,
2773 .release = single_release,
2777 * Table of proc files we need to create.
2779 struct mega_proc_file {
2780 const char *name;
2781 unsigned short ptr_offset;
2782 int (*show) (struct seq_file *m, void *v);
2785 static const struct mega_proc_file mega_proc_files[] = {
2786 { "config", offsetof(adapter_t, proc_read), proc_show_config },
2787 { "stat", offsetof(adapter_t, proc_stat), proc_show_stat },
2788 { "mailbox", offsetof(adapter_t, proc_mbox), proc_show_mbox },
2789 #if MEGA_HAVE_ENH_PROC
2790 { "rebuild-rate", offsetof(adapter_t, proc_rr), proc_show_rebuild_rate },
2791 { "battery-status", offsetof(adapter_t, proc_battery), proc_show_battery },
2792 { "diskdrives-ch0", offsetof(adapter_t, proc_pdrvstat[0]), proc_show_pdrv_ch0 },
2793 { "diskdrives-ch1", offsetof(adapter_t, proc_pdrvstat[1]), proc_show_pdrv_ch1 },
2794 { "diskdrives-ch2", offsetof(adapter_t, proc_pdrvstat[2]), proc_show_pdrv_ch2 },
2795 { "diskdrives-ch3", offsetof(adapter_t, proc_pdrvstat[3]), proc_show_pdrv_ch3 },
2796 { "raiddrives-0-9", offsetof(adapter_t, proc_rdrvstat[0]), proc_show_rdrv_10 },
2797 { "raiddrives-10-19", offsetof(adapter_t, proc_rdrvstat[1]), proc_show_rdrv_20 },
2798 { "raiddrives-20-29", offsetof(adapter_t, proc_rdrvstat[2]), proc_show_rdrv_30 },
2799 { "raiddrives-30-39", offsetof(adapter_t, proc_rdrvstat[3]), proc_show_rdrv_40 },
2800 #endif
2801 { NULL }
2805 * mega_create_proc_entry()
2806 * @index - index in soft state array
2807 * @parent - parent node for this /proc entry
2809 * Creates /proc entries for our controllers.
2811 static void
2812 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2814 const struct mega_proc_file *f;
2815 adapter_t *adapter = hba_soft_state[index];
2816 struct proc_dir_entry *dir, *de, **ppde;
2817 u8 string[16];
2819 sprintf(string, "hba%d", adapter->host->host_no);
2821 dir = adapter->controller_proc_dir_entry =
2822 proc_mkdir_data(string, 0, parent, adapter);
2823 if(!dir) {
2824 printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2825 return;
2828 for (f = mega_proc_files; f->name; f++) {
2829 de = proc_create_data(f->name, S_IRUSR, dir, &mega_proc_fops,
2830 f->show);
2831 if (!de) {
2832 printk(KERN_WARNING "\nmegaraid: proc_create failed\n");
2833 return;
2836 ppde = (void *)adapter + f->ptr_offset;
2837 *ppde = de;
2841 #else
2842 static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2845 #endif
2849 * megaraid_biosparam()
2851 * Return the disk geometry for a particular disk
2853 static int
2854 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
2855 sector_t capacity, int geom[])
2857 adapter_t *adapter;
2858 unsigned char *bh;
2859 int heads;
2860 int sectors;
2861 int cylinders;
2862 int rval;
2864 /* Get pointer to host config structure */
2865 adapter = (adapter_t *)sdev->host->hostdata;
2867 if (IS_RAID_CH(adapter, sdev->channel)) {
2868 /* Default heads (64) & sectors (32) */
2869 heads = 64;
2870 sectors = 32;
2871 cylinders = (ulong)capacity / (heads * sectors);
2874 * Handle extended translation size for logical drives
2875 * > 1Gb
2877 if ((ulong)capacity >= 0x200000) {
2878 heads = 255;
2879 sectors = 63;
2880 cylinders = (ulong)capacity / (heads * sectors);
2883 /* return result */
2884 geom[0] = heads;
2885 geom[1] = sectors;
2886 geom[2] = cylinders;
2888 else {
2889 bh = scsi_bios_ptable(bdev);
2891 if( bh ) {
2892 rval = scsi_partsize(bh, capacity,
2893 &geom[2], &geom[0], &geom[1]);
2894 kfree(bh);
2895 if( rval != -1 )
2896 return rval;
2899 printk(KERN_INFO
2900 "megaraid: invalid partition on this disk on channel %d\n",
2901 sdev->channel);
2903 /* Default heads (64) & sectors (32) */
2904 heads = 64;
2905 sectors = 32;
2906 cylinders = (ulong)capacity / (heads * sectors);
2908 /* Handle extended translation size for logical drives > 1Gb */
2909 if ((ulong)capacity >= 0x200000) {
2910 heads = 255;
2911 sectors = 63;
2912 cylinders = (ulong)capacity / (heads * sectors);
2915 /* return result */
2916 geom[0] = heads;
2917 geom[1] = sectors;
2918 geom[2] = cylinders;
2921 return 0;
2925 * mega_init_scb()
2926 * @adapter - pointer to our soft state
2928 * Allocate memory for the various pointers in the scb structures:
2929 * scatter-gather list pointer, passthru and extended passthru structure
2930 * pointers.
2932 static int
2933 mega_init_scb(adapter_t *adapter)
2935 scb_t *scb;
2936 int i;
2938 for( i = 0; i < adapter->max_cmds; i++ ) {
2940 scb = &adapter->scb_list[i];
2942 scb->sgl64 = NULL;
2943 scb->sgl = NULL;
2944 scb->pthru = NULL;
2945 scb->epthru = NULL;
2948 for( i = 0; i < adapter->max_cmds; i++ ) {
2950 scb = &adapter->scb_list[i];
2952 scb->idx = i;
2954 scb->sgl64 = pci_alloc_consistent(adapter->dev,
2955 sizeof(mega_sgl64) * adapter->sglen,
2956 &scb->sgl_dma_addr);
2958 scb->sgl = (mega_sglist *)scb->sgl64;
2960 if( !scb->sgl ) {
2961 printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
2962 mega_free_sgl(adapter);
2963 return -1;
2966 scb->pthru = pci_alloc_consistent(adapter->dev,
2967 sizeof(mega_passthru),
2968 &scb->pthru_dma_addr);
2970 if( !scb->pthru ) {
2971 printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
2972 mega_free_sgl(adapter);
2973 return -1;
2976 scb->epthru = pci_alloc_consistent(adapter->dev,
2977 sizeof(mega_ext_passthru),
2978 &scb->epthru_dma_addr);
2980 if( !scb->epthru ) {
2981 printk(KERN_WARNING
2982 "Can't allocate extended passthru.\n");
2983 mega_free_sgl(adapter);
2984 return -1;
2988 scb->dma_type = MEGA_DMA_TYPE_NONE;
2991 * Link to free list
2992 * lock not required since we are loading the driver, so no
2993 * commands possible right now.
2995 scb->state = SCB_FREE;
2996 scb->cmd = NULL;
2997 list_add(&scb->list, &adapter->free_list);
3000 return 0;
3005 * megadev_open()
3006 * @inode - unused
3007 * @filep - unused
3009 * Routines for the character/ioctl interface to the driver. Find out if this
3010 * is a valid open.
3012 static int
3013 megadev_open (struct inode *inode, struct file *filep)
3016 * Only allow superuser to access private ioctl interface
3018 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
3020 return 0;
3025 * megadev_ioctl()
3026 * @inode - Our device inode
3027 * @filep - unused
3028 * @cmd - ioctl command
3029 * @arg - user buffer
3031 * ioctl entry point for our private ioctl interface. We move the data in from
3032 * the user space, prepare the command (if necessary, convert the old MIMD
3033 * ioctl to new ioctl command), and issue a synchronous command to the
3034 * controller.
3036 static int
3037 megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3039 adapter_t *adapter;
3040 nitioctl_t uioc;
3041 int adapno;
3042 int rval;
3043 mega_passthru __user *upthru; /* user address for passthru */
3044 mega_passthru *pthru; /* copy user passthru here */
3045 dma_addr_t pthru_dma_hndl;
3046 void *data = NULL; /* data to be transferred */
3047 dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
3048 megacmd_t mc;
3049 megastat_t __user *ustats;
3050 int num_ldrv;
3051 u32 uxferaddr = 0;
3052 struct pci_dev *pdev;
3054 ustats = NULL; /* avoid compilation warnings */
3055 num_ldrv = 0;
3058 * Make sure only USCSICMD are issued through this interface.
3059 * MIMD application would still fire different command.
3061 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3062 return -EINVAL;
3066 * Check and convert a possible MIMD command to NIT command.
3067 * mega_m_to_n() copies the data from the user space, so we do not
3068 * have to do it here.
3069 * NOTE: We will need some user address to copyout the data, therefore
3070 * the inteface layer will also provide us with the required user
3071 * addresses.
3073 memset(&uioc, 0, sizeof(nitioctl_t));
3074 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3075 return rval;
3078 switch( uioc.opcode ) {
3080 case GET_DRIVER_VER:
3081 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3082 return (-EFAULT);
3084 break;
3086 case GET_N_ADAP:
3087 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3088 return (-EFAULT);
3091 * Shucks. MIMD interface returns a positive value for number
3092 * of adapters. TODO: Change it to return 0 when there is no
3093 * applicatio using mimd interface.
3095 return hba_count;
3097 case GET_ADAP_INFO:
3100 * Which adapter
3102 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3103 return (-ENODEV);
3105 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3106 sizeof(struct mcontroller)) )
3107 return (-EFAULT);
3108 break;
3110 #if MEGA_HAVE_STATS
3112 case GET_STATS:
3114 * Which adapter
3116 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3117 return (-ENODEV);
3119 adapter = hba_soft_state[adapno];
3121 ustats = uioc.uioc_uaddr;
3123 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3124 return (-EFAULT);
3127 * Check for the validity of the logical drive number
3129 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3131 if( copy_to_user(ustats->nreads, adapter->nreads,
3132 num_ldrv*sizeof(u32)) )
3133 return -EFAULT;
3135 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3136 num_ldrv*sizeof(u32)) )
3137 return -EFAULT;
3139 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3140 num_ldrv*sizeof(u32)) )
3141 return -EFAULT;
3143 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3144 num_ldrv*sizeof(u32)) )
3145 return -EFAULT;
3147 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3148 num_ldrv*sizeof(u32)) )
3149 return -EFAULT;
3151 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3152 num_ldrv*sizeof(u32)) )
3153 return -EFAULT;
3155 return 0;
3157 #endif
3158 case MBOX_CMD:
3161 * Which adapter
3163 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3164 return (-ENODEV);
3166 adapter = hba_soft_state[adapno];
3169 * Deletion of logical drive is a special case. The adapter
3170 * should be quiescent before this command is issued.
3172 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3173 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3176 * Do we support this feature
3178 if( !adapter->support_random_del ) {
3179 printk(KERN_WARNING "megaraid: logdrv ");
3180 printk("delete on non-supporting F/W.\n");
3182 return (-EINVAL);
3185 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3187 if( rval == 0 ) {
3188 memset(&mc, 0, sizeof(megacmd_t));
3190 mc.status = rval;
3192 rval = mega_n_to_m((void __user *)arg, &mc);
3195 return rval;
3198 * This interface only support the regular passthru commands.
3199 * Reject extended passthru and 64-bit passthru
3201 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3202 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3204 printk(KERN_WARNING "megaraid: rejected passthru.\n");
3206 return (-EINVAL);
3210 * For all internal commands, the buffer must be allocated in
3211 * <4GB address range
3213 if( make_local_pdev(adapter, &pdev) != 0 )
3214 return -EIO;
3216 /* Is it a passthru command or a DCMD */
3217 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3218 /* Passthru commands */
3220 pthru = pci_alloc_consistent(pdev,
3221 sizeof(mega_passthru),
3222 &pthru_dma_hndl);
3224 if( pthru == NULL ) {
3225 free_local_pdev(pdev);
3226 return (-ENOMEM);
3230 * The user passthru structure
3232 upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
3235 * Copy in the user passthru here.
3237 if( copy_from_user(pthru, upthru,
3238 sizeof(mega_passthru)) ) {
3240 pci_free_consistent(pdev,
3241 sizeof(mega_passthru), pthru,
3242 pthru_dma_hndl);
3244 free_local_pdev(pdev);
3246 return (-EFAULT);
3250 * Is there a data transfer
3252 if( pthru->dataxferlen ) {
3253 data = pci_alloc_consistent(pdev,
3254 pthru->dataxferlen,
3255 &data_dma_hndl);
3257 if( data == NULL ) {
3258 pci_free_consistent(pdev,
3259 sizeof(mega_passthru),
3260 pthru,
3261 pthru_dma_hndl);
3263 free_local_pdev(pdev);
3265 return (-ENOMEM);
3269 * Save the user address and point the kernel
3270 * address at just allocated memory
3272 uxferaddr = pthru->dataxferaddr;
3273 pthru->dataxferaddr = data_dma_hndl;
3278 * Is data coming down-stream
3280 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3282 * Get the user data
3284 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3285 pthru->dataxferlen) ) {
3286 rval = (-EFAULT);
3287 goto freemem_and_return;
3291 memset(&mc, 0, sizeof(megacmd_t));
3293 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3294 mc.xferaddr = (u32)pthru_dma_hndl;
3297 * Issue the command
3299 mega_internal_command(adapter, &mc, pthru);
3301 rval = mega_n_to_m((void __user *)arg, &mc);
3303 if( rval ) goto freemem_and_return;
3307 * Is data going up-stream
3309 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3310 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3311 pthru->dataxferlen) ) {
3312 rval = (-EFAULT);
3317 * Send the request sense data also, irrespective of
3318 * whether the user has asked for it or not.
3320 if (copy_to_user(upthru->reqsensearea,
3321 pthru->reqsensearea, 14))
3322 rval = -EFAULT;
3324 freemem_and_return:
3325 if( pthru->dataxferlen ) {
3326 pci_free_consistent(pdev,
3327 pthru->dataxferlen, data,
3328 data_dma_hndl);
3331 pci_free_consistent(pdev, sizeof(mega_passthru),
3332 pthru, pthru_dma_hndl);
3334 free_local_pdev(pdev);
3336 return rval;
3338 else {
3339 /* DCMD commands */
3342 * Is there a data transfer
3344 if( uioc.xferlen ) {
3345 data = pci_alloc_consistent(pdev,
3346 uioc.xferlen, &data_dma_hndl);
3348 if( data == NULL ) {
3349 free_local_pdev(pdev);
3350 return (-ENOMEM);
3353 uxferaddr = MBOX(uioc)->xferaddr;
3357 * Is data coming down-stream
3359 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3361 * Get the user data
3363 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3364 uioc.xferlen) ) {
3366 pci_free_consistent(pdev,
3367 uioc.xferlen,
3368 data, data_dma_hndl);
3370 free_local_pdev(pdev);
3372 return (-EFAULT);
3376 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3378 mc.xferaddr = (u32)data_dma_hndl;
3381 * Issue the command
3383 mega_internal_command(adapter, &mc, NULL);
3385 rval = mega_n_to_m((void __user *)arg, &mc);
3387 if( rval ) {
3388 if( uioc.xferlen ) {
3389 pci_free_consistent(pdev,
3390 uioc.xferlen, data,
3391 data_dma_hndl);
3394 free_local_pdev(pdev);
3396 return rval;
3400 * Is data going up-stream
3402 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3403 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3404 uioc.xferlen) ) {
3406 rval = (-EFAULT);
3410 if( uioc.xferlen ) {
3411 pci_free_consistent(pdev,
3412 uioc.xferlen, data,
3413 data_dma_hndl);
3416 free_local_pdev(pdev);
3418 return rval;
3421 default:
3422 return (-EINVAL);
3425 return 0;
3428 static long
3429 megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3431 int ret;
3433 mutex_lock(&megadev_mutex);
3434 ret = megadev_ioctl(filep, cmd, arg);
3435 mutex_unlock(&megadev_mutex);
3437 return ret;
3441 * mega_m_to_n()
3442 * @arg - user address
3443 * @uioc - new ioctl structure
3445 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3446 * structure
3448 * Converts the older mimd ioctl structure to newer NIT structure
3450 static int
3451 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3453 struct uioctl_t uioc_mimd;
3454 char signature[8] = {0};
3455 u8 opcode;
3456 u8 subopcode;
3460 * check is the application conforms to NIT. We do not have to do much
3461 * in that case.
3462 * We exploit the fact that the signature is stored in the very
3463 * beginning of the structure.
3466 if( copy_from_user(signature, arg, 7) )
3467 return (-EFAULT);
3469 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3472 * NOTE NOTE: The nit ioctl is still under flux because of
3473 * change of mailbox definition, in HPE. No applications yet
3474 * use this interface and let's not have applications use this
3475 * interface till the new specifitions are in place.
3477 return -EINVAL;
3478 #if 0
3479 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3480 return (-EFAULT);
3481 return 0;
3482 #endif
3486 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3488 * Get the user ioctl structure
3490 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3491 return (-EFAULT);
3495 * Get the opcode and subopcode for the commands
3497 opcode = uioc_mimd.ui.fcs.opcode;
3498 subopcode = uioc_mimd.ui.fcs.subopcode;
3500 switch (opcode) {
3501 case 0x82:
3503 switch (subopcode) {
3505 case MEGAIOC_QDRVRVER: /* Query driver version */
3506 uioc->opcode = GET_DRIVER_VER;
3507 uioc->uioc_uaddr = uioc_mimd.data;
3508 break;
3510 case MEGAIOC_QNADAP: /* Get # of adapters */
3511 uioc->opcode = GET_N_ADAP;
3512 uioc->uioc_uaddr = uioc_mimd.data;
3513 break;
3515 case MEGAIOC_QADAPINFO: /* Get adapter information */
3516 uioc->opcode = GET_ADAP_INFO;
3517 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3518 uioc->uioc_uaddr = uioc_mimd.data;
3519 break;
3521 default:
3522 return(-EINVAL);
3525 break;
3528 case 0x81:
3530 uioc->opcode = MBOX_CMD;
3531 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3533 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3535 uioc->xferlen = uioc_mimd.ui.fcs.length;
3537 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3538 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3540 break;
3542 case 0x80:
3544 uioc->opcode = MBOX_CMD;
3545 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3547 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3550 * Choose the xferlen bigger of input and output data
3552 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3553 uioc_mimd.outlen : uioc_mimd.inlen;
3555 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3556 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3558 break;
3560 default:
3561 return (-EINVAL);
3565 return 0;
3569 * mega_n_to_m()
3570 * @arg - user address
3571 * @mc - mailbox command
3573 * Updates the status information to the application, depending on application
3574 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3576 static int
3577 mega_n_to_m(void __user *arg, megacmd_t *mc)
3579 nitioctl_t __user *uiocp;
3580 megacmd_t __user *umc;
3581 mega_passthru __user *upthru;
3582 struct uioctl_t __user *uioc_mimd;
3583 char signature[8] = {0};
3586 * check is the application conforms to NIT.
3588 if( copy_from_user(signature, arg, 7) )
3589 return -EFAULT;
3591 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3593 uiocp = arg;
3595 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3596 return (-EFAULT);
3598 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3600 umc = MBOX_P(uiocp);
3602 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3603 return -EFAULT;
3605 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3606 return (-EFAULT);
3609 else {
3610 uioc_mimd = arg;
3612 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3613 return (-EFAULT);
3615 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3617 umc = (megacmd_t __user *)uioc_mimd->mbox;
3619 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3620 return (-EFAULT);
3622 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3623 return (-EFAULT);
3627 return 0;
3632 * MEGARAID 'FW' commands.
3636 * mega_is_bios_enabled()
3637 * @adapter - pointer to our soft state
3639 * issue command to find out if the BIOS is enabled for this controller
3641 static int
3642 mega_is_bios_enabled(adapter_t *adapter)
3644 unsigned char raw_mbox[sizeof(struct mbox_out)];
3645 mbox_t *mbox;
3646 int ret;
3648 mbox = (mbox_t *)raw_mbox;
3650 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3652 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3654 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3656 raw_mbox[0] = IS_BIOS_ENABLED;
3657 raw_mbox[2] = GET_BIOS;
3660 ret = issue_scb_block(adapter, raw_mbox);
3662 return *(char *)adapter->mega_buffer;
3667 * mega_enum_raid_scsi()
3668 * @adapter - pointer to our soft state
3670 * Find out what channels are RAID/SCSI. This information is used to
3671 * differentiate the virtual channels and physical channels and to support
3672 * ROMB feature and non-disk devices.
3674 static void
3675 mega_enum_raid_scsi(adapter_t *adapter)
3677 unsigned char raw_mbox[sizeof(struct mbox_out)];
3678 mbox_t *mbox;
3679 int i;
3681 mbox = (mbox_t *)raw_mbox;
3683 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3686 * issue command to find out what channels are raid/scsi
3688 raw_mbox[0] = CHNL_CLASS;
3689 raw_mbox[2] = GET_CHNL_CLASS;
3691 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3693 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3696 * Non-ROMB firmware fail this command, so all channels
3697 * must be shown RAID
3699 adapter->mega_ch_class = 0xFF;
3701 if(!issue_scb_block(adapter, raw_mbox)) {
3702 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3706 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
3707 if( (adapter->mega_ch_class >> i) & 0x01 ) {
3708 printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
3711 else {
3712 printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
3717 return;
3722 * mega_get_boot_drv()
3723 * @adapter - pointer to our soft state
3725 * Find out which device is the boot device. Note, any logical drive or any
3726 * phyical device (e.g., a CDROM) can be designated as a boot device.
3728 static void
3729 mega_get_boot_drv(adapter_t *adapter)
3731 struct private_bios_data *prv_bios_data;
3732 unsigned char raw_mbox[sizeof(struct mbox_out)];
3733 mbox_t *mbox;
3734 u16 cksum = 0;
3735 u8 *cksum_p;
3736 u8 boot_pdrv;
3737 int i;
3739 mbox = (mbox_t *)raw_mbox;
3741 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3743 raw_mbox[0] = BIOS_PVT_DATA;
3744 raw_mbox[2] = GET_BIOS_PVT_DATA;
3746 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3748 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3750 adapter->boot_ldrv_enabled = 0;
3751 adapter->boot_ldrv = 0;
3753 adapter->boot_pdrv_enabled = 0;
3754 adapter->boot_pdrv_ch = 0;
3755 adapter->boot_pdrv_tgt = 0;
3757 if(issue_scb_block(adapter, raw_mbox) == 0) {
3758 prv_bios_data =
3759 (struct private_bios_data *)adapter->mega_buffer;
3761 cksum = 0;
3762 cksum_p = (char *)prv_bios_data;
3763 for (i = 0; i < 14; i++ ) {
3764 cksum += (u16)(*cksum_p++);
3767 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
3770 * If MSB is set, a physical drive is set as boot
3771 * device
3773 if( prv_bios_data->boot_drv & 0x80 ) {
3774 adapter->boot_pdrv_enabled = 1;
3775 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
3776 adapter->boot_pdrv_ch = boot_pdrv / 16;
3777 adapter->boot_pdrv_tgt = boot_pdrv % 16;
3779 else {
3780 adapter->boot_ldrv_enabled = 1;
3781 adapter->boot_ldrv = prv_bios_data->boot_drv;
3789 * mega_support_random_del()
3790 * @adapter - pointer to our soft state
3792 * Find out if this controller supports random deletion and addition of
3793 * logical drives
3795 static int
3796 mega_support_random_del(adapter_t *adapter)
3798 unsigned char raw_mbox[sizeof(struct mbox_out)];
3799 mbox_t *mbox;
3800 int rval;
3802 mbox = (mbox_t *)raw_mbox;
3804 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3807 * issue command
3809 raw_mbox[0] = FC_DEL_LOGDRV;
3810 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3812 rval = issue_scb_block(adapter, raw_mbox);
3814 return !rval;
3819 * mega_support_ext_cdb()
3820 * @adapter - pointer to our soft state
3822 * Find out if this firmware support cdblen > 10
3824 static int
3825 mega_support_ext_cdb(adapter_t *adapter)
3827 unsigned char raw_mbox[sizeof(struct mbox_out)];
3828 mbox_t *mbox;
3829 int rval;
3831 mbox = (mbox_t *)raw_mbox;
3833 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3835 * issue command to find out if controller supports extended CDBs.
3837 raw_mbox[0] = 0xA4;
3838 raw_mbox[2] = 0x16;
3840 rval = issue_scb_block(adapter, raw_mbox);
3842 return !rval;
3847 * mega_del_logdrv()
3848 * @adapter - pointer to our soft state
3849 * @logdrv - logical drive to be deleted
3851 * Delete the specified logical drive. It is the responsibility of the user
3852 * app to let the OS know about this operation.
3854 static int
3855 mega_del_logdrv(adapter_t *adapter, int logdrv)
3857 unsigned long flags;
3858 scb_t *scb;
3859 int rval;
3862 * Stop sending commands to the controller, queue them internally.
3863 * When deletion is complete, ISR will flush the queue.
3865 atomic_set(&adapter->quiescent, 1);
3868 * Wait till all the issued commands are complete and there are no
3869 * commands in the pending queue
3871 while (atomic_read(&adapter->pend_cmds) > 0 ||
3872 !list_empty(&adapter->pending_list))
3873 msleep(1000); /* sleep for 1s */
3875 rval = mega_do_del_logdrv(adapter, logdrv);
3877 spin_lock_irqsave(&adapter->lock, flags);
3880 * If delete operation was successful, add 0x80 to the logical drive
3881 * ids for commands in the pending queue.
3883 if (adapter->read_ldidmap) {
3884 struct list_head *pos;
3885 list_for_each(pos, &adapter->pending_list) {
3886 scb = list_entry(pos, scb_t, list);
3887 if (scb->pthru->logdrv < 0x80 )
3888 scb->pthru->logdrv += 0x80;
3892 atomic_set(&adapter->quiescent, 0);
3894 mega_runpendq(adapter);
3896 spin_unlock_irqrestore(&adapter->lock, flags);
3898 return rval;
3902 static int
3903 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
3905 megacmd_t mc;
3906 int rval;
3908 memset( &mc, 0, sizeof(megacmd_t));
3910 mc.cmd = FC_DEL_LOGDRV;
3911 mc.opcode = OP_DEL_LOGDRV;
3912 mc.subopcode = logdrv;
3914 rval = mega_internal_command(adapter, &mc, NULL);
3916 /* log this event */
3917 if(rval) {
3918 printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
3919 return rval;
3923 * After deleting first logical drive, the logical drives must be
3924 * addressed by adding 0x80 to the logical drive id.
3926 adapter->read_ldidmap = 1;
3928 return rval;
3933 * mega_get_max_sgl()
3934 * @adapter - pointer to our soft state
3936 * Find out the maximum number of scatter-gather elements supported by this
3937 * version of the firmware
3939 static void
3940 mega_get_max_sgl(adapter_t *adapter)
3942 unsigned char raw_mbox[sizeof(struct mbox_out)];
3943 mbox_t *mbox;
3945 mbox = (mbox_t *)raw_mbox;
3947 memset(mbox, 0, sizeof(raw_mbox));
3949 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3951 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3953 raw_mbox[0] = MAIN_MISC_OPCODE;
3954 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3957 if( issue_scb_block(adapter, raw_mbox) ) {
3959 * f/w does not support this command. Choose the default value
3961 adapter->sglen = MIN_SGLIST;
3963 else {
3964 adapter->sglen = *((char *)adapter->mega_buffer);
3967 * Make sure this is not more than the resources we are
3968 * planning to allocate
3970 if ( adapter->sglen > MAX_SGLIST )
3971 adapter->sglen = MAX_SGLIST;
3974 return;
3979 * mega_support_cluster()
3980 * @adapter - pointer to our soft state
3982 * Find out if this firmware support cluster calls.
3984 static int
3985 mega_support_cluster(adapter_t *adapter)
3987 unsigned char raw_mbox[sizeof(struct mbox_out)];
3988 mbox_t *mbox;
3990 mbox = (mbox_t *)raw_mbox;
3992 memset(mbox, 0, sizeof(raw_mbox));
3994 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3996 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3999 * Try to get the initiator id. This command will succeed iff the
4000 * clustering is available on this HBA.
4002 raw_mbox[0] = MEGA_GET_TARGET_ID;
4004 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
4007 * Cluster support available. Get the initiator target id.
4008 * Tell our id to mid-layer too.
4010 adapter->this_id = *(u32 *)adapter->mega_buffer;
4011 adapter->host->this_id = adapter->this_id;
4013 return 1;
4016 return 0;
4019 #ifdef CONFIG_PROC_FS
4021 * mega_adapinq()
4022 * @adapter - pointer to our soft state
4023 * @dma_handle - DMA address of the buffer
4025 * Issue internal commands while interrupts are available.
4026 * We only issue direct mailbox commands from within the driver. ioctl()
4027 * interface using these routines can issue passthru commands.
4029 static int
4030 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
4032 megacmd_t mc;
4034 memset(&mc, 0, sizeof(megacmd_t));
4036 if( adapter->flag & BOARD_40LD ) {
4037 mc.cmd = FC_NEW_CONFIG;
4038 mc.opcode = NC_SUBOP_ENQUIRY3;
4039 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
4041 else {
4042 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
4045 mc.xferaddr = (u32)dma_handle;
4047 if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
4048 return -1;
4051 return 0;
4055 /** mega_internal_dev_inquiry()
4056 * @adapter - pointer to our soft state
4057 * @ch - channel for this device
4058 * @tgt - ID of this device
4059 * @buf_dma_handle - DMA address of the buffer
4061 * Issue the scsi inquiry for the specified device.
4063 static int
4064 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4065 dma_addr_t buf_dma_handle)
4067 mega_passthru *pthru;
4068 dma_addr_t pthru_dma_handle;
4069 megacmd_t mc;
4070 int rval;
4071 struct pci_dev *pdev;
4075 * For all internal commands, the buffer must be allocated in <4GB
4076 * address range
4078 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4080 pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4081 &pthru_dma_handle);
4083 if( pthru == NULL ) {
4084 free_local_pdev(pdev);
4085 return -1;
4088 pthru->timeout = 2;
4089 pthru->ars = 1;
4090 pthru->reqsenselen = 14;
4091 pthru->islogical = 0;
4093 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4095 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4097 pthru->cdblen = 6;
4099 pthru->cdb[0] = INQUIRY;
4100 pthru->cdb[1] = 0;
4101 pthru->cdb[2] = 0;
4102 pthru->cdb[3] = 0;
4103 pthru->cdb[4] = 255;
4104 pthru->cdb[5] = 0;
4107 pthru->dataxferaddr = (u32)buf_dma_handle;
4108 pthru->dataxferlen = 256;
4110 memset(&mc, 0, sizeof(megacmd_t));
4112 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4113 mc.xferaddr = (u32)pthru_dma_handle;
4115 rval = mega_internal_command(adapter, &mc, pthru);
4117 pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4118 pthru_dma_handle);
4120 free_local_pdev(pdev);
4122 return rval;
4124 #endif
4127 * mega_internal_command()
4128 * @adapter - pointer to our soft state
4129 * @mc - the mailbox command
4130 * @pthru - Passthru structure for DCDB commands
4132 * Issue the internal commands in interrupt mode.
4133 * The last argument is the address of the passthru structure if the command
4134 * to be fired is a passthru command
4136 * lockscope specifies whether the caller has already acquired the lock. Of
4137 * course, the caller must know which lock we are talking about.
4139 * Note: parameter 'pthru' is null for non-passthru commands.
4141 static int
4142 mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4144 Scsi_Cmnd *scmd;
4145 struct scsi_device *sdev;
4146 scb_t *scb;
4147 int rval;
4149 scmd = scsi_allocate_command(GFP_KERNEL);
4150 if (!scmd)
4151 return -ENOMEM;
4154 * The internal commands share one command id and hence are
4155 * serialized. This is so because we want to reserve maximum number of
4156 * available command ids for the I/O commands.
4158 mutex_lock(&adapter->int_mtx);
4160 scb = &adapter->int_scb;
4161 memset(scb, 0, sizeof(scb_t));
4163 sdev = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
4164 scmd->device = sdev;
4166 memset(adapter->int_cdb, 0, sizeof(adapter->int_cdb));
4167 scmd->cmnd = adapter->int_cdb;
4168 scmd->device->host = adapter->host;
4169 scmd->host_scribble = (void *)scb;
4170 scmd->cmnd[0] = MEGA_INTERNAL_CMD;
4172 scb->state |= SCB_ACTIVE;
4173 scb->cmd = scmd;
4175 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4178 * Is it a passthru command
4180 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4182 scb->pthru = pthru;
4185 scb->idx = CMDID_INT_CMDS;
4187 megaraid_queue_lck(scmd, mega_internal_done);
4189 wait_for_completion(&adapter->int_waitq);
4191 rval = scmd->result;
4192 mc->status = scmd->result;
4193 kfree(sdev);
4196 * Print a debug message for all failed commands. Applications can use
4197 * this information.
4199 if( scmd->result && trace_level ) {
4200 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4201 mc->cmd, mc->opcode, mc->subopcode, scmd->result);
4204 mutex_unlock(&adapter->int_mtx);
4206 scsi_free_command(GFP_KERNEL, scmd);
4208 return rval;
4213 * mega_internal_done()
4214 * @scmd - internal scsi command
4216 * Callback routine for internal commands.
4218 static void
4219 mega_internal_done(Scsi_Cmnd *scmd)
4221 adapter_t *adapter;
4223 adapter = (adapter_t *)scmd->device->host->hostdata;
4225 complete(&adapter->int_waitq);
4230 static struct scsi_host_template megaraid_template = {
4231 .module = THIS_MODULE,
4232 .name = "MegaRAID",
4233 .proc_name = "megaraid_legacy",
4234 .info = megaraid_info,
4235 .queuecommand = megaraid_queue,
4236 .bios_param = megaraid_biosparam,
4237 .max_sectors = MAX_SECTORS_PER_IO,
4238 .can_queue = MAX_COMMANDS,
4239 .this_id = DEFAULT_INITIATOR_ID,
4240 .sg_tablesize = MAX_SGLIST,
4241 .cmd_per_lun = DEF_CMD_PER_LUN,
4242 .use_clustering = ENABLE_CLUSTERING,
4243 .eh_abort_handler = megaraid_abort,
4244 .eh_device_reset_handler = megaraid_reset,
4245 .eh_bus_reset_handler = megaraid_reset,
4246 .eh_host_reset_handler = megaraid_reset,
4249 static int
4250 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4252 struct Scsi_Host *host;
4253 adapter_t *adapter;
4254 unsigned long mega_baseport, tbase, flag = 0;
4255 u16 subsysid, subsysvid;
4256 u8 pci_bus, pci_dev_func;
4257 int irq, i, j;
4258 int error = -ENODEV;
4260 if (pci_enable_device(pdev))
4261 goto out;
4262 pci_set_master(pdev);
4264 pci_bus = pdev->bus->number;
4265 pci_dev_func = pdev->devfn;
4268 * The megaraid3 stuff reports the ID of the Intel part which is not
4269 * remotely specific to the megaraid
4271 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4272 u16 magic;
4274 * Don't fall over the Compaq management cards using the same
4275 * PCI identifier
4277 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4278 pdev->subsystem_device == 0xC000)
4279 return -ENODEV;
4280 /* Now check the magic signature byte */
4281 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4282 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4283 return -ENODEV;
4284 /* Ok it is probably a megaraid */
4288 * For these vendor and device ids, signature offsets are not
4289 * valid and 64 bit is implicit
4291 if (id->driver_data & BOARD_64BIT)
4292 flag |= BOARD_64BIT;
4293 else {
4294 u32 magic64;
4296 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4297 if (magic64 == HBA_SIGNATURE_64BIT)
4298 flag |= BOARD_64BIT;
4301 subsysvid = pdev->subsystem_vendor;
4302 subsysid = pdev->subsystem_device;
4304 printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4305 id->vendor, id->device, pci_bus);
4307 printk("slot %d:func %d\n",
4308 PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
4310 /* Read the base port and IRQ from PCI */
4311 mega_baseport = pci_resource_start(pdev, 0);
4312 irq = pdev->irq;
4314 tbase = mega_baseport;
4315 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4316 flag |= BOARD_MEMMAP;
4318 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4319 printk(KERN_WARNING "megaraid: mem region busy!\n");
4320 goto out_disable_device;
4323 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4324 if (!mega_baseport) {
4325 printk(KERN_WARNING
4326 "megaraid: could not map hba memory\n");
4327 goto out_release_region;
4329 } else {
4330 flag |= BOARD_IOMAP;
4331 mega_baseport += 0x10;
4333 if (!request_region(mega_baseport, 16, "megaraid"))
4334 goto out_disable_device;
4337 /* Initialize SCSI Host structure */
4338 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4339 if (!host)
4340 goto out_iounmap;
4342 adapter = (adapter_t *)host->hostdata;
4343 memset(adapter, 0, sizeof(adapter_t));
4345 printk(KERN_NOTICE
4346 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4347 host->host_no, mega_baseport, irq);
4349 adapter->base = mega_baseport;
4350 if (flag & BOARD_MEMMAP)
4351 adapter->mmio_base = (void __iomem *) mega_baseport;
4353 INIT_LIST_HEAD(&adapter->free_list);
4354 INIT_LIST_HEAD(&adapter->pending_list);
4355 INIT_LIST_HEAD(&adapter->completed_list);
4357 adapter->flag = flag;
4358 spin_lock_init(&adapter->lock);
4360 host->cmd_per_lun = max_cmd_per_lun;
4361 host->max_sectors = max_sectors_per_io;
4363 adapter->dev = pdev;
4364 adapter->host = host;
4366 adapter->host->irq = irq;
4368 if (flag & BOARD_MEMMAP)
4369 adapter->host->base = tbase;
4370 else {
4371 adapter->host->io_port = tbase;
4372 adapter->host->n_io_port = 16;
4375 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4378 * Allocate buffer to issue internal commands.
4380 adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4381 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4382 if (!adapter->mega_buffer) {
4383 printk(KERN_WARNING "megaraid: out of RAM.\n");
4384 goto out_host_put;
4387 adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4388 if (!adapter->scb_list) {
4389 printk(KERN_WARNING "megaraid: out of RAM.\n");
4390 goto out_free_cmd_buffer;
4393 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4394 megaraid_isr_memmapped : megaraid_isr_iomapped,
4395 IRQF_SHARED, "megaraid", adapter)) {
4396 printk(KERN_WARNING
4397 "megaraid: Couldn't register IRQ %d!\n", irq);
4398 goto out_free_scb_list;
4401 if (mega_setup_mailbox(adapter))
4402 goto out_free_irq;
4404 if (mega_query_adapter(adapter))
4405 goto out_free_mbox;
4408 * Have checks for some buggy f/w
4410 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4412 * Which firmware
4414 if (!strcmp(adapter->fw_version, "3.00") ||
4415 !strcmp(adapter->fw_version, "3.01")) {
4417 printk( KERN_WARNING
4418 "megaraid: Your card is a Dell PERC "
4419 "2/SC RAID controller with "
4420 "firmware\nmegaraid: 3.00 or 3.01. "
4421 "This driver is known to have "
4422 "corruption issues\nmegaraid: with "
4423 "those firmware versions on this "
4424 "specific card. In order\nmegaraid: "
4425 "to protect your data, please upgrade "
4426 "your firmware to version\nmegaraid: "
4427 "3.10 or later, available from the "
4428 "Dell Technical Support web\n"
4429 "megaraid: site at\nhttp://support."
4430 "dell.com/us/en/filelib/download/"
4431 "index.asp?fileid=2940\n"
4437 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4438 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4439 * support, since this firmware cannot handle 64 bit
4440 * addressing
4442 if ((subsysvid == PCI_VENDOR_ID_HP) &&
4443 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4445 * which firmware
4447 if (!strcmp(adapter->fw_version, "H01.07") ||
4448 !strcmp(adapter->fw_version, "H01.08") ||
4449 !strcmp(adapter->fw_version, "H01.09") ) {
4450 printk(KERN_WARNING
4451 "megaraid: Firmware H.01.07, "
4452 "H.01.08, and H.01.09 on 1M/2M "
4453 "controllers\n"
4454 "megaraid: do not support 64 bit "
4455 "addressing.\nmegaraid: DISABLING "
4456 "64 bit support.\n");
4457 adapter->flag &= ~BOARD_64BIT;
4461 if (mega_is_bios_enabled(adapter))
4462 mega_hbas[hba_count].is_bios_enabled = 1;
4463 mega_hbas[hba_count].hostdata_addr = adapter;
4466 * Find out which channel is raid and which is scsi. This is
4467 * for ROMB support.
4469 mega_enum_raid_scsi(adapter);
4472 * Find out if a logical drive is set as the boot drive. If
4473 * there is one, will make that as the first logical drive.
4474 * ROMB: Do we have to boot from a physical drive. Then all
4475 * the physical drives would appear before the logical disks.
4476 * Else, all the physical drives would be exported to the mid
4477 * layer after logical drives.
4479 mega_get_boot_drv(adapter);
4481 if (adapter->boot_pdrv_enabled) {
4482 j = adapter->product_info.nchannels;
4483 for( i = 0; i < j; i++ )
4484 adapter->logdrv_chan[i] = 0;
4485 for( i = j; i < NVIRT_CHAN + j; i++ )
4486 adapter->logdrv_chan[i] = 1;
4487 } else {
4488 for (i = 0; i < NVIRT_CHAN; i++)
4489 adapter->logdrv_chan[i] = 1;
4490 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4491 adapter->logdrv_chan[i] = 0;
4492 adapter->mega_ch_class <<= NVIRT_CHAN;
4496 * Do we support random deletion and addition of logical
4497 * drives
4499 adapter->read_ldidmap = 0; /* set it after first logdrv
4500 delete cmd */
4501 adapter->support_random_del = mega_support_random_del(adapter);
4503 /* Initialize SCBs */
4504 if (mega_init_scb(adapter))
4505 goto out_free_mbox;
4508 * Reset the pending commands counter
4510 atomic_set(&adapter->pend_cmds, 0);
4513 * Reset the adapter quiescent flag
4515 atomic_set(&adapter->quiescent, 0);
4517 hba_soft_state[hba_count] = adapter;
4520 * Fill in the structure which needs to be passed back to the
4521 * application when it does an ioctl() for controller related
4522 * information.
4524 i = hba_count;
4526 mcontroller[i].base = mega_baseport;
4527 mcontroller[i].irq = irq;
4528 mcontroller[i].numldrv = adapter->numldrv;
4529 mcontroller[i].pcibus = pci_bus;
4530 mcontroller[i].pcidev = id->device;
4531 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4532 mcontroller[i].pciid = -1;
4533 mcontroller[i].pcivendor = id->vendor;
4534 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4535 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4538 /* Set the Mode of addressing to 64 bit if we can */
4539 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4540 pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4541 adapter->has_64bit_addr = 1;
4542 } else {
4543 pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4544 adapter->has_64bit_addr = 0;
4547 mutex_init(&adapter->int_mtx);
4548 init_completion(&adapter->int_waitq);
4550 adapter->this_id = DEFAULT_INITIATOR_ID;
4551 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4553 #if MEGA_HAVE_CLUSTERING
4555 * Is cluster support enabled on this controller
4556 * Note: In a cluster the HBAs ( the initiators ) will have
4557 * different target IDs and we cannot assume it to be 7. Call
4558 * to mega_support_cluster() will get the target ids also if
4559 * the cluster support is available
4561 adapter->has_cluster = mega_support_cluster(adapter);
4562 if (adapter->has_cluster) {
4563 printk(KERN_NOTICE
4564 "megaraid: Cluster driver, initiator id:%d\n",
4565 adapter->this_id);
4567 #endif
4569 pci_set_drvdata(pdev, host);
4571 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4573 error = scsi_add_host(host, &pdev->dev);
4574 if (error)
4575 goto out_free_mbox;
4577 scsi_scan_host(host);
4578 hba_count++;
4579 return 0;
4581 out_free_mbox:
4582 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4583 adapter->una_mbox64, adapter->una_mbox64_dma);
4584 out_free_irq:
4585 free_irq(adapter->host->irq, adapter);
4586 out_free_scb_list:
4587 kfree(adapter->scb_list);
4588 out_free_cmd_buffer:
4589 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4590 adapter->mega_buffer, adapter->buf_dma_handle);
4591 out_host_put:
4592 scsi_host_put(host);
4593 out_iounmap:
4594 if (flag & BOARD_MEMMAP)
4595 iounmap((void *)mega_baseport);
4596 out_release_region:
4597 if (flag & BOARD_MEMMAP)
4598 release_mem_region(tbase, 128);
4599 else
4600 release_region(mega_baseport, 16);
4601 out_disable_device:
4602 pci_disable_device(pdev);
4603 out:
4604 return error;
4607 static void
4608 __megaraid_shutdown(adapter_t *adapter)
4610 u_char raw_mbox[sizeof(struct mbox_out)];
4611 mbox_t *mbox = (mbox_t *)raw_mbox;
4612 int i;
4614 /* Flush adapter cache */
4615 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4616 raw_mbox[0] = FLUSH_ADAPTER;
4618 free_irq(adapter->host->irq, adapter);
4620 /* Issue a blocking (interrupts disabled) command to the card */
4621 issue_scb_block(adapter, raw_mbox);
4623 /* Flush disks cache */
4624 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4625 raw_mbox[0] = FLUSH_SYSTEM;
4627 /* Issue a blocking (interrupts disabled) command to the card */
4628 issue_scb_block(adapter, raw_mbox);
4630 if (atomic_read(&adapter->pend_cmds) > 0)
4631 printk(KERN_WARNING "megaraid: pending commands!!\n");
4634 * Have a delibrate delay to make sure all the caches are
4635 * actually flushed.
4637 for (i = 0; i <= 10; i++)
4638 mdelay(1000);
4641 static void
4642 megaraid_remove_one(struct pci_dev *pdev)
4644 struct Scsi_Host *host = pci_get_drvdata(pdev);
4645 adapter_t *adapter = (adapter_t *)host->hostdata;
4647 scsi_remove_host(host);
4649 __megaraid_shutdown(adapter);
4651 /* Free our resources */
4652 if (adapter->flag & BOARD_MEMMAP) {
4653 iounmap((void *)adapter->base);
4654 release_mem_region(adapter->host->base, 128);
4655 } else
4656 release_region(adapter->base, 16);
4658 mega_free_sgl(adapter);
4660 #ifdef CONFIG_PROC_FS
4661 if (adapter->controller_proc_dir_entry) {
4662 remove_proc_entry("stat", adapter->controller_proc_dir_entry);
4663 remove_proc_entry("config",
4664 adapter->controller_proc_dir_entry);
4665 remove_proc_entry("mailbox",
4666 adapter->controller_proc_dir_entry);
4667 #if MEGA_HAVE_ENH_PROC
4668 remove_proc_entry("rebuild-rate",
4669 adapter->controller_proc_dir_entry);
4670 remove_proc_entry("battery-status",
4671 adapter->controller_proc_dir_entry);
4673 remove_proc_entry("diskdrives-ch0",
4674 adapter->controller_proc_dir_entry);
4675 remove_proc_entry("diskdrives-ch1",
4676 adapter->controller_proc_dir_entry);
4677 remove_proc_entry("diskdrives-ch2",
4678 adapter->controller_proc_dir_entry);
4679 remove_proc_entry("diskdrives-ch3",
4680 adapter->controller_proc_dir_entry);
4682 remove_proc_entry("raiddrives-0-9",
4683 adapter->controller_proc_dir_entry);
4684 remove_proc_entry("raiddrives-10-19",
4685 adapter->controller_proc_dir_entry);
4686 remove_proc_entry("raiddrives-20-29",
4687 adapter->controller_proc_dir_entry);
4688 remove_proc_entry("raiddrives-30-39",
4689 adapter->controller_proc_dir_entry);
4690 #endif
4692 char buf[12] = { 0 };
4693 sprintf(buf, "hba%d", adapter->host->host_no);
4694 remove_proc_entry(buf, mega_proc_dir_entry);
4697 #endif
4699 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4700 adapter->mega_buffer, adapter->buf_dma_handle);
4701 kfree(adapter->scb_list);
4702 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4703 adapter->una_mbox64, adapter->una_mbox64_dma);
4705 scsi_host_put(host);
4706 pci_disable_device(pdev);
4708 hba_count--;
4711 static void
4712 megaraid_shutdown(struct pci_dev *pdev)
4714 struct Scsi_Host *host = pci_get_drvdata(pdev);
4715 adapter_t *adapter = (adapter_t *)host->hostdata;
4717 __megaraid_shutdown(adapter);
4720 static struct pci_device_id megaraid_pci_tbl[] = {
4721 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4722 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4723 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
4724 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4725 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
4726 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4727 {0,}
4729 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
4731 static struct pci_driver megaraid_pci_driver = {
4732 .name = "megaraid_legacy",
4733 .id_table = megaraid_pci_tbl,
4734 .probe = megaraid_probe_one,
4735 .remove = megaraid_remove_one,
4736 .shutdown = megaraid_shutdown,
4739 static int __init megaraid_init(void)
4741 int error;
4743 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
4744 max_cmd_per_lun = MAX_CMD_PER_LUN;
4745 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
4746 max_mbox_busy_wait = MBOX_BUSY_WAIT;
4748 #ifdef CONFIG_PROC_FS
4749 mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
4750 if (!mega_proc_dir_entry) {
4751 printk(KERN_WARNING
4752 "megaraid: failed to create megaraid root\n");
4754 #endif
4755 error = pci_register_driver(&megaraid_pci_driver);
4756 if (error) {
4757 #ifdef CONFIG_PROC_FS
4758 remove_proc_entry("megaraid", NULL);
4759 #endif
4760 return error;
4764 * Register the driver as a character device, for applications
4765 * to access it for ioctls.
4766 * First argument (major) to register_chrdev implies a dynamic
4767 * major number allocation.
4769 major = register_chrdev(0, "megadev_legacy", &megadev_fops);
4770 if (!major) {
4771 printk(KERN_WARNING
4772 "megaraid: failed to register char device\n");
4775 return 0;
4778 static void __exit megaraid_exit(void)
4781 * Unregister the character device interface to the driver.
4783 unregister_chrdev(major, "megadev_legacy");
4785 pci_unregister_driver(&megaraid_pci_driver);
4787 #ifdef CONFIG_PROC_FS
4788 remove_proc_entry("megaraid", NULL);
4789 #endif
4792 module_init(megaraid_init);
4793 module_exit(megaraid_exit);
4795 /* vi: set ts=8 sw=8 tw=78: */