powerpc/mm/slice: create header files dedicated to slices
[linux-2.6/btrfs-unstable.git] / drivers / scsi / megaraid.c
blob7195cff51d4c4181f60ab3330796818771590798
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 <linux/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 dev_warn(&adapter->dev->dev,
272 "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 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
315 "%c%d%d.%d%d",
316 adapter->product_info.fw_version[2],
318 adapter->product_info.fw_version[1] & 0x0f,
320 adapter->product_info.fw_version[0] & 0x0f);
321 snprintf(adapter->bios_version, sizeof(adapter->fw_version),
322 "%c%d%d.%d%d",
323 adapter->product_info.bios_version[2],
325 adapter->product_info.bios_version[1] & 0x0f,
327 adapter->product_info.bios_version[0] & 0x0f);
328 } else {
329 memcpy(adapter->fw_version,
330 (char *)adapter->product_info.fw_version, 4);
331 adapter->fw_version[4] = 0;
333 memcpy(adapter->bios_version,
334 (char *)adapter->product_info.bios_version, 4);
336 adapter->bios_version[4] = 0;
339 dev_notice(&adapter->dev->dev, "[%s:%s] detected %d logical drives\n",
340 adapter->fw_version, adapter->bios_version, adapter->numldrv);
343 * Do we support extended (>10 bytes) cdbs
345 adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
346 if (adapter->support_ext_cdb)
347 dev_notice(&adapter->dev->dev, "supports extended CDBs\n");
350 return 0;
354 * mega_runpendq()
355 * @adapter - pointer to our soft state
357 * Runs through the list of pending requests.
359 static inline void
360 mega_runpendq(adapter_t *adapter)
362 if(!list_empty(&adapter->pending_list))
363 __mega_runpendq(adapter);
367 * megaraid_queue()
368 * @scmd - Issue this scsi command
369 * @done - the callback hook into the scsi mid-layer
371 * The command queuing entry point for the mid-layer.
373 static int
374 megaraid_queue_lck(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
376 adapter_t *adapter;
377 scb_t *scb;
378 int busy=0;
379 unsigned long flags;
381 adapter = (adapter_t *)scmd->device->host->hostdata;
383 scmd->scsi_done = done;
387 * Allocate and build a SCB request
388 * busy flag will be set if mega_build_cmd() command could not
389 * allocate scb. We will return non-zero status in that case.
390 * NOTE: scb can be null even though certain commands completed
391 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
392 * return 0 in that case.
395 spin_lock_irqsave(&adapter->lock, flags);
396 scb = mega_build_cmd(adapter, scmd, &busy);
397 if (!scb)
398 goto out;
400 scb->state |= SCB_PENDQ;
401 list_add_tail(&scb->list, &adapter->pending_list);
404 * Check if the HBA is in quiescent state, e.g., during a
405 * delete logical drive opertion. If it is, don't run
406 * the pending_list.
408 if (atomic_read(&adapter->quiescent) == 0)
409 mega_runpendq(adapter);
411 busy = 0;
412 out:
413 spin_unlock_irqrestore(&adapter->lock, flags);
414 return busy;
417 static DEF_SCSI_QCMD(megaraid_queue)
420 * mega_allocate_scb()
421 * @adapter - pointer to our soft state
422 * @cmd - scsi command from the mid-layer
424 * Allocate a SCB structure. This is the central structure for controller
425 * commands.
427 static inline scb_t *
428 mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
430 struct list_head *head = &adapter->free_list;
431 scb_t *scb;
433 /* Unlink command from Free List */
434 if( !list_empty(head) ) {
436 scb = list_entry(head->next, scb_t, list);
438 list_del_init(head->next);
440 scb->state = SCB_ACTIVE;
441 scb->cmd = cmd;
442 scb->dma_type = MEGA_DMA_TYPE_NONE;
444 return scb;
447 return NULL;
451 * mega_get_ldrv_num()
452 * @adapter - pointer to our soft state
453 * @cmd - scsi mid layer command
454 * @channel - channel on the controller
456 * Calculate the logical drive number based on the information in scsi command
457 * and the channel number.
459 static inline int
460 mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
462 int tgt;
463 int ldrv_num;
465 tgt = cmd->device->id;
467 if ( tgt > adapter->this_id )
468 tgt--; /* we do not get inquires for initiator id */
470 ldrv_num = (channel * 15) + tgt;
474 * If we have a logical drive with boot enabled, project it first
476 if( adapter->boot_ldrv_enabled ) {
477 if( ldrv_num == 0 ) {
478 ldrv_num = adapter->boot_ldrv;
480 else {
481 if( ldrv_num <= adapter->boot_ldrv ) {
482 ldrv_num--;
488 * If "delete logical drive" feature is enabled on this controller.
489 * Do only if at least one delete logical drive operation was done.
491 * Also, after logical drive deletion, instead of logical drive number,
492 * the value returned should be 0x80+logical drive id.
494 * These is valid only for IO commands.
497 if (adapter->support_random_del && adapter->read_ldidmap )
498 switch (cmd->cmnd[0]) {
499 case READ_6: /* fall through */
500 case WRITE_6: /* fall through */
501 case READ_10: /* fall through */
502 case WRITE_10:
503 ldrv_num += 0x80;
506 return ldrv_num;
510 * mega_build_cmd()
511 * @adapter - pointer to our soft state
512 * @cmd - Prepare using this scsi command
513 * @busy - busy flag if no resources
515 * Prepares a command and scatter gather list for the controller. This routine
516 * also finds out if the commands is intended for a logical drive or a
517 * physical device and prepares the controller command accordingly.
519 * We also re-order the logical drives and physical devices based on their
520 * boot settings.
522 static scb_t *
523 mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
525 mega_ext_passthru *epthru;
526 mega_passthru *pthru;
527 scb_t *scb;
528 mbox_t *mbox;
529 u32 seg;
530 char islogical;
531 int max_ldrv_num;
532 int channel = 0;
533 int target = 0;
534 int ldrv_num = 0; /* logical drive number */
537 * We know what channels our logical drives are on - mega_find_card()
539 islogical = adapter->logdrv_chan[cmd->device->channel];
542 * The theory: If physical drive is chosen for boot, all the physical
543 * devices are exported before the logical drives, otherwise physical
544 * devices are pushed after logical drives, in which case - Kernel sees
545 * the physical devices on virtual channel which is obviously converted
546 * to actual channel on the HBA.
548 if( adapter->boot_pdrv_enabled ) {
549 if( islogical ) {
550 /* logical channel */
551 channel = cmd->device->channel -
552 adapter->product_info.nchannels;
554 else {
555 /* this is physical channel */
556 channel = cmd->device->channel;
557 target = cmd->device->id;
560 * boot from a physical disk, that disk needs to be
561 * exposed first IF both the channels are SCSI, then
562 * booting from the second channel is not allowed.
564 if( target == 0 ) {
565 target = adapter->boot_pdrv_tgt;
567 else if( target == adapter->boot_pdrv_tgt ) {
568 target = 0;
572 else {
573 if( islogical ) {
574 /* this is the logical channel */
575 channel = cmd->device->channel;
577 else {
578 /* physical channel */
579 channel = cmd->device->channel - NVIRT_CHAN;
580 target = cmd->device->id;
585 if(islogical) {
587 /* have just LUN 0 for each target on virtual channels */
588 if (cmd->device->lun) {
589 cmd->result = (DID_BAD_TARGET << 16);
590 cmd->scsi_done(cmd);
591 return NULL;
594 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
597 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
598 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
601 * max_ldrv_num increases by 0x80 if some logical drive was
602 * deleted.
604 if(adapter->read_ldidmap)
605 max_ldrv_num += 0x80;
607 if(ldrv_num > max_ldrv_num ) {
608 cmd->result = (DID_BAD_TARGET << 16);
609 cmd->scsi_done(cmd);
610 return NULL;
614 else {
615 if( cmd->device->lun > 7) {
617 * Do not support lun >7 for physically accessed
618 * devices
620 cmd->result = (DID_BAD_TARGET << 16);
621 cmd->scsi_done(cmd);
622 return NULL;
628 * Logical drive commands
631 if(islogical) {
632 switch (cmd->cmnd[0]) {
633 case TEST_UNIT_READY:
634 #if MEGA_HAVE_CLUSTERING
636 * Do we support clustering and is the support enabled
637 * If no, return success always
639 if( !adapter->has_cluster ) {
640 cmd->result = (DID_OK << 16);
641 cmd->scsi_done(cmd);
642 return NULL;
645 if(!(scb = mega_allocate_scb(adapter, cmd))) {
646 *busy = 1;
647 return NULL;
650 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
651 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
652 scb->raw_mbox[3] = ldrv_num;
654 scb->dma_direction = PCI_DMA_NONE;
656 return scb;
657 #else
658 cmd->result = (DID_OK << 16);
659 cmd->scsi_done(cmd);
660 return NULL;
661 #endif
663 case MODE_SENSE: {
664 char *buf;
665 struct scatterlist *sg;
667 sg = scsi_sglist(cmd);
668 buf = kmap_atomic(sg_page(sg)) + sg->offset;
670 memset(buf, 0, cmd->cmnd[4]);
671 kunmap_atomic(buf - sg->offset);
673 cmd->result = (DID_OK << 16);
674 cmd->scsi_done(cmd);
675 return NULL;
678 case READ_CAPACITY:
679 case INQUIRY:
681 if(!(adapter->flag & (1L << cmd->device->channel))) {
683 dev_notice(&adapter->dev->dev,
684 "scsi%d: scanning scsi channel %d "
685 "for logical drives\n",
686 adapter->host->host_no,
687 cmd->device->channel);
689 adapter->flag |= (1L << cmd->device->channel);
692 /* Allocate a SCB and initialize passthru */
693 if(!(scb = mega_allocate_scb(adapter, cmd))) {
694 *busy = 1;
695 return NULL;
697 pthru = scb->pthru;
699 mbox = (mbox_t *)scb->raw_mbox;
700 memset(mbox, 0, sizeof(scb->raw_mbox));
701 memset(pthru, 0, sizeof(mega_passthru));
703 pthru->timeout = 0;
704 pthru->ars = 1;
705 pthru->reqsenselen = 14;
706 pthru->islogical = 1;
707 pthru->logdrv = ldrv_num;
708 pthru->cdblen = cmd->cmd_len;
709 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
711 if( adapter->has_64bit_addr ) {
712 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
714 else {
715 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
718 scb->dma_direction = PCI_DMA_FROMDEVICE;
720 pthru->numsgelements = mega_build_sglist(adapter, scb,
721 &pthru->dataxferaddr, &pthru->dataxferlen);
723 mbox->m_out.xferaddr = scb->pthru_dma_addr;
725 return scb;
727 case READ_6:
728 case WRITE_6:
729 case READ_10:
730 case WRITE_10:
731 case READ_12:
732 case WRITE_12:
734 /* Allocate a SCB and initialize mailbox */
735 if(!(scb = mega_allocate_scb(adapter, cmd))) {
736 *busy = 1;
737 return NULL;
739 mbox = (mbox_t *)scb->raw_mbox;
741 memset(mbox, 0, sizeof(scb->raw_mbox));
742 mbox->m_out.logdrv = ldrv_num;
745 * A little hack: 2nd bit is zero for all scsi read
746 * commands and is set for all scsi write commands
748 if( adapter->has_64bit_addr ) {
749 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
750 MEGA_MBOXCMD_LWRITE64:
751 MEGA_MBOXCMD_LREAD64 ;
753 else {
754 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
755 MEGA_MBOXCMD_LWRITE:
756 MEGA_MBOXCMD_LREAD ;
760 * 6-byte READ(0x08) or WRITE(0x0A) cdb
762 if( cmd->cmd_len == 6 ) {
763 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
764 mbox->m_out.lba =
765 ((u32)cmd->cmnd[1] << 16) |
766 ((u32)cmd->cmnd[2] << 8) |
767 (u32)cmd->cmnd[3];
769 mbox->m_out.lba &= 0x1FFFFF;
771 #if MEGA_HAVE_STATS
773 * Take modulo 0x80, since the logical drive
774 * number increases by 0x80 when a logical
775 * drive was deleted
777 if (*cmd->cmnd == READ_6) {
778 adapter->nreads[ldrv_num%0x80]++;
779 adapter->nreadblocks[ldrv_num%0x80] +=
780 mbox->m_out.numsectors;
781 } else {
782 adapter->nwrites[ldrv_num%0x80]++;
783 adapter->nwriteblocks[ldrv_num%0x80] +=
784 mbox->m_out.numsectors;
786 #endif
790 * 10-byte READ(0x28) or WRITE(0x2A) cdb
792 if( cmd->cmd_len == 10 ) {
793 mbox->m_out.numsectors =
794 (u32)cmd->cmnd[8] |
795 ((u32)cmd->cmnd[7] << 8);
796 mbox->m_out.lba =
797 ((u32)cmd->cmnd[2] << 24) |
798 ((u32)cmd->cmnd[3] << 16) |
799 ((u32)cmd->cmnd[4] << 8) |
800 (u32)cmd->cmnd[5];
802 #if MEGA_HAVE_STATS
803 if (*cmd->cmnd == READ_10) {
804 adapter->nreads[ldrv_num%0x80]++;
805 adapter->nreadblocks[ldrv_num%0x80] +=
806 mbox->m_out.numsectors;
807 } else {
808 adapter->nwrites[ldrv_num%0x80]++;
809 adapter->nwriteblocks[ldrv_num%0x80] +=
810 mbox->m_out.numsectors;
812 #endif
816 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
818 if( cmd->cmd_len == 12 ) {
819 mbox->m_out.lba =
820 ((u32)cmd->cmnd[2] << 24) |
821 ((u32)cmd->cmnd[3] << 16) |
822 ((u32)cmd->cmnd[4] << 8) |
823 (u32)cmd->cmnd[5];
825 mbox->m_out.numsectors =
826 ((u32)cmd->cmnd[6] << 24) |
827 ((u32)cmd->cmnd[7] << 16) |
828 ((u32)cmd->cmnd[8] << 8) |
829 (u32)cmd->cmnd[9];
831 #if MEGA_HAVE_STATS
832 if (*cmd->cmnd == READ_12) {
833 adapter->nreads[ldrv_num%0x80]++;
834 adapter->nreadblocks[ldrv_num%0x80] +=
835 mbox->m_out.numsectors;
836 } else {
837 adapter->nwrites[ldrv_num%0x80]++;
838 adapter->nwriteblocks[ldrv_num%0x80] +=
839 mbox->m_out.numsectors;
841 #endif
845 * If it is a read command
847 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
848 scb->dma_direction = PCI_DMA_FROMDEVICE;
850 else {
851 scb->dma_direction = PCI_DMA_TODEVICE;
854 /* Calculate Scatter-Gather info */
855 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
856 (u32 *)&mbox->m_out.xferaddr, &seg);
858 return scb;
860 #if MEGA_HAVE_CLUSTERING
861 case RESERVE: /* Fall through */
862 case RELEASE:
865 * Do we support clustering and is the support enabled
867 if( ! adapter->has_cluster ) {
869 cmd->result = (DID_BAD_TARGET << 16);
870 cmd->scsi_done(cmd);
871 return NULL;
874 /* Allocate a SCB and initialize mailbox */
875 if(!(scb = mega_allocate_scb(adapter, cmd))) {
876 *busy = 1;
877 return NULL;
880 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
881 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
882 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
884 scb->raw_mbox[3] = ldrv_num;
886 scb->dma_direction = PCI_DMA_NONE;
888 return scb;
889 #endif
891 default:
892 cmd->result = (DID_BAD_TARGET << 16);
893 cmd->scsi_done(cmd);
894 return NULL;
899 * Passthru drive commands
901 else {
902 /* Allocate a SCB and initialize passthru */
903 if(!(scb = mega_allocate_scb(adapter, cmd))) {
904 *busy = 1;
905 return NULL;
908 mbox = (mbox_t *)scb->raw_mbox;
909 memset(mbox, 0, sizeof(scb->raw_mbox));
911 if( adapter->support_ext_cdb ) {
913 epthru = mega_prepare_extpassthru(adapter, scb, cmd,
914 channel, target);
916 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
918 mbox->m_out.xferaddr = scb->epthru_dma_addr;
921 else {
923 pthru = mega_prepare_passthru(adapter, scb, cmd,
924 channel, target);
926 /* Initialize mailbox */
927 if( adapter->has_64bit_addr ) {
928 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
930 else {
931 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
934 mbox->m_out.xferaddr = scb->pthru_dma_addr;
937 return scb;
939 return NULL;
944 * mega_prepare_passthru()
945 * @adapter - pointer to our soft state
946 * @scb - our scsi control block
947 * @cmd - scsi command from the mid-layer
948 * @channel - actual channel on the controller
949 * @target - actual id on the controller.
951 * prepare a command for the scsi physical devices.
953 static mega_passthru *
954 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
955 int channel, int target)
957 mega_passthru *pthru;
959 pthru = scb->pthru;
960 memset(pthru, 0, sizeof (mega_passthru));
962 /* 0=6sec/1=60sec/2=10min/3=3hrs */
963 pthru->timeout = 2;
965 pthru->ars = 1;
966 pthru->reqsenselen = 14;
967 pthru->islogical = 0;
969 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
971 pthru->target = (adapter->flag & BOARD_40LD) ?
972 (channel << 4) | target : target;
974 pthru->cdblen = cmd->cmd_len;
975 pthru->logdrv = cmd->device->lun;
977 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
979 /* Not sure about the direction */
980 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
982 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
983 switch (cmd->cmnd[0]) {
984 case INQUIRY:
985 case READ_CAPACITY:
986 if(!(adapter->flag & (1L << cmd->device->channel))) {
988 dev_notice(&adapter->dev->dev,
989 "scsi%d: scanning scsi channel %d [P%d] "
990 "for physical devices\n",
991 adapter->host->host_no,
992 cmd->device->channel, channel);
994 adapter->flag |= (1L << cmd->device->channel);
996 /* Fall through */
997 default:
998 pthru->numsgelements = mega_build_sglist(adapter, scb,
999 &pthru->dataxferaddr, &pthru->dataxferlen);
1000 break;
1002 return pthru;
1007 * mega_prepare_extpassthru()
1008 * @adapter - pointer to our soft state
1009 * @scb - our scsi control block
1010 * @cmd - scsi command from the mid-layer
1011 * @channel - actual channel on the controller
1012 * @target - actual id on the controller.
1014 * prepare a command for the scsi physical devices. This rountine prepares
1015 * commands for devices which can take extended CDBs (>10 bytes)
1017 static mega_ext_passthru *
1018 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1019 int channel, int target)
1021 mega_ext_passthru *epthru;
1023 epthru = scb->epthru;
1024 memset(epthru, 0, sizeof(mega_ext_passthru));
1026 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1027 epthru->timeout = 2;
1029 epthru->ars = 1;
1030 epthru->reqsenselen = 14;
1031 epthru->islogical = 0;
1033 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1034 epthru->target = (adapter->flag & BOARD_40LD) ?
1035 (channel << 4) | target : target;
1037 epthru->cdblen = cmd->cmd_len;
1038 epthru->logdrv = cmd->device->lun;
1040 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1042 /* Not sure about the direction */
1043 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1045 switch(cmd->cmnd[0]) {
1046 case INQUIRY:
1047 case READ_CAPACITY:
1048 if(!(adapter->flag & (1L << cmd->device->channel))) {
1050 dev_notice(&adapter->dev->dev,
1051 "scsi%d: scanning scsi channel %d [P%d] "
1052 "for physical devices\n",
1053 adapter->host->host_no,
1054 cmd->device->channel, channel);
1056 adapter->flag |= (1L << cmd->device->channel);
1058 /* Fall through */
1059 default:
1060 epthru->numsgelements = mega_build_sglist(adapter, scb,
1061 &epthru->dataxferaddr, &epthru->dataxferlen);
1062 break;
1065 return epthru;
1068 static void
1069 __mega_runpendq(adapter_t *adapter)
1071 scb_t *scb;
1072 struct list_head *pos, *next;
1074 /* Issue any pending commands to the card */
1075 list_for_each_safe(pos, next, &adapter->pending_list) {
1077 scb = list_entry(pos, scb_t, list);
1079 if( !(scb->state & SCB_ISSUED) ) {
1081 if( issue_scb(adapter, scb) != 0 )
1082 return;
1086 return;
1091 * issue_scb()
1092 * @adapter - pointer to our soft state
1093 * @scb - scsi control block
1095 * Post a command to the card if the mailbox is available, otherwise return
1096 * busy. We also take the scb from the pending list if the mailbox is
1097 * available.
1099 static int
1100 issue_scb(adapter_t *adapter, scb_t *scb)
1102 volatile mbox64_t *mbox64 = adapter->mbox64;
1103 volatile mbox_t *mbox = adapter->mbox;
1104 unsigned int i = 0;
1106 if(unlikely(mbox->m_in.busy)) {
1107 do {
1108 udelay(1);
1109 i++;
1110 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1112 if(mbox->m_in.busy) return -1;
1115 /* Copy mailbox data into host structure */
1116 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1117 sizeof(struct mbox_out));
1119 mbox->m_out.cmdid = scb->idx; /* Set cmdid */
1120 mbox->m_in.busy = 1; /* Set busy */
1124 * Increment the pending queue counter
1126 atomic_inc(&adapter->pend_cmds);
1128 switch (mbox->m_out.cmd) {
1129 case MEGA_MBOXCMD_LREAD64:
1130 case MEGA_MBOXCMD_LWRITE64:
1131 case MEGA_MBOXCMD_PASSTHRU64:
1132 case MEGA_MBOXCMD_EXTPTHRU:
1133 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1134 mbox64->xfer_segment_hi = 0;
1135 mbox->m_out.xferaddr = 0xFFFFFFFF;
1136 break;
1137 default:
1138 mbox64->xfer_segment_lo = 0;
1139 mbox64->xfer_segment_hi = 0;
1143 * post the command
1145 scb->state |= SCB_ISSUED;
1147 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1148 mbox->m_in.poll = 0;
1149 mbox->m_in.ack = 0;
1150 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1152 else {
1153 irq_enable(adapter);
1154 issue_command(adapter);
1157 return 0;
1161 * Wait until the controller's mailbox is available
1163 static inline int
1164 mega_busywait_mbox (adapter_t *adapter)
1166 if (adapter->mbox->m_in.busy)
1167 return __mega_busywait_mbox(adapter);
1168 return 0;
1172 * issue_scb_block()
1173 * @adapter - pointer to our soft state
1174 * @raw_mbox - the mailbox
1176 * Issue a scb in synchronous and non-interrupt mode
1178 static int
1179 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1181 volatile mbox64_t *mbox64 = adapter->mbox64;
1182 volatile mbox_t *mbox = adapter->mbox;
1183 u8 byte;
1185 /* Wait until mailbox is free */
1186 if(mega_busywait_mbox (adapter))
1187 goto bug_blocked_mailbox;
1189 /* Copy mailbox data into host structure */
1190 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1191 mbox->m_out.cmdid = 0xFE;
1192 mbox->m_in.busy = 1;
1194 switch (raw_mbox[0]) {
1195 case MEGA_MBOXCMD_LREAD64:
1196 case MEGA_MBOXCMD_LWRITE64:
1197 case MEGA_MBOXCMD_PASSTHRU64:
1198 case MEGA_MBOXCMD_EXTPTHRU:
1199 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1200 mbox64->xfer_segment_hi = 0;
1201 mbox->m_out.xferaddr = 0xFFFFFFFF;
1202 break;
1203 default:
1204 mbox64->xfer_segment_lo = 0;
1205 mbox64->xfer_segment_hi = 0;
1208 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1209 mbox->m_in.poll = 0;
1210 mbox->m_in.ack = 0;
1211 mbox->m_in.numstatus = 0xFF;
1212 mbox->m_in.status = 0xFF;
1213 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1215 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1216 cpu_relax();
1218 mbox->m_in.numstatus = 0xFF;
1220 while( (volatile u8)mbox->m_in.poll != 0x77 )
1221 cpu_relax();
1223 mbox->m_in.poll = 0;
1224 mbox->m_in.ack = 0x77;
1226 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1228 while(RDINDOOR(adapter) & 0x2)
1229 cpu_relax();
1231 else {
1232 irq_disable(adapter);
1233 issue_command(adapter);
1235 while (!((byte = irq_state(adapter)) & INTR_VALID))
1236 cpu_relax();
1238 set_irq_state(adapter, byte);
1239 irq_enable(adapter);
1240 irq_ack(adapter);
1243 return mbox->m_in.status;
1245 bug_blocked_mailbox:
1246 dev_warn(&adapter->dev->dev, "Blocked mailbox......!!\n");
1247 udelay (1000);
1248 return -1;
1253 * megaraid_isr_iomapped()
1254 * @irq - irq
1255 * @devp - pointer to our soft state
1257 * Interrupt service routine for io-mapped controllers.
1258 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1259 * and service the completed commands.
1261 static irqreturn_t
1262 megaraid_isr_iomapped(int irq, void *devp)
1264 adapter_t *adapter = devp;
1265 unsigned long flags;
1266 u8 status;
1267 u8 nstatus;
1268 u8 completed[MAX_FIRMWARE_STATUS];
1269 u8 byte;
1270 int handled = 0;
1274 * loop till F/W has more commands for us to complete.
1276 spin_lock_irqsave(&adapter->lock, flags);
1278 do {
1279 /* Check if a valid interrupt is pending */
1280 byte = irq_state(adapter);
1281 if( (byte & VALID_INTR_BYTE) == 0 ) {
1283 * No more pending commands
1285 goto out_unlock;
1287 set_irq_state(adapter, byte);
1289 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1290 == 0xFF)
1291 cpu_relax();
1292 adapter->mbox->m_in.numstatus = 0xFF;
1294 status = adapter->mbox->m_in.status;
1297 * decrement the pending queue counter
1299 atomic_sub(nstatus, &adapter->pend_cmds);
1301 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1302 nstatus);
1304 /* Acknowledge interrupt */
1305 irq_ack(adapter);
1307 mega_cmd_done(adapter, completed, nstatus, status);
1309 mega_rundoneq(adapter);
1311 handled = 1;
1313 /* Loop through any pending requests */
1314 if(atomic_read(&adapter->quiescent) == 0) {
1315 mega_runpendq(adapter);
1318 } while(1);
1320 out_unlock:
1322 spin_unlock_irqrestore(&adapter->lock, flags);
1324 return IRQ_RETVAL(handled);
1329 * megaraid_isr_memmapped()
1330 * @irq - irq
1331 * @devp - pointer to our soft state
1333 * Interrupt service routine for memory-mapped controllers.
1334 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1335 * and service the completed commands.
1337 static irqreturn_t
1338 megaraid_isr_memmapped(int irq, void *devp)
1340 adapter_t *adapter = devp;
1341 unsigned long flags;
1342 u8 status;
1343 u32 dword = 0;
1344 u8 nstatus;
1345 u8 completed[MAX_FIRMWARE_STATUS];
1346 int handled = 0;
1350 * loop till F/W has more commands for us to complete.
1352 spin_lock_irqsave(&adapter->lock, flags);
1354 do {
1355 /* Check if a valid interrupt is pending */
1356 dword = RDOUTDOOR(adapter);
1357 if(dword != 0x10001234) {
1359 * No more pending commands
1361 goto out_unlock;
1363 WROUTDOOR(adapter, 0x10001234);
1365 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1366 == 0xFF) {
1367 cpu_relax();
1369 adapter->mbox->m_in.numstatus = 0xFF;
1371 status = adapter->mbox->m_in.status;
1374 * decrement the pending queue counter
1376 atomic_sub(nstatus, &adapter->pend_cmds);
1378 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1379 nstatus);
1381 /* Acknowledge interrupt */
1382 WRINDOOR(adapter, 0x2);
1384 handled = 1;
1386 while( RDINDOOR(adapter) & 0x02 )
1387 cpu_relax();
1389 mega_cmd_done(adapter, completed, nstatus, status);
1391 mega_rundoneq(adapter);
1393 /* Loop through any pending requests */
1394 if(atomic_read(&adapter->quiescent) == 0) {
1395 mega_runpendq(adapter);
1398 } while(1);
1400 out_unlock:
1402 spin_unlock_irqrestore(&adapter->lock, flags);
1404 return IRQ_RETVAL(handled);
1407 * mega_cmd_done()
1408 * @adapter - pointer to our soft state
1409 * @completed - array of ids of completed commands
1410 * @nstatus - number of completed commands
1411 * @status - status of the last command completed
1413 * Complete the commands and call the scsi mid-layer callback hooks.
1415 static void
1416 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1418 mega_ext_passthru *epthru = NULL;
1419 struct scatterlist *sgl;
1420 Scsi_Cmnd *cmd = NULL;
1421 mega_passthru *pthru = NULL;
1422 mbox_t *mbox = NULL;
1423 u8 c;
1424 scb_t *scb;
1425 int islogical;
1426 int cmdid;
1427 int i;
1430 * for all the commands completed, call the mid-layer callback routine
1431 * and free the scb.
1433 for( i = 0; i < nstatus; i++ ) {
1435 cmdid = completed[i];
1438 * Only free SCBs for the commands coming down from the
1439 * mid-layer, not for which were issued internally
1441 * For internal command, restore the status returned by the
1442 * firmware so that user can interpret it.
1444 if (cmdid == CMDID_INT_CMDS) {
1445 scb = &adapter->int_scb;
1447 list_del_init(&scb->list);
1448 scb->state = SCB_FREE;
1450 adapter->int_status = status;
1451 complete(&adapter->int_waitq);
1452 } else {
1453 scb = &adapter->scb_list[cmdid];
1456 * Make sure f/w has completed a valid command
1458 if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1459 dev_crit(&adapter->dev->dev, "invalid command "
1460 "Id %d, scb->state:%x, scsi cmd:%p\n",
1461 cmdid, scb->state, scb->cmd);
1463 continue;
1467 * Was a abort issued for this command
1469 if( scb->state & SCB_ABORT ) {
1471 dev_warn(&adapter->dev->dev,
1472 "aborted cmd [%x] complete\n",
1473 scb->idx);
1475 scb->cmd->result = (DID_ABORT << 16);
1477 list_add_tail(SCSI_LIST(scb->cmd),
1478 &adapter->completed_list);
1480 mega_free_scb(adapter, scb);
1482 continue;
1486 * Was a reset issued for this command
1488 if( scb->state & SCB_RESET ) {
1490 dev_warn(&adapter->dev->dev,
1491 "reset cmd [%x] complete\n",
1492 scb->idx);
1494 scb->cmd->result = (DID_RESET << 16);
1496 list_add_tail(SCSI_LIST(scb->cmd),
1497 &adapter->completed_list);
1499 mega_free_scb (adapter, scb);
1501 continue;
1504 cmd = scb->cmd;
1505 pthru = scb->pthru;
1506 epthru = scb->epthru;
1507 mbox = (mbox_t *)scb->raw_mbox;
1509 #if MEGA_HAVE_STATS
1512 int logdrv = mbox->m_out.logdrv;
1514 islogical = adapter->logdrv_chan[cmd->channel];
1516 * Maintain an error counter for the logical drive.
1517 * Some application like SNMP agent need such
1518 * statistics
1520 if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1521 cmd->cmnd[0] == READ_10 ||
1522 cmd->cmnd[0] == READ_12)) {
1524 * Logical drive number increases by 0x80 when
1525 * a logical drive is deleted
1527 adapter->rd_errors[logdrv%0x80]++;
1530 if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1531 cmd->cmnd[0] == WRITE_10 ||
1532 cmd->cmnd[0] == WRITE_12)) {
1534 * Logical drive number increases by 0x80 when
1535 * a logical drive is deleted
1537 adapter->wr_errors[logdrv%0x80]++;
1541 #endif
1545 * Do not return the presence of hard disk on the channel so,
1546 * inquiry sent, and returned data==hard disk or removable
1547 * hard disk and not logical, request should return failure! -
1548 * PJ
1550 islogical = adapter->logdrv_chan[cmd->device->channel];
1551 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1553 sgl = scsi_sglist(cmd);
1554 if( sg_page(sgl) ) {
1555 c = *(unsigned char *) sg_virt(&sgl[0]);
1556 } else {
1557 dev_warn(&adapter->dev->dev, "invalid sg\n");
1558 c = 0;
1561 if(IS_RAID_CH(adapter, cmd->device->channel) &&
1562 ((c & 0x1F ) == TYPE_DISK)) {
1563 status = 0xF0;
1567 /* clear result; otherwise, success returns corrupt value */
1568 cmd->result = 0;
1570 /* Convert MegaRAID status to Linux error code */
1571 switch (status) {
1572 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1573 cmd->result |= (DID_OK << 16);
1574 break;
1576 case 0x02: /* ERROR_ABORTED, i.e.
1577 SCSI_STATUS_CHECK_CONDITION */
1579 /* set sense_buffer and result fields */
1580 if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1581 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1583 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1584 14);
1586 cmd->result = (DRIVER_SENSE << 24) |
1587 (DID_OK << 16) |
1588 (CHECK_CONDITION << 1);
1590 else {
1591 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1593 memcpy(cmd->sense_buffer,
1594 epthru->reqsensearea, 14);
1596 cmd->result = (DRIVER_SENSE << 24) |
1597 (DID_OK << 16) |
1598 (CHECK_CONDITION << 1);
1599 } else {
1600 cmd->sense_buffer[0] = 0x70;
1601 cmd->sense_buffer[2] = ABORTED_COMMAND;
1602 cmd->result |= (CHECK_CONDITION << 1);
1605 break;
1607 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1608 SCSI_STATUS_BUSY */
1609 cmd->result |= (DID_BUS_BUSY << 16) | status;
1610 break;
1612 default:
1613 #if MEGA_HAVE_CLUSTERING
1615 * If TEST_UNIT_READY fails, we know
1616 * MEGA_RESERVATION_STATUS failed
1618 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1619 cmd->result |= (DID_ERROR << 16) |
1620 (RESERVATION_CONFLICT << 1);
1622 else
1624 * Error code returned is 1 if Reserve or Release
1625 * failed or the input parameter is invalid
1627 if( status == 1 &&
1628 (cmd->cmnd[0] == RESERVE ||
1629 cmd->cmnd[0] == RELEASE) ) {
1631 cmd->result |= (DID_ERROR << 16) |
1632 (RESERVATION_CONFLICT << 1);
1634 else
1635 #endif
1636 cmd->result |= (DID_BAD_TARGET << 16)|status;
1639 mega_free_scb(adapter, scb);
1641 /* Add Scsi_Command to end of completed queue */
1642 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1648 * mega_runpendq()
1650 * Run through the list of completed requests and finish it
1652 static void
1653 mega_rundoneq (adapter_t *adapter)
1655 Scsi_Cmnd *cmd;
1656 struct list_head *pos;
1658 list_for_each(pos, &adapter->completed_list) {
1660 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1662 cmd = list_entry(spos, Scsi_Cmnd, SCp);
1663 cmd->scsi_done(cmd);
1666 INIT_LIST_HEAD(&adapter->completed_list);
1671 * Free a SCB structure
1672 * Note: We assume the scsi commands associated with this scb is not free yet.
1674 static void
1675 mega_free_scb(adapter_t *adapter, scb_t *scb)
1677 switch( scb->dma_type ) {
1679 case MEGA_DMA_TYPE_NONE:
1680 break;
1682 case MEGA_SGLIST:
1683 scsi_dma_unmap(scb->cmd);
1684 break;
1685 default:
1686 break;
1690 * Remove from the pending list
1692 list_del_init(&scb->list);
1694 /* Link the scb back into free list */
1695 scb->state = SCB_FREE;
1696 scb->cmd = NULL;
1698 list_add(&scb->list, &adapter->free_list);
1702 static int
1703 __mega_busywait_mbox (adapter_t *adapter)
1705 volatile mbox_t *mbox = adapter->mbox;
1706 long counter;
1708 for (counter = 0; counter < 10000; counter++) {
1709 if (!mbox->m_in.busy)
1710 return 0;
1711 udelay(100);
1712 cond_resched();
1714 return -1; /* give up after 1 second */
1718 * Copies data to SGLIST
1719 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1721 static int
1722 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1724 struct scatterlist *sg;
1725 Scsi_Cmnd *cmd;
1726 int sgcnt;
1727 int idx;
1729 cmd = scb->cmd;
1732 * Copy Scatter-Gather list info into controller structure.
1734 * The number of sg elements returned must not exceed our limit
1736 sgcnt = scsi_dma_map(cmd);
1738 scb->dma_type = MEGA_SGLIST;
1740 BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1742 *len = 0;
1744 if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1745 sg = scsi_sglist(cmd);
1746 scb->dma_h_bulkdata = sg_dma_address(sg);
1747 *buf = (u32)scb->dma_h_bulkdata;
1748 *len = sg_dma_len(sg);
1749 return 0;
1752 scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1753 if (adapter->has_64bit_addr) {
1754 scb->sgl64[idx].address = sg_dma_address(sg);
1755 *len += scb->sgl64[idx].length = sg_dma_len(sg);
1756 } else {
1757 scb->sgl[idx].address = sg_dma_address(sg);
1758 *len += scb->sgl[idx].length = sg_dma_len(sg);
1762 /* Reset pointer and length fields */
1763 *buf = scb->sgl_dma_addr;
1765 /* Return count of SG requests */
1766 return sgcnt;
1771 * mega_8_to_40ld()
1773 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1774 * Enquiry3 structures for later use
1776 static void
1777 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1778 mega_product_info *product_info)
1780 int i;
1782 product_info->max_commands = inquiry->adapter_info.max_commands;
1783 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1784 product_info->nchannels = inquiry->adapter_info.nchannels;
1786 for (i = 0; i < 4; i++) {
1787 product_info->fw_version[i] =
1788 inquiry->adapter_info.fw_version[i];
1790 product_info->bios_version[i] =
1791 inquiry->adapter_info.bios_version[i];
1793 enquiry3->cache_flush_interval =
1794 inquiry->adapter_info.cache_flush_interval;
1796 product_info->dram_size = inquiry->adapter_info.dram_size;
1798 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1800 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1801 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1802 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1803 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1806 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1807 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1810 static inline void
1811 mega_free_sgl(adapter_t *adapter)
1813 scb_t *scb;
1814 int i;
1816 for(i = 0; i < adapter->max_cmds; i++) {
1818 scb = &adapter->scb_list[i];
1820 if( scb->sgl64 ) {
1821 pci_free_consistent(adapter->dev,
1822 sizeof(mega_sgl64) * adapter->sglen,
1823 scb->sgl64,
1824 scb->sgl_dma_addr);
1826 scb->sgl64 = NULL;
1829 if( scb->pthru ) {
1830 pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1831 scb->pthru, scb->pthru_dma_addr);
1833 scb->pthru = NULL;
1836 if( scb->epthru ) {
1837 pci_free_consistent(adapter->dev,
1838 sizeof(mega_ext_passthru),
1839 scb->epthru, scb->epthru_dma_addr);
1841 scb->epthru = NULL;
1849 * Get information about the card/driver
1851 const char *
1852 megaraid_info(struct Scsi_Host *host)
1854 static char buffer[512];
1855 adapter_t *adapter;
1857 adapter = (adapter_t *)host->hostdata;
1859 sprintf (buffer,
1860 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1861 adapter->fw_version, adapter->product_info.max_commands,
1862 adapter->host->max_id, adapter->host->max_channel,
1863 (u32)adapter->host->max_lun);
1864 return buffer;
1868 * Abort a previous SCSI request. Only commands on the pending list can be
1869 * aborted. All the commands issued to the F/W must complete.
1871 static int
1872 megaraid_abort(Scsi_Cmnd *cmd)
1874 adapter_t *adapter;
1875 int rval;
1877 adapter = (adapter_t *)cmd->device->host->hostdata;
1879 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1882 * This is required here to complete any completed requests
1883 * to be communicated over to the mid layer.
1885 mega_rundoneq(adapter);
1887 return rval;
1891 static int
1892 megaraid_reset(struct scsi_cmnd *cmd)
1894 adapter_t *adapter;
1895 megacmd_t mc;
1896 int rval;
1898 adapter = (adapter_t *)cmd->device->host->hostdata;
1900 #if MEGA_HAVE_CLUSTERING
1901 mc.cmd = MEGA_CLUSTER_CMD;
1902 mc.opcode = MEGA_RESET_RESERVATIONS;
1904 if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1905 dev_warn(&adapter->dev->dev, "reservation reset failed\n");
1907 else {
1908 dev_info(&adapter->dev->dev, "reservation reset\n");
1910 #endif
1912 spin_lock_irq(&adapter->lock);
1914 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1917 * This is required here to complete any completed requests
1918 * to be communicated over to the mid layer.
1920 mega_rundoneq(adapter);
1921 spin_unlock_irq(&adapter->lock);
1923 return rval;
1927 * megaraid_abort_and_reset()
1928 * @adapter - megaraid soft state
1929 * @cmd - scsi command to be aborted or reset
1930 * @aor - abort or reset flag
1932 * Try to locate the scsi command in the pending queue. If found and is not
1933 * issued to the controller, abort/reset it. Otherwise return failure
1935 static int
1936 megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
1938 struct list_head *pos, *next;
1939 scb_t *scb;
1941 dev_warn(&adapter->dev->dev, "%s cmd=%x <c=%d t=%d l=%d>\n",
1942 (aor == SCB_ABORT)? "ABORTING":"RESET",
1943 cmd->cmnd[0], cmd->device->channel,
1944 cmd->device->id, (u32)cmd->device->lun);
1946 if(list_empty(&adapter->pending_list))
1947 return FAILED;
1949 list_for_each_safe(pos, next, &adapter->pending_list) {
1951 scb = list_entry(pos, scb_t, list);
1953 if (scb->cmd == cmd) { /* Found command */
1955 scb->state |= aor;
1958 * Check if this command has firmware ownership. If
1959 * yes, we cannot reset this command. Whenever f/w
1960 * completes this command, we will return appropriate
1961 * status from ISR.
1963 if( scb->state & SCB_ISSUED ) {
1965 dev_warn(&adapter->dev->dev,
1966 "%s[%x], fw owner\n",
1967 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1968 scb->idx);
1970 return FAILED;
1972 else {
1975 * Not yet issued! Remove from the pending
1976 * list
1978 dev_warn(&adapter->dev->dev,
1979 "%s-[%x], driver owner\n",
1980 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1981 scb->idx);
1983 mega_free_scb(adapter, scb);
1985 if( aor == SCB_ABORT ) {
1986 cmd->result = (DID_ABORT << 16);
1988 else {
1989 cmd->result = (DID_RESET << 16);
1992 list_add_tail(SCSI_LIST(cmd),
1993 &adapter->completed_list);
1995 return SUCCESS;
2000 return FAILED;
2003 static inline int
2004 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2006 *pdev = pci_alloc_dev(NULL);
2008 if( *pdev == NULL ) return -1;
2010 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2012 if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
2013 kfree(*pdev);
2014 return -1;
2017 return 0;
2020 static inline void
2021 free_local_pdev(struct pci_dev *pdev)
2023 kfree(pdev);
2027 * mega_allocate_inquiry()
2028 * @dma_handle - handle returned for dma address
2029 * @pdev - handle to pci device
2031 * allocates memory for inquiry structure
2033 static inline void *
2034 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2036 return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2040 static inline void
2041 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2043 pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2047 #ifdef CONFIG_PROC_FS
2048 /* Following code handles /proc fs */
2051 * proc_show_config()
2052 * @m - Synthetic file construction data
2053 * @v - File iterator
2055 * Display configuration information about the controller.
2057 static int
2058 proc_show_config(struct seq_file *m, void *v)
2061 adapter_t *adapter = m->private;
2063 seq_puts(m, MEGARAID_VERSION);
2064 if(adapter->product_info.product_name[0])
2065 seq_printf(m, "%s\n", adapter->product_info.product_name);
2067 seq_puts(m, "Controller Type: ");
2069 if( adapter->flag & BOARD_MEMMAP )
2070 seq_puts(m, "438/466/467/471/493/518/520/531/532\n");
2071 else
2072 seq_puts(m, "418/428/434\n");
2074 if(adapter->flag & BOARD_40LD)
2075 seq_puts(m, "Controller Supports 40 Logical Drives\n");
2077 if(adapter->flag & BOARD_64BIT)
2078 seq_puts(m, "Controller capable of 64-bit memory addressing\n");
2079 if( adapter->has_64bit_addr )
2080 seq_puts(m, "Controller using 64-bit memory addressing\n");
2081 else
2082 seq_puts(m, "Controller is not using 64-bit memory addressing\n");
2084 seq_printf(m, "Base = %08lx, Irq = %d, ",
2085 adapter->base, adapter->host->irq);
2087 seq_printf(m, "Logical Drives = %d, Channels = %d\n",
2088 adapter->numldrv, adapter->product_info.nchannels);
2090 seq_printf(m, "Version =%s:%s, DRAM = %dMb\n",
2091 adapter->fw_version, adapter->bios_version,
2092 adapter->product_info.dram_size);
2094 seq_printf(m, "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2095 adapter->product_info.max_commands, adapter->max_cmds);
2097 seq_printf(m, "support_ext_cdb = %d\n", adapter->support_ext_cdb);
2098 seq_printf(m, "support_random_del = %d\n", adapter->support_random_del);
2099 seq_printf(m, "boot_ldrv_enabled = %d\n", adapter->boot_ldrv_enabled);
2100 seq_printf(m, "boot_ldrv = %d\n", adapter->boot_ldrv);
2101 seq_printf(m, "boot_pdrv_enabled = %d\n", adapter->boot_pdrv_enabled);
2102 seq_printf(m, "boot_pdrv_ch = %d\n", adapter->boot_pdrv_ch);
2103 seq_printf(m, "boot_pdrv_tgt = %d\n", adapter->boot_pdrv_tgt);
2104 seq_printf(m, "quiescent = %d\n",
2105 atomic_read(&adapter->quiescent));
2106 seq_printf(m, "has_cluster = %d\n", adapter->has_cluster);
2108 seq_puts(m, "\nModule Parameters:\n");
2109 seq_printf(m, "max_cmd_per_lun = %d\n", max_cmd_per_lun);
2110 seq_printf(m, "max_sectors_per_io = %d\n", max_sectors_per_io);
2111 return 0;
2115 * proc_show_stat()
2116 * @m - Synthetic file construction data
2117 * @v - File iterator
2119 * Display statistical information about the I/O activity.
2121 static int
2122 proc_show_stat(struct seq_file *m, void *v)
2124 adapter_t *adapter = m->private;
2125 #if MEGA_HAVE_STATS
2126 int i;
2127 #endif
2129 seq_puts(m, "Statistical Information for this controller\n");
2130 seq_printf(m, "pend_cmds = %d\n", atomic_read(&adapter->pend_cmds));
2131 #if MEGA_HAVE_STATS
2132 for(i = 0; i < adapter->numldrv; i++) {
2133 seq_printf(m, "Logical Drive %d:\n", i);
2134 seq_printf(m, "\tReads Issued = %lu, Writes Issued = %lu\n",
2135 adapter->nreads[i], adapter->nwrites[i]);
2136 seq_printf(m, "\tSectors Read = %lu, Sectors Written = %lu\n",
2137 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2138 seq_printf(m, "\tRead errors = %lu, Write errors = %lu\n\n",
2139 adapter->rd_errors[i], adapter->wr_errors[i]);
2141 #else
2142 seq_puts(m, "IO and error counters not compiled in driver.\n");
2143 #endif
2144 return 0;
2149 * proc_show_mbox()
2150 * @m - Synthetic file construction data
2151 * @v - File iterator
2153 * Display mailbox information for the last command issued. This information
2154 * is good for debugging.
2156 static int
2157 proc_show_mbox(struct seq_file *m, void *v)
2159 adapter_t *adapter = m->private;
2160 volatile mbox_t *mbox = adapter->mbox;
2162 seq_puts(m, "Contents of Mail Box Structure\n");
2163 seq_printf(m, " Fw Command = 0x%02x\n", mbox->m_out.cmd);
2164 seq_printf(m, " Cmd Sequence = 0x%02x\n", mbox->m_out.cmdid);
2165 seq_printf(m, " No of Sectors= %04d\n", mbox->m_out.numsectors);
2166 seq_printf(m, " LBA = 0x%02x\n", mbox->m_out.lba);
2167 seq_printf(m, " DTA = 0x%08x\n", mbox->m_out.xferaddr);
2168 seq_printf(m, " Logical Drive= 0x%02x\n", mbox->m_out.logdrv);
2169 seq_printf(m, " No of SG Elmt= 0x%02x\n", mbox->m_out.numsgelements);
2170 seq_printf(m, " Busy = %01x\n", mbox->m_in.busy);
2171 seq_printf(m, " Status = 0x%02x\n", mbox->m_in.status);
2172 return 0;
2177 * proc_show_rebuild_rate()
2178 * @m - Synthetic file construction data
2179 * @v - File iterator
2181 * Display current rebuild rate
2183 static int
2184 proc_show_rebuild_rate(struct seq_file *m, void *v)
2186 adapter_t *adapter = m->private;
2187 dma_addr_t dma_handle;
2188 caddr_t inquiry;
2189 struct pci_dev *pdev;
2191 if( make_local_pdev(adapter, &pdev) != 0 )
2192 return 0;
2194 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2195 goto free_pdev;
2197 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2198 seq_puts(m, "Adapter inquiry failed.\n");
2199 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2200 goto free_inquiry;
2203 if( adapter->flag & BOARD_40LD )
2204 seq_printf(m, "Rebuild Rate: [%d%%]\n",
2205 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2206 else
2207 seq_printf(m, "Rebuild Rate: [%d%%]\n",
2208 ((mraid_ext_inquiry *)
2209 inquiry)->raid_inq.adapter_info.rebuild_rate);
2211 free_inquiry:
2212 mega_free_inquiry(inquiry, dma_handle, pdev);
2213 free_pdev:
2214 free_local_pdev(pdev);
2215 return 0;
2220 * proc_show_battery()
2221 * @m - Synthetic file construction data
2222 * @v - File iterator
2224 * Display information about the battery module on the controller.
2226 static int
2227 proc_show_battery(struct seq_file *m, void *v)
2229 adapter_t *adapter = m->private;
2230 dma_addr_t dma_handle;
2231 caddr_t inquiry;
2232 struct pci_dev *pdev;
2233 u8 battery_status;
2235 if( make_local_pdev(adapter, &pdev) != 0 )
2236 return 0;
2238 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2239 goto free_pdev;
2241 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2242 seq_puts(m, "Adapter inquiry failed.\n");
2243 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2244 goto free_inquiry;
2247 if( adapter->flag & BOARD_40LD ) {
2248 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2250 else {
2251 battery_status = ((mraid_ext_inquiry *)inquiry)->
2252 raid_inq.adapter_info.battery_status;
2256 * Decode the battery status
2258 seq_printf(m, "Battery Status:[%d]", battery_status);
2260 if(battery_status == MEGA_BATT_CHARGE_DONE)
2261 seq_puts(m, " Charge Done");
2263 if(battery_status & MEGA_BATT_MODULE_MISSING)
2264 seq_puts(m, " Module Missing");
2266 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2267 seq_puts(m, " Low Voltage");
2269 if(battery_status & MEGA_BATT_TEMP_HIGH)
2270 seq_puts(m, " Temperature High");
2272 if(battery_status & MEGA_BATT_PACK_MISSING)
2273 seq_puts(m, " Pack Missing");
2275 if(battery_status & MEGA_BATT_CHARGE_INPROG)
2276 seq_puts(m, " Charge In-progress");
2278 if(battery_status & MEGA_BATT_CHARGE_FAIL)
2279 seq_puts(m, " Charge Fail");
2281 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2282 seq_puts(m, " Cycles Exceeded");
2284 seq_putc(m, '\n');
2286 free_inquiry:
2287 mega_free_inquiry(inquiry, dma_handle, pdev);
2288 free_pdev:
2289 free_local_pdev(pdev);
2290 return 0;
2295 * Display scsi inquiry
2297 static void
2298 mega_print_inquiry(struct seq_file *m, char *scsi_inq)
2300 int i;
2302 seq_puts(m, " Vendor: ");
2303 seq_write(m, scsi_inq + 8, 8);
2304 seq_puts(m, " Model: ");
2305 seq_write(m, scsi_inq + 16, 16);
2306 seq_puts(m, " Rev: ");
2307 seq_write(m, scsi_inq + 32, 4);
2308 seq_putc(m, '\n');
2310 i = scsi_inq[0] & 0x1f;
2311 seq_printf(m, " Type: %s ", scsi_device_type(i));
2313 seq_printf(m, " ANSI SCSI revision: %02x",
2314 scsi_inq[2] & 0x07);
2316 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2317 seq_puts(m, " CCS\n");
2318 else
2319 seq_putc(m, '\n');
2323 * proc_show_pdrv()
2324 * @m - Synthetic file construction data
2325 * @page - buffer to write the data in
2326 * @adapter - pointer to our soft state
2328 * Display information about the physical drives.
2330 static int
2331 proc_show_pdrv(struct seq_file *m, adapter_t *adapter, int channel)
2333 dma_addr_t dma_handle;
2334 char *scsi_inq;
2335 dma_addr_t scsi_inq_dma_handle;
2336 caddr_t inquiry;
2337 struct pci_dev *pdev;
2338 u8 *pdrv_state;
2339 u8 state;
2340 int tgt;
2341 int max_channels;
2342 int i;
2344 if( make_local_pdev(adapter, &pdev) != 0 )
2345 return 0;
2347 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2348 goto free_pdev;
2350 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2351 seq_puts(m, "Adapter inquiry failed.\n");
2352 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2353 goto free_inquiry;
2357 scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2358 if( scsi_inq == NULL ) {
2359 seq_puts(m, "memory not available for scsi inq.\n");
2360 goto free_inquiry;
2363 if( adapter->flag & BOARD_40LD ) {
2364 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2366 else {
2367 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2368 raid_inq.pdrv_info.pdrv_state;
2371 max_channels = adapter->product_info.nchannels;
2373 if( channel >= max_channels ) {
2374 goto free_pci;
2377 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2379 i = channel*16 + tgt;
2381 state = *(pdrv_state + i);
2382 switch( state & 0x0F ) {
2383 case PDRV_ONLINE:
2384 seq_printf(m, "Channel:%2d Id:%2d State: Online",
2385 channel, tgt);
2386 break;
2388 case PDRV_FAILED:
2389 seq_printf(m, "Channel:%2d Id:%2d State: Failed",
2390 channel, tgt);
2391 break;
2393 case PDRV_RBLD:
2394 seq_printf(m, "Channel:%2d Id:%2d State: Rebuild",
2395 channel, tgt);
2396 break;
2398 case PDRV_HOTSPARE:
2399 seq_printf(m, "Channel:%2d Id:%2d State: Hot spare",
2400 channel, tgt);
2401 break;
2403 default:
2404 seq_printf(m, "Channel:%2d Id:%2d State: Un-configured",
2405 channel, tgt);
2406 break;
2410 * This interface displays inquiries for disk drives
2411 * only. Inquries for logical drives and non-disk
2412 * devices are available through /proc/scsi/scsi
2414 memset(scsi_inq, 0, 256);
2415 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2416 scsi_inq_dma_handle) ||
2417 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2418 continue;
2422 * Check for overflow. We print less than 240
2423 * characters for inquiry
2425 seq_puts(m, ".\n");
2426 mega_print_inquiry(m, scsi_inq);
2429 free_pci:
2430 pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2431 free_inquiry:
2432 mega_free_inquiry(inquiry, dma_handle, pdev);
2433 free_pdev:
2434 free_local_pdev(pdev);
2435 return 0;
2439 * proc_show_pdrv_ch0()
2440 * @m - Synthetic file construction data
2441 * @v - File iterator
2443 * Display information about the physical drives on physical channel 0.
2445 static int
2446 proc_show_pdrv_ch0(struct seq_file *m, void *v)
2448 return proc_show_pdrv(m, m->private, 0);
2453 * proc_show_pdrv_ch1()
2454 * @m - Synthetic file construction data
2455 * @v - File iterator
2457 * Display information about the physical drives on physical channel 1.
2459 static int
2460 proc_show_pdrv_ch1(struct seq_file *m, void *v)
2462 return proc_show_pdrv(m, m->private, 1);
2467 * proc_show_pdrv_ch2()
2468 * @m - Synthetic file construction data
2469 * @v - File iterator
2471 * Display information about the physical drives on physical channel 2.
2473 static int
2474 proc_show_pdrv_ch2(struct seq_file *m, void *v)
2476 return proc_show_pdrv(m, m->private, 2);
2481 * proc_show_pdrv_ch3()
2482 * @m - Synthetic file construction data
2483 * @v - File iterator
2485 * Display information about the physical drives on physical channel 3.
2487 static int
2488 proc_show_pdrv_ch3(struct seq_file *m, void *v)
2490 return proc_show_pdrv(m, m->private, 3);
2495 * proc_show_rdrv()
2496 * @m - Synthetic file construction data
2497 * @adapter - pointer to our soft state
2498 * @start - starting logical drive to display
2499 * @end - ending logical drive to display
2501 * We do not print the inquiry information since its already available through
2502 * /proc/scsi/scsi interface
2504 static int
2505 proc_show_rdrv(struct seq_file *m, adapter_t *adapter, int start, int end )
2507 dma_addr_t dma_handle;
2508 logdrv_param *lparam;
2509 megacmd_t mc;
2510 char *disk_array;
2511 dma_addr_t disk_array_dma_handle;
2512 caddr_t inquiry;
2513 struct pci_dev *pdev;
2514 u8 *rdrv_state;
2515 int num_ldrv;
2516 u32 array_sz;
2517 int i;
2519 if( make_local_pdev(adapter, &pdev) != 0 )
2520 return 0;
2522 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2523 goto free_pdev;
2525 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2526 seq_puts(m, "Adapter inquiry failed.\n");
2527 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2528 goto free_inquiry;
2531 memset(&mc, 0, sizeof(megacmd_t));
2533 if( adapter->flag & BOARD_40LD ) {
2534 array_sz = sizeof(disk_array_40ld);
2536 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2538 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2540 else {
2541 array_sz = sizeof(disk_array_8ld);
2543 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2544 raid_inq.logdrv_info.ldrv_state;
2546 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2547 raid_inq.logdrv_info.num_ldrv;
2550 disk_array = pci_alloc_consistent(pdev, array_sz,
2551 &disk_array_dma_handle);
2553 if( disk_array == NULL ) {
2554 seq_puts(m, "memory not available.\n");
2555 goto free_inquiry;
2558 mc.xferaddr = (u32)disk_array_dma_handle;
2560 if( adapter->flag & BOARD_40LD ) {
2561 mc.cmd = FC_NEW_CONFIG;
2562 mc.opcode = OP_DCMD_READ_CONFIG;
2564 if( mega_internal_command(adapter, &mc, NULL) ) {
2565 seq_puts(m, "40LD read config failed.\n");
2566 goto free_pci;
2570 else {
2571 mc.cmd = NEW_READ_CONFIG_8LD;
2573 if( mega_internal_command(adapter, &mc, NULL) ) {
2574 mc.cmd = READ_CONFIG_8LD;
2575 if( mega_internal_command(adapter, &mc, NULL) ) {
2576 seq_puts(m, "8LD read config failed.\n");
2577 goto free_pci;
2582 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2584 if( adapter->flag & BOARD_40LD ) {
2585 lparam =
2586 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
2588 else {
2589 lparam =
2590 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
2594 * Check for overflow. We print less than 240 characters for
2595 * information about each logical drive.
2597 seq_printf(m, "Logical drive:%2d:, ", i);
2599 switch( rdrv_state[i] & 0x0F ) {
2600 case RDRV_OFFLINE:
2601 seq_puts(m, "state: offline");
2602 break;
2603 case RDRV_DEGRADED:
2604 seq_puts(m, "state: degraded");
2605 break;
2606 case RDRV_OPTIMAL:
2607 seq_puts(m, "state: optimal");
2608 break;
2609 case RDRV_DELETED:
2610 seq_puts(m, "state: deleted");
2611 break;
2612 default:
2613 seq_puts(m, "state: unknown");
2614 break;
2618 * Check if check consistency or initialization is going on
2619 * for this logical drive.
2621 if( (rdrv_state[i] & 0xF0) == 0x20 )
2622 seq_puts(m, ", check-consistency in progress");
2623 else if( (rdrv_state[i] & 0xF0) == 0x10 )
2624 seq_puts(m, ", initialization in progress");
2626 seq_putc(m, '\n');
2628 seq_printf(m, "Span depth:%3d, ", lparam->span_depth);
2629 seq_printf(m, "RAID level:%3d, ", lparam->level);
2630 seq_printf(m, "Stripe size:%3d, ",
2631 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
2632 seq_printf(m, "Row size:%3d\n", lparam->row_size);
2634 seq_puts(m, "Read Policy: ");
2635 switch(lparam->read_ahead) {
2636 case NO_READ_AHEAD:
2637 seq_puts(m, "No read ahead, ");
2638 break;
2639 case READ_AHEAD:
2640 seq_puts(m, "Read ahead, ");
2641 break;
2642 case ADAP_READ_AHEAD:
2643 seq_puts(m, "Adaptive, ");
2644 break;
2648 seq_puts(m, "Write Policy: ");
2649 switch(lparam->write_mode) {
2650 case WRMODE_WRITE_THRU:
2651 seq_puts(m, "Write thru, ");
2652 break;
2653 case WRMODE_WRITE_BACK:
2654 seq_puts(m, "Write back, ");
2655 break;
2658 seq_puts(m, "Cache Policy: ");
2659 switch(lparam->direct_io) {
2660 case CACHED_IO:
2661 seq_puts(m, "Cached IO\n\n");
2662 break;
2663 case DIRECT_IO:
2664 seq_puts(m, "Direct IO\n\n");
2665 break;
2669 free_pci:
2670 pci_free_consistent(pdev, array_sz, disk_array,
2671 disk_array_dma_handle);
2672 free_inquiry:
2673 mega_free_inquiry(inquiry, dma_handle, pdev);
2674 free_pdev:
2675 free_local_pdev(pdev);
2676 return 0;
2680 * proc_show_rdrv_10()
2681 * @m - Synthetic file construction data
2682 * @v - File iterator
2684 * Display real time information about the logical drives 0 through 9.
2686 static int
2687 proc_show_rdrv_10(struct seq_file *m, void *v)
2689 return proc_show_rdrv(m, m->private, 0, 9);
2694 * proc_show_rdrv_20()
2695 * @m - Synthetic file construction data
2696 * @v - File iterator
2698 * Display real time information about the logical drives 0 through 9.
2700 static int
2701 proc_show_rdrv_20(struct seq_file *m, void *v)
2703 return proc_show_rdrv(m, m->private, 10, 19);
2708 * proc_show_rdrv_30()
2709 * @m - Synthetic file construction data
2710 * @v - File iterator
2712 * Display real time information about the logical drives 0 through 9.
2714 static int
2715 proc_show_rdrv_30(struct seq_file *m, void *v)
2717 return proc_show_rdrv(m, m->private, 20, 29);
2722 * proc_show_rdrv_40()
2723 * @m - Synthetic file construction data
2724 * @v - File iterator
2726 * Display real time information about the logical drives 0 through 9.
2728 static int
2729 proc_show_rdrv_40(struct seq_file *m, void *v)
2731 return proc_show_rdrv(m, m->private, 30, 39);
2736 * seq_file wrappers for procfile show routines.
2738 static int mega_proc_open(struct inode *inode, struct file *file)
2740 adapter_t *adapter = proc_get_parent_data(inode);
2741 int (*show)(struct seq_file *, void *) = PDE_DATA(inode);
2743 return single_open(file, show, adapter);
2746 static const struct file_operations mega_proc_fops = {
2747 .open = mega_proc_open,
2748 .read = seq_read,
2749 .llseek = seq_lseek,
2750 .release = single_release,
2754 * Table of proc files we need to create.
2756 struct mega_proc_file {
2757 const char *name;
2758 unsigned short ptr_offset;
2759 int (*show) (struct seq_file *m, void *v);
2762 static const struct mega_proc_file mega_proc_files[] = {
2763 { "config", offsetof(adapter_t, proc_read), proc_show_config },
2764 { "stat", offsetof(adapter_t, proc_stat), proc_show_stat },
2765 { "mailbox", offsetof(adapter_t, proc_mbox), proc_show_mbox },
2766 #if MEGA_HAVE_ENH_PROC
2767 { "rebuild-rate", offsetof(adapter_t, proc_rr), proc_show_rebuild_rate },
2768 { "battery-status", offsetof(adapter_t, proc_battery), proc_show_battery },
2769 { "diskdrives-ch0", offsetof(adapter_t, proc_pdrvstat[0]), proc_show_pdrv_ch0 },
2770 { "diskdrives-ch1", offsetof(adapter_t, proc_pdrvstat[1]), proc_show_pdrv_ch1 },
2771 { "diskdrives-ch2", offsetof(adapter_t, proc_pdrvstat[2]), proc_show_pdrv_ch2 },
2772 { "diskdrives-ch3", offsetof(adapter_t, proc_pdrvstat[3]), proc_show_pdrv_ch3 },
2773 { "raiddrives-0-9", offsetof(adapter_t, proc_rdrvstat[0]), proc_show_rdrv_10 },
2774 { "raiddrives-10-19", offsetof(adapter_t, proc_rdrvstat[1]), proc_show_rdrv_20 },
2775 { "raiddrives-20-29", offsetof(adapter_t, proc_rdrvstat[2]), proc_show_rdrv_30 },
2776 { "raiddrives-30-39", offsetof(adapter_t, proc_rdrvstat[3]), proc_show_rdrv_40 },
2777 #endif
2778 { NULL }
2782 * mega_create_proc_entry()
2783 * @index - index in soft state array
2784 * @parent - parent node for this /proc entry
2786 * Creates /proc entries for our controllers.
2788 static void
2789 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2791 const struct mega_proc_file *f;
2792 adapter_t *adapter = hba_soft_state[index];
2793 struct proc_dir_entry *dir, *de, **ppde;
2794 u8 string[16];
2796 sprintf(string, "hba%d", adapter->host->host_no);
2798 dir = adapter->controller_proc_dir_entry =
2799 proc_mkdir_data(string, 0, parent, adapter);
2800 if(!dir) {
2801 dev_warn(&adapter->dev->dev, "proc_mkdir failed\n");
2802 return;
2805 for (f = mega_proc_files; f->name; f++) {
2806 de = proc_create_data(f->name, S_IRUSR, dir, &mega_proc_fops,
2807 f->show);
2808 if (!de) {
2809 dev_warn(&adapter->dev->dev, "proc_create failed\n");
2810 return;
2813 ppde = (void *)adapter + f->ptr_offset;
2814 *ppde = de;
2818 #else
2819 static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2822 #endif
2826 * megaraid_biosparam()
2828 * Return the disk geometry for a particular disk
2830 static int
2831 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
2832 sector_t capacity, int geom[])
2834 adapter_t *adapter;
2835 unsigned char *bh;
2836 int heads;
2837 int sectors;
2838 int cylinders;
2839 int rval;
2841 /* Get pointer to host config structure */
2842 adapter = (adapter_t *)sdev->host->hostdata;
2844 if (IS_RAID_CH(adapter, sdev->channel)) {
2845 /* Default heads (64) & sectors (32) */
2846 heads = 64;
2847 sectors = 32;
2848 cylinders = (ulong)capacity / (heads * sectors);
2851 * Handle extended translation size for logical drives
2852 * > 1Gb
2854 if ((ulong)capacity >= 0x200000) {
2855 heads = 255;
2856 sectors = 63;
2857 cylinders = (ulong)capacity / (heads * sectors);
2860 /* return result */
2861 geom[0] = heads;
2862 geom[1] = sectors;
2863 geom[2] = cylinders;
2865 else {
2866 bh = scsi_bios_ptable(bdev);
2868 if( bh ) {
2869 rval = scsi_partsize(bh, capacity,
2870 &geom[2], &geom[0], &geom[1]);
2871 kfree(bh);
2872 if( rval != -1 )
2873 return rval;
2876 dev_info(&adapter->dev->dev,
2877 "invalid partition on this disk on channel %d\n",
2878 sdev->channel);
2880 /* Default heads (64) & sectors (32) */
2881 heads = 64;
2882 sectors = 32;
2883 cylinders = (ulong)capacity / (heads * sectors);
2885 /* Handle extended translation size for logical drives > 1Gb */
2886 if ((ulong)capacity >= 0x200000) {
2887 heads = 255;
2888 sectors = 63;
2889 cylinders = (ulong)capacity / (heads * sectors);
2892 /* return result */
2893 geom[0] = heads;
2894 geom[1] = sectors;
2895 geom[2] = cylinders;
2898 return 0;
2902 * mega_init_scb()
2903 * @adapter - pointer to our soft state
2905 * Allocate memory for the various pointers in the scb structures:
2906 * scatter-gather list pointer, passthru and extended passthru structure
2907 * pointers.
2909 static int
2910 mega_init_scb(adapter_t *adapter)
2912 scb_t *scb;
2913 int i;
2915 for( i = 0; i < adapter->max_cmds; i++ ) {
2917 scb = &adapter->scb_list[i];
2919 scb->sgl64 = NULL;
2920 scb->sgl = NULL;
2921 scb->pthru = NULL;
2922 scb->epthru = NULL;
2925 for( i = 0; i < adapter->max_cmds; i++ ) {
2927 scb = &adapter->scb_list[i];
2929 scb->idx = i;
2931 scb->sgl64 = pci_alloc_consistent(adapter->dev,
2932 sizeof(mega_sgl64) * adapter->sglen,
2933 &scb->sgl_dma_addr);
2935 scb->sgl = (mega_sglist *)scb->sgl64;
2937 if( !scb->sgl ) {
2938 dev_warn(&adapter->dev->dev, "RAID: Can't allocate sglist\n");
2939 mega_free_sgl(adapter);
2940 return -1;
2943 scb->pthru = pci_alloc_consistent(adapter->dev,
2944 sizeof(mega_passthru),
2945 &scb->pthru_dma_addr);
2947 if( !scb->pthru ) {
2948 dev_warn(&adapter->dev->dev, "RAID: Can't allocate passthru\n");
2949 mega_free_sgl(adapter);
2950 return -1;
2953 scb->epthru = pci_alloc_consistent(adapter->dev,
2954 sizeof(mega_ext_passthru),
2955 &scb->epthru_dma_addr);
2957 if( !scb->epthru ) {
2958 dev_warn(&adapter->dev->dev,
2959 "Can't allocate extended passthru\n");
2960 mega_free_sgl(adapter);
2961 return -1;
2965 scb->dma_type = MEGA_DMA_TYPE_NONE;
2968 * Link to free list
2969 * lock not required since we are loading the driver, so no
2970 * commands possible right now.
2972 scb->state = SCB_FREE;
2973 scb->cmd = NULL;
2974 list_add(&scb->list, &adapter->free_list);
2977 return 0;
2982 * megadev_open()
2983 * @inode - unused
2984 * @filep - unused
2986 * Routines for the character/ioctl interface to the driver. Find out if this
2987 * is a valid open.
2989 static int
2990 megadev_open (struct inode *inode, struct file *filep)
2993 * Only allow superuser to access private ioctl interface
2995 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
2997 return 0;
3002 * megadev_ioctl()
3003 * @inode - Our device inode
3004 * @filep - unused
3005 * @cmd - ioctl command
3006 * @arg - user buffer
3008 * ioctl entry point for our private ioctl interface. We move the data in from
3009 * the user space, prepare the command (if necessary, convert the old MIMD
3010 * ioctl to new ioctl command), and issue a synchronous command to the
3011 * controller.
3013 static int
3014 megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3016 adapter_t *adapter;
3017 nitioctl_t uioc;
3018 int adapno;
3019 int rval;
3020 mega_passthru __user *upthru; /* user address for passthru */
3021 mega_passthru *pthru; /* copy user passthru here */
3022 dma_addr_t pthru_dma_hndl;
3023 void *data = NULL; /* data to be transferred */
3024 dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
3025 megacmd_t mc;
3026 megastat_t __user *ustats;
3027 int num_ldrv;
3028 u32 uxferaddr = 0;
3029 struct pci_dev *pdev;
3031 ustats = NULL; /* avoid compilation warnings */
3032 num_ldrv = 0;
3035 * Make sure only USCSICMD are issued through this interface.
3036 * MIMD application would still fire different command.
3038 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3039 return -EINVAL;
3043 * Check and convert a possible MIMD command to NIT command.
3044 * mega_m_to_n() copies the data from the user space, so we do not
3045 * have to do it here.
3046 * NOTE: We will need some user address to copyout the data, therefore
3047 * the inteface layer will also provide us with the required user
3048 * addresses.
3050 memset(&uioc, 0, sizeof(nitioctl_t));
3051 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3052 return rval;
3055 switch( uioc.opcode ) {
3057 case GET_DRIVER_VER:
3058 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3059 return (-EFAULT);
3061 break;
3063 case GET_N_ADAP:
3064 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3065 return (-EFAULT);
3068 * Shucks. MIMD interface returns a positive value for number
3069 * of adapters. TODO: Change it to return 0 when there is no
3070 * applicatio using mimd interface.
3072 return hba_count;
3074 case GET_ADAP_INFO:
3077 * Which adapter
3079 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3080 return (-ENODEV);
3082 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3083 sizeof(struct mcontroller)) )
3084 return (-EFAULT);
3085 break;
3087 #if MEGA_HAVE_STATS
3089 case GET_STATS:
3091 * Which adapter
3093 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3094 return (-ENODEV);
3096 adapter = hba_soft_state[adapno];
3098 ustats = uioc.uioc_uaddr;
3100 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3101 return (-EFAULT);
3104 * Check for the validity of the logical drive number
3106 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3108 if( copy_to_user(ustats->nreads, adapter->nreads,
3109 num_ldrv*sizeof(u32)) )
3110 return -EFAULT;
3112 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3113 num_ldrv*sizeof(u32)) )
3114 return -EFAULT;
3116 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3117 num_ldrv*sizeof(u32)) )
3118 return -EFAULT;
3120 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3121 num_ldrv*sizeof(u32)) )
3122 return -EFAULT;
3124 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3125 num_ldrv*sizeof(u32)) )
3126 return -EFAULT;
3128 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3129 num_ldrv*sizeof(u32)) )
3130 return -EFAULT;
3132 return 0;
3134 #endif
3135 case MBOX_CMD:
3138 * Which adapter
3140 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3141 return (-ENODEV);
3143 adapter = hba_soft_state[adapno];
3146 * Deletion of logical drive is a special case. The adapter
3147 * should be quiescent before this command is issued.
3149 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3150 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3153 * Do we support this feature
3155 if( !adapter->support_random_del ) {
3156 dev_warn(&adapter->dev->dev, "logdrv "
3157 "delete on non-supporting F/W\n");
3159 return (-EINVAL);
3162 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3164 if( rval == 0 ) {
3165 memset(&mc, 0, sizeof(megacmd_t));
3167 mc.status = rval;
3169 rval = mega_n_to_m((void __user *)arg, &mc);
3172 return rval;
3175 * This interface only support the regular passthru commands.
3176 * Reject extended passthru and 64-bit passthru
3178 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3179 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3181 dev_warn(&adapter->dev->dev, "rejected passthru\n");
3183 return (-EINVAL);
3187 * For all internal commands, the buffer must be allocated in
3188 * <4GB address range
3190 if( make_local_pdev(adapter, &pdev) != 0 )
3191 return -EIO;
3193 /* Is it a passthru command or a DCMD */
3194 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3195 /* Passthru commands */
3197 pthru = pci_alloc_consistent(pdev,
3198 sizeof(mega_passthru),
3199 &pthru_dma_hndl);
3201 if( pthru == NULL ) {
3202 free_local_pdev(pdev);
3203 return (-ENOMEM);
3207 * The user passthru structure
3209 upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
3212 * Copy in the user passthru here.
3214 if( copy_from_user(pthru, upthru,
3215 sizeof(mega_passthru)) ) {
3217 pci_free_consistent(pdev,
3218 sizeof(mega_passthru), pthru,
3219 pthru_dma_hndl);
3221 free_local_pdev(pdev);
3223 return (-EFAULT);
3227 * Is there a data transfer
3229 if( pthru->dataxferlen ) {
3230 data = pci_alloc_consistent(pdev,
3231 pthru->dataxferlen,
3232 &data_dma_hndl);
3234 if( data == NULL ) {
3235 pci_free_consistent(pdev,
3236 sizeof(mega_passthru),
3237 pthru,
3238 pthru_dma_hndl);
3240 free_local_pdev(pdev);
3242 return (-ENOMEM);
3246 * Save the user address and point the kernel
3247 * address at just allocated memory
3249 uxferaddr = pthru->dataxferaddr;
3250 pthru->dataxferaddr = data_dma_hndl;
3255 * Is data coming down-stream
3257 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3259 * Get the user data
3261 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3262 pthru->dataxferlen) ) {
3263 rval = (-EFAULT);
3264 goto freemem_and_return;
3268 memset(&mc, 0, sizeof(megacmd_t));
3270 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3271 mc.xferaddr = (u32)pthru_dma_hndl;
3274 * Issue the command
3276 mega_internal_command(adapter, &mc, pthru);
3278 rval = mega_n_to_m((void __user *)arg, &mc);
3280 if( rval ) goto freemem_and_return;
3284 * Is data going up-stream
3286 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3287 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3288 pthru->dataxferlen) ) {
3289 rval = (-EFAULT);
3294 * Send the request sense data also, irrespective of
3295 * whether the user has asked for it or not.
3297 if (copy_to_user(upthru->reqsensearea,
3298 pthru->reqsensearea, 14))
3299 rval = -EFAULT;
3301 freemem_and_return:
3302 if( pthru->dataxferlen ) {
3303 pci_free_consistent(pdev,
3304 pthru->dataxferlen, data,
3305 data_dma_hndl);
3308 pci_free_consistent(pdev, sizeof(mega_passthru),
3309 pthru, pthru_dma_hndl);
3311 free_local_pdev(pdev);
3313 return rval;
3315 else {
3316 /* DCMD commands */
3319 * Is there a data transfer
3321 if( uioc.xferlen ) {
3322 data = pci_alloc_consistent(pdev,
3323 uioc.xferlen, &data_dma_hndl);
3325 if( data == NULL ) {
3326 free_local_pdev(pdev);
3327 return (-ENOMEM);
3330 uxferaddr = MBOX(uioc)->xferaddr;
3334 * Is data coming down-stream
3336 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3338 * Get the user data
3340 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3341 uioc.xferlen) ) {
3343 pci_free_consistent(pdev,
3344 uioc.xferlen,
3345 data, data_dma_hndl);
3347 free_local_pdev(pdev);
3349 return (-EFAULT);
3353 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3355 mc.xferaddr = (u32)data_dma_hndl;
3358 * Issue the command
3360 mega_internal_command(adapter, &mc, NULL);
3362 rval = mega_n_to_m((void __user *)arg, &mc);
3364 if( rval ) {
3365 if( uioc.xferlen ) {
3366 pci_free_consistent(pdev,
3367 uioc.xferlen, data,
3368 data_dma_hndl);
3371 free_local_pdev(pdev);
3373 return rval;
3377 * Is data going up-stream
3379 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3380 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3381 uioc.xferlen) ) {
3383 rval = (-EFAULT);
3387 if( uioc.xferlen ) {
3388 pci_free_consistent(pdev,
3389 uioc.xferlen, data,
3390 data_dma_hndl);
3393 free_local_pdev(pdev);
3395 return rval;
3398 default:
3399 return (-EINVAL);
3402 return 0;
3405 static long
3406 megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3408 int ret;
3410 mutex_lock(&megadev_mutex);
3411 ret = megadev_ioctl(filep, cmd, arg);
3412 mutex_unlock(&megadev_mutex);
3414 return ret;
3418 * mega_m_to_n()
3419 * @arg - user address
3420 * @uioc - new ioctl structure
3422 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3423 * structure
3425 * Converts the older mimd ioctl structure to newer NIT structure
3427 static int
3428 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3430 struct uioctl_t uioc_mimd;
3431 char signature[8] = {0};
3432 u8 opcode;
3433 u8 subopcode;
3437 * check is the application conforms to NIT. We do not have to do much
3438 * in that case.
3439 * We exploit the fact that the signature is stored in the very
3440 * beginning of the structure.
3443 if( copy_from_user(signature, arg, 7) )
3444 return (-EFAULT);
3446 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3449 * NOTE NOTE: The nit ioctl is still under flux because of
3450 * change of mailbox definition, in HPE. No applications yet
3451 * use this interface and let's not have applications use this
3452 * interface till the new specifitions are in place.
3454 return -EINVAL;
3455 #if 0
3456 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3457 return (-EFAULT);
3458 return 0;
3459 #endif
3463 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3465 * Get the user ioctl structure
3467 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3468 return (-EFAULT);
3472 * Get the opcode and subopcode for the commands
3474 opcode = uioc_mimd.ui.fcs.opcode;
3475 subopcode = uioc_mimd.ui.fcs.subopcode;
3477 switch (opcode) {
3478 case 0x82:
3480 switch (subopcode) {
3482 case MEGAIOC_QDRVRVER: /* Query driver version */
3483 uioc->opcode = GET_DRIVER_VER;
3484 uioc->uioc_uaddr = uioc_mimd.data;
3485 break;
3487 case MEGAIOC_QNADAP: /* Get # of adapters */
3488 uioc->opcode = GET_N_ADAP;
3489 uioc->uioc_uaddr = uioc_mimd.data;
3490 break;
3492 case MEGAIOC_QADAPINFO: /* Get adapter information */
3493 uioc->opcode = GET_ADAP_INFO;
3494 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3495 uioc->uioc_uaddr = uioc_mimd.data;
3496 break;
3498 default:
3499 return(-EINVAL);
3502 break;
3505 case 0x81:
3507 uioc->opcode = MBOX_CMD;
3508 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3510 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3512 uioc->xferlen = uioc_mimd.ui.fcs.length;
3514 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3515 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3517 break;
3519 case 0x80:
3521 uioc->opcode = MBOX_CMD;
3522 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3524 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3527 * Choose the xferlen bigger of input and output data
3529 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3530 uioc_mimd.outlen : uioc_mimd.inlen;
3532 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3533 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3535 break;
3537 default:
3538 return (-EINVAL);
3542 return 0;
3546 * mega_n_to_m()
3547 * @arg - user address
3548 * @mc - mailbox command
3550 * Updates the status information to the application, depending on application
3551 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3553 static int
3554 mega_n_to_m(void __user *arg, megacmd_t *mc)
3556 nitioctl_t __user *uiocp;
3557 megacmd_t __user *umc;
3558 mega_passthru __user *upthru;
3559 struct uioctl_t __user *uioc_mimd;
3560 char signature[8] = {0};
3563 * check is the application conforms to NIT.
3565 if( copy_from_user(signature, arg, 7) )
3566 return -EFAULT;
3568 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3570 uiocp = arg;
3572 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3573 return (-EFAULT);
3575 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3577 umc = MBOX_P(uiocp);
3579 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3580 return -EFAULT;
3582 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3583 return (-EFAULT);
3586 else {
3587 uioc_mimd = arg;
3589 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3590 return (-EFAULT);
3592 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3594 umc = (megacmd_t __user *)uioc_mimd->mbox;
3596 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3597 return (-EFAULT);
3599 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3600 return (-EFAULT);
3604 return 0;
3609 * MEGARAID 'FW' commands.
3613 * mega_is_bios_enabled()
3614 * @adapter - pointer to our soft state
3616 * issue command to find out if the BIOS is enabled for this controller
3618 static int
3619 mega_is_bios_enabled(adapter_t *adapter)
3621 unsigned char raw_mbox[sizeof(struct mbox_out)];
3622 mbox_t *mbox;
3623 int ret;
3625 mbox = (mbox_t *)raw_mbox;
3627 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3629 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3631 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3633 raw_mbox[0] = IS_BIOS_ENABLED;
3634 raw_mbox[2] = GET_BIOS;
3637 ret = issue_scb_block(adapter, raw_mbox);
3639 return *(char *)adapter->mega_buffer;
3644 * mega_enum_raid_scsi()
3645 * @adapter - pointer to our soft state
3647 * Find out what channels are RAID/SCSI. This information is used to
3648 * differentiate the virtual channels and physical channels and to support
3649 * ROMB feature and non-disk devices.
3651 static void
3652 mega_enum_raid_scsi(adapter_t *adapter)
3654 unsigned char raw_mbox[sizeof(struct mbox_out)];
3655 mbox_t *mbox;
3656 int i;
3658 mbox = (mbox_t *)raw_mbox;
3660 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3663 * issue command to find out what channels are raid/scsi
3665 raw_mbox[0] = CHNL_CLASS;
3666 raw_mbox[2] = GET_CHNL_CLASS;
3668 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3670 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3673 * Non-ROMB firmware fail this command, so all channels
3674 * must be shown RAID
3676 adapter->mega_ch_class = 0xFF;
3678 if(!issue_scb_block(adapter, raw_mbox)) {
3679 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3683 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
3684 if( (adapter->mega_ch_class >> i) & 0x01 ) {
3685 dev_info(&adapter->dev->dev, "channel[%d] is raid\n",
3688 else {
3689 dev_info(&adapter->dev->dev, "channel[%d] is scsi\n",
3694 return;
3699 * mega_get_boot_drv()
3700 * @adapter - pointer to our soft state
3702 * Find out which device is the boot device. Note, any logical drive or any
3703 * phyical device (e.g., a CDROM) can be designated as a boot device.
3705 static void
3706 mega_get_boot_drv(adapter_t *adapter)
3708 struct private_bios_data *prv_bios_data;
3709 unsigned char raw_mbox[sizeof(struct mbox_out)];
3710 mbox_t *mbox;
3711 u16 cksum = 0;
3712 u8 *cksum_p;
3713 u8 boot_pdrv;
3714 int i;
3716 mbox = (mbox_t *)raw_mbox;
3718 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3720 raw_mbox[0] = BIOS_PVT_DATA;
3721 raw_mbox[2] = GET_BIOS_PVT_DATA;
3723 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3725 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3727 adapter->boot_ldrv_enabled = 0;
3728 adapter->boot_ldrv = 0;
3730 adapter->boot_pdrv_enabled = 0;
3731 adapter->boot_pdrv_ch = 0;
3732 adapter->boot_pdrv_tgt = 0;
3734 if(issue_scb_block(adapter, raw_mbox) == 0) {
3735 prv_bios_data =
3736 (struct private_bios_data *)adapter->mega_buffer;
3738 cksum = 0;
3739 cksum_p = (char *)prv_bios_data;
3740 for (i = 0; i < 14; i++ ) {
3741 cksum += (u16)(*cksum_p++);
3744 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
3747 * If MSB is set, a physical drive is set as boot
3748 * device
3750 if( prv_bios_data->boot_drv & 0x80 ) {
3751 adapter->boot_pdrv_enabled = 1;
3752 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
3753 adapter->boot_pdrv_ch = boot_pdrv / 16;
3754 adapter->boot_pdrv_tgt = boot_pdrv % 16;
3756 else {
3757 adapter->boot_ldrv_enabled = 1;
3758 adapter->boot_ldrv = prv_bios_data->boot_drv;
3766 * mega_support_random_del()
3767 * @adapter - pointer to our soft state
3769 * Find out if this controller supports random deletion and addition of
3770 * logical drives
3772 static int
3773 mega_support_random_del(adapter_t *adapter)
3775 unsigned char raw_mbox[sizeof(struct mbox_out)];
3776 mbox_t *mbox;
3777 int rval;
3779 mbox = (mbox_t *)raw_mbox;
3781 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3784 * issue command
3786 raw_mbox[0] = FC_DEL_LOGDRV;
3787 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3789 rval = issue_scb_block(adapter, raw_mbox);
3791 return !rval;
3796 * mega_support_ext_cdb()
3797 * @adapter - pointer to our soft state
3799 * Find out if this firmware support cdblen > 10
3801 static int
3802 mega_support_ext_cdb(adapter_t *adapter)
3804 unsigned char raw_mbox[sizeof(struct mbox_out)];
3805 mbox_t *mbox;
3806 int rval;
3808 mbox = (mbox_t *)raw_mbox;
3810 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3812 * issue command to find out if controller supports extended CDBs.
3814 raw_mbox[0] = 0xA4;
3815 raw_mbox[2] = 0x16;
3817 rval = issue_scb_block(adapter, raw_mbox);
3819 return !rval;
3824 * mega_del_logdrv()
3825 * @adapter - pointer to our soft state
3826 * @logdrv - logical drive to be deleted
3828 * Delete the specified logical drive. It is the responsibility of the user
3829 * app to let the OS know about this operation.
3831 static int
3832 mega_del_logdrv(adapter_t *adapter, int logdrv)
3834 unsigned long flags;
3835 scb_t *scb;
3836 int rval;
3839 * Stop sending commands to the controller, queue them internally.
3840 * When deletion is complete, ISR will flush the queue.
3842 atomic_set(&adapter->quiescent, 1);
3845 * Wait till all the issued commands are complete and there are no
3846 * commands in the pending queue
3848 while (atomic_read(&adapter->pend_cmds) > 0 ||
3849 !list_empty(&adapter->pending_list))
3850 msleep(1000); /* sleep for 1s */
3852 rval = mega_do_del_logdrv(adapter, logdrv);
3854 spin_lock_irqsave(&adapter->lock, flags);
3857 * If delete operation was successful, add 0x80 to the logical drive
3858 * ids for commands in the pending queue.
3860 if (adapter->read_ldidmap) {
3861 struct list_head *pos;
3862 list_for_each(pos, &adapter->pending_list) {
3863 scb = list_entry(pos, scb_t, list);
3864 if (scb->pthru->logdrv < 0x80 )
3865 scb->pthru->logdrv += 0x80;
3869 atomic_set(&adapter->quiescent, 0);
3871 mega_runpendq(adapter);
3873 spin_unlock_irqrestore(&adapter->lock, flags);
3875 return rval;
3879 static int
3880 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
3882 megacmd_t mc;
3883 int rval;
3885 memset( &mc, 0, sizeof(megacmd_t));
3887 mc.cmd = FC_DEL_LOGDRV;
3888 mc.opcode = OP_DEL_LOGDRV;
3889 mc.subopcode = logdrv;
3891 rval = mega_internal_command(adapter, &mc, NULL);
3893 /* log this event */
3894 if(rval) {
3895 dev_warn(&adapter->dev->dev, "Delete LD-%d failed", logdrv);
3896 return rval;
3900 * After deleting first logical drive, the logical drives must be
3901 * addressed by adding 0x80 to the logical drive id.
3903 adapter->read_ldidmap = 1;
3905 return rval;
3910 * mega_get_max_sgl()
3911 * @adapter - pointer to our soft state
3913 * Find out the maximum number of scatter-gather elements supported by this
3914 * version of the firmware
3916 static void
3917 mega_get_max_sgl(adapter_t *adapter)
3919 unsigned char raw_mbox[sizeof(struct mbox_out)];
3920 mbox_t *mbox;
3922 mbox = (mbox_t *)raw_mbox;
3924 memset(mbox, 0, sizeof(raw_mbox));
3926 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3928 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3930 raw_mbox[0] = MAIN_MISC_OPCODE;
3931 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3934 if( issue_scb_block(adapter, raw_mbox) ) {
3936 * f/w does not support this command. Choose the default value
3938 adapter->sglen = MIN_SGLIST;
3940 else {
3941 adapter->sglen = *((char *)adapter->mega_buffer);
3944 * Make sure this is not more than the resources we are
3945 * planning to allocate
3947 if ( adapter->sglen > MAX_SGLIST )
3948 adapter->sglen = MAX_SGLIST;
3951 return;
3956 * mega_support_cluster()
3957 * @adapter - pointer to our soft state
3959 * Find out if this firmware support cluster calls.
3961 static int
3962 mega_support_cluster(adapter_t *adapter)
3964 unsigned char raw_mbox[sizeof(struct mbox_out)];
3965 mbox_t *mbox;
3967 mbox = (mbox_t *)raw_mbox;
3969 memset(mbox, 0, sizeof(raw_mbox));
3971 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3973 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3976 * Try to get the initiator id. This command will succeed iff the
3977 * clustering is available on this HBA.
3979 raw_mbox[0] = MEGA_GET_TARGET_ID;
3981 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
3984 * Cluster support available. Get the initiator target id.
3985 * Tell our id to mid-layer too.
3987 adapter->this_id = *(u32 *)adapter->mega_buffer;
3988 adapter->host->this_id = adapter->this_id;
3990 return 1;
3993 return 0;
3996 #ifdef CONFIG_PROC_FS
3998 * mega_adapinq()
3999 * @adapter - pointer to our soft state
4000 * @dma_handle - DMA address of the buffer
4002 * Issue internal commands while interrupts are available.
4003 * We only issue direct mailbox commands from within the driver. ioctl()
4004 * interface using these routines can issue passthru commands.
4006 static int
4007 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
4009 megacmd_t mc;
4011 memset(&mc, 0, sizeof(megacmd_t));
4013 if( adapter->flag & BOARD_40LD ) {
4014 mc.cmd = FC_NEW_CONFIG;
4015 mc.opcode = NC_SUBOP_ENQUIRY3;
4016 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
4018 else {
4019 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
4022 mc.xferaddr = (u32)dma_handle;
4024 if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
4025 return -1;
4028 return 0;
4032 /** mega_internal_dev_inquiry()
4033 * @adapter - pointer to our soft state
4034 * @ch - channel for this device
4035 * @tgt - ID of this device
4036 * @buf_dma_handle - DMA address of the buffer
4038 * Issue the scsi inquiry for the specified device.
4040 static int
4041 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4042 dma_addr_t buf_dma_handle)
4044 mega_passthru *pthru;
4045 dma_addr_t pthru_dma_handle;
4046 megacmd_t mc;
4047 int rval;
4048 struct pci_dev *pdev;
4052 * For all internal commands, the buffer must be allocated in <4GB
4053 * address range
4055 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4057 pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4058 &pthru_dma_handle);
4060 if( pthru == NULL ) {
4061 free_local_pdev(pdev);
4062 return -1;
4065 pthru->timeout = 2;
4066 pthru->ars = 1;
4067 pthru->reqsenselen = 14;
4068 pthru->islogical = 0;
4070 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4072 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4074 pthru->cdblen = 6;
4076 pthru->cdb[0] = INQUIRY;
4077 pthru->cdb[1] = 0;
4078 pthru->cdb[2] = 0;
4079 pthru->cdb[3] = 0;
4080 pthru->cdb[4] = 255;
4081 pthru->cdb[5] = 0;
4084 pthru->dataxferaddr = (u32)buf_dma_handle;
4085 pthru->dataxferlen = 256;
4087 memset(&mc, 0, sizeof(megacmd_t));
4089 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4090 mc.xferaddr = (u32)pthru_dma_handle;
4092 rval = mega_internal_command(adapter, &mc, pthru);
4094 pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4095 pthru_dma_handle);
4097 free_local_pdev(pdev);
4099 return rval;
4101 #endif
4104 * mega_internal_command()
4105 * @adapter - pointer to our soft state
4106 * @mc - the mailbox command
4107 * @pthru - Passthru structure for DCDB commands
4109 * Issue the internal commands in interrupt mode.
4110 * The last argument is the address of the passthru structure if the command
4111 * to be fired is a passthru command
4113 * Note: parameter 'pthru' is null for non-passthru commands.
4115 static int
4116 mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4118 unsigned long flags;
4119 scb_t *scb;
4120 int rval;
4123 * The internal commands share one command id and hence are
4124 * serialized. This is so because we want to reserve maximum number of
4125 * available command ids for the I/O commands.
4127 mutex_lock(&adapter->int_mtx);
4129 scb = &adapter->int_scb;
4130 memset(scb, 0, sizeof(scb_t));
4132 scb->idx = CMDID_INT_CMDS;
4133 scb->state |= SCB_ACTIVE | SCB_PENDQ;
4135 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4138 * Is it a passthru command
4140 if (mc->cmd == MEGA_MBOXCMD_PASSTHRU)
4141 scb->pthru = pthru;
4143 spin_lock_irqsave(&adapter->lock, flags);
4144 list_add_tail(&scb->list, &adapter->pending_list);
4146 * Check if the HBA is in quiescent state, e.g., during a
4147 * delete logical drive opertion. If it is, don't run
4148 * the pending_list.
4150 if (atomic_read(&adapter->quiescent) == 0)
4151 mega_runpendq(adapter);
4152 spin_unlock_irqrestore(&adapter->lock, flags);
4154 wait_for_completion(&adapter->int_waitq);
4156 mc->status = rval = adapter->int_status;
4159 * Print a debug message for all failed commands. Applications can use
4160 * this information.
4162 if (rval && trace_level) {
4163 dev_info(&adapter->dev->dev, "cmd [%x, %x, %x] status:[%x]\n",
4164 mc->cmd, mc->opcode, mc->subopcode, rval);
4167 mutex_unlock(&adapter->int_mtx);
4168 return rval;
4171 static struct scsi_host_template megaraid_template = {
4172 .module = THIS_MODULE,
4173 .name = "MegaRAID",
4174 .proc_name = "megaraid_legacy",
4175 .info = megaraid_info,
4176 .queuecommand = megaraid_queue,
4177 .bios_param = megaraid_biosparam,
4178 .max_sectors = MAX_SECTORS_PER_IO,
4179 .can_queue = MAX_COMMANDS,
4180 .this_id = DEFAULT_INITIATOR_ID,
4181 .sg_tablesize = MAX_SGLIST,
4182 .cmd_per_lun = DEF_CMD_PER_LUN,
4183 .use_clustering = ENABLE_CLUSTERING,
4184 .eh_abort_handler = megaraid_abort,
4185 .eh_device_reset_handler = megaraid_reset,
4186 .eh_bus_reset_handler = megaraid_reset,
4187 .eh_host_reset_handler = megaraid_reset,
4188 .no_write_same = 1,
4191 static int
4192 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4194 struct Scsi_Host *host;
4195 adapter_t *adapter;
4196 unsigned long mega_baseport, tbase, flag = 0;
4197 u16 subsysid, subsysvid;
4198 u8 pci_bus, pci_dev_func;
4199 int irq, i, j;
4200 int error = -ENODEV;
4202 if (pci_enable_device(pdev))
4203 goto out;
4204 pci_set_master(pdev);
4206 pci_bus = pdev->bus->number;
4207 pci_dev_func = pdev->devfn;
4210 * The megaraid3 stuff reports the ID of the Intel part which is not
4211 * remotely specific to the megaraid
4213 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4214 u16 magic;
4216 * Don't fall over the Compaq management cards using the same
4217 * PCI identifier
4219 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4220 pdev->subsystem_device == 0xC000)
4221 return -ENODEV;
4222 /* Now check the magic signature byte */
4223 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4224 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4225 return -ENODEV;
4226 /* Ok it is probably a megaraid */
4230 * For these vendor and device ids, signature offsets are not
4231 * valid and 64 bit is implicit
4233 if (id->driver_data & BOARD_64BIT)
4234 flag |= BOARD_64BIT;
4235 else {
4236 u32 magic64;
4238 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4239 if (magic64 == HBA_SIGNATURE_64BIT)
4240 flag |= BOARD_64BIT;
4243 subsysvid = pdev->subsystem_vendor;
4244 subsysid = pdev->subsystem_device;
4246 dev_notice(&pdev->dev, "found 0x%4.04x:0x%4.04x\n",
4247 id->vendor, id->device);
4249 /* Read the base port and IRQ from PCI */
4250 mega_baseport = pci_resource_start(pdev, 0);
4251 irq = pdev->irq;
4253 tbase = mega_baseport;
4254 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4255 flag |= BOARD_MEMMAP;
4257 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4258 dev_warn(&pdev->dev, "mem region busy!\n");
4259 goto out_disable_device;
4262 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4263 if (!mega_baseport) {
4264 dev_warn(&pdev->dev, "could not map hba memory\n");
4265 goto out_release_region;
4267 } else {
4268 flag |= BOARD_IOMAP;
4269 mega_baseport += 0x10;
4271 if (!request_region(mega_baseport, 16, "megaraid"))
4272 goto out_disable_device;
4275 /* Initialize SCSI Host structure */
4276 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4277 if (!host)
4278 goto out_iounmap;
4280 adapter = (adapter_t *)host->hostdata;
4281 memset(adapter, 0, sizeof(adapter_t));
4283 dev_notice(&pdev->dev,
4284 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4285 host->host_no, mega_baseport, irq);
4287 adapter->base = mega_baseport;
4288 if (flag & BOARD_MEMMAP)
4289 adapter->mmio_base = (void __iomem *) mega_baseport;
4291 INIT_LIST_HEAD(&adapter->free_list);
4292 INIT_LIST_HEAD(&adapter->pending_list);
4293 INIT_LIST_HEAD(&adapter->completed_list);
4295 adapter->flag = flag;
4296 spin_lock_init(&adapter->lock);
4298 host->cmd_per_lun = max_cmd_per_lun;
4299 host->max_sectors = max_sectors_per_io;
4301 adapter->dev = pdev;
4302 adapter->host = host;
4304 adapter->host->irq = irq;
4306 if (flag & BOARD_MEMMAP)
4307 adapter->host->base = tbase;
4308 else {
4309 adapter->host->io_port = tbase;
4310 adapter->host->n_io_port = 16;
4313 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4316 * Allocate buffer to issue internal commands.
4318 adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4319 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4320 if (!adapter->mega_buffer) {
4321 dev_warn(&pdev->dev, "out of RAM\n");
4322 goto out_host_put;
4325 adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4326 if (!adapter->scb_list) {
4327 dev_warn(&pdev->dev, "out of RAM\n");
4328 goto out_free_cmd_buffer;
4331 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4332 megaraid_isr_memmapped : megaraid_isr_iomapped,
4333 IRQF_SHARED, "megaraid", adapter)) {
4334 dev_warn(&pdev->dev, "Couldn't register IRQ %d!\n", irq);
4335 goto out_free_scb_list;
4338 if (mega_setup_mailbox(adapter))
4339 goto out_free_irq;
4341 if (mega_query_adapter(adapter))
4342 goto out_free_mbox;
4345 * Have checks for some buggy f/w
4347 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4349 * Which firmware
4351 if (!strcmp(adapter->fw_version, "3.00") ||
4352 !strcmp(adapter->fw_version, "3.01")) {
4354 dev_warn(&pdev->dev,
4355 "Your card is a Dell PERC "
4356 "2/SC RAID controller with "
4357 "firmware\nmegaraid: 3.00 or 3.01. "
4358 "This driver is known to have "
4359 "corruption issues\nmegaraid: with "
4360 "those firmware versions on this "
4361 "specific card. In order\nmegaraid: "
4362 "to protect your data, please upgrade "
4363 "your firmware to version\nmegaraid: "
4364 "3.10 or later, available from the "
4365 "Dell Technical Support web\n"
4366 "megaraid: site at\nhttp://support."
4367 "dell.com/us/en/filelib/download/"
4368 "index.asp?fileid=2940\n"
4374 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4375 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4376 * support, since this firmware cannot handle 64 bit
4377 * addressing
4379 if ((subsysvid == PCI_VENDOR_ID_HP) &&
4380 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4382 * which firmware
4384 if (!strcmp(adapter->fw_version, "H01.07") ||
4385 !strcmp(adapter->fw_version, "H01.08") ||
4386 !strcmp(adapter->fw_version, "H01.09") ) {
4387 dev_warn(&pdev->dev,
4388 "Firmware H.01.07, "
4389 "H.01.08, and H.01.09 on 1M/2M "
4390 "controllers\n"
4391 "do not support 64 bit "
4392 "addressing.\nDISABLING "
4393 "64 bit support.\n");
4394 adapter->flag &= ~BOARD_64BIT;
4398 if (mega_is_bios_enabled(adapter))
4399 mega_hbas[hba_count].is_bios_enabled = 1;
4400 mega_hbas[hba_count].hostdata_addr = adapter;
4403 * Find out which channel is raid and which is scsi. This is
4404 * for ROMB support.
4406 mega_enum_raid_scsi(adapter);
4409 * Find out if a logical drive is set as the boot drive. If
4410 * there is one, will make that as the first logical drive.
4411 * ROMB: Do we have to boot from a physical drive. Then all
4412 * the physical drives would appear before the logical disks.
4413 * Else, all the physical drives would be exported to the mid
4414 * layer after logical drives.
4416 mega_get_boot_drv(adapter);
4418 if (adapter->boot_pdrv_enabled) {
4419 j = adapter->product_info.nchannels;
4420 for( i = 0; i < j; i++ )
4421 adapter->logdrv_chan[i] = 0;
4422 for( i = j; i < NVIRT_CHAN + j; i++ )
4423 adapter->logdrv_chan[i] = 1;
4424 } else {
4425 for (i = 0; i < NVIRT_CHAN; i++)
4426 adapter->logdrv_chan[i] = 1;
4427 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4428 adapter->logdrv_chan[i] = 0;
4429 adapter->mega_ch_class <<= NVIRT_CHAN;
4433 * Do we support random deletion and addition of logical
4434 * drives
4436 adapter->read_ldidmap = 0; /* set it after first logdrv
4437 delete cmd */
4438 adapter->support_random_del = mega_support_random_del(adapter);
4440 /* Initialize SCBs */
4441 if (mega_init_scb(adapter))
4442 goto out_free_mbox;
4445 * Reset the pending commands counter
4447 atomic_set(&adapter->pend_cmds, 0);
4450 * Reset the adapter quiescent flag
4452 atomic_set(&adapter->quiescent, 0);
4454 hba_soft_state[hba_count] = adapter;
4457 * Fill in the structure which needs to be passed back to the
4458 * application when it does an ioctl() for controller related
4459 * information.
4461 i = hba_count;
4463 mcontroller[i].base = mega_baseport;
4464 mcontroller[i].irq = irq;
4465 mcontroller[i].numldrv = adapter->numldrv;
4466 mcontroller[i].pcibus = pci_bus;
4467 mcontroller[i].pcidev = id->device;
4468 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4469 mcontroller[i].pciid = -1;
4470 mcontroller[i].pcivendor = id->vendor;
4471 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4472 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4475 /* Set the Mode of addressing to 64 bit if we can */
4476 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4477 pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4478 adapter->has_64bit_addr = 1;
4479 } else {
4480 pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4481 adapter->has_64bit_addr = 0;
4484 mutex_init(&adapter->int_mtx);
4485 init_completion(&adapter->int_waitq);
4487 adapter->this_id = DEFAULT_INITIATOR_ID;
4488 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4490 #if MEGA_HAVE_CLUSTERING
4492 * Is cluster support enabled on this controller
4493 * Note: In a cluster the HBAs ( the initiators ) will have
4494 * different target IDs and we cannot assume it to be 7. Call
4495 * to mega_support_cluster() will get the target ids also if
4496 * the cluster support is available
4498 adapter->has_cluster = mega_support_cluster(adapter);
4499 if (adapter->has_cluster) {
4500 dev_notice(&pdev->dev,
4501 "Cluster driver, initiator id:%d\n",
4502 adapter->this_id);
4504 #endif
4506 pci_set_drvdata(pdev, host);
4508 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4510 error = scsi_add_host(host, &pdev->dev);
4511 if (error)
4512 goto out_free_mbox;
4514 scsi_scan_host(host);
4515 hba_count++;
4516 return 0;
4518 out_free_mbox:
4519 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4520 adapter->una_mbox64, adapter->una_mbox64_dma);
4521 out_free_irq:
4522 free_irq(adapter->host->irq, adapter);
4523 out_free_scb_list:
4524 kfree(adapter->scb_list);
4525 out_free_cmd_buffer:
4526 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4527 adapter->mega_buffer, adapter->buf_dma_handle);
4528 out_host_put:
4529 scsi_host_put(host);
4530 out_iounmap:
4531 if (flag & BOARD_MEMMAP)
4532 iounmap((void *)mega_baseport);
4533 out_release_region:
4534 if (flag & BOARD_MEMMAP)
4535 release_mem_region(tbase, 128);
4536 else
4537 release_region(mega_baseport, 16);
4538 out_disable_device:
4539 pci_disable_device(pdev);
4540 out:
4541 return error;
4544 static void
4545 __megaraid_shutdown(adapter_t *adapter)
4547 u_char raw_mbox[sizeof(struct mbox_out)];
4548 mbox_t *mbox = (mbox_t *)raw_mbox;
4549 int i;
4551 /* Flush adapter cache */
4552 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4553 raw_mbox[0] = FLUSH_ADAPTER;
4555 free_irq(adapter->host->irq, adapter);
4557 /* Issue a blocking (interrupts disabled) command to the card */
4558 issue_scb_block(adapter, raw_mbox);
4560 /* Flush disks cache */
4561 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4562 raw_mbox[0] = FLUSH_SYSTEM;
4564 /* Issue a blocking (interrupts disabled) command to the card */
4565 issue_scb_block(adapter, raw_mbox);
4567 if (atomic_read(&adapter->pend_cmds) > 0)
4568 dev_warn(&adapter->dev->dev, "pending commands!!\n");
4571 * Have a delibrate delay to make sure all the caches are
4572 * actually flushed.
4574 for (i = 0; i <= 10; i++)
4575 mdelay(1000);
4578 static void
4579 megaraid_remove_one(struct pci_dev *pdev)
4581 struct Scsi_Host *host = pci_get_drvdata(pdev);
4582 adapter_t *adapter = (adapter_t *)host->hostdata;
4584 scsi_remove_host(host);
4586 __megaraid_shutdown(adapter);
4588 /* Free our resources */
4589 if (adapter->flag & BOARD_MEMMAP) {
4590 iounmap((void *)adapter->base);
4591 release_mem_region(adapter->host->base, 128);
4592 } else
4593 release_region(adapter->base, 16);
4595 mega_free_sgl(adapter);
4597 #ifdef CONFIG_PROC_FS
4598 if (adapter->controller_proc_dir_entry) {
4599 remove_proc_entry("stat", adapter->controller_proc_dir_entry);
4600 remove_proc_entry("config",
4601 adapter->controller_proc_dir_entry);
4602 remove_proc_entry("mailbox",
4603 adapter->controller_proc_dir_entry);
4604 #if MEGA_HAVE_ENH_PROC
4605 remove_proc_entry("rebuild-rate",
4606 adapter->controller_proc_dir_entry);
4607 remove_proc_entry("battery-status",
4608 adapter->controller_proc_dir_entry);
4610 remove_proc_entry("diskdrives-ch0",
4611 adapter->controller_proc_dir_entry);
4612 remove_proc_entry("diskdrives-ch1",
4613 adapter->controller_proc_dir_entry);
4614 remove_proc_entry("diskdrives-ch2",
4615 adapter->controller_proc_dir_entry);
4616 remove_proc_entry("diskdrives-ch3",
4617 adapter->controller_proc_dir_entry);
4619 remove_proc_entry("raiddrives-0-9",
4620 adapter->controller_proc_dir_entry);
4621 remove_proc_entry("raiddrives-10-19",
4622 adapter->controller_proc_dir_entry);
4623 remove_proc_entry("raiddrives-20-29",
4624 adapter->controller_proc_dir_entry);
4625 remove_proc_entry("raiddrives-30-39",
4626 adapter->controller_proc_dir_entry);
4627 #endif
4629 char buf[12] = { 0 };
4630 sprintf(buf, "hba%d", adapter->host->host_no);
4631 remove_proc_entry(buf, mega_proc_dir_entry);
4634 #endif
4636 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4637 adapter->mega_buffer, adapter->buf_dma_handle);
4638 kfree(adapter->scb_list);
4639 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4640 adapter->una_mbox64, adapter->una_mbox64_dma);
4642 scsi_host_put(host);
4643 pci_disable_device(pdev);
4645 hba_count--;
4648 static void
4649 megaraid_shutdown(struct pci_dev *pdev)
4651 struct Scsi_Host *host = pci_get_drvdata(pdev);
4652 adapter_t *adapter = (adapter_t *)host->hostdata;
4654 __megaraid_shutdown(adapter);
4657 static struct pci_device_id megaraid_pci_tbl[] = {
4658 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4659 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4660 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
4661 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4662 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
4663 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4664 {0,}
4666 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
4668 static struct pci_driver megaraid_pci_driver = {
4669 .name = "megaraid_legacy",
4670 .id_table = megaraid_pci_tbl,
4671 .probe = megaraid_probe_one,
4672 .remove = megaraid_remove_one,
4673 .shutdown = megaraid_shutdown,
4676 static int __init megaraid_init(void)
4678 int error;
4680 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
4681 max_cmd_per_lun = MAX_CMD_PER_LUN;
4682 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
4683 max_mbox_busy_wait = MBOX_BUSY_WAIT;
4685 #ifdef CONFIG_PROC_FS
4686 mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
4687 if (!mega_proc_dir_entry) {
4688 printk(KERN_WARNING
4689 "megaraid: failed to create megaraid root\n");
4691 #endif
4692 error = pci_register_driver(&megaraid_pci_driver);
4693 if (error) {
4694 #ifdef CONFIG_PROC_FS
4695 remove_proc_entry("megaraid", NULL);
4696 #endif
4697 return error;
4701 * Register the driver as a character device, for applications
4702 * to access it for ioctls.
4703 * First argument (major) to register_chrdev implies a dynamic
4704 * major number allocation.
4706 major = register_chrdev(0, "megadev_legacy", &megadev_fops);
4707 if (!major) {
4708 printk(KERN_WARNING
4709 "megaraid: failed to register char device\n");
4712 return 0;
4715 static void __exit megaraid_exit(void)
4718 * Unregister the character device interface to the driver.
4720 unregister_chrdev(major, "megadev_legacy");
4722 pci_unregister_driver(&megaraid_pci_driver);
4724 #ifdef CONFIG_PROC_FS
4725 remove_proc_entry("megaraid", NULL);
4726 #endif
4729 module_init(megaraid_init);
4730 module_exit(megaraid_exit);
4732 /* vi: set ts=8 sw=8 tw=78: */