1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Linux MegaRAID device driver
6 * Copyright (c) 2002 LSI Logic Corporation.
8 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
10 * - speed-ups (list handling fixes, issued_list, optimizations.)
13 * Copyright (c) 2003 Christoph Hellwig <hch@lst.de>
14 * - new-style, hotplug-aware pci probing and scsi registration
16 * Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
17 * <Seokmann.Ju@lsil.com>
19 * Description: Linux device driver for LSI Logic MegaRAID controller
21 * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
24 * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
25 * and others. Please send updates to the mailing list
26 * linux-scsi@vger.kernel.org .
31 #include <linux/blkdev.h>
32 #include <linux/uaccess.h>
34 #include <linux/completion.h>
35 #include <linux/delay.h>
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <linux/reboot.h>
39 #include <linux/module.h>
40 #include <linux/list.h>
41 #include <linux/interrupt.h>
42 #include <linux/pci.h>
43 #include <linux/init.h>
44 #include <linux/dma-mapping.h>
45 #include <linux/mutex.h>
46 #include <linux/slab.h>
47 #include <scsi/scsicam.h>
50 #include <scsi/scsi_host.h>
54 #define MEGARAID_MODULE_VERSION "2.00.4"
56 MODULE_AUTHOR ("sju@lsil.com");
57 MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
58 MODULE_LICENSE ("GPL");
59 MODULE_VERSION(MEGARAID_MODULE_VERSION);
61 static DEFINE_MUTEX(megadev_mutex);
62 static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
63 module_param(max_cmd_per_lun, uint, 0);
64 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)");
66 static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
67 module_param(max_sectors_per_io, ushort, 0);
68 MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
71 static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
72 module_param(max_mbox_busy_wait, ushort, 0);
73 MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
75 #define RDINDOOR(adapter) readl((adapter)->mmio_base + 0x20)
76 #define RDOUTDOOR(adapter) readl((adapter)->mmio_base + 0x2C)
77 #define WRINDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x20)
78 #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
85 static adapter_t *hba_soft_state[MAX_CONTROLLERS];
86 static struct proc_dir_entry *mega_proc_dir_entry;
88 /* For controller re-ordering */
89 static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
92 megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
95 * The File Operations structure for the serial/ioctl interface of the driver
97 static const struct file_operations megadev_fops = {
99 .unlocked_ioctl = megadev_unlocked_ioctl,
100 .open = megadev_open,
101 .llseek = noop_llseek,
105 * Array to structures for storing the information about the controllers. This
106 * information is sent to the user level applications, when they do an ioctl
107 * for this information.
109 static struct mcontroller mcontroller[MAX_CONTROLLERS];
111 /* The current driver version */
112 static u32 driver_ver = 0x02000000;
114 /* major number used by the device for character interface */
117 #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
121 * Debug variable to print some diagnostic messages
123 static int trace_level;
126 * mega_setup_mailbox()
127 * @adapter: pointer to our soft state
129 * Allocates a 8 byte aligned memory for the handshake mailbox.
132 mega_setup_mailbox(adapter_t *adapter)
136 adapter->una_mbox64 = dma_alloc_coherent(&adapter->dev->dev,
138 &adapter->una_mbox64_dma,
141 if( !adapter->una_mbox64 ) return -1;
143 adapter->mbox = &adapter->una_mbox64->mbox;
145 adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
148 adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
150 align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
152 adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
155 * Register the mailbox if the controller is an io-mapped controller
157 if( adapter->flag & BOARD_IOMAP ) {
159 outb(adapter->mbox_dma & 0xFF,
160 adapter->host->io_port + MBOX_PORT0);
162 outb((adapter->mbox_dma >> 8) & 0xFF,
163 adapter->host->io_port + MBOX_PORT1);
165 outb((adapter->mbox_dma >> 16) & 0xFF,
166 adapter->host->io_port + MBOX_PORT2);
168 outb((adapter->mbox_dma >> 24) & 0xFF,
169 adapter->host->io_port + MBOX_PORT3);
171 outb(ENABLE_MBOX_BYTE,
172 adapter->host->io_port + ENABLE_MBOX_REGION);
184 * mega_query_adapter()
185 * @adapter - pointer to our soft state
187 * Issue the adapter inquiry commands to the controller and find out
188 * information and parameter about the devices attached
191 mega_query_adapter(adapter_t *adapter)
193 dma_addr_t prod_info_dma_handle;
194 mega_inquiry3 *inquiry3;
195 struct mbox_out mbox;
196 u8 *raw_mbox = (u8 *)&mbox;
199 /* Initialize adapter inquiry mailbox */
201 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
202 memset(&mbox, 0, sizeof(mbox));
205 * Try to issue Inquiry3 command
206 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
207 * update enquiry3 structure
209 mbox.xferaddr = (u32)adapter->buf_dma_handle;
211 inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
213 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
214 raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
215 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
217 /* Issue a blocking command to the card */
218 if ((retval = issue_scb_block(adapter, raw_mbox))) {
219 /* the adapter does not support 40ld */
221 mraid_ext_inquiry *ext_inq;
223 dma_addr_t dma_handle;
225 ext_inq = dma_alloc_coherent(&adapter->dev->dev,
226 sizeof(mraid_ext_inquiry),
227 &dma_handle, GFP_KERNEL);
229 if( ext_inq == NULL ) return -1;
231 inq = &ext_inq->raid_inq;
233 mbox.xferaddr = (u32)dma_handle;
235 /*issue old 0x04 command to adapter */
236 mbox.cmd = MEGA_MBOXCMD_ADPEXTINQ;
238 issue_scb_block(adapter, raw_mbox);
241 * update Enquiry3 and ProductInfo structures with
242 * mraid_inquiry structure
244 mega_8_to_40ld(inq, inquiry3,
245 (mega_product_info *)&adapter->product_info);
247 dma_free_coherent(&adapter->dev->dev,
248 sizeof(mraid_ext_inquiry), ext_inq,
251 } else { /*adapter supports 40ld */
252 adapter->flag |= BOARD_40LD;
255 * get product_info, which is static information and will be
258 prod_info_dma_handle = dma_map_single(&adapter->dev->dev,
259 (void *)&adapter->product_info,
260 sizeof(mega_product_info),
263 mbox.xferaddr = prod_info_dma_handle;
265 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
266 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
268 if ((retval = issue_scb_block(adapter, raw_mbox)))
269 dev_warn(&adapter->dev->dev,
270 "Product_info cmd failed with error: %d\n",
273 dma_unmap_single(&adapter->dev->dev, prod_info_dma_handle,
274 sizeof(mega_product_info), DMA_FROM_DEVICE);
279 * kernel scans the channels from 0 to <= max_channel
281 adapter->host->max_channel =
282 adapter->product_info.nchannels + NVIRT_CHAN -1;
284 adapter->host->max_id = 16; /* max targets per channel */
286 adapter->host->max_lun = 7; /* Up to 7 luns for non disk devices */
288 adapter->host->cmd_per_lun = max_cmd_per_lun;
290 adapter->numldrv = inquiry3->num_ldrv;
292 adapter->max_cmds = adapter->product_info.max_commands;
294 if(adapter->max_cmds > MAX_COMMANDS)
295 adapter->max_cmds = MAX_COMMANDS;
297 adapter->host->can_queue = adapter->max_cmds - 1;
300 * Get the maximum number of scatter-gather elements supported by this
303 mega_get_max_sgl(adapter);
305 adapter->host->sg_tablesize = adapter->sglen;
307 /* use HP firmware and bios version encoding
308 Note: fw_version[0|1] and bios_version[0|1] were originally shifted
309 right 8 bits making them zero. This 0 value was hardcoded to fix
311 if (adapter->product_info.subsysvid == PCI_VENDOR_ID_HP) {
312 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
314 adapter->product_info.fw_version[2],
316 adapter->product_info.fw_version[1] & 0x0f,
318 adapter->product_info.fw_version[0] & 0x0f);
319 snprintf(adapter->bios_version, sizeof(adapter->fw_version),
321 adapter->product_info.bios_version[2],
323 adapter->product_info.bios_version[1] & 0x0f,
325 adapter->product_info.bios_version[0] & 0x0f);
327 memcpy(adapter->fw_version,
328 (char *)adapter->product_info.fw_version, 4);
329 adapter->fw_version[4] = 0;
331 memcpy(adapter->bios_version,
332 (char *)adapter->product_info.bios_version, 4);
334 adapter->bios_version[4] = 0;
337 dev_notice(&adapter->dev->dev, "[%s:%s] detected %d logical drives\n",
338 adapter->fw_version, adapter->bios_version, adapter->numldrv);
341 * Do we support extended (>10 bytes) cdbs
343 adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
344 if (adapter->support_ext_cdb)
345 dev_notice(&adapter->dev->dev, "supports extended CDBs\n");
353 * @adapter: pointer to our soft state
355 * Runs through the list of pending requests.
358 mega_runpendq(adapter_t *adapter)
360 if(!list_empty(&adapter->pending_list))
361 __mega_runpendq(adapter);
366 * @scmd - Issue this scsi command
367 * @done - the callback hook into the scsi mid-layer
369 * The command queuing entry point for the mid-layer.
371 static int megaraid_queue_lck(struct scsi_cmnd *scmd)
378 adapter = (adapter_t *)scmd->device->host->hostdata;
381 * Allocate and build a SCB request
382 * busy flag will be set if mega_build_cmd() command could not
383 * allocate scb. We will return non-zero status in that case.
384 * NOTE: scb can be null even though certain commands completed
385 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
386 * return 0 in that case.
389 spin_lock_irqsave(&adapter->lock, flags);
390 scb = mega_build_cmd(adapter, scmd, &busy);
394 scb->state |= SCB_PENDQ;
395 list_add_tail(&scb->list, &adapter->pending_list);
398 * Check if the HBA is in quiescent state, e.g., during a
399 * delete logical drive opertion. If it is, don't run
402 if (atomic_read(&adapter->quiescent) == 0)
403 mega_runpendq(adapter);
407 spin_unlock_irqrestore(&adapter->lock, flags);
411 static DEF_SCSI_QCMD(megaraid_queue)
414 * mega_allocate_scb()
415 * @adapter: pointer to our soft state
416 * @cmd: scsi command from the mid-layer
418 * Allocate a SCB structure. This is the central structure for controller
421 static inline scb_t *
422 mega_allocate_scb(adapter_t *adapter, struct scsi_cmnd *cmd)
424 struct list_head *head = &adapter->free_list;
427 /* Unlink command from Free List */
428 if( !list_empty(head) ) {
430 scb = list_entry(head->next, scb_t, list);
432 list_del_init(head->next);
434 scb->state = SCB_ACTIVE;
436 scb->dma_type = MEGA_DMA_TYPE_NONE;
445 * mega_get_ldrv_num()
446 * @adapter: pointer to our soft state
447 * @cmd: scsi mid layer command
448 * @channel: channel on the controller
450 * Calculate the logical drive number based on the information in scsi command
451 * and the channel number.
454 mega_get_ldrv_num(adapter_t *adapter, struct scsi_cmnd *cmd, int channel)
459 tgt = cmd->device->id;
461 if ( tgt > adapter->this_id )
462 tgt--; /* we do not get inquires for initiator id */
464 ldrv_num = (channel * 15) + tgt;
468 * If we have a logical drive with boot enabled, project it first
470 if( adapter->boot_ldrv_enabled ) {
471 if( ldrv_num == 0 ) {
472 ldrv_num = adapter->boot_ldrv;
475 if( ldrv_num <= adapter->boot_ldrv ) {
482 * If "delete logical drive" feature is enabled on this controller.
483 * Do only if at least one delete logical drive operation was done.
485 * Also, after logical drive deletion, instead of logical drive number,
486 * the value returned should be 0x80+logical drive id.
488 * These is valid only for IO commands.
491 if (adapter->support_random_del && adapter->read_ldidmap )
492 switch (cmd->cmnd[0]) {
505 * @adapter: pointer to our soft state
506 * @cmd: Prepare using this scsi command
507 * @busy: busy flag if no resources
509 * Prepares a command and scatter gather list for the controller. This routine
510 * also finds out if the commands is intended for a logical drive or a
511 * physical device and prepares the controller command accordingly.
513 * We also re-order the logical drives and physical devices based on their
517 mega_build_cmd(adapter_t *adapter, struct scsi_cmnd *cmd, int *busy)
519 mega_passthru *pthru;
527 int ldrv_num = 0; /* logical drive number */
530 * We know what channels our logical drives are on - mega_find_card()
532 islogical = adapter->logdrv_chan[cmd->device->channel];
535 * The theory: If physical drive is chosen for boot, all the physical
536 * devices are exported before the logical drives, otherwise physical
537 * devices are pushed after logical drives, in which case - Kernel sees
538 * the physical devices on virtual channel which is obviously converted
539 * to actual channel on the HBA.
541 if( adapter->boot_pdrv_enabled ) {
543 /* logical channel */
544 channel = cmd->device->channel -
545 adapter->product_info.nchannels;
548 /* this is physical channel */
549 channel = cmd->device->channel;
550 target = cmd->device->id;
553 * boot from a physical disk, that disk needs to be
554 * exposed first IF both the channels are SCSI, then
555 * booting from the second channel is not allowed.
558 target = adapter->boot_pdrv_tgt;
560 else if( target == adapter->boot_pdrv_tgt ) {
567 /* this is the logical channel */
568 channel = cmd->device->channel;
571 /* physical channel */
572 channel = cmd->device->channel - NVIRT_CHAN;
573 target = cmd->device->id;
580 /* have just LUN 0 for each target on virtual channels */
581 if (cmd->device->lun) {
582 cmd->result = (DID_BAD_TARGET << 16);
587 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
590 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
591 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
594 * max_ldrv_num increases by 0x80 if some logical drive was
597 if(adapter->read_ldidmap)
598 max_ldrv_num += 0x80;
600 if(ldrv_num > max_ldrv_num ) {
601 cmd->result = (DID_BAD_TARGET << 16);
608 if( cmd->device->lun > 7) {
610 * Do not support lun >7 for physically accessed
613 cmd->result = (DID_BAD_TARGET << 16);
621 * Logical drive commands
625 switch (cmd->cmnd[0]) {
626 case TEST_UNIT_READY:
627 #if MEGA_HAVE_CLUSTERING
629 * Do we support clustering and is the support enabled
630 * If no, return success always
632 if( !adapter->has_cluster ) {
633 cmd->result = (DID_OK << 16);
638 if(!(scb = mega_allocate_scb(adapter, cmd))) {
643 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
644 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
645 scb->raw_mbox[3] = ldrv_num;
647 scb->dma_direction = DMA_NONE;
651 cmd->result = (DID_OK << 16);
658 struct scatterlist *sg;
660 sg = scsi_sglist(cmd);
661 buf = kmap_atomic(sg_page(sg)) + sg->offset;
663 memset(buf, 0, cmd->cmnd[4]);
664 kunmap_atomic(buf - sg->offset);
666 cmd->result = (DID_OK << 16);
674 if(!(adapter->flag & (1L << cmd->device->channel))) {
676 dev_notice(&adapter->dev->dev,
677 "scsi%d: scanning scsi channel %d "
678 "for logical drives\n",
679 adapter->host->host_no,
680 cmd->device->channel);
682 adapter->flag |= (1L << cmd->device->channel);
685 /* Allocate a SCB and initialize passthru */
686 if(!(scb = mega_allocate_scb(adapter, cmd))) {
692 mbox = (mbox_t *)scb->raw_mbox;
693 memset(mbox, 0, sizeof(scb->raw_mbox));
694 memset(pthru, 0, sizeof(mega_passthru));
698 pthru->reqsenselen = 14;
699 pthru->islogical = 1;
700 pthru->logdrv = ldrv_num;
701 pthru->cdblen = cmd->cmd_len;
702 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
704 if( adapter->has_64bit_addr ) {
705 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
708 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
711 scb->dma_direction = DMA_FROM_DEVICE;
713 pthru->numsgelements = mega_build_sglist(adapter, scb,
714 &pthru->dataxferaddr, &pthru->dataxferlen);
716 mbox->m_out.xferaddr = scb->pthru_dma_addr;
727 /* Allocate a SCB and initialize mailbox */
728 if(!(scb = mega_allocate_scb(adapter, cmd))) {
732 mbox = (mbox_t *)scb->raw_mbox;
734 memset(mbox, 0, sizeof(scb->raw_mbox));
735 mbox->m_out.logdrv = ldrv_num;
738 * A little hack: 2nd bit is zero for all scsi read
739 * commands and is set for all scsi write commands
741 if( adapter->has_64bit_addr ) {
742 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
743 MEGA_MBOXCMD_LWRITE64:
744 MEGA_MBOXCMD_LREAD64 ;
747 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
753 * 6-byte READ(0x08) or WRITE(0x0A) cdb
755 if( cmd->cmd_len == 6 ) {
756 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
758 ((u32)cmd->cmnd[1] << 16) |
759 ((u32)cmd->cmnd[2] << 8) |
762 mbox->m_out.lba &= 0x1FFFFF;
766 * Take modulo 0x80, since the logical drive
767 * number increases by 0x80 when a logical
770 if (*cmd->cmnd == READ_6) {
771 adapter->nreads[ldrv_num%0x80]++;
772 adapter->nreadblocks[ldrv_num%0x80] +=
773 mbox->m_out.numsectors;
775 adapter->nwrites[ldrv_num%0x80]++;
776 adapter->nwriteblocks[ldrv_num%0x80] +=
777 mbox->m_out.numsectors;
783 * 10-byte READ(0x28) or WRITE(0x2A) cdb
785 if( cmd->cmd_len == 10 ) {
786 mbox->m_out.numsectors =
788 ((u32)cmd->cmnd[7] << 8);
790 ((u32)cmd->cmnd[2] << 24) |
791 ((u32)cmd->cmnd[3] << 16) |
792 ((u32)cmd->cmnd[4] << 8) |
796 if (*cmd->cmnd == READ_10) {
797 adapter->nreads[ldrv_num%0x80]++;
798 adapter->nreadblocks[ldrv_num%0x80] +=
799 mbox->m_out.numsectors;
801 adapter->nwrites[ldrv_num%0x80]++;
802 adapter->nwriteblocks[ldrv_num%0x80] +=
803 mbox->m_out.numsectors;
809 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
811 if( cmd->cmd_len == 12 ) {
813 ((u32)cmd->cmnd[2] << 24) |
814 ((u32)cmd->cmnd[3] << 16) |
815 ((u32)cmd->cmnd[4] << 8) |
818 mbox->m_out.numsectors =
819 ((u32)cmd->cmnd[6] << 24) |
820 ((u32)cmd->cmnd[7] << 16) |
821 ((u32)cmd->cmnd[8] << 8) |
825 if (*cmd->cmnd == READ_12) {
826 adapter->nreads[ldrv_num%0x80]++;
827 adapter->nreadblocks[ldrv_num%0x80] +=
828 mbox->m_out.numsectors;
830 adapter->nwrites[ldrv_num%0x80]++;
831 adapter->nwriteblocks[ldrv_num%0x80] +=
832 mbox->m_out.numsectors;
838 * If it is a read command
840 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
841 scb->dma_direction = DMA_FROM_DEVICE;
844 scb->dma_direction = DMA_TO_DEVICE;
847 /* Calculate Scatter-Gather info */
848 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
849 (u32 *)&mbox->m_out.xferaddr, &seg);
853 #if MEGA_HAVE_CLUSTERING
858 * Do we support clustering and is the support enabled
860 if( ! adapter->has_cluster ) {
862 cmd->result = (DID_BAD_TARGET << 16);
867 /* Allocate a SCB and initialize mailbox */
868 if(!(scb = mega_allocate_scb(adapter, cmd))) {
873 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
874 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
875 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
877 scb->raw_mbox[3] = ldrv_num;
879 scb->dma_direction = DMA_NONE;
885 cmd->result = (DID_BAD_TARGET << 16);
892 * Passthru drive commands
895 /* Allocate a SCB and initialize passthru */
896 if(!(scb = mega_allocate_scb(adapter, cmd))) {
901 mbox = (mbox_t *)scb->raw_mbox;
902 memset(mbox, 0, sizeof(scb->raw_mbox));
904 if( adapter->support_ext_cdb ) {
906 mega_prepare_extpassthru(adapter, scb, cmd,
909 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
911 mbox->m_out.xferaddr = scb->epthru_dma_addr;
916 pthru = mega_prepare_passthru(adapter, scb, cmd,
919 /* Initialize mailbox */
920 if( adapter->has_64bit_addr ) {
921 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
924 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
927 mbox->m_out.xferaddr = scb->pthru_dma_addr;
937 * mega_prepare_passthru()
938 * @adapter: pointer to our soft state
939 * @scb: our scsi control block
940 * @cmd: scsi command from the mid-layer
941 * @channel: actual channel on the controller
942 * @target: actual id on the controller.
944 * prepare a command for the scsi physical devices.
946 static mega_passthru *
947 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, struct scsi_cmnd *cmd,
948 int channel, int target)
950 mega_passthru *pthru;
953 memset(pthru, 0, sizeof (mega_passthru));
955 /* 0=6sec/1=60sec/2=10min/3=3hrs */
959 pthru->reqsenselen = 14;
960 pthru->islogical = 0;
962 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
964 pthru->target = (adapter->flag & BOARD_40LD) ?
965 (channel << 4) | target : target;
967 pthru->cdblen = cmd->cmd_len;
968 pthru->logdrv = cmd->device->lun;
970 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
972 /* Not sure about the direction */
973 scb->dma_direction = DMA_BIDIRECTIONAL;
975 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
976 switch (cmd->cmnd[0]) {
979 if(!(adapter->flag & (1L << cmd->device->channel))) {
981 dev_notice(&adapter->dev->dev,
982 "scsi%d: scanning scsi channel %d [P%d] "
983 "for physical devices\n",
984 adapter->host->host_no,
985 cmd->device->channel, channel);
987 adapter->flag |= (1L << cmd->device->channel);
991 pthru->numsgelements = mega_build_sglist(adapter, scb,
992 &pthru->dataxferaddr, &pthru->dataxferlen);
1000 * mega_prepare_extpassthru()
1001 * @adapter: pointer to our soft state
1002 * @scb: our scsi control block
1003 * @cmd: scsi command from the mid-layer
1004 * @channel: actual channel on the controller
1005 * @target: actual id on the controller.
1007 * prepare a command for the scsi physical devices. This rountine prepares
1008 * commands for devices which can take extended CDBs (>10 bytes)
1010 static mega_ext_passthru *
1011 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb,
1012 struct scsi_cmnd *cmd,
1013 int channel, int target)
1015 mega_ext_passthru *epthru;
1017 epthru = scb->epthru;
1018 memset(epthru, 0, sizeof(mega_ext_passthru));
1020 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1021 epthru->timeout = 2;
1024 epthru->reqsenselen = 14;
1025 epthru->islogical = 0;
1027 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1028 epthru->target = (adapter->flag & BOARD_40LD) ?
1029 (channel << 4) | target : target;
1031 epthru->cdblen = cmd->cmd_len;
1032 epthru->logdrv = cmd->device->lun;
1034 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1036 /* Not sure about the direction */
1037 scb->dma_direction = DMA_BIDIRECTIONAL;
1039 switch(cmd->cmnd[0]) {
1042 if(!(adapter->flag & (1L << cmd->device->channel))) {
1044 dev_notice(&adapter->dev->dev,
1045 "scsi%d: scanning scsi channel %d [P%d] "
1046 "for physical devices\n",
1047 adapter->host->host_no,
1048 cmd->device->channel, channel);
1050 adapter->flag |= (1L << cmd->device->channel);
1054 epthru->numsgelements = mega_build_sglist(adapter, scb,
1055 &epthru->dataxferaddr, &epthru->dataxferlen);
1063 __mega_runpendq(adapter_t *adapter)
1066 struct list_head *pos, *next;
1068 /* Issue any pending commands to the card */
1069 list_for_each_safe(pos, next, &adapter->pending_list) {
1071 scb = list_entry(pos, scb_t, list);
1073 if( !(scb->state & SCB_ISSUED) ) {
1075 if( issue_scb(adapter, scb) != 0 )
1086 * @adapter: pointer to our soft state
1087 * @scb: scsi control block
1089 * Post a command to the card if the mailbox is available, otherwise return
1090 * busy. We also take the scb from the pending list if the mailbox is
1094 issue_scb(adapter_t *adapter, scb_t *scb)
1096 volatile mbox64_t *mbox64 = adapter->mbox64;
1097 volatile mbox_t *mbox = adapter->mbox;
1100 if(unlikely(mbox->m_in.busy)) {
1104 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1106 if(mbox->m_in.busy) return -1;
1109 /* Copy mailbox data into host structure */
1110 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1111 sizeof(struct mbox_out));
1113 mbox->m_out.cmdid = scb->idx; /* Set cmdid */
1114 mbox->m_in.busy = 1; /* Set busy */
1118 * Increment the pending queue counter
1120 atomic_inc(&adapter->pend_cmds);
1122 switch (mbox->m_out.cmd) {
1123 case MEGA_MBOXCMD_LREAD64:
1124 case MEGA_MBOXCMD_LWRITE64:
1125 case MEGA_MBOXCMD_PASSTHRU64:
1126 case MEGA_MBOXCMD_EXTPTHRU:
1127 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1128 mbox64->xfer_segment_hi = 0;
1129 mbox->m_out.xferaddr = 0xFFFFFFFF;
1132 mbox64->xfer_segment_lo = 0;
1133 mbox64->xfer_segment_hi = 0;
1139 scb->state |= SCB_ISSUED;
1141 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1142 mbox->m_in.poll = 0;
1144 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1147 irq_enable(adapter);
1148 issue_command(adapter);
1155 * Wait until the controller's mailbox is available
1158 mega_busywait_mbox (adapter_t *adapter)
1160 if (adapter->mbox->m_in.busy)
1161 return __mega_busywait_mbox(adapter);
1167 * @adapter: pointer to our soft state
1168 * @raw_mbox: the mailbox
1170 * Issue a scb in synchronous and non-interrupt mode
1173 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1175 volatile mbox64_t *mbox64 = adapter->mbox64;
1176 volatile mbox_t *mbox = adapter->mbox;
1179 /* Wait until mailbox is free */
1180 if(mega_busywait_mbox (adapter))
1181 goto bug_blocked_mailbox;
1183 /* Copy mailbox data into host structure */
1184 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1185 mbox->m_out.cmdid = 0xFE;
1186 mbox->m_in.busy = 1;
1188 switch (raw_mbox[0]) {
1189 case MEGA_MBOXCMD_LREAD64:
1190 case MEGA_MBOXCMD_LWRITE64:
1191 case MEGA_MBOXCMD_PASSTHRU64:
1192 case MEGA_MBOXCMD_EXTPTHRU:
1193 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1194 mbox64->xfer_segment_hi = 0;
1195 mbox->m_out.xferaddr = 0xFFFFFFFF;
1198 mbox64->xfer_segment_lo = 0;
1199 mbox64->xfer_segment_hi = 0;
1202 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1203 mbox->m_in.poll = 0;
1205 mbox->m_in.numstatus = 0xFF;
1206 mbox->m_in.status = 0xFF;
1207 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1209 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1212 mbox->m_in.numstatus = 0xFF;
1214 while( (volatile u8)mbox->m_in.poll != 0x77 )
1217 mbox->m_in.poll = 0;
1218 mbox->m_in.ack = 0x77;
1220 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1222 while(RDINDOOR(adapter) & 0x2)
1226 irq_disable(adapter);
1227 issue_command(adapter);
1229 while (!((byte = irq_state(adapter)) & INTR_VALID))
1232 set_irq_state(adapter, byte);
1233 irq_enable(adapter);
1237 return mbox->m_in.status;
1239 bug_blocked_mailbox:
1240 dev_warn(&adapter->dev->dev, "Blocked mailbox......!!\n");
1247 * megaraid_isr_iomapped()
1249 * @devp: pointer to our soft state
1251 * Interrupt service routine for io-mapped controllers.
1252 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1253 * and service the completed commands.
1256 megaraid_isr_iomapped(int irq, void *devp)
1258 adapter_t *adapter = devp;
1259 unsigned long flags;
1262 u8 completed[MAX_FIRMWARE_STATUS];
1268 * loop till F/W has more commands for us to complete.
1270 spin_lock_irqsave(&adapter->lock, flags);
1273 /* Check if a valid interrupt is pending */
1274 byte = irq_state(adapter);
1275 if( (byte & VALID_INTR_BYTE) == 0 ) {
1277 * No more pending commands
1281 set_irq_state(adapter, byte);
1283 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1286 adapter->mbox->m_in.numstatus = 0xFF;
1288 status = adapter->mbox->m_in.status;
1291 * decrement the pending queue counter
1293 atomic_sub(nstatus, &adapter->pend_cmds);
1295 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1298 /* Acknowledge interrupt */
1301 mega_cmd_done(adapter, completed, nstatus, status);
1303 mega_rundoneq(adapter);
1307 /* Loop through any pending requests */
1308 if(atomic_read(&adapter->quiescent) == 0) {
1309 mega_runpendq(adapter);
1316 spin_unlock_irqrestore(&adapter->lock, flags);
1318 return IRQ_RETVAL(handled);
1323 * megaraid_isr_memmapped()
1325 * @devp: pointer to our soft state
1327 * Interrupt service routine for memory-mapped controllers.
1328 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1329 * and service the completed commands.
1332 megaraid_isr_memmapped(int irq, void *devp)
1334 adapter_t *adapter = devp;
1335 unsigned long flags;
1339 u8 completed[MAX_FIRMWARE_STATUS];
1344 * loop till F/W has more commands for us to complete.
1346 spin_lock_irqsave(&adapter->lock, flags);
1349 /* Check if a valid interrupt is pending */
1350 dword = RDOUTDOOR(adapter);
1351 if(dword != 0x10001234) {
1353 * No more pending commands
1357 WROUTDOOR(adapter, 0x10001234);
1359 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1363 adapter->mbox->m_in.numstatus = 0xFF;
1365 status = adapter->mbox->m_in.status;
1368 * decrement the pending queue counter
1370 atomic_sub(nstatus, &adapter->pend_cmds);
1372 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1375 /* Acknowledge interrupt */
1376 WRINDOOR(adapter, 0x2);
1380 while( RDINDOOR(adapter) & 0x02 )
1383 mega_cmd_done(adapter, completed, nstatus, status);
1385 mega_rundoneq(adapter);
1387 /* Loop through any pending requests */
1388 if(atomic_read(&adapter->quiescent) == 0) {
1389 mega_runpendq(adapter);
1396 spin_unlock_irqrestore(&adapter->lock, flags);
1398 return IRQ_RETVAL(handled);
1402 * @adapter: pointer to our soft state
1403 * @completed: array of ids of completed commands
1404 * @nstatus: number of completed commands
1405 * @status: status of the last command completed
1407 * Complete the commands and call the scsi mid-layer callback hooks.
1410 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1412 mega_ext_passthru *epthru = NULL;
1413 struct scatterlist *sgl;
1414 struct scsi_cmnd *cmd = NULL;
1415 mega_passthru *pthru = NULL;
1416 mbox_t *mbox = NULL;
1424 * for all the commands completed, call the mid-layer callback routine
1427 for( i = 0; i < nstatus; i++ ) {
1429 cmdid = completed[i];
1432 * Only free SCBs for the commands coming down from the
1433 * mid-layer, not for which were issued internally
1435 * For internal command, restore the status returned by the
1436 * firmware so that user can interpret it.
1438 if (cmdid == CMDID_INT_CMDS) {
1439 scb = &adapter->int_scb;
1441 list_del_init(&scb->list);
1442 scb->state = SCB_FREE;
1444 adapter->int_status = status;
1445 complete(&adapter->int_waitq);
1447 scb = &adapter->scb_list[cmdid];
1450 * Make sure f/w has completed a valid command
1452 if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1453 dev_crit(&adapter->dev->dev, "invalid command "
1454 "Id %d, scb->state:%x, scsi cmd:%p\n",
1455 cmdid, scb->state, scb->cmd);
1461 * Was a abort issued for this command
1463 if( scb->state & SCB_ABORT ) {
1465 dev_warn(&adapter->dev->dev,
1466 "aborted cmd [%x] complete\n",
1469 scb->cmd->result = (DID_ABORT << 16);
1471 list_add_tail(SCSI_LIST(scb->cmd),
1472 &adapter->completed_list);
1474 mega_free_scb(adapter, scb);
1480 * Was a reset issued for this command
1482 if( scb->state & SCB_RESET ) {
1484 dev_warn(&adapter->dev->dev,
1485 "reset cmd [%x] complete\n",
1488 scb->cmd->result = (DID_RESET << 16);
1490 list_add_tail(SCSI_LIST(scb->cmd),
1491 &adapter->completed_list);
1493 mega_free_scb (adapter, scb);
1500 epthru = scb->epthru;
1501 mbox = (mbox_t *)scb->raw_mbox;
1506 int logdrv = mbox->m_out.logdrv;
1508 islogical = adapter->logdrv_chan[cmd->channel];
1510 * Maintain an error counter for the logical drive.
1511 * Some application like SNMP agent need such
1514 if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1515 cmd->cmnd[0] == READ_10 ||
1516 cmd->cmnd[0] == READ_12)) {
1518 * Logical drive number increases by 0x80 when
1519 * a logical drive is deleted
1521 adapter->rd_errors[logdrv%0x80]++;
1524 if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1525 cmd->cmnd[0] == WRITE_10 ||
1526 cmd->cmnd[0] == WRITE_12)) {
1528 * Logical drive number increases by 0x80 when
1529 * a logical drive is deleted
1531 adapter->wr_errors[logdrv%0x80]++;
1539 * Do not return the presence of hard disk on the channel so,
1540 * inquiry sent, and returned data==hard disk or removable
1541 * hard disk and not logical, request should return failure! -
1544 islogical = adapter->logdrv_chan[cmd->device->channel];
1545 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1547 sgl = scsi_sglist(cmd);
1548 if( sg_page(sgl) ) {
1549 c = *(unsigned char *) sg_virt(&sgl[0]);
1551 dev_warn(&adapter->dev->dev, "invalid sg\n");
1555 if(IS_RAID_CH(adapter, cmd->device->channel) &&
1556 ((c & 0x1F ) == TYPE_DISK)) {
1561 /* clear result; otherwise, success returns corrupt value */
1564 /* Convert MegaRAID status to Linux error code */
1566 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1567 cmd->result |= (DID_OK << 16);
1570 case 0x02: /* ERROR_ABORTED, i.e.
1571 SCSI_STATUS_CHECK_CONDITION */
1573 /* set sense_buffer and result fields */
1574 if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1575 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1577 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1580 cmd->result = SAM_STAT_CHECK_CONDITION;
1583 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1585 memcpy(cmd->sense_buffer,
1586 epthru->reqsensearea, 14);
1588 cmd->result = SAM_STAT_CHECK_CONDITION;
1590 scsi_build_sense(cmd, 0,
1591 ABORTED_COMMAND, 0, 0);
1595 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1597 cmd->result |= (DID_BUS_BUSY << 16) | status;
1601 #if MEGA_HAVE_CLUSTERING
1603 * If TEST_UNIT_READY fails, we know
1604 * MEGA_RESERVATION_STATUS failed
1606 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1607 cmd->result |= (DID_ERROR << 16) |
1608 SAM_STAT_RESERVATION_CONFLICT;
1612 * Error code returned is 1 if Reserve or Release
1613 * failed or the input parameter is invalid
1616 (cmd->cmnd[0] == RESERVE ||
1617 cmd->cmnd[0] == RELEASE) ) {
1619 cmd->result |= (DID_ERROR << 16) |
1620 SAM_STAT_RESERVATION_CONFLICT;
1624 cmd->result |= (DID_BAD_TARGET << 16)|status;
1627 mega_free_scb(adapter, scb);
1629 /* Add Scsi_Command to end of completed queue */
1630 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1638 * Run through the list of completed requests and finish it
1641 mega_rundoneq (adapter_t *adapter)
1643 struct scsi_cmnd *cmd;
1644 struct list_head *pos;
1646 list_for_each(pos, &adapter->completed_list) {
1648 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1650 cmd = list_entry(spos, struct scsi_cmnd, SCp);
1654 INIT_LIST_HEAD(&adapter->completed_list);
1659 * Free a SCB structure
1660 * Note: We assume the scsi commands associated with this scb is not free yet.
1663 mega_free_scb(adapter_t *adapter, scb_t *scb)
1665 switch( scb->dma_type ) {
1667 case MEGA_DMA_TYPE_NONE:
1671 scsi_dma_unmap(scb->cmd);
1678 * Remove from the pending list
1680 list_del_init(&scb->list);
1682 /* Link the scb back into free list */
1683 scb->state = SCB_FREE;
1686 list_add(&scb->list, &adapter->free_list);
1691 __mega_busywait_mbox (adapter_t *adapter)
1693 volatile mbox_t *mbox = adapter->mbox;
1696 for (counter = 0; counter < 10000; counter++) {
1697 if (!mbox->m_in.busy)
1702 return -1; /* give up after 1 second */
1706 * Copies data to SGLIST
1707 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1710 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1712 struct scatterlist *sg;
1713 struct scsi_cmnd *cmd;
1720 * Copy Scatter-Gather list info into controller structure.
1722 * The number of sg elements returned must not exceed our limit
1724 sgcnt = scsi_dma_map(cmd);
1726 scb->dma_type = MEGA_SGLIST;
1728 BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1732 if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1733 sg = scsi_sglist(cmd);
1734 scb->dma_h_bulkdata = sg_dma_address(sg);
1735 *buf = (u32)scb->dma_h_bulkdata;
1736 *len = sg_dma_len(sg);
1740 scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1741 if (adapter->has_64bit_addr) {
1742 scb->sgl64[idx].address = sg_dma_address(sg);
1743 *len += scb->sgl64[idx].length = sg_dma_len(sg);
1745 scb->sgl[idx].address = sg_dma_address(sg);
1746 *len += scb->sgl[idx].length = sg_dma_len(sg);
1750 /* Reset pointer and length fields */
1751 *buf = scb->sgl_dma_addr;
1753 /* Return count of SG requests */
1761 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1762 * Enquiry3 structures for later use
1765 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1766 mega_product_info *product_info)
1770 product_info->max_commands = inquiry->adapter_info.max_commands;
1771 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1772 product_info->nchannels = inquiry->adapter_info.nchannels;
1774 for (i = 0; i < 4; i++) {
1775 product_info->fw_version[i] =
1776 inquiry->adapter_info.fw_version[i];
1778 product_info->bios_version[i] =
1779 inquiry->adapter_info.bios_version[i];
1781 enquiry3->cache_flush_interval =
1782 inquiry->adapter_info.cache_flush_interval;
1784 product_info->dram_size = inquiry->adapter_info.dram_size;
1786 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1788 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1789 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1790 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1791 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1794 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1795 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1799 mega_free_sgl(adapter_t *adapter)
1804 for(i = 0; i < adapter->max_cmds; i++) {
1806 scb = &adapter->scb_list[i];
1809 dma_free_coherent(&adapter->dev->dev,
1810 sizeof(mega_sgl64) * adapter->sglen,
1811 scb->sgl64, scb->sgl_dma_addr);
1817 dma_free_coherent(&adapter->dev->dev,
1818 sizeof(mega_passthru), scb->pthru,
1819 scb->pthru_dma_addr);
1825 dma_free_coherent(&adapter->dev->dev,
1826 sizeof(mega_ext_passthru),
1827 scb->epthru, scb->epthru_dma_addr);
1837 * Get information about the card/driver
1840 megaraid_info(struct Scsi_Host *host)
1842 static char buffer[512];
1845 adapter = (adapter_t *)host->hostdata;
1848 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1849 adapter->fw_version, adapter->product_info.max_commands,
1850 adapter->host->max_id, adapter->host->max_channel,
1851 (u32)adapter->host->max_lun);
1856 * Abort a previous SCSI request. Only commands on the pending list can be
1857 * aborted. All the commands issued to the F/W must complete.
1860 megaraid_abort(struct scsi_cmnd *cmd)
1865 adapter = (adapter_t *)cmd->device->host->hostdata;
1867 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1870 * This is required here to complete any completed requests
1871 * to be communicated over to the mid layer.
1873 mega_rundoneq(adapter);
1880 megaraid_reset(struct scsi_cmnd *cmd)
1886 adapter = (adapter_t *)cmd->device->host->hostdata;
1888 #if MEGA_HAVE_CLUSTERING
1889 mc.cmd = MEGA_CLUSTER_CMD;
1890 mc.opcode = MEGA_RESET_RESERVATIONS;
1892 if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1893 dev_warn(&adapter->dev->dev, "reservation reset failed\n");
1896 dev_info(&adapter->dev->dev, "reservation reset\n");
1900 spin_lock_irq(&adapter->lock);
1902 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1905 * This is required here to complete any completed requests
1906 * to be communicated over to the mid layer.
1908 mega_rundoneq(adapter);
1909 spin_unlock_irq(&adapter->lock);
1915 * megaraid_abort_and_reset()
1916 * @adapter: megaraid soft state
1917 * @cmd: scsi command to be aborted or reset
1918 * @aor: abort or reset flag
1920 * Try to locate the scsi command in the pending queue. If found and is not
1921 * issued to the controller, abort/reset it. Otherwise return failure
1924 megaraid_abort_and_reset(adapter_t *adapter, struct scsi_cmnd *cmd, int aor)
1926 struct list_head *pos, *next;
1929 dev_warn(&adapter->dev->dev, "%s cmd=%x <c=%d t=%d l=%d>\n",
1930 (aor == SCB_ABORT)? "ABORTING":"RESET",
1931 cmd->cmnd[0], cmd->device->channel,
1932 cmd->device->id, (u32)cmd->device->lun);
1934 if(list_empty(&adapter->pending_list))
1937 list_for_each_safe(pos, next, &adapter->pending_list) {
1939 scb = list_entry(pos, scb_t, list);
1941 if (scb->cmd == cmd) { /* Found command */
1946 * Check if this command has firmware ownership. If
1947 * yes, we cannot reset this command. Whenever f/w
1948 * completes this command, we will return appropriate
1951 if( scb->state & SCB_ISSUED ) {
1953 dev_warn(&adapter->dev->dev,
1954 "%s[%x], fw owner\n",
1955 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1963 * Not yet issued! Remove from the pending
1966 dev_warn(&adapter->dev->dev,
1967 "%s-[%x], driver owner\n",
1968 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1971 mega_free_scb(adapter, scb);
1973 if( aor == SCB_ABORT ) {
1974 cmd->result = (DID_ABORT << 16);
1977 cmd->result = (DID_RESET << 16);
1980 list_add_tail(SCSI_LIST(cmd),
1981 &adapter->completed_list);
1992 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
1994 *pdev = pci_alloc_dev(NULL);
1996 if( *pdev == NULL ) return -1;
1998 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2000 if (dma_set_mask(&(*pdev)->dev, DMA_BIT_MASK(32)) != 0) {
2009 free_local_pdev(struct pci_dev *pdev)
2015 * mega_allocate_inquiry()
2016 * @dma_handle: handle returned for dma address
2017 * @pdev: handle to pci device
2019 * allocates memory for inquiry structure
2021 static inline void *
2022 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2024 return dma_alloc_coherent(&pdev->dev, sizeof(mega_inquiry3),
2025 dma_handle, GFP_KERNEL);
2030 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2032 dma_free_coherent(&pdev->dev, sizeof(mega_inquiry3), inquiry,
2037 #ifdef CONFIG_PROC_FS
2038 /* Following code handles /proc fs */
2041 * proc_show_config()
2042 * @m: Synthetic file construction data
2045 * Display configuration information about the controller.
2048 proc_show_config(struct seq_file *m, void *v)
2051 adapter_t *adapter = m->private;
2053 seq_puts(m, MEGARAID_VERSION);
2054 if(adapter->product_info.product_name[0])
2055 seq_printf(m, "%s\n", adapter->product_info.product_name);
2057 seq_puts(m, "Controller Type: ");
2059 if( adapter->flag & BOARD_MEMMAP )
2060 seq_puts(m, "438/466/467/471/493/518/520/531/532\n");
2062 seq_puts(m, "418/428/434\n");
2064 if(adapter->flag & BOARD_40LD)
2065 seq_puts(m, "Controller Supports 40 Logical Drives\n");
2067 if(adapter->flag & BOARD_64BIT)
2068 seq_puts(m, "Controller capable of 64-bit memory addressing\n");
2069 if( adapter->has_64bit_addr )
2070 seq_puts(m, "Controller using 64-bit memory addressing\n");
2072 seq_puts(m, "Controller is not using 64-bit memory addressing\n");
2074 seq_printf(m, "Base = %08lx, Irq = %d, ",
2075 adapter->base, adapter->host->irq);
2077 seq_printf(m, "Logical Drives = %d, Channels = %d\n",
2078 adapter->numldrv, adapter->product_info.nchannels);
2080 seq_printf(m, "Version =%s:%s, DRAM = %dMb\n",
2081 adapter->fw_version, adapter->bios_version,
2082 adapter->product_info.dram_size);
2084 seq_printf(m, "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2085 adapter->product_info.max_commands, adapter->max_cmds);
2087 seq_printf(m, "support_ext_cdb = %d\n", adapter->support_ext_cdb);
2088 seq_printf(m, "support_random_del = %d\n", adapter->support_random_del);
2089 seq_printf(m, "boot_ldrv_enabled = %d\n", adapter->boot_ldrv_enabled);
2090 seq_printf(m, "boot_ldrv = %d\n", adapter->boot_ldrv);
2091 seq_printf(m, "boot_pdrv_enabled = %d\n", adapter->boot_pdrv_enabled);
2092 seq_printf(m, "boot_pdrv_ch = %d\n", adapter->boot_pdrv_ch);
2093 seq_printf(m, "boot_pdrv_tgt = %d\n", adapter->boot_pdrv_tgt);
2094 seq_printf(m, "quiescent = %d\n",
2095 atomic_read(&adapter->quiescent));
2096 seq_printf(m, "has_cluster = %d\n", adapter->has_cluster);
2098 seq_puts(m, "\nModule Parameters:\n");
2099 seq_printf(m, "max_cmd_per_lun = %d\n", max_cmd_per_lun);
2100 seq_printf(m, "max_sectors_per_io = %d\n", max_sectors_per_io);
2106 * @m: Synthetic file construction data
2109 * Display statistical information about the I/O activity.
2112 proc_show_stat(struct seq_file *m, void *v)
2114 adapter_t *adapter = m->private;
2119 seq_puts(m, "Statistical Information for this controller\n");
2120 seq_printf(m, "pend_cmds = %d\n", atomic_read(&adapter->pend_cmds));
2122 for(i = 0; i < adapter->numldrv; i++) {
2123 seq_printf(m, "Logical Drive %d:\n", i);
2124 seq_printf(m, "\tReads Issued = %lu, Writes Issued = %lu\n",
2125 adapter->nreads[i], adapter->nwrites[i]);
2126 seq_printf(m, "\tSectors Read = %lu, Sectors Written = %lu\n",
2127 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2128 seq_printf(m, "\tRead errors = %lu, Write errors = %lu\n\n",
2129 adapter->rd_errors[i], adapter->wr_errors[i]);
2132 seq_puts(m, "IO and error counters not compiled in driver.\n");
2140 * @m: Synthetic file construction data
2143 * Display mailbox information for the last command issued. This information
2144 * is good for debugging.
2147 proc_show_mbox(struct seq_file *m, void *v)
2149 adapter_t *adapter = m->private;
2150 volatile mbox_t *mbox = adapter->mbox;
2152 seq_puts(m, "Contents of Mail Box Structure\n");
2153 seq_printf(m, " Fw Command = 0x%02x\n", mbox->m_out.cmd);
2154 seq_printf(m, " Cmd Sequence = 0x%02x\n", mbox->m_out.cmdid);
2155 seq_printf(m, " No of Sectors= %04d\n", mbox->m_out.numsectors);
2156 seq_printf(m, " LBA = 0x%02x\n", mbox->m_out.lba);
2157 seq_printf(m, " DTA = 0x%08x\n", mbox->m_out.xferaddr);
2158 seq_printf(m, " Logical Drive= 0x%02x\n", mbox->m_out.logdrv);
2159 seq_printf(m, " No of SG Elmt= 0x%02x\n", mbox->m_out.numsgelements);
2160 seq_printf(m, " Busy = %01x\n", mbox->m_in.busy);
2161 seq_printf(m, " Status = 0x%02x\n", mbox->m_in.status);
2167 * proc_show_rebuild_rate()
2168 * @m: Synthetic file construction data
2171 * Display current rebuild rate
2174 proc_show_rebuild_rate(struct seq_file *m, void *v)
2176 adapter_t *adapter = m->private;
2177 dma_addr_t dma_handle;
2179 struct pci_dev *pdev;
2181 if( make_local_pdev(adapter, &pdev) != 0 )
2184 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2187 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2188 seq_puts(m, "Adapter inquiry failed.\n");
2189 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2193 if( adapter->flag & BOARD_40LD )
2194 seq_printf(m, "Rebuild Rate: [%d%%]\n",
2195 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2197 seq_printf(m, "Rebuild Rate: [%d%%]\n",
2198 ((mraid_ext_inquiry *)
2199 inquiry)->raid_inq.adapter_info.rebuild_rate);
2202 mega_free_inquiry(inquiry, dma_handle, pdev);
2204 free_local_pdev(pdev);
2210 * proc_show_battery()
2211 * @m: Synthetic file construction data
2214 * Display information about the battery module on the controller.
2217 proc_show_battery(struct seq_file *m, void *v)
2219 adapter_t *adapter = m->private;
2220 dma_addr_t dma_handle;
2222 struct pci_dev *pdev;
2225 if( make_local_pdev(adapter, &pdev) != 0 )
2228 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2231 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2232 seq_puts(m, "Adapter inquiry failed.\n");
2233 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2237 if( adapter->flag & BOARD_40LD ) {
2238 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2241 battery_status = ((mraid_ext_inquiry *)inquiry)->
2242 raid_inq.adapter_info.battery_status;
2246 * Decode the battery status
2248 seq_printf(m, "Battery Status:[%d]", battery_status);
2250 if(battery_status == MEGA_BATT_CHARGE_DONE)
2251 seq_puts(m, " Charge Done");
2253 if(battery_status & MEGA_BATT_MODULE_MISSING)
2254 seq_puts(m, " Module Missing");
2256 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2257 seq_puts(m, " Low Voltage");
2259 if(battery_status & MEGA_BATT_TEMP_HIGH)
2260 seq_puts(m, " Temperature High");
2262 if(battery_status & MEGA_BATT_PACK_MISSING)
2263 seq_puts(m, " Pack Missing");
2265 if(battery_status & MEGA_BATT_CHARGE_INPROG)
2266 seq_puts(m, " Charge In-progress");
2268 if(battery_status & MEGA_BATT_CHARGE_FAIL)
2269 seq_puts(m, " Charge Fail");
2271 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2272 seq_puts(m, " Cycles Exceeded");
2277 mega_free_inquiry(inquiry, dma_handle, pdev);
2279 free_local_pdev(pdev);
2285 * Display scsi inquiry
2288 mega_print_inquiry(struct seq_file *m, char *scsi_inq)
2292 seq_puts(m, " Vendor: ");
2293 seq_write(m, scsi_inq + 8, 8);
2294 seq_puts(m, " Model: ");
2295 seq_write(m, scsi_inq + 16, 16);
2296 seq_puts(m, " Rev: ");
2297 seq_write(m, scsi_inq + 32, 4);
2300 i = scsi_inq[0] & 0x1f;
2301 seq_printf(m, " Type: %s ", scsi_device_type(i));
2303 seq_printf(m, " ANSI SCSI revision: %02x",
2304 scsi_inq[2] & 0x07);
2306 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2307 seq_puts(m, " CCS\n");
2314 * @m: Synthetic file construction data
2315 * @adapter: pointer to our soft state
2318 * Display information about the physical drives.
2321 proc_show_pdrv(struct seq_file *m, adapter_t *adapter, int channel)
2323 dma_addr_t dma_handle;
2325 dma_addr_t scsi_inq_dma_handle;
2327 struct pci_dev *pdev;
2334 if( make_local_pdev(adapter, &pdev) != 0 )
2337 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2340 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2341 seq_puts(m, "Adapter inquiry failed.\n");
2342 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2347 scsi_inq = dma_alloc_coherent(&pdev->dev, 256, &scsi_inq_dma_handle,
2349 if( scsi_inq == NULL ) {
2350 seq_puts(m, "memory not available for scsi inq.\n");
2354 if( adapter->flag & BOARD_40LD ) {
2355 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2358 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2359 raid_inq.pdrv_info.pdrv_state;
2362 max_channels = adapter->product_info.nchannels;
2364 if( channel >= max_channels ) {
2368 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2370 i = channel*16 + tgt;
2372 state = *(pdrv_state + i);
2373 switch( state & 0x0F ) {
2375 seq_printf(m, "Channel:%2d Id:%2d State: Online",
2380 seq_printf(m, "Channel:%2d Id:%2d State: Failed",
2385 seq_printf(m, "Channel:%2d Id:%2d State: Rebuild",
2390 seq_printf(m, "Channel:%2d Id:%2d State: Hot spare",
2395 seq_printf(m, "Channel:%2d Id:%2d State: Un-configured",
2401 * This interface displays inquiries for disk drives
2402 * only. Inquries for logical drives and non-disk
2403 * devices are available through /proc/scsi/scsi
2405 memset(scsi_inq, 0, 256);
2406 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2407 scsi_inq_dma_handle) ||
2408 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2413 * Check for overflow. We print less than 240
2414 * characters for inquiry
2417 mega_print_inquiry(m, scsi_inq);
2421 dma_free_coherent(&pdev->dev, 256, scsi_inq, scsi_inq_dma_handle);
2423 mega_free_inquiry(inquiry, dma_handle, pdev);
2425 free_local_pdev(pdev);
2430 * proc_show_pdrv_ch0()
2431 * @m: Synthetic file construction data
2434 * Display information about the physical drives on physical channel 0.
2437 proc_show_pdrv_ch0(struct seq_file *m, void *v)
2439 return proc_show_pdrv(m, m->private, 0);
2444 * proc_show_pdrv_ch1()
2445 * @m: Synthetic file construction data
2448 * Display information about the physical drives on physical channel 1.
2451 proc_show_pdrv_ch1(struct seq_file *m, void *v)
2453 return proc_show_pdrv(m, m->private, 1);
2458 * proc_show_pdrv_ch2()
2459 * @m: Synthetic file construction data
2462 * Display information about the physical drives on physical channel 2.
2465 proc_show_pdrv_ch2(struct seq_file *m, void *v)
2467 return proc_show_pdrv(m, m->private, 2);
2472 * proc_show_pdrv_ch3()
2473 * @m: Synthetic file construction data
2476 * Display information about the physical drives on physical channel 3.
2479 proc_show_pdrv_ch3(struct seq_file *m, void *v)
2481 return proc_show_pdrv(m, m->private, 3);
2487 * @m: Synthetic file construction data
2488 * @adapter: pointer to our soft state
2489 * @start: starting logical drive to display
2490 * @end: ending logical drive to display
2492 * We do not print the inquiry information since its already available through
2493 * /proc/scsi/scsi interface
2496 proc_show_rdrv(struct seq_file *m, adapter_t *adapter, int start, int end )
2498 dma_addr_t dma_handle;
2499 logdrv_param *lparam;
2502 dma_addr_t disk_array_dma_handle;
2504 struct pci_dev *pdev;
2510 if( make_local_pdev(adapter, &pdev) != 0 )
2513 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2516 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2517 seq_puts(m, "Adapter inquiry failed.\n");
2518 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2522 memset(&mc, 0, sizeof(megacmd_t));
2524 if( adapter->flag & BOARD_40LD ) {
2525 array_sz = sizeof(disk_array_40ld);
2527 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2529 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2532 array_sz = sizeof(disk_array_8ld);
2534 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2535 raid_inq.logdrv_info.ldrv_state;
2537 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2538 raid_inq.logdrv_info.num_ldrv;
2541 disk_array = dma_alloc_coherent(&pdev->dev, array_sz,
2542 &disk_array_dma_handle, GFP_KERNEL);
2544 if( disk_array == NULL ) {
2545 seq_puts(m, "memory not available.\n");
2549 mc.xferaddr = (u32)disk_array_dma_handle;
2551 if( adapter->flag & BOARD_40LD ) {
2552 mc.cmd = FC_NEW_CONFIG;
2553 mc.opcode = OP_DCMD_READ_CONFIG;
2555 if( mega_internal_command(adapter, &mc, NULL) ) {
2556 seq_puts(m, "40LD read config failed.\n");
2562 mc.cmd = NEW_READ_CONFIG_8LD;
2564 if( mega_internal_command(adapter, &mc, NULL) ) {
2565 mc.cmd = READ_CONFIG_8LD;
2566 if( mega_internal_command(adapter, &mc, NULL) ) {
2567 seq_puts(m, "8LD read config failed.\n");
2573 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2575 if( adapter->flag & BOARD_40LD ) {
2577 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
2581 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
2585 * Check for overflow. We print less than 240 characters for
2586 * information about each logical drive.
2588 seq_printf(m, "Logical drive:%2d:, ", i);
2590 switch( rdrv_state[i] & 0x0F ) {
2592 seq_puts(m, "state: offline");
2595 seq_puts(m, "state: degraded");
2598 seq_puts(m, "state: optimal");
2601 seq_puts(m, "state: deleted");
2604 seq_puts(m, "state: unknown");
2609 * Check if check consistency or initialization is going on
2610 * for this logical drive.
2612 if( (rdrv_state[i] & 0xF0) == 0x20 )
2613 seq_puts(m, ", check-consistency in progress");
2614 else if( (rdrv_state[i] & 0xF0) == 0x10 )
2615 seq_puts(m, ", initialization in progress");
2619 seq_printf(m, "Span depth:%3d, ", lparam->span_depth);
2620 seq_printf(m, "RAID level:%3d, ", lparam->level);
2621 seq_printf(m, "Stripe size:%3d, ",
2622 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
2623 seq_printf(m, "Row size:%3d\n", lparam->row_size);
2625 seq_puts(m, "Read Policy: ");
2626 switch(lparam->read_ahead) {
2628 seq_puts(m, "No read ahead, ");
2631 seq_puts(m, "Read ahead, ");
2633 case ADAP_READ_AHEAD:
2634 seq_puts(m, "Adaptive, ");
2639 seq_puts(m, "Write Policy: ");
2640 switch(lparam->write_mode) {
2641 case WRMODE_WRITE_THRU:
2642 seq_puts(m, "Write thru, ");
2644 case WRMODE_WRITE_BACK:
2645 seq_puts(m, "Write back, ");
2649 seq_puts(m, "Cache Policy: ");
2650 switch(lparam->direct_io) {
2652 seq_puts(m, "Cached IO\n\n");
2655 seq_puts(m, "Direct IO\n\n");
2661 dma_free_coherent(&pdev->dev, array_sz, disk_array,
2662 disk_array_dma_handle);
2664 mega_free_inquiry(inquiry, dma_handle, pdev);
2666 free_local_pdev(pdev);
2671 * proc_show_rdrv_10()
2672 * @m: Synthetic file construction data
2675 * Display real time information about the logical drives 0 through 9.
2678 proc_show_rdrv_10(struct seq_file *m, void *v)
2680 return proc_show_rdrv(m, m->private, 0, 9);
2685 * proc_show_rdrv_20()
2686 * @m: Synthetic file construction data
2689 * Display real time information about the logical drives 0 through 9.
2692 proc_show_rdrv_20(struct seq_file *m, void *v)
2694 return proc_show_rdrv(m, m->private, 10, 19);
2699 * proc_show_rdrv_30()
2700 * @m: Synthetic file construction data
2703 * Display real time information about the logical drives 0 through 9.
2706 proc_show_rdrv_30(struct seq_file *m, void *v)
2708 return proc_show_rdrv(m, m->private, 20, 29);
2713 * proc_show_rdrv_40()
2714 * @m: Synthetic file construction data
2717 * Display real time information about the logical drives 0 through 9.
2720 proc_show_rdrv_40(struct seq_file *m, void *v)
2722 return proc_show_rdrv(m, m->private, 30, 39);
2726 * mega_create_proc_entry()
2727 * @index: index in soft state array
2728 * @parent: parent node for this /proc entry
2730 * Creates /proc entries for our controllers.
2733 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2735 adapter_t *adapter = hba_soft_state[index];
2736 struct proc_dir_entry *dir;
2739 sprintf(string, "hba%d", adapter->host->host_no);
2740 dir = proc_mkdir_data(string, 0, parent, adapter);
2742 dev_warn(&adapter->dev->dev, "proc_mkdir failed\n");
2746 proc_create_single_data("config", S_IRUSR, dir,
2747 proc_show_config, adapter);
2748 proc_create_single_data("stat", S_IRUSR, dir,
2749 proc_show_stat, adapter);
2750 proc_create_single_data("mailbox", S_IRUSR, dir,
2751 proc_show_mbox, adapter);
2752 #if MEGA_HAVE_ENH_PROC
2753 proc_create_single_data("rebuild-rate", S_IRUSR, dir,
2754 proc_show_rebuild_rate, adapter);
2755 proc_create_single_data("battery-status", S_IRUSR, dir,
2756 proc_show_battery, adapter);
2757 proc_create_single_data("diskdrives-ch0", S_IRUSR, dir,
2758 proc_show_pdrv_ch0, adapter);
2759 proc_create_single_data("diskdrives-ch1", S_IRUSR, dir,
2760 proc_show_pdrv_ch1, adapter);
2761 proc_create_single_data("diskdrives-ch2", S_IRUSR, dir,
2762 proc_show_pdrv_ch2, adapter);
2763 proc_create_single_data("diskdrives-ch3", S_IRUSR, dir,
2764 proc_show_pdrv_ch3, adapter);
2765 proc_create_single_data("raiddrives-0-9", S_IRUSR, dir,
2766 proc_show_rdrv_10, adapter);
2767 proc_create_single_data("raiddrives-10-19", S_IRUSR, dir,
2768 proc_show_rdrv_20, adapter);
2769 proc_create_single_data("raiddrives-20-29", S_IRUSR, dir,
2770 proc_show_rdrv_30, adapter);
2771 proc_create_single_data("raiddrives-30-39", S_IRUSR, dir,
2772 proc_show_rdrv_40, adapter);
2777 static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2784 * megaraid_biosparam()
2786 * Return the disk geometry for a particular disk
2789 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
2790 sector_t capacity, int geom[])
2797 /* Get pointer to host config structure */
2798 adapter = (adapter_t *)sdev->host->hostdata;
2800 if (IS_RAID_CH(adapter, sdev->channel)) {
2801 /* Default heads (64) & sectors (32) */
2804 cylinders = (ulong)capacity / (heads * sectors);
2807 * Handle extended translation size for logical drives
2810 if ((ulong)capacity >= 0x200000) {
2813 cylinders = (ulong)capacity / (heads * sectors);
2819 geom[2] = cylinders;
2822 if (scsi_partsize(bdev, capacity, geom))
2825 dev_info(&adapter->dev->dev,
2826 "invalid partition on this disk on channel %d\n",
2829 /* Default heads (64) & sectors (32) */
2832 cylinders = (ulong)capacity / (heads * sectors);
2834 /* Handle extended translation size for logical drives > 1Gb */
2835 if ((ulong)capacity >= 0x200000) {
2838 cylinders = (ulong)capacity / (heads * sectors);
2844 geom[2] = cylinders;
2852 * @adapter: pointer to our soft state
2854 * Allocate memory for the various pointers in the scb structures:
2855 * scatter-gather list pointer, passthru and extended passthru structure
2859 mega_init_scb(adapter_t *adapter)
2864 for( i = 0; i < adapter->max_cmds; i++ ) {
2866 scb = &adapter->scb_list[i];
2874 for( i = 0; i < adapter->max_cmds; i++ ) {
2876 scb = &adapter->scb_list[i];
2880 scb->sgl64 = dma_alloc_coherent(&adapter->dev->dev,
2881 sizeof(mega_sgl64) * adapter->sglen,
2882 &scb->sgl_dma_addr, GFP_KERNEL);
2884 scb->sgl = (mega_sglist *)scb->sgl64;
2887 dev_warn(&adapter->dev->dev, "RAID: Can't allocate sglist\n");
2888 mega_free_sgl(adapter);
2892 scb->pthru = dma_alloc_coherent(&adapter->dev->dev,
2893 sizeof(mega_passthru),
2894 &scb->pthru_dma_addr, GFP_KERNEL);
2897 dev_warn(&adapter->dev->dev, "RAID: Can't allocate passthru\n");
2898 mega_free_sgl(adapter);
2902 scb->epthru = dma_alloc_coherent(&adapter->dev->dev,
2903 sizeof(mega_ext_passthru),
2904 &scb->epthru_dma_addr, GFP_KERNEL);
2906 if( !scb->epthru ) {
2907 dev_warn(&adapter->dev->dev,
2908 "Can't allocate extended passthru\n");
2909 mega_free_sgl(adapter);
2914 scb->dma_type = MEGA_DMA_TYPE_NONE;
2918 * lock not required since we are loading the driver, so no
2919 * commands possible right now.
2921 scb->state = SCB_FREE;
2923 list_add(&scb->list, &adapter->free_list);
2935 * Routines for the character/ioctl interface to the driver. Find out if this
2939 megadev_open (struct inode *inode, struct file *filep)
2942 * Only allow superuser to access private ioctl interface
2944 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
2952 * @filep: Our device file
2953 * @cmd: ioctl command
2956 * ioctl entry point for our private ioctl interface. We move the data in from
2957 * the user space, prepare the command (if necessary, convert the old MIMD
2958 * ioctl to new ioctl command), and issue a synchronous command to the
2962 megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
2968 mega_passthru __user *upthru; /* user address for passthru */
2969 mega_passthru *pthru; /* copy user passthru here */
2970 dma_addr_t pthru_dma_hndl;
2971 void *data = NULL; /* data to be transferred */
2972 dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
2975 megastat_t __user *ustats = NULL;
2979 struct pci_dev *pdev;
2982 * Make sure only USCSICMD are issued through this interface.
2983 * MIMD application would still fire different command.
2985 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
2990 * Check and convert a possible MIMD command to NIT command.
2991 * mega_m_to_n() copies the data from the user space, so we do not
2992 * have to do it here.
2993 * NOTE: We will need some user address to copyout the data, therefore
2994 * the inteface layer will also provide us with the required user
2997 memset(&uioc, 0, sizeof(nitioctl_t));
2998 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3002 switch( uioc.opcode ) {
3004 case GET_DRIVER_VER:
3005 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3011 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3015 * Shucks. MIMD interface returns a positive value for number
3016 * of adapters. TODO: Change it to return 0 when there is no
3017 * applicatio using mimd interface.
3026 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3029 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3030 sizeof(struct mcontroller)) )
3040 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3043 adapter = hba_soft_state[adapno];
3045 ustats = uioc.uioc_uaddr;
3047 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3051 * Check for the validity of the logical drive number
3053 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3055 if( copy_to_user(ustats->nreads, adapter->nreads,
3056 num_ldrv*sizeof(u32)) )
3059 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3060 num_ldrv*sizeof(u32)) )
3063 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3064 num_ldrv*sizeof(u32)) )
3067 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3068 num_ldrv*sizeof(u32)) )
3071 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3072 num_ldrv*sizeof(u32)) )
3075 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3076 num_ldrv*sizeof(u32)) )
3087 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3090 adapter = hba_soft_state[adapno];
3093 * Deletion of logical drive is a special case. The adapter
3094 * should be quiescent before this command is issued.
3096 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3097 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3100 * Do we support this feature
3102 if( !adapter->support_random_del ) {
3103 dev_warn(&adapter->dev->dev, "logdrv "
3104 "delete on non-supporting F/W\n");
3109 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3112 memset(&mc, 0, sizeof(megacmd_t));
3116 rval = mega_n_to_m((void __user *)arg, &mc);
3122 * This interface only support the regular passthru commands.
3123 * Reject extended passthru and 64-bit passthru
3125 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3126 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3128 dev_warn(&adapter->dev->dev, "rejected passthru\n");
3134 * For all internal commands, the buffer must be allocated in
3135 * <4GB address range
3137 if( make_local_pdev(adapter, &pdev) != 0 )
3140 /* Is it a passthru command or a DCMD */
3141 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3142 /* Passthru commands */
3144 pthru = dma_alloc_coherent(&pdev->dev,
3145 sizeof(mega_passthru),
3146 &pthru_dma_hndl, GFP_KERNEL);
3148 if( pthru == NULL ) {
3149 free_local_pdev(pdev);
3154 * The user passthru structure
3156 upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
3159 * Copy in the user passthru here.
3161 if( copy_from_user(pthru, upthru,
3162 sizeof(mega_passthru)) ) {
3164 dma_free_coherent(&pdev->dev,
3165 sizeof(mega_passthru),
3166 pthru, pthru_dma_hndl);
3168 free_local_pdev(pdev);
3174 * Is there a data transfer
3176 if( pthru->dataxferlen ) {
3177 data = dma_alloc_coherent(&pdev->dev,
3182 if( data == NULL ) {
3183 dma_free_coherent(&pdev->dev,
3184 sizeof(mega_passthru),
3188 free_local_pdev(pdev);
3194 * Save the user address and point the kernel
3195 * address at just allocated memory
3197 uxferaddr = pthru->dataxferaddr;
3198 pthru->dataxferaddr = data_dma_hndl;
3203 * Is data coming down-stream
3205 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3209 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3210 pthru->dataxferlen) ) {
3212 goto freemem_and_return;
3216 memset(&mc, 0, sizeof(megacmd_t));
3218 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3219 mc.xferaddr = (u32)pthru_dma_hndl;
3224 mega_internal_command(adapter, &mc, pthru);
3226 rval = mega_n_to_m((void __user *)arg, &mc);
3228 if( rval ) goto freemem_and_return;
3232 * Is data going up-stream
3234 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3235 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3236 pthru->dataxferlen) ) {
3242 * Send the request sense data also, irrespective of
3243 * whether the user has asked for it or not.
3245 if (copy_to_user(upthru->reqsensearea,
3246 pthru->reqsensearea, 14))
3250 if( pthru->dataxferlen ) {
3251 dma_free_coherent(&pdev->dev,
3252 pthru->dataxferlen, data,
3256 dma_free_coherent(&pdev->dev, sizeof(mega_passthru),
3257 pthru, pthru_dma_hndl);
3259 free_local_pdev(pdev);
3267 * Is there a data transfer
3269 if( uioc.xferlen ) {
3270 data = dma_alloc_coherent(&pdev->dev,
3275 if( data == NULL ) {
3276 free_local_pdev(pdev);
3280 uxferaddr = MBOX(uioc)->xferaddr;
3284 * Is data coming down-stream
3286 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3290 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3293 dma_free_coherent(&pdev->dev,
3297 free_local_pdev(pdev);
3303 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3305 mc.xferaddr = (u32)data_dma_hndl;
3310 mega_internal_command(adapter, &mc, NULL);
3312 rval = mega_n_to_m((void __user *)arg, &mc);
3315 if( uioc.xferlen ) {
3316 dma_free_coherent(&pdev->dev,
3321 free_local_pdev(pdev);
3327 * Is data going up-stream
3329 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3330 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3337 if( uioc.xferlen ) {
3338 dma_free_coherent(&pdev->dev, uioc.xferlen,
3339 data, data_dma_hndl);
3342 free_local_pdev(pdev);
3355 megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3359 mutex_lock(&megadev_mutex);
3360 ret = megadev_ioctl(filep, cmd, arg);
3361 mutex_unlock(&megadev_mutex);
3368 * @arg: user address
3369 * @uioc: new ioctl structure
3371 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3374 * Converts the older mimd ioctl structure to newer NIT structure
3377 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3379 struct uioctl_t uioc_mimd;
3380 char signature[8] = {0};
3386 * check is the application conforms to NIT. We do not have to do much
3388 * We exploit the fact that the signature is stored in the very
3389 * beginning of the structure.
3392 if( copy_from_user(signature, arg, 7) )
3395 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3398 * NOTE NOTE: The nit ioctl is still under flux because of
3399 * change of mailbox definition, in HPE. No applications yet
3400 * use this interface and let's not have applications use this
3401 * interface till the new specifitions are in place.
3405 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3412 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3414 * Get the user ioctl structure
3416 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3421 * Get the opcode and subopcode for the commands
3423 opcode = uioc_mimd.ui.fcs.opcode;
3424 subopcode = uioc_mimd.ui.fcs.subopcode;
3429 switch (subopcode) {
3431 case MEGAIOC_QDRVRVER: /* Query driver version */
3432 uioc->opcode = GET_DRIVER_VER;
3433 uioc->uioc_uaddr = uioc_mimd.data;
3436 case MEGAIOC_QNADAP: /* Get # of adapters */
3437 uioc->opcode = GET_N_ADAP;
3438 uioc->uioc_uaddr = uioc_mimd.data;
3441 case MEGAIOC_QADAPINFO: /* Get adapter information */
3442 uioc->opcode = GET_ADAP_INFO;
3443 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3444 uioc->uioc_uaddr = uioc_mimd.data;
3456 uioc->opcode = MBOX_CMD;
3457 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3459 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3461 uioc->xferlen = uioc_mimd.ui.fcs.length;
3463 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3464 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3470 uioc->opcode = MBOX_CMD;
3471 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3473 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3476 * Choose the xferlen bigger of input and output data
3478 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3479 uioc_mimd.outlen : uioc_mimd.inlen;
3481 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3482 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3496 * @arg: user address
3497 * @mc: mailbox command
3499 * Updates the status information to the application, depending on application
3500 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3503 mega_n_to_m(void __user *arg, megacmd_t *mc)
3505 nitioctl_t __user *uiocp;
3506 megacmd_t __user *umc;
3507 mega_passthru __user *upthru;
3508 struct uioctl_t __user *uioc_mimd;
3509 char signature[8] = {0};
3512 * check is the application conforms to NIT.
3514 if( copy_from_user(signature, arg, 7) )
3517 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3521 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3524 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3526 umc = MBOX_P(uiocp);
3528 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3531 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3538 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3541 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3543 umc = (megacmd_t __user *)uioc_mimd->mbox;
3545 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3548 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3558 * MEGARAID 'FW' commands.
3562 * mega_is_bios_enabled()
3563 * @adapter: pointer to our soft state
3565 * issue command to find out if the BIOS is enabled for this controller
3568 mega_is_bios_enabled(adapter_t *adapter)
3570 struct mbox_out mbox;
3571 unsigned char *raw_mbox = (u8 *)&mbox;
3573 memset(&mbox, 0, sizeof(mbox));
3575 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3577 mbox.xferaddr = (u32)adapter->buf_dma_handle;
3579 raw_mbox[0] = IS_BIOS_ENABLED;
3580 raw_mbox[2] = GET_BIOS;
3582 issue_scb_block(adapter, raw_mbox);
3584 return *(char *)adapter->mega_buffer;
3589 * mega_enum_raid_scsi()
3590 * @adapter: pointer to our soft state
3592 * Find out what channels are RAID/SCSI. This information is used to
3593 * differentiate the virtual channels and physical channels and to support
3594 * ROMB feature and non-disk devices.
3597 mega_enum_raid_scsi(adapter_t *adapter)
3599 struct mbox_out mbox;
3600 unsigned char *raw_mbox = (u8 *)&mbox;
3603 memset(&mbox, 0, sizeof(mbox));
3606 * issue command to find out what channels are raid/scsi
3608 raw_mbox[0] = CHNL_CLASS;
3609 raw_mbox[2] = GET_CHNL_CLASS;
3611 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3613 mbox.xferaddr = (u32)adapter->buf_dma_handle;
3616 * Non-ROMB firmware fail this command, so all channels
3617 * must be shown RAID
3619 adapter->mega_ch_class = 0xFF;
3621 if(!issue_scb_block(adapter, raw_mbox)) {
3622 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3626 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
3627 if( (adapter->mega_ch_class >> i) & 0x01 ) {
3628 dev_info(&adapter->dev->dev, "channel[%d] is raid\n",
3632 dev_info(&adapter->dev->dev, "channel[%d] is scsi\n",
3642 * mega_get_boot_drv()
3643 * @adapter: pointer to our soft state
3645 * Find out which device is the boot device. Note, any logical drive or any
3646 * phyical device (e.g., a CDROM) can be designated as a boot device.
3649 mega_get_boot_drv(adapter_t *adapter)
3651 struct private_bios_data *prv_bios_data;
3652 struct mbox_out mbox;
3653 unsigned char *raw_mbox = (u8 *)&mbox;
3659 memset(&mbox, 0, sizeof(mbox));
3661 raw_mbox[0] = BIOS_PVT_DATA;
3662 raw_mbox[2] = GET_BIOS_PVT_DATA;
3664 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3666 mbox.xferaddr = (u32)adapter->buf_dma_handle;
3668 adapter->boot_ldrv_enabled = 0;
3669 adapter->boot_ldrv = 0;
3671 adapter->boot_pdrv_enabled = 0;
3672 adapter->boot_pdrv_ch = 0;
3673 adapter->boot_pdrv_tgt = 0;
3675 if(issue_scb_block(adapter, raw_mbox) == 0) {
3677 (struct private_bios_data *)adapter->mega_buffer;
3680 cksum_p = (char *)prv_bios_data;
3681 for (i = 0; i < 14; i++ ) {
3682 cksum += (u16)(*cksum_p++);
3685 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
3688 * If MSB is set, a physical drive is set as boot
3691 if( prv_bios_data->boot_drv & 0x80 ) {
3692 adapter->boot_pdrv_enabled = 1;
3693 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
3694 adapter->boot_pdrv_ch = boot_pdrv / 16;
3695 adapter->boot_pdrv_tgt = boot_pdrv % 16;
3698 adapter->boot_ldrv_enabled = 1;
3699 adapter->boot_ldrv = prv_bios_data->boot_drv;
3707 * mega_support_random_del()
3708 * @adapter: pointer to our soft state
3710 * Find out if this controller supports random deletion and addition of
3714 mega_support_random_del(adapter_t *adapter)
3716 struct mbox_out mbox;
3717 unsigned char *raw_mbox = (u8 *)&mbox;
3720 memset(&mbox, 0, sizeof(mbox));
3725 raw_mbox[0] = FC_DEL_LOGDRV;
3726 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3728 rval = issue_scb_block(adapter, raw_mbox);
3735 * mega_support_ext_cdb()
3736 * @adapter: pointer to our soft state
3738 * Find out if this firmware support cdblen > 10
3741 mega_support_ext_cdb(adapter_t *adapter)
3743 struct mbox_out mbox;
3744 unsigned char *raw_mbox = (u8 *)&mbox;
3747 memset(&mbox, 0, sizeof(mbox));
3749 * issue command to find out if controller supports extended CDBs.
3754 rval = issue_scb_block(adapter, raw_mbox);
3762 * @adapter: pointer to our soft state
3763 * @logdrv: logical drive to be deleted
3765 * Delete the specified logical drive. It is the responsibility of the user
3766 * app to let the OS know about this operation.
3769 mega_del_logdrv(adapter_t *adapter, int logdrv)
3771 unsigned long flags;
3776 * Stop sending commands to the controller, queue them internally.
3777 * When deletion is complete, ISR will flush the queue.
3779 atomic_set(&adapter->quiescent, 1);
3782 * Wait till all the issued commands are complete and there are no
3783 * commands in the pending queue
3785 while (atomic_read(&adapter->pend_cmds) > 0 ||
3786 !list_empty(&adapter->pending_list))
3787 msleep(1000); /* sleep for 1s */
3789 rval = mega_do_del_logdrv(adapter, logdrv);
3791 spin_lock_irqsave(&adapter->lock, flags);
3794 * If delete operation was successful, add 0x80 to the logical drive
3795 * ids for commands in the pending queue.
3797 if (adapter->read_ldidmap) {
3798 struct list_head *pos;
3799 list_for_each(pos, &adapter->pending_list) {
3800 scb = list_entry(pos, scb_t, list);
3801 if (scb->pthru->logdrv < 0x80 )
3802 scb->pthru->logdrv += 0x80;
3806 atomic_set(&adapter->quiescent, 0);
3808 mega_runpendq(adapter);
3810 spin_unlock_irqrestore(&adapter->lock, flags);
3817 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
3822 memset( &mc, 0, sizeof(megacmd_t));
3824 mc.cmd = FC_DEL_LOGDRV;
3825 mc.opcode = OP_DEL_LOGDRV;
3826 mc.subopcode = logdrv;
3828 rval = mega_internal_command(adapter, &mc, NULL);
3830 /* log this event */
3832 dev_warn(&adapter->dev->dev, "Delete LD-%d failed", logdrv);
3837 * After deleting first logical drive, the logical drives must be
3838 * addressed by adding 0x80 to the logical drive id.
3840 adapter->read_ldidmap = 1;
3847 * mega_get_max_sgl()
3848 * @adapter: pointer to our soft state
3850 * Find out the maximum number of scatter-gather elements supported by this
3851 * version of the firmware
3854 mega_get_max_sgl(adapter_t *adapter)
3856 struct mbox_out mbox;
3857 unsigned char *raw_mbox = (u8 *)&mbox;
3859 memset(&mbox, 0, sizeof(mbox));
3861 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3863 mbox.xferaddr = (u32)adapter->buf_dma_handle;
3865 raw_mbox[0] = MAIN_MISC_OPCODE;
3866 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3869 if( issue_scb_block(adapter, raw_mbox) ) {
3871 * f/w does not support this command. Choose the default value
3873 adapter->sglen = MIN_SGLIST;
3876 adapter->sglen = *((char *)adapter->mega_buffer);
3879 * Make sure this is not more than the resources we are
3880 * planning to allocate
3882 if ( adapter->sglen > MAX_SGLIST )
3883 adapter->sglen = MAX_SGLIST;
3891 * mega_support_cluster()
3892 * @adapter: pointer to our soft state
3894 * Find out if this firmware support cluster calls.
3897 mega_support_cluster(adapter_t *adapter)
3899 struct mbox_out mbox;
3900 unsigned char *raw_mbox = (u8 *)&mbox;
3902 memset(&mbox, 0, sizeof(mbox));
3904 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3906 mbox.xferaddr = (u32)adapter->buf_dma_handle;
3909 * Try to get the initiator id. This command will succeed iff the
3910 * clustering is available on this HBA.
3912 raw_mbox[0] = MEGA_GET_TARGET_ID;
3914 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
3917 * Cluster support available. Get the initiator target id.
3918 * Tell our id to mid-layer too.
3920 adapter->this_id = *(u32 *)adapter->mega_buffer;
3921 adapter->host->this_id = adapter->this_id;
3929 #ifdef CONFIG_PROC_FS
3932 * @adapter: pointer to our soft state
3933 * @dma_handle: DMA address of the buffer
3935 * Issue internal commands while interrupts are available.
3936 * We only issue direct mailbox commands from within the driver. ioctl()
3937 * interface using these routines can issue passthru commands.
3940 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
3944 memset(&mc, 0, sizeof(megacmd_t));
3946 if( adapter->flag & BOARD_40LD ) {
3947 mc.cmd = FC_NEW_CONFIG;
3948 mc.opcode = NC_SUBOP_ENQUIRY3;
3949 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
3952 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
3955 mc.xferaddr = (u32)dma_handle;
3957 if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
3966 * mega_internal_dev_inquiry()
3967 * @adapter: pointer to our soft state
3968 * @ch: channel for this device
3969 * @tgt: ID of this device
3970 * @buf_dma_handle: DMA address of the buffer
3972 * Issue the scsi inquiry for the specified device.
3975 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
3976 dma_addr_t buf_dma_handle)
3978 mega_passthru *pthru;
3979 dma_addr_t pthru_dma_handle;
3982 struct pci_dev *pdev;
3986 * For all internal commands, the buffer must be allocated in <4GB
3989 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
3991 pthru = dma_alloc_coherent(&pdev->dev, sizeof(mega_passthru),
3992 &pthru_dma_handle, GFP_KERNEL);
3994 if( pthru == NULL ) {
3995 free_local_pdev(pdev);
4001 pthru->reqsenselen = 14;
4002 pthru->islogical = 0;
4004 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4006 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4010 pthru->cdb[0] = INQUIRY;
4014 pthru->cdb[4] = 255;
4018 pthru->dataxferaddr = (u32)buf_dma_handle;
4019 pthru->dataxferlen = 256;
4021 memset(&mc, 0, sizeof(megacmd_t));
4023 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4024 mc.xferaddr = (u32)pthru_dma_handle;
4026 rval = mega_internal_command(adapter, &mc, pthru);
4028 dma_free_coherent(&pdev->dev, sizeof(mega_passthru), pthru,
4031 free_local_pdev(pdev);
4038 * mega_internal_command()
4039 * @adapter: pointer to our soft state
4040 * @mc: the mailbox command
4041 * @pthru: Passthru structure for DCDB commands
4043 * Issue the internal commands in interrupt mode.
4044 * The last argument is the address of the passthru structure if the command
4045 * to be fired is a passthru command
4047 * Note: parameter 'pthru' is null for non-passthru commands.
4050 mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4052 unsigned long flags;
4057 * The internal commands share one command id and hence are
4058 * serialized. This is so because we want to reserve maximum number of
4059 * available command ids for the I/O commands.
4061 mutex_lock(&adapter->int_mtx);
4063 scb = &adapter->int_scb;
4064 memset(scb, 0, sizeof(scb_t));
4066 scb->idx = CMDID_INT_CMDS;
4067 scb->state |= SCB_ACTIVE | SCB_PENDQ;
4069 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4072 * Is it a passthru command
4074 if (mc->cmd == MEGA_MBOXCMD_PASSTHRU)
4077 spin_lock_irqsave(&adapter->lock, flags);
4078 list_add_tail(&scb->list, &adapter->pending_list);
4080 * Check if the HBA is in quiescent state, e.g., during a
4081 * delete logical drive opertion. If it is, don't run
4084 if (atomic_read(&adapter->quiescent) == 0)
4085 mega_runpendq(adapter);
4086 spin_unlock_irqrestore(&adapter->lock, flags);
4088 wait_for_completion(&adapter->int_waitq);
4090 mc->status = rval = adapter->int_status;
4093 * Print a debug message for all failed commands. Applications can use
4096 if (rval && trace_level) {
4097 dev_info(&adapter->dev->dev, "cmd [%x, %x, %x] status:[%x]\n",
4098 mc->cmd, mc->opcode, mc->subopcode, rval);
4101 mutex_unlock(&adapter->int_mtx);
4105 static struct scsi_host_template megaraid_template = {
4106 .module = THIS_MODULE,
4108 .proc_name = "megaraid_legacy",
4109 .info = megaraid_info,
4110 .queuecommand = megaraid_queue,
4111 .bios_param = megaraid_biosparam,
4112 .max_sectors = MAX_SECTORS_PER_IO,
4113 .can_queue = MAX_COMMANDS,
4114 .this_id = DEFAULT_INITIATOR_ID,
4115 .sg_tablesize = MAX_SGLIST,
4116 .cmd_per_lun = DEF_CMD_PER_LUN,
4117 .eh_abort_handler = megaraid_abort,
4118 .eh_device_reset_handler = megaraid_reset,
4119 .eh_bus_reset_handler = megaraid_reset,
4120 .eh_host_reset_handler = megaraid_reset,
4125 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4127 struct Scsi_Host *host;
4129 unsigned long mega_baseport, tbase, flag = 0;
4130 u16 subsysid, subsysvid;
4131 u8 pci_bus, pci_dev_func;
4133 int error = -ENODEV;
4135 if (hba_count >= MAX_CONTROLLERS)
4138 if (pci_enable_device(pdev))
4140 pci_set_master(pdev);
4142 pci_bus = pdev->bus->number;
4143 pci_dev_func = pdev->devfn;
4146 * The megaraid3 stuff reports the ID of the Intel part which is not
4147 * remotely specific to the megaraid
4149 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4152 * Don't fall over the Compaq management cards using the same
4155 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4156 pdev->subsystem_device == 0xC000)
4157 goto out_disable_device;
4158 /* Now check the magic signature byte */
4159 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4160 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4161 goto out_disable_device;
4162 /* Ok it is probably a megaraid */
4166 * For these vendor and device ids, signature offsets are not
4167 * valid and 64 bit is implicit
4169 if (id->driver_data & BOARD_64BIT)
4170 flag |= BOARD_64BIT;
4174 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4175 if (magic64 == HBA_SIGNATURE_64BIT)
4176 flag |= BOARD_64BIT;
4179 subsysvid = pdev->subsystem_vendor;
4180 subsysid = pdev->subsystem_device;
4182 dev_notice(&pdev->dev, "found 0x%4.04x:0x%4.04x\n",
4183 id->vendor, id->device);
4185 /* Read the base port and IRQ from PCI */
4186 mega_baseport = pci_resource_start(pdev, 0);
4189 tbase = mega_baseport;
4190 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4191 flag |= BOARD_MEMMAP;
4193 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4194 dev_warn(&pdev->dev, "mem region busy!\n");
4195 goto out_disable_device;
4198 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4199 if (!mega_baseport) {
4200 dev_warn(&pdev->dev, "could not map hba memory\n");
4201 goto out_release_region;
4204 flag |= BOARD_IOMAP;
4205 mega_baseport += 0x10;
4207 if (!request_region(mega_baseport, 16, "megaraid"))
4208 goto out_disable_device;
4211 /* Initialize SCSI Host structure */
4212 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4216 adapter = (adapter_t *)host->hostdata;
4217 memset(adapter, 0, sizeof(adapter_t));
4219 dev_notice(&pdev->dev,
4220 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4221 host->host_no, mega_baseport, irq);
4223 adapter->base = mega_baseport;
4224 if (flag & BOARD_MEMMAP)
4225 adapter->mmio_base = (void __iomem *) mega_baseport;
4227 INIT_LIST_HEAD(&adapter->free_list);
4228 INIT_LIST_HEAD(&adapter->pending_list);
4229 INIT_LIST_HEAD(&adapter->completed_list);
4231 adapter->flag = flag;
4232 spin_lock_init(&adapter->lock);
4234 host->cmd_per_lun = max_cmd_per_lun;
4235 host->max_sectors = max_sectors_per_io;
4237 adapter->dev = pdev;
4238 adapter->host = host;
4240 adapter->host->irq = irq;
4242 if (flag & BOARD_MEMMAP)
4243 adapter->host->base = tbase;
4245 adapter->host->io_port = tbase;
4246 adapter->host->n_io_port = 16;
4249 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4252 * Allocate buffer to issue internal commands.
4254 adapter->mega_buffer = dma_alloc_coherent(&adapter->dev->dev,
4256 &adapter->buf_dma_handle,
4258 if (!adapter->mega_buffer) {
4259 dev_warn(&pdev->dev, "out of RAM\n");
4263 adapter->scb_list = kmalloc_array(MAX_COMMANDS, sizeof(scb_t),
4265 if (!adapter->scb_list) {
4266 dev_warn(&pdev->dev, "out of RAM\n");
4267 goto out_free_cmd_buffer;
4270 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4271 megaraid_isr_memmapped : megaraid_isr_iomapped,
4272 IRQF_SHARED, "megaraid", adapter)) {
4273 dev_warn(&pdev->dev, "Couldn't register IRQ %d!\n", irq);
4274 goto out_free_scb_list;
4277 if (mega_setup_mailbox(adapter))
4280 if (mega_query_adapter(adapter))
4284 * Have checks for some buggy f/w
4286 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4290 if (!strcmp(adapter->fw_version, "3.00") ||
4291 !strcmp(adapter->fw_version, "3.01")) {
4293 dev_warn(&pdev->dev,
4294 "Your card is a Dell PERC "
4295 "2/SC RAID controller with "
4296 "firmware\nmegaraid: 3.00 or 3.01. "
4297 "This driver is known to have "
4298 "corruption issues\nmegaraid: with "
4299 "those firmware versions on this "
4300 "specific card. In order\nmegaraid: "
4301 "to protect your data, please upgrade "
4302 "your firmware to version\nmegaraid: "
4303 "3.10 or later, available from the "
4304 "Dell Technical Support web\n"
4305 "megaraid: site at\nhttp://support."
4306 "dell.com/us/en/filelib/download/"
4307 "index.asp?fileid=2940\n"
4313 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4314 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4315 * support, since this firmware cannot handle 64 bit
4318 if ((subsysvid == PCI_VENDOR_ID_HP) &&
4319 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4323 if (!strcmp(adapter->fw_version, "H01.07") ||
4324 !strcmp(adapter->fw_version, "H01.08") ||
4325 !strcmp(adapter->fw_version, "H01.09") ) {
4326 dev_warn(&pdev->dev,
4327 "Firmware H.01.07, "
4328 "H.01.08, and H.01.09 on 1M/2M "
4330 "do not support 64 bit "
4331 "addressing.\nDISABLING "
4332 "64 bit support.\n");
4333 adapter->flag &= ~BOARD_64BIT;
4337 if (mega_is_bios_enabled(adapter))
4338 mega_hbas[hba_count].is_bios_enabled = 1;
4339 mega_hbas[hba_count].hostdata_addr = adapter;
4342 * Find out which channel is raid and which is scsi. This is
4345 mega_enum_raid_scsi(adapter);
4348 * Find out if a logical drive is set as the boot drive. If
4349 * there is one, will make that as the first logical drive.
4350 * ROMB: Do we have to boot from a physical drive. Then all
4351 * the physical drives would appear before the logical disks.
4352 * Else, all the physical drives would be exported to the mid
4353 * layer after logical drives.
4355 mega_get_boot_drv(adapter);
4357 if (adapter->boot_pdrv_enabled) {
4358 j = adapter->product_info.nchannels;
4359 for( i = 0; i < j; i++ )
4360 adapter->logdrv_chan[i] = 0;
4361 for( i = j; i < NVIRT_CHAN + j; i++ )
4362 adapter->logdrv_chan[i] = 1;
4364 for (i = 0; i < NVIRT_CHAN; i++)
4365 adapter->logdrv_chan[i] = 1;
4366 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4367 adapter->logdrv_chan[i] = 0;
4368 adapter->mega_ch_class <<= NVIRT_CHAN;
4372 * Do we support random deletion and addition of logical
4375 adapter->read_ldidmap = 0; /* set it after first logdrv
4377 adapter->support_random_del = mega_support_random_del(adapter);
4379 /* Initialize SCBs */
4380 if (mega_init_scb(adapter))
4384 * Reset the pending commands counter
4386 atomic_set(&adapter->pend_cmds, 0);
4389 * Reset the adapter quiescent flag
4391 atomic_set(&adapter->quiescent, 0);
4393 hba_soft_state[hba_count] = adapter;
4396 * Fill in the structure which needs to be passed back to the
4397 * application when it does an ioctl() for controller related
4402 mcontroller[i].base = mega_baseport;
4403 mcontroller[i].irq = irq;
4404 mcontroller[i].numldrv = adapter->numldrv;
4405 mcontroller[i].pcibus = pci_bus;
4406 mcontroller[i].pcidev = id->device;
4407 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4408 mcontroller[i].pciid = -1;
4409 mcontroller[i].pcivendor = id->vendor;
4410 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4411 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4414 /* Set the Mode of addressing to 64 bit if we can */
4415 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4416 dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
4417 adapter->has_64bit_addr = 1;
4419 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
4420 adapter->has_64bit_addr = 0;
4423 mutex_init(&adapter->int_mtx);
4424 init_completion(&adapter->int_waitq);
4426 adapter->this_id = DEFAULT_INITIATOR_ID;
4427 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4429 #if MEGA_HAVE_CLUSTERING
4431 * Is cluster support enabled on this controller
4432 * Note: In a cluster the HBAs ( the initiators ) will have
4433 * different target IDs and we cannot assume it to be 7. Call
4434 * to mega_support_cluster() will get the target ids also if
4435 * the cluster support is available
4437 adapter->has_cluster = mega_support_cluster(adapter);
4438 if (adapter->has_cluster) {
4439 dev_notice(&pdev->dev,
4440 "Cluster driver, initiator id:%d\n",
4445 pci_set_drvdata(pdev, host);
4447 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4449 error = scsi_add_host(host, &pdev->dev);
4453 scsi_scan_host(host);
4458 dma_free_coherent(&adapter->dev->dev, sizeof(mbox64_t),
4459 adapter->una_mbox64, adapter->una_mbox64_dma);
4461 free_irq(adapter->host->irq, adapter);
4463 kfree(adapter->scb_list);
4464 out_free_cmd_buffer:
4465 dma_free_coherent(&adapter->dev->dev, MEGA_BUFFER_SIZE,
4466 adapter->mega_buffer, adapter->buf_dma_handle);
4468 scsi_host_put(host);
4470 if (flag & BOARD_MEMMAP)
4471 iounmap((void *)mega_baseport);
4473 if (flag & BOARD_MEMMAP)
4474 release_mem_region(tbase, 128);
4476 release_region(mega_baseport, 16);
4478 pci_disable_device(pdev);
4484 __megaraid_shutdown(adapter_t *adapter)
4486 u_char raw_mbox[sizeof(struct mbox_out)];
4487 mbox_t *mbox = (mbox_t *)raw_mbox;
4490 /* Flush adapter cache */
4491 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4492 raw_mbox[0] = FLUSH_ADAPTER;
4494 free_irq(adapter->host->irq, adapter);
4496 /* Issue a blocking (interrupts disabled) command to the card */
4497 issue_scb_block(adapter, raw_mbox);
4499 /* Flush disks cache */
4500 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4501 raw_mbox[0] = FLUSH_SYSTEM;
4503 /* Issue a blocking (interrupts disabled) command to the card */
4504 issue_scb_block(adapter, raw_mbox);
4506 if (atomic_read(&adapter->pend_cmds) > 0)
4507 dev_warn(&adapter->dev->dev, "pending commands!!\n");
4510 * Have a delibrate delay to make sure all the caches are
4513 for (i = 0; i <= 10; i++)
4518 megaraid_remove_one(struct pci_dev *pdev)
4520 struct Scsi_Host *host = pci_get_drvdata(pdev);
4521 adapter_t *adapter = (adapter_t *)host->hostdata;
4522 char buf[12] = { 0 };
4524 scsi_remove_host(host);
4526 __megaraid_shutdown(adapter);
4528 /* Free our resources */
4529 if (adapter->flag & BOARD_MEMMAP) {
4530 iounmap((void *)adapter->base);
4531 release_mem_region(adapter->host->base, 128);
4533 release_region(adapter->base, 16);
4535 mega_free_sgl(adapter);
4537 sprintf(buf, "hba%d", adapter->host->host_no);
4538 remove_proc_subtree(buf, mega_proc_dir_entry);
4540 dma_free_coherent(&adapter->dev->dev, MEGA_BUFFER_SIZE,
4541 adapter->mega_buffer, adapter->buf_dma_handle);
4542 kfree(adapter->scb_list);
4543 dma_free_coherent(&adapter->dev->dev, sizeof(mbox64_t),
4544 adapter->una_mbox64, adapter->una_mbox64_dma);
4546 scsi_host_put(host);
4547 pci_disable_device(pdev);
4553 megaraid_shutdown(struct pci_dev *pdev)
4555 struct Scsi_Host *host = pci_get_drvdata(pdev);
4556 adapter_t *adapter = (adapter_t *)host->hostdata;
4558 __megaraid_shutdown(adapter);
4561 static struct pci_device_id megaraid_pci_tbl[] = {
4562 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4563 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4564 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
4565 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4566 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
4567 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4570 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
4572 static struct pci_driver megaraid_pci_driver = {
4573 .name = "megaraid_legacy",
4574 .id_table = megaraid_pci_tbl,
4575 .probe = megaraid_probe_one,
4576 .remove = megaraid_remove_one,
4577 .shutdown = megaraid_shutdown,
4580 static int __init megaraid_init(void)
4584 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
4585 max_cmd_per_lun = MAX_CMD_PER_LUN;
4586 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
4587 max_mbox_busy_wait = MBOX_BUSY_WAIT;
4589 #ifdef CONFIG_PROC_FS
4590 mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
4591 if (!mega_proc_dir_entry) {
4593 "megaraid: failed to create megaraid root\n");
4596 error = pci_register_driver(&megaraid_pci_driver);
4598 #ifdef CONFIG_PROC_FS
4599 remove_proc_entry("megaraid", NULL);
4605 * Register the driver as a character device, for applications
4606 * to access it for ioctls.
4607 * First argument (major) to register_chrdev implies a dynamic
4608 * major number allocation.
4610 major = register_chrdev(0, "megadev_legacy", &megadev_fops);
4613 "megaraid: failed to register char device\n");
4619 static void __exit megaraid_exit(void)
4622 * Unregister the character device interface to the driver.
4624 unregister_chrdev(major, "megadev_legacy");
4626 pci_unregister_driver(&megaraid_pci_driver);
4628 #ifdef CONFIG_PROC_FS
4629 remove_proc_entry("megaraid", NULL);
4633 module_init(megaraid_init);
4634 module_exit(megaraid_exit);
4636 /* vi: set ts=8 sw=8 tw=78: */