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.04.01-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_SAS1078GEN2)},
76 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
78 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
79 /* xscale IOP, vega */
80 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
85 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
87 static int megasas_mgmt_majorno;
88 static struct megasas_mgmt_info megasas_mgmt_info;
89 static struct fasync_struct *megasas_async_queue;
90 static DEFINE_MUTEX(megasas_async_queue_mutex);
92 static u32 megasas_dbg_lvl;
95 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
99 * megasas_get_cmd - Get a command from the free pool
100 * @instance: Adapter soft state
102 * Returns a free command from the pool
104 static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
108 struct megasas_cmd *cmd = NULL;
110 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
112 if (!list_empty(&instance->cmd_pool)) {
113 cmd = list_entry((&instance->cmd_pool)->next,
114 struct megasas_cmd, list);
115 list_del_init(&cmd->list);
117 printk(KERN_ERR "megasas: Command pool empty!\n");
120 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
125 * megasas_return_cmd - Return a cmd to free command pool
126 * @instance: Adapter soft state
127 * @cmd: Command packet to be returned to free command pool
130 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
134 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
137 list_add_tail(&cmd->list, &instance->cmd_pool);
139 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
144 * The following functions are defined for xscale
145 * (deviceid : 1064R, PERC5) controllers
149 * megasas_enable_intr_xscale - Enables interrupts
150 * @regs: MFI register set
153 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
155 writel(1, &(regs)->outbound_intr_mask);
157 /* Dummy readl to force pci flush */
158 readl(®s->outbound_intr_mask);
162 * megasas_disable_intr_xscale -Disables interrupt
163 * @regs: MFI register set
166 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
169 writel(mask, ®s->outbound_intr_mask);
170 /* Dummy readl to force pci flush */
171 readl(®s->outbound_intr_mask);
175 * megasas_read_fw_status_reg_xscale - returns the current FW status value
176 * @regs: MFI register set
179 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
181 return readl(&(regs)->outbound_msg_0);
184 * megasas_clear_interrupt_xscale - Check & clear interrupt
185 * @regs: MFI register set
188 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
192 * Check if it is our interrupt
194 status = readl(®s->outbound_intr_status);
196 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
201 * Clear the interrupt by writing back the same value
203 writel(status, ®s->outbound_intr_status);
205 /* Dummy readl to force pci flush */
206 readl(®s->outbound_intr_status);
212 * megasas_fire_cmd_xscale - Sends command to the FW
213 * @frame_phys_addr : Physical address of cmd
214 * @frame_count : Number of frames for the command
215 * @regs : MFI register set
218 megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
220 writel((frame_phys_addr >> 3)|(frame_count),
221 &(regs)->inbound_queue_port);
224 static struct megasas_instance_template megasas_instance_template_xscale = {
226 .fire_cmd = megasas_fire_cmd_xscale,
227 .enable_intr = megasas_enable_intr_xscale,
228 .disable_intr = megasas_disable_intr_xscale,
229 .clear_intr = megasas_clear_intr_xscale,
230 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
234 * This is the end of set of functions & definitions specific
235 * to xscale (deviceid : 1064R, PERC5) controllers
239 * The following functions are defined for ppc (deviceid : 0x60)
244 * megasas_enable_intr_ppc - Enables interrupts
245 * @regs: MFI register set
248 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
250 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
252 writel(~0x80000004, &(regs)->outbound_intr_mask);
254 /* Dummy readl to force pci flush */
255 readl(®s->outbound_intr_mask);
259 * megasas_disable_intr_ppc - Disable interrupt
260 * @regs: MFI register set
263 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
265 u32 mask = 0xFFFFFFFF;
266 writel(mask, ®s->outbound_intr_mask);
267 /* Dummy readl to force pci flush */
268 readl(®s->outbound_intr_mask);
272 * megasas_read_fw_status_reg_ppc - returns the current FW status value
273 * @regs: MFI register set
276 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
278 return readl(&(regs)->outbound_scratch_pad);
282 * megasas_clear_interrupt_ppc - Check & clear interrupt
283 * @regs: MFI register set
286 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
290 * Check if it is our interrupt
292 status = readl(®s->outbound_intr_status);
294 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
299 * Clear the interrupt by writing back the same value
301 writel(status, ®s->outbound_doorbell_clear);
303 /* Dummy readl to force pci flush */
304 readl(®s->outbound_doorbell_clear);
309 * megasas_fire_cmd_ppc - Sends command to the FW
310 * @frame_phys_addr : Physical address of cmd
311 * @frame_count : Number of frames for the command
312 * @regs : MFI register set
315 megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
317 writel((frame_phys_addr | (frame_count<<1))|1,
318 &(regs)->inbound_queue_port);
321 static struct megasas_instance_template megasas_instance_template_ppc = {
323 .fire_cmd = megasas_fire_cmd_ppc,
324 .enable_intr = megasas_enable_intr_ppc,
325 .disable_intr = megasas_disable_intr_ppc,
326 .clear_intr = megasas_clear_intr_ppc,
327 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
331 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
336 * megasas_enable_intr_gen2 - Enables interrupts
337 * @regs: MFI register set
340 megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
342 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
344 /* write ~0x00000005 (4 & 1) to the intr mask*/
345 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
347 /* Dummy readl to force pci flush */
348 readl(®s->outbound_intr_mask);
352 * megasas_disable_intr_gen2 - Disables interrupt
353 * @regs: MFI register set
356 megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
358 u32 mask = 0xFFFFFFFF;
359 writel(mask, ®s->outbound_intr_mask);
360 /* Dummy readl to force pci flush */
361 readl(®s->outbound_intr_mask);
365 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
366 * @regs: MFI register set
369 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
371 return readl(&(regs)->outbound_scratch_pad);
375 * megasas_clear_interrupt_gen2 - Check & clear interrupt
376 * @regs: MFI register set
379 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
383 * Check if it is our interrupt
385 status = readl(®s->outbound_intr_status);
387 if (!(status & MFI_GEN2_ENABLE_INTERRUPT_MASK))
391 * Clear the interrupt by writing back the same value
393 writel(status, ®s->outbound_doorbell_clear);
395 /* Dummy readl to force pci flush */
396 readl(®s->outbound_intr_status);
401 * megasas_fire_cmd_gen2 - Sends command to the FW
402 * @frame_phys_addr : Physical address of cmd
403 * @frame_count : Number of frames for the command
404 * @regs : MFI register set
407 megasas_fire_cmd_gen2(dma_addr_t frame_phys_addr, u32 frame_count,
408 struct megasas_register_set __iomem *regs)
410 writel((frame_phys_addr | (frame_count<<1))|1,
411 &(regs)->inbound_queue_port);
414 static struct megasas_instance_template megasas_instance_template_gen2 = {
416 .fire_cmd = megasas_fire_cmd_gen2,
417 .enable_intr = megasas_enable_intr_gen2,
418 .disable_intr = megasas_disable_intr_gen2,
419 .clear_intr = megasas_clear_intr_gen2,
420 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
424 * This is the end of set of functions & definitions
425 * specific to ppc (deviceid : 0x60) controllers
429 * megasas_issue_polled - Issues a polling command
430 * @instance: Adapter soft state
431 * @cmd: Command packet to be issued
433 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
436 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
439 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
441 struct megasas_header *frame_hdr = &cmd->frame->hdr;
443 frame_hdr->cmd_status = 0xFF;
444 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
447 * Issue the frame using inbound queue port
449 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
452 * Wait for cmd_status to change
454 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
459 if (frame_hdr->cmd_status == 0xff)
466 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
467 * @instance: Adapter soft state
468 * @cmd: Command to be issued
470 * This function waits on an event for the command to be returned from ISR.
471 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
472 * Used to issue ioctl commands.
475 megasas_issue_blocked_cmd(struct megasas_instance *instance,
476 struct megasas_cmd *cmd)
478 cmd->cmd_status = ENODATA;
480 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
482 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
483 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
489 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
490 * @instance: Adapter soft state
491 * @cmd_to_abort: Previously issued cmd to be aborted
493 * MFI firmware can abort previously issued AEN comamnd (automatic event
494 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
495 * cmd and waits for return status.
496 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
499 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
500 struct megasas_cmd *cmd_to_abort)
502 struct megasas_cmd *cmd;
503 struct megasas_abort_frame *abort_fr;
505 cmd = megasas_get_cmd(instance);
510 abort_fr = &cmd->frame->abort;
513 * Prepare and issue the abort frame
515 abort_fr->cmd = MFI_CMD_ABORT;
516 abort_fr->cmd_status = 0xFF;
518 abort_fr->abort_context = cmd_to_abort->index;
519 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
520 abort_fr->abort_mfi_phys_addr_hi = 0;
523 cmd->cmd_status = 0xFF;
525 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
528 * Wait for this cmd to complete
530 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
531 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
533 megasas_return_cmd(instance, cmd);
538 * megasas_make_sgl32 - Prepares 32-bit SGL
539 * @instance: Adapter soft state
540 * @scp: SCSI command from the mid-layer
541 * @mfi_sgl: SGL to be filled in
543 * If successful, this function returns the number of SG elements. Otherwise,
547 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
548 union megasas_sgl *mfi_sgl)
552 struct scatterlist *os_sgl;
554 sge_count = scsi_dma_map(scp);
555 BUG_ON(sge_count < 0);
558 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
559 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
560 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
567 * megasas_make_sgl64 - Prepares 64-bit SGL
568 * @instance: Adapter soft state
569 * @scp: SCSI command from the mid-layer
570 * @mfi_sgl: SGL to be filled in
572 * If successful, this function returns the number of SG elements. Otherwise,
576 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
577 union megasas_sgl *mfi_sgl)
581 struct scatterlist *os_sgl;
583 sge_count = scsi_dma_map(scp);
584 BUG_ON(sge_count < 0);
587 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
588 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
589 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
596 * megasas_get_frame_count - Computes the number of frames
597 * @frame_type : type of frame- io or pthru frame
598 * @sge_count : number of sg elements
600 * Returns the number of frames required for numnber of sge's (sge_count)
603 static u32 megasas_get_frame_count(u8 sge_count, u8 frame_type)
610 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
611 sizeof(struct megasas_sge32);
614 * Main frame can contain 2 SGEs for 64-bit SGLs and
615 * 3 SGEs for 32-bit SGLs for ldio &
616 * 1 SGEs for 64-bit SGLs and
617 * 2 SGEs for 32-bit SGLs for pthru frame
619 if (unlikely(frame_type == PTHRU_FRAME)) {
621 num_cnt = sge_count - 1;
623 num_cnt = sge_count - 2;
626 num_cnt = sge_count - 2;
628 num_cnt = sge_count - 3;
632 sge_bytes = sge_sz * num_cnt;
634 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
635 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
646 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
647 * @instance: Adapter soft state
649 * @cmd: Command to be prepared in
651 * This function prepares CDB commands. These are typcially pass-through
652 * commands to the devices.
655 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
656 struct megasas_cmd *cmd)
661 struct megasas_pthru_frame *pthru;
663 is_logical = MEGASAS_IS_LOGICAL(scp);
664 device_id = MEGASAS_DEV_INDEX(instance, scp);
665 pthru = (struct megasas_pthru_frame *)cmd->frame;
667 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
668 flags = MFI_FRAME_DIR_WRITE;
669 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
670 flags = MFI_FRAME_DIR_READ;
671 else if (scp->sc_data_direction == PCI_DMA_NONE)
672 flags = MFI_FRAME_DIR_NONE;
675 * Prepare the DCDB frame
677 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
678 pthru->cmd_status = 0x0;
679 pthru->scsi_status = 0x0;
680 pthru->target_id = device_id;
681 pthru->lun = scp->device->lun;
682 pthru->cdb_len = scp->cmd_len;
684 pthru->flags = flags;
685 pthru->data_xfer_len = scsi_bufflen(scp);
687 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
693 pthru->flags |= MFI_FRAME_SGL64;
694 pthru->sge_count = megasas_make_sgl64(instance, scp,
697 pthru->sge_count = megasas_make_sgl32(instance, scp,
701 * Sense info specific
703 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
704 pthru->sense_buf_phys_addr_hi = 0;
705 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
708 * Compute the total number of frames this command consumes. FW uses
709 * this number to pull sufficient number of frames from host memory.
711 cmd->frame_count = megasas_get_frame_count(pthru->sge_count,
714 return cmd->frame_count;
718 * megasas_build_ldio - Prepares IOs to logical devices
719 * @instance: Adapter soft state
721 * @cmd: Command to to be prepared
723 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
726 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
727 struct megasas_cmd *cmd)
730 u8 sc = scp->cmnd[0];
732 struct megasas_io_frame *ldio;
734 device_id = MEGASAS_DEV_INDEX(instance, scp);
735 ldio = (struct megasas_io_frame *)cmd->frame;
737 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
738 flags = MFI_FRAME_DIR_WRITE;
739 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
740 flags = MFI_FRAME_DIR_READ;
743 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
745 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
746 ldio->cmd_status = 0x0;
747 ldio->scsi_status = 0x0;
748 ldio->target_id = device_id;
750 ldio->reserved_0 = 0;
753 ldio->start_lba_hi = 0;
754 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
757 * 6-byte READ(0x08) or WRITE(0x0A) cdb
759 if (scp->cmd_len == 6) {
760 ldio->lba_count = (u32) scp->cmnd[4];
761 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
762 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
764 ldio->start_lba_lo &= 0x1FFFFF;
768 * 10-byte READ(0x28) or WRITE(0x2A) cdb
770 else if (scp->cmd_len == 10) {
771 ldio->lba_count = (u32) scp->cmnd[8] |
772 ((u32) scp->cmnd[7] << 8);
773 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
774 ((u32) scp->cmnd[3] << 16) |
775 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
779 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
781 else if (scp->cmd_len == 12) {
782 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
783 ((u32) scp->cmnd[7] << 16) |
784 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
786 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
787 ((u32) scp->cmnd[3] << 16) |
788 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
792 * 16-byte READ(0x88) or WRITE(0x8A) cdb
794 else if (scp->cmd_len == 16) {
795 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
796 ((u32) scp->cmnd[11] << 16) |
797 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
799 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
800 ((u32) scp->cmnd[7] << 16) |
801 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
803 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
804 ((u32) scp->cmnd[3] << 16) |
805 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
813 ldio->flags |= MFI_FRAME_SGL64;
814 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
816 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
819 * Sense info specific
821 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
822 ldio->sense_buf_phys_addr_hi = 0;
823 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
826 * Compute the total number of frames this command consumes. FW uses
827 * this number to pull sufficient number of frames from host memory.
829 cmd->frame_count = megasas_get_frame_count(ldio->sge_count, IO_FRAME);
831 return cmd->frame_count;
835 * megasas_is_ldio - Checks if the cmd is for logical drive
836 * @scmd: SCSI command
838 * Called by megasas_queue_command to find out if the command to be queued
839 * is a logical drive command
841 static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
843 if (!MEGASAS_IS_LOGICAL(cmd))
845 switch (cmd->cmnd[0]) {
861 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
863 * @instance: Adapter soft state
866 megasas_dump_pending_frames(struct megasas_instance *instance)
868 struct megasas_cmd *cmd;
870 union megasas_sgl *mfi_sgl;
871 struct megasas_io_frame *ldio;
872 struct megasas_pthru_frame *pthru;
874 u32 max_cmd = instance->max_fw_cmds;
876 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
877 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
879 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
881 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
883 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
884 for (i = 0; i < max_cmd; i++) {
885 cmd = instance->cmd_list[i];
888 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
889 if (megasas_is_ldio(cmd->scmd)){
890 ldio = (struct megasas_io_frame *)cmd->frame;
891 mfi_sgl = &ldio->sgl;
892 sgcount = ldio->sge_count;
893 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);
896 pthru = (struct megasas_pthru_frame *) cmd->frame;
897 mfi_sgl = &pthru->sgl;
898 sgcount = pthru->sge_count;
899 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);
901 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
902 for (n = 0; n < sgcount; n++){
904 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) ;
906 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
909 printk(KERN_ERR "\n");
911 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
912 for (i = 0; i < max_cmd; i++) {
914 cmd = instance->cmd_list[i];
916 if(cmd->sync_cmd == 1){
917 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
920 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
924 * megasas_queue_command - Queue entry point
925 * @scmd: SCSI command to be queued
926 * @done: Callback entry point
929 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
932 struct megasas_cmd *cmd;
933 struct megasas_instance *instance;
935 instance = (struct megasas_instance *)
936 scmd->device->host->hostdata;
938 /* Don't process if we have already declared adapter dead */
939 if (instance->hw_crit_error)
940 return SCSI_MLQUEUE_HOST_BUSY;
942 scmd->scsi_done = done;
945 if (MEGASAS_IS_LOGICAL(scmd) &&
946 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
947 scmd->result = DID_BAD_TARGET << 16;
951 switch (scmd->cmnd[0]) {
952 case SYNCHRONIZE_CACHE:
954 * FW takes care of flush cache on its own
955 * No need to send it down
957 scmd->result = DID_OK << 16;
963 cmd = megasas_get_cmd(instance);
965 return SCSI_MLQUEUE_HOST_BUSY;
968 * Logical drive command
970 if (megasas_is_ldio(scmd))
971 frame_count = megasas_build_ldio(instance, scmd, cmd);
973 frame_count = megasas_build_dcdb(instance, scmd, cmd);
979 scmd->SCp.ptr = (char *)cmd;
982 * Issue the command to the FW
984 atomic_inc(&instance->fw_outstanding);
986 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
988 * Check if we have pend cmds to be completed
990 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
991 tasklet_schedule(&instance->isr_tasklet);
997 megasas_return_cmd(instance, cmd);
1003 static int megasas_slave_configure(struct scsi_device *sdev)
1006 * Don't export physical disk devices to the disk driver.
1008 * FIXME: Currently we don't export them to the midlayer at all.
1009 * That will be fixed once LSI engineers have audited the
1010 * firmware for possible issues.
1012 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
1016 * The RAID firmware may require extended timeouts.
1018 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
1019 sdev->timeout = MEGASAS_DEFAULT_CMD_TIMEOUT * HZ;
1024 * megasas_complete_cmd_dpc - Returns FW's controller structure
1025 * @instance_addr: Address of adapter soft state
1027 * Tasklet to complete cmds
1029 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1034 struct megasas_cmd *cmd;
1035 struct megasas_instance *instance =
1036 (struct megasas_instance *)instance_addr;
1037 unsigned long flags;
1039 /* If we have already declared adapter dead, donot complete cmds */
1040 if (instance->hw_crit_error)
1043 spin_lock_irqsave(&instance->completion_lock, flags);
1045 producer = *instance->producer;
1046 consumer = *instance->consumer;
1048 while (consumer != producer) {
1049 context = instance->reply_queue[consumer];
1051 cmd = instance->cmd_list[context];
1053 megasas_complete_cmd(instance, cmd, DID_OK);
1056 if (consumer == (instance->max_fw_cmds + 1)) {
1061 *instance->consumer = producer;
1063 spin_unlock_irqrestore(&instance->completion_lock, flags);
1066 * Check if we can restore can_queue
1068 if (instance->flag & MEGASAS_FW_BUSY
1069 && time_after(jiffies, instance->last_time + 5 * HZ)
1070 && atomic_read(&instance->fw_outstanding) < 17) {
1072 spin_lock_irqsave(instance->host->host_lock, flags);
1073 instance->flag &= ~MEGASAS_FW_BUSY;
1074 instance->host->can_queue =
1075 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1077 spin_unlock_irqrestore(instance->host->host_lock, flags);
1082 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1083 * @instance: Adapter soft state
1085 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1086 * complete all its outstanding commands. Returns error if one or more IOs
1087 * are pending after this time period. It also marks the controller dead.
1089 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1092 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1094 for (i = 0; i < wait_time; i++) {
1096 int outstanding = atomic_read(&instance->fw_outstanding);
1101 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1102 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1103 "commands to complete\n",i,outstanding);
1105 * Call cmd completion routine. Cmd to be
1106 * be completed directly without depending on isr.
1108 megasas_complete_cmd_dpc((unsigned long)instance);
1114 if (atomic_read(&instance->fw_outstanding)) {
1116 * Send signal to FW to stop processing any pending cmds.
1117 * The controller will be taken offline by the OS now.
1119 writel(MFI_STOP_ADP,
1120 &instance->reg_set->inbound_doorbell);
1121 megasas_dump_pending_frames(instance);
1122 instance->hw_crit_error = 1;
1130 * megasas_generic_reset - Generic reset routine
1131 * @scmd: Mid-layer SCSI command
1133 * This routine implements a generic reset handler for device, bus and host
1134 * reset requests. Device, bus and host specific reset handlers can use this
1135 * function after they do their specific tasks.
1137 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1140 struct megasas_instance *instance;
1142 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1144 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1145 scmd->serial_number, scmd->cmnd[0], scmd->retries);
1147 if (instance->hw_crit_error) {
1148 printk(KERN_ERR "megasas: cannot recover from previous reset "
1153 ret_val = megasas_wait_for_outstanding(instance);
1154 if (ret_val == SUCCESS)
1155 printk(KERN_NOTICE "megasas: reset successful \n");
1157 printk(KERN_ERR "megasas: failed to do reset\n");
1163 * megasas_reset_timer - quiesce the adapter if required
1166 * Sets the FW busy flag and reduces the host->can_queue if the
1167 * cmd has not been completed within the timeout period.
1170 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1172 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1173 struct megasas_instance *instance;
1174 unsigned long flags;
1176 if (time_after(jiffies, scmd->jiffies_at_alloc +
1177 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1178 return BLK_EH_NOT_HANDLED;
1181 instance = cmd->instance;
1182 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1183 /* FW is busy, throttle IO */
1184 spin_lock_irqsave(instance->host->host_lock, flags);
1186 instance->host->can_queue = 16;
1187 instance->last_time = jiffies;
1188 instance->flag |= MEGASAS_FW_BUSY;
1190 spin_unlock_irqrestore(instance->host->host_lock, flags);
1192 return BLK_EH_RESET_TIMER;
1196 * megasas_reset_device - Device reset handler entry point
1198 static int megasas_reset_device(struct scsi_cmnd *scmd)
1203 * First wait for all commands to complete
1205 ret = megasas_generic_reset(scmd);
1211 * megasas_reset_bus_host - Bus & host reset handler entry point
1213 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1218 * First wait for all commands to complete
1220 ret = megasas_generic_reset(scmd);
1226 * megasas_bios_param - Returns disk geometry for a disk
1227 * @sdev: device handle
1228 * @bdev: block device
1229 * @capacity: drive capacity
1230 * @geom: geometry parameters
1233 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1234 sector_t capacity, int geom[])
1240 /* Default heads (64) & sectors (32) */
1244 tmp = heads * sectors;
1245 cylinders = capacity;
1247 sector_div(cylinders, tmp);
1250 * Handle extended translation size for logical drives > 1Gb
1253 if (capacity >= 0x200000) {
1256 tmp = heads*sectors;
1257 cylinders = capacity;
1258 sector_div(cylinders, tmp);
1263 geom[2] = cylinders;
1269 * megasas_service_aen - Processes an event notification
1270 * @instance: Adapter soft state
1271 * @cmd: AEN command completed by the ISR
1273 * For AEN, driver sends a command down to FW that is held by the FW till an
1274 * event occurs. When an event of interest occurs, FW completes the command
1275 * that it was previously holding.
1277 * This routines sends SIGIO signal to processes that have registered with the
1281 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1284 * Don't signal app if it is just an aborted previously registered aen
1286 if (!cmd->abort_aen)
1287 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1291 instance->aen_cmd = NULL;
1292 megasas_return_cmd(instance, cmd);
1296 * Scsi host template for megaraid_sas driver
1298 static struct scsi_host_template megasas_template = {
1300 .module = THIS_MODULE,
1301 .name = "LSI SAS based MegaRAID driver",
1302 .proc_name = "megaraid_sas",
1303 .slave_configure = megasas_slave_configure,
1304 .queuecommand = megasas_queue_command,
1305 .eh_device_reset_handler = megasas_reset_device,
1306 .eh_bus_reset_handler = megasas_reset_bus_host,
1307 .eh_host_reset_handler = megasas_reset_bus_host,
1308 .eh_timed_out = megasas_reset_timer,
1309 .bios_param = megasas_bios_param,
1310 .use_clustering = ENABLE_CLUSTERING,
1314 * megasas_complete_int_cmd - Completes an internal command
1315 * @instance: Adapter soft state
1316 * @cmd: Command to be completed
1318 * The megasas_issue_blocked_cmd() function waits for a command to complete
1319 * after it issues a command. This function wakes up that waiting routine by
1320 * calling wake_up() on the wait queue.
1323 megasas_complete_int_cmd(struct megasas_instance *instance,
1324 struct megasas_cmd *cmd)
1326 cmd->cmd_status = cmd->frame->io.cmd_status;
1328 if (cmd->cmd_status == ENODATA) {
1329 cmd->cmd_status = 0;
1331 wake_up(&instance->int_cmd_wait_q);
1335 * megasas_complete_abort - Completes aborting a command
1336 * @instance: Adapter soft state
1337 * @cmd: Cmd that was issued to abort another cmd
1339 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1340 * after it issues an abort on a previously issued command. This function
1341 * wakes up all functions waiting on the same wait queue.
1344 megasas_complete_abort(struct megasas_instance *instance,
1345 struct megasas_cmd *cmd)
1347 if (cmd->sync_cmd) {
1349 cmd->cmd_status = 0;
1350 wake_up(&instance->abort_cmd_wait_q);
1357 * megasas_complete_cmd - Completes a command
1358 * @instance: Adapter soft state
1359 * @cmd: Command to be completed
1360 * @alt_status: If non-zero, use this value as status to
1361 * SCSI mid-layer instead of the value returned
1362 * by the FW. This should be used if caller wants
1363 * an alternate status (as in the case of aborted
1367 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1371 struct megasas_header *hdr = &cmd->frame->hdr;
1374 cmd->scmd->SCp.ptr = NULL;
1378 case MFI_CMD_PD_SCSI_IO:
1379 case MFI_CMD_LD_SCSI_IO:
1382 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1383 * issued either through an IO path or an IOCTL path. If it
1384 * was via IOCTL, we will send it to internal completion.
1386 if (cmd->sync_cmd) {
1388 megasas_complete_int_cmd(instance, cmd);
1392 case MFI_CMD_LD_READ:
1393 case MFI_CMD_LD_WRITE:
1396 cmd->scmd->result = alt_status << 16;
1402 atomic_dec(&instance->fw_outstanding);
1404 scsi_dma_unmap(cmd->scmd);
1405 cmd->scmd->scsi_done(cmd->scmd);
1406 megasas_return_cmd(instance, cmd);
1411 switch (hdr->cmd_status) {
1414 cmd->scmd->result = DID_OK << 16;
1417 case MFI_STAT_SCSI_IO_FAILED:
1418 case MFI_STAT_LD_INIT_IN_PROGRESS:
1420 (DID_ERROR << 16) | hdr->scsi_status;
1423 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1425 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1427 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1428 memset(cmd->scmd->sense_buffer, 0,
1429 SCSI_SENSE_BUFFERSIZE);
1430 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1433 cmd->scmd->result |= DRIVER_SENSE << 24;
1438 case MFI_STAT_LD_OFFLINE:
1439 case MFI_STAT_DEVICE_NOT_FOUND:
1440 cmd->scmd->result = DID_BAD_TARGET << 16;
1444 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1446 cmd->scmd->result = DID_ERROR << 16;
1450 atomic_dec(&instance->fw_outstanding);
1452 scsi_dma_unmap(cmd->scmd);
1453 cmd->scmd->scsi_done(cmd->scmd);
1454 megasas_return_cmd(instance, cmd);
1463 * See if got an event notification
1465 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1466 megasas_service_aen(instance, cmd);
1468 megasas_complete_int_cmd(instance, cmd);
1474 * Cmd issued to abort another cmd returned
1476 megasas_complete_abort(instance, cmd);
1480 printk("megasas: Unknown command completed! [0x%X]\n",
1487 * megasas_deplete_reply_queue - Processes all completed commands
1488 * @instance: Adapter soft state
1489 * @alt_status: Alternate status to be returned to
1490 * SCSI mid-layer instead of the status
1491 * returned by the FW
1494 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1497 * Check if it is our interrupt
1498 * Clear the interrupt
1500 if(instance->instancet->clear_intr(instance->reg_set))
1503 if (instance->hw_crit_error)
1506 * Schedule the tasklet for cmd completion
1508 tasklet_schedule(&instance->isr_tasklet);
1514 * megasas_isr - isr entry point
1516 static irqreturn_t megasas_isr(int irq, void *devp)
1518 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1523 * megasas_transition_to_ready - Move the FW to READY state
1524 * @instance: Adapter soft state
1526 * During the initialization, FW passes can potentially be in any one of
1527 * several possible states. If the FW in operational, waiting-for-handshake
1528 * states, driver must take steps to bring it to ready state. Otherwise, it
1529 * has to wait for the ready state.
1532 megasas_transition_to_ready(struct megasas_instance* instance)
1539 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
1541 if (fw_state != MFI_STATE_READY)
1542 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1545 while (fw_state != MFI_STATE_READY) {
1549 case MFI_STATE_FAULT:
1551 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1554 case MFI_STATE_WAIT_HANDSHAKE:
1556 * Set the CLR bit in inbound doorbell
1558 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1559 &instance->reg_set->inbound_doorbell);
1562 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1565 case MFI_STATE_BOOT_MESSAGE_PENDING:
1566 writel(MFI_INIT_HOTPLUG,
1567 &instance->reg_set->inbound_doorbell);
1570 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1573 case MFI_STATE_OPERATIONAL:
1575 * Bring it to READY state; assuming max wait 10 secs
1577 instance->instancet->disable_intr(instance->reg_set);
1578 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
1581 cur_state = MFI_STATE_OPERATIONAL;
1584 case MFI_STATE_UNDEFINED:
1586 * This state should not last for more than 2 seconds
1589 cur_state = MFI_STATE_UNDEFINED;
1592 case MFI_STATE_BB_INIT:
1594 cur_state = MFI_STATE_BB_INIT;
1597 case MFI_STATE_FW_INIT:
1599 cur_state = MFI_STATE_FW_INIT;
1602 case MFI_STATE_FW_INIT_2:
1604 cur_state = MFI_STATE_FW_INIT_2;
1607 case MFI_STATE_DEVICE_SCAN:
1609 cur_state = MFI_STATE_DEVICE_SCAN;
1612 case MFI_STATE_FLUSH_CACHE:
1614 cur_state = MFI_STATE_FLUSH_CACHE;
1618 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1624 * The cur_state should not last for more than max_wait secs
1626 for (i = 0; i < (max_wait * 1000); i++) {
1627 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1630 if (fw_state == cur_state) {
1637 * Return error if fw_state hasn't changed after max_wait
1639 if (fw_state == cur_state) {
1640 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1641 "in %d secs\n", fw_state, max_wait);
1645 printk(KERN_INFO "megasas: FW now in Ready state\n");
1651 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1652 * @instance: Adapter soft state
1654 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1657 u32 max_cmd = instance->max_fw_cmds;
1658 struct megasas_cmd *cmd;
1660 if (!instance->frame_dma_pool)
1664 * Return all frames to pool
1666 for (i = 0; i < max_cmd; i++) {
1668 cmd = instance->cmd_list[i];
1671 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1672 cmd->frame_phys_addr);
1675 pci_pool_free(instance->sense_dma_pool, cmd->sense,
1676 cmd->sense_phys_addr);
1680 * Now destroy the pool itself
1682 pci_pool_destroy(instance->frame_dma_pool);
1683 pci_pool_destroy(instance->sense_dma_pool);
1685 instance->frame_dma_pool = NULL;
1686 instance->sense_dma_pool = NULL;
1690 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1691 * @instance: Adapter soft state
1693 * Each command packet has an embedded DMA memory buffer that is used for
1694 * filling MFI frame and the SG list that immediately follows the frame. This
1695 * function creates those DMA memory buffers for each command packet by using
1696 * PCI pool facility.
1698 static int megasas_create_frame_pool(struct megasas_instance *instance)
1706 struct megasas_cmd *cmd;
1708 max_cmd = instance->max_fw_cmds;
1711 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1712 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1714 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1715 sizeof(struct megasas_sge32);
1718 * Calculated the number of 64byte frames required for SGL
1720 sgl_sz = sge_sz * instance->max_num_sge;
1721 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1724 * We need one extra frame for the MFI command
1728 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1730 * Use DMA pool facility provided by PCI layer
1732 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1733 instance->pdev, total_sz, 64,
1736 if (!instance->frame_dma_pool) {
1737 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1741 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1742 instance->pdev, 128, 4, 0);
1744 if (!instance->sense_dma_pool) {
1745 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1747 pci_pool_destroy(instance->frame_dma_pool);
1748 instance->frame_dma_pool = NULL;
1754 * Allocate and attach a frame to each of the commands in cmd_list.
1755 * By making cmd->index as the context instead of the &cmd, we can
1756 * always use 32bit context regardless of the architecture
1758 for (i = 0; i < max_cmd; i++) {
1760 cmd = instance->cmd_list[i];
1762 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1763 GFP_KERNEL, &cmd->frame_phys_addr);
1765 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1766 GFP_KERNEL, &cmd->sense_phys_addr);
1769 * megasas_teardown_frame_pool() takes care of freeing
1770 * whatever has been allocated
1772 if (!cmd->frame || !cmd->sense) {
1773 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1774 megasas_teardown_frame_pool(instance);
1778 cmd->frame->io.context = cmd->index;
1785 * megasas_free_cmds - Free all the cmds in the free cmd pool
1786 * @instance: Adapter soft state
1788 static void megasas_free_cmds(struct megasas_instance *instance)
1791 /* First free the MFI frame pool */
1792 megasas_teardown_frame_pool(instance);
1794 /* Free all the commands in the cmd_list */
1795 for (i = 0; i < instance->max_fw_cmds; i++)
1796 kfree(instance->cmd_list[i]);
1798 /* Free the cmd_list buffer itself */
1799 kfree(instance->cmd_list);
1800 instance->cmd_list = NULL;
1802 INIT_LIST_HEAD(&instance->cmd_pool);
1806 * megasas_alloc_cmds - Allocates the command packets
1807 * @instance: Adapter soft state
1809 * Each command that is issued to the FW, whether IO commands from the OS or
1810 * internal commands like IOCTLs, are wrapped in local data structure called
1811 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1814 * Each frame has a 32-bit field called context (tag). This context is used
1815 * to get back the megasas_cmd from the frame when a frame gets completed in
1816 * the ISR. Typically the address of the megasas_cmd itself would be used as
1817 * the context. But we wanted to keep the differences between 32 and 64 bit
1818 * systems to the mininum. We always use 32 bit integers for the context. In
1819 * this driver, the 32 bit values are the indices into an array cmd_list.
1820 * This array is used only to look up the megasas_cmd given the context. The
1821 * free commands themselves are maintained in a linked list called cmd_pool.
1823 static int megasas_alloc_cmds(struct megasas_instance *instance)
1828 struct megasas_cmd *cmd;
1830 max_cmd = instance->max_fw_cmds;
1833 * instance->cmd_list is an array of struct megasas_cmd pointers.
1834 * Allocate the dynamic array first and then allocate individual
1837 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
1839 if (!instance->cmd_list) {
1840 printk(KERN_DEBUG "megasas: out of memory\n");
1845 for (i = 0; i < max_cmd; i++) {
1846 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1849 if (!instance->cmd_list[i]) {
1851 for (j = 0; j < i; j++)
1852 kfree(instance->cmd_list[j]);
1854 kfree(instance->cmd_list);
1855 instance->cmd_list = NULL;
1862 * Add all the commands to command pool (instance->cmd_pool)
1864 for (i = 0; i < max_cmd; i++) {
1865 cmd = instance->cmd_list[i];
1866 memset(cmd, 0, sizeof(struct megasas_cmd));
1868 cmd->instance = instance;
1870 list_add_tail(&cmd->list, &instance->cmd_pool);
1874 * Create a frame pool and assign one frame to each cmd
1876 if (megasas_create_frame_pool(instance)) {
1877 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1878 megasas_free_cmds(instance);
1885 * megasas_get_controller_info - Returns FW's controller structure
1886 * @instance: Adapter soft state
1887 * @ctrl_info: Controller information structure
1889 * Issues an internal command (DCMD) to get the FW's controller structure.
1890 * This information is mainly used to find out the maximum IO transfer per
1891 * command supported by the FW.
1894 megasas_get_ctrl_info(struct megasas_instance *instance,
1895 struct megasas_ctrl_info *ctrl_info)
1898 struct megasas_cmd *cmd;
1899 struct megasas_dcmd_frame *dcmd;
1900 struct megasas_ctrl_info *ci;
1901 dma_addr_t ci_h = 0;
1903 cmd = megasas_get_cmd(instance);
1906 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1910 dcmd = &cmd->frame->dcmd;
1912 ci = pci_alloc_consistent(instance->pdev,
1913 sizeof(struct megasas_ctrl_info), &ci_h);
1916 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1917 megasas_return_cmd(instance, cmd);
1921 memset(ci, 0, sizeof(*ci));
1922 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1924 dcmd->cmd = MFI_CMD_DCMD;
1925 dcmd->cmd_status = 0xFF;
1926 dcmd->sge_count = 1;
1927 dcmd->flags = MFI_FRAME_DIR_READ;
1929 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1930 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1931 dcmd->sgl.sge32[0].phys_addr = ci_h;
1932 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1934 if (!megasas_issue_polled(instance, cmd)) {
1936 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1941 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1944 megasas_return_cmd(instance, cmd);
1949 * megasas_issue_init_mfi - Initializes the FW
1950 * @instance: Adapter soft state
1952 * Issues the INIT MFI cmd
1955 megasas_issue_init_mfi(struct megasas_instance *instance)
1959 struct megasas_cmd *cmd;
1961 struct megasas_init_frame *init_frame;
1962 struct megasas_init_queue_info *initq_info;
1963 dma_addr_t init_frame_h;
1964 dma_addr_t initq_info_h;
1967 * Prepare a init frame. Note the init frame points to queue info
1968 * structure. Each frame has SGL allocated after first 64 bytes. For
1969 * this frame - since we don't need any SGL - we use SGL's space as
1970 * queue info structure
1972 * We will not get a NULL command below. We just created the pool.
1974 cmd = megasas_get_cmd(instance);
1976 init_frame = (struct megasas_init_frame *)cmd->frame;
1977 initq_info = (struct megasas_init_queue_info *)
1978 ((unsigned long)init_frame + 64);
1980 init_frame_h = cmd->frame_phys_addr;
1981 initq_info_h = init_frame_h + 64;
1983 context = init_frame->context;
1984 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1985 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1986 init_frame->context = context;
1988 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1989 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1991 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1992 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1994 init_frame->cmd = MFI_CMD_INIT;
1995 init_frame->cmd_status = 0xFF;
1996 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1998 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
2001 * disable the intr before firing the init frame to FW
2003 instance->instancet->disable_intr(instance->reg_set);
2006 * Issue the init frame in polled mode
2009 if (megasas_issue_polled(instance, cmd)) {
2010 printk(KERN_ERR "megasas: Failed to init firmware\n");
2011 megasas_return_cmd(instance, cmd);
2015 megasas_return_cmd(instance, cmd);
2024 * megasas_start_timer - Initializes a timer object
2025 * @instance: Adapter soft state
2026 * @timer: timer object to be initialized
2027 * @fn: timer function
2028 * @interval: time interval between timer function call
2031 megasas_start_timer(struct megasas_instance *instance,
2032 struct timer_list *timer,
2033 void *fn, unsigned long interval)
2036 timer->expires = jiffies + interval;
2037 timer->data = (unsigned long)instance;
2038 timer->function = fn;
2043 * megasas_io_completion_timer - Timer fn
2044 * @instance_addr: Address of adapter soft state
2046 * Schedules tasklet for cmd completion
2047 * if poll_mode_io is set
2050 megasas_io_completion_timer(unsigned long instance_addr)
2052 struct megasas_instance *instance =
2053 (struct megasas_instance *)instance_addr;
2055 if (atomic_read(&instance->fw_outstanding))
2056 tasklet_schedule(&instance->isr_tasklet);
2060 mod_timer(&instance->io_completion_timer,
2061 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
2065 * megasas_init_mfi - Initializes the FW
2066 * @instance: Adapter soft state
2068 * This is the main function for initializing MFI firmware.
2070 static int megasas_init_mfi(struct megasas_instance *instance)
2077 struct megasas_register_set __iomem *reg_set;
2078 struct megasas_ctrl_info *ctrl_info;
2080 * Map the message registers
2082 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
2083 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
2084 instance->base_addr = pci_resource_start(instance->pdev, 1);
2086 instance->base_addr = pci_resource_start(instance->pdev, 0);
2089 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
2090 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
2094 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
2096 if (!instance->reg_set) {
2097 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
2101 reg_set = instance->reg_set;
2103 switch(instance->pdev->device)
2105 case PCI_DEVICE_ID_LSI_SAS1078R:
2106 case PCI_DEVICE_ID_LSI_SAS1078DE:
2107 instance->instancet = &megasas_instance_template_ppc;
2109 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
2110 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
2111 instance->instancet = &megasas_instance_template_gen2;
2113 case PCI_DEVICE_ID_LSI_SAS1064R:
2114 case PCI_DEVICE_ID_DELL_PERC5:
2116 instance->instancet = &megasas_instance_template_xscale;
2121 * We expect the FW state to be READY
2123 if (megasas_transition_to_ready(instance))
2124 goto fail_ready_state;
2127 * Get various operational parameters from status register
2129 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
2131 * Reduce the max supported cmds by 1. This is to ensure that the
2132 * reply_q_sz (1 more than the max cmd that driver may send)
2133 * does not exceed max cmds that the FW can support
2135 instance->max_fw_cmds = instance->max_fw_cmds-1;
2136 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2139 * Create a pool of commands
2141 if (megasas_alloc_cmds(instance))
2142 goto fail_alloc_cmds;
2145 * Allocate memory for reply queue. Length of reply queue should
2146 * be _one_ more than the maximum commands handled by the firmware.
2148 * Note: When FW completes commands, it places corresponding contex
2149 * values in this circular reply queue. This circular queue is a fairly
2150 * typical producer-consumer queue. FW is the producer (of completed
2151 * commands) and the driver is the consumer.
2153 context_sz = sizeof(u32);
2154 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2156 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2158 &instance->reply_queue_h);
2160 if (!instance->reply_queue) {
2161 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2162 goto fail_reply_queue;
2165 if (megasas_issue_init_mfi(instance))
2168 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2171 * Compute the max allowed sectors per IO: The controller info has two
2172 * limits on max sectors. Driver should use the minimum of these two.
2174 * 1 << stripe_sz_ops.min = max sectors per strip
2176 * Note that older firmwares ( < FW ver 30) didn't report information
2177 * to calculate max_sectors_1. So the number ended up as zero always.
2180 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2182 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2183 ctrl_info->max_strips_per_io;
2184 max_sectors_2 = ctrl_info->max_request_size;
2186 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2189 instance->max_sectors_per_req = instance->max_num_sge *
2191 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2192 instance->max_sectors_per_req = tmp_sectors;
2197 * Setup tasklet for cmd completion
2200 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2201 (unsigned long)instance);
2203 /* Initialize the cmd completion timer */
2205 megasas_start_timer(instance, &instance->io_completion_timer,
2206 megasas_io_completion_timer,
2207 MEGASAS_COMPLETION_TIMER_INTERVAL);
2212 pci_free_consistent(instance->pdev, reply_q_sz,
2213 instance->reply_queue, instance->reply_queue_h);
2215 megasas_free_cmds(instance);
2219 iounmap(instance->reg_set);
2222 pci_release_regions(instance->pdev);
2228 * megasas_release_mfi - Reverses the FW initialization
2229 * @intance: Adapter soft state
2231 static void megasas_release_mfi(struct megasas_instance *instance)
2233 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2235 pci_free_consistent(instance->pdev, reply_q_sz,
2236 instance->reply_queue, instance->reply_queue_h);
2238 megasas_free_cmds(instance);
2240 iounmap(instance->reg_set);
2242 pci_release_regions(instance->pdev);
2246 * megasas_get_seq_num - Gets latest event sequence numbers
2247 * @instance: Adapter soft state
2248 * @eli: FW event log sequence numbers information
2250 * FW maintains a log of all events in a non-volatile area. Upper layers would
2251 * usually find out the latest sequence number of the events, the seq number at
2252 * the boot etc. They would "read" all the events below the latest seq number
2253 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2254 * number), they would subsribe to AEN (asynchronous event notification) and
2255 * wait for the events to happen.
2258 megasas_get_seq_num(struct megasas_instance *instance,
2259 struct megasas_evt_log_info *eli)
2261 struct megasas_cmd *cmd;
2262 struct megasas_dcmd_frame *dcmd;
2263 struct megasas_evt_log_info *el_info;
2264 dma_addr_t el_info_h = 0;
2266 cmd = megasas_get_cmd(instance);
2272 dcmd = &cmd->frame->dcmd;
2273 el_info = pci_alloc_consistent(instance->pdev,
2274 sizeof(struct megasas_evt_log_info),
2278 megasas_return_cmd(instance, cmd);
2282 memset(el_info, 0, sizeof(*el_info));
2283 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2285 dcmd->cmd = MFI_CMD_DCMD;
2286 dcmd->cmd_status = 0x0;
2287 dcmd->sge_count = 1;
2288 dcmd->flags = MFI_FRAME_DIR_READ;
2290 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2291 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2292 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2293 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2295 megasas_issue_blocked_cmd(instance, cmd);
2298 * Copy the data back into callers buffer
2300 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2302 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2303 el_info, el_info_h);
2305 megasas_return_cmd(instance, cmd);
2311 * megasas_register_aen - Registers for asynchronous event notification
2312 * @instance: Adapter soft state
2313 * @seq_num: The starting sequence number
2314 * @class_locale: Class of the event
2316 * This function subscribes for AEN for events beyond the @seq_num. It requests
2317 * to be notified if and only if the event is of type @class_locale
2320 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2321 u32 class_locale_word)
2324 struct megasas_cmd *cmd;
2325 struct megasas_dcmd_frame *dcmd;
2326 union megasas_evt_class_locale curr_aen;
2327 union megasas_evt_class_locale prev_aen;
2330 * If there an AEN pending already (aen_cmd), check if the
2331 * class_locale of that pending AEN is inclusive of the new
2332 * AEN request we currently have. If it is, then we don't have
2333 * to do anything. In other words, whichever events the current
2334 * AEN request is subscribing to, have already been subscribed
2337 * If the old_cmd is _not_ inclusive, then we have to abort
2338 * that command, form a class_locale that is superset of both
2339 * old and current and re-issue to the FW
2342 curr_aen.word = class_locale_word;
2344 if (instance->aen_cmd) {
2346 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2349 * A class whose enum value is smaller is inclusive of all
2350 * higher values. If a PROGRESS (= -1) was previously
2351 * registered, then a new registration requests for higher
2352 * classes need not be sent to FW. They are automatically
2355 * Locale numbers don't have such hierarchy. They are bitmap
2358 if ((prev_aen.members.class <= curr_aen.members.class) &&
2359 !((prev_aen.members.locale & curr_aen.members.locale) ^
2360 curr_aen.members.locale)) {
2362 * Previously issued event registration includes
2363 * current request. Nothing to do.
2367 curr_aen.members.locale |= prev_aen.members.locale;
2369 if (prev_aen.members.class < curr_aen.members.class)
2370 curr_aen.members.class = prev_aen.members.class;
2372 instance->aen_cmd->abort_aen = 1;
2373 ret_val = megasas_issue_blocked_abort_cmd(instance,
2378 printk(KERN_DEBUG "megasas: Failed to abort "
2379 "previous AEN command\n");
2385 cmd = megasas_get_cmd(instance);
2390 dcmd = &cmd->frame->dcmd;
2392 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2395 * Prepare DCMD for aen registration
2397 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2399 dcmd->cmd = MFI_CMD_DCMD;
2400 dcmd->cmd_status = 0x0;
2401 dcmd->sge_count = 1;
2402 dcmd->flags = MFI_FRAME_DIR_READ;
2404 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2405 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2406 dcmd->mbox.w[0] = seq_num;
2407 dcmd->mbox.w[1] = curr_aen.word;
2408 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2409 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2412 * Store reference to the cmd used to register for AEN. When an
2413 * application wants us to register for AEN, we have to abort this
2414 * cmd and re-register with a new EVENT LOCALE supplied by that app
2416 instance->aen_cmd = cmd;
2419 * Issue the aen registration frame
2421 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
2427 * megasas_start_aen - Subscribes to AEN during driver load time
2428 * @instance: Adapter soft state
2430 static int megasas_start_aen(struct megasas_instance *instance)
2432 struct megasas_evt_log_info eli;
2433 union megasas_evt_class_locale class_locale;
2436 * Get the latest sequence number from FW
2438 memset(&eli, 0, sizeof(eli));
2440 if (megasas_get_seq_num(instance, &eli))
2444 * Register AEN with FW for latest sequence number plus 1
2446 class_locale.members.reserved = 0;
2447 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2448 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2450 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2455 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2456 * @instance: Adapter soft state
2458 static int megasas_io_attach(struct megasas_instance *instance)
2460 struct Scsi_Host *host = instance->host;
2463 * Export parameters required by SCSI mid-layer
2465 host->irq = instance->pdev->irq;
2466 host->unique_id = instance->unique_id;
2467 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2468 host->this_id = instance->init_id;
2469 host->sg_tablesize = instance->max_num_sge;
2470 host->max_sectors = instance->max_sectors_per_req;
2471 host->cmd_per_lun = 128;
2472 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2473 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2474 host->max_lun = MEGASAS_MAX_LUN;
2475 host->max_cmd_len = 16;
2478 * Notify the mid-layer about the new controller
2480 if (scsi_add_host(host, &instance->pdev->dev)) {
2481 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2486 * Trigger SCSI to scan our drives
2488 scsi_scan_host(host);
2493 megasas_set_dma_mask(struct pci_dev *pdev)
2496 * All our contollers are capable of performing 64-bit DMA
2499 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2501 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2502 goto fail_set_dma_mask;
2505 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2506 goto fail_set_dma_mask;
2515 * megasas_probe_one - PCI hotplug entry point
2516 * @pdev: PCI device structure
2517 * @id: PCI ids of supported hotplugged adapter
2519 static int __devinit
2520 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2523 struct Scsi_Host *host;
2524 struct megasas_instance *instance;
2527 * Announce PCI information
2529 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2530 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2531 pdev->subsystem_device);
2533 printk("bus %d:slot %d:func %d\n",
2534 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2537 * PCI prepping: enable device set bus mastering and dma mask
2539 rval = pci_enable_device(pdev);
2545 pci_set_master(pdev);
2547 if (megasas_set_dma_mask(pdev))
2548 goto fail_set_dma_mask;
2550 host = scsi_host_alloc(&megasas_template,
2551 sizeof(struct megasas_instance));
2554 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2555 goto fail_alloc_instance;
2558 instance = (struct megasas_instance *)host->hostdata;
2559 memset(instance, 0, sizeof(*instance));
2561 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2562 &instance->producer_h);
2563 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2564 &instance->consumer_h);
2566 if (!instance->producer || !instance->consumer) {
2567 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2568 "producer, consumer\n");
2569 goto fail_alloc_dma_buf;
2572 *instance->producer = 0;
2573 *instance->consumer = 0;
2575 instance->evt_detail = pci_alloc_consistent(pdev,
2577 megasas_evt_detail),
2578 &instance->evt_detail_h);
2580 if (!instance->evt_detail) {
2581 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2582 "event detail structure\n");
2583 goto fail_alloc_dma_buf;
2587 * Initialize locks and queues
2589 INIT_LIST_HEAD(&instance->cmd_pool);
2591 atomic_set(&instance->fw_outstanding,0);
2593 init_waitqueue_head(&instance->int_cmd_wait_q);
2594 init_waitqueue_head(&instance->abort_cmd_wait_q);
2596 spin_lock_init(&instance->cmd_pool_lock);
2597 spin_lock_init(&instance->completion_lock);
2599 mutex_init(&instance->aen_mutex);
2600 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2603 * Initialize PCI related and misc parameters
2605 instance->pdev = pdev;
2606 instance->host = host;
2607 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2608 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2610 megasas_dbg_lvl = 0;
2612 instance->last_time = 0;
2615 * Initialize MFI Firmware
2617 if (megasas_init_mfi(instance))
2623 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
2624 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2628 instance->instancet->enable_intr(instance->reg_set);
2631 * Store instance in PCI softstate
2633 pci_set_drvdata(pdev, instance);
2636 * Add this controller to megasas_mgmt_info structure so that it
2637 * can be exported to management applications
2639 megasas_mgmt_info.count++;
2640 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2641 megasas_mgmt_info.max_index++;
2644 * Initiate AEN (Asynchronous Event Notification)
2646 if (megasas_start_aen(instance)) {
2647 printk(KERN_DEBUG "megasas: start aen failed\n");
2648 goto fail_start_aen;
2652 * Register with SCSI mid-layer
2654 if (megasas_io_attach(instance))
2655 goto fail_io_attach;
2661 megasas_mgmt_info.count--;
2662 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2663 megasas_mgmt_info.max_index--;
2665 pci_set_drvdata(pdev, NULL);
2666 instance->instancet->disable_intr(instance->reg_set);
2667 free_irq(instance->pdev->irq, instance);
2669 megasas_release_mfi(instance);
2674 if (instance->evt_detail)
2675 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2676 instance->evt_detail,
2677 instance->evt_detail_h);
2679 if (instance->producer)
2680 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2681 instance->producer_h);
2682 if (instance->consumer)
2683 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2684 instance->consumer_h);
2685 scsi_host_put(host);
2687 fail_alloc_instance:
2689 pci_disable_device(pdev);
2695 * megasas_flush_cache - Requests FW to flush all its caches
2696 * @instance: Adapter soft state
2698 static void megasas_flush_cache(struct megasas_instance *instance)
2700 struct megasas_cmd *cmd;
2701 struct megasas_dcmd_frame *dcmd;
2703 cmd = megasas_get_cmd(instance);
2708 dcmd = &cmd->frame->dcmd;
2710 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2712 dcmd->cmd = MFI_CMD_DCMD;
2713 dcmd->cmd_status = 0x0;
2714 dcmd->sge_count = 0;
2715 dcmd->flags = MFI_FRAME_DIR_NONE;
2717 dcmd->data_xfer_len = 0;
2718 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2719 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2721 megasas_issue_blocked_cmd(instance, cmd);
2723 megasas_return_cmd(instance, cmd);
2729 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2730 * @instance: Adapter soft state
2731 * @opcode: Shutdown/Hibernate
2733 static void megasas_shutdown_controller(struct megasas_instance *instance,
2736 struct megasas_cmd *cmd;
2737 struct megasas_dcmd_frame *dcmd;
2739 cmd = megasas_get_cmd(instance);
2744 if (instance->aen_cmd)
2745 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2747 dcmd = &cmd->frame->dcmd;
2749 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2751 dcmd->cmd = MFI_CMD_DCMD;
2752 dcmd->cmd_status = 0x0;
2753 dcmd->sge_count = 0;
2754 dcmd->flags = MFI_FRAME_DIR_NONE;
2756 dcmd->data_xfer_len = 0;
2757 dcmd->opcode = opcode;
2759 megasas_issue_blocked_cmd(instance, cmd);
2761 megasas_return_cmd(instance, cmd);
2768 * megasas_suspend - driver suspend entry point
2769 * @pdev: PCI device structure
2770 * @state: PCI power state to suspend routine
2773 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
2775 struct Scsi_Host *host;
2776 struct megasas_instance *instance;
2778 instance = pci_get_drvdata(pdev);
2779 host = instance->host;
2782 del_timer_sync(&instance->io_completion_timer);
2784 megasas_flush_cache(instance);
2785 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
2786 tasklet_kill(&instance->isr_tasklet);
2788 pci_set_drvdata(instance->pdev, instance);
2789 instance->instancet->disable_intr(instance->reg_set);
2790 free_irq(instance->pdev->irq, instance);
2792 pci_save_state(pdev);
2793 pci_disable_device(pdev);
2795 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2801 * megasas_resume- driver resume entry point
2802 * @pdev: PCI device structure
2805 megasas_resume(struct pci_dev *pdev)
2808 struct Scsi_Host *host;
2809 struct megasas_instance *instance;
2811 instance = pci_get_drvdata(pdev);
2812 host = instance->host;
2813 pci_set_power_state(pdev, PCI_D0);
2814 pci_enable_wake(pdev, PCI_D0, 0);
2815 pci_restore_state(pdev);
2818 * PCI prepping: enable device set bus mastering and dma mask
2820 rval = pci_enable_device(pdev);
2823 printk(KERN_ERR "megasas: Enable device failed\n");
2827 pci_set_master(pdev);
2829 if (megasas_set_dma_mask(pdev))
2830 goto fail_set_dma_mask;
2833 * Initialize MFI Firmware
2836 *instance->producer = 0;
2837 *instance->consumer = 0;
2839 atomic_set(&instance->fw_outstanding, 0);
2842 * We expect the FW state to be READY
2844 if (megasas_transition_to_ready(instance))
2845 goto fail_ready_state;
2847 if (megasas_issue_init_mfi(instance))
2850 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2851 (unsigned long)instance);
2856 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
2857 "megasas", instance)) {
2858 printk(KERN_ERR "megasas: Failed to register IRQ\n");
2862 instance->instancet->enable_intr(instance->reg_set);
2865 * Initiate AEN (Asynchronous Event Notification)
2867 if (megasas_start_aen(instance))
2868 printk(KERN_ERR "megasas: Start AEN failed\n");
2870 /* Initialize the cmd completion timer */
2872 megasas_start_timer(instance, &instance->io_completion_timer,
2873 megasas_io_completion_timer,
2874 MEGASAS_COMPLETION_TIMER_INTERVAL);
2879 if (instance->evt_detail)
2880 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2881 instance->evt_detail,
2882 instance->evt_detail_h);
2884 if (instance->producer)
2885 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2886 instance->producer_h);
2887 if (instance->consumer)
2888 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2889 instance->consumer_h);
2890 scsi_host_put(host);
2895 pci_disable_device(pdev);
2900 #define megasas_suspend NULL
2901 #define megasas_resume NULL
2905 * megasas_detach_one - PCI hot"un"plug entry point
2906 * @pdev: PCI device structure
2908 static void __devexit megasas_detach_one(struct pci_dev *pdev)
2911 struct Scsi_Host *host;
2912 struct megasas_instance *instance;
2914 instance = pci_get_drvdata(pdev);
2915 host = instance->host;
2918 del_timer_sync(&instance->io_completion_timer);
2920 scsi_remove_host(instance->host);
2921 megasas_flush_cache(instance);
2922 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
2923 tasklet_kill(&instance->isr_tasklet);
2926 * Take the instance off the instance array. Note that we will not
2927 * decrement the max_index. We let this array be sparse array
2929 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2930 if (megasas_mgmt_info.instance[i] == instance) {
2931 megasas_mgmt_info.count--;
2932 megasas_mgmt_info.instance[i] = NULL;
2938 pci_set_drvdata(instance->pdev, NULL);
2940 instance->instancet->disable_intr(instance->reg_set);
2942 free_irq(instance->pdev->irq, instance);
2944 megasas_release_mfi(instance);
2946 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2947 instance->evt_detail, instance->evt_detail_h);
2949 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2950 instance->producer_h);
2952 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2953 instance->consumer_h);
2955 scsi_host_put(host);
2957 pci_set_drvdata(pdev, NULL);
2959 pci_disable_device(pdev);
2965 * megasas_shutdown - Shutdown entry point
2966 * @device: Generic device structure
2968 static void megasas_shutdown(struct pci_dev *pdev)
2970 struct megasas_instance *instance = pci_get_drvdata(pdev);
2971 megasas_flush_cache(instance);
2972 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
2976 * megasas_mgmt_open - char node "open" entry point
2978 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2980 cycle_kernel_lock();
2982 * Allow only those users with admin rights
2984 if (!capable(CAP_SYS_ADMIN))
2991 * megasas_mgmt_release - char node "release" entry point
2993 static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2995 filep->private_data = NULL;
2996 fasync_helper(-1, filep, 0, &megasas_async_queue);
3002 * megasas_mgmt_fasync - Async notifier registration from applications
3004 * This function adds the calling process to a driver global queue. When an
3005 * event occurs, SIGIO will be sent to all processes in this queue.
3007 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
3011 mutex_lock(&megasas_async_queue_mutex);
3013 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
3015 mutex_unlock(&megasas_async_queue_mutex);
3018 /* For sanity check when we get ioctl */
3019 filep->private_data = filep;
3023 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
3029 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
3030 * @instance: Adapter soft state
3031 * @argp: User's ioctl packet
3034 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
3035 struct megasas_iocpacket __user * user_ioc,
3036 struct megasas_iocpacket *ioc)
3038 struct megasas_sge32 *kern_sge32;
3039 struct megasas_cmd *cmd;
3040 void *kbuff_arr[MAX_IOCTL_SGE];
3041 dma_addr_t buf_handle = 0;
3044 dma_addr_t sense_handle;
3047 memset(kbuff_arr, 0, sizeof(kbuff_arr));
3049 if (ioc->sge_count > MAX_IOCTL_SGE) {
3050 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
3051 ioc->sge_count, MAX_IOCTL_SGE);
3055 cmd = megasas_get_cmd(instance);
3057 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
3062 * User's IOCTL packet has 2 frames (maximum). Copy those two
3063 * frames into our cmd's frames. cmd->frame's context will get
3064 * overwritten when we copy from user's frames. So set that value
3067 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
3068 cmd->frame->hdr.context = cmd->index;
3071 * The management interface between applications and the fw uses
3072 * MFI frames. E.g, RAID configuration changes, LD property changes
3073 * etc are accomplishes through different kinds of MFI frames. The
3074 * driver needs to care only about substituting user buffers with
3075 * kernel buffers in SGLs. The location of SGL is embedded in the
3076 * struct iocpacket itself.
3078 kern_sge32 = (struct megasas_sge32 *)
3079 ((unsigned long)cmd->frame + ioc->sgl_off);
3082 * For each user buffer, create a mirror buffer and copy in
3084 for (i = 0; i < ioc->sge_count; i++) {
3085 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
3086 ioc->sgl[i].iov_len,
3087 &buf_handle, GFP_KERNEL);
3088 if (!kbuff_arr[i]) {
3089 printk(KERN_DEBUG "megasas: Failed to alloc "
3090 "kernel SGL buffer for IOCTL \n");
3096 * We don't change the dma_coherent_mask, so
3097 * pci_alloc_consistent only returns 32bit addresses
3099 kern_sge32[i].phys_addr = (u32) buf_handle;
3100 kern_sge32[i].length = ioc->sgl[i].iov_len;
3103 * We created a kernel buffer corresponding to the
3104 * user buffer. Now copy in from the user buffer
3106 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
3107 (u32) (ioc->sgl[i].iov_len))) {
3113 if (ioc->sense_len) {
3114 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
3115 &sense_handle, GFP_KERNEL);
3122 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
3123 *sense_ptr = sense_handle;
3127 * Set the sync_cmd flag so that the ISR knows not to complete this
3128 * cmd to the SCSI mid-layer
3131 megasas_issue_blocked_cmd(instance, cmd);
3135 * copy out the kernel buffers to user buffers
3137 for (i = 0; i < ioc->sge_count; i++) {
3138 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3139 ioc->sgl[i].iov_len)) {
3146 * copy out the sense
3148 if (ioc->sense_len) {
3150 * sense_ptr points to the location that has the user
3151 * sense buffer address
3153 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
3156 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3157 sense, ioc->sense_len)) {
3158 printk(KERN_ERR "megasas: Failed to copy out to user "
3166 * copy the status codes returned by the fw
3168 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3169 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3170 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3176 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
3177 sense, sense_handle);
3180 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
3181 dma_free_coherent(&instance->pdev->dev,
3182 kern_sge32[i].length,
3183 kbuff_arr[i], kern_sge32[i].phys_addr);
3186 megasas_return_cmd(instance, cmd);
3190 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
3194 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3196 if ((megasas_mgmt_info.instance[i]) &&
3197 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
3198 return megasas_mgmt_info.instance[i];
3204 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3206 struct megasas_iocpacket __user *user_ioc =
3207 (struct megasas_iocpacket __user *)arg;
3208 struct megasas_iocpacket *ioc;
3209 struct megasas_instance *instance;
3212 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3216 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3221 instance = megasas_lookup_instance(ioc->host_no);
3228 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3230 if (down_interruptible(&instance->ioctl_sem)) {
3231 error = -ERESTARTSYS;
3234 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3235 up(&instance->ioctl_sem);
3242 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3244 struct megasas_instance *instance;
3245 struct megasas_aen aen;
3248 if (file->private_data != file) {
3249 printk(KERN_DEBUG "megasas: fasync_helper was not "
3254 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3257 instance = megasas_lookup_instance(aen.host_no);
3262 mutex_lock(&instance->aen_mutex);
3263 error = megasas_register_aen(instance, aen.seq_num,
3264 aen.class_locale_word);
3265 mutex_unlock(&instance->aen_mutex);
3270 * megasas_mgmt_ioctl - char node ioctl entry point
3273 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3276 case MEGASAS_IOC_FIRMWARE:
3277 return megasas_mgmt_ioctl_fw(file, arg);
3279 case MEGASAS_IOC_GET_AEN:
3280 return megasas_mgmt_ioctl_aen(file, arg);
3286 #ifdef CONFIG_COMPAT
3287 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3289 struct compat_megasas_iocpacket __user *cioc =
3290 (struct compat_megasas_iocpacket __user *)arg;
3291 struct megasas_iocpacket __user *ioc =
3292 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3296 if (clear_user(ioc, sizeof(*ioc)))
3299 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3300 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3301 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3302 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3303 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3304 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3307 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3310 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3311 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3312 copy_in_user(&ioc->sgl[i].iov_len,
3313 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3317 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3319 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3320 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3321 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3328 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3332 case MEGASAS_IOC_FIRMWARE32:
3333 return megasas_mgmt_compat_ioctl_fw(file, arg);
3334 case MEGASAS_IOC_GET_AEN:
3335 return megasas_mgmt_ioctl_aen(file, arg);
3343 * File operations structure for management interface
3345 static const struct file_operations megasas_mgmt_fops = {
3346 .owner = THIS_MODULE,
3347 .open = megasas_mgmt_open,
3348 .release = megasas_mgmt_release,
3349 .fasync = megasas_mgmt_fasync,
3350 .unlocked_ioctl = megasas_mgmt_ioctl,
3351 #ifdef CONFIG_COMPAT
3352 .compat_ioctl = megasas_mgmt_compat_ioctl,
3357 * PCI hotplug support registration structure
3359 static struct pci_driver megasas_pci_driver = {
3361 .name = "megaraid_sas",
3362 .id_table = megasas_pci_table,
3363 .probe = megasas_probe_one,
3364 .remove = __devexit_p(megasas_detach_one),
3365 .suspend = megasas_suspend,
3366 .resume = megasas_resume,
3367 .shutdown = megasas_shutdown,
3371 * Sysfs driver attributes
3373 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3375 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3379 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3382 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3384 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3388 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3392 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3394 return sprintf(buf, "%u\n", megasas_dbg_lvl);
3398 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3401 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3402 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3408 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
3409 megasas_sysfs_set_dbg_lvl);
3412 megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3414 return sprintf(buf, "%u\n", poll_mode_io);
3418 megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3419 const char *buf, size_t count)
3422 int tmp = poll_mode_io;
3424 struct megasas_instance *instance;
3426 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3427 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3432 * Check if poll_mode_io is already set or is same as previous value
3434 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3439 * Start timers for all adapters
3441 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3442 instance = megasas_mgmt_info.instance[i];
3444 megasas_start_timer(instance,
3445 &instance->io_completion_timer,
3446 megasas_io_completion_timer,
3447 MEGASAS_COMPLETION_TIMER_INTERVAL);
3452 * Delete timers for all adapters
3454 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3455 instance = megasas_mgmt_info.instance[i];
3457 del_timer_sync(&instance->io_completion_timer);
3465 static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3466 megasas_sysfs_show_poll_mode_io,
3467 megasas_sysfs_set_poll_mode_io);
3470 * megasas_init - Driver load entry point
3472 static int __init megasas_init(void)
3477 * Announce driver version and other information
3479 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3480 MEGASAS_EXT_VERSION);
3482 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3485 * Register character device node
3487 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3490 printk(KERN_DEBUG "megasas: failed to open device node\n");
3494 megasas_mgmt_majorno = rval;
3497 * Register ourselves as PCI hotplug module
3499 rval = pci_register_driver(&megasas_pci_driver);
3502 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
3506 rval = driver_create_file(&megasas_pci_driver.driver,
3507 &driver_attr_version);
3509 goto err_dcf_attr_ver;
3510 rval = driver_create_file(&megasas_pci_driver.driver,
3511 &driver_attr_release_date);
3513 goto err_dcf_rel_date;
3514 rval = driver_create_file(&megasas_pci_driver.driver,
3515 &driver_attr_dbg_lvl);
3517 goto err_dcf_dbg_lvl;
3518 rval = driver_create_file(&megasas_pci_driver.driver,
3519 &driver_attr_poll_mode_io);
3521 goto err_dcf_poll_mode_io;
3525 err_dcf_poll_mode_io:
3526 driver_remove_file(&megasas_pci_driver.driver,
3527 &driver_attr_dbg_lvl);
3529 driver_remove_file(&megasas_pci_driver.driver,
3530 &driver_attr_release_date);
3532 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3534 pci_unregister_driver(&megasas_pci_driver);
3536 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3541 * megasas_exit - Driver unload entry point
3543 static void __exit megasas_exit(void)
3545 driver_remove_file(&megasas_pci_driver.driver,
3546 &driver_attr_poll_mode_io);
3547 driver_remove_file(&megasas_pci_driver.driver,
3548 &driver_attr_dbg_lvl);
3549 driver_remove_file(&megasas_pci_driver.driver,
3550 &driver_attr_release_date);
3551 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3553 pci_unregister_driver(&megasas_pci_driver);
3554 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3557 module_init(megasas_init);
3558 module_exit(megasas_exit);