2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2003-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Authors: Avago Technologies
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
28 * Send feedback to: megaraidlinux.pdl@avagotech.com
30 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/list.h>
38 #include <linux/moduleparam.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/uio.h>
44 #include <linux/slab.h>
45 #include <asm/uaccess.h>
47 #include <linux/compat.h>
48 #include <linux/blkdev.h>
49 #include <linux/mutex.h>
50 #include <linux/poll.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_device.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_tcq.h>
57 #include "megaraid_sas_fusion.h"
58 #include "megaraid_sas.h"
61 * Number of sectors per IO command
62 * Will be set in megasas_init_mfi if user does not provide
64 static unsigned int max_sectors;
65 module_param_named(max_sectors, max_sectors, int, 0);
66 MODULE_PARM_DESC(max_sectors,
67 "Maximum number of sectors per IO command");
69 static int msix_disable;
70 module_param(msix_disable, int, S_IRUGO);
71 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73 static unsigned int msix_vectors;
74 module_param(msix_vectors, int, S_IRUGO);
75 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77 static int allow_vf_ioctls;
78 module_param(allow_vf_ioctls, int, S_IRUGO);
79 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
81 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
82 module_param(throttlequeuedepth, int, S_IRUGO);
83 MODULE_PARM_DESC(throttlequeuedepth,
84 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
86 int resetwaittime = MEGASAS_RESET_WAIT_TIME;
87 module_param(resetwaittime, int, S_IRUGO);
88 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
89 "before resetting adapter. Default: 180");
91 int smp_affinity_enable = 1;
92 module_param(smp_affinity_enable, int, S_IRUGO);
93 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(MEGASAS_VERSION);
97 MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
98 MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
100 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
101 static int megasas_get_pd_list(struct megasas_instance *instance);
102 static int megasas_ld_list_query(struct megasas_instance *instance,
104 static int megasas_issue_init_mfi(struct megasas_instance *instance);
105 static int megasas_register_aen(struct megasas_instance *instance,
106 u32 seq_num, u32 class_locale_word);
108 * PCI ID table for all supported controllers
110 static struct pci_device_id megasas_pci_table[] = {
112 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
114 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
116 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
118 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
120 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
122 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
124 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
126 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
127 /* xscale IOP, vega */
128 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
141 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
143 static int megasas_mgmt_majorno;
144 struct megasas_mgmt_info megasas_mgmt_info;
145 static struct fasync_struct *megasas_async_queue;
146 static DEFINE_MUTEX(megasas_async_queue_mutex);
148 static int megasas_poll_wait_aen;
149 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
150 static u32 support_poll_for_event;
152 static u32 support_device_change;
154 /* define lock for aen poll */
155 spinlock_t poll_aen_lock;
158 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
161 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
163 megasas_adp_reset_gen2(struct megasas_instance *instance,
164 struct megasas_register_set __iomem *reg_set);
165 static irqreturn_t megasas_isr(int irq, void *devp);
167 megasas_init_adapter_mfi(struct megasas_instance *instance);
169 megasas_build_and_issue_cmd(struct megasas_instance *instance,
170 struct scsi_cmnd *scmd);
171 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
173 megasas_release_fusion(struct megasas_instance *instance);
175 megasas_ioc_init_fusion(struct megasas_instance *instance);
177 megasas_free_cmds_fusion(struct megasas_instance *instance);
179 megasas_get_map_info(struct megasas_instance *instance);
181 megasas_sync_map_info(struct megasas_instance *instance);
183 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
185 void megasas_reset_reply_desc(struct megasas_instance *instance);
186 int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout);
187 void megasas_fusion_ocr_wq(struct work_struct *work);
188 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
190 int megasas_check_mpio_paths(struct megasas_instance *instance,
191 struct scsi_cmnd *scmd);
194 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
196 instance->instancet->fire_cmd(instance,
197 cmd->frame_phys_addr, 0, instance->reg_set);
201 * megasas_get_cmd - Get a command from the free pool
202 * @instance: Adapter soft state
204 * Returns a free command from the pool
206 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
210 struct megasas_cmd *cmd = NULL;
212 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
214 if (!list_empty(&instance->cmd_pool)) {
215 cmd = list_entry((&instance->cmd_pool)->next,
216 struct megasas_cmd, list);
217 list_del_init(&cmd->list);
219 dev_err(&instance->pdev->dev, "Command pool empty!\n");
222 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
227 * megasas_return_cmd - Return a cmd to free command pool
228 * @instance: Adapter soft state
229 * @cmd: Command packet to be returned to free command pool
232 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
236 struct megasas_cmd_fusion *cmd_fusion;
237 struct fusion_context *fusion = instance->ctrl_context;
239 /* This flag is used only for fusion adapter.
240 * Wait for Interrupt for Polled mode DCMD
242 if (cmd->flags & DRV_DCMD_POLLED_MODE)
245 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
248 blk_tags = instance->max_scsi_cmds + cmd->index;
249 cmd_fusion = fusion->cmd_list[blk_tags];
250 megasas_return_cmd_fusion(instance, cmd_fusion);
253 cmd->frame_count = 0;
255 if (!fusion && reset_devices)
256 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
257 list_add(&cmd->list, (&instance->cmd_pool)->next);
259 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
264 * The following functions are defined for xscale
265 * (deviceid : 1064R, PERC5) controllers
269 * megasas_enable_intr_xscale - Enables interrupts
270 * @regs: MFI register set
273 megasas_enable_intr_xscale(struct megasas_instance *instance)
275 struct megasas_register_set __iomem *regs;
277 regs = instance->reg_set;
278 writel(0, &(regs)->outbound_intr_mask);
280 /* Dummy readl to force pci flush */
281 readl(®s->outbound_intr_mask);
285 * megasas_disable_intr_xscale -Disables interrupt
286 * @regs: MFI register set
289 megasas_disable_intr_xscale(struct megasas_instance *instance)
291 struct megasas_register_set __iomem *regs;
294 regs = instance->reg_set;
295 writel(mask, ®s->outbound_intr_mask);
296 /* Dummy readl to force pci flush */
297 readl(®s->outbound_intr_mask);
301 * megasas_read_fw_status_reg_xscale - returns the current FW status value
302 * @regs: MFI register set
305 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
307 return readl(&(regs)->outbound_msg_0);
310 * megasas_clear_interrupt_xscale - Check & clear interrupt
311 * @regs: MFI register set
314 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
320 * Check if it is our interrupt
322 status = readl(®s->outbound_intr_status);
324 if (status & MFI_OB_INTR_STATUS_MASK)
325 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
326 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
327 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
330 * Clear the interrupt by writing back the same value
333 writel(status, ®s->outbound_intr_status);
335 /* Dummy readl to force pci flush */
336 readl(®s->outbound_intr_status);
342 * megasas_fire_cmd_xscale - Sends command to the FW
343 * @frame_phys_addr : Physical address of cmd
344 * @frame_count : Number of frames for the command
345 * @regs : MFI register set
348 megasas_fire_cmd_xscale(struct megasas_instance *instance,
349 dma_addr_t frame_phys_addr,
351 struct megasas_register_set __iomem *regs)
355 spin_lock_irqsave(&instance->hba_lock, flags);
356 writel((frame_phys_addr >> 3)|(frame_count),
357 &(regs)->inbound_queue_port);
358 spin_unlock_irqrestore(&instance->hba_lock, flags);
362 * megasas_adp_reset_xscale - For controller reset
363 * @regs: MFI register set
366 megasas_adp_reset_xscale(struct megasas_instance *instance,
367 struct megasas_register_set __iomem *regs)
372 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
374 for (i = 0; i < 3; i++)
375 msleep(1000); /* sleep for 3 secs */
377 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
378 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
380 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
382 pci_write_config_dword(instance->pdev,
383 MFI_1068_PCSR_OFFSET, pcidata);
385 for (i = 0; i < 2; i++)
386 msleep(1000); /* need to wait 2 secs again */
389 pci_read_config_dword(instance->pdev,
390 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
391 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
392 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
393 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
395 pci_write_config_dword(instance->pdev,
396 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
403 * megasas_check_reset_xscale - For controller reset check
404 * @regs: MFI register set
407 megasas_check_reset_xscale(struct megasas_instance *instance,
408 struct megasas_register_set __iomem *regs)
410 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
411 (le32_to_cpu(*instance->consumer) ==
412 MEGASAS_ADPRESET_INPROG_SIGN))
417 static struct megasas_instance_template megasas_instance_template_xscale = {
419 .fire_cmd = megasas_fire_cmd_xscale,
420 .enable_intr = megasas_enable_intr_xscale,
421 .disable_intr = megasas_disable_intr_xscale,
422 .clear_intr = megasas_clear_intr_xscale,
423 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
424 .adp_reset = megasas_adp_reset_xscale,
425 .check_reset = megasas_check_reset_xscale,
426 .service_isr = megasas_isr,
427 .tasklet = megasas_complete_cmd_dpc,
428 .init_adapter = megasas_init_adapter_mfi,
429 .build_and_issue_cmd = megasas_build_and_issue_cmd,
430 .issue_dcmd = megasas_issue_dcmd,
434 * This is the end of set of functions & definitions specific
435 * to xscale (deviceid : 1064R, PERC5) controllers
439 * The following functions are defined for ppc (deviceid : 0x60)
444 * megasas_enable_intr_ppc - Enables interrupts
445 * @regs: MFI register set
448 megasas_enable_intr_ppc(struct megasas_instance *instance)
450 struct megasas_register_set __iomem *regs;
452 regs = instance->reg_set;
453 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
455 writel(~0x80000000, &(regs)->outbound_intr_mask);
457 /* Dummy readl to force pci flush */
458 readl(®s->outbound_intr_mask);
462 * megasas_disable_intr_ppc - Disable interrupt
463 * @regs: MFI register set
466 megasas_disable_intr_ppc(struct megasas_instance *instance)
468 struct megasas_register_set __iomem *regs;
469 u32 mask = 0xFFFFFFFF;
471 regs = instance->reg_set;
472 writel(mask, ®s->outbound_intr_mask);
473 /* Dummy readl to force pci flush */
474 readl(®s->outbound_intr_mask);
478 * megasas_read_fw_status_reg_ppc - returns the current FW status value
479 * @regs: MFI register set
482 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
484 return readl(&(regs)->outbound_scratch_pad);
488 * megasas_clear_interrupt_ppc - Check & clear interrupt
489 * @regs: MFI register set
492 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
494 u32 status, mfiStatus = 0;
497 * Check if it is our interrupt
499 status = readl(®s->outbound_intr_status);
501 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
502 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
504 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
505 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
508 * Clear the interrupt by writing back the same value
510 writel(status, ®s->outbound_doorbell_clear);
512 /* Dummy readl to force pci flush */
513 readl(®s->outbound_doorbell_clear);
519 * megasas_fire_cmd_ppc - Sends command to the FW
520 * @frame_phys_addr : Physical address of cmd
521 * @frame_count : Number of frames for the command
522 * @regs : MFI register set
525 megasas_fire_cmd_ppc(struct megasas_instance *instance,
526 dma_addr_t frame_phys_addr,
528 struct megasas_register_set __iomem *regs)
532 spin_lock_irqsave(&instance->hba_lock, flags);
533 writel((frame_phys_addr | (frame_count<<1))|1,
534 &(regs)->inbound_queue_port);
535 spin_unlock_irqrestore(&instance->hba_lock, flags);
539 * megasas_check_reset_ppc - For controller reset check
540 * @regs: MFI register set
543 megasas_check_reset_ppc(struct megasas_instance *instance,
544 struct megasas_register_set __iomem *regs)
546 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
552 static struct megasas_instance_template megasas_instance_template_ppc = {
554 .fire_cmd = megasas_fire_cmd_ppc,
555 .enable_intr = megasas_enable_intr_ppc,
556 .disable_intr = megasas_disable_intr_ppc,
557 .clear_intr = megasas_clear_intr_ppc,
558 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
559 .adp_reset = megasas_adp_reset_xscale,
560 .check_reset = megasas_check_reset_ppc,
561 .service_isr = megasas_isr,
562 .tasklet = megasas_complete_cmd_dpc,
563 .init_adapter = megasas_init_adapter_mfi,
564 .build_and_issue_cmd = megasas_build_and_issue_cmd,
565 .issue_dcmd = megasas_issue_dcmd,
569 * megasas_enable_intr_skinny - Enables interrupts
570 * @regs: MFI register set
573 megasas_enable_intr_skinny(struct megasas_instance *instance)
575 struct megasas_register_set __iomem *regs;
577 regs = instance->reg_set;
578 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
580 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
582 /* Dummy readl to force pci flush */
583 readl(®s->outbound_intr_mask);
587 * megasas_disable_intr_skinny - Disables interrupt
588 * @regs: MFI register set
591 megasas_disable_intr_skinny(struct megasas_instance *instance)
593 struct megasas_register_set __iomem *regs;
594 u32 mask = 0xFFFFFFFF;
596 regs = instance->reg_set;
597 writel(mask, ®s->outbound_intr_mask);
598 /* Dummy readl to force pci flush */
599 readl(®s->outbound_intr_mask);
603 * megasas_read_fw_status_reg_skinny - returns the current FW status value
604 * @regs: MFI register set
607 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
609 return readl(&(regs)->outbound_scratch_pad);
613 * megasas_clear_interrupt_skinny - Check & clear interrupt
614 * @regs: MFI register set
617 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
623 * Check if it is our interrupt
625 status = readl(®s->outbound_intr_status);
627 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
632 * Check if it is our interrupt
634 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
636 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
638 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
641 * Clear the interrupt by writing back the same value
643 writel(status, ®s->outbound_intr_status);
646 * dummy read to flush PCI
648 readl(®s->outbound_intr_status);
654 * megasas_fire_cmd_skinny - Sends command to the FW
655 * @frame_phys_addr : Physical address of cmd
656 * @frame_count : Number of frames for the command
657 * @regs : MFI register set
660 megasas_fire_cmd_skinny(struct megasas_instance *instance,
661 dma_addr_t frame_phys_addr,
663 struct megasas_register_set __iomem *regs)
667 spin_lock_irqsave(&instance->hba_lock, flags);
668 writel(upper_32_bits(frame_phys_addr),
669 &(regs)->inbound_high_queue_port);
670 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
671 &(regs)->inbound_low_queue_port);
672 spin_unlock_irqrestore(&instance->hba_lock, flags);
676 * megasas_check_reset_skinny - For controller reset check
677 * @regs: MFI register set
680 megasas_check_reset_skinny(struct megasas_instance *instance,
681 struct megasas_register_set __iomem *regs)
683 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
689 static struct megasas_instance_template megasas_instance_template_skinny = {
691 .fire_cmd = megasas_fire_cmd_skinny,
692 .enable_intr = megasas_enable_intr_skinny,
693 .disable_intr = megasas_disable_intr_skinny,
694 .clear_intr = megasas_clear_intr_skinny,
695 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
696 .adp_reset = megasas_adp_reset_gen2,
697 .check_reset = megasas_check_reset_skinny,
698 .service_isr = megasas_isr,
699 .tasklet = megasas_complete_cmd_dpc,
700 .init_adapter = megasas_init_adapter_mfi,
701 .build_and_issue_cmd = megasas_build_and_issue_cmd,
702 .issue_dcmd = megasas_issue_dcmd,
707 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
712 * megasas_enable_intr_gen2 - Enables interrupts
713 * @regs: MFI register set
716 megasas_enable_intr_gen2(struct megasas_instance *instance)
718 struct megasas_register_set __iomem *regs;
720 regs = instance->reg_set;
721 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
723 /* write ~0x00000005 (4 & 1) to the intr mask*/
724 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
726 /* Dummy readl to force pci flush */
727 readl(®s->outbound_intr_mask);
731 * megasas_disable_intr_gen2 - Disables interrupt
732 * @regs: MFI register set
735 megasas_disable_intr_gen2(struct megasas_instance *instance)
737 struct megasas_register_set __iomem *regs;
738 u32 mask = 0xFFFFFFFF;
740 regs = instance->reg_set;
741 writel(mask, ®s->outbound_intr_mask);
742 /* Dummy readl to force pci flush */
743 readl(®s->outbound_intr_mask);
747 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
748 * @regs: MFI register set
751 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
753 return readl(&(regs)->outbound_scratch_pad);
757 * megasas_clear_interrupt_gen2 - Check & clear interrupt
758 * @regs: MFI register set
761 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
767 * Check if it is our interrupt
769 status = readl(®s->outbound_intr_status);
771 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
772 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
774 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
775 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
779 * Clear the interrupt by writing back the same value
782 writel(status, ®s->outbound_doorbell_clear);
784 /* Dummy readl to force pci flush */
785 readl(®s->outbound_intr_status);
790 * megasas_fire_cmd_gen2 - Sends command to the FW
791 * @frame_phys_addr : Physical address of cmd
792 * @frame_count : Number of frames for the command
793 * @regs : MFI register set
796 megasas_fire_cmd_gen2(struct megasas_instance *instance,
797 dma_addr_t frame_phys_addr,
799 struct megasas_register_set __iomem *regs)
803 spin_lock_irqsave(&instance->hba_lock, flags);
804 writel((frame_phys_addr | (frame_count<<1))|1,
805 &(regs)->inbound_queue_port);
806 spin_unlock_irqrestore(&instance->hba_lock, flags);
810 * megasas_adp_reset_gen2 - For controller reset
811 * @regs: MFI register set
814 megasas_adp_reset_gen2(struct megasas_instance *instance,
815 struct megasas_register_set __iomem *reg_set)
819 u32 __iomem *seq_offset = ®_set->seq_offset;
820 u32 __iomem *hostdiag_offset = ®_set->host_diag;
822 if (instance->instancet == &megasas_instance_template_skinny) {
823 seq_offset = ®_set->fusion_seq_offset;
824 hostdiag_offset = ®_set->fusion_host_diag;
827 writel(0, seq_offset);
828 writel(4, seq_offset);
829 writel(0xb, seq_offset);
830 writel(2, seq_offset);
831 writel(7, seq_offset);
832 writel(0xd, seq_offset);
836 HostDiag = (u32)readl(hostdiag_offset);
838 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
840 HostDiag = (u32)readl(hostdiag_offset);
841 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
849 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
851 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
855 HostDiag = (u32)readl(hostdiag_offset);
856 while (HostDiag & DIAG_RESET_ADAPTER) {
858 HostDiag = (u32)readl(hostdiag_offset);
859 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
870 * megasas_check_reset_gen2 - For controller reset check
871 * @regs: MFI register set
874 megasas_check_reset_gen2(struct megasas_instance *instance,
875 struct megasas_register_set __iomem *regs)
877 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
884 static struct megasas_instance_template megasas_instance_template_gen2 = {
886 .fire_cmd = megasas_fire_cmd_gen2,
887 .enable_intr = megasas_enable_intr_gen2,
888 .disable_intr = megasas_disable_intr_gen2,
889 .clear_intr = megasas_clear_intr_gen2,
890 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
891 .adp_reset = megasas_adp_reset_gen2,
892 .check_reset = megasas_check_reset_gen2,
893 .service_isr = megasas_isr,
894 .tasklet = megasas_complete_cmd_dpc,
895 .init_adapter = megasas_init_adapter_mfi,
896 .build_and_issue_cmd = megasas_build_and_issue_cmd,
897 .issue_dcmd = megasas_issue_dcmd,
901 * This is the end of set of functions & definitions
902 * specific to gen2 (deviceid : 0x78, 0x79) controllers
906 * Template added for TB (Fusion)
908 extern struct megasas_instance_template megasas_instance_template_fusion;
911 * megasas_issue_polled - Issues a polling command
912 * @instance: Adapter soft state
913 * @cmd: Command packet to be issued
915 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
918 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
921 struct megasas_header *frame_hdr = &cmd->frame->hdr;
923 frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
924 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
927 * Issue the frame using inbound queue port
929 instance->instancet->issue_dcmd(instance, cmd);
932 * Wait for cmd_status to change
934 if (instance->requestorId)
935 seconds = MEGASAS_ROUTINE_WAIT_TIME_VF;
937 seconds = MFI_POLL_TIMEOUT_SECS;
938 return wait_and_poll(instance, cmd, seconds);
942 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
943 * @instance: Adapter soft state
944 * @cmd: Command to be issued
945 * @timeout: Timeout in seconds
947 * This function waits on an event for the command to be returned from ISR.
948 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
949 * Used to issue ioctl commands.
952 megasas_issue_blocked_cmd(struct megasas_instance *instance,
953 struct megasas_cmd *cmd, int timeout)
957 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
959 instance->instancet->issue_dcmd(instance, cmd);
961 ret = wait_event_timeout(instance->int_cmd_wait_q,
962 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
966 wait_event(instance->int_cmd_wait_q,
967 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
969 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
974 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
975 * @instance: Adapter soft state
976 * @cmd_to_abort: Previously issued cmd to be aborted
977 * @timeout: Timeout in seconds
979 * MFI firmware can abort previously issued AEN comamnd (automatic event
980 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
981 * cmd and waits for return status.
982 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
985 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
986 struct megasas_cmd *cmd_to_abort, int timeout)
988 struct megasas_cmd *cmd;
989 struct megasas_abort_frame *abort_fr;
992 cmd = megasas_get_cmd(instance);
997 abort_fr = &cmd->frame->abort;
1000 * Prepare and issue the abort frame
1002 abort_fr->cmd = MFI_CMD_ABORT;
1003 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1004 abort_fr->flags = cpu_to_le16(0);
1005 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1006 abort_fr->abort_mfi_phys_addr_lo =
1007 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1008 abort_fr->abort_mfi_phys_addr_hi =
1009 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1012 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1014 instance->instancet->issue_dcmd(instance, cmd);
1017 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1018 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1020 dev_err(&instance->pdev->dev, "Command timedout"
1021 "from %s\n", __func__);
1025 wait_event(instance->abort_cmd_wait_q,
1026 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1030 megasas_return_cmd(instance, cmd);
1035 * megasas_make_sgl32 - Prepares 32-bit SGL
1036 * @instance: Adapter soft state
1037 * @scp: SCSI command from the mid-layer
1038 * @mfi_sgl: SGL to be filled in
1040 * If successful, this function returns the number of SG elements. Otherwise,
1044 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1045 union megasas_sgl *mfi_sgl)
1049 struct scatterlist *os_sgl;
1051 sge_count = scsi_dma_map(scp);
1052 BUG_ON(sge_count < 0);
1055 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1056 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1057 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1064 * megasas_make_sgl64 - Prepares 64-bit SGL
1065 * @instance: Adapter soft state
1066 * @scp: SCSI command from the mid-layer
1067 * @mfi_sgl: SGL to be filled in
1069 * If successful, this function returns the number of SG elements. Otherwise,
1073 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1074 union megasas_sgl *mfi_sgl)
1078 struct scatterlist *os_sgl;
1080 sge_count = scsi_dma_map(scp);
1081 BUG_ON(sge_count < 0);
1084 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1085 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1086 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1093 * megasas_make_sgl_skinny - Prepares IEEE SGL
1094 * @instance: Adapter soft state
1095 * @scp: SCSI command from the mid-layer
1096 * @mfi_sgl: SGL to be filled in
1098 * If successful, this function returns the number of SG elements. Otherwise,
1102 megasas_make_sgl_skinny(struct megasas_instance *instance,
1103 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1107 struct scatterlist *os_sgl;
1109 sge_count = scsi_dma_map(scp);
1112 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1113 mfi_sgl->sge_skinny[i].length =
1114 cpu_to_le32(sg_dma_len(os_sgl));
1115 mfi_sgl->sge_skinny[i].phys_addr =
1116 cpu_to_le64(sg_dma_address(os_sgl));
1117 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1124 * megasas_get_frame_count - Computes the number of frames
1125 * @frame_type : type of frame- io or pthru frame
1126 * @sge_count : number of sg elements
1128 * Returns the number of frames required for numnber of sge's (sge_count)
1131 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1132 u8 sge_count, u8 frame_type)
1137 u32 frame_count = 0;
1139 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1140 sizeof(struct megasas_sge32);
1142 if (instance->flag_ieee) {
1143 sge_sz = sizeof(struct megasas_sge_skinny);
1147 * Main frame can contain 2 SGEs for 64-bit SGLs and
1148 * 3 SGEs for 32-bit SGLs for ldio &
1149 * 1 SGEs for 64-bit SGLs and
1150 * 2 SGEs for 32-bit SGLs for pthru frame
1152 if (unlikely(frame_type == PTHRU_FRAME)) {
1153 if (instance->flag_ieee == 1) {
1154 num_cnt = sge_count - 1;
1155 } else if (IS_DMA64)
1156 num_cnt = sge_count - 1;
1158 num_cnt = sge_count - 2;
1160 if (instance->flag_ieee == 1) {
1161 num_cnt = sge_count - 1;
1162 } else if (IS_DMA64)
1163 num_cnt = sge_count - 2;
1165 num_cnt = sge_count - 3;
1169 sge_bytes = sge_sz * num_cnt;
1171 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1172 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1177 if (frame_count > 7)
1183 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1184 * @instance: Adapter soft state
1185 * @scp: SCSI command
1186 * @cmd: Command to be prepared in
1188 * This function prepares CDB commands. These are typcially pass-through
1189 * commands to the devices.
1192 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1193 struct megasas_cmd *cmd)
1198 struct megasas_pthru_frame *pthru;
1200 is_logical = MEGASAS_IS_LOGICAL(scp);
1201 device_id = MEGASAS_DEV_INDEX(scp);
1202 pthru = (struct megasas_pthru_frame *)cmd->frame;
1204 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1205 flags = MFI_FRAME_DIR_WRITE;
1206 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1207 flags = MFI_FRAME_DIR_READ;
1208 else if (scp->sc_data_direction == PCI_DMA_NONE)
1209 flags = MFI_FRAME_DIR_NONE;
1211 if (instance->flag_ieee == 1) {
1212 flags |= MFI_FRAME_IEEE;
1216 * Prepare the DCDB frame
1218 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1219 pthru->cmd_status = 0x0;
1220 pthru->scsi_status = 0x0;
1221 pthru->target_id = device_id;
1222 pthru->lun = scp->device->lun;
1223 pthru->cdb_len = scp->cmd_len;
1226 pthru->flags = cpu_to_le16(flags);
1227 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1229 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1232 * If the command is for the tape device, set the
1233 * pthru timeout to the os layer timeout value.
1235 if (scp->device->type == TYPE_TAPE) {
1236 if ((scp->request->timeout / HZ) > 0xFFFF)
1237 pthru->timeout = cpu_to_le16(0xFFFF);
1239 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1245 if (instance->flag_ieee == 1) {
1246 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1247 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1249 } else if (IS_DMA64) {
1250 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1251 pthru->sge_count = megasas_make_sgl64(instance, scp,
1254 pthru->sge_count = megasas_make_sgl32(instance, scp,
1257 if (pthru->sge_count > instance->max_num_sge) {
1258 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1264 * Sense info specific
1266 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1267 pthru->sense_buf_phys_addr_hi =
1268 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1269 pthru->sense_buf_phys_addr_lo =
1270 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1273 * Compute the total number of frames this command consumes. FW uses
1274 * this number to pull sufficient number of frames from host memory.
1276 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1279 return cmd->frame_count;
1283 * megasas_build_ldio - Prepares IOs to logical devices
1284 * @instance: Adapter soft state
1285 * @scp: SCSI command
1286 * @cmd: Command to be prepared
1288 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1291 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1292 struct megasas_cmd *cmd)
1295 u8 sc = scp->cmnd[0];
1297 struct megasas_io_frame *ldio;
1299 device_id = MEGASAS_DEV_INDEX(scp);
1300 ldio = (struct megasas_io_frame *)cmd->frame;
1302 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1303 flags = MFI_FRAME_DIR_WRITE;
1304 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1305 flags = MFI_FRAME_DIR_READ;
1307 if (instance->flag_ieee == 1) {
1308 flags |= MFI_FRAME_IEEE;
1312 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1314 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1315 ldio->cmd_status = 0x0;
1316 ldio->scsi_status = 0x0;
1317 ldio->target_id = device_id;
1319 ldio->reserved_0 = 0;
1321 ldio->flags = cpu_to_le16(flags);
1322 ldio->start_lba_hi = 0;
1323 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1326 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1328 if (scp->cmd_len == 6) {
1329 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1330 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1331 ((u32) scp->cmnd[2] << 8) |
1332 (u32) scp->cmnd[3]);
1334 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1338 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1340 else if (scp->cmd_len == 10) {
1341 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1342 ((u32) scp->cmnd[7] << 8));
1343 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1344 ((u32) scp->cmnd[3] << 16) |
1345 ((u32) scp->cmnd[4] << 8) |
1346 (u32) scp->cmnd[5]);
1350 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1352 else if (scp->cmd_len == 12) {
1353 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1354 ((u32) scp->cmnd[7] << 16) |
1355 ((u32) scp->cmnd[8] << 8) |
1356 (u32) scp->cmnd[9]);
1358 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1359 ((u32) scp->cmnd[3] << 16) |
1360 ((u32) scp->cmnd[4] << 8) |
1361 (u32) scp->cmnd[5]);
1365 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1367 else if (scp->cmd_len == 16) {
1368 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1369 ((u32) scp->cmnd[11] << 16) |
1370 ((u32) scp->cmnd[12] << 8) |
1371 (u32) scp->cmnd[13]);
1373 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1374 ((u32) scp->cmnd[7] << 16) |
1375 ((u32) scp->cmnd[8] << 8) |
1376 (u32) scp->cmnd[9]);
1378 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1379 ((u32) scp->cmnd[3] << 16) |
1380 ((u32) scp->cmnd[4] << 8) |
1381 (u32) scp->cmnd[5]);
1388 if (instance->flag_ieee) {
1389 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1390 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1392 } else if (IS_DMA64) {
1393 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1394 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1396 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1398 if (ldio->sge_count > instance->max_num_sge) {
1399 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1405 * Sense info specific
1407 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1408 ldio->sense_buf_phys_addr_hi = 0;
1409 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1412 * Compute the total number of frames this command consumes. FW uses
1413 * this number to pull sufficient number of frames from host memory.
1415 cmd->frame_count = megasas_get_frame_count(instance,
1416 ldio->sge_count, IO_FRAME);
1418 return cmd->frame_count;
1422 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1423 * and whether it's RW or non RW
1424 * @scmd: SCSI command
1427 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1431 switch (cmd->cmnd[0]) {
1440 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1441 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1444 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1445 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1451 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1453 * @instance: Adapter soft state
1456 megasas_dump_pending_frames(struct megasas_instance *instance)
1458 struct megasas_cmd *cmd;
1460 union megasas_sgl *mfi_sgl;
1461 struct megasas_io_frame *ldio;
1462 struct megasas_pthru_frame *pthru;
1464 u32 max_cmd = instance->max_fw_cmds;
1466 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1467 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1469 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1471 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1473 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1474 for (i = 0; i < max_cmd; i++) {
1475 cmd = instance->cmd_list[i];
1478 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1479 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1480 ldio = (struct megasas_io_frame *)cmd->frame;
1481 mfi_sgl = &ldio->sgl;
1482 sgcount = ldio->sge_count;
1483 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1484 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1485 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1486 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1487 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1489 pthru = (struct megasas_pthru_frame *) cmd->frame;
1490 mfi_sgl = &pthru->sgl;
1491 sgcount = pthru->sge_count;
1492 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1493 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1494 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1495 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1496 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1498 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1499 for (n = 0; n < sgcount; n++) {
1501 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1502 le32_to_cpu(mfi_sgl->sge64[n].length),
1503 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1505 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1506 le32_to_cpu(mfi_sgl->sge32[n].length),
1507 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1511 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1512 for (i = 0; i < max_cmd; i++) {
1514 cmd = instance->cmd_list[i];
1516 if (cmd->sync_cmd == 1)
1517 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1519 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1523 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1524 struct scsi_cmnd *scmd)
1526 struct megasas_cmd *cmd;
1529 cmd = megasas_get_cmd(instance);
1531 return SCSI_MLQUEUE_HOST_BUSY;
1534 * Logical drive command
1536 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1537 frame_count = megasas_build_ldio(instance, scmd, cmd);
1539 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1542 goto out_return_cmd;
1545 scmd->SCp.ptr = (char *)cmd;
1548 * Issue the command to the FW
1550 atomic_inc(&instance->fw_outstanding);
1552 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1553 cmd->frame_count-1, instance->reg_set);
1557 megasas_return_cmd(instance, cmd);
1563 * megasas_queue_command - Queue entry point
1564 * @scmd: SCSI command to be queued
1565 * @done: Callback entry point
1568 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1570 struct megasas_instance *instance;
1571 unsigned long flags;
1573 instance = (struct megasas_instance *)
1574 scmd->device->host->hostdata;
1576 if (instance->unload == 1) {
1577 scmd->result = DID_NO_CONNECT << 16;
1578 scmd->scsi_done(scmd);
1582 if (instance->issuepend_done == 0)
1583 return SCSI_MLQUEUE_HOST_BUSY;
1585 spin_lock_irqsave(&instance->hba_lock, flags);
1587 /* Check for an mpio path and adjust behavior */
1588 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1589 if (megasas_check_mpio_paths(instance, scmd) ==
1590 (DID_RESET << 16)) {
1591 spin_unlock_irqrestore(&instance->hba_lock, flags);
1592 return SCSI_MLQUEUE_HOST_BUSY;
1594 spin_unlock_irqrestore(&instance->hba_lock, flags);
1595 scmd->result = DID_NO_CONNECT << 16;
1596 scmd->scsi_done(scmd);
1601 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1602 spin_unlock_irqrestore(&instance->hba_lock, flags);
1603 scmd->result = DID_NO_CONNECT << 16;
1604 scmd->scsi_done(scmd);
1608 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1609 spin_unlock_irqrestore(&instance->hba_lock, flags);
1610 return SCSI_MLQUEUE_HOST_BUSY;
1613 spin_unlock_irqrestore(&instance->hba_lock, flags);
1617 if (MEGASAS_IS_LOGICAL(scmd) &&
1618 (scmd->device->id >= instance->fw_supported_vd_count ||
1619 scmd->device->lun)) {
1620 scmd->result = DID_BAD_TARGET << 16;
1624 switch (scmd->cmnd[0]) {
1625 case SYNCHRONIZE_CACHE:
1627 * FW takes care of flush cache on its own
1628 * No need to send it down
1630 scmd->result = DID_OK << 16;
1636 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1637 dev_err(&instance->pdev->dev, "Err returned from build_and_issue_cmd\n");
1638 return SCSI_MLQUEUE_HOST_BUSY;
1644 scmd->scsi_done(scmd);
1648 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1652 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1654 if ((megasas_mgmt_info.instance[i]) &&
1655 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1656 return megasas_mgmt_info.instance[i];
1662 static int megasas_slave_configure(struct scsi_device *sdev)
1665 * The RAID firmware may require extended timeouts.
1667 blk_queue_rq_timeout(sdev->request_queue,
1668 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1673 static int megasas_slave_alloc(struct scsi_device *sdev)
1676 struct megasas_instance *instance ;
1678 instance = megasas_lookup_instance(sdev->host->host_no);
1679 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1681 * Open the OS scan to the SYSTEM PD
1684 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1686 if (instance->pd_list[pd_index].driveState ==
1687 MR_PD_STATE_SYSTEM) {
1696 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1698 * @instance: Adapter soft state
1701 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
1704 struct megasas_cmd *cmd_mfi;
1705 struct megasas_cmd_fusion *cmd_fusion;
1706 struct fusion_context *fusion = instance->ctrl_context;
1708 /* Find all outstanding ioctls */
1710 for (i = 0; i < instance->max_fw_cmds; i++) {
1711 cmd_fusion = fusion->cmd_list[i];
1712 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1713 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1714 if (cmd_mfi->sync_cmd &&
1715 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1716 megasas_complete_cmd(instance,
1721 for (i = 0; i < instance->max_fw_cmds; i++) {
1722 cmd_mfi = instance->cmd_list[i];
1723 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1725 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1731 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1733 /* Set critical error to block I/O & ioctls in case caller didn't */
1734 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1735 /* Wait 1 second to ensure IO or ioctls in build have posted */
1737 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1738 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1739 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1740 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
1741 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1742 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1743 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1745 readl(&instance->reg_set->doorbell);
1746 if (instance->mpio && instance->requestorId)
1747 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
1749 writel(MFI_STOP_ADP,
1750 &instance->reg_set->inbound_doorbell);
1752 /* Complete outstanding ioctls when adapter is killed */
1753 megasas_complete_outstanding_ioctls(instance);
1757 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1758 * restored to max value
1759 * @instance: Adapter soft state
1763 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1765 unsigned long flags;
1767 if (instance->flag & MEGASAS_FW_BUSY
1768 && time_after(jiffies, instance->last_time + 5 * HZ)
1769 && atomic_read(&instance->fw_outstanding) <
1770 instance->throttlequeuedepth + 1) {
1772 spin_lock_irqsave(instance->host->host_lock, flags);
1773 instance->flag &= ~MEGASAS_FW_BUSY;
1775 instance->host->can_queue = instance->max_scsi_cmds;
1776 spin_unlock_irqrestore(instance->host->host_lock, flags);
1781 * megasas_complete_cmd_dpc - Returns FW's controller structure
1782 * @instance_addr: Address of adapter soft state
1784 * Tasklet to complete cmds
1786 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1791 struct megasas_cmd *cmd;
1792 struct megasas_instance *instance =
1793 (struct megasas_instance *)instance_addr;
1794 unsigned long flags;
1796 /* If we have already declared adapter dead, donot complete cmds */
1797 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1800 spin_lock_irqsave(&instance->completion_lock, flags);
1802 producer = le32_to_cpu(*instance->producer);
1803 consumer = le32_to_cpu(*instance->consumer);
1805 while (consumer != producer) {
1806 context = le32_to_cpu(instance->reply_queue[consumer]);
1807 if (context >= instance->max_fw_cmds) {
1808 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
1813 cmd = instance->cmd_list[context];
1815 megasas_complete_cmd(instance, cmd, DID_OK);
1818 if (consumer == (instance->max_fw_cmds + 1)) {
1823 *instance->consumer = cpu_to_le32(producer);
1825 spin_unlock_irqrestore(&instance->completion_lock, flags);
1828 * Check if we can restore can_queue
1830 megasas_check_and_restore_queue_depth(instance);
1834 * megasas_start_timer - Initializes a timer object
1835 * @instance: Adapter soft state
1836 * @timer: timer object to be initialized
1837 * @fn: timer function
1838 * @interval: time interval between timer function call
1841 void megasas_start_timer(struct megasas_instance *instance,
1842 struct timer_list *timer,
1843 void *fn, unsigned long interval)
1846 timer->expires = jiffies + interval;
1847 timer->data = (unsigned long)instance;
1848 timer->function = fn;
1853 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1856 process_fw_state_change_wq(struct work_struct *work);
1858 void megasas_do_ocr(struct megasas_instance *instance)
1860 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1861 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1862 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1863 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
1865 instance->instancet->disable_intr(instance);
1866 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1867 instance->issuepend_done = 0;
1869 atomic_set(&instance->fw_outstanding, 0);
1870 megasas_internal_reset_defer_cmds(instance);
1871 process_fw_state_change_wq(&instance->work_init);
1874 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1877 struct megasas_cmd *cmd;
1878 struct megasas_dcmd_frame *dcmd;
1879 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
1880 dma_addr_t new_affiliation_111_h;
1884 cmd = megasas_get_cmd(instance);
1887 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
1888 "Failed to get cmd for scsi%d\n",
1889 instance->host->host_no);
1893 dcmd = &cmd->frame->dcmd;
1895 if (!instance->vf_affiliation_111) {
1896 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
1897 "affiliation for scsi%d\n", instance->host->host_no);
1898 megasas_return_cmd(instance, cmd);
1903 memset(instance->vf_affiliation_111, 0,
1904 sizeof(struct MR_LD_VF_AFFILIATION_111));
1906 new_affiliation_111 =
1907 pci_alloc_consistent(instance->pdev,
1908 sizeof(struct MR_LD_VF_AFFILIATION_111),
1909 &new_affiliation_111_h);
1910 if (!new_affiliation_111) {
1911 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
1912 "memory for new affiliation for scsi%d\n",
1913 instance->host->host_no);
1914 megasas_return_cmd(instance, cmd);
1917 memset(new_affiliation_111, 0,
1918 sizeof(struct MR_LD_VF_AFFILIATION_111));
1921 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1923 dcmd->cmd = MFI_CMD_DCMD;
1924 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
1925 dcmd->sge_count = 1;
1926 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
1929 dcmd->data_xfer_len =
1930 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
1931 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
1934 dcmd->sgl.sge32[0].phys_addr =
1935 cpu_to_le32(instance->vf_affiliation_111_h);
1937 dcmd->sgl.sge32[0].phys_addr =
1938 cpu_to_le32(new_affiliation_111_h);
1940 dcmd->sgl.sge32[0].length = cpu_to_le32(
1941 sizeof(struct MR_LD_VF_AFFILIATION_111));
1943 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
1944 "scsi%d\n", instance->host->host_no);
1946 megasas_issue_blocked_cmd(instance, cmd, 0);
1948 if (dcmd->cmd_status) {
1949 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
1950 " failed with status 0x%x for scsi%d\n",
1951 dcmd->cmd_status, instance->host->host_no);
1952 retval = 1; /* Do a scan if we couldn't get affiliation */
1957 thisVf = new_affiliation_111->thisVf;
1958 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
1959 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
1960 new_affiliation_111->map[ld].policy[thisVf]) {
1961 dev_warn(&instance->pdev->dev, "SR-IOV: "
1962 "Got new LD/VF affiliation for scsi%d\n",
1963 instance->host->host_no);
1964 memcpy(instance->vf_affiliation_111,
1965 new_affiliation_111,
1966 sizeof(struct MR_LD_VF_AFFILIATION_111));
1972 if (new_affiliation_111) {
1973 pci_free_consistent(instance->pdev,
1974 sizeof(struct MR_LD_VF_AFFILIATION_111),
1975 new_affiliation_111,
1976 new_affiliation_111_h);
1979 megasas_return_cmd(instance, cmd);
1984 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
1987 struct megasas_cmd *cmd;
1988 struct megasas_dcmd_frame *dcmd;
1989 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
1990 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
1991 dma_addr_t new_affiliation_h;
1992 int i, j, retval = 0, found = 0, doscan = 0;
1995 cmd = megasas_get_cmd(instance);
1998 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
1999 "Failed to get cmd for scsi%d\n",
2000 instance->host->host_no);
2004 dcmd = &cmd->frame->dcmd;
2006 if (!instance->vf_affiliation) {
2007 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2008 "affiliation for scsi%d\n", instance->host->host_no);
2009 megasas_return_cmd(instance, cmd);
2014 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2015 sizeof(struct MR_LD_VF_AFFILIATION));
2018 pci_alloc_consistent(instance->pdev,
2019 (MAX_LOGICAL_DRIVES + 1) *
2020 sizeof(struct MR_LD_VF_AFFILIATION),
2021 &new_affiliation_h);
2022 if (!new_affiliation) {
2023 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2024 "memory for new affiliation for scsi%d\n",
2025 instance->host->host_no);
2026 megasas_return_cmd(instance, cmd);
2029 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2030 sizeof(struct MR_LD_VF_AFFILIATION));
2033 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2035 dcmd->cmd = MFI_CMD_DCMD;
2036 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2037 dcmd->sge_count = 1;
2038 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2041 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2042 sizeof(struct MR_LD_VF_AFFILIATION));
2043 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2046 dcmd->sgl.sge32[0].phys_addr =
2047 cpu_to_le32(instance->vf_affiliation_h);
2049 dcmd->sgl.sge32[0].phys_addr =
2050 cpu_to_le32(new_affiliation_h);
2052 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2053 sizeof(struct MR_LD_VF_AFFILIATION));
2055 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2056 "scsi%d\n", instance->host->host_no);
2058 megasas_issue_blocked_cmd(instance, cmd, 0);
2060 if (dcmd->cmd_status) {
2061 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2062 " failed with status 0x%x for scsi%d\n",
2063 dcmd->cmd_status, instance->host->host_no);
2064 retval = 1; /* Do a scan if we couldn't get affiliation */
2069 if (!new_affiliation->ldCount) {
2070 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2071 "affiliation for passive path for scsi%d\n",
2072 instance->host->host_no);
2076 newmap = new_affiliation->map;
2077 savedmap = instance->vf_affiliation->map;
2078 thisVf = new_affiliation->thisVf;
2079 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2081 for (j = 0; j < instance->vf_affiliation->ldCount;
2083 if (newmap->ref.targetId ==
2084 savedmap->ref.targetId) {
2086 if (newmap->policy[thisVf] !=
2087 savedmap->policy[thisVf]) {
2092 savedmap = (struct MR_LD_VF_MAP *)
2093 ((unsigned char *)savedmap +
2096 if (!found && newmap->policy[thisVf] !=
2097 MR_LD_ACCESS_HIDDEN) {
2101 newmap = (struct MR_LD_VF_MAP *)
2102 ((unsigned char *)newmap + newmap->size);
2105 newmap = new_affiliation->map;
2106 savedmap = instance->vf_affiliation->map;
2108 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2110 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2111 if (savedmap->ref.targetId ==
2112 newmap->ref.targetId) {
2114 if (savedmap->policy[thisVf] !=
2115 newmap->policy[thisVf]) {
2120 newmap = (struct MR_LD_VF_MAP *)
2121 ((unsigned char *)newmap +
2124 if (!found && savedmap->policy[thisVf] !=
2125 MR_LD_ACCESS_HIDDEN) {
2129 savedmap = (struct MR_LD_VF_MAP *)
2130 ((unsigned char *)savedmap +
2136 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2137 "affiliation for scsi%d\n", instance->host->host_no);
2138 memcpy(instance->vf_affiliation, new_affiliation,
2139 new_affiliation->size);
2143 if (new_affiliation)
2144 pci_free_consistent(instance->pdev,
2145 (MAX_LOGICAL_DRIVES + 1) *
2146 sizeof(struct MR_LD_VF_AFFILIATION),
2147 new_affiliation, new_affiliation_h);
2148 megasas_return_cmd(instance, cmd);
2153 /* This function will get the current SR-IOV LD/VF affiliation */
2154 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2159 if (instance->PlasmaFW111)
2160 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2162 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2166 /* This function will tell FW to start the SR-IOV heartbeat */
2167 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2170 struct megasas_cmd *cmd;
2171 struct megasas_dcmd_frame *dcmd;
2174 cmd = megasas_get_cmd(instance);
2177 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2178 "Failed to get cmd for scsi%d\n",
2179 instance->host->host_no);
2183 dcmd = &cmd->frame->dcmd;
2186 instance->hb_host_mem =
2187 pci_zalloc_consistent(instance->pdev,
2188 sizeof(struct MR_CTRL_HB_HOST_MEM),
2189 &instance->hb_host_mem_h);
2190 if (!instance->hb_host_mem) {
2191 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2192 " memory for heartbeat host memory for scsi%d\n",
2193 instance->host->host_no);
2199 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2201 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2202 dcmd->cmd = MFI_CMD_DCMD;
2203 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2204 dcmd->sge_count = 1;
2205 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2208 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2209 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2210 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2211 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2213 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2214 instance->host->host_no);
2216 if (instance->ctrl_context && !instance->mask_interrupts)
2217 retval = megasas_issue_blocked_cmd(instance, cmd,
2218 MEGASAS_ROUTINE_WAIT_TIME_VF);
2220 retval = megasas_issue_polled(instance, cmd);
2223 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2224 "_MEM_ALLOC DCMD %s for scsi%d\n",
2225 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2226 "timed out" : "failed", instance->host->host_no);
2231 megasas_return_cmd(instance, cmd);
2236 /* Handler for SR-IOV heartbeat */
2237 void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2239 struct megasas_instance *instance =
2240 (struct megasas_instance *)instance_addr;
2242 if (instance->hb_host_mem->HB.fwCounter !=
2243 instance->hb_host_mem->HB.driverCounter) {
2244 instance->hb_host_mem->HB.driverCounter =
2245 instance->hb_host_mem->HB.fwCounter;
2246 mod_timer(&instance->sriov_heartbeat_timer,
2247 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2249 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2250 "completed for scsi%d\n", instance->host->host_no);
2251 schedule_work(&instance->work_init);
2256 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2257 * @instance: Adapter soft state
2259 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2260 * complete all its outstanding commands. Returns error if one or more IOs
2261 * are pending after this time period. It also marks the controller dead.
2263 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2267 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2269 unsigned long flags;
2270 struct list_head clist_local;
2271 struct megasas_cmd *reset_cmd;
2273 u8 kill_adapter_flag;
2275 spin_lock_irqsave(&instance->hba_lock, flags);
2276 adprecovery = instance->adprecovery;
2277 spin_unlock_irqrestore(&instance->hba_lock, flags);
2279 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2281 INIT_LIST_HEAD(&clist_local);
2282 spin_lock_irqsave(&instance->hba_lock, flags);
2283 list_splice_init(&instance->internal_reset_pending_q,
2285 spin_unlock_irqrestore(&instance->hba_lock, flags);
2287 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2288 for (i = 0; i < wait_time; i++) {
2290 spin_lock_irqsave(&instance->hba_lock, flags);
2291 adprecovery = instance->adprecovery;
2292 spin_unlock_irqrestore(&instance->hba_lock, flags);
2293 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2297 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2298 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2299 spin_lock_irqsave(&instance->hba_lock, flags);
2300 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2301 spin_unlock_irqrestore(&instance->hba_lock, flags);
2306 while (!list_empty(&clist_local)) {
2307 reset_cmd = list_entry((&clist_local)->next,
2308 struct megasas_cmd, list);
2309 list_del_init(&reset_cmd->list);
2310 if (reset_cmd->scmd) {
2311 reset_cmd->scmd->result = DID_RESET << 16;
2312 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2313 reset_index, reset_cmd,
2314 reset_cmd->scmd->cmnd[0]);
2316 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2317 megasas_return_cmd(instance, reset_cmd);
2318 } else if (reset_cmd->sync_cmd) {
2319 dev_notice(&instance->pdev->dev, "%p synch cmds"
2323 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2324 instance->instancet->fire_cmd(instance,
2325 reset_cmd->frame_phys_addr,
2326 0, instance->reg_set);
2328 dev_notice(&instance->pdev->dev, "%p unexpected"
2338 for (i = 0; i < resetwaittime; i++) {
2339 int outstanding = atomic_read(&instance->fw_outstanding);
2344 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2345 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2346 "commands to complete\n",i,outstanding);
2348 * Call cmd completion routine. Cmd to be
2349 * be completed directly without depending on isr.
2351 megasas_complete_cmd_dpc((unsigned long)instance);
2358 kill_adapter_flag = 0;
2360 fw_state = instance->instancet->read_fw_status_reg(
2361 instance->reg_set) & MFI_STATE_MASK;
2362 if ((fw_state == MFI_STATE_FAULT) &&
2363 (instance->disableOnlineCtrlReset == 0)) {
2365 kill_adapter_flag = 2;
2368 megasas_do_ocr(instance);
2369 kill_adapter_flag = 1;
2371 /* wait for 1 secs to let FW finish the pending cmds */
2377 if (atomic_read(&instance->fw_outstanding) && !kill_adapter_flag) {
2378 if (instance->disableOnlineCtrlReset == 0) {
2379 megasas_do_ocr(instance);
2381 /* wait for 5 secs to let FW finish the pending cmds */
2382 for (i = 0; i < wait_time; i++) {
2384 atomic_read(&instance->fw_outstanding);
2392 if (atomic_read(&instance->fw_outstanding) ||
2393 (kill_adapter_flag == 2)) {
2394 dev_notice(&instance->pdev->dev, "pending cmds after reset\n");
2396 * Send signal to FW to stop processing any pending cmds.
2397 * The controller will be taken offline by the OS now.
2399 if ((instance->pdev->device ==
2400 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2401 (instance->pdev->device ==
2402 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2403 writel(MFI_STOP_ADP,
2404 &instance->reg_set->doorbell);
2406 writel(MFI_STOP_ADP,
2407 &instance->reg_set->inbound_doorbell);
2409 megasas_dump_pending_frames(instance);
2410 spin_lock_irqsave(&instance->hba_lock, flags);
2411 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2412 spin_unlock_irqrestore(&instance->hba_lock, flags);
2416 dev_notice(&instance->pdev->dev, "no pending cmds after reset\n");
2422 * megasas_generic_reset - Generic reset routine
2423 * @scmd: Mid-layer SCSI command
2425 * This routine implements a generic reset handler for device, bus and host
2426 * reset requests. Device, bus and host specific reset handlers can use this
2427 * function after they do their specific tasks.
2429 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2432 struct megasas_instance *instance;
2434 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2436 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2437 scmd->cmnd[0], scmd->retries);
2439 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2440 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2444 ret_val = megasas_wait_for_outstanding(instance);
2445 if (ret_val == SUCCESS)
2446 dev_notice(&instance->pdev->dev, "reset successful\n");
2448 dev_err(&instance->pdev->dev, "failed to do reset\n");
2454 * megasas_reset_timer - quiesce the adapter if required
2457 * Sets the FW busy flag and reduces the host->can_queue if the
2458 * cmd has not been completed within the timeout period.
2461 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2463 struct megasas_instance *instance;
2464 unsigned long flags;
2466 if (time_after(jiffies, scmd->jiffies_at_alloc +
2467 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
2468 return BLK_EH_NOT_HANDLED;
2471 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2472 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2473 /* FW is busy, throttle IO */
2474 spin_lock_irqsave(instance->host->host_lock, flags);
2476 instance->host->can_queue = instance->throttlequeuedepth;
2477 instance->last_time = jiffies;
2478 instance->flag |= MEGASAS_FW_BUSY;
2480 spin_unlock_irqrestore(instance->host->host_lock, flags);
2482 return BLK_EH_RESET_TIMER;
2486 * megasas_reset_device - Device reset handler entry point
2488 static int megasas_reset_device(struct scsi_cmnd *scmd)
2491 * First wait for all commands to complete
2493 return megasas_generic_reset(scmd);
2497 * megasas_reset_bus_host - Bus & host reset handler entry point
2499 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2502 struct megasas_instance *instance;
2504 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2507 * First wait for all commands to complete
2509 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
2510 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
2511 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2512 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2513 ret = megasas_reset_fusion(scmd->device->host, 1);
2515 ret = megasas_generic_reset(scmd);
2521 * megasas_bios_param - Returns disk geometry for a disk
2522 * @sdev: device handle
2523 * @bdev: block device
2524 * @capacity: drive capacity
2525 * @geom: geometry parameters
2528 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2529 sector_t capacity, int geom[])
2536 /* Default heads (64) & sectors (32) */
2540 tmp = heads * sectors;
2541 cylinders = capacity;
2543 sector_div(cylinders, tmp);
2546 * Handle extended translation size for logical drives > 1Gb
2549 if (capacity >= 0x200000) {
2552 tmp = heads*sectors;
2553 cylinders = capacity;
2554 sector_div(cylinders, tmp);
2559 geom[2] = cylinders;
2564 static void megasas_aen_polling(struct work_struct *work);
2567 * megasas_service_aen - Processes an event notification
2568 * @instance: Adapter soft state
2569 * @cmd: AEN command completed by the ISR
2571 * For AEN, driver sends a command down to FW that is held by the FW till an
2572 * event occurs. When an event of interest occurs, FW completes the command
2573 * that it was previously holding.
2575 * This routines sends SIGIO signal to processes that have registered with the
2579 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2581 unsigned long flags;
2584 * Don't signal app if it is just an aborted previously registered aen
2586 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2587 spin_lock_irqsave(&poll_aen_lock, flags);
2588 megasas_poll_wait_aen = 1;
2589 spin_unlock_irqrestore(&poll_aen_lock, flags);
2590 wake_up(&megasas_poll_wait);
2591 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2596 instance->aen_cmd = NULL;
2598 megasas_return_cmd(instance, cmd);
2600 if ((instance->unload == 0) &&
2601 ((instance->issuepend_done == 1))) {
2602 struct megasas_aen_event *ev;
2604 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2606 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
2608 ev->instance = instance;
2610 INIT_DELAYED_WORK(&ev->hotplug_work,
2611 megasas_aen_polling);
2612 schedule_delayed_work(&ev->hotplug_work, 0);
2618 megasas_fw_crash_buffer_store(struct device *cdev,
2619 struct device_attribute *attr, const char *buf, size_t count)
2621 struct Scsi_Host *shost = class_to_shost(cdev);
2622 struct megasas_instance *instance =
2623 (struct megasas_instance *) shost->hostdata;
2625 unsigned long flags;
2627 if (kstrtoint(buf, 0, &val) != 0)
2630 spin_lock_irqsave(&instance->crashdump_lock, flags);
2631 instance->fw_crash_buffer_offset = val;
2632 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2637 megasas_fw_crash_buffer_show(struct device *cdev,
2638 struct device_attribute *attr, char *buf)
2640 struct Scsi_Host *shost = class_to_shost(cdev);
2641 struct megasas_instance *instance =
2642 (struct megasas_instance *) shost->hostdata;
2644 unsigned long buff_addr;
2645 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2646 unsigned long src_addr;
2647 unsigned long flags;
2650 spin_lock_irqsave(&instance->crashdump_lock, flags);
2651 buff_offset = instance->fw_crash_buffer_offset;
2652 if (!instance->crash_dump_buf &&
2653 !((instance->fw_crash_state == AVAILABLE) ||
2654 (instance->fw_crash_state == COPYING))) {
2655 dev_err(&instance->pdev->dev,
2656 "Firmware crash dump is not available\n");
2657 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2661 buff_addr = (unsigned long) buf;
2663 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
2664 dev_err(&instance->pdev->dev,
2665 "Firmware crash dump offset is out of range\n");
2666 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2670 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2671 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2673 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2674 (buff_offset % dmachunk);
2675 memcpy(buf, (void *)src_addr, size);
2676 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2682 megasas_fw_crash_buffer_size_show(struct device *cdev,
2683 struct device_attribute *attr, char *buf)
2685 struct Scsi_Host *shost = class_to_shost(cdev);
2686 struct megasas_instance *instance =
2687 (struct megasas_instance *) shost->hostdata;
2689 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2690 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2694 megasas_fw_crash_state_store(struct device *cdev,
2695 struct device_attribute *attr, const char *buf, size_t count)
2697 struct Scsi_Host *shost = class_to_shost(cdev);
2698 struct megasas_instance *instance =
2699 (struct megasas_instance *) shost->hostdata;
2701 unsigned long flags;
2703 if (kstrtoint(buf, 0, &val) != 0)
2706 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2707 dev_err(&instance->pdev->dev, "application updates invalid "
2708 "firmware crash state\n");
2712 instance->fw_crash_state = val;
2714 if ((val == COPIED) || (val == COPY_ERROR)) {
2715 spin_lock_irqsave(&instance->crashdump_lock, flags);
2716 megasas_free_host_crash_buffer(instance);
2717 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2718 if (val == COPY_ERROR)
2719 dev_info(&instance->pdev->dev, "application failed to "
2720 "copy Firmware crash dump\n");
2722 dev_info(&instance->pdev->dev, "Firmware crash dump "
2723 "copied successfully\n");
2729 megasas_fw_crash_state_show(struct device *cdev,
2730 struct device_attribute *attr, char *buf)
2732 struct Scsi_Host *shost = class_to_shost(cdev);
2733 struct megasas_instance *instance =
2734 (struct megasas_instance *) shost->hostdata;
2736 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2740 megasas_page_size_show(struct device *cdev,
2741 struct device_attribute *attr, char *buf)
2743 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2746 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2747 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2748 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2749 megasas_fw_crash_buffer_size_show, NULL);
2750 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2751 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2752 static DEVICE_ATTR(page_size, S_IRUGO,
2753 megasas_page_size_show, NULL);
2755 struct device_attribute *megaraid_host_attrs[] = {
2756 &dev_attr_fw_crash_buffer_size,
2757 &dev_attr_fw_crash_buffer,
2758 &dev_attr_fw_crash_state,
2759 &dev_attr_page_size,
2764 * Scsi host template for megaraid_sas driver
2766 static struct scsi_host_template megasas_template = {
2768 .module = THIS_MODULE,
2769 .name = "Avago SAS based MegaRAID driver",
2770 .proc_name = "megaraid_sas",
2771 .slave_configure = megasas_slave_configure,
2772 .slave_alloc = megasas_slave_alloc,
2773 .queuecommand = megasas_queue_command,
2774 .eh_device_reset_handler = megasas_reset_device,
2775 .eh_bus_reset_handler = megasas_reset_bus_host,
2776 .eh_host_reset_handler = megasas_reset_bus_host,
2777 .eh_timed_out = megasas_reset_timer,
2778 .shost_attrs = megaraid_host_attrs,
2779 .bios_param = megasas_bios_param,
2780 .use_clustering = ENABLE_CLUSTERING,
2781 .change_queue_depth = scsi_change_queue_depth,
2786 * megasas_complete_int_cmd - Completes an internal command
2787 * @instance: Adapter soft state
2788 * @cmd: Command to be completed
2790 * The megasas_issue_blocked_cmd() function waits for a command to complete
2791 * after it issues a command. This function wakes up that waiting routine by
2792 * calling wake_up() on the wait queue.
2795 megasas_complete_int_cmd(struct megasas_instance *instance,
2796 struct megasas_cmd *cmd)
2798 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
2799 wake_up(&instance->int_cmd_wait_q);
2803 * megasas_complete_abort - Completes aborting a command
2804 * @instance: Adapter soft state
2805 * @cmd: Cmd that was issued to abort another cmd
2807 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2808 * after it issues an abort on a previously issued command. This function
2809 * wakes up all functions waiting on the same wait queue.
2812 megasas_complete_abort(struct megasas_instance *instance,
2813 struct megasas_cmd *cmd)
2815 if (cmd->sync_cmd) {
2817 cmd->cmd_status_drv = 0;
2818 wake_up(&instance->abort_cmd_wait_q);
2823 * megasas_complete_cmd - Completes a command
2824 * @instance: Adapter soft state
2825 * @cmd: Command to be completed
2826 * @alt_status: If non-zero, use this value as status to
2827 * SCSI mid-layer instead of the value returned
2828 * by the FW. This should be used if caller wants
2829 * an alternate status (as in the case of aborted
2833 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2837 struct megasas_header *hdr = &cmd->frame->hdr;
2838 unsigned long flags;
2839 struct fusion_context *fusion = instance->ctrl_context;
2842 /* flag for the retry reset */
2843 cmd->retry_for_fw_reset = 0;
2846 cmd->scmd->SCp.ptr = NULL;
2849 case MFI_CMD_INVALID:
2850 /* Some older 1068 controller FW may keep a pended
2851 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2852 when booting the kdump kernel. Ignore this command to
2853 prevent a kernel panic on shutdown of the kdump kernel. */
2854 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
2856 dev_warn(&instance->pdev->dev, "If you have a controller "
2857 "other than PERC5, please upgrade your firmware\n");
2859 case MFI_CMD_PD_SCSI_IO:
2860 case MFI_CMD_LD_SCSI_IO:
2863 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2864 * issued either through an IO path or an IOCTL path. If it
2865 * was via IOCTL, we will send it to internal completion.
2867 if (cmd->sync_cmd) {
2869 megasas_complete_int_cmd(instance, cmd);
2873 case MFI_CMD_LD_READ:
2874 case MFI_CMD_LD_WRITE:
2877 cmd->scmd->result = alt_status << 16;
2883 atomic_dec(&instance->fw_outstanding);
2885 scsi_dma_unmap(cmd->scmd);
2886 cmd->scmd->scsi_done(cmd->scmd);
2887 megasas_return_cmd(instance, cmd);
2892 switch (hdr->cmd_status) {
2895 cmd->scmd->result = DID_OK << 16;
2898 case MFI_STAT_SCSI_IO_FAILED:
2899 case MFI_STAT_LD_INIT_IN_PROGRESS:
2901 (DID_ERROR << 16) | hdr->scsi_status;
2904 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2906 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2908 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2909 memset(cmd->scmd->sense_buffer, 0,
2910 SCSI_SENSE_BUFFERSIZE);
2911 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2914 cmd->scmd->result |= DRIVER_SENSE << 24;
2919 case MFI_STAT_LD_OFFLINE:
2920 case MFI_STAT_DEVICE_NOT_FOUND:
2921 cmd->scmd->result = DID_BAD_TARGET << 16;
2925 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
2927 cmd->scmd->result = DID_ERROR << 16;
2931 atomic_dec(&instance->fw_outstanding);
2933 scsi_dma_unmap(cmd->scmd);
2934 cmd->scmd->scsi_done(cmd->scmd);
2935 megasas_return_cmd(instance, cmd);
2942 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
2943 /* Check for LD map update */
2944 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
2945 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
2946 fusion->fast_path_io = 0;
2947 spin_lock_irqsave(instance->host->host_lock, flags);
2948 if (cmd->frame->hdr.cmd_status != 0) {
2949 if (cmd->frame->hdr.cmd_status !=
2951 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
2952 cmd->frame->hdr.cmd_status);
2954 megasas_return_cmd(instance, cmd);
2955 spin_unlock_irqrestore(
2956 instance->host->host_lock,
2962 megasas_return_cmd(instance, cmd);
2965 * Set fast path IO to ZERO.
2966 * Validate Map will set proper value.
2967 * Meanwhile all IOs will go as LD IO.
2969 if (MR_ValidateMapInfo(instance))
2970 fusion->fast_path_io = 1;
2972 fusion->fast_path_io = 0;
2973 megasas_sync_map_info(instance);
2974 spin_unlock_irqrestore(instance->host->host_lock,
2978 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2979 opcode == MR_DCMD_CTRL_EVENT_GET) {
2980 spin_lock_irqsave(&poll_aen_lock, flags);
2981 megasas_poll_wait_aen = 0;
2982 spin_unlock_irqrestore(&poll_aen_lock, flags);
2986 * See if got an event notification
2988 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
2989 megasas_service_aen(instance, cmd);
2991 megasas_complete_int_cmd(instance, cmd);
2997 * Cmd issued to abort another cmd returned
2999 megasas_complete_abort(instance, cmd);
3003 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3010 * megasas_issue_pending_cmds_again - issue all pending cmds
3011 * in FW again because of the fw reset
3012 * @instance: Adapter soft state
3015 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3017 struct megasas_cmd *cmd;
3018 struct list_head clist_local;
3019 union megasas_evt_class_locale class_locale;
3020 unsigned long flags;
3023 INIT_LIST_HEAD(&clist_local);
3024 spin_lock_irqsave(&instance->hba_lock, flags);
3025 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3026 spin_unlock_irqrestore(&instance->hba_lock, flags);
3028 while (!list_empty(&clist_local)) {
3029 cmd = list_entry((&clist_local)->next,
3030 struct megasas_cmd, list);
3031 list_del_init(&cmd->list);
3033 if (cmd->sync_cmd || cmd->scmd) {
3034 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3035 "detected to be pending while HBA reset\n",
3036 cmd, cmd->scmd, cmd->sync_cmd);
3038 cmd->retry_for_fw_reset++;
3040 if (cmd->retry_for_fw_reset == 3) {
3041 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3042 "was tried multiple times during reset."
3043 "Shutting down the HBA\n",
3044 cmd, cmd->scmd, cmd->sync_cmd);
3045 instance->instancet->disable_intr(instance);
3046 atomic_set(&instance->fw_reset_no_pci_access, 1);
3047 megaraid_sas_kill_hba(instance);
3052 if (cmd->sync_cmd == 1) {
3054 dev_notice(&instance->pdev->dev, "unexpected"
3055 "cmd attached to internal command!\n");
3057 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3058 "on the internal reset queue,"
3059 "issue it again.\n", cmd);
3060 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3061 instance->instancet->fire_cmd(instance,
3062 cmd->frame_phys_addr,
3063 0, instance->reg_set);
3064 } else if (cmd->scmd) {
3065 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3066 "detected on the internal queue, issue again.\n",
3067 cmd, cmd->scmd->cmnd[0]);
3069 atomic_inc(&instance->fw_outstanding);
3070 instance->instancet->fire_cmd(instance,
3071 cmd->frame_phys_addr,
3072 cmd->frame_count-1, instance->reg_set);
3074 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3075 "internal reset defer list while re-issue!!\n",
3080 if (instance->aen_cmd) {
3081 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3082 megasas_return_cmd(instance, instance->aen_cmd);
3084 instance->aen_cmd = NULL;
3088 * Initiate AEN (Asynchronous Event Notification)
3090 seq_num = instance->last_seq_num;
3091 class_locale.members.reserved = 0;
3092 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3093 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3095 megasas_register_aen(instance, seq_num, class_locale.word);
3099 * Move the internal reset pending commands to a deferred queue.
3101 * We move the commands pending at internal reset time to a
3102 * pending queue. This queue would be flushed after successful
3103 * completion of the internal reset sequence. if the internal reset
3104 * did not complete in time, the kernel reset handler would flush
3108 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3110 struct megasas_cmd *cmd;
3112 u32 max_cmd = instance->max_fw_cmds;
3114 unsigned long flags;
3117 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3118 for (i = 0; i < max_cmd; i++) {
3119 cmd = instance->cmd_list[i];
3120 if (cmd->sync_cmd == 1 || cmd->scmd) {
3121 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3122 "on the defer queue as internal\n",
3123 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3125 if (!list_empty(&cmd->list)) {
3126 dev_notice(&instance->pdev->dev, "ERROR while"
3127 " moving this cmd:%p, %d %p, it was"
3128 "discovered on some list?\n",
3129 cmd, cmd->sync_cmd, cmd->scmd);
3131 list_del_init(&cmd->list);
3134 list_add_tail(&cmd->list,
3135 &instance->internal_reset_pending_q);
3138 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3143 process_fw_state_change_wq(struct work_struct *work)
3145 struct megasas_instance *instance =
3146 container_of(work, struct megasas_instance, work_init);
3148 unsigned long flags;
3150 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
3151 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3152 instance->adprecovery);
3156 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
3157 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3158 "state, restarting it...\n");
3160 instance->instancet->disable_intr(instance);
3161 atomic_set(&instance->fw_outstanding, 0);
3163 atomic_set(&instance->fw_reset_no_pci_access, 1);
3164 instance->instancet->adp_reset(instance, instance->reg_set);
3165 atomic_set(&instance->fw_reset_no_pci_access, 0);
3167 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3168 "initiating next stage...\n");
3170 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3171 "state 2 starting...\n");
3173 /* waiting for about 20 second before start the second init */
3174 for (wait = 0; wait < 30; wait++) {
3178 if (megasas_transition_to_ready(instance, 1)) {
3179 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3181 atomic_set(&instance->fw_reset_no_pci_access, 1);
3182 megaraid_sas_kill_hba(instance);
3186 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3187 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3188 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3190 *instance->consumer = *instance->producer;
3192 *instance->consumer = 0;
3193 *instance->producer = 0;
3196 megasas_issue_init_mfi(instance);
3198 spin_lock_irqsave(&instance->hba_lock, flags);
3199 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3200 spin_unlock_irqrestore(&instance->hba_lock, flags);
3201 instance->instancet->enable_intr(instance);
3203 megasas_issue_pending_cmds_again(instance);
3204 instance->issuepend_done = 1;
3209 * megasas_deplete_reply_queue - Processes all completed commands
3210 * @instance: Adapter soft state
3211 * @alt_status: Alternate status to be returned to
3212 * SCSI mid-layer instead of the status
3213 * returned by the FW
3214 * Note: this must be called with hba lock held
3217 megasas_deplete_reply_queue(struct megasas_instance *instance,
3223 if ((mfiStatus = instance->instancet->check_reset(instance,
3224 instance->reg_set)) == 1) {
3228 if ((mfiStatus = instance->instancet->clear_intr(
3231 /* Hardware may not set outbound_intr_status in MSI-X mode */
3232 if (!instance->msix_vectors)
3236 instance->mfiStatus = mfiStatus;
3238 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3239 fw_state = instance->instancet->read_fw_status_reg(
3240 instance->reg_set) & MFI_STATE_MASK;
3242 if (fw_state != MFI_STATE_FAULT) {
3243 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3247 if ((fw_state == MFI_STATE_FAULT) &&
3248 (instance->disableOnlineCtrlReset == 0)) {
3249 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3251 if ((instance->pdev->device ==
3252 PCI_DEVICE_ID_LSI_SAS1064R) ||
3253 (instance->pdev->device ==
3254 PCI_DEVICE_ID_DELL_PERC5) ||
3255 (instance->pdev->device ==
3256 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3258 *instance->consumer =
3259 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3263 instance->instancet->disable_intr(instance);
3264 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
3265 instance->issuepend_done = 0;
3267 atomic_set(&instance->fw_outstanding, 0);
3268 megasas_internal_reset_defer_cmds(instance);
3270 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3271 fw_state, instance->adprecovery);
3273 schedule_work(&instance->work_init);
3277 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3278 fw_state, instance->disableOnlineCtrlReset);
3282 tasklet_schedule(&instance->isr_tasklet);
3286 * megasas_isr - isr entry point
3288 static irqreturn_t megasas_isr(int irq, void *devp)
3290 struct megasas_irq_context *irq_context = devp;
3291 struct megasas_instance *instance = irq_context->instance;
3292 unsigned long flags;
3295 if (atomic_read(&instance->fw_reset_no_pci_access))
3298 spin_lock_irqsave(&instance->hba_lock, flags);
3299 rc = megasas_deplete_reply_queue(instance, DID_OK);
3300 spin_unlock_irqrestore(&instance->hba_lock, flags);
3306 * megasas_transition_to_ready - Move the FW to READY state
3307 * @instance: Adapter soft state
3309 * During the initialization, FW passes can potentially be in any one of
3310 * several possible states. If the FW in operational, waiting-for-handshake
3311 * states, driver must take steps to bring it to ready state. Otherwise, it
3312 * has to wait for the ready state.
3315 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3321 u32 abs_state, curr_abs_state;
3323 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3324 fw_state = abs_state & MFI_STATE_MASK;
3326 if (fw_state != MFI_STATE_READY)
3327 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3330 while (fw_state != MFI_STATE_READY) {
3334 case MFI_STATE_FAULT:
3335 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3337 max_wait = MEGASAS_RESET_WAIT_TIME;
3338 cur_state = MFI_STATE_FAULT;
3343 case MFI_STATE_WAIT_HANDSHAKE:
3345 * Set the CLR bit in inbound doorbell
3347 if ((instance->pdev->device ==
3348 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3349 (instance->pdev->device ==
3350 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3351 (instance->pdev->device ==
3352 PCI_DEVICE_ID_LSI_FUSION) ||
3353 (instance->pdev->device ==
3354 PCI_DEVICE_ID_LSI_PLASMA) ||
3355 (instance->pdev->device ==
3356 PCI_DEVICE_ID_LSI_INVADER) ||
3357 (instance->pdev->device ==
3358 PCI_DEVICE_ID_LSI_FURY)) {
3360 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3361 &instance->reg_set->doorbell);
3364 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3365 &instance->reg_set->inbound_doorbell);
3368 max_wait = MEGASAS_RESET_WAIT_TIME;
3369 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3372 case MFI_STATE_BOOT_MESSAGE_PENDING:
3373 if ((instance->pdev->device ==
3374 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3375 (instance->pdev->device ==
3376 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3377 (instance->pdev->device ==
3378 PCI_DEVICE_ID_LSI_FUSION) ||
3379 (instance->pdev->device ==
3380 PCI_DEVICE_ID_LSI_PLASMA) ||
3381 (instance->pdev->device ==
3382 PCI_DEVICE_ID_LSI_INVADER) ||
3383 (instance->pdev->device ==
3384 PCI_DEVICE_ID_LSI_FURY)) {
3385 writel(MFI_INIT_HOTPLUG,
3386 &instance->reg_set->doorbell);
3388 writel(MFI_INIT_HOTPLUG,
3389 &instance->reg_set->inbound_doorbell);
3391 max_wait = MEGASAS_RESET_WAIT_TIME;
3392 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3395 case MFI_STATE_OPERATIONAL:
3397 * Bring it to READY state; assuming max wait 10 secs
3399 instance->instancet->disable_intr(instance);
3400 if ((instance->pdev->device ==
3401 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3402 (instance->pdev->device ==
3403 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3404 (instance->pdev->device
3405 == PCI_DEVICE_ID_LSI_FUSION) ||
3406 (instance->pdev->device
3407 == PCI_DEVICE_ID_LSI_PLASMA) ||
3408 (instance->pdev->device
3409 == PCI_DEVICE_ID_LSI_INVADER) ||
3410 (instance->pdev->device
3411 == PCI_DEVICE_ID_LSI_FURY)) {
3412 writel(MFI_RESET_FLAGS,
3413 &instance->reg_set->doorbell);
3414 if ((instance->pdev->device ==
3415 PCI_DEVICE_ID_LSI_FUSION) ||
3416 (instance->pdev->device ==
3417 PCI_DEVICE_ID_LSI_PLASMA) ||
3418 (instance->pdev->device ==
3419 PCI_DEVICE_ID_LSI_INVADER) ||
3420 (instance->pdev->device ==
3421 PCI_DEVICE_ID_LSI_FURY)) {
3422 for (i = 0; i < (10 * 1000); i += 20) {
3433 writel(MFI_RESET_FLAGS,
3434 &instance->reg_set->inbound_doorbell);
3436 max_wait = MEGASAS_RESET_WAIT_TIME;
3437 cur_state = MFI_STATE_OPERATIONAL;
3440 case MFI_STATE_UNDEFINED:
3442 * This state should not last for more than 2 seconds
3444 max_wait = MEGASAS_RESET_WAIT_TIME;
3445 cur_state = MFI_STATE_UNDEFINED;
3448 case MFI_STATE_BB_INIT:
3449 max_wait = MEGASAS_RESET_WAIT_TIME;
3450 cur_state = MFI_STATE_BB_INIT;
3453 case MFI_STATE_FW_INIT:
3454 max_wait = MEGASAS_RESET_WAIT_TIME;
3455 cur_state = MFI_STATE_FW_INIT;
3458 case MFI_STATE_FW_INIT_2:
3459 max_wait = MEGASAS_RESET_WAIT_TIME;
3460 cur_state = MFI_STATE_FW_INIT_2;
3463 case MFI_STATE_DEVICE_SCAN:
3464 max_wait = MEGASAS_RESET_WAIT_TIME;
3465 cur_state = MFI_STATE_DEVICE_SCAN;
3468 case MFI_STATE_FLUSH_CACHE:
3469 max_wait = MEGASAS_RESET_WAIT_TIME;
3470 cur_state = MFI_STATE_FLUSH_CACHE;
3474 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3480 * The cur_state should not last for more than max_wait secs
3482 for (i = 0; i < (max_wait * 1000); i++) {
3483 curr_abs_state = instance->instancet->
3484 read_fw_status_reg(instance->reg_set);
3486 if (abs_state == curr_abs_state) {
3493 * Return error if fw_state hasn't changed after max_wait
3495 if (curr_abs_state == abs_state) {
3496 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3497 "in %d secs\n", fw_state, max_wait);
3501 abs_state = curr_abs_state;
3502 fw_state = curr_abs_state & MFI_STATE_MASK;
3504 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3510 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3511 * @instance: Adapter soft state
3513 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3516 u32 max_cmd = instance->max_mfi_cmds;
3517 struct megasas_cmd *cmd;
3519 if (!instance->frame_dma_pool)
3523 * Return all frames to pool
3525 for (i = 0; i < max_cmd; i++) {
3527 cmd = instance->cmd_list[i];
3530 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3531 cmd->frame_phys_addr);
3534 pci_pool_free(instance->sense_dma_pool, cmd->sense,
3535 cmd->sense_phys_addr);
3539 * Now destroy the pool itself
3541 pci_pool_destroy(instance->frame_dma_pool);
3542 pci_pool_destroy(instance->sense_dma_pool);
3544 instance->frame_dma_pool = NULL;
3545 instance->sense_dma_pool = NULL;
3549 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3550 * @instance: Adapter soft state
3552 * Each command packet has an embedded DMA memory buffer that is used for
3553 * filling MFI frame and the SG list that immediately follows the frame. This
3554 * function creates those DMA memory buffers for each command packet by using
3555 * PCI pool facility.
3557 static int megasas_create_frame_pool(struct megasas_instance *instance)
3564 struct megasas_cmd *cmd;
3566 max_cmd = instance->max_mfi_cmds;
3569 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3570 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3572 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3573 sizeof(struct megasas_sge32);
3575 if (instance->flag_ieee)
3576 sge_sz = sizeof(struct megasas_sge_skinny);
3579 * For MFI controllers.
3581 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3582 * Total 960 byte (15 MFI frame of 64 byte)
3584 * Fusion adapter require only 3 extra frame.
3585 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3586 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3587 * Total 192 byte (3 MFI frame of 64 byte)
3589 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
3590 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3592 * Use DMA pool facility provided by PCI layer
3594 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
3595 instance->pdev, total_sz, 256, 0);
3597 if (!instance->frame_dma_pool) {
3598 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
3602 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3603 instance->pdev, 128, 4, 0);
3605 if (!instance->sense_dma_pool) {
3606 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
3608 pci_pool_destroy(instance->frame_dma_pool);
3609 instance->frame_dma_pool = NULL;
3615 * Allocate and attach a frame to each of the commands in cmd_list.
3616 * By making cmd->index as the context instead of the &cmd, we can
3617 * always use 32bit context regardless of the architecture
3619 for (i = 0; i < max_cmd; i++) {
3621 cmd = instance->cmd_list[i];
3623 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3624 GFP_KERNEL, &cmd->frame_phys_addr);
3626 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3627 GFP_KERNEL, &cmd->sense_phys_addr);
3630 * megasas_teardown_frame_pool() takes care of freeing
3631 * whatever has been allocated
3633 if (!cmd->frame || !cmd->sense) {
3634 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
3635 megasas_teardown_frame_pool(instance);
3639 memset(cmd->frame, 0, total_sz);
3640 cmd->frame->io.context = cpu_to_le32(cmd->index);
3641 cmd->frame->io.pad_0 = 0;
3642 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
3643 (instance->pdev->device != PCI_DEVICE_ID_LSI_PLASMA) &&
3644 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
3645 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
3647 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
3654 * megasas_free_cmds - Free all the cmds in the free cmd pool
3655 * @instance: Adapter soft state
3657 void megasas_free_cmds(struct megasas_instance *instance)
3661 /* First free the MFI frame pool */
3662 megasas_teardown_frame_pool(instance);
3664 /* Free all the commands in the cmd_list */
3665 for (i = 0; i < instance->max_mfi_cmds; i++)
3667 kfree(instance->cmd_list[i]);
3669 /* Free the cmd_list buffer itself */
3670 kfree(instance->cmd_list);
3671 instance->cmd_list = NULL;
3673 INIT_LIST_HEAD(&instance->cmd_pool);
3677 * megasas_alloc_cmds - Allocates the command packets
3678 * @instance: Adapter soft state
3680 * Each command that is issued to the FW, whether IO commands from the OS or
3681 * internal commands like IOCTLs, are wrapped in local data structure called
3682 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3685 * Each frame has a 32-bit field called context (tag). This context is used
3686 * to get back the megasas_cmd from the frame when a frame gets completed in
3687 * the ISR. Typically the address of the megasas_cmd itself would be used as
3688 * the context. But we wanted to keep the differences between 32 and 64 bit
3689 * systems to the mininum. We always use 32 bit integers for the context. In
3690 * this driver, the 32 bit values are the indices into an array cmd_list.
3691 * This array is used only to look up the megasas_cmd given the context. The
3692 * free commands themselves are maintained in a linked list called cmd_pool.
3694 int megasas_alloc_cmds(struct megasas_instance *instance)
3699 struct megasas_cmd *cmd;
3700 struct fusion_context *fusion;
3702 fusion = instance->ctrl_context;
3703 max_cmd = instance->max_mfi_cmds;
3706 * instance->cmd_list is an array of struct megasas_cmd pointers.
3707 * Allocate the dynamic array first and then allocate individual
3710 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3712 if (!instance->cmd_list) {
3713 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
3717 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3719 for (i = 0; i < max_cmd; i++) {
3720 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3723 if (!instance->cmd_list[i]) {
3725 for (j = 0; j < i; j++)
3726 kfree(instance->cmd_list[j]);
3728 kfree(instance->cmd_list);
3729 instance->cmd_list = NULL;
3735 for (i = 0; i < max_cmd; i++) {
3736 cmd = instance->cmd_list[i];
3737 memset(cmd, 0, sizeof(struct megasas_cmd));
3740 cmd->instance = instance;
3742 list_add_tail(&cmd->list, &instance->cmd_pool);
3746 * Create a frame pool and assign one frame to each cmd
3748 if (megasas_create_frame_pool(instance)) {
3749 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
3750 megasas_free_cmds(instance);
3757 * megasas_get_pd_list_info - Returns FW's pd_list structure
3758 * @instance: Adapter soft state
3759 * @pd_list: pd_list structure
3761 * Issues an internal command (DCMD) to get the FW's controller PD
3762 * list structure. This information is mainly used to find out SYSTEM
3763 * supported by the FW.
3766 megasas_get_pd_list(struct megasas_instance *instance)
3768 int ret = 0, pd_index = 0;
3769 struct megasas_cmd *cmd;
3770 struct megasas_dcmd_frame *dcmd;
3771 struct MR_PD_LIST *ci;
3772 struct MR_PD_ADDRESS *pd_addr;
3773 dma_addr_t ci_h = 0;
3775 cmd = megasas_get_cmd(instance);
3778 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
3782 dcmd = &cmd->frame->dcmd;
3784 ci = pci_alloc_consistent(instance->pdev,
3785 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3788 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
3789 megasas_return_cmd(instance, cmd);
3793 memset(ci, 0, sizeof(*ci));
3794 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3796 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3797 dcmd->mbox.b[1] = 0;
3798 dcmd->cmd = MFI_CMD_DCMD;
3799 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3800 dcmd->sge_count = 1;
3801 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3804 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3805 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3806 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3807 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3809 if (instance->ctrl_context && !instance->mask_interrupts)
3810 ret = megasas_issue_blocked_cmd(instance, cmd,
3811 MEGASAS_BLOCKED_CMD_TIMEOUT);
3813 ret = megasas_issue_polled(instance, cmd);
3816 * the following function will get the instance PD LIST.
3822 (le32_to_cpu(ci->count) <
3823 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3825 memset(instance->local_pd_list, 0,
3826 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3828 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
3830 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
3831 le16_to_cpu(pd_addr->deviceId);
3832 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
3833 pd_addr->scsiDevType;
3834 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
3838 memcpy(instance->pd_list, instance->local_pd_list,
3839 sizeof(instance->pd_list));
3842 pci_free_consistent(instance->pdev,
3843 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3846 megasas_return_cmd(instance, cmd);
3852 * megasas_get_ld_list_info - Returns FW's ld_list structure
3853 * @instance: Adapter soft state
3854 * @ld_list: ld_list structure
3856 * Issues an internal command (DCMD) to get the FW's controller PD
3857 * list structure. This information is mainly used to find out SYSTEM
3858 * supported by the FW.
3861 megasas_get_ld_list(struct megasas_instance *instance)
3863 int ret = 0, ld_index = 0, ids = 0;
3864 struct megasas_cmd *cmd;
3865 struct megasas_dcmd_frame *dcmd;
3866 struct MR_LD_LIST *ci;
3867 dma_addr_t ci_h = 0;
3870 cmd = megasas_get_cmd(instance);
3873 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
3877 dcmd = &cmd->frame->dcmd;
3879 ci = pci_alloc_consistent(instance->pdev,
3880 sizeof(struct MR_LD_LIST),
3884 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
3885 megasas_return_cmd(instance, cmd);
3889 memset(ci, 0, sizeof(*ci));
3890 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3892 if (instance->supportmax256vd)
3893 dcmd->mbox.b[0] = 1;
3894 dcmd->cmd = MFI_CMD_DCMD;
3895 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3896 dcmd->sge_count = 1;
3897 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3899 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3900 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
3901 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3902 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
3905 if (instance->ctrl_context && !instance->mask_interrupts)
3906 ret = megasas_issue_blocked_cmd(instance, cmd,
3907 MEGASAS_BLOCKED_CMD_TIMEOUT);
3909 ret = megasas_issue_polled(instance, cmd);
3912 ld_count = le32_to_cpu(ci->ldCount);
3914 /* the following function will get the instance PD LIST */
3916 if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
3917 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
3919 for (ld_index = 0; ld_index < ld_count; ld_index++) {
3920 if (ci->ldList[ld_index].state != 0) {
3921 ids = ci->ldList[ld_index].ref.targetId;
3922 instance->ld_ids[ids] =
3923 ci->ldList[ld_index].ref.targetId;
3928 pci_free_consistent(instance->pdev,
3929 sizeof(struct MR_LD_LIST),
3933 megasas_return_cmd(instance, cmd);
3938 * megasas_ld_list_query - Returns FW's ld_list structure
3939 * @instance: Adapter soft state
3940 * @ld_list: ld_list structure
3942 * Issues an internal command (DCMD) to get the FW's controller PD
3943 * list structure. This information is mainly used to find out SYSTEM
3944 * supported by the FW.
3947 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
3949 int ret = 0, ld_index = 0, ids = 0;
3950 struct megasas_cmd *cmd;
3951 struct megasas_dcmd_frame *dcmd;
3952 struct MR_LD_TARGETID_LIST *ci;
3953 dma_addr_t ci_h = 0;
3956 cmd = megasas_get_cmd(instance);
3959 dev_warn(&instance->pdev->dev,
3960 "megasas_ld_list_query: Failed to get cmd\n");
3964 dcmd = &cmd->frame->dcmd;
3966 ci = pci_alloc_consistent(instance->pdev,
3967 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
3970 dev_warn(&instance->pdev->dev,
3971 "Failed to alloc mem for ld_list_query\n");
3972 megasas_return_cmd(instance, cmd);
3976 memset(ci, 0, sizeof(*ci));
3977 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3979 dcmd->mbox.b[0] = query_type;
3980 if (instance->supportmax256vd)
3981 dcmd->mbox.b[2] = 1;
3983 dcmd->cmd = MFI_CMD_DCMD;
3984 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3985 dcmd->sge_count = 1;
3986 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3988 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
3989 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
3990 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3991 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
3994 if (instance->ctrl_context && !instance->mask_interrupts)
3995 ret = megasas_issue_blocked_cmd(instance, cmd,
3996 MEGASAS_BLOCKED_CMD_TIMEOUT);
3998 ret = megasas_issue_polled(instance, cmd);
4000 tgtid_count = le32_to_cpu(ci->count);
4002 if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
4003 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4004 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4005 ids = ci->targetId[ld_index];
4006 instance->ld_ids[ids] = ci->targetId[ld_index];
4011 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4014 megasas_return_cmd(instance, cmd);
4020 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4021 * instance : Controller's instance
4023 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4025 struct fusion_context *fusion;
4029 fusion = instance->ctrl_context;
4030 /* For MFI based controllers return dummy success */
4034 instance->supportmax256vd =
4035 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4036 /* Below is additional check to address future FW enhancement */
4037 if (instance->ctrl_info->max_lds > 64)
4038 instance->supportmax256vd = 1;
4040 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4041 * MEGASAS_MAX_DEV_PER_CHANNEL;
4042 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4043 * MEGASAS_MAX_DEV_PER_CHANNEL;
4044 if (instance->supportmax256vd) {
4045 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4046 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4048 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4049 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4052 dev_info(&instance->pdev->dev,
4053 "firmware type\t: %s\n",
4054 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4055 "Legacy(64 VD) firmware");
4057 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4058 (sizeof(struct MR_LD_SPAN_MAP) *
4059 (instance->fw_supported_vd_count - 1));
4060 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4061 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
4062 (sizeof(struct MR_LD_SPAN_MAP) *
4063 (instance->drv_supported_vd_count - 1));
4065 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4068 if (instance->supportmax256vd)
4069 fusion->current_map_sz = new_map_sz;
4071 fusion->current_map_sz = old_map_sz;
4075 * megasas_get_controller_info - Returns FW's controller structure
4076 * @instance: Adapter soft state
4078 * Issues an internal command (DCMD) to get the FW's controller structure.
4079 * This information is mainly used to find out the maximum IO transfer per
4080 * command supported by the FW.
4083 megasas_get_ctrl_info(struct megasas_instance *instance)
4086 struct megasas_cmd *cmd;
4087 struct megasas_dcmd_frame *dcmd;
4088 struct megasas_ctrl_info *ci;
4089 struct megasas_ctrl_info *ctrl_info;
4090 dma_addr_t ci_h = 0;
4092 ctrl_info = instance->ctrl_info;
4094 cmd = megasas_get_cmd(instance);
4097 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4101 dcmd = &cmd->frame->dcmd;
4103 ci = pci_alloc_consistent(instance->pdev,
4104 sizeof(struct megasas_ctrl_info), &ci_h);
4107 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
4108 megasas_return_cmd(instance, cmd);
4112 memset(ci, 0, sizeof(*ci));
4113 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4115 dcmd->cmd = MFI_CMD_DCMD;
4116 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4117 dcmd->sge_count = 1;
4118 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4121 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4122 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4123 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4124 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4125 dcmd->mbox.b[0] = 1;
4127 if (instance->ctrl_context && !instance->mask_interrupts)
4128 ret = megasas_issue_blocked_cmd(instance, cmd,
4129 MEGASAS_BLOCKED_CMD_TIMEOUT);
4131 ret = megasas_issue_polled(instance, cmd);
4134 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
4135 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4136 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4137 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4138 megasas_update_ext_vd_details(instance);
4139 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4140 dev_info(&instance->pdev->dev,
4141 "controller type\t: %s(%dMB)\n",
4142 instance->is_imr ? "iMR" : "MR",
4143 le16_to_cpu(ctrl_info->memory_size));
4146 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4149 megasas_return_cmd(instance, cmd);
4154 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4157 * @instance: Adapter soft state
4158 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4159 MR_CRASH_BUF_TURN_OFF = 0
4160 MR_CRASH_BUF_TURN_ON = 1
4161 * @return 0 on success non-zero on failure.
4162 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4163 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4164 * that driver supports crash dump feature. This DCMD will be sent only if
4165 * crash dump feature is supported by the FW.
4168 int megasas_set_crash_dump_params(struct megasas_instance *instance,
4172 struct megasas_cmd *cmd;
4173 struct megasas_dcmd_frame *dcmd;
4175 cmd = megasas_get_cmd(instance);
4178 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4183 dcmd = &cmd->frame->dcmd;
4185 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4186 dcmd->mbox.b[0] = crash_buf_state;
4187 dcmd->cmd = MFI_CMD_DCMD;
4188 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4189 dcmd->sge_count = 1;
4190 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4193 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4194 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4195 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4196 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4198 if (instance->ctrl_context && !instance->mask_interrupts)
4199 ret = megasas_issue_blocked_cmd(instance, cmd,
4200 MEGASAS_BLOCKED_CMD_TIMEOUT);
4202 ret = megasas_issue_polled(instance, cmd);
4204 megasas_return_cmd(instance, cmd);
4209 * megasas_issue_init_mfi - Initializes the FW
4210 * @instance: Adapter soft state
4212 * Issues the INIT MFI cmd
4215 megasas_issue_init_mfi(struct megasas_instance *instance)
4218 struct megasas_cmd *cmd;
4219 struct megasas_init_frame *init_frame;
4220 struct megasas_init_queue_info *initq_info;
4221 dma_addr_t init_frame_h;
4222 dma_addr_t initq_info_h;
4225 * Prepare a init frame. Note the init frame points to queue info
4226 * structure. Each frame has SGL allocated after first 64 bytes. For
4227 * this frame - since we don't need any SGL - we use SGL's space as
4228 * queue info structure
4230 * We will not get a NULL command below. We just created the pool.
4232 cmd = megasas_get_cmd(instance);
4234 init_frame = (struct megasas_init_frame *)cmd->frame;
4235 initq_info = (struct megasas_init_queue_info *)
4236 ((unsigned long)init_frame + 64);
4238 init_frame_h = cmd->frame_phys_addr;
4239 initq_info_h = init_frame_h + 64;
4241 context = init_frame->context;
4242 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4243 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4244 init_frame->context = context;
4246 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4247 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
4249 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4250 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
4252 init_frame->cmd = MFI_CMD_INIT;
4253 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
4254 init_frame->queue_info_new_phys_addr_lo =
4255 cpu_to_le32(lower_32_bits(initq_info_h));
4256 init_frame->queue_info_new_phys_addr_hi =
4257 cpu_to_le32(upper_32_bits(initq_info_h));
4259 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
4262 * disable the intr before firing the init frame to FW
4264 instance->instancet->disable_intr(instance);
4267 * Issue the init frame in polled mode
4270 if (megasas_issue_polled(instance, cmd)) {
4271 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
4272 megasas_return_cmd(instance, cmd);
4276 megasas_return_cmd(instance, cmd);
4285 megasas_init_adapter_mfi(struct megasas_instance *instance)
4287 struct megasas_register_set __iomem *reg_set;
4291 reg_set = instance->reg_set;
4294 * Get various operational parameters from status register
4296 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
4298 * Reduce the max supported cmds by 1. This is to ensure that the
4299 * reply_q_sz (1 more than the max cmd that driver may send)
4300 * does not exceed max cmds that the FW can support
4302 instance->max_fw_cmds = instance->max_fw_cmds-1;
4303 instance->max_mfi_cmds = instance->max_fw_cmds;
4304 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
4307 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4308 * are reserved for IOCTL + driver's internal DCMDs.
4310 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4311 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4312 instance->max_scsi_cmds = (instance->max_fw_cmds -
4313 MEGASAS_SKINNY_INT_CMDS);
4314 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4316 instance->max_scsi_cmds = (instance->max_fw_cmds -
4318 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4322 * Create a pool of commands
4324 if (megasas_alloc_cmds(instance))
4325 goto fail_alloc_cmds;
4328 * Allocate memory for reply queue. Length of reply queue should
4329 * be _one_ more than the maximum commands handled by the firmware.
4331 * Note: When FW completes commands, it places corresponding contex
4332 * values in this circular reply queue. This circular queue is a fairly
4333 * typical producer-consumer queue. FW is the producer (of completed
4334 * commands) and the driver is the consumer.
4336 context_sz = sizeof(u32);
4337 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4339 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4341 &instance->reply_queue_h);
4343 if (!instance->reply_queue) {
4344 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
4345 goto fail_reply_queue;
4348 if (megasas_issue_init_mfi(instance))
4351 if (megasas_get_ctrl_info(instance)) {
4352 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4353 "Fail from %s %d\n", instance->unique_id,
4354 __func__, __LINE__);
4358 instance->fw_support_ieee = 0;
4359 instance->fw_support_ieee =
4360 (instance->instancet->read_fw_status_reg(reg_set) &
4363 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
4364 instance->fw_support_ieee);
4366 if (instance->fw_support_ieee)
4367 instance->flag_ieee = 1;
4373 pci_free_consistent(instance->pdev, reply_q_sz,
4374 instance->reply_queue, instance->reply_queue_h);
4376 megasas_free_cmds(instance);
4383 * megasas_setup_irqs_msix - register legacy interrupts.
4384 * @instance: Adapter soft state
4386 * Do not enable interrupt, only setup ISRs.
4388 * Return 0 on success.
4391 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4393 struct pci_dev *pdev;
4395 pdev = instance->pdev;
4396 instance->irq_context[0].instance = instance;
4397 instance->irq_context[0].MSIxIndex = 0;
4398 if (request_irq(pdev->irq, instance->instancet->service_isr,
4399 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4400 dev_err(&instance->pdev->dev,
4401 "Failed to register IRQ from %s %d\n",
4402 __func__, __LINE__);
4409 * megasas_setup_irqs_msix - register MSI-x interrupts.
4410 * @instance: Adapter soft state
4411 * @is_probe: Driver probe check
4413 * Do not enable interrupt, only setup ISRs.
4415 * Return 0 on success.
4418 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4421 struct pci_dev *pdev;
4423 pdev = instance->pdev;
4426 cpu = cpumask_first(cpu_online_mask);
4427 for (i = 0; i < instance->msix_vectors; i++) {
4428 instance->irq_context[i].instance = instance;
4429 instance->irq_context[i].MSIxIndex = i;
4430 if (request_irq(instance->msixentry[i].vector,
4431 instance->instancet->service_isr, 0, "megasas",
4432 &instance->irq_context[i])) {
4433 dev_err(&instance->pdev->dev,
4434 "Failed to register IRQ for vector %d.\n", i);
4435 for (j = 0; j < i; j++) {
4436 if (smp_affinity_enable)
4437 irq_set_affinity_hint(
4438 instance->msixentry[j].vector, NULL);
4439 free_irq(instance->msixentry[j].vector,
4440 &instance->irq_context[j]);
4442 /* Retry irq register for IO_APIC*/
4443 instance->msix_vectors = 0;
4445 return megasas_setup_irqs_ioapic(instance);
4449 if (smp_affinity_enable) {
4450 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4452 dev_err(&instance->pdev->dev,
4453 "Failed to set affinity hint"
4454 " for cpu %d\n", cpu);
4455 cpu = cpumask_next(cpu, cpu_online_mask);
4462 * megasas_destroy_irqs- unregister interrupts.
4463 * @instance: Adapter soft state
4467 megasas_destroy_irqs(struct megasas_instance *instance) {
4471 if (instance->msix_vectors)
4472 for (i = 0; i < instance->msix_vectors; i++) {
4473 if (smp_affinity_enable)
4474 irq_set_affinity_hint(
4475 instance->msixentry[i].vector, NULL);
4476 free_irq(instance->msixentry[i].vector,
4477 &instance->irq_context[i]);
4480 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4484 * megasas_init_fw - Initializes the FW
4485 * @instance: Adapter soft state
4487 * This is the main function for initializing firmware
4490 static int megasas_init_fw(struct megasas_instance *instance)
4494 u32 tmp_sectors, msix_enable, scratch_pad_2;
4495 resource_size_t base_addr;
4496 struct megasas_register_set __iomem *reg_set;
4497 struct megasas_ctrl_info *ctrl_info = NULL;
4498 unsigned long bar_list;
4499 int i, loop, fw_msix_count = 0;
4500 struct IOV_111 *iovPtr;
4502 /* Find first memory bar */
4503 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4504 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
4505 if (pci_request_selected_regions(instance->pdev, instance->bar,
4507 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
4511 base_addr = pci_resource_start(instance->pdev, instance->bar);
4512 instance->reg_set = ioremap_nocache(base_addr, 8192);
4514 if (!instance->reg_set) {
4515 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
4519 reg_set = instance->reg_set;
4521 switch (instance->pdev->device) {
4522 case PCI_DEVICE_ID_LSI_FUSION:
4523 case PCI_DEVICE_ID_LSI_PLASMA:
4524 case PCI_DEVICE_ID_LSI_INVADER:
4525 case PCI_DEVICE_ID_LSI_FURY:
4526 instance->instancet = &megasas_instance_template_fusion;
4528 case PCI_DEVICE_ID_LSI_SAS1078R:
4529 case PCI_DEVICE_ID_LSI_SAS1078DE:
4530 instance->instancet = &megasas_instance_template_ppc;
4532 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4533 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4534 instance->instancet = &megasas_instance_template_gen2;
4536 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4537 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4538 instance->instancet = &megasas_instance_template_skinny;
4540 case PCI_DEVICE_ID_LSI_SAS1064R:
4541 case PCI_DEVICE_ID_DELL_PERC5:
4543 instance->instancet = &megasas_instance_template_xscale;
4547 if (megasas_transition_to_ready(instance, 0)) {
4548 atomic_set(&instance->fw_reset_no_pci_access, 1);
4549 instance->instancet->adp_reset
4550 (instance, instance->reg_set);
4551 atomic_set(&instance->fw_reset_no_pci_access, 0);
4552 dev_info(&instance->pdev->dev,
4553 "FW restarted successfully from %s!\n",
4556 /*waitting for about 30 second before retry*/
4559 if (megasas_transition_to_ready(instance, 0))
4560 goto fail_ready_state;
4564 * MSI-X host index 0 is common for all adapter.
4565 * It is used for all MPT based Adapters.
4567 instance->reply_post_host_index_addr[0] =
4568 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
4569 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4571 /* Check if MSI-X is supported while in ready state */
4572 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4574 if (msix_enable && !msix_disable) {
4575 scratch_pad_2 = readl
4576 (&instance->reg_set->outbound_scratch_pad_2);
4577 /* Check max MSI-X vectors */
4578 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4579 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA)) {
4580 instance->msix_vectors = (scratch_pad_2
4581 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4582 fw_msix_count = instance->msix_vectors;
4584 instance->msix_vectors =
4586 instance->msix_vectors);
4587 } else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
4588 || (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
4589 /* Invader/Fury supports more than 8 MSI-X */
4590 instance->msix_vectors = ((scratch_pad_2
4591 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4592 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4593 fw_msix_count = instance->msix_vectors;
4594 /* Save 1-15 reply post index address to local memory
4595 * Index 0 is already saved from reg offset
4596 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4598 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4599 instance->reply_post_host_index_addr[loop] =
4601 ((u8 __iomem *)instance->reg_set +
4602 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4606 instance->msix_vectors = min(msix_vectors,
4607 instance->msix_vectors);
4609 instance->msix_vectors = 1;
4610 /* Don't bother allocating more MSI-X vectors than cpus */
4611 instance->msix_vectors = min(instance->msix_vectors,
4612 (unsigned int)num_online_cpus());
4613 for (i = 0; i < instance->msix_vectors; i++)
4614 instance->msixentry[i].entry = i;
4615 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4616 1, instance->msix_vectors);
4618 instance->msix_vectors = i;
4620 instance->msix_vectors = 0;
4623 dev_info(&instance->pdev->dev,
4624 "firmware supports msix\t: (%d)", fw_msix_count);
4625 dev_info(&instance->pdev->dev,
4626 "current msix/online cpus\t: (%d/%d)\n",
4627 instance->msix_vectors, (unsigned int)num_online_cpus());
4629 if (instance->msix_vectors ?
4630 megasas_setup_irqs_msix(instance, 1) :
4631 megasas_setup_irqs_ioapic(instance))
4632 goto fail_setup_irqs;
4634 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4636 if (instance->ctrl_info == NULL)
4637 goto fail_init_adapter;
4640 * Below are default value for legacy Firmware.
4641 * non-fusion based controllers
4643 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4644 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4645 /* Get operational params, sge flags, send init cmd to controller */
4646 if (instance->instancet->init_adapter(instance))
4647 goto fail_init_adapter;
4649 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4650 (unsigned long)instance);
4652 instance->instancet->enable_intr(instance);
4654 dev_err(&instance->pdev->dev, "INIT adapter done\n");
4657 * the following function will get the PD LIST.
4659 memset(instance->pd_list, 0,
4660 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
4661 if (megasas_get_pd_list(instance) < 0) {
4662 dev_err(&instance->pdev->dev, "failed to get PD list\n");
4663 goto fail_get_pd_list;
4666 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4667 if (megasas_ld_list_query(instance,
4668 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4669 megasas_get_ld_list(instance);
4672 * Compute the max allowed sectors per IO: The controller info has two
4673 * limits on max sectors. Driver should use the minimum of these two.
4675 * 1 << stripe_sz_ops.min = max sectors per strip
4677 * Note that older firmwares ( < FW ver 30) didn't report information
4678 * to calculate max_sectors_1. So the number ended up as zero always.
4681 ctrl_info = instance->ctrl_info;
4683 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4684 le16_to_cpu(ctrl_info->max_strips_per_io);
4685 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
4687 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
4689 instance->disableOnlineCtrlReset =
4690 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4691 instance->mpio = ctrl_info->adapterOperations2.mpio;
4692 instance->UnevenSpanSupport =
4693 ctrl_info->adapterOperations2.supportUnevenSpans;
4694 if (instance->UnevenSpanSupport) {
4695 struct fusion_context *fusion = instance->ctrl_context;
4696 if (MR_ValidateMapInfo(instance))
4697 fusion->fast_path_io = 1;
4699 fusion->fast_path_io = 0;
4702 if (ctrl_info->host_interface.SRIOV) {
4703 if (!ctrl_info->adapterOperations2.activePassive)
4704 instance->PlasmaFW111 = 1;
4706 if (!instance->PlasmaFW111)
4707 instance->requestorId =
4708 ctrl_info->iov.requestorId;
4710 iovPtr = (struct IOV_111 *)((unsigned char *)ctrl_info + IOV_111_OFFSET);
4711 instance->requestorId = iovPtr->requestorId;
4713 dev_warn(&instance->pdev->dev, "I am VF "
4714 "requestorId %d\n", instance->requestorId);
4717 instance->crash_dump_fw_support =
4718 ctrl_info->adapterOperations3.supportCrashDump;
4719 instance->crash_dump_drv_support =
4720 (instance->crash_dump_fw_support &&
4721 instance->crash_dump_buf);
4722 if (instance->crash_dump_drv_support)
4723 megasas_set_crash_dump_params(instance,
4724 MR_CRASH_BUF_TURN_OFF);
4727 if (instance->crash_dump_buf)
4728 pci_free_consistent(instance->pdev,
4730 instance->crash_dump_buf,
4731 instance->crash_dump_h);
4732 instance->crash_dump_buf = NULL;
4735 instance->secure_jbod_support =
4736 ctrl_info->adapterOperations3.supportSecurityonJBOD;
4738 dev_info(&instance->pdev->dev,
4739 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
4740 le16_to_cpu(ctrl_info->pci.vendor_id),
4741 le16_to_cpu(ctrl_info->pci.device_id),
4742 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
4743 le16_to_cpu(ctrl_info->pci.sub_device_id));
4744 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
4745 instance->UnevenSpanSupport ? "yes" : "no");
4746 dev_info(&instance->pdev->dev, "disable ocr : %s\n",
4747 instance->disableOnlineCtrlReset ? "yes" : "no");
4748 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
4749 instance->crash_dump_drv_support ? "yes" : "no");
4750 dev_info(&instance->pdev->dev, "secure jbod : %s\n",
4751 instance->secure_jbod_support ? "yes" : "no");
4754 instance->max_sectors_per_req = instance->max_num_sge *
4756 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4757 instance->max_sectors_per_req = tmp_sectors;
4759 /* Check for valid throttlequeuedepth module parameter */
4760 if (throttlequeuedepth &&
4761 throttlequeuedepth <= instance->max_scsi_cmds)
4762 instance->throttlequeuedepth = throttlequeuedepth;
4764 instance->throttlequeuedepth =
4765 MEGASAS_THROTTLE_QUEUE_DEPTH;
4768 /* Launch SR-IOV heartbeat timer */
4769 if (instance->requestorId) {
4770 if (!megasas_sriov_start_heartbeat(instance, 1))
4771 megasas_start_timer(instance,
4772 &instance->sriov_heartbeat_timer,
4773 megasas_sriov_heartbeat_handler,
4774 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4776 instance->skip_heartbeat_timer_del = 1;
4782 instance->instancet->disable_intr(instance);
4784 megasas_destroy_irqs(instance);
4786 if (instance->msix_vectors)
4787 pci_disable_msix(instance->pdev);
4788 instance->msix_vectors = 0;
4790 kfree(instance->ctrl_info);
4791 instance->ctrl_info = NULL;
4792 iounmap(instance->reg_set);
4795 pci_release_selected_regions(instance->pdev, instance->bar);
4801 * megasas_release_mfi - Reverses the FW initialization
4802 * @instance: Adapter soft state
4804 static void megasas_release_mfi(struct megasas_instance *instance)
4806 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
4808 if (instance->reply_queue)
4809 pci_free_consistent(instance->pdev, reply_q_sz,
4810 instance->reply_queue, instance->reply_queue_h);
4812 megasas_free_cmds(instance);
4814 iounmap(instance->reg_set);
4816 pci_release_selected_regions(instance->pdev, instance->bar);
4820 * megasas_get_seq_num - Gets latest event sequence numbers
4821 * @instance: Adapter soft state
4822 * @eli: FW event log sequence numbers information
4824 * FW maintains a log of all events in a non-volatile area. Upper layers would
4825 * usually find out the latest sequence number of the events, the seq number at
4826 * the boot etc. They would "read" all the events below the latest seq number
4827 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4828 * number), they would subsribe to AEN (asynchronous event notification) and
4829 * wait for the events to happen.
4832 megasas_get_seq_num(struct megasas_instance *instance,
4833 struct megasas_evt_log_info *eli)
4835 struct megasas_cmd *cmd;
4836 struct megasas_dcmd_frame *dcmd;
4837 struct megasas_evt_log_info *el_info;
4838 dma_addr_t el_info_h = 0;
4840 cmd = megasas_get_cmd(instance);
4846 dcmd = &cmd->frame->dcmd;
4847 el_info = pci_alloc_consistent(instance->pdev,
4848 sizeof(struct megasas_evt_log_info),
4852 megasas_return_cmd(instance, cmd);
4856 memset(el_info, 0, sizeof(*el_info));
4857 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4859 dcmd->cmd = MFI_CMD_DCMD;
4860 dcmd->cmd_status = 0x0;
4861 dcmd->sge_count = 1;
4862 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4865 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4866 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
4867 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
4868 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4870 if (megasas_issue_blocked_cmd(instance, cmd, 30))
4871 dev_err(&instance->pdev->dev, "Command timedout"
4872 "from %s\n", __func__);
4875 * Copy the data back into callers buffer
4877 eli->newest_seq_num = el_info->newest_seq_num;
4878 eli->oldest_seq_num = el_info->oldest_seq_num;
4879 eli->clear_seq_num = el_info->clear_seq_num;
4880 eli->shutdown_seq_num = el_info->shutdown_seq_num;
4881 eli->boot_seq_num = el_info->boot_seq_num;
4884 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
4885 el_info, el_info_h);
4887 megasas_return_cmd(instance, cmd);
4893 * megasas_register_aen - Registers for asynchronous event notification
4894 * @instance: Adapter soft state
4895 * @seq_num: The starting sequence number
4896 * @class_locale: Class of the event
4898 * This function subscribes for AEN for events beyond the @seq_num. It requests
4899 * to be notified if and only if the event is of type @class_locale
4902 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
4903 u32 class_locale_word)
4906 struct megasas_cmd *cmd;
4907 struct megasas_dcmd_frame *dcmd;
4908 union megasas_evt_class_locale curr_aen;
4909 union megasas_evt_class_locale prev_aen;
4912 * If there an AEN pending already (aen_cmd), check if the
4913 * class_locale of that pending AEN is inclusive of the new
4914 * AEN request we currently have. If it is, then we don't have
4915 * to do anything. In other words, whichever events the current
4916 * AEN request is subscribing to, have already been subscribed
4919 * If the old_cmd is _not_ inclusive, then we have to abort
4920 * that command, form a class_locale that is superset of both
4921 * old and current and re-issue to the FW
4924 curr_aen.word = class_locale_word;
4926 if (instance->aen_cmd) {
4929 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
4932 * A class whose enum value is smaller is inclusive of all
4933 * higher values. If a PROGRESS (= -1) was previously
4934 * registered, then a new registration requests for higher
4935 * classes need not be sent to FW. They are automatically
4938 * Locale numbers don't have such hierarchy. They are bitmap
4941 if ((prev_aen.members.class <= curr_aen.members.class) &&
4942 !((prev_aen.members.locale & curr_aen.members.locale) ^
4943 curr_aen.members.locale)) {
4945 * Previously issued event registration includes
4946 * current request. Nothing to do.
4950 curr_aen.members.locale |= prev_aen.members.locale;
4952 if (prev_aen.members.class < curr_aen.members.class)
4953 curr_aen.members.class = prev_aen.members.class;
4955 instance->aen_cmd->abort_aen = 1;
4956 ret_val = megasas_issue_blocked_abort_cmd(instance,
4961 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
4962 "previous AEN command\n");
4968 cmd = megasas_get_cmd(instance);
4973 dcmd = &cmd->frame->dcmd;
4975 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
4978 * Prepare DCMD for aen registration
4980 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4982 dcmd->cmd = MFI_CMD_DCMD;
4983 dcmd->cmd_status = 0x0;
4984 dcmd->sge_count = 1;
4985 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4988 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
4989 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
4990 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
4991 instance->last_seq_num = seq_num;
4992 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
4993 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
4994 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
4996 if (instance->aen_cmd != NULL) {
4997 megasas_return_cmd(instance, cmd);
5002 * Store reference to the cmd used to register for AEN. When an
5003 * application wants us to register for AEN, we have to abort this
5004 * cmd and re-register with a new EVENT LOCALE supplied by that app
5006 instance->aen_cmd = cmd;
5009 * Issue the aen registration frame
5011 instance->instancet->issue_dcmd(instance, cmd);
5017 * megasas_start_aen - Subscribes to AEN during driver load time
5018 * @instance: Adapter soft state
5020 static int megasas_start_aen(struct megasas_instance *instance)
5022 struct megasas_evt_log_info eli;
5023 union megasas_evt_class_locale class_locale;
5026 * Get the latest sequence number from FW
5028 memset(&eli, 0, sizeof(eli));
5030 if (megasas_get_seq_num(instance, &eli))
5034 * Register AEN with FW for latest sequence number plus 1
5036 class_locale.members.reserved = 0;
5037 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5038 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5040 return megasas_register_aen(instance,
5041 le32_to_cpu(eli.newest_seq_num) + 1,
5046 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5047 * @instance: Adapter soft state
5049 static int megasas_io_attach(struct megasas_instance *instance)
5051 struct Scsi_Host *host = instance->host;
5055 * Export parameters required by SCSI mid-layer
5057 host->irq = instance->pdev->irq;
5058 host->unique_id = instance->unique_id;
5059 host->can_queue = instance->max_scsi_cmds;
5060 host->this_id = instance->init_id;
5061 host->sg_tablesize = instance->max_num_sge;
5063 if (instance->fw_support_ieee)
5064 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5067 * Check if the module parameter value for max_sectors can be used
5069 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5070 instance->max_sectors_per_req = max_sectors;
5073 if (((instance->pdev->device ==
5074 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5075 (instance->pdev->device ==
5076 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5077 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5078 instance->max_sectors_per_req = max_sectors;
5080 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
5081 "and <= %d (or < 1MB for GEN2 controller)\n",
5082 instance->max_sectors_per_req);
5087 host->max_sectors = instance->max_sectors_per_req;
5088 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
5089 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5090 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5091 host->max_lun = MEGASAS_MAX_LUN;
5092 host->max_cmd_len = 16;
5094 /* Fusion only supports host reset */
5095 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5096 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
5097 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5098 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
5099 host->hostt->eh_device_reset_handler = NULL;
5100 host->hostt->eh_bus_reset_handler = NULL;
5102 error = scsi_init_shared_tag_map(host, host->can_queue);
5104 dev_err(&instance->pdev->dev,
5105 "Failed to shared tag from %s %d\n",
5106 __func__, __LINE__);
5111 * Notify the mid-layer about the new controller
5113 if (scsi_add_host(host, &instance->pdev->dev)) {
5114 dev_err(&instance->pdev->dev,
5115 "Failed to add host from %s %d\n",
5116 __func__, __LINE__);
5124 megasas_set_dma_mask(struct pci_dev *pdev)
5127 * All our controllers are capable of performing 64-bit DMA
5130 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
5132 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5133 goto fail_set_dma_mask;
5136 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5137 goto fail_set_dma_mask;
5140 * Ensure that all data structures are allocated in 32-bit
5143 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5144 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5145 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5146 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5147 dev_info(&pdev->dev, "set 32bit DMA mask"
5148 "and 32 bit consistent mask\n");
5150 goto fail_set_dma_mask;
5160 * megasas_probe_one - PCI hotplug entry point
5161 * @pdev: PCI device structure
5162 * @id: PCI ids of supported hotplugged adapter
5164 static int megasas_probe_one(struct pci_dev *pdev,
5165 const struct pci_device_id *id)
5168 struct Scsi_Host *host;
5169 struct megasas_instance *instance;
5171 struct fusion_context *fusion = NULL;
5173 /* Reset MSI-X in the kdump kernel */
5174 if (reset_devices) {
5175 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5177 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
5179 if (control & PCI_MSIX_FLAGS_ENABLE) {
5180 dev_info(&pdev->dev, "resetting MSI-X\n");
5181 pci_write_config_word(pdev,
5182 pos + PCI_MSIX_FLAGS,
5184 ~PCI_MSIX_FLAGS_ENABLE);
5190 * PCI prepping: enable device set bus mastering and dma mask
5192 rval = pci_enable_device_mem(pdev);
5198 pci_set_master(pdev);
5200 if (megasas_set_dma_mask(pdev))
5201 goto fail_set_dma_mask;
5203 host = scsi_host_alloc(&megasas_template,
5204 sizeof(struct megasas_instance));
5207 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
5208 goto fail_alloc_instance;
5211 instance = (struct megasas_instance *)host->hostdata;
5212 memset(instance, 0, sizeof(*instance));
5213 atomic_set(&instance->fw_reset_no_pci_access, 0);
5214 instance->pdev = pdev;
5216 switch (instance->pdev->device) {
5217 case PCI_DEVICE_ID_LSI_FUSION:
5218 case PCI_DEVICE_ID_LSI_PLASMA:
5219 case PCI_DEVICE_ID_LSI_INVADER:
5220 case PCI_DEVICE_ID_LSI_FURY:
5222 instance->ctrl_context_pages =
5223 get_order(sizeof(struct fusion_context));
5224 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5225 instance->ctrl_context_pages);
5226 if (!instance->ctrl_context) {
5227 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
5228 "memory for Fusion context info\n");
5229 goto fail_alloc_dma_buf;
5231 fusion = instance->ctrl_context;
5233 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
5236 default: /* For all other supported controllers */
5238 instance->producer =
5239 pci_alloc_consistent(pdev, sizeof(u32),
5240 &instance->producer_h);
5241 instance->consumer =
5242 pci_alloc_consistent(pdev, sizeof(u32),
5243 &instance->consumer_h);
5245 if (!instance->producer || !instance->consumer) {
5246 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate"
5247 "memory for producer, consumer\n");
5248 goto fail_alloc_dma_buf;
5251 *instance->producer = 0;
5252 *instance->consumer = 0;
5256 instance->system_info_buf = pci_zalloc_consistent(pdev,
5257 sizeof(struct MR_DRV_SYSTEM_INFO),
5258 &instance->system_info_h);
5260 if (!instance->system_info_buf)
5261 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5263 /* Crash dump feature related initialisation*/
5264 instance->drv_buf_index = 0;
5265 instance->drv_buf_alloc = 0;
5266 instance->crash_dump_fw_support = 0;
5267 instance->crash_dump_app_support = 0;
5268 instance->fw_crash_state = UNAVAILABLE;
5269 spin_lock_init(&instance->crashdump_lock);
5270 instance->crash_dump_buf = NULL;
5273 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5275 &instance->crash_dump_h);
5276 if (!instance->crash_dump_buf)
5277 dev_err(&pdev->dev, "Can't allocate Firmware "
5278 "crash dump DMA buffer\n");
5280 megasas_poll_wait_aen = 0;
5281 instance->flag_ieee = 0;
5282 instance->ev = NULL;
5283 instance->issuepend_done = 1;
5284 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
5285 instance->is_imr = 0;
5287 instance->evt_detail = pci_alloc_consistent(pdev,
5289 megasas_evt_detail),
5290 &instance->evt_detail_h);
5292 if (!instance->evt_detail) {
5293 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
5294 "event detail structure\n");
5295 goto fail_alloc_dma_buf;
5299 * Initialize locks and queues
5301 INIT_LIST_HEAD(&instance->cmd_pool);
5302 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
5304 atomic_set(&instance->fw_outstanding,0);
5306 init_waitqueue_head(&instance->int_cmd_wait_q);
5307 init_waitqueue_head(&instance->abort_cmd_wait_q);
5309 spin_lock_init(&instance->mfi_pool_lock);
5310 spin_lock_init(&instance->hba_lock);
5311 spin_lock_init(&instance->completion_lock);
5313 mutex_init(&instance->aen_mutex);
5314 mutex_init(&instance->reset_mutex);
5317 * Initialize PCI related and misc parameters
5319 instance->host = host;
5320 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5321 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
5322 instance->ctrl_info = NULL;
5325 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5326 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
5327 instance->flag_ieee = 1;
5329 megasas_dbg_lvl = 0;
5331 instance->unload = 1;
5332 instance->last_time = 0;
5333 instance->disableOnlineCtrlReset = 1;
5334 instance->UnevenSpanSupport = 0;
5336 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5337 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
5338 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5339 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
5340 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
5341 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5343 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
5346 * Initialize MFI Firmware
5348 if (megasas_init_fw(instance))
5351 if (instance->requestorId) {
5352 if (instance->PlasmaFW111) {
5353 instance->vf_affiliation_111 =
5354 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5355 &instance->vf_affiliation_111_h);
5356 if (!instance->vf_affiliation_111)
5357 dev_warn(&pdev->dev, "Can't allocate "
5358 "memory for VF affiliation buffer\n");
5360 instance->vf_affiliation =
5361 pci_alloc_consistent(pdev,
5362 (MAX_LOGICAL_DRIVES + 1) *
5363 sizeof(struct MR_LD_VF_AFFILIATION),
5364 &instance->vf_affiliation_h);
5365 if (!instance->vf_affiliation)
5366 dev_warn(&pdev->dev, "Can't allocate "
5367 "memory for VF affiliation buffer\n");
5372 * Store instance in PCI softstate
5374 pci_set_drvdata(pdev, instance);
5377 * Add this controller to megasas_mgmt_info structure so that it
5378 * can be exported to management applications
5380 megasas_mgmt_info.count++;
5381 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5382 megasas_mgmt_info.max_index++;
5385 * Register with SCSI mid-layer
5387 if (megasas_io_attach(instance))
5388 goto fail_io_attach;
5390 instance->unload = 0;
5392 * Trigger SCSI to scan our drives
5394 scsi_scan_host(host);
5397 * Initiate AEN (Asynchronous Event Notification)
5399 if (megasas_start_aen(instance)) {
5400 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
5401 goto fail_start_aen;
5404 /* Get current SR-IOV LD/VF affiliation */
5405 if (instance->requestorId)
5406 megasas_get_ld_vf_affiliation(instance, 1);
5412 megasas_mgmt_info.count--;
5413 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5414 megasas_mgmt_info.max_index--;
5416 instance->instancet->disable_intr(instance);
5417 megasas_destroy_irqs(instance);
5419 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5420 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
5421 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5422 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
5423 megasas_release_fusion(instance);
5425 megasas_release_mfi(instance);
5426 if (instance->msix_vectors)
5427 pci_disable_msix(instance->pdev);
5430 if (instance->evt_detail)
5431 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5432 instance->evt_detail,
5433 instance->evt_detail_h);
5435 if (instance->producer)
5436 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5437 instance->producer_h);
5438 if (instance->consumer)
5439 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5440 instance->consumer_h);
5441 scsi_host_put(host);
5443 fail_alloc_instance:
5445 pci_disable_device(pdev);
5451 * megasas_flush_cache - Requests FW to flush all its caches
5452 * @instance: Adapter soft state
5454 static void megasas_flush_cache(struct megasas_instance *instance)
5456 struct megasas_cmd *cmd;
5457 struct megasas_dcmd_frame *dcmd;
5459 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5462 cmd = megasas_get_cmd(instance);
5467 dcmd = &cmd->frame->dcmd;
5469 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5471 dcmd->cmd = MFI_CMD_DCMD;
5472 dcmd->cmd_status = 0x0;
5473 dcmd->sge_count = 0;
5474 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5477 dcmd->data_xfer_len = 0;
5478 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
5479 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5481 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5482 dev_err(&instance->pdev->dev, "Command timedout"
5483 " from %s\n", __func__);
5485 megasas_return_cmd(instance, cmd);
5489 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5490 * @instance: Adapter soft state
5491 * @opcode: Shutdown/Hibernate
5493 static void megasas_shutdown_controller(struct megasas_instance *instance,
5496 struct megasas_cmd *cmd;
5497 struct megasas_dcmd_frame *dcmd;
5499 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5502 cmd = megasas_get_cmd(instance);
5507 if (instance->aen_cmd)
5508 megasas_issue_blocked_abort_cmd(instance,
5509 instance->aen_cmd, 30);
5510 if (instance->map_update_cmd)
5511 megasas_issue_blocked_abort_cmd(instance,
5512 instance->map_update_cmd, 30);
5513 dcmd = &cmd->frame->dcmd;
5515 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5517 dcmd->cmd = MFI_CMD_DCMD;
5518 dcmd->cmd_status = 0x0;
5519 dcmd->sge_count = 0;
5520 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5523 dcmd->data_xfer_len = 0;
5524 dcmd->opcode = cpu_to_le32(opcode);
5526 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5527 dev_err(&instance->pdev->dev, "Command timedout"
5528 "from %s\n", __func__);
5530 megasas_return_cmd(instance, cmd);
5535 * megasas_suspend - driver suspend entry point
5536 * @pdev: PCI device structure
5537 * @state: PCI power state to suspend routine
5540 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5542 struct Scsi_Host *host;
5543 struct megasas_instance *instance;
5545 instance = pci_get_drvdata(pdev);
5546 host = instance->host;
5547 instance->unload = 1;
5549 /* Shutdown SR-IOV heartbeat timer */
5550 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5551 del_timer_sync(&instance->sriov_heartbeat_timer);
5553 megasas_flush_cache(instance);
5554 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
5556 /* cancel the delayed work if this work still in queue */
5557 if (instance->ev != NULL) {
5558 struct megasas_aen_event *ev = instance->ev;
5559 cancel_delayed_work_sync(&ev->hotplug_work);
5560 instance->ev = NULL;
5563 tasklet_kill(&instance->isr_tasklet);
5565 pci_set_drvdata(instance->pdev, instance);
5566 instance->instancet->disable_intr(instance);
5568 megasas_destroy_irqs(instance);
5570 if (instance->msix_vectors)
5571 pci_disable_msix(instance->pdev);
5573 pci_save_state(pdev);
5574 pci_disable_device(pdev);
5576 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5582 * megasas_resume- driver resume entry point
5583 * @pdev: PCI device structure
5586 megasas_resume(struct pci_dev *pdev)
5589 struct Scsi_Host *host;
5590 struct megasas_instance *instance;
5592 instance = pci_get_drvdata(pdev);
5593 host = instance->host;
5594 pci_set_power_state(pdev, PCI_D0);
5595 pci_enable_wake(pdev, PCI_D0, 0);
5596 pci_restore_state(pdev);
5599 * PCI prepping: enable device set bus mastering and dma mask
5601 rval = pci_enable_device_mem(pdev);
5604 dev_err(&pdev->dev, "Enable device failed\n");
5608 pci_set_master(pdev);
5610 if (megasas_set_dma_mask(pdev))
5611 goto fail_set_dma_mask;
5614 * Initialize MFI Firmware
5617 atomic_set(&instance->fw_outstanding, 0);
5620 * We expect the FW state to be READY
5622 if (megasas_transition_to_ready(instance, 0))
5623 goto fail_ready_state;
5625 /* Now re-enable MSI-X */
5626 if (instance->msix_vectors &&
5627 pci_enable_msix_exact(instance->pdev, instance->msixentry,
5628 instance->msix_vectors))
5629 goto fail_reenable_msix;
5631 switch (instance->pdev->device) {
5632 case PCI_DEVICE_ID_LSI_FUSION:
5633 case PCI_DEVICE_ID_LSI_PLASMA:
5634 case PCI_DEVICE_ID_LSI_INVADER:
5635 case PCI_DEVICE_ID_LSI_FURY:
5637 megasas_reset_reply_desc(instance);
5638 if (megasas_ioc_init_fusion(instance)) {
5639 megasas_free_cmds(instance);
5640 megasas_free_cmds_fusion(instance);
5643 if (!megasas_get_map_info(instance))
5644 megasas_sync_map_info(instance);
5648 *instance->producer = 0;
5649 *instance->consumer = 0;
5650 if (megasas_issue_init_mfi(instance))
5655 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5656 (unsigned long)instance);
5658 if (instance->msix_vectors ?
5659 megasas_setup_irqs_msix(instance, 0) :
5660 megasas_setup_irqs_ioapic(instance))
5663 /* Re-launch SR-IOV heartbeat timer */
5664 if (instance->requestorId) {
5665 if (!megasas_sriov_start_heartbeat(instance, 0))
5666 megasas_start_timer(instance,
5667 &instance->sriov_heartbeat_timer,
5668 megasas_sriov_heartbeat_handler,
5669 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5671 instance->skip_heartbeat_timer_del = 1;
5676 instance->instancet->enable_intr(instance);
5677 instance->unload = 0;
5680 * Initiate AEN (Asynchronous Event Notification)
5682 if (megasas_start_aen(instance))
5683 dev_err(&instance->pdev->dev, "Start AEN failed\n");
5688 if (instance->evt_detail)
5689 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5690 instance->evt_detail,
5691 instance->evt_detail_h);
5693 if (instance->producer)
5694 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5695 instance->producer_h);
5696 if (instance->consumer)
5697 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5698 instance->consumer_h);
5699 scsi_host_put(host);
5705 pci_disable_device(pdev);
5710 #define megasas_suspend NULL
5711 #define megasas_resume NULL
5715 * megasas_detach_one - PCI hot"un"plug entry point
5716 * @pdev: PCI device structure
5718 static void megasas_detach_one(struct pci_dev *pdev)
5721 struct Scsi_Host *host;
5722 struct megasas_instance *instance;
5723 struct fusion_context *fusion;
5725 instance = pci_get_drvdata(pdev);
5726 instance->unload = 1;
5727 host = instance->host;
5728 fusion = instance->ctrl_context;
5730 /* Shutdown SR-IOV heartbeat timer */
5731 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5732 del_timer_sync(&instance->sriov_heartbeat_timer);
5734 if (instance->fw_crash_state != UNAVAILABLE)
5735 megasas_free_host_crash_buffer(instance);
5736 scsi_remove_host(instance->host);
5737 megasas_flush_cache(instance);
5738 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5740 /* cancel the delayed work if this work still in queue*/
5741 if (instance->ev != NULL) {
5742 struct megasas_aen_event *ev = instance->ev;
5743 cancel_delayed_work_sync(&ev->hotplug_work);
5744 instance->ev = NULL;
5747 /* cancel all wait events */
5748 wake_up_all(&instance->int_cmd_wait_q);
5750 tasklet_kill(&instance->isr_tasklet);
5753 * Take the instance off the instance array. Note that we will not
5754 * decrement the max_index. We let this array be sparse array
5756 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5757 if (megasas_mgmt_info.instance[i] == instance) {
5758 megasas_mgmt_info.count--;
5759 megasas_mgmt_info.instance[i] = NULL;
5765 instance->instancet->disable_intr(instance);
5767 megasas_destroy_irqs(instance);
5769 if (instance->msix_vectors)
5770 pci_disable_msix(instance->pdev);
5772 switch (instance->pdev->device) {
5773 case PCI_DEVICE_ID_LSI_FUSION:
5774 case PCI_DEVICE_ID_LSI_PLASMA:
5775 case PCI_DEVICE_ID_LSI_INVADER:
5776 case PCI_DEVICE_ID_LSI_FURY:
5777 megasas_release_fusion(instance);
5778 for (i = 0; i < 2 ; i++) {
5779 if (fusion->ld_map[i])
5780 dma_free_coherent(&instance->pdev->dev,
5783 fusion->ld_map_phys[i]);
5784 if (fusion->ld_drv_map[i])
5785 free_pages((ulong)fusion->ld_drv_map[i],
5786 fusion->drv_map_pages);
5788 free_pages((ulong)instance->ctrl_context,
5789 instance->ctrl_context_pages);
5792 megasas_release_mfi(instance);
5793 pci_free_consistent(pdev, sizeof(u32),
5795 instance->producer_h);
5796 pci_free_consistent(pdev, sizeof(u32),
5798 instance->consumer_h);
5802 kfree(instance->ctrl_info);
5804 if (instance->evt_detail)
5805 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5806 instance->evt_detail, instance->evt_detail_h);
5808 if (instance->vf_affiliation)
5809 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5810 sizeof(struct MR_LD_VF_AFFILIATION),
5811 instance->vf_affiliation,
5812 instance->vf_affiliation_h);
5814 if (instance->vf_affiliation_111)
5815 pci_free_consistent(pdev,
5816 sizeof(struct MR_LD_VF_AFFILIATION_111),
5817 instance->vf_affiliation_111,
5818 instance->vf_affiliation_111_h);
5820 if (instance->hb_host_mem)
5821 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5822 instance->hb_host_mem,
5823 instance->hb_host_mem_h);
5825 if (instance->crash_dump_buf)
5826 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5827 instance->crash_dump_buf, instance->crash_dump_h);
5829 if (instance->system_info_buf)
5830 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
5831 instance->system_info_buf, instance->system_info_h);
5833 scsi_host_put(host);
5835 pci_disable_device(pdev);
5839 * megasas_shutdown - Shutdown entry point
5840 * @device: Generic device structure
5842 static void megasas_shutdown(struct pci_dev *pdev)
5844 struct megasas_instance *instance = pci_get_drvdata(pdev);
5846 instance->unload = 1;
5847 megasas_flush_cache(instance);
5848 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5849 instance->instancet->disable_intr(instance);
5850 megasas_destroy_irqs(instance);
5852 if (instance->msix_vectors)
5853 pci_disable_msix(instance->pdev);
5857 * megasas_mgmt_open - char node "open" entry point
5859 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
5862 * Allow only those users with admin rights
5864 if (!capable(CAP_SYS_ADMIN))
5871 * megasas_mgmt_fasync - Async notifier registration from applications
5873 * This function adds the calling process to a driver global queue. When an
5874 * event occurs, SIGIO will be sent to all processes in this queue.
5876 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
5880 mutex_lock(&megasas_async_queue_mutex);
5882 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
5884 mutex_unlock(&megasas_async_queue_mutex);
5887 /* For sanity check when we get ioctl */
5888 filep->private_data = filep;
5892 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
5898 * megasas_mgmt_poll - char node "poll" entry point
5900 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
5903 unsigned long flags;
5905 poll_wait(file, &megasas_poll_wait, wait);
5906 spin_lock_irqsave(&poll_aen_lock, flags);
5907 if (megasas_poll_wait_aen)
5908 mask = (POLLIN | POLLRDNORM);
5911 megasas_poll_wait_aen = 0;
5912 spin_unlock_irqrestore(&poll_aen_lock, flags);
5917 * megasas_set_crash_dump_params_ioctl:
5918 * Send CRASH_DUMP_MODE DCMD to all controllers
5919 * @cmd: MFI command frame
5922 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
5924 struct megasas_instance *local_instance;
5928 crash_support = cmd->frame->dcmd.mbox.w[0];
5930 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5931 local_instance = megasas_mgmt_info.instance[i];
5932 if (local_instance && local_instance->crash_dump_drv_support) {
5933 if ((local_instance->adprecovery ==
5934 MEGASAS_HBA_OPERATIONAL) &&
5935 !megasas_set_crash_dump_params(local_instance,
5937 local_instance->crash_dump_app_support =
5939 dev_info(&local_instance->pdev->dev,
5940 "Application firmware crash "
5941 "dump mode set success\n");
5944 dev_info(&local_instance->pdev->dev,
5945 "Application firmware crash "
5946 "dump mode set failed\n");
5955 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
5956 * @instance: Adapter soft state
5957 * @argp: User's ioctl packet
5960 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
5961 struct megasas_iocpacket __user * user_ioc,
5962 struct megasas_iocpacket *ioc)
5964 struct megasas_sge32 *kern_sge32;
5965 struct megasas_cmd *cmd;
5966 void *kbuff_arr[MAX_IOCTL_SGE];
5967 dma_addr_t buf_handle = 0;
5970 dma_addr_t sense_handle;
5971 unsigned long *sense_ptr;
5973 memset(kbuff_arr, 0, sizeof(kbuff_arr));
5975 if (ioc->sge_count > MAX_IOCTL_SGE) {
5976 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
5977 ioc->sge_count, MAX_IOCTL_SGE);
5981 cmd = megasas_get_cmd(instance);
5983 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
5988 * User's IOCTL packet has 2 frames (maximum). Copy those two
5989 * frames into our cmd's frames. cmd->frame's context will get
5990 * overwritten when we copy from user's frames. So set that value
5993 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
5994 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
5995 cmd->frame->hdr.pad_0 = 0;
5996 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
5998 MFI_FRAME_SENSE64));
6000 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6001 error = megasas_set_crash_dump_params_ioctl(cmd);
6002 megasas_return_cmd(instance, cmd);
6007 * The management interface between applications and the fw uses
6008 * MFI frames. E.g, RAID configuration changes, LD property changes
6009 * etc are accomplishes through different kinds of MFI frames. The
6010 * driver needs to care only about substituting user buffers with
6011 * kernel buffers in SGLs. The location of SGL is embedded in the
6012 * struct iocpacket itself.
6014 kern_sge32 = (struct megasas_sge32 *)
6015 ((unsigned long)cmd->frame + ioc->sgl_off);
6018 * For each user buffer, create a mirror buffer and copy in
6020 for (i = 0; i < ioc->sge_count; i++) {
6021 if (!ioc->sgl[i].iov_len)
6024 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
6025 ioc->sgl[i].iov_len,
6026 &buf_handle, GFP_KERNEL);
6027 if (!kbuff_arr[i]) {
6028 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6029 "kernel SGL buffer for IOCTL\n");
6035 * We don't change the dma_coherent_mask, so
6036 * pci_alloc_consistent only returns 32bit addresses
6038 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6039 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
6042 * We created a kernel buffer corresponding to the
6043 * user buffer. Now copy in from the user buffer
6045 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6046 (u32) (ioc->sgl[i].iov_len))) {
6052 if (ioc->sense_len) {
6053 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6054 &sense_handle, GFP_KERNEL);
6061 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
6062 *sense_ptr = cpu_to_le32(sense_handle);
6066 * Set the sync_cmd flag so that the ISR knows not to complete this
6067 * cmd to the SCSI mid-layer
6070 megasas_issue_blocked_cmd(instance, cmd, 0);
6073 if (instance->unload == 1) {
6074 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6075 "don't submit data to application\n");
6079 * copy out the kernel buffers to user buffers
6081 for (i = 0; i < ioc->sge_count; i++) {
6082 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6083 ioc->sgl[i].iov_len)) {
6090 * copy out the sense
6092 if (ioc->sense_len) {
6094 * sense_ptr points to the location that has the user
6095 * sense buffer address
6097 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6100 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6101 sense, ioc->sense_len)) {
6102 dev_err(&instance->pdev->dev, "Failed to copy out to user "
6110 * copy the status codes returned by the fw
6112 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6113 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6114 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
6120 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
6121 sense, sense_handle);
6124 for (i = 0; i < ioc->sge_count; i++) {
6126 dma_free_coherent(&instance->pdev->dev,
6127 le32_to_cpu(kern_sge32[i].length),
6129 le32_to_cpu(kern_sge32[i].phys_addr));
6130 kbuff_arr[i] = NULL;
6133 megasas_return_cmd(instance, cmd);
6137 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6139 struct megasas_iocpacket __user *user_ioc =
6140 (struct megasas_iocpacket __user *)arg;
6141 struct megasas_iocpacket *ioc;
6142 struct megasas_instance *instance;
6145 unsigned long flags;
6146 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6148 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6152 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6157 instance = megasas_lookup_instance(ioc->host_no);
6163 /* Adjust ioctl wait time for VF mode */
6164 if (instance->requestorId)
6165 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6167 /* Block ioctls in VF mode */
6168 if (instance->requestorId && !allow_vf_ioctls) {
6173 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6174 dev_err(&instance->pdev->dev, "Controller in crit error\n");
6179 if (instance->unload == 1) {
6184 if (down_interruptible(&instance->ioctl_sem)) {
6185 error = -ERESTARTSYS;
6189 for (i = 0; i < wait_time; i++) {
6191 spin_lock_irqsave(&instance->hba_lock, flags);
6192 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6193 spin_unlock_irqrestore(&instance->hba_lock, flags);
6196 spin_unlock_irqrestore(&instance->hba_lock, flags);
6198 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6199 dev_notice(&instance->pdev->dev, "waiting"
6200 "for controller reset to finish\n");
6206 spin_lock_irqsave(&instance->hba_lock, flags);
6207 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6208 spin_unlock_irqrestore(&instance->hba_lock, flags);
6210 dev_err(&instance->pdev->dev, "timed out while"
6211 "waiting for HBA to recover\n");
6215 spin_unlock_irqrestore(&instance->hba_lock, flags);
6217 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
6219 up(&instance->ioctl_sem);
6226 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6228 struct megasas_instance *instance;
6229 struct megasas_aen aen;
6232 unsigned long flags;
6233 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6235 if (file->private_data != file) {
6236 printk(KERN_DEBUG "megasas: fasync_helper was not "
6241 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6244 instance = megasas_lookup_instance(aen.host_no);
6249 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6253 if (instance->unload == 1) {
6257 for (i = 0; i < wait_time; i++) {
6259 spin_lock_irqsave(&instance->hba_lock, flags);
6260 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6261 spin_unlock_irqrestore(&instance->hba_lock,
6266 spin_unlock_irqrestore(&instance->hba_lock, flags);
6268 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6269 dev_notice(&instance->pdev->dev, "waiting for"
6270 "controller reset to finish\n");
6276 spin_lock_irqsave(&instance->hba_lock, flags);
6277 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6278 spin_unlock_irqrestore(&instance->hba_lock, flags);
6279 dev_err(&instance->pdev->dev, "timed out while waiting"
6280 "for HBA to recover\n");
6283 spin_unlock_irqrestore(&instance->hba_lock, flags);
6285 mutex_lock(&instance->aen_mutex);
6286 error = megasas_register_aen(instance, aen.seq_num,
6287 aen.class_locale_word);
6288 mutex_unlock(&instance->aen_mutex);
6293 * megasas_mgmt_ioctl - char node ioctl entry point
6296 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6299 case MEGASAS_IOC_FIRMWARE:
6300 return megasas_mgmt_ioctl_fw(file, arg);
6302 case MEGASAS_IOC_GET_AEN:
6303 return megasas_mgmt_ioctl_aen(file, arg);
6309 #ifdef CONFIG_COMPAT
6310 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6312 struct compat_megasas_iocpacket __user *cioc =
6313 (struct compat_megasas_iocpacket __user *)arg;
6314 struct megasas_iocpacket __user *ioc =
6315 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6320 if (clear_user(ioc, sizeof(*ioc)))
6323 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6324 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6325 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6326 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6327 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6328 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6332 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6333 * sense_len is not null, so prepare the 64bit value under
6334 * the same condition.
6336 if (ioc->sense_len) {
6337 void __user **sense_ioc_ptr =
6338 (void __user **)(ioc->frame.raw + ioc->sense_off);
6339 compat_uptr_t *sense_cioc_ptr =
6340 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
6341 if (get_user(ptr, sense_cioc_ptr) ||
6342 put_user(compat_ptr(ptr), sense_ioc_ptr))
6346 for (i = 0; i < MAX_IOCTL_SGE; i++) {
6347 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6348 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6349 copy_in_user(&ioc->sgl[i].iov_len,
6350 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6354 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6356 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6357 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6358 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6365 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6369 case MEGASAS_IOC_FIRMWARE32:
6370 return megasas_mgmt_compat_ioctl_fw(file, arg);
6371 case MEGASAS_IOC_GET_AEN:
6372 return megasas_mgmt_ioctl_aen(file, arg);
6380 * File operations structure for management interface
6382 static const struct file_operations megasas_mgmt_fops = {
6383 .owner = THIS_MODULE,
6384 .open = megasas_mgmt_open,
6385 .fasync = megasas_mgmt_fasync,
6386 .unlocked_ioctl = megasas_mgmt_ioctl,
6387 .poll = megasas_mgmt_poll,
6388 #ifdef CONFIG_COMPAT
6389 .compat_ioctl = megasas_mgmt_compat_ioctl,
6391 .llseek = noop_llseek,
6395 * PCI hotplug support registration structure
6397 static struct pci_driver megasas_pci_driver = {
6399 .name = "megaraid_sas",
6400 .id_table = megasas_pci_table,
6401 .probe = megasas_probe_one,
6402 .remove = megasas_detach_one,
6403 .suspend = megasas_suspend,
6404 .resume = megasas_resume,
6405 .shutdown = megasas_shutdown,
6409 * Sysfs driver attributes
6411 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6413 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6417 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6420 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6422 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6426 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6429 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6431 return sprintf(buf, "%u\n", support_poll_for_event);
6434 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6435 megasas_sysfs_show_support_poll_for_event, NULL);
6438 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6440 return sprintf(buf, "%u\n", support_device_change);
6443 static DRIVER_ATTR(support_device_change, S_IRUGO,
6444 megasas_sysfs_show_support_device_change, NULL);
6447 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6449 return sprintf(buf, "%u\n", megasas_dbg_lvl);
6453 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6457 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
6458 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6464 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
6465 megasas_sysfs_set_dbg_lvl);
6468 megasas_aen_polling(struct work_struct *work)
6470 struct megasas_aen_event *ev =
6471 container_of(work, struct megasas_aen_event, hotplug_work.work);
6472 struct megasas_instance *instance = ev->instance;
6473 union megasas_evt_class_locale class_locale;
6474 struct Scsi_Host *host;
6475 struct scsi_device *sdev1;
6478 int i, j, doscan = 0;
6479 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
6483 printk(KERN_ERR "invalid instance!\n");
6488 /* Adjust event workqueue thread wait time for VF mode */
6489 if (instance->requestorId)
6490 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6492 /* Don't run the event workqueue thread if OCR is running */
6493 for (i = 0; i < wait_time; i++) {
6494 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL)
6496 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6497 dev_notice(&instance->pdev->dev, "%s waiting for "
6498 "controller reset to finish for scsi%d\n",
6499 __func__, instance->host->host_no);
6504 instance->ev = NULL;
6505 host = instance->host;
6506 if (instance->evt_detail) {
6508 switch (le32_to_cpu(instance->evt_detail->code)) {
6509 case MR_EVT_PD_INSERTED:
6510 if (megasas_get_pd_list(instance) == 0) {
6511 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6513 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6517 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6519 sdev1 = scsi_device_lookup(host, i, j, 0);
6521 if (instance->pd_list[pd_index].driveState
6522 == MR_PD_STATE_SYSTEM) {
6524 scsi_add_device(host, i, j, 0);
6527 scsi_device_put(sdev1);
6535 case MR_EVT_PD_REMOVED:
6536 if (megasas_get_pd_list(instance) == 0) {
6537 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6539 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6543 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6545 sdev1 = scsi_device_lookup(host, i, j, 0);
6547 if (instance->pd_list[pd_index].driveState
6548 == MR_PD_STATE_SYSTEM) {
6550 scsi_device_put(sdev1);
6553 scsi_remove_device(sdev1);
6554 scsi_device_put(sdev1);
6563 case MR_EVT_LD_OFFLINE:
6564 case MR_EVT_CFG_CLEARED:
6565 case MR_EVT_LD_DELETED:
6566 if (!instance->requestorId ||
6567 (instance->requestorId &&
6568 megasas_get_ld_vf_affiliation(instance, 0))) {
6569 if (megasas_ld_list_query(instance,
6570 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6571 megasas_get_ld_list(instance);
6572 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6574 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6578 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6580 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6582 if (instance->ld_ids[ld_index]
6585 scsi_device_put(sdev1);
6588 scsi_remove_device(sdev1);
6589 scsi_device_put(sdev1);
6597 case MR_EVT_LD_CREATED:
6598 if (!instance->requestorId ||
6599 (instance->requestorId &&
6600 megasas_get_ld_vf_affiliation(instance, 0))) {
6601 if (megasas_ld_list_query(instance,
6602 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6603 megasas_get_ld_list(instance);
6604 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6606 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6609 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6611 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6613 if (instance->ld_ids[ld_index]
6616 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6619 scsi_device_put(sdev1);
6625 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
6626 case MR_EVT_FOREIGN_CFG_IMPORTED:
6627 case MR_EVT_LD_STATE_CHANGE:
6635 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
6641 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
6642 instance->host->host_no);
6643 if (megasas_get_pd_list(instance) == 0) {
6644 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6645 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6646 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
6647 sdev1 = scsi_device_lookup(host, i, j, 0);
6648 if (instance->pd_list[pd_index].driveState ==
6649 MR_PD_STATE_SYSTEM) {
6651 scsi_add_device(host, i, j, 0);
6654 scsi_device_put(sdev1);
6657 scsi_remove_device(sdev1);
6658 scsi_device_put(sdev1);
6665 if (!instance->requestorId ||
6666 (instance->requestorId &&
6667 megasas_get_ld_vf_affiliation(instance, 0))) {
6668 if (megasas_ld_list_query(instance,
6669 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6670 megasas_get_ld_list(instance);
6671 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6672 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL;
6675 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6677 sdev1 = scsi_device_lookup(host,
6678 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6679 if (instance->ld_ids[ld_index]
6682 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6684 scsi_device_put(sdev1);
6687 scsi_remove_device(sdev1);
6688 scsi_device_put(sdev1);
6696 if (instance->aen_cmd != NULL) {
6701 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
6703 /* Register AEN with FW for latest sequence number plus 1 */
6704 class_locale.members.reserved = 0;
6705 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6706 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6707 mutex_lock(&instance->aen_mutex);
6708 error = megasas_register_aen(instance, seq_num,
6710 mutex_unlock(&instance->aen_mutex);
6713 dev_err(&instance->pdev->dev, "register aen failed error %x\n", error);
6719 * megasas_init - Driver load entry point
6721 static int __init megasas_init(void)
6726 * Announce driver version and other information
6728 pr_info("megasas: %s\n", MEGASAS_VERSION);
6730 spin_lock_init(&poll_aen_lock);
6732 support_poll_for_event = 2;
6733 support_device_change = 1;
6735 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
6738 * Register character device node
6740 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
6743 printk(KERN_DEBUG "megasas: failed to open device node\n");
6747 megasas_mgmt_majorno = rval;
6750 * Register ourselves as PCI hotplug module
6752 rval = pci_register_driver(&megasas_pci_driver);
6755 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
6759 rval = driver_create_file(&megasas_pci_driver.driver,
6760 &driver_attr_version);
6762 goto err_dcf_attr_ver;
6764 rval = driver_create_file(&megasas_pci_driver.driver,
6765 &driver_attr_release_date);
6767 goto err_dcf_rel_date;
6769 rval = driver_create_file(&megasas_pci_driver.driver,
6770 &driver_attr_support_poll_for_event);
6772 goto err_dcf_support_poll_for_event;
6774 rval = driver_create_file(&megasas_pci_driver.driver,
6775 &driver_attr_dbg_lvl);
6777 goto err_dcf_dbg_lvl;
6778 rval = driver_create_file(&megasas_pci_driver.driver,
6779 &driver_attr_support_device_change);
6781 goto err_dcf_support_device_change;
6785 err_dcf_support_device_change:
6786 driver_remove_file(&megasas_pci_driver.driver,
6787 &driver_attr_dbg_lvl);
6789 driver_remove_file(&megasas_pci_driver.driver,
6790 &driver_attr_support_poll_for_event);
6791 err_dcf_support_poll_for_event:
6792 driver_remove_file(&megasas_pci_driver.driver,
6793 &driver_attr_release_date);
6795 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6797 pci_unregister_driver(&megasas_pci_driver);
6799 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6804 * megasas_exit - Driver unload entry point
6806 static void __exit megasas_exit(void)
6808 driver_remove_file(&megasas_pci_driver.driver,
6809 &driver_attr_dbg_lvl);
6810 driver_remove_file(&megasas_pci_driver.driver,
6811 &driver_attr_support_poll_for_event);
6812 driver_remove_file(&megasas_pci_driver.driver,
6813 &driver_attr_support_device_change);
6814 driver_remove_file(&megasas_pci_driver.driver,
6815 &driver_attr_release_date);
6816 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6818 pci_unregister_driver(&megasas_pci_driver);
6819 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6822 module_init(megasas_init);
6823 module_exit(megasas_exit);