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.17.1-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 <linux/slab.h>
39 #include <asm/uaccess.h>
41 #include <linux/compat.h>
42 #include <linux/blkdev.h>
43 #include <linux/mutex.h>
44 #include <linux/poll.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_cmnd.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_host.h>
50 #include "megaraid_sas.h"
53 * poll_mode_io:1- schedule complete completion from q cmd
55 static unsigned int poll_mode_io;
56 module_param_named(poll_mode_io, poll_mode_io, int, 0);
57 MODULE_PARM_DESC(poll_mode_io,
58 "Complete cmds from IO path, (default=0)");
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(MEGASAS_VERSION);
62 MODULE_AUTHOR("megaraidlinux@lsi.com");
63 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
65 static int megasas_transition_to_ready(struct megasas_instance *instance);
66 static int megasas_get_pd_list(struct megasas_instance *instance);
67 static int megasas_issue_init_mfi(struct megasas_instance *instance);
68 static int megasas_register_aen(struct megasas_instance *instance,
69 u32 seq_num, u32 class_locale_word);
71 * PCI ID table for all supported controllers
73 static struct pci_device_id megasas_pci_table[] = {
75 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
77 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
79 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
81 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
83 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
85 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
87 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
89 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
90 /* xscale IOP, vega */
91 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
96 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
98 static int megasas_mgmt_majorno;
99 static struct megasas_mgmt_info megasas_mgmt_info;
100 static struct fasync_struct *megasas_async_queue;
101 static DEFINE_MUTEX(megasas_async_queue_mutex);
103 static int megasas_poll_wait_aen;
104 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
105 static u32 support_poll_for_event;
106 static u32 megasas_dbg_lvl;
108 /* define lock for aen poll */
109 spinlock_t poll_aen_lock;
112 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
116 * megasas_get_cmd - Get a command from the free pool
117 * @instance: Adapter soft state
119 * Returns a free command from the pool
121 static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
125 struct megasas_cmd *cmd = NULL;
127 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
129 if (!list_empty(&instance->cmd_pool)) {
130 cmd = list_entry((&instance->cmd_pool)->next,
131 struct megasas_cmd, list);
132 list_del_init(&cmd->list);
134 printk(KERN_ERR "megasas: Command pool empty!\n");
137 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
142 * megasas_return_cmd - Return a cmd to free command pool
143 * @instance: Adapter soft state
144 * @cmd: Command packet to be returned to free command pool
147 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
151 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
154 list_add_tail(&cmd->list, &instance->cmd_pool);
156 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
161 * The following functions are defined for xscale
162 * (deviceid : 1064R, PERC5) controllers
166 * megasas_enable_intr_xscale - Enables interrupts
167 * @regs: MFI register set
170 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
172 writel(0, &(regs)->outbound_intr_mask);
174 /* Dummy readl to force pci flush */
175 readl(®s->outbound_intr_mask);
179 * megasas_disable_intr_xscale -Disables interrupt
180 * @regs: MFI register set
183 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
186 writel(mask, ®s->outbound_intr_mask);
187 /* Dummy readl to force pci flush */
188 readl(®s->outbound_intr_mask);
192 * megasas_read_fw_status_reg_xscale - returns the current FW status value
193 * @regs: MFI register set
196 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
198 return readl(&(regs)->outbound_msg_0);
201 * megasas_clear_interrupt_xscale - Check & clear interrupt
202 * @regs: MFI register set
205 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
210 * Check if it is our interrupt
212 status = readl(®s->outbound_intr_status);
214 if (status & MFI_OB_INTR_STATUS_MASK)
215 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
216 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
217 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
220 * Clear the interrupt by writing back the same value
223 writel(status, ®s->outbound_intr_status);
225 /* Dummy readl to force pci flush */
226 readl(®s->outbound_intr_status);
232 * megasas_fire_cmd_xscale - Sends command to the FW
233 * @frame_phys_addr : Physical address of cmd
234 * @frame_count : Number of frames for the command
235 * @regs : MFI register set
238 megasas_fire_cmd_xscale(struct megasas_instance *instance,
239 dma_addr_t frame_phys_addr,
241 struct megasas_register_set __iomem *regs)
244 spin_lock_irqsave(&instance->hba_lock, flags);
245 writel((frame_phys_addr >> 3)|(frame_count),
246 &(regs)->inbound_queue_port);
247 spin_unlock_irqrestore(&instance->hba_lock, flags);
251 * megasas_adp_reset_xscale - For controller reset
252 * @regs: MFI register set
255 megasas_adp_reset_xscale(struct megasas_instance *instance,
256 struct megasas_register_set __iomem *regs)
260 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
262 for (i = 0; i < 3; i++)
263 msleep(1000); /* sleep for 3 secs */
265 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
266 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
268 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
270 pci_write_config_dword(instance->pdev,
271 MFI_1068_PCSR_OFFSET, pcidata);
273 for (i = 0; i < 2; i++)
274 msleep(1000); /* need to wait 2 secs again */
277 pci_read_config_dword(instance->pdev,
278 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
279 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
280 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
281 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
283 pci_write_config_dword(instance->pdev,
284 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
291 * megasas_check_reset_xscale - For controller reset check
292 * @regs: MFI register set
295 megasas_check_reset_xscale(struct megasas_instance *instance,
296 struct megasas_register_set __iomem *regs)
299 consumer = *instance->consumer;
301 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
302 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
308 static struct megasas_instance_template megasas_instance_template_xscale = {
310 .fire_cmd = megasas_fire_cmd_xscale,
311 .enable_intr = megasas_enable_intr_xscale,
312 .disable_intr = megasas_disable_intr_xscale,
313 .clear_intr = megasas_clear_intr_xscale,
314 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
315 .adp_reset = megasas_adp_reset_xscale,
316 .check_reset = megasas_check_reset_xscale,
320 * This is the end of set of functions & definitions specific
321 * to xscale (deviceid : 1064R, PERC5) controllers
325 * The following functions are defined for ppc (deviceid : 0x60)
330 * megasas_enable_intr_ppc - Enables interrupts
331 * @regs: MFI register set
334 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
336 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
338 writel(~0x80000000, &(regs)->outbound_intr_mask);
340 /* Dummy readl to force pci flush */
341 readl(®s->outbound_intr_mask);
345 * megasas_disable_intr_ppc - Disable interrupt
346 * @regs: MFI register set
349 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
351 u32 mask = 0xFFFFFFFF;
352 writel(mask, ®s->outbound_intr_mask);
353 /* Dummy readl to force pci flush */
354 readl(®s->outbound_intr_mask);
358 * megasas_read_fw_status_reg_ppc - returns the current FW status value
359 * @regs: MFI register set
362 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
364 return readl(&(regs)->outbound_scratch_pad);
368 * megasas_clear_interrupt_ppc - Check & clear interrupt
369 * @regs: MFI register set
372 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
376 * Check if it is our interrupt
378 status = readl(®s->outbound_intr_status);
380 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
385 * Clear the interrupt by writing back the same value
387 writel(status, ®s->outbound_doorbell_clear);
389 /* Dummy readl to force pci flush */
390 readl(®s->outbound_doorbell_clear);
395 * megasas_fire_cmd_ppc - Sends command to the FW
396 * @frame_phys_addr : Physical address of cmd
397 * @frame_count : Number of frames for the command
398 * @regs : MFI register set
401 megasas_fire_cmd_ppc(struct megasas_instance *instance,
402 dma_addr_t frame_phys_addr,
404 struct megasas_register_set __iomem *regs)
407 spin_lock_irqsave(&instance->hba_lock, flags);
408 writel((frame_phys_addr | (frame_count<<1))|1,
409 &(regs)->inbound_queue_port);
410 spin_unlock_irqrestore(&instance->hba_lock, flags);
414 * megasas_adp_reset_ppc - For controller reset
415 * @regs: MFI register set
418 megasas_adp_reset_ppc(struct megasas_instance *instance,
419 struct megasas_register_set __iomem *regs)
425 * megasas_check_reset_ppc - For controller reset check
426 * @regs: MFI register set
429 megasas_check_reset_ppc(struct megasas_instance *instance,
430 struct megasas_register_set __iomem *regs)
434 static struct megasas_instance_template megasas_instance_template_ppc = {
436 .fire_cmd = megasas_fire_cmd_ppc,
437 .enable_intr = megasas_enable_intr_ppc,
438 .disable_intr = megasas_disable_intr_ppc,
439 .clear_intr = megasas_clear_intr_ppc,
440 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
441 .adp_reset = megasas_adp_reset_ppc,
442 .check_reset = megasas_check_reset_ppc,
446 * megasas_enable_intr_skinny - Enables interrupts
447 * @regs: MFI register set
450 megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
452 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
454 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
456 /* Dummy readl to force pci flush */
457 readl(®s->outbound_intr_mask);
461 * megasas_disable_intr_skinny - Disables interrupt
462 * @regs: MFI register set
465 megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
467 u32 mask = 0xFFFFFFFF;
468 writel(mask, ®s->outbound_intr_mask);
469 /* Dummy readl to force pci flush */
470 readl(®s->outbound_intr_mask);
474 * megasas_read_fw_status_reg_skinny - returns the current FW status value
475 * @regs: MFI register set
478 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
480 return readl(&(regs)->outbound_scratch_pad);
484 * megasas_clear_interrupt_skinny - Check & clear interrupt
485 * @regs: MFI register set
488 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
492 * Check if it is our interrupt
494 status = readl(®s->outbound_intr_status);
496 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
501 * Clear the interrupt by writing back the same value
503 writel(status, ®s->outbound_intr_status);
506 * dummy read to flush PCI
508 readl(®s->outbound_intr_status);
514 * megasas_fire_cmd_skinny - Sends command to the FW
515 * @frame_phys_addr : Physical address of cmd
516 * @frame_count : Number of frames for the command
517 * @regs : MFI register set
520 megasas_fire_cmd_skinny(struct megasas_instance *instance,
521 dma_addr_t frame_phys_addr,
523 struct megasas_register_set __iomem *regs)
526 spin_lock_irqsave(&instance->hba_lock, flags);
527 writel(0, &(regs)->inbound_high_queue_port);
528 writel((frame_phys_addr | (frame_count<<1))|1,
529 &(regs)->inbound_low_queue_port);
530 spin_unlock_irqrestore(&instance->hba_lock, flags);
534 * megasas_adp_reset_skinny - For controller reset
535 * @regs: MFI register set
538 megasas_adp_reset_skinny(struct megasas_instance *instance,
539 struct megasas_register_set __iomem *regs)
545 * megasas_check_reset_skinny - For controller reset check
546 * @regs: MFI register set
549 megasas_check_reset_skinny(struct megasas_instance *instance,
550 struct megasas_register_set __iomem *regs)
555 static struct megasas_instance_template megasas_instance_template_skinny = {
557 .fire_cmd = megasas_fire_cmd_skinny,
558 .enable_intr = megasas_enable_intr_skinny,
559 .disable_intr = megasas_disable_intr_skinny,
560 .clear_intr = megasas_clear_intr_skinny,
561 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
562 .adp_reset = megasas_adp_reset_skinny,
563 .check_reset = megasas_check_reset_skinny,
568 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
573 * megasas_enable_intr_gen2 - Enables interrupts
574 * @regs: MFI register set
577 megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
579 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
581 /* write ~0x00000005 (4 & 1) to the intr mask*/
582 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
584 /* Dummy readl to force pci flush */
585 readl(®s->outbound_intr_mask);
589 * megasas_disable_intr_gen2 - Disables interrupt
590 * @regs: MFI register set
593 megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
595 u32 mask = 0xFFFFFFFF;
596 writel(mask, ®s->outbound_intr_mask);
597 /* Dummy readl to force pci flush */
598 readl(®s->outbound_intr_mask);
602 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
603 * @regs: MFI register set
606 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
608 return readl(&(regs)->outbound_scratch_pad);
612 * megasas_clear_interrupt_gen2 - Check & clear interrupt
613 * @regs: MFI register set
616 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
621 * Check if it is our interrupt
623 status = readl(®s->outbound_intr_status);
625 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
626 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
628 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
629 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
633 * Clear the interrupt by writing back the same value
636 writel(status, ®s->outbound_doorbell_clear);
638 /* Dummy readl to force pci flush */
639 readl(®s->outbound_intr_status);
644 * megasas_fire_cmd_gen2 - Sends command to the FW
645 * @frame_phys_addr : Physical address of cmd
646 * @frame_count : Number of frames for the command
647 * @regs : MFI register set
650 megasas_fire_cmd_gen2(struct megasas_instance *instance,
651 dma_addr_t frame_phys_addr,
653 struct megasas_register_set __iomem *regs)
656 spin_lock_irqsave(&instance->hba_lock, flags);
657 writel((frame_phys_addr | (frame_count<<1))|1,
658 &(regs)->inbound_queue_port);
659 spin_unlock_irqrestore(&instance->hba_lock, flags);
663 * megasas_adp_reset_gen2 - For controller reset
664 * @regs: MFI register set
667 megasas_adp_reset_gen2(struct megasas_instance *instance,
668 struct megasas_register_set __iomem *reg_set)
673 writel(0, ®_set->seq_offset);
674 writel(4, ®_set->seq_offset);
675 writel(0xb, ®_set->seq_offset);
676 writel(2, ®_set->seq_offset);
677 writel(7, ®_set->seq_offset);
678 writel(0xd, ®_set->seq_offset);
681 HostDiag = (u32)readl(®_set->host_diag);
683 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
685 HostDiag = (u32)readl(®_set->host_diag);
686 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
694 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
696 writel((HostDiag | DIAG_RESET_ADAPTER), ®_set->host_diag);
700 HostDiag = (u32)readl(®_set->host_diag);
701 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
703 HostDiag = (u32)readl(®_set->host_diag);
704 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
715 * megasas_check_reset_gen2 - For controller reset check
716 * @regs: MFI register set
719 megasas_check_reset_gen2(struct megasas_instance *instance,
720 struct megasas_register_set __iomem *regs)
725 static struct megasas_instance_template megasas_instance_template_gen2 = {
727 .fire_cmd = megasas_fire_cmd_gen2,
728 .enable_intr = megasas_enable_intr_gen2,
729 .disable_intr = megasas_disable_intr_gen2,
730 .clear_intr = megasas_clear_intr_gen2,
731 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
732 .adp_reset = megasas_adp_reset_gen2,
733 .check_reset = megasas_check_reset_gen2,
737 * This is the end of set of functions & definitions
738 * specific to gen2 (deviceid : 0x78, 0x79) controllers
742 * megasas_issue_polled - Issues a polling command
743 * @instance: Adapter soft state
744 * @cmd: Command packet to be issued
746 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
749 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
752 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
754 struct megasas_header *frame_hdr = &cmd->frame->hdr;
756 frame_hdr->cmd_status = 0xFF;
757 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
760 * Issue the frame using inbound queue port
762 instance->instancet->fire_cmd(instance,
763 cmd->frame_phys_addr, 0, instance->reg_set);
766 * Wait for cmd_status to change
768 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
773 if (frame_hdr->cmd_status == 0xff)
780 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
781 * @instance: Adapter soft state
782 * @cmd: Command to be issued
784 * This function waits on an event for the command to be returned from ISR.
785 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
786 * Used to issue ioctl commands.
789 megasas_issue_blocked_cmd(struct megasas_instance *instance,
790 struct megasas_cmd *cmd)
792 cmd->cmd_status = ENODATA;
794 instance->instancet->fire_cmd(instance,
795 cmd->frame_phys_addr, 0, instance->reg_set);
797 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
803 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
804 * @instance: Adapter soft state
805 * @cmd_to_abort: Previously issued cmd to be aborted
807 * MFI firmware can abort previously issued AEN comamnd (automatic event
808 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
809 * cmd and waits for return status.
810 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
813 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
814 struct megasas_cmd *cmd_to_abort)
816 struct megasas_cmd *cmd;
817 struct megasas_abort_frame *abort_fr;
819 cmd = megasas_get_cmd(instance);
824 abort_fr = &cmd->frame->abort;
827 * Prepare and issue the abort frame
829 abort_fr->cmd = MFI_CMD_ABORT;
830 abort_fr->cmd_status = 0xFF;
832 abort_fr->abort_context = cmd_to_abort->index;
833 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
834 abort_fr->abort_mfi_phys_addr_hi = 0;
837 cmd->cmd_status = 0xFF;
839 instance->instancet->fire_cmd(instance,
840 cmd->frame_phys_addr, 0, instance->reg_set);
843 * Wait for this cmd to complete
845 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
848 megasas_return_cmd(instance, cmd);
853 * megasas_make_sgl32 - Prepares 32-bit SGL
854 * @instance: Adapter soft state
855 * @scp: SCSI command from the mid-layer
856 * @mfi_sgl: SGL to be filled in
858 * If successful, this function returns the number of SG elements. Otherwise,
862 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
863 union megasas_sgl *mfi_sgl)
867 struct scatterlist *os_sgl;
869 sge_count = scsi_dma_map(scp);
870 BUG_ON(sge_count < 0);
873 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
874 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
875 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
882 * megasas_make_sgl64 - Prepares 64-bit SGL
883 * @instance: Adapter soft state
884 * @scp: SCSI command from the mid-layer
885 * @mfi_sgl: SGL to be filled in
887 * If successful, this function returns the number of SG elements. Otherwise,
891 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
892 union megasas_sgl *mfi_sgl)
896 struct scatterlist *os_sgl;
898 sge_count = scsi_dma_map(scp);
899 BUG_ON(sge_count < 0);
902 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
903 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
904 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
911 * megasas_make_sgl_skinny - Prepares IEEE SGL
912 * @instance: Adapter soft state
913 * @scp: SCSI command from the mid-layer
914 * @mfi_sgl: SGL to be filled in
916 * If successful, this function returns the number of SG elements. Otherwise,
920 megasas_make_sgl_skinny(struct megasas_instance *instance,
921 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
925 struct scatterlist *os_sgl;
927 sge_count = scsi_dma_map(scp);
930 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
931 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
932 mfi_sgl->sge_skinny[i].phys_addr =
933 sg_dma_address(os_sgl);
940 * megasas_get_frame_count - Computes the number of frames
941 * @frame_type : type of frame- io or pthru frame
942 * @sge_count : number of sg elements
944 * Returns the number of frames required for numnber of sge's (sge_count)
947 static u32 megasas_get_frame_count(struct megasas_instance *instance,
948 u8 sge_count, u8 frame_type)
955 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
956 sizeof(struct megasas_sge32);
958 if (instance->flag_ieee) {
959 sge_sz = sizeof(struct megasas_sge_skinny);
963 * Main frame can contain 2 SGEs for 64-bit SGLs and
964 * 3 SGEs for 32-bit SGLs for ldio &
965 * 1 SGEs for 64-bit SGLs and
966 * 2 SGEs for 32-bit SGLs for pthru frame
968 if (unlikely(frame_type == PTHRU_FRAME)) {
969 if (instance->flag_ieee == 1) {
970 num_cnt = sge_count - 1;
972 num_cnt = sge_count - 1;
974 num_cnt = sge_count - 2;
976 if (instance->flag_ieee == 1) {
977 num_cnt = sge_count - 1;
979 num_cnt = sge_count - 2;
981 num_cnt = sge_count - 3;
985 sge_bytes = sge_sz * num_cnt;
987 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
988 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
999 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1000 * @instance: Adapter soft state
1001 * @scp: SCSI command
1002 * @cmd: Command to be prepared in
1004 * This function prepares CDB commands. These are typcially pass-through
1005 * commands to the devices.
1008 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1009 struct megasas_cmd *cmd)
1014 struct megasas_pthru_frame *pthru;
1016 is_logical = MEGASAS_IS_LOGICAL(scp);
1017 device_id = MEGASAS_DEV_INDEX(instance, scp);
1018 pthru = (struct megasas_pthru_frame *)cmd->frame;
1020 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1021 flags = MFI_FRAME_DIR_WRITE;
1022 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1023 flags = MFI_FRAME_DIR_READ;
1024 else if (scp->sc_data_direction == PCI_DMA_NONE)
1025 flags = MFI_FRAME_DIR_NONE;
1027 if (instance->flag_ieee == 1) {
1028 flags |= MFI_FRAME_IEEE;
1032 * Prepare the DCDB frame
1034 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1035 pthru->cmd_status = 0x0;
1036 pthru->scsi_status = 0x0;
1037 pthru->target_id = device_id;
1038 pthru->lun = scp->device->lun;
1039 pthru->cdb_len = scp->cmd_len;
1042 pthru->flags = flags;
1043 pthru->data_xfer_len = scsi_bufflen(scp);
1045 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1048 * If the command is for the tape device, set the
1049 * pthru timeout to the os layer timeout value.
1051 if (scp->device->type == TYPE_TAPE) {
1052 if ((scp->request->timeout / HZ) > 0xFFFF)
1053 pthru->timeout = 0xFFFF;
1055 pthru->timeout = scp->request->timeout / HZ;
1061 if (instance->flag_ieee == 1) {
1062 pthru->flags |= MFI_FRAME_SGL64;
1063 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1065 } else if (IS_DMA64) {
1066 pthru->flags |= MFI_FRAME_SGL64;
1067 pthru->sge_count = megasas_make_sgl64(instance, scp,
1070 pthru->sge_count = megasas_make_sgl32(instance, scp,
1073 if (pthru->sge_count > instance->max_num_sge) {
1074 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1080 * Sense info specific
1082 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1083 pthru->sense_buf_phys_addr_hi = 0;
1084 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1087 * Compute the total number of frames this command consumes. FW uses
1088 * this number to pull sufficient number of frames from host memory.
1090 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1093 return cmd->frame_count;
1097 * megasas_build_ldio - Prepares IOs to logical devices
1098 * @instance: Adapter soft state
1099 * @scp: SCSI command
1100 * @cmd: Command to be prepared
1102 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1105 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1106 struct megasas_cmd *cmd)
1109 u8 sc = scp->cmnd[0];
1111 struct megasas_io_frame *ldio;
1113 device_id = MEGASAS_DEV_INDEX(instance, scp);
1114 ldio = (struct megasas_io_frame *)cmd->frame;
1116 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1117 flags = MFI_FRAME_DIR_WRITE;
1118 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1119 flags = MFI_FRAME_DIR_READ;
1121 if (instance->flag_ieee == 1) {
1122 flags |= MFI_FRAME_IEEE;
1126 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1128 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1129 ldio->cmd_status = 0x0;
1130 ldio->scsi_status = 0x0;
1131 ldio->target_id = device_id;
1133 ldio->reserved_0 = 0;
1135 ldio->flags = flags;
1136 ldio->start_lba_hi = 0;
1137 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1140 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1142 if (scp->cmd_len == 6) {
1143 ldio->lba_count = (u32) scp->cmnd[4];
1144 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1145 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1147 ldio->start_lba_lo &= 0x1FFFFF;
1151 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1153 else if (scp->cmd_len == 10) {
1154 ldio->lba_count = (u32) scp->cmnd[8] |
1155 ((u32) scp->cmnd[7] << 8);
1156 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1157 ((u32) scp->cmnd[3] << 16) |
1158 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1162 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1164 else if (scp->cmd_len == 12) {
1165 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1166 ((u32) scp->cmnd[7] << 16) |
1167 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1169 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1170 ((u32) scp->cmnd[3] << 16) |
1171 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1175 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1177 else if (scp->cmd_len == 16) {
1178 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1179 ((u32) scp->cmnd[11] << 16) |
1180 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1182 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1183 ((u32) scp->cmnd[7] << 16) |
1184 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1186 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1187 ((u32) scp->cmnd[3] << 16) |
1188 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1195 if (instance->flag_ieee) {
1196 ldio->flags |= MFI_FRAME_SGL64;
1197 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1199 } else if (IS_DMA64) {
1200 ldio->flags |= MFI_FRAME_SGL64;
1201 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1203 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1205 if (ldio->sge_count > instance->max_num_sge) {
1206 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1212 * Sense info specific
1214 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1215 ldio->sense_buf_phys_addr_hi = 0;
1216 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1219 * Compute the total number of frames this command consumes. FW uses
1220 * this number to pull sufficient number of frames from host memory.
1222 cmd->frame_count = megasas_get_frame_count(instance,
1223 ldio->sge_count, IO_FRAME);
1225 return cmd->frame_count;
1229 * megasas_is_ldio - Checks if the cmd is for logical drive
1230 * @scmd: SCSI command
1232 * Called by megasas_queue_command to find out if the command to be queued
1233 * is a logical drive command
1235 static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
1237 if (!MEGASAS_IS_LOGICAL(cmd))
1239 switch (cmd->cmnd[0]) {
1255 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1257 * @instance: Adapter soft state
1260 megasas_dump_pending_frames(struct megasas_instance *instance)
1262 struct megasas_cmd *cmd;
1264 union megasas_sgl *mfi_sgl;
1265 struct megasas_io_frame *ldio;
1266 struct megasas_pthru_frame *pthru;
1268 u32 max_cmd = instance->max_fw_cmds;
1270 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1271 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1273 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1275 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1277 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1278 for (i = 0; i < max_cmd; i++) {
1279 cmd = instance->cmd_list[i];
1282 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1283 if (megasas_is_ldio(cmd->scmd)){
1284 ldio = (struct megasas_io_frame *)cmd->frame;
1285 mfi_sgl = &ldio->sgl;
1286 sgcount = ldio->sge_count;
1287 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);
1290 pthru = (struct megasas_pthru_frame *) cmd->frame;
1291 mfi_sgl = &pthru->sgl;
1292 sgcount = pthru->sge_count;
1293 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);
1295 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1296 for (n = 0; n < sgcount; n++){
1298 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) ;
1300 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1303 printk(KERN_ERR "\n");
1305 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1306 for (i = 0; i < max_cmd; i++) {
1308 cmd = instance->cmd_list[i];
1310 if(cmd->sync_cmd == 1){
1311 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1314 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1318 * megasas_queue_command - Queue entry point
1319 * @scmd: SCSI command to be queued
1320 * @done: Callback entry point
1323 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1326 struct megasas_cmd *cmd;
1327 struct megasas_instance *instance;
1328 unsigned long flags;
1330 instance = (struct megasas_instance *)
1331 scmd->device->host->hostdata;
1333 if (instance->issuepend_done == 0)
1334 return SCSI_MLQUEUE_HOST_BUSY;
1336 spin_lock_irqsave(&instance->hba_lock, flags);
1337 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1338 spin_unlock_irqrestore(&instance->hba_lock, flags);
1339 return SCSI_MLQUEUE_HOST_BUSY;
1342 spin_unlock_irqrestore(&instance->hba_lock, flags);
1344 scmd->scsi_done = done;
1347 if (MEGASAS_IS_LOGICAL(scmd) &&
1348 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1349 scmd->result = DID_BAD_TARGET << 16;
1353 switch (scmd->cmnd[0]) {
1354 case SYNCHRONIZE_CACHE:
1356 * FW takes care of flush cache on its own
1357 * No need to send it down
1359 scmd->result = DID_OK << 16;
1365 cmd = megasas_get_cmd(instance);
1367 return SCSI_MLQUEUE_HOST_BUSY;
1370 * Logical drive command
1372 if (megasas_is_ldio(scmd))
1373 frame_count = megasas_build_ldio(instance, scmd, cmd);
1375 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1378 goto out_return_cmd;
1381 scmd->SCp.ptr = (char *)cmd;
1384 * Issue the command to the FW
1386 atomic_inc(&instance->fw_outstanding);
1388 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1389 cmd->frame_count-1, instance->reg_set);
1391 * Check if we have pend cmds to be completed
1393 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1394 tasklet_schedule(&instance->isr_tasklet);
1400 megasas_return_cmd(instance, cmd);
1406 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1410 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1412 if ((megasas_mgmt_info.instance[i]) &&
1413 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1414 return megasas_mgmt_info.instance[i];
1420 static int megasas_slave_configure(struct scsi_device *sdev)
1423 struct megasas_instance *instance ;
1425 instance = megasas_lookup_instance(sdev->host->host_no);
1428 * Don't export physical disk devices to the disk driver.
1430 * FIXME: Currently we don't export them to the midlayer at all.
1431 * That will be fixed once LSI engineers have audited the
1432 * firmware for possible issues.
1434 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1435 sdev->type == TYPE_DISK) {
1436 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1438 if (instance->pd_list[pd_index].driveState ==
1439 MR_PD_STATE_SYSTEM) {
1440 blk_queue_rq_timeout(sdev->request_queue,
1441 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1448 * The RAID firmware may require extended timeouts.
1450 blk_queue_rq_timeout(sdev->request_queue,
1451 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1455 static int megasas_slave_alloc(struct scsi_device *sdev)
1458 struct megasas_instance *instance ;
1459 instance = megasas_lookup_instance(sdev->host->host_no);
1460 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1461 (sdev->type == TYPE_DISK)) {
1463 * Open the OS scan to the SYSTEM PD
1466 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1468 if ((instance->pd_list[pd_index].driveState ==
1469 MR_PD_STATE_SYSTEM) &&
1470 (instance->pd_list[pd_index].driveType ==
1479 static void megaraid_sas_kill_hba(struct megasas_instance *instance)
1481 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1482 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1483 writel(MFI_STOP_ADP,
1484 &instance->reg_set->reserved_0[0]);
1486 writel(MFI_STOP_ADP,
1487 &instance->reg_set->inbound_doorbell);
1492 * megasas_complete_cmd_dpc - Returns FW's controller structure
1493 * @instance_addr: Address of adapter soft state
1495 * Tasklet to complete cmds
1497 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1502 struct megasas_cmd *cmd;
1503 struct megasas_instance *instance =
1504 (struct megasas_instance *)instance_addr;
1505 unsigned long flags;
1507 /* If we have already declared adapter dead, donot complete cmds */
1508 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1511 spin_lock_irqsave(&instance->completion_lock, flags);
1513 producer = *instance->producer;
1514 consumer = *instance->consumer;
1516 while (consumer != producer) {
1517 context = instance->reply_queue[consumer];
1518 if (context >= instance->max_fw_cmds) {
1519 printk(KERN_ERR "Unexpected context value %x\n",
1524 cmd = instance->cmd_list[context];
1526 megasas_complete_cmd(instance, cmd, DID_OK);
1529 if (consumer == (instance->max_fw_cmds + 1)) {
1534 *instance->consumer = producer;
1536 spin_unlock_irqrestore(&instance->completion_lock, flags);
1539 * Check if we can restore can_queue
1541 if (instance->flag & MEGASAS_FW_BUSY
1542 && time_after(jiffies, instance->last_time + 5 * HZ)
1543 && atomic_read(&instance->fw_outstanding) < 17) {
1545 spin_lock_irqsave(instance->host->host_lock, flags);
1546 instance->flag &= ~MEGASAS_FW_BUSY;
1547 if ((instance->pdev->device ==
1548 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1549 (instance->pdev->device ==
1550 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1551 instance->host->can_queue =
1552 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1554 instance->host->can_queue =
1555 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1557 spin_unlock_irqrestore(instance->host->host_lock, flags);
1562 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1563 * @instance: Adapter soft state
1565 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1566 * complete all its outstanding commands. Returns error if one or more IOs
1567 * are pending after this time period. It also marks the controller dead.
1569 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1573 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1575 unsigned long flags;
1576 struct list_head clist_local;
1577 struct megasas_cmd *reset_cmd;
1579 spin_lock_irqsave(&instance->hba_lock, flags);
1580 adprecovery = instance->adprecovery;
1581 spin_unlock_irqrestore(&instance->hba_lock, flags);
1583 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1585 INIT_LIST_HEAD(&clist_local);
1586 spin_lock_irqsave(&instance->hba_lock, flags);
1587 list_splice_init(&instance->internal_reset_pending_q,
1589 spin_unlock_irqrestore(&instance->hba_lock, flags);
1591 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1592 for (i = 0; i < wait_time; i++) {
1594 spin_lock_irqsave(&instance->hba_lock, flags);
1595 adprecovery = instance->adprecovery;
1596 spin_unlock_irqrestore(&instance->hba_lock, flags);
1597 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1601 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1602 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1603 spin_lock_irqsave(&instance->hba_lock, flags);
1604 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1605 spin_unlock_irqrestore(&instance->hba_lock, flags);
1610 while (!list_empty(&clist_local)) {
1611 reset_cmd = list_entry((&clist_local)->next,
1612 struct megasas_cmd, list);
1613 list_del_init(&reset_cmd->list);
1614 if (reset_cmd->scmd) {
1615 reset_cmd->scmd->result = DID_RESET << 16;
1616 printk(KERN_NOTICE "%d:%p reset [%02x], %#lx\n",
1617 reset_index, reset_cmd,
1618 reset_cmd->scmd->cmnd[0],
1619 reset_cmd->scmd->serial_number);
1621 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1622 megasas_return_cmd(instance, reset_cmd);
1623 } else if (reset_cmd->sync_cmd) {
1624 printk(KERN_NOTICE "megasas:%p synch cmds"
1628 reset_cmd->cmd_status = ENODATA;
1629 instance->instancet->fire_cmd(instance,
1630 reset_cmd->frame_phys_addr,
1631 0, instance->reg_set);
1633 printk(KERN_NOTICE "megasas: %p unexpected"
1643 for (i = 0; i < wait_time; i++) {
1645 int outstanding = atomic_read(&instance->fw_outstanding);
1650 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1651 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1652 "commands to complete\n",i,outstanding);
1654 * Call cmd completion routine. Cmd to be
1655 * be completed directly without depending on isr.
1657 megasas_complete_cmd_dpc((unsigned long)instance);
1663 if (atomic_read(&instance->fw_outstanding)) {
1664 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
1666 * Send signal to FW to stop processing any pending cmds.
1667 * The controller will be taken offline by the OS now.
1669 if ((instance->pdev->device ==
1670 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1671 (instance->pdev->device ==
1672 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1673 writel(MFI_STOP_ADP,
1674 &instance->reg_set->reserved_0[0]);
1676 writel(MFI_STOP_ADP,
1677 &instance->reg_set->inbound_doorbell);
1679 megasas_dump_pending_frames(instance);
1680 spin_lock_irqsave(&instance->hba_lock, flags);
1681 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1682 spin_unlock_irqrestore(&instance->hba_lock, flags);
1686 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1692 * megasas_generic_reset - Generic reset routine
1693 * @scmd: Mid-layer SCSI command
1695 * This routine implements a generic reset handler for device, bus and host
1696 * reset requests. Device, bus and host specific reset handlers can use this
1697 * function after they do their specific tasks.
1699 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1702 struct megasas_instance *instance;
1704 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1706 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1707 scmd->serial_number, scmd->cmnd[0], scmd->retries);
1709 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1710 printk(KERN_ERR "megasas: cannot recover from previous reset "
1715 ret_val = megasas_wait_for_outstanding(instance);
1716 if (ret_val == SUCCESS)
1717 printk(KERN_NOTICE "megasas: reset successful \n");
1719 printk(KERN_ERR "megasas: failed to do reset\n");
1725 * megasas_reset_timer - quiesce the adapter if required
1728 * Sets the FW busy flag and reduces the host->can_queue if the
1729 * cmd has not been completed within the timeout period.
1732 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1734 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1735 struct megasas_instance *instance;
1736 unsigned long flags;
1738 if (time_after(jiffies, scmd->jiffies_at_alloc +
1739 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1740 return BLK_EH_NOT_HANDLED;
1743 instance = cmd->instance;
1744 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1745 /* FW is busy, throttle IO */
1746 spin_lock_irqsave(instance->host->host_lock, flags);
1748 instance->host->can_queue = 16;
1749 instance->last_time = jiffies;
1750 instance->flag |= MEGASAS_FW_BUSY;
1752 spin_unlock_irqrestore(instance->host->host_lock, flags);
1754 return BLK_EH_RESET_TIMER;
1758 * megasas_reset_device - Device reset handler entry point
1760 static int megasas_reset_device(struct scsi_cmnd *scmd)
1765 * First wait for all commands to complete
1767 ret = megasas_generic_reset(scmd);
1773 * megasas_reset_bus_host - Bus & host reset handler entry point
1775 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1780 * First wait for all commands to complete
1782 ret = megasas_generic_reset(scmd);
1788 * megasas_bios_param - Returns disk geometry for a disk
1789 * @sdev: device handle
1790 * @bdev: block device
1791 * @capacity: drive capacity
1792 * @geom: geometry parameters
1795 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1796 sector_t capacity, int geom[])
1802 /* Default heads (64) & sectors (32) */
1806 tmp = heads * sectors;
1807 cylinders = capacity;
1809 sector_div(cylinders, tmp);
1812 * Handle extended translation size for logical drives > 1Gb
1815 if (capacity >= 0x200000) {
1818 tmp = heads*sectors;
1819 cylinders = capacity;
1820 sector_div(cylinders, tmp);
1825 geom[2] = cylinders;
1830 static void megasas_aen_polling(struct work_struct *work);
1833 * megasas_service_aen - Processes an event notification
1834 * @instance: Adapter soft state
1835 * @cmd: AEN command completed by the ISR
1837 * For AEN, driver sends a command down to FW that is held by the FW till an
1838 * event occurs. When an event of interest occurs, FW completes the command
1839 * that it was previously holding.
1841 * This routines sends SIGIO signal to processes that have registered with the
1845 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1847 unsigned long flags;
1849 * Don't signal app if it is just an aborted previously registered aen
1851 if ((!cmd->abort_aen) && (instance->unload == 0)) {
1852 spin_lock_irqsave(&poll_aen_lock, flags);
1853 megasas_poll_wait_aen = 1;
1854 spin_unlock_irqrestore(&poll_aen_lock, flags);
1855 wake_up(&megasas_poll_wait);
1856 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1861 instance->aen_cmd = NULL;
1862 megasas_return_cmd(instance, cmd);
1864 if ((instance->unload == 0) &&
1865 ((instance->issuepend_done == 1))) {
1866 struct megasas_aen_event *ev;
1867 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
1869 printk(KERN_ERR "megasas_service_aen: out of memory\n");
1871 ev->instance = instance;
1873 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
1874 schedule_delayed_work(
1875 (struct delayed_work *)&ev->hotplug_work, 0);
1881 * Scsi host template for megaraid_sas driver
1883 static struct scsi_host_template megasas_template = {
1885 .module = THIS_MODULE,
1886 .name = "LSI SAS based MegaRAID driver",
1887 .proc_name = "megaraid_sas",
1888 .slave_configure = megasas_slave_configure,
1889 .slave_alloc = megasas_slave_alloc,
1890 .queuecommand = megasas_queue_command,
1891 .eh_device_reset_handler = megasas_reset_device,
1892 .eh_bus_reset_handler = megasas_reset_bus_host,
1893 .eh_host_reset_handler = megasas_reset_bus_host,
1894 .eh_timed_out = megasas_reset_timer,
1895 .bios_param = megasas_bios_param,
1896 .use_clustering = ENABLE_CLUSTERING,
1900 * megasas_complete_int_cmd - Completes an internal command
1901 * @instance: Adapter soft state
1902 * @cmd: Command to be completed
1904 * The megasas_issue_blocked_cmd() function waits for a command to complete
1905 * after it issues a command. This function wakes up that waiting routine by
1906 * calling wake_up() on the wait queue.
1909 megasas_complete_int_cmd(struct megasas_instance *instance,
1910 struct megasas_cmd *cmd)
1912 cmd->cmd_status = cmd->frame->io.cmd_status;
1914 if (cmd->cmd_status == ENODATA) {
1915 cmd->cmd_status = 0;
1917 wake_up(&instance->int_cmd_wait_q);
1921 * megasas_complete_abort - Completes aborting a command
1922 * @instance: Adapter soft state
1923 * @cmd: Cmd that was issued to abort another cmd
1925 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1926 * after it issues an abort on a previously issued command. This function
1927 * wakes up all functions waiting on the same wait queue.
1930 megasas_complete_abort(struct megasas_instance *instance,
1931 struct megasas_cmd *cmd)
1933 if (cmd->sync_cmd) {
1935 cmd->cmd_status = 0;
1936 wake_up(&instance->abort_cmd_wait_q);
1943 * megasas_complete_cmd - Completes a command
1944 * @instance: Adapter soft state
1945 * @cmd: Command to be completed
1946 * @alt_status: If non-zero, use this value as status to
1947 * SCSI mid-layer instead of the value returned
1948 * by the FW. This should be used if caller wants
1949 * an alternate status (as in the case of aborted
1953 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1957 struct megasas_header *hdr = &cmd->frame->hdr;
1958 unsigned long flags;
1960 /* flag for the retry reset */
1961 cmd->retry_for_fw_reset = 0;
1964 cmd->scmd->SCp.ptr = NULL;
1968 case MFI_CMD_PD_SCSI_IO:
1969 case MFI_CMD_LD_SCSI_IO:
1972 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1973 * issued either through an IO path or an IOCTL path. If it
1974 * was via IOCTL, we will send it to internal completion.
1976 if (cmd->sync_cmd) {
1978 megasas_complete_int_cmd(instance, cmd);
1982 case MFI_CMD_LD_READ:
1983 case MFI_CMD_LD_WRITE:
1986 cmd->scmd->result = alt_status << 16;
1992 atomic_dec(&instance->fw_outstanding);
1994 scsi_dma_unmap(cmd->scmd);
1995 cmd->scmd->scsi_done(cmd->scmd);
1996 megasas_return_cmd(instance, cmd);
2001 switch (hdr->cmd_status) {
2004 cmd->scmd->result = DID_OK << 16;
2007 case MFI_STAT_SCSI_IO_FAILED:
2008 case MFI_STAT_LD_INIT_IN_PROGRESS:
2010 (DID_ERROR << 16) | hdr->scsi_status;
2013 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2015 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2017 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2018 memset(cmd->scmd->sense_buffer, 0,
2019 SCSI_SENSE_BUFFERSIZE);
2020 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2023 cmd->scmd->result |= DRIVER_SENSE << 24;
2028 case MFI_STAT_LD_OFFLINE:
2029 case MFI_STAT_DEVICE_NOT_FOUND:
2030 cmd->scmd->result = DID_BAD_TARGET << 16;
2034 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2036 cmd->scmd->result = DID_ERROR << 16;
2040 atomic_dec(&instance->fw_outstanding);
2042 scsi_dma_unmap(cmd->scmd);
2043 cmd->scmd->scsi_done(cmd->scmd);
2044 megasas_return_cmd(instance, cmd);
2051 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2052 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2053 spin_lock_irqsave(&poll_aen_lock, flags);
2054 megasas_poll_wait_aen = 0;
2055 spin_unlock_irqrestore(&poll_aen_lock, flags);
2059 * See if got an event notification
2061 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2062 megasas_service_aen(instance, cmd);
2064 megasas_complete_int_cmd(instance, cmd);
2070 * Cmd issued to abort another cmd returned
2072 megasas_complete_abort(instance, cmd);
2076 printk("megasas: Unknown command completed! [0x%X]\n",
2083 * megasas_issue_pending_cmds_again - issue all pending cmds
2084 * in FW again because of the fw reset
2085 * @instance: Adapter soft state
2088 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2090 struct megasas_cmd *cmd;
2091 struct list_head clist_local;
2092 union megasas_evt_class_locale class_locale;
2093 unsigned long flags;
2096 INIT_LIST_HEAD(&clist_local);
2097 spin_lock_irqsave(&instance->hba_lock, flags);
2098 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2099 spin_unlock_irqrestore(&instance->hba_lock, flags);
2101 while (!list_empty(&clist_local)) {
2102 cmd = list_entry((&clist_local)->next,
2103 struct megasas_cmd, list);
2104 list_del_init(&cmd->list);
2106 if (cmd->sync_cmd || cmd->scmd) {
2107 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2108 "detected to be pending while HBA reset.\n",
2109 cmd, cmd->scmd, cmd->sync_cmd);
2111 cmd->retry_for_fw_reset++;
2113 if (cmd->retry_for_fw_reset == 3) {
2114 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2115 "was tried multiple times during reset."
2116 "Shutting down the HBA\n",
2117 cmd, cmd->scmd, cmd->sync_cmd);
2118 megaraid_sas_kill_hba(instance);
2120 instance->adprecovery =
2121 MEGASAS_HW_CRITICAL_ERROR;
2126 if (cmd->sync_cmd == 1) {
2128 printk(KERN_NOTICE "megaraid_sas: unexpected"
2129 "cmd attached to internal command!\n");
2131 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2132 "on the internal reset queue,"
2133 "issue it again.\n", cmd);
2134 cmd->cmd_status = ENODATA;
2135 instance->instancet->fire_cmd(instance,
2136 cmd->frame_phys_addr ,
2137 0, instance->reg_set);
2138 } else if (cmd->scmd) {
2139 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x],%#lx"
2140 "detected on the internal queue, issue again.\n",
2141 cmd, cmd->scmd->cmnd[0], cmd->scmd->serial_number);
2143 atomic_inc(&instance->fw_outstanding);
2144 instance->instancet->fire_cmd(instance,
2145 cmd->frame_phys_addr,
2146 cmd->frame_count-1, instance->reg_set);
2148 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2149 "internal reset defer list while re-issue!!\n",
2154 if (instance->aen_cmd) {
2155 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2156 megasas_return_cmd(instance, instance->aen_cmd);
2158 instance->aen_cmd = NULL;
2162 * Initiate AEN (Asynchronous Event Notification)
2164 seq_num = instance->last_seq_num;
2165 class_locale.members.reserved = 0;
2166 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2167 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2169 megasas_register_aen(instance, seq_num, class_locale.word);
2173 * Move the internal reset pending commands to a deferred queue.
2175 * We move the commands pending at internal reset time to a
2176 * pending queue. This queue would be flushed after successful
2177 * completion of the internal reset sequence. if the internal reset
2178 * did not complete in time, the kernel reset handler would flush
2182 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2184 struct megasas_cmd *cmd;
2186 u32 max_cmd = instance->max_fw_cmds;
2188 unsigned long flags;
2191 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2192 for (i = 0; i < max_cmd; i++) {
2193 cmd = instance->cmd_list[i];
2194 if (cmd->sync_cmd == 1 || cmd->scmd) {
2195 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2196 "on the defer queue as internal\n",
2197 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2199 if (!list_empty(&cmd->list)) {
2200 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2201 " moving this cmd:%p, %d %p, it was"
2202 "discovered on some list?\n",
2203 cmd, cmd->sync_cmd, cmd->scmd);
2205 list_del_init(&cmd->list);
2208 list_add_tail(&cmd->list,
2209 &instance->internal_reset_pending_q);
2212 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2217 process_fw_state_change_wq(struct work_struct *work)
2219 struct megasas_instance *instance =
2220 container_of(work, struct megasas_instance, work_init);
2222 unsigned long flags;
2224 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2225 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2226 instance->adprecovery);
2230 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2231 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2232 "state, restarting it...\n");
2234 instance->instancet->disable_intr(instance->reg_set);
2235 atomic_set(&instance->fw_outstanding, 0);
2237 atomic_set(&instance->fw_reset_no_pci_access, 1);
2238 instance->instancet->adp_reset(instance, instance->reg_set);
2239 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2241 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2242 "initiating next stage...\n");
2244 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2245 "state 2 starting...\n");
2247 /*waitting for about 20 second before start the second init*/
2248 for (wait = 0; wait < 30; wait++) {
2252 if (megasas_transition_to_ready(instance)) {
2253 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2255 megaraid_sas_kill_hba(instance);
2256 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2260 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2261 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2262 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2264 *instance->consumer = *instance->producer;
2266 *instance->consumer = 0;
2267 *instance->producer = 0;
2270 megasas_issue_init_mfi(instance);
2272 spin_lock_irqsave(&instance->hba_lock, flags);
2273 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2274 spin_unlock_irqrestore(&instance->hba_lock, flags);
2275 instance->instancet->enable_intr(instance->reg_set);
2277 megasas_issue_pending_cmds_again(instance);
2278 instance->issuepend_done = 1;
2284 * megasas_deplete_reply_queue - Processes all completed commands
2285 * @instance: Adapter soft state
2286 * @alt_status: Alternate status to be returned to
2287 * SCSI mid-layer instead of the status
2288 * returned by the FW
2289 * Note: this must be called with hba lock held
2292 megasas_deplete_reply_queue(struct megasas_instance *instance,
2298 if ((mfiStatus = instance->instancet->check_reset(instance,
2299 instance->reg_set)) == 1) {
2303 if ((mfiStatus = instance->instancet->clear_intr(
2309 instance->mfiStatus = mfiStatus;
2311 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2312 fw_state = instance->instancet->read_fw_status_reg(
2313 instance->reg_set) & MFI_STATE_MASK;
2315 if (fw_state != MFI_STATE_FAULT) {
2316 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2320 if ((fw_state == MFI_STATE_FAULT) &&
2321 (instance->disableOnlineCtrlReset == 0)) {
2322 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2324 if ((instance->pdev->device ==
2325 PCI_DEVICE_ID_LSI_SAS1064R) ||
2326 (instance->pdev->device ==
2327 PCI_DEVICE_ID_DELL_PERC5) ||
2328 (instance->pdev->device ==
2329 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2331 *instance->consumer =
2332 MEGASAS_ADPRESET_INPROG_SIGN;
2336 instance->instancet->disable_intr(instance->reg_set);
2337 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2338 instance->issuepend_done = 0;
2340 atomic_set(&instance->fw_outstanding, 0);
2341 megasas_internal_reset_defer_cmds(instance);
2343 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2344 fw_state, instance->adprecovery);
2346 schedule_work(&instance->work_init);
2350 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2351 fw_state, instance->disableOnlineCtrlReset);
2355 tasklet_schedule(&instance->isr_tasklet);
2359 * megasas_isr - isr entry point
2361 static irqreturn_t megasas_isr(int irq, void *devp)
2363 struct megasas_instance *instance;
2364 unsigned long flags;
2368 &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
2371 instance = (struct megasas_instance *)devp;
2373 spin_lock_irqsave(&instance->hba_lock, flags);
2374 rc = megasas_deplete_reply_queue(instance, DID_OK);
2375 spin_unlock_irqrestore(&instance->hba_lock, flags);
2381 * megasas_transition_to_ready - Move the FW to READY state
2382 * @instance: Adapter soft state
2384 * During the initialization, FW passes can potentially be in any one of
2385 * several possible states. If the FW in operational, waiting-for-handshake
2386 * states, driver must take steps to bring it to ready state. Otherwise, it
2387 * has to wait for the ready state.
2390 megasas_transition_to_ready(struct megasas_instance* instance)
2396 u32 abs_state, curr_abs_state;
2398 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2400 if (fw_state != MFI_STATE_READY)
2401 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2404 while (fw_state != MFI_STATE_READY) {
2407 instance->instancet->read_fw_status_reg(instance->reg_set);
2411 case MFI_STATE_FAULT:
2413 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
2416 case MFI_STATE_WAIT_HANDSHAKE:
2418 * Set the CLR bit in inbound doorbell
2420 if ((instance->pdev->device ==
2421 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2422 (instance->pdev->device ==
2423 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2426 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2427 &instance->reg_set->reserved_0[0]);
2430 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2431 &instance->reg_set->inbound_doorbell);
2434 max_wait = MEGASAS_RESET_WAIT_TIME;
2435 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2438 case MFI_STATE_BOOT_MESSAGE_PENDING:
2439 if ((instance->pdev->device ==
2440 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2441 (instance->pdev->device ==
2442 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2443 writel(MFI_INIT_HOTPLUG,
2444 &instance->reg_set->reserved_0[0]);
2446 writel(MFI_INIT_HOTPLUG,
2447 &instance->reg_set->inbound_doorbell);
2449 max_wait = MEGASAS_RESET_WAIT_TIME;
2450 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2453 case MFI_STATE_OPERATIONAL:
2455 * Bring it to READY state; assuming max wait 10 secs
2457 instance->instancet->disable_intr(instance->reg_set);
2458 if ((instance->pdev->device ==
2459 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2460 (instance->pdev->device ==
2461 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2462 writel(MFI_RESET_FLAGS,
2463 &instance->reg_set->reserved_0[0]);
2465 writel(MFI_RESET_FLAGS,
2466 &instance->reg_set->inbound_doorbell);
2468 max_wait = MEGASAS_RESET_WAIT_TIME;
2469 cur_state = MFI_STATE_OPERATIONAL;
2472 case MFI_STATE_UNDEFINED:
2474 * This state should not last for more than 2 seconds
2476 max_wait = MEGASAS_RESET_WAIT_TIME;
2477 cur_state = MFI_STATE_UNDEFINED;
2480 case MFI_STATE_BB_INIT:
2481 max_wait = MEGASAS_RESET_WAIT_TIME;
2482 cur_state = MFI_STATE_BB_INIT;
2485 case MFI_STATE_FW_INIT:
2486 max_wait = MEGASAS_RESET_WAIT_TIME;
2487 cur_state = MFI_STATE_FW_INIT;
2490 case MFI_STATE_FW_INIT_2:
2491 max_wait = MEGASAS_RESET_WAIT_TIME;
2492 cur_state = MFI_STATE_FW_INIT_2;
2495 case MFI_STATE_DEVICE_SCAN:
2496 max_wait = MEGASAS_RESET_WAIT_TIME;
2497 cur_state = MFI_STATE_DEVICE_SCAN;
2500 case MFI_STATE_FLUSH_CACHE:
2501 max_wait = MEGASAS_RESET_WAIT_TIME;
2502 cur_state = MFI_STATE_FLUSH_CACHE;
2506 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2512 * The cur_state should not last for more than max_wait secs
2514 for (i = 0; i < (max_wait * 1000); i++) {
2515 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
2518 instance->instancet->read_fw_status_reg(instance->reg_set);
2520 if (abs_state == curr_abs_state) {
2527 * Return error if fw_state hasn't changed after max_wait
2529 if (curr_abs_state == abs_state) {
2530 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2531 "in %d secs\n", fw_state, max_wait);
2535 printk(KERN_INFO "megasas: FW now in Ready state\n");
2541 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2542 * @instance: Adapter soft state
2544 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2547 u32 max_cmd = instance->max_fw_cmds;
2548 struct megasas_cmd *cmd;
2550 if (!instance->frame_dma_pool)
2554 * Return all frames to pool
2556 for (i = 0; i < max_cmd; i++) {
2558 cmd = instance->cmd_list[i];
2561 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2562 cmd->frame_phys_addr);
2565 pci_pool_free(instance->sense_dma_pool, cmd->sense,
2566 cmd->sense_phys_addr);
2570 * Now destroy the pool itself
2572 pci_pool_destroy(instance->frame_dma_pool);
2573 pci_pool_destroy(instance->sense_dma_pool);
2575 instance->frame_dma_pool = NULL;
2576 instance->sense_dma_pool = NULL;
2580 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2581 * @instance: Adapter soft state
2583 * Each command packet has an embedded DMA memory buffer that is used for
2584 * filling MFI frame and the SG list that immediately follows the frame. This
2585 * function creates those DMA memory buffers for each command packet by using
2586 * PCI pool facility.
2588 static int megasas_create_frame_pool(struct megasas_instance *instance)
2596 struct megasas_cmd *cmd;
2598 max_cmd = instance->max_fw_cmds;
2601 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2602 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2604 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2605 sizeof(struct megasas_sge32);
2607 if (instance->flag_ieee) {
2608 sge_sz = sizeof(struct megasas_sge_skinny);
2612 * Calculated the number of 64byte frames required for SGL
2614 sgl_sz = sge_sz * instance->max_num_sge;
2615 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2619 * We need one extra frame for the MFI command
2623 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2625 * Use DMA pool facility provided by PCI layer
2627 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2628 instance->pdev, total_sz, 64,
2631 if (!instance->frame_dma_pool) {
2632 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2636 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2637 instance->pdev, 128, 4, 0);
2639 if (!instance->sense_dma_pool) {
2640 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2642 pci_pool_destroy(instance->frame_dma_pool);
2643 instance->frame_dma_pool = NULL;
2649 * Allocate and attach a frame to each of the commands in cmd_list.
2650 * By making cmd->index as the context instead of the &cmd, we can
2651 * always use 32bit context regardless of the architecture
2653 for (i = 0; i < max_cmd; i++) {
2655 cmd = instance->cmd_list[i];
2657 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2658 GFP_KERNEL, &cmd->frame_phys_addr);
2660 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2661 GFP_KERNEL, &cmd->sense_phys_addr);
2664 * megasas_teardown_frame_pool() takes care of freeing
2665 * whatever has been allocated
2667 if (!cmd->frame || !cmd->sense) {
2668 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2669 megasas_teardown_frame_pool(instance);
2673 cmd->frame->io.context = cmd->index;
2674 cmd->frame->io.pad_0 = 0;
2681 * megasas_free_cmds - Free all the cmds in the free cmd pool
2682 * @instance: Adapter soft state
2684 static void megasas_free_cmds(struct megasas_instance *instance)
2687 /* First free the MFI frame pool */
2688 megasas_teardown_frame_pool(instance);
2690 /* Free all the commands in the cmd_list */
2691 for (i = 0; i < instance->max_fw_cmds; i++)
2692 kfree(instance->cmd_list[i]);
2694 /* Free the cmd_list buffer itself */
2695 kfree(instance->cmd_list);
2696 instance->cmd_list = NULL;
2698 INIT_LIST_HEAD(&instance->cmd_pool);
2702 * megasas_alloc_cmds - Allocates the command packets
2703 * @instance: Adapter soft state
2705 * Each command that is issued to the FW, whether IO commands from the OS or
2706 * internal commands like IOCTLs, are wrapped in local data structure called
2707 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2710 * Each frame has a 32-bit field called context (tag). This context is used
2711 * to get back the megasas_cmd from the frame when a frame gets completed in
2712 * the ISR. Typically the address of the megasas_cmd itself would be used as
2713 * the context. But we wanted to keep the differences between 32 and 64 bit
2714 * systems to the mininum. We always use 32 bit integers for the context. In
2715 * this driver, the 32 bit values are the indices into an array cmd_list.
2716 * This array is used only to look up the megasas_cmd given the context. The
2717 * free commands themselves are maintained in a linked list called cmd_pool.
2719 static int megasas_alloc_cmds(struct megasas_instance *instance)
2724 struct megasas_cmd *cmd;
2726 max_cmd = instance->max_fw_cmds;
2729 * instance->cmd_list is an array of struct megasas_cmd pointers.
2730 * Allocate the dynamic array first and then allocate individual
2733 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
2735 if (!instance->cmd_list) {
2736 printk(KERN_DEBUG "megasas: out of memory\n");
2741 for (i = 0; i < max_cmd; i++) {
2742 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2745 if (!instance->cmd_list[i]) {
2747 for (j = 0; j < i; j++)
2748 kfree(instance->cmd_list[j]);
2750 kfree(instance->cmd_list);
2751 instance->cmd_list = NULL;
2758 * Add all the commands to command pool (instance->cmd_pool)
2760 for (i = 0; i < max_cmd; i++) {
2761 cmd = instance->cmd_list[i];
2762 memset(cmd, 0, sizeof(struct megasas_cmd));
2765 cmd->instance = instance;
2767 list_add_tail(&cmd->list, &instance->cmd_pool);
2771 * Create a frame pool and assign one frame to each cmd
2773 if (megasas_create_frame_pool(instance)) {
2774 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2775 megasas_free_cmds(instance);
2782 * megasas_get_pd_list_info - Returns FW's pd_list structure
2783 * @instance: Adapter soft state
2784 * @pd_list: pd_list structure
2786 * Issues an internal command (DCMD) to get the FW's controller PD
2787 * list structure. This information is mainly used to find out SYSTEM
2788 * supported by the FW.
2791 megasas_get_pd_list(struct megasas_instance *instance)
2793 int ret = 0, pd_index = 0;
2794 struct megasas_cmd *cmd;
2795 struct megasas_dcmd_frame *dcmd;
2796 struct MR_PD_LIST *ci;
2797 struct MR_PD_ADDRESS *pd_addr;
2798 dma_addr_t ci_h = 0;
2800 cmd = megasas_get_cmd(instance);
2803 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
2807 dcmd = &cmd->frame->dcmd;
2809 ci = pci_alloc_consistent(instance->pdev,
2810 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
2813 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
2814 megasas_return_cmd(instance, cmd);
2818 memset(ci, 0, sizeof(*ci));
2819 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2821 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
2822 dcmd->mbox.b[1] = 0;
2823 dcmd->cmd = MFI_CMD_DCMD;
2824 dcmd->cmd_status = 0xFF;
2825 dcmd->sge_count = 1;
2826 dcmd->flags = MFI_FRAME_DIR_READ;
2829 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2830 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
2831 dcmd->sgl.sge32[0].phys_addr = ci_h;
2832 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2834 if (!megasas_issue_polled(instance, cmd)) {
2841 * the following function will get the instance PD LIST.
2848 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
2850 memset(instance->pd_list, 0,
2851 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
2853 for (pd_index = 0; pd_index < ci->count; pd_index++) {
2855 instance->pd_list[pd_addr->deviceId].tid =
2857 instance->pd_list[pd_addr->deviceId].driveType =
2858 pd_addr->scsiDevType;
2859 instance->pd_list[pd_addr->deviceId].driveState =
2865 pci_free_consistent(instance->pdev,
2866 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
2868 megasas_return_cmd(instance, cmd);
2874 * megasas_get_ld_list_info - Returns FW's ld_list structure
2875 * @instance: Adapter soft state
2876 * @ld_list: ld_list structure
2878 * Issues an internal command (DCMD) to get the FW's controller PD
2879 * list structure. This information is mainly used to find out SYSTEM
2880 * supported by the FW.
2883 megasas_get_ld_list(struct megasas_instance *instance)
2885 int ret = 0, ld_index = 0, ids = 0;
2886 struct megasas_cmd *cmd;
2887 struct megasas_dcmd_frame *dcmd;
2888 struct MR_LD_LIST *ci;
2889 dma_addr_t ci_h = 0;
2891 cmd = megasas_get_cmd(instance);
2894 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
2898 dcmd = &cmd->frame->dcmd;
2900 ci = pci_alloc_consistent(instance->pdev,
2901 sizeof(struct MR_LD_LIST),
2905 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
2906 megasas_return_cmd(instance, cmd);
2910 memset(ci, 0, sizeof(*ci));
2911 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2913 dcmd->cmd = MFI_CMD_DCMD;
2914 dcmd->cmd_status = 0xFF;
2915 dcmd->sge_count = 1;
2916 dcmd->flags = MFI_FRAME_DIR_READ;
2918 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
2919 dcmd->opcode = MR_DCMD_LD_GET_LIST;
2920 dcmd->sgl.sge32[0].phys_addr = ci_h;
2921 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
2924 if (!megasas_issue_polled(instance, cmd)) {
2930 /* the following function will get the instance PD LIST */
2932 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
2933 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2935 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
2936 if (ci->ldList[ld_index].state != 0) {
2937 ids = ci->ldList[ld_index].ref.targetId;
2938 instance->ld_ids[ids] =
2939 ci->ldList[ld_index].ref.targetId;
2944 pci_free_consistent(instance->pdev,
2945 sizeof(struct MR_LD_LIST),
2949 megasas_return_cmd(instance, cmd);
2954 * megasas_get_controller_info - Returns FW's controller structure
2955 * @instance: Adapter soft state
2956 * @ctrl_info: Controller information structure
2958 * Issues an internal command (DCMD) to get the FW's controller structure.
2959 * This information is mainly used to find out the maximum IO transfer per
2960 * command supported by the FW.
2963 megasas_get_ctrl_info(struct megasas_instance *instance,
2964 struct megasas_ctrl_info *ctrl_info)
2967 struct megasas_cmd *cmd;
2968 struct megasas_dcmd_frame *dcmd;
2969 struct megasas_ctrl_info *ci;
2970 dma_addr_t ci_h = 0;
2972 cmd = megasas_get_cmd(instance);
2975 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
2979 dcmd = &cmd->frame->dcmd;
2981 ci = pci_alloc_consistent(instance->pdev,
2982 sizeof(struct megasas_ctrl_info), &ci_h);
2985 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
2986 megasas_return_cmd(instance, cmd);
2990 memset(ci, 0, sizeof(*ci));
2991 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2993 dcmd->cmd = MFI_CMD_DCMD;
2994 dcmd->cmd_status = 0xFF;
2995 dcmd->sge_count = 1;
2996 dcmd->flags = MFI_FRAME_DIR_READ;
2999 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3000 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3001 dcmd->sgl.sge32[0].phys_addr = ci_h;
3002 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3004 if (!megasas_issue_polled(instance, cmd)) {
3006 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3011 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3014 megasas_return_cmd(instance, cmd);
3019 * megasas_issue_init_mfi - Initializes the FW
3020 * @instance: Adapter soft state
3022 * Issues the INIT MFI cmd
3025 megasas_issue_init_mfi(struct megasas_instance *instance)
3029 struct megasas_cmd *cmd;
3031 struct megasas_init_frame *init_frame;
3032 struct megasas_init_queue_info *initq_info;
3033 dma_addr_t init_frame_h;
3034 dma_addr_t initq_info_h;
3037 * Prepare a init frame. Note the init frame points to queue info
3038 * structure. Each frame has SGL allocated after first 64 bytes. For
3039 * this frame - since we don't need any SGL - we use SGL's space as
3040 * queue info structure
3042 * We will not get a NULL command below. We just created the pool.
3044 cmd = megasas_get_cmd(instance);
3046 init_frame = (struct megasas_init_frame *)cmd->frame;
3047 initq_info = (struct megasas_init_queue_info *)
3048 ((unsigned long)init_frame + 64);
3050 init_frame_h = cmd->frame_phys_addr;
3051 initq_info_h = init_frame_h + 64;
3053 context = init_frame->context;
3054 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3055 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3056 init_frame->context = context;
3058 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3059 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3061 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3062 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3064 init_frame->cmd = MFI_CMD_INIT;
3065 init_frame->cmd_status = 0xFF;
3066 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3068 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3071 * disable the intr before firing the init frame to FW
3073 instance->instancet->disable_intr(instance->reg_set);
3076 * Issue the init frame in polled mode
3079 if (megasas_issue_polled(instance, cmd)) {
3080 printk(KERN_ERR "megasas: Failed to init firmware\n");
3081 megasas_return_cmd(instance, cmd);
3085 megasas_return_cmd(instance, cmd);
3094 * megasas_start_timer - Initializes a timer object
3095 * @instance: Adapter soft state
3096 * @timer: timer object to be initialized
3097 * @fn: timer function
3098 * @interval: time interval between timer function call
3101 megasas_start_timer(struct megasas_instance *instance,
3102 struct timer_list *timer,
3103 void *fn, unsigned long interval)
3106 timer->expires = jiffies + interval;
3107 timer->data = (unsigned long)instance;
3108 timer->function = fn;
3113 * megasas_io_completion_timer - Timer fn
3114 * @instance_addr: Address of adapter soft state
3116 * Schedules tasklet for cmd completion
3117 * if poll_mode_io is set
3120 megasas_io_completion_timer(unsigned long instance_addr)
3122 struct megasas_instance *instance =
3123 (struct megasas_instance *)instance_addr;
3125 if (atomic_read(&instance->fw_outstanding))
3126 tasklet_schedule(&instance->isr_tasklet);
3130 mod_timer(&instance->io_completion_timer,
3131 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3135 * megasas_init_mfi - Initializes the FW
3136 * @instance: Adapter soft state
3138 * This is the main function for initializing MFI firmware.
3140 static int megasas_init_mfi(struct megasas_instance *instance)
3147 struct megasas_register_set __iomem *reg_set;
3148 struct megasas_ctrl_info *ctrl_info;
3150 * Map the message registers
3152 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3153 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3154 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3155 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
3156 instance->base_addr = pci_resource_start(instance->pdev, 1);
3158 instance->base_addr = pci_resource_start(instance->pdev, 0);
3161 if (pci_request_selected_regions(instance->pdev,
3162 pci_select_bars(instance->pdev, IORESOURCE_MEM),
3164 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3168 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3170 if (!instance->reg_set) {
3171 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3175 reg_set = instance->reg_set;
3177 switch(instance->pdev->device)
3179 case PCI_DEVICE_ID_LSI_SAS1078R:
3180 case PCI_DEVICE_ID_LSI_SAS1078DE:
3181 instance->instancet = &megasas_instance_template_ppc;
3183 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3184 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3185 instance->instancet = &megasas_instance_template_gen2;
3187 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3188 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3189 instance->instancet = &megasas_instance_template_skinny;
3191 case PCI_DEVICE_ID_LSI_SAS1064R:
3192 case PCI_DEVICE_ID_DELL_PERC5:
3194 instance->instancet = &megasas_instance_template_xscale;
3199 * We expect the FW state to be READY
3201 if (megasas_transition_to_ready(instance))
3202 goto fail_ready_state;
3205 * Get various operational parameters from status register
3207 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
3209 * Reduce the max supported cmds by 1. This is to ensure that the
3210 * reply_q_sz (1 more than the max cmd that driver may send)
3211 * does not exceed max cmds that the FW can support
3213 instance->max_fw_cmds = instance->max_fw_cmds-1;
3214 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
3217 * Create a pool of commands
3219 if (megasas_alloc_cmds(instance))
3220 goto fail_alloc_cmds;
3223 * Allocate memory for reply queue. Length of reply queue should
3224 * be _one_ more than the maximum commands handled by the firmware.
3226 * Note: When FW completes commands, it places corresponding contex
3227 * values in this circular reply queue. This circular queue is a fairly
3228 * typical producer-consumer queue. FW is the producer (of completed
3229 * commands) and the driver is the consumer.
3231 context_sz = sizeof(u32);
3232 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3234 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3236 &instance->reply_queue_h);
3238 if (!instance->reply_queue) {
3239 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3240 goto fail_reply_queue;
3243 if (megasas_issue_init_mfi(instance))
3246 instance->fw_support_ieee = 0;
3247 instance->fw_support_ieee =
3248 (instance->instancet->read_fw_status_reg(reg_set) &
3251 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3252 instance->fw_support_ieee);
3254 if (instance->fw_support_ieee)
3255 instance->flag_ieee = 1;
3258 * the following function will get the PD LIST.
3261 memset(instance->pd_list, 0 ,
3262 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3263 megasas_get_pd_list(instance);
3265 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3266 megasas_get_ld_list(instance);
3268 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3271 * Compute the max allowed sectors per IO: The controller info has two
3272 * limits on max sectors. Driver should use the minimum of these two.
3274 * 1 << stripe_sz_ops.min = max sectors per strip
3276 * Note that older firmwares ( < FW ver 30) didn't report information
3277 * to calculate max_sectors_1. So the number ended up as zero always.
3280 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3282 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3283 ctrl_info->max_strips_per_io;
3284 max_sectors_2 = ctrl_info->max_request_size;
3286 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
3287 instance->disableOnlineCtrlReset =
3288 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
3291 instance->max_sectors_per_req = instance->max_num_sge *
3293 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3294 instance->max_sectors_per_req = tmp_sectors;
3299 * Setup tasklet for cmd completion
3302 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3303 (unsigned long)instance);
3305 /* Initialize the cmd completion timer */
3307 megasas_start_timer(instance, &instance->io_completion_timer,
3308 megasas_io_completion_timer,
3309 MEGASAS_COMPLETION_TIMER_INTERVAL);
3314 pci_free_consistent(instance->pdev, reply_q_sz,
3315 instance->reply_queue, instance->reply_queue_h);
3317 megasas_free_cmds(instance);
3321 iounmap(instance->reg_set);
3324 pci_release_selected_regions(instance->pdev,
3325 pci_select_bars(instance->pdev, IORESOURCE_MEM));
3331 * megasas_release_mfi - Reverses the FW initialization
3332 * @intance: Adapter soft state
3334 static void megasas_release_mfi(struct megasas_instance *instance)
3336 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
3338 pci_free_consistent(instance->pdev, reply_q_sz,
3339 instance->reply_queue, instance->reply_queue_h);
3341 megasas_free_cmds(instance);
3343 iounmap(instance->reg_set);
3345 pci_release_selected_regions(instance->pdev,
3346 pci_select_bars(instance->pdev, IORESOURCE_MEM));
3350 * megasas_get_seq_num - Gets latest event sequence numbers
3351 * @instance: Adapter soft state
3352 * @eli: FW event log sequence numbers information
3354 * FW maintains a log of all events in a non-volatile area. Upper layers would
3355 * usually find out the latest sequence number of the events, the seq number at
3356 * the boot etc. They would "read" all the events below the latest seq number
3357 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3358 * number), they would subsribe to AEN (asynchronous event notification) and
3359 * wait for the events to happen.
3362 megasas_get_seq_num(struct megasas_instance *instance,
3363 struct megasas_evt_log_info *eli)
3365 struct megasas_cmd *cmd;
3366 struct megasas_dcmd_frame *dcmd;
3367 struct megasas_evt_log_info *el_info;
3368 dma_addr_t el_info_h = 0;
3370 cmd = megasas_get_cmd(instance);
3376 dcmd = &cmd->frame->dcmd;
3377 el_info = pci_alloc_consistent(instance->pdev,
3378 sizeof(struct megasas_evt_log_info),
3382 megasas_return_cmd(instance, cmd);
3386 memset(el_info, 0, sizeof(*el_info));
3387 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3389 dcmd->cmd = MFI_CMD_DCMD;
3390 dcmd->cmd_status = 0x0;
3391 dcmd->sge_count = 1;
3392 dcmd->flags = MFI_FRAME_DIR_READ;
3395 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3396 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3397 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3398 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3400 megasas_issue_blocked_cmd(instance, cmd);
3403 * Copy the data back into callers buffer
3405 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3407 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3408 el_info, el_info_h);
3410 megasas_return_cmd(instance, cmd);
3416 * megasas_register_aen - Registers for asynchronous event notification
3417 * @instance: Adapter soft state
3418 * @seq_num: The starting sequence number
3419 * @class_locale: Class of the event
3421 * This function subscribes for AEN for events beyond the @seq_num. It requests
3422 * to be notified if and only if the event is of type @class_locale
3425 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3426 u32 class_locale_word)
3429 struct megasas_cmd *cmd;
3430 struct megasas_dcmd_frame *dcmd;
3431 union megasas_evt_class_locale curr_aen;
3432 union megasas_evt_class_locale prev_aen;
3435 * If there an AEN pending already (aen_cmd), check if the
3436 * class_locale of that pending AEN is inclusive of the new
3437 * AEN request we currently have. If it is, then we don't have
3438 * to do anything. In other words, whichever events the current
3439 * AEN request is subscribing to, have already been subscribed
3442 * If the old_cmd is _not_ inclusive, then we have to abort
3443 * that command, form a class_locale that is superset of both
3444 * old and current and re-issue to the FW
3447 curr_aen.word = class_locale_word;
3449 if (instance->aen_cmd) {
3451 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3454 * A class whose enum value is smaller is inclusive of all
3455 * higher values. If a PROGRESS (= -1) was previously
3456 * registered, then a new registration requests for higher
3457 * classes need not be sent to FW. They are automatically
3460 * Locale numbers don't have such hierarchy. They are bitmap
3463 if ((prev_aen.members.class <= curr_aen.members.class) &&
3464 !((prev_aen.members.locale & curr_aen.members.locale) ^
3465 curr_aen.members.locale)) {
3467 * Previously issued event registration includes
3468 * current request. Nothing to do.
3472 curr_aen.members.locale |= prev_aen.members.locale;
3474 if (prev_aen.members.class < curr_aen.members.class)
3475 curr_aen.members.class = prev_aen.members.class;
3477 instance->aen_cmd->abort_aen = 1;
3478 ret_val = megasas_issue_blocked_abort_cmd(instance,
3483 printk(KERN_DEBUG "megasas: Failed to abort "
3484 "previous AEN command\n");
3490 cmd = megasas_get_cmd(instance);
3495 dcmd = &cmd->frame->dcmd;
3497 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3500 * Prepare DCMD for aen registration
3502 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3504 dcmd->cmd = MFI_CMD_DCMD;
3505 dcmd->cmd_status = 0x0;
3506 dcmd->sge_count = 1;
3507 dcmd->flags = MFI_FRAME_DIR_READ;
3510 instance->last_seq_num = seq_num;
3511 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3512 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3513 dcmd->mbox.w[0] = seq_num;
3514 dcmd->mbox.w[1] = curr_aen.word;
3515 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3516 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3518 if (instance->aen_cmd != NULL) {
3519 megasas_return_cmd(instance, cmd);
3524 * Store reference to the cmd used to register for AEN. When an
3525 * application wants us to register for AEN, we have to abort this
3526 * cmd and re-register with a new EVENT LOCALE supplied by that app
3528 instance->aen_cmd = cmd;
3531 * Issue the aen registration frame
3533 instance->instancet->fire_cmd(instance,
3534 cmd->frame_phys_addr, 0, instance->reg_set);
3540 * megasas_start_aen - Subscribes to AEN during driver load time
3541 * @instance: Adapter soft state
3543 static int megasas_start_aen(struct megasas_instance *instance)
3545 struct megasas_evt_log_info eli;
3546 union megasas_evt_class_locale class_locale;
3549 * Get the latest sequence number from FW
3551 memset(&eli, 0, sizeof(eli));
3553 if (megasas_get_seq_num(instance, &eli))
3557 * Register AEN with FW for latest sequence number plus 1
3559 class_locale.members.reserved = 0;
3560 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3561 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3563 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3568 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3569 * @instance: Adapter soft state
3571 static int megasas_io_attach(struct megasas_instance *instance)
3573 struct Scsi_Host *host = instance->host;
3576 * Export parameters required by SCSI mid-layer
3578 host->irq = instance->pdev->irq;
3579 host->unique_id = instance->unique_id;
3580 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3581 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3583 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3586 instance->max_fw_cmds - MEGASAS_INT_CMDS;
3587 host->this_id = instance->init_id;
3588 host->sg_tablesize = instance->max_num_sge;
3589 host->max_sectors = instance->max_sectors_per_req;
3590 host->cmd_per_lun = 128;
3591 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3592 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3593 host->max_lun = MEGASAS_MAX_LUN;
3594 host->max_cmd_len = 16;
3597 * Notify the mid-layer about the new controller
3599 if (scsi_add_host(host, &instance->pdev->dev)) {
3600 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3605 * Trigger SCSI to scan our drives
3607 scsi_scan_host(host);
3612 megasas_set_dma_mask(struct pci_dev *pdev)
3615 * All our contollers are capable of performing 64-bit DMA
3618 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
3620 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3621 goto fail_set_dma_mask;
3624 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3625 goto fail_set_dma_mask;
3634 * megasas_probe_one - PCI hotplug entry point
3635 * @pdev: PCI device structure
3636 * @id: PCI ids of supported hotplugged adapter
3638 static int __devinit
3639 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3642 struct Scsi_Host *host;
3643 struct megasas_instance *instance;
3646 * Announce PCI information
3648 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3649 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3650 pdev->subsystem_device);
3652 printk("bus %d:slot %d:func %d\n",
3653 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3656 * PCI prepping: enable device set bus mastering and dma mask
3658 rval = pci_enable_device_mem(pdev);
3664 pci_set_master(pdev);
3666 if (megasas_set_dma_mask(pdev))
3667 goto fail_set_dma_mask;
3669 host = scsi_host_alloc(&megasas_template,
3670 sizeof(struct megasas_instance));
3673 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
3674 goto fail_alloc_instance;
3677 instance = (struct megasas_instance *)host->hostdata;
3678 memset(instance, 0, sizeof(*instance));
3679 atomic_set( &instance->fw_reset_no_pci_access, 0 );
3681 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
3682 &instance->producer_h);
3683 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
3684 &instance->consumer_h);
3686 if (!instance->producer || !instance->consumer) {
3687 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3688 "producer, consumer\n");
3689 goto fail_alloc_dma_buf;
3692 *instance->producer = 0;
3693 *instance->consumer = 0;
3694 megasas_poll_wait_aen = 0;
3695 instance->flag_ieee = 0;
3696 instance->ev = NULL;
3697 instance->issuepend_done = 1;
3698 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3699 megasas_poll_wait_aen = 0;
3701 instance->evt_detail = pci_alloc_consistent(pdev,
3703 megasas_evt_detail),
3704 &instance->evt_detail_h);
3706 if (!instance->evt_detail) {
3707 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3708 "event detail structure\n");
3709 goto fail_alloc_dma_buf;
3713 * Initialize locks and queues
3715 INIT_LIST_HEAD(&instance->cmd_pool);
3716 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
3718 atomic_set(&instance->fw_outstanding,0);
3720 init_waitqueue_head(&instance->int_cmd_wait_q);
3721 init_waitqueue_head(&instance->abort_cmd_wait_q);
3723 spin_lock_init(&instance->cmd_pool_lock);
3724 spin_lock_init(&instance->hba_lock);
3725 spin_lock_init(&instance->completion_lock);
3726 spin_lock_init(&poll_aen_lock);
3728 mutex_init(&instance->aen_mutex);
3731 * Initialize PCI related and misc parameters
3733 instance->pdev = pdev;
3734 instance->host = host;
3735 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
3736 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
3738 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3739 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3740 instance->flag_ieee = 1;
3741 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
3743 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
3745 megasas_dbg_lvl = 0;
3747 instance->unload = 1;
3748 instance->last_time = 0;
3749 instance->disableOnlineCtrlReset = 1;
3751 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
3754 * Initialize MFI Firmware
3756 if (megasas_init_mfi(instance))
3762 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
3763 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
3767 instance->instancet->enable_intr(instance->reg_set);
3770 * Store instance in PCI softstate
3772 pci_set_drvdata(pdev, instance);
3775 * Add this controller to megasas_mgmt_info structure so that it
3776 * can be exported to management applications
3778 megasas_mgmt_info.count++;
3779 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
3780 megasas_mgmt_info.max_index++;
3783 * Initiate AEN (Asynchronous Event Notification)
3785 if (megasas_start_aen(instance)) {
3786 printk(KERN_DEBUG "megasas: start aen failed\n");
3787 goto fail_start_aen;
3791 * Register with SCSI mid-layer
3793 if (megasas_io_attach(instance))
3794 goto fail_io_attach;
3796 instance->unload = 0;
3801 megasas_mgmt_info.count--;
3802 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
3803 megasas_mgmt_info.max_index--;
3805 pci_set_drvdata(pdev, NULL);
3806 instance->instancet->disable_intr(instance->reg_set);
3807 free_irq(instance->pdev->irq, instance);
3809 megasas_release_mfi(instance);
3814 if (instance->evt_detail)
3815 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3816 instance->evt_detail,
3817 instance->evt_detail_h);
3819 if (instance->producer)
3820 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3821 instance->producer_h);
3822 if (instance->consumer)
3823 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3824 instance->consumer_h);
3825 scsi_host_put(host);
3827 fail_alloc_instance:
3829 pci_disable_device(pdev);
3835 * megasas_flush_cache - Requests FW to flush all its caches
3836 * @instance: Adapter soft state
3838 static void megasas_flush_cache(struct megasas_instance *instance)
3840 struct megasas_cmd *cmd;
3841 struct megasas_dcmd_frame *dcmd;
3843 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3846 cmd = megasas_get_cmd(instance);
3851 dcmd = &cmd->frame->dcmd;
3853 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3855 dcmd->cmd = MFI_CMD_DCMD;
3856 dcmd->cmd_status = 0x0;
3857 dcmd->sge_count = 0;
3858 dcmd->flags = MFI_FRAME_DIR_NONE;
3861 dcmd->data_xfer_len = 0;
3862 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
3863 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
3865 megasas_issue_blocked_cmd(instance, cmd);
3867 megasas_return_cmd(instance, cmd);
3873 * megasas_shutdown_controller - Instructs FW to shutdown the controller
3874 * @instance: Adapter soft state
3875 * @opcode: Shutdown/Hibernate
3877 static void megasas_shutdown_controller(struct megasas_instance *instance,
3880 struct megasas_cmd *cmd;
3881 struct megasas_dcmd_frame *dcmd;
3883 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3886 cmd = megasas_get_cmd(instance);
3891 if (instance->aen_cmd)
3892 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
3894 dcmd = &cmd->frame->dcmd;
3896 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3898 dcmd->cmd = MFI_CMD_DCMD;
3899 dcmd->cmd_status = 0x0;
3900 dcmd->sge_count = 0;
3901 dcmd->flags = MFI_FRAME_DIR_NONE;
3904 dcmd->data_xfer_len = 0;
3905 dcmd->opcode = opcode;
3907 megasas_issue_blocked_cmd(instance, cmd);
3909 megasas_return_cmd(instance, cmd);
3916 * megasas_suspend - driver suspend entry point
3917 * @pdev: PCI device structure
3918 * @state: PCI power state to suspend routine
3921 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
3923 struct Scsi_Host *host;
3924 struct megasas_instance *instance;
3926 instance = pci_get_drvdata(pdev);
3927 host = instance->host;
3928 instance->unload = 1;
3931 del_timer_sync(&instance->io_completion_timer);
3933 megasas_flush_cache(instance);
3934 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
3936 /* cancel the delayed work if this work still in queue */
3937 if (instance->ev != NULL) {
3938 struct megasas_aen_event *ev = instance->ev;
3939 cancel_delayed_work(
3940 (struct delayed_work *)&ev->hotplug_work);
3941 flush_scheduled_work();
3942 instance->ev = NULL;
3945 tasklet_kill(&instance->isr_tasklet);
3947 pci_set_drvdata(instance->pdev, instance);
3948 instance->instancet->disable_intr(instance->reg_set);
3949 free_irq(instance->pdev->irq, instance);
3951 pci_save_state(pdev);
3952 pci_disable_device(pdev);
3954 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3960 * megasas_resume- driver resume entry point
3961 * @pdev: PCI device structure
3964 megasas_resume(struct pci_dev *pdev)
3967 struct Scsi_Host *host;
3968 struct megasas_instance *instance;
3970 instance = pci_get_drvdata(pdev);
3971 host = instance->host;
3972 pci_set_power_state(pdev, PCI_D0);
3973 pci_enable_wake(pdev, PCI_D0, 0);
3974 pci_restore_state(pdev);
3977 * PCI prepping: enable device set bus mastering and dma mask
3979 rval = pci_enable_device_mem(pdev);
3982 printk(KERN_ERR "megasas: Enable device failed\n");
3986 pci_set_master(pdev);
3988 if (megasas_set_dma_mask(pdev))
3989 goto fail_set_dma_mask;
3992 * Initialize MFI Firmware
3995 *instance->producer = 0;
3996 *instance->consumer = 0;
3998 atomic_set(&instance->fw_outstanding, 0);
4001 * We expect the FW state to be READY
4003 if (megasas_transition_to_ready(instance))
4004 goto fail_ready_state;
4006 if (megasas_issue_init_mfi(instance))
4009 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
4010 (unsigned long)instance);
4015 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
4016 "megasas", instance)) {
4017 printk(KERN_ERR "megasas: Failed to register IRQ\n");
4021 instance->instancet->enable_intr(instance->reg_set);
4024 * Initiate AEN (Asynchronous Event Notification)
4026 if (megasas_start_aen(instance))
4027 printk(KERN_ERR "megasas: Start AEN failed\n");
4029 /* Initialize the cmd completion timer */
4031 megasas_start_timer(instance, &instance->io_completion_timer,
4032 megasas_io_completion_timer,
4033 MEGASAS_COMPLETION_TIMER_INTERVAL);
4034 instance->unload = 0;
4040 if (instance->evt_detail)
4041 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4042 instance->evt_detail,
4043 instance->evt_detail_h);
4045 if (instance->producer)
4046 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4047 instance->producer_h);
4048 if (instance->consumer)
4049 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4050 instance->consumer_h);
4051 scsi_host_put(host);
4056 pci_disable_device(pdev);
4061 #define megasas_suspend NULL
4062 #define megasas_resume NULL
4066 * megasas_detach_one - PCI hot"un"plug entry point
4067 * @pdev: PCI device structure
4069 static void __devexit megasas_detach_one(struct pci_dev *pdev)
4072 struct Scsi_Host *host;
4073 struct megasas_instance *instance;
4075 instance = pci_get_drvdata(pdev);
4076 instance->unload = 1;
4077 host = instance->host;
4080 del_timer_sync(&instance->io_completion_timer);
4082 scsi_remove_host(instance->host);
4083 megasas_flush_cache(instance);
4084 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4086 /* cancel the delayed work if this work still in queue*/
4087 if (instance->ev != NULL) {
4088 struct megasas_aen_event *ev = instance->ev;
4089 cancel_delayed_work(
4090 (struct delayed_work *)&ev->hotplug_work);
4091 flush_scheduled_work();
4092 instance->ev = NULL;
4095 tasklet_kill(&instance->isr_tasklet);
4098 * Take the instance off the instance array. Note that we will not
4099 * decrement the max_index. We let this array be sparse array
4101 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4102 if (megasas_mgmt_info.instance[i] == instance) {
4103 megasas_mgmt_info.count--;
4104 megasas_mgmt_info.instance[i] = NULL;
4110 pci_set_drvdata(instance->pdev, NULL);
4112 instance->instancet->disable_intr(instance->reg_set);
4114 free_irq(instance->pdev->irq, instance);
4116 megasas_release_mfi(instance);
4118 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4119 instance->evt_detail, instance->evt_detail_h);
4121 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4122 instance->producer_h);
4124 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4125 instance->consumer_h);
4127 scsi_host_put(host);
4129 pci_set_drvdata(pdev, NULL);
4131 pci_disable_device(pdev);
4137 * megasas_shutdown - Shutdown entry point
4138 * @device: Generic device structure
4140 static void megasas_shutdown(struct pci_dev *pdev)
4142 struct megasas_instance *instance = pci_get_drvdata(pdev);
4143 instance->unload = 1;
4144 megasas_flush_cache(instance);
4145 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4149 * megasas_mgmt_open - char node "open" entry point
4151 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4153 cycle_kernel_lock();
4155 * Allow only those users with admin rights
4157 if (!capable(CAP_SYS_ADMIN))
4164 * megasas_mgmt_fasync - Async notifier registration from applications
4166 * This function adds the calling process to a driver global queue. When an
4167 * event occurs, SIGIO will be sent to all processes in this queue.
4169 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4173 mutex_lock(&megasas_async_queue_mutex);
4175 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4177 mutex_unlock(&megasas_async_queue_mutex);
4180 /* For sanity check when we get ioctl */
4181 filep->private_data = filep;
4185 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4191 * megasas_mgmt_poll - char node "poll" entry point
4193 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4196 unsigned long flags;
4197 poll_wait(file, &megasas_poll_wait, wait);
4198 spin_lock_irqsave(&poll_aen_lock, flags);
4199 if (megasas_poll_wait_aen)
4200 mask = (POLLIN | POLLRDNORM);
4203 spin_unlock_irqrestore(&poll_aen_lock, flags);
4208 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4209 * @instance: Adapter soft state
4210 * @argp: User's ioctl packet
4213 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4214 struct megasas_iocpacket __user * user_ioc,
4215 struct megasas_iocpacket *ioc)
4217 struct megasas_sge32 *kern_sge32;
4218 struct megasas_cmd *cmd;
4219 void *kbuff_arr[MAX_IOCTL_SGE];
4220 dma_addr_t buf_handle = 0;
4223 dma_addr_t sense_handle;
4224 unsigned long *sense_ptr;
4226 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4228 if (ioc->sge_count > MAX_IOCTL_SGE) {
4229 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4230 ioc->sge_count, MAX_IOCTL_SGE);
4234 cmd = megasas_get_cmd(instance);
4236 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4241 * User's IOCTL packet has 2 frames (maximum). Copy those two
4242 * frames into our cmd's frames. cmd->frame's context will get
4243 * overwritten when we copy from user's frames. So set that value
4246 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4247 cmd->frame->hdr.context = cmd->index;
4248 cmd->frame->hdr.pad_0 = 0;
4251 * The management interface between applications and the fw uses
4252 * MFI frames. E.g, RAID configuration changes, LD property changes
4253 * etc are accomplishes through different kinds of MFI frames. The
4254 * driver needs to care only about substituting user buffers with
4255 * kernel buffers in SGLs. The location of SGL is embedded in the
4256 * struct iocpacket itself.
4258 kern_sge32 = (struct megasas_sge32 *)
4259 ((unsigned long)cmd->frame + ioc->sgl_off);
4262 * For each user buffer, create a mirror buffer and copy in
4264 for (i = 0; i < ioc->sge_count; i++) {
4265 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
4266 ioc->sgl[i].iov_len,
4267 &buf_handle, GFP_KERNEL);
4268 if (!kbuff_arr[i]) {
4269 printk(KERN_DEBUG "megasas: Failed to alloc "
4270 "kernel SGL buffer for IOCTL \n");
4276 * We don't change the dma_coherent_mask, so
4277 * pci_alloc_consistent only returns 32bit addresses
4279 kern_sge32[i].phys_addr = (u32) buf_handle;
4280 kern_sge32[i].length = ioc->sgl[i].iov_len;
4283 * We created a kernel buffer corresponding to the
4284 * user buffer. Now copy in from the user buffer
4286 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4287 (u32) (ioc->sgl[i].iov_len))) {
4293 if (ioc->sense_len) {
4294 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4295 &sense_handle, GFP_KERNEL);
4302 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
4303 *sense_ptr = sense_handle;
4307 * Set the sync_cmd flag so that the ISR knows not to complete this
4308 * cmd to the SCSI mid-layer
4311 megasas_issue_blocked_cmd(instance, cmd);
4315 * copy out the kernel buffers to user buffers
4317 for (i = 0; i < ioc->sge_count; i++) {
4318 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4319 ioc->sgl[i].iov_len)) {
4326 * copy out the sense
4328 if (ioc->sense_len) {
4330 * sense_ptr points to the location that has the user
4331 * sense buffer address
4333 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4336 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4337 sense, ioc->sense_len)) {
4338 printk(KERN_ERR "megasas: Failed to copy out to user "
4346 * copy the status codes returned by the fw
4348 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4349 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4350 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4356 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
4357 sense, sense_handle);
4360 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
4361 dma_free_coherent(&instance->pdev->dev,
4362 kern_sge32[i].length,
4363 kbuff_arr[i], kern_sge32[i].phys_addr);
4366 megasas_return_cmd(instance, cmd);
4370 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4372 struct megasas_iocpacket __user *user_ioc =
4373 (struct megasas_iocpacket __user *)arg;
4374 struct megasas_iocpacket *ioc;
4375 struct megasas_instance *instance;
4378 unsigned long flags;
4379 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4381 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4385 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4390 instance = megasas_lookup_instance(ioc->host_no);
4396 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4397 printk(KERN_ERR "Controller in crit error\n");
4402 if (instance->unload == 1) {
4408 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4410 if (down_interruptible(&instance->ioctl_sem)) {
4411 error = -ERESTARTSYS;
4415 for (i = 0; i < wait_time; i++) {
4417 spin_lock_irqsave(&instance->hba_lock, flags);
4418 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4419 spin_unlock_irqrestore(&instance->hba_lock, flags);
4422 spin_unlock_irqrestore(&instance->hba_lock, flags);
4424 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4425 printk(KERN_NOTICE "megasas: waiting"
4426 "for controller reset to finish\n");
4432 spin_lock_irqsave(&instance->hba_lock, flags);
4433 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4434 spin_unlock_irqrestore(&instance->hba_lock, flags);
4436 printk(KERN_ERR "megaraid_sas: timed out while"
4437 "waiting for HBA to recover\n");
4441 spin_unlock_irqrestore(&instance->hba_lock, flags);
4443 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4444 up(&instance->ioctl_sem);
4451 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4453 struct megasas_instance *instance;
4454 struct megasas_aen aen;
4457 unsigned long flags;
4458 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4460 if (file->private_data != file) {
4461 printk(KERN_DEBUG "megasas: fasync_helper was not "
4466 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4469 instance = megasas_lookup_instance(aen.host_no);
4474 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4478 if (instance->unload == 1) {
4482 for (i = 0; i < wait_time; i++) {
4484 spin_lock_irqsave(&instance->hba_lock, flags);
4485 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4486 spin_unlock_irqrestore(&instance->hba_lock,
4491 spin_unlock_irqrestore(&instance->hba_lock, flags);
4493 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4494 printk(KERN_NOTICE "megasas: waiting for"
4495 "controller reset to finish\n");
4501 spin_lock_irqsave(&instance->hba_lock, flags);
4502 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4503 spin_unlock_irqrestore(&instance->hba_lock, flags);
4504 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4505 "for HBA to recover.\n");
4508 spin_unlock_irqrestore(&instance->hba_lock, flags);
4510 mutex_lock(&instance->aen_mutex);
4511 error = megasas_register_aen(instance, aen.seq_num,
4512 aen.class_locale_word);
4513 mutex_unlock(&instance->aen_mutex);
4518 * megasas_mgmt_ioctl - char node ioctl entry point
4521 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4524 case MEGASAS_IOC_FIRMWARE:
4525 return megasas_mgmt_ioctl_fw(file, arg);
4527 case MEGASAS_IOC_GET_AEN:
4528 return megasas_mgmt_ioctl_aen(file, arg);
4534 #ifdef CONFIG_COMPAT
4535 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
4537 struct compat_megasas_iocpacket __user *cioc =
4538 (struct compat_megasas_iocpacket __user *)arg;
4539 struct megasas_iocpacket __user *ioc =
4540 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
4545 if (clear_user(ioc, sizeof(*ioc)))
4548 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
4549 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
4550 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
4551 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
4552 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
4553 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
4557 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
4558 * sense_len is not null, so prepare the 64bit value under
4559 * the same condition.
4561 if (ioc->sense_len) {
4562 void __user **sense_ioc_ptr =
4563 (void __user **)(ioc->frame.raw + ioc->sense_off);
4564 compat_uptr_t *sense_cioc_ptr =
4565 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
4566 if (get_user(ptr, sense_cioc_ptr) ||
4567 put_user(compat_ptr(ptr), sense_ioc_ptr))
4571 for (i = 0; i < MAX_IOCTL_SGE; i++) {
4572 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
4573 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
4574 copy_in_user(&ioc->sgl[i].iov_len,
4575 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
4579 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
4581 if (copy_in_user(&cioc->frame.hdr.cmd_status,
4582 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
4583 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
4590 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
4594 case MEGASAS_IOC_FIRMWARE32:
4595 return megasas_mgmt_compat_ioctl_fw(file, arg);
4596 case MEGASAS_IOC_GET_AEN:
4597 return megasas_mgmt_ioctl_aen(file, arg);
4605 * File operations structure for management interface
4607 static const struct file_operations megasas_mgmt_fops = {
4608 .owner = THIS_MODULE,
4609 .open = megasas_mgmt_open,
4610 .fasync = megasas_mgmt_fasync,
4611 .unlocked_ioctl = megasas_mgmt_ioctl,
4612 .poll = megasas_mgmt_poll,
4613 #ifdef CONFIG_COMPAT
4614 .compat_ioctl = megasas_mgmt_compat_ioctl,
4619 * PCI hotplug support registration structure
4621 static struct pci_driver megasas_pci_driver = {
4623 .name = "megaraid_sas",
4624 .id_table = megasas_pci_table,
4625 .probe = megasas_probe_one,
4626 .remove = __devexit_p(megasas_detach_one),
4627 .suspend = megasas_suspend,
4628 .resume = megasas_resume,
4629 .shutdown = megasas_shutdown,
4633 * Sysfs driver attributes
4635 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
4637 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
4641 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
4644 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
4646 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
4650 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
4654 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
4656 return sprintf(buf, "%u\n", support_poll_for_event);
4659 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
4660 megasas_sysfs_show_support_poll_for_event, NULL);
4663 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
4665 return sprintf(buf, "%u\n", megasas_dbg_lvl);
4669 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
4672 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
4673 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
4679 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
4680 megasas_sysfs_set_dbg_lvl);
4683 megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
4685 return sprintf(buf, "%u\n", poll_mode_io);
4689 megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
4690 const char *buf, size_t count)
4693 int tmp = poll_mode_io;
4695 struct megasas_instance *instance;
4697 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
4698 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
4703 * Check if poll_mode_io is already set or is same as previous value
4705 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
4710 * Start timers for all adapters
4712 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4713 instance = megasas_mgmt_info.instance[i];
4715 megasas_start_timer(instance,
4716 &instance->io_completion_timer,
4717 megasas_io_completion_timer,
4718 MEGASAS_COMPLETION_TIMER_INTERVAL);
4723 * Delete timers for all adapters
4725 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4726 instance = megasas_mgmt_info.instance[i];
4728 del_timer_sync(&instance->io_completion_timer);
4737 megasas_aen_polling(struct work_struct *work)
4739 struct megasas_aen_event *ev =
4740 container_of(work, struct megasas_aen_event, hotplug_work);
4741 struct megasas_instance *instance = ev->instance;
4742 union megasas_evt_class_locale class_locale;
4743 struct Scsi_Host *host;
4744 struct scsi_device *sdev1;
4747 int i, j, doscan = 0;
4752 printk(KERN_ERR "invalid instance!\n");
4756 instance->ev = NULL;
4757 host = instance->host;
4758 if (instance->evt_detail) {
4760 switch (instance->evt_detail->code) {
4761 case MR_EVT_PD_INSERTED:
4762 if (megasas_get_pd_list(instance) == 0) {
4763 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4765 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4769 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4772 scsi_device_lookup(host, i, j, 0);
4774 if (instance->pd_list[pd_index].driveState
4775 == MR_PD_STATE_SYSTEM) {
4777 scsi_add_device(host, i, j, 0);
4781 scsi_device_put(sdev1);
4789 case MR_EVT_PD_REMOVED:
4790 if (megasas_get_pd_list(instance) == 0) {
4791 megasas_get_pd_list(instance);
4792 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4794 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4798 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4801 scsi_device_lookup(host, i, j, 0);
4803 if (instance->pd_list[pd_index].driveState
4804 == MR_PD_STATE_SYSTEM) {
4806 scsi_device_put(sdev1);
4810 scsi_remove_device(sdev1);
4811 scsi_device_put(sdev1);
4820 case MR_EVT_LD_OFFLINE:
4821 case MR_EVT_LD_DELETED:
4822 megasas_get_ld_list(instance);
4823 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4825 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4829 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4831 sdev1 = scsi_device_lookup(host,
4832 i + MEGASAS_MAX_LD_CHANNELS,
4836 if (instance->ld_ids[ld_index] != 0xff) {
4838 scsi_device_put(sdev1);
4842 scsi_remove_device(sdev1);
4843 scsi_device_put(sdev1);
4850 case MR_EVT_LD_CREATED:
4851 megasas_get_ld_list(instance);
4852 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4854 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4857 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4859 sdev1 = scsi_device_lookup(host,
4860 i+MEGASAS_MAX_LD_CHANNELS,
4863 if (instance->ld_ids[ld_index] !=
4866 scsi_add_device(host,
4872 scsi_device_put(sdev1);
4878 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
4879 case MR_EVT_FOREIGN_CFG_IMPORTED:
4887 printk(KERN_ERR "invalid evt_detail!\n");
4893 printk(KERN_INFO "scanning ...\n");
4894 megasas_get_pd_list(instance);
4895 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4896 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
4897 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
4898 sdev1 = scsi_device_lookup(host, i, j, 0);
4899 if (instance->pd_list[pd_index].driveState ==
4900 MR_PD_STATE_SYSTEM) {
4902 scsi_add_device(host, i, j, 0);
4905 scsi_device_put(sdev1);
4908 scsi_remove_device(sdev1);
4909 scsi_device_put(sdev1);
4915 megasas_get_ld_list(instance);
4916 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4917 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
4919 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4921 sdev1 = scsi_device_lookup(host,
4922 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
4923 if (instance->ld_ids[ld_index] != 0xff) {
4925 scsi_add_device(host,
4929 scsi_device_put(sdev1);
4933 scsi_remove_device(sdev1);
4934 scsi_device_put(sdev1);
4941 if ( instance->aen_cmd != NULL ) {
4946 seq_num = instance->evt_detail->seq_num + 1;
4948 /* Register AEN with FW for latest sequence number plus 1 */
4949 class_locale.members.reserved = 0;
4950 class_locale.members.locale = MR_EVT_LOCALE_ALL;
4951 class_locale.members.class = MR_EVT_CLASS_DEBUG;
4952 mutex_lock(&instance->aen_mutex);
4953 error = megasas_register_aen(instance, seq_num,
4955 mutex_unlock(&instance->aen_mutex);
4958 printk(KERN_ERR "register aen failed error %x\n", error);
4964 static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
4965 megasas_sysfs_show_poll_mode_io,
4966 megasas_sysfs_set_poll_mode_io);
4969 * megasas_init - Driver load entry point
4971 static int __init megasas_init(void)
4976 * Announce driver version and other information
4978 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
4979 MEGASAS_EXT_VERSION);
4981 support_poll_for_event = 2;
4983 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
4986 * Register character device node
4988 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
4991 printk(KERN_DEBUG "megasas: failed to open device node\n");
4995 megasas_mgmt_majorno = rval;
4998 * Register ourselves as PCI hotplug module
5000 rval = pci_register_driver(&megasas_pci_driver);
5003 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
5007 rval = driver_create_file(&megasas_pci_driver.driver,
5008 &driver_attr_version);
5010 goto err_dcf_attr_ver;
5011 rval = driver_create_file(&megasas_pci_driver.driver,
5012 &driver_attr_release_date);
5014 goto err_dcf_rel_date;
5016 rval = driver_create_file(&megasas_pci_driver.driver,
5017 &driver_attr_support_poll_for_event);
5019 goto err_dcf_support_poll_for_event;
5021 rval = driver_create_file(&megasas_pci_driver.driver,
5022 &driver_attr_dbg_lvl);
5024 goto err_dcf_dbg_lvl;
5025 rval = driver_create_file(&megasas_pci_driver.driver,
5026 &driver_attr_poll_mode_io);
5028 goto err_dcf_poll_mode_io;
5032 err_dcf_poll_mode_io:
5033 driver_remove_file(&megasas_pci_driver.driver,
5034 &driver_attr_dbg_lvl);
5036 driver_remove_file(&megasas_pci_driver.driver,
5037 &driver_attr_support_poll_for_event);
5039 err_dcf_support_poll_for_event:
5040 driver_remove_file(&megasas_pci_driver.driver,
5041 &driver_attr_release_date);
5044 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5046 pci_unregister_driver(&megasas_pci_driver);
5048 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5053 * megasas_exit - Driver unload entry point
5055 static void __exit megasas_exit(void)
5057 driver_remove_file(&megasas_pci_driver.driver,
5058 &driver_attr_poll_mode_io);
5059 driver_remove_file(&megasas_pci_driver.driver,
5060 &driver_attr_dbg_lvl);
5061 driver_remove_file(&megasas_pci_driver.driver,
5062 &driver_attr_release_date);
5063 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5065 pci_unregister_driver(&megasas_pci_driver);
5066 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5069 module_init(megasas_init);
5070 module_exit(megasas_exit);