3 * Linux MegaRAID driver for SAS based RAID controllers
5 * Copyright (c) 2003-2005 LSI 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.20-rc1
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/interrupt.h>
35 #include <linux/delay.h>
36 #include <linux/smp_lock.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"
51 * poll_mode_io:1- schedule complete completion from q cmd
53 static unsigned int poll_mode_io;
54 module_param_named(poll_mode_io, poll_mode_io, int, 0);
55 MODULE_PARM_DESC(poll_mode_io,
56 "Complete cmds from IO path, (default=0)");
58 MODULE_LICENSE("GPL");
59 MODULE_VERSION(MEGASAS_VERSION);
60 MODULE_AUTHOR("megaraidlinux@lsi.com");
61 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
64 * PCI ID table for all supported controllers
66 static struct pci_device_id megasas_pci_table[] = {
68 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
70 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
72 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
74 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
75 /* xscale IOP, vega */
76 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
81 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
83 static int megasas_mgmt_majorno;
84 static struct megasas_mgmt_info megasas_mgmt_info;
85 static struct fasync_struct *megasas_async_queue;
86 static DEFINE_MUTEX(megasas_async_queue_mutex);
88 static u32 megasas_dbg_lvl;
91 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
95 * megasas_get_cmd - Get a command from the free pool
96 * @instance: Adapter soft state
98 * Returns a free command from the pool
100 static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
104 struct megasas_cmd *cmd = NULL;
106 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
108 if (!list_empty(&instance->cmd_pool)) {
109 cmd = list_entry((&instance->cmd_pool)->next,
110 struct megasas_cmd, list);
111 list_del_init(&cmd->list);
113 printk(KERN_ERR "megasas: Command pool empty!\n");
116 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
121 * megasas_return_cmd - Return a cmd to free command pool
122 * @instance: Adapter soft state
123 * @cmd: Command packet to be returned to free command pool
126 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
130 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
133 list_add_tail(&cmd->list, &instance->cmd_pool);
135 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
140 * The following functions are defined for xscale
141 * (deviceid : 1064R, PERC5) controllers
145 * megasas_enable_intr_xscale - Enables interrupts
146 * @regs: MFI register set
149 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
151 writel(1, &(regs)->outbound_intr_mask);
153 /* Dummy readl to force pci flush */
154 readl(®s->outbound_intr_mask);
158 * megasas_disable_intr_xscale -Disables interrupt
159 * @regs: MFI register set
162 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
165 writel(mask, ®s->outbound_intr_mask);
166 /* Dummy readl to force pci flush */
167 readl(®s->outbound_intr_mask);
171 * megasas_read_fw_status_reg_xscale - returns the current FW status value
172 * @regs: MFI register set
175 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
177 return readl(&(regs)->outbound_msg_0);
180 * megasas_clear_interrupt_xscale - Check & clear interrupt
181 * @regs: MFI register set
184 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
188 * Check if it is our interrupt
190 status = readl(®s->outbound_intr_status);
192 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
197 * Clear the interrupt by writing back the same value
199 writel(status, ®s->outbound_intr_status);
205 * megasas_fire_cmd_xscale - Sends command to the FW
206 * @frame_phys_addr : Physical address of cmd
207 * @frame_count : Number of frames for the command
208 * @regs : MFI register set
211 megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
213 writel((frame_phys_addr >> 3)|(frame_count),
214 &(regs)->inbound_queue_port);
217 static struct megasas_instance_template megasas_instance_template_xscale = {
219 .fire_cmd = megasas_fire_cmd_xscale,
220 .enable_intr = megasas_enable_intr_xscale,
221 .disable_intr = megasas_disable_intr_xscale,
222 .clear_intr = megasas_clear_intr_xscale,
223 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
227 * This is the end of set of functions & definitions specific
228 * to xscale (deviceid : 1064R, PERC5) controllers
232 * The following functions are defined for ppc (deviceid : 0x60)
237 * megasas_enable_intr_ppc - Enables interrupts
238 * @regs: MFI register set
241 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
243 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
245 writel(~0x80000004, &(regs)->outbound_intr_mask);
247 /* Dummy readl to force pci flush */
248 readl(®s->outbound_intr_mask);
252 * megasas_disable_intr_ppc - Disable interrupt
253 * @regs: MFI register set
256 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
258 u32 mask = 0xFFFFFFFF;
259 writel(mask, ®s->outbound_intr_mask);
260 /* Dummy readl to force pci flush */
261 readl(®s->outbound_intr_mask);
265 * megasas_read_fw_status_reg_ppc - returns the current FW status value
266 * @regs: MFI register set
269 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
271 return readl(&(regs)->outbound_scratch_pad);
275 * megasas_clear_interrupt_ppc - Check & clear interrupt
276 * @regs: MFI register set
279 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
283 * Check if it is our interrupt
285 status = readl(®s->outbound_intr_status);
287 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
292 * Clear the interrupt by writing back the same value
294 writel(status, ®s->outbound_doorbell_clear);
299 * megasas_fire_cmd_ppc - Sends command to the FW
300 * @frame_phys_addr : Physical address of cmd
301 * @frame_count : Number of frames for the command
302 * @regs : MFI register set
305 megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
307 writel((frame_phys_addr | (frame_count<<1))|1,
308 &(regs)->inbound_queue_port);
311 static struct megasas_instance_template megasas_instance_template_ppc = {
313 .fire_cmd = megasas_fire_cmd_ppc,
314 .enable_intr = megasas_enable_intr_ppc,
315 .disable_intr = megasas_disable_intr_ppc,
316 .clear_intr = megasas_clear_intr_ppc,
317 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
321 * This is the end of set of functions & definitions
322 * specific to ppc (deviceid : 0x60) controllers
326 * megasas_issue_polled - Issues a polling command
327 * @instance: Adapter soft state
328 * @cmd: Command packet to be issued
330 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
333 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
336 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
338 struct megasas_header *frame_hdr = &cmd->frame->hdr;
340 frame_hdr->cmd_status = 0xFF;
341 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
344 * Issue the frame using inbound queue port
346 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
349 * Wait for cmd_status to change
351 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
356 if (frame_hdr->cmd_status == 0xff)
363 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
364 * @instance: Adapter soft state
365 * @cmd: Command to be issued
367 * This function waits on an event for the command to be returned from ISR.
368 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
369 * Used to issue ioctl commands.
372 megasas_issue_blocked_cmd(struct megasas_instance *instance,
373 struct megasas_cmd *cmd)
375 cmd->cmd_status = ENODATA;
377 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
379 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
380 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
386 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
387 * @instance: Adapter soft state
388 * @cmd_to_abort: Previously issued cmd to be aborted
390 * MFI firmware can abort previously issued AEN comamnd (automatic event
391 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
392 * cmd and waits for return status.
393 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
396 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
397 struct megasas_cmd *cmd_to_abort)
399 struct megasas_cmd *cmd;
400 struct megasas_abort_frame *abort_fr;
402 cmd = megasas_get_cmd(instance);
407 abort_fr = &cmd->frame->abort;
410 * Prepare and issue the abort frame
412 abort_fr->cmd = MFI_CMD_ABORT;
413 abort_fr->cmd_status = 0xFF;
415 abort_fr->abort_context = cmd_to_abort->index;
416 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
417 abort_fr->abort_mfi_phys_addr_hi = 0;
420 cmd->cmd_status = 0xFF;
422 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
425 * Wait for this cmd to complete
427 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
428 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
430 megasas_return_cmd(instance, cmd);
435 * megasas_make_sgl32 - Prepares 32-bit SGL
436 * @instance: Adapter soft state
437 * @scp: SCSI command from the mid-layer
438 * @mfi_sgl: SGL to be filled in
440 * If successful, this function returns the number of SG elements. Otherwise,
444 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
445 union megasas_sgl *mfi_sgl)
449 struct scatterlist *os_sgl;
451 sge_count = scsi_dma_map(scp);
452 BUG_ON(sge_count < 0);
455 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
456 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
457 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
464 * megasas_make_sgl64 - Prepares 64-bit SGL
465 * @instance: Adapter soft state
466 * @scp: SCSI command from the mid-layer
467 * @mfi_sgl: SGL to be filled in
469 * If successful, this function returns the number of SG elements. Otherwise,
473 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
474 union megasas_sgl *mfi_sgl)
478 struct scatterlist *os_sgl;
480 sge_count = scsi_dma_map(scp);
481 BUG_ON(sge_count < 0);
484 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
485 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
486 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
493 * megasas_get_frame_count - Computes the number of frames
494 * @frame_type : type of frame- io or pthru frame
495 * @sge_count : number of sg elements
497 * Returns the number of frames required for numnber of sge's (sge_count)
500 static u32 megasas_get_frame_count(u8 sge_count, u8 frame_type)
507 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
508 sizeof(struct megasas_sge32);
511 * Main frame can contain 2 SGEs for 64-bit SGLs and
512 * 3 SGEs for 32-bit SGLs for ldio &
513 * 1 SGEs for 64-bit SGLs and
514 * 2 SGEs for 32-bit SGLs for pthru frame
516 if (unlikely(frame_type == PTHRU_FRAME)) {
518 num_cnt = sge_count - 1;
520 num_cnt = sge_count - 2;
523 num_cnt = sge_count - 2;
525 num_cnt = sge_count - 3;
529 sge_bytes = sge_sz * num_cnt;
531 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
532 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
543 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
544 * @instance: Adapter soft state
546 * @cmd: Command to be prepared in
548 * This function prepares CDB commands. These are typcially pass-through
549 * commands to the devices.
552 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
553 struct megasas_cmd *cmd)
558 struct megasas_pthru_frame *pthru;
560 is_logical = MEGASAS_IS_LOGICAL(scp);
561 device_id = MEGASAS_DEV_INDEX(instance, scp);
562 pthru = (struct megasas_pthru_frame *)cmd->frame;
564 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
565 flags = MFI_FRAME_DIR_WRITE;
566 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
567 flags = MFI_FRAME_DIR_READ;
568 else if (scp->sc_data_direction == PCI_DMA_NONE)
569 flags = MFI_FRAME_DIR_NONE;
572 * Prepare the DCDB frame
574 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
575 pthru->cmd_status = 0x0;
576 pthru->scsi_status = 0x0;
577 pthru->target_id = device_id;
578 pthru->lun = scp->device->lun;
579 pthru->cdb_len = scp->cmd_len;
581 pthru->flags = flags;
582 pthru->data_xfer_len = scsi_bufflen(scp);
584 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
590 pthru->flags |= MFI_FRAME_SGL64;
591 pthru->sge_count = megasas_make_sgl64(instance, scp,
594 pthru->sge_count = megasas_make_sgl32(instance, scp,
598 * Sense info specific
600 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
601 pthru->sense_buf_phys_addr_hi = 0;
602 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
605 * Compute the total number of frames this command consumes. FW uses
606 * this number to pull sufficient number of frames from host memory.
608 cmd->frame_count = megasas_get_frame_count(pthru->sge_count,
611 return cmd->frame_count;
615 * megasas_build_ldio - Prepares IOs to logical devices
616 * @instance: Adapter soft state
618 * @cmd: Command to to be prepared
620 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
623 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
624 struct megasas_cmd *cmd)
627 u8 sc = scp->cmnd[0];
629 struct megasas_io_frame *ldio;
631 device_id = MEGASAS_DEV_INDEX(instance, scp);
632 ldio = (struct megasas_io_frame *)cmd->frame;
634 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
635 flags = MFI_FRAME_DIR_WRITE;
636 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
637 flags = MFI_FRAME_DIR_READ;
640 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
642 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
643 ldio->cmd_status = 0x0;
644 ldio->scsi_status = 0x0;
645 ldio->target_id = device_id;
647 ldio->reserved_0 = 0;
650 ldio->start_lba_hi = 0;
651 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
654 * 6-byte READ(0x08) or WRITE(0x0A) cdb
656 if (scp->cmd_len == 6) {
657 ldio->lba_count = (u32) scp->cmnd[4];
658 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
659 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
661 ldio->start_lba_lo &= 0x1FFFFF;
665 * 10-byte READ(0x28) or WRITE(0x2A) cdb
667 else if (scp->cmd_len == 10) {
668 ldio->lba_count = (u32) scp->cmnd[8] |
669 ((u32) scp->cmnd[7] << 8);
670 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
671 ((u32) scp->cmnd[3] << 16) |
672 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
676 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
678 else if (scp->cmd_len == 12) {
679 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
680 ((u32) scp->cmnd[7] << 16) |
681 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
683 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
684 ((u32) scp->cmnd[3] << 16) |
685 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
689 * 16-byte READ(0x88) or WRITE(0x8A) cdb
691 else if (scp->cmd_len == 16) {
692 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
693 ((u32) scp->cmnd[11] << 16) |
694 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
696 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
697 ((u32) scp->cmnd[7] << 16) |
698 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
700 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
701 ((u32) scp->cmnd[3] << 16) |
702 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
710 ldio->flags |= MFI_FRAME_SGL64;
711 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
713 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
716 * Sense info specific
718 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
719 ldio->sense_buf_phys_addr_hi = 0;
720 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
723 * Compute the total number of frames this command consumes. FW uses
724 * this number to pull sufficient number of frames from host memory.
726 cmd->frame_count = megasas_get_frame_count(ldio->sge_count, IO_FRAME);
728 return cmd->frame_count;
732 * megasas_is_ldio - Checks if the cmd is for logical drive
733 * @scmd: SCSI command
735 * Called by megasas_queue_command to find out if the command to be queued
736 * is a logical drive command
738 static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
740 if (!MEGASAS_IS_LOGICAL(cmd))
742 switch (cmd->cmnd[0]) {
758 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
760 * @instance: Adapter soft state
763 megasas_dump_pending_frames(struct megasas_instance *instance)
765 struct megasas_cmd *cmd;
767 union megasas_sgl *mfi_sgl;
768 struct megasas_io_frame *ldio;
769 struct megasas_pthru_frame *pthru;
771 u32 max_cmd = instance->max_fw_cmds;
773 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
774 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
776 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
778 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
780 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
781 for (i = 0; i < max_cmd; i++) {
782 cmd = instance->cmd_list[i];
785 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
786 if (megasas_is_ldio(cmd->scmd)){
787 ldio = (struct megasas_io_frame *)cmd->frame;
788 mfi_sgl = &ldio->sgl;
789 sgcount = ldio->sge_count;
790 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);
793 pthru = (struct megasas_pthru_frame *) cmd->frame;
794 mfi_sgl = &pthru->sgl;
795 sgcount = pthru->sge_count;
796 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);
798 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
799 for (n = 0; n < sgcount; n++){
801 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) ;
803 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
806 printk(KERN_ERR "\n");
808 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
809 for (i = 0; i < max_cmd; i++) {
811 cmd = instance->cmd_list[i];
813 if(cmd->sync_cmd == 1){
814 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
817 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
821 * megasas_queue_command - Queue entry point
822 * @scmd: SCSI command to be queued
823 * @done: Callback entry point
826 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
829 struct megasas_cmd *cmd;
830 struct megasas_instance *instance;
832 instance = (struct megasas_instance *)
833 scmd->device->host->hostdata;
835 /* Don't process if we have already declared adapter dead */
836 if (instance->hw_crit_error)
837 return SCSI_MLQUEUE_HOST_BUSY;
839 scmd->scsi_done = done;
842 if (MEGASAS_IS_LOGICAL(scmd) &&
843 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
844 scmd->result = DID_BAD_TARGET << 16;
848 switch (scmd->cmnd[0]) {
849 case SYNCHRONIZE_CACHE:
851 * FW takes care of flush cache on its own
852 * No need to send it down
854 scmd->result = DID_OK << 16;
860 cmd = megasas_get_cmd(instance);
862 return SCSI_MLQUEUE_HOST_BUSY;
865 * Logical drive command
867 if (megasas_is_ldio(scmd))
868 frame_count = megasas_build_ldio(instance, scmd, cmd);
870 frame_count = megasas_build_dcdb(instance, scmd, cmd);
876 scmd->SCp.ptr = (char *)cmd;
879 * Issue the command to the FW
881 atomic_inc(&instance->fw_outstanding);
883 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
885 * Check if we have pend cmds to be completed
887 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
888 tasklet_schedule(&instance->isr_tasklet);
894 megasas_return_cmd(instance, cmd);
900 static int megasas_slave_configure(struct scsi_device *sdev)
903 * Don't export physical disk devices to the disk driver.
905 * FIXME: Currently we don't export them to the midlayer at all.
906 * That will be fixed once LSI engineers have audited the
907 * firmware for possible issues.
909 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
913 * The RAID firmware may require extended timeouts.
915 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
916 sdev->timeout = MEGASAS_DEFAULT_CMD_TIMEOUT * HZ;
921 * megasas_complete_cmd_dpc - Returns FW's controller structure
922 * @instance_addr: Address of adapter soft state
924 * Tasklet to complete cmds
926 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
931 struct megasas_cmd *cmd;
932 struct megasas_instance *instance =
933 (struct megasas_instance *)instance_addr;
936 /* If we have already declared adapter dead, donot complete cmds */
937 if (instance->hw_crit_error)
940 spin_lock_irqsave(&instance->completion_lock, flags);
942 producer = *instance->producer;
943 consumer = *instance->consumer;
945 while (consumer != producer) {
946 context = instance->reply_queue[consumer];
948 cmd = instance->cmd_list[context];
950 megasas_complete_cmd(instance, cmd, DID_OK);
953 if (consumer == (instance->max_fw_cmds + 1)) {
958 *instance->consumer = producer;
960 spin_unlock_irqrestore(&instance->completion_lock, flags);
963 * Check if we can restore can_queue
965 if (instance->flag & MEGASAS_FW_BUSY
966 && time_after(jiffies, instance->last_time + 5 * HZ)
967 && atomic_read(&instance->fw_outstanding) < 17) {
969 spin_lock_irqsave(instance->host->host_lock, flags);
970 instance->flag &= ~MEGASAS_FW_BUSY;
971 instance->host->can_queue =
972 instance->max_fw_cmds - MEGASAS_INT_CMDS;
974 spin_unlock_irqrestore(instance->host->host_lock, flags);
979 * megasas_wait_for_outstanding - Wait for all outstanding cmds
980 * @instance: Adapter soft state
982 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
983 * complete all its outstanding commands. Returns error if one or more IOs
984 * are pending after this time period. It also marks the controller dead.
986 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
989 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
991 for (i = 0; i < wait_time; i++) {
993 int outstanding = atomic_read(&instance->fw_outstanding);
998 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
999 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1000 "commands to complete\n",i,outstanding);
1002 * Call cmd completion routine. Cmd to be
1003 * be completed directly without depending on isr.
1005 megasas_complete_cmd_dpc((unsigned long)instance);
1011 if (atomic_read(&instance->fw_outstanding)) {
1013 * Send signal to FW to stop processing any pending cmds.
1014 * The controller will be taken offline by the OS now.
1016 writel(MFI_STOP_ADP,
1017 &instance->reg_set->inbound_doorbell);
1018 megasas_dump_pending_frames(instance);
1019 instance->hw_crit_error = 1;
1027 * megasas_generic_reset - Generic reset routine
1028 * @scmd: Mid-layer SCSI command
1030 * This routine implements a generic reset handler for device, bus and host
1031 * reset requests. Device, bus and host specific reset handlers can use this
1032 * function after they do their specific tasks.
1034 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1037 struct megasas_instance *instance;
1039 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1041 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1042 scmd->serial_number, scmd->cmnd[0], scmd->retries);
1044 if (instance->hw_crit_error) {
1045 printk(KERN_ERR "megasas: cannot recover from previous reset "
1050 ret_val = megasas_wait_for_outstanding(instance);
1051 if (ret_val == SUCCESS)
1052 printk(KERN_NOTICE "megasas: reset successful \n");
1054 printk(KERN_ERR "megasas: failed to do reset\n");
1060 * megasas_reset_timer - quiesce the adapter if required
1063 * Sets the FW busy flag and reduces the host->can_queue if the
1064 * cmd has not been completed within the timeout period.
1067 scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1069 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1070 struct megasas_instance *instance;
1071 unsigned long flags;
1073 if (time_after(jiffies, scmd->jiffies_at_alloc +
1074 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1075 return EH_NOT_HANDLED;
1078 instance = cmd->instance;
1079 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1080 /* FW is busy, throttle IO */
1081 spin_lock_irqsave(instance->host->host_lock, flags);
1083 instance->host->can_queue = 16;
1084 instance->last_time = jiffies;
1085 instance->flag |= MEGASAS_FW_BUSY;
1087 spin_unlock_irqrestore(instance->host->host_lock, flags);
1089 return EH_RESET_TIMER;
1093 * megasas_reset_device - Device reset handler entry point
1095 static int megasas_reset_device(struct scsi_cmnd *scmd)
1100 * First wait for all commands to complete
1102 ret = megasas_generic_reset(scmd);
1108 * megasas_reset_bus_host - Bus & host reset handler entry point
1110 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1115 * First wait for all commands to complete
1117 ret = megasas_generic_reset(scmd);
1123 * megasas_bios_param - Returns disk geometry for a disk
1124 * @sdev: device handle
1125 * @bdev: block device
1126 * @capacity: drive capacity
1127 * @geom: geometry parameters
1130 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1131 sector_t capacity, int geom[])
1137 /* Default heads (64) & sectors (32) */
1141 tmp = heads * sectors;
1142 cylinders = capacity;
1144 sector_div(cylinders, tmp);
1147 * Handle extended translation size for logical drives > 1Gb
1150 if (capacity >= 0x200000) {
1153 tmp = heads*sectors;
1154 cylinders = capacity;
1155 sector_div(cylinders, tmp);
1160 geom[2] = cylinders;
1166 * megasas_service_aen - Processes an event notification
1167 * @instance: Adapter soft state
1168 * @cmd: AEN command completed by the ISR
1170 * For AEN, driver sends a command down to FW that is held by the FW till an
1171 * event occurs. When an event of interest occurs, FW completes the command
1172 * that it was previously holding.
1174 * This routines sends SIGIO signal to processes that have registered with the
1178 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1181 * Don't signal app if it is just an aborted previously registered aen
1183 if (!cmd->abort_aen)
1184 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1188 instance->aen_cmd = NULL;
1189 megasas_return_cmd(instance, cmd);
1193 * Scsi host template for megaraid_sas driver
1195 static struct scsi_host_template megasas_template = {
1197 .module = THIS_MODULE,
1198 .name = "LSI SAS based MegaRAID driver",
1199 .proc_name = "megaraid_sas",
1200 .slave_configure = megasas_slave_configure,
1201 .queuecommand = megasas_queue_command,
1202 .eh_device_reset_handler = megasas_reset_device,
1203 .eh_bus_reset_handler = megasas_reset_bus_host,
1204 .eh_host_reset_handler = megasas_reset_bus_host,
1205 .eh_timed_out = megasas_reset_timer,
1206 .bios_param = megasas_bios_param,
1207 .use_clustering = ENABLE_CLUSTERING,
1211 * megasas_complete_int_cmd - Completes an internal command
1212 * @instance: Adapter soft state
1213 * @cmd: Command to be completed
1215 * The megasas_issue_blocked_cmd() function waits for a command to complete
1216 * after it issues a command. This function wakes up that waiting routine by
1217 * calling wake_up() on the wait queue.
1220 megasas_complete_int_cmd(struct megasas_instance *instance,
1221 struct megasas_cmd *cmd)
1223 cmd->cmd_status = cmd->frame->io.cmd_status;
1225 if (cmd->cmd_status == ENODATA) {
1226 cmd->cmd_status = 0;
1228 wake_up(&instance->int_cmd_wait_q);
1232 * megasas_complete_abort - Completes aborting a command
1233 * @instance: Adapter soft state
1234 * @cmd: Cmd that was issued to abort another cmd
1236 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1237 * after it issues an abort on a previously issued command. This function
1238 * wakes up all functions waiting on the same wait queue.
1241 megasas_complete_abort(struct megasas_instance *instance,
1242 struct megasas_cmd *cmd)
1244 if (cmd->sync_cmd) {
1246 cmd->cmd_status = 0;
1247 wake_up(&instance->abort_cmd_wait_q);
1254 * megasas_complete_cmd - Completes a command
1255 * @instance: Adapter soft state
1256 * @cmd: Command to be completed
1257 * @alt_status: If non-zero, use this value as status to
1258 * SCSI mid-layer instead of the value returned
1259 * by the FW. This should be used if caller wants
1260 * an alternate status (as in the case of aborted
1264 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1268 struct megasas_header *hdr = &cmd->frame->hdr;
1271 cmd->scmd->SCp.ptr = NULL;
1275 case MFI_CMD_PD_SCSI_IO:
1276 case MFI_CMD_LD_SCSI_IO:
1279 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1280 * issued either through an IO path or an IOCTL path. If it
1281 * was via IOCTL, we will send it to internal completion.
1283 if (cmd->sync_cmd) {
1285 megasas_complete_int_cmd(instance, cmd);
1289 case MFI_CMD_LD_READ:
1290 case MFI_CMD_LD_WRITE:
1293 cmd->scmd->result = alt_status << 16;
1299 atomic_dec(&instance->fw_outstanding);
1301 scsi_dma_unmap(cmd->scmd);
1302 cmd->scmd->scsi_done(cmd->scmd);
1303 megasas_return_cmd(instance, cmd);
1308 switch (hdr->cmd_status) {
1311 cmd->scmd->result = DID_OK << 16;
1314 case MFI_STAT_SCSI_IO_FAILED:
1315 case MFI_STAT_LD_INIT_IN_PROGRESS:
1317 (DID_ERROR << 16) | hdr->scsi_status;
1320 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1322 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1324 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1325 memset(cmd->scmd->sense_buffer, 0,
1326 SCSI_SENSE_BUFFERSIZE);
1327 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1330 cmd->scmd->result |= DRIVER_SENSE << 24;
1335 case MFI_STAT_LD_OFFLINE:
1336 case MFI_STAT_DEVICE_NOT_FOUND:
1337 cmd->scmd->result = DID_BAD_TARGET << 16;
1341 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1343 cmd->scmd->result = DID_ERROR << 16;
1347 atomic_dec(&instance->fw_outstanding);
1349 scsi_dma_unmap(cmd->scmd);
1350 cmd->scmd->scsi_done(cmd->scmd);
1351 megasas_return_cmd(instance, cmd);
1360 * See if got an event notification
1362 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1363 megasas_service_aen(instance, cmd);
1365 megasas_complete_int_cmd(instance, cmd);
1371 * Cmd issued to abort another cmd returned
1373 megasas_complete_abort(instance, cmd);
1377 printk("megasas: Unknown command completed! [0x%X]\n",
1384 * megasas_deplete_reply_queue - Processes all completed commands
1385 * @instance: Adapter soft state
1386 * @alt_status: Alternate status to be returned to
1387 * SCSI mid-layer instead of the status
1388 * returned by the FW
1391 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1394 * Check if it is our interrupt
1395 * Clear the interrupt
1397 if(instance->instancet->clear_intr(instance->reg_set))
1400 if (instance->hw_crit_error)
1403 * Schedule the tasklet for cmd completion
1405 tasklet_schedule(&instance->isr_tasklet);
1411 * megasas_isr - isr entry point
1413 static irqreturn_t megasas_isr(int irq, void *devp)
1415 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1420 * megasas_transition_to_ready - Move the FW to READY state
1421 * @instance: Adapter soft state
1423 * During the initialization, FW passes can potentially be in any one of
1424 * several possible states. If the FW in operational, waiting-for-handshake
1425 * states, driver must take steps to bring it to ready state. Otherwise, it
1426 * has to wait for the ready state.
1429 megasas_transition_to_ready(struct megasas_instance* instance)
1436 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
1438 if (fw_state != MFI_STATE_READY)
1439 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1442 while (fw_state != MFI_STATE_READY) {
1446 case MFI_STATE_FAULT:
1448 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1451 case MFI_STATE_WAIT_HANDSHAKE:
1453 * Set the CLR bit in inbound doorbell
1455 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1456 &instance->reg_set->inbound_doorbell);
1459 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1462 case MFI_STATE_BOOT_MESSAGE_PENDING:
1463 writel(MFI_INIT_HOTPLUG,
1464 &instance->reg_set->inbound_doorbell);
1467 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1470 case MFI_STATE_OPERATIONAL:
1472 * Bring it to READY state; assuming max wait 10 secs
1474 instance->instancet->disable_intr(instance->reg_set);
1475 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
1478 cur_state = MFI_STATE_OPERATIONAL;
1481 case MFI_STATE_UNDEFINED:
1483 * This state should not last for more than 2 seconds
1486 cur_state = MFI_STATE_UNDEFINED;
1489 case MFI_STATE_BB_INIT:
1491 cur_state = MFI_STATE_BB_INIT;
1494 case MFI_STATE_FW_INIT:
1496 cur_state = MFI_STATE_FW_INIT;
1499 case MFI_STATE_FW_INIT_2:
1501 cur_state = MFI_STATE_FW_INIT_2;
1504 case MFI_STATE_DEVICE_SCAN:
1506 cur_state = MFI_STATE_DEVICE_SCAN;
1509 case MFI_STATE_FLUSH_CACHE:
1511 cur_state = MFI_STATE_FLUSH_CACHE;
1515 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1521 * The cur_state should not last for more than max_wait secs
1523 for (i = 0; i < (max_wait * 1000); i++) {
1524 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1527 if (fw_state == cur_state) {
1534 * Return error if fw_state hasn't changed after max_wait
1536 if (fw_state == cur_state) {
1537 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1538 "in %d secs\n", fw_state, max_wait);
1542 printk(KERN_INFO "megasas: FW now in Ready state\n");
1548 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1549 * @instance: Adapter soft state
1551 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1554 u32 max_cmd = instance->max_fw_cmds;
1555 struct megasas_cmd *cmd;
1557 if (!instance->frame_dma_pool)
1561 * Return all frames to pool
1563 for (i = 0; i < max_cmd; i++) {
1565 cmd = instance->cmd_list[i];
1568 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1569 cmd->frame_phys_addr);
1572 pci_pool_free(instance->sense_dma_pool, cmd->sense,
1573 cmd->sense_phys_addr);
1577 * Now destroy the pool itself
1579 pci_pool_destroy(instance->frame_dma_pool);
1580 pci_pool_destroy(instance->sense_dma_pool);
1582 instance->frame_dma_pool = NULL;
1583 instance->sense_dma_pool = NULL;
1587 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1588 * @instance: Adapter soft state
1590 * Each command packet has an embedded DMA memory buffer that is used for
1591 * filling MFI frame and the SG list that immediately follows the frame. This
1592 * function creates those DMA memory buffers for each command packet by using
1593 * PCI pool facility.
1595 static int megasas_create_frame_pool(struct megasas_instance *instance)
1603 struct megasas_cmd *cmd;
1605 max_cmd = instance->max_fw_cmds;
1608 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1609 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1611 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1612 sizeof(struct megasas_sge32);
1615 * Calculated the number of 64byte frames required for SGL
1617 sgl_sz = sge_sz * instance->max_num_sge;
1618 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1621 * We need one extra frame for the MFI command
1625 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1627 * Use DMA pool facility provided by PCI layer
1629 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1630 instance->pdev, total_sz, 64,
1633 if (!instance->frame_dma_pool) {
1634 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1638 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1639 instance->pdev, 128, 4, 0);
1641 if (!instance->sense_dma_pool) {
1642 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1644 pci_pool_destroy(instance->frame_dma_pool);
1645 instance->frame_dma_pool = NULL;
1651 * Allocate and attach a frame to each of the commands in cmd_list.
1652 * By making cmd->index as the context instead of the &cmd, we can
1653 * always use 32bit context regardless of the architecture
1655 for (i = 0; i < max_cmd; i++) {
1657 cmd = instance->cmd_list[i];
1659 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1660 GFP_KERNEL, &cmd->frame_phys_addr);
1662 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1663 GFP_KERNEL, &cmd->sense_phys_addr);
1666 * megasas_teardown_frame_pool() takes care of freeing
1667 * whatever has been allocated
1669 if (!cmd->frame || !cmd->sense) {
1670 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1671 megasas_teardown_frame_pool(instance);
1675 cmd->frame->io.context = cmd->index;
1682 * megasas_free_cmds - Free all the cmds in the free cmd pool
1683 * @instance: Adapter soft state
1685 static void megasas_free_cmds(struct megasas_instance *instance)
1688 /* First free the MFI frame pool */
1689 megasas_teardown_frame_pool(instance);
1691 /* Free all the commands in the cmd_list */
1692 for (i = 0; i < instance->max_fw_cmds; i++)
1693 kfree(instance->cmd_list[i]);
1695 /* Free the cmd_list buffer itself */
1696 kfree(instance->cmd_list);
1697 instance->cmd_list = NULL;
1699 INIT_LIST_HEAD(&instance->cmd_pool);
1703 * megasas_alloc_cmds - Allocates the command packets
1704 * @instance: Adapter soft state
1706 * Each command that is issued to the FW, whether IO commands from the OS or
1707 * internal commands like IOCTLs, are wrapped in local data structure called
1708 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1711 * Each frame has a 32-bit field called context (tag). This context is used
1712 * to get back the megasas_cmd from the frame when a frame gets completed in
1713 * the ISR. Typically the address of the megasas_cmd itself would be used as
1714 * the context. But we wanted to keep the differences between 32 and 64 bit
1715 * systems to the mininum. We always use 32 bit integers for the context. In
1716 * this driver, the 32 bit values are the indices into an array cmd_list.
1717 * This array is used only to look up the megasas_cmd given the context. The
1718 * free commands themselves are maintained in a linked list called cmd_pool.
1720 static int megasas_alloc_cmds(struct megasas_instance *instance)
1725 struct megasas_cmd *cmd;
1727 max_cmd = instance->max_fw_cmds;
1730 * instance->cmd_list is an array of struct megasas_cmd pointers.
1731 * Allocate the dynamic array first and then allocate individual
1734 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
1736 if (!instance->cmd_list) {
1737 printk(KERN_DEBUG "megasas: out of memory\n");
1742 for (i = 0; i < max_cmd; i++) {
1743 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1746 if (!instance->cmd_list[i]) {
1748 for (j = 0; j < i; j++)
1749 kfree(instance->cmd_list[j]);
1751 kfree(instance->cmd_list);
1752 instance->cmd_list = NULL;
1759 * Add all the commands to command pool (instance->cmd_pool)
1761 for (i = 0; i < max_cmd; i++) {
1762 cmd = instance->cmd_list[i];
1763 memset(cmd, 0, sizeof(struct megasas_cmd));
1765 cmd->instance = instance;
1767 list_add_tail(&cmd->list, &instance->cmd_pool);
1771 * Create a frame pool and assign one frame to each cmd
1773 if (megasas_create_frame_pool(instance)) {
1774 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1775 megasas_free_cmds(instance);
1782 * megasas_get_controller_info - Returns FW's controller structure
1783 * @instance: Adapter soft state
1784 * @ctrl_info: Controller information structure
1786 * Issues an internal command (DCMD) to get the FW's controller structure.
1787 * This information is mainly used to find out the maximum IO transfer per
1788 * command supported by the FW.
1791 megasas_get_ctrl_info(struct megasas_instance *instance,
1792 struct megasas_ctrl_info *ctrl_info)
1795 struct megasas_cmd *cmd;
1796 struct megasas_dcmd_frame *dcmd;
1797 struct megasas_ctrl_info *ci;
1798 dma_addr_t ci_h = 0;
1800 cmd = megasas_get_cmd(instance);
1803 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1807 dcmd = &cmd->frame->dcmd;
1809 ci = pci_alloc_consistent(instance->pdev,
1810 sizeof(struct megasas_ctrl_info), &ci_h);
1813 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1814 megasas_return_cmd(instance, cmd);
1818 memset(ci, 0, sizeof(*ci));
1819 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1821 dcmd->cmd = MFI_CMD_DCMD;
1822 dcmd->cmd_status = 0xFF;
1823 dcmd->sge_count = 1;
1824 dcmd->flags = MFI_FRAME_DIR_READ;
1826 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1827 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1828 dcmd->sgl.sge32[0].phys_addr = ci_h;
1829 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1831 if (!megasas_issue_polled(instance, cmd)) {
1833 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1838 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1841 megasas_return_cmd(instance, cmd);
1846 * megasas_issue_init_mfi - Initializes the FW
1847 * @instance: Adapter soft state
1849 * Issues the INIT MFI cmd
1852 megasas_issue_init_mfi(struct megasas_instance *instance)
1856 struct megasas_cmd *cmd;
1858 struct megasas_init_frame *init_frame;
1859 struct megasas_init_queue_info *initq_info;
1860 dma_addr_t init_frame_h;
1861 dma_addr_t initq_info_h;
1864 * Prepare a init frame. Note the init frame points to queue info
1865 * structure. Each frame has SGL allocated after first 64 bytes. For
1866 * this frame - since we don't need any SGL - we use SGL's space as
1867 * queue info structure
1869 * We will not get a NULL command below. We just created the pool.
1871 cmd = megasas_get_cmd(instance);
1873 init_frame = (struct megasas_init_frame *)cmd->frame;
1874 initq_info = (struct megasas_init_queue_info *)
1875 ((unsigned long)init_frame + 64);
1877 init_frame_h = cmd->frame_phys_addr;
1878 initq_info_h = init_frame_h + 64;
1880 context = init_frame->context;
1881 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1882 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1883 init_frame->context = context;
1885 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1886 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1888 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1889 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1891 init_frame->cmd = MFI_CMD_INIT;
1892 init_frame->cmd_status = 0xFF;
1893 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1895 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1898 * disable the intr before firing the init frame to FW
1900 instance->instancet->disable_intr(instance->reg_set);
1903 * Issue the init frame in polled mode
1906 if (megasas_issue_polled(instance, cmd)) {
1907 printk(KERN_ERR "megasas: Failed to init firmware\n");
1908 megasas_return_cmd(instance, cmd);
1912 megasas_return_cmd(instance, cmd);
1921 * megasas_start_timer - Initializes a timer object
1922 * @instance: Adapter soft state
1923 * @timer: timer object to be initialized
1924 * @fn: timer function
1925 * @interval: time interval between timer function call
1928 megasas_start_timer(struct megasas_instance *instance,
1929 struct timer_list *timer,
1930 void *fn, unsigned long interval)
1933 timer->expires = jiffies + interval;
1934 timer->data = (unsigned long)instance;
1935 timer->function = fn;
1940 * megasas_io_completion_timer - Timer fn
1941 * @instance_addr: Address of adapter soft state
1943 * Schedules tasklet for cmd completion
1944 * if poll_mode_io is set
1947 megasas_io_completion_timer(unsigned long instance_addr)
1949 struct megasas_instance *instance =
1950 (struct megasas_instance *)instance_addr;
1952 if (atomic_read(&instance->fw_outstanding))
1953 tasklet_schedule(&instance->isr_tasklet);
1957 mod_timer(&instance->io_completion_timer,
1958 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
1962 * megasas_init_mfi - Initializes the FW
1963 * @instance: Adapter soft state
1965 * This is the main function for initializing MFI firmware.
1967 static int megasas_init_mfi(struct megasas_instance *instance)
1974 struct megasas_register_set __iomem *reg_set;
1975 struct megasas_ctrl_info *ctrl_info;
1977 * Map the message registers
1979 instance->base_addr = pci_resource_start(instance->pdev, 0);
1981 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
1982 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1986 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1988 if (!instance->reg_set) {
1989 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1993 reg_set = instance->reg_set;
1995 switch(instance->pdev->device)
1997 case PCI_DEVICE_ID_LSI_SAS1078R:
1998 case PCI_DEVICE_ID_LSI_SAS1078DE:
1999 instance->instancet = &megasas_instance_template_ppc;
2001 case PCI_DEVICE_ID_LSI_SAS1064R:
2002 case PCI_DEVICE_ID_DELL_PERC5:
2004 instance->instancet = &megasas_instance_template_xscale;
2009 * We expect the FW state to be READY
2011 if (megasas_transition_to_ready(instance))
2012 goto fail_ready_state;
2015 * Get various operational parameters from status register
2017 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
2019 * Reduce the max supported cmds by 1. This is to ensure that the
2020 * reply_q_sz (1 more than the max cmd that driver may send)
2021 * does not exceed max cmds that the FW can support
2023 instance->max_fw_cmds = instance->max_fw_cmds-1;
2024 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2027 * Create a pool of commands
2029 if (megasas_alloc_cmds(instance))
2030 goto fail_alloc_cmds;
2033 * Allocate memory for reply queue. Length of reply queue should
2034 * be _one_ more than the maximum commands handled by the firmware.
2036 * Note: When FW completes commands, it places corresponding contex
2037 * values in this circular reply queue. This circular queue is a fairly
2038 * typical producer-consumer queue. FW is the producer (of completed
2039 * commands) and the driver is the consumer.
2041 context_sz = sizeof(u32);
2042 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2044 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2046 &instance->reply_queue_h);
2048 if (!instance->reply_queue) {
2049 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2050 goto fail_reply_queue;
2053 if (megasas_issue_init_mfi(instance))
2056 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2059 * Compute the max allowed sectors per IO: The controller info has two
2060 * limits on max sectors. Driver should use the minimum of these two.
2062 * 1 << stripe_sz_ops.min = max sectors per strip
2064 * Note that older firmwares ( < FW ver 30) didn't report information
2065 * to calculate max_sectors_1. So the number ended up as zero always.
2068 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2070 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2071 ctrl_info->max_strips_per_io;
2072 max_sectors_2 = ctrl_info->max_request_size;
2074 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2077 instance->max_sectors_per_req = instance->max_num_sge *
2079 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2080 instance->max_sectors_per_req = tmp_sectors;
2085 * Setup tasklet for cmd completion
2088 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2089 (unsigned long)instance);
2091 /* Initialize the cmd completion timer */
2093 megasas_start_timer(instance, &instance->io_completion_timer,
2094 megasas_io_completion_timer,
2095 MEGASAS_COMPLETION_TIMER_INTERVAL);
2100 pci_free_consistent(instance->pdev, reply_q_sz,
2101 instance->reply_queue, instance->reply_queue_h);
2103 megasas_free_cmds(instance);
2107 iounmap(instance->reg_set);
2110 pci_release_regions(instance->pdev);
2116 * megasas_release_mfi - Reverses the FW initialization
2117 * @intance: Adapter soft state
2119 static void megasas_release_mfi(struct megasas_instance *instance)
2121 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2123 pci_free_consistent(instance->pdev, reply_q_sz,
2124 instance->reply_queue, instance->reply_queue_h);
2126 megasas_free_cmds(instance);
2128 iounmap(instance->reg_set);
2130 pci_release_regions(instance->pdev);
2134 * megasas_get_seq_num - Gets latest event sequence numbers
2135 * @instance: Adapter soft state
2136 * @eli: FW event log sequence numbers information
2138 * FW maintains a log of all events in a non-volatile area. Upper layers would
2139 * usually find out the latest sequence number of the events, the seq number at
2140 * the boot etc. They would "read" all the events below the latest seq number
2141 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2142 * number), they would subsribe to AEN (asynchronous event notification) and
2143 * wait for the events to happen.
2146 megasas_get_seq_num(struct megasas_instance *instance,
2147 struct megasas_evt_log_info *eli)
2149 struct megasas_cmd *cmd;
2150 struct megasas_dcmd_frame *dcmd;
2151 struct megasas_evt_log_info *el_info;
2152 dma_addr_t el_info_h = 0;
2154 cmd = megasas_get_cmd(instance);
2160 dcmd = &cmd->frame->dcmd;
2161 el_info = pci_alloc_consistent(instance->pdev,
2162 sizeof(struct megasas_evt_log_info),
2166 megasas_return_cmd(instance, cmd);
2170 memset(el_info, 0, sizeof(*el_info));
2171 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2173 dcmd->cmd = MFI_CMD_DCMD;
2174 dcmd->cmd_status = 0x0;
2175 dcmd->sge_count = 1;
2176 dcmd->flags = MFI_FRAME_DIR_READ;
2178 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2179 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2180 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2181 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2183 megasas_issue_blocked_cmd(instance, cmd);
2186 * Copy the data back into callers buffer
2188 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2190 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2191 el_info, el_info_h);
2193 megasas_return_cmd(instance, cmd);
2199 * megasas_register_aen - Registers for asynchronous event notification
2200 * @instance: Adapter soft state
2201 * @seq_num: The starting sequence number
2202 * @class_locale: Class of the event
2204 * This function subscribes for AEN for events beyond the @seq_num. It requests
2205 * to be notified if and only if the event is of type @class_locale
2208 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2209 u32 class_locale_word)
2212 struct megasas_cmd *cmd;
2213 struct megasas_dcmd_frame *dcmd;
2214 union megasas_evt_class_locale curr_aen;
2215 union megasas_evt_class_locale prev_aen;
2218 * If there an AEN pending already (aen_cmd), check if the
2219 * class_locale of that pending AEN is inclusive of the new
2220 * AEN request we currently have. If it is, then we don't have
2221 * to do anything. In other words, whichever events the current
2222 * AEN request is subscribing to, have already been subscribed
2225 * If the old_cmd is _not_ inclusive, then we have to abort
2226 * that command, form a class_locale that is superset of both
2227 * old and current and re-issue to the FW
2230 curr_aen.word = class_locale_word;
2232 if (instance->aen_cmd) {
2234 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2237 * A class whose enum value is smaller is inclusive of all
2238 * higher values. If a PROGRESS (= -1) was previously
2239 * registered, then a new registration requests for higher
2240 * classes need not be sent to FW. They are automatically
2243 * Locale numbers don't have such hierarchy. They are bitmap
2246 if ((prev_aen.members.class <= curr_aen.members.class) &&
2247 !((prev_aen.members.locale & curr_aen.members.locale) ^
2248 curr_aen.members.locale)) {
2250 * Previously issued event registration includes
2251 * current request. Nothing to do.
2255 curr_aen.members.locale |= prev_aen.members.locale;
2257 if (prev_aen.members.class < curr_aen.members.class)
2258 curr_aen.members.class = prev_aen.members.class;
2260 instance->aen_cmd->abort_aen = 1;
2261 ret_val = megasas_issue_blocked_abort_cmd(instance,
2266 printk(KERN_DEBUG "megasas: Failed to abort "
2267 "previous AEN command\n");
2273 cmd = megasas_get_cmd(instance);
2278 dcmd = &cmd->frame->dcmd;
2280 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2283 * Prepare DCMD for aen registration
2285 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2287 dcmd->cmd = MFI_CMD_DCMD;
2288 dcmd->cmd_status = 0x0;
2289 dcmd->sge_count = 1;
2290 dcmd->flags = MFI_FRAME_DIR_READ;
2292 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2293 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2294 dcmd->mbox.w[0] = seq_num;
2295 dcmd->mbox.w[1] = curr_aen.word;
2296 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2297 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2300 * Store reference to the cmd used to register for AEN. When an
2301 * application wants us to register for AEN, we have to abort this
2302 * cmd and re-register with a new EVENT LOCALE supplied by that app
2304 instance->aen_cmd = cmd;
2307 * Issue the aen registration frame
2309 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
2315 * megasas_start_aen - Subscribes to AEN during driver load time
2316 * @instance: Adapter soft state
2318 static int megasas_start_aen(struct megasas_instance *instance)
2320 struct megasas_evt_log_info eli;
2321 union megasas_evt_class_locale class_locale;
2324 * Get the latest sequence number from FW
2326 memset(&eli, 0, sizeof(eli));
2328 if (megasas_get_seq_num(instance, &eli))
2332 * Register AEN with FW for latest sequence number plus 1
2334 class_locale.members.reserved = 0;
2335 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2336 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2338 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2343 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2344 * @instance: Adapter soft state
2346 static int megasas_io_attach(struct megasas_instance *instance)
2348 struct Scsi_Host *host = instance->host;
2351 * Export parameters required by SCSI mid-layer
2353 host->irq = instance->pdev->irq;
2354 host->unique_id = instance->unique_id;
2355 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2356 host->this_id = instance->init_id;
2357 host->sg_tablesize = instance->max_num_sge;
2358 host->max_sectors = instance->max_sectors_per_req;
2359 host->cmd_per_lun = 128;
2360 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2361 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2362 host->max_lun = MEGASAS_MAX_LUN;
2363 host->max_cmd_len = 16;
2366 * Notify the mid-layer about the new controller
2368 if (scsi_add_host(host, &instance->pdev->dev)) {
2369 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2374 * Trigger SCSI to scan our drives
2376 scsi_scan_host(host);
2381 megasas_set_dma_mask(struct pci_dev *pdev)
2384 * All our contollers are capable of performing 64-bit DMA
2387 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2389 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2390 goto fail_set_dma_mask;
2393 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2394 goto fail_set_dma_mask;
2403 * megasas_probe_one - PCI hotplug entry point
2404 * @pdev: PCI device structure
2405 * @id: PCI ids of supported hotplugged adapter
2407 static int __devinit
2408 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2411 struct Scsi_Host *host;
2412 struct megasas_instance *instance;
2415 * Announce PCI information
2417 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2418 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2419 pdev->subsystem_device);
2421 printk("bus %d:slot %d:func %d\n",
2422 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2425 * PCI prepping: enable device set bus mastering and dma mask
2427 rval = pci_enable_device(pdev);
2433 pci_set_master(pdev);
2435 if (megasas_set_dma_mask(pdev))
2436 goto fail_set_dma_mask;
2438 host = scsi_host_alloc(&megasas_template,
2439 sizeof(struct megasas_instance));
2442 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2443 goto fail_alloc_instance;
2446 instance = (struct megasas_instance *)host->hostdata;
2447 memset(instance, 0, sizeof(*instance));
2449 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2450 &instance->producer_h);
2451 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2452 &instance->consumer_h);
2454 if (!instance->producer || !instance->consumer) {
2455 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2456 "producer, consumer\n");
2457 goto fail_alloc_dma_buf;
2460 *instance->producer = 0;
2461 *instance->consumer = 0;
2463 instance->evt_detail = pci_alloc_consistent(pdev,
2465 megasas_evt_detail),
2466 &instance->evt_detail_h);
2468 if (!instance->evt_detail) {
2469 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2470 "event detail structure\n");
2471 goto fail_alloc_dma_buf;
2475 * Initialize locks and queues
2477 INIT_LIST_HEAD(&instance->cmd_pool);
2479 atomic_set(&instance->fw_outstanding,0);
2481 init_waitqueue_head(&instance->int_cmd_wait_q);
2482 init_waitqueue_head(&instance->abort_cmd_wait_q);
2484 spin_lock_init(&instance->cmd_pool_lock);
2485 spin_lock_init(&instance->completion_lock);
2487 mutex_init(&instance->aen_mutex);
2488 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2491 * Initialize PCI related and misc parameters
2493 instance->pdev = pdev;
2494 instance->host = host;
2495 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2496 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2498 megasas_dbg_lvl = 0;
2500 instance->last_time = 0;
2503 * Initialize MFI Firmware
2505 if (megasas_init_mfi(instance))
2511 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
2512 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2516 instance->instancet->enable_intr(instance->reg_set);
2519 * Store instance in PCI softstate
2521 pci_set_drvdata(pdev, instance);
2524 * Add this controller to megasas_mgmt_info structure so that it
2525 * can be exported to management applications
2527 megasas_mgmt_info.count++;
2528 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2529 megasas_mgmt_info.max_index++;
2532 * Initiate AEN (Asynchronous Event Notification)
2534 if (megasas_start_aen(instance)) {
2535 printk(KERN_DEBUG "megasas: start aen failed\n");
2536 goto fail_start_aen;
2540 * Register with SCSI mid-layer
2542 if (megasas_io_attach(instance))
2543 goto fail_io_attach;
2549 megasas_mgmt_info.count--;
2550 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2551 megasas_mgmt_info.max_index--;
2553 pci_set_drvdata(pdev, NULL);
2554 instance->instancet->disable_intr(instance->reg_set);
2555 free_irq(instance->pdev->irq, instance);
2557 megasas_release_mfi(instance);
2562 if (instance->evt_detail)
2563 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2564 instance->evt_detail,
2565 instance->evt_detail_h);
2567 if (instance->producer)
2568 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2569 instance->producer_h);
2570 if (instance->consumer)
2571 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2572 instance->consumer_h);
2573 scsi_host_put(host);
2575 fail_alloc_instance:
2577 pci_disable_device(pdev);
2583 * megasas_flush_cache - Requests FW to flush all its caches
2584 * @instance: Adapter soft state
2586 static void megasas_flush_cache(struct megasas_instance *instance)
2588 struct megasas_cmd *cmd;
2589 struct megasas_dcmd_frame *dcmd;
2591 cmd = megasas_get_cmd(instance);
2596 dcmd = &cmd->frame->dcmd;
2598 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2600 dcmd->cmd = MFI_CMD_DCMD;
2601 dcmd->cmd_status = 0x0;
2602 dcmd->sge_count = 0;
2603 dcmd->flags = MFI_FRAME_DIR_NONE;
2605 dcmd->data_xfer_len = 0;
2606 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2607 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2609 megasas_issue_blocked_cmd(instance, cmd);
2611 megasas_return_cmd(instance, cmd);
2617 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2618 * @instance: Adapter soft state
2619 * @opcode: Shutdown/Hibernate
2621 static void megasas_shutdown_controller(struct megasas_instance *instance,
2624 struct megasas_cmd *cmd;
2625 struct megasas_dcmd_frame *dcmd;
2627 cmd = megasas_get_cmd(instance);
2632 if (instance->aen_cmd)
2633 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2635 dcmd = &cmd->frame->dcmd;
2637 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2639 dcmd->cmd = MFI_CMD_DCMD;
2640 dcmd->cmd_status = 0x0;
2641 dcmd->sge_count = 0;
2642 dcmd->flags = MFI_FRAME_DIR_NONE;
2644 dcmd->data_xfer_len = 0;
2645 dcmd->opcode = opcode;
2647 megasas_issue_blocked_cmd(instance, cmd);
2649 megasas_return_cmd(instance, cmd);
2656 * megasas_suspend - driver suspend entry point
2657 * @pdev: PCI device structure
2658 * @state: PCI power state to suspend routine
2661 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
2663 struct Scsi_Host *host;
2664 struct megasas_instance *instance;
2666 instance = pci_get_drvdata(pdev);
2667 host = instance->host;
2670 del_timer_sync(&instance->io_completion_timer);
2672 megasas_flush_cache(instance);
2673 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
2674 tasklet_kill(&instance->isr_tasklet);
2676 pci_set_drvdata(instance->pdev, instance);
2677 instance->instancet->disable_intr(instance->reg_set);
2678 free_irq(instance->pdev->irq, instance);
2680 pci_save_state(pdev);
2681 pci_disable_device(pdev);
2683 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2689 * megasas_resume- driver resume entry point
2690 * @pdev: PCI device structure
2693 megasas_resume(struct pci_dev *pdev)
2696 struct Scsi_Host *host;
2697 struct megasas_instance *instance;
2699 instance = pci_get_drvdata(pdev);
2700 host = instance->host;
2701 pci_set_power_state(pdev, PCI_D0);
2702 pci_enable_wake(pdev, PCI_D0, 0);
2703 pci_restore_state(pdev);
2706 * PCI prepping: enable device set bus mastering and dma mask
2708 rval = pci_enable_device(pdev);
2711 printk(KERN_ERR "megasas: Enable device failed\n");
2715 pci_set_master(pdev);
2717 if (megasas_set_dma_mask(pdev))
2718 goto fail_set_dma_mask;
2721 * Initialize MFI Firmware
2724 *instance->producer = 0;
2725 *instance->consumer = 0;
2727 atomic_set(&instance->fw_outstanding, 0);
2730 * We expect the FW state to be READY
2732 if (megasas_transition_to_ready(instance))
2733 goto fail_ready_state;
2735 if (megasas_issue_init_mfi(instance))
2738 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2739 (unsigned long)instance);
2744 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
2745 "megasas", instance)) {
2746 printk(KERN_ERR "megasas: Failed to register IRQ\n");
2750 instance->instancet->enable_intr(instance->reg_set);
2753 * Initiate AEN (Asynchronous Event Notification)
2755 if (megasas_start_aen(instance))
2756 printk(KERN_ERR "megasas: Start AEN failed\n");
2758 /* Initialize the cmd completion timer */
2760 megasas_start_timer(instance, &instance->io_completion_timer,
2761 megasas_io_completion_timer,
2762 MEGASAS_COMPLETION_TIMER_INTERVAL);
2767 if (instance->evt_detail)
2768 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2769 instance->evt_detail,
2770 instance->evt_detail_h);
2772 if (instance->producer)
2773 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2774 instance->producer_h);
2775 if (instance->consumer)
2776 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2777 instance->consumer_h);
2778 scsi_host_put(host);
2783 pci_disable_device(pdev);
2788 #define megasas_suspend NULL
2789 #define megasas_resume NULL
2793 * megasas_detach_one - PCI hot"un"plug entry point
2794 * @pdev: PCI device structure
2796 static void __devexit megasas_detach_one(struct pci_dev *pdev)
2799 struct Scsi_Host *host;
2800 struct megasas_instance *instance;
2802 instance = pci_get_drvdata(pdev);
2803 host = instance->host;
2806 del_timer_sync(&instance->io_completion_timer);
2808 scsi_remove_host(instance->host);
2809 megasas_flush_cache(instance);
2810 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
2811 tasklet_kill(&instance->isr_tasklet);
2814 * Take the instance off the instance array. Note that we will not
2815 * decrement the max_index. We let this array be sparse array
2817 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2818 if (megasas_mgmt_info.instance[i] == instance) {
2819 megasas_mgmt_info.count--;
2820 megasas_mgmt_info.instance[i] = NULL;
2826 pci_set_drvdata(instance->pdev, NULL);
2828 instance->instancet->disable_intr(instance->reg_set);
2830 free_irq(instance->pdev->irq, instance);
2832 megasas_release_mfi(instance);
2834 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2835 instance->evt_detail, instance->evt_detail_h);
2837 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2838 instance->producer_h);
2840 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2841 instance->consumer_h);
2843 scsi_host_put(host);
2845 pci_set_drvdata(pdev, NULL);
2847 pci_disable_device(pdev);
2853 * megasas_shutdown - Shutdown entry point
2854 * @device: Generic device structure
2856 static void megasas_shutdown(struct pci_dev *pdev)
2858 struct megasas_instance *instance = pci_get_drvdata(pdev);
2859 megasas_flush_cache(instance);
2863 * megasas_mgmt_open - char node "open" entry point
2865 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2867 cycle_kernel_lock();
2869 * Allow only those users with admin rights
2871 if (!capable(CAP_SYS_ADMIN))
2878 * megasas_mgmt_release - char node "release" entry point
2880 static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2882 filep->private_data = NULL;
2883 fasync_helper(-1, filep, 0, &megasas_async_queue);
2889 * megasas_mgmt_fasync - Async notifier registration from applications
2891 * This function adds the calling process to a driver global queue. When an
2892 * event occurs, SIGIO will be sent to all processes in this queue.
2894 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2898 mutex_lock(&megasas_async_queue_mutex);
2900 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2902 mutex_unlock(&megasas_async_queue_mutex);
2905 /* For sanity check when we get ioctl */
2906 filep->private_data = filep;
2910 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2916 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2917 * @instance: Adapter soft state
2918 * @argp: User's ioctl packet
2921 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2922 struct megasas_iocpacket __user * user_ioc,
2923 struct megasas_iocpacket *ioc)
2925 struct megasas_sge32 *kern_sge32;
2926 struct megasas_cmd *cmd;
2927 void *kbuff_arr[MAX_IOCTL_SGE];
2928 dma_addr_t buf_handle = 0;
2931 dma_addr_t sense_handle;
2934 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2936 if (ioc->sge_count > MAX_IOCTL_SGE) {
2937 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2938 ioc->sge_count, MAX_IOCTL_SGE);
2942 cmd = megasas_get_cmd(instance);
2944 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2949 * User's IOCTL packet has 2 frames (maximum). Copy those two
2950 * frames into our cmd's frames. cmd->frame's context will get
2951 * overwritten when we copy from user's frames. So set that value
2954 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2955 cmd->frame->hdr.context = cmd->index;
2958 * The management interface between applications and the fw uses
2959 * MFI frames. E.g, RAID configuration changes, LD property changes
2960 * etc are accomplishes through different kinds of MFI frames. The
2961 * driver needs to care only about substituting user buffers with
2962 * kernel buffers in SGLs. The location of SGL is embedded in the
2963 * struct iocpacket itself.
2965 kern_sge32 = (struct megasas_sge32 *)
2966 ((unsigned long)cmd->frame + ioc->sgl_off);
2969 * For each user buffer, create a mirror buffer and copy in
2971 for (i = 0; i < ioc->sge_count; i++) {
2972 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
2973 ioc->sgl[i].iov_len,
2974 &buf_handle, GFP_KERNEL);
2975 if (!kbuff_arr[i]) {
2976 printk(KERN_DEBUG "megasas: Failed to alloc "
2977 "kernel SGL buffer for IOCTL \n");
2983 * We don't change the dma_coherent_mask, so
2984 * pci_alloc_consistent only returns 32bit addresses
2986 kern_sge32[i].phys_addr = (u32) buf_handle;
2987 kern_sge32[i].length = ioc->sgl[i].iov_len;
2990 * We created a kernel buffer corresponding to the
2991 * user buffer. Now copy in from the user buffer
2993 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2994 (u32) (ioc->sgl[i].iov_len))) {
3000 if (ioc->sense_len) {
3001 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
3002 &sense_handle, GFP_KERNEL);
3009 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
3010 *sense_ptr = sense_handle;
3014 * Set the sync_cmd flag so that the ISR knows not to complete this
3015 * cmd to the SCSI mid-layer
3018 megasas_issue_blocked_cmd(instance, cmd);
3022 * copy out the kernel buffers to user buffers
3024 for (i = 0; i < ioc->sge_count; i++) {
3025 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3026 ioc->sgl[i].iov_len)) {
3033 * copy out the sense
3035 if (ioc->sense_len) {
3037 * sense_ptr points to the location that has the user
3038 * sense buffer address
3040 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
3043 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3044 sense, ioc->sense_len)) {
3045 printk(KERN_ERR "megasas: Failed to copy out to user "
3053 * copy the status codes returned by the fw
3055 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3056 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3057 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3063 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
3064 sense, sense_handle);
3067 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
3068 dma_free_coherent(&instance->pdev->dev,
3069 kern_sge32[i].length,
3070 kbuff_arr[i], kern_sge32[i].phys_addr);
3073 megasas_return_cmd(instance, cmd);
3077 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
3081 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3083 if ((megasas_mgmt_info.instance[i]) &&
3084 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
3085 return megasas_mgmt_info.instance[i];
3091 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3093 struct megasas_iocpacket __user *user_ioc =
3094 (struct megasas_iocpacket __user *)arg;
3095 struct megasas_iocpacket *ioc;
3096 struct megasas_instance *instance;
3099 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3103 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3108 instance = megasas_lookup_instance(ioc->host_no);
3115 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3117 if (down_interruptible(&instance->ioctl_sem)) {
3118 error = -ERESTARTSYS;
3121 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3122 up(&instance->ioctl_sem);
3129 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3131 struct megasas_instance *instance;
3132 struct megasas_aen aen;
3135 if (file->private_data != file) {
3136 printk(KERN_DEBUG "megasas: fasync_helper was not "
3141 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3144 instance = megasas_lookup_instance(aen.host_no);
3149 mutex_lock(&instance->aen_mutex);
3150 error = megasas_register_aen(instance, aen.seq_num,
3151 aen.class_locale_word);
3152 mutex_unlock(&instance->aen_mutex);
3157 * megasas_mgmt_ioctl - char node ioctl entry point
3160 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3163 case MEGASAS_IOC_FIRMWARE:
3164 return megasas_mgmt_ioctl_fw(file, arg);
3166 case MEGASAS_IOC_GET_AEN:
3167 return megasas_mgmt_ioctl_aen(file, arg);
3173 #ifdef CONFIG_COMPAT
3174 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3176 struct compat_megasas_iocpacket __user *cioc =
3177 (struct compat_megasas_iocpacket __user *)arg;
3178 struct megasas_iocpacket __user *ioc =
3179 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3183 if (clear_user(ioc, sizeof(*ioc)))
3186 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3187 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3188 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3189 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3190 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3191 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3194 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3197 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3198 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3199 copy_in_user(&ioc->sgl[i].iov_len,
3200 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3204 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3206 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3207 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3208 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3215 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3219 case MEGASAS_IOC_FIRMWARE32:
3220 return megasas_mgmt_compat_ioctl_fw(file, arg);
3221 case MEGASAS_IOC_GET_AEN:
3222 return megasas_mgmt_ioctl_aen(file, arg);
3230 * File operations structure for management interface
3232 static const struct file_operations megasas_mgmt_fops = {
3233 .owner = THIS_MODULE,
3234 .open = megasas_mgmt_open,
3235 .release = megasas_mgmt_release,
3236 .fasync = megasas_mgmt_fasync,
3237 .unlocked_ioctl = megasas_mgmt_ioctl,
3238 #ifdef CONFIG_COMPAT
3239 .compat_ioctl = megasas_mgmt_compat_ioctl,
3244 * PCI hotplug support registration structure
3246 static struct pci_driver megasas_pci_driver = {
3248 .name = "megaraid_sas",
3249 .id_table = megasas_pci_table,
3250 .probe = megasas_probe_one,
3251 .remove = __devexit_p(megasas_detach_one),
3252 .suspend = megasas_suspend,
3253 .resume = megasas_resume,
3254 .shutdown = megasas_shutdown,
3258 * Sysfs driver attributes
3260 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3262 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3266 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3269 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3271 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3275 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3279 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3281 return sprintf(buf, "%u\n", megasas_dbg_lvl);
3285 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3288 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3289 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3295 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
3296 megasas_sysfs_set_dbg_lvl);
3299 megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3301 return sprintf(buf, "%u\n", poll_mode_io);
3305 megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3306 const char *buf, size_t count)
3309 int tmp = poll_mode_io;
3311 struct megasas_instance *instance;
3313 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3314 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3319 * Check if poll_mode_io is already set or is same as previous value
3321 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3326 * Start timers for all adapters
3328 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3329 instance = megasas_mgmt_info.instance[i];
3331 megasas_start_timer(instance,
3332 &instance->io_completion_timer,
3333 megasas_io_completion_timer,
3334 MEGASAS_COMPLETION_TIMER_INTERVAL);
3339 * Delete timers for all adapters
3341 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3342 instance = megasas_mgmt_info.instance[i];
3344 del_timer_sync(&instance->io_completion_timer);
3352 static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3353 megasas_sysfs_show_poll_mode_io,
3354 megasas_sysfs_set_poll_mode_io);
3357 * megasas_init - Driver load entry point
3359 static int __init megasas_init(void)
3364 * Announce driver version and other information
3366 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3367 MEGASAS_EXT_VERSION);
3369 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3372 * Register character device node
3374 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3377 printk(KERN_DEBUG "megasas: failed to open device node\n");
3381 megasas_mgmt_majorno = rval;
3384 * Register ourselves as PCI hotplug module
3386 rval = pci_register_driver(&megasas_pci_driver);
3389 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
3393 rval = driver_create_file(&megasas_pci_driver.driver,
3394 &driver_attr_version);
3396 goto err_dcf_attr_ver;
3397 rval = driver_create_file(&megasas_pci_driver.driver,
3398 &driver_attr_release_date);
3400 goto err_dcf_rel_date;
3401 rval = driver_create_file(&megasas_pci_driver.driver,
3402 &driver_attr_dbg_lvl);
3404 goto err_dcf_dbg_lvl;
3405 rval = driver_create_file(&megasas_pci_driver.driver,
3406 &driver_attr_poll_mode_io);
3408 goto err_dcf_poll_mode_io;
3412 err_dcf_poll_mode_io:
3413 driver_remove_file(&megasas_pci_driver.driver,
3414 &driver_attr_dbg_lvl);
3416 driver_remove_file(&megasas_pci_driver.driver,
3417 &driver_attr_release_date);
3419 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3421 pci_unregister_driver(&megasas_pci_driver);
3423 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3428 * megasas_exit - Driver unload entry point
3430 static void __exit megasas_exit(void)
3432 driver_remove_file(&megasas_pci_driver.driver,
3433 &driver_attr_poll_mode_io);
3434 driver_remove_file(&megasas_pci_driver.driver,
3435 &driver_attr_dbg_lvl);
3436 driver_remove_file(&megasas_pci_driver.driver,
3437 &driver_attr_release_date);
3438 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3440 pci_unregister_driver(&megasas_pci_driver);
3441 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3444 module_init(megasas_init);
3445 module_exit(megasas_exit);