3 * Linux MegaRAID driver for SAS based RAID controllers
5 * Copyright (c) 2003-2005 LSI Logic Corporation.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * FILE : megaraid_sas.c
13 * Version : v00.00.03.10-rc5
16 * (email-id : megaraidlinux@lsi.com)
21 * List of supported controllers
23 * OEM Product Name VID DID SSVID SSID
24 * --- ------------ --- --- ---- ----
27 #include <linux/kernel.h>
28 #include <linux/types.h>
29 #include <linux/pci.h>
30 #include <linux/list.h>
31 #include <linux/moduleparam.h>
32 #include <linux/module.h>
33 #include <linux/spinlock.h>
34 #include <linux/mutex.h>
35 #include <linux/interrupt.h>
36 #include <linux/delay.h>
37 #include <linux/uio.h>
38 #include <asm/uaccess.h>
40 #include <linux/compat.h>
41 #include <linux/blkdev.h>
42 #include <linux/mutex.h>
44 #include <scsi/scsi.h>
45 #include <scsi/scsi_cmnd.h>
46 #include <scsi/scsi_device.h>
47 #include <scsi/scsi_host.h>
48 #include "megaraid_sas.h"
50 MODULE_LICENSE("GPL");
51 MODULE_VERSION(MEGASAS_VERSION);
52 MODULE_AUTHOR("megaraidlinux@lsi.com");
53 MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
56 * PCI ID table for all supported controllers
58 static struct pci_device_id megasas_pci_table[] = {
60 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
62 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
64 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
65 /* xscale IOP, vega */
66 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
71 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
73 static int megasas_mgmt_majorno;
74 static struct megasas_mgmt_info megasas_mgmt_info;
75 static struct fasync_struct *megasas_async_queue;
76 static DEFINE_MUTEX(megasas_async_queue_mutex);
78 static u32 megasas_dbg_lvl;
81 * megasas_get_cmd - Get a command from the free pool
82 * @instance: Adapter soft state
84 * Returns a free command from the pool
86 static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
90 struct megasas_cmd *cmd = NULL;
92 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
94 if (!list_empty(&instance->cmd_pool)) {
95 cmd = list_entry((&instance->cmd_pool)->next,
96 struct megasas_cmd, list);
97 list_del_init(&cmd->list);
99 printk(KERN_ERR "megasas: Command pool empty!\n");
102 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
107 * megasas_return_cmd - Return a cmd to free command pool
108 * @instance: Adapter soft state
109 * @cmd: Command packet to be returned to free command pool
112 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
116 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
119 list_add_tail(&cmd->list, &instance->cmd_pool);
121 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
126 * The following functions are defined for xscale
127 * (deviceid : 1064R, PERC5) controllers
131 * megasas_enable_intr_xscale - Enables interrupts
132 * @regs: MFI register set
135 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
137 writel(1, &(regs)->outbound_intr_mask);
139 /* Dummy readl to force pci flush */
140 readl(®s->outbound_intr_mask);
144 * megasas_disable_intr_xscale -Disables interrupt
145 * @regs: MFI register set
148 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
151 writel(mask, ®s->outbound_intr_mask);
152 /* Dummy readl to force pci flush */
153 readl(®s->outbound_intr_mask);
157 * megasas_read_fw_status_reg_xscale - returns the current FW status value
158 * @regs: MFI register set
161 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
163 return readl(&(regs)->outbound_msg_0);
166 * megasas_clear_interrupt_xscale - Check & clear interrupt
167 * @regs: MFI register set
170 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
174 * Check if it is our interrupt
176 status = readl(®s->outbound_intr_status);
178 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
183 * Clear the interrupt by writing back the same value
185 writel(status, ®s->outbound_intr_status);
191 * megasas_fire_cmd_xscale - Sends command to the FW
192 * @frame_phys_addr : Physical address of cmd
193 * @frame_count : Number of frames for the command
194 * @regs : MFI register set
197 megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
199 writel((frame_phys_addr >> 3)|(frame_count),
200 &(regs)->inbound_queue_port);
203 static struct megasas_instance_template megasas_instance_template_xscale = {
205 .fire_cmd = megasas_fire_cmd_xscale,
206 .enable_intr = megasas_enable_intr_xscale,
207 .disable_intr = megasas_disable_intr_xscale,
208 .clear_intr = megasas_clear_intr_xscale,
209 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
213 * This is the end of set of functions & definitions specific
214 * to xscale (deviceid : 1064R, PERC5) controllers
218 * The following functions are defined for ppc (deviceid : 0x60)
223 * megasas_enable_intr_ppc - Enables interrupts
224 * @regs: MFI register set
227 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
229 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
231 writel(~0x80000004, &(regs)->outbound_intr_mask);
233 /* Dummy readl to force pci flush */
234 readl(®s->outbound_intr_mask);
238 * megasas_disable_intr_ppc - Disable interrupt
239 * @regs: MFI register set
242 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
244 u32 mask = 0xFFFFFFFF;
245 writel(mask, ®s->outbound_intr_mask);
246 /* Dummy readl to force pci flush */
247 readl(®s->outbound_intr_mask);
251 * megasas_read_fw_status_reg_ppc - returns the current FW status value
252 * @regs: MFI register set
255 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
257 return readl(&(regs)->outbound_scratch_pad);
261 * megasas_clear_interrupt_ppc - Check & clear interrupt
262 * @regs: MFI register set
265 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
269 * Check if it is our interrupt
271 status = readl(®s->outbound_intr_status);
273 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
278 * Clear the interrupt by writing back the same value
280 writel(status, ®s->outbound_doorbell_clear);
285 * megasas_fire_cmd_ppc - Sends command to the FW
286 * @frame_phys_addr : Physical address of cmd
287 * @frame_count : Number of frames for the command
288 * @regs : MFI register set
291 megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
293 writel((frame_phys_addr | (frame_count<<1))|1,
294 &(regs)->inbound_queue_port);
297 static struct megasas_instance_template megasas_instance_template_ppc = {
299 .fire_cmd = megasas_fire_cmd_ppc,
300 .enable_intr = megasas_enable_intr_ppc,
301 .disable_intr = megasas_disable_intr_ppc,
302 .clear_intr = megasas_clear_intr_ppc,
303 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
307 * This is the end of set of functions & definitions
308 * specific to ppc (deviceid : 0x60) controllers
312 * megasas_issue_polled - Issues a polling command
313 * @instance: Adapter soft state
314 * @cmd: Command packet to be issued
316 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
319 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
322 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
324 struct megasas_header *frame_hdr = &cmd->frame->hdr;
326 frame_hdr->cmd_status = 0xFF;
327 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
330 * Issue the frame using inbound queue port
332 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
335 * Wait for cmd_status to change
337 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
342 if (frame_hdr->cmd_status == 0xff)
349 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
350 * @instance: Adapter soft state
351 * @cmd: Command to be issued
353 * This function waits on an event for the command to be returned from ISR.
354 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
355 * Used to issue ioctl commands.
358 megasas_issue_blocked_cmd(struct megasas_instance *instance,
359 struct megasas_cmd *cmd)
361 cmd->cmd_status = ENODATA;
363 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
365 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
366 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
372 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
373 * @instance: Adapter soft state
374 * @cmd_to_abort: Previously issued cmd to be aborted
376 * MFI firmware can abort previously issued AEN comamnd (automatic event
377 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
378 * cmd and waits for return status.
379 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
382 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
383 struct megasas_cmd *cmd_to_abort)
385 struct megasas_cmd *cmd;
386 struct megasas_abort_frame *abort_fr;
388 cmd = megasas_get_cmd(instance);
393 abort_fr = &cmd->frame->abort;
396 * Prepare and issue the abort frame
398 abort_fr->cmd = MFI_CMD_ABORT;
399 abort_fr->cmd_status = 0xFF;
401 abort_fr->abort_context = cmd_to_abort->index;
402 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
403 abort_fr->abort_mfi_phys_addr_hi = 0;
406 cmd->cmd_status = 0xFF;
408 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
411 * Wait for this cmd to complete
413 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
414 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
416 megasas_return_cmd(instance, cmd);
421 * megasas_make_sgl32 - Prepares 32-bit SGL
422 * @instance: Adapter soft state
423 * @scp: SCSI command from the mid-layer
424 * @mfi_sgl: SGL to be filled in
426 * If successful, this function returns the number of SG elements. Otherwise,
430 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
431 union megasas_sgl *mfi_sgl)
435 struct scatterlist *os_sgl;
437 sge_count = scsi_dma_map(scp);
438 BUG_ON(sge_count < 0);
441 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
442 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
443 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
450 * megasas_make_sgl64 - Prepares 64-bit SGL
451 * @instance: Adapter soft state
452 * @scp: SCSI command from the mid-layer
453 * @mfi_sgl: SGL to be filled in
455 * If successful, this function returns the number of SG elements. Otherwise,
459 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
460 union megasas_sgl *mfi_sgl)
464 struct scatterlist *os_sgl;
466 sge_count = scsi_dma_map(scp);
467 BUG_ON(sge_count < 0);
470 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
471 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
472 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
479 * megasas_get_frame_count - Computes the number of frames
480 * @sge_count : number of sg elements
482 * Returns the number of frames required for numnber of sge's (sge_count)
485 static u32 megasas_get_frame_count(u8 sge_count)
492 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
493 sizeof(struct megasas_sge32);
496 * Main frame can contain 2 SGEs for 64-bit SGLs and
497 * 3 SGEs for 32-bit SGLs
500 num_cnt = sge_count - 2;
502 num_cnt = sge_count - 3;
505 sge_bytes = sge_sz * num_cnt;
507 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
508 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
519 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
520 * @instance: Adapter soft state
522 * @cmd: Command to be prepared in
524 * This function prepares CDB commands. These are typcially pass-through
525 * commands to the devices.
528 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
529 struct megasas_cmd *cmd)
534 struct megasas_pthru_frame *pthru;
536 is_logical = MEGASAS_IS_LOGICAL(scp);
537 device_id = MEGASAS_DEV_INDEX(instance, scp);
538 pthru = (struct megasas_pthru_frame *)cmd->frame;
540 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
541 flags = MFI_FRAME_DIR_WRITE;
542 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
543 flags = MFI_FRAME_DIR_READ;
544 else if (scp->sc_data_direction == PCI_DMA_NONE)
545 flags = MFI_FRAME_DIR_NONE;
548 * Prepare the DCDB frame
550 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
551 pthru->cmd_status = 0x0;
552 pthru->scsi_status = 0x0;
553 pthru->target_id = device_id;
554 pthru->lun = scp->device->lun;
555 pthru->cdb_len = scp->cmd_len;
557 pthru->flags = flags;
558 pthru->data_xfer_len = scsi_bufflen(scp);
560 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
566 pthru->flags |= MFI_FRAME_SGL64;
567 pthru->sge_count = megasas_make_sgl64(instance, scp,
570 pthru->sge_count = megasas_make_sgl32(instance, scp,
574 * Sense info specific
576 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
577 pthru->sense_buf_phys_addr_hi = 0;
578 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
581 * Compute the total number of frames this command consumes. FW uses
582 * this number to pull sufficient number of frames from host memory.
584 cmd->frame_count = megasas_get_frame_count(pthru->sge_count);
586 return cmd->frame_count;
590 * megasas_build_ldio - Prepares IOs to logical devices
591 * @instance: Adapter soft state
593 * @cmd: Command to to be prepared
595 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
598 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
599 struct megasas_cmd *cmd)
602 u8 sc = scp->cmnd[0];
604 struct megasas_io_frame *ldio;
606 device_id = MEGASAS_DEV_INDEX(instance, scp);
607 ldio = (struct megasas_io_frame *)cmd->frame;
609 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
610 flags = MFI_FRAME_DIR_WRITE;
611 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
612 flags = MFI_FRAME_DIR_READ;
615 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
617 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
618 ldio->cmd_status = 0x0;
619 ldio->scsi_status = 0x0;
620 ldio->target_id = device_id;
622 ldio->reserved_0 = 0;
625 ldio->start_lba_hi = 0;
626 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
629 * 6-byte READ(0x08) or WRITE(0x0A) cdb
631 if (scp->cmd_len == 6) {
632 ldio->lba_count = (u32) scp->cmnd[4];
633 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
634 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
636 ldio->start_lba_lo &= 0x1FFFFF;
640 * 10-byte READ(0x28) or WRITE(0x2A) cdb
642 else if (scp->cmd_len == 10) {
643 ldio->lba_count = (u32) scp->cmnd[8] |
644 ((u32) scp->cmnd[7] << 8);
645 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
646 ((u32) scp->cmnd[3] << 16) |
647 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
651 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
653 else if (scp->cmd_len == 12) {
654 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
655 ((u32) scp->cmnd[7] << 16) |
656 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
658 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
659 ((u32) scp->cmnd[3] << 16) |
660 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
664 * 16-byte READ(0x88) or WRITE(0x8A) cdb
666 else if (scp->cmd_len == 16) {
667 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
668 ((u32) scp->cmnd[11] << 16) |
669 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
671 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
672 ((u32) scp->cmnd[7] << 16) |
673 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
675 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
676 ((u32) scp->cmnd[3] << 16) |
677 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
685 ldio->flags |= MFI_FRAME_SGL64;
686 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
688 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
691 * Sense info specific
693 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
694 ldio->sense_buf_phys_addr_hi = 0;
695 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
698 * Compute the total number of frames this command consumes. FW uses
699 * this number to pull sufficient number of frames from host memory.
701 cmd->frame_count = megasas_get_frame_count(ldio->sge_count);
703 return cmd->frame_count;
707 * megasas_is_ldio - Checks if the cmd is for logical drive
708 * @scmd: SCSI command
710 * Called by megasas_queue_command to find out if the command to be queued
711 * is a logical drive command
713 static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
715 if (!MEGASAS_IS_LOGICAL(cmd))
717 switch (cmd->cmnd[0]) {
733 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
735 * @instance: Adapter soft state
738 megasas_dump_pending_frames(struct megasas_instance *instance)
740 struct megasas_cmd *cmd;
742 union megasas_sgl *mfi_sgl;
743 struct megasas_io_frame *ldio;
744 struct megasas_pthru_frame *pthru;
746 u32 max_cmd = instance->max_fw_cmds;
748 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
749 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
751 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
753 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
755 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
756 for (i = 0; i < max_cmd; i++) {
757 cmd = instance->cmd_list[i];
760 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
761 if (megasas_is_ldio(cmd->scmd)){
762 ldio = (struct megasas_io_frame *)cmd->frame;
763 mfi_sgl = &ldio->sgl;
764 sgcount = ldio->sge_count;
765 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no, cmd->frame_count,ldio->cmd,ldio->target_id, ldio->start_lba_lo,ldio->start_lba_hi,ldio->sense_buf_phys_addr_lo,sgcount);
768 pthru = (struct megasas_pthru_frame *) cmd->frame;
769 mfi_sgl = &pthru->sgl;
770 sgcount = pthru->sge_count;
771 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no,cmd->frame_count,pthru->cmd,pthru->target_id,pthru->lun,pthru->cdb_len , pthru->data_xfer_len,pthru->sense_buf_phys_addr_lo,sgcount);
773 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
774 for (n = 0; n < sgcount; n++){
776 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%08lx ",mfi_sgl->sge64[n].length , (unsigned long)mfi_sgl->sge64[n].phys_addr) ;
778 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
781 printk(KERN_ERR "\n");
783 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
784 for (i = 0; i < max_cmd; i++) {
786 cmd = instance->cmd_list[i];
788 if(cmd->sync_cmd == 1){
789 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
792 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
796 * megasas_queue_command - Queue entry point
797 * @scmd: SCSI command to be queued
798 * @done: Callback entry point
801 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
804 struct megasas_cmd *cmd;
805 struct megasas_instance *instance;
807 instance = (struct megasas_instance *)
808 scmd->device->host->hostdata;
810 /* Don't process if we have already declared adapter dead */
811 if (instance->hw_crit_error)
812 return SCSI_MLQUEUE_HOST_BUSY;
814 scmd->scsi_done = done;
817 if (MEGASAS_IS_LOGICAL(scmd) &&
818 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
819 scmd->result = DID_BAD_TARGET << 16;
823 switch (scmd->cmnd[0]) {
824 case SYNCHRONIZE_CACHE:
826 * FW takes care of flush cache on its own
827 * No need to send it down
829 scmd->result = DID_OK << 16;
835 cmd = megasas_get_cmd(instance);
837 return SCSI_MLQUEUE_HOST_BUSY;
840 * Logical drive command
842 if (megasas_is_ldio(scmd))
843 frame_count = megasas_build_ldio(instance, scmd, cmd);
845 frame_count = megasas_build_dcdb(instance, scmd, cmd);
851 scmd->SCp.ptr = (char *)cmd;
854 * Issue the command to the FW
856 atomic_inc(&instance->fw_outstanding);
858 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
863 megasas_return_cmd(instance, cmd);
869 static int megasas_slave_configure(struct scsi_device *sdev)
872 * Don't export physical disk devices to the disk driver.
874 * FIXME: Currently we don't export them to the midlayer at all.
875 * That will be fixed once LSI engineers have audited the
876 * firmware for possible issues.
878 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
882 * The RAID firmware may require extended timeouts.
884 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
885 sdev->timeout = MEGASAS_DEFAULT_CMD_TIMEOUT * HZ;
890 * megasas_wait_for_outstanding - Wait for all outstanding cmds
891 * @instance: Adapter soft state
893 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
894 * complete all its outstanding commands. Returns error if one or more IOs
895 * are pending after this time period. It also marks the controller dead.
897 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
900 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
902 for (i = 0; i < wait_time; i++) {
904 int outstanding = atomic_read(&instance->fw_outstanding);
909 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
910 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
911 "commands to complete\n",i,outstanding);
917 if (atomic_read(&instance->fw_outstanding)) {
919 * Send signal to FW to stop processing any pending cmds.
920 * The controller will be taken offline by the OS now.
923 &instance->reg_set->inbound_doorbell);
924 megasas_dump_pending_frames(instance);
925 instance->hw_crit_error = 1;
933 * megasas_generic_reset - Generic reset routine
934 * @scmd: Mid-layer SCSI command
936 * This routine implements a generic reset handler for device, bus and host
937 * reset requests. Device, bus and host specific reset handlers can use this
938 * function after they do their specific tasks.
940 static int megasas_generic_reset(struct scsi_cmnd *scmd)
943 struct megasas_instance *instance;
945 instance = (struct megasas_instance *)scmd->device->host->hostdata;
947 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
948 scmd->serial_number, scmd->cmnd[0], scmd->retries);
950 if (instance->hw_crit_error) {
951 printk(KERN_ERR "megasas: cannot recover from previous reset "
956 ret_val = megasas_wait_for_outstanding(instance);
957 if (ret_val == SUCCESS)
958 printk(KERN_NOTICE "megasas: reset successful \n");
960 printk(KERN_ERR "megasas: failed to do reset\n");
966 * megasas_reset_timer - quiesce the adapter if required
969 * Sets the FW busy flag and reduces the host->can_queue if the
970 * cmd has not been completed within the timeout period.
973 scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
975 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
976 struct megasas_instance *instance;
979 if (time_after(jiffies, scmd->jiffies_at_alloc +
980 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
981 return EH_NOT_HANDLED;
984 instance = cmd->instance;
985 if (!(instance->flag & MEGASAS_FW_BUSY)) {
986 /* FW is busy, throttle IO */
987 spin_lock_irqsave(instance->host->host_lock, flags);
989 instance->host->can_queue = 16;
990 instance->last_time = jiffies;
991 instance->flag |= MEGASAS_FW_BUSY;
993 spin_unlock_irqrestore(instance->host->host_lock, flags);
995 return EH_RESET_TIMER;
999 * megasas_reset_device - Device reset handler entry point
1001 static int megasas_reset_device(struct scsi_cmnd *scmd)
1006 * First wait for all commands to complete
1008 ret = megasas_generic_reset(scmd);
1014 * megasas_reset_bus_host - Bus & host reset handler entry point
1016 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1021 * First wait for all commands to complete
1023 ret = megasas_generic_reset(scmd);
1029 * megasas_bios_param - Returns disk geometry for a disk
1030 * @sdev: device handle
1031 * @bdev: block device
1032 * @capacity: drive capacity
1033 * @geom: geometry parameters
1036 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1037 sector_t capacity, int geom[])
1043 /* Default heads (64) & sectors (32) */
1047 tmp = heads * sectors;
1048 cylinders = capacity;
1050 sector_div(cylinders, tmp);
1053 * Handle extended translation size for logical drives > 1Gb
1056 if (capacity >= 0x200000) {
1059 tmp = heads*sectors;
1060 cylinders = capacity;
1061 sector_div(cylinders, tmp);
1066 geom[2] = cylinders;
1072 * megasas_service_aen - Processes an event notification
1073 * @instance: Adapter soft state
1074 * @cmd: AEN command completed by the ISR
1076 * For AEN, driver sends a command down to FW that is held by the FW till an
1077 * event occurs. When an event of interest occurs, FW completes the command
1078 * that it was previously holding.
1080 * This routines sends SIGIO signal to processes that have registered with the
1084 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1087 * Don't signal app if it is just an aborted previously registered aen
1089 if (!cmd->abort_aen)
1090 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1094 instance->aen_cmd = NULL;
1095 megasas_return_cmd(instance, cmd);
1099 * Scsi host template for megaraid_sas driver
1101 static struct scsi_host_template megasas_template = {
1103 .module = THIS_MODULE,
1104 .name = "LSI Logic SAS based MegaRAID driver",
1105 .proc_name = "megaraid_sas",
1106 .slave_configure = megasas_slave_configure,
1107 .queuecommand = megasas_queue_command,
1108 .eh_device_reset_handler = megasas_reset_device,
1109 .eh_bus_reset_handler = megasas_reset_bus_host,
1110 .eh_host_reset_handler = megasas_reset_bus_host,
1111 .eh_timed_out = megasas_reset_timer,
1112 .bios_param = megasas_bios_param,
1113 .use_clustering = ENABLE_CLUSTERING,
1114 .use_sg_chaining = ENABLE_SG_CHAINING,
1118 * megasas_complete_int_cmd - Completes an internal command
1119 * @instance: Adapter soft state
1120 * @cmd: Command to be completed
1122 * The megasas_issue_blocked_cmd() function waits for a command to complete
1123 * after it issues a command. This function wakes up that waiting routine by
1124 * calling wake_up() on the wait queue.
1127 megasas_complete_int_cmd(struct megasas_instance *instance,
1128 struct megasas_cmd *cmd)
1130 cmd->cmd_status = cmd->frame->io.cmd_status;
1132 if (cmd->cmd_status == ENODATA) {
1133 cmd->cmd_status = 0;
1135 wake_up(&instance->int_cmd_wait_q);
1139 * megasas_complete_abort - Completes aborting a command
1140 * @instance: Adapter soft state
1141 * @cmd: Cmd that was issued to abort another cmd
1143 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1144 * after it issues an abort on a previously issued command. This function
1145 * wakes up all functions waiting on the same wait queue.
1148 megasas_complete_abort(struct megasas_instance *instance,
1149 struct megasas_cmd *cmd)
1151 if (cmd->sync_cmd) {
1153 cmd->cmd_status = 0;
1154 wake_up(&instance->abort_cmd_wait_q);
1161 * megasas_complete_cmd - Completes a command
1162 * @instance: Adapter soft state
1163 * @cmd: Command to be completed
1164 * @alt_status: If non-zero, use this value as status to
1165 * SCSI mid-layer instead of the value returned
1166 * by the FW. This should be used if caller wants
1167 * an alternate status (as in the case of aborted
1171 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1175 struct megasas_header *hdr = &cmd->frame->hdr;
1178 cmd->scmd->SCp.ptr = NULL;
1182 case MFI_CMD_PD_SCSI_IO:
1183 case MFI_CMD_LD_SCSI_IO:
1186 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1187 * issued either through an IO path or an IOCTL path. If it
1188 * was via IOCTL, we will send it to internal completion.
1190 if (cmd->sync_cmd) {
1192 megasas_complete_int_cmd(instance, cmd);
1196 case MFI_CMD_LD_READ:
1197 case MFI_CMD_LD_WRITE:
1200 cmd->scmd->result = alt_status << 16;
1206 atomic_dec(&instance->fw_outstanding);
1208 scsi_dma_unmap(cmd->scmd);
1209 cmd->scmd->scsi_done(cmd->scmd);
1210 megasas_return_cmd(instance, cmd);
1215 switch (hdr->cmd_status) {
1218 cmd->scmd->result = DID_OK << 16;
1221 case MFI_STAT_SCSI_IO_FAILED:
1222 case MFI_STAT_LD_INIT_IN_PROGRESS:
1224 (DID_ERROR << 16) | hdr->scsi_status;
1227 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1229 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1231 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1232 memset(cmd->scmd->sense_buffer, 0,
1233 SCSI_SENSE_BUFFERSIZE);
1234 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1237 cmd->scmd->result |= DRIVER_SENSE << 24;
1242 case MFI_STAT_LD_OFFLINE:
1243 case MFI_STAT_DEVICE_NOT_FOUND:
1244 cmd->scmd->result = DID_BAD_TARGET << 16;
1248 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1250 cmd->scmd->result = DID_ERROR << 16;
1254 atomic_dec(&instance->fw_outstanding);
1256 scsi_dma_unmap(cmd->scmd);
1257 cmd->scmd->scsi_done(cmd->scmd);
1258 megasas_return_cmd(instance, cmd);
1267 * See if got an event notification
1269 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1270 megasas_service_aen(instance, cmd);
1272 megasas_complete_int_cmd(instance, cmd);
1278 * Cmd issued to abort another cmd returned
1280 megasas_complete_abort(instance, cmd);
1284 printk("megasas: Unknown command completed! [0x%X]\n",
1291 * megasas_deplete_reply_queue - Processes all completed commands
1292 * @instance: Adapter soft state
1293 * @alt_status: Alternate status to be returned to
1294 * SCSI mid-layer instead of the status
1295 * returned by the FW
1298 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1301 * Check if it is our interrupt
1302 * Clear the interrupt
1304 if(instance->instancet->clear_intr(instance->reg_set))
1307 if (instance->hw_crit_error)
1310 * Schedule the tasklet for cmd completion
1312 tasklet_schedule(&instance->isr_tasklet);
1318 * megasas_isr - isr entry point
1320 static irqreturn_t megasas_isr(int irq, void *devp)
1322 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1327 * megasas_transition_to_ready - Move the FW to READY state
1328 * @instance: Adapter soft state
1330 * During the initialization, FW passes can potentially be in any one of
1331 * several possible states. If the FW in operational, waiting-for-handshake
1332 * states, driver must take steps to bring it to ready state. Otherwise, it
1333 * has to wait for the ready state.
1336 megasas_transition_to_ready(struct megasas_instance* instance)
1343 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
1345 if (fw_state != MFI_STATE_READY)
1346 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1349 while (fw_state != MFI_STATE_READY) {
1353 case MFI_STATE_FAULT:
1355 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1358 case MFI_STATE_WAIT_HANDSHAKE:
1360 * Set the CLR bit in inbound doorbell
1362 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1363 &instance->reg_set->inbound_doorbell);
1366 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1369 case MFI_STATE_BOOT_MESSAGE_PENDING:
1370 writel(MFI_INIT_HOTPLUG,
1371 &instance->reg_set->inbound_doorbell);
1374 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1377 case MFI_STATE_OPERATIONAL:
1379 * Bring it to READY state; assuming max wait 10 secs
1381 instance->instancet->disable_intr(instance->reg_set);
1382 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
1385 cur_state = MFI_STATE_OPERATIONAL;
1388 case MFI_STATE_UNDEFINED:
1390 * This state should not last for more than 2 seconds
1393 cur_state = MFI_STATE_UNDEFINED;
1396 case MFI_STATE_BB_INIT:
1398 cur_state = MFI_STATE_BB_INIT;
1401 case MFI_STATE_FW_INIT:
1403 cur_state = MFI_STATE_FW_INIT;
1406 case MFI_STATE_FW_INIT_2:
1408 cur_state = MFI_STATE_FW_INIT_2;
1411 case MFI_STATE_DEVICE_SCAN:
1413 cur_state = MFI_STATE_DEVICE_SCAN;
1416 case MFI_STATE_FLUSH_CACHE:
1418 cur_state = MFI_STATE_FLUSH_CACHE;
1422 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1428 * The cur_state should not last for more than max_wait secs
1430 for (i = 0; i < (max_wait * 1000); i++) {
1431 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1434 if (fw_state == cur_state) {
1441 * Return error if fw_state hasn't changed after max_wait
1443 if (fw_state == cur_state) {
1444 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1445 "in %d secs\n", fw_state, max_wait);
1449 printk(KERN_INFO "megasas: FW now in Ready state\n");
1455 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1456 * @instance: Adapter soft state
1458 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1461 u32 max_cmd = instance->max_fw_cmds;
1462 struct megasas_cmd *cmd;
1464 if (!instance->frame_dma_pool)
1468 * Return all frames to pool
1470 for (i = 0; i < max_cmd; i++) {
1472 cmd = instance->cmd_list[i];
1475 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1476 cmd->frame_phys_addr);
1479 pci_pool_free(instance->sense_dma_pool, cmd->sense,
1480 cmd->sense_phys_addr);
1484 * Now destroy the pool itself
1486 pci_pool_destroy(instance->frame_dma_pool);
1487 pci_pool_destroy(instance->sense_dma_pool);
1489 instance->frame_dma_pool = NULL;
1490 instance->sense_dma_pool = NULL;
1494 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1495 * @instance: Adapter soft state
1497 * Each command packet has an embedded DMA memory buffer that is used for
1498 * filling MFI frame and the SG list that immediately follows the frame. This
1499 * function creates those DMA memory buffers for each command packet by using
1500 * PCI pool facility.
1502 static int megasas_create_frame_pool(struct megasas_instance *instance)
1510 struct megasas_cmd *cmd;
1512 max_cmd = instance->max_fw_cmds;
1515 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1516 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1518 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1519 sizeof(struct megasas_sge32);
1522 * Calculated the number of 64byte frames required for SGL
1524 sgl_sz = sge_sz * instance->max_num_sge;
1525 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1528 * We need one extra frame for the MFI command
1532 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1534 * Use DMA pool facility provided by PCI layer
1536 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1537 instance->pdev, total_sz, 64,
1540 if (!instance->frame_dma_pool) {
1541 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1545 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1546 instance->pdev, 128, 4, 0);
1548 if (!instance->sense_dma_pool) {
1549 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1551 pci_pool_destroy(instance->frame_dma_pool);
1552 instance->frame_dma_pool = NULL;
1558 * Allocate and attach a frame to each of the commands in cmd_list.
1559 * By making cmd->index as the context instead of the &cmd, we can
1560 * always use 32bit context regardless of the architecture
1562 for (i = 0; i < max_cmd; i++) {
1564 cmd = instance->cmd_list[i];
1566 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1567 GFP_KERNEL, &cmd->frame_phys_addr);
1569 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1570 GFP_KERNEL, &cmd->sense_phys_addr);
1573 * megasas_teardown_frame_pool() takes care of freeing
1574 * whatever has been allocated
1576 if (!cmd->frame || !cmd->sense) {
1577 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1578 megasas_teardown_frame_pool(instance);
1582 cmd->frame->io.context = cmd->index;
1589 * megasas_free_cmds - Free all the cmds in the free cmd pool
1590 * @instance: Adapter soft state
1592 static void megasas_free_cmds(struct megasas_instance *instance)
1595 /* First free the MFI frame pool */
1596 megasas_teardown_frame_pool(instance);
1598 /* Free all the commands in the cmd_list */
1599 for (i = 0; i < instance->max_fw_cmds; i++)
1600 kfree(instance->cmd_list[i]);
1602 /* Free the cmd_list buffer itself */
1603 kfree(instance->cmd_list);
1604 instance->cmd_list = NULL;
1606 INIT_LIST_HEAD(&instance->cmd_pool);
1610 * megasas_alloc_cmds - Allocates the command packets
1611 * @instance: Adapter soft state
1613 * Each command that is issued to the FW, whether IO commands from the OS or
1614 * internal commands like IOCTLs, are wrapped in local data structure called
1615 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1618 * Each frame has a 32-bit field called context (tag). This context is used
1619 * to get back the megasas_cmd from the frame when a frame gets completed in
1620 * the ISR. Typically the address of the megasas_cmd itself would be used as
1621 * the context. But we wanted to keep the differences between 32 and 64 bit
1622 * systems to the mininum. We always use 32 bit integers for the context. In
1623 * this driver, the 32 bit values are the indices into an array cmd_list.
1624 * This array is used only to look up the megasas_cmd given the context. The
1625 * free commands themselves are maintained in a linked list called cmd_pool.
1627 static int megasas_alloc_cmds(struct megasas_instance *instance)
1632 struct megasas_cmd *cmd;
1634 max_cmd = instance->max_fw_cmds;
1637 * instance->cmd_list is an array of struct megasas_cmd pointers.
1638 * Allocate the dynamic array first and then allocate individual
1641 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
1643 if (!instance->cmd_list) {
1644 printk(KERN_DEBUG "megasas: out of memory\n");
1649 for (i = 0; i < max_cmd; i++) {
1650 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1653 if (!instance->cmd_list[i]) {
1655 for (j = 0; j < i; j++)
1656 kfree(instance->cmd_list[j]);
1658 kfree(instance->cmd_list);
1659 instance->cmd_list = NULL;
1666 * Add all the commands to command pool (instance->cmd_pool)
1668 for (i = 0; i < max_cmd; i++) {
1669 cmd = instance->cmd_list[i];
1670 memset(cmd, 0, sizeof(struct megasas_cmd));
1672 cmd->instance = instance;
1674 list_add_tail(&cmd->list, &instance->cmd_pool);
1678 * Create a frame pool and assign one frame to each cmd
1680 if (megasas_create_frame_pool(instance)) {
1681 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1682 megasas_free_cmds(instance);
1689 * megasas_get_controller_info - Returns FW's controller structure
1690 * @instance: Adapter soft state
1691 * @ctrl_info: Controller information structure
1693 * Issues an internal command (DCMD) to get the FW's controller structure.
1694 * This information is mainly used to find out the maximum IO transfer per
1695 * command supported by the FW.
1698 megasas_get_ctrl_info(struct megasas_instance *instance,
1699 struct megasas_ctrl_info *ctrl_info)
1702 struct megasas_cmd *cmd;
1703 struct megasas_dcmd_frame *dcmd;
1704 struct megasas_ctrl_info *ci;
1705 dma_addr_t ci_h = 0;
1707 cmd = megasas_get_cmd(instance);
1710 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1714 dcmd = &cmd->frame->dcmd;
1716 ci = pci_alloc_consistent(instance->pdev,
1717 sizeof(struct megasas_ctrl_info), &ci_h);
1720 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1721 megasas_return_cmd(instance, cmd);
1725 memset(ci, 0, sizeof(*ci));
1726 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1728 dcmd->cmd = MFI_CMD_DCMD;
1729 dcmd->cmd_status = 0xFF;
1730 dcmd->sge_count = 1;
1731 dcmd->flags = MFI_FRAME_DIR_READ;
1733 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1734 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1735 dcmd->sgl.sge32[0].phys_addr = ci_h;
1736 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1738 if (!megasas_issue_polled(instance, cmd)) {
1740 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1745 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1748 megasas_return_cmd(instance, cmd);
1753 * megasas_complete_cmd_dpc - Returns FW's controller structure
1754 * @instance_addr: Address of adapter soft state
1756 * Tasklet to complete cmds
1758 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1763 struct megasas_cmd *cmd;
1764 struct megasas_instance *instance = (struct megasas_instance *)instance_addr;
1765 unsigned long flags;
1767 /* If we have already declared adapter dead, donot complete cmds */
1768 if (instance->hw_crit_error)
1771 producer = *instance->producer;
1772 consumer = *instance->consumer;
1774 while (consumer != producer) {
1775 context = instance->reply_queue[consumer];
1777 cmd = instance->cmd_list[context];
1779 megasas_complete_cmd(instance, cmd, DID_OK);
1782 if (consumer == (instance->max_fw_cmds + 1)) {
1787 *instance->consumer = producer;
1790 * Check if we can restore can_queue
1792 if (instance->flag & MEGASAS_FW_BUSY
1793 && time_after(jiffies, instance->last_time + 5 * HZ)
1794 && atomic_read(&instance->fw_outstanding) < 17) {
1796 spin_lock_irqsave(instance->host->host_lock, flags);
1797 instance->flag &= ~MEGASAS_FW_BUSY;
1798 instance->host->can_queue =
1799 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1801 spin_unlock_irqrestore(instance->host->host_lock, flags);
1807 * megasas_init_mfi - Initializes the FW
1808 * @instance: Adapter soft state
1810 * This is the main function for initializing MFI firmware.
1812 static int megasas_init_mfi(struct megasas_instance *instance)
1818 struct megasas_register_set __iomem *reg_set;
1820 struct megasas_cmd *cmd;
1821 struct megasas_ctrl_info *ctrl_info;
1823 struct megasas_init_frame *init_frame;
1824 struct megasas_init_queue_info *initq_info;
1825 dma_addr_t init_frame_h;
1826 dma_addr_t initq_info_h;
1829 * Map the message registers
1831 instance->base_addr = pci_resource_start(instance->pdev, 0);
1833 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1834 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1838 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1840 if (!instance->reg_set) {
1841 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1845 reg_set = instance->reg_set;
1847 switch(instance->pdev->device)
1849 case PCI_DEVICE_ID_LSI_SAS1078R:
1850 instance->instancet = &megasas_instance_template_ppc;
1852 case PCI_DEVICE_ID_LSI_SAS1064R:
1853 case PCI_DEVICE_ID_DELL_PERC5:
1855 instance->instancet = &megasas_instance_template_xscale;
1860 * We expect the FW state to be READY
1862 if (megasas_transition_to_ready(instance))
1863 goto fail_ready_state;
1866 * Get various operational parameters from status register
1868 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
1870 * Reduce the max supported cmds by 1. This is to ensure that the
1871 * reply_q_sz (1 more than the max cmd that driver may send)
1872 * does not exceed max cmds that the FW can support
1874 instance->max_fw_cmds = instance->max_fw_cmds-1;
1875 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
1878 * Create a pool of commands
1880 if (megasas_alloc_cmds(instance))
1881 goto fail_alloc_cmds;
1884 * Allocate memory for reply queue. Length of reply queue should
1885 * be _one_ more than the maximum commands handled by the firmware.
1887 * Note: When FW completes commands, it places corresponding contex
1888 * values in this circular reply queue. This circular queue is a fairly
1889 * typical producer-consumer queue. FW is the producer (of completed
1890 * commands) and the driver is the consumer.
1892 context_sz = sizeof(u32);
1893 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1895 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1897 &instance->reply_queue_h);
1899 if (!instance->reply_queue) {
1900 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1901 goto fail_reply_queue;
1905 * Prepare a init frame. Note the init frame points to queue info
1906 * structure. Each frame has SGL allocated after first 64 bytes. For
1907 * this frame - since we don't need any SGL - we use SGL's space as
1908 * queue info structure
1910 * We will not get a NULL command below. We just created the pool.
1912 cmd = megasas_get_cmd(instance);
1914 init_frame = (struct megasas_init_frame *)cmd->frame;
1915 initq_info = (struct megasas_init_queue_info *)
1916 ((unsigned long)init_frame + 64);
1918 init_frame_h = cmd->frame_phys_addr;
1919 initq_info_h = init_frame_h + 64;
1921 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1922 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1924 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1925 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1927 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1928 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1930 init_frame->cmd = MFI_CMD_INIT;
1931 init_frame->cmd_status = 0xFF;
1932 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1934 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1937 * disable the intr before firing the init frame to FW
1939 instance->instancet->disable_intr(instance->reg_set);
1942 * Issue the init frame in polled mode
1944 if (megasas_issue_polled(instance, cmd)) {
1945 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1949 megasas_return_cmd(instance, cmd);
1951 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1954 * Compute the max allowed sectors per IO: The controller info has two
1955 * limits on max sectors. Driver should use the minimum of these two.
1957 * 1 << stripe_sz_ops.min = max sectors per strip
1959 * Note that older firmwares ( < FW ver 30) didn't report information
1960 * to calculate max_sectors_1. So the number ended up as zero always.
1962 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1964 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1965 ctrl_info->max_strips_per_io;
1966 max_sectors_2 = ctrl_info->max_request_size;
1968 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1969 ? max_sectors_1 : max_sectors_2;
1971 instance->max_sectors_per_req = instance->max_num_sge *
1977 * Setup tasklet for cmd completion
1980 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
1981 (unsigned long)instance);
1985 megasas_return_cmd(instance, cmd);
1987 pci_free_consistent(instance->pdev, reply_q_sz,
1988 instance->reply_queue, instance->reply_queue_h);
1990 megasas_free_cmds(instance);
1994 iounmap(instance->reg_set);
1997 pci_release_regions(instance->pdev);
2003 * megasas_release_mfi - Reverses the FW initialization
2004 * @intance: Adapter soft state
2006 static void megasas_release_mfi(struct megasas_instance *instance)
2008 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2010 pci_free_consistent(instance->pdev, reply_q_sz,
2011 instance->reply_queue, instance->reply_queue_h);
2013 megasas_free_cmds(instance);
2015 iounmap(instance->reg_set);
2017 pci_release_regions(instance->pdev);
2021 * megasas_get_seq_num - Gets latest event sequence numbers
2022 * @instance: Adapter soft state
2023 * @eli: FW event log sequence numbers information
2025 * FW maintains a log of all events in a non-volatile area. Upper layers would
2026 * usually find out the latest sequence number of the events, the seq number at
2027 * the boot etc. They would "read" all the events below the latest seq number
2028 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2029 * number), they would subsribe to AEN (asynchronous event notification) and
2030 * wait for the events to happen.
2033 megasas_get_seq_num(struct megasas_instance *instance,
2034 struct megasas_evt_log_info *eli)
2036 struct megasas_cmd *cmd;
2037 struct megasas_dcmd_frame *dcmd;
2038 struct megasas_evt_log_info *el_info;
2039 dma_addr_t el_info_h = 0;
2041 cmd = megasas_get_cmd(instance);
2047 dcmd = &cmd->frame->dcmd;
2048 el_info = pci_alloc_consistent(instance->pdev,
2049 sizeof(struct megasas_evt_log_info),
2053 megasas_return_cmd(instance, cmd);
2057 memset(el_info, 0, sizeof(*el_info));
2058 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2060 dcmd->cmd = MFI_CMD_DCMD;
2061 dcmd->cmd_status = 0x0;
2062 dcmd->sge_count = 1;
2063 dcmd->flags = MFI_FRAME_DIR_READ;
2065 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2066 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2067 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2068 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2070 megasas_issue_blocked_cmd(instance, cmd);
2073 * Copy the data back into callers buffer
2075 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2077 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2078 el_info, el_info_h);
2080 megasas_return_cmd(instance, cmd);
2086 * megasas_register_aen - Registers for asynchronous event notification
2087 * @instance: Adapter soft state
2088 * @seq_num: The starting sequence number
2089 * @class_locale: Class of the event
2091 * This function subscribes for AEN for events beyond the @seq_num. It requests
2092 * to be notified if and only if the event is of type @class_locale
2095 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2096 u32 class_locale_word)
2099 struct megasas_cmd *cmd;
2100 struct megasas_dcmd_frame *dcmd;
2101 union megasas_evt_class_locale curr_aen;
2102 union megasas_evt_class_locale prev_aen;
2105 * If there an AEN pending already (aen_cmd), check if the
2106 * class_locale of that pending AEN is inclusive of the new
2107 * AEN request we currently have. If it is, then we don't have
2108 * to do anything. In other words, whichever events the current
2109 * AEN request is subscribing to, have already been subscribed
2112 * If the old_cmd is _not_ inclusive, then we have to abort
2113 * that command, form a class_locale that is superset of both
2114 * old and current and re-issue to the FW
2117 curr_aen.word = class_locale_word;
2119 if (instance->aen_cmd) {
2121 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2124 * A class whose enum value is smaller is inclusive of all
2125 * higher values. If a PROGRESS (= -1) was previously
2126 * registered, then a new registration requests for higher
2127 * classes need not be sent to FW. They are automatically
2130 * Locale numbers don't have such hierarchy. They are bitmap
2133 if ((prev_aen.members.class <= curr_aen.members.class) &&
2134 !((prev_aen.members.locale & curr_aen.members.locale) ^
2135 curr_aen.members.locale)) {
2137 * Previously issued event registration includes
2138 * current request. Nothing to do.
2142 curr_aen.members.locale |= prev_aen.members.locale;
2144 if (prev_aen.members.class < curr_aen.members.class)
2145 curr_aen.members.class = prev_aen.members.class;
2147 instance->aen_cmd->abort_aen = 1;
2148 ret_val = megasas_issue_blocked_abort_cmd(instance,
2153 printk(KERN_DEBUG "megasas: Failed to abort "
2154 "previous AEN command\n");
2160 cmd = megasas_get_cmd(instance);
2165 dcmd = &cmd->frame->dcmd;
2167 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2170 * Prepare DCMD for aen registration
2172 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2174 dcmd->cmd = MFI_CMD_DCMD;
2175 dcmd->cmd_status = 0x0;
2176 dcmd->sge_count = 1;
2177 dcmd->flags = MFI_FRAME_DIR_READ;
2179 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2180 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2181 dcmd->mbox.w[0] = seq_num;
2182 dcmd->mbox.w[1] = curr_aen.word;
2183 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2184 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2187 * Store reference to the cmd used to register for AEN. When an
2188 * application wants us to register for AEN, we have to abort this
2189 * cmd and re-register with a new EVENT LOCALE supplied by that app
2191 instance->aen_cmd = cmd;
2194 * Issue the aen registration frame
2196 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
2202 * megasas_start_aen - Subscribes to AEN during driver load time
2203 * @instance: Adapter soft state
2205 static int megasas_start_aen(struct megasas_instance *instance)
2207 struct megasas_evt_log_info eli;
2208 union megasas_evt_class_locale class_locale;
2211 * Get the latest sequence number from FW
2213 memset(&eli, 0, sizeof(eli));
2215 if (megasas_get_seq_num(instance, &eli))
2219 * Register AEN with FW for latest sequence number plus 1
2221 class_locale.members.reserved = 0;
2222 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2223 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2225 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2230 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2231 * @instance: Adapter soft state
2233 static int megasas_io_attach(struct megasas_instance *instance)
2235 struct Scsi_Host *host = instance->host;
2238 * Export parameters required by SCSI mid-layer
2240 host->irq = instance->pdev->irq;
2241 host->unique_id = instance->unique_id;
2242 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2243 host->this_id = instance->init_id;
2244 host->sg_tablesize = instance->max_num_sge;
2245 host->max_sectors = instance->max_sectors_per_req;
2246 host->cmd_per_lun = 128;
2247 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2248 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2249 host->max_lun = MEGASAS_MAX_LUN;
2250 host->max_cmd_len = 16;
2253 * Notify the mid-layer about the new controller
2255 if (scsi_add_host(host, &instance->pdev->dev)) {
2256 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2261 * Trigger SCSI to scan our drives
2263 scsi_scan_host(host);
2268 * megasas_probe_one - PCI hotplug entry point
2269 * @pdev: PCI device structure
2270 * @id: PCI ids of supported hotplugged adapter
2272 static int __devinit
2273 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2276 struct Scsi_Host *host;
2277 struct megasas_instance *instance;
2280 * Announce PCI information
2282 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2283 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2284 pdev->subsystem_device);
2286 printk("bus %d:slot %d:func %d\n",
2287 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2290 * PCI prepping: enable device set bus mastering and dma mask
2292 rval = pci_enable_device(pdev);
2298 pci_set_master(pdev);
2301 * All our contollers are capable of performing 64-bit DMA
2304 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2306 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2307 goto fail_set_dma_mask;
2310 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2311 goto fail_set_dma_mask;
2314 host = scsi_host_alloc(&megasas_template,
2315 sizeof(struct megasas_instance));
2318 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2319 goto fail_alloc_instance;
2322 instance = (struct megasas_instance *)host->hostdata;
2323 memset(instance, 0, sizeof(*instance));
2325 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2326 &instance->producer_h);
2327 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2328 &instance->consumer_h);
2330 if (!instance->producer || !instance->consumer) {
2331 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2332 "producer, consumer\n");
2333 goto fail_alloc_dma_buf;
2336 *instance->producer = 0;
2337 *instance->consumer = 0;
2339 instance->evt_detail = pci_alloc_consistent(pdev,
2341 megasas_evt_detail),
2342 &instance->evt_detail_h);
2344 if (!instance->evt_detail) {
2345 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2346 "event detail structure\n");
2347 goto fail_alloc_dma_buf;
2351 * Initialize locks and queues
2353 INIT_LIST_HEAD(&instance->cmd_pool);
2355 atomic_set(&instance->fw_outstanding,0);
2357 init_waitqueue_head(&instance->int_cmd_wait_q);
2358 init_waitqueue_head(&instance->abort_cmd_wait_q);
2360 spin_lock_init(&instance->cmd_pool_lock);
2362 mutex_init(&instance->aen_mutex);
2363 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2366 * Initialize PCI related and misc parameters
2368 instance->pdev = pdev;
2369 instance->host = host;
2370 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2371 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2373 megasas_dbg_lvl = 0;
2375 instance->last_time = 0;
2378 * Initialize MFI Firmware
2380 if (megasas_init_mfi(instance))
2386 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
2387 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2391 instance->instancet->enable_intr(instance->reg_set);
2394 * Store instance in PCI softstate
2396 pci_set_drvdata(pdev, instance);
2399 * Add this controller to megasas_mgmt_info structure so that it
2400 * can be exported to management applications
2402 megasas_mgmt_info.count++;
2403 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2404 megasas_mgmt_info.max_index++;
2407 * Initiate AEN (Asynchronous Event Notification)
2409 if (megasas_start_aen(instance)) {
2410 printk(KERN_DEBUG "megasas: start aen failed\n");
2411 goto fail_start_aen;
2415 * Register with SCSI mid-layer
2417 if (megasas_io_attach(instance))
2418 goto fail_io_attach;
2424 megasas_mgmt_info.count--;
2425 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2426 megasas_mgmt_info.max_index--;
2428 pci_set_drvdata(pdev, NULL);
2429 instance->instancet->disable_intr(instance->reg_set);
2430 free_irq(instance->pdev->irq, instance);
2432 megasas_release_mfi(instance);
2437 if (instance->evt_detail)
2438 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2439 instance->evt_detail,
2440 instance->evt_detail_h);
2442 if (instance->producer)
2443 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2444 instance->producer_h);
2445 if (instance->consumer)
2446 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2447 instance->consumer_h);
2448 scsi_host_put(host);
2450 fail_alloc_instance:
2452 pci_disable_device(pdev);
2458 * megasas_flush_cache - Requests FW to flush all its caches
2459 * @instance: Adapter soft state
2461 static void megasas_flush_cache(struct megasas_instance *instance)
2463 struct megasas_cmd *cmd;
2464 struct megasas_dcmd_frame *dcmd;
2466 cmd = megasas_get_cmd(instance);
2471 dcmd = &cmd->frame->dcmd;
2473 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2475 dcmd->cmd = MFI_CMD_DCMD;
2476 dcmd->cmd_status = 0x0;
2477 dcmd->sge_count = 0;
2478 dcmd->flags = MFI_FRAME_DIR_NONE;
2480 dcmd->data_xfer_len = 0;
2481 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2482 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2484 megasas_issue_blocked_cmd(instance, cmd);
2486 megasas_return_cmd(instance, cmd);
2492 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2493 * @instance: Adapter soft state
2495 static void megasas_shutdown_controller(struct megasas_instance *instance)
2497 struct megasas_cmd *cmd;
2498 struct megasas_dcmd_frame *dcmd;
2500 cmd = megasas_get_cmd(instance);
2505 if (instance->aen_cmd)
2506 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2508 dcmd = &cmd->frame->dcmd;
2510 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2512 dcmd->cmd = MFI_CMD_DCMD;
2513 dcmd->cmd_status = 0x0;
2514 dcmd->sge_count = 0;
2515 dcmd->flags = MFI_FRAME_DIR_NONE;
2517 dcmd->data_xfer_len = 0;
2518 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2520 megasas_issue_blocked_cmd(instance, cmd);
2522 megasas_return_cmd(instance, cmd);
2528 * megasas_detach_one - PCI hot"un"plug entry point
2529 * @pdev: PCI device structure
2531 static void megasas_detach_one(struct pci_dev *pdev)
2534 struct Scsi_Host *host;
2535 struct megasas_instance *instance;
2537 instance = pci_get_drvdata(pdev);
2538 host = instance->host;
2540 scsi_remove_host(instance->host);
2541 megasas_flush_cache(instance);
2542 megasas_shutdown_controller(instance);
2543 tasklet_kill(&instance->isr_tasklet);
2546 * Take the instance off the instance array. Note that we will not
2547 * decrement the max_index. We let this array be sparse array
2549 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2550 if (megasas_mgmt_info.instance[i] == instance) {
2551 megasas_mgmt_info.count--;
2552 megasas_mgmt_info.instance[i] = NULL;
2558 pci_set_drvdata(instance->pdev, NULL);
2560 instance->instancet->disable_intr(instance->reg_set);
2562 free_irq(instance->pdev->irq, instance);
2564 megasas_release_mfi(instance);
2566 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2567 instance->evt_detail, instance->evt_detail_h);
2569 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2570 instance->producer_h);
2572 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2573 instance->consumer_h);
2575 scsi_host_put(host);
2577 pci_set_drvdata(pdev, NULL);
2579 pci_disable_device(pdev);
2585 * megasas_shutdown - Shutdown entry point
2586 * @device: Generic device structure
2588 static void megasas_shutdown(struct pci_dev *pdev)
2590 struct megasas_instance *instance = pci_get_drvdata(pdev);
2591 megasas_flush_cache(instance);
2595 * megasas_mgmt_open - char node "open" entry point
2597 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2600 * Allow only those users with admin rights
2602 if (!capable(CAP_SYS_ADMIN))
2609 * megasas_mgmt_release - char node "release" entry point
2611 static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2613 filep->private_data = NULL;
2614 fasync_helper(-1, filep, 0, &megasas_async_queue);
2620 * megasas_mgmt_fasync - Async notifier registration from applications
2622 * This function adds the calling process to a driver global queue. When an
2623 * event occurs, SIGIO will be sent to all processes in this queue.
2625 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2629 mutex_lock(&megasas_async_queue_mutex);
2631 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2633 mutex_unlock(&megasas_async_queue_mutex);
2636 /* For sanity check when we get ioctl */
2637 filep->private_data = filep;
2641 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2647 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2648 * @instance: Adapter soft state
2649 * @argp: User's ioctl packet
2652 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2653 struct megasas_iocpacket __user * user_ioc,
2654 struct megasas_iocpacket *ioc)
2656 struct megasas_sge32 *kern_sge32;
2657 struct megasas_cmd *cmd;
2658 void *kbuff_arr[MAX_IOCTL_SGE];
2659 dma_addr_t buf_handle = 0;
2662 dma_addr_t sense_handle;
2665 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2667 if (ioc->sge_count > MAX_IOCTL_SGE) {
2668 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2669 ioc->sge_count, MAX_IOCTL_SGE);
2673 cmd = megasas_get_cmd(instance);
2675 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2680 * User's IOCTL packet has 2 frames (maximum). Copy those two
2681 * frames into our cmd's frames. cmd->frame's context will get
2682 * overwritten when we copy from user's frames. So set that value
2685 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2686 cmd->frame->hdr.context = cmd->index;
2689 * The management interface between applications and the fw uses
2690 * MFI frames. E.g, RAID configuration changes, LD property changes
2691 * etc are accomplishes through different kinds of MFI frames. The
2692 * driver needs to care only about substituting user buffers with
2693 * kernel buffers in SGLs. The location of SGL is embedded in the
2694 * struct iocpacket itself.
2696 kern_sge32 = (struct megasas_sge32 *)
2697 ((unsigned long)cmd->frame + ioc->sgl_off);
2700 * For each user buffer, create a mirror buffer and copy in
2702 for (i = 0; i < ioc->sge_count; i++) {
2703 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
2704 ioc->sgl[i].iov_len,
2705 &buf_handle, GFP_KERNEL);
2706 if (!kbuff_arr[i]) {
2707 printk(KERN_DEBUG "megasas: Failed to alloc "
2708 "kernel SGL buffer for IOCTL \n");
2714 * We don't change the dma_coherent_mask, so
2715 * pci_alloc_consistent only returns 32bit addresses
2717 kern_sge32[i].phys_addr = (u32) buf_handle;
2718 kern_sge32[i].length = ioc->sgl[i].iov_len;
2721 * We created a kernel buffer corresponding to the
2722 * user buffer. Now copy in from the user buffer
2724 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2725 (u32) (ioc->sgl[i].iov_len))) {
2731 if (ioc->sense_len) {
2732 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
2733 &sense_handle, GFP_KERNEL);
2740 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2741 *sense_ptr = sense_handle;
2745 * Set the sync_cmd flag so that the ISR knows not to complete this
2746 * cmd to the SCSI mid-layer
2749 megasas_issue_blocked_cmd(instance, cmd);
2753 * copy out the kernel buffers to user buffers
2755 for (i = 0; i < ioc->sge_count; i++) {
2756 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2757 ioc->sgl[i].iov_len)) {
2764 * copy out the sense
2766 if (ioc->sense_len) {
2768 * sense_ptr points to the location that has the user
2769 * sense buffer address
2771 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2774 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2775 sense, ioc->sense_len)) {
2782 * copy the status codes returned by the fw
2784 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2785 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2786 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2792 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
2793 sense, sense_handle);
2796 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
2797 dma_free_coherent(&instance->pdev->dev,
2798 kern_sge32[i].length,
2799 kbuff_arr[i], kern_sge32[i].phys_addr);
2802 megasas_return_cmd(instance, cmd);
2806 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2810 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2812 if ((megasas_mgmt_info.instance[i]) &&
2813 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2814 return megasas_mgmt_info.instance[i];
2820 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2822 struct megasas_iocpacket __user *user_ioc =
2823 (struct megasas_iocpacket __user *)arg;
2824 struct megasas_iocpacket *ioc;
2825 struct megasas_instance *instance;
2828 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2832 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2837 instance = megasas_lookup_instance(ioc->host_no);
2844 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2846 if (down_interruptible(&instance->ioctl_sem)) {
2847 error = -ERESTARTSYS;
2850 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2851 up(&instance->ioctl_sem);
2858 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2860 struct megasas_instance *instance;
2861 struct megasas_aen aen;
2864 if (file->private_data != file) {
2865 printk(KERN_DEBUG "megasas: fasync_helper was not "
2870 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2873 instance = megasas_lookup_instance(aen.host_no);
2878 mutex_lock(&instance->aen_mutex);
2879 error = megasas_register_aen(instance, aen.seq_num,
2880 aen.class_locale_word);
2881 mutex_unlock(&instance->aen_mutex);
2886 * megasas_mgmt_ioctl - char node ioctl entry point
2889 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2892 case MEGASAS_IOC_FIRMWARE:
2893 return megasas_mgmt_ioctl_fw(file, arg);
2895 case MEGASAS_IOC_GET_AEN:
2896 return megasas_mgmt_ioctl_aen(file, arg);
2902 #ifdef CONFIG_COMPAT
2903 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2905 struct compat_megasas_iocpacket __user *cioc =
2906 (struct compat_megasas_iocpacket __user *)arg;
2907 struct megasas_iocpacket __user *ioc =
2908 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2912 if (clear_user(ioc, sizeof(*ioc)))
2915 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2916 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2917 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2918 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2919 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2920 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2923 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2926 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2927 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2928 copy_in_user(&ioc->sgl[i].iov_len,
2929 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2933 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2935 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2936 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2937 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2944 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2948 case MEGASAS_IOC_FIRMWARE32:
2949 return megasas_mgmt_compat_ioctl_fw(file, arg);
2950 case MEGASAS_IOC_GET_AEN:
2951 return megasas_mgmt_ioctl_aen(file, arg);
2959 * File operations structure for management interface
2961 static const struct file_operations megasas_mgmt_fops = {
2962 .owner = THIS_MODULE,
2963 .open = megasas_mgmt_open,
2964 .release = megasas_mgmt_release,
2965 .fasync = megasas_mgmt_fasync,
2966 .unlocked_ioctl = megasas_mgmt_ioctl,
2967 #ifdef CONFIG_COMPAT
2968 .compat_ioctl = megasas_mgmt_compat_ioctl,
2973 * PCI hotplug support registration structure
2975 static struct pci_driver megasas_pci_driver = {
2977 .name = "megaraid_sas",
2978 .id_table = megasas_pci_table,
2979 .probe = megasas_probe_one,
2980 .remove = __devexit_p(megasas_detach_one),
2981 .shutdown = megasas_shutdown,
2985 * Sysfs driver attributes
2987 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2989 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2993 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2996 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2998 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3002 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3006 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3008 return sprintf(buf,"%u",megasas_dbg_lvl);
3012 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3015 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3016 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3022 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
3023 megasas_sysfs_set_dbg_lvl);
3026 * megasas_init - Driver load entry point
3028 static int __init megasas_init(void)
3033 * Announce driver version and other information
3035 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3036 MEGASAS_EXT_VERSION);
3038 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3041 * Register character device node
3043 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3046 printk(KERN_DEBUG "megasas: failed to open device node\n");
3050 megasas_mgmt_majorno = rval;
3053 * Register ourselves as PCI hotplug module
3055 rval = pci_register_driver(&megasas_pci_driver);
3058 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
3062 rval = driver_create_file(&megasas_pci_driver.driver,
3063 &driver_attr_version);
3065 goto err_dcf_attr_ver;
3066 rval = driver_create_file(&megasas_pci_driver.driver,
3067 &driver_attr_release_date);
3069 goto err_dcf_rel_date;
3070 rval = driver_create_file(&megasas_pci_driver.driver,
3071 &driver_attr_dbg_lvl);
3073 goto err_dcf_dbg_lvl;
3077 driver_remove_file(&megasas_pci_driver.driver,
3078 &driver_attr_release_date);
3080 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3082 pci_unregister_driver(&megasas_pci_driver);
3084 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3089 * megasas_exit - Driver unload entry point
3091 static void __exit megasas_exit(void)
3093 driver_remove_file(&megasas_pci_driver.driver,
3094 &driver_attr_dbg_lvl);
3095 driver_remove_file(&megasas_pci_driver.driver,
3096 &driver_attr_release_date);
3097 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3099 pci_unregister_driver(&megasas_pci_driver);
3100 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3103 module_init(megasas_init);
3104 module_exit(megasas_exit);