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 <linux/uaccess.h>
46 #include <asm/unaligned.h>
48 #include <linux/compat.h>
49 #include <linux/blkdev.h>
50 #include <linux/mutex.h>
51 #include <linux/poll.h>
52 #include <linux/vmalloc.h>
54 #include <scsi/scsi.h>
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_device.h>
57 #include <scsi/scsi_host.h>
58 #include <scsi/scsi_tcq.h>
59 #include "megaraid_sas_fusion.h"
60 #include "megaraid_sas.h"
63 * Number of sectors per IO command
64 * Will be set in megasas_init_mfi if user does not provide
66 static unsigned int max_sectors;
67 module_param_named(max_sectors, max_sectors, int, 0);
68 MODULE_PARM_DESC(max_sectors,
69 "Maximum number of sectors per IO command");
71 static int msix_disable;
72 module_param(msix_disable, int, S_IRUGO);
73 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
75 static unsigned int msix_vectors;
76 module_param(msix_vectors, int, S_IRUGO);
77 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
79 static int allow_vf_ioctls;
80 module_param(allow_vf_ioctls, int, S_IRUGO);
81 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
83 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
84 module_param(throttlequeuedepth, int, S_IRUGO);
85 MODULE_PARM_DESC(throttlequeuedepth,
86 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
88 unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
89 module_param(resetwaittime, int, S_IRUGO);
90 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
91 "before resetting adapter. Default: 180");
93 int smp_affinity_enable = 1;
94 module_param(smp_affinity_enable, int, S_IRUGO);
95 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
98 module_param(rdpq_enable, int, S_IRUGO);
99 MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
101 unsigned int dual_qdepth_disable;
102 module_param(dual_qdepth_disable, int, S_IRUGO);
103 MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
105 unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
106 module_param(scmd_timeout, int, S_IRUGO);
107 MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
109 MODULE_LICENSE("GPL");
110 MODULE_VERSION(MEGASAS_VERSION);
111 MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
112 MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
114 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
115 static int megasas_get_pd_list(struct megasas_instance *instance);
116 static int megasas_ld_list_query(struct megasas_instance *instance,
118 static int megasas_issue_init_mfi(struct megasas_instance *instance);
119 static int megasas_register_aen(struct megasas_instance *instance,
120 u32 seq_num, u32 class_locale_word);
121 static void megasas_get_pd_info(struct megasas_instance *instance,
122 struct scsi_device *sdev);
123 static int megasas_get_target_prop(struct megasas_instance *instance,
124 struct scsi_device *sdev);
126 * PCI ID table for all supported controllers
128 static struct pci_device_id megasas_pci_table[] = {
130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
144 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
145 /* xscale IOP, vega */
146 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
159 /* Intruder 24 port*/
160 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
161 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
165 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
167 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
171 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
173 static int megasas_mgmt_majorno;
174 struct megasas_mgmt_info megasas_mgmt_info;
175 static struct fasync_struct *megasas_async_queue;
176 static DEFINE_MUTEX(megasas_async_queue_mutex);
178 static int megasas_poll_wait_aen;
179 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
180 static u32 support_poll_for_event;
182 static u32 support_device_change;
184 /* define lock for aen poll */
185 spinlock_t poll_aen_lock;
188 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
191 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
193 megasas_adp_reset_gen2(struct megasas_instance *instance,
194 struct megasas_register_set __iomem *reg_set);
195 static irqreturn_t megasas_isr(int irq, void *devp);
197 megasas_init_adapter_mfi(struct megasas_instance *instance);
199 megasas_build_and_issue_cmd(struct megasas_instance *instance,
200 struct scsi_cmnd *scmd);
201 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
203 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
205 void megasas_fusion_ocr_wq(struct work_struct *work);
206 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
210 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
212 instance->instancet->fire_cmd(instance,
213 cmd->frame_phys_addr, 0, instance->reg_set);
218 * megasas_get_cmd - Get a command from the free pool
219 * @instance: Adapter soft state
221 * Returns a free command from the pool
223 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
227 struct megasas_cmd *cmd = NULL;
229 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
231 if (!list_empty(&instance->cmd_pool)) {
232 cmd = list_entry((&instance->cmd_pool)->next,
233 struct megasas_cmd, list);
234 list_del_init(&cmd->list);
236 dev_err(&instance->pdev->dev, "Command pool empty!\n");
239 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
244 * megasas_return_cmd - Return a cmd to free command pool
245 * @instance: Adapter soft state
246 * @cmd: Command packet to be returned to free command pool
249 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
253 struct megasas_cmd_fusion *cmd_fusion;
254 struct fusion_context *fusion = instance->ctrl_context;
256 /* This flag is used only for fusion adapter.
257 * Wait for Interrupt for Polled mode DCMD
259 if (cmd->flags & DRV_DCMD_POLLED_MODE)
262 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
265 blk_tags = instance->max_scsi_cmds + cmd->index;
266 cmd_fusion = fusion->cmd_list[blk_tags];
267 megasas_return_cmd_fusion(instance, cmd_fusion);
270 cmd->frame_count = 0;
272 memset(cmd->frame, 0, instance->mfi_frame_size);
273 cmd->frame->io.context = cpu_to_le32(cmd->index);
274 if (!fusion && reset_devices)
275 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
276 list_add(&cmd->list, (&instance->cmd_pool)->next);
278 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
283 format_timestamp(uint32_t timestamp)
285 static char buffer[32];
287 if ((timestamp & 0xff000000) == 0xff000000)
288 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
291 snprintf(buffer, sizeof(buffer), "%us", timestamp);
296 format_class(int8_t class)
298 static char buffer[6];
301 case MFI_EVT_CLASS_DEBUG:
303 case MFI_EVT_CLASS_PROGRESS:
305 case MFI_EVT_CLASS_INFO:
307 case MFI_EVT_CLASS_WARNING:
309 case MFI_EVT_CLASS_CRITICAL:
311 case MFI_EVT_CLASS_FATAL:
313 case MFI_EVT_CLASS_DEAD:
316 snprintf(buffer, sizeof(buffer), "%d", class);
322 * megasas_decode_evt: Decode FW AEN event and print critical event
324 * @instance: Adapter soft state
327 megasas_decode_evt(struct megasas_instance *instance)
329 struct megasas_evt_detail *evt_detail = instance->evt_detail;
330 union megasas_evt_class_locale class_locale;
331 class_locale.word = le32_to_cpu(evt_detail->cl.word);
333 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
334 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
335 le32_to_cpu(evt_detail->seq_num),
336 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
337 (class_locale.members.locale),
338 format_class(class_locale.members.class),
339 evt_detail->description);
343 * The following functions are defined for xscale
344 * (deviceid : 1064R, PERC5) controllers
348 * megasas_enable_intr_xscale - Enables interrupts
349 * @regs: MFI register set
352 megasas_enable_intr_xscale(struct megasas_instance *instance)
354 struct megasas_register_set __iomem *regs;
356 regs = instance->reg_set;
357 writel(0, &(regs)->outbound_intr_mask);
359 /* Dummy readl to force pci flush */
360 readl(®s->outbound_intr_mask);
364 * megasas_disable_intr_xscale -Disables interrupt
365 * @regs: MFI register set
368 megasas_disable_intr_xscale(struct megasas_instance *instance)
370 struct megasas_register_set __iomem *regs;
373 regs = instance->reg_set;
374 writel(mask, ®s->outbound_intr_mask);
375 /* Dummy readl to force pci flush */
376 readl(®s->outbound_intr_mask);
380 * megasas_read_fw_status_reg_xscale - returns the current FW status value
381 * @regs: MFI register set
384 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
386 return readl(&(regs)->outbound_msg_0);
389 * megasas_clear_interrupt_xscale - Check & clear interrupt
390 * @regs: MFI register set
393 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
399 * Check if it is our interrupt
401 status = readl(®s->outbound_intr_status);
403 if (status & MFI_OB_INTR_STATUS_MASK)
404 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
405 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
406 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
409 * Clear the interrupt by writing back the same value
412 writel(status, ®s->outbound_intr_status);
414 /* Dummy readl to force pci flush */
415 readl(®s->outbound_intr_status);
421 * megasas_fire_cmd_xscale - Sends command to the FW
422 * @frame_phys_addr : Physical address of cmd
423 * @frame_count : Number of frames for the command
424 * @regs : MFI register set
427 megasas_fire_cmd_xscale(struct megasas_instance *instance,
428 dma_addr_t frame_phys_addr,
430 struct megasas_register_set __iomem *regs)
434 spin_lock_irqsave(&instance->hba_lock, flags);
435 writel((frame_phys_addr >> 3)|(frame_count),
436 &(regs)->inbound_queue_port);
437 spin_unlock_irqrestore(&instance->hba_lock, flags);
441 * megasas_adp_reset_xscale - For controller reset
442 * @regs: MFI register set
445 megasas_adp_reset_xscale(struct megasas_instance *instance,
446 struct megasas_register_set __iomem *regs)
451 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
453 for (i = 0; i < 3; i++)
454 msleep(1000); /* sleep for 3 secs */
456 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
457 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
459 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
461 pci_write_config_dword(instance->pdev,
462 MFI_1068_PCSR_OFFSET, pcidata);
464 for (i = 0; i < 2; i++)
465 msleep(1000); /* need to wait 2 secs again */
468 pci_read_config_dword(instance->pdev,
469 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
470 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
471 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
472 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
474 pci_write_config_dword(instance->pdev,
475 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
482 * megasas_check_reset_xscale - For controller reset check
483 * @regs: MFI register set
486 megasas_check_reset_xscale(struct megasas_instance *instance,
487 struct megasas_register_set __iomem *regs)
489 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
490 (le32_to_cpu(*instance->consumer) ==
491 MEGASAS_ADPRESET_INPROG_SIGN))
496 static struct megasas_instance_template megasas_instance_template_xscale = {
498 .fire_cmd = megasas_fire_cmd_xscale,
499 .enable_intr = megasas_enable_intr_xscale,
500 .disable_intr = megasas_disable_intr_xscale,
501 .clear_intr = megasas_clear_intr_xscale,
502 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
503 .adp_reset = megasas_adp_reset_xscale,
504 .check_reset = megasas_check_reset_xscale,
505 .service_isr = megasas_isr,
506 .tasklet = megasas_complete_cmd_dpc,
507 .init_adapter = megasas_init_adapter_mfi,
508 .build_and_issue_cmd = megasas_build_and_issue_cmd,
509 .issue_dcmd = megasas_issue_dcmd,
513 * This is the end of set of functions & definitions specific
514 * to xscale (deviceid : 1064R, PERC5) controllers
518 * The following functions are defined for ppc (deviceid : 0x60)
523 * megasas_enable_intr_ppc - Enables interrupts
524 * @regs: MFI register set
527 megasas_enable_intr_ppc(struct megasas_instance *instance)
529 struct megasas_register_set __iomem *regs;
531 regs = instance->reg_set;
532 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
534 writel(~0x80000000, &(regs)->outbound_intr_mask);
536 /* Dummy readl to force pci flush */
537 readl(®s->outbound_intr_mask);
541 * megasas_disable_intr_ppc - Disable interrupt
542 * @regs: MFI register set
545 megasas_disable_intr_ppc(struct megasas_instance *instance)
547 struct megasas_register_set __iomem *regs;
548 u32 mask = 0xFFFFFFFF;
550 regs = instance->reg_set;
551 writel(mask, ®s->outbound_intr_mask);
552 /* Dummy readl to force pci flush */
553 readl(®s->outbound_intr_mask);
557 * megasas_read_fw_status_reg_ppc - returns the current FW status value
558 * @regs: MFI register set
561 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
563 return readl(&(regs)->outbound_scratch_pad);
567 * megasas_clear_interrupt_ppc - Check & clear interrupt
568 * @regs: MFI register set
571 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
573 u32 status, mfiStatus = 0;
576 * Check if it is our interrupt
578 status = readl(®s->outbound_intr_status);
580 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
581 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
583 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
584 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
587 * Clear the interrupt by writing back the same value
589 writel(status, ®s->outbound_doorbell_clear);
591 /* Dummy readl to force pci flush */
592 readl(®s->outbound_doorbell_clear);
598 * megasas_fire_cmd_ppc - Sends command to the FW
599 * @frame_phys_addr : Physical address of cmd
600 * @frame_count : Number of frames for the command
601 * @regs : MFI register set
604 megasas_fire_cmd_ppc(struct megasas_instance *instance,
605 dma_addr_t frame_phys_addr,
607 struct megasas_register_set __iomem *regs)
611 spin_lock_irqsave(&instance->hba_lock, flags);
612 writel((frame_phys_addr | (frame_count<<1))|1,
613 &(regs)->inbound_queue_port);
614 spin_unlock_irqrestore(&instance->hba_lock, flags);
618 * megasas_check_reset_ppc - For controller reset check
619 * @regs: MFI register set
622 megasas_check_reset_ppc(struct megasas_instance *instance,
623 struct megasas_register_set __iomem *regs)
625 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
631 static struct megasas_instance_template megasas_instance_template_ppc = {
633 .fire_cmd = megasas_fire_cmd_ppc,
634 .enable_intr = megasas_enable_intr_ppc,
635 .disable_intr = megasas_disable_intr_ppc,
636 .clear_intr = megasas_clear_intr_ppc,
637 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
638 .adp_reset = megasas_adp_reset_xscale,
639 .check_reset = megasas_check_reset_ppc,
640 .service_isr = megasas_isr,
641 .tasklet = megasas_complete_cmd_dpc,
642 .init_adapter = megasas_init_adapter_mfi,
643 .build_and_issue_cmd = megasas_build_and_issue_cmd,
644 .issue_dcmd = megasas_issue_dcmd,
648 * megasas_enable_intr_skinny - Enables interrupts
649 * @regs: MFI register set
652 megasas_enable_intr_skinny(struct megasas_instance *instance)
654 struct megasas_register_set __iomem *regs;
656 regs = instance->reg_set;
657 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
659 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
661 /* Dummy readl to force pci flush */
662 readl(®s->outbound_intr_mask);
666 * megasas_disable_intr_skinny - Disables interrupt
667 * @regs: MFI register set
670 megasas_disable_intr_skinny(struct megasas_instance *instance)
672 struct megasas_register_set __iomem *regs;
673 u32 mask = 0xFFFFFFFF;
675 regs = instance->reg_set;
676 writel(mask, ®s->outbound_intr_mask);
677 /* Dummy readl to force pci flush */
678 readl(®s->outbound_intr_mask);
682 * megasas_read_fw_status_reg_skinny - returns the current FW status value
683 * @regs: MFI register set
686 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
688 return readl(&(regs)->outbound_scratch_pad);
692 * megasas_clear_interrupt_skinny - Check & clear interrupt
693 * @regs: MFI register set
696 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
702 * Check if it is our interrupt
704 status = readl(®s->outbound_intr_status);
706 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
711 * Check if it is our interrupt
713 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
715 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
717 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
720 * Clear the interrupt by writing back the same value
722 writel(status, ®s->outbound_intr_status);
725 * dummy read to flush PCI
727 readl(®s->outbound_intr_status);
733 * megasas_fire_cmd_skinny - Sends command to the FW
734 * @frame_phys_addr : Physical address of cmd
735 * @frame_count : Number of frames for the command
736 * @regs : MFI register set
739 megasas_fire_cmd_skinny(struct megasas_instance *instance,
740 dma_addr_t frame_phys_addr,
742 struct megasas_register_set __iomem *regs)
746 spin_lock_irqsave(&instance->hba_lock, flags);
747 writel(upper_32_bits(frame_phys_addr),
748 &(regs)->inbound_high_queue_port);
749 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
750 &(regs)->inbound_low_queue_port);
752 spin_unlock_irqrestore(&instance->hba_lock, flags);
756 * megasas_check_reset_skinny - For controller reset check
757 * @regs: MFI register set
760 megasas_check_reset_skinny(struct megasas_instance *instance,
761 struct megasas_register_set __iomem *regs)
763 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
769 static struct megasas_instance_template megasas_instance_template_skinny = {
771 .fire_cmd = megasas_fire_cmd_skinny,
772 .enable_intr = megasas_enable_intr_skinny,
773 .disable_intr = megasas_disable_intr_skinny,
774 .clear_intr = megasas_clear_intr_skinny,
775 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
776 .adp_reset = megasas_adp_reset_gen2,
777 .check_reset = megasas_check_reset_skinny,
778 .service_isr = megasas_isr,
779 .tasklet = megasas_complete_cmd_dpc,
780 .init_adapter = megasas_init_adapter_mfi,
781 .build_and_issue_cmd = megasas_build_and_issue_cmd,
782 .issue_dcmd = megasas_issue_dcmd,
787 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
792 * megasas_enable_intr_gen2 - Enables interrupts
793 * @regs: MFI register set
796 megasas_enable_intr_gen2(struct megasas_instance *instance)
798 struct megasas_register_set __iomem *regs;
800 regs = instance->reg_set;
801 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
803 /* write ~0x00000005 (4 & 1) to the intr mask*/
804 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
806 /* Dummy readl to force pci flush */
807 readl(®s->outbound_intr_mask);
811 * megasas_disable_intr_gen2 - Disables interrupt
812 * @regs: MFI register set
815 megasas_disable_intr_gen2(struct megasas_instance *instance)
817 struct megasas_register_set __iomem *regs;
818 u32 mask = 0xFFFFFFFF;
820 regs = instance->reg_set;
821 writel(mask, ®s->outbound_intr_mask);
822 /* Dummy readl to force pci flush */
823 readl(®s->outbound_intr_mask);
827 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
828 * @regs: MFI register set
831 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
833 return readl(&(regs)->outbound_scratch_pad);
837 * megasas_clear_interrupt_gen2 - Check & clear interrupt
838 * @regs: MFI register set
841 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
847 * Check if it is our interrupt
849 status = readl(®s->outbound_intr_status);
851 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
852 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
854 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
855 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
859 * Clear the interrupt by writing back the same value
862 writel(status, ®s->outbound_doorbell_clear);
864 /* Dummy readl to force pci flush */
865 readl(®s->outbound_intr_status);
870 * megasas_fire_cmd_gen2 - Sends command to the FW
871 * @frame_phys_addr : Physical address of cmd
872 * @frame_count : Number of frames for the command
873 * @regs : MFI register set
876 megasas_fire_cmd_gen2(struct megasas_instance *instance,
877 dma_addr_t frame_phys_addr,
879 struct megasas_register_set __iomem *regs)
883 spin_lock_irqsave(&instance->hba_lock, flags);
884 writel((frame_phys_addr | (frame_count<<1))|1,
885 &(regs)->inbound_queue_port);
886 spin_unlock_irqrestore(&instance->hba_lock, flags);
890 * megasas_adp_reset_gen2 - For controller reset
891 * @regs: MFI register set
894 megasas_adp_reset_gen2(struct megasas_instance *instance,
895 struct megasas_register_set __iomem *reg_set)
899 u32 __iomem *seq_offset = ®_set->seq_offset;
900 u32 __iomem *hostdiag_offset = ®_set->host_diag;
902 if (instance->instancet == &megasas_instance_template_skinny) {
903 seq_offset = ®_set->fusion_seq_offset;
904 hostdiag_offset = ®_set->fusion_host_diag;
907 writel(0, seq_offset);
908 writel(4, seq_offset);
909 writel(0xb, seq_offset);
910 writel(2, seq_offset);
911 writel(7, seq_offset);
912 writel(0xd, seq_offset);
916 HostDiag = (u32)readl(hostdiag_offset);
918 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
920 HostDiag = (u32)readl(hostdiag_offset);
921 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
929 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
931 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
935 HostDiag = (u32)readl(hostdiag_offset);
936 while (HostDiag & DIAG_RESET_ADAPTER) {
938 HostDiag = (u32)readl(hostdiag_offset);
939 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
950 * megasas_check_reset_gen2 - For controller reset check
951 * @regs: MFI register set
954 megasas_check_reset_gen2(struct megasas_instance *instance,
955 struct megasas_register_set __iomem *regs)
957 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
963 static struct megasas_instance_template megasas_instance_template_gen2 = {
965 .fire_cmd = megasas_fire_cmd_gen2,
966 .enable_intr = megasas_enable_intr_gen2,
967 .disable_intr = megasas_disable_intr_gen2,
968 .clear_intr = megasas_clear_intr_gen2,
969 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
970 .adp_reset = megasas_adp_reset_gen2,
971 .check_reset = megasas_check_reset_gen2,
972 .service_isr = megasas_isr,
973 .tasklet = megasas_complete_cmd_dpc,
974 .init_adapter = megasas_init_adapter_mfi,
975 .build_and_issue_cmd = megasas_build_and_issue_cmd,
976 .issue_dcmd = megasas_issue_dcmd,
980 * This is the end of set of functions & definitions
981 * specific to gen2 (deviceid : 0x78, 0x79) controllers
985 * Template added for TB (Fusion)
987 extern struct megasas_instance_template megasas_instance_template_fusion;
990 * megasas_issue_polled - Issues a polling command
991 * @instance: Adapter soft state
992 * @cmd: Command packet to be issued
994 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
997 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
999 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1001 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1002 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1004 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1005 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1006 __func__, __LINE__);
1007 return DCMD_NOT_FIRED;
1010 instance->instancet->issue_dcmd(instance, cmd);
1012 return wait_and_poll(instance, cmd, instance->requestorId ?
1013 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1017 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1018 * @instance: Adapter soft state
1019 * @cmd: Command to be issued
1020 * @timeout: Timeout in seconds
1022 * This function waits on an event for the command to be returned from ISR.
1023 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1024 * Used to issue ioctl commands.
1027 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1028 struct megasas_cmd *cmd, int timeout)
1031 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1033 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1034 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1035 __func__, __LINE__);
1036 return DCMD_NOT_FIRED;
1039 instance->instancet->issue_dcmd(instance, cmd);
1042 ret = wait_event_timeout(instance->int_cmd_wait_q,
1043 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1045 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1046 __func__, __LINE__);
1047 return DCMD_TIMEOUT;
1050 wait_event(instance->int_cmd_wait_q,
1051 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1053 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1054 DCMD_SUCCESS : DCMD_FAILED;
1058 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1059 * @instance: Adapter soft state
1060 * @cmd_to_abort: Previously issued cmd to be aborted
1061 * @timeout: Timeout in seconds
1063 * MFI firmware can abort previously issued AEN comamnd (automatic event
1064 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1065 * cmd and waits for return status.
1066 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1069 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1070 struct megasas_cmd *cmd_to_abort, int timeout)
1072 struct megasas_cmd *cmd;
1073 struct megasas_abort_frame *abort_fr;
1076 cmd = megasas_get_cmd(instance);
1081 abort_fr = &cmd->frame->abort;
1084 * Prepare and issue the abort frame
1086 abort_fr->cmd = MFI_CMD_ABORT;
1087 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1088 abort_fr->flags = cpu_to_le16(0);
1089 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1090 abort_fr->abort_mfi_phys_addr_lo =
1091 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1092 abort_fr->abort_mfi_phys_addr_hi =
1093 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1096 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1098 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1099 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1100 __func__, __LINE__);
1101 return DCMD_NOT_FIRED;
1104 instance->instancet->issue_dcmd(instance, cmd);
1107 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1108 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1110 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1111 __func__, __LINE__);
1112 return DCMD_TIMEOUT;
1115 wait_event(instance->abort_cmd_wait_q,
1116 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1120 megasas_return_cmd(instance, cmd);
1121 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1122 DCMD_SUCCESS : DCMD_FAILED;
1126 * megasas_make_sgl32 - Prepares 32-bit SGL
1127 * @instance: Adapter soft state
1128 * @scp: SCSI command from the mid-layer
1129 * @mfi_sgl: SGL to be filled in
1131 * If successful, this function returns the number of SG elements. Otherwise,
1135 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1136 union megasas_sgl *mfi_sgl)
1140 struct scatterlist *os_sgl;
1142 sge_count = scsi_dma_map(scp);
1143 BUG_ON(sge_count < 0);
1146 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1147 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1148 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1155 * megasas_make_sgl64 - Prepares 64-bit SGL
1156 * @instance: Adapter soft state
1157 * @scp: SCSI command from the mid-layer
1158 * @mfi_sgl: SGL to be filled in
1160 * If successful, this function returns the number of SG elements. Otherwise,
1164 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1165 union megasas_sgl *mfi_sgl)
1169 struct scatterlist *os_sgl;
1171 sge_count = scsi_dma_map(scp);
1172 BUG_ON(sge_count < 0);
1175 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1176 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1177 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1184 * megasas_make_sgl_skinny - Prepares IEEE SGL
1185 * @instance: Adapter soft state
1186 * @scp: SCSI command from the mid-layer
1187 * @mfi_sgl: SGL to be filled in
1189 * If successful, this function returns the number of SG elements. Otherwise,
1193 megasas_make_sgl_skinny(struct megasas_instance *instance,
1194 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1198 struct scatterlist *os_sgl;
1200 sge_count = scsi_dma_map(scp);
1203 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1204 mfi_sgl->sge_skinny[i].length =
1205 cpu_to_le32(sg_dma_len(os_sgl));
1206 mfi_sgl->sge_skinny[i].phys_addr =
1207 cpu_to_le64(sg_dma_address(os_sgl));
1208 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1215 * megasas_get_frame_count - Computes the number of frames
1216 * @frame_type : type of frame- io or pthru frame
1217 * @sge_count : number of sg elements
1219 * Returns the number of frames required for numnber of sge's (sge_count)
1222 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1223 u8 sge_count, u8 frame_type)
1228 u32 frame_count = 0;
1230 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1231 sizeof(struct megasas_sge32);
1233 if (instance->flag_ieee) {
1234 sge_sz = sizeof(struct megasas_sge_skinny);
1238 * Main frame can contain 2 SGEs for 64-bit SGLs and
1239 * 3 SGEs for 32-bit SGLs for ldio &
1240 * 1 SGEs for 64-bit SGLs and
1241 * 2 SGEs for 32-bit SGLs for pthru frame
1243 if (unlikely(frame_type == PTHRU_FRAME)) {
1244 if (instance->flag_ieee == 1) {
1245 num_cnt = sge_count - 1;
1246 } else if (IS_DMA64)
1247 num_cnt = sge_count - 1;
1249 num_cnt = sge_count - 2;
1251 if (instance->flag_ieee == 1) {
1252 num_cnt = sge_count - 1;
1253 } else if (IS_DMA64)
1254 num_cnt = sge_count - 2;
1256 num_cnt = sge_count - 3;
1260 sge_bytes = sge_sz * num_cnt;
1262 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1263 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1268 if (frame_count > 7)
1274 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1275 * @instance: Adapter soft state
1276 * @scp: SCSI command
1277 * @cmd: Command to be prepared in
1279 * This function prepares CDB commands. These are typcially pass-through
1280 * commands to the devices.
1283 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1284 struct megasas_cmd *cmd)
1289 struct megasas_pthru_frame *pthru;
1291 is_logical = MEGASAS_IS_LOGICAL(scp->device);
1292 device_id = MEGASAS_DEV_INDEX(scp);
1293 pthru = (struct megasas_pthru_frame *)cmd->frame;
1295 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1296 flags = MFI_FRAME_DIR_WRITE;
1297 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1298 flags = MFI_FRAME_DIR_READ;
1299 else if (scp->sc_data_direction == PCI_DMA_NONE)
1300 flags = MFI_FRAME_DIR_NONE;
1302 if (instance->flag_ieee == 1) {
1303 flags |= MFI_FRAME_IEEE;
1307 * Prepare the DCDB frame
1309 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1310 pthru->cmd_status = 0x0;
1311 pthru->scsi_status = 0x0;
1312 pthru->target_id = device_id;
1313 pthru->lun = scp->device->lun;
1314 pthru->cdb_len = scp->cmd_len;
1317 pthru->flags = cpu_to_le16(flags);
1318 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1320 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1323 * If the command is for the tape device, set the
1324 * pthru timeout to the os layer timeout value.
1326 if (scp->device->type == TYPE_TAPE) {
1327 if ((scp->request->timeout / HZ) > 0xFFFF)
1328 pthru->timeout = cpu_to_le16(0xFFFF);
1330 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1336 if (instance->flag_ieee == 1) {
1337 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1338 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1340 } else if (IS_DMA64) {
1341 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1342 pthru->sge_count = megasas_make_sgl64(instance, scp,
1345 pthru->sge_count = megasas_make_sgl32(instance, scp,
1348 if (pthru->sge_count > instance->max_num_sge) {
1349 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1355 * Sense info specific
1357 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1358 pthru->sense_buf_phys_addr_hi =
1359 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1360 pthru->sense_buf_phys_addr_lo =
1361 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1364 * Compute the total number of frames this command consumes. FW uses
1365 * this number to pull sufficient number of frames from host memory.
1367 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1370 return cmd->frame_count;
1374 * megasas_build_ldio - Prepares IOs to logical devices
1375 * @instance: Adapter soft state
1376 * @scp: SCSI command
1377 * @cmd: Command to be prepared
1379 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1382 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1383 struct megasas_cmd *cmd)
1386 u8 sc = scp->cmnd[0];
1388 struct megasas_io_frame *ldio;
1390 device_id = MEGASAS_DEV_INDEX(scp);
1391 ldio = (struct megasas_io_frame *)cmd->frame;
1393 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1394 flags = MFI_FRAME_DIR_WRITE;
1395 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1396 flags = MFI_FRAME_DIR_READ;
1398 if (instance->flag_ieee == 1) {
1399 flags |= MFI_FRAME_IEEE;
1403 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1405 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1406 ldio->cmd_status = 0x0;
1407 ldio->scsi_status = 0x0;
1408 ldio->target_id = device_id;
1410 ldio->reserved_0 = 0;
1412 ldio->flags = cpu_to_le16(flags);
1413 ldio->start_lba_hi = 0;
1414 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1417 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1419 if (scp->cmd_len == 6) {
1420 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1421 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1422 ((u32) scp->cmnd[2] << 8) |
1423 (u32) scp->cmnd[3]);
1425 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1429 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1431 else if (scp->cmd_len == 10) {
1432 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1433 ((u32) scp->cmnd[7] << 8));
1434 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1435 ((u32) scp->cmnd[3] << 16) |
1436 ((u32) scp->cmnd[4] << 8) |
1437 (u32) scp->cmnd[5]);
1441 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1443 else if (scp->cmd_len == 12) {
1444 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1445 ((u32) scp->cmnd[7] << 16) |
1446 ((u32) scp->cmnd[8] << 8) |
1447 (u32) scp->cmnd[9]);
1449 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1450 ((u32) scp->cmnd[3] << 16) |
1451 ((u32) scp->cmnd[4] << 8) |
1452 (u32) scp->cmnd[5]);
1456 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1458 else if (scp->cmd_len == 16) {
1459 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1460 ((u32) scp->cmnd[11] << 16) |
1461 ((u32) scp->cmnd[12] << 8) |
1462 (u32) scp->cmnd[13]);
1464 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1465 ((u32) scp->cmnd[7] << 16) |
1466 ((u32) scp->cmnd[8] << 8) |
1467 (u32) scp->cmnd[9]);
1469 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1470 ((u32) scp->cmnd[3] << 16) |
1471 ((u32) scp->cmnd[4] << 8) |
1472 (u32) scp->cmnd[5]);
1479 if (instance->flag_ieee) {
1480 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1481 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1483 } else if (IS_DMA64) {
1484 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1485 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1487 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1489 if (ldio->sge_count > instance->max_num_sge) {
1490 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1496 * Sense info specific
1498 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1499 ldio->sense_buf_phys_addr_hi = 0;
1500 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1503 * Compute the total number of frames this command consumes. FW uses
1504 * this number to pull sufficient number of frames from host memory.
1506 cmd->frame_count = megasas_get_frame_count(instance,
1507 ldio->sge_count, IO_FRAME);
1509 return cmd->frame_count;
1513 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1514 * and whether it's RW or non RW
1515 * @scmd: SCSI command
1518 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1522 switch (cmd->cmnd[0]) {
1531 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1532 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1535 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1536 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1542 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1544 * @instance: Adapter soft state
1547 megasas_dump_pending_frames(struct megasas_instance *instance)
1549 struct megasas_cmd *cmd;
1551 union megasas_sgl *mfi_sgl;
1552 struct megasas_io_frame *ldio;
1553 struct megasas_pthru_frame *pthru;
1555 u16 max_cmd = instance->max_fw_cmds;
1557 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1558 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1560 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1562 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1564 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1565 for (i = 0; i < max_cmd; i++) {
1566 cmd = instance->cmd_list[i];
1569 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1570 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1571 ldio = (struct megasas_io_frame *)cmd->frame;
1572 mfi_sgl = &ldio->sgl;
1573 sgcount = ldio->sge_count;
1574 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1575 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1576 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1577 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1578 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1580 pthru = (struct megasas_pthru_frame *) cmd->frame;
1581 mfi_sgl = &pthru->sgl;
1582 sgcount = pthru->sge_count;
1583 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1584 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1585 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1586 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1587 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1589 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1590 for (n = 0; n < sgcount; n++) {
1592 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1593 le32_to_cpu(mfi_sgl->sge64[n].length),
1594 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1596 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1597 le32_to_cpu(mfi_sgl->sge32[n].length),
1598 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1602 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1603 for (i = 0; i < max_cmd; i++) {
1605 cmd = instance->cmd_list[i];
1607 if (cmd->sync_cmd == 1)
1608 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1610 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1614 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1615 struct scsi_cmnd *scmd)
1617 struct megasas_cmd *cmd;
1620 cmd = megasas_get_cmd(instance);
1622 return SCSI_MLQUEUE_HOST_BUSY;
1625 * Logical drive command
1627 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1628 frame_count = megasas_build_ldio(instance, scmd, cmd);
1630 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1633 goto out_return_cmd;
1636 scmd->SCp.ptr = (char *)cmd;
1639 * Issue the command to the FW
1641 atomic_inc(&instance->fw_outstanding);
1643 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1644 cmd->frame_count-1, instance->reg_set);
1648 megasas_return_cmd(instance, cmd);
1649 return SCSI_MLQUEUE_HOST_BUSY;
1654 * megasas_queue_command - Queue entry point
1655 * @scmd: SCSI command to be queued
1656 * @done: Callback entry point
1659 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1661 struct megasas_instance *instance;
1662 struct MR_PRIV_DEVICE *mr_device_priv_data;
1664 instance = (struct megasas_instance *)
1665 scmd->device->host->hostdata;
1667 if (instance->unload == 1) {
1668 scmd->result = DID_NO_CONNECT << 16;
1669 scmd->scsi_done(scmd);
1673 if (instance->issuepend_done == 0)
1674 return SCSI_MLQUEUE_HOST_BUSY;
1677 /* Check for an mpio path and adjust behavior */
1678 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1679 if (megasas_check_mpio_paths(instance, scmd) ==
1680 (DID_REQUEUE << 16)) {
1681 return SCSI_MLQUEUE_HOST_BUSY;
1683 scmd->result = DID_NO_CONNECT << 16;
1684 scmd->scsi_done(scmd);
1689 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1690 scmd->result = DID_NO_CONNECT << 16;
1691 scmd->scsi_done(scmd);
1695 mr_device_priv_data = scmd->device->hostdata;
1696 if (!mr_device_priv_data) {
1697 scmd->result = DID_NO_CONNECT << 16;
1698 scmd->scsi_done(scmd);
1702 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1703 return SCSI_MLQUEUE_HOST_BUSY;
1705 if (mr_device_priv_data->tm_busy)
1706 return SCSI_MLQUEUE_DEVICE_BUSY;
1711 if (MEGASAS_IS_LOGICAL(scmd->device) &&
1712 (scmd->device->id >= instance->fw_supported_vd_count ||
1713 scmd->device->lun)) {
1714 scmd->result = DID_BAD_TARGET << 16;
1718 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1719 MEGASAS_IS_LOGICAL(scmd->device) &&
1720 (!instance->fw_sync_cache_support)) {
1721 scmd->result = DID_OK << 16;
1725 return instance->instancet->build_and_issue_cmd(instance, scmd);
1728 scmd->scsi_done(scmd);
1732 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1736 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1738 if ((megasas_mgmt_info.instance[i]) &&
1739 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1740 return megasas_mgmt_info.instance[i];
1747 * megasas_set_dynamic_target_properties -
1748 * Device property set by driver may not be static and it is required to be
1752 * set dma alignment (only for eedp protection enable vd).
1754 * @sdev: OS provided scsi device
1758 void megasas_set_dynamic_target_properties(struct scsi_device *sdev)
1760 u16 pd_index = 0, ld;
1762 struct megasas_instance *instance;
1763 struct fusion_context *fusion;
1764 struct MR_PRIV_DEVICE *mr_device_priv_data;
1765 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1766 struct MR_LD_RAID *raid;
1767 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1769 instance = megasas_lookup_instance(sdev->host->host_no);
1770 fusion = instance->ctrl_context;
1771 mr_device_priv_data = sdev->hostdata;
1773 if (!fusion || !mr_device_priv_data)
1776 if (MEGASAS_IS_LOGICAL(sdev)) {
1777 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1779 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1780 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1781 if (ld >= instance->fw_supported_vd_count)
1783 raid = MR_LdRaidGet(ld, local_map_ptr);
1785 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1786 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1788 mr_device_priv_data->is_tm_capable =
1789 raid->capability.tmCapable;
1790 } else if (instance->use_seqnum_jbod_fp) {
1791 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1793 pd_sync = (void *)fusion->pd_seq_sync
1794 [(instance->pd_seq_map_id - 1) & 1];
1795 mr_device_priv_data->is_tm_capable =
1796 pd_sync->seq[pd_index].capability.tmCapable;
1801 * megasas_set_nvme_device_properties -
1803 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1804 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1806 * MR firmware provides value in KB. Caller of this function converts
1809 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1810 * MR firmware provides value 128 as (32 * 4K) = 128K.
1812 * @sdev: scsi device
1813 * @max_io_size: maximum io transfer size
1817 megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
1819 struct megasas_instance *instance;
1820 u32 mr_nvme_pg_size;
1822 instance = (struct megasas_instance *)sdev->host->hostdata;
1823 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1824 MR_DEFAULT_NVME_PAGE_SIZE);
1826 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1828 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1829 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1834 * megasas_set_static_target_properties -
1835 * Device property set by driver are static and it is not required to be
1836 * updated after OCR.
1839 * set device queue depth
1840 * set nvme device properties. see - megasas_set_nvme_device_properties
1842 * @sdev: scsi device
1843 * @is_target_prop true, if fw provided target properties.
1845 static void megasas_set_static_target_properties(struct scsi_device *sdev,
1846 bool is_target_prop)
1848 u16 target_index = 0;
1850 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1851 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
1853 struct megasas_instance *instance;
1854 struct MR_PRIV_DEVICE *mr_device_priv_data;
1856 instance = megasas_lookup_instance(sdev->host->host_no);
1857 mr_device_priv_data = sdev->hostdata;
1858 interface_type = mr_device_priv_data->interface_type;
1861 * The RAID firmware may require extended timeouts.
1863 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
1865 target_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
1867 switch (interface_type) {
1869 device_qd = MEGASAS_SAS_QD;
1872 device_qd = MEGASAS_SATA_QD;
1875 device_qd = MEGASAS_NVME_QD;
1879 if (is_target_prop) {
1880 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1881 if (tgt_device_qd &&
1882 (tgt_device_qd <= instance->host->can_queue))
1883 device_qd = tgt_device_qd;
1885 /* max_io_size_kb will be set to non zero for
1886 * nvme based vd and syspd.
1888 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1891 if (instance->nvme_page_size && max_io_size_kb)
1892 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
1894 scsi_change_queue_depth(sdev, device_qd);
1899 static int megasas_slave_configure(struct scsi_device *sdev)
1902 struct megasas_instance *instance;
1903 int ret_target_prop = DCMD_FAILED;
1904 bool is_target_prop = false;
1906 instance = megasas_lookup_instance(sdev->host->host_no);
1907 if (instance->pd_list_not_supported) {
1908 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
1909 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1911 if (instance->pd_list[pd_index].driveState !=
1917 mutex_lock(&instance->hba_mutex);
1918 /* Send DCMD to Firmware and cache the information */
1919 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
1920 megasas_get_pd_info(instance, sdev);
1922 /* Some ventura firmware may not have instance->nvme_page_size set.
1923 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
1925 if ((instance->tgt_prop) && (instance->nvme_page_size))
1926 ret_target_prop = megasas_get_target_prop(instance, sdev);
1928 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
1929 megasas_set_static_target_properties(sdev, is_target_prop);
1931 mutex_unlock(&instance->hba_mutex);
1933 /* This sdev property may change post OCR */
1934 megasas_set_dynamic_target_properties(sdev);
1939 static int megasas_slave_alloc(struct scsi_device *sdev)
1942 struct megasas_instance *instance ;
1943 struct MR_PRIV_DEVICE *mr_device_priv_data;
1945 instance = megasas_lookup_instance(sdev->host->host_no);
1946 if (!MEGASAS_IS_LOGICAL(sdev)) {
1948 * Open the OS scan to the SYSTEM PD
1951 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1953 if ((instance->pd_list_not_supported ||
1954 instance->pd_list[pd_index].driveState ==
1955 MR_PD_STATE_SYSTEM)) {
1962 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
1964 if (!mr_device_priv_data)
1966 sdev->hostdata = mr_device_priv_data;
1968 atomic_set(&mr_device_priv_data->r1_ldio_hint,
1969 instance->r1_ldio_hint_default);
1973 static void megasas_slave_destroy(struct scsi_device *sdev)
1975 kfree(sdev->hostdata);
1976 sdev->hostdata = NULL;
1980 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1982 * @instance: Adapter soft state
1985 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
1988 struct megasas_cmd *cmd_mfi;
1989 struct megasas_cmd_fusion *cmd_fusion;
1990 struct fusion_context *fusion = instance->ctrl_context;
1992 /* Find all outstanding ioctls */
1994 for (i = 0; i < instance->max_fw_cmds; i++) {
1995 cmd_fusion = fusion->cmd_list[i];
1996 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1997 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1998 if (cmd_mfi->sync_cmd &&
1999 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
2000 megasas_complete_cmd(instance,
2005 for (i = 0; i < instance->max_fw_cmds; i++) {
2006 cmd_mfi = instance->cmd_list[i];
2007 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2009 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2015 void megaraid_sas_kill_hba(struct megasas_instance *instance)
2017 /* Set critical error to block I/O & ioctls in case caller didn't */
2018 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2019 /* Wait 1 second to ensure IO or ioctls in build have posted */
2021 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2022 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2023 (instance->ctrl_context)) {
2024 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2026 readl(&instance->reg_set->doorbell);
2027 if (instance->requestorId && instance->peerIsPresent)
2028 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2030 writel(MFI_STOP_ADP,
2031 &instance->reg_set->inbound_doorbell);
2033 /* Complete outstanding ioctls when adapter is killed */
2034 megasas_complete_outstanding_ioctls(instance);
2038 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2039 * restored to max value
2040 * @instance: Adapter soft state
2044 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2046 unsigned long flags;
2048 if (instance->flag & MEGASAS_FW_BUSY
2049 && time_after(jiffies, instance->last_time + 5 * HZ)
2050 && atomic_read(&instance->fw_outstanding) <
2051 instance->throttlequeuedepth + 1) {
2053 spin_lock_irqsave(instance->host->host_lock, flags);
2054 instance->flag &= ~MEGASAS_FW_BUSY;
2056 instance->host->can_queue = instance->cur_can_queue;
2057 spin_unlock_irqrestore(instance->host->host_lock, flags);
2062 * megasas_complete_cmd_dpc - Returns FW's controller structure
2063 * @instance_addr: Address of adapter soft state
2065 * Tasklet to complete cmds
2067 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2072 struct megasas_cmd *cmd;
2073 struct megasas_instance *instance =
2074 (struct megasas_instance *)instance_addr;
2075 unsigned long flags;
2077 /* If we have already declared adapter dead, donot complete cmds */
2078 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2081 spin_lock_irqsave(&instance->completion_lock, flags);
2083 producer = le32_to_cpu(*instance->producer);
2084 consumer = le32_to_cpu(*instance->consumer);
2086 while (consumer != producer) {
2087 context = le32_to_cpu(instance->reply_queue[consumer]);
2088 if (context >= instance->max_fw_cmds) {
2089 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2094 cmd = instance->cmd_list[context];
2096 megasas_complete_cmd(instance, cmd, DID_OK);
2099 if (consumer == (instance->max_fw_cmds + 1)) {
2104 *instance->consumer = cpu_to_le32(producer);
2106 spin_unlock_irqrestore(&instance->completion_lock, flags);
2109 * Check if we can restore can_queue
2111 megasas_check_and_restore_queue_depth(instance);
2115 * megasas_start_timer - Initializes a timer object
2116 * @instance: Adapter soft state
2117 * @timer: timer object to be initialized
2118 * @fn: timer function
2119 * @interval: time interval between timer function call
2122 void megasas_start_timer(struct megasas_instance *instance,
2123 struct timer_list *timer,
2124 void *fn, unsigned long interval)
2127 timer->expires = jiffies + interval;
2128 timer->data = (unsigned long)instance;
2129 timer->function = fn;
2134 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2137 process_fw_state_change_wq(struct work_struct *work);
2139 void megasas_do_ocr(struct megasas_instance *instance)
2141 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2142 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2143 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2144 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2146 instance->instancet->disable_intr(instance);
2147 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2148 instance->issuepend_done = 0;
2150 atomic_set(&instance->fw_outstanding, 0);
2151 megasas_internal_reset_defer_cmds(instance);
2152 process_fw_state_change_wq(&instance->work_init);
2155 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2158 struct megasas_cmd *cmd;
2159 struct megasas_dcmd_frame *dcmd;
2160 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2161 dma_addr_t new_affiliation_111_h;
2165 cmd = megasas_get_cmd(instance);
2168 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2169 "Failed to get cmd for scsi%d\n",
2170 instance->host->host_no);
2174 dcmd = &cmd->frame->dcmd;
2176 if (!instance->vf_affiliation_111) {
2177 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2178 "affiliation for scsi%d\n", instance->host->host_no);
2179 megasas_return_cmd(instance, cmd);
2184 memset(instance->vf_affiliation_111, 0,
2185 sizeof(struct MR_LD_VF_AFFILIATION_111));
2187 new_affiliation_111 =
2188 pci_alloc_consistent(instance->pdev,
2189 sizeof(struct MR_LD_VF_AFFILIATION_111),
2190 &new_affiliation_111_h);
2191 if (!new_affiliation_111) {
2192 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2193 "memory for new affiliation for scsi%d\n",
2194 instance->host->host_no);
2195 megasas_return_cmd(instance, cmd);
2198 memset(new_affiliation_111, 0,
2199 sizeof(struct MR_LD_VF_AFFILIATION_111));
2202 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2204 dcmd->cmd = MFI_CMD_DCMD;
2205 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2206 dcmd->sge_count = 1;
2207 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2210 dcmd->data_xfer_len =
2211 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2212 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2215 dcmd->sgl.sge32[0].phys_addr =
2216 cpu_to_le32(instance->vf_affiliation_111_h);
2218 dcmd->sgl.sge32[0].phys_addr =
2219 cpu_to_le32(new_affiliation_111_h);
2221 dcmd->sgl.sge32[0].length = cpu_to_le32(
2222 sizeof(struct MR_LD_VF_AFFILIATION_111));
2224 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2225 "scsi%d\n", instance->host->host_no);
2227 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2228 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2229 " failed with status 0x%x for scsi%d\n",
2230 dcmd->cmd_status, instance->host->host_no);
2231 retval = 1; /* Do a scan if we couldn't get affiliation */
2236 thisVf = new_affiliation_111->thisVf;
2237 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2238 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2239 new_affiliation_111->map[ld].policy[thisVf]) {
2240 dev_warn(&instance->pdev->dev, "SR-IOV: "
2241 "Got new LD/VF affiliation for scsi%d\n",
2242 instance->host->host_no);
2243 memcpy(instance->vf_affiliation_111,
2244 new_affiliation_111,
2245 sizeof(struct MR_LD_VF_AFFILIATION_111));
2251 if (new_affiliation_111) {
2252 pci_free_consistent(instance->pdev,
2253 sizeof(struct MR_LD_VF_AFFILIATION_111),
2254 new_affiliation_111,
2255 new_affiliation_111_h);
2258 megasas_return_cmd(instance, cmd);
2263 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2266 struct megasas_cmd *cmd;
2267 struct megasas_dcmd_frame *dcmd;
2268 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2269 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2270 dma_addr_t new_affiliation_h;
2271 int i, j, retval = 0, found = 0, doscan = 0;
2274 cmd = megasas_get_cmd(instance);
2277 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2278 "Failed to get cmd for scsi%d\n",
2279 instance->host->host_no);
2283 dcmd = &cmd->frame->dcmd;
2285 if (!instance->vf_affiliation) {
2286 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2287 "affiliation for scsi%d\n", instance->host->host_no);
2288 megasas_return_cmd(instance, cmd);
2293 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2294 sizeof(struct MR_LD_VF_AFFILIATION));
2297 pci_alloc_consistent(instance->pdev,
2298 (MAX_LOGICAL_DRIVES + 1) *
2299 sizeof(struct MR_LD_VF_AFFILIATION),
2300 &new_affiliation_h);
2301 if (!new_affiliation) {
2302 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2303 "memory for new affiliation for scsi%d\n",
2304 instance->host->host_no);
2305 megasas_return_cmd(instance, cmd);
2308 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2309 sizeof(struct MR_LD_VF_AFFILIATION));
2312 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2314 dcmd->cmd = MFI_CMD_DCMD;
2315 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2316 dcmd->sge_count = 1;
2317 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2320 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2321 sizeof(struct MR_LD_VF_AFFILIATION));
2322 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2325 dcmd->sgl.sge32[0].phys_addr =
2326 cpu_to_le32(instance->vf_affiliation_h);
2328 dcmd->sgl.sge32[0].phys_addr =
2329 cpu_to_le32(new_affiliation_h);
2331 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2332 sizeof(struct MR_LD_VF_AFFILIATION));
2334 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2335 "scsi%d\n", instance->host->host_no);
2338 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2339 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2340 " failed with status 0x%x for scsi%d\n",
2341 dcmd->cmd_status, instance->host->host_no);
2342 retval = 1; /* Do a scan if we couldn't get affiliation */
2347 if (!new_affiliation->ldCount) {
2348 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2349 "affiliation for passive path for scsi%d\n",
2350 instance->host->host_no);
2354 newmap = new_affiliation->map;
2355 savedmap = instance->vf_affiliation->map;
2356 thisVf = new_affiliation->thisVf;
2357 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2359 for (j = 0; j < instance->vf_affiliation->ldCount;
2361 if (newmap->ref.targetId ==
2362 savedmap->ref.targetId) {
2364 if (newmap->policy[thisVf] !=
2365 savedmap->policy[thisVf]) {
2370 savedmap = (struct MR_LD_VF_MAP *)
2371 ((unsigned char *)savedmap +
2374 if (!found && newmap->policy[thisVf] !=
2375 MR_LD_ACCESS_HIDDEN) {
2379 newmap = (struct MR_LD_VF_MAP *)
2380 ((unsigned char *)newmap + newmap->size);
2383 newmap = new_affiliation->map;
2384 savedmap = instance->vf_affiliation->map;
2386 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2388 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2389 if (savedmap->ref.targetId ==
2390 newmap->ref.targetId) {
2392 if (savedmap->policy[thisVf] !=
2393 newmap->policy[thisVf]) {
2398 newmap = (struct MR_LD_VF_MAP *)
2399 ((unsigned char *)newmap +
2402 if (!found && savedmap->policy[thisVf] !=
2403 MR_LD_ACCESS_HIDDEN) {
2407 savedmap = (struct MR_LD_VF_MAP *)
2408 ((unsigned char *)savedmap +
2414 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2415 "affiliation for scsi%d\n", instance->host->host_no);
2416 memcpy(instance->vf_affiliation, new_affiliation,
2417 new_affiliation->size);
2421 if (new_affiliation)
2422 pci_free_consistent(instance->pdev,
2423 (MAX_LOGICAL_DRIVES + 1) *
2424 sizeof(struct MR_LD_VF_AFFILIATION),
2425 new_affiliation, new_affiliation_h);
2426 megasas_return_cmd(instance, cmd);
2431 /* This function will get the current SR-IOV LD/VF affiliation */
2432 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2437 if (instance->PlasmaFW111)
2438 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2440 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2444 /* This function will tell FW to start the SR-IOV heartbeat */
2445 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2448 struct megasas_cmd *cmd;
2449 struct megasas_dcmd_frame *dcmd;
2452 cmd = megasas_get_cmd(instance);
2455 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2456 "Failed to get cmd for scsi%d\n",
2457 instance->host->host_no);
2461 dcmd = &cmd->frame->dcmd;
2464 instance->hb_host_mem =
2465 pci_zalloc_consistent(instance->pdev,
2466 sizeof(struct MR_CTRL_HB_HOST_MEM),
2467 &instance->hb_host_mem_h);
2468 if (!instance->hb_host_mem) {
2469 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2470 " memory for heartbeat host memory for scsi%d\n",
2471 instance->host->host_no);
2477 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2479 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2480 dcmd->cmd = MFI_CMD_DCMD;
2481 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2482 dcmd->sge_count = 1;
2483 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2486 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2487 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2488 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2489 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2491 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2492 instance->host->host_no);
2494 if (instance->ctrl_context && !instance->mask_interrupts)
2495 retval = megasas_issue_blocked_cmd(instance, cmd,
2496 MEGASAS_ROUTINE_WAIT_TIME_VF);
2498 retval = megasas_issue_polled(instance, cmd);
2501 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2502 "_MEM_ALLOC DCMD %s for scsi%d\n",
2503 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2504 "timed out" : "failed", instance->host->host_no);
2509 megasas_return_cmd(instance, cmd);
2514 /* Handler for SR-IOV heartbeat */
2515 void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2517 struct megasas_instance *instance =
2518 (struct megasas_instance *)instance_addr;
2520 if (instance->hb_host_mem->HB.fwCounter !=
2521 instance->hb_host_mem->HB.driverCounter) {
2522 instance->hb_host_mem->HB.driverCounter =
2523 instance->hb_host_mem->HB.fwCounter;
2524 mod_timer(&instance->sriov_heartbeat_timer,
2525 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2527 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2528 "completed for scsi%d\n", instance->host->host_no);
2529 schedule_work(&instance->work_init);
2534 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2535 * @instance: Adapter soft state
2537 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2538 * complete all its outstanding commands. Returns error if one or more IOs
2539 * are pending after this time period. It also marks the controller dead.
2541 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2543 int i, sl, outstanding;
2545 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2546 unsigned long flags;
2547 struct list_head clist_local;
2548 struct megasas_cmd *reset_cmd;
2551 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2552 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2553 __func__, __LINE__);
2557 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2559 INIT_LIST_HEAD(&clist_local);
2560 spin_lock_irqsave(&instance->hba_lock, flags);
2561 list_splice_init(&instance->internal_reset_pending_q,
2563 spin_unlock_irqrestore(&instance->hba_lock, flags);
2565 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2566 for (i = 0; i < wait_time; i++) {
2568 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2572 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2573 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2574 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2579 while (!list_empty(&clist_local)) {
2580 reset_cmd = list_entry((&clist_local)->next,
2581 struct megasas_cmd, list);
2582 list_del_init(&reset_cmd->list);
2583 if (reset_cmd->scmd) {
2584 reset_cmd->scmd->result = DID_REQUEUE << 16;
2585 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2586 reset_index, reset_cmd,
2587 reset_cmd->scmd->cmnd[0]);
2589 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2590 megasas_return_cmd(instance, reset_cmd);
2591 } else if (reset_cmd->sync_cmd) {
2592 dev_notice(&instance->pdev->dev, "%p synch cmds"
2596 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2597 instance->instancet->fire_cmd(instance,
2598 reset_cmd->frame_phys_addr,
2599 0, instance->reg_set);
2601 dev_notice(&instance->pdev->dev, "%p unexpected"
2611 for (i = 0; i < resetwaittime; i++) {
2612 outstanding = atomic_read(&instance->fw_outstanding);
2617 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2618 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2619 "commands to complete\n",i,outstanding);
2621 * Call cmd completion routine. Cmd to be
2622 * be completed directly without depending on isr.
2624 megasas_complete_cmd_dpc((unsigned long)instance);
2631 outstanding = atomic_read(&instance->fw_outstanding);
2632 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2634 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2635 goto no_outstanding;
2637 if (instance->disableOnlineCtrlReset)
2638 goto kill_hba_and_failed;
2640 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2641 dev_info(&instance->pdev->dev,
2642 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2643 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2645 goto kill_hba_and_failed;
2646 megasas_do_ocr(instance);
2648 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2649 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2650 __func__, __LINE__);
2653 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2654 __func__, __LINE__);
2656 for (sl = 0; sl < 10; sl++)
2659 outstanding = atomic_read(&instance->fw_outstanding);
2661 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2662 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2663 goto no_outstanding;
2670 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2671 __func__, __LINE__);
2674 kill_hba_and_failed:
2676 /* Reset not supported, kill adapter */
2677 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2678 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2679 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2680 atomic_read(&instance->fw_outstanding));
2681 megasas_dump_pending_frames(instance);
2682 megaraid_sas_kill_hba(instance);
2688 * megasas_generic_reset - Generic reset routine
2689 * @scmd: Mid-layer SCSI command
2691 * This routine implements a generic reset handler for device, bus and host
2692 * reset requests. Device, bus and host specific reset handlers can use this
2693 * function after they do their specific tasks.
2695 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2698 struct megasas_instance *instance;
2700 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2702 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2703 scmd->cmnd[0], scmd->retries);
2705 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2706 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2710 ret_val = megasas_wait_for_outstanding(instance);
2711 if (ret_val == SUCCESS)
2712 dev_notice(&instance->pdev->dev, "reset successful\n");
2714 dev_err(&instance->pdev->dev, "failed to do reset\n");
2720 * megasas_reset_timer - quiesce the adapter if required
2723 * Sets the FW busy flag and reduces the host->can_queue if the
2724 * cmd has not been completed within the timeout period.
2727 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2729 struct megasas_instance *instance;
2730 unsigned long flags;
2732 if (time_after(jiffies, scmd->jiffies_at_alloc +
2733 (scmd_timeout * 2) * HZ)) {
2734 return BLK_EH_NOT_HANDLED;
2737 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2738 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2739 /* FW is busy, throttle IO */
2740 spin_lock_irqsave(instance->host->host_lock, flags);
2742 instance->host->can_queue = instance->throttlequeuedepth;
2743 instance->last_time = jiffies;
2744 instance->flag |= MEGASAS_FW_BUSY;
2746 spin_unlock_irqrestore(instance->host->host_lock, flags);
2748 return BLK_EH_RESET_TIMER;
2752 * megasas_dump_frame - This function will dump MPT/MFI frame
2755 megasas_dump_frame(void *mpi_request, int sz)
2758 __le32 *mfp = (__le32 *)mpi_request;
2760 printk(KERN_INFO "IO request frame:\n\t");
2761 for (i = 0; i < sz / sizeof(__le32); i++) {
2762 if (i && ((i % 8) == 0))
2764 printk("%08x ", le32_to_cpu(mfp[i]));
2770 * megasas_reset_bus_host - Bus & host reset handler entry point
2772 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2775 struct megasas_instance *instance;
2777 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2779 scmd_printk(KERN_INFO, scmd,
2780 "Controller reset is requested due to IO timeout\n"
2781 "SCSI command pointer: (%p)\t SCSI host state: %d\t"
2782 " SCSI host busy: %d\t FW outstanding: %d\n",
2783 scmd, scmd->device->host->shost_state,
2784 atomic_read((atomic_t *)&scmd->device->host->host_busy),
2785 atomic_read(&instance->fw_outstanding));
2788 * First wait for all commands to complete
2790 if (instance->ctrl_context) {
2791 struct megasas_cmd_fusion *cmd;
2792 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2794 megasas_dump_frame(cmd->io_request,
2795 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
2796 ret = megasas_reset_fusion(scmd->device->host,
2797 SCSIIO_TIMEOUT_OCR);
2799 ret = megasas_generic_reset(scmd);
2805 * megasas_task_abort - Issues task abort request to firmware
2806 * (supported only for fusion adapters)
2807 * @scmd: SCSI command pointer
2809 static int megasas_task_abort(struct scsi_cmnd *scmd)
2812 struct megasas_instance *instance;
2814 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2816 if (instance->ctrl_context)
2817 ret = megasas_task_abort_fusion(scmd);
2819 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2827 * megasas_reset_target: Issues target reset request to firmware
2828 * (supported only for fusion adapters)
2829 * @scmd: SCSI command pointer
2831 static int megasas_reset_target(struct scsi_cmnd *scmd)
2834 struct megasas_instance *instance;
2836 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2838 if (instance->ctrl_context)
2839 ret = megasas_reset_target_fusion(scmd);
2841 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2849 * megasas_bios_param - Returns disk geometry for a disk
2850 * @sdev: device handle
2851 * @bdev: block device
2852 * @capacity: drive capacity
2853 * @geom: geometry parameters
2856 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2857 sector_t capacity, int geom[])
2864 /* Default heads (64) & sectors (32) */
2868 tmp = heads * sectors;
2869 cylinders = capacity;
2871 sector_div(cylinders, tmp);
2874 * Handle extended translation size for logical drives > 1Gb
2877 if (capacity >= 0x200000) {
2880 tmp = heads*sectors;
2881 cylinders = capacity;
2882 sector_div(cylinders, tmp);
2887 geom[2] = cylinders;
2892 static void megasas_aen_polling(struct work_struct *work);
2895 * megasas_service_aen - Processes an event notification
2896 * @instance: Adapter soft state
2897 * @cmd: AEN command completed by the ISR
2899 * For AEN, driver sends a command down to FW that is held by the FW till an
2900 * event occurs. When an event of interest occurs, FW completes the command
2901 * that it was previously holding.
2903 * This routines sends SIGIO signal to processes that have registered with the
2907 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2909 unsigned long flags;
2912 * Don't signal app if it is just an aborted previously registered aen
2914 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2915 spin_lock_irqsave(&poll_aen_lock, flags);
2916 megasas_poll_wait_aen = 1;
2917 spin_unlock_irqrestore(&poll_aen_lock, flags);
2918 wake_up(&megasas_poll_wait);
2919 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2924 instance->aen_cmd = NULL;
2926 megasas_return_cmd(instance, cmd);
2928 if ((instance->unload == 0) &&
2929 ((instance->issuepend_done == 1))) {
2930 struct megasas_aen_event *ev;
2932 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2934 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
2936 ev->instance = instance;
2938 INIT_DELAYED_WORK(&ev->hotplug_work,
2939 megasas_aen_polling);
2940 schedule_delayed_work(&ev->hotplug_work, 0);
2946 megasas_fw_crash_buffer_store(struct device *cdev,
2947 struct device_attribute *attr, const char *buf, size_t count)
2949 struct Scsi_Host *shost = class_to_shost(cdev);
2950 struct megasas_instance *instance =
2951 (struct megasas_instance *) shost->hostdata;
2953 unsigned long flags;
2955 if (kstrtoint(buf, 0, &val) != 0)
2958 spin_lock_irqsave(&instance->crashdump_lock, flags);
2959 instance->fw_crash_buffer_offset = val;
2960 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2965 megasas_fw_crash_buffer_show(struct device *cdev,
2966 struct device_attribute *attr, char *buf)
2968 struct Scsi_Host *shost = class_to_shost(cdev);
2969 struct megasas_instance *instance =
2970 (struct megasas_instance *) shost->hostdata;
2972 unsigned long buff_addr;
2973 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2974 unsigned long src_addr;
2975 unsigned long flags;
2978 spin_lock_irqsave(&instance->crashdump_lock, flags);
2979 buff_offset = instance->fw_crash_buffer_offset;
2980 if (!instance->crash_dump_buf &&
2981 !((instance->fw_crash_state == AVAILABLE) ||
2982 (instance->fw_crash_state == COPYING))) {
2983 dev_err(&instance->pdev->dev,
2984 "Firmware crash dump is not available\n");
2985 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2989 buff_addr = (unsigned long) buf;
2991 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
2992 dev_err(&instance->pdev->dev,
2993 "Firmware crash dump offset is out of range\n");
2994 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2998 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2999 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3001 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3002 (buff_offset % dmachunk);
3003 memcpy(buf, (void *)src_addr, size);
3004 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3010 megasas_fw_crash_buffer_size_show(struct device *cdev,
3011 struct device_attribute *attr, char *buf)
3013 struct Scsi_Host *shost = class_to_shost(cdev);
3014 struct megasas_instance *instance =
3015 (struct megasas_instance *) shost->hostdata;
3017 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3018 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3022 megasas_fw_crash_state_store(struct device *cdev,
3023 struct device_attribute *attr, const char *buf, size_t count)
3025 struct Scsi_Host *shost = class_to_shost(cdev);
3026 struct megasas_instance *instance =
3027 (struct megasas_instance *) shost->hostdata;
3029 unsigned long flags;
3031 if (kstrtoint(buf, 0, &val) != 0)
3034 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3035 dev_err(&instance->pdev->dev, "application updates invalid "
3036 "firmware crash state\n");
3040 instance->fw_crash_state = val;
3042 if ((val == COPIED) || (val == COPY_ERROR)) {
3043 spin_lock_irqsave(&instance->crashdump_lock, flags);
3044 megasas_free_host_crash_buffer(instance);
3045 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3046 if (val == COPY_ERROR)
3047 dev_info(&instance->pdev->dev, "application failed to "
3048 "copy Firmware crash dump\n");
3050 dev_info(&instance->pdev->dev, "Firmware crash dump "
3051 "copied successfully\n");
3057 megasas_fw_crash_state_show(struct device *cdev,
3058 struct device_attribute *attr, char *buf)
3060 struct Scsi_Host *shost = class_to_shost(cdev);
3061 struct megasas_instance *instance =
3062 (struct megasas_instance *) shost->hostdata;
3064 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3068 megasas_page_size_show(struct device *cdev,
3069 struct device_attribute *attr, char *buf)
3071 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3075 megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3078 struct Scsi_Host *shost = class_to_shost(cdev);
3079 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3081 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3084 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3085 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3086 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3087 megasas_fw_crash_buffer_size_show, NULL);
3088 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3089 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3090 static DEVICE_ATTR(page_size, S_IRUGO,
3091 megasas_page_size_show, NULL);
3092 static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3093 megasas_ldio_outstanding_show, NULL);
3095 struct device_attribute *megaraid_host_attrs[] = {
3096 &dev_attr_fw_crash_buffer_size,
3097 &dev_attr_fw_crash_buffer,
3098 &dev_attr_fw_crash_state,
3099 &dev_attr_page_size,
3100 &dev_attr_ldio_outstanding,
3105 * Scsi host template for megaraid_sas driver
3107 static struct scsi_host_template megasas_template = {
3109 .module = THIS_MODULE,
3110 .name = "Avago SAS based MegaRAID driver",
3111 .proc_name = "megaraid_sas",
3112 .slave_configure = megasas_slave_configure,
3113 .slave_alloc = megasas_slave_alloc,
3114 .slave_destroy = megasas_slave_destroy,
3115 .queuecommand = megasas_queue_command,
3116 .eh_target_reset_handler = megasas_reset_target,
3117 .eh_abort_handler = megasas_task_abort,
3118 .eh_host_reset_handler = megasas_reset_bus_host,
3119 .eh_timed_out = megasas_reset_timer,
3120 .shost_attrs = megaraid_host_attrs,
3121 .bios_param = megasas_bios_param,
3122 .use_clustering = ENABLE_CLUSTERING,
3123 .change_queue_depth = scsi_change_queue_depth,
3128 * megasas_complete_int_cmd - Completes an internal command
3129 * @instance: Adapter soft state
3130 * @cmd: Command to be completed
3132 * The megasas_issue_blocked_cmd() function waits for a command to complete
3133 * after it issues a command. This function wakes up that waiting routine by
3134 * calling wake_up() on the wait queue.
3137 megasas_complete_int_cmd(struct megasas_instance *instance,
3138 struct megasas_cmd *cmd)
3140 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
3141 wake_up(&instance->int_cmd_wait_q);
3145 * megasas_complete_abort - Completes aborting a command
3146 * @instance: Adapter soft state
3147 * @cmd: Cmd that was issued to abort another cmd
3149 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3150 * after it issues an abort on a previously issued command. This function
3151 * wakes up all functions waiting on the same wait queue.
3154 megasas_complete_abort(struct megasas_instance *instance,
3155 struct megasas_cmd *cmd)
3157 if (cmd->sync_cmd) {
3159 cmd->cmd_status_drv = 0;
3160 wake_up(&instance->abort_cmd_wait_q);
3165 * megasas_complete_cmd - Completes a command
3166 * @instance: Adapter soft state
3167 * @cmd: Command to be completed
3168 * @alt_status: If non-zero, use this value as status to
3169 * SCSI mid-layer instead of the value returned
3170 * by the FW. This should be used if caller wants
3171 * an alternate status (as in the case of aborted
3175 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3179 struct megasas_header *hdr = &cmd->frame->hdr;
3180 unsigned long flags;
3181 struct fusion_context *fusion = instance->ctrl_context;
3184 /* flag for the retry reset */
3185 cmd->retry_for_fw_reset = 0;
3188 cmd->scmd->SCp.ptr = NULL;
3191 case MFI_CMD_INVALID:
3192 /* Some older 1068 controller FW may keep a pended
3193 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3194 when booting the kdump kernel. Ignore this command to
3195 prevent a kernel panic on shutdown of the kdump kernel. */
3196 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3198 dev_warn(&instance->pdev->dev, "If you have a controller "
3199 "other than PERC5, please upgrade your firmware\n");
3201 case MFI_CMD_PD_SCSI_IO:
3202 case MFI_CMD_LD_SCSI_IO:
3205 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3206 * issued either through an IO path or an IOCTL path. If it
3207 * was via IOCTL, we will send it to internal completion.
3209 if (cmd->sync_cmd) {
3211 megasas_complete_int_cmd(instance, cmd);
3215 case MFI_CMD_LD_READ:
3216 case MFI_CMD_LD_WRITE:
3219 cmd->scmd->result = alt_status << 16;
3225 atomic_dec(&instance->fw_outstanding);
3227 scsi_dma_unmap(cmd->scmd);
3228 cmd->scmd->scsi_done(cmd->scmd);
3229 megasas_return_cmd(instance, cmd);
3234 switch (hdr->cmd_status) {
3237 cmd->scmd->result = DID_OK << 16;
3240 case MFI_STAT_SCSI_IO_FAILED:
3241 case MFI_STAT_LD_INIT_IN_PROGRESS:
3243 (DID_ERROR << 16) | hdr->scsi_status;
3246 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3248 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3250 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3251 memset(cmd->scmd->sense_buffer, 0,
3252 SCSI_SENSE_BUFFERSIZE);
3253 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3256 cmd->scmd->result |= DRIVER_SENSE << 24;
3261 case MFI_STAT_LD_OFFLINE:
3262 case MFI_STAT_DEVICE_NOT_FOUND:
3263 cmd->scmd->result = DID_BAD_TARGET << 16;
3267 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3269 cmd->scmd->result = DID_ERROR << 16;
3273 atomic_dec(&instance->fw_outstanding);
3275 scsi_dma_unmap(cmd->scmd);
3276 cmd->scmd->scsi_done(cmd->scmd);
3277 megasas_return_cmd(instance, cmd);
3284 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3285 /* Check for LD map update */
3286 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3287 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3288 fusion->fast_path_io = 0;
3289 spin_lock_irqsave(instance->host->host_lock, flags);
3290 instance->map_update_cmd = NULL;
3291 if (cmd->frame->hdr.cmd_status != 0) {
3292 if (cmd->frame->hdr.cmd_status !=
3294 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3295 cmd->frame->hdr.cmd_status);
3297 megasas_return_cmd(instance, cmd);
3298 spin_unlock_irqrestore(
3299 instance->host->host_lock,
3305 megasas_return_cmd(instance, cmd);
3308 * Set fast path IO to ZERO.
3309 * Validate Map will set proper value.
3310 * Meanwhile all IOs will go as LD IO.
3312 if (MR_ValidateMapInfo(instance))
3313 fusion->fast_path_io = 1;
3315 fusion->fast_path_io = 0;
3316 megasas_sync_map_info(instance);
3317 spin_unlock_irqrestore(instance->host->host_lock,
3321 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3322 opcode == MR_DCMD_CTRL_EVENT_GET) {
3323 spin_lock_irqsave(&poll_aen_lock, flags);
3324 megasas_poll_wait_aen = 0;
3325 spin_unlock_irqrestore(&poll_aen_lock, flags);
3328 /* FW has an updated PD sequence */
3329 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3330 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3332 spin_lock_irqsave(instance->host->host_lock, flags);
3333 status = cmd->frame->hdr.cmd_status;
3334 instance->jbod_seq_cmd = NULL;
3335 megasas_return_cmd(instance, cmd);
3337 if (status == MFI_STAT_OK) {
3338 instance->pd_seq_map_id++;
3339 /* Re-register a pd sync seq num cmd */
3340 if (megasas_sync_pd_seq_num(instance, true))
3341 instance->use_seqnum_jbod_fp = false;
3343 instance->use_seqnum_jbod_fp = false;
3345 spin_unlock_irqrestore(instance->host->host_lock, flags);
3350 * See if got an event notification
3352 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3353 megasas_service_aen(instance, cmd);
3355 megasas_complete_int_cmd(instance, cmd);
3361 * Cmd issued to abort another cmd returned
3363 megasas_complete_abort(instance, cmd);
3367 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3374 * megasas_issue_pending_cmds_again - issue all pending cmds
3375 * in FW again because of the fw reset
3376 * @instance: Adapter soft state
3379 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3381 struct megasas_cmd *cmd;
3382 struct list_head clist_local;
3383 union megasas_evt_class_locale class_locale;
3384 unsigned long flags;
3387 INIT_LIST_HEAD(&clist_local);
3388 spin_lock_irqsave(&instance->hba_lock, flags);
3389 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3390 spin_unlock_irqrestore(&instance->hba_lock, flags);
3392 while (!list_empty(&clist_local)) {
3393 cmd = list_entry((&clist_local)->next,
3394 struct megasas_cmd, list);
3395 list_del_init(&cmd->list);
3397 if (cmd->sync_cmd || cmd->scmd) {
3398 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3399 "detected to be pending while HBA reset\n",
3400 cmd, cmd->scmd, cmd->sync_cmd);
3402 cmd->retry_for_fw_reset++;
3404 if (cmd->retry_for_fw_reset == 3) {
3405 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3406 "was tried multiple times during reset."
3407 "Shutting down the HBA\n",
3408 cmd, cmd->scmd, cmd->sync_cmd);
3409 instance->instancet->disable_intr(instance);
3410 atomic_set(&instance->fw_reset_no_pci_access, 1);
3411 megaraid_sas_kill_hba(instance);
3416 if (cmd->sync_cmd == 1) {
3418 dev_notice(&instance->pdev->dev, "unexpected"
3419 "cmd attached to internal command!\n");
3421 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3422 "on the internal reset queue,"
3423 "issue it again.\n", cmd);
3424 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3425 instance->instancet->fire_cmd(instance,
3426 cmd->frame_phys_addr,
3427 0, instance->reg_set);
3428 } else if (cmd->scmd) {
3429 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3430 "detected on the internal queue, issue again.\n",
3431 cmd, cmd->scmd->cmnd[0]);
3433 atomic_inc(&instance->fw_outstanding);
3434 instance->instancet->fire_cmd(instance,
3435 cmd->frame_phys_addr,
3436 cmd->frame_count-1, instance->reg_set);
3438 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3439 "internal reset defer list while re-issue!!\n",
3444 if (instance->aen_cmd) {
3445 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3446 megasas_return_cmd(instance, instance->aen_cmd);
3448 instance->aen_cmd = NULL;
3452 * Initiate AEN (Asynchronous Event Notification)
3454 seq_num = instance->last_seq_num;
3455 class_locale.members.reserved = 0;
3456 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3457 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3459 megasas_register_aen(instance, seq_num, class_locale.word);
3463 * Move the internal reset pending commands to a deferred queue.
3465 * We move the commands pending at internal reset time to a
3466 * pending queue. This queue would be flushed after successful
3467 * completion of the internal reset sequence. if the internal reset
3468 * did not complete in time, the kernel reset handler would flush
3472 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3474 struct megasas_cmd *cmd;
3476 u16 max_cmd = instance->max_fw_cmds;
3478 unsigned long flags;
3481 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3482 for (i = 0; i < max_cmd; i++) {
3483 cmd = instance->cmd_list[i];
3484 if (cmd->sync_cmd == 1 || cmd->scmd) {
3485 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3486 "on the defer queue as internal\n",
3487 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3489 if (!list_empty(&cmd->list)) {
3490 dev_notice(&instance->pdev->dev, "ERROR while"
3491 " moving this cmd:%p, %d %p, it was"
3492 "discovered on some list?\n",
3493 cmd, cmd->sync_cmd, cmd->scmd);
3495 list_del_init(&cmd->list);
3498 list_add_tail(&cmd->list,
3499 &instance->internal_reset_pending_q);
3502 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3507 process_fw_state_change_wq(struct work_struct *work)
3509 struct megasas_instance *instance =
3510 container_of(work, struct megasas_instance, work_init);
3512 unsigned long flags;
3514 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3515 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3516 atomic_read(&instance->adprecovery));
3520 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3521 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3522 "state, restarting it...\n");
3524 instance->instancet->disable_intr(instance);
3525 atomic_set(&instance->fw_outstanding, 0);
3527 atomic_set(&instance->fw_reset_no_pci_access, 1);
3528 instance->instancet->adp_reset(instance, instance->reg_set);
3529 atomic_set(&instance->fw_reset_no_pci_access, 0);
3531 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3532 "initiating next stage...\n");
3534 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3535 "state 2 starting...\n");
3537 /* waiting for about 20 second before start the second init */
3538 for (wait = 0; wait < 30; wait++) {
3542 if (megasas_transition_to_ready(instance, 1)) {
3543 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3545 atomic_set(&instance->fw_reset_no_pci_access, 1);
3546 megaraid_sas_kill_hba(instance);
3550 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3551 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3552 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3554 *instance->consumer = *instance->producer;
3556 *instance->consumer = 0;
3557 *instance->producer = 0;
3560 megasas_issue_init_mfi(instance);
3562 spin_lock_irqsave(&instance->hba_lock, flags);
3563 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3564 spin_unlock_irqrestore(&instance->hba_lock, flags);
3565 instance->instancet->enable_intr(instance);
3567 megasas_issue_pending_cmds_again(instance);
3568 instance->issuepend_done = 1;
3573 * megasas_deplete_reply_queue - Processes all completed commands
3574 * @instance: Adapter soft state
3575 * @alt_status: Alternate status to be returned to
3576 * SCSI mid-layer instead of the status
3577 * returned by the FW
3578 * Note: this must be called with hba lock held
3581 megasas_deplete_reply_queue(struct megasas_instance *instance,
3587 if ((mfiStatus = instance->instancet->check_reset(instance,
3588 instance->reg_set)) == 1) {
3592 if ((mfiStatus = instance->instancet->clear_intr(
3595 /* Hardware may not set outbound_intr_status in MSI-X mode */
3596 if (!instance->msix_vectors)
3600 instance->mfiStatus = mfiStatus;
3602 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3603 fw_state = instance->instancet->read_fw_status_reg(
3604 instance->reg_set) & MFI_STATE_MASK;
3606 if (fw_state != MFI_STATE_FAULT) {
3607 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3611 if ((fw_state == MFI_STATE_FAULT) &&
3612 (instance->disableOnlineCtrlReset == 0)) {
3613 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3615 if ((instance->pdev->device ==
3616 PCI_DEVICE_ID_LSI_SAS1064R) ||
3617 (instance->pdev->device ==
3618 PCI_DEVICE_ID_DELL_PERC5) ||
3619 (instance->pdev->device ==
3620 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3622 *instance->consumer =
3623 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3627 instance->instancet->disable_intr(instance);
3628 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
3629 instance->issuepend_done = 0;
3631 atomic_set(&instance->fw_outstanding, 0);
3632 megasas_internal_reset_defer_cmds(instance);
3634 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3635 fw_state, atomic_read(&instance->adprecovery));
3637 schedule_work(&instance->work_init);
3641 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3642 fw_state, instance->disableOnlineCtrlReset);
3646 tasklet_schedule(&instance->isr_tasklet);
3650 * megasas_isr - isr entry point
3652 static irqreturn_t megasas_isr(int irq, void *devp)
3654 struct megasas_irq_context *irq_context = devp;
3655 struct megasas_instance *instance = irq_context->instance;
3656 unsigned long flags;
3659 if (atomic_read(&instance->fw_reset_no_pci_access))
3662 spin_lock_irqsave(&instance->hba_lock, flags);
3663 rc = megasas_deplete_reply_queue(instance, DID_OK);
3664 spin_unlock_irqrestore(&instance->hba_lock, flags);
3670 * megasas_transition_to_ready - Move the FW to READY state
3671 * @instance: Adapter soft state
3673 * During the initialization, FW passes can potentially be in any one of
3674 * several possible states. If the FW in operational, waiting-for-handshake
3675 * states, driver must take steps to bring it to ready state. Otherwise, it
3676 * has to wait for the ready state.
3679 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3685 u32 abs_state, curr_abs_state;
3687 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3688 fw_state = abs_state & MFI_STATE_MASK;
3690 if (fw_state != MFI_STATE_READY)
3691 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3694 while (fw_state != MFI_STATE_READY) {
3698 case MFI_STATE_FAULT:
3699 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3701 max_wait = MEGASAS_RESET_WAIT_TIME;
3702 cur_state = MFI_STATE_FAULT;
3707 case MFI_STATE_WAIT_HANDSHAKE:
3709 * Set the CLR bit in inbound doorbell
3711 if ((instance->pdev->device ==
3712 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3713 (instance->pdev->device ==
3714 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3715 (instance->ctrl_context))
3717 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3718 &instance->reg_set->doorbell);
3721 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3722 &instance->reg_set->inbound_doorbell);
3724 max_wait = MEGASAS_RESET_WAIT_TIME;
3725 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3728 case MFI_STATE_BOOT_MESSAGE_PENDING:
3729 if ((instance->pdev->device ==
3730 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3731 (instance->pdev->device ==
3732 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3733 (instance->ctrl_context))
3734 writel(MFI_INIT_HOTPLUG,
3735 &instance->reg_set->doorbell);
3737 writel(MFI_INIT_HOTPLUG,
3738 &instance->reg_set->inbound_doorbell);
3740 max_wait = MEGASAS_RESET_WAIT_TIME;
3741 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3744 case MFI_STATE_OPERATIONAL:
3746 * Bring it to READY state; assuming max wait 10 secs
3748 instance->instancet->disable_intr(instance);
3749 if ((instance->pdev->device ==
3750 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3751 (instance->pdev->device ==
3752 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3753 (instance->ctrl_context)) {
3754 writel(MFI_RESET_FLAGS,
3755 &instance->reg_set->doorbell);
3757 if (instance->ctrl_context) {
3758 for (i = 0; i < (10 * 1000); i += 20) {
3769 writel(MFI_RESET_FLAGS,
3770 &instance->reg_set->inbound_doorbell);
3772 max_wait = MEGASAS_RESET_WAIT_TIME;
3773 cur_state = MFI_STATE_OPERATIONAL;
3776 case MFI_STATE_UNDEFINED:
3778 * This state should not last for more than 2 seconds
3780 max_wait = MEGASAS_RESET_WAIT_TIME;
3781 cur_state = MFI_STATE_UNDEFINED;
3784 case MFI_STATE_BB_INIT:
3785 max_wait = MEGASAS_RESET_WAIT_TIME;
3786 cur_state = MFI_STATE_BB_INIT;
3789 case MFI_STATE_FW_INIT:
3790 max_wait = MEGASAS_RESET_WAIT_TIME;
3791 cur_state = MFI_STATE_FW_INIT;
3794 case MFI_STATE_FW_INIT_2:
3795 max_wait = MEGASAS_RESET_WAIT_TIME;
3796 cur_state = MFI_STATE_FW_INIT_2;
3799 case MFI_STATE_DEVICE_SCAN:
3800 max_wait = MEGASAS_RESET_WAIT_TIME;
3801 cur_state = MFI_STATE_DEVICE_SCAN;
3804 case MFI_STATE_FLUSH_CACHE:
3805 max_wait = MEGASAS_RESET_WAIT_TIME;
3806 cur_state = MFI_STATE_FLUSH_CACHE;
3810 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3816 * The cur_state should not last for more than max_wait secs
3818 for (i = 0; i < (max_wait * 1000); i++) {
3819 curr_abs_state = instance->instancet->
3820 read_fw_status_reg(instance->reg_set);
3822 if (abs_state == curr_abs_state) {
3829 * Return error if fw_state hasn't changed after max_wait
3831 if (curr_abs_state == abs_state) {
3832 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3833 "in %d secs\n", fw_state, max_wait);
3837 abs_state = curr_abs_state;
3838 fw_state = curr_abs_state & MFI_STATE_MASK;
3840 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3846 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3847 * @instance: Adapter soft state
3849 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3852 u16 max_cmd = instance->max_mfi_cmds;
3853 struct megasas_cmd *cmd;
3855 if (!instance->frame_dma_pool)
3859 * Return all frames to pool
3861 for (i = 0; i < max_cmd; i++) {
3863 cmd = instance->cmd_list[i];
3866 dma_pool_free(instance->frame_dma_pool, cmd->frame,
3867 cmd->frame_phys_addr);
3870 dma_pool_free(instance->sense_dma_pool, cmd->sense,
3871 cmd->sense_phys_addr);
3875 * Now destroy the pool itself
3877 dma_pool_destroy(instance->frame_dma_pool);
3878 dma_pool_destroy(instance->sense_dma_pool);
3880 instance->frame_dma_pool = NULL;
3881 instance->sense_dma_pool = NULL;
3885 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3886 * @instance: Adapter soft state
3888 * Each command packet has an embedded DMA memory buffer that is used for
3889 * filling MFI frame and the SG list that immediately follows the frame. This
3890 * function creates those DMA memory buffers for each command packet by using
3891 * PCI pool facility.
3893 static int megasas_create_frame_pool(struct megasas_instance *instance)
3899 struct megasas_cmd *cmd;
3901 max_cmd = instance->max_mfi_cmds;
3904 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3905 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3907 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3908 sizeof(struct megasas_sge32);
3910 if (instance->flag_ieee)
3911 sge_sz = sizeof(struct megasas_sge_skinny);
3914 * For MFI controllers.
3916 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3917 * Total 960 byte (15 MFI frame of 64 byte)
3919 * Fusion adapter require only 3 extra frame.
3920 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3921 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3922 * Total 192 byte (3 MFI frame of 64 byte)
3924 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
3925 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
3927 * Use DMA pool facility provided by PCI layer
3929 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
3930 &instance->pdev->dev,
3931 instance->mfi_frame_size, 256, 0);
3933 if (!instance->frame_dma_pool) {
3934 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
3938 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
3939 &instance->pdev->dev, 128,
3942 if (!instance->sense_dma_pool) {
3943 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
3945 dma_pool_destroy(instance->frame_dma_pool);
3946 instance->frame_dma_pool = NULL;
3952 * Allocate and attach a frame to each of the commands in cmd_list.
3953 * By making cmd->index as the context instead of the &cmd, we can
3954 * always use 32bit context regardless of the architecture
3956 for (i = 0; i < max_cmd; i++) {
3958 cmd = instance->cmd_list[i];
3960 cmd->frame = dma_pool_alloc(instance->frame_dma_pool,
3961 GFP_KERNEL, &cmd->frame_phys_addr);
3963 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
3964 GFP_KERNEL, &cmd->sense_phys_addr);
3967 * megasas_teardown_frame_pool() takes care of freeing
3968 * whatever has been allocated
3970 if (!cmd->frame || !cmd->sense) {
3971 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
3972 megasas_teardown_frame_pool(instance);
3976 memset(cmd->frame, 0, instance->mfi_frame_size);
3977 cmd->frame->io.context = cpu_to_le32(cmd->index);
3978 cmd->frame->io.pad_0 = 0;
3979 if (!instance->ctrl_context && reset_devices)
3980 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
3987 * megasas_free_cmds - Free all the cmds in the free cmd pool
3988 * @instance: Adapter soft state
3990 void megasas_free_cmds(struct megasas_instance *instance)
3994 /* First free the MFI frame pool */
3995 megasas_teardown_frame_pool(instance);
3997 /* Free all the commands in the cmd_list */
3998 for (i = 0; i < instance->max_mfi_cmds; i++)
4000 kfree(instance->cmd_list[i]);
4002 /* Free the cmd_list buffer itself */
4003 kfree(instance->cmd_list);
4004 instance->cmd_list = NULL;
4006 INIT_LIST_HEAD(&instance->cmd_pool);
4010 * megasas_alloc_cmds - Allocates the command packets
4011 * @instance: Adapter soft state
4013 * Each command that is issued to the FW, whether IO commands from the OS or
4014 * internal commands like IOCTLs, are wrapped in local data structure called
4015 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4018 * Each frame has a 32-bit field called context (tag). This context is used
4019 * to get back the megasas_cmd from the frame when a frame gets completed in
4020 * the ISR. Typically the address of the megasas_cmd itself would be used as
4021 * the context. But we wanted to keep the differences between 32 and 64 bit
4022 * systems to the mininum. We always use 32 bit integers for the context. In
4023 * this driver, the 32 bit values are the indices into an array cmd_list.
4024 * This array is used only to look up the megasas_cmd given the context. The
4025 * free commands themselves are maintained in a linked list called cmd_pool.
4027 int megasas_alloc_cmds(struct megasas_instance *instance)
4032 struct megasas_cmd *cmd;
4033 struct fusion_context *fusion;
4035 fusion = instance->ctrl_context;
4036 max_cmd = instance->max_mfi_cmds;
4039 * instance->cmd_list is an array of struct megasas_cmd pointers.
4040 * Allocate the dynamic array first and then allocate individual
4043 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
4045 if (!instance->cmd_list) {
4046 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
4050 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
4052 for (i = 0; i < max_cmd; i++) {
4053 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4056 if (!instance->cmd_list[i]) {
4058 for (j = 0; j < i; j++)
4059 kfree(instance->cmd_list[j]);
4061 kfree(instance->cmd_list);
4062 instance->cmd_list = NULL;
4068 for (i = 0; i < max_cmd; i++) {
4069 cmd = instance->cmd_list[i];
4070 memset(cmd, 0, sizeof(struct megasas_cmd));
4073 cmd->instance = instance;
4075 list_add_tail(&cmd->list, &instance->cmd_pool);
4079 * Create a frame pool and assign one frame to each cmd
4081 if (megasas_create_frame_pool(instance)) {
4082 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
4083 megasas_free_cmds(instance);
4090 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4091 * @instance: Adapter soft state
4093 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4094 * or FW is not under OCR.
4097 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4099 if (!instance->ctrl_context)
4100 return KILL_ADAPTER;
4101 else if (instance->unload ||
4102 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4103 return IGNORE_TIMEOUT;
4105 return INITIATE_OCR;
4109 megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
4112 struct megasas_cmd *cmd;
4113 struct megasas_dcmd_frame *dcmd;
4115 struct MR_PRIV_DEVICE *mr_device_priv_data;
4118 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
4119 cmd = megasas_get_cmd(instance);
4122 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4126 dcmd = &cmd->frame->dcmd;
4128 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4129 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4131 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4132 dcmd->cmd = MFI_CMD_DCMD;
4133 dcmd->cmd_status = 0xFF;
4134 dcmd->sge_count = 1;
4135 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4138 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4139 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4140 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
4141 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
4143 if (instance->ctrl_context && !instance->mask_interrupts)
4144 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4146 ret = megasas_issue_polled(instance, cmd);
4150 mr_device_priv_data = sdev->hostdata;
4151 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4152 mr_device_priv_data->interface_type =
4153 instance->pd_info->state.ddf.pdType.intf;
4158 switch (dcmd_timeout_ocr_possible(instance)) {
4160 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4161 megasas_reset_fusion(instance->host,
4162 MFI_IO_TIMEOUT_OCR);
4165 megaraid_sas_kill_hba(instance);
4167 case IGNORE_TIMEOUT:
4168 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4169 __func__, __LINE__);
4176 if (ret != DCMD_TIMEOUT)
4177 megasas_return_cmd(instance, cmd);
4182 * megasas_get_pd_list_info - Returns FW's pd_list structure
4183 * @instance: Adapter soft state
4184 * @pd_list: pd_list structure
4186 * Issues an internal command (DCMD) to get the FW's controller PD
4187 * list structure. This information is mainly used to find out SYSTEM
4188 * supported by the FW.
4191 megasas_get_pd_list(struct megasas_instance *instance)
4193 int ret = 0, pd_index = 0;
4194 struct megasas_cmd *cmd;
4195 struct megasas_dcmd_frame *dcmd;
4196 struct MR_PD_LIST *ci;
4197 struct MR_PD_ADDRESS *pd_addr;
4198 dma_addr_t ci_h = 0;
4200 if (instance->pd_list_not_supported) {
4201 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4202 "not supported by firmware\n");
4206 cmd = megasas_get_cmd(instance);
4209 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4213 dcmd = &cmd->frame->dcmd;
4215 ci = pci_alloc_consistent(instance->pdev,
4216 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
4219 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
4220 megasas_return_cmd(instance, cmd);
4224 memset(ci, 0, sizeof(*ci));
4225 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4227 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4228 dcmd->mbox.b[1] = 0;
4229 dcmd->cmd = MFI_CMD_DCMD;
4230 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4231 dcmd->sge_count = 1;
4232 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4235 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4236 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4237 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4238 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4240 if (instance->ctrl_context && !instance->mask_interrupts)
4241 ret = megasas_issue_blocked_cmd(instance, cmd,
4242 MFI_IO_TIMEOUT_SECS);
4244 ret = megasas_issue_polled(instance, cmd);
4248 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4249 "failed/not supported by firmware\n");
4251 if (instance->ctrl_context)
4252 megaraid_sas_kill_hba(instance);
4254 instance->pd_list_not_supported = 1;
4258 switch (dcmd_timeout_ocr_possible(instance)) {
4260 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4262 * DCMD failed from AEN path.
4263 * AEN path already hold reset_mutex to avoid PCI access
4264 * while OCR is in progress.
4266 mutex_unlock(&instance->reset_mutex);
4267 megasas_reset_fusion(instance->host,
4268 MFI_IO_TIMEOUT_OCR);
4269 mutex_lock(&instance->reset_mutex);
4272 megaraid_sas_kill_hba(instance);
4274 case IGNORE_TIMEOUT:
4275 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4276 __func__, __LINE__);
4285 if ((le32_to_cpu(ci->count) >
4286 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4289 memset(instance->local_pd_list, 0,
4290 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4292 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4293 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
4294 le16_to_cpu(pd_addr->deviceId);
4295 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
4296 pd_addr->scsiDevType;
4297 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
4302 memcpy(instance->pd_list, instance->local_pd_list,
4303 sizeof(instance->pd_list));
4308 pci_free_consistent(instance->pdev,
4309 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4312 if (ret != DCMD_TIMEOUT)
4313 megasas_return_cmd(instance, cmd);
4319 * megasas_get_ld_list_info - Returns FW's ld_list structure
4320 * @instance: Adapter soft state
4321 * @ld_list: ld_list structure
4323 * Issues an internal command (DCMD) to get the FW's controller PD
4324 * list structure. This information is mainly used to find out SYSTEM
4325 * supported by the FW.
4328 megasas_get_ld_list(struct megasas_instance *instance)
4330 int ret = 0, ld_index = 0, ids = 0;
4331 struct megasas_cmd *cmd;
4332 struct megasas_dcmd_frame *dcmd;
4333 struct MR_LD_LIST *ci;
4334 dma_addr_t ci_h = 0;
4337 cmd = megasas_get_cmd(instance);
4340 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4344 dcmd = &cmd->frame->dcmd;
4346 ci = pci_alloc_consistent(instance->pdev,
4347 sizeof(struct MR_LD_LIST),
4351 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
4352 megasas_return_cmd(instance, cmd);
4356 memset(ci, 0, sizeof(*ci));
4357 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4359 if (instance->supportmax256vd)
4360 dcmd->mbox.b[0] = 1;
4361 dcmd->cmd = MFI_CMD_DCMD;
4362 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4363 dcmd->sge_count = 1;
4364 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4366 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4367 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4368 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4369 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
4372 if (instance->ctrl_context && !instance->mask_interrupts)
4373 ret = megasas_issue_blocked_cmd(instance, cmd,
4374 MFI_IO_TIMEOUT_SECS);
4376 ret = megasas_issue_polled(instance, cmd);
4378 ld_count = le32_to_cpu(ci->ldCount);
4382 megaraid_sas_kill_hba(instance);
4386 switch (dcmd_timeout_ocr_possible(instance)) {
4388 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4390 * DCMD failed from AEN path.
4391 * AEN path already hold reset_mutex to avoid PCI access
4392 * while OCR is in progress.
4394 mutex_unlock(&instance->reset_mutex);
4395 megasas_reset_fusion(instance->host,
4396 MFI_IO_TIMEOUT_OCR);
4397 mutex_lock(&instance->reset_mutex);
4400 megaraid_sas_kill_hba(instance);
4402 case IGNORE_TIMEOUT:
4403 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4404 __func__, __LINE__);
4411 if (ld_count > instance->fw_supported_vd_count)
4414 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4416 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4417 if (ci->ldList[ld_index].state != 0) {
4418 ids = ci->ldList[ld_index].ref.targetId;
4419 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4426 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
4428 if (ret != DCMD_TIMEOUT)
4429 megasas_return_cmd(instance, cmd);
4435 * megasas_ld_list_query - Returns FW's ld_list structure
4436 * @instance: Adapter soft state
4437 * @ld_list: ld_list structure
4439 * Issues an internal command (DCMD) to get the FW's controller PD
4440 * list structure. This information is mainly used to find out SYSTEM
4441 * supported by the FW.
4444 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4446 int ret = 0, ld_index = 0, ids = 0;
4447 struct megasas_cmd *cmd;
4448 struct megasas_dcmd_frame *dcmd;
4449 struct MR_LD_TARGETID_LIST *ci;
4450 dma_addr_t ci_h = 0;
4453 cmd = megasas_get_cmd(instance);
4456 dev_warn(&instance->pdev->dev,
4457 "megasas_ld_list_query: Failed to get cmd\n");
4461 dcmd = &cmd->frame->dcmd;
4463 ci = pci_alloc_consistent(instance->pdev,
4464 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4467 dev_warn(&instance->pdev->dev,
4468 "Failed to alloc mem for ld_list_query\n");
4469 megasas_return_cmd(instance, cmd);
4473 memset(ci, 0, sizeof(*ci));
4474 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4476 dcmd->mbox.b[0] = query_type;
4477 if (instance->supportmax256vd)
4478 dcmd->mbox.b[2] = 1;
4480 dcmd->cmd = MFI_CMD_DCMD;
4481 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4482 dcmd->sge_count = 1;
4483 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4485 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4486 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4487 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4488 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4491 if (instance->ctrl_context && !instance->mask_interrupts)
4492 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4494 ret = megasas_issue_polled(instance, cmd);
4498 dev_info(&instance->pdev->dev,
4499 "DCMD not supported by firmware - %s %d\n",
4500 __func__, __LINE__);
4501 ret = megasas_get_ld_list(instance);
4504 switch (dcmd_timeout_ocr_possible(instance)) {
4506 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4508 * DCMD failed from AEN path.
4509 * AEN path already hold reset_mutex to avoid PCI access
4510 * while OCR is in progress.
4512 mutex_unlock(&instance->reset_mutex);
4513 megasas_reset_fusion(instance->host,
4514 MFI_IO_TIMEOUT_OCR);
4515 mutex_lock(&instance->reset_mutex);
4518 megaraid_sas_kill_hba(instance);
4520 case IGNORE_TIMEOUT:
4521 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4522 __func__, __LINE__);
4528 tgtid_count = le32_to_cpu(ci->count);
4530 if ((tgtid_count > (instance->fw_supported_vd_count)))
4533 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4534 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4535 ids = ci->targetId[ld_index];
4536 instance->ld_ids[ids] = ci->targetId[ld_index];
4542 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4545 if (ret != DCMD_TIMEOUT)
4546 megasas_return_cmd(instance, cmd);
4552 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4553 * instance : Controller's instance
4555 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4557 struct fusion_context *fusion;
4558 u32 ventura_map_sz = 0;
4560 fusion = instance->ctrl_context;
4561 /* For MFI based controllers return dummy success */
4565 instance->supportmax256vd =
4566 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4567 /* Below is additional check to address future FW enhancement */
4568 if (instance->ctrl_info->max_lds > 64)
4569 instance->supportmax256vd = 1;
4571 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4572 * MEGASAS_MAX_DEV_PER_CHANNEL;
4573 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4574 * MEGASAS_MAX_DEV_PER_CHANNEL;
4575 if (instance->supportmax256vd) {
4576 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4577 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4579 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4580 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4583 dev_info(&instance->pdev->dev,
4584 "firmware type\t: %s\n",
4585 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4586 "Legacy(64 VD) firmware");
4588 if (instance->max_raid_mapsize) {
4589 ventura_map_sz = instance->max_raid_mapsize *
4590 MR_MIN_MAP_SIZE; /* 64k */
4591 fusion->current_map_sz = ventura_map_sz;
4592 fusion->max_map_sz = ventura_map_sz;
4594 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4595 (sizeof(struct MR_LD_SPAN_MAP) *
4596 (instance->fw_supported_vd_count - 1));
4597 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4599 fusion->max_map_sz =
4600 max(fusion->old_map_sz, fusion->new_map_sz);
4602 if (instance->supportmax256vd)
4603 fusion->current_map_sz = fusion->new_map_sz;
4605 fusion->current_map_sz = fusion->old_map_sz;
4607 /* irrespective of FW raid maps, driver raid map is constant */
4608 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
4612 * megasas_get_controller_info - Returns FW's controller structure
4613 * @instance: Adapter soft state
4615 * Issues an internal command (DCMD) to get the FW's controller structure.
4616 * This information is mainly used to find out the maximum IO transfer per
4617 * command supported by the FW.
4620 megasas_get_ctrl_info(struct megasas_instance *instance)
4623 struct megasas_cmd *cmd;
4624 struct megasas_dcmd_frame *dcmd;
4625 struct megasas_ctrl_info *ci;
4626 struct megasas_ctrl_info *ctrl_info;
4627 dma_addr_t ci_h = 0;
4629 ctrl_info = instance->ctrl_info;
4631 cmd = megasas_get_cmd(instance);
4634 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4638 dcmd = &cmd->frame->dcmd;
4640 ci = pci_alloc_consistent(instance->pdev,
4641 sizeof(struct megasas_ctrl_info), &ci_h);
4644 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
4645 megasas_return_cmd(instance, cmd);
4649 memset(ci, 0, sizeof(*ci));
4650 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4652 dcmd->cmd = MFI_CMD_DCMD;
4653 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4654 dcmd->sge_count = 1;
4655 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4658 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4659 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4660 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4661 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4662 dcmd->mbox.b[0] = 1;
4664 if (instance->ctrl_context && !instance->mask_interrupts)
4665 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4667 ret = megasas_issue_polled(instance, cmd);
4671 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
4672 /* Save required controller information in
4673 * CPU endianness format.
4675 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4676 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4677 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4678 le16_to_cpus((u16 *)&ctrl_info->adapter_operations4);
4680 /* Update the latest Ext VD info.
4681 * From Init path, store current firmware details.
4682 * From OCR path, detect any firmware properties changes.
4683 * in case of Firmware upgrade without system reboot.
4685 megasas_update_ext_vd_details(instance);
4686 instance->use_seqnum_jbod_fp =
4687 ctrl_info->adapterOperations3.useSeqNumJbodFP;
4688 instance->support_morethan256jbod =
4689 ctrl_info->adapter_operations4.support_pd_map_target_id;
4691 /*Check whether controller is iMR or MR */
4692 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4693 dev_info(&instance->pdev->dev,
4694 "controller type\t: %s(%dMB)\n",
4695 instance->is_imr ? "iMR" : "MR",
4696 le16_to_cpu(ctrl_info->memory_size));
4698 instance->disableOnlineCtrlReset =
4699 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4700 instance->secure_jbod_support =
4701 ctrl_info->adapterOperations3.supportSecurityonJBOD;
4702 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4703 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
4704 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4705 instance->secure_jbod_support ? "Yes" : "No");
4709 switch (dcmd_timeout_ocr_possible(instance)) {
4711 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4712 megasas_reset_fusion(instance->host,
4713 MFI_IO_TIMEOUT_OCR);
4716 megaraid_sas_kill_hba(instance);
4718 case IGNORE_TIMEOUT:
4719 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4720 __func__, __LINE__);
4724 megaraid_sas_kill_hba(instance);
4729 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4732 megasas_return_cmd(instance, cmd);
4739 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4742 * @instance: Adapter soft state
4743 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4744 MR_CRASH_BUF_TURN_OFF = 0
4745 MR_CRASH_BUF_TURN_ON = 1
4746 * @return 0 on success non-zero on failure.
4747 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4748 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4749 * that driver supports crash dump feature. This DCMD will be sent only if
4750 * crash dump feature is supported by the FW.
4753 int megasas_set_crash_dump_params(struct megasas_instance *instance,
4757 struct megasas_cmd *cmd;
4758 struct megasas_dcmd_frame *dcmd;
4760 cmd = megasas_get_cmd(instance);
4763 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4768 dcmd = &cmd->frame->dcmd;
4770 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4771 dcmd->mbox.b[0] = crash_buf_state;
4772 dcmd->cmd = MFI_CMD_DCMD;
4773 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4774 dcmd->sge_count = 1;
4775 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4778 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4779 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4780 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4781 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4783 if (instance->ctrl_context && !instance->mask_interrupts)
4784 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4786 ret = megasas_issue_polled(instance, cmd);
4788 if (ret == DCMD_TIMEOUT) {
4789 switch (dcmd_timeout_ocr_possible(instance)) {
4791 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4792 megasas_reset_fusion(instance->host,
4793 MFI_IO_TIMEOUT_OCR);
4796 megaraid_sas_kill_hba(instance);
4798 case IGNORE_TIMEOUT:
4799 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4800 __func__, __LINE__);
4804 megasas_return_cmd(instance, cmd);
4810 * megasas_issue_init_mfi - Initializes the FW
4811 * @instance: Adapter soft state
4813 * Issues the INIT MFI cmd
4816 megasas_issue_init_mfi(struct megasas_instance *instance)
4819 struct megasas_cmd *cmd;
4820 struct megasas_init_frame *init_frame;
4821 struct megasas_init_queue_info *initq_info;
4822 dma_addr_t init_frame_h;
4823 dma_addr_t initq_info_h;
4826 * Prepare a init frame. Note the init frame points to queue info
4827 * structure. Each frame has SGL allocated after first 64 bytes. For
4828 * this frame - since we don't need any SGL - we use SGL's space as
4829 * queue info structure
4831 * We will not get a NULL command below. We just created the pool.
4833 cmd = megasas_get_cmd(instance);
4835 init_frame = (struct megasas_init_frame *)cmd->frame;
4836 initq_info = (struct megasas_init_queue_info *)
4837 ((unsigned long)init_frame + 64);
4839 init_frame_h = cmd->frame_phys_addr;
4840 initq_info_h = init_frame_h + 64;
4842 context = init_frame->context;
4843 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4844 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4845 init_frame->context = context;
4847 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4848 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
4850 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4851 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
4853 init_frame->cmd = MFI_CMD_INIT;
4854 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
4855 init_frame->queue_info_new_phys_addr_lo =
4856 cpu_to_le32(lower_32_bits(initq_info_h));
4857 init_frame->queue_info_new_phys_addr_hi =
4858 cpu_to_le32(upper_32_bits(initq_info_h));
4860 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
4863 * disable the intr before firing the init frame to FW
4865 instance->instancet->disable_intr(instance);
4868 * Issue the init frame in polled mode
4871 if (megasas_issue_polled(instance, cmd)) {
4872 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
4873 megasas_return_cmd(instance, cmd);
4877 megasas_return_cmd(instance, cmd);
4886 megasas_init_adapter_mfi(struct megasas_instance *instance)
4888 struct megasas_register_set __iomem *reg_set;
4892 reg_set = instance->reg_set;
4895 * Get various operational parameters from status register
4897 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
4899 * Reduce the max supported cmds by 1. This is to ensure that the
4900 * reply_q_sz (1 more than the max cmd that driver may send)
4901 * does not exceed max cmds that the FW can support
4903 instance->max_fw_cmds = instance->max_fw_cmds-1;
4904 instance->max_mfi_cmds = instance->max_fw_cmds;
4905 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
4908 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4909 * are reserved for IOCTL + driver's internal DCMDs.
4911 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4912 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4913 instance->max_scsi_cmds = (instance->max_fw_cmds -
4914 MEGASAS_SKINNY_INT_CMDS);
4915 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4917 instance->max_scsi_cmds = (instance->max_fw_cmds -
4919 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4922 instance->cur_can_queue = instance->max_scsi_cmds;
4924 * Create a pool of commands
4926 if (megasas_alloc_cmds(instance))
4927 goto fail_alloc_cmds;
4930 * Allocate memory for reply queue. Length of reply queue should
4931 * be _one_ more than the maximum commands handled by the firmware.
4933 * Note: When FW completes commands, it places corresponding contex
4934 * values in this circular reply queue. This circular queue is a fairly
4935 * typical producer-consumer queue. FW is the producer (of completed
4936 * commands) and the driver is the consumer.
4938 context_sz = sizeof(u32);
4939 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4941 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4943 &instance->reply_queue_h);
4945 if (!instance->reply_queue) {
4946 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
4947 goto fail_reply_queue;
4950 if (megasas_issue_init_mfi(instance))
4953 if (megasas_get_ctrl_info(instance)) {
4954 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4955 "Fail from %s %d\n", instance->unique_id,
4956 __func__, __LINE__);
4960 instance->fw_support_ieee = 0;
4961 instance->fw_support_ieee =
4962 (instance->instancet->read_fw_status_reg(reg_set) &
4965 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
4966 instance->fw_support_ieee);
4968 if (instance->fw_support_ieee)
4969 instance->flag_ieee = 1;
4975 pci_free_consistent(instance->pdev, reply_q_sz,
4976 instance->reply_queue, instance->reply_queue_h);
4978 megasas_free_cmds(instance);
4985 * megasas_setup_irqs_ioapic - register legacy interrupts.
4986 * @instance: Adapter soft state
4988 * Do not enable interrupt, only setup ISRs.
4990 * Return 0 on success.
4993 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4995 struct pci_dev *pdev;
4997 pdev = instance->pdev;
4998 instance->irq_context[0].instance = instance;
4999 instance->irq_context[0].MSIxIndex = 0;
5000 if (request_irq(pci_irq_vector(pdev, 0),
5001 instance->instancet->service_isr, IRQF_SHARED,
5002 "megasas", &instance->irq_context[0])) {
5003 dev_err(&instance->pdev->dev,
5004 "Failed to register IRQ from %s %d\n",
5005 __func__, __LINE__);
5012 * megasas_setup_irqs_msix - register MSI-x interrupts.
5013 * @instance: Adapter soft state
5014 * @is_probe: Driver probe check
5016 * Do not enable interrupt, only setup ISRs.
5018 * Return 0 on success.
5021 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5024 struct pci_dev *pdev;
5026 pdev = instance->pdev;
5029 for (i = 0; i < instance->msix_vectors; i++) {
5030 instance->irq_context[i].instance = instance;
5031 instance->irq_context[i].MSIxIndex = i;
5032 if (request_irq(pci_irq_vector(pdev, i),
5033 instance->instancet->service_isr, 0, "megasas",
5034 &instance->irq_context[i])) {
5035 dev_err(&instance->pdev->dev,
5036 "Failed to register IRQ for vector %d.\n", i);
5037 for (j = 0; j < i; j++)
5038 free_irq(pci_irq_vector(pdev, j),
5039 &instance->irq_context[j]);
5040 /* Retry irq register for IO_APIC*/
5041 instance->msix_vectors = 0;
5043 pci_free_irq_vectors(instance->pdev);
5044 return megasas_setup_irqs_ioapic(instance);
5054 * megasas_destroy_irqs- unregister interrupts.
5055 * @instance: Adapter soft state
5059 megasas_destroy_irqs(struct megasas_instance *instance) {
5063 if (instance->msix_vectors)
5064 for (i = 0; i < instance->msix_vectors; i++) {
5065 free_irq(pci_irq_vector(instance->pdev, i),
5066 &instance->irq_context[i]);
5069 free_irq(pci_irq_vector(instance->pdev, 0),
5070 &instance->irq_context[0]);
5074 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5075 * @instance: Adapter soft state
5076 * @is_probe: Driver probe check
5078 * Return 0 on success.
5081 megasas_setup_jbod_map(struct megasas_instance *instance)
5084 struct fusion_context *fusion = instance->ctrl_context;
5087 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5088 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5090 if (reset_devices || !fusion ||
5091 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
5092 dev_info(&instance->pdev->dev,
5093 "Jbod map is not supported %s %d\n",
5094 __func__, __LINE__);
5095 instance->use_seqnum_jbod_fp = false;
5099 if (fusion->pd_seq_sync[0])
5102 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5103 fusion->pd_seq_sync[i] = dma_alloc_coherent
5104 (&instance->pdev->dev, pd_seq_map_sz,
5105 &fusion->pd_seq_phys[i], GFP_KERNEL);
5106 if (!fusion->pd_seq_sync[i]) {
5107 dev_err(&instance->pdev->dev,
5108 "Failed to allocate memory from %s %d\n",
5109 __func__, __LINE__);
5111 dma_free_coherent(&instance->pdev->dev,
5112 pd_seq_map_sz, fusion->pd_seq_sync[0],
5113 fusion->pd_seq_phys[0]);
5114 fusion->pd_seq_sync[0] = NULL;
5116 instance->use_seqnum_jbod_fp = false;
5122 if (!megasas_sync_pd_seq_num(instance, false) &&
5123 !megasas_sync_pd_seq_num(instance, true))
5124 instance->use_seqnum_jbod_fp = true;
5126 instance->use_seqnum_jbod_fp = false;
5130 * megasas_init_fw - Initializes the FW
5131 * @instance: Adapter soft state
5133 * This is the main function for initializing firmware
5136 static int megasas_init_fw(struct megasas_instance *instance)
5139 u32 max_sectors_2, tmp_sectors, msix_enable;
5140 u32 scratch_pad_2, scratch_pad_3, scratch_pad_4;
5141 resource_size_t base_addr;
5142 struct megasas_register_set __iomem *reg_set;
5143 struct megasas_ctrl_info *ctrl_info = NULL;
5144 unsigned long bar_list;
5145 int i, j, loop, fw_msix_count = 0;
5146 struct IOV_111 *iovPtr;
5147 struct fusion_context *fusion;
5149 fusion = instance->ctrl_context;
5151 /* Find first memory bar */
5152 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
5153 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
5154 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
5156 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
5160 base_addr = pci_resource_start(instance->pdev, instance->bar);
5161 instance->reg_set = ioremap_nocache(base_addr, 8192);
5163 if (!instance->reg_set) {
5164 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
5168 reg_set = instance->reg_set;
5171 instance->instancet = &megasas_instance_template_fusion;
5173 switch (instance->pdev->device) {
5174 case PCI_DEVICE_ID_LSI_SAS1078R:
5175 case PCI_DEVICE_ID_LSI_SAS1078DE:
5176 instance->instancet = &megasas_instance_template_ppc;
5178 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5179 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5180 instance->instancet = &megasas_instance_template_gen2;
5182 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5183 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5184 instance->instancet = &megasas_instance_template_skinny;
5186 case PCI_DEVICE_ID_LSI_SAS1064R:
5187 case PCI_DEVICE_ID_DELL_PERC5:
5189 instance->instancet = &megasas_instance_template_xscale;
5190 instance->pd_list_not_supported = 1;
5195 if (megasas_transition_to_ready(instance, 0)) {
5196 atomic_set(&instance->fw_reset_no_pci_access, 1);
5197 instance->instancet->adp_reset
5198 (instance, instance->reg_set);
5199 atomic_set(&instance->fw_reset_no_pci_access, 0);
5200 dev_info(&instance->pdev->dev,
5201 "FW restarted successfully from %s!\n",
5204 /*waitting for about 30 second before retry*/
5207 if (megasas_transition_to_ready(instance, 0))
5208 goto fail_ready_state;
5211 if (instance->is_ventura) {
5213 readl(&instance->reg_set->outbound_scratch_pad_3);
5214 instance->max_raid_mapsize = ((scratch_pad_3 >>
5215 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5216 MR_MAX_RAID_MAP_SIZE_MASK);
5219 /* Check if MSI-X is supported while in ready state */
5220 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5222 if (msix_enable && !msix_disable) {
5223 int irq_flags = PCI_IRQ_MSIX;
5225 scratch_pad_2 = readl
5226 (&instance->reg_set->outbound_scratch_pad_2);
5227 /* Check max MSI-X vectors */
5229 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
5230 instance->msix_vectors = (scratch_pad_2
5231 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5232 fw_msix_count = instance->msix_vectors;
5233 } else { /* Invader series supports more than 8 MSI-x vectors*/
5234 instance->msix_vectors = ((scratch_pad_2
5235 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5236 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
5237 if (instance->msix_vectors > 16)
5238 instance->msix_combined = true;
5241 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5243 fw_msix_count = instance->msix_vectors;
5244 /* Save 1-15 reply post index address to local memory
5245 * Index 0 is already saved from reg offset
5246 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5248 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5249 instance->reply_post_host_index_addr[loop] =
5251 ((u8 __iomem *)instance->reg_set +
5252 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5257 instance->msix_vectors = min(msix_vectors,
5258 instance->msix_vectors);
5259 } else /* MFI adapters */
5260 instance->msix_vectors = 1;
5261 /* Don't bother allocating more MSI-X vectors than cpus */
5262 instance->msix_vectors = min(instance->msix_vectors,
5263 (unsigned int)num_online_cpus());
5264 if (smp_affinity_enable)
5265 irq_flags |= PCI_IRQ_AFFINITY;
5266 i = pci_alloc_irq_vectors(instance->pdev, 1,
5267 instance->msix_vectors, irq_flags);
5269 instance->msix_vectors = i;
5271 instance->msix_vectors = 0;
5274 * MSI-X host index 0 is common for all adapter.
5275 * It is used for all MPT based Adapters.
5277 if (instance->msix_combined) {
5278 instance->reply_post_host_index_addr[0] =
5279 (u32 *)((u8 *)instance->reg_set +
5280 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5282 instance->reply_post_host_index_addr[0] =
5283 (u32 *)((u8 *)instance->reg_set +
5284 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5287 if (!instance->msix_vectors) {
5288 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5290 goto fail_setup_irqs;
5293 dev_info(&instance->pdev->dev,
5294 "firmware supports msix\t: (%d)", fw_msix_count);
5295 dev_info(&instance->pdev->dev,
5296 "current msix/online cpus\t: (%d/%d)\n",
5297 instance->msix_vectors, (unsigned int)num_online_cpus());
5298 dev_info(&instance->pdev->dev,
5299 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
5301 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5302 (unsigned long)instance);
5304 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5306 if (instance->ctrl_info == NULL)
5307 goto fail_init_adapter;
5310 * Below are default value for legacy Firmware.
5311 * non-fusion based controllers
5313 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5314 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5315 /* Get operational params, sge flags, send init cmd to controller */
5316 if (instance->instancet->init_adapter(instance))
5317 goto fail_init_adapter;
5319 if (instance->is_ventura) {
5321 readl(&instance->reg_set->outbound_scratch_pad_4);
5322 if ((scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK) >=
5323 MR_DEFAULT_NVME_PAGE_SHIFT)
5324 instance->nvme_page_size =
5325 (1 << (scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK));
5327 dev_info(&instance->pdev->dev,
5328 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5331 if (instance->msix_vectors ?
5332 megasas_setup_irqs_msix(instance, 1) :
5333 megasas_setup_irqs_ioapic(instance))
5334 goto fail_init_adapter;
5336 instance->instancet->enable_intr(instance);
5338 dev_info(&instance->pdev->dev, "INIT adapter done\n");
5340 megasas_setup_jbod_map(instance);
5343 * the following function will get the PD LIST.
5345 memset(instance->pd_list, 0,
5346 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5347 if (megasas_get_pd_list(instance) < 0) {
5348 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5349 goto fail_get_ld_pd_list;
5352 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5354 /* stream detection initialization */
5355 if (instance->is_ventura && fusion) {
5356 fusion->stream_detect_by_ld =
5357 kzalloc(sizeof(struct LD_STREAM_DETECT *)
5358 * MAX_LOGICAL_DRIVES_EXT,
5360 if (!fusion->stream_detect_by_ld) {
5361 dev_err(&instance->pdev->dev,
5362 "unable to allocate stream detection for pool of LDs\n");
5363 goto fail_get_ld_pd_list;
5365 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5366 fusion->stream_detect_by_ld[i] =
5367 kmalloc(sizeof(struct LD_STREAM_DETECT),
5369 if (!fusion->stream_detect_by_ld[i]) {
5370 dev_err(&instance->pdev->dev,
5371 "unable to allocate stream detect by LD\n ");
5372 for (j = 0; j < i; ++j)
5373 kfree(fusion->stream_detect_by_ld[j]);
5374 kfree(fusion->stream_detect_by_ld);
5375 fusion->stream_detect_by_ld = NULL;
5376 goto fail_get_ld_pd_list;
5378 fusion->stream_detect_by_ld[i]->mru_bit_map
5383 if (megasas_ld_list_query(instance,
5384 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5385 goto fail_get_ld_pd_list;
5388 * Compute the max allowed sectors per IO: The controller info has two
5389 * limits on max sectors. Driver should use the minimum of these two.
5391 * 1 << stripe_sz_ops.min = max sectors per strip
5393 * Note that older firmwares ( < FW ver 30) didn't report information
5394 * to calculate max_sectors_1. So the number ended up as zero always.
5397 ctrl_info = instance->ctrl_info;
5399 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5400 le16_to_cpu(ctrl_info->max_strips_per_io);
5401 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
5403 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
5405 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5406 instance->passive = ctrl_info->cluster.passive;
5407 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
5408 instance->UnevenSpanSupport =
5409 ctrl_info->adapterOperations2.supportUnevenSpans;
5410 if (instance->UnevenSpanSupport) {
5411 struct fusion_context *fusion = instance->ctrl_context;
5412 if (MR_ValidateMapInfo(instance))
5413 fusion->fast_path_io = 1;
5415 fusion->fast_path_io = 0;
5418 if (ctrl_info->host_interface.SRIOV) {
5419 instance->requestorId = ctrl_info->iov.requestorId;
5420 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5421 if (!ctrl_info->adapterOperations2.activePassive)
5422 instance->PlasmaFW111 = 1;
5424 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5425 instance->PlasmaFW111 ? "1.11" : "new");
5427 if (instance->PlasmaFW111) {
5428 iovPtr = (struct IOV_111 *)
5429 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5430 instance->requestorId = iovPtr->requestorId;
5433 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5434 instance->requestorId);
5437 instance->crash_dump_fw_support =
5438 ctrl_info->adapterOperations3.supportCrashDump;
5439 instance->crash_dump_drv_support =
5440 (instance->crash_dump_fw_support &&
5441 instance->crash_dump_buf);
5442 if (instance->crash_dump_drv_support)
5443 megasas_set_crash_dump_params(instance,
5444 MR_CRASH_BUF_TURN_OFF);
5447 if (instance->crash_dump_buf)
5448 pci_free_consistent(instance->pdev,
5450 instance->crash_dump_buf,
5451 instance->crash_dump_h);
5452 instance->crash_dump_buf = NULL;
5456 dev_info(&instance->pdev->dev,
5457 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5458 le16_to_cpu(ctrl_info->pci.vendor_id),
5459 le16_to_cpu(ctrl_info->pci.device_id),
5460 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5461 le16_to_cpu(ctrl_info->pci.sub_device_id));
5462 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5463 instance->UnevenSpanSupport ? "yes" : "no");
5464 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5465 instance->crash_dump_drv_support ? "yes" : "no");
5466 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5467 instance->use_seqnum_jbod_fp ? "yes" : "no");
5470 instance->max_sectors_per_req = instance->max_num_sge *
5471 SGE_BUFFER_SIZE / 512;
5472 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5473 instance->max_sectors_per_req = tmp_sectors;
5475 /* Check for valid throttlequeuedepth module parameter */
5476 if (throttlequeuedepth &&
5477 throttlequeuedepth <= instance->max_scsi_cmds)
5478 instance->throttlequeuedepth = throttlequeuedepth;
5480 instance->throttlequeuedepth =
5481 MEGASAS_THROTTLE_QUEUE_DEPTH;
5483 if ((resetwaittime < 1) ||
5484 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
5485 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5487 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5488 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
5490 /* Launch SR-IOV heartbeat timer */
5491 if (instance->requestorId) {
5492 if (!megasas_sriov_start_heartbeat(instance, 1))
5493 megasas_start_timer(instance,
5494 &instance->sriov_heartbeat_timer,
5495 megasas_sriov_heartbeat_handler,
5496 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5498 instance->skip_heartbeat_timer_del = 1;
5503 fail_get_ld_pd_list:
5504 instance->instancet->disable_intr(instance);
5506 megasas_destroy_irqs(instance);
5508 if (instance->msix_vectors)
5509 pci_free_irq_vectors(instance->pdev);
5510 instance->msix_vectors = 0;
5512 kfree(instance->ctrl_info);
5513 instance->ctrl_info = NULL;
5514 iounmap(instance->reg_set);
5517 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
5519 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5520 __func__, __LINE__);
5525 * megasas_release_mfi - Reverses the FW initialization
5526 * @instance: Adapter soft state
5528 static void megasas_release_mfi(struct megasas_instance *instance)
5530 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
5532 if (instance->reply_queue)
5533 pci_free_consistent(instance->pdev, reply_q_sz,
5534 instance->reply_queue, instance->reply_queue_h);
5536 megasas_free_cmds(instance);
5538 iounmap(instance->reg_set);
5540 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
5544 * megasas_get_seq_num - Gets latest event sequence numbers
5545 * @instance: Adapter soft state
5546 * @eli: FW event log sequence numbers information
5548 * FW maintains a log of all events in a non-volatile area. Upper layers would
5549 * usually find out the latest sequence number of the events, the seq number at
5550 * the boot etc. They would "read" all the events below the latest seq number
5551 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5552 * number), they would subsribe to AEN (asynchronous event notification) and
5553 * wait for the events to happen.
5556 megasas_get_seq_num(struct megasas_instance *instance,
5557 struct megasas_evt_log_info *eli)
5559 struct megasas_cmd *cmd;
5560 struct megasas_dcmd_frame *dcmd;
5561 struct megasas_evt_log_info *el_info;
5562 dma_addr_t el_info_h = 0;
5564 cmd = megasas_get_cmd(instance);
5570 dcmd = &cmd->frame->dcmd;
5571 el_info = pci_alloc_consistent(instance->pdev,
5572 sizeof(struct megasas_evt_log_info),
5576 megasas_return_cmd(instance, cmd);
5580 memset(el_info, 0, sizeof(*el_info));
5581 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5583 dcmd->cmd = MFI_CMD_DCMD;
5584 dcmd->cmd_status = 0x0;
5585 dcmd->sge_count = 1;
5586 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5589 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5590 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5591 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5592 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5594 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5597 * Copy the data back into callers buffer
5599 eli->newest_seq_num = el_info->newest_seq_num;
5600 eli->oldest_seq_num = el_info->oldest_seq_num;
5601 eli->clear_seq_num = el_info->clear_seq_num;
5602 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5603 eli->boot_seq_num = el_info->boot_seq_num;
5605 dev_err(&instance->pdev->dev, "DCMD failed "
5606 "from %s\n", __func__);
5608 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5609 el_info, el_info_h);
5611 megasas_return_cmd(instance, cmd);
5617 * megasas_register_aen - Registers for asynchronous event notification
5618 * @instance: Adapter soft state
5619 * @seq_num: The starting sequence number
5620 * @class_locale: Class of the event
5622 * This function subscribes for AEN for events beyond the @seq_num. It requests
5623 * to be notified if and only if the event is of type @class_locale
5626 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5627 u32 class_locale_word)
5630 struct megasas_cmd *cmd;
5631 struct megasas_dcmd_frame *dcmd;
5632 union megasas_evt_class_locale curr_aen;
5633 union megasas_evt_class_locale prev_aen;
5636 * If there an AEN pending already (aen_cmd), check if the
5637 * class_locale of that pending AEN is inclusive of the new
5638 * AEN request we currently have. If it is, then we don't have
5639 * to do anything. In other words, whichever events the current
5640 * AEN request is subscribing to, have already been subscribed
5643 * If the old_cmd is _not_ inclusive, then we have to abort
5644 * that command, form a class_locale that is superset of both
5645 * old and current and re-issue to the FW
5648 curr_aen.word = class_locale_word;
5650 if (instance->aen_cmd) {
5653 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
5655 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
5656 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
5657 dev_info(&instance->pdev->dev,
5658 "%s %d out of range class %d send by application\n",
5659 __func__, __LINE__, curr_aen.members.class);
5664 * A class whose enum value is smaller is inclusive of all
5665 * higher values. If a PROGRESS (= -1) was previously
5666 * registered, then a new registration requests for higher
5667 * classes need not be sent to FW. They are automatically
5670 * Locale numbers don't have such hierarchy. They are bitmap
5673 if ((prev_aen.members.class <= curr_aen.members.class) &&
5674 !((prev_aen.members.locale & curr_aen.members.locale) ^
5675 curr_aen.members.locale)) {
5677 * Previously issued event registration includes
5678 * current request. Nothing to do.
5682 curr_aen.members.locale |= prev_aen.members.locale;
5684 if (prev_aen.members.class < curr_aen.members.class)
5685 curr_aen.members.class = prev_aen.members.class;
5687 instance->aen_cmd->abort_aen = 1;
5688 ret_val = megasas_issue_blocked_abort_cmd(instance,
5693 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
5694 "previous AEN command\n");
5700 cmd = megasas_get_cmd(instance);
5705 dcmd = &cmd->frame->dcmd;
5707 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5710 * Prepare DCMD for aen registration
5712 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5714 dcmd->cmd = MFI_CMD_DCMD;
5715 dcmd->cmd_status = 0x0;
5716 dcmd->sge_count = 1;
5717 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5720 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5721 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5722 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
5723 instance->last_seq_num = seq_num;
5724 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5725 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5726 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
5728 if (instance->aen_cmd != NULL) {
5729 megasas_return_cmd(instance, cmd);
5734 * Store reference to the cmd used to register for AEN. When an
5735 * application wants us to register for AEN, we have to abort this
5736 * cmd and re-register with a new EVENT LOCALE supplied by that app
5738 instance->aen_cmd = cmd;
5741 * Issue the aen registration frame
5743 instance->instancet->issue_dcmd(instance, cmd);
5748 /* megasas_get_target_prop - Send DCMD with below details to firmware.
5750 * This DCMD will fetch few properties of LD/system PD defined
5751 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
5753 * DCMD send by drivers whenever new target is added to the OS.
5755 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
5756 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
5757 * 0 = system PD, 1 = LD.
5758 * dcmd.mbox.s[1] - TargetID for LD/system PD.
5759 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
5761 * @instance: Adapter soft state
5762 * @sdev: OS provided scsi device
5764 * Returns 0 on success non-zero on failure.
5767 megasas_get_target_prop(struct megasas_instance *instance,
5768 struct scsi_device *sdev)
5771 struct megasas_cmd *cmd;
5772 struct megasas_dcmd_frame *dcmd;
5773 u16 targetId = (sdev->channel % 2) + sdev->id;
5775 cmd = megasas_get_cmd(instance);
5778 dev_err(&instance->pdev->dev,
5779 "Failed to get cmd %s\n", __func__);
5783 dcmd = &cmd->frame->dcmd;
5785 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
5786 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5787 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
5789 dcmd->mbox.s[1] = cpu_to_le16(targetId);
5790 dcmd->cmd = MFI_CMD_DCMD;
5791 dcmd->cmd_status = 0xFF;
5792 dcmd->sge_count = 1;
5793 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5796 dcmd->data_xfer_len =
5797 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5798 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
5799 dcmd->sgl.sge32[0].phys_addr =
5800 cpu_to_le32(instance->tgt_prop_h);
5801 dcmd->sgl.sge32[0].length =
5802 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5804 if (instance->ctrl_context && !instance->mask_interrupts)
5805 ret = megasas_issue_blocked_cmd(instance,
5806 cmd, MFI_IO_TIMEOUT_SECS);
5808 ret = megasas_issue_polled(instance, cmd);
5812 switch (dcmd_timeout_ocr_possible(instance)) {
5814 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5815 megasas_reset_fusion(instance->host,
5816 MFI_IO_TIMEOUT_OCR);
5819 megaraid_sas_kill_hba(instance);
5821 case IGNORE_TIMEOUT:
5822 dev_info(&instance->pdev->dev,
5823 "Ignore DCMD timeout: %s %d\n",
5824 __func__, __LINE__);
5830 megasas_return_cmd(instance, cmd);
5832 if (ret != DCMD_SUCCESS)
5833 dev_err(&instance->pdev->dev,
5834 "return from %s %d return value %d\n",
5835 __func__, __LINE__, ret);
5841 * megasas_start_aen - Subscribes to AEN during driver load time
5842 * @instance: Adapter soft state
5844 static int megasas_start_aen(struct megasas_instance *instance)
5846 struct megasas_evt_log_info eli;
5847 union megasas_evt_class_locale class_locale;
5850 * Get the latest sequence number from FW
5852 memset(&eli, 0, sizeof(eli));
5854 if (megasas_get_seq_num(instance, &eli))
5858 * Register AEN with FW for latest sequence number plus 1
5860 class_locale.members.reserved = 0;
5861 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5862 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5864 return megasas_register_aen(instance,
5865 le32_to_cpu(eli.newest_seq_num) + 1,
5870 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5871 * @instance: Adapter soft state
5873 static int megasas_io_attach(struct megasas_instance *instance)
5875 struct Scsi_Host *host = instance->host;
5878 * Export parameters required by SCSI mid-layer
5880 host->unique_id = instance->unique_id;
5881 host->can_queue = instance->max_scsi_cmds;
5882 host->this_id = instance->init_id;
5883 host->sg_tablesize = instance->max_num_sge;
5885 if (instance->fw_support_ieee)
5886 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5889 * Check if the module parameter value for max_sectors can be used
5891 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5892 instance->max_sectors_per_req = max_sectors;
5895 if (((instance->pdev->device ==
5896 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5897 (instance->pdev->device ==
5898 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5899 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5900 instance->max_sectors_per_req = max_sectors;
5902 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
5903 "and <= %d (or < 1MB for GEN2 controller)\n",
5904 instance->max_sectors_per_req);
5909 host->max_sectors = instance->max_sectors_per_req;
5910 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
5911 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5912 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5913 host->max_lun = MEGASAS_MAX_LUN;
5914 host->max_cmd_len = 16;
5917 * Notify the mid-layer about the new controller
5919 if (scsi_add_host(host, &instance->pdev->dev)) {
5920 dev_err(&instance->pdev->dev,
5921 "Failed to add host from %s %d\n",
5922 __func__, __LINE__);
5930 megasas_set_dma_mask(struct pci_dev *pdev)
5933 * All our controllers are capable of performing 64-bit DMA
5936 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
5938 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5939 goto fail_set_dma_mask;
5942 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5943 goto fail_set_dma_mask;
5946 * Ensure that all data structures are allocated in 32-bit
5949 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5950 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5951 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5952 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5953 dev_info(&pdev->dev, "set 32bit DMA mask"
5954 "and 32 bit consistent mask\n");
5956 goto fail_set_dma_mask;
5966 * megasas_probe_one - PCI hotplug entry point
5967 * @pdev: PCI device structure
5968 * @id: PCI ids of supported hotplugged adapter
5970 static int megasas_probe_one(struct pci_dev *pdev,
5971 const struct pci_device_id *id)
5974 struct Scsi_Host *host;
5975 struct megasas_instance *instance;
5977 struct fusion_context *fusion = NULL;
5979 /* Reset MSI-X in the kdump kernel */
5980 if (reset_devices) {
5981 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5983 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
5985 if (control & PCI_MSIX_FLAGS_ENABLE) {
5986 dev_info(&pdev->dev, "resetting MSI-X\n");
5987 pci_write_config_word(pdev,
5988 pos + PCI_MSIX_FLAGS,
5990 ~PCI_MSIX_FLAGS_ENABLE);
5996 * PCI prepping: enable device set bus mastering and dma mask
5998 rval = pci_enable_device_mem(pdev);
6004 pci_set_master(pdev);
6006 if (megasas_set_dma_mask(pdev))
6007 goto fail_set_dma_mask;
6009 host = scsi_host_alloc(&megasas_template,
6010 sizeof(struct megasas_instance));
6013 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
6014 goto fail_alloc_instance;
6017 instance = (struct megasas_instance *)host->hostdata;
6018 memset(instance, 0, sizeof(*instance));
6019 atomic_set(&instance->fw_reset_no_pci_access, 0);
6020 instance->pdev = pdev;
6022 switch (instance->pdev->device) {
6023 case PCI_DEVICE_ID_LSI_VENTURA:
6024 case PCI_DEVICE_ID_LSI_HARPOON:
6025 case PCI_DEVICE_ID_LSI_TOMCAT:
6026 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6027 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6028 instance->is_ventura = true;
6029 case PCI_DEVICE_ID_LSI_FUSION:
6030 case PCI_DEVICE_ID_LSI_PLASMA:
6031 case PCI_DEVICE_ID_LSI_INVADER:
6032 case PCI_DEVICE_ID_LSI_FURY:
6033 case PCI_DEVICE_ID_LSI_INTRUDER:
6034 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6035 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6036 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6038 if (megasas_alloc_fusion_context(instance)) {
6039 megasas_free_fusion_context(instance);
6040 goto fail_alloc_dma_buf;
6042 fusion = instance->ctrl_context;
6044 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
6045 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
6046 fusion->adapter_type = THUNDERBOLT_SERIES;
6047 else if (instance->is_ventura)
6048 fusion->adapter_type = VENTURA_SERIES;
6050 fusion->adapter_type = INVADER_SERIES;
6053 default: /* For all other supported controllers */
6055 instance->producer =
6056 pci_alloc_consistent(pdev, sizeof(u32),
6057 &instance->producer_h);
6058 instance->consumer =
6059 pci_alloc_consistent(pdev, sizeof(u32),
6060 &instance->consumer_h);
6062 if (!instance->producer || !instance->consumer) {
6063 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
6064 "memory for producer, consumer\n");
6065 goto fail_alloc_dma_buf;
6068 *instance->producer = 0;
6069 *instance->consumer = 0;
6073 /* Crash dump feature related initialisation*/
6074 instance->drv_buf_index = 0;
6075 instance->drv_buf_alloc = 0;
6076 instance->crash_dump_fw_support = 0;
6077 instance->crash_dump_app_support = 0;
6078 instance->fw_crash_state = UNAVAILABLE;
6079 spin_lock_init(&instance->crashdump_lock);
6080 instance->crash_dump_buf = NULL;
6082 megasas_poll_wait_aen = 0;
6083 instance->flag_ieee = 0;
6084 instance->ev = NULL;
6085 instance->issuepend_done = 1;
6086 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
6087 instance->is_imr = 0;
6089 instance->evt_detail = pci_alloc_consistent(pdev,
6091 megasas_evt_detail),
6092 &instance->evt_detail_h);
6094 if (!instance->evt_detail) {
6095 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
6096 "event detail structure\n");
6097 goto fail_alloc_dma_buf;
6100 if (!reset_devices) {
6101 instance->system_info_buf = pci_zalloc_consistent(pdev,
6102 sizeof(struct MR_DRV_SYSTEM_INFO),
6103 &instance->system_info_h);
6104 if (!instance->system_info_buf)
6105 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
6107 instance->pd_info = pci_alloc_consistent(pdev,
6108 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
6110 if (!instance->pd_info)
6111 dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
6113 instance->tgt_prop = pci_alloc_consistent(pdev,
6114 sizeof(struct MR_TARGET_PROPERTIES), &instance->tgt_prop_h);
6116 if (!instance->tgt_prop)
6117 dev_err(&instance->pdev->dev, "Failed to alloc mem for tgt_prop\n");
6119 instance->crash_dump_buf = pci_alloc_consistent(pdev,
6121 &instance->crash_dump_h);
6122 if (!instance->crash_dump_buf)
6123 dev_err(&pdev->dev, "Can't allocate Firmware "
6124 "crash dump DMA buffer\n");
6128 * Initialize locks and queues
6130 INIT_LIST_HEAD(&instance->cmd_pool);
6131 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
6133 atomic_set(&instance->fw_outstanding,0);
6135 init_waitqueue_head(&instance->int_cmd_wait_q);
6136 init_waitqueue_head(&instance->abort_cmd_wait_q);
6138 spin_lock_init(&instance->mfi_pool_lock);
6139 spin_lock_init(&instance->hba_lock);
6140 spin_lock_init(&instance->stream_lock);
6141 spin_lock_init(&instance->completion_lock);
6143 mutex_init(&instance->reset_mutex);
6144 mutex_init(&instance->hba_mutex);
6147 * Initialize PCI related and misc parameters
6149 instance->host = host;
6150 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
6151 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
6152 instance->ctrl_info = NULL;
6155 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
6156 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
6157 instance->flag_ieee = 1;
6159 megasas_dbg_lvl = 0;
6161 instance->unload = 1;
6162 instance->last_time = 0;
6163 instance->disableOnlineCtrlReset = 1;
6164 instance->UnevenSpanSupport = 0;
6166 if (instance->ctrl_context) {
6167 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
6168 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
6170 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
6173 * Initialize MFI Firmware
6175 if (megasas_init_fw(instance))
6178 if (instance->requestorId) {
6179 if (instance->PlasmaFW111) {
6180 instance->vf_affiliation_111 =
6181 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
6182 &instance->vf_affiliation_111_h);
6183 if (!instance->vf_affiliation_111)
6184 dev_warn(&pdev->dev, "Can't allocate "
6185 "memory for VF affiliation buffer\n");
6187 instance->vf_affiliation =
6188 pci_alloc_consistent(pdev,
6189 (MAX_LOGICAL_DRIVES + 1) *
6190 sizeof(struct MR_LD_VF_AFFILIATION),
6191 &instance->vf_affiliation_h);
6192 if (!instance->vf_affiliation)
6193 dev_warn(&pdev->dev, "Can't allocate "
6194 "memory for VF affiliation buffer\n");
6199 * Store instance in PCI softstate
6201 pci_set_drvdata(pdev, instance);
6204 * Add this controller to megasas_mgmt_info structure so that it
6205 * can be exported to management applications
6207 megasas_mgmt_info.count++;
6208 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
6209 megasas_mgmt_info.max_index++;
6212 * Register with SCSI mid-layer
6214 if (megasas_io_attach(instance))
6215 goto fail_io_attach;
6217 instance->unload = 0;
6219 * Trigger SCSI to scan our drives
6221 scsi_scan_host(host);
6224 * Initiate AEN (Asynchronous Event Notification)
6226 if (megasas_start_aen(instance)) {
6227 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
6228 goto fail_start_aen;
6231 /* Get current SR-IOV LD/VF affiliation */
6232 if (instance->requestorId)
6233 megasas_get_ld_vf_affiliation(instance, 1);
6239 megasas_mgmt_info.count--;
6240 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
6241 megasas_mgmt_info.max_index--;
6243 instance->instancet->disable_intr(instance);
6244 megasas_destroy_irqs(instance);
6246 if (instance->ctrl_context)
6247 megasas_release_fusion(instance);
6249 megasas_release_mfi(instance);
6250 if (instance->msix_vectors)
6251 pci_free_irq_vectors(instance->pdev);
6254 if (instance->evt_detail)
6255 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6256 instance->evt_detail,
6257 instance->evt_detail_h);
6259 if (instance->pd_info)
6260 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6262 instance->pd_info_h);
6263 if (instance->tgt_prop)
6264 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6266 instance->tgt_prop_h);
6267 if (instance->producer)
6268 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6269 instance->producer_h);
6270 if (instance->consumer)
6271 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6272 instance->consumer_h);
6273 scsi_host_put(host);
6275 fail_alloc_instance:
6277 pci_disable_device(pdev);
6283 * megasas_flush_cache - Requests FW to flush all its caches
6284 * @instance: Adapter soft state
6286 static void megasas_flush_cache(struct megasas_instance *instance)
6288 struct megasas_cmd *cmd;
6289 struct megasas_dcmd_frame *dcmd;
6291 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6294 cmd = megasas_get_cmd(instance);
6299 dcmd = &cmd->frame->dcmd;
6301 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6303 dcmd->cmd = MFI_CMD_DCMD;
6304 dcmd->cmd_status = 0x0;
6305 dcmd->sge_count = 0;
6306 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
6309 dcmd->data_xfer_len = 0;
6310 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
6311 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6313 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6315 dev_err(&instance->pdev->dev,
6316 "return from %s %d\n", __func__, __LINE__);
6320 megasas_return_cmd(instance, cmd);
6324 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6325 * @instance: Adapter soft state
6326 * @opcode: Shutdown/Hibernate
6328 static void megasas_shutdown_controller(struct megasas_instance *instance,
6331 struct megasas_cmd *cmd;
6332 struct megasas_dcmd_frame *dcmd;
6334 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6337 cmd = megasas_get_cmd(instance);
6342 if (instance->aen_cmd)
6343 megasas_issue_blocked_abort_cmd(instance,
6344 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
6345 if (instance->map_update_cmd)
6346 megasas_issue_blocked_abort_cmd(instance,
6347 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
6348 if (instance->jbod_seq_cmd)
6349 megasas_issue_blocked_abort_cmd(instance,
6350 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
6352 dcmd = &cmd->frame->dcmd;
6354 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6356 dcmd->cmd = MFI_CMD_DCMD;
6357 dcmd->cmd_status = 0x0;
6358 dcmd->sge_count = 0;
6359 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
6362 dcmd->data_xfer_len = 0;
6363 dcmd->opcode = cpu_to_le32(opcode);
6365 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6367 dev_err(&instance->pdev->dev,
6368 "return from %s %d\n", __func__, __LINE__);
6372 megasas_return_cmd(instance, cmd);
6377 * megasas_suspend - driver suspend entry point
6378 * @pdev: PCI device structure
6379 * @state: PCI power state to suspend routine
6382 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6384 struct Scsi_Host *host;
6385 struct megasas_instance *instance;
6387 instance = pci_get_drvdata(pdev);
6388 host = instance->host;
6389 instance->unload = 1;
6391 /* Shutdown SR-IOV heartbeat timer */
6392 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6393 del_timer_sync(&instance->sriov_heartbeat_timer);
6395 megasas_flush_cache(instance);
6396 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
6398 /* cancel the delayed work if this work still in queue */
6399 if (instance->ev != NULL) {
6400 struct megasas_aen_event *ev = instance->ev;
6401 cancel_delayed_work_sync(&ev->hotplug_work);
6402 instance->ev = NULL;
6405 tasklet_kill(&instance->isr_tasklet);
6407 pci_set_drvdata(instance->pdev, instance);
6408 instance->instancet->disable_intr(instance);
6410 megasas_destroy_irqs(instance);
6412 if (instance->msix_vectors)
6413 pci_free_irq_vectors(instance->pdev);
6415 pci_save_state(pdev);
6416 pci_disable_device(pdev);
6418 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6424 * megasas_resume- driver resume entry point
6425 * @pdev: PCI device structure
6428 megasas_resume(struct pci_dev *pdev)
6431 struct Scsi_Host *host;
6432 struct megasas_instance *instance;
6433 int irq_flags = PCI_IRQ_LEGACY;
6435 instance = pci_get_drvdata(pdev);
6436 host = instance->host;
6437 pci_set_power_state(pdev, PCI_D0);
6438 pci_enable_wake(pdev, PCI_D0, 0);
6439 pci_restore_state(pdev);
6442 * PCI prepping: enable device set bus mastering and dma mask
6444 rval = pci_enable_device_mem(pdev);
6447 dev_err(&pdev->dev, "Enable device failed\n");
6451 pci_set_master(pdev);
6453 if (megasas_set_dma_mask(pdev))
6454 goto fail_set_dma_mask;
6457 * Initialize MFI Firmware
6460 atomic_set(&instance->fw_outstanding, 0);
6463 * We expect the FW state to be READY
6465 if (megasas_transition_to_ready(instance, 0))
6466 goto fail_ready_state;
6468 /* Now re-enable MSI-X */
6469 if (instance->msix_vectors) {
6470 irq_flags = PCI_IRQ_MSIX;
6471 if (smp_affinity_enable)
6472 irq_flags |= PCI_IRQ_AFFINITY;
6474 rval = pci_alloc_irq_vectors(instance->pdev, 1,
6475 instance->msix_vectors ?
6476 instance->msix_vectors : 1, irq_flags);
6478 goto fail_reenable_msix;
6480 if (instance->ctrl_context) {
6481 megasas_reset_reply_desc(instance);
6482 if (megasas_ioc_init_fusion(instance)) {
6483 megasas_free_cmds(instance);
6484 megasas_free_cmds_fusion(instance);
6487 if (!megasas_get_map_info(instance))
6488 megasas_sync_map_info(instance);
6490 *instance->producer = 0;
6491 *instance->consumer = 0;
6492 if (megasas_issue_init_mfi(instance))
6496 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6497 (unsigned long)instance);
6499 if (instance->msix_vectors ?
6500 megasas_setup_irqs_msix(instance, 0) :
6501 megasas_setup_irqs_ioapic(instance))
6504 /* Re-launch SR-IOV heartbeat timer */
6505 if (instance->requestorId) {
6506 if (!megasas_sriov_start_heartbeat(instance, 0))
6507 megasas_start_timer(instance,
6508 &instance->sriov_heartbeat_timer,
6509 megasas_sriov_heartbeat_handler,
6510 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
6512 instance->skip_heartbeat_timer_del = 1;
6517 instance->instancet->enable_intr(instance);
6518 megasas_setup_jbod_map(instance);
6519 instance->unload = 0;
6522 * Initiate AEN (Asynchronous Event Notification)
6524 if (megasas_start_aen(instance))
6525 dev_err(&instance->pdev->dev, "Start AEN failed\n");
6530 if (instance->evt_detail)
6531 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6532 instance->evt_detail,
6533 instance->evt_detail_h);
6535 if (instance->pd_info)
6536 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6538 instance->pd_info_h);
6539 if (instance->tgt_prop)
6540 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6542 instance->tgt_prop_h);
6543 if (instance->producer)
6544 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6545 instance->producer_h);
6546 if (instance->consumer)
6547 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6548 instance->consumer_h);
6549 scsi_host_put(host);
6555 pci_disable_device(pdev);
6560 #define megasas_suspend NULL
6561 #define megasas_resume NULL
6565 megasas_wait_for_adapter_operational(struct megasas_instance *instance)
6567 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
6570 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6573 for (i = 0; i < wait_time; i++) {
6574 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
6577 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
6578 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
6583 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6584 dev_info(&instance->pdev->dev, "%s timed out while waiting for HBA to recover.\n",
6593 * megasas_detach_one - PCI hot"un"plug entry point
6594 * @pdev: PCI device structure
6596 static void megasas_detach_one(struct pci_dev *pdev)
6599 struct Scsi_Host *host;
6600 struct megasas_instance *instance;
6601 struct fusion_context *fusion;
6604 instance = pci_get_drvdata(pdev);
6605 instance->unload = 1;
6606 host = instance->host;
6607 fusion = instance->ctrl_context;
6609 /* Shutdown SR-IOV heartbeat timer */
6610 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6611 del_timer_sync(&instance->sriov_heartbeat_timer);
6613 if (instance->fw_crash_state != UNAVAILABLE)
6614 megasas_free_host_crash_buffer(instance);
6615 scsi_remove_host(instance->host);
6617 if (megasas_wait_for_adapter_operational(instance))
6618 goto skip_firing_dcmds;
6620 megasas_flush_cache(instance);
6621 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
6624 /* cancel the delayed work if this work still in queue*/
6625 if (instance->ev != NULL) {
6626 struct megasas_aen_event *ev = instance->ev;
6627 cancel_delayed_work_sync(&ev->hotplug_work);
6628 instance->ev = NULL;
6631 /* cancel all wait events */
6632 wake_up_all(&instance->int_cmd_wait_q);
6634 tasklet_kill(&instance->isr_tasklet);
6637 * Take the instance off the instance array. Note that we will not
6638 * decrement the max_index. We let this array be sparse array
6640 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6641 if (megasas_mgmt_info.instance[i] == instance) {
6642 megasas_mgmt_info.count--;
6643 megasas_mgmt_info.instance[i] = NULL;
6649 instance->instancet->disable_intr(instance);
6651 megasas_destroy_irqs(instance);
6653 if (instance->msix_vectors)
6654 pci_free_irq_vectors(instance->pdev);
6656 if (instance->is_ventura) {
6657 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
6658 kfree(fusion->stream_detect_by_ld[i]);
6659 kfree(fusion->stream_detect_by_ld);
6660 fusion->stream_detect_by_ld = NULL;
6664 if (instance->ctrl_context) {
6665 megasas_release_fusion(instance);
6666 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6667 (sizeof(struct MR_PD_CFG_SEQ) *
6668 (MAX_PHYSICAL_DEVICES - 1));
6669 for (i = 0; i < 2 ; i++) {
6670 if (fusion->ld_map[i])
6671 dma_free_coherent(&instance->pdev->dev,
6674 fusion->ld_map_phys[i]);
6675 if (fusion->ld_drv_map[i]) {
6676 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
6677 vfree(fusion->ld_drv_map[i]);
6679 free_pages((ulong)fusion->ld_drv_map[i],
6680 fusion->drv_map_pages);
6683 if (fusion->pd_seq_sync[i])
6684 dma_free_coherent(&instance->pdev->dev,
6686 fusion->pd_seq_sync[i],
6687 fusion->pd_seq_phys[i]);
6689 megasas_free_fusion_context(instance);
6691 megasas_release_mfi(instance);
6692 pci_free_consistent(pdev, sizeof(u32),
6694 instance->producer_h);
6695 pci_free_consistent(pdev, sizeof(u32),
6697 instance->consumer_h);
6700 kfree(instance->ctrl_info);
6702 if (instance->evt_detail)
6703 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6704 instance->evt_detail, instance->evt_detail_h);
6705 if (instance->pd_info)
6706 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6708 instance->pd_info_h);
6709 if (instance->tgt_prop)
6710 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6712 instance->tgt_prop_h);
6713 if (instance->vf_affiliation)
6714 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6715 sizeof(struct MR_LD_VF_AFFILIATION),
6716 instance->vf_affiliation,
6717 instance->vf_affiliation_h);
6719 if (instance->vf_affiliation_111)
6720 pci_free_consistent(pdev,
6721 sizeof(struct MR_LD_VF_AFFILIATION_111),
6722 instance->vf_affiliation_111,
6723 instance->vf_affiliation_111_h);
6725 if (instance->hb_host_mem)
6726 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6727 instance->hb_host_mem,
6728 instance->hb_host_mem_h);
6730 if (instance->crash_dump_buf)
6731 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6732 instance->crash_dump_buf, instance->crash_dump_h);
6734 if (instance->system_info_buf)
6735 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6736 instance->system_info_buf, instance->system_info_h);
6738 scsi_host_put(host);
6740 pci_disable_device(pdev);
6744 * megasas_shutdown - Shutdown entry point
6745 * @device: Generic device structure
6747 static void megasas_shutdown(struct pci_dev *pdev)
6749 struct megasas_instance *instance = pci_get_drvdata(pdev);
6751 instance->unload = 1;
6753 if (megasas_wait_for_adapter_operational(instance))
6754 goto skip_firing_dcmds;
6756 megasas_flush_cache(instance);
6757 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
6760 instance->instancet->disable_intr(instance);
6761 megasas_destroy_irqs(instance);
6763 if (instance->msix_vectors)
6764 pci_free_irq_vectors(instance->pdev);
6768 * megasas_mgmt_open - char node "open" entry point
6770 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6773 * Allow only those users with admin rights
6775 if (!capable(CAP_SYS_ADMIN))
6782 * megasas_mgmt_fasync - Async notifier registration from applications
6784 * This function adds the calling process to a driver global queue. When an
6785 * event occurs, SIGIO will be sent to all processes in this queue.
6787 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6791 mutex_lock(&megasas_async_queue_mutex);
6793 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6795 mutex_unlock(&megasas_async_queue_mutex);
6798 /* For sanity check when we get ioctl */
6799 filep->private_data = filep;
6803 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6809 * megasas_mgmt_poll - char node "poll" entry point
6811 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6814 unsigned long flags;
6816 poll_wait(file, &megasas_poll_wait, wait);
6817 spin_lock_irqsave(&poll_aen_lock, flags);
6818 if (megasas_poll_wait_aen)
6819 mask = (POLLIN | POLLRDNORM);
6822 megasas_poll_wait_aen = 0;
6823 spin_unlock_irqrestore(&poll_aen_lock, flags);
6828 * megasas_set_crash_dump_params_ioctl:
6829 * Send CRASH_DUMP_MODE DCMD to all controllers
6830 * @cmd: MFI command frame
6833 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
6835 struct megasas_instance *local_instance;
6839 crash_support = cmd->frame->dcmd.mbox.w[0];
6841 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6842 local_instance = megasas_mgmt_info.instance[i];
6843 if (local_instance && local_instance->crash_dump_drv_support) {
6844 if ((atomic_read(&local_instance->adprecovery) ==
6845 MEGASAS_HBA_OPERATIONAL) &&
6846 !megasas_set_crash_dump_params(local_instance,
6848 local_instance->crash_dump_app_support =
6850 dev_info(&local_instance->pdev->dev,
6851 "Application firmware crash "
6852 "dump mode set success\n");
6855 dev_info(&local_instance->pdev->dev,
6856 "Application firmware crash "
6857 "dump mode set failed\n");
6866 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6867 * @instance: Adapter soft state
6868 * @argp: User's ioctl packet
6871 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6872 struct megasas_iocpacket __user * user_ioc,
6873 struct megasas_iocpacket *ioc)
6875 struct megasas_sge32 *kern_sge32;
6876 struct megasas_cmd *cmd;
6877 void *kbuff_arr[MAX_IOCTL_SGE];
6878 dma_addr_t buf_handle = 0;
6881 dma_addr_t sense_handle;
6882 unsigned long *sense_ptr;
6885 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6887 if (ioc->sge_count > MAX_IOCTL_SGE) {
6888 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
6889 ioc->sge_count, MAX_IOCTL_SGE);
6893 cmd = megasas_get_cmd(instance);
6895 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
6900 * User's IOCTL packet has 2 frames (maximum). Copy those two
6901 * frames into our cmd's frames. cmd->frame's context will get
6902 * overwritten when we copy from user's frames. So set that value
6905 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
6906 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
6907 cmd->frame->hdr.pad_0 = 0;
6908 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6910 MFI_FRAME_SENSE64));
6911 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
6913 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
6914 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
6915 megasas_return_cmd(instance, cmd);
6920 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6921 error = megasas_set_crash_dump_params_ioctl(cmd);
6922 megasas_return_cmd(instance, cmd);
6927 * The management interface between applications and the fw uses
6928 * MFI frames. E.g, RAID configuration changes, LD property changes
6929 * etc are accomplishes through different kinds of MFI frames. The
6930 * driver needs to care only about substituting user buffers with
6931 * kernel buffers in SGLs. The location of SGL is embedded in the
6932 * struct iocpacket itself.
6934 kern_sge32 = (struct megasas_sge32 *)
6935 ((unsigned long)cmd->frame + ioc->sgl_off);
6938 * For each user buffer, create a mirror buffer and copy in
6940 for (i = 0; i < ioc->sge_count; i++) {
6941 if (!ioc->sgl[i].iov_len)
6944 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
6945 ioc->sgl[i].iov_len,
6946 &buf_handle, GFP_KERNEL);
6947 if (!kbuff_arr[i]) {
6948 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6949 "kernel SGL buffer for IOCTL\n");
6955 * We don't change the dma_coherent_mask, so
6956 * pci_alloc_consistent only returns 32bit addresses
6958 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6959 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
6962 * We created a kernel buffer corresponding to the
6963 * user buffer. Now copy in from the user buffer
6965 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6966 (u32) (ioc->sgl[i].iov_len))) {
6972 if (ioc->sense_len) {
6973 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6974 &sense_handle, GFP_KERNEL);
6981 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
6982 *sense_ptr = cpu_to_le32(sense_handle);
6986 * Set the sync_cmd flag so that the ISR knows not to complete this
6987 * cmd to the SCSI mid-layer
6990 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
6992 dev_err(&instance->pdev->dev,
6993 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
6994 __func__, __LINE__, opcode, cmd->cmd_status_drv);
7000 if (instance->unload == 1) {
7001 dev_info(&instance->pdev->dev, "Driver unload is in progress "
7002 "don't submit data to application\n");
7006 * copy out the kernel buffers to user buffers
7008 for (i = 0; i < ioc->sge_count; i++) {
7009 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7010 ioc->sgl[i].iov_len)) {
7017 * copy out the sense
7019 if (ioc->sense_len) {
7021 * sense_ptr points to the location that has the user
7022 * sense buffer address
7024 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7027 if (copy_to_user((void __user *)((unsigned long)
7028 get_unaligned((unsigned long *)sense_ptr)),
7029 sense, ioc->sense_len)) {
7030 dev_err(&instance->pdev->dev, "Failed to copy out to user "
7038 * copy the status codes returned by the fw
7040 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7041 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
7042 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
7048 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
7049 sense, sense_handle);
7052 for (i = 0; i < ioc->sge_count; i++) {
7054 dma_free_coherent(&instance->pdev->dev,
7055 le32_to_cpu(kern_sge32[i].length),
7057 le32_to_cpu(kern_sge32[i].phys_addr));
7058 kbuff_arr[i] = NULL;
7062 megasas_return_cmd(instance, cmd);
7066 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7068 struct megasas_iocpacket __user *user_ioc =
7069 (struct megasas_iocpacket __user *)arg;
7070 struct megasas_iocpacket *ioc;
7071 struct megasas_instance *instance;
7074 unsigned long flags;
7075 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
7077 ioc = memdup_user(user_ioc, sizeof(*ioc));
7079 return PTR_ERR(ioc);
7081 instance = megasas_lookup_instance(ioc->host_no);
7087 /* Adjust ioctl wait time for VF mode */
7088 if (instance->requestorId)
7089 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7091 /* Block ioctls in VF mode */
7092 if (instance->requestorId && !allow_vf_ioctls) {
7097 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
7098 dev_err(&instance->pdev->dev, "Controller in crit error\n");
7103 if (instance->unload == 1) {
7108 if (down_interruptible(&instance->ioctl_sem)) {
7109 error = -ERESTARTSYS;
7113 for (i = 0; i < wait_time; i++) {
7115 spin_lock_irqsave(&instance->hba_lock, flags);
7116 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
7117 spin_unlock_irqrestore(&instance->hba_lock, flags);
7120 spin_unlock_irqrestore(&instance->hba_lock, flags);
7122 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
7123 dev_notice(&instance->pdev->dev, "waiting"
7124 "for controller reset to finish\n");
7130 spin_lock_irqsave(&instance->hba_lock, flags);
7131 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
7132 spin_unlock_irqrestore(&instance->hba_lock, flags);
7134 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
7138 spin_unlock_irqrestore(&instance->hba_lock, flags);
7140 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
7142 up(&instance->ioctl_sem);
7149 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7151 struct megasas_instance *instance;
7152 struct megasas_aen aen;
7155 unsigned long flags;
7156 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
7158 if (file->private_data != file) {
7159 printk(KERN_DEBUG "megasas: fasync_helper was not "
7164 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7167 instance = megasas_lookup_instance(aen.host_no);
7172 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
7176 if (instance->unload == 1) {
7180 for (i = 0; i < wait_time; i++) {
7182 spin_lock_irqsave(&instance->hba_lock, flags);
7183 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
7184 spin_unlock_irqrestore(&instance->hba_lock,
7189 spin_unlock_irqrestore(&instance->hba_lock, flags);
7191 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
7192 dev_notice(&instance->pdev->dev, "waiting for"
7193 "controller reset to finish\n");
7199 spin_lock_irqsave(&instance->hba_lock, flags);
7200 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
7201 spin_unlock_irqrestore(&instance->hba_lock, flags);
7202 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
7205 spin_unlock_irqrestore(&instance->hba_lock, flags);
7207 mutex_lock(&instance->reset_mutex);
7208 error = megasas_register_aen(instance, aen.seq_num,
7209 aen.class_locale_word);
7210 mutex_unlock(&instance->reset_mutex);
7215 * megasas_mgmt_ioctl - char node ioctl entry point
7218 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7221 case MEGASAS_IOC_FIRMWARE:
7222 return megasas_mgmt_ioctl_fw(file, arg);
7224 case MEGASAS_IOC_GET_AEN:
7225 return megasas_mgmt_ioctl_aen(file, arg);
7231 #ifdef CONFIG_COMPAT
7232 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
7234 struct compat_megasas_iocpacket __user *cioc =
7235 (struct compat_megasas_iocpacket __user *)arg;
7236 struct megasas_iocpacket __user *ioc =
7237 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
7241 u32 local_sense_off;
7242 u32 local_sense_len;
7245 if (clear_user(ioc, sizeof(*ioc)))
7248 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
7249 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
7250 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
7251 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
7252 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
7253 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7257 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7258 * sense_len is not null, so prepare the 64bit value under
7259 * the same condition.
7261 if (get_user(local_sense_off, &ioc->sense_off) ||
7262 get_user(local_sense_len, &ioc->sense_len) ||
7263 get_user(user_sense_off, &cioc->sense_off))
7266 if (local_sense_len) {
7267 void __user **sense_ioc_ptr =
7268 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
7269 compat_uptr_t *sense_cioc_ptr =
7270 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
7271 if (get_user(ptr, sense_cioc_ptr) ||
7272 put_user(compat_ptr(ptr), sense_ioc_ptr))
7276 for (i = 0; i < MAX_IOCTL_SGE; i++) {
7277 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
7278 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
7279 copy_in_user(&ioc->sgl[i].iov_len,
7280 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
7284 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
7286 if (copy_in_user(&cioc->frame.hdr.cmd_status,
7287 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
7288 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
7295 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
7299 case MEGASAS_IOC_FIRMWARE32:
7300 return megasas_mgmt_compat_ioctl_fw(file, arg);
7301 case MEGASAS_IOC_GET_AEN:
7302 return megasas_mgmt_ioctl_aen(file, arg);
7310 * File operations structure for management interface
7312 static const struct file_operations megasas_mgmt_fops = {
7313 .owner = THIS_MODULE,
7314 .open = megasas_mgmt_open,
7315 .fasync = megasas_mgmt_fasync,
7316 .unlocked_ioctl = megasas_mgmt_ioctl,
7317 .poll = megasas_mgmt_poll,
7318 #ifdef CONFIG_COMPAT
7319 .compat_ioctl = megasas_mgmt_compat_ioctl,
7321 .llseek = noop_llseek,
7325 * PCI hotplug support registration structure
7327 static struct pci_driver megasas_pci_driver = {
7329 .name = "megaraid_sas",
7330 .id_table = megasas_pci_table,
7331 .probe = megasas_probe_one,
7332 .remove = megasas_detach_one,
7333 .suspend = megasas_suspend,
7334 .resume = megasas_resume,
7335 .shutdown = megasas_shutdown,
7339 * Sysfs driver attributes
7341 static ssize_t version_show(struct device_driver *dd, char *buf)
7343 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
7346 static DRIVER_ATTR_RO(version);
7348 static ssize_t release_date_show(struct device_driver *dd, char *buf)
7350 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
7353 static DRIVER_ATTR_RO(release_date);
7355 static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
7357 return sprintf(buf, "%u\n", support_poll_for_event);
7359 static DRIVER_ATTR_RO(support_poll_for_event);
7361 static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
7363 return sprintf(buf, "%u\n", support_device_change);
7365 static DRIVER_ATTR_RO(support_device_change);
7367 static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
7369 return sprintf(buf, "%u\n", megasas_dbg_lvl);
7372 static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
7377 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
7378 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7383 static DRIVER_ATTR_RW(dbg_lvl);
7385 static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
7387 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
7388 scsi_remove_device(sdev);
7389 scsi_device_put(sdev);
7393 megasas_aen_polling(struct work_struct *work)
7395 struct megasas_aen_event *ev =
7396 container_of(work, struct megasas_aen_event, hotplug_work.work);
7397 struct megasas_instance *instance = ev->instance;
7398 union megasas_evt_class_locale class_locale;
7399 struct Scsi_Host *host;
7400 struct scsi_device *sdev1;
7403 int i, j, doscan = 0;
7404 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
7406 u8 dcmd_ret = DCMD_SUCCESS;
7409 printk(KERN_ERR "invalid instance!\n");
7414 /* Adjust event workqueue thread wait time for VF mode */
7415 if (instance->requestorId)
7416 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7418 /* Don't run the event workqueue thread if OCR is running */
7419 mutex_lock(&instance->reset_mutex);
7421 instance->ev = NULL;
7422 host = instance->host;
7423 if (instance->evt_detail) {
7424 megasas_decode_evt(instance);
7426 switch (le32_to_cpu(instance->evt_detail->code)) {
7428 case MR_EVT_PD_INSERTED:
7429 case MR_EVT_PD_REMOVED:
7430 dcmd_ret = megasas_get_pd_list(instance);
7431 if (dcmd_ret == DCMD_SUCCESS)
7432 doscan = SCAN_PD_CHANNEL;
7435 case MR_EVT_LD_OFFLINE:
7436 case MR_EVT_CFG_CLEARED:
7437 case MR_EVT_LD_DELETED:
7438 case MR_EVT_LD_CREATED:
7439 if (!instance->requestorId ||
7440 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7441 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7443 if (dcmd_ret == DCMD_SUCCESS)
7444 doscan = SCAN_VD_CHANNEL;
7448 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7449 case MR_EVT_FOREIGN_CFG_IMPORTED:
7450 case MR_EVT_LD_STATE_CHANGE:
7451 dcmd_ret = megasas_get_pd_list(instance);
7453 if (dcmd_ret != DCMD_SUCCESS)
7456 if (!instance->requestorId ||
7457 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7458 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7460 if (dcmd_ret != DCMD_SUCCESS)
7463 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7464 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7465 instance->host->host_no);
7468 case MR_EVT_CTRL_PROP_CHANGED:
7469 dcmd_ret = megasas_get_ctrl_info(instance);
7476 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7477 mutex_unlock(&instance->reset_mutex);
7482 mutex_unlock(&instance->reset_mutex);
7484 if (doscan & SCAN_PD_CHANNEL) {
7485 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7486 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7487 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
7488 sdev1 = scsi_device_lookup(host, i, j, 0);
7489 if (instance->pd_list[pd_index].driveState ==
7490 MR_PD_STATE_SYSTEM) {
7492 scsi_add_device(host, i, j, 0);
7494 scsi_device_put(sdev1);
7497 megasas_remove_scsi_device(sdev1);
7503 if (doscan & SCAN_VD_CHANNEL) {
7504 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7505 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7506 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7507 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7508 if (instance->ld_ids[ld_index] != 0xff) {
7510 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7512 scsi_device_put(sdev1);
7515 megasas_remove_scsi_device(sdev1);
7521 if (dcmd_ret == DCMD_SUCCESS)
7522 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7524 seq_num = instance->last_seq_num;
7526 /* Register AEN with FW for latest sequence number plus 1 */
7527 class_locale.members.reserved = 0;
7528 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7529 class_locale.members.class = MR_EVT_CLASS_DEBUG;
7531 if (instance->aen_cmd != NULL) {
7536 mutex_lock(&instance->reset_mutex);
7537 error = megasas_register_aen(instance, seq_num,
7540 dev_err(&instance->pdev->dev,
7541 "register aen failed error %x\n", error);
7543 mutex_unlock(&instance->reset_mutex);
7548 * megasas_init - Driver load entry point
7550 static int __init megasas_init(void)
7555 * Booted in kdump kernel, minimize memory footprints by
7556 * disabling few features
7558 if (reset_devices) {
7561 dual_qdepth_disable = 1;
7565 * Announce driver version and other information
7567 pr_info("megasas: %s\n", MEGASAS_VERSION);
7569 spin_lock_init(&poll_aen_lock);
7571 support_poll_for_event = 2;
7572 support_device_change = 1;
7574 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7577 * Register character device node
7579 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7582 printk(KERN_DEBUG "megasas: failed to open device node\n");
7586 megasas_mgmt_majorno = rval;
7589 * Register ourselves as PCI hotplug module
7591 rval = pci_register_driver(&megasas_pci_driver);
7594 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
7598 rval = driver_create_file(&megasas_pci_driver.driver,
7599 &driver_attr_version);
7601 goto err_dcf_attr_ver;
7603 rval = driver_create_file(&megasas_pci_driver.driver,
7604 &driver_attr_release_date);
7606 goto err_dcf_rel_date;
7608 rval = driver_create_file(&megasas_pci_driver.driver,
7609 &driver_attr_support_poll_for_event);
7611 goto err_dcf_support_poll_for_event;
7613 rval = driver_create_file(&megasas_pci_driver.driver,
7614 &driver_attr_dbg_lvl);
7616 goto err_dcf_dbg_lvl;
7617 rval = driver_create_file(&megasas_pci_driver.driver,
7618 &driver_attr_support_device_change);
7620 goto err_dcf_support_device_change;
7624 err_dcf_support_device_change:
7625 driver_remove_file(&megasas_pci_driver.driver,
7626 &driver_attr_dbg_lvl);
7628 driver_remove_file(&megasas_pci_driver.driver,
7629 &driver_attr_support_poll_for_event);
7630 err_dcf_support_poll_for_event:
7631 driver_remove_file(&megasas_pci_driver.driver,
7632 &driver_attr_release_date);
7634 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7636 pci_unregister_driver(&megasas_pci_driver);
7638 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7643 * megasas_exit - Driver unload entry point
7645 static void __exit megasas_exit(void)
7647 driver_remove_file(&megasas_pci_driver.driver,
7648 &driver_attr_dbg_lvl);
7649 driver_remove_file(&megasas_pci_driver.driver,
7650 &driver_attr_support_poll_for_event);
7651 driver_remove_file(&megasas_pci_driver.driver,
7652 &driver_attr_support_device_change);
7653 driver_remove_file(&megasas_pci_driver.driver,
7654 &driver_attr_release_date);
7655 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7657 pci_unregister_driver(&megasas_pci_driver);
7658 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7661 module_init(megasas_init);
7662 module_exit(megasas_exit);