2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2011 LSI Corporation.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * FILE: megaraid_sas_base.c
21 * Version : v00.00.06.12-rc1
23 * Authors: LSI Corporation
27 * Adam Radford <linuxraid@lsi.com>
29 * Send feedback to: <megaraidlinux@lsi.com>
31 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/pci.h>
38 #include <linux/list.h>
39 #include <linux/moduleparam.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/interrupt.h>
43 #include <linux/delay.h>
44 #include <linux/uio.h>
45 #include <linux/slab.h>
46 #include <asm/uaccess.h>
48 #include <linux/compat.h>
49 #include <linux/blkdev.h>
50 #include <linux/mutex.h>
51 #include <linux/poll.h>
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_device.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_tcq.h>
58 #include "megaraid_sas_fusion.h"
59 #include "megaraid_sas.h"
62 * poll_mode_io:1- schedule complete completion from q cmd
64 static unsigned int poll_mode_io;
65 module_param_named(poll_mode_io, poll_mode_io, int, 0);
66 MODULE_PARM_DESC(poll_mode_io,
67 "Complete cmds from IO path, (default=0)");
70 * Number of sectors per IO command
71 * Will be set in megasas_init_mfi if user does not provide
73 static unsigned int max_sectors;
74 module_param_named(max_sectors, max_sectors, int, 0);
75 MODULE_PARM_DESC(max_sectors,
76 "Maximum number of sectors per IO command");
78 static int msix_disable;
79 module_param(msix_disable, int, S_IRUGO);
80 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
82 MODULE_LICENSE("GPL");
83 MODULE_VERSION(MEGASAS_VERSION);
84 MODULE_AUTHOR("megaraidlinux@lsi.com");
85 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
87 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
88 static int megasas_get_pd_list(struct megasas_instance *instance);
89 static int megasas_issue_init_mfi(struct megasas_instance *instance);
90 static int megasas_register_aen(struct megasas_instance *instance,
91 u32 seq_num, u32 class_locale_word);
93 * PCI ID table for all supported controllers
95 static struct pci_device_id megasas_pci_table[] = {
97 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
99 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
101 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
103 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
111 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
112 /* xscale IOP, vega */
113 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
115 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
122 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
124 static int megasas_mgmt_majorno;
125 static struct megasas_mgmt_info megasas_mgmt_info;
126 static struct fasync_struct *megasas_async_queue;
127 static DEFINE_MUTEX(megasas_async_queue_mutex);
129 static int megasas_poll_wait_aen;
130 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
131 static u32 support_poll_for_event;
133 static u32 support_device_change;
135 /* define lock for aen poll */
136 spinlock_t poll_aen_lock;
139 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
142 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
144 megasas_adp_reset_gen2(struct megasas_instance *instance,
145 struct megasas_register_set __iomem *reg_set);
146 static irqreturn_t megasas_isr(int irq, void *devp);
148 megasas_init_adapter_mfi(struct megasas_instance *instance);
150 megasas_build_and_issue_cmd(struct megasas_instance *instance,
151 struct scsi_cmnd *scmd);
152 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
154 megasas_release_fusion(struct megasas_instance *instance);
156 megasas_ioc_init_fusion(struct megasas_instance *instance);
158 megasas_free_cmds_fusion(struct megasas_instance *instance);
160 megasas_get_map_info(struct megasas_instance *instance);
162 megasas_sync_map_info(struct megasas_instance *instance);
164 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
165 void megasas_reset_reply_desc(struct megasas_instance *instance);
166 u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
167 struct LD_LOAD_BALANCE_INFO *lbInfo);
168 int megasas_reset_fusion(struct Scsi_Host *shost);
169 void megasas_fusion_ocr_wq(struct work_struct *work);
172 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
174 instance->instancet->fire_cmd(instance,
175 cmd->frame_phys_addr, 0, instance->reg_set);
179 * megasas_get_cmd - Get a command from the free pool
180 * @instance: Adapter soft state
182 * Returns a free command from the pool
184 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
188 struct megasas_cmd *cmd = NULL;
190 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
192 if (!list_empty(&instance->cmd_pool)) {
193 cmd = list_entry((&instance->cmd_pool)->next,
194 struct megasas_cmd, list);
195 list_del_init(&cmd->list);
197 printk(KERN_ERR "megasas: Command pool empty!\n");
200 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
205 * megasas_return_cmd - Return a cmd to free command pool
206 * @instance: Adapter soft state
207 * @cmd: Command packet to be returned to free command pool
210 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
214 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
217 cmd->frame_count = 0;
218 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
219 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
221 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
222 list_add_tail(&cmd->list, &instance->cmd_pool);
224 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
229 * The following functions are defined for xscale
230 * (deviceid : 1064R, PERC5) controllers
234 * megasas_enable_intr_xscale - Enables interrupts
235 * @regs: MFI register set
238 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
240 writel(0, &(regs)->outbound_intr_mask);
242 /* Dummy readl to force pci flush */
243 readl(®s->outbound_intr_mask);
247 * megasas_disable_intr_xscale -Disables interrupt
248 * @regs: MFI register set
251 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
254 writel(mask, ®s->outbound_intr_mask);
255 /* Dummy readl to force pci flush */
256 readl(®s->outbound_intr_mask);
260 * megasas_read_fw_status_reg_xscale - returns the current FW status value
261 * @regs: MFI register set
264 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
266 return readl(&(regs)->outbound_msg_0);
269 * megasas_clear_interrupt_xscale - Check & clear interrupt
270 * @regs: MFI register set
273 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
278 * Check if it is our interrupt
280 status = readl(®s->outbound_intr_status);
282 if (status & MFI_OB_INTR_STATUS_MASK)
283 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
284 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
285 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
288 * Clear the interrupt by writing back the same value
291 writel(status, ®s->outbound_intr_status);
293 /* Dummy readl to force pci flush */
294 readl(®s->outbound_intr_status);
300 * megasas_fire_cmd_xscale - Sends command to the FW
301 * @frame_phys_addr : Physical address of cmd
302 * @frame_count : Number of frames for the command
303 * @regs : MFI register set
306 megasas_fire_cmd_xscale(struct megasas_instance *instance,
307 dma_addr_t frame_phys_addr,
309 struct megasas_register_set __iomem *regs)
312 spin_lock_irqsave(&instance->hba_lock, flags);
313 writel((frame_phys_addr >> 3)|(frame_count),
314 &(regs)->inbound_queue_port);
315 spin_unlock_irqrestore(&instance->hba_lock, flags);
319 * megasas_adp_reset_xscale - For controller reset
320 * @regs: MFI register set
323 megasas_adp_reset_xscale(struct megasas_instance *instance,
324 struct megasas_register_set __iomem *regs)
328 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
330 for (i = 0; i < 3; i++)
331 msleep(1000); /* sleep for 3 secs */
333 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
334 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
336 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
338 pci_write_config_dword(instance->pdev,
339 MFI_1068_PCSR_OFFSET, pcidata);
341 for (i = 0; i < 2; i++)
342 msleep(1000); /* need to wait 2 secs again */
345 pci_read_config_dword(instance->pdev,
346 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
347 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
348 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
349 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
351 pci_write_config_dword(instance->pdev,
352 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
359 * megasas_check_reset_xscale - For controller reset check
360 * @regs: MFI register set
363 megasas_check_reset_xscale(struct megasas_instance *instance,
364 struct megasas_register_set __iomem *regs)
367 consumer = *instance->consumer;
369 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
370 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
376 static struct megasas_instance_template megasas_instance_template_xscale = {
378 .fire_cmd = megasas_fire_cmd_xscale,
379 .enable_intr = megasas_enable_intr_xscale,
380 .disable_intr = megasas_disable_intr_xscale,
381 .clear_intr = megasas_clear_intr_xscale,
382 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
383 .adp_reset = megasas_adp_reset_xscale,
384 .check_reset = megasas_check_reset_xscale,
385 .service_isr = megasas_isr,
386 .tasklet = megasas_complete_cmd_dpc,
387 .init_adapter = megasas_init_adapter_mfi,
388 .build_and_issue_cmd = megasas_build_and_issue_cmd,
389 .issue_dcmd = megasas_issue_dcmd,
393 * This is the end of set of functions & definitions specific
394 * to xscale (deviceid : 1064R, PERC5) controllers
398 * The following functions are defined for ppc (deviceid : 0x60)
403 * megasas_enable_intr_ppc - Enables interrupts
404 * @regs: MFI register set
407 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
409 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
411 writel(~0x80000000, &(regs)->outbound_intr_mask);
413 /* Dummy readl to force pci flush */
414 readl(®s->outbound_intr_mask);
418 * megasas_disable_intr_ppc - Disable interrupt
419 * @regs: MFI register set
422 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
424 u32 mask = 0xFFFFFFFF;
425 writel(mask, ®s->outbound_intr_mask);
426 /* Dummy readl to force pci flush */
427 readl(®s->outbound_intr_mask);
431 * megasas_read_fw_status_reg_ppc - returns the current FW status value
432 * @regs: MFI register set
435 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
437 return readl(&(regs)->outbound_scratch_pad);
441 * megasas_clear_interrupt_ppc - Check & clear interrupt
442 * @regs: MFI register set
445 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
447 u32 status, mfiStatus = 0;
450 * Check if it is our interrupt
452 status = readl(®s->outbound_intr_status);
454 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
455 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
457 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
458 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
461 * Clear the interrupt by writing back the same value
463 writel(status, ®s->outbound_doorbell_clear);
465 /* Dummy readl to force pci flush */
466 readl(®s->outbound_doorbell_clear);
472 * megasas_fire_cmd_ppc - Sends command to the FW
473 * @frame_phys_addr : Physical address of cmd
474 * @frame_count : Number of frames for the command
475 * @regs : MFI register set
478 megasas_fire_cmd_ppc(struct megasas_instance *instance,
479 dma_addr_t frame_phys_addr,
481 struct megasas_register_set __iomem *regs)
484 spin_lock_irqsave(&instance->hba_lock, flags);
485 writel((frame_phys_addr | (frame_count<<1))|1,
486 &(regs)->inbound_queue_port);
487 spin_unlock_irqrestore(&instance->hba_lock, flags);
491 * megasas_check_reset_ppc - For controller reset check
492 * @regs: MFI register set
495 megasas_check_reset_ppc(struct megasas_instance *instance,
496 struct megasas_register_set __iomem *regs)
498 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
504 static struct megasas_instance_template megasas_instance_template_ppc = {
506 .fire_cmd = megasas_fire_cmd_ppc,
507 .enable_intr = megasas_enable_intr_ppc,
508 .disable_intr = megasas_disable_intr_ppc,
509 .clear_intr = megasas_clear_intr_ppc,
510 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
511 .adp_reset = megasas_adp_reset_xscale,
512 .check_reset = megasas_check_reset_ppc,
513 .service_isr = megasas_isr,
514 .tasklet = megasas_complete_cmd_dpc,
515 .init_adapter = megasas_init_adapter_mfi,
516 .build_and_issue_cmd = megasas_build_and_issue_cmd,
517 .issue_dcmd = megasas_issue_dcmd,
521 * megasas_enable_intr_skinny - Enables interrupts
522 * @regs: MFI register set
525 megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
527 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
529 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
531 /* Dummy readl to force pci flush */
532 readl(®s->outbound_intr_mask);
536 * megasas_disable_intr_skinny - Disables interrupt
537 * @regs: MFI register set
540 megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
542 u32 mask = 0xFFFFFFFF;
543 writel(mask, ®s->outbound_intr_mask);
544 /* Dummy readl to force pci flush */
545 readl(®s->outbound_intr_mask);
549 * megasas_read_fw_status_reg_skinny - returns the current FW status value
550 * @regs: MFI register set
553 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
555 return readl(&(regs)->outbound_scratch_pad);
559 * megasas_clear_interrupt_skinny - Check & clear interrupt
560 * @regs: MFI register set
563 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
569 * Check if it is our interrupt
571 status = readl(®s->outbound_intr_status);
573 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
578 * Check if it is our interrupt
580 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
582 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
584 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
587 * Clear the interrupt by writing back the same value
589 writel(status, ®s->outbound_intr_status);
592 * dummy read to flush PCI
594 readl(®s->outbound_intr_status);
600 * megasas_fire_cmd_skinny - Sends command to the FW
601 * @frame_phys_addr : Physical address of cmd
602 * @frame_count : Number of frames for the command
603 * @regs : MFI register set
606 megasas_fire_cmd_skinny(struct megasas_instance *instance,
607 dma_addr_t frame_phys_addr,
609 struct megasas_register_set __iomem *regs)
612 spin_lock_irqsave(&instance->hba_lock, flags);
613 writel(0, &(regs)->inbound_high_queue_port);
614 writel((frame_phys_addr | (frame_count<<1))|1,
615 &(regs)->inbound_low_queue_port);
616 spin_unlock_irqrestore(&instance->hba_lock, flags);
620 * megasas_check_reset_skinny - For controller reset check
621 * @regs: MFI register set
624 megasas_check_reset_skinny(struct megasas_instance *instance,
625 struct megasas_register_set __iomem *regs)
627 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
633 static struct megasas_instance_template megasas_instance_template_skinny = {
635 .fire_cmd = megasas_fire_cmd_skinny,
636 .enable_intr = megasas_enable_intr_skinny,
637 .disable_intr = megasas_disable_intr_skinny,
638 .clear_intr = megasas_clear_intr_skinny,
639 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
640 .adp_reset = megasas_adp_reset_gen2,
641 .check_reset = megasas_check_reset_skinny,
642 .service_isr = megasas_isr,
643 .tasklet = megasas_complete_cmd_dpc,
644 .init_adapter = megasas_init_adapter_mfi,
645 .build_and_issue_cmd = megasas_build_and_issue_cmd,
646 .issue_dcmd = megasas_issue_dcmd,
651 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
656 * megasas_enable_intr_gen2 - Enables interrupts
657 * @regs: MFI register set
660 megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
662 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
664 /* write ~0x00000005 (4 & 1) to the intr mask*/
665 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
667 /* Dummy readl to force pci flush */
668 readl(®s->outbound_intr_mask);
672 * megasas_disable_intr_gen2 - Disables interrupt
673 * @regs: MFI register set
676 megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
678 u32 mask = 0xFFFFFFFF;
679 writel(mask, ®s->outbound_intr_mask);
680 /* Dummy readl to force pci flush */
681 readl(®s->outbound_intr_mask);
685 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
686 * @regs: MFI register set
689 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
691 return readl(&(regs)->outbound_scratch_pad);
695 * megasas_clear_interrupt_gen2 - Check & clear interrupt
696 * @regs: MFI register set
699 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
704 * Check if it is our interrupt
706 status = readl(®s->outbound_intr_status);
708 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
709 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
711 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
712 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
716 * Clear the interrupt by writing back the same value
719 writel(status, ®s->outbound_doorbell_clear);
721 /* Dummy readl to force pci flush */
722 readl(®s->outbound_intr_status);
727 * megasas_fire_cmd_gen2 - Sends command to the FW
728 * @frame_phys_addr : Physical address of cmd
729 * @frame_count : Number of frames for the command
730 * @regs : MFI register set
733 megasas_fire_cmd_gen2(struct megasas_instance *instance,
734 dma_addr_t frame_phys_addr,
736 struct megasas_register_set __iomem *regs)
739 spin_lock_irqsave(&instance->hba_lock, flags);
740 writel((frame_phys_addr | (frame_count<<1))|1,
741 &(regs)->inbound_queue_port);
742 spin_unlock_irqrestore(&instance->hba_lock, flags);
746 * megasas_adp_reset_gen2 - For controller reset
747 * @regs: MFI register set
750 megasas_adp_reset_gen2(struct megasas_instance *instance,
751 struct megasas_register_set __iomem *reg_set)
755 u32 *seq_offset = ®_set->seq_offset;
756 u32 *hostdiag_offset = ®_set->host_diag;
758 if (instance->instancet == &megasas_instance_template_skinny) {
759 seq_offset = ®_set->fusion_seq_offset;
760 hostdiag_offset = ®_set->fusion_host_diag;
763 writel(0, seq_offset);
764 writel(4, seq_offset);
765 writel(0xb, seq_offset);
766 writel(2, seq_offset);
767 writel(7, seq_offset);
768 writel(0xd, seq_offset);
772 HostDiag = (u32)readl(hostdiag_offset);
774 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
776 HostDiag = (u32)readl(hostdiag_offset);
777 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
785 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
787 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
791 HostDiag = (u32)readl(hostdiag_offset);
792 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
794 HostDiag = (u32)readl(hostdiag_offset);
795 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
806 * megasas_check_reset_gen2 - For controller reset check
807 * @regs: MFI register set
810 megasas_check_reset_gen2(struct megasas_instance *instance,
811 struct megasas_register_set __iomem *regs)
813 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
820 static struct megasas_instance_template megasas_instance_template_gen2 = {
822 .fire_cmd = megasas_fire_cmd_gen2,
823 .enable_intr = megasas_enable_intr_gen2,
824 .disable_intr = megasas_disable_intr_gen2,
825 .clear_intr = megasas_clear_intr_gen2,
826 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
827 .adp_reset = megasas_adp_reset_gen2,
828 .check_reset = megasas_check_reset_gen2,
829 .service_isr = megasas_isr,
830 .tasklet = megasas_complete_cmd_dpc,
831 .init_adapter = megasas_init_adapter_mfi,
832 .build_and_issue_cmd = megasas_build_and_issue_cmd,
833 .issue_dcmd = megasas_issue_dcmd,
837 * This is the end of set of functions & definitions
838 * specific to gen2 (deviceid : 0x78, 0x79) controllers
842 * Template added for TB (Fusion)
844 extern struct megasas_instance_template megasas_instance_template_fusion;
847 * megasas_issue_polled - Issues a polling command
848 * @instance: Adapter soft state
849 * @cmd: Command packet to be issued
851 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
854 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
857 struct megasas_header *frame_hdr = &cmd->frame->hdr;
859 frame_hdr->cmd_status = 0xFF;
860 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
863 * Issue the frame using inbound queue port
865 instance->instancet->issue_dcmd(instance, cmd);
868 * Wait for cmd_status to change
870 return wait_and_poll(instance, cmd);
874 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
875 * @instance: Adapter soft state
876 * @cmd: Command to be issued
878 * This function waits on an event for the command to be returned from ISR.
879 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
880 * Used to issue ioctl commands.
883 megasas_issue_blocked_cmd(struct megasas_instance *instance,
884 struct megasas_cmd *cmd)
886 cmd->cmd_status = ENODATA;
888 instance->instancet->issue_dcmd(instance, cmd);
890 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
896 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
897 * @instance: Adapter soft state
898 * @cmd_to_abort: Previously issued cmd to be aborted
900 * MFI firmware can abort previously issued AEN command (automatic event
901 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
902 * cmd and waits for return status.
903 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
906 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
907 struct megasas_cmd *cmd_to_abort)
909 struct megasas_cmd *cmd;
910 struct megasas_abort_frame *abort_fr;
912 cmd = megasas_get_cmd(instance);
917 abort_fr = &cmd->frame->abort;
920 * Prepare and issue the abort frame
922 abort_fr->cmd = MFI_CMD_ABORT;
923 abort_fr->cmd_status = 0xFF;
925 abort_fr->abort_context = cmd_to_abort->index;
926 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
927 abort_fr->abort_mfi_phys_addr_hi = 0;
930 cmd->cmd_status = 0xFF;
932 instance->instancet->issue_dcmd(instance, cmd);
935 * Wait for this cmd to complete
937 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
940 megasas_return_cmd(instance, cmd);
945 * megasas_make_sgl32 - Prepares 32-bit SGL
946 * @instance: Adapter soft state
947 * @scp: SCSI command from the mid-layer
948 * @mfi_sgl: SGL to be filled in
950 * If successful, this function returns the number of SG elements. Otherwise,
954 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
955 union megasas_sgl *mfi_sgl)
959 struct scatterlist *os_sgl;
961 sge_count = scsi_dma_map(scp);
962 BUG_ON(sge_count < 0);
965 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
966 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
967 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
974 * megasas_make_sgl64 - Prepares 64-bit SGL
975 * @instance: Adapter soft state
976 * @scp: SCSI command from the mid-layer
977 * @mfi_sgl: SGL to be filled in
979 * If successful, this function returns the number of SG elements. Otherwise,
983 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
984 union megasas_sgl *mfi_sgl)
988 struct scatterlist *os_sgl;
990 sge_count = scsi_dma_map(scp);
991 BUG_ON(sge_count < 0);
994 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
995 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
996 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
1003 * megasas_make_sgl_skinny - Prepares IEEE SGL
1004 * @instance: Adapter soft state
1005 * @scp: SCSI command from the mid-layer
1006 * @mfi_sgl: SGL to be filled in
1008 * If successful, this function returns the number of SG elements. Otherwise,
1012 megasas_make_sgl_skinny(struct megasas_instance *instance,
1013 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1017 struct scatterlist *os_sgl;
1019 sge_count = scsi_dma_map(scp);
1022 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1023 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1024 mfi_sgl->sge_skinny[i].phys_addr =
1025 sg_dma_address(os_sgl);
1026 mfi_sgl->sge_skinny[i].flag = 0;
1033 * megasas_get_frame_count - Computes the number of frames
1034 * @frame_type : type of frame- io or pthru frame
1035 * @sge_count : number of sg elements
1037 * Returns the number of frames required for numnber of sge's (sge_count)
1040 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1041 u8 sge_count, u8 frame_type)
1048 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1049 sizeof(struct megasas_sge32);
1051 if (instance->flag_ieee) {
1052 sge_sz = sizeof(struct megasas_sge_skinny);
1056 * Main frame can contain 2 SGEs for 64-bit SGLs and
1057 * 3 SGEs for 32-bit SGLs for ldio &
1058 * 1 SGEs for 64-bit SGLs and
1059 * 2 SGEs for 32-bit SGLs for pthru frame
1061 if (unlikely(frame_type == PTHRU_FRAME)) {
1062 if (instance->flag_ieee == 1) {
1063 num_cnt = sge_count - 1;
1064 } else if (IS_DMA64)
1065 num_cnt = sge_count - 1;
1067 num_cnt = sge_count - 2;
1069 if (instance->flag_ieee == 1) {
1070 num_cnt = sge_count - 1;
1071 } else if (IS_DMA64)
1072 num_cnt = sge_count - 2;
1074 num_cnt = sge_count - 3;
1078 sge_bytes = sge_sz * num_cnt;
1080 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1081 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1086 if (frame_count > 7)
1092 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1093 * @instance: Adapter soft state
1094 * @scp: SCSI command
1095 * @cmd: Command to be prepared in
1097 * This function prepares CDB commands. These are typcially pass-through
1098 * commands to the devices.
1101 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1102 struct megasas_cmd *cmd)
1107 struct megasas_pthru_frame *pthru;
1109 is_logical = MEGASAS_IS_LOGICAL(scp);
1110 device_id = MEGASAS_DEV_INDEX(instance, scp);
1111 pthru = (struct megasas_pthru_frame *)cmd->frame;
1113 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1114 flags = MFI_FRAME_DIR_WRITE;
1115 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1116 flags = MFI_FRAME_DIR_READ;
1117 else if (scp->sc_data_direction == PCI_DMA_NONE)
1118 flags = MFI_FRAME_DIR_NONE;
1120 if (instance->flag_ieee == 1) {
1121 flags |= MFI_FRAME_IEEE;
1125 * Prepare the DCDB frame
1127 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1128 pthru->cmd_status = 0x0;
1129 pthru->scsi_status = 0x0;
1130 pthru->target_id = device_id;
1131 pthru->lun = scp->device->lun;
1132 pthru->cdb_len = scp->cmd_len;
1135 pthru->flags = flags;
1136 pthru->data_xfer_len = scsi_bufflen(scp);
1138 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1141 * If the command is for the tape device, set the
1142 * pthru timeout to the os layer timeout value.
1144 if (scp->device->type == TYPE_TAPE) {
1145 if ((scp->request->timeout / HZ) > 0xFFFF)
1146 pthru->timeout = 0xFFFF;
1148 pthru->timeout = scp->request->timeout / HZ;
1154 if (instance->flag_ieee == 1) {
1155 pthru->flags |= MFI_FRAME_SGL64;
1156 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1158 } else if (IS_DMA64) {
1159 pthru->flags |= MFI_FRAME_SGL64;
1160 pthru->sge_count = megasas_make_sgl64(instance, scp,
1163 pthru->sge_count = megasas_make_sgl32(instance, scp,
1166 if (pthru->sge_count > instance->max_num_sge) {
1167 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1173 * Sense info specific
1175 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1176 pthru->sense_buf_phys_addr_hi = 0;
1177 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1180 * Compute the total number of frames this command consumes. FW uses
1181 * this number to pull sufficient number of frames from host memory.
1183 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1186 return cmd->frame_count;
1190 * megasas_build_ldio - Prepares IOs to logical devices
1191 * @instance: Adapter soft state
1192 * @scp: SCSI command
1193 * @cmd: Command to be prepared
1195 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1198 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1199 struct megasas_cmd *cmd)
1202 u8 sc = scp->cmnd[0];
1204 struct megasas_io_frame *ldio;
1206 device_id = MEGASAS_DEV_INDEX(instance, scp);
1207 ldio = (struct megasas_io_frame *)cmd->frame;
1209 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1210 flags = MFI_FRAME_DIR_WRITE;
1211 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1212 flags = MFI_FRAME_DIR_READ;
1214 if (instance->flag_ieee == 1) {
1215 flags |= MFI_FRAME_IEEE;
1219 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1221 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1222 ldio->cmd_status = 0x0;
1223 ldio->scsi_status = 0x0;
1224 ldio->target_id = device_id;
1226 ldio->reserved_0 = 0;
1228 ldio->flags = flags;
1229 ldio->start_lba_hi = 0;
1230 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1233 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1235 if (scp->cmd_len == 6) {
1236 ldio->lba_count = (u32) scp->cmnd[4];
1237 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1238 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1240 ldio->start_lba_lo &= 0x1FFFFF;
1244 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1246 else if (scp->cmd_len == 10) {
1247 ldio->lba_count = (u32) scp->cmnd[8] |
1248 ((u32) scp->cmnd[7] << 8);
1249 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1250 ((u32) scp->cmnd[3] << 16) |
1251 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1255 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1257 else if (scp->cmd_len == 12) {
1258 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1259 ((u32) scp->cmnd[7] << 16) |
1260 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1262 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1263 ((u32) scp->cmnd[3] << 16) |
1264 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1268 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1270 else if (scp->cmd_len == 16) {
1271 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1272 ((u32) scp->cmnd[11] << 16) |
1273 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1275 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1276 ((u32) scp->cmnd[7] << 16) |
1277 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1279 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1280 ((u32) scp->cmnd[3] << 16) |
1281 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1288 if (instance->flag_ieee) {
1289 ldio->flags |= MFI_FRAME_SGL64;
1290 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1292 } else if (IS_DMA64) {
1293 ldio->flags |= MFI_FRAME_SGL64;
1294 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1296 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1298 if (ldio->sge_count > instance->max_num_sge) {
1299 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1305 * Sense info specific
1307 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1308 ldio->sense_buf_phys_addr_hi = 0;
1309 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1312 * Compute the total number of frames this command consumes. FW uses
1313 * this number to pull sufficient number of frames from host memory.
1315 cmd->frame_count = megasas_get_frame_count(instance,
1316 ldio->sge_count, IO_FRAME);
1318 return cmd->frame_count;
1322 * megasas_is_ldio - Checks if the cmd is for logical drive
1323 * @scmd: SCSI command
1325 * Called by megasas_queue_command to find out if the command to be queued
1326 * is a logical drive command
1328 inline int megasas_is_ldio(struct scsi_cmnd *cmd)
1330 if (!MEGASAS_IS_LOGICAL(cmd))
1332 switch (cmd->cmnd[0]) {
1348 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1350 * @instance: Adapter soft state
1353 megasas_dump_pending_frames(struct megasas_instance *instance)
1355 struct megasas_cmd *cmd;
1357 union megasas_sgl *mfi_sgl;
1358 struct megasas_io_frame *ldio;
1359 struct megasas_pthru_frame *pthru;
1361 u32 max_cmd = instance->max_fw_cmds;
1363 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1364 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1366 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1368 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1370 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1371 for (i = 0; i < max_cmd; i++) {
1372 cmd = instance->cmd_list[i];
1375 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1376 if (megasas_is_ldio(cmd->scmd)){
1377 ldio = (struct megasas_io_frame *)cmd->frame;
1378 mfi_sgl = &ldio->sgl;
1379 sgcount = ldio->sge_count;
1380 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);
1383 pthru = (struct megasas_pthru_frame *) cmd->frame;
1384 mfi_sgl = &pthru->sgl;
1385 sgcount = pthru->sge_count;
1386 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);
1388 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1389 for (n = 0; n < sgcount; n++){
1391 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) ;
1393 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1396 printk(KERN_ERR "\n");
1398 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1399 for (i = 0; i < max_cmd; i++) {
1401 cmd = instance->cmd_list[i];
1403 if(cmd->sync_cmd == 1){
1404 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1407 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1411 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1412 struct scsi_cmnd *scmd)
1414 struct megasas_cmd *cmd;
1417 cmd = megasas_get_cmd(instance);
1419 return SCSI_MLQUEUE_HOST_BUSY;
1422 * Logical drive command
1424 if (megasas_is_ldio(scmd))
1425 frame_count = megasas_build_ldio(instance, scmd, cmd);
1427 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1430 goto out_return_cmd;
1433 scmd->SCp.ptr = (char *)cmd;
1436 * Issue the command to the FW
1438 atomic_inc(&instance->fw_outstanding);
1440 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1441 cmd->frame_count-1, instance->reg_set);
1443 * Check if we have pend cmds to be completed
1445 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1446 tasklet_schedule(&instance->isr_tasklet);
1450 megasas_return_cmd(instance, cmd);
1456 * megasas_queue_command - Queue entry point
1457 * @scmd: SCSI command to be queued
1458 * @done: Callback entry point
1461 megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1463 struct megasas_instance *instance;
1464 unsigned long flags;
1466 instance = (struct megasas_instance *)
1467 scmd->device->host->hostdata;
1469 if (instance->issuepend_done == 0)
1470 return SCSI_MLQUEUE_HOST_BUSY;
1472 spin_lock_irqsave(&instance->hba_lock, flags);
1473 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1474 spin_unlock_irqrestore(&instance->hba_lock, flags);
1475 return SCSI_MLQUEUE_HOST_BUSY;
1478 spin_unlock_irqrestore(&instance->hba_lock, flags);
1480 scmd->scsi_done = done;
1483 if (MEGASAS_IS_LOGICAL(scmd) &&
1484 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1485 scmd->result = DID_BAD_TARGET << 16;
1489 switch (scmd->cmnd[0]) {
1490 case SYNCHRONIZE_CACHE:
1492 * FW takes care of flush cache on its own
1493 * No need to send it down
1495 scmd->result = DID_OK << 16;
1501 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1502 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
1503 return SCSI_MLQUEUE_HOST_BUSY;
1513 static DEF_SCSI_QCMD(megasas_queue_command)
1515 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1519 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1521 if ((megasas_mgmt_info.instance[i]) &&
1522 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1523 return megasas_mgmt_info.instance[i];
1529 static int megasas_slave_configure(struct scsi_device *sdev)
1532 struct megasas_instance *instance ;
1534 instance = megasas_lookup_instance(sdev->host->host_no);
1537 * Don't export physical disk devices to the disk driver.
1539 * FIXME: Currently we don't export them to the midlayer at all.
1540 * That will be fixed once LSI engineers have audited the
1541 * firmware for possible issues.
1543 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1544 sdev->type == TYPE_DISK) {
1545 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1547 if (instance->pd_list[pd_index].driveState ==
1548 MR_PD_STATE_SYSTEM) {
1549 blk_queue_rq_timeout(sdev->request_queue,
1550 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1557 * The RAID firmware may require extended timeouts.
1559 blk_queue_rq_timeout(sdev->request_queue,
1560 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1564 static int megasas_slave_alloc(struct scsi_device *sdev)
1567 struct megasas_instance *instance ;
1568 instance = megasas_lookup_instance(sdev->host->host_no);
1569 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1570 (sdev->type == TYPE_DISK)) {
1572 * Open the OS scan to the SYSTEM PD
1575 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1577 if ((instance->pd_list[pd_index].driveState ==
1578 MR_PD_STATE_SYSTEM) &&
1579 (instance->pd_list[pd_index].driveType ==
1588 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1590 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1591 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1592 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1593 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
1594 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1596 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1601 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1602 * restored to max value
1603 * @instance: Adapter soft state
1607 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1609 unsigned long flags;
1610 if (instance->flag & MEGASAS_FW_BUSY
1611 && time_after(jiffies, instance->last_time + 5 * HZ)
1612 && atomic_read(&instance->fw_outstanding) < 17) {
1614 spin_lock_irqsave(instance->host->host_lock, flags);
1615 instance->flag &= ~MEGASAS_FW_BUSY;
1616 if ((instance->pdev->device ==
1617 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1618 (instance->pdev->device ==
1619 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1620 instance->host->can_queue =
1621 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1623 instance->host->can_queue =
1624 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1626 spin_unlock_irqrestore(instance->host->host_lock, flags);
1631 * megasas_complete_cmd_dpc - Returns FW's controller structure
1632 * @instance_addr: Address of adapter soft state
1634 * Tasklet to complete cmds
1636 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1641 struct megasas_cmd *cmd;
1642 struct megasas_instance *instance =
1643 (struct megasas_instance *)instance_addr;
1644 unsigned long flags;
1646 /* If we have already declared adapter dead, donot complete cmds */
1647 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1650 spin_lock_irqsave(&instance->completion_lock, flags);
1652 producer = *instance->producer;
1653 consumer = *instance->consumer;
1655 while (consumer != producer) {
1656 context = instance->reply_queue[consumer];
1657 if (context >= instance->max_fw_cmds) {
1658 printk(KERN_ERR "Unexpected context value %x\n",
1663 cmd = instance->cmd_list[context];
1665 megasas_complete_cmd(instance, cmd, DID_OK);
1668 if (consumer == (instance->max_fw_cmds + 1)) {
1673 *instance->consumer = producer;
1675 spin_unlock_irqrestore(&instance->completion_lock, flags);
1678 * Check if we can restore can_queue
1680 megasas_check_and_restore_queue_depth(instance);
1684 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1687 process_fw_state_change_wq(struct work_struct *work);
1689 void megasas_do_ocr(struct megasas_instance *instance)
1691 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1692 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1693 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1694 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1696 instance->instancet->disable_intr(instance->reg_set);
1697 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1698 instance->issuepend_done = 0;
1700 atomic_set(&instance->fw_outstanding, 0);
1701 megasas_internal_reset_defer_cmds(instance);
1702 process_fw_state_change_wq(&instance->work_init);
1706 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1707 * @instance: Adapter soft state
1709 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
1710 * complete all its outstanding commands. Returns error if one or more IOs
1711 * are pending after this time period. It also marks the controller dead.
1713 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1717 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1719 unsigned long flags;
1720 struct list_head clist_local;
1721 struct megasas_cmd *reset_cmd;
1723 u8 kill_adapter_flag;
1725 spin_lock_irqsave(&instance->hba_lock, flags);
1726 adprecovery = instance->adprecovery;
1727 spin_unlock_irqrestore(&instance->hba_lock, flags);
1729 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1731 INIT_LIST_HEAD(&clist_local);
1732 spin_lock_irqsave(&instance->hba_lock, flags);
1733 list_splice_init(&instance->internal_reset_pending_q,
1735 spin_unlock_irqrestore(&instance->hba_lock, flags);
1737 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1738 for (i = 0; i < wait_time; i++) {
1740 spin_lock_irqsave(&instance->hba_lock, flags);
1741 adprecovery = instance->adprecovery;
1742 spin_unlock_irqrestore(&instance->hba_lock, flags);
1743 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1747 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1748 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1749 spin_lock_irqsave(&instance->hba_lock, flags);
1750 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1751 spin_unlock_irqrestore(&instance->hba_lock, flags);
1756 while (!list_empty(&clist_local)) {
1757 reset_cmd = list_entry((&clist_local)->next,
1758 struct megasas_cmd, list);
1759 list_del_init(&reset_cmd->list);
1760 if (reset_cmd->scmd) {
1761 reset_cmd->scmd->result = DID_RESET << 16;
1762 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
1763 reset_index, reset_cmd,
1764 reset_cmd->scmd->cmnd[0]);
1766 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1767 megasas_return_cmd(instance, reset_cmd);
1768 } else if (reset_cmd->sync_cmd) {
1769 printk(KERN_NOTICE "megasas:%p synch cmds"
1773 reset_cmd->cmd_status = ENODATA;
1774 instance->instancet->fire_cmd(instance,
1775 reset_cmd->frame_phys_addr,
1776 0, instance->reg_set);
1778 printk(KERN_NOTICE "megasas: %p unexpected"
1788 for (i = 0; i < wait_time; i++) {
1790 int outstanding = atomic_read(&instance->fw_outstanding);
1795 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1796 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1797 "commands to complete\n",i,outstanding);
1799 * Call cmd completion routine. Cmd to be
1800 * be completed directly without depending on isr.
1802 megasas_complete_cmd_dpc((unsigned long)instance);
1809 kill_adapter_flag = 0;
1811 fw_state = instance->instancet->read_fw_status_reg(
1812 instance->reg_set) & MFI_STATE_MASK;
1813 if ((fw_state == MFI_STATE_FAULT) &&
1814 (instance->disableOnlineCtrlReset == 0)) {
1816 kill_adapter_flag = 2;
1819 megasas_do_ocr(instance);
1820 kill_adapter_flag = 1;
1822 /* wait for 1 secs to let FW finish the pending cmds */
1828 if (atomic_read(&instance->fw_outstanding) &&
1829 !kill_adapter_flag) {
1830 if (instance->disableOnlineCtrlReset == 0) {
1832 megasas_do_ocr(instance);
1834 /* wait for 5 secs to let FW finish the pending cmds */
1835 for (i = 0; i < wait_time; i++) {
1837 atomic_read(&instance->fw_outstanding);
1845 if (atomic_read(&instance->fw_outstanding) ||
1846 (kill_adapter_flag == 2)) {
1847 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
1849 * Send signal to FW to stop processing any pending cmds.
1850 * The controller will be taken offline by the OS now.
1852 if ((instance->pdev->device ==
1853 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1854 (instance->pdev->device ==
1855 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1856 writel(MFI_STOP_ADP,
1857 &instance->reg_set->doorbell);
1859 writel(MFI_STOP_ADP,
1860 &instance->reg_set->inbound_doorbell);
1862 megasas_dump_pending_frames(instance);
1863 spin_lock_irqsave(&instance->hba_lock, flags);
1864 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1865 spin_unlock_irqrestore(&instance->hba_lock, flags);
1869 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1875 * megasas_generic_reset - Generic reset routine
1876 * @scmd: Mid-layer SCSI command
1878 * This routine implements a generic reset handler for device, bus and host
1879 * reset requests. Device, bus and host specific reset handlers can use this
1880 * function after they do their specific tasks.
1882 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1885 struct megasas_instance *instance;
1887 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1889 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1890 scmd->cmnd[0], scmd->retries);
1892 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1893 printk(KERN_ERR "megasas: cannot recover from previous reset "
1898 ret_val = megasas_wait_for_outstanding(instance);
1899 if (ret_val == SUCCESS)
1900 printk(KERN_NOTICE "megasas: reset successful \n");
1902 printk(KERN_ERR "megasas: failed to do reset\n");
1908 * megasas_reset_timer - quiesce the adapter if required
1911 * Sets the FW busy flag and reduces the host->can_queue if the
1912 * cmd has not been completed within the timeout period.
1915 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1917 struct megasas_instance *instance;
1918 unsigned long flags;
1920 if (time_after(jiffies, scmd->jiffies_at_alloc +
1921 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1922 return BLK_EH_NOT_HANDLED;
1925 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1926 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1927 /* FW is busy, throttle IO */
1928 spin_lock_irqsave(instance->host->host_lock, flags);
1930 instance->host->can_queue = 16;
1931 instance->last_time = jiffies;
1932 instance->flag |= MEGASAS_FW_BUSY;
1934 spin_unlock_irqrestore(instance->host->host_lock, flags);
1936 return BLK_EH_RESET_TIMER;
1940 * megasas_reset_device - Device reset handler entry point
1942 static int megasas_reset_device(struct scsi_cmnd *scmd)
1947 * First wait for all commands to complete
1949 ret = megasas_generic_reset(scmd);
1955 * megasas_reset_bus_host - Bus & host reset handler entry point
1957 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1960 struct megasas_instance *instance;
1961 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1964 * First wait for all commands to complete
1966 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1967 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
1968 ret = megasas_reset_fusion(scmd->device->host);
1970 ret = megasas_generic_reset(scmd);
1976 * megasas_bios_param - Returns disk geometry for a disk
1977 * @sdev: device handle
1978 * @bdev: block device
1979 * @capacity: drive capacity
1980 * @geom: geometry parameters
1983 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1984 sector_t capacity, int geom[])
1990 /* Default heads (64) & sectors (32) */
1994 tmp = heads * sectors;
1995 cylinders = capacity;
1997 sector_div(cylinders, tmp);
2000 * Handle extended translation size for logical drives > 1Gb
2003 if (capacity >= 0x200000) {
2006 tmp = heads*sectors;
2007 cylinders = capacity;
2008 sector_div(cylinders, tmp);
2013 geom[2] = cylinders;
2018 static void megasas_aen_polling(struct work_struct *work);
2021 * megasas_service_aen - Processes an event notification
2022 * @instance: Adapter soft state
2023 * @cmd: AEN command completed by the ISR
2025 * For AEN, driver sends a command down to FW that is held by the FW till an
2026 * event occurs. When an event of interest occurs, FW completes the command
2027 * that it was previously holding.
2029 * This routines sends SIGIO signal to processes that have registered with the
2033 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2035 unsigned long flags;
2037 * Don't signal app if it is just an aborted previously registered aen
2039 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2040 spin_lock_irqsave(&poll_aen_lock, flags);
2041 megasas_poll_wait_aen = 1;
2042 spin_unlock_irqrestore(&poll_aen_lock, flags);
2043 wake_up(&megasas_poll_wait);
2044 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2049 instance->aen_cmd = NULL;
2050 megasas_return_cmd(instance, cmd);
2052 if ((instance->unload == 0) &&
2053 ((instance->issuepend_done == 1))) {
2054 struct megasas_aen_event *ev;
2055 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2057 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2059 ev->instance = instance;
2061 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
2062 schedule_delayed_work(
2063 (struct delayed_work *)&ev->hotplug_work, 0);
2068 static int megasas_change_queue_depth(struct scsi_device *sdev,
2069 int queue_depth, int reason)
2071 if (reason != SCSI_QDEPTH_DEFAULT)
2074 if (queue_depth > sdev->host->can_queue)
2075 queue_depth = sdev->host->can_queue;
2076 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2083 * Scsi host template for megaraid_sas driver
2085 static struct scsi_host_template megasas_template = {
2087 .module = THIS_MODULE,
2088 .name = "LSI SAS based MegaRAID driver",
2089 .proc_name = "megaraid_sas",
2090 .slave_configure = megasas_slave_configure,
2091 .slave_alloc = megasas_slave_alloc,
2092 .queuecommand = megasas_queue_command,
2093 .eh_device_reset_handler = megasas_reset_device,
2094 .eh_bus_reset_handler = megasas_reset_bus_host,
2095 .eh_host_reset_handler = megasas_reset_bus_host,
2096 .eh_timed_out = megasas_reset_timer,
2097 .bios_param = megasas_bios_param,
2098 .use_clustering = ENABLE_CLUSTERING,
2099 .change_queue_depth = megasas_change_queue_depth,
2103 * megasas_complete_int_cmd - Completes an internal command
2104 * @instance: Adapter soft state
2105 * @cmd: Command to be completed
2107 * The megasas_issue_blocked_cmd() function waits for a command to complete
2108 * after it issues a command. This function wakes up that waiting routine by
2109 * calling wake_up() on the wait queue.
2112 megasas_complete_int_cmd(struct megasas_instance *instance,
2113 struct megasas_cmd *cmd)
2115 cmd->cmd_status = cmd->frame->io.cmd_status;
2117 if (cmd->cmd_status == ENODATA) {
2118 cmd->cmd_status = 0;
2120 wake_up(&instance->int_cmd_wait_q);
2124 * megasas_complete_abort - Completes aborting a command
2125 * @instance: Adapter soft state
2126 * @cmd: Cmd that was issued to abort another cmd
2128 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2129 * after it issues an abort on a previously issued command. This function
2130 * wakes up all functions waiting on the same wait queue.
2133 megasas_complete_abort(struct megasas_instance *instance,
2134 struct megasas_cmd *cmd)
2136 if (cmd->sync_cmd) {
2138 cmd->cmd_status = 0;
2139 wake_up(&instance->abort_cmd_wait_q);
2146 * megasas_complete_cmd - Completes a command
2147 * @instance: Adapter soft state
2148 * @cmd: Command to be completed
2149 * @alt_status: If non-zero, use this value as status to
2150 * SCSI mid-layer instead of the value returned
2151 * by the FW. This should be used if caller wants
2152 * an alternate status (as in the case of aborted
2156 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2160 struct megasas_header *hdr = &cmd->frame->hdr;
2161 unsigned long flags;
2162 struct fusion_context *fusion = instance->ctrl_context;
2164 /* flag for the retry reset */
2165 cmd->retry_for_fw_reset = 0;
2168 cmd->scmd->SCp.ptr = NULL;
2171 case MFI_CMD_INVALID:
2172 /* Some older 1068 controller FW may keep a pended
2173 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2174 when booting the kdump kernel. Ignore this command to
2175 prevent a kernel panic on shutdown of the kdump kernel. */
2176 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2178 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2179 "other than PERC5, please upgrade your firmware.\n");
2181 case MFI_CMD_PD_SCSI_IO:
2182 case MFI_CMD_LD_SCSI_IO:
2185 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2186 * issued either through an IO path or an IOCTL path. If it
2187 * was via IOCTL, we will send it to internal completion.
2189 if (cmd->sync_cmd) {
2191 megasas_complete_int_cmd(instance, cmd);
2195 case MFI_CMD_LD_READ:
2196 case MFI_CMD_LD_WRITE:
2199 cmd->scmd->result = alt_status << 16;
2205 atomic_dec(&instance->fw_outstanding);
2207 scsi_dma_unmap(cmd->scmd);
2208 cmd->scmd->scsi_done(cmd->scmd);
2209 megasas_return_cmd(instance, cmd);
2214 switch (hdr->cmd_status) {
2217 cmd->scmd->result = DID_OK << 16;
2220 case MFI_STAT_SCSI_IO_FAILED:
2221 case MFI_STAT_LD_INIT_IN_PROGRESS:
2223 (DID_ERROR << 16) | hdr->scsi_status;
2226 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2228 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2230 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2231 memset(cmd->scmd->sense_buffer, 0,
2232 SCSI_SENSE_BUFFERSIZE);
2233 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2236 cmd->scmd->result |= DRIVER_SENSE << 24;
2241 case MFI_STAT_LD_OFFLINE:
2242 case MFI_STAT_DEVICE_NOT_FOUND:
2243 cmd->scmd->result = DID_BAD_TARGET << 16;
2247 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2249 cmd->scmd->result = DID_ERROR << 16;
2253 atomic_dec(&instance->fw_outstanding);
2255 scsi_dma_unmap(cmd->scmd);
2256 cmd->scmd->scsi_done(cmd->scmd);
2257 megasas_return_cmd(instance, cmd);
2264 /* Check for LD map update */
2265 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2266 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2267 spin_lock_irqsave(instance->host->host_lock, flags);
2268 if (cmd->frame->hdr.cmd_status != 0) {
2269 if (cmd->frame->hdr.cmd_status !=
2271 printk(KERN_WARNING "megasas: map sync"
2272 "failed, status = 0x%x.\n",
2273 cmd->frame->hdr.cmd_status);
2275 megasas_return_cmd(instance, cmd);
2276 spin_unlock_irqrestore(
2277 instance->host->host_lock,
2283 megasas_return_cmd(instance, cmd);
2284 if (MR_ValidateMapInfo(
2285 fusion->ld_map[(instance->map_id & 1)],
2286 fusion->load_balance_info))
2287 fusion->fast_path_io = 1;
2289 fusion->fast_path_io = 0;
2290 megasas_sync_map_info(instance);
2291 spin_unlock_irqrestore(instance->host->host_lock,
2295 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2296 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2297 spin_lock_irqsave(&poll_aen_lock, flags);
2298 megasas_poll_wait_aen = 0;
2299 spin_unlock_irqrestore(&poll_aen_lock, flags);
2303 * See if got an event notification
2305 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2306 megasas_service_aen(instance, cmd);
2308 megasas_complete_int_cmd(instance, cmd);
2314 * Cmd issued to abort another cmd returned
2316 megasas_complete_abort(instance, cmd);
2320 printk("megasas: Unknown command completed! [0x%X]\n",
2327 * megasas_issue_pending_cmds_again - issue all pending cmds
2328 * in FW again because of the fw reset
2329 * @instance: Adapter soft state
2332 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2334 struct megasas_cmd *cmd;
2335 struct list_head clist_local;
2336 union megasas_evt_class_locale class_locale;
2337 unsigned long flags;
2340 INIT_LIST_HEAD(&clist_local);
2341 spin_lock_irqsave(&instance->hba_lock, flags);
2342 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2343 spin_unlock_irqrestore(&instance->hba_lock, flags);
2345 while (!list_empty(&clist_local)) {
2346 cmd = list_entry((&clist_local)->next,
2347 struct megasas_cmd, list);
2348 list_del_init(&cmd->list);
2350 if (cmd->sync_cmd || cmd->scmd) {
2351 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2352 "detected to be pending while HBA reset.\n",
2353 cmd, cmd->scmd, cmd->sync_cmd);
2355 cmd->retry_for_fw_reset++;
2357 if (cmd->retry_for_fw_reset == 3) {
2358 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2359 "was tried multiple times during reset."
2360 "Shutting down the HBA\n",
2361 cmd, cmd->scmd, cmd->sync_cmd);
2362 megaraid_sas_kill_hba(instance);
2364 instance->adprecovery =
2365 MEGASAS_HW_CRITICAL_ERROR;
2370 if (cmd->sync_cmd == 1) {
2372 printk(KERN_NOTICE "megaraid_sas: unexpected"
2373 "cmd attached to internal command!\n");
2375 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2376 "on the internal reset queue,"
2377 "issue it again.\n", cmd);
2378 cmd->cmd_status = ENODATA;
2379 instance->instancet->fire_cmd(instance,
2380 cmd->frame_phys_addr ,
2381 0, instance->reg_set);
2382 } else if (cmd->scmd) {
2383 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
2384 "detected on the internal queue, issue again.\n",
2385 cmd, cmd->scmd->cmnd[0]);
2387 atomic_inc(&instance->fw_outstanding);
2388 instance->instancet->fire_cmd(instance,
2389 cmd->frame_phys_addr,
2390 cmd->frame_count-1, instance->reg_set);
2392 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2393 "internal reset defer list while re-issue!!\n",
2398 if (instance->aen_cmd) {
2399 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2400 megasas_return_cmd(instance, instance->aen_cmd);
2402 instance->aen_cmd = NULL;
2406 * Initiate AEN (Asynchronous Event Notification)
2408 seq_num = instance->last_seq_num;
2409 class_locale.members.reserved = 0;
2410 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2411 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2413 megasas_register_aen(instance, seq_num, class_locale.word);
2417 * Move the internal reset pending commands to a deferred queue.
2419 * We move the commands pending at internal reset time to a
2420 * pending queue. This queue would be flushed after successful
2421 * completion of the internal reset sequence. if the internal reset
2422 * did not complete in time, the kernel reset handler would flush
2426 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2428 struct megasas_cmd *cmd;
2430 u32 max_cmd = instance->max_fw_cmds;
2432 unsigned long flags;
2435 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2436 for (i = 0; i < max_cmd; i++) {
2437 cmd = instance->cmd_list[i];
2438 if (cmd->sync_cmd == 1 || cmd->scmd) {
2439 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2440 "on the defer queue as internal\n",
2441 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2443 if (!list_empty(&cmd->list)) {
2444 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2445 " moving this cmd:%p, %d %p, it was"
2446 "discovered on some list?\n",
2447 cmd, cmd->sync_cmd, cmd->scmd);
2449 list_del_init(&cmd->list);
2452 list_add_tail(&cmd->list,
2453 &instance->internal_reset_pending_q);
2456 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2461 process_fw_state_change_wq(struct work_struct *work)
2463 struct megasas_instance *instance =
2464 container_of(work, struct megasas_instance, work_init);
2466 unsigned long flags;
2468 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2469 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2470 instance->adprecovery);
2474 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2475 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2476 "state, restarting it...\n");
2478 instance->instancet->disable_intr(instance->reg_set);
2479 atomic_set(&instance->fw_outstanding, 0);
2481 atomic_set(&instance->fw_reset_no_pci_access, 1);
2482 instance->instancet->adp_reset(instance, instance->reg_set);
2483 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2485 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2486 "initiating next stage...\n");
2488 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2489 "state 2 starting...\n");
2491 /*waitting for about 20 second before start the second init*/
2492 for (wait = 0; wait < 30; wait++) {
2496 if (megasas_transition_to_ready(instance, 1)) {
2497 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2499 megaraid_sas_kill_hba(instance);
2500 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2504 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2505 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2506 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2508 *instance->consumer = *instance->producer;
2510 *instance->consumer = 0;
2511 *instance->producer = 0;
2514 megasas_issue_init_mfi(instance);
2516 spin_lock_irqsave(&instance->hba_lock, flags);
2517 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2518 spin_unlock_irqrestore(&instance->hba_lock, flags);
2519 instance->instancet->enable_intr(instance->reg_set);
2521 megasas_issue_pending_cmds_again(instance);
2522 instance->issuepend_done = 1;
2528 * megasas_deplete_reply_queue - Processes all completed commands
2529 * @instance: Adapter soft state
2530 * @alt_status: Alternate status to be returned to
2531 * SCSI mid-layer instead of the status
2532 * returned by the FW
2533 * Note: this must be called with hba lock held
2536 megasas_deplete_reply_queue(struct megasas_instance *instance,
2542 if ((mfiStatus = instance->instancet->check_reset(instance,
2543 instance->reg_set)) == 1) {
2547 if ((mfiStatus = instance->instancet->clear_intr(
2550 /* Hardware may not set outbound_intr_status in MSI-X mode */
2551 if (!instance->msix_vectors)
2555 instance->mfiStatus = mfiStatus;
2557 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2558 fw_state = instance->instancet->read_fw_status_reg(
2559 instance->reg_set) & MFI_STATE_MASK;
2561 if (fw_state != MFI_STATE_FAULT) {
2562 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2566 if ((fw_state == MFI_STATE_FAULT) &&
2567 (instance->disableOnlineCtrlReset == 0)) {
2568 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2570 if ((instance->pdev->device ==
2571 PCI_DEVICE_ID_LSI_SAS1064R) ||
2572 (instance->pdev->device ==
2573 PCI_DEVICE_ID_DELL_PERC5) ||
2574 (instance->pdev->device ==
2575 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2577 *instance->consumer =
2578 MEGASAS_ADPRESET_INPROG_SIGN;
2582 instance->instancet->disable_intr(instance->reg_set);
2583 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2584 instance->issuepend_done = 0;
2586 atomic_set(&instance->fw_outstanding, 0);
2587 megasas_internal_reset_defer_cmds(instance);
2589 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2590 fw_state, instance->adprecovery);
2592 schedule_work(&instance->work_init);
2596 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2597 fw_state, instance->disableOnlineCtrlReset);
2601 tasklet_schedule(&instance->isr_tasklet);
2605 * megasas_isr - isr entry point
2607 static irqreturn_t megasas_isr(int irq, void *devp)
2609 struct megasas_irq_context *irq_context = devp;
2610 struct megasas_instance *instance = irq_context->instance;
2611 unsigned long flags;
2614 if (atomic_read(&instance->fw_reset_no_pci_access))
2617 spin_lock_irqsave(&instance->hba_lock, flags);
2618 rc = megasas_deplete_reply_queue(instance, DID_OK);
2619 spin_unlock_irqrestore(&instance->hba_lock, flags);
2625 * megasas_transition_to_ready - Move the FW to READY state
2626 * @instance: Adapter soft state
2628 * During the initialization, FW passes can potentially be in any one of
2629 * several possible states. If the FW in operational, waiting-for-handshake
2630 * states, driver must take steps to bring it to ready state. Otherwise, it
2631 * has to wait for the ready state.
2634 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
2640 u32 abs_state, curr_abs_state;
2642 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2644 if (fw_state != MFI_STATE_READY)
2645 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2648 while (fw_state != MFI_STATE_READY) {
2651 instance->instancet->read_fw_status_reg(instance->reg_set);
2655 case MFI_STATE_FAULT:
2656 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
2658 max_wait = MEGASAS_RESET_WAIT_TIME;
2659 cur_state = MFI_STATE_FAULT;
2664 case MFI_STATE_WAIT_HANDSHAKE:
2666 * Set the CLR bit in inbound doorbell
2668 if ((instance->pdev->device ==
2669 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2670 (instance->pdev->device ==
2671 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2672 (instance->pdev->device ==
2673 PCI_DEVICE_ID_LSI_FUSION) ||
2674 (instance->pdev->device ==
2675 PCI_DEVICE_ID_LSI_INVADER)) {
2677 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2678 &instance->reg_set->doorbell);
2681 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2682 &instance->reg_set->inbound_doorbell);
2685 max_wait = MEGASAS_RESET_WAIT_TIME;
2686 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2689 case MFI_STATE_BOOT_MESSAGE_PENDING:
2690 if ((instance->pdev->device ==
2691 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2692 (instance->pdev->device ==
2693 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2694 (instance->pdev->device ==
2695 PCI_DEVICE_ID_LSI_FUSION) ||
2696 (instance->pdev->device ==
2697 PCI_DEVICE_ID_LSI_INVADER)) {
2698 writel(MFI_INIT_HOTPLUG,
2699 &instance->reg_set->doorbell);
2701 writel(MFI_INIT_HOTPLUG,
2702 &instance->reg_set->inbound_doorbell);
2704 max_wait = MEGASAS_RESET_WAIT_TIME;
2705 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2708 case MFI_STATE_OPERATIONAL:
2710 * Bring it to READY state; assuming max wait 10 secs
2712 instance->instancet->disable_intr(instance->reg_set);
2713 if ((instance->pdev->device ==
2714 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2715 (instance->pdev->device ==
2716 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2717 (instance->pdev->device
2718 == PCI_DEVICE_ID_LSI_FUSION) ||
2719 (instance->pdev->device
2720 == PCI_DEVICE_ID_LSI_INVADER)) {
2721 writel(MFI_RESET_FLAGS,
2722 &instance->reg_set->doorbell);
2723 if ((instance->pdev->device ==
2724 PCI_DEVICE_ID_LSI_FUSION) ||
2725 (instance->pdev->device ==
2726 PCI_DEVICE_ID_LSI_INVADER)) {
2727 for (i = 0; i < (10 * 1000); i += 20) {
2738 writel(MFI_RESET_FLAGS,
2739 &instance->reg_set->inbound_doorbell);
2741 max_wait = MEGASAS_RESET_WAIT_TIME;
2742 cur_state = MFI_STATE_OPERATIONAL;
2745 case MFI_STATE_UNDEFINED:
2747 * This state should not last for more than 2 seconds
2749 max_wait = MEGASAS_RESET_WAIT_TIME;
2750 cur_state = MFI_STATE_UNDEFINED;
2753 case MFI_STATE_BB_INIT:
2754 max_wait = MEGASAS_RESET_WAIT_TIME;
2755 cur_state = MFI_STATE_BB_INIT;
2758 case MFI_STATE_FW_INIT:
2759 max_wait = MEGASAS_RESET_WAIT_TIME;
2760 cur_state = MFI_STATE_FW_INIT;
2763 case MFI_STATE_FW_INIT_2:
2764 max_wait = MEGASAS_RESET_WAIT_TIME;
2765 cur_state = MFI_STATE_FW_INIT_2;
2768 case MFI_STATE_DEVICE_SCAN:
2769 max_wait = MEGASAS_RESET_WAIT_TIME;
2770 cur_state = MFI_STATE_DEVICE_SCAN;
2773 case MFI_STATE_FLUSH_CACHE:
2774 max_wait = MEGASAS_RESET_WAIT_TIME;
2775 cur_state = MFI_STATE_FLUSH_CACHE;
2779 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2785 * The cur_state should not last for more than max_wait secs
2787 for (i = 0; i < (max_wait * 1000); i++) {
2788 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
2791 instance->instancet->read_fw_status_reg(instance->reg_set);
2793 if (abs_state == curr_abs_state) {
2800 * Return error if fw_state hasn't changed after max_wait
2802 if (curr_abs_state == abs_state) {
2803 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2804 "in %d secs\n", fw_state, max_wait);
2808 printk(KERN_INFO "megasas: FW now in Ready state\n");
2814 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2815 * @instance: Adapter soft state
2817 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2820 u32 max_cmd = instance->max_mfi_cmds;
2821 struct megasas_cmd *cmd;
2823 if (!instance->frame_dma_pool)
2827 * Return all frames to pool
2829 for (i = 0; i < max_cmd; i++) {
2831 cmd = instance->cmd_list[i];
2834 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2835 cmd->frame_phys_addr);
2838 pci_pool_free(instance->sense_dma_pool, cmd->sense,
2839 cmd->sense_phys_addr);
2843 * Now destroy the pool itself
2845 pci_pool_destroy(instance->frame_dma_pool);
2846 pci_pool_destroy(instance->sense_dma_pool);
2848 instance->frame_dma_pool = NULL;
2849 instance->sense_dma_pool = NULL;
2853 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2854 * @instance: Adapter soft state
2856 * Each command packet has an embedded DMA memory buffer that is used for
2857 * filling MFI frame and the SG list that immediately follows the frame. This
2858 * function creates those DMA memory buffers for each command packet by using
2859 * PCI pool facility.
2861 static int megasas_create_frame_pool(struct megasas_instance *instance)
2869 struct megasas_cmd *cmd;
2871 max_cmd = instance->max_mfi_cmds;
2874 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2875 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2877 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2878 sizeof(struct megasas_sge32);
2880 if (instance->flag_ieee) {
2881 sge_sz = sizeof(struct megasas_sge_skinny);
2885 * Calculated the number of 64byte frames required for SGL
2887 sgl_sz = sge_sz * instance->max_num_sge;
2888 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2892 * We need one extra frame for the MFI command
2896 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2898 * Use DMA pool facility provided by PCI layer
2900 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2901 instance->pdev, total_sz, 64,
2904 if (!instance->frame_dma_pool) {
2905 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2909 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2910 instance->pdev, 128, 4, 0);
2912 if (!instance->sense_dma_pool) {
2913 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2915 pci_pool_destroy(instance->frame_dma_pool);
2916 instance->frame_dma_pool = NULL;
2922 * Allocate and attach a frame to each of the commands in cmd_list.
2923 * By making cmd->index as the context instead of the &cmd, we can
2924 * always use 32bit context regardless of the architecture
2926 for (i = 0; i < max_cmd; i++) {
2928 cmd = instance->cmd_list[i];
2930 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2931 GFP_KERNEL, &cmd->frame_phys_addr);
2933 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2934 GFP_KERNEL, &cmd->sense_phys_addr);
2937 * megasas_teardown_frame_pool() takes care of freeing
2938 * whatever has been allocated
2940 if (!cmd->frame || !cmd->sense) {
2941 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2942 megasas_teardown_frame_pool(instance);
2946 memset(cmd->frame, 0, total_sz);
2947 cmd->frame->io.context = cmd->index;
2948 cmd->frame->io.pad_0 = 0;
2949 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
2950 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
2952 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
2959 * megasas_free_cmds - Free all the cmds in the free cmd pool
2960 * @instance: Adapter soft state
2962 void megasas_free_cmds(struct megasas_instance *instance)
2965 /* First free the MFI frame pool */
2966 megasas_teardown_frame_pool(instance);
2968 /* Free all the commands in the cmd_list */
2969 for (i = 0; i < instance->max_mfi_cmds; i++)
2971 kfree(instance->cmd_list[i]);
2973 /* Free the cmd_list buffer itself */
2974 kfree(instance->cmd_list);
2975 instance->cmd_list = NULL;
2977 INIT_LIST_HEAD(&instance->cmd_pool);
2981 * megasas_alloc_cmds - Allocates the command packets
2982 * @instance: Adapter soft state
2984 * Each command that is issued to the FW, whether IO commands from the OS or
2985 * internal commands like IOCTLs, are wrapped in local data structure called
2986 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2989 * Each frame has a 32-bit field called context (tag). This context is used
2990 * to get back the megasas_cmd from the frame when a frame gets completed in
2991 * the ISR. Typically the address of the megasas_cmd itself would be used as
2992 * the context. But we wanted to keep the differences between 32 and 64 bit
2993 * systems to the mininum. We always use 32 bit integers for the context. In
2994 * this driver, the 32 bit values are the indices into an array cmd_list.
2995 * This array is used only to look up the megasas_cmd given the context. The
2996 * free commands themselves are maintained in a linked list called cmd_pool.
2998 int megasas_alloc_cmds(struct megasas_instance *instance)
3003 struct megasas_cmd *cmd;
3005 max_cmd = instance->max_mfi_cmds;
3008 * instance->cmd_list is an array of struct megasas_cmd pointers.
3009 * Allocate the dynamic array first and then allocate individual
3012 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3014 if (!instance->cmd_list) {
3015 printk(KERN_DEBUG "megasas: out of memory\n");
3019 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3021 for (i = 0; i < max_cmd; i++) {
3022 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3025 if (!instance->cmd_list[i]) {
3027 for (j = 0; j < i; j++)
3028 kfree(instance->cmd_list[j]);
3030 kfree(instance->cmd_list);
3031 instance->cmd_list = NULL;
3038 * Add all the commands to command pool (instance->cmd_pool)
3040 for (i = 0; i < max_cmd; i++) {
3041 cmd = instance->cmd_list[i];
3042 memset(cmd, 0, sizeof(struct megasas_cmd));
3045 cmd->instance = instance;
3047 list_add_tail(&cmd->list, &instance->cmd_pool);
3051 * Create a frame pool and assign one frame to each cmd
3053 if (megasas_create_frame_pool(instance)) {
3054 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3055 megasas_free_cmds(instance);
3062 * megasas_get_pd_list_info - Returns FW's pd_list structure
3063 * @instance: Adapter soft state
3064 * @pd_list: pd_list structure
3066 * Issues an internal command (DCMD) to get the FW's controller PD
3067 * list structure. This information is mainly used to find out SYSTEM
3068 * supported by the FW.
3071 megasas_get_pd_list(struct megasas_instance *instance)
3073 int ret = 0, pd_index = 0;
3074 struct megasas_cmd *cmd;
3075 struct megasas_dcmd_frame *dcmd;
3076 struct MR_PD_LIST *ci;
3077 struct MR_PD_ADDRESS *pd_addr;
3078 dma_addr_t ci_h = 0;
3080 cmd = megasas_get_cmd(instance);
3083 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3087 dcmd = &cmd->frame->dcmd;
3089 ci = pci_alloc_consistent(instance->pdev,
3090 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3093 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3094 megasas_return_cmd(instance, cmd);
3098 memset(ci, 0, sizeof(*ci));
3099 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3101 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3102 dcmd->mbox.b[1] = 0;
3103 dcmd->cmd = MFI_CMD_DCMD;
3104 dcmd->cmd_status = 0xFF;
3105 dcmd->sge_count = 1;
3106 dcmd->flags = MFI_FRAME_DIR_READ;
3109 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3110 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3111 dcmd->sgl.sge32[0].phys_addr = ci_h;
3112 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3114 if (!megasas_issue_polled(instance, cmd)) {
3121 * the following function will get the instance PD LIST.
3128 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3130 memset(instance->pd_list, 0,
3131 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3133 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3135 instance->pd_list[pd_addr->deviceId].tid =
3137 instance->pd_list[pd_addr->deviceId].driveType =
3138 pd_addr->scsiDevType;
3139 instance->pd_list[pd_addr->deviceId].driveState =
3145 pci_free_consistent(instance->pdev,
3146 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3148 megasas_return_cmd(instance, cmd);
3154 * megasas_get_ld_list_info - Returns FW's ld_list structure
3155 * @instance: Adapter soft state
3156 * @ld_list: ld_list structure
3158 * Issues an internal command (DCMD) to get the FW's controller PD
3159 * list structure. This information is mainly used to find out SYSTEM
3160 * supported by the FW.
3163 megasas_get_ld_list(struct megasas_instance *instance)
3165 int ret = 0, ld_index = 0, ids = 0;
3166 struct megasas_cmd *cmd;
3167 struct megasas_dcmd_frame *dcmd;
3168 struct MR_LD_LIST *ci;
3169 dma_addr_t ci_h = 0;
3171 cmd = megasas_get_cmd(instance);
3174 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3178 dcmd = &cmd->frame->dcmd;
3180 ci = pci_alloc_consistent(instance->pdev,
3181 sizeof(struct MR_LD_LIST),
3185 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3186 megasas_return_cmd(instance, cmd);
3190 memset(ci, 0, sizeof(*ci));
3191 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3193 dcmd->cmd = MFI_CMD_DCMD;
3194 dcmd->cmd_status = 0xFF;
3195 dcmd->sge_count = 1;
3196 dcmd->flags = MFI_FRAME_DIR_READ;
3198 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3199 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3200 dcmd->sgl.sge32[0].phys_addr = ci_h;
3201 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3204 if (!megasas_issue_polled(instance, cmd)) {
3210 /* the following function will get the instance PD LIST */
3212 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
3213 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3215 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3216 if (ci->ldList[ld_index].state != 0) {
3217 ids = ci->ldList[ld_index].ref.targetId;
3218 instance->ld_ids[ids] =
3219 ci->ldList[ld_index].ref.targetId;
3224 pci_free_consistent(instance->pdev,
3225 sizeof(struct MR_LD_LIST),
3229 megasas_return_cmd(instance, cmd);
3234 * megasas_get_controller_info - Returns FW's controller structure
3235 * @instance: Adapter soft state
3236 * @ctrl_info: Controller information structure
3238 * Issues an internal command (DCMD) to get the FW's controller structure.
3239 * This information is mainly used to find out the maximum IO transfer per
3240 * command supported by the FW.
3243 megasas_get_ctrl_info(struct megasas_instance *instance,
3244 struct megasas_ctrl_info *ctrl_info)
3247 struct megasas_cmd *cmd;
3248 struct megasas_dcmd_frame *dcmd;
3249 struct megasas_ctrl_info *ci;
3250 dma_addr_t ci_h = 0;
3252 cmd = megasas_get_cmd(instance);
3255 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3259 dcmd = &cmd->frame->dcmd;
3261 ci = pci_alloc_consistent(instance->pdev,
3262 sizeof(struct megasas_ctrl_info), &ci_h);
3265 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3266 megasas_return_cmd(instance, cmd);
3270 memset(ci, 0, sizeof(*ci));
3271 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3273 dcmd->cmd = MFI_CMD_DCMD;
3274 dcmd->cmd_status = 0xFF;
3275 dcmd->sge_count = 1;
3276 dcmd->flags = MFI_FRAME_DIR_READ;
3279 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3280 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3281 dcmd->sgl.sge32[0].phys_addr = ci_h;
3282 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3284 if (!megasas_issue_polled(instance, cmd)) {
3286 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3291 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3294 megasas_return_cmd(instance, cmd);
3299 * megasas_issue_init_mfi - Initializes the FW
3300 * @instance: Adapter soft state
3302 * Issues the INIT MFI cmd
3305 megasas_issue_init_mfi(struct megasas_instance *instance)
3309 struct megasas_cmd *cmd;
3311 struct megasas_init_frame *init_frame;
3312 struct megasas_init_queue_info *initq_info;
3313 dma_addr_t init_frame_h;
3314 dma_addr_t initq_info_h;
3317 * Prepare a init frame. Note the init frame points to queue info
3318 * structure. Each frame has SGL allocated after first 64 bytes. For
3319 * this frame - since we don't need any SGL - we use SGL's space as
3320 * queue info structure
3322 * We will not get a NULL command below. We just created the pool.
3324 cmd = megasas_get_cmd(instance);
3326 init_frame = (struct megasas_init_frame *)cmd->frame;
3327 initq_info = (struct megasas_init_queue_info *)
3328 ((unsigned long)init_frame + 64);
3330 init_frame_h = cmd->frame_phys_addr;
3331 initq_info_h = init_frame_h + 64;
3333 context = init_frame->context;
3334 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3335 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3336 init_frame->context = context;
3338 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3339 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3341 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3342 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3344 init_frame->cmd = MFI_CMD_INIT;
3345 init_frame->cmd_status = 0xFF;
3346 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3348 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3351 * disable the intr before firing the init frame to FW
3353 instance->instancet->disable_intr(instance->reg_set);
3356 * Issue the init frame in polled mode
3359 if (megasas_issue_polled(instance, cmd)) {
3360 printk(KERN_ERR "megasas: Failed to init firmware\n");
3361 megasas_return_cmd(instance, cmd);
3365 megasas_return_cmd(instance, cmd);
3374 * megasas_start_timer - Initializes a timer object
3375 * @instance: Adapter soft state
3376 * @timer: timer object to be initialized
3377 * @fn: timer function
3378 * @interval: time interval between timer function call
3381 megasas_start_timer(struct megasas_instance *instance,
3382 struct timer_list *timer,
3383 void *fn, unsigned long interval)
3386 timer->expires = jiffies + interval;
3387 timer->data = (unsigned long)instance;
3388 timer->function = fn;
3393 * megasas_io_completion_timer - Timer fn
3394 * @instance_addr: Address of adapter soft state
3396 * Schedules tasklet for cmd completion
3397 * if poll_mode_io is set
3400 megasas_io_completion_timer(unsigned long instance_addr)
3402 struct megasas_instance *instance =
3403 (struct megasas_instance *)instance_addr;
3405 if (atomic_read(&instance->fw_outstanding))
3406 tasklet_schedule(&instance->isr_tasklet);
3410 mod_timer(&instance->io_completion_timer,
3411 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3415 megasas_init_adapter_mfi(struct megasas_instance *instance)
3417 struct megasas_register_set __iomem *reg_set;
3421 reg_set = instance->reg_set;
3424 * Get various operational parameters from status register
3426 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
3428 * Reduce the max supported cmds by 1. This is to ensure that the
3429 * reply_q_sz (1 more than the max cmd that driver may send)
3430 * does not exceed max cmds that the FW can support
3432 instance->max_fw_cmds = instance->max_fw_cmds-1;
3433 instance->max_mfi_cmds = instance->max_fw_cmds;
3434 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
3437 * Create a pool of commands
3439 if (megasas_alloc_cmds(instance))
3440 goto fail_alloc_cmds;
3443 * Allocate memory for reply queue. Length of reply queue should
3444 * be _one_ more than the maximum commands handled by the firmware.
3446 * Note: When FW completes commands, it places corresponding contex
3447 * values in this circular reply queue. This circular queue is a fairly
3448 * typical producer-consumer queue. FW is the producer (of completed
3449 * commands) and the driver is the consumer.
3451 context_sz = sizeof(u32);
3452 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3454 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3456 &instance->reply_queue_h);
3458 if (!instance->reply_queue) {
3459 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3460 goto fail_reply_queue;
3463 if (megasas_issue_init_mfi(instance))
3466 instance->fw_support_ieee = 0;
3467 instance->fw_support_ieee =
3468 (instance->instancet->read_fw_status_reg(reg_set) &
3471 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3472 instance->fw_support_ieee);
3474 if (instance->fw_support_ieee)
3475 instance->flag_ieee = 1;
3481 pci_free_consistent(instance->pdev, reply_q_sz,
3482 instance->reply_queue, instance->reply_queue_h);
3484 megasas_free_cmds(instance);
3491 * megasas_init_fw - Initializes the FW
3492 * @instance: Adapter soft state
3494 * This is the main function for initializing firmware
3497 static int megasas_init_fw(struct megasas_instance *instance)
3501 u32 tmp_sectors, msix_enable;
3502 resource_size_t base_addr;
3503 struct megasas_register_set __iomem *reg_set;
3504 struct megasas_ctrl_info *ctrl_info;
3505 unsigned long bar_list;
3508 /* Find first memory bar */
3509 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3510 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3511 if (pci_request_selected_regions(instance->pdev, instance->bar,
3513 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3517 base_addr = pci_resource_start(instance->pdev, instance->bar);
3518 instance->reg_set = ioremap_nocache(base_addr, 8192);
3520 if (!instance->reg_set) {
3521 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3525 reg_set = instance->reg_set;
3527 switch (instance->pdev->device) {
3528 case PCI_DEVICE_ID_LSI_FUSION:
3529 case PCI_DEVICE_ID_LSI_INVADER:
3530 instance->instancet = &megasas_instance_template_fusion;
3532 case PCI_DEVICE_ID_LSI_SAS1078R:
3533 case PCI_DEVICE_ID_LSI_SAS1078DE:
3534 instance->instancet = &megasas_instance_template_ppc;
3536 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3537 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3538 instance->instancet = &megasas_instance_template_gen2;
3540 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3541 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3542 instance->instancet = &megasas_instance_template_skinny;
3544 case PCI_DEVICE_ID_LSI_SAS1064R:
3545 case PCI_DEVICE_ID_DELL_PERC5:
3547 instance->instancet = &megasas_instance_template_xscale;
3551 if (megasas_transition_to_ready(instance, 0)) {
3552 atomic_set(&instance->fw_reset_no_pci_access, 1);
3553 instance->instancet->adp_reset
3554 (instance, instance->reg_set);
3555 atomic_set(&instance->fw_reset_no_pci_access, 0);
3556 dev_info(&instance->pdev->dev,
3557 "megasas: FW restarted successfully from %s!\n",
3560 /*waitting for about 30 second before retry*/
3563 if (megasas_transition_to_ready(instance, 0))
3564 goto fail_ready_state;
3567 /* Check if MSI-X is supported while in ready state */
3568 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3570 if (msix_enable && !msix_disable) {
3571 /* Check max MSI-X vectors */
3572 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3573 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3574 instance->msix_vectors = (readl(&instance->reg_set->
3575 outbound_scratch_pad_2
3578 instance->msix_vectors = 1;
3579 /* Don't bother allocating more MSI-X vectors than cpus */
3580 instance->msix_vectors = min(instance->msix_vectors,
3581 (unsigned int)num_online_cpus());
3582 for (i = 0; i < instance->msix_vectors; i++)
3583 instance->msixentry[i].entry = i;
3584 i = pci_enable_msix(instance->pdev, instance->msixentry,
3585 instance->msix_vectors);
3588 if (!pci_enable_msix(instance->pdev,
3589 instance->msixentry, i))
3590 instance->msix_vectors = i;
3592 instance->msix_vectors = 0;
3595 instance->msix_vectors = 0;
3598 /* Get operational params, sge flags, send init cmd to controller */
3599 if (instance->instancet->init_adapter(instance))
3600 goto fail_init_adapter;
3602 printk(KERN_ERR "megasas: INIT adapter done\n");
3605 * the following function will get the PD LIST.
3608 memset(instance->pd_list, 0 ,
3609 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3610 megasas_get_pd_list(instance);
3612 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3613 megasas_get_ld_list(instance);
3615 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3618 * Compute the max allowed sectors per IO: The controller info has two
3619 * limits on max sectors. Driver should use the minimum of these two.
3621 * 1 << stripe_sz_ops.min = max sectors per strip
3623 * Note that older firmwares ( < FW ver 30) didn't report information
3624 * to calculate max_sectors_1. So the number ended up as zero always.
3627 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3629 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3630 ctrl_info->max_strips_per_io;
3631 max_sectors_2 = ctrl_info->max_request_size;
3633 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
3634 instance->disableOnlineCtrlReset =
3635 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
3638 instance->max_sectors_per_req = instance->max_num_sge *
3639 SGE_BUFFER_SIZE / 512;
3640 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3641 instance->max_sectors_per_req = tmp_sectors;
3646 * Setup tasklet for cmd completion
3649 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
3650 (unsigned long)instance);
3652 /* Initialize the cmd completion timer */
3654 megasas_start_timer(instance, &instance->io_completion_timer,
3655 megasas_io_completion_timer,
3656 MEGASAS_COMPLETION_TIMER_INTERVAL);
3661 iounmap(instance->reg_set);
3664 pci_release_selected_regions(instance->pdev, instance->bar);
3670 * megasas_release_mfi - Reverses the FW initialization
3671 * @intance: Adapter soft state
3673 static void megasas_release_mfi(struct megasas_instance *instance)
3675 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
3677 if (instance->reply_queue)
3678 pci_free_consistent(instance->pdev, reply_q_sz,
3679 instance->reply_queue, instance->reply_queue_h);
3681 megasas_free_cmds(instance);
3683 iounmap(instance->reg_set);
3685 pci_release_selected_regions(instance->pdev, instance->bar);
3689 * megasas_get_seq_num - Gets latest event sequence numbers
3690 * @instance: Adapter soft state
3691 * @eli: FW event log sequence numbers information
3693 * FW maintains a log of all events in a non-volatile area. Upper layers would
3694 * usually find out the latest sequence number of the events, the seq number at
3695 * the boot etc. They would "read" all the events below the latest seq number
3696 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3697 * number), they would subsribe to AEN (asynchronous event notification) and
3698 * wait for the events to happen.
3701 megasas_get_seq_num(struct megasas_instance *instance,
3702 struct megasas_evt_log_info *eli)
3704 struct megasas_cmd *cmd;
3705 struct megasas_dcmd_frame *dcmd;
3706 struct megasas_evt_log_info *el_info;
3707 dma_addr_t el_info_h = 0;
3709 cmd = megasas_get_cmd(instance);
3715 dcmd = &cmd->frame->dcmd;
3716 el_info = pci_alloc_consistent(instance->pdev,
3717 sizeof(struct megasas_evt_log_info),
3721 megasas_return_cmd(instance, cmd);
3725 memset(el_info, 0, sizeof(*el_info));
3726 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3728 dcmd->cmd = MFI_CMD_DCMD;
3729 dcmd->cmd_status = 0x0;
3730 dcmd->sge_count = 1;
3731 dcmd->flags = MFI_FRAME_DIR_READ;
3734 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3735 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3736 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3737 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3739 megasas_issue_blocked_cmd(instance, cmd);
3742 * Copy the data back into callers buffer
3744 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3746 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3747 el_info, el_info_h);
3749 megasas_return_cmd(instance, cmd);
3755 * megasas_register_aen - Registers for asynchronous event notification
3756 * @instance: Adapter soft state
3757 * @seq_num: The starting sequence number
3758 * @class_locale: Class of the event
3760 * This function subscribes for AEN for events beyond the @seq_num. It requests
3761 * to be notified if and only if the event is of type @class_locale
3764 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3765 u32 class_locale_word)
3768 struct megasas_cmd *cmd;
3769 struct megasas_dcmd_frame *dcmd;
3770 union megasas_evt_class_locale curr_aen;
3771 union megasas_evt_class_locale prev_aen;
3774 * If there an AEN pending already (aen_cmd), check if the
3775 * class_locale of that pending AEN is inclusive of the new
3776 * AEN request we currently have. If it is, then we don't have
3777 * to do anything. In other words, whichever events the current
3778 * AEN request is subscribing to, have already been subscribed
3781 * If the old_cmd is _not_ inclusive, then we have to abort
3782 * that command, form a class_locale that is superset of both
3783 * old and current and re-issue to the FW
3786 curr_aen.word = class_locale_word;
3788 if (instance->aen_cmd) {
3790 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3793 * A class whose enum value is smaller is inclusive of all
3794 * higher values. If a PROGRESS (= -1) was previously
3795 * registered, then a new registration requests for higher
3796 * classes need not be sent to FW. They are automatically
3799 * Locale numbers don't have such hierarchy. They are bitmap
3802 if ((prev_aen.members.class <= curr_aen.members.class) &&
3803 !((prev_aen.members.locale & curr_aen.members.locale) ^
3804 curr_aen.members.locale)) {
3806 * Previously issued event registration includes
3807 * current request. Nothing to do.
3811 curr_aen.members.locale |= prev_aen.members.locale;
3813 if (prev_aen.members.class < curr_aen.members.class)
3814 curr_aen.members.class = prev_aen.members.class;
3816 instance->aen_cmd->abort_aen = 1;
3817 ret_val = megasas_issue_blocked_abort_cmd(instance,
3822 printk(KERN_DEBUG "megasas: Failed to abort "
3823 "previous AEN command\n");
3829 cmd = megasas_get_cmd(instance);
3834 dcmd = &cmd->frame->dcmd;
3836 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3839 * Prepare DCMD for aen registration
3841 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3843 dcmd->cmd = MFI_CMD_DCMD;
3844 dcmd->cmd_status = 0x0;
3845 dcmd->sge_count = 1;
3846 dcmd->flags = MFI_FRAME_DIR_READ;
3849 instance->last_seq_num = seq_num;
3850 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3851 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3852 dcmd->mbox.w[0] = seq_num;
3853 dcmd->mbox.w[1] = curr_aen.word;
3854 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3855 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3857 if (instance->aen_cmd != NULL) {
3858 megasas_return_cmd(instance, cmd);
3863 * Store reference to the cmd used to register for AEN. When an
3864 * application wants us to register for AEN, we have to abort this
3865 * cmd and re-register with a new EVENT LOCALE supplied by that app
3867 instance->aen_cmd = cmd;
3870 * Issue the aen registration frame
3872 instance->instancet->issue_dcmd(instance, cmd);
3878 * megasas_start_aen - Subscribes to AEN during driver load time
3879 * @instance: Adapter soft state
3881 static int megasas_start_aen(struct megasas_instance *instance)
3883 struct megasas_evt_log_info eli;
3884 union megasas_evt_class_locale class_locale;
3887 * Get the latest sequence number from FW
3889 memset(&eli, 0, sizeof(eli));
3891 if (megasas_get_seq_num(instance, &eli))
3895 * Register AEN with FW for latest sequence number plus 1
3897 class_locale.members.reserved = 0;
3898 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3899 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3901 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3906 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3907 * @instance: Adapter soft state
3909 static int megasas_io_attach(struct megasas_instance *instance)
3911 struct Scsi_Host *host = instance->host;
3914 * Export parameters required by SCSI mid-layer
3916 host->irq = instance->pdev->irq;
3917 host->unique_id = instance->unique_id;
3918 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3919 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3921 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3924 instance->max_fw_cmds - MEGASAS_INT_CMDS;
3925 host->this_id = instance->init_id;
3926 host->sg_tablesize = instance->max_num_sge;
3928 if (instance->fw_support_ieee)
3929 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3932 * Check if the module parameter value for max_sectors can be used
3934 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3935 instance->max_sectors_per_req = max_sectors;
3938 if (((instance->pdev->device ==
3939 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3940 (instance->pdev->device ==
3941 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3942 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3943 instance->max_sectors_per_req = max_sectors;
3945 printk(KERN_INFO "megasas: max_sectors should be > 0"
3946 "and <= %d (or < 1MB for GEN2 controller)\n",
3947 instance->max_sectors_per_req);
3952 host->max_sectors = instance->max_sectors_per_req;
3953 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
3954 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3955 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3956 host->max_lun = MEGASAS_MAX_LUN;
3957 host->max_cmd_len = 16;
3959 /* Fusion only supports host reset */
3960 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3961 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3962 host->hostt->eh_device_reset_handler = NULL;
3963 host->hostt->eh_bus_reset_handler = NULL;
3967 * Notify the mid-layer about the new controller
3969 if (scsi_add_host(host, &instance->pdev->dev)) {
3970 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3975 * Trigger SCSI to scan our drives
3977 scsi_scan_host(host);
3982 megasas_set_dma_mask(struct pci_dev *pdev)
3985 * All our contollers are capable of performing 64-bit DMA
3988 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
3990 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3991 goto fail_set_dma_mask;
3994 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3995 goto fail_set_dma_mask;
4004 * megasas_probe_one - PCI hotplug entry point
4005 * @pdev: PCI device structure
4006 * @id: PCI ids of supported hotplugged adapter
4008 static int __devinit
4009 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4011 int rval, pos, i, j;
4012 struct Scsi_Host *host;
4013 struct megasas_instance *instance;
4016 /* Reset MSI-X in the kdump kernel */
4017 if (reset_devices) {
4018 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
4020 pci_read_config_word(pdev, msi_control_reg(pos),
4022 if (control & PCI_MSIX_FLAGS_ENABLE) {
4023 dev_info(&pdev->dev, "resetting MSI-X\n");
4024 pci_write_config_word(pdev,
4025 msi_control_reg(pos),
4027 ~PCI_MSIX_FLAGS_ENABLE);
4033 * Announce PCI information
4035 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4036 pdev->vendor, pdev->device, pdev->subsystem_vendor,
4037 pdev->subsystem_device);
4039 printk("bus %d:slot %d:func %d\n",
4040 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
4043 * PCI prepping: enable device set bus mastering and dma mask
4045 rval = pci_enable_device_mem(pdev);
4051 pci_set_master(pdev);
4053 if (megasas_set_dma_mask(pdev))
4054 goto fail_set_dma_mask;
4056 host = scsi_host_alloc(&megasas_template,
4057 sizeof(struct megasas_instance));
4060 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4061 goto fail_alloc_instance;
4064 instance = (struct megasas_instance *)host->hostdata;
4065 memset(instance, 0, sizeof(*instance));
4066 atomic_set( &instance->fw_reset_no_pci_access, 0 );
4067 instance->pdev = pdev;
4069 switch (instance->pdev->device) {
4070 case PCI_DEVICE_ID_LSI_FUSION:
4071 case PCI_DEVICE_ID_LSI_INVADER:
4073 struct fusion_context *fusion;
4075 instance->ctrl_context =
4076 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4077 if (!instance->ctrl_context) {
4078 printk(KERN_DEBUG "megasas: Failed to allocate "
4079 "memory for Fusion context info\n");
4080 goto fail_alloc_dma_buf;
4082 fusion = instance->ctrl_context;
4083 INIT_LIST_HEAD(&fusion->cmd_pool);
4084 spin_lock_init(&fusion->cmd_pool_lock);
4087 default: /* For all other supported controllers */
4089 instance->producer =
4090 pci_alloc_consistent(pdev, sizeof(u32),
4091 &instance->producer_h);
4092 instance->consumer =
4093 pci_alloc_consistent(pdev, sizeof(u32),
4094 &instance->consumer_h);
4096 if (!instance->producer || !instance->consumer) {
4097 printk(KERN_DEBUG "megasas: Failed to allocate"
4098 "memory for producer, consumer\n");
4099 goto fail_alloc_dma_buf;
4102 *instance->producer = 0;
4103 *instance->consumer = 0;
4107 megasas_poll_wait_aen = 0;
4108 instance->flag_ieee = 0;
4109 instance->ev = NULL;
4110 instance->issuepend_done = 1;
4111 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4112 megasas_poll_wait_aen = 0;
4114 instance->evt_detail = pci_alloc_consistent(pdev,
4116 megasas_evt_detail),
4117 &instance->evt_detail_h);
4119 if (!instance->evt_detail) {
4120 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4121 "event detail structure\n");
4122 goto fail_alloc_dma_buf;
4126 * Initialize locks and queues
4128 INIT_LIST_HEAD(&instance->cmd_pool);
4129 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
4131 atomic_set(&instance->fw_outstanding,0);
4133 init_waitqueue_head(&instance->int_cmd_wait_q);
4134 init_waitqueue_head(&instance->abort_cmd_wait_q);
4136 spin_lock_init(&instance->cmd_pool_lock);
4137 spin_lock_init(&instance->hba_lock);
4138 spin_lock_init(&instance->completion_lock);
4140 mutex_init(&instance->aen_mutex);
4141 mutex_init(&instance->reset_mutex);
4144 * Initialize PCI related and misc parameters
4146 instance->host = host;
4147 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4148 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4150 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4151 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4152 instance->flag_ieee = 1;
4153 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4155 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4157 megasas_dbg_lvl = 0;
4159 instance->unload = 1;
4160 instance->last_time = 0;
4161 instance->disableOnlineCtrlReset = 1;
4163 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4164 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
4165 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4167 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
4170 * Initialize MFI Firmware
4172 if (megasas_init_fw(instance))
4178 if (instance->msix_vectors) {
4179 for (i = 0 ; i < instance->msix_vectors; i++) {
4180 instance->irq_context[i].instance = instance;
4181 instance->irq_context[i].MSIxIndex = i;
4182 if (request_irq(instance->msixentry[i].vector,
4183 instance->instancet->service_isr, 0,
4185 &instance->irq_context[i])) {
4186 printk(KERN_DEBUG "megasas: Failed to "
4187 "register IRQ for vector %d.\n", i);
4188 for (j = 0 ; j < i ; j++)
4190 instance->msixentry[j].vector,
4191 &instance->irq_context[j]);
4196 instance->irq_context[0].instance = instance;
4197 instance->irq_context[0].MSIxIndex = 0;
4198 if (request_irq(pdev->irq, instance->instancet->service_isr,
4199 IRQF_SHARED, "megasas",
4200 &instance->irq_context[0])) {
4201 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4206 instance->instancet->enable_intr(instance->reg_set);
4209 * Store instance in PCI softstate
4211 pci_set_drvdata(pdev, instance);
4214 * Add this controller to megasas_mgmt_info structure so that it
4215 * can be exported to management applications
4217 megasas_mgmt_info.count++;
4218 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4219 megasas_mgmt_info.max_index++;
4222 * Register with SCSI mid-layer
4224 if (megasas_io_attach(instance))
4225 goto fail_io_attach;
4227 instance->unload = 0;
4230 * Initiate AEN (Asynchronous Event Notification)
4232 if (megasas_start_aen(instance)) {
4233 printk(KERN_DEBUG "megasas: start aen failed\n");
4234 goto fail_start_aen;
4241 megasas_mgmt_info.count--;
4242 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4243 megasas_mgmt_info.max_index--;
4245 pci_set_drvdata(pdev, NULL);
4246 instance->instancet->disable_intr(instance->reg_set);
4247 if (instance->msix_vectors)
4248 for (i = 0 ; i < instance->msix_vectors; i++)
4249 free_irq(instance->msixentry[i].vector,
4250 &instance->irq_context[i]);
4252 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4254 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4255 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
4256 megasas_release_fusion(instance);
4258 megasas_release_mfi(instance);
4260 if (instance->msix_vectors)
4261 pci_disable_msix(instance->pdev);
4263 if (instance->evt_detail)
4264 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4265 instance->evt_detail,
4266 instance->evt_detail_h);
4268 if (instance->producer)
4269 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4270 instance->producer_h);
4271 if (instance->consumer)
4272 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4273 instance->consumer_h);
4274 scsi_host_put(host);
4276 fail_alloc_instance:
4278 pci_disable_device(pdev);
4284 * megasas_flush_cache - Requests FW to flush all its caches
4285 * @instance: Adapter soft state
4287 static void megasas_flush_cache(struct megasas_instance *instance)
4289 struct megasas_cmd *cmd;
4290 struct megasas_dcmd_frame *dcmd;
4292 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4295 cmd = megasas_get_cmd(instance);
4300 dcmd = &cmd->frame->dcmd;
4302 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4304 dcmd->cmd = MFI_CMD_DCMD;
4305 dcmd->cmd_status = 0x0;
4306 dcmd->sge_count = 0;
4307 dcmd->flags = MFI_FRAME_DIR_NONE;
4310 dcmd->data_xfer_len = 0;
4311 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4312 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4314 megasas_issue_blocked_cmd(instance, cmd);
4316 megasas_return_cmd(instance, cmd);
4322 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4323 * @instance: Adapter soft state
4324 * @opcode: Shutdown/Hibernate
4326 static void megasas_shutdown_controller(struct megasas_instance *instance,
4329 struct megasas_cmd *cmd;
4330 struct megasas_dcmd_frame *dcmd;
4332 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4335 cmd = megasas_get_cmd(instance);
4340 if (instance->aen_cmd)
4341 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
4342 if (instance->map_update_cmd)
4343 megasas_issue_blocked_abort_cmd(instance,
4344 instance->map_update_cmd);
4345 dcmd = &cmd->frame->dcmd;
4347 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4349 dcmd->cmd = MFI_CMD_DCMD;
4350 dcmd->cmd_status = 0x0;
4351 dcmd->sge_count = 0;
4352 dcmd->flags = MFI_FRAME_DIR_NONE;
4355 dcmd->data_xfer_len = 0;
4356 dcmd->opcode = opcode;
4358 megasas_issue_blocked_cmd(instance, cmd);
4360 megasas_return_cmd(instance, cmd);
4367 * megasas_suspend - driver suspend entry point
4368 * @pdev: PCI device structure
4369 * @state: PCI power state to suspend routine
4372 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4374 struct Scsi_Host *host;
4375 struct megasas_instance *instance;
4378 instance = pci_get_drvdata(pdev);
4379 host = instance->host;
4380 instance->unload = 1;
4383 del_timer_sync(&instance->io_completion_timer);
4385 megasas_flush_cache(instance);
4386 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
4388 /* cancel the delayed work if this work still in queue */
4389 if (instance->ev != NULL) {
4390 struct megasas_aen_event *ev = instance->ev;
4391 cancel_delayed_work_sync(
4392 (struct delayed_work *)&ev->hotplug_work);
4393 instance->ev = NULL;
4396 tasklet_kill(&instance->isr_tasklet);
4398 pci_set_drvdata(instance->pdev, instance);
4399 instance->instancet->disable_intr(instance->reg_set);
4401 if (instance->msix_vectors)
4402 for (i = 0 ; i < instance->msix_vectors; i++)
4403 free_irq(instance->msixentry[i].vector,
4404 &instance->irq_context[i]);
4406 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4407 if (instance->msix_vectors)
4408 pci_disable_msix(instance->pdev);
4410 pci_save_state(pdev);
4411 pci_disable_device(pdev);
4413 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4419 * megasas_resume- driver resume entry point
4420 * @pdev: PCI device structure
4423 megasas_resume(struct pci_dev *pdev)
4426 struct Scsi_Host *host;
4427 struct megasas_instance *instance;
4429 instance = pci_get_drvdata(pdev);
4430 host = instance->host;
4431 pci_set_power_state(pdev, PCI_D0);
4432 pci_enable_wake(pdev, PCI_D0, 0);
4433 pci_restore_state(pdev);
4436 * PCI prepping: enable device set bus mastering and dma mask
4438 rval = pci_enable_device_mem(pdev);
4441 printk(KERN_ERR "megasas: Enable device failed\n");
4445 pci_set_master(pdev);
4447 if (megasas_set_dma_mask(pdev))
4448 goto fail_set_dma_mask;
4451 * Initialize MFI Firmware
4454 atomic_set(&instance->fw_outstanding, 0);
4457 * We expect the FW state to be READY
4459 if (megasas_transition_to_ready(instance, 0))
4460 goto fail_ready_state;
4462 /* Now re-enable MSI-X */
4463 if (instance->msix_vectors)
4464 pci_enable_msix(instance->pdev, instance->msixentry,
4465 instance->msix_vectors);
4467 switch (instance->pdev->device) {
4468 case PCI_DEVICE_ID_LSI_FUSION:
4469 case PCI_DEVICE_ID_LSI_INVADER:
4471 megasas_reset_reply_desc(instance);
4472 if (megasas_ioc_init_fusion(instance)) {
4473 megasas_free_cmds(instance);
4474 megasas_free_cmds_fusion(instance);
4477 if (!megasas_get_map_info(instance))
4478 megasas_sync_map_info(instance);
4482 *instance->producer = 0;
4483 *instance->consumer = 0;
4484 if (megasas_issue_init_mfi(instance))
4489 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4490 (unsigned long)instance);
4495 if (instance->msix_vectors) {
4496 for (i = 0 ; i < instance->msix_vectors; i++) {
4497 instance->irq_context[i].instance = instance;
4498 instance->irq_context[i].MSIxIndex = i;
4499 if (request_irq(instance->msixentry[i].vector,
4500 instance->instancet->service_isr, 0,
4502 &instance->irq_context[i])) {
4503 printk(KERN_DEBUG "megasas: Failed to "
4504 "register IRQ for vector %d.\n", i);
4505 for (j = 0 ; j < i ; j++)
4507 instance->msixentry[j].vector,
4508 &instance->irq_context[j]);
4513 instance->irq_context[0].instance = instance;
4514 instance->irq_context[0].MSIxIndex = 0;
4515 if (request_irq(pdev->irq, instance->instancet->service_isr,
4516 IRQF_SHARED, "megasas",
4517 &instance->irq_context[0])) {
4518 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4523 instance->instancet->enable_intr(instance->reg_set);
4525 /* Initialize the cmd completion timer */
4527 megasas_start_timer(instance, &instance->io_completion_timer,
4528 megasas_io_completion_timer,
4529 MEGASAS_COMPLETION_TIMER_INTERVAL);
4530 instance->unload = 0;
4533 * Initiate AEN (Asynchronous Event Notification)
4535 if (megasas_start_aen(instance))
4536 printk(KERN_ERR "megasas: Start AEN failed\n");
4542 if (instance->evt_detail)
4543 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4544 instance->evt_detail,
4545 instance->evt_detail_h);
4547 if (instance->producer)
4548 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4549 instance->producer_h);
4550 if (instance->consumer)
4551 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4552 instance->consumer_h);
4553 scsi_host_put(host);
4558 pci_disable_device(pdev);
4563 #define megasas_suspend NULL
4564 #define megasas_resume NULL
4568 * megasas_detach_one - PCI hot"un"plug entry point
4569 * @pdev: PCI device structure
4571 static void __devexit megasas_detach_one(struct pci_dev *pdev)
4574 struct Scsi_Host *host;
4575 struct megasas_instance *instance;
4576 struct fusion_context *fusion;
4578 instance = pci_get_drvdata(pdev);
4579 instance->unload = 1;
4580 host = instance->host;
4581 fusion = instance->ctrl_context;
4584 del_timer_sync(&instance->io_completion_timer);
4586 scsi_remove_host(instance->host);
4587 megasas_flush_cache(instance);
4588 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4590 /* cancel the delayed work if this work still in queue*/
4591 if (instance->ev != NULL) {
4592 struct megasas_aen_event *ev = instance->ev;
4593 cancel_delayed_work_sync(
4594 (struct delayed_work *)&ev->hotplug_work);
4595 instance->ev = NULL;
4598 tasklet_kill(&instance->isr_tasklet);
4601 * Take the instance off the instance array. Note that we will not
4602 * decrement the max_index. We let this array be sparse array
4604 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4605 if (megasas_mgmt_info.instance[i] == instance) {
4606 megasas_mgmt_info.count--;
4607 megasas_mgmt_info.instance[i] = NULL;
4613 pci_set_drvdata(instance->pdev, NULL);
4615 instance->instancet->disable_intr(instance->reg_set);
4617 if (instance->msix_vectors)
4618 for (i = 0 ; i < instance->msix_vectors; i++)
4619 free_irq(instance->msixentry[i].vector,
4620 &instance->irq_context[i]);
4622 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4623 if (instance->msix_vectors)
4624 pci_disable_msix(instance->pdev);
4626 switch (instance->pdev->device) {
4627 case PCI_DEVICE_ID_LSI_FUSION:
4628 case PCI_DEVICE_ID_LSI_INVADER:
4629 megasas_release_fusion(instance);
4630 for (i = 0; i < 2 ; i++)
4631 if (fusion->ld_map[i])
4632 dma_free_coherent(&instance->pdev->dev,
4637 kfree(instance->ctrl_context);
4640 megasas_release_mfi(instance);
4641 pci_free_consistent(pdev,
4642 sizeof(struct megasas_evt_detail),
4643 instance->evt_detail,
4644 instance->evt_detail_h);
4645 pci_free_consistent(pdev, sizeof(u32),
4647 instance->producer_h);
4648 pci_free_consistent(pdev, sizeof(u32),
4650 instance->consumer_h);
4654 scsi_host_put(host);
4656 pci_set_drvdata(pdev, NULL);
4658 pci_disable_device(pdev);
4664 * megasas_shutdown - Shutdown entry point
4665 * @device: Generic device structure
4667 static void megasas_shutdown(struct pci_dev *pdev)
4670 struct megasas_instance *instance = pci_get_drvdata(pdev);
4672 instance->unload = 1;
4673 megasas_flush_cache(instance);
4674 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4675 instance->instancet->disable_intr(instance->reg_set);
4676 if (instance->msix_vectors)
4677 for (i = 0 ; i < instance->msix_vectors; i++)
4678 free_irq(instance->msixentry[i].vector,
4679 &instance->irq_context[i]);
4681 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4682 if (instance->msix_vectors)
4683 pci_disable_msix(instance->pdev);
4687 * megasas_mgmt_open - char node "open" entry point
4689 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4692 * Allow only those users with admin rights
4694 if (!capable(CAP_SYS_ADMIN))
4701 * megasas_mgmt_fasync - Async notifier registration from applications
4703 * This function adds the calling process to a driver global queue. When an
4704 * event occurs, SIGIO will be sent to all processes in this queue.
4706 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4710 mutex_lock(&megasas_async_queue_mutex);
4712 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4714 mutex_unlock(&megasas_async_queue_mutex);
4717 /* For sanity check when we get ioctl */
4718 filep->private_data = filep;
4722 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4728 * megasas_mgmt_poll - char node "poll" entry point
4730 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4733 unsigned long flags;
4734 poll_wait(file, &megasas_poll_wait, wait);
4735 spin_lock_irqsave(&poll_aen_lock, flags);
4736 if (megasas_poll_wait_aen)
4737 mask = (POLLIN | POLLRDNORM);
4740 spin_unlock_irqrestore(&poll_aen_lock, flags);
4745 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4746 * @instance: Adapter soft state
4747 * @argp: User's ioctl packet
4750 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4751 struct megasas_iocpacket __user * user_ioc,
4752 struct megasas_iocpacket *ioc)
4754 struct megasas_sge32 *kern_sge32;
4755 struct megasas_cmd *cmd;
4756 void *kbuff_arr[MAX_IOCTL_SGE];
4757 dma_addr_t buf_handle = 0;
4760 dma_addr_t sense_handle;
4761 unsigned long *sense_ptr;
4763 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4765 if (ioc->sge_count > MAX_IOCTL_SGE) {
4766 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4767 ioc->sge_count, MAX_IOCTL_SGE);
4771 cmd = megasas_get_cmd(instance);
4773 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4778 * User's IOCTL packet has 2 frames (maximum). Copy those two
4779 * frames into our cmd's frames. cmd->frame's context will get
4780 * overwritten when we copy from user's frames. So set that value
4783 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4784 cmd->frame->hdr.context = cmd->index;
4785 cmd->frame->hdr.pad_0 = 0;
4788 * The management interface between applications and the fw uses
4789 * MFI frames. E.g, RAID configuration changes, LD property changes
4790 * etc are accomplishes through different kinds of MFI frames. The
4791 * driver needs to care only about substituting user buffers with
4792 * kernel buffers in SGLs. The location of SGL is embedded in the
4793 * struct iocpacket itself.
4795 kern_sge32 = (struct megasas_sge32 *)
4796 ((unsigned long)cmd->frame + ioc->sgl_off);
4799 * For each user buffer, create a mirror buffer and copy in
4801 for (i = 0; i < ioc->sge_count; i++) {
4802 if (!ioc->sgl[i].iov_len)
4805 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
4806 ioc->sgl[i].iov_len,
4807 &buf_handle, GFP_KERNEL);
4808 if (!kbuff_arr[i]) {
4809 printk(KERN_DEBUG "megasas: Failed to alloc "
4810 "kernel SGL buffer for IOCTL \n");
4816 * We don't change the dma_coherent_mask, so
4817 * pci_alloc_consistent only returns 32bit addresses
4819 kern_sge32[i].phys_addr = (u32) buf_handle;
4820 kern_sge32[i].length = ioc->sgl[i].iov_len;
4823 * We created a kernel buffer corresponding to the
4824 * user buffer. Now copy in from the user buffer
4826 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4827 (u32) (ioc->sgl[i].iov_len))) {
4833 if (ioc->sense_len) {
4834 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4835 &sense_handle, GFP_KERNEL);
4842 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
4843 *sense_ptr = sense_handle;
4847 * Set the sync_cmd flag so that the ISR knows not to complete this
4848 * cmd to the SCSI mid-layer
4851 megasas_issue_blocked_cmd(instance, cmd);
4855 * copy out the kernel buffers to user buffers
4857 for (i = 0; i < ioc->sge_count; i++) {
4858 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4859 ioc->sgl[i].iov_len)) {
4866 * copy out the sense
4868 if (ioc->sense_len) {
4870 * sense_ptr points to the location that has the user
4871 * sense buffer address
4873 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4876 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4877 sense, ioc->sense_len)) {
4878 printk(KERN_ERR "megasas: Failed to copy out to user "
4886 * copy the status codes returned by the fw
4888 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4889 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4890 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4896 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
4897 sense, sense_handle);
4900 for (i = 0; i < ioc->sge_count; i++) {
4902 dma_free_coherent(&instance->pdev->dev,
4903 kern_sge32[i].length,
4905 kern_sge32[i].phys_addr);
4908 megasas_return_cmd(instance, cmd);
4912 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4914 struct megasas_iocpacket __user *user_ioc =
4915 (struct megasas_iocpacket __user *)arg;
4916 struct megasas_iocpacket *ioc;
4917 struct megasas_instance *instance;
4920 unsigned long flags;
4921 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4923 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4927 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4932 instance = megasas_lookup_instance(ioc->host_no);
4938 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4939 printk(KERN_ERR "Controller in crit error\n");
4944 if (instance->unload == 1) {
4950 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4952 if (down_interruptible(&instance->ioctl_sem)) {
4953 error = -ERESTARTSYS;
4957 for (i = 0; i < wait_time; i++) {
4959 spin_lock_irqsave(&instance->hba_lock, flags);
4960 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4961 spin_unlock_irqrestore(&instance->hba_lock, flags);
4964 spin_unlock_irqrestore(&instance->hba_lock, flags);
4966 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4967 printk(KERN_NOTICE "megasas: waiting"
4968 "for controller reset to finish\n");
4974 spin_lock_irqsave(&instance->hba_lock, flags);
4975 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4976 spin_unlock_irqrestore(&instance->hba_lock, flags);
4978 printk(KERN_ERR "megaraid_sas: timed out while"
4979 "waiting for HBA to recover\n");
4983 spin_unlock_irqrestore(&instance->hba_lock, flags);
4985 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4986 up(&instance->ioctl_sem);
4993 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4995 struct megasas_instance *instance;
4996 struct megasas_aen aen;
4999 unsigned long flags;
5000 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
5002 if (file->private_data != file) {
5003 printk(KERN_DEBUG "megasas: fasync_helper was not "
5008 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
5011 instance = megasas_lookup_instance(aen.host_no);
5016 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
5020 if (instance->unload == 1) {
5024 for (i = 0; i < wait_time; i++) {
5026 spin_lock_irqsave(&instance->hba_lock, flags);
5027 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
5028 spin_unlock_irqrestore(&instance->hba_lock,
5033 spin_unlock_irqrestore(&instance->hba_lock, flags);
5035 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
5036 printk(KERN_NOTICE "megasas: waiting for"
5037 "controller reset to finish\n");
5043 spin_lock_irqsave(&instance->hba_lock, flags);
5044 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
5045 spin_unlock_irqrestore(&instance->hba_lock, flags);
5046 printk(KERN_ERR "megaraid_sas: timed out while waiting"
5047 "for HBA to recover.\n");
5050 spin_unlock_irqrestore(&instance->hba_lock, flags);
5052 mutex_lock(&instance->aen_mutex);
5053 error = megasas_register_aen(instance, aen.seq_num,
5054 aen.class_locale_word);
5055 mutex_unlock(&instance->aen_mutex);
5060 * megasas_mgmt_ioctl - char node ioctl entry point
5063 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5066 case MEGASAS_IOC_FIRMWARE:
5067 return megasas_mgmt_ioctl_fw(file, arg);
5069 case MEGASAS_IOC_GET_AEN:
5070 return megasas_mgmt_ioctl_aen(file, arg);
5076 #ifdef CONFIG_COMPAT
5077 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
5079 struct compat_megasas_iocpacket __user *cioc =
5080 (struct compat_megasas_iocpacket __user *)arg;
5081 struct megasas_iocpacket __user *ioc =
5082 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
5086 unsigned long local_raw_ptr;
5087 u32 local_sense_off;
5088 u32 local_sense_len;
5090 if (clear_user(ioc, sizeof(*ioc)))
5093 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
5094 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
5095 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
5096 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
5097 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
5098 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
5102 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5103 * sense_len is not null, so prepare the 64bit value under
5104 * the same condition.
5106 if (get_user(local_raw_ptr, ioc->frame.raw) ||
5107 get_user(local_sense_off, &ioc->sense_off) ||
5108 get_user(local_sense_len, &ioc->sense_len))
5112 if (local_sense_len) {
5113 void __user **sense_ioc_ptr =
5114 (void __user **)((u8*)local_raw_ptr + local_sense_off);
5115 compat_uptr_t *sense_cioc_ptr =
5116 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
5117 if (get_user(ptr, sense_cioc_ptr) ||
5118 put_user(compat_ptr(ptr), sense_ioc_ptr))
5122 for (i = 0; i < MAX_IOCTL_SGE; i++) {
5123 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
5124 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
5125 copy_in_user(&ioc->sgl[i].iov_len,
5126 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
5130 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
5132 if (copy_in_user(&cioc->frame.hdr.cmd_status,
5133 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
5134 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
5141 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5145 case MEGASAS_IOC_FIRMWARE32:
5146 return megasas_mgmt_compat_ioctl_fw(file, arg);
5147 case MEGASAS_IOC_GET_AEN:
5148 return megasas_mgmt_ioctl_aen(file, arg);
5156 * File operations structure for management interface
5158 static const struct file_operations megasas_mgmt_fops = {
5159 .owner = THIS_MODULE,
5160 .open = megasas_mgmt_open,
5161 .fasync = megasas_mgmt_fasync,
5162 .unlocked_ioctl = megasas_mgmt_ioctl,
5163 .poll = megasas_mgmt_poll,
5164 #ifdef CONFIG_COMPAT
5165 .compat_ioctl = megasas_mgmt_compat_ioctl,
5167 .llseek = noop_llseek,
5171 * PCI hotplug support registration structure
5173 static struct pci_driver megasas_pci_driver = {
5175 .name = "megaraid_sas",
5176 .id_table = megasas_pci_table,
5177 .probe = megasas_probe_one,
5178 .remove = __devexit_p(megasas_detach_one),
5179 .suspend = megasas_suspend,
5180 .resume = megasas_resume,
5181 .shutdown = megasas_shutdown,
5185 * Sysfs driver attributes
5187 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5189 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5193 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5196 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5198 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5202 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5206 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5208 return sprintf(buf, "%u\n", support_poll_for_event);
5211 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5212 megasas_sysfs_show_support_poll_for_event, NULL);
5215 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5217 return sprintf(buf, "%u\n", support_device_change);
5220 static DRIVER_ATTR(support_device_change, S_IRUGO,
5221 megasas_sysfs_show_support_device_change, NULL);
5224 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5226 return sprintf(buf, "%u\n", megasas_dbg_lvl);
5230 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5233 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5234 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5240 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
5241 megasas_sysfs_set_dbg_lvl);
5244 megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
5246 return sprintf(buf, "%u\n", poll_mode_io);
5250 megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
5251 const char *buf, size_t count)
5254 int tmp = poll_mode_io;
5256 struct megasas_instance *instance;
5258 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
5259 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
5264 * Check if poll_mode_io is already set or is same as previous value
5266 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
5271 * Start timers for all adapters
5273 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5274 instance = megasas_mgmt_info.instance[i];
5276 megasas_start_timer(instance,
5277 &instance->io_completion_timer,
5278 megasas_io_completion_timer,
5279 MEGASAS_COMPLETION_TIMER_INTERVAL);
5284 * Delete timers for all adapters
5286 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5287 instance = megasas_mgmt_info.instance[i];
5289 del_timer_sync(&instance->io_completion_timer);
5298 megasas_aen_polling(struct work_struct *work)
5300 struct megasas_aen_event *ev =
5301 container_of(work, struct megasas_aen_event, hotplug_work);
5302 struct megasas_instance *instance = ev->instance;
5303 union megasas_evt_class_locale class_locale;
5304 struct Scsi_Host *host;
5305 struct scsi_device *sdev1;
5308 int i, j, doscan = 0;
5313 printk(KERN_ERR "invalid instance!\n");
5317 instance->ev = NULL;
5318 host = instance->host;
5319 if (instance->evt_detail) {
5321 switch (instance->evt_detail->code) {
5322 case MR_EVT_PD_INSERTED:
5323 if (megasas_get_pd_list(instance) == 0) {
5324 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5326 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5330 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5333 scsi_device_lookup(host, i, j, 0);
5335 if (instance->pd_list[pd_index].driveState
5336 == MR_PD_STATE_SYSTEM) {
5338 scsi_add_device(host, i, j, 0);
5342 scsi_device_put(sdev1);
5350 case MR_EVT_PD_REMOVED:
5351 if (megasas_get_pd_list(instance) == 0) {
5352 megasas_get_pd_list(instance);
5353 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5355 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5359 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5362 scsi_device_lookup(host, i, j, 0);
5364 if (instance->pd_list[pd_index].driveState
5365 == MR_PD_STATE_SYSTEM) {
5367 scsi_device_put(sdev1);
5371 scsi_remove_device(sdev1);
5372 scsi_device_put(sdev1);
5381 case MR_EVT_LD_OFFLINE:
5382 case MR_EVT_CFG_CLEARED:
5383 case MR_EVT_LD_DELETED:
5384 megasas_get_ld_list(instance);
5385 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5387 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5391 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5393 sdev1 = scsi_device_lookup(host,
5394 i + MEGASAS_MAX_LD_CHANNELS,
5398 if (instance->ld_ids[ld_index] != 0xff) {
5400 scsi_device_put(sdev1);
5404 scsi_remove_device(sdev1);
5405 scsi_device_put(sdev1);
5412 case MR_EVT_LD_CREATED:
5413 megasas_get_ld_list(instance);
5414 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5416 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5419 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5421 sdev1 = scsi_device_lookup(host,
5422 i+MEGASAS_MAX_LD_CHANNELS,
5425 if (instance->ld_ids[ld_index] !=
5428 scsi_add_device(host,
5434 scsi_device_put(sdev1);
5440 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
5441 case MR_EVT_FOREIGN_CFG_IMPORTED:
5442 case MR_EVT_LD_STATE_CHANGE:
5450 printk(KERN_ERR "invalid evt_detail!\n");
5456 printk(KERN_INFO "scanning ...\n");
5457 megasas_get_pd_list(instance);
5458 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5459 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5460 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5461 sdev1 = scsi_device_lookup(host, i, j, 0);
5462 if (instance->pd_list[pd_index].driveState ==
5463 MR_PD_STATE_SYSTEM) {
5465 scsi_add_device(host, i, j, 0);
5468 scsi_device_put(sdev1);
5471 scsi_remove_device(sdev1);
5472 scsi_device_put(sdev1);
5478 megasas_get_ld_list(instance);
5479 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5480 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5482 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5484 sdev1 = scsi_device_lookup(host,
5485 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5486 if (instance->ld_ids[ld_index] != 0xff) {
5488 scsi_add_device(host,
5492 scsi_device_put(sdev1);
5496 scsi_remove_device(sdev1);
5497 scsi_device_put(sdev1);
5504 if ( instance->aen_cmd != NULL ) {
5509 seq_num = instance->evt_detail->seq_num + 1;
5511 /* Register AEN with FW for latest sequence number plus 1 */
5512 class_locale.members.reserved = 0;
5513 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5514 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5515 mutex_lock(&instance->aen_mutex);
5516 error = megasas_register_aen(instance, seq_num,
5518 mutex_unlock(&instance->aen_mutex);
5521 printk(KERN_ERR "register aen failed error %x\n", error);
5527 static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
5528 megasas_sysfs_show_poll_mode_io,
5529 megasas_sysfs_set_poll_mode_io);
5532 * megasas_init - Driver load entry point
5534 static int __init megasas_init(void)
5539 * Announce driver version and other information
5541 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5542 MEGASAS_EXT_VERSION);
5544 spin_lock_init(&poll_aen_lock);
5546 support_poll_for_event = 2;
5547 support_device_change = 1;
5549 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5552 * Register character device node
5554 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5557 printk(KERN_DEBUG "megasas: failed to open device node\n");
5561 megasas_mgmt_majorno = rval;
5564 * Register ourselves as PCI hotplug module
5566 rval = pci_register_driver(&megasas_pci_driver);
5569 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
5573 rval = driver_create_file(&megasas_pci_driver.driver,
5574 &driver_attr_version);
5576 goto err_dcf_attr_ver;
5577 rval = driver_create_file(&megasas_pci_driver.driver,
5578 &driver_attr_release_date);
5580 goto err_dcf_rel_date;
5582 rval = driver_create_file(&megasas_pci_driver.driver,
5583 &driver_attr_support_poll_for_event);
5585 goto err_dcf_support_poll_for_event;
5587 rval = driver_create_file(&megasas_pci_driver.driver,
5588 &driver_attr_dbg_lvl);
5590 goto err_dcf_dbg_lvl;
5591 rval = driver_create_file(&megasas_pci_driver.driver,
5592 &driver_attr_poll_mode_io);
5594 goto err_dcf_poll_mode_io;
5596 rval = driver_create_file(&megasas_pci_driver.driver,
5597 &driver_attr_support_device_change);
5599 goto err_dcf_support_device_change;
5603 err_dcf_support_device_change:
5604 driver_remove_file(&megasas_pci_driver.driver,
5605 &driver_attr_poll_mode_io);
5607 err_dcf_poll_mode_io:
5608 driver_remove_file(&megasas_pci_driver.driver,
5609 &driver_attr_dbg_lvl);
5611 driver_remove_file(&megasas_pci_driver.driver,
5612 &driver_attr_support_poll_for_event);
5614 err_dcf_support_poll_for_event:
5615 driver_remove_file(&megasas_pci_driver.driver,
5616 &driver_attr_release_date);
5619 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5621 pci_unregister_driver(&megasas_pci_driver);
5623 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5628 * megasas_exit - Driver unload entry point
5630 static void __exit megasas_exit(void)
5632 driver_remove_file(&megasas_pci_driver.driver,
5633 &driver_attr_poll_mode_io);
5634 driver_remove_file(&megasas_pci_driver.driver,
5635 &driver_attr_dbg_lvl);
5636 driver_remove_file(&megasas_pci_driver.driver,
5637 &driver_attr_support_poll_for_event);
5638 driver_remove_file(&megasas_pci_driver.driver,
5639 &driver_attr_support_device_change);
5640 driver_remove_file(&megasas_pci_driver.driver,
5641 &driver_attr_release_date);
5642 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5644 pci_unregister_driver(&megasas_pci_driver);
5645 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5648 module_init(megasas_init);
5649 module_exit(megasas_exit);