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.)
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
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
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"
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 = pci_alloc_consistent(adapter->dev,
137 sizeof(mbox64_t), &adapter->una_mbox64_dma);
139 if( !adapter->una_mbox64 ) return -1;
141 adapter->mbox = &adapter->una_mbox64->mbox;
143 adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
146 adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
148 align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
150 adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
153 * Register the mailbox if the controller is an io-mapped controller
155 if( adapter->flag & BOARD_IOMAP ) {
157 outb(adapter->mbox_dma & 0xFF,
158 adapter->host->io_port + MBOX_PORT0);
160 outb((adapter->mbox_dma >> 8) & 0xFF,
161 adapter->host->io_port + MBOX_PORT1);
163 outb((adapter->mbox_dma >> 16) & 0xFF,
164 adapter->host->io_port + MBOX_PORT2);
166 outb((adapter->mbox_dma >> 24) & 0xFF,
167 adapter->host->io_port + MBOX_PORT3);
169 outb(ENABLE_MBOX_BYTE,
170 adapter->host->io_port + ENABLE_MBOX_REGION);
182 * mega_query_adapter()
183 * @adapter - pointer to our soft state
185 * Issue the adapter inquiry commands to the controller and find out
186 * information and parameter about the devices attached
189 mega_query_adapter(adapter_t *adapter)
191 dma_addr_t prod_info_dma_handle;
192 mega_inquiry3 *inquiry3;
193 u8 raw_mbox[sizeof(struct mbox_out)];
197 /* Initialize adapter inquiry mailbox */
199 mbox = (mbox_t *)raw_mbox;
201 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
202 memset(&mbox->m_out, 0, sizeof(raw_mbox));
205 * Try to issue Inquiry3 command
206 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
207 * update enquiry3 structure
209 mbox->m_out.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 = pci_alloc_consistent(adapter->dev,
226 sizeof(mraid_ext_inquiry), &dma_handle);
228 if( ext_inq == NULL ) return -1;
230 inq = &ext_inq->raid_inq;
232 mbox->m_out.xferaddr = (u32)dma_handle;
234 /*issue old 0x04 command to adapter */
235 mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
237 issue_scb_block(adapter, raw_mbox);
240 * update Enquiry3 and ProductInfo structures with
241 * mraid_inquiry structure
243 mega_8_to_40ld(inq, inquiry3,
244 (mega_product_info *)&adapter->product_info);
246 pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
247 ext_inq, dma_handle);
249 } else { /*adapter supports 40ld */
250 adapter->flag |= BOARD_40LD;
253 * get product_info, which is static information and will be
256 prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
257 &adapter->product_info,
258 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
260 mbox->m_out.xferaddr = prod_info_dma_handle;
262 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
263 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
265 if ((retval = issue_scb_block(adapter, raw_mbox)))
266 dev_warn(&adapter->dev->dev,
267 "Product_info cmd failed with error: %d\n",
270 pci_unmap_single(adapter->dev, prod_info_dma_handle,
271 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
276 * kernel scans the channels from 0 to <= max_channel
278 adapter->host->max_channel =
279 adapter->product_info.nchannels + NVIRT_CHAN -1;
281 adapter->host->max_id = 16; /* max targets per channel */
283 adapter->host->max_lun = 7; /* Up to 7 luns for non disk devices */
285 adapter->host->cmd_per_lun = max_cmd_per_lun;
287 adapter->numldrv = inquiry3->num_ldrv;
289 adapter->max_cmds = adapter->product_info.max_commands;
291 if(adapter->max_cmds > MAX_COMMANDS)
292 adapter->max_cmds = MAX_COMMANDS;
294 adapter->host->can_queue = adapter->max_cmds - 1;
297 * Get the maximum number of scatter-gather elements supported by this
300 mega_get_max_sgl(adapter);
302 adapter->host->sg_tablesize = adapter->sglen;
304 /* use HP firmware and bios version encoding
305 Note: fw_version[0|1] and bios_version[0|1] were originally shifted
306 right 8 bits making them zero. This 0 value was hardcoded to fix
308 if (adapter->product_info.subsysvid == PCI_VENDOR_ID_HP) {
309 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
311 adapter->product_info.fw_version[2],
313 adapter->product_info.fw_version[1] & 0x0f,
315 adapter->product_info.fw_version[0] & 0x0f);
316 snprintf(adapter->bios_version, sizeof(adapter->fw_version),
318 adapter->product_info.bios_version[2],
320 adapter->product_info.bios_version[1] & 0x0f,
322 adapter->product_info.bios_version[0] & 0x0f);
324 memcpy(adapter->fw_version,
325 (char *)adapter->product_info.fw_version, 4);
326 adapter->fw_version[4] = 0;
328 memcpy(adapter->bios_version,
329 (char *)adapter->product_info.bios_version, 4);
331 adapter->bios_version[4] = 0;
334 dev_notice(&adapter->dev->dev, "[%s:%s] detected %d logical drives\n",
335 adapter->fw_version, adapter->bios_version, adapter->numldrv);
338 * Do we support extended (>10 bytes) cdbs
340 adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
341 if (adapter->support_ext_cdb)
342 dev_notice(&adapter->dev->dev, "supports extended CDBs\n");
350 * @adapter: pointer to our soft state
352 * Runs through the list of pending requests.
355 mega_runpendq(adapter_t *adapter)
357 if(!list_empty(&adapter->pending_list))
358 __mega_runpendq(adapter);
363 * @scmd - Issue this scsi command
364 * @done - the callback hook into the scsi mid-layer
366 * The command queuing entry point for the mid-layer.
369 megaraid_queue_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
376 adapter = (adapter_t *)scmd->device->host->hostdata;
378 scmd->scsi_done = done;
382 * Allocate and build a SCB request
383 * busy flag will be set if mega_build_cmd() command could not
384 * allocate scb. We will return non-zero status in that case.
385 * NOTE: scb can be null even though certain commands completed
386 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
387 * return 0 in that case.
390 spin_lock_irqsave(&adapter->lock, flags);
391 scb = mega_build_cmd(adapter, scmd, &busy);
395 scb->state |= SCB_PENDQ;
396 list_add_tail(&scb->list, &adapter->pending_list);
399 * Check if the HBA is in quiescent state, e.g., during a
400 * delete logical drive opertion. If it is, don't run
403 if (atomic_read(&adapter->quiescent) == 0)
404 mega_runpendq(adapter);
408 spin_unlock_irqrestore(&adapter->lock, flags);
412 static DEF_SCSI_QCMD(megaraid_queue)
415 * mega_allocate_scb()
416 * @adapter: pointer to our soft state
417 * @cmd: scsi command from the mid-layer
419 * Allocate a SCB structure. This is the central structure for controller
422 static inline scb_t *
423 mega_allocate_scb(adapter_t *adapter, struct scsi_cmnd *cmd)
425 struct list_head *head = &adapter->free_list;
428 /* Unlink command from Free List */
429 if( !list_empty(head) ) {
431 scb = list_entry(head->next, scb_t, list);
433 list_del_init(head->next);
435 scb->state = SCB_ACTIVE;
437 scb->dma_type = MEGA_DMA_TYPE_NONE;
446 * mega_get_ldrv_num()
447 * @adapter: pointer to our soft state
448 * @cmd: scsi mid layer command
449 * @channel: channel on the controller
451 * Calculate the logical drive number based on the information in scsi command
452 * and the channel number.
455 mega_get_ldrv_num(adapter_t *adapter, struct scsi_cmnd *cmd, int channel)
460 tgt = cmd->device->id;
462 if ( tgt > adapter->this_id )
463 tgt--; /* we do not get inquires for initiator id */
465 ldrv_num = (channel * 15) + tgt;
469 * If we have a logical drive with boot enabled, project it first
471 if( adapter->boot_ldrv_enabled ) {
472 if( ldrv_num == 0 ) {
473 ldrv_num = adapter->boot_ldrv;
476 if( ldrv_num <= adapter->boot_ldrv ) {
483 * If "delete logical drive" feature is enabled on this controller.
484 * Do only if at least one delete logical drive operation was done.
486 * Also, after logical drive deletion, instead of logical drive number,
487 * the value returned should be 0x80+logical drive id.
489 * These is valid only for IO commands.
492 if (adapter->support_random_del && adapter->read_ldidmap )
493 switch (cmd->cmnd[0]) {
494 case READ_6: /* fall through */
495 case WRITE_6: /* fall through */
496 case READ_10: /* fall through */
506 * @adapter: pointer to our soft state
507 * @cmd: Prepare using this scsi command
508 * @busy: busy flag if no resources
510 * Prepares a command and scatter gather list for the controller. This routine
511 * also finds out if the commands is intended for a logical drive or a
512 * physical device and prepares the controller command accordingly.
514 * We also re-order the logical drives and physical devices based on their
518 mega_build_cmd(adapter_t *adapter, struct scsi_cmnd *cmd, int *busy)
520 mega_passthru *pthru;
528 int ldrv_num = 0; /* logical drive number */
531 * We know what channels our logical drives are on - mega_find_card()
533 islogical = adapter->logdrv_chan[cmd->device->channel];
536 * The theory: If physical drive is chosen for boot, all the physical
537 * devices are exported before the logical drives, otherwise physical
538 * devices are pushed after logical drives, in which case - Kernel sees
539 * the physical devices on virtual channel which is obviously converted
540 * to actual channel on the HBA.
542 if( adapter->boot_pdrv_enabled ) {
544 /* logical channel */
545 channel = cmd->device->channel -
546 adapter->product_info.nchannels;
549 /* this is physical channel */
550 channel = cmd->device->channel;
551 target = cmd->device->id;
554 * boot from a physical disk, that disk needs to be
555 * exposed first IF both the channels are SCSI, then
556 * booting from the second channel is not allowed.
559 target = adapter->boot_pdrv_tgt;
561 else if( target == adapter->boot_pdrv_tgt ) {
568 /* this is the logical channel */
569 channel = cmd->device->channel;
572 /* physical channel */
573 channel = cmd->device->channel - NVIRT_CHAN;
574 target = cmd->device->id;
581 /* have just LUN 0 for each target on virtual channels */
582 if (cmd->device->lun) {
583 cmd->result = (DID_BAD_TARGET << 16);
588 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
591 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
592 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
595 * max_ldrv_num increases by 0x80 if some logical drive was
598 if(adapter->read_ldidmap)
599 max_ldrv_num += 0x80;
601 if(ldrv_num > max_ldrv_num ) {
602 cmd->result = (DID_BAD_TARGET << 16);
609 if( cmd->device->lun > 7) {
611 * Do not support lun >7 for physically accessed
614 cmd->result = (DID_BAD_TARGET << 16);
622 * Logical drive commands
626 switch (cmd->cmnd[0]) {
627 case TEST_UNIT_READY:
628 #if MEGA_HAVE_CLUSTERING
630 * Do we support clustering and is the support enabled
631 * If no, return success always
633 if( !adapter->has_cluster ) {
634 cmd->result = (DID_OK << 16);
639 if(!(scb = mega_allocate_scb(adapter, cmd))) {
644 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
645 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
646 scb->raw_mbox[3] = ldrv_num;
648 scb->dma_direction = PCI_DMA_NONE;
652 cmd->result = (DID_OK << 16);
659 struct scatterlist *sg;
661 sg = scsi_sglist(cmd);
662 buf = kmap_atomic(sg_page(sg)) + sg->offset;
664 memset(buf, 0, cmd->cmnd[4]);
665 kunmap_atomic(buf - sg->offset);
667 cmd->result = (DID_OK << 16);
675 if(!(adapter->flag & (1L << cmd->device->channel))) {
677 dev_notice(&adapter->dev->dev,
678 "scsi%d: scanning scsi channel %d "
679 "for logical drives\n",
680 adapter->host->host_no,
681 cmd->device->channel);
683 adapter->flag |= (1L << cmd->device->channel);
686 /* Allocate a SCB and initialize passthru */
687 if(!(scb = mega_allocate_scb(adapter, cmd))) {
693 mbox = (mbox_t *)scb->raw_mbox;
694 memset(mbox, 0, sizeof(scb->raw_mbox));
695 memset(pthru, 0, sizeof(mega_passthru));
699 pthru->reqsenselen = 14;
700 pthru->islogical = 1;
701 pthru->logdrv = ldrv_num;
702 pthru->cdblen = cmd->cmd_len;
703 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
705 if( adapter->has_64bit_addr ) {
706 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
709 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
712 scb->dma_direction = PCI_DMA_FROMDEVICE;
714 pthru->numsgelements = mega_build_sglist(adapter, scb,
715 &pthru->dataxferaddr, &pthru->dataxferlen);
717 mbox->m_out.xferaddr = scb->pthru_dma_addr;
728 /* Allocate a SCB and initialize mailbox */
729 if(!(scb = mega_allocate_scb(adapter, cmd))) {
733 mbox = (mbox_t *)scb->raw_mbox;
735 memset(mbox, 0, sizeof(scb->raw_mbox));
736 mbox->m_out.logdrv = ldrv_num;
739 * A little hack: 2nd bit is zero for all scsi read
740 * commands and is set for all scsi write commands
742 if( adapter->has_64bit_addr ) {
743 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
744 MEGA_MBOXCMD_LWRITE64:
745 MEGA_MBOXCMD_LREAD64 ;
748 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
754 * 6-byte READ(0x08) or WRITE(0x0A) cdb
756 if( cmd->cmd_len == 6 ) {
757 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
759 ((u32)cmd->cmnd[1] << 16) |
760 ((u32)cmd->cmnd[2] << 8) |
763 mbox->m_out.lba &= 0x1FFFFF;
767 * Take modulo 0x80, since the logical drive
768 * number increases by 0x80 when a logical
771 if (*cmd->cmnd == READ_6) {
772 adapter->nreads[ldrv_num%0x80]++;
773 adapter->nreadblocks[ldrv_num%0x80] +=
774 mbox->m_out.numsectors;
776 adapter->nwrites[ldrv_num%0x80]++;
777 adapter->nwriteblocks[ldrv_num%0x80] +=
778 mbox->m_out.numsectors;
784 * 10-byte READ(0x28) or WRITE(0x2A) cdb
786 if( cmd->cmd_len == 10 ) {
787 mbox->m_out.numsectors =
789 ((u32)cmd->cmnd[7] << 8);
791 ((u32)cmd->cmnd[2] << 24) |
792 ((u32)cmd->cmnd[3] << 16) |
793 ((u32)cmd->cmnd[4] << 8) |
797 if (*cmd->cmnd == READ_10) {
798 adapter->nreads[ldrv_num%0x80]++;
799 adapter->nreadblocks[ldrv_num%0x80] +=
800 mbox->m_out.numsectors;
802 adapter->nwrites[ldrv_num%0x80]++;
803 adapter->nwriteblocks[ldrv_num%0x80] +=
804 mbox->m_out.numsectors;
810 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
812 if( cmd->cmd_len == 12 ) {
814 ((u32)cmd->cmnd[2] << 24) |
815 ((u32)cmd->cmnd[3] << 16) |
816 ((u32)cmd->cmnd[4] << 8) |
819 mbox->m_out.numsectors =
820 ((u32)cmd->cmnd[6] << 24) |
821 ((u32)cmd->cmnd[7] << 16) |
822 ((u32)cmd->cmnd[8] << 8) |
826 if (*cmd->cmnd == READ_12) {
827 adapter->nreads[ldrv_num%0x80]++;
828 adapter->nreadblocks[ldrv_num%0x80] +=
829 mbox->m_out.numsectors;
831 adapter->nwrites[ldrv_num%0x80]++;
832 adapter->nwriteblocks[ldrv_num%0x80] +=
833 mbox->m_out.numsectors;
839 * If it is a read command
841 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
842 scb->dma_direction = PCI_DMA_FROMDEVICE;
845 scb->dma_direction = PCI_DMA_TODEVICE;
848 /* Calculate Scatter-Gather info */
849 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
850 (u32 *)&mbox->m_out.xferaddr, &seg);
854 #if MEGA_HAVE_CLUSTERING
855 case RESERVE: /* Fall through */
859 * Do we support clustering and is the support enabled
861 if( ! adapter->has_cluster ) {
863 cmd->result = (DID_BAD_TARGET << 16);
868 /* Allocate a SCB and initialize mailbox */
869 if(!(scb = mega_allocate_scb(adapter, cmd))) {
874 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
875 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
876 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
878 scb->raw_mbox[3] = ldrv_num;
880 scb->dma_direction = PCI_DMA_NONE;
886 cmd->result = (DID_BAD_TARGET << 16);
893 * Passthru drive commands
896 /* Allocate a SCB and initialize passthru */
897 if(!(scb = mega_allocate_scb(adapter, cmd))) {
902 mbox = (mbox_t *)scb->raw_mbox;
903 memset(mbox, 0, sizeof(scb->raw_mbox));
905 if( adapter->support_ext_cdb ) {
907 mega_prepare_extpassthru(adapter, scb, cmd,
910 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
912 mbox->m_out.xferaddr = scb->epthru_dma_addr;
917 pthru = mega_prepare_passthru(adapter, scb, cmd,
920 /* Initialize mailbox */
921 if( adapter->has_64bit_addr ) {
922 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
925 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
928 mbox->m_out.xferaddr = scb->pthru_dma_addr;
938 * mega_prepare_passthru()
939 * @adapter: pointer to our soft state
940 * @scb: our scsi control block
941 * @cmd: scsi command from the mid-layer
942 * @channel: actual channel on the controller
943 * @target: actual id on the controller.
945 * prepare a command for the scsi physical devices.
947 static mega_passthru *
948 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, struct scsi_cmnd *cmd,
949 int channel, int target)
951 mega_passthru *pthru;
954 memset(pthru, 0, sizeof (mega_passthru));
956 /* 0=6sec/1=60sec/2=10min/3=3hrs */
960 pthru->reqsenselen = 14;
961 pthru->islogical = 0;
963 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
965 pthru->target = (adapter->flag & BOARD_40LD) ?
966 (channel << 4) | target : target;
968 pthru->cdblen = cmd->cmd_len;
969 pthru->logdrv = cmd->device->lun;
971 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
973 /* Not sure about the direction */
974 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
976 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
977 switch (cmd->cmnd[0]) {
980 if(!(adapter->flag & (1L << cmd->device->channel))) {
982 dev_notice(&adapter->dev->dev,
983 "scsi%d: scanning scsi channel %d [P%d] "
984 "for physical devices\n",
985 adapter->host->host_no,
986 cmd->device->channel, channel);
988 adapter->flag |= (1L << cmd->device->channel);
992 pthru->numsgelements = mega_build_sglist(adapter, scb,
993 &pthru->dataxferaddr, &pthru->dataxferlen);
1001 * mega_prepare_extpassthru()
1002 * @adapter: pointer to our soft state
1003 * @scb: our scsi control block
1004 * @cmd: scsi command from the mid-layer
1005 * @channel: actual channel on the controller
1006 * @target: actual id on the controller.
1008 * prepare a command for the scsi physical devices. This rountine prepares
1009 * commands for devices which can take extended CDBs (>10 bytes)
1011 static mega_ext_passthru *
1012 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb,
1013 struct scsi_cmnd *cmd,
1014 int channel, int target)
1016 mega_ext_passthru *epthru;
1018 epthru = scb->epthru;
1019 memset(epthru, 0, sizeof(mega_ext_passthru));
1021 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1022 epthru->timeout = 2;
1025 epthru->reqsenselen = 14;
1026 epthru->islogical = 0;
1028 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1029 epthru->target = (adapter->flag & BOARD_40LD) ?
1030 (channel << 4) | target : target;
1032 epthru->cdblen = cmd->cmd_len;
1033 epthru->logdrv = cmd->device->lun;
1035 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1037 /* Not sure about the direction */
1038 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1040 switch(cmd->cmnd[0]) {
1043 if(!(adapter->flag & (1L << cmd->device->channel))) {
1045 dev_notice(&adapter->dev->dev,
1046 "scsi%d: scanning scsi channel %d [P%d] "
1047 "for physical devices\n",
1048 adapter->host->host_no,
1049 cmd->device->channel, channel);
1051 adapter->flag |= (1L << cmd->device->channel);
1055 epthru->numsgelements = mega_build_sglist(adapter, scb,
1056 &epthru->dataxferaddr, &epthru->dataxferlen);
1064 __mega_runpendq(adapter_t *adapter)
1067 struct list_head *pos, *next;
1069 /* Issue any pending commands to the card */
1070 list_for_each_safe(pos, next, &adapter->pending_list) {
1072 scb = list_entry(pos, scb_t, list);
1074 if( !(scb->state & SCB_ISSUED) ) {
1076 if( issue_scb(adapter, scb) != 0 )
1087 * @adapter: pointer to our soft state
1088 * @scb: scsi control block
1090 * Post a command to the card if the mailbox is available, otherwise return
1091 * busy. We also take the scb from the pending list if the mailbox is
1095 issue_scb(adapter_t *adapter, scb_t *scb)
1097 volatile mbox64_t *mbox64 = adapter->mbox64;
1098 volatile mbox_t *mbox = adapter->mbox;
1101 if(unlikely(mbox->m_in.busy)) {
1105 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1107 if(mbox->m_in.busy) return -1;
1110 /* Copy mailbox data into host structure */
1111 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1112 sizeof(struct mbox_out));
1114 mbox->m_out.cmdid = scb->idx; /* Set cmdid */
1115 mbox->m_in.busy = 1; /* Set busy */
1119 * Increment the pending queue counter
1121 atomic_inc(&adapter->pend_cmds);
1123 switch (mbox->m_out.cmd) {
1124 case MEGA_MBOXCMD_LREAD64:
1125 case MEGA_MBOXCMD_LWRITE64:
1126 case MEGA_MBOXCMD_PASSTHRU64:
1127 case MEGA_MBOXCMD_EXTPTHRU:
1128 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1129 mbox64->xfer_segment_hi = 0;
1130 mbox->m_out.xferaddr = 0xFFFFFFFF;
1133 mbox64->xfer_segment_lo = 0;
1134 mbox64->xfer_segment_hi = 0;
1140 scb->state |= SCB_ISSUED;
1142 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1143 mbox->m_in.poll = 0;
1145 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1148 irq_enable(adapter);
1149 issue_command(adapter);
1156 * Wait until the controller's mailbox is available
1159 mega_busywait_mbox (adapter_t *adapter)
1161 if (adapter->mbox->m_in.busy)
1162 return __mega_busywait_mbox(adapter);
1168 * @adapter: pointer to our soft state
1169 * @raw_mbox: the mailbox
1171 * Issue a scb in synchronous and non-interrupt mode
1174 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1176 volatile mbox64_t *mbox64 = adapter->mbox64;
1177 volatile mbox_t *mbox = adapter->mbox;
1180 /* Wait until mailbox is free */
1181 if(mega_busywait_mbox (adapter))
1182 goto bug_blocked_mailbox;
1184 /* Copy mailbox data into host structure */
1185 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1186 mbox->m_out.cmdid = 0xFE;
1187 mbox->m_in.busy = 1;
1189 switch (raw_mbox[0]) {
1190 case MEGA_MBOXCMD_LREAD64:
1191 case MEGA_MBOXCMD_LWRITE64:
1192 case MEGA_MBOXCMD_PASSTHRU64:
1193 case MEGA_MBOXCMD_EXTPTHRU:
1194 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1195 mbox64->xfer_segment_hi = 0;
1196 mbox->m_out.xferaddr = 0xFFFFFFFF;
1199 mbox64->xfer_segment_lo = 0;
1200 mbox64->xfer_segment_hi = 0;
1203 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1204 mbox->m_in.poll = 0;
1206 mbox->m_in.numstatus = 0xFF;
1207 mbox->m_in.status = 0xFF;
1208 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1210 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1213 mbox->m_in.numstatus = 0xFF;
1215 while( (volatile u8)mbox->m_in.poll != 0x77 )
1218 mbox->m_in.poll = 0;
1219 mbox->m_in.ack = 0x77;
1221 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1223 while(RDINDOOR(adapter) & 0x2)
1227 irq_disable(adapter);
1228 issue_command(adapter);
1230 while (!((byte = irq_state(adapter)) & INTR_VALID))
1233 set_irq_state(adapter, byte);
1234 irq_enable(adapter);
1238 return mbox->m_in.status;
1240 bug_blocked_mailbox:
1241 dev_warn(&adapter->dev->dev, "Blocked mailbox......!!\n");
1248 * megaraid_isr_iomapped()
1250 * @devp: pointer to our soft state
1252 * Interrupt service routine for io-mapped controllers.
1253 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1254 * and service the completed commands.
1257 megaraid_isr_iomapped(int irq, void *devp)
1259 adapter_t *adapter = devp;
1260 unsigned long flags;
1263 u8 completed[MAX_FIRMWARE_STATUS];
1269 * loop till F/W has more commands for us to complete.
1271 spin_lock_irqsave(&adapter->lock, flags);
1274 /* Check if a valid interrupt is pending */
1275 byte = irq_state(adapter);
1276 if( (byte & VALID_INTR_BYTE) == 0 ) {
1278 * No more pending commands
1282 set_irq_state(adapter, byte);
1284 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1287 adapter->mbox->m_in.numstatus = 0xFF;
1289 status = adapter->mbox->m_in.status;
1292 * decrement the pending queue counter
1294 atomic_sub(nstatus, &adapter->pend_cmds);
1296 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1299 /* Acknowledge interrupt */
1302 mega_cmd_done(adapter, completed, nstatus, status);
1304 mega_rundoneq(adapter);
1308 /* Loop through any pending requests */
1309 if(atomic_read(&adapter->quiescent) == 0) {
1310 mega_runpendq(adapter);
1317 spin_unlock_irqrestore(&adapter->lock, flags);
1319 return IRQ_RETVAL(handled);
1324 * megaraid_isr_memmapped()
1326 * @devp: pointer to our soft state
1328 * Interrupt service routine for memory-mapped controllers.
1329 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1330 * and service the completed commands.
1333 megaraid_isr_memmapped(int irq, void *devp)
1335 adapter_t *adapter = devp;
1336 unsigned long flags;
1340 u8 completed[MAX_FIRMWARE_STATUS];
1345 * loop till F/W has more commands for us to complete.
1347 spin_lock_irqsave(&adapter->lock, flags);
1350 /* Check if a valid interrupt is pending */
1351 dword = RDOUTDOOR(adapter);
1352 if(dword != 0x10001234) {
1354 * No more pending commands
1358 WROUTDOOR(adapter, 0x10001234);
1360 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1364 adapter->mbox->m_in.numstatus = 0xFF;
1366 status = adapter->mbox->m_in.status;
1369 * decrement the pending queue counter
1371 atomic_sub(nstatus, &adapter->pend_cmds);
1373 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1376 /* Acknowledge interrupt */
1377 WRINDOOR(adapter, 0x2);
1381 while( RDINDOOR(adapter) & 0x02 )
1384 mega_cmd_done(adapter, completed, nstatus, status);
1386 mega_rundoneq(adapter);
1388 /* Loop through any pending requests */
1389 if(atomic_read(&adapter->quiescent) == 0) {
1390 mega_runpendq(adapter);
1397 spin_unlock_irqrestore(&adapter->lock, flags);
1399 return IRQ_RETVAL(handled);
1403 * @adapter: pointer to our soft state
1404 * @completed: array of ids of completed commands
1405 * @nstatus: number of completed commands
1406 * @status: status of the last command completed
1408 * Complete the commands and call the scsi mid-layer callback hooks.
1411 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1413 mega_ext_passthru *epthru = NULL;
1414 struct scatterlist *sgl;
1415 struct scsi_cmnd *cmd = NULL;
1416 mega_passthru *pthru = NULL;
1417 mbox_t *mbox = NULL;
1425 * for all the commands completed, call the mid-layer callback routine
1428 for( i = 0; i < nstatus; i++ ) {
1430 cmdid = completed[i];
1433 * Only free SCBs for the commands coming down from the
1434 * mid-layer, not for which were issued internally
1436 * For internal command, restore the status returned by the
1437 * firmware so that user can interpret it.
1439 if (cmdid == CMDID_INT_CMDS) {
1440 scb = &adapter->int_scb;
1442 list_del_init(&scb->list);
1443 scb->state = SCB_FREE;
1445 adapter->int_status = status;
1446 complete(&adapter->int_waitq);
1448 scb = &adapter->scb_list[cmdid];
1451 * Make sure f/w has completed a valid command
1453 if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1454 dev_crit(&adapter->dev->dev, "invalid command "
1455 "Id %d, scb->state:%x, scsi cmd:%p\n",
1456 cmdid, scb->state, scb->cmd);
1462 * Was a abort issued for this command
1464 if( scb->state & SCB_ABORT ) {
1466 dev_warn(&adapter->dev->dev,
1467 "aborted cmd [%x] complete\n",
1470 scb->cmd->result = (DID_ABORT << 16);
1472 list_add_tail(SCSI_LIST(scb->cmd),
1473 &adapter->completed_list);
1475 mega_free_scb(adapter, scb);
1481 * Was a reset issued for this command
1483 if( scb->state & SCB_RESET ) {
1485 dev_warn(&adapter->dev->dev,
1486 "reset cmd [%x] complete\n",
1489 scb->cmd->result = (DID_RESET << 16);
1491 list_add_tail(SCSI_LIST(scb->cmd),
1492 &adapter->completed_list);
1494 mega_free_scb (adapter, scb);
1501 epthru = scb->epthru;
1502 mbox = (mbox_t *)scb->raw_mbox;
1507 int logdrv = mbox->m_out.logdrv;
1509 islogical = adapter->logdrv_chan[cmd->channel];
1511 * Maintain an error counter for the logical drive.
1512 * Some application like SNMP agent need such
1515 if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1516 cmd->cmnd[0] == READ_10 ||
1517 cmd->cmnd[0] == READ_12)) {
1519 * Logical drive number increases by 0x80 when
1520 * a logical drive is deleted
1522 adapter->rd_errors[logdrv%0x80]++;
1525 if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1526 cmd->cmnd[0] == WRITE_10 ||
1527 cmd->cmnd[0] == WRITE_12)) {
1529 * Logical drive number increases by 0x80 when
1530 * a logical drive is deleted
1532 adapter->wr_errors[logdrv%0x80]++;
1540 * Do not return the presence of hard disk on the channel so,
1541 * inquiry sent, and returned data==hard disk or removable
1542 * hard disk and not logical, request should return failure! -
1545 islogical = adapter->logdrv_chan[cmd->device->channel];
1546 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1548 sgl = scsi_sglist(cmd);
1549 if( sg_page(sgl) ) {
1550 c = *(unsigned char *) sg_virt(&sgl[0]);
1552 dev_warn(&adapter->dev->dev, "invalid sg\n");
1556 if(IS_RAID_CH(adapter, cmd->device->channel) &&
1557 ((c & 0x1F ) == TYPE_DISK)) {
1562 /* clear result; otherwise, success returns corrupt value */
1565 /* Convert MegaRAID status to Linux error code */
1567 case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1568 cmd->result |= (DID_OK << 16);
1571 case 0x02: /* ERROR_ABORTED, i.e.
1572 SCSI_STATUS_CHECK_CONDITION */
1574 /* set sense_buffer and result fields */
1575 if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1576 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1578 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1581 cmd->result = (DRIVER_SENSE << 24) |
1583 (CHECK_CONDITION << 1);
1586 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1588 memcpy(cmd->sense_buffer,
1589 epthru->reqsensearea, 14);
1591 cmd->result = (DRIVER_SENSE << 24) |
1593 (CHECK_CONDITION << 1);
1595 cmd->sense_buffer[0] = 0x70;
1596 cmd->sense_buffer[2] = ABORTED_COMMAND;
1597 cmd->result |= (CHECK_CONDITION << 1);
1602 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1604 cmd->result |= (DID_BUS_BUSY << 16) | status;
1608 #if MEGA_HAVE_CLUSTERING
1610 * If TEST_UNIT_READY fails, we know
1611 * MEGA_RESERVATION_STATUS failed
1613 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1614 cmd->result |= (DID_ERROR << 16) |
1615 (RESERVATION_CONFLICT << 1);
1619 * Error code returned is 1 if Reserve or Release
1620 * failed or the input parameter is invalid
1623 (cmd->cmnd[0] == RESERVE ||
1624 cmd->cmnd[0] == RELEASE) ) {
1626 cmd->result |= (DID_ERROR << 16) |
1627 (RESERVATION_CONFLICT << 1);
1631 cmd->result |= (DID_BAD_TARGET << 16)|status;
1634 mega_free_scb(adapter, scb);
1636 /* Add Scsi_Command to end of completed queue */
1637 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1645 * Run through the list of completed requests and finish it
1648 mega_rundoneq (adapter_t *adapter)
1650 struct scsi_cmnd *cmd;
1651 struct list_head *pos;
1653 list_for_each(pos, &adapter->completed_list) {
1655 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1657 cmd = list_entry(spos, struct scsi_cmnd, SCp);
1658 cmd->scsi_done(cmd);
1661 INIT_LIST_HEAD(&adapter->completed_list);
1666 * Free a SCB structure
1667 * Note: We assume the scsi commands associated with this scb is not free yet.
1670 mega_free_scb(adapter_t *adapter, scb_t *scb)
1672 switch( scb->dma_type ) {
1674 case MEGA_DMA_TYPE_NONE:
1678 scsi_dma_unmap(scb->cmd);
1685 * Remove from the pending list
1687 list_del_init(&scb->list);
1689 /* Link the scb back into free list */
1690 scb->state = SCB_FREE;
1693 list_add(&scb->list, &adapter->free_list);
1698 __mega_busywait_mbox (adapter_t *adapter)
1700 volatile mbox_t *mbox = adapter->mbox;
1703 for (counter = 0; counter < 10000; counter++) {
1704 if (!mbox->m_in.busy)
1709 return -1; /* give up after 1 second */
1713 * Copies data to SGLIST
1714 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1717 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1719 struct scatterlist *sg;
1720 struct scsi_cmnd *cmd;
1727 * Copy Scatter-Gather list info into controller structure.
1729 * The number of sg elements returned must not exceed our limit
1731 sgcnt = scsi_dma_map(cmd);
1733 scb->dma_type = MEGA_SGLIST;
1735 BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1739 if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1740 sg = scsi_sglist(cmd);
1741 scb->dma_h_bulkdata = sg_dma_address(sg);
1742 *buf = (u32)scb->dma_h_bulkdata;
1743 *len = sg_dma_len(sg);
1747 scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1748 if (adapter->has_64bit_addr) {
1749 scb->sgl64[idx].address = sg_dma_address(sg);
1750 *len += scb->sgl64[idx].length = sg_dma_len(sg);
1752 scb->sgl[idx].address = sg_dma_address(sg);
1753 *len += scb->sgl[idx].length = sg_dma_len(sg);
1757 /* Reset pointer and length fields */
1758 *buf = scb->sgl_dma_addr;
1760 /* Return count of SG requests */
1768 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1769 * Enquiry3 structures for later use
1772 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1773 mega_product_info *product_info)
1777 product_info->max_commands = inquiry->adapter_info.max_commands;
1778 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1779 product_info->nchannels = inquiry->adapter_info.nchannels;
1781 for (i = 0; i < 4; i++) {
1782 product_info->fw_version[i] =
1783 inquiry->adapter_info.fw_version[i];
1785 product_info->bios_version[i] =
1786 inquiry->adapter_info.bios_version[i];
1788 enquiry3->cache_flush_interval =
1789 inquiry->adapter_info.cache_flush_interval;
1791 product_info->dram_size = inquiry->adapter_info.dram_size;
1793 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1795 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1796 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1797 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1798 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1801 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1802 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1806 mega_free_sgl(adapter_t *adapter)
1811 for(i = 0; i < adapter->max_cmds; i++) {
1813 scb = &adapter->scb_list[i];
1816 pci_free_consistent(adapter->dev,
1817 sizeof(mega_sgl64) * adapter->sglen,
1825 pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1826 scb->pthru, scb->pthru_dma_addr);
1832 pci_free_consistent(adapter->dev,
1833 sizeof(mega_ext_passthru),
1834 scb->epthru, scb->epthru_dma_addr);
1844 * Get information about the card/driver
1847 megaraid_info(struct Scsi_Host *host)
1849 static char buffer[512];
1852 adapter = (adapter_t *)host->hostdata;
1855 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1856 adapter->fw_version, adapter->product_info.max_commands,
1857 adapter->host->max_id, adapter->host->max_channel,
1858 (u32)adapter->host->max_lun);
1863 * Abort a previous SCSI request. Only commands on the pending list can be
1864 * aborted. All the commands issued to the F/W must complete.
1867 megaraid_abort(struct scsi_cmnd *cmd)
1872 adapter = (adapter_t *)cmd->device->host->hostdata;
1874 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1877 * This is required here to complete any completed requests
1878 * to be communicated over to the mid layer.
1880 mega_rundoneq(adapter);
1887 megaraid_reset(struct scsi_cmnd *cmd)
1893 adapter = (adapter_t *)cmd->device->host->hostdata;
1895 #if MEGA_HAVE_CLUSTERING
1896 mc.cmd = MEGA_CLUSTER_CMD;
1897 mc.opcode = MEGA_RESET_RESERVATIONS;
1899 if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1900 dev_warn(&adapter->dev->dev, "reservation reset failed\n");
1903 dev_info(&adapter->dev->dev, "reservation reset\n");
1907 spin_lock_irq(&adapter->lock);
1909 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1912 * This is required here to complete any completed requests
1913 * to be communicated over to the mid layer.
1915 mega_rundoneq(adapter);
1916 spin_unlock_irq(&adapter->lock);
1922 * megaraid_abort_and_reset()
1923 * @adapter: megaraid soft state
1924 * @cmd: scsi command to be aborted or reset
1925 * @aor: abort or reset flag
1927 * Try to locate the scsi command in the pending queue. If found and is not
1928 * issued to the controller, abort/reset it. Otherwise return failure
1931 megaraid_abort_and_reset(adapter_t *adapter, struct scsi_cmnd *cmd, int aor)
1933 struct list_head *pos, *next;
1936 dev_warn(&adapter->dev->dev, "%s cmd=%x <c=%d t=%d l=%d>\n",
1937 (aor == SCB_ABORT)? "ABORTING":"RESET",
1938 cmd->cmnd[0], cmd->device->channel,
1939 cmd->device->id, (u32)cmd->device->lun);
1941 if(list_empty(&adapter->pending_list))
1944 list_for_each_safe(pos, next, &adapter->pending_list) {
1946 scb = list_entry(pos, scb_t, list);
1948 if (scb->cmd == cmd) { /* Found command */
1953 * Check if this command has firmware ownership. If
1954 * yes, we cannot reset this command. Whenever f/w
1955 * completes this command, we will return appropriate
1958 if( scb->state & SCB_ISSUED ) {
1960 dev_warn(&adapter->dev->dev,
1961 "%s[%x], fw owner\n",
1962 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1970 * Not yet issued! Remove from the pending
1973 dev_warn(&adapter->dev->dev,
1974 "%s-[%x], driver owner\n",
1975 (aor==SCB_ABORT) ? "ABORTING":"RESET",
1978 mega_free_scb(adapter, scb);
1980 if( aor == SCB_ABORT ) {
1981 cmd->result = (DID_ABORT << 16);
1984 cmd->result = (DID_RESET << 16);
1987 list_add_tail(SCSI_LIST(cmd),
1988 &adapter->completed_list);
1999 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2001 *pdev = pci_alloc_dev(NULL);
2003 if( *pdev == NULL ) return -1;
2005 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2007 if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
2016 free_local_pdev(struct pci_dev *pdev)
2022 * mega_allocate_inquiry()
2023 * @dma_handle: handle returned for dma address
2024 * @pdev: handle to pci device
2026 * allocates memory for inquiry structure
2028 static inline void *
2029 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2031 return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2036 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2038 pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2042 #ifdef CONFIG_PROC_FS
2043 /* Following code handles /proc fs */
2046 * proc_show_config()
2047 * @m: Synthetic file construction data
2050 * Display configuration information about the controller.
2053 proc_show_config(struct seq_file *m, void *v)
2056 adapter_t *adapter = m->private;
2058 seq_puts(m, MEGARAID_VERSION);
2059 if(adapter->product_info.product_name[0])
2060 seq_printf(m, "%s\n", adapter->product_info.product_name);
2062 seq_puts(m, "Controller Type: ");
2064 if( adapter->flag & BOARD_MEMMAP )
2065 seq_puts(m, "438/466/467/471/493/518/520/531/532\n");
2067 seq_puts(m, "418/428/434\n");
2069 if(adapter->flag & BOARD_40LD)
2070 seq_puts(m, "Controller Supports 40 Logical Drives\n");
2072 if(adapter->flag & BOARD_64BIT)
2073 seq_puts(m, "Controller capable of 64-bit memory addressing\n");
2074 if( adapter->has_64bit_addr )
2075 seq_puts(m, "Controller using 64-bit memory addressing\n");
2077 seq_puts(m, "Controller is not using 64-bit memory addressing\n");
2079 seq_printf(m, "Base = %08lx, Irq = %d, ",
2080 adapter->base, adapter->host->irq);
2082 seq_printf(m, "Logical Drives = %d, Channels = %d\n",
2083 adapter->numldrv, adapter->product_info.nchannels);
2085 seq_printf(m, "Version =%s:%s, DRAM = %dMb\n",
2086 adapter->fw_version, adapter->bios_version,
2087 adapter->product_info.dram_size);
2089 seq_printf(m, "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2090 adapter->product_info.max_commands, adapter->max_cmds);
2092 seq_printf(m, "support_ext_cdb = %d\n", adapter->support_ext_cdb);
2093 seq_printf(m, "support_random_del = %d\n", adapter->support_random_del);
2094 seq_printf(m, "boot_ldrv_enabled = %d\n", adapter->boot_ldrv_enabled);
2095 seq_printf(m, "boot_ldrv = %d\n", adapter->boot_ldrv);
2096 seq_printf(m, "boot_pdrv_enabled = %d\n", adapter->boot_pdrv_enabled);
2097 seq_printf(m, "boot_pdrv_ch = %d\n", adapter->boot_pdrv_ch);
2098 seq_printf(m, "boot_pdrv_tgt = %d\n", adapter->boot_pdrv_tgt);
2099 seq_printf(m, "quiescent = %d\n",
2100 atomic_read(&adapter->quiescent));
2101 seq_printf(m, "has_cluster = %d\n", adapter->has_cluster);
2103 seq_puts(m, "\nModule Parameters:\n");
2104 seq_printf(m, "max_cmd_per_lun = %d\n", max_cmd_per_lun);
2105 seq_printf(m, "max_sectors_per_io = %d\n", max_sectors_per_io);
2111 * @m: Synthetic file construction data
2114 * Display statistical information about the I/O activity.
2117 proc_show_stat(struct seq_file *m, void *v)
2119 adapter_t *adapter = m->private;
2124 seq_puts(m, "Statistical Information for this controller\n");
2125 seq_printf(m, "pend_cmds = %d\n", atomic_read(&adapter->pend_cmds));
2127 for(i = 0; i < adapter->numldrv; i++) {
2128 seq_printf(m, "Logical Drive %d:\n", i);
2129 seq_printf(m, "\tReads Issued = %lu, Writes Issued = %lu\n",
2130 adapter->nreads[i], adapter->nwrites[i]);
2131 seq_printf(m, "\tSectors Read = %lu, Sectors Written = %lu\n",
2132 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2133 seq_printf(m, "\tRead errors = %lu, Write errors = %lu\n\n",
2134 adapter->rd_errors[i], adapter->wr_errors[i]);
2137 seq_puts(m, "IO and error counters not compiled in driver.\n");
2145 * @m: Synthetic file construction data
2148 * Display mailbox information for the last command issued. This information
2149 * is good for debugging.
2152 proc_show_mbox(struct seq_file *m, void *v)
2154 adapter_t *adapter = m->private;
2155 volatile mbox_t *mbox = adapter->mbox;
2157 seq_puts(m, "Contents of Mail Box Structure\n");
2158 seq_printf(m, " Fw Command = 0x%02x\n", mbox->m_out.cmd);
2159 seq_printf(m, " Cmd Sequence = 0x%02x\n", mbox->m_out.cmdid);
2160 seq_printf(m, " No of Sectors= %04d\n", mbox->m_out.numsectors);
2161 seq_printf(m, " LBA = 0x%02x\n", mbox->m_out.lba);
2162 seq_printf(m, " DTA = 0x%08x\n", mbox->m_out.xferaddr);
2163 seq_printf(m, " Logical Drive= 0x%02x\n", mbox->m_out.logdrv);
2164 seq_printf(m, " No of SG Elmt= 0x%02x\n", mbox->m_out.numsgelements);
2165 seq_printf(m, " Busy = %01x\n", mbox->m_in.busy);
2166 seq_printf(m, " Status = 0x%02x\n", mbox->m_in.status);
2172 * proc_show_rebuild_rate()
2173 * @m: Synthetic file construction data
2176 * Display current rebuild rate
2179 proc_show_rebuild_rate(struct seq_file *m, void *v)
2181 adapter_t *adapter = m->private;
2182 dma_addr_t dma_handle;
2184 struct pci_dev *pdev;
2186 if( make_local_pdev(adapter, &pdev) != 0 )
2189 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2192 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2193 seq_puts(m, "Adapter inquiry failed.\n");
2194 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2198 if( adapter->flag & BOARD_40LD )
2199 seq_printf(m, "Rebuild Rate: [%d%%]\n",
2200 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2202 seq_printf(m, "Rebuild Rate: [%d%%]\n",
2203 ((mraid_ext_inquiry *)
2204 inquiry)->raid_inq.adapter_info.rebuild_rate);
2207 mega_free_inquiry(inquiry, dma_handle, pdev);
2209 free_local_pdev(pdev);
2215 * proc_show_battery()
2216 * @m: Synthetic file construction data
2219 * Display information about the battery module on the controller.
2222 proc_show_battery(struct seq_file *m, void *v)
2224 adapter_t *adapter = m->private;
2225 dma_addr_t dma_handle;
2227 struct pci_dev *pdev;
2230 if( make_local_pdev(adapter, &pdev) != 0 )
2233 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2236 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2237 seq_puts(m, "Adapter inquiry failed.\n");
2238 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2242 if( adapter->flag & BOARD_40LD ) {
2243 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2246 battery_status = ((mraid_ext_inquiry *)inquiry)->
2247 raid_inq.adapter_info.battery_status;
2251 * Decode the battery status
2253 seq_printf(m, "Battery Status:[%d]", battery_status);
2255 if(battery_status == MEGA_BATT_CHARGE_DONE)
2256 seq_puts(m, " Charge Done");
2258 if(battery_status & MEGA_BATT_MODULE_MISSING)
2259 seq_puts(m, " Module Missing");
2261 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2262 seq_puts(m, " Low Voltage");
2264 if(battery_status & MEGA_BATT_TEMP_HIGH)
2265 seq_puts(m, " Temperature High");
2267 if(battery_status & MEGA_BATT_PACK_MISSING)
2268 seq_puts(m, " Pack Missing");
2270 if(battery_status & MEGA_BATT_CHARGE_INPROG)
2271 seq_puts(m, " Charge In-progress");
2273 if(battery_status & MEGA_BATT_CHARGE_FAIL)
2274 seq_puts(m, " Charge Fail");
2276 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2277 seq_puts(m, " Cycles Exceeded");
2282 mega_free_inquiry(inquiry, dma_handle, pdev);
2284 free_local_pdev(pdev);
2290 * Display scsi inquiry
2293 mega_print_inquiry(struct seq_file *m, char *scsi_inq)
2297 seq_puts(m, " Vendor: ");
2298 seq_write(m, scsi_inq + 8, 8);
2299 seq_puts(m, " Model: ");
2300 seq_write(m, scsi_inq + 16, 16);
2301 seq_puts(m, " Rev: ");
2302 seq_write(m, scsi_inq + 32, 4);
2305 i = scsi_inq[0] & 0x1f;
2306 seq_printf(m, " Type: %s ", scsi_device_type(i));
2308 seq_printf(m, " ANSI SCSI revision: %02x",
2309 scsi_inq[2] & 0x07);
2311 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2312 seq_puts(m, " CCS\n");
2319 * @m: Synthetic file construction data
2320 * @adapter: pointer to our soft state
2323 * Display information about the physical drives.
2326 proc_show_pdrv(struct seq_file *m, adapter_t *adapter, int channel)
2328 dma_addr_t dma_handle;
2330 dma_addr_t scsi_inq_dma_handle;
2332 struct pci_dev *pdev;
2339 if( make_local_pdev(adapter, &pdev) != 0 )
2342 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2345 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2346 seq_puts(m, "Adapter inquiry failed.\n");
2347 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2352 scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2353 if( scsi_inq == NULL ) {
2354 seq_puts(m, "memory not available for scsi inq.\n");
2358 if( adapter->flag & BOARD_40LD ) {
2359 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2362 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2363 raid_inq.pdrv_info.pdrv_state;
2366 max_channels = adapter->product_info.nchannels;
2368 if( channel >= max_channels ) {
2372 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2374 i = channel*16 + tgt;
2376 state = *(pdrv_state + i);
2377 switch( state & 0x0F ) {
2379 seq_printf(m, "Channel:%2d Id:%2d State: Online",
2384 seq_printf(m, "Channel:%2d Id:%2d State: Failed",
2389 seq_printf(m, "Channel:%2d Id:%2d State: Rebuild",
2394 seq_printf(m, "Channel:%2d Id:%2d State: Hot spare",
2399 seq_printf(m, "Channel:%2d Id:%2d State: Un-configured",
2405 * This interface displays inquiries for disk drives
2406 * only. Inquries for logical drives and non-disk
2407 * devices are available through /proc/scsi/scsi
2409 memset(scsi_inq, 0, 256);
2410 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2411 scsi_inq_dma_handle) ||
2412 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2417 * Check for overflow. We print less than 240
2418 * characters for inquiry
2421 mega_print_inquiry(m, scsi_inq);
2425 pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2427 mega_free_inquiry(inquiry, dma_handle, pdev);
2429 free_local_pdev(pdev);
2434 * proc_show_pdrv_ch0()
2435 * @m: Synthetic file construction data
2438 * Display information about the physical drives on physical channel 0.
2441 proc_show_pdrv_ch0(struct seq_file *m, void *v)
2443 return proc_show_pdrv(m, m->private, 0);
2448 * proc_show_pdrv_ch1()
2449 * @m: Synthetic file construction data
2452 * Display information about the physical drives on physical channel 1.
2455 proc_show_pdrv_ch1(struct seq_file *m, void *v)
2457 return proc_show_pdrv(m, m->private, 1);
2462 * proc_show_pdrv_ch2()
2463 * @m: Synthetic file construction data
2466 * Display information about the physical drives on physical channel 2.
2469 proc_show_pdrv_ch2(struct seq_file *m, void *v)
2471 return proc_show_pdrv(m, m->private, 2);
2476 * proc_show_pdrv_ch3()
2477 * @m: Synthetic file construction data
2480 * Display information about the physical drives on physical channel 3.
2483 proc_show_pdrv_ch3(struct seq_file *m, void *v)
2485 return proc_show_pdrv(m, m->private, 3);
2491 * @m: Synthetic file construction data
2492 * @adapter: pointer to our soft state
2493 * @start: starting logical drive to display
2494 * @end: ending logical drive to display
2496 * We do not print the inquiry information since its already available through
2497 * /proc/scsi/scsi interface
2500 proc_show_rdrv(struct seq_file *m, adapter_t *adapter, int start, int end )
2502 dma_addr_t dma_handle;
2503 logdrv_param *lparam;
2506 dma_addr_t disk_array_dma_handle;
2508 struct pci_dev *pdev;
2514 if( make_local_pdev(adapter, &pdev) != 0 )
2517 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2520 if( mega_adapinq(adapter, dma_handle) != 0 ) {
2521 seq_puts(m, "Adapter inquiry failed.\n");
2522 dev_warn(&adapter->dev->dev, "inquiry failed\n");
2526 memset(&mc, 0, sizeof(megacmd_t));
2528 if( adapter->flag & BOARD_40LD ) {
2529 array_sz = sizeof(disk_array_40ld);
2531 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2533 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2536 array_sz = sizeof(disk_array_8ld);
2538 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2539 raid_inq.logdrv_info.ldrv_state;
2541 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2542 raid_inq.logdrv_info.num_ldrv;
2545 disk_array = pci_alloc_consistent(pdev, array_sz,
2546 &disk_array_dma_handle);
2548 if( disk_array == NULL ) {
2549 seq_puts(m, "memory not available.\n");
2553 mc.xferaddr = (u32)disk_array_dma_handle;
2555 if( adapter->flag & BOARD_40LD ) {
2556 mc.cmd = FC_NEW_CONFIG;
2557 mc.opcode = OP_DCMD_READ_CONFIG;
2559 if( mega_internal_command(adapter, &mc, NULL) ) {
2560 seq_puts(m, "40LD read config failed.\n");
2566 mc.cmd = NEW_READ_CONFIG_8LD;
2568 if( mega_internal_command(adapter, &mc, NULL) ) {
2569 mc.cmd = READ_CONFIG_8LD;
2570 if( mega_internal_command(adapter, &mc, NULL) ) {
2571 seq_puts(m, "8LD read config failed.\n");
2577 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2579 if( adapter->flag & BOARD_40LD ) {
2581 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
2585 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
2589 * Check for overflow. We print less than 240 characters for
2590 * information about each logical drive.
2592 seq_printf(m, "Logical drive:%2d:, ", i);
2594 switch( rdrv_state[i] & 0x0F ) {
2596 seq_puts(m, "state: offline");
2599 seq_puts(m, "state: degraded");
2602 seq_puts(m, "state: optimal");
2605 seq_puts(m, "state: deleted");
2608 seq_puts(m, "state: unknown");
2613 * Check if check consistency or initialization is going on
2614 * for this logical drive.
2616 if( (rdrv_state[i] & 0xF0) == 0x20 )
2617 seq_puts(m, ", check-consistency in progress");
2618 else if( (rdrv_state[i] & 0xF0) == 0x10 )
2619 seq_puts(m, ", initialization in progress");
2623 seq_printf(m, "Span depth:%3d, ", lparam->span_depth);
2624 seq_printf(m, "RAID level:%3d, ", lparam->level);
2625 seq_printf(m, "Stripe size:%3d, ",
2626 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
2627 seq_printf(m, "Row size:%3d\n", lparam->row_size);
2629 seq_puts(m, "Read Policy: ");
2630 switch(lparam->read_ahead) {
2632 seq_puts(m, "No read ahead, ");
2635 seq_puts(m, "Read ahead, ");
2637 case ADAP_READ_AHEAD:
2638 seq_puts(m, "Adaptive, ");
2643 seq_puts(m, "Write Policy: ");
2644 switch(lparam->write_mode) {
2645 case WRMODE_WRITE_THRU:
2646 seq_puts(m, "Write thru, ");
2648 case WRMODE_WRITE_BACK:
2649 seq_puts(m, "Write back, ");
2653 seq_puts(m, "Cache Policy: ");
2654 switch(lparam->direct_io) {
2656 seq_puts(m, "Cached IO\n\n");
2659 seq_puts(m, "Direct IO\n\n");
2665 pci_free_consistent(pdev, array_sz, disk_array,
2666 disk_array_dma_handle);
2668 mega_free_inquiry(inquiry, dma_handle, pdev);
2670 free_local_pdev(pdev);
2675 * proc_show_rdrv_10()
2676 * @m: Synthetic file construction data
2679 * Display real time information about the logical drives 0 through 9.
2682 proc_show_rdrv_10(struct seq_file *m, void *v)
2684 return proc_show_rdrv(m, m->private, 0, 9);
2689 * proc_show_rdrv_20()
2690 * @m: Synthetic file construction data
2693 * Display real time information about the logical drives 0 through 9.
2696 proc_show_rdrv_20(struct seq_file *m, void *v)
2698 return proc_show_rdrv(m, m->private, 10, 19);
2703 * proc_show_rdrv_30()
2704 * @m: Synthetic file construction data
2707 * Display real time information about the logical drives 0 through 9.
2710 proc_show_rdrv_30(struct seq_file *m, void *v)
2712 return proc_show_rdrv(m, m->private, 20, 29);
2717 * proc_show_rdrv_40()
2718 * @m: Synthetic file construction data
2721 * Display real time information about the logical drives 0 through 9.
2724 proc_show_rdrv_40(struct seq_file *m, void *v)
2726 return proc_show_rdrv(m, m->private, 30, 39);
2730 * mega_create_proc_entry()
2731 * @index: index in soft state array
2732 * @parent: parent node for this /proc entry
2734 * Creates /proc entries for our controllers.
2737 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2739 adapter_t *adapter = hba_soft_state[index];
2740 struct proc_dir_entry *dir;
2743 sprintf(string, "hba%d", adapter->host->host_no);
2744 dir = proc_mkdir_data(string, 0, parent, adapter);
2746 dev_warn(&adapter->dev->dev, "proc_mkdir failed\n");
2750 proc_create_single_data("config", S_IRUSR, dir,
2751 proc_show_config, adapter);
2752 proc_create_single_data("stat", S_IRUSR, dir,
2753 proc_show_stat, adapter);
2754 proc_create_single_data("mailbox", S_IRUSR, dir,
2755 proc_show_mbox, adapter);
2756 #if MEGA_HAVE_ENH_PROC
2757 proc_create_single_data("rebuild-rate", S_IRUSR, dir,
2758 proc_show_rebuild_rate, adapter);
2759 proc_create_single_data("battery-status", S_IRUSR, dir,
2760 proc_show_battery, adapter);
2761 proc_create_single_data("diskdrives-ch0", S_IRUSR, dir,
2762 proc_show_pdrv_ch0, adapter);
2763 proc_create_single_data("diskdrives-ch1", S_IRUSR, dir,
2764 proc_show_pdrv_ch1, adapter);
2765 proc_create_single_data("diskdrives-ch2", S_IRUSR, dir,
2766 proc_show_pdrv_ch2, adapter);
2767 proc_create_single_data("diskdrives-ch3", S_IRUSR, dir,
2768 proc_show_pdrv_ch3, adapter);
2769 proc_create_single_data("raiddrives-0-9", S_IRUSR, dir,
2770 proc_show_rdrv_10, adapter);
2771 proc_create_single_data("raiddrives-10-19", S_IRUSR, dir,
2772 proc_show_rdrv_20, adapter);
2773 proc_create_single_data("raiddrives-20-29", S_IRUSR, dir,
2774 proc_show_rdrv_30, adapter);
2775 proc_create_single_data("raiddrives-30-39", S_IRUSR, dir,
2776 proc_show_rdrv_40, adapter);
2781 static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2788 * megaraid_biosparam()
2790 * Return the disk geometry for a particular disk
2793 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
2794 sector_t capacity, int geom[])
2801 /* Get pointer to host config structure */
2802 adapter = (adapter_t *)sdev->host->hostdata;
2804 if (IS_RAID_CH(adapter, sdev->channel)) {
2805 /* Default heads (64) & sectors (32) */
2808 cylinders = (ulong)capacity / (heads * sectors);
2811 * Handle extended translation size for logical drives
2814 if ((ulong)capacity >= 0x200000) {
2817 cylinders = (ulong)capacity / (heads * sectors);
2823 geom[2] = cylinders;
2826 if (scsi_partsize(bdev, capacity, geom))
2829 dev_info(&adapter->dev->dev,
2830 "invalid partition on this disk on channel %d\n",
2833 /* Default heads (64) & sectors (32) */
2836 cylinders = (ulong)capacity / (heads * sectors);
2838 /* Handle extended translation size for logical drives > 1Gb */
2839 if ((ulong)capacity >= 0x200000) {
2842 cylinders = (ulong)capacity / (heads * sectors);
2848 geom[2] = cylinders;
2856 * @adapter: pointer to our soft state
2858 * Allocate memory for the various pointers in the scb structures:
2859 * scatter-gather list pointer, passthru and extended passthru structure
2863 mega_init_scb(adapter_t *adapter)
2868 for( i = 0; i < adapter->max_cmds; i++ ) {
2870 scb = &adapter->scb_list[i];
2878 for( i = 0; i < adapter->max_cmds; i++ ) {
2880 scb = &adapter->scb_list[i];
2884 scb->sgl64 = pci_alloc_consistent(adapter->dev,
2885 sizeof(mega_sgl64) * adapter->sglen,
2886 &scb->sgl_dma_addr);
2888 scb->sgl = (mega_sglist *)scb->sgl64;
2891 dev_warn(&adapter->dev->dev, "RAID: Can't allocate sglist\n");
2892 mega_free_sgl(adapter);
2896 scb->pthru = pci_alloc_consistent(adapter->dev,
2897 sizeof(mega_passthru),
2898 &scb->pthru_dma_addr);
2901 dev_warn(&adapter->dev->dev, "RAID: Can't allocate passthru\n");
2902 mega_free_sgl(adapter);
2906 scb->epthru = pci_alloc_consistent(adapter->dev,
2907 sizeof(mega_ext_passthru),
2908 &scb->epthru_dma_addr);
2910 if( !scb->epthru ) {
2911 dev_warn(&adapter->dev->dev,
2912 "Can't allocate extended passthru\n");
2913 mega_free_sgl(adapter);
2918 scb->dma_type = MEGA_DMA_TYPE_NONE;
2922 * lock not required since we are loading the driver, so no
2923 * commands possible right now.
2925 scb->state = SCB_FREE;
2927 list_add(&scb->list, &adapter->free_list);
2939 * Routines for the character/ioctl interface to the driver. Find out if this
2943 megadev_open (struct inode *inode, struct file *filep)
2946 * Only allow superuser to access private ioctl interface
2948 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
2956 * @filep: Our device file
2957 * @cmd: ioctl command
2960 * ioctl entry point for our private ioctl interface. We move the data in from
2961 * the user space, prepare the command (if necessary, convert the old MIMD
2962 * ioctl to new ioctl command), and issue a synchronous command to the
2966 megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
2972 mega_passthru __user *upthru; /* user address for passthru */
2973 mega_passthru *pthru; /* copy user passthru here */
2974 dma_addr_t pthru_dma_hndl;
2975 void *data = NULL; /* data to be transferred */
2976 dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
2979 megastat_t __user *ustats = NULL;
2983 struct pci_dev *pdev;
2986 * Make sure only USCSICMD are issued through this interface.
2987 * MIMD application would still fire different command.
2989 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
2994 * Check and convert a possible MIMD command to NIT command.
2995 * mega_m_to_n() copies the data from the user space, so we do not
2996 * have to do it here.
2997 * NOTE: We will need some user address to copyout the data, therefore
2998 * the inteface layer will also provide us with the required user
3001 memset(&uioc, 0, sizeof(nitioctl_t));
3002 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3006 switch( uioc.opcode ) {
3008 case GET_DRIVER_VER:
3009 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3015 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3019 * Shucks. MIMD interface returns a positive value for number
3020 * of adapters. TODO: Change it to return 0 when there is no
3021 * applicatio using mimd interface.
3030 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3033 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3034 sizeof(struct mcontroller)) )
3044 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3047 adapter = hba_soft_state[adapno];
3049 ustats = uioc.uioc_uaddr;
3051 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3055 * Check for the validity of the logical drive number
3057 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3059 if( copy_to_user(ustats->nreads, adapter->nreads,
3060 num_ldrv*sizeof(u32)) )
3063 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3064 num_ldrv*sizeof(u32)) )
3067 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3068 num_ldrv*sizeof(u32)) )
3071 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3072 num_ldrv*sizeof(u32)) )
3075 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3076 num_ldrv*sizeof(u32)) )
3079 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3080 num_ldrv*sizeof(u32)) )
3091 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3094 adapter = hba_soft_state[adapno];
3097 * Deletion of logical drive is a special case. The adapter
3098 * should be quiescent before this command is issued.
3100 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3101 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3104 * Do we support this feature
3106 if( !adapter->support_random_del ) {
3107 dev_warn(&adapter->dev->dev, "logdrv "
3108 "delete on non-supporting F/W\n");
3113 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3116 memset(&mc, 0, sizeof(megacmd_t));
3120 rval = mega_n_to_m((void __user *)arg, &mc);
3126 * This interface only support the regular passthru commands.
3127 * Reject extended passthru and 64-bit passthru
3129 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3130 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3132 dev_warn(&adapter->dev->dev, "rejected passthru\n");
3138 * For all internal commands, the buffer must be allocated in
3139 * <4GB address range
3141 if( make_local_pdev(adapter, &pdev) != 0 )
3144 /* Is it a passthru command or a DCMD */
3145 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3146 /* Passthru commands */
3148 pthru = pci_alloc_consistent(pdev,
3149 sizeof(mega_passthru),
3152 if( pthru == NULL ) {
3153 free_local_pdev(pdev);
3158 * The user passthru structure
3160 upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
3163 * Copy in the user passthru here.
3165 if( copy_from_user(pthru, upthru,
3166 sizeof(mega_passthru)) ) {
3168 pci_free_consistent(pdev,
3169 sizeof(mega_passthru), pthru,
3172 free_local_pdev(pdev);
3178 * Is there a data transfer
3180 if( pthru->dataxferlen ) {
3181 data = pci_alloc_consistent(pdev,
3185 if( data == NULL ) {
3186 pci_free_consistent(pdev,
3187 sizeof(mega_passthru),
3191 free_local_pdev(pdev);
3197 * Save the user address and point the kernel
3198 * address at just allocated memory
3200 uxferaddr = pthru->dataxferaddr;
3201 pthru->dataxferaddr = data_dma_hndl;
3206 * Is data coming down-stream
3208 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3212 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3213 pthru->dataxferlen) ) {
3215 goto freemem_and_return;
3219 memset(&mc, 0, sizeof(megacmd_t));
3221 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3222 mc.xferaddr = (u32)pthru_dma_hndl;
3227 mega_internal_command(adapter, &mc, pthru);
3229 rval = mega_n_to_m((void __user *)arg, &mc);
3231 if( rval ) goto freemem_and_return;
3235 * Is data going up-stream
3237 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3238 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3239 pthru->dataxferlen) ) {
3245 * Send the request sense data also, irrespective of
3246 * whether the user has asked for it or not.
3248 if (copy_to_user(upthru->reqsensearea,
3249 pthru->reqsensearea, 14))
3253 if( pthru->dataxferlen ) {
3254 pci_free_consistent(pdev,
3255 pthru->dataxferlen, data,
3259 pci_free_consistent(pdev, sizeof(mega_passthru),
3260 pthru, pthru_dma_hndl);
3262 free_local_pdev(pdev);
3270 * Is there a data transfer
3272 if( uioc.xferlen ) {
3273 data = pci_alloc_consistent(pdev,
3274 uioc.xferlen, &data_dma_hndl);
3276 if( data == NULL ) {
3277 free_local_pdev(pdev);
3281 uxferaddr = MBOX(uioc)->xferaddr;
3285 * Is data coming down-stream
3287 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3291 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3294 pci_free_consistent(pdev,
3296 data, data_dma_hndl);
3298 free_local_pdev(pdev);
3304 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3306 mc.xferaddr = (u32)data_dma_hndl;
3311 mega_internal_command(adapter, &mc, NULL);
3313 rval = mega_n_to_m((void __user *)arg, &mc);
3316 if( uioc.xferlen ) {
3317 pci_free_consistent(pdev,
3322 free_local_pdev(pdev);
3328 * Is data going up-stream
3330 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3331 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3338 if( uioc.xferlen ) {
3339 pci_free_consistent(pdev,
3344 free_local_pdev(pdev);
3357 megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3361 mutex_lock(&megadev_mutex);
3362 ret = megadev_ioctl(filep, cmd, arg);
3363 mutex_unlock(&megadev_mutex);
3370 * @arg: user address
3371 * @uioc: new ioctl structure
3373 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3376 * Converts the older mimd ioctl structure to newer NIT structure
3379 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3381 struct uioctl_t uioc_mimd;
3382 char signature[8] = {0};
3388 * check is the application conforms to NIT. We do not have to do much
3390 * We exploit the fact that the signature is stored in the very
3391 * beginning of the structure.
3394 if( copy_from_user(signature, arg, 7) )
3397 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3400 * NOTE NOTE: The nit ioctl is still under flux because of
3401 * change of mailbox definition, in HPE. No applications yet
3402 * use this interface and let's not have applications use this
3403 * interface till the new specifitions are in place.
3407 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3414 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3416 * Get the user ioctl structure
3418 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3423 * Get the opcode and subopcode for the commands
3425 opcode = uioc_mimd.ui.fcs.opcode;
3426 subopcode = uioc_mimd.ui.fcs.subopcode;
3431 switch (subopcode) {
3433 case MEGAIOC_QDRVRVER: /* Query driver version */
3434 uioc->opcode = GET_DRIVER_VER;
3435 uioc->uioc_uaddr = uioc_mimd.data;
3438 case MEGAIOC_QNADAP: /* Get # of adapters */
3439 uioc->opcode = GET_N_ADAP;
3440 uioc->uioc_uaddr = uioc_mimd.data;
3443 case MEGAIOC_QADAPINFO: /* Get adapter information */
3444 uioc->opcode = GET_ADAP_INFO;
3445 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3446 uioc->uioc_uaddr = uioc_mimd.data;
3458 uioc->opcode = MBOX_CMD;
3459 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3461 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3463 uioc->xferlen = uioc_mimd.ui.fcs.length;
3465 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3466 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3472 uioc->opcode = MBOX_CMD;
3473 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3475 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3478 * Choose the xferlen bigger of input and output data
3480 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3481 uioc_mimd.outlen : uioc_mimd.inlen;
3483 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3484 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3498 * @arg: user address
3499 * @mc: mailbox command
3501 * Updates the status information to the application, depending on application
3502 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3505 mega_n_to_m(void __user *arg, megacmd_t *mc)
3507 nitioctl_t __user *uiocp;
3508 megacmd_t __user *umc;
3509 mega_passthru __user *upthru;
3510 struct uioctl_t __user *uioc_mimd;
3511 char signature[8] = {0};
3514 * check is the application conforms to NIT.
3516 if( copy_from_user(signature, arg, 7) )
3519 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3523 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3526 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3528 umc = MBOX_P(uiocp);
3530 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3533 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3540 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3543 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3545 umc = (megacmd_t __user *)uioc_mimd->mbox;
3547 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3550 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3560 * MEGARAID 'FW' commands.
3564 * mega_is_bios_enabled()
3565 * @adapter: pointer to our soft state
3567 * issue command to find out if the BIOS is enabled for this controller
3570 mega_is_bios_enabled(adapter_t *adapter)
3572 unsigned char raw_mbox[sizeof(struct mbox_out)];
3575 mbox = (mbox_t *)raw_mbox;
3577 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3579 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3581 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3583 raw_mbox[0] = IS_BIOS_ENABLED;
3584 raw_mbox[2] = GET_BIOS;
3586 issue_scb_block(adapter, raw_mbox);
3588 return *(char *)adapter->mega_buffer;
3593 * mega_enum_raid_scsi()
3594 * @adapter: pointer to our soft state
3596 * Find out what channels are RAID/SCSI. This information is used to
3597 * differentiate the virtual channels and physical channels and to support
3598 * ROMB feature and non-disk devices.
3601 mega_enum_raid_scsi(adapter_t *adapter)
3603 unsigned char raw_mbox[sizeof(struct mbox_out)];
3607 mbox = (mbox_t *)raw_mbox;
3609 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3612 * issue command to find out what channels are raid/scsi
3614 raw_mbox[0] = CHNL_CLASS;
3615 raw_mbox[2] = GET_CHNL_CLASS;
3617 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3619 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3622 * Non-ROMB firmware fail this command, so all channels
3623 * must be shown RAID
3625 adapter->mega_ch_class = 0xFF;
3627 if(!issue_scb_block(adapter, raw_mbox)) {
3628 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3632 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
3633 if( (adapter->mega_ch_class >> i) & 0x01 ) {
3634 dev_info(&adapter->dev->dev, "channel[%d] is raid\n",
3638 dev_info(&adapter->dev->dev, "channel[%d] is scsi\n",
3648 * mega_get_boot_drv()
3649 * @adapter: pointer to our soft state
3651 * Find out which device is the boot device. Note, any logical drive or any
3652 * phyical device (e.g., a CDROM) can be designated as a boot device.
3655 mega_get_boot_drv(adapter_t *adapter)
3657 struct private_bios_data *prv_bios_data;
3658 unsigned char raw_mbox[sizeof(struct mbox_out)];
3665 mbox = (mbox_t *)raw_mbox;
3667 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3669 raw_mbox[0] = BIOS_PVT_DATA;
3670 raw_mbox[2] = GET_BIOS_PVT_DATA;
3672 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3674 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3676 adapter->boot_ldrv_enabled = 0;
3677 adapter->boot_ldrv = 0;
3679 adapter->boot_pdrv_enabled = 0;
3680 adapter->boot_pdrv_ch = 0;
3681 adapter->boot_pdrv_tgt = 0;
3683 if(issue_scb_block(adapter, raw_mbox) == 0) {
3685 (struct private_bios_data *)adapter->mega_buffer;
3688 cksum_p = (char *)prv_bios_data;
3689 for (i = 0; i < 14; i++ ) {
3690 cksum += (u16)(*cksum_p++);
3693 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
3696 * If MSB is set, a physical drive is set as boot
3699 if( prv_bios_data->boot_drv & 0x80 ) {
3700 adapter->boot_pdrv_enabled = 1;
3701 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
3702 adapter->boot_pdrv_ch = boot_pdrv / 16;
3703 adapter->boot_pdrv_tgt = boot_pdrv % 16;
3706 adapter->boot_ldrv_enabled = 1;
3707 adapter->boot_ldrv = prv_bios_data->boot_drv;
3715 * mega_support_random_del()
3716 * @adapter: pointer to our soft state
3718 * Find out if this controller supports random deletion and addition of
3722 mega_support_random_del(adapter_t *adapter)
3724 unsigned char raw_mbox[sizeof(struct mbox_out)];
3728 mbox = (mbox_t *)raw_mbox;
3730 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3735 raw_mbox[0] = FC_DEL_LOGDRV;
3736 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3738 rval = issue_scb_block(adapter, raw_mbox);
3745 * mega_support_ext_cdb()
3746 * @adapter: pointer to our soft state
3748 * Find out if this firmware support cdblen > 10
3751 mega_support_ext_cdb(adapter_t *adapter)
3753 unsigned char raw_mbox[sizeof(struct mbox_out)];
3757 mbox = (mbox_t *)raw_mbox;
3759 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3761 * issue command to find out if controller supports extended CDBs.
3766 rval = issue_scb_block(adapter, raw_mbox);
3774 * @adapter: pointer to our soft state
3775 * @logdrv: logical drive to be deleted
3777 * Delete the specified logical drive. It is the responsibility of the user
3778 * app to let the OS know about this operation.
3781 mega_del_logdrv(adapter_t *adapter, int logdrv)
3783 unsigned long flags;
3788 * Stop sending commands to the controller, queue them internally.
3789 * When deletion is complete, ISR will flush the queue.
3791 atomic_set(&adapter->quiescent, 1);
3794 * Wait till all the issued commands are complete and there are no
3795 * commands in the pending queue
3797 while (atomic_read(&adapter->pend_cmds) > 0 ||
3798 !list_empty(&adapter->pending_list))
3799 msleep(1000); /* sleep for 1s */
3801 rval = mega_do_del_logdrv(adapter, logdrv);
3803 spin_lock_irqsave(&adapter->lock, flags);
3806 * If delete operation was successful, add 0x80 to the logical drive
3807 * ids for commands in the pending queue.
3809 if (adapter->read_ldidmap) {
3810 struct list_head *pos;
3811 list_for_each(pos, &adapter->pending_list) {
3812 scb = list_entry(pos, scb_t, list);
3813 if (scb->pthru->logdrv < 0x80 )
3814 scb->pthru->logdrv += 0x80;
3818 atomic_set(&adapter->quiescent, 0);
3820 mega_runpendq(adapter);
3822 spin_unlock_irqrestore(&adapter->lock, flags);
3829 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
3834 memset( &mc, 0, sizeof(megacmd_t));
3836 mc.cmd = FC_DEL_LOGDRV;
3837 mc.opcode = OP_DEL_LOGDRV;
3838 mc.subopcode = logdrv;
3840 rval = mega_internal_command(adapter, &mc, NULL);
3842 /* log this event */
3844 dev_warn(&adapter->dev->dev, "Delete LD-%d failed", logdrv);
3849 * After deleting first logical drive, the logical drives must be
3850 * addressed by adding 0x80 to the logical drive id.
3852 adapter->read_ldidmap = 1;
3859 * mega_get_max_sgl()
3860 * @adapter: pointer to our soft state
3862 * Find out the maximum number of scatter-gather elements supported by this
3863 * version of the firmware
3866 mega_get_max_sgl(adapter_t *adapter)
3868 unsigned char raw_mbox[sizeof(struct mbox_out)];
3871 mbox = (mbox_t *)raw_mbox;
3873 memset(mbox, 0, sizeof(raw_mbox));
3875 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3877 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3879 raw_mbox[0] = MAIN_MISC_OPCODE;
3880 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3883 if( issue_scb_block(adapter, raw_mbox) ) {
3885 * f/w does not support this command. Choose the default value
3887 adapter->sglen = MIN_SGLIST;
3890 adapter->sglen = *((char *)adapter->mega_buffer);
3893 * Make sure this is not more than the resources we are
3894 * planning to allocate
3896 if ( adapter->sglen > MAX_SGLIST )
3897 adapter->sglen = MAX_SGLIST;
3905 * mega_support_cluster()
3906 * @adapter: pointer to our soft state
3908 * Find out if this firmware support cluster calls.
3911 mega_support_cluster(adapter_t *adapter)
3913 unsigned char raw_mbox[sizeof(struct mbox_out)];
3916 mbox = (mbox_t *)raw_mbox;
3918 memset(mbox, 0, sizeof(raw_mbox));
3920 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3922 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3925 * Try to get the initiator id. This command will succeed iff the
3926 * clustering is available on this HBA.
3928 raw_mbox[0] = MEGA_GET_TARGET_ID;
3930 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
3933 * Cluster support available. Get the initiator target id.
3934 * Tell our id to mid-layer too.
3936 adapter->this_id = *(u32 *)adapter->mega_buffer;
3937 adapter->host->this_id = adapter->this_id;
3945 #ifdef CONFIG_PROC_FS
3948 * @adapter: pointer to our soft state
3949 * @dma_handle: DMA address of the buffer
3951 * Issue internal commands while interrupts are available.
3952 * We only issue direct mailbox commands from within the driver. ioctl()
3953 * interface using these routines can issue passthru commands.
3956 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
3960 memset(&mc, 0, sizeof(megacmd_t));
3962 if( adapter->flag & BOARD_40LD ) {
3963 mc.cmd = FC_NEW_CONFIG;
3964 mc.opcode = NC_SUBOP_ENQUIRY3;
3965 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
3968 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
3971 mc.xferaddr = (u32)dma_handle;
3973 if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
3982 * mega_internal_dev_inquiry()
3983 * @adapter: pointer to our soft state
3984 * @ch: channel for this device
3985 * @tgt: ID of this device
3986 * @buf_dma_handle: DMA address of the buffer
3988 * Issue the scsi inquiry for the specified device.
3991 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
3992 dma_addr_t buf_dma_handle)
3994 mega_passthru *pthru;
3995 dma_addr_t pthru_dma_handle;
3998 struct pci_dev *pdev;
4002 * For all internal commands, the buffer must be allocated in <4GB
4005 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4007 pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4010 if( pthru == NULL ) {
4011 free_local_pdev(pdev);
4017 pthru->reqsenselen = 14;
4018 pthru->islogical = 0;
4020 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4022 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4026 pthru->cdb[0] = INQUIRY;
4030 pthru->cdb[4] = 255;
4034 pthru->dataxferaddr = (u32)buf_dma_handle;
4035 pthru->dataxferlen = 256;
4037 memset(&mc, 0, sizeof(megacmd_t));
4039 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4040 mc.xferaddr = (u32)pthru_dma_handle;
4042 rval = mega_internal_command(adapter, &mc, pthru);
4044 pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4047 free_local_pdev(pdev);
4054 * mega_internal_command()
4055 * @adapter: pointer to our soft state
4056 * @mc: the mailbox command
4057 * @pthru: Passthru structure for DCDB commands
4059 * Issue the internal commands in interrupt mode.
4060 * The last argument is the address of the passthru structure if the command
4061 * to be fired is a passthru command
4063 * Note: parameter 'pthru' is null for non-passthru commands.
4066 mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4068 unsigned long flags;
4073 * The internal commands share one command id and hence are
4074 * serialized. This is so because we want to reserve maximum number of
4075 * available command ids for the I/O commands.
4077 mutex_lock(&adapter->int_mtx);
4079 scb = &adapter->int_scb;
4080 memset(scb, 0, sizeof(scb_t));
4082 scb->idx = CMDID_INT_CMDS;
4083 scb->state |= SCB_ACTIVE | SCB_PENDQ;
4085 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4088 * Is it a passthru command
4090 if (mc->cmd == MEGA_MBOXCMD_PASSTHRU)
4093 spin_lock_irqsave(&adapter->lock, flags);
4094 list_add_tail(&scb->list, &adapter->pending_list);
4096 * Check if the HBA is in quiescent state, e.g., during a
4097 * delete logical drive opertion. If it is, don't run
4100 if (atomic_read(&adapter->quiescent) == 0)
4101 mega_runpendq(adapter);
4102 spin_unlock_irqrestore(&adapter->lock, flags);
4104 wait_for_completion(&adapter->int_waitq);
4106 mc->status = rval = adapter->int_status;
4109 * Print a debug message for all failed commands. Applications can use
4112 if (rval && trace_level) {
4113 dev_info(&adapter->dev->dev, "cmd [%x, %x, %x] status:[%x]\n",
4114 mc->cmd, mc->opcode, mc->subopcode, rval);
4117 mutex_unlock(&adapter->int_mtx);
4121 static struct scsi_host_template megaraid_template = {
4122 .module = THIS_MODULE,
4124 .proc_name = "megaraid_legacy",
4125 .info = megaraid_info,
4126 .queuecommand = megaraid_queue,
4127 .bios_param = megaraid_biosparam,
4128 .max_sectors = MAX_SECTORS_PER_IO,
4129 .can_queue = MAX_COMMANDS,
4130 .this_id = DEFAULT_INITIATOR_ID,
4131 .sg_tablesize = MAX_SGLIST,
4132 .cmd_per_lun = DEF_CMD_PER_LUN,
4133 .eh_abort_handler = megaraid_abort,
4134 .eh_device_reset_handler = megaraid_reset,
4135 .eh_bus_reset_handler = megaraid_reset,
4136 .eh_host_reset_handler = megaraid_reset,
4141 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4143 struct Scsi_Host *host;
4145 unsigned long mega_baseport, tbase, flag = 0;
4146 u16 subsysid, subsysvid;
4147 u8 pci_bus, pci_dev_func;
4149 int error = -ENODEV;
4151 if (hba_count >= MAX_CONTROLLERS)
4154 if (pci_enable_device(pdev))
4156 pci_set_master(pdev);
4158 pci_bus = pdev->bus->number;
4159 pci_dev_func = pdev->devfn;
4162 * The megaraid3 stuff reports the ID of the Intel part which is not
4163 * remotely specific to the megaraid
4165 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4168 * Don't fall over the Compaq management cards using the same
4171 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4172 pdev->subsystem_device == 0xC000)
4173 goto out_disable_device;
4174 /* Now check the magic signature byte */
4175 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4176 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4177 goto out_disable_device;
4178 /* Ok it is probably a megaraid */
4182 * For these vendor and device ids, signature offsets are not
4183 * valid and 64 bit is implicit
4185 if (id->driver_data & BOARD_64BIT)
4186 flag |= BOARD_64BIT;
4190 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4191 if (magic64 == HBA_SIGNATURE_64BIT)
4192 flag |= BOARD_64BIT;
4195 subsysvid = pdev->subsystem_vendor;
4196 subsysid = pdev->subsystem_device;
4198 dev_notice(&pdev->dev, "found 0x%4.04x:0x%4.04x\n",
4199 id->vendor, id->device);
4201 /* Read the base port and IRQ from PCI */
4202 mega_baseport = pci_resource_start(pdev, 0);
4205 tbase = mega_baseport;
4206 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4207 flag |= BOARD_MEMMAP;
4209 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4210 dev_warn(&pdev->dev, "mem region busy!\n");
4211 goto out_disable_device;
4214 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4215 if (!mega_baseport) {
4216 dev_warn(&pdev->dev, "could not map hba memory\n");
4217 goto out_release_region;
4220 flag |= BOARD_IOMAP;
4221 mega_baseport += 0x10;
4223 if (!request_region(mega_baseport, 16, "megaraid"))
4224 goto out_disable_device;
4227 /* Initialize SCSI Host structure */
4228 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4232 adapter = (adapter_t *)host->hostdata;
4233 memset(adapter, 0, sizeof(adapter_t));
4235 dev_notice(&pdev->dev,
4236 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4237 host->host_no, mega_baseport, irq);
4239 adapter->base = mega_baseport;
4240 if (flag & BOARD_MEMMAP)
4241 adapter->mmio_base = (void __iomem *) mega_baseport;
4243 INIT_LIST_HEAD(&adapter->free_list);
4244 INIT_LIST_HEAD(&adapter->pending_list);
4245 INIT_LIST_HEAD(&adapter->completed_list);
4247 adapter->flag = flag;
4248 spin_lock_init(&adapter->lock);
4250 host->cmd_per_lun = max_cmd_per_lun;
4251 host->max_sectors = max_sectors_per_io;
4253 adapter->dev = pdev;
4254 adapter->host = host;
4256 adapter->host->irq = irq;
4258 if (flag & BOARD_MEMMAP)
4259 adapter->host->base = tbase;
4261 adapter->host->io_port = tbase;
4262 adapter->host->n_io_port = 16;
4265 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4268 * Allocate buffer to issue internal commands.
4270 adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4271 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4272 if (!adapter->mega_buffer) {
4273 dev_warn(&pdev->dev, "out of RAM\n");
4277 adapter->scb_list = kmalloc_array(MAX_COMMANDS, sizeof(scb_t),
4279 if (!adapter->scb_list) {
4280 dev_warn(&pdev->dev, "out of RAM\n");
4281 goto out_free_cmd_buffer;
4284 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4285 megaraid_isr_memmapped : megaraid_isr_iomapped,
4286 IRQF_SHARED, "megaraid", adapter)) {
4287 dev_warn(&pdev->dev, "Couldn't register IRQ %d!\n", irq);
4288 goto out_free_scb_list;
4291 if (mega_setup_mailbox(adapter))
4294 if (mega_query_adapter(adapter))
4298 * Have checks for some buggy f/w
4300 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4304 if (!strcmp(adapter->fw_version, "3.00") ||
4305 !strcmp(adapter->fw_version, "3.01")) {
4307 dev_warn(&pdev->dev,
4308 "Your card is a Dell PERC "
4309 "2/SC RAID controller with "
4310 "firmware\nmegaraid: 3.00 or 3.01. "
4311 "This driver is known to have "
4312 "corruption issues\nmegaraid: with "
4313 "those firmware versions on this "
4314 "specific card. In order\nmegaraid: "
4315 "to protect your data, please upgrade "
4316 "your firmware to version\nmegaraid: "
4317 "3.10 or later, available from the "
4318 "Dell Technical Support web\n"
4319 "megaraid: site at\nhttp://support."
4320 "dell.com/us/en/filelib/download/"
4321 "index.asp?fileid=2940\n"
4327 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4328 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4329 * support, since this firmware cannot handle 64 bit
4332 if ((subsysvid == PCI_VENDOR_ID_HP) &&
4333 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4337 if (!strcmp(adapter->fw_version, "H01.07") ||
4338 !strcmp(adapter->fw_version, "H01.08") ||
4339 !strcmp(adapter->fw_version, "H01.09") ) {
4340 dev_warn(&pdev->dev,
4341 "Firmware H.01.07, "
4342 "H.01.08, and H.01.09 on 1M/2M "
4344 "do not support 64 bit "
4345 "addressing.\nDISABLING "
4346 "64 bit support.\n");
4347 adapter->flag &= ~BOARD_64BIT;
4351 if (mega_is_bios_enabled(adapter))
4352 mega_hbas[hba_count].is_bios_enabled = 1;
4353 mega_hbas[hba_count].hostdata_addr = adapter;
4356 * Find out which channel is raid and which is scsi. This is
4359 mega_enum_raid_scsi(adapter);
4362 * Find out if a logical drive is set as the boot drive. If
4363 * there is one, will make that as the first logical drive.
4364 * ROMB: Do we have to boot from a physical drive. Then all
4365 * the physical drives would appear before the logical disks.
4366 * Else, all the physical drives would be exported to the mid
4367 * layer after logical drives.
4369 mega_get_boot_drv(adapter);
4371 if (adapter->boot_pdrv_enabled) {
4372 j = adapter->product_info.nchannels;
4373 for( i = 0; i < j; i++ )
4374 adapter->logdrv_chan[i] = 0;
4375 for( i = j; i < NVIRT_CHAN + j; i++ )
4376 adapter->logdrv_chan[i] = 1;
4378 for (i = 0; i < NVIRT_CHAN; i++)
4379 adapter->logdrv_chan[i] = 1;
4380 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4381 adapter->logdrv_chan[i] = 0;
4382 adapter->mega_ch_class <<= NVIRT_CHAN;
4386 * Do we support random deletion and addition of logical
4389 adapter->read_ldidmap = 0; /* set it after first logdrv
4391 adapter->support_random_del = mega_support_random_del(adapter);
4393 /* Initialize SCBs */
4394 if (mega_init_scb(adapter))
4398 * Reset the pending commands counter
4400 atomic_set(&adapter->pend_cmds, 0);
4403 * Reset the adapter quiescent flag
4405 atomic_set(&adapter->quiescent, 0);
4407 hba_soft_state[hba_count] = adapter;
4410 * Fill in the structure which needs to be passed back to the
4411 * application when it does an ioctl() for controller related
4416 mcontroller[i].base = mega_baseport;
4417 mcontroller[i].irq = irq;
4418 mcontroller[i].numldrv = adapter->numldrv;
4419 mcontroller[i].pcibus = pci_bus;
4420 mcontroller[i].pcidev = id->device;
4421 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4422 mcontroller[i].pciid = -1;
4423 mcontroller[i].pcivendor = id->vendor;
4424 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4425 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4428 /* Set the Mode of addressing to 64 bit if we can */
4429 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4430 pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4431 adapter->has_64bit_addr = 1;
4433 pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4434 adapter->has_64bit_addr = 0;
4437 mutex_init(&adapter->int_mtx);
4438 init_completion(&adapter->int_waitq);
4440 adapter->this_id = DEFAULT_INITIATOR_ID;
4441 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4443 #if MEGA_HAVE_CLUSTERING
4445 * Is cluster support enabled on this controller
4446 * Note: In a cluster the HBAs ( the initiators ) will have
4447 * different target IDs and we cannot assume it to be 7. Call
4448 * to mega_support_cluster() will get the target ids also if
4449 * the cluster support is available
4451 adapter->has_cluster = mega_support_cluster(adapter);
4452 if (adapter->has_cluster) {
4453 dev_notice(&pdev->dev,
4454 "Cluster driver, initiator id:%d\n",
4459 pci_set_drvdata(pdev, host);
4461 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4463 error = scsi_add_host(host, &pdev->dev);
4467 scsi_scan_host(host);
4472 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4473 adapter->una_mbox64, adapter->una_mbox64_dma);
4475 free_irq(adapter->host->irq, adapter);
4477 kfree(adapter->scb_list);
4478 out_free_cmd_buffer:
4479 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4480 adapter->mega_buffer, adapter->buf_dma_handle);
4482 scsi_host_put(host);
4484 if (flag & BOARD_MEMMAP)
4485 iounmap((void *)mega_baseport);
4487 if (flag & BOARD_MEMMAP)
4488 release_mem_region(tbase, 128);
4490 release_region(mega_baseport, 16);
4492 pci_disable_device(pdev);
4498 __megaraid_shutdown(adapter_t *adapter)
4500 u_char raw_mbox[sizeof(struct mbox_out)];
4501 mbox_t *mbox = (mbox_t *)raw_mbox;
4504 /* Flush adapter cache */
4505 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4506 raw_mbox[0] = FLUSH_ADAPTER;
4508 free_irq(adapter->host->irq, adapter);
4510 /* Issue a blocking (interrupts disabled) command to the card */
4511 issue_scb_block(adapter, raw_mbox);
4513 /* Flush disks cache */
4514 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4515 raw_mbox[0] = FLUSH_SYSTEM;
4517 /* Issue a blocking (interrupts disabled) command to the card */
4518 issue_scb_block(adapter, raw_mbox);
4520 if (atomic_read(&adapter->pend_cmds) > 0)
4521 dev_warn(&adapter->dev->dev, "pending commands!!\n");
4524 * Have a delibrate delay to make sure all the caches are
4527 for (i = 0; i <= 10; i++)
4532 megaraid_remove_one(struct pci_dev *pdev)
4534 struct Scsi_Host *host = pci_get_drvdata(pdev);
4535 adapter_t *adapter = (adapter_t *)host->hostdata;
4536 char buf[12] = { 0 };
4538 scsi_remove_host(host);
4540 __megaraid_shutdown(adapter);
4542 /* Free our resources */
4543 if (adapter->flag & BOARD_MEMMAP) {
4544 iounmap((void *)adapter->base);
4545 release_mem_region(adapter->host->base, 128);
4547 release_region(adapter->base, 16);
4549 mega_free_sgl(adapter);
4551 sprintf(buf, "hba%d", adapter->host->host_no);
4552 remove_proc_subtree(buf, mega_proc_dir_entry);
4554 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4555 adapter->mega_buffer, adapter->buf_dma_handle);
4556 kfree(adapter->scb_list);
4557 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4558 adapter->una_mbox64, adapter->una_mbox64_dma);
4560 scsi_host_put(host);
4561 pci_disable_device(pdev);
4567 megaraid_shutdown(struct pci_dev *pdev)
4569 struct Scsi_Host *host = pci_get_drvdata(pdev);
4570 adapter_t *adapter = (adapter_t *)host->hostdata;
4572 __megaraid_shutdown(adapter);
4575 static struct pci_device_id megaraid_pci_tbl[] = {
4576 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4577 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4578 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
4579 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4580 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
4581 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4584 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
4586 static struct pci_driver megaraid_pci_driver = {
4587 .name = "megaraid_legacy",
4588 .id_table = megaraid_pci_tbl,
4589 .probe = megaraid_probe_one,
4590 .remove = megaraid_remove_one,
4591 .shutdown = megaraid_shutdown,
4594 static int __init megaraid_init(void)
4598 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
4599 max_cmd_per_lun = MAX_CMD_PER_LUN;
4600 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
4601 max_mbox_busy_wait = MBOX_BUSY_WAIT;
4603 #ifdef CONFIG_PROC_FS
4604 mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
4605 if (!mega_proc_dir_entry) {
4607 "megaraid: failed to create megaraid root\n");
4610 error = pci_register_driver(&megaraid_pci_driver);
4612 #ifdef CONFIG_PROC_FS
4613 remove_proc_entry("megaraid", NULL);
4619 * Register the driver as a character device, for applications
4620 * to access it for ioctls.
4621 * First argument (major) to register_chrdev implies a dynamic
4622 * major number allocation.
4624 major = register_chrdev(0, "megadev_legacy", &megadev_fops);
4627 "megaraid: failed to register char device\n");
4633 static void __exit megaraid_exit(void)
4636 * Unregister the character device interface to the driver.
4638 unregister_chrdev(major, "megadev_legacy");
4640 pci_unregister_driver(&megaraid_pci_driver);
4642 #ifdef CONFIG_PROC_FS
4643 remove_proc_entry("megaraid", NULL);
4647 module_init(megaraid_init);
4648 module_exit(megaraid_exit);
4650 /* vi: set ts=8 sw=8 tw=78: */