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.16-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/uio.h>
37 #include <asm/uaccess.h>
39 #include <linux/compat.h>
40 #include <linux/blkdev.h>
41 #include <linux/mutex.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_device.h>
46 #include <scsi/scsi_host.h>
47 #include "megaraid_sas.h"
50 * poll_mode_io:1- schedule complete completion from q cmd
52 static unsigned int poll_mode_io;
53 module_param_named(poll_mode_io, poll_mode_io, int, 0);
54 MODULE_PARM_DESC(poll_mode_io,
55 "Complete cmds from IO path, (default=0)");
57 MODULE_LICENSE("GPL");
58 MODULE_VERSION(MEGASAS_VERSION);
59 MODULE_AUTHOR("megaraidlinux@lsi.com");
60 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
63 * PCI ID table for all supported controllers
65 static struct pci_device_id megasas_pci_table[] = {
67 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
69 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
71 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
72 /* xscale IOP, vega */
73 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
78 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
80 static int megasas_mgmt_majorno;
81 static struct megasas_mgmt_info megasas_mgmt_info;
82 static struct fasync_struct *megasas_async_queue;
83 static DEFINE_MUTEX(megasas_async_queue_mutex);
85 static u32 megasas_dbg_lvl;
88 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
92 * megasas_get_cmd - Get a command from the free pool
93 * @instance: Adapter soft state
95 * Returns a free command from the pool
97 static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
101 struct megasas_cmd *cmd = NULL;
103 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
105 if (!list_empty(&instance->cmd_pool)) {
106 cmd = list_entry((&instance->cmd_pool)->next,
107 struct megasas_cmd, list);
108 list_del_init(&cmd->list);
110 printk(KERN_ERR "megasas: Command pool empty!\n");
113 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
118 * megasas_return_cmd - Return a cmd to free command pool
119 * @instance: Adapter soft state
120 * @cmd: Command packet to be returned to free command pool
123 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
127 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
130 list_add_tail(&cmd->list, &instance->cmd_pool);
132 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
137 * The following functions are defined for xscale
138 * (deviceid : 1064R, PERC5) controllers
142 * megasas_enable_intr_xscale - Enables interrupts
143 * @regs: MFI register set
146 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
148 writel(1, &(regs)->outbound_intr_mask);
150 /* Dummy readl to force pci flush */
151 readl(®s->outbound_intr_mask);
155 * megasas_disable_intr_xscale -Disables interrupt
156 * @regs: MFI register set
159 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
162 writel(mask, ®s->outbound_intr_mask);
163 /* Dummy readl to force pci flush */
164 readl(®s->outbound_intr_mask);
168 * megasas_read_fw_status_reg_xscale - returns the current FW status value
169 * @regs: MFI register set
172 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
174 return readl(&(regs)->outbound_msg_0);
177 * megasas_clear_interrupt_xscale - Check & clear interrupt
178 * @regs: MFI register set
181 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
185 * Check if it is our interrupt
187 status = readl(®s->outbound_intr_status);
189 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
194 * Clear the interrupt by writing back the same value
196 writel(status, ®s->outbound_intr_status);
202 * megasas_fire_cmd_xscale - Sends command to the FW
203 * @frame_phys_addr : Physical address of cmd
204 * @frame_count : Number of frames for the command
205 * @regs : MFI register set
208 megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
210 writel((frame_phys_addr >> 3)|(frame_count),
211 &(regs)->inbound_queue_port);
214 static struct megasas_instance_template megasas_instance_template_xscale = {
216 .fire_cmd = megasas_fire_cmd_xscale,
217 .enable_intr = megasas_enable_intr_xscale,
218 .disable_intr = megasas_disable_intr_xscale,
219 .clear_intr = megasas_clear_intr_xscale,
220 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
224 * This is the end of set of functions & definitions specific
225 * to xscale (deviceid : 1064R, PERC5) controllers
229 * The following functions are defined for ppc (deviceid : 0x60)
234 * megasas_enable_intr_ppc - Enables interrupts
235 * @regs: MFI register set
238 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
240 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
242 writel(~0x80000004, &(regs)->outbound_intr_mask);
244 /* Dummy readl to force pci flush */
245 readl(®s->outbound_intr_mask);
249 * megasas_disable_intr_ppc - Disable interrupt
250 * @regs: MFI register set
253 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
255 u32 mask = 0xFFFFFFFF;
256 writel(mask, ®s->outbound_intr_mask);
257 /* Dummy readl to force pci flush */
258 readl(®s->outbound_intr_mask);
262 * megasas_read_fw_status_reg_ppc - returns the current FW status value
263 * @regs: MFI register set
266 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
268 return readl(&(regs)->outbound_scratch_pad);
272 * megasas_clear_interrupt_ppc - Check & clear interrupt
273 * @regs: MFI register set
276 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
280 * Check if it is our interrupt
282 status = readl(®s->outbound_intr_status);
284 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
289 * Clear the interrupt by writing back the same value
291 writel(status, ®s->outbound_doorbell_clear);
296 * megasas_fire_cmd_ppc - Sends command to the FW
297 * @frame_phys_addr : Physical address of cmd
298 * @frame_count : Number of frames for the command
299 * @regs : MFI register set
302 megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
304 writel((frame_phys_addr | (frame_count<<1))|1,
305 &(regs)->inbound_queue_port);
308 static struct megasas_instance_template megasas_instance_template_ppc = {
310 .fire_cmd = megasas_fire_cmd_ppc,
311 .enable_intr = megasas_enable_intr_ppc,
312 .disable_intr = megasas_disable_intr_ppc,
313 .clear_intr = megasas_clear_intr_ppc,
314 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
318 * This is the end of set of functions & definitions
319 * specific to ppc (deviceid : 0x60) controllers
323 * megasas_issue_polled - Issues a polling command
324 * @instance: Adapter soft state
325 * @cmd: Command packet to be issued
327 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
330 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
333 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
335 struct megasas_header *frame_hdr = &cmd->frame->hdr;
337 frame_hdr->cmd_status = 0xFF;
338 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
341 * Issue the frame using inbound queue port
343 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
346 * Wait for cmd_status to change
348 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
353 if (frame_hdr->cmd_status == 0xff)
360 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
361 * @instance: Adapter soft state
362 * @cmd: Command to be issued
364 * This function waits on an event for the command to be returned from ISR.
365 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
366 * Used to issue ioctl commands.
369 megasas_issue_blocked_cmd(struct megasas_instance *instance,
370 struct megasas_cmd *cmd)
372 cmd->cmd_status = ENODATA;
374 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
376 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
377 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
383 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
384 * @instance: Adapter soft state
385 * @cmd_to_abort: Previously issued cmd to be aborted
387 * MFI firmware can abort previously issued AEN comamnd (automatic event
388 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
389 * cmd and waits for return status.
390 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
393 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
394 struct megasas_cmd *cmd_to_abort)
396 struct megasas_cmd *cmd;
397 struct megasas_abort_frame *abort_fr;
399 cmd = megasas_get_cmd(instance);
404 abort_fr = &cmd->frame->abort;
407 * Prepare and issue the abort frame
409 abort_fr->cmd = MFI_CMD_ABORT;
410 abort_fr->cmd_status = 0xFF;
412 abort_fr->abort_context = cmd_to_abort->index;
413 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
414 abort_fr->abort_mfi_phys_addr_hi = 0;
417 cmd->cmd_status = 0xFF;
419 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
422 * Wait for this cmd to complete
424 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
425 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
427 megasas_return_cmd(instance, cmd);
432 * megasas_make_sgl32 - Prepares 32-bit SGL
433 * @instance: Adapter soft state
434 * @scp: SCSI command from the mid-layer
435 * @mfi_sgl: SGL to be filled in
437 * If successful, this function returns the number of SG elements. Otherwise,
441 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
442 union megasas_sgl *mfi_sgl)
446 struct scatterlist *os_sgl;
448 sge_count = scsi_dma_map(scp);
449 BUG_ON(sge_count < 0);
452 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
453 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
454 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
461 * megasas_make_sgl64 - Prepares 64-bit SGL
462 * @instance: Adapter soft state
463 * @scp: SCSI command from the mid-layer
464 * @mfi_sgl: SGL to be filled in
466 * If successful, this function returns the number of SG elements. Otherwise,
470 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
471 union megasas_sgl *mfi_sgl)
475 struct scatterlist *os_sgl;
477 sge_count = scsi_dma_map(scp);
478 BUG_ON(sge_count < 0);
481 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
482 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
483 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
490 * megasas_get_frame_count - Computes the number of frames
491 * @frame_type : type of frame- io or pthru frame
492 * @sge_count : number of sg elements
494 * Returns the number of frames required for numnber of sge's (sge_count)
497 static u32 megasas_get_frame_count(u8 sge_count, u8 frame_type)
504 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
505 sizeof(struct megasas_sge32);
508 * Main frame can contain 2 SGEs for 64-bit SGLs and
509 * 3 SGEs for 32-bit SGLs for ldio &
510 * 1 SGEs for 64-bit SGLs and
511 * 2 SGEs for 32-bit SGLs for pthru frame
513 if (unlikely(frame_type == PTHRU_FRAME)) {
515 num_cnt = sge_count - 1;
517 num_cnt = sge_count - 2;
520 num_cnt = sge_count - 2;
522 num_cnt = sge_count - 3;
526 sge_bytes = sge_sz * num_cnt;
528 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
529 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
540 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
541 * @instance: Adapter soft state
543 * @cmd: Command to be prepared in
545 * This function prepares CDB commands. These are typcially pass-through
546 * commands to the devices.
549 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
550 struct megasas_cmd *cmd)
555 struct megasas_pthru_frame *pthru;
557 is_logical = MEGASAS_IS_LOGICAL(scp);
558 device_id = MEGASAS_DEV_INDEX(instance, scp);
559 pthru = (struct megasas_pthru_frame *)cmd->frame;
561 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
562 flags = MFI_FRAME_DIR_WRITE;
563 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
564 flags = MFI_FRAME_DIR_READ;
565 else if (scp->sc_data_direction == PCI_DMA_NONE)
566 flags = MFI_FRAME_DIR_NONE;
569 * Prepare the DCDB frame
571 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
572 pthru->cmd_status = 0x0;
573 pthru->scsi_status = 0x0;
574 pthru->target_id = device_id;
575 pthru->lun = scp->device->lun;
576 pthru->cdb_len = scp->cmd_len;
578 pthru->flags = flags;
579 pthru->data_xfer_len = scsi_bufflen(scp);
581 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
587 pthru->flags |= MFI_FRAME_SGL64;
588 pthru->sge_count = megasas_make_sgl64(instance, scp,
591 pthru->sge_count = megasas_make_sgl32(instance, scp,
595 * Sense info specific
597 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
598 pthru->sense_buf_phys_addr_hi = 0;
599 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
602 * Compute the total number of frames this command consumes. FW uses
603 * this number to pull sufficient number of frames from host memory.
605 cmd->frame_count = megasas_get_frame_count(pthru->sge_count,
608 return cmd->frame_count;
612 * megasas_build_ldio - Prepares IOs to logical devices
613 * @instance: Adapter soft state
615 * @cmd: Command to to be prepared
617 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
620 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
621 struct megasas_cmd *cmd)
624 u8 sc = scp->cmnd[0];
626 struct megasas_io_frame *ldio;
628 device_id = MEGASAS_DEV_INDEX(instance, scp);
629 ldio = (struct megasas_io_frame *)cmd->frame;
631 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
632 flags = MFI_FRAME_DIR_WRITE;
633 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
634 flags = MFI_FRAME_DIR_READ;
637 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
639 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
640 ldio->cmd_status = 0x0;
641 ldio->scsi_status = 0x0;
642 ldio->target_id = device_id;
644 ldio->reserved_0 = 0;
647 ldio->start_lba_hi = 0;
648 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
651 * 6-byte READ(0x08) or WRITE(0x0A) cdb
653 if (scp->cmd_len == 6) {
654 ldio->lba_count = (u32) scp->cmnd[4];
655 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
656 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
658 ldio->start_lba_lo &= 0x1FFFFF;
662 * 10-byte READ(0x28) or WRITE(0x2A) cdb
664 else if (scp->cmd_len == 10) {
665 ldio->lba_count = (u32) scp->cmnd[8] |
666 ((u32) scp->cmnd[7] << 8);
667 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
668 ((u32) scp->cmnd[3] << 16) |
669 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
673 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
675 else if (scp->cmd_len == 12) {
676 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
677 ((u32) scp->cmnd[7] << 16) |
678 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
680 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
681 ((u32) scp->cmnd[3] << 16) |
682 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
686 * 16-byte READ(0x88) or WRITE(0x8A) cdb
688 else if (scp->cmd_len == 16) {
689 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
690 ((u32) scp->cmnd[11] << 16) |
691 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
693 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
694 ((u32) scp->cmnd[7] << 16) |
695 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
697 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
698 ((u32) scp->cmnd[3] << 16) |
699 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
707 ldio->flags |= MFI_FRAME_SGL64;
708 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
710 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
713 * Sense info specific
715 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
716 ldio->sense_buf_phys_addr_hi = 0;
717 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
720 * Compute the total number of frames this command consumes. FW uses
721 * this number to pull sufficient number of frames from host memory.
723 cmd->frame_count = megasas_get_frame_count(ldio->sge_count, IO_FRAME);
725 return cmd->frame_count;
729 * megasas_is_ldio - Checks if the cmd is for logical drive
730 * @scmd: SCSI command
732 * Called by megasas_queue_command to find out if the command to be queued
733 * is a logical drive command
735 static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
737 if (!MEGASAS_IS_LOGICAL(cmd))
739 switch (cmd->cmnd[0]) {
755 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
757 * @instance: Adapter soft state
760 megasas_dump_pending_frames(struct megasas_instance *instance)
762 struct megasas_cmd *cmd;
764 union megasas_sgl *mfi_sgl;
765 struct megasas_io_frame *ldio;
766 struct megasas_pthru_frame *pthru;
768 u32 max_cmd = instance->max_fw_cmds;
770 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
771 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
773 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
775 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
777 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
778 for (i = 0; i < max_cmd; i++) {
779 cmd = instance->cmd_list[i];
782 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
783 if (megasas_is_ldio(cmd->scmd)){
784 ldio = (struct megasas_io_frame *)cmd->frame;
785 mfi_sgl = &ldio->sgl;
786 sgcount = ldio->sge_count;
787 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);
790 pthru = (struct megasas_pthru_frame *) cmd->frame;
791 mfi_sgl = &pthru->sgl;
792 sgcount = pthru->sge_count;
793 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);
795 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
796 for (n = 0; n < sgcount; n++){
798 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) ;
800 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
803 printk(KERN_ERR "\n");
805 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
806 for (i = 0; i < max_cmd; i++) {
808 cmd = instance->cmd_list[i];
810 if(cmd->sync_cmd == 1){
811 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
814 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
818 * megasas_queue_command - Queue entry point
819 * @scmd: SCSI command to be queued
820 * @done: Callback entry point
823 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
826 struct megasas_cmd *cmd;
827 struct megasas_instance *instance;
829 instance = (struct megasas_instance *)
830 scmd->device->host->hostdata;
832 /* Don't process if we have already declared adapter dead */
833 if (instance->hw_crit_error)
834 return SCSI_MLQUEUE_HOST_BUSY;
836 scmd->scsi_done = done;
839 if (MEGASAS_IS_LOGICAL(scmd) &&
840 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
841 scmd->result = DID_BAD_TARGET << 16;
845 switch (scmd->cmnd[0]) {
846 case SYNCHRONIZE_CACHE:
848 * FW takes care of flush cache on its own
849 * No need to send it down
851 scmd->result = DID_OK << 16;
857 cmd = megasas_get_cmd(instance);
859 return SCSI_MLQUEUE_HOST_BUSY;
862 * Logical drive command
864 if (megasas_is_ldio(scmd))
865 frame_count = megasas_build_ldio(instance, scmd, cmd);
867 frame_count = megasas_build_dcdb(instance, scmd, cmd);
873 scmd->SCp.ptr = (char *)cmd;
876 * Issue the command to the FW
878 atomic_inc(&instance->fw_outstanding);
880 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
882 * Check if we have pend cmds to be completed
884 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
885 tasklet_schedule(&instance->isr_tasklet);
891 megasas_return_cmd(instance, cmd);
897 static int megasas_slave_configure(struct scsi_device *sdev)
900 * Don't export physical disk devices to the disk driver.
902 * FIXME: Currently we don't export them to the midlayer at all.
903 * That will be fixed once LSI engineers have audited the
904 * firmware for possible issues.
906 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
910 * The RAID firmware may require extended timeouts.
912 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
913 sdev->timeout = MEGASAS_DEFAULT_CMD_TIMEOUT * HZ;
918 * megasas_complete_cmd_dpc - Returns FW's controller structure
919 * @instance_addr: Address of adapter soft state
921 * Tasklet to complete cmds
923 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
928 struct megasas_cmd *cmd;
929 struct megasas_instance *instance =
930 (struct megasas_instance *)instance_addr;
933 /* If we have already declared adapter dead, donot complete cmds */
934 if (instance->hw_crit_error)
937 spin_lock_irqsave(&instance->completion_lock, flags);
939 producer = *instance->producer;
940 consumer = *instance->consumer;
942 while (consumer != producer) {
943 context = instance->reply_queue[consumer];
945 cmd = instance->cmd_list[context];
947 megasas_complete_cmd(instance, cmd, DID_OK);
950 if (consumer == (instance->max_fw_cmds + 1)) {
955 *instance->consumer = producer;
957 spin_unlock_irqrestore(&instance->completion_lock, flags);
960 * Check if we can restore can_queue
962 if (instance->flag & MEGASAS_FW_BUSY
963 && time_after(jiffies, instance->last_time + 5 * HZ)
964 && atomic_read(&instance->fw_outstanding) < 17) {
966 spin_lock_irqsave(instance->host->host_lock, flags);
967 instance->flag &= ~MEGASAS_FW_BUSY;
968 instance->host->can_queue =
969 instance->max_fw_cmds - MEGASAS_INT_CMDS;
971 spin_unlock_irqrestore(instance->host->host_lock, flags);
976 * megasas_wait_for_outstanding - Wait for all outstanding cmds
977 * @instance: Adapter soft state
979 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
980 * complete all its outstanding commands. Returns error if one or more IOs
981 * are pending after this time period. It also marks the controller dead.
983 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
986 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
988 for (i = 0; i < wait_time; i++) {
990 int outstanding = atomic_read(&instance->fw_outstanding);
995 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
996 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
997 "commands to complete\n",i,outstanding);
999 * Call cmd completion routine. Cmd to be
1000 * be completed directly without depending on isr.
1002 megasas_complete_cmd_dpc((unsigned long)instance);
1008 if (atomic_read(&instance->fw_outstanding)) {
1010 * Send signal to FW to stop processing any pending cmds.
1011 * The controller will be taken offline by the OS now.
1013 writel(MFI_STOP_ADP,
1014 &instance->reg_set->inbound_doorbell);
1015 megasas_dump_pending_frames(instance);
1016 instance->hw_crit_error = 1;
1024 * megasas_generic_reset - Generic reset routine
1025 * @scmd: Mid-layer SCSI command
1027 * This routine implements a generic reset handler for device, bus and host
1028 * reset requests. Device, bus and host specific reset handlers can use this
1029 * function after they do their specific tasks.
1031 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1034 struct megasas_instance *instance;
1036 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1038 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1039 scmd->serial_number, scmd->cmnd[0], scmd->retries);
1041 if (instance->hw_crit_error) {
1042 printk(KERN_ERR "megasas: cannot recover from previous reset "
1047 ret_val = megasas_wait_for_outstanding(instance);
1048 if (ret_val == SUCCESS)
1049 printk(KERN_NOTICE "megasas: reset successful \n");
1051 printk(KERN_ERR "megasas: failed to do reset\n");
1057 * megasas_reset_timer - quiesce the adapter if required
1060 * Sets the FW busy flag and reduces the host->can_queue if the
1061 * cmd has not been completed within the timeout period.
1064 scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1066 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1067 struct megasas_instance *instance;
1068 unsigned long flags;
1070 if (time_after(jiffies, scmd->jiffies_at_alloc +
1071 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1072 return EH_NOT_HANDLED;
1075 instance = cmd->instance;
1076 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1077 /* FW is busy, throttle IO */
1078 spin_lock_irqsave(instance->host->host_lock, flags);
1080 instance->host->can_queue = 16;
1081 instance->last_time = jiffies;
1082 instance->flag |= MEGASAS_FW_BUSY;
1084 spin_unlock_irqrestore(instance->host->host_lock, flags);
1086 return EH_RESET_TIMER;
1090 * megasas_reset_device - Device reset handler entry point
1092 static int megasas_reset_device(struct scsi_cmnd *scmd)
1097 * First wait for all commands to complete
1099 ret = megasas_generic_reset(scmd);
1105 * megasas_reset_bus_host - Bus & host reset handler entry point
1107 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1112 * First wait for all commands to complete
1114 ret = megasas_generic_reset(scmd);
1120 * megasas_bios_param - Returns disk geometry for a disk
1121 * @sdev: device handle
1122 * @bdev: block device
1123 * @capacity: drive capacity
1124 * @geom: geometry parameters
1127 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1128 sector_t capacity, int geom[])
1134 /* Default heads (64) & sectors (32) */
1138 tmp = heads * sectors;
1139 cylinders = capacity;
1141 sector_div(cylinders, tmp);
1144 * Handle extended translation size for logical drives > 1Gb
1147 if (capacity >= 0x200000) {
1150 tmp = heads*sectors;
1151 cylinders = capacity;
1152 sector_div(cylinders, tmp);
1157 geom[2] = cylinders;
1163 * megasas_service_aen - Processes an event notification
1164 * @instance: Adapter soft state
1165 * @cmd: AEN command completed by the ISR
1167 * For AEN, driver sends a command down to FW that is held by the FW till an
1168 * event occurs. When an event of interest occurs, FW completes the command
1169 * that it was previously holding.
1171 * This routines sends SIGIO signal to processes that have registered with the
1175 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1178 * Don't signal app if it is just an aborted previously registered aen
1180 if (!cmd->abort_aen)
1181 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1185 instance->aen_cmd = NULL;
1186 megasas_return_cmd(instance, cmd);
1190 * Scsi host template for megaraid_sas driver
1192 static struct scsi_host_template megasas_template = {
1194 .module = THIS_MODULE,
1195 .name = "LSI SAS based MegaRAID driver",
1196 .proc_name = "megaraid_sas",
1197 .slave_configure = megasas_slave_configure,
1198 .queuecommand = megasas_queue_command,
1199 .eh_device_reset_handler = megasas_reset_device,
1200 .eh_bus_reset_handler = megasas_reset_bus_host,
1201 .eh_host_reset_handler = megasas_reset_bus_host,
1202 .eh_timed_out = megasas_reset_timer,
1203 .bios_param = megasas_bios_param,
1204 .use_clustering = ENABLE_CLUSTERING,
1208 * megasas_complete_int_cmd - Completes an internal command
1209 * @instance: Adapter soft state
1210 * @cmd: Command to be completed
1212 * The megasas_issue_blocked_cmd() function waits for a command to complete
1213 * after it issues a command. This function wakes up that waiting routine by
1214 * calling wake_up() on the wait queue.
1217 megasas_complete_int_cmd(struct megasas_instance *instance,
1218 struct megasas_cmd *cmd)
1220 cmd->cmd_status = cmd->frame->io.cmd_status;
1222 if (cmd->cmd_status == ENODATA) {
1223 cmd->cmd_status = 0;
1225 wake_up(&instance->int_cmd_wait_q);
1229 * megasas_complete_abort - Completes aborting a command
1230 * @instance: Adapter soft state
1231 * @cmd: Cmd that was issued to abort another cmd
1233 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1234 * after it issues an abort on a previously issued command. This function
1235 * wakes up all functions waiting on the same wait queue.
1238 megasas_complete_abort(struct megasas_instance *instance,
1239 struct megasas_cmd *cmd)
1241 if (cmd->sync_cmd) {
1243 cmd->cmd_status = 0;
1244 wake_up(&instance->abort_cmd_wait_q);
1251 * megasas_complete_cmd - Completes a command
1252 * @instance: Adapter soft state
1253 * @cmd: Command to be completed
1254 * @alt_status: If non-zero, use this value as status to
1255 * SCSI mid-layer instead of the value returned
1256 * by the FW. This should be used if caller wants
1257 * an alternate status (as in the case of aborted
1261 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1265 struct megasas_header *hdr = &cmd->frame->hdr;
1268 cmd->scmd->SCp.ptr = NULL;
1272 case MFI_CMD_PD_SCSI_IO:
1273 case MFI_CMD_LD_SCSI_IO:
1276 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1277 * issued either through an IO path or an IOCTL path. If it
1278 * was via IOCTL, we will send it to internal completion.
1280 if (cmd->sync_cmd) {
1282 megasas_complete_int_cmd(instance, cmd);
1286 case MFI_CMD_LD_READ:
1287 case MFI_CMD_LD_WRITE:
1290 cmd->scmd->result = alt_status << 16;
1296 atomic_dec(&instance->fw_outstanding);
1298 scsi_dma_unmap(cmd->scmd);
1299 cmd->scmd->scsi_done(cmd->scmd);
1300 megasas_return_cmd(instance, cmd);
1305 switch (hdr->cmd_status) {
1308 cmd->scmd->result = DID_OK << 16;
1311 case MFI_STAT_SCSI_IO_FAILED:
1312 case MFI_STAT_LD_INIT_IN_PROGRESS:
1314 (DID_ERROR << 16) | hdr->scsi_status;
1317 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1319 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1321 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1322 memset(cmd->scmd->sense_buffer, 0,
1323 SCSI_SENSE_BUFFERSIZE);
1324 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1327 cmd->scmd->result |= DRIVER_SENSE << 24;
1332 case MFI_STAT_LD_OFFLINE:
1333 case MFI_STAT_DEVICE_NOT_FOUND:
1334 cmd->scmd->result = DID_BAD_TARGET << 16;
1338 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1340 cmd->scmd->result = DID_ERROR << 16;
1344 atomic_dec(&instance->fw_outstanding);
1346 scsi_dma_unmap(cmd->scmd);
1347 cmd->scmd->scsi_done(cmd->scmd);
1348 megasas_return_cmd(instance, cmd);
1357 * See if got an event notification
1359 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1360 megasas_service_aen(instance, cmd);
1362 megasas_complete_int_cmd(instance, cmd);
1368 * Cmd issued to abort another cmd returned
1370 megasas_complete_abort(instance, cmd);
1374 printk("megasas: Unknown command completed! [0x%X]\n",
1381 * megasas_deplete_reply_queue - Processes all completed commands
1382 * @instance: Adapter soft state
1383 * @alt_status: Alternate status to be returned to
1384 * SCSI mid-layer instead of the status
1385 * returned by the FW
1388 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1391 * Check if it is our interrupt
1392 * Clear the interrupt
1394 if(instance->instancet->clear_intr(instance->reg_set))
1397 if (instance->hw_crit_error)
1400 * Schedule the tasklet for cmd completion
1402 tasklet_schedule(&instance->isr_tasklet);
1408 * megasas_isr - isr entry point
1410 static irqreturn_t megasas_isr(int irq, void *devp)
1412 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1417 * megasas_transition_to_ready - Move the FW to READY state
1418 * @instance: Adapter soft state
1420 * During the initialization, FW passes can potentially be in any one of
1421 * several possible states. If the FW in operational, waiting-for-handshake
1422 * states, driver must take steps to bring it to ready state. Otherwise, it
1423 * has to wait for the ready state.
1426 megasas_transition_to_ready(struct megasas_instance* instance)
1433 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
1435 if (fw_state != MFI_STATE_READY)
1436 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1439 while (fw_state != MFI_STATE_READY) {
1443 case MFI_STATE_FAULT:
1445 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1448 case MFI_STATE_WAIT_HANDSHAKE:
1450 * Set the CLR bit in inbound doorbell
1452 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1453 &instance->reg_set->inbound_doorbell);
1456 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1459 case MFI_STATE_BOOT_MESSAGE_PENDING:
1460 writel(MFI_INIT_HOTPLUG,
1461 &instance->reg_set->inbound_doorbell);
1464 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1467 case MFI_STATE_OPERATIONAL:
1469 * Bring it to READY state; assuming max wait 10 secs
1471 instance->instancet->disable_intr(instance->reg_set);
1472 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
1475 cur_state = MFI_STATE_OPERATIONAL;
1478 case MFI_STATE_UNDEFINED:
1480 * This state should not last for more than 2 seconds
1483 cur_state = MFI_STATE_UNDEFINED;
1486 case MFI_STATE_BB_INIT:
1488 cur_state = MFI_STATE_BB_INIT;
1491 case MFI_STATE_FW_INIT:
1493 cur_state = MFI_STATE_FW_INIT;
1496 case MFI_STATE_FW_INIT_2:
1498 cur_state = MFI_STATE_FW_INIT_2;
1501 case MFI_STATE_DEVICE_SCAN:
1503 cur_state = MFI_STATE_DEVICE_SCAN;
1506 case MFI_STATE_FLUSH_CACHE:
1508 cur_state = MFI_STATE_FLUSH_CACHE;
1512 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1518 * The cur_state should not last for more than max_wait secs
1520 for (i = 0; i < (max_wait * 1000); i++) {
1521 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1524 if (fw_state == cur_state) {
1531 * Return error if fw_state hasn't changed after max_wait
1533 if (fw_state == cur_state) {
1534 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1535 "in %d secs\n", fw_state, max_wait);
1539 printk(KERN_INFO "megasas: FW now in Ready state\n");
1545 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1546 * @instance: Adapter soft state
1548 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1551 u32 max_cmd = instance->max_fw_cmds;
1552 struct megasas_cmd *cmd;
1554 if (!instance->frame_dma_pool)
1558 * Return all frames to pool
1560 for (i = 0; i < max_cmd; i++) {
1562 cmd = instance->cmd_list[i];
1565 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1566 cmd->frame_phys_addr);
1569 pci_pool_free(instance->sense_dma_pool, cmd->sense,
1570 cmd->sense_phys_addr);
1574 * Now destroy the pool itself
1576 pci_pool_destroy(instance->frame_dma_pool);
1577 pci_pool_destroy(instance->sense_dma_pool);
1579 instance->frame_dma_pool = NULL;
1580 instance->sense_dma_pool = NULL;
1584 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1585 * @instance: Adapter soft state
1587 * Each command packet has an embedded DMA memory buffer that is used for
1588 * filling MFI frame and the SG list that immediately follows the frame. This
1589 * function creates those DMA memory buffers for each command packet by using
1590 * PCI pool facility.
1592 static int megasas_create_frame_pool(struct megasas_instance *instance)
1600 struct megasas_cmd *cmd;
1602 max_cmd = instance->max_fw_cmds;
1605 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1606 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1608 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1609 sizeof(struct megasas_sge32);
1612 * Calculated the number of 64byte frames required for SGL
1614 sgl_sz = sge_sz * instance->max_num_sge;
1615 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1618 * We need one extra frame for the MFI command
1622 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1624 * Use DMA pool facility provided by PCI layer
1626 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1627 instance->pdev, total_sz, 64,
1630 if (!instance->frame_dma_pool) {
1631 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1635 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1636 instance->pdev, 128, 4, 0);
1638 if (!instance->sense_dma_pool) {
1639 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1641 pci_pool_destroy(instance->frame_dma_pool);
1642 instance->frame_dma_pool = NULL;
1648 * Allocate and attach a frame to each of the commands in cmd_list.
1649 * By making cmd->index as the context instead of the &cmd, we can
1650 * always use 32bit context regardless of the architecture
1652 for (i = 0; i < max_cmd; i++) {
1654 cmd = instance->cmd_list[i];
1656 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1657 GFP_KERNEL, &cmd->frame_phys_addr);
1659 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1660 GFP_KERNEL, &cmd->sense_phys_addr);
1663 * megasas_teardown_frame_pool() takes care of freeing
1664 * whatever has been allocated
1666 if (!cmd->frame || !cmd->sense) {
1667 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1668 megasas_teardown_frame_pool(instance);
1672 cmd->frame->io.context = cmd->index;
1679 * megasas_free_cmds - Free all the cmds in the free cmd pool
1680 * @instance: Adapter soft state
1682 static void megasas_free_cmds(struct megasas_instance *instance)
1685 /* First free the MFI frame pool */
1686 megasas_teardown_frame_pool(instance);
1688 /* Free all the commands in the cmd_list */
1689 for (i = 0; i < instance->max_fw_cmds; i++)
1690 kfree(instance->cmd_list[i]);
1692 /* Free the cmd_list buffer itself */
1693 kfree(instance->cmd_list);
1694 instance->cmd_list = NULL;
1696 INIT_LIST_HEAD(&instance->cmd_pool);
1700 * megasas_alloc_cmds - Allocates the command packets
1701 * @instance: Adapter soft state
1703 * Each command that is issued to the FW, whether IO commands from the OS or
1704 * internal commands like IOCTLs, are wrapped in local data structure called
1705 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1708 * Each frame has a 32-bit field called context (tag). This context is used
1709 * to get back the megasas_cmd from the frame when a frame gets completed in
1710 * the ISR. Typically the address of the megasas_cmd itself would be used as
1711 * the context. But we wanted to keep the differences between 32 and 64 bit
1712 * systems to the mininum. We always use 32 bit integers for the context. In
1713 * this driver, the 32 bit values are the indices into an array cmd_list.
1714 * This array is used only to look up the megasas_cmd given the context. The
1715 * free commands themselves are maintained in a linked list called cmd_pool.
1717 static int megasas_alloc_cmds(struct megasas_instance *instance)
1722 struct megasas_cmd *cmd;
1724 max_cmd = instance->max_fw_cmds;
1727 * instance->cmd_list is an array of struct megasas_cmd pointers.
1728 * Allocate the dynamic array first and then allocate individual
1731 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
1733 if (!instance->cmd_list) {
1734 printk(KERN_DEBUG "megasas: out of memory\n");
1739 for (i = 0; i < max_cmd; i++) {
1740 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1743 if (!instance->cmd_list[i]) {
1745 for (j = 0; j < i; j++)
1746 kfree(instance->cmd_list[j]);
1748 kfree(instance->cmd_list);
1749 instance->cmd_list = NULL;
1756 * Add all the commands to command pool (instance->cmd_pool)
1758 for (i = 0; i < max_cmd; i++) {
1759 cmd = instance->cmd_list[i];
1760 memset(cmd, 0, sizeof(struct megasas_cmd));
1762 cmd->instance = instance;
1764 list_add_tail(&cmd->list, &instance->cmd_pool);
1768 * Create a frame pool and assign one frame to each cmd
1770 if (megasas_create_frame_pool(instance)) {
1771 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1772 megasas_free_cmds(instance);
1779 * megasas_get_controller_info - Returns FW's controller structure
1780 * @instance: Adapter soft state
1781 * @ctrl_info: Controller information structure
1783 * Issues an internal command (DCMD) to get the FW's controller structure.
1784 * This information is mainly used to find out the maximum IO transfer per
1785 * command supported by the FW.
1788 megasas_get_ctrl_info(struct megasas_instance *instance,
1789 struct megasas_ctrl_info *ctrl_info)
1792 struct megasas_cmd *cmd;
1793 struct megasas_dcmd_frame *dcmd;
1794 struct megasas_ctrl_info *ci;
1795 dma_addr_t ci_h = 0;
1797 cmd = megasas_get_cmd(instance);
1800 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1804 dcmd = &cmd->frame->dcmd;
1806 ci = pci_alloc_consistent(instance->pdev,
1807 sizeof(struct megasas_ctrl_info), &ci_h);
1810 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1811 megasas_return_cmd(instance, cmd);
1815 memset(ci, 0, sizeof(*ci));
1816 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1818 dcmd->cmd = MFI_CMD_DCMD;
1819 dcmd->cmd_status = 0xFF;
1820 dcmd->sge_count = 1;
1821 dcmd->flags = MFI_FRAME_DIR_READ;
1823 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1824 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1825 dcmd->sgl.sge32[0].phys_addr = ci_h;
1826 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1828 if (!megasas_issue_polled(instance, cmd)) {
1830 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1835 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1838 megasas_return_cmd(instance, cmd);
1843 * megasas_issue_init_mfi - Initializes the FW
1844 * @instance: Adapter soft state
1846 * Issues the INIT MFI cmd
1849 megasas_issue_init_mfi(struct megasas_instance *instance)
1853 struct megasas_cmd *cmd;
1855 struct megasas_init_frame *init_frame;
1856 struct megasas_init_queue_info *initq_info;
1857 dma_addr_t init_frame_h;
1858 dma_addr_t initq_info_h;
1861 * Prepare a init frame. Note the init frame points to queue info
1862 * structure. Each frame has SGL allocated after first 64 bytes. For
1863 * this frame - since we don't need any SGL - we use SGL's space as
1864 * queue info structure
1866 * We will not get a NULL command below. We just created the pool.
1868 cmd = megasas_get_cmd(instance);
1870 init_frame = (struct megasas_init_frame *)cmd->frame;
1871 initq_info = (struct megasas_init_queue_info *)
1872 ((unsigned long)init_frame + 64);
1874 init_frame_h = cmd->frame_phys_addr;
1875 initq_info_h = init_frame_h + 64;
1877 context = init_frame->context;
1878 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1879 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1880 init_frame->context = context;
1882 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1883 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1885 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1886 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1888 init_frame->cmd = MFI_CMD_INIT;
1889 init_frame->cmd_status = 0xFF;
1890 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1892 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1895 * disable the intr before firing the init frame to FW
1897 instance->instancet->disable_intr(instance->reg_set);
1900 * Issue the init frame in polled mode
1903 if (megasas_issue_polled(instance, cmd)) {
1904 printk(KERN_ERR "megasas: Failed to init firmware\n");
1905 megasas_return_cmd(instance, cmd);
1909 megasas_return_cmd(instance, cmd);
1918 * megasas_start_timer - Initializes a timer object
1919 * @instance: Adapter soft state
1920 * @timer: timer object to be initialized
1921 * @fn: timer function
1922 * @interval: time interval between timer function call
1925 megasas_start_timer(struct megasas_instance *instance,
1926 struct timer_list *timer,
1927 void *fn, unsigned long interval)
1930 timer->expires = jiffies + interval;
1931 timer->data = (unsigned long)instance;
1932 timer->function = fn;
1937 * megasas_io_completion_timer - Timer fn
1938 * @instance_addr: Address of adapter soft state
1940 * Schedules tasklet for cmd completion
1941 * if poll_mode_io is set
1944 megasas_io_completion_timer(unsigned long instance_addr)
1946 struct megasas_instance *instance =
1947 (struct megasas_instance *)instance_addr;
1949 if (atomic_read(&instance->fw_outstanding))
1950 tasklet_schedule(&instance->isr_tasklet);
1954 mod_timer(&instance->io_completion_timer,
1955 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
1959 * megasas_init_mfi - Initializes the FW
1960 * @instance: Adapter soft state
1962 * This is the main function for initializing MFI firmware.
1964 static int megasas_init_mfi(struct megasas_instance *instance)
1971 struct megasas_register_set __iomem *reg_set;
1972 struct megasas_ctrl_info *ctrl_info;
1974 * Map the message registers
1976 instance->base_addr = pci_resource_start(instance->pdev, 0);
1978 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
1979 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1983 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1985 if (!instance->reg_set) {
1986 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1990 reg_set = instance->reg_set;
1992 switch(instance->pdev->device)
1994 case PCI_DEVICE_ID_LSI_SAS1078R:
1995 instance->instancet = &megasas_instance_template_ppc;
1997 case PCI_DEVICE_ID_LSI_SAS1064R:
1998 case PCI_DEVICE_ID_DELL_PERC5:
2000 instance->instancet = &megasas_instance_template_xscale;
2005 * We expect the FW state to be READY
2007 if (megasas_transition_to_ready(instance))
2008 goto fail_ready_state;
2011 * Get various operational parameters from status register
2013 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
2015 * Reduce the max supported cmds by 1. This is to ensure that the
2016 * reply_q_sz (1 more than the max cmd that driver may send)
2017 * does not exceed max cmds that the FW can support
2019 instance->max_fw_cmds = instance->max_fw_cmds-1;
2020 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2023 * Create a pool of commands
2025 if (megasas_alloc_cmds(instance))
2026 goto fail_alloc_cmds;
2029 * Allocate memory for reply queue. Length of reply queue should
2030 * be _one_ more than the maximum commands handled by the firmware.
2032 * Note: When FW completes commands, it places corresponding contex
2033 * values in this circular reply queue. This circular queue is a fairly
2034 * typical producer-consumer queue. FW is the producer (of completed
2035 * commands) and the driver is the consumer.
2037 context_sz = sizeof(u32);
2038 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2040 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2042 &instance->reply_queue_h);
2044 if (!instance->reply_queue) {
2045 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2046 goto fail_reply_queue;
2049 if (megasas_issue_init_mfi(instance))
2052 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2055 * Compute the max allowed sectors per IO: The controller info has two
2056 * limits on max sectors. Driver should use the minimum of these two.
2058 * 1 << stripe_sz_ops.min = max sectors per strip
2060 * Note that older firmwares ( < FW ver 30) didn't report information
2061 * to calculate max_sectors_1. So the number ended up as zero always.
2064 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2066 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2067 ctrl_info->max_strips_per_io;
2068 max_sectors_2 = ctrl_info->max_request_size;
2070 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2073 instance->max_sectors_per_req = instance->max_num_sge *
2075 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2076 instance->max_sectors_per_req = tmp_sectors;
2081 * Setup tasklet for cmd completion
2084 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2085 (unsigned long)instance);
2087 /* Initialize the cmd completion timer */
2089 megasas_start_timer(instance, &instance->io_completion_timer,
2090 megasas_io_completion_timer,
2091 MEGASAS_COMPLETION_TIMER_INTERVAL);
2096 pci_free_consistent(instance->pdev, reply_q_sz,
2097 instance->reply_queue, instance->reply_queue_h);
2099 megasas_free_cmds(instance);
2103 iounmap(instance->reg_set);
2106 pci_release_regions(instance->pdev);
2112 * megasas_release_mfi - Reverses the FW initialization
2113 * @intance: Adapter soft state
2115 static void megasas_release_mfi(struct megasas_instance *instance)
2117 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2119 pci_free_consistent(instance->pdev, reply_q_sz,
2120 instance->reply_queue, instance->reply_queue_h);
2122 megasas_free_cmds(instance);
2124 iounmap(instance->reg_set);
2126 pci_release_regions(instance->pdev);
2130 * megasas_get_seq_num - Gets latest event sequence numbers
2131 * @instance: Adapter soft state
2132 * @eli: FW event log sequence numbers information
2134 * FW maintains a log of all events in a non-volatile area. Upper layers would
2135 * usually find out the latest sequence number of the events, the seq number at
2136 * the boot etc. They would "read" all the events below the latest seq number
2137 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2138 * number), they would subsribe to AEN (asynchronous event notification) and
2139 * wait for the events to happen.
2142 megasas_get_seq_num(struct megasas_instance *instance,
2143 struct megasas_evt_log_info *eli)
2145 struct megasas_cmd *cmd;
2146 struct megasas_dcmd_frame *dcmd;
2147 struct megasas_evt_log_info *el_info;
2148 dma_addr_t el_info_h = 0;
2150 cmd = megasas_get_cmd(instance);
2156 dcmd = &cmd->frame->dcmd;
2157 el_info = pci_alloc_consistent(instance->pdev,
2158 sizeof(struct megasas_evt_log_info),
2162 megasas_return_cmd(instance, cmd);
2166 memset(el_info, 0, sizeof(*el_info));
2167 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2169 dcmd->cmd = MFI_CMD_DCMD;
2170 dcmd->cmd_status = 0x0;
2171 dcmd->sge_count = 1;
2172 dcmd->flags = MFI_FRAME_DIR_READ;
2174 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2175 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2176 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2177 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2179 megasas_issue_blocked_cmd(instance, cmd);
2182 * Copy the data back into callers buffer
2184 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2186 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2187 el_info, el_info_h);
2189 megasas_return_cmd(instance, cmd);
2195 * megasas_register_aen - Registers for asynchronous event notification
2196 * @instance: Adapter soft state
2197 * @seq_num: The starting sequence number
2198 * @class_locale: Class of the event
2200 * This function subscribes for AEN for events beyond the @seq_num. It requests
2201 * to be notified if and only if the event is of type @class_locale
2204 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2205 u32 class_locale_word)
2208 struct megasas_cmd *cmd;
2209 struct megasas_dcmd_frame *dcmd;
2210 union megasas_evt_class_locale curr_aen;
2211 union megasas_evt_class_locale prev_aen;
2214 * If there an AEN pending already (aen_cmd), check if the
2215 * class_locale of that pending AEN is inclusive of the new
2216 * AEN request we currently have. If it is, then we don't have
2217 * to do anything. In other words, whichever events the current
2218 * AEN request is subscribing to, have already been subscribed
2221 * If the old_cmd is _not_ inclusive, then we have to abort
2222 * that command, form a class_locale that is superset of both
2223 * old and current and re-issue to the FW
2226 curr_aen.word = class_locale_word;
2228 if (instance->aen_cmd) {
2230 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2233 * A class whose enum value is smaller is inclusive of all
2234 * higher values. If a PROGRESS (= -1) was previously
2235 * registered, then a new registration requests for higher
2236 * classes need not be sent to FW. They are automatically
2239 * Locale numbers don't have such hierarchy. They are bitmap
2242 if ((prev_aen.members.class <= curr_aen.members.class) &&
2243 !((prev_aen.members.locale & curr_aen.members.locale) ^
2244 curr_aen.members.locale)) {
2246 * Previously issued event registration includes
2247 * current request. Nothing to do.
2251 curr_aen.members.locale |= prev_aen.members.locale;
2253 if (prev_aen.members.class < curr_aen.members.class)
2254 curr_aen.members.class = prev_aen.members.class;
2256 instance->aen_cmd->abort_aen = 1;
2257 ret_val = megasas_issue_blocked_abort_cmd(instance,
2262 printk(KERN_DEBUG "megasas: Failed to abort "
2263 "previous AEN command\n");
2269 cmd = megasas_get_cmd(instance);
2274 dcmd = &cmd->frame->dcmd;
2276 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2279 * Prepare DCMD for aen registration
2281 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2283 dcmd->cmd = MFI_CMD_DCMD;
2284 dcmd->cmd_status = 0x0;
2285 dcmd->sge_count = 1;
2286 dcmd->flags = MFI_FRAME_DIR_READ;
2288 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2289 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2290 dcmd->mbox.w[0] = seq_num;
2291 dcmd->mbox.w[1] = curr_aen.word;
2292 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2293 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2296 * Store reference to the cmd used to register for AEN. When an
2297 * application wants us to register for AEN, we have to abort this
2298 * cmd and re-register with a new EVENT LOCALE supplied by that app
2300 instance->aen_cmd = cmd;
2303 * Issue the aen registration frame
2305 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
2311 * megasas_start_aen - Subscribes to AEN during driver load time
2312 * @instance: Adapter soft state
2314 static int megasas_start_aen(struct megasas_instance *instance)
2316 struct megasas_evt_log_info eli;
2317 union megasas_evt_class_locale class_locale;
2320 * Get the latest sequence number from FW
2322 memset(&eli, 0, sizeof(eli));
2324 if (megasas_get_seq_num(instance, &eli))
2328 * Register AEN with FW for latest sequence number plus 1
2330 class_locale.members.reserved = 0;
2331 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2332 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2334 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2339 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2340 * @instance: Adapter soft state
2342 static int megasas_io_attach(struct megasas_instance *instance)
2344 struct Scsi_Host *host = instance->host;
2347 * Export parameters required by SCSI mid-layer
2349 host->irq = instance->pdev->irq;
2350 host->unique_id = instance->unique_id;
2351 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2352 host->this_id = instance->init_id;
2353 host->sg_tablesize = instance->max_num_sge;
2354 host->max_sectors = instance->max_sectors_per_req;
2355 host->cmd_per_lun = 128;
2356 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2357 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2358 host->max_lun = MEGASAS_MAX_LUN;
2359 host->max_cmd_len = 16;
2362 * Notify the mid-layer about the new controller
2364 if (scsi_add_host(host, &instance->pdev->dev)) {
2365 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2370 * Trigger SCSI to scan our drives
2372 scsi_scan_host(host);
2377 megasas_set_dma_mask(struct pci_dev *pdev)
2380 * All our contollers are capable of performing 64-bit DMA
2383 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2385 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2386 goto fail_set_dma_mask;
2389 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2390 goto fail_set_dma_mask;
2399 * megasas_probe_one - PCI hotplug entry point
2400 * @pdev: PCI device structure
2401 * @id: PCI ids of supported hotplugged adapter
2403 static int __devinit
2404 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2407 struct Scsi_Host *host;
2408 struct megasas_instance *instance;
2411 * Announce PCI information
2413 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2414 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2415 pdev->subsystem_device);
2417 printk("bus %d:slot %d:func %d\n",
2418 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2421 * PCI prepping: enable device set bus mastering and dma mask
2423 rval = pci_enable_device(pdev);
2429 pci_set_master(pdev);
2431 if (megasas_set_dma_mask(pdev))
2432 goto fail_set_dma_mask;
2434 host = scsi_host_alloc(&megasas_template,
2435 sizeof(struct megasas_instance));
2438 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2439 goto fail_alloc_instance;
2442 instance = (struct megasas_instance *)host->hostdata;
2443 memset(instance, 0, sizeof(*instance));
2445 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2446 &instance->producer_h);
2447 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2448 &instance->consumer_h);
2450 if (!instance->producer || !instance->consumer) {
2451 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2452 "producer, consumer\n");
2453 goto fail_alloc_dma_buf;
2456 *instance->producer = 0;
2457 *instance->consumer = 0;
2459 instance->evt_detail = pci_alloc_consistent(pdev,
2461 megasas_evt_detail),
2462 &instance->evt_detail_h);
2464 if (!instance->evt_detail) {
2465 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2466 "event detail structure\n");
2467 goto fail_alloc_dma_buf;
2471 * Initialize locks and queues
2473 INIT_LIST_HEAD(&instance->cmd_pool);
2475 atomic_set(&instance->fw_outstanding,0);
2477 init_waitqueue_head(&instance->int_cmd_wait_q);
2478 init_waitqueue_head(&instance->abort_cmd_wait_q);
2480 spin_lock_init(&instance->cmd_pool_lock);
2481 spin_lock_init(&instance->completion_lock);
2483 mutex_init(&instance->aen_mutex);
2484 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2487 * Initialize PCI related and misc parameters
2489 instance->pdev = pdev;
2490 instance->host = host;
2491 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2492 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2494 megasas_dbg_lvl = 0;
2496 instance->last_time = 0;
2499 * Initialize MFI Firmware
2501 if (megasas_init_mfi(instance))
2507 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
2508 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2512 instance->instancet->enable_intr(instance->reg_set);
2515 * Store instance in PCI softstate
2517 pci_set_drvdata(pdev, instance);
2520 * Add this controller to megasas_mgmt_info structure so that it
2521 * can be exported to management applications
2523 megasas_mgmt_info.count++;
2524 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2525 megasas_mgmt_info.max_index++;
2528 * Initiate AEN (Asynchronous Event Notification)
2530 if (megasas_start_aen(instance)) {
2531 printk(KERN_DEBUG "megasas: start aen failed\n");
2532 goto fail_start_aen;
2536 * Register with SCSI mid-layer
2538 if (megasas_io_attach(instance))
2539 goto fail_io_attach;
2545 megasas_mgmt_info.count--;
2546 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2547 megasas_mgmt_info.max_index--;
2549 pci_set_drvdata(pdev, NULL);
2550 instance->instancet->disable_intr(instance->reg_set);
2551 free_irq(instance->pdev->irq, instance);
2553 megasas_release_mfi(instance);
2558 if (instance->evt_detail)
2559 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2560 instance->evt_detail,
2561 instance->evt_detail_h);
2563 if (instance->producer)
2564 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2565 instance->producer_h);
2566 if (instance->consumer)
2567 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2568 instance->consumer_h);
2569 scsi_host_put(host);
2571 fail_alloc_instance:
2573 pci_disable_device(pdev);
2579 * megasas_flush_cache - Requests FW to flush all its caches
2580 * @instance: Adapter soft state
2582 static void megasas_flush_cache(struct megasas_instance *instance)
2584 struct megasas_cmd *cmd;
2585 struct megasas_dcmd_frame *dcmd;
2587 cmd = megasas_get_cmd(instance);
2592 dcmd = &cmd->frame->dcmd;
2594 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2596 dcmd->cmd = MFI_CMD_DCMD;
2597 dcmd->cmd_status = 0x0;
2598 dcmd->sge_count = 0;
2599 dcmd->flags = MFI_FRAME_DIR_NONE;
2601 dcmd->data_xfer_len = 0;
2602 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2603 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2605 megasas_issue_blocked_cmd(instance, cmd);
2607 megasas_return_cmd(instance, cmd);
2613 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2614 * @instance: Adapter soft state
2615 * @opcode: Shutdown/Hibernate
2617 static void megasas_shutdown_controller(struct megasas_instance *instance,
2620 struct megasas_cmd *cmd;
2621 struct megasas_dcmd_frame *dcmd;
2623 cmd = megasas_get_cmd(instance);
2628 if (instance->aen_cmd)
2629 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2631 dcmd = &cmd->frame->dcmd;
2633 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2635 dcmd->cmd = MFI_CMD_DCMD;
2636 dcmd->cmd_status = 0x0;
2637 dcmd->sge_count = 0;
2638 dcmd->flags = MFI_FRAME_DIR_NONE;
2640 dcmd->data_xfer_len = 0;
2641 dcmd->opcode = opcode;
2643 megasas_issue_blocked_cmd(instance, cmd);
2645 megasas_return_cmd(instance, cmd);
2651 * megasas_suspend - driver suspend entry point
2652 * @pdev: PCI device structure
2653 * @state: PCI power state to suspend routine
2655 static int __devinit
2656 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
2658 struct Scsi_Host *host;
2659 struct megasas_instance *instance;
2661 instance = pci_get_drvdata(pdev);
2662 host = instance->host;
2665 del_timer_sync(&instance->io_completion_timer);
2667 megasas_flush_cache(instance);
2668 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
2669 tasklet_kill(&instance->isr_tasklet);
2671 pci_set_drvdata(instance->pdev, instance);
2672 instance->instancet->disable_intr(instance->reg_set);
2673 free_irq(instance->pdev->irq, instance);
2675 pci_save_state(pdev);
2676 pci_disable_device(pdev);
2678 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2684 * megasas_resume- driver resume entry point
2685 * @pdev: PCI device structure
2687 static int __devinit
2688 megasas_resume(struct pci_dev *pdev)
2691 struct Scsi_Host *host;
2692 struct megasas_instance *instance;
2694 instance = pci_get_drvdata(pdev);
2695 host = instance->host;
2696 pci_set_power_state(pdev, PCI_D0);
2697 pci_enable_wake(pdev, PCI_D0, 0);
2698 pci_restore_state(pdev);
2701 * PCI prepping: enable device set bus mastering and dma mask
2703 rval = pci_enable_device(pdev);
2706 printk(KERN_ERR "megasas: Enable device failed\n");
2710 pci_set_master(pdev);
2712 if (megasas_set_dma_mask(pdev))
2713 goto fail_set_dma_mask;
2716 * Initialize MFI Firmware
2719 *instance->producer = 0;
2720 *instance->consumer = 0;
2722 atomic_set(&instance->fw_outstanding, 0);
2725 * We expect the FW state to be READY
2727 if (megasas_transition_to_ready(instance))
2728 goto fail_ready_state;
2730 if (megasas_issue_init_mfi(instance))
2733 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2734 (unsigned long)instance);
2739 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
2740 "megasas", instance)) {
2741 printk(KERN_ERR "megasas: Failed to register IRQ\n");
2745 instance->instancet->enable_intr(instance->reg_set);
2748 * Initiate AEN (Asynchronous Event Notification)
2750 if (megasas_start_aen(instance))
2751 printk(KERN_ERR "megasas: Start AEN failed\n");
2753 /* Initialize the cmd completion timer */
2755 megasas_start_timer(instance, &instance->io_completion_timer,
2756 megasas_io_completion_timer,
2757 MEGASAS_COMPLETION_TIMER_INTERVAL);
2762 if (instance->evt_detail)
2763 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2764 instance->evt_detail,
2765 instance->evt_detail_h);
2767 if (instance->producer)
2768 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2769 instance->producer_h);
2770 if (instance->consumer)
2771 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2772 instance->consumer_h);
2773 scsi_host_put(host);
2778 pci_disable_device(pdev);
2784 * megasas_detach_one - PCI hot"un"plug entry point
2785 * @pdev: PCI device structure
2787 static void megasas_detach_one(struct pci_dev *pdev)
2790 struct Scsi_Host *host;
2791 struct megasas_instance *instance;
2793 instance = pci_get_drvdata(pdev);
2794 host = instance->host;
2797 del_timer_sync(&instance->io_completion_timer);
2799 scsi_remove_host(instance->host);
2800 megasas_flush_cache(instance);
2801 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
2802 tasklet_kill(&instance->isr_tasklet);
2805 * Take the instance off the instance array. Note that we will not
2806 * decrement the max_index. We let this array be sparse array
2808 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2809 if (megasas_mgmt_info.instance[i] == instance) {
2810 megasas_mgmt_info.count--;
2811 megasas_mgmt_info.instance[i] = NULL;
2817 pci_set_drvdata(instance->pdev, NULL);
2819 instance->instancet->disable_intr(instance->reg_set);
2821 free_irq(instance->pdev->irq, instance);
2823 megasas_release_mfi(instance);
2825 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2826 instance->evt_detail, instance->evt_detail_h);
2828 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2829 instance->producer_h);
2831 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2832 instance->consumer_h);
2834 scsi_host_put(host);
2836 pci_set_drvdata(pdev, NULL);
2838 pci_disable_device(pdev);
2844 * megasas_shutdown - Shutdown entry point
2845 * @device: Generic device structure
2847 static void megasas_shutdown(struct pci_dev *pdev)
2849 struct megasas_instance *instance = pci_get_drvdata(pdev);
2850 megasas_flush_cache(instance);
2854 * megasas_mgmt_open - char node "open" entry point
2856 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2859 * Allow only those users with admin rights
2861 if (!capable(CAP_SYS_ADMIN))
2868 * megasas_mgmt_release - char node "release" entry point
2870 static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2872 filep->private_data = NULL;
2873 fasync_helper(-1, filep, 0, &megasas_async_queue);
2879 * megasas_mgmt_fasync - Async notifier registration from applications
2881 * This function adds the calling process to a driver global queue. When an
2882 * event occurs, SIGIO will be sent to all processes in this queue.
2884 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2888 mutex_lock(&megasas_async_queue_mutex);
2890 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2892 mutex_unlock(&megasas_async_queue_mutex);
2895 /* For sanity check when we get ioctl */
2896 filep->private_data = filep;
2900 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2906 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2907 * @instance: Adapter soft state
2908 * @argp: User's ioctl packet
2911 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2912 struct megasas_iocpacket __user * user_ioc,
2913 struct megasas_iocpacket *ioc)
2915 struct megasas_sge32 *kern_sge32;
2916 struct megasas_cmd *cmd;
2917 void *kbuff_arr[MAX_IOCTL_SGE];
2918 dma_addr_t buf_handle = 0;
2921 dma_addr_t sense_handle;
2924 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2926 if (ioc->sge_count > MAX_IOCTL_SGE) {
2927 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2928 ioc->sge_count, MAX_IOCTL_SGE);
2932 cmd = megasas_get_cmd(instance);
2934 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2939 * User's IOCTL packet has 2 frames (maximum). Copy those two
2940 * frames into our cmd's frames. cmd->frame's context will get
2941 * overwritten when we copy from user's frames. So set that value
2944 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2945 cmd->frame->hdr.context = cmd->index;
2948 * The management interface between applications and the fw uses
2949 * MFI frames. E.g, RAID configuration changes, LD property changes
2950 * etc are accomplishes through different kinds of MFI frames. The
2951 * driver needs to care only about substituting user buffers with
2952 * kernel buffers in SGLs. The location of SGL is embedded in the
2953 * struct iocpacket itself.
2955 kern_sge32 = (struct megasas_sge32 *)
2956 ((unsigned long)cmd->frame + ioc->sgl_off);
2959 * For each user buffer, create a mirror buffer and copy in
2961 for (i = 0; i < ioc->sge_count; i++) {
2962 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
2963 ioc->sgl[i].iov_len,
2964 &buf_handle, GFP_KERNEL);
2965 if (!kbuff_arr[i]) {
2966 printk(KERN_DEBUG "megasas: Failed to alloc "
2967 "kernel SGL buffer for IOCTL \n");
2973 * We don't change the dma_coherent_mask, so
2974 * pci_alloc_consistent only returns 32bit addresses
2976 kern_sge32[i].phys_addr = (u32) buf_handle;
2977 kern_sge32[i].length = ioc->sgl[i].iov_len;
2980 * We created a kernel buffer corresponding to the
2981 * user buffer. Now copy in from the user buffer
2983 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2984 (u32) (ioc->sgl[i].iov_len))) {
2990 if (ioc->sense_len) {
2991 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
2992 &sense_handle, GFP_KERNEL);
2999 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
3000 *sense_ptr = sense_handle;
3004 * Set the sync_cmd flag so that the ISR knows not to complete this
3005 * cmd to the SCSI mid-layer
3008 megasas_issue_blocked_cmd(instance, cmd);
3012 * copy out the kernel buffers to user buffers
3014 for (i = 0; i < ioc->sge_count; i++) {
3015 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3016 ioc->sgl[i].iov_len)) {
3023 * copy out the sense
3025 if (ioc->sense_len) {
3027 * sense_ptr points to the location that has the user
3028 * sense buffer address
3030 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
3033 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3034 sense, ioc->sense_len)) {
3035 printk(KERN_ERR "megasas: Failed to copy out to user "
3043 * copy the status codes returned by the fw
3045 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3046 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3047 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3053 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
3054 sense, sense_handle);
3057 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
3058 dma_free_coherent(&instance->pdev->dev,
3059 kern_sge32[i].length,
3060 kbuff_arr[i], kern_sge32[i].phys_addr);
3063 megasas_return_cmd(instance, cmd);
3067 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
3071 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3073 if ((megasas_mgmt_info.instance[i]) &&
3074 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
3075 return megasas_mgmt_info.instance[i];
3081 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3083 struct megasas_iocpacket __user *user_ioc =
3084 (struct megasas_iocpacket __user *)arg;
3085 struct megasas_iocpacket *ioc;
3086 struct megasas_instance *instance;
3089 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3093 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3098 instance = megasas_lookup_instance(ioc->host_no);
3105 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3107 if (down_interruptible(&instance->ioctl_sem)) {
3108 error = -ERESTARTSYS;
3111 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3112 up(&instance->ioctl_sem);
3119 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3121 struct megasas_instance *instance;
3122 struct megasas_aen aen;
3125 if (file->private_data != file) {
3126 printk(KERN_DEBUG "megasas: fasync_helper was not "
3131 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3134 instance = megasas_lookup_instance(aen.host_no);
3139 mutex_lock(&instance->aen_mutex);
3140 error = megasas_register_aen(instance, aen.seq_num,
3141 aen.class_locale_word);
3142 mutex_unlock(&instance->aen_mutex);
3147 * megasas_mgmt_ioctl - char node ioctl entry point
3150 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3153 case MEGASAS_IOC_FIRMWARE:
3154 return megasas_mgmt_ioctl_fw(file, arg);
3156 case MEGASAS_IOC_GET_AEN:
3157 return megasas_mgmt_ioctl_aen(file, arg);
3163 #ifdef CONFIG_COMPAT
3164 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3166 struct compat_megasas_iocpacket __user *cioc =
3167 (struct compat_megasas_iocpacket __user *)arg;
3168 struct megasas_iocpacket __user *ioc =
3169 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3173 if (clear_user(ioc, sizeof(*ioc)))
3176 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3177 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3178 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3179 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3180 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3181 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3184 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3187 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3188 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3189 copy_in_user(&ioc->sgl[i].iov_len,
3190 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3194 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3196 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3197 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3198 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3205 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3209 case MEGASAS_IOC_FIRMWARE32:
3210 return megasas_mgmt_compat_ioctl_fw(file, arg);
3211 case MEGASAS_IOC_GET_AEN:
3212 return megasas_mgmt_ioctl_aen(file, arg);
3220 * File operations structure for management interface
3222 static const struct file_operations megasas_mgmt_fops = {
3223 .owner = THIS_MODULE,
3224 .open = megasas_mgmt_open,
3225 .release = megasas_mgmt_release,
3226 .fasync = megasas_mgmt_fasync,
3227 .unlocked_ioctl = megasas_mgmt_ioctl,
3228 #ifdef CONFIG_COMPAT
3229 .compat_ioctl = megasas_mgmt_compat_ioctl,
3234 * PCI hotplug support registration structure
3236 static struct pci_driver megasas_pci_driver = {
3238 .name = "megaraid_sas",
3239 .id_table = megasas_pci_table,
3240 .probe = megasas_probe_one,
3241 .remove = __devexit_p(megasas_detach_one),
3242 .suspend = megasas_suspend,
3243 .resume = megasas_resume,
3244 .shutdown = megasas_shutdown,
3248 * Sysfs driver attributes
3250 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3252 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3256 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3259 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3261 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3265 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3269 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3271 return sprintf(buf, "%u\n", megasas_dbg_lvl);
3275 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3278 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3279 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3285 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
3286 megasas_sysfs_set_dbg_lvl);
3289 megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3291 return sprintf(buf, "%u\n", poll_mode_io);
3295 megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3296 const char *buf, size_t count)
3299 int tmp = poll_mode_io;
3301 struct megasas_instance *instance;
3303 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3304 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3309 * Check if poll_mode_io is already set or is same as previous value
3311 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3316 * Start timers for all adapters
3318 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3319 instance = megasas_mgmt_info.instance[i];
3321 megasas_start_timer(instance,
3322 &instance->io_completion_timer,
3323 megasas_io_completion_timer,
3324 MEGASAS_COMPLETION_TIMER_INTERVAL);
3329 * Delete timers for all adapters
3331 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3332 instance = megasas_mgmt_info.instance[i];
3334 del_timer_sync(&instance->io_completion_timer);
3342 static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3343 megasas_sysfs_show_poll_mode_io,
3344 megasas_sysfs_set_poll_mode_io);
3347 * megasas_init - Driver load entry point
3349 static int __init megasas_init(void)
3354 * Announce driver version and other information
3356 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3357 MEGASAS_EXT_VERSION);
3359 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3362 * Register character device node
3364 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3367 printk(KERN_DEBUG "megasas: failed to open device node\n");
3371 megasas_mgmt_majorno = rval;
3374 * Register ourselves as PCI hotplug module
3376 rval = pci_register_driver(&megasas_pci_driver);
3379 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
3383 rval = driver_create_file(&megasas_pci_driver.driver,
3384 &driver_attr_version);
3386 goto err_dcf_attr_ver;
3387 rval = driver_create_file(&megasas_pci_driver.driver,
3388 &driver_attr_release_date);
3390 goto err_dcf_rel_date;
3391 rval = driver_create_file(&megasas_pci_driver.driver,
3392 &driver_attr_dbg_lvl);
3394 goto err_dcf_dbg_lvl;
3395 rval = driver_create_file(&megasas_pci_driver.driver,
3396 &driver_attr_poll_mode_io);
3398 goto err_dcf_poll_mode_io;
3402 err_dcf_poll_mode_io:
3403 driver_remove_file(&megasas_pci_driver.driver,
3404 &driver_attr_dbg_lvl);
3406 driver_remove_file(&megasas_pci_driver.driver,
3407 &driver_attr_release_date);
3409 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3411 pci_unregister_driver(&megasas_pci_driver);
3413 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3418 * megasas_exit - Driver unload entry point
3420 static void __exit megasas_exit(void)
3422 driver_remove_file(&megasas_pci_driver.driver,
3423 &driver_attr_poll_mode_io);
3424 driver_remove_file(&megasas_pci_driver.driver,
3425 &driver_attr_dbg_lvl);
3426 driver_remove_file(&megasas_pci_driver.driver,
3427 &driver_attr_release_date);
3428 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3430 pci_unregister_driver(&megasas_pci_driver);
3431 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3434 module_init(megasas_init);
3435 module_exit(megasas_exit);