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 cmd_mfi->frame->hdr.cmd_status =
2001 MFI_STAT_WRONG_STATE;
2002 megasas_complete_cmd(instance,
2008 for (i = 0; i < instance->max_fw_cmds; i++) {
2009 cmd_mfi = instance->cmd_list[i];
2010 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2012 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2018 void megaraid_sas_kill_hba(struct megasas_instance *instance)
2020 /* Set critical error to block I/O & ioctls in case caller didn't */
2021 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2022 /* Wait 1 second to ensure IO or ioctls in build have posted */
2024 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2025 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2026 (instance->adapter_type != MFI_SERIES)) {
2027 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2029 readl(&instance->reg_set->doorbell);
2030 if (instance->requestorId && instance->peerIsPresent)
2031 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2033 writel(MFI_STOP_ADP,
2034 &instance->reg_set->inbound_doorbell);
2036 /* Complete outstanding ioctls when adapter is killed */
2037 megasas_complete_outstanding_ioctls(instance);
2041 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2042 * restored to max value
2043 * @instance: Adapter soft state
2047 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2049 unsigned long flags;
2051 if (instance->flag & MEGASAS_FW_BUSY
2052 && time_after(jiffies, instance->last_time + 5 * HZ)
2053 && atomic_read(&instance->fw_outstanding) <
2054 instance->throttlequeuedepth + 1) {
2056 spin_lock_irqsave(instance->host->host_lock, flags);
2057 instance->flag &= ~MEGASAS_FW_BUSY;
2059 instance->host->can_queue = instance->cur_can_queue;
2060 spin_unlock_irqrestore(instance->host->host_lock, flags);
2065 * megasas_complete_cmd_dpc - Returns FW's controller structure
2066 * @instance_addr: Address of adapter soft state
2068 * Tasklet to complete cmds
2070 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2075 struct megasas_cmd *cmd;
2076 struct megasas_instance *instance =
2077 (struct megasas_instance *)instance_addr;
2078 unsigned long flags;
2080 /* If we have already declared adapter dead, donot complete cmds */
2081 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2084 spin_lock_irqsave(&instance->completion_lock, flags);
2086 producer = le32_to_cpu(*instance->producer);
2087 consumer = le32_to_cpu(*instance->consumer);
2089 while (consumer != producer) {
2090 context = le32_to_cpu(instance->reply_queue[consumer]);
2091 if (context >= instance->max_fw_cmds) {
2092 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2097 cmd = instance->cmd_list[context];
2099 megasas_complete_cmd(instance, cmd, DID_OK);
2102 if (consumer == (instance->max_fw_cmds + 1)) {
2107 *instance->consumer = cpu_to_le32(producer);
2109 spin_unlock_irqrestore(&instance->completion_lock, flags);
2112 * Check if we can restore can_queue
2114 megasas_check_and_restore_queue_depth(instance);
2118 * megasas_start_timer - Initializes a timer object
2119 * @instance: Adapter soft state
2120 * @timer: timer object to be initialized
2121 * @fn: timer function
2122 * @interval: time interval between timer function call
2125 void megasas_start_timer(struct megasas_instance *instance,
2126 struct timer_list *timer,
2127 void *fn, unsigned long interval)
2130 timer->expires = jiffies + interval;
2131 timer->data = (unsigned long)instance;
2132 timer->function = fn;
2137 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2140 process_fw_state_change_wq(struct work_struct *work);
2142 void megasas_do_ocr(struct megasas_instance *instance)
2144 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2145 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2146 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2147 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2149 instance->instancet->disable_intr(instance);
2150 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2151 instance->issuepend_done = 0;
2153 atomic_set(&instance->fw_outstanding, 0);
2154 megasas_internal_reset_defer_cmds(instance);
2155 process_fw_state_change_wq(&instance->work_init);
2158 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2161 struct megasas_cmd *cmd;
2162 struct megasas_dcmd_frame *dcmd;
2163 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2164 dma_addr_t new_affiliation_111_h;
2168 cmd = megasas_get_cmd(instance);
2171 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2172 "Failed to get cmd for scsi%d\n",
2173 instance->host->host_no);
2177 dcmd = &cmd->frame->dcmd;
2179 if (!instance->vf_affiliation_111) {
2180 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2181 "affiliation for scsi%d\n", instance->host->host_no);
2182 megasas_return_cmd(instance, cmd);
2187 memset(instance->vf_affiliation_111, 0,
2188 sizeof(struct MR_LD_VF_AFFILIATION_111));
2190 new_affiliation_111 =
2191 pci_alloc_consistent(instance->pdev,
2192 sizeof(struct MR_LD_VF_AFFILIATION_111),
2193 &new_affiliation_111_h);
2194 if (!new_affiliation_111) {
2195 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2196 "memory for new affiliation for scsi%d\n",
2197 instance->host->host_no);
2198 megasas_return_cmd(instance, cmd);
2201 memset(new_affiliation_111, 0,
2202 sizeof(struct MR_LD_VF_AFFILIATION_111));
2205 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2207 dcmd->cmd = MFI_CMD_DCMD;
2208 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2209 dcmd->sge_count = 1;
2210 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2213 dcmd->data_xfer_len =
2214 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2215 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2218 dcmd->sgl.sge32[0].phys_addr =
2219 cpu_to_le32(instance->vf_affiliation_111_h);
2221 dcmd->sgl.sge32[0].phys_addr =
2222 cpu_to_le32(new_affiliation_111_h);
2224 dcmd->sgl.sge32[0].length = cpu_to_le32(
2225 sizeof(struct MR_LD_VF_AFFILIATION_111));
2227 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2228 "scsi%d\n", instance->host->host_no);
2230 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2231 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2232 " failed with status 0x%x for scsi%d\n",
2233 dcmd->cmd_status, instance->host->host_no);
2234 retval = 1; /* Do a scan if we couldn't get affiliation */
2239 thisVf = new_affiliation_111->thisVf;
2240 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2241 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2242 new_affiliation_111->map[ld].policy[thisVf]) {
2243 dev_warn(&instance->pdev->dev, "SR-IOV: "
2244 "Got new LD/VF affiliation for scsi%d\n",
2245 instance->host->host_no);
2246 memcpy(instance->vf_affiliation_111,
2247 new_affiliation_111,
2248 sizeof(struct MR_LD_VF_AFFILIATION_111));
2254 if (new_affiliation_111) {
2255 pci_free_consistent(instance->pdev,
2256 sizeof(struct MR_LD_VF_AFFILIATION_111),
2257 new_affiliation_111,
2258 new_affiliation_111_h);
2261 megasas_return_cmd(instance, cmd);
2266 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2269 struct megasas_cmd *cmd;
2270 struct megasas_dcmd_frame *dcmd;
2271 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2272 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2273 dma_addr_t new_affiliation_h;
2274 int i, j, retval = 0, found = 0, doscan = 0;
2277 cmd = megasas_get_cmd(instance);
2280 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2281 "Failed to get cmd for scsi%d\n",
2282 instance->host->host_no);
2286 dcmd = &cmd->frame->dcmd;
2288 if (!instance->vf_affiliation) {
2289 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2290 "affiliation for scsi%d\n", instance->host->host_no);
2291 megasas_return_cmd(instance, cmd);
2296 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2297 sizeof(struct MR_LD_VF_AFFILIATION));
2300 pci_alloc_consistent(instance->pdev,
2301 (MAX_LOGICAL_DRIVES + 1) *
2302 sizeof(struct MR_LD_VF_AFFILIATION),
2303 &new_affiliation_h);
2304 if (!new_affiliation) {
2305 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2306 "memory for new affiliation for scsi%d\n",
2307 instance->host->host_no);
2308 megasas_return_cmd(instance, cmd);
2311 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2312 sizeof(struct MR_LD_VF_AFFILIATION));
2315 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2317 dcmd->cmd = MFI_CMD_DCMD;
2318 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2319 dcmd->sge_count = 1;
2320 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2323 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2324 sizeof(struct MR_LD_VF_AFFILIATION));
2325 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2328 dcmd->sgl.sge32[0].phys_addr =
2329 cpu_to_le32(instance->vf_affiliation_h);
2331 dcmd->sgl.sge32[0].phys_addr =
2332 cpu_to_le32(new_affiliation_h);
2334 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2335 sizeof(struct MR_LD_VF_AFFILIATION));
2337 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2338 "scsi%d\n", instance->host->host_no);
2341 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2342 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2343 " failed with status 0x%x for scsi%d\n",
2344 dcmd->cmd_status, instance->host->host_no);
2345 retval = 1; /* Do a scan if we couldn't get affiliation */
2350 if (!new_affiliation->ldCount) {
2351 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2352 "affiliation for passive path for scsi%d\n",
2353 instance->host->host_no);
2357 newmap = new_affiliation->map;
2358 savedmap = instance->vf_affiliation->map;
2359 thisVf = new_affiliation->thisVf;
2360 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2362 for (j = 0; j < instance->vf_affiliation->ldCount;
2364 if (newmap->ref.targetId ==
2365 savedmap->ref.targetId) {
2367 if (newmap->policy[thisVf] !=
2368 savedmap->policy[thisVf]) {
2373 savedmap = (struct MR_LD_VF_MAP *)
2374 ((unsigned char *)savedmap +
2377 if (!found && newmap->policy[thisVf] !=
2378 MR_LD_ACCESS_HIDDEN) {
2382 newmap = (struct MR_LD_VF_MAP *)
2383 ((unsigned char *)newmap + newmap->size);
2386 newmap = new_affiliation->map;
2387 savedmap = instance->vf_affiliation->map;
2389 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2391 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2392 if (savedmap->ref.targetId ==
2393 newmap->ref.targetId) {
2395 if (savedmap->policy[thisVf] !=
2396 newmap->policy[thisVf]) {
2401 newmap = (struct MR_LD_VF_MAP *)
2402 ((unsigned char *)newmap +
2405 if (!found && savedmap->policy[thisVf] !=
2406 MR_LD_ACCESS_HIDDEN) {
2410 savedmap = (struct MR_LD_VF_MAP *)
2411 ((unsigned char *)savedmap +
2417 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2418 "affiliation for scsi%d\n", instance->host->host_no);
2419 memcpy(instance->vf_affiliation, new_affiliation,
2420 new_affiliation->size);
2424 if (new_affiliation)
2425 pci_free_consistent(instance->pdev,
2426 (MAX_LOGICAL_DRIVES + 1) *
2427 sizeof(struct MR_LD_VF_AFFILIATION),
2428 new_affiliation, new_affiliation_h);
2429 megasas_return_cmd(instance, cmd);
2434 /* This function will get the current SR-IOV LD/VF affiliation */
2435 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2440 if (instance->PlasmaFW111)
2441 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2443 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2447 /* This function will tell FW to start the SR-IOV heartbeat */
2448 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2451 struct megasas_cmd *cmd;
2452 struct megasas_dcmd_frame *dcmd;
2455 cmd = megasas_get_cmd(instance);
2458 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2459 "Failed to get cmd for scsi%d\n",
2460 instance->host->host_no);
2464 dcmd = &cmd->frame->dcmd;
2467 instance->hb_host_mem =
2468 pci_zalloc_consistent(instance->pdev,
2469 sizeof(struct MR_CTRL_HB_HOST_MEM),
2470 &instance->hb_host_mem_h);
2471 if (!instance->hb_host_mem) {
2472 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2473 " memory for heartbeat host memory for scsi%d\n",
2474 instance->host->host_no);
2480 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2482 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2483 dcmd->cmd = MFI_CMD_DCMD;
2484 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2485 dcmd->sge_count = 1;
2486 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2489 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2490 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2491 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2492 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2494 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2495 instance->host->host_no);
2497 if ((instance->adapter_type != MFI_SERIES) &&
2498 !instance->mask_interrupts)
2499 retval = megasas_issue_blocked_cmd(instance, cmd,
2500 MEGASAS_ROUTINE_WAIT_TIME_VF);
2502 retval = megasas_issue_polled(instance, cmd);
2505 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2506 "_MEM_ALLOC DCMD %s for scsi%d\n",
2507 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2508 "timed out" : "failed", instance->host->host_no);
2513 megasas_return_cmd(instance, cmd);
2518 /* Handler for SR-IOV heartbeat */
2519 void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2521 struct megasas_instance *instance =
2522 (struct megasas_instance *)instance_addr;
2524 if (instance->hb_host_mem->HB.fwCounter !=
2525 instance->hb_host_mem->HB.driverCounter) {
2526 instance->hb_host_mem->HB.driverCounter =
2527 instance->hb_host_mem->HB.fwCounter;
2528 mod_timer(&instance->sriov_heartbeat_timer,
2529 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2531 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2532 "completed for scsi%d\n", instance->host->host_no);
2533 schedule_work(&instance->work_init);
2538 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2539 * @instance: Adapter soft state
2541 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2542 * complete all its outstanding commands. Returns error if one or more IOs
2543 * are pending after this time period. It also marks the controller dead.
2545 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2547 int i, sl, outstanding;
2549 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2550 unsigned long flags;
2551 struct list_head clist_local;
2552 struct megasas_cmd *reset_cmd;
2555 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2556 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2557 __func__, __LINE__);
2561 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2563 INIT_LIST_HEAD(&clist_local);
2564 spin_lock_irqsave(&instance->hba_lock, flags);
2565 list_splice_init(&instance->internal_reset_pending_q,
2567 spin_unlock_irqrestore(&instance->hba_lock, flags);
2569 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2570 for (i = 0; i < wait_time; i++) {
2572 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2576 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2577 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2578 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2583 while (!list_empty(&clist_local)) {
2584 reset_cmd = list_entry((&clist_local)->next,
2585 struct megasas_cmd, list);
2586 list_del_init(&reset_cmd->list);
2587 if (reset_cmd->scmd) {
2588 reset_cmd->scmd->result = DID_REQUEUE << 16;
2589 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2590 reset_index, reset_cmd,
2591 reset_cmd->scmd->cmnd[0]);
2593 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2594 megasas_return_cmd(instance, reset_cmd);
2595 } else if (reset_cmd->sync_cmd) {
2596 dev_notice(&instance->pdev->dev, "%p synch cmds"
2600 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2601 instance->instancet->fire_cmd(instance,
2602 reset_cmd->frame_phys_addr,
2603 0, instance->reg_set);
2605 dev_notice(&instance->pdev->dev, "%p unexpected"
2615 for (i = 0; i < resetwaittime; i++) {
2616 outstanding = atomic_read(&instance->fw_outstanding);
2621 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2622 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2623 "commands to complete\n",i,outstanding);
2625 * Call cmd completion routine. Cmd to be
2626 * be completed directly without depending on isr.
2628 megasas_complete_cmd_dpc((unsigned long)instance);
2635 outstanding = atomic_read(&instance->fw_outstanding);
2636 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2638 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2639 goto no_outstanding;
2641 if (instance->disableOnlineCtrlReset)
2642 goto kill_hba_and_failed;
2644 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2645 dev_info(&instance->pdev->dev,
2646 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2647 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2649 goto kill_hba_and_failed;
2650 megasas_do_ocr(instance);
2652 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2653 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2654 __func__, __LINE__);
2657 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2658 __func__, __LINE__);
2660 for (sl = 0; sl < 10; sl++)
2663 outstanding = atomic_read(&instance->fw_outstanding);
2665 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2666 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2667 goto no_outstanding;
2674 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2675 __func__, __LINE__);
2678 kill_hba_and_failed:
2680 /* Reset not supported, kill adapter */
2681 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2682 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2683 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2684 atomic_read(&instance->fw_outstanding));
2685 megasas_dump_pending_frames(instance);
2686 megaraid_sas_kill_hba(instance);
2692 * megasas_generic_reset - Generic reset routine
2693 * @scmd: Mid-layer SCSI command
2695 * This routine implements a generic reset handler for device, bus and host
2696 * reset requests. Device, bus and host specific reset handlers can use this
2697 * function after they do their specific tasks.
2699 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2702 struct megasas_instance *instance;
2704 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2706 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2707 scmd->cmnd[0], scmd->retries);
2709 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2710 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2714 ret_val = megasas_wait_for_outstanding(instance);
2715 if (ret_val == SUCCESS)
2716 dev_notice(&instance->pdev->dev, "reset successful\n");
2718 dev_err(&instance->pdev->dev, "failed to do reset\n");
2724 * megasas_reset_timer - quiesce the adapter if required
2727 * Sets the FW busy flag and reduces the host->can_queue if the
2728 * cmd has not been completed within the timeout period.
2731 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2733 struct megasas_instance *instance;
2734 unsigned long flags;
2736 if (time_after(jiffies, scmd->jiffies_at_alloc +
2737 (scmd_timeout * 2) * HZ)) {
2738 return BLK_EH_NOT_HANDLED;
2741 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2742 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2743 /* FW is busy, throttle IO */
2744 spin_lock_irqsave(instance->host->host_lock, flags);
2746 instance->host->can_queue = instance->throttlequeuedepth;
2747 instance->last_time = jiffies;
2748 instance->flag |= MEGASAS_FW_BUSY;
2750 spin_unlock_irqrestore(instance->host->host_lock, flags);
2752 return BLK_EH_RESET_TIMER;
2756 * megasas_dump_frame - This function will dump MPT/MFI frame
2759 megasas_dump_frame(void *mpi_request, int sz)
2762 __le32 *mfp = (__le32 *)mpi_request;
2764 printk(KERN_INFO "IO request frame:\n\t");
2765 for (i = 0; i < sz / sizeof(__le32); i++) {
2766 if (i && ((i % 8) == 0))
2768 printk("%08x ", le32_to_cpu(mfp[i]));
2774 * megasas_reset_bus_host - Bus & host reset handler entry point
2776 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2779 struct megasas_instance *instance;
2781 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2783 scmd_printk(KERN_INFO, scmd,
2784 "Controller reset is requested due to IO timeout\n"
2785 "SCSI command pointer: (%p)\t SCSI host state: %d\t"
2786 " SCSI host busy: %d\t FW outstanding: %d\n",
2787 scmd, scmd->device->host->shost_state,
2788 atomic_read((atomic_t *)&scmd->device->host->host_busy),
2789 atomic_read(&instance->fw_outstanding));
2792 * First wait for all commands to complete
2794 if (instance->adapter_type == MFI_SERIES) {
2795 ret = megasas_generic_reset(scmd);
2797 struct megasas_cmd_fusion *cmd;
2798 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2800 megasas_dump_frame(cmd->io_request,
2801 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
2802 ret = megasas_reset_fusion(scmd->device->host,
2803 SCSIIO_TIMEOUT_OCR);
2810 * megasas_task_abort - Issues task abort request to firmware
2811 * (supported only for fusion adapters)
2812 * @scmd: SCSI command pointer
2814 static int megasas_task_abort(struct scsi_cmnd *scmd)
2817 struct megasas_instance *instance;
2819 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2821 if (instance->adapter_type != MFI_SERIES)
2822 ret = megasas_task_abort_fusion(scmd);
2824 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2832 * megasas_reset_target: Issues target reset request to firmware
2833 * (supported only for fusion adapters)
2834 * @scmd: SCSI command pointer
2836 static int megasas_reset_target(struct scsi_cmnd *scmd)
2839 struct megasas_instance *instance;
2841 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2843 if (instance->adapter_type != MFI_SERIES)
2844 ret = megasas_reset_target_fusion(scmd);
2846 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2854 * megasas_bios_param - Returns disk geometry for a disk
2855 * @sdev: device handle
2856 * @bdev: block device
2857 * @capacity: drive capacity
2858 * @geom: geometry parameters
2861 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2862 sector_t capacity, int geom[])
2869 /* Default heads (64) & sectors (32) */
2873 tmp = heads * sectors;
2874 cylinders = capacity;
2876 sector_div(cylinders, tmp);
2879 * Handle extended translation size for logical drives > 1Gb
2882 if (capacity >= 0x200000) {
2885 tmp = heads*sectors;
2886 cylinders = capacity;
2887 sector_div(cylinders, tmp);
2892 geom[2] = cylinders;
2897 static void megasas_aen_polling(struct work_struct *work);
2900 * megasas_service_aen - Processes an event notification
2901 * @instance: Adapter soft state
2902 * @cmd: AEN command completed by the ISR
2904 * For AEN, driver sends a command down to FW that is held by the FW till an
2905 * event occurs. When an event of interest occurs, FW completes the command
2906 * that it was previously holding.
2908 * This routines sends SIGIO signal to processes that have registered with the
2912 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2914 unsigned long flags;
2917 * Don't signal app if it is just an aborted previously registered aen
2919 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2920 spin_lock_irqsave(&poll_aen_lock, flags);
2921 megasas_poll_wait_aen = 1;
2922 spin_unlock_irqrestore(&poll_aen_lock, flags);
2923 wake_up(&megasas_poll_wait);
2924 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2929 instance->aen_cmd = NULL;
2931 megasas_return_cmd(instance, cmd);
2933 if ((instance->unload == 0) &&
2934 ((instance->issuepend_done == 1))) {
2935 struct megasas_aen_event *ev;
2937 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2939 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
2941 ev->instance = instance;
2943 INIT_DELAYED_WORK(&ev->hotplug_work,
2944 megasas_aen_polling);
2945 schedule_delayed_work(&ev->hotplug_work, 0);
2951 megasas_fw_crash_buffer_store(struct device *cdev,
2952 struct device_attribute *attr, const char *buf, size_t count)
2954 struct Scsi_Host *shost = class_to_shost(cdev);
2955 struct megasas_instance *instance =
2956 (struct megasas_instance *) shost->hostdata;
2958 unsigned long flags;
2960 if (kstrtoint(buf, 0, &val) != 0)
2963 spin_lock_irqsave(&instance->crashdump_lock, flags);
2964 instance->fw_crash_buffer_offset = val;
2965 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2970 megasas_fw_crash_buffer_show(struct device *cdev,
2971 struct device_attribute *attr, char *buf)
2973 struct Scsi_Host *shost = class_to_shost(cdev);
2974 struct megasas_instance *instance =
2975 (struct megasas_instance *) shost->hostdata;
2977 unsigned long buff_addr;
2978 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2979 unsigned long chunk_left_bytes;
2980 unsigned long src_addr;
2981 unsigned long flags;
2984 spin_lock_irqsave(&instance->crashdump_lock, flags);
2985 buff_offset = instance->fw_crash_buffer_offset;
2986 if (!instance->crash_dump_buf ||
2987 !((instance->fw_crash_state == AVAILABLE) ||
2988 (instance->fw_crash_state == COPYING))) {
2989 dev_err(&instance->pdev->dev,
2990 "Firmware crash dump is not available\n");
2991 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2995 buff_addr = (unsigned long) buf;
2997 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
2998 dev_err(&instance->pdev->dev,
2999 "Firmware crash dump offset is out of range\n");
3000 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3004 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3005 chunk_left_bytes = dmachunk - (buff_offset % dmachunk);
3006 size = (size > chunk_left_bytes) ? chunk_left_bytes : size;
3007 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3009 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3010 (buff_offset % dmachunk);
3011 memcpy(buf, (void *)src_addr, size);
3012 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3018 megasas_fw_crash_buffer_size_show(struct device *cdev,
3019 struct device_attribute *attr, char *buf)
3021 struct Scsi_Host *shost = class_to_shost(cdev);
3022 struct megasas_instance *instance =
3023 (struct megasas_instance *) shost->hostdata;
3025 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3026 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3030 megasas_fw_crash_state_store(struct device *cdev,
3031 struct device_attribute *attr, const char *buf, size_t count)
3033 struct Scsi_Host *shost = class_to_shost(cdev);
3034 struct megasas_instance *instance =
3035 (struct megasas_instance *) shost->hostdata;
3037 unsigned long flags;
3039 if (kstrtoint(buf, 0, &val) != 0)
3042 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3043 dev_err(&instance->pdev->dev, "application updates invalid "
3044 "firmware crash state\n");
3048 instance->fw_crash_state = val;
3050 if ((val == COPIED) || (val == COPY_ERROR)) {
3051 spin_lock_irqsave(&instance->crashdump_lock, flags);
3052 megasas_free_host_crash_buffer(instance);
3053 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3054 if (val == COPY_ERROR)
3055 dev_info(&instance->pdev->dev, "application failed to "
3056 "copy Firmware crash dump\n");
3058 dev_info(&instance->pdev->dev, "Firmware crash dump "
3059 "copied successfully\n");
3065 megasas_fw_crash_state_show(struct device *cdev,
3066 struct device_attribute *attr, char *buf)
3068 struct Scsi_Host *shost = class_to_shost(cdev);
3069 struct megasas_instance *instance =
3070 (struct megasas_instance *) shost->hostdata;
3072 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3076 megasas_page_size_show(struct device *cdev,
3077 struct device_attribute *attr, char *buf)
3079 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3083 megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3086 struct Scsi_Host *shost = class_to_shost(cdev);
3087 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3089 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3092 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3093 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3094 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3095 megasas_fw_crash_buffer_size_show, NULL);
3096 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3097 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3098 static DEVICE_ATTR(page_size, S_IRUGO,
3099 megasas_page_size_show, NULL);
3100 static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3101 megasas_ldio_outstanding_show, NULL);
3103 struct device_attribute *megaraid_host_attrs[] = {
3104 &dev_attr_fw_crash_buffer_size,
3105 &dev_attr_fw_crash_buffer,
3106 &dev_attr_fw_crash_state,
3107 &dev_attr_page_size,
3108 &dev_attr_ldio_outstanding,
3113 * Scsi host template for megaraid_sas driver
3115 static struct scsi_host_template megasas_template = {
3117 .module = THIS_MODULE,
3118 .name = "Avago SAS based MegaRAID driver",
3119 .proc_name = "megaraid_sas",
3120 .slave_configure = megasas_slave_configure,
3121 .slave_alloc = megasas_slave_alloc,
3122 .slave_destroy = megasas_slave_destroy,
3123 .queuecommand = megasas_queue_command,
3124 .eh_target_reset_handler = megasas_reset_target,
3125 .eh_abort_handler = megasas_task_abort,
3126 .eh_host_reset_handler = megasas_reset_bus_host,
3127 .eh_timed_out = megasas_reset_timer,
3128 .shost_attrs = megaraid_host_attrs,
3129 .bios_param = megasas_bios_param,
3130 .use_clustering = ENABLE_CLUSTERING,
3131 .change_queue_depth = scsi_change_queue_depth,
3136 * megasas_complete_int_cmd - Completes an internal command
3137 * @instance: Adapter soft state
3138 * @cmd: Command to be completed
3140 * The megasas_issue_blocked_cmd() function waits for a command to complete
3141 * after it issues a command. This function wakes up that waiting routine by
3142 * calling wake_up() on the wait queue.
3145 megasas_complete_int_cmd(struct megasas_instance *instance,
3146 struct megasas_cmd *cmd)
3148 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
3149 wake_up(&instance->int_cmd_wait_q);
3153 * megasas_complete_abort - Completes aborting a command
3154 * @instance: Adapter soft state
3155 * @cmd: Cmd that was issued to abort another cmd
3157 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3158 * after it issues an abort on a previously issued command. This function
3159 * wakes up all functions waiting on the same wait queue.
3162 megasas_complete_abort(struct megasas_instance *instance,
3163 struct megasas_cmd *cmd)
3165 if (cmd->sync_cmd) {
3167 cmd->cmd_status_drv = 0;
3168 wake_up(&instance->abort_cmd_wait_q);
3173 * megasas_complete_cmd - Completes a command
3174 * @instance: Adapter soft state
3175 * @cmd: Command to be completed
3176 * @alt_status: If non-zero, use this value as status to
3177 * SCSI mid-layer instead of the value returned
3178 * by the FW. This should be used if caller wants
3179 * an alternate status (as in the case of aborted
3183 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3187 struct megasas_header *hdr = &cmd->frame->hdr;
3188 unsigned long flags;
3189 struct fusion_context *fusion = instance->ctrl_context;
3192 /* flag for the retry reset */
3193 cmd->retry_for_fw_reset = 0;
3196 cmd->scmd->SCp.ptr = NULL;
3199 case MFI_CMD_INVALID:
3200 /* Some older 1068 controller FW may keep a pended
3201 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3202 when booting the kdump kernel. Ignore this command to
3203 prevent a kernel panic on shutdown of the kdump kernel. */
3204 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3206 dev_warn(&instance->pdev->dev, "If you have a controller "
3207 "other than PERC5, please upgrade your firmware\n");
3209 case MFI_CMD_PD_SCSI_IO:
3210 case MFI_CMD_LD_SCSI_IO:
3213 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3214 * issued either through an IO path or an IOCTL path. If it
3215 * was via IOCTL, we will send it to internal completion.
3217 if (cmd->sync_cmd) {
3219 megasas_complete_int_cmd(instance, cmd);
3223 case MFI_CMD_LD_READ:
3224 case MFI_CMD_LD_WRITE:
3227 cmd->scmd->result = alt_status << 16;
3233 atomic_dec(&instance->fw_outstanding);
3235 scsi_dma_unmap(cmd->scmd);
3236 cmd->scmd->scsi_done(cmd->scmd);
3237 megasas_return_cmd(instance, cmd);
3242 switch (hdr->cmd_status) {
3245 cmd->scmd->result = DID_OK << 16;
3248 case MFI_STAT_SCSI_IO_FAILED:
3249 case MFI_STAT_LD_INIT_IN_PROGRESS:
3251 (DID_ERROR << 16) | hdr->scsi_status;
3254 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3256 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3258 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3259 memset(cmd->scmd->sense_buffer, 0,
3260 SCSI_SENSE_BUFFERSIZE);
3261 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3264 cmd->scmd->result |= DRIVER_SENSE << 24;
3269 case MFI_STAT_LD_OFFLINE:
3270 case MFI_STAT_DEVICE_NOT_FOUND:
3271 cmd->scmd->result = DID_BAD_TARGET << 16;
3275 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3277 cmd->scmd->result = DID_ERROR << 16;
3281 atomic_dec(&instance->fw_outstanding);
3283 scsi_dma_unmap(cmd->scmd);
3284 cmd->scmd->scsi_done(cmd->scmd);
3285 megasas_return_cmd(instance, cmd);
3292 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3293 /* Check for LD map update */
3294 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3295 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3296 fusion->fast_path_io = 0;
3297 spin_lock_irqsave(instance->host->host_lock, flags);
3298 instance->map_update_cmd = NULL;
3299 if (cmd->frame->hdr.cmd_status != 0) {
3300 if (cmd->frame->hdr.cmd_status !=
3302 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3303 cmd->frame->hdr.cmd_status);
3305 megasas_return_cmd(instance, cmd);
3306 spin_unlock_irqrestore(
3307 instance->host->host_lock,
3313 megasas_return_cmd(instance, cmd);
3316 * Set fast path IO to ZERO.
3317 * Validate Map will set proper value.
3318 * Meanwhile all IOs will go as LD IO.
3320 if (MR_ValidateMapInfo(instance))
3321 fusion->fast_path_io = 1;
3323 fusion->fast_path_io = 0;
3324 megasas_sync_map_info(instance);
3325 spin_unlock_irqrestore(instance->host->host_lock,
3329 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3330 opcode == MR_DCMD_CTRL_EVENT_GET) {
3331 spin_lock_irqsave(&poll_aen_lock, flags);
3332 megasas_poll_wait_aen = 0;
3333 spin_unlock_irqrestore(&poll_aen_lock, flags);
3336 /* FW has an updated PD sequence */
3337 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3338 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3340 spin_lock_irqsave(instance->host->host_lock, flags);
3341 status = cmd->frame->hdr.cmd_status;
3342 instance->jbod_seq_cmd = NULL;
3343 megasas_return_cmd(instance, cmd);
3345 if (status == MFI_STAT_OK) {
3346 instance->pd_seq_map_id++;
3347 /* Re-register a pd sync seq num cmd */
3348 if (megasas_sync_pd_seq_num(instance, true))
3349 instance->use_seqnum_jbod_fp = false;
3351 instance->use_seqnum_jbod_fp = false;
3353 spin_unlock_irqrestore(instance->host->host_lock, flags);
3358 * See if got an event notification
3360 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3361 megasas_service_aen(instance, cmd);
3363 megasas_complete_int_cmd(instance, cmd);
3369 * Cmd issued to abort another cmd returned
3371 megasas_complete_abort(instance, cmd);
3375 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3382 * megasas_issue_pending_cmds_again - issue all pending cmds
3383 * in FW again because of the fw reset
3384 * @instance: Adapter soft state
3387 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3389 struct megasas_cmd *cmd;
3390 struct list_head clist_local;
3391 union megasas_evt_class_locale class_locale;
3392 unsigned long flags;
3395 INIT_LIST_HEAD(&clist_local);
3396 spin_lock_irqsave(&instance->hba_lock, flags);
3397 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3398 spin_unlock_irqrestore(&instance->hba_lock, flags);
3400 while (!list_empty(&clist_local)) {
3401 cmd = list_entry((&clist_local)->next,
3402 struct megasas_cmd, list);
3403 list_del_init(&cmd->list);
3405 if (cmd->sync_cmd || cmd->scmd) {
3406 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3407 "detected to be pending while HBA reset\n",
3408 cmd, cmd->scmd, cmd->sync_cmd);
3410 cmd->retry_for_fw_reset++;
3412 if (cmd->retry_for_fw_reset == 3) {
3413 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3414 "was tried multiple times during reset."
3415 "Shutting down the HBA\n",
3416 cmd, cmd->scmd, cmd->sync_cmd);
3417 instance->instancet->disable_intr(instance);
3418 atomic_set(&instance->fw_reset_no_pci_access, 1);
3419 megaraid_sas_kill_hba(instance);
3424 if (cmd->sync_cmd == 1) {
3426 dev_notice(&instance->pdev->dev, "unexpected"
3427 "cmd attached to internal command!\n");
3429 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3430 "on the internal reset queue,"
3431 "issue it again.\n", cmd);
3432 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3433 instance->instancet->fire_cmd(instance,
3434 cmd->frame_phys_addr,
3435 0, instance->reg_set);
3436 } else if (cmd->scmd) {
3437 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3438 "detected on the internal queue, issue again.\n",
3439 cmd, cmd->scmd->cmnd[0]);
3441 atomic_inc(&instance->fw_outstanding);
3442 instance->instancet->fire_cmd(instance,
3443 cmd->frame_phys_addr,
3444 cmd->frame_count-1, instance->reg_set);
3446 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3447 "internal reset defer list while re-issue!!\n",
3452 if (instance->aen_cmd) {
3453 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3454 megasas_return_cmd(instance, instance->aen_cmd);
3456 instance->aen_cmd = NULL;
3460 * Initiate AEN (Asynchronous Event Notification)
3462 seq_num = instance->last_seq_num;
3463 class_locale.members.reserved = 0;
3464 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3465 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3467 megasas_register_aen(instance, seq_num, class_locale.word);
3471 * Move the internal reset pending commands to a deferred queue.
3473 * We move the commands pending at internal reset time to a
3474 * pending queue. This queue would be flushed after successful
3475 * completion of the internal reset sequence. if the internal reset
3476 * did not complete in time, the kernel reset handler would flush
3480 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3482 struct megasas_cmd *cmd;
3484 u16 max_cmd = instance->max_fw_cmds;
3486 unsigned long flags;
3489 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3490 for (i = 0; i < max_cmd; i++) {
3491 cmd = instance->cmd_list[i];
3492 if (cmd->sync_cmd == 1 || cmd->scmd) {
3493 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3494 "on the defer queue as internal\n",
3495 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3497 if (!list_empty(&cmd->list)) {
3498 dev_notice(&instance->pdev->dev, "ERROR while"
3499 " moving this cmd:%p, %d %p, it was"
3500 "discovered on some list?\n",
3501 cmd, cmd->sync_cmd, cmd->scmd);
3503 list_del_init(&cmd->list);
3506 list_add_tail(&cmd->list,
3507 &instance->internal_reset_pending_q);
3510 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3515 process_fw_state_change_wq(struct work_struct *work)
3517 struct megasas_instance *instance =
3518 container_of(work, struct megasas_instance, work_init);
3520 unsigned long flags;
3522 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3523 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3524 atomic_read(&instance->adprecovery));
3528 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3529 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3530 "state, restarting it...\n");
3532 instance->instancet->disable_intr(instance);
3533 atomic_set(&instance->fw_outstanding, 0);
3535 atomic_set(&instance->fw_reset_no_pci_access, 1);
3536 instance->instancet->adp_reset(instance, instance->reg_set);
3537 atomic_set(&instance->fw_reset_no_pci_access, 0);
3539 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3540 "initiating next stage...\n");
3542 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3543 "state 2 starting...\n");
3545 /* waiting for about 20 second before start the second init */
3546 for (wait = 0; wait < 30; wait++) {
3550 if (megasas_transition_to_ready(instance, 1)) {
3551 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3553 atomic_set(&instance->fw_reset_no_pci_access, 1);
3554 megaraid_sas_kill_hba(instance);
3558 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3559 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3560 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3562 *instance->consumer = *instance->producer;
3564 *instance->consumer = 0;
3565 *instance->producer = 0;
3568 megasas_issue_init_mfi(instance);
3570 spin_lock_irqsave(&instance->hba_lock, flags);
3571 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3572 spin_unlock_irqrestore(&instance->hba_lock, flags);
3573 instance->instancet->enable_intr(instance);
3575 megasas_issue_pending_cmds_again(instance);
3576 instance->issuepend_done = 1;
3581 * megasas_deplete_reply_queue - Processes all completed commands
3582 * @instance: Adapter soft state
3583 * @alt_status: Alternate status to be returned to
3584 * SCSI mid-layer instead of the status
3585 * returned by the FW
3586 * Note: this must be called with hba lock held
3589 megasas_deplete_reply_queue(struct megasas_instance *instance,
3595 if ((mfiStatus = instance->instancet->check_reset(instance,
3596 instance->reg_set)) == 1) {
3600 if ((mfiStatus = instance->instancet->clear_intr(
3603 /* Hardware may not set outbound_intr_status in MSI-X mode */
3604 if (!instance->msix_vectors)
3608 instance->mfiStatus = mfiStatus;
3610 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3611 fw_state = instance->instancet->read_fw_status_reg(
3612 instance->reg_set) & MFI_STATE_MASK;
3614 if (fw_state != MFI_STATE_FAULT) {
3615 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3619 if ((fw_state == MFI_STATE_FAULT) &&
3620 (instance->disableOnlineCtrlReset == 0)) {
3621 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3623 if ((instance->pdev->device ==
3624 PCI_DEVICE_ID_LSI_SAS1064R) ||
3625 (instance->pdev->device ==
3626 PCI_DEVICE_ID_DELL_PERC5) ||
3627 (instance->pdev->device ==
3628 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3630 *instance->consumer =
3631 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3635 instance->instancet->disable_intr(instance);
3636 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
3637 instance->issuepend_done = 0;
3639 atomic_set(&instance->fw_outstanding, 0);
3640 megasas_internal_reset_defer_cmds(instance);
3642 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3643 fw_state, atomic_read(&instance->adprecovery));
3645 schedule_work(&instance->work_init);
3649 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3650 fw_state, instance->disableOnlineCtrlReset);
3654 tasklet_schedule(&instance->isr_tasklet);
3658 * megasas_isr - isr entry point
3660 static irqreturn_t megasas_isr(int irq, void *devp)
3662 struct megasas_irq_context *irq_context = devp;
3663 struct megasas_instance *instance = irq_context->instance;
3664 unsigned long flags;
3667 if (atomic_read(&instance->fw_reset_no_pci_access))
3670 spin_lock_irqsave(&instance->hba_lock, flags);
3671 rc = megasas_deplete_reply_queue(instance, DID_OK);
3672 spin_unlock_irqrestore(&instance->hba_lock, flags);
3678 * megasas_transition_to_ready - Move the FW to READY state
3679 * @instance: Adapter soft state
3681 * During the initialization, FW passes can potentially be in any one of
3682 * several possible states. If the FW in operational, waiting-for-handshake
3683 * states, driver must take steps to bring it to ready state. Otherwise, it
3684 * has to wait for the ready state.
3687 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3693 u32 abs_state, curr_abs_state;
3695 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3696 fw_state = abs_state & MFI_STATE_MASK;
3698 if (fw_state != MFI_STATE_READY)
3699 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3702 while (fw_state != MFI_STATE_READY) {
3706 case MFI_STATE_FAULT:
3707 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3709 max_wait = MEGASAS_RESET_WAIT_TIME;
3710 cur_state = MFI_STATE_FAULT;
3715 case MFI_STATE_WAIT_HANDSHAKE:
3717 * Set the CLR bit in inbound doorbell
3719 if ((instance->pdev->device ==
3720 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3721 (instance->pdev->device ==
3722 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3723 (instance->adapter_type != MFI_SERIES))
3725 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3726 &instance->reg_set->doorbell);
3729 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3730 &instance->reg_set->inbound_doorbell);
3732 max_wait = MEGASAS_RESET_WAIT_TIME;
3733 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3736 case MFI_STATE_BOOT_MESSAGE_PENDING:
3737 if ((instance->pdev->device ==
3738 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3739 (instance->pdev->device ==
3740 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3741 (instance->adapter_type != MFI_SERIES))
3742 writel(MFI_INIT_HOTPLUG,
3743 &instance->reg_set->doorbell);
3745 writel(MFI_INIT_HOTPLUG,
3746 &instance->reg_set->inbound_doorbell);
3748 max_wait = MEGASAS_RESET_WAIT_TIME;
3749 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3752 case MFI_STATE_OPERATIONAL:
3754 * Bring it to READY state; assuming max wait 10 secs
3756 instance->instancet->disable_intr(instance);
3757 if ((instance->pdev->device ==
3758 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3759 (instance->pdev->device ==
3760 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3761 (instance->adapter_type != MFI_SERIES)) {
3762 writel(MFI_RESET_FLAGS,
3763 &instance->reg_set->doorbell);
3765 if (instance->adapter_type != MFI_SERIES) {
3766 for (i = 0; i < (10 * 1000); i += 20) {
3777 writel(MFI_RESET_FLAGS,
3778 &instance->reg_set->inbound_doorbell);
3780 max_wait = MEGASAS_RESET_WAIT_TIME;
3781 cur_state = MFI_STATE_OPERATIONAL;
3784 case MFI_STATE_UNDEFINED:
3786 * This state should not last for more than 2 seconds
3788 max_wait = MEGASAS_RESET_WAIT_TIME;
3789 cur_state = MFI_STATE_UNDEFINED;
3792 case MFI_STATE_BB_INIT:
3793 max_wait = MEGASAS_RESET_WAIT_TIME;
3794 cur_state = MFI_STATE_BB_INIT;
3797 case MFI_STATE_FW_INIT:
3798 max_wait = MEGASAS_RESET_WAIT_TIME;
3799 cur_state = MFI_STATE_FW_INIT;
3802 case MFI_STATE_FW_INIT_2:
3803 max_wait = MEGASAS_RESET_WAIT_TIME;
3804 cur_state = MFI_STATE_FW_INIT_2;
3807 case MFI_STATE_DEVICE_SCAN:
3808 max_wait = MEGASAS_RESET_WAIT_TIME;
3809 cur_state = MFI_STATE_DEVICE_SCAN;
3812 case MFI_STATE_FLUSH_CACHE:
3813 max_wait = MEGASAS_RESET_WAIT_TIME;
3814 cur_state = MFI_STATE_FLUSH_CACHE;
3818 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3824 * The cur_state should not last for more than max_wait secs
3826 for (i = 0; i < max_wait * 50; i++) {
3827 curr_abs_state = instance->instancet->
3828 read_fw_status_reg(instance->reg_set);
3830 if (abs_state == curr_abs_state) {
3837 * Return error if fw_state hasn't changed after max_wait
3839 if (curr_abs_state == abs_state) {
3840 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3841 "in %d secs\n", fw_state, max_wait);
3845 abs_state = curr_abs_state;
3846 fw_state = curr_abs_state & MFI_STATE_MASK;
3848 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3854 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3855 * @instance: Adapter soft state
3857 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3860 u16 max_cmd = instance->max_mfi_cmds;
3861 struct megasas_cmd *cmd;
3863 if (!instance->frame_dma_pool)
3867 * Return all frames to pool
3869 for (i = 0; i < max_cmd; i++) {
3871 cmd = instance->cmd_list[i];
3874 dma_pool_free(instance->frame_dma_pool, cmd->frame,
3875 cmd->frame_phys_addr);
3878 dma_pool_free(instance->sense_dma_pool, cmd->sense,
3879 cmd->sense_phys_addr);
3883 * Now destroy the pool itself
3885 dma_pool_destroy(instance->frame_dma_pool);
3886 dma_pool_destroy(instance->sense_dma_pool);
3888 instance->frame_dma_pool = NULL;
3889 instance->sense_dma_pool = NULL;
3893 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3894 * @instance: Adapter soft state
3896 * Each command packet has an embedded DMA memory buffer that is used for
3897 * filling MFI frame and the SG list that immediately follows the frame. This
3898 * function creates those DMA memory buffers for each command packet by using
3899 * PCI pool facility.
3901 static int megasas_create_frame_pool(struct megasas_instance *instance)
3907 struct megasas_cmd *cmd;
3909 max_cmd = instance->max_mfi_cmds;
3912 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3913 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3915 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3916 sizeof(struct megasas_sge32);
3918 if (instance->flag_ieee)
3919 sge_sz = sizeof(struct megasas_sge_skinny);
3922 * For MFI controllers.
3924 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3925 * Total 960 byte (15 MFI frame of 64 byte)
3927 * Fusion adapter require only 3 extra frame.
3928 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3929 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3930 * Total 192 byte (3 MFI frame of 64 byte)
3932 frame_count = (instance->adapter_type == MFI_SERIES) ?
3934 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
3936 * Use DMA pool facility provided by PCI layer
3938 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
3939 &instance->pdev->dev,
3940 instance->mfi_frame_size, 256, 0);
3942 if (!instance->frame_dma_pool) {
3943 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
3947 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
3948 &instance->pdev->dev, 128,
3951 if (!instance->sense_dma_pool) {
3952 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
3954 dma_pool_destroy(instance->frame_dma_pool);
3955 instance->frame_dma_pool = NULL;
3961 * Allocate and attach a frame to each of the commands in cmd_list.
3962 * By making cmd->index as the context instead of the &cmd, we can
3963 * always use 32bit context regardless of the architecture
3965 for (i = 0; i < max_cmd; i++) {
3967 cmd = instance->cmd_list[i];
3969 cmd->frame = dma_pool_alloc(instance->frame_dma_pool,
3970 GFP_KERNEL, &cmd->frame_phys_addr);
3972 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
3973 GFP_KERNEL, &cmd->sense_phys_addr);
3976 * megasas_teardown_frame_pool() takes care of freeing
3977 * whatever has been allocated
3979 if (!cmd->frame || !cmd->sense) {
3980 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
3981 megasas_teardown_frame_pool(instance);
3985 memset(cmd->frame, 0, instance->mfi_frame_size);
3986 cmd->frame->io.context = cpu_to_le32(cmd->index);
3987 cmd->frame->io.pad_0 = 0;
3988 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
3989 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
3996 * megasas_free_cmds - Free all the cmds in the free cmd pool
3997 * @instance: Adapter soft state
3999 void megasas_free_cmds(struct megasas_instance *instance)
4003 /* First free the MFI frame pool */
4004 megasas_teardown_frame_pool(instance);
4006 /* Free all the commands in the cmd_list */
4007 for (i = 0; i < instance->max_mfi_cmds; i++)
4009 kfree(instance->cmd_list[i]);
4011 /* Free the cmd_list buffer itself */
4012 kfree(instance->cmd_list);
4013 instance->cmd_list = NULL;
4015 INIT_LIST_HEAD(&instance->cmd_pool);
4019 * megasas_alloc_cmds - Allocates the command packets
4020 * @instance: Adapter soft state
4022 * Each command that is issued to the FW, whether IO commands from the OS or
4023 * internal commands like IOCTLs, are wrapped in local data structure called
4024 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4027 * Each frame has a 32-bit field called context (tag). This context is used
4028 * to get back the megasas_cmd from the frame when a frame gets completed in
4029 * the ISR. Typically the address of the megasas_cmd itself would be used as
4030 * the context. But we wanted to keep the differences between 32 and 64 bit
4031 * systems to the mininum. We always use 32 bit integers for the context. In
4032 * this driver, the 32 bit values are the indices into an array cmd_list.
4033 * This array is used only to look up the megasas_cmd given the context. The
4034 * free commands themselves are maintained in a linked list called cmd_pool.
4036 int megasas_alloc_cmds(struct megasas_instance *instance)
4041 struct megasas_cmd *cmd;
4042 struct fusion_context *fusion;
4044 fusion = instance->ctrl_context;
4045 max_cmd = instance->max_mfi_cmds;
4048 * instance->cmd_list is an array of struct megasas_cmd pointers.
4049 * Allocate the dynamic array first and then allocate individual
4052 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
4054 if (!instance->cmd_list) {
4055 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
4059 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
4061 for (i = 0; i < max_cmd; i++) {
4062 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4065 if (!instance->cmd_list[i]) {
4067 for (j = 0; j < i; j++)
4068 kfree(instance->cmd_list[j]);
4070 kfree(instance->cmd_list);
4071 instance->cmd_list = NULL;
4077 for (i = 0; i < max_cmd; i++) {
4078 cmd = instance->cmd_list[i];
4079 memset(cmd, 0, sizeof(struct megasas_cmd));
4082 cmd->instance = instance;
4084 list_add_tail(&cmd->list, &instance->cmd_pool);
4088 * Create a frame pool and assign one frame to each cmd
4090 if (megasas_create_frame_pool(instance)) {
4091 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
4092 megasas_free_cmds(instance);
4100 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4101 * @instance: Adapter soft state
4103 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4104 * or FW is not under OCR.
4107 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4109 if (instance->adapter_type == MFI_SERIES)
4110 return KILL_ADAPTER;
4111 else if (instance->unload ||
4112 test_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE,
4113 &instance->reset_flags))
4114 return IGNORE_TIMEOUT;
4116 return INITIATE_OCR;
4120 megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
4123 struct megasas_cmd *cmd;
4124 struct megasas_dcmd_frame *dcmd;
4126 struct MR_PRIV_DEVICE *mr_device_priv_data;
4129 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
4130 cmd = megasas_get_cmd(instance);
4133 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4137 dcmd = &cmd->frame->dcmd;
4139 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4140 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4142 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4143 dcmd->cmd = MFI_CMD_DCMD;
4144 dcmd->cmd_status = 0xFF;
4145 dcmd->sge_count = 1;
4146 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4149 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4150 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4151 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
4152 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
4154 if ((instance->adapter_type != MFI_SERIES) &&
4155 !instance->mask_interrupts)
4156 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4158 ret = megasas_issue_polled(instance, cmd);
4162 mr_device_priv_data = sdev->hostdata;
4163 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4164 mr_device_priv_data->interface_type =
4165 instance->pd_info->state.ddf.pdType.intf;
4170 switch (dcmd_timeout_ocr_possible(instance)) {
4172 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4173 megasas_reset_fusion(instance->host,
4174 MFI_IO_TIMEOUT_OCR);
4177 megaraid_sas_kill_hba(instance);
4179 case IGNORE_TIMEOUT:
4180 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4181 __func__, __LINE__);
4188 if (ret != DCMD_TIMEOUT)
4189 megasas_return_cmd(instance, cmd);
4194 * megasas_get_pd_list_info - Returns FW's pd_list structure
4195 * @instance: Adapter soft state
4196 * @pd_list: pd_list structure
4198 * Issues an internal command (DCMD) to get the FW's controller PD
4199 * list structure. This information is mainly used to find out SYSTEM
4200 * supported by the FW.
4203 megasas_get_pd_list(struct megasas_instance *instance)
4205 int ret = 0, pd_index = 0;
4206 struct megasas_cmd *cmd;
4207 struct megasas_dcmd_frame *dcmd;
4208 struct MR_PD_LIST *ci;
4209 struct MR_PD_ADDRESS *pd_addr;
4210 dma_addr_t ci_h = 0;
4212 if (instance->pd_list_not_supported) {
4213 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4214 "not supported by firmware\n");
4218 cmd = megasas_get_cmd(instance);
4221 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4225 dcmd = &cmd->frame->dcmd;
4227 ci = pci_alloc_consistent(instance->pdev,
4228 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
4231 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
4232 megasas_return_cmd(instance, cmd);
4236 memset(ci, 0, sizeof(*ci));
4237 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4239 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4240 dcmd->mbox.b[1] = 0;
4241 dcmd->cmd = MFI_CMD_DCMD;
4242 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4243 dcmd->sge_count = 1;
4244 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4247 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4248 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4249 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4250 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4252 if ((instance->adapter_type != MFI_SERIES) &&
4253 !instance->mask_interrupts)
4254 ret = megasas_issue_blocked_cmd(instance, cmd,
4255 MFI_IO_TIMEOUT_SECS);
4257 ret = megasas_issue_polled(instance, cmd);
4261 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4262 "failed/not supported by firmware\n");
4264 if (instance->adapter_type != MFI_SERIES)
4265 megaraid_sas_kill_hba(instance);
4267 instance->pd_list_not_supported = 1;
4271 switch (dcmd_timeout_ocr_possible(instance)) {
4273 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4275 * DCMD failed from AEN path.
4276 * AEN path already hold reset_mutex to avoid PCI access
4277 * while OCR is in progress.
4279 mutex_unlock(&instance->reset_mutex);
4280 megasas_reset_fusion(instance->host,
4281 MFI_IO_TIMEOUT_OCR);
4282 mutex_lock(&instance->reset_mutex);
4285 megaraid_sas_kill_hba(instance);
4287 case IGNORE_TIMEOUT:
4288 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4289 __func__, __LINE__);
4298 if ((le32_to_cpu(ci->count) >
4299 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4302 memset(instance->local_pd_list, 0,
4303 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4305 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4306 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
4307 le16_to_cpu(pd_addr->deviceId);
4308 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
4309 pd_addr->scsiDevType;
4310 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
4315 memcpy(instance->pd_list, instance->local_pd_list,
4316 sizeof(instance->pd_list));
4321 pci_free_consistent(instance->pdev,
4322 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4325 if (ret != DCMD_TIMEOUT)
4326 megasas_return_cmd(instance, cmd);
4332 * megasas_get_ld_list_info - Returns FW's ld_list structure
4333 * @instance: Adapter soft state
4334 * @ld_list: ld_list structure
4336 * Issues an internal command (DCMD) to get the FW's controller PD
4337 * list structure. This information is mainly used to find out SYSTEM
4338 * supported by the FW.
4341 megasas_get_ld_list(struct megasas_instance *instance)
4343 int ret = 0, ld_index = 0, ids = 0;
4344 struct megasas_cmd *cmd;
4345 struct megasas_dcmd_frame *dcmd;
4346 struct MR_LD_LIST *ci;
4347 dma_addr_t ci_h = 0;
4350 cmd = megasas_get_cmd(instance);
4353 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4357 dcmd = &cmd->frame->dcmd;
4359 ci = pci_alloc_consistent(instance->pdev,
4360 sizeof(struct MR_LD_LIST),
4364 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
4365 megasas_return_cmd(instance, cmd);
4369 memset(ci, 0, sizeof(*ci));
4370 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4372 if (instance->supportmax256vd)
4373 dcmd->mbox.b[0] = 1;
4374 dcmd->cmd = MFI_CMD_DCMD;
4375 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4376 dcmd->sge_count = 1;
4377 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4379 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4380 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4381 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4382 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
4385 if ((instance->adapter_type != MFI_SERIES) &&
4386 !instance->mask_interrupts)
4387 ret = megasas_issue_blocked_cmd(instance, cmd,
4388 MFI_IO_TIMEOUT_SECS);
4390 ret = megasas_issue_polled(instance, cmd);
4392 ld_count = le32_to_cpu(ci->ldCount);
4396 megaraid_sas_kill_hba(instance);
4400 switch (dcmd_timeout_ocr_possible(instance)) {
4402 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4404 * DCMD failed from AEN path.
4405 * AEN path already hold reset_mutex to avoid PCI access
4406 * while OCR is in progress.
4408 mutex_unlock(&instance->reset_mutex);
4409 megasas_reset_fusion(instance->host,
4410 MFI_IO_TIMEOUT_OCR);
4411 mutex_lock(&instance->reset_mutex);
4414 megaraid_sas_kill_hba(instance);
4416 case IGNORE_TIMEOUT:
4417 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4418 __func__, __LINE__);
4425 if (ld_count > instance->fw_supported_vd_count)
4428 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4430 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4431 if (ci->ldList[ld_index].state != 0) {
4432 ids = ci->ldList[ld_index].ref.targetId;
4433 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4440 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
4442 if (ret != DCMD_TIMEOUT)
4443 megasas_return_cmd(instance, cmd);
4449 * megasas_ld_list_query - Returns FW's ld_list structure
4450 * @instance: Adapter soft state
4451 * @ld_list: ld_list structure
4453 * Issues an internal command (DCMD) to get the FW's controller PD
4454 * list structure. This information is mainly used to find out SYSTEM
4455 * supported by the FW.
4458 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4460 int ret = 0, ld_index = 0, ids = 0;
4461 struct megasas_cmd *cmd;
4462 struct megasas_dcmd_frame *dcmd;
4463 struct MR_LD_TARGETID_LIST *ci;
4464 dma_addr_t ci_h = 0;
4467 cmd = megasas_get_cmd(instance);
4470 dev_warn(&instance->pdev->dev,
4471 "megasas_ld_list_query: Failed to get cmd\n");
4475 dcmd = &cmd->frame->dcmd;
4477 ci = pci_alloc_consistent(instance->pdev,
4478 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4481 dev_warn(&instance->pdev->dev,
4482 "Failed to alloc mem for ld_list_query\n");
4483 megasas_return_cmd(instance, cmd);
4487 memset(ci, 0, sizeof(*ci));
4488 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4490 dcmd->mbox.b[0] = query_type;
4491 if (instance->supportmax256vd)
4492 dcmd->mbox.b[2] = 1;
4494 dcmd->cmd = MFI_CMD_DCMD;
4495 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4496 dcmd->sge_count = 1;
4497 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4499 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4500 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4501 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4502 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4505 if ((instance->adapter_type != MFI_SERIES) &&
4506 !instance->mask_interrupts)
4507 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4509 ret = megasas_issue_polled(instance, cmd);
4513 dev_info(&instance->pdev->dev,
4514 "DCMD not supported by firmware - %s %d\n",
4515 __func__, __LINE__);
4516 ret = megasas_get_ld_list(instance);
4519 switch (dcmd_timeout_ocr_possible(instance)) {
4521 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4523 * DCMD failed from AEN path.
4524 * AEN path already hold reset_mutex to avoid PCI access
4525 * while OCR is in progress.
4527 mutex_unlock(&instance->reset_mutex);
4528 megasas_reset_fusion(instance->host,
4529 MFI_IO_TIMEOUT_OCR);
4530 mutex_lock(&instance->reset_mutex);
4533 megaraid_sas_kill_hba(instance);
4535 case IGNORE_TIMEOUT:
4536 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4537 __func__, __LINE__);
4543 tgtid_count = le32_to_cpu(ci->count);
4545 if ((tgtid_count > (instance->fw_supported_vd_count)))
4548 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4549 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4550 ids = ci->targetId[ld_index];
4551 instance->ld_ids[ids] = ci->targetId[ld_index];
4557 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4560 if (ret != DCMD_TIMEOUT)
4561 megasas_return_cmd(instance, cmd);
4567 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4568 * instance : Controller's instance
4570 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4572 struct fusion_context *fusion;
4573 u32 ventura_map_sz = 0;
4575 fusion = instance->ctrl_context;
4576 /* For MFI based controllers return dummy success */
4580 instance->supportmax256vd =
4581 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4582 /* Below is additional check to address future FW enhancement */
4583 if (instance->ctrl_info->max_lds > 64)
4584 instance->supportmax256vd = 1;
4586 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4587 * MEGASAS_MAX_DEV_PER_CHANNEL;
4588 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4589 * MEGASAS_MAX_DEV_PER_CHANNEL;
4590 if (instance->supportmax256vd) {
4591 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4592 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4594 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4595 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4598 dev_info(&instance->pdev->dev,
4599 "firmware type\t: %s\n",
4600 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4601 "Legacy(64 VD) firmware");
4603 if (instance->max_raid_mapsize) {
4604 ventura_map_sz = instance->max_raid_mapsize *
4605 MR_MIN_MAP_SIZE; /* 64k */
4606 fusion->current_map_sz = ventura_map_sz;
4607 fusion->max_map_sz = ventura_map_sz;
4609 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4610 (sizeof(struct MR_LD_SPAN_MAP) *
4611 (instance->fw_supported_vd_count - 1));
4612 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4614 fusion->max_map_sz =
4615 max(fusion->old_map_sz, fusion->new_map_sz);
4617 if (instance->supportmax256vd)
4618 fusion->current_map_sz = fusion->new_map_sz;
4620 fusion->current_map_sz = fusion->old_map_sz;
4622 /* irrespective of FW raid maps, driver raid map is constant */
4623 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
4627 * megasas_get_controller_info - Returns FW's controller structure
4628 * @instance: Adapter soft state
4630 * Issues an internal command (DCMD) to get the FW's controller structure.
4631 * This information is mainly used to find out the maximum IO transfer per
4632 * command supported by the FW.
4635 megasas_get_ctrl_info(struct megasas_instance *instance)
4638 struct megasas_cmd *cmd;
4639 struct megasas_dcmd_frame *dcmd;
4640 struct megasas_ctrl_info *ci;
4641 struct megasas_ctrl_info *ctrl_info;
4642 dma_addr_t ci_h = 0;
4644 ctrl_info = instance->ctrl_info;
4646 cmd = megasas_get_cmd(instance);
4649 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4653 dcmd = &cmd->frame->dcmd;
4655 ci = pci_alloc_consistent(instance->pdev,
4656 sizeof(struct megasas_ctrl_info), &ci_h);
4659 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
4660 megasas_return_cmd(instance, cmd);
4664 memset(ci, 0, sizeof(*ci));
4665 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4667 dcmd->cmd = MFI_CMD_DCMD;
4668 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4669 dcmd->sge_count = 1;
4670 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4673 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4674 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4675 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4676 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4677 dcmd->mbox.b[0] = 1;
4679 if ((instance->adapter_type != MFI_SERIES) &&
4680 !instance->mask_interrupts)
4681 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4683 ret = megasas_issue_polled(instance, cmd);
4687 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
4688 /* Save required controller information in
4689 * CPU endianness format.
4691 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4692 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4693 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4694 le16_to_cpus((u16 *)&ctrl_info->adapter_operations4);
4696 /* Update the latest Ext VD info.
4697 * From Init path, store current firmware details.
4698 * From OCR path, detect any firmware properties changes.
4699 * in case of Firmware upgrade without system reboot.
4701 megasas_update_ext_vd_details(instance);
4702 instance->use_seqnum_jbod_fp =
4703 ctrl_info->adapterOperations3.useSeqNumJbodFP;
4704 instance->support_morethan256jbod =
4705 ctrl_info->adapter_operations4.support_pd_map_target_id;
4707 /*Check whether controller is iMR or MR */
4708 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4709 dev_info(&instance->pdev->dev,
4710 "controller type\t: %s(%dMB)\n",
4711 instance->is_imr ? "iMR" : "MR",
4712 le16_to_cpu(ctrl_info->memory_size));
4714 instance->disableOnlineCtrlReset =
4715 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4716 instance->secure_jbod_support =
4717 ctrl_info->adapterOperations3.supportSecurityonJBOD;
4718 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4719 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
4720 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4721 instance->secure_jbod_support ? "Yes" : "No");
4725 switch (dcmd_timeout_ocr_possible(instance)) {
4727 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4728 megasas_reset_fusion(instance->host,
4729 MFI_IO_TIMEOUT_OCR);
4732 megaraid_sas_kill_hba(instance);
4734 case IGNORE_TIMEOUT:
4735 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4736 __func__, __LINE__);
4740 megaraid_sas_kill_hba(instance);
4745 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4748 megasas_return_cmd(instance, cmd);
4755 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4758 * @instance: Adapter soft state
4759 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4760 MR_CRASH_BUF_TURN_OFF = 0
4761 MR_CRASH_BUF_TURN_ON = 1
4762 * @return 0 on success non-zero on failure.
4763 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4764 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4765 * that driver supports crash dump feature. This DCMD will be sent only if
4766 * crash dump feature is supported by the FW.
4769 int megasas_set_crash_dump_params(struct megasas_instance *instance,
4773 struct megasas_cmd *cmd;
4774 struct megasas_dcmd_frame *dcmd;
4776 cmd = megasas_get_cmd(instance);
4779 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4784 dcmd = &cmd->frame->dcmd;
4786 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4787 dcmd->mbox.b[0] = crash_buf_state;
4788 dcmd->cmd = MFI_CMD_DCMD;
4789 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4790 dcmd->sge_count = 1;
4791 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4794 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4795 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4796 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4797 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4799 if ((instance->adapter_type != MFI_SERIES) &&
4800 !instance->mask_interrupts)
4801 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4803 ret = megasas_issue_polled(instance, cmd);
4805 if (ret == DCMD_TIMEOUT) {
4806 switch (dcmd_timeout_ocr_possible(instance)) {
4808 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4809 megasas_reset_fusion(instance->host,
4810 MFI_IO_TIMEOUT_OCR);
4813 megaraid_sas_kill_hba(instance);
4815 case IGNORE_TIMEOUT:
4816 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4817 __func__, __LINE__);
4821 megasas_return_cmd(instance, cmd);
4827 * megasas_issue_init_mfi - Initializes the FW
4828 * @instance: Adapter soft state
4830 * Issues the INIT MFI cmd
4833 megasas_issue_init_mfi(struct megasas_instance *instance)
4836 struct megasas_cmd *cmd;
4837 struct megasas_init_frame *init_frame;
4838 struct megasas_init_queue_info *initq_info;
4839 dma_addr_t init_frame_h;
4840 dma_addr_t initq_info_h;
4843 * Prepare a init frame. Note the init frame points to queue info
4844 * structure. Each frame has SGL allocated after first 64 bytes. For
4845 * this frame - since we don't need any SGL - we use SGL's space as
4846 * queue info structure
4848 * We will not get a NULL command below. We just created the pool.
4850 cmd = megasas_get_cmd(instance);
4852 init_frame = (struct megasas_init_frame *)cmd->frame;
4853 initq_info = (struct megasas_init_queue_info *)
4854 ((unsigned long)init_frame + 64);
4856 init_frame_h = cmd->frame_phys_addr;
4857 initq_info_h = init_frame_h + 64;
4859 context = init_frame->context;
4860 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4861 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4862 init_frame->context = context;
4864 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4865 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
4867 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4868 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
4870 init_frame->cmd = MFI_CMD_INIT;
4871 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
4872 init_frame->queue_info_new_phys_addr_lo =
4873 cpu_to_le32(lower_32_bits(initq_info_h));
4874 init_frame->queue_info_new_phys_addr_hi =
4875 cpu_to_le32(upper_32_bits(initq_info_h));
4877 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
4880 * disable the intr before firing the init frame to FW
4882 instance->instancet->disable_intr(instance);
4885 * Issue the init frame in polled mode
4888 if (megasas_issue_polled(instance, cmd)) {
4889 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
4890 megasas_return_cmd(instance, cmd);
4894 megasas_return_cmd(instance, cmd);
4903 megasas_init_adapter_mfi(struct megasas_instance *instance)
4905 struct megasas_register_set __iomem *reg_set;
4909 reg_set = instance->reg_set;
4912 * Get various operational parameters from status register
4914 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
4916 * Reduce the max supported cmds by 1. This is to ensure that the
4917 * reply_q_sz (1 more than the max cmd that driver may send)
4918 * does not exceed max cmds that the FW can support
4920 instance->max_fw_cmds = instance->max_fw_cmds-1;
4921 instance->max_mfi_cmds = instance->max_fw_cmds;
4922 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
4925 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4926 * are reserved for IOCTL + driver's internal DCMDs.
4928 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4929 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4930 instance->max_scsi_cmds = (instance->max_fw_cmds -
4931 MEGASAS_SKINNY_INT_CMDS);
4932 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4934 instance->max_scsi_cmds = (instance->max_fw_cmds -
4936 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4939 instance->cur_can_queue = instance->max_scsi_cmds;
4941 * Create a pool of commands
4943 if (megasas_alloc_cmds(instance))
4944 goto fail_alloc_cmds;
4947 * Allocate memory for reply queue. Length of reply queue should
4948 * be _one_ more than the maximum commands handled by the firmware.
4950 * Note: When FW completes commands, it places corresponding contex
4951 * values in this circular reply queue. This circular queue is a fairly
4952 * typical producer-consumer queue. FW is the producer (of completed
4953 * commands) and the driver is the consumer.
4955 context_sz = sizeof(u32);
4956 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4958 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4960 &instance->reply_queue_h);
4962 if (!instance->reply_queue) {
4963 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
4964 goto fail_reply_queue;
4967 if (megasas_issue_init_mfi(instance))
4970 if (megasas_get_ctrl_info(instance)) {
4971 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4972 "Fail from %s %d\n", instance->unique_id,
4973 __func__, __LINE__);
4977 instance->fw_support_ieee = 0;
4978 instance->fw_support_ieee =
4979 (instance->instancet->read_fw_status_reg(reg_set) &
4982 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
4983 instance->fw_support_ieee);
4985 if (instance->fw_support_ieee)
4986 instance->flag_ieee = 1;
4992 pci_free_consistent(instance->pdev, reply_q_sz,
4993 instance->reply_queue, instance->reply_queue_h);
4995 megasas_free_cmds(instance);
5002 * megasas_setup_irqs_ioapic - register legacy interrupts.
5003 * @instance: Adapter soft state
5005 * Do not enable interrupt, only setup ISRs.
5007 * Return 0 on success.
5010 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5012 struct pci_dev *pdev;
5014 pdev = instance->pdev;
5015 instance->irq_context[0].instance = instance;
5016 instance->irq_context[0].MSIxIndex = 0;
5017 if (request_irq(pci_irq_vector(pdev, 0),
5018 instance->instancet->service_isr, IRQF_SHARED,
5019 "megasas", &instance->irq_context[0])) {
5020 dev_err(&instance->pdev->dev,
5021 "Failed to register IRQ from %s %d\n",
5022 __func__, __LINE__);
5029 * megasas_setup_irqs_msix - register MSI-x interrupts.
5030 * @instance: Adapter soft state
5031 * @is_probe: Driver probe check
5033 * Do not enable interrupt, only setup ISRs.
5035 * Return 0 on success.
5038 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5041 struct pci_dev *pdev;
5043 pdev = instance->pdev;
5046 for (i = 0; i < instance->msix_vectors; i++) {
5047 instance->irq_context[i].instance = instance;
5048 instance->irq_context[i].MSIxIndex = i;
5049 if (request_irq(pci_irq_vector(pdev, i),
5050 instance->instancet->service_isr, 0, "megasas",
5051 &instance->irq_context[i])) {
5052 dev_err(&instance->pdev->dev,
5053 "Failed to register IRQ for vector %d.\n", i);
5054 for (j = 0; j < i; j++)
5055 free_irq(pci_irq_vector(pdev, j),
5056 &instance->irq_context[j]);
5057 /* Retry irq register for IO_APIC*/
5058 instance->msix_vectors = 0;
5060 pci_free_irq_vectors(instance->pdev);
5061 return megasas_setup_irqs_ioapic(instance);
5071 * megasas_destroy_irqs- unregister interrupts.
5072 * @instance: Adapter soft state
5076 megasas_destroy_irqs(struct megasas_instance *instance) {
5080 if (instance->msix_vectors)
5081 for (i = 0; i < instance->msix_vectors; i++) {
5082 free_irq(pci_irq_vector(instance->pdev, i),
5083 &instance->irq_context[i]);
5086 free_irq(pci_irq_vector(instance->pdev, 0),
5087 &instance->irq_context[0]);
5091 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5092 * @instance: Adapter soft state
5093 * @is_probe: Driver probe check
5095 * Return 0 on success.
5098 megasas_setup_jbod_map(struct megasas_instance *instance)
5101 struct fusion_context *fusion = instance->ctrl_context;
5104 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5105 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5107 if (reset_devices || !fusion ||
5108 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
5109 dev_info(&instance->pdev->dev,
5110 "Jbod map is not supported %s %d\n",
5111 __func__, __LINE__);
5112 instance->use_seqnum_jbod_fp = false;
5116 if (fusion->pd_seq_sync[0])
5119 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5120 fusion->pd_seq_sync[i] = dma_alloc_coherent
5121 (&instance->pdev->dev, pd_seq_map_sz,
5122 &fusion->pd_seq_phys[i], GFP_KERNEL);
5123 if (!fusion->pd_seq_sync[i]) {
5124 dev_err(&instance->pdev->dev,
5125 "Failed to allocate memory from %s %d\n",
5126 __func__, __LINE__);
5128 dma_free_coherent(&instance->pdev->dev,
5129 pd_seq_map_sz, fusion->pd_seq_sync[0],
5130 fusion->pd_seq_phys[0]);
5131 fusion->pd_seq_sync[0] = NULL;
5133 instance->use_seqnum_jbod_fp = false;
5139 if (!megasas_sync_pd_seq_num(instance, false) &&
5140 !megasas_sync_pd_seq_num(instance, true))
5141 instance->use_seqnum_jbod_fp = true;
5143 instance->use_seqnum_jbod_fp = false;
5146 static void megasas_setup_reply_map(struct megasas_instance *instance)
5148 const struct cpumask *mask;
5149 unsigned int queue, cpu;
5151 for (queue = 0; queue < instance->msix_vectors; queue++) {
5152 mask = pci_irq_get_affinity(instance->pdev, queue);
5156 for_each_cpu(cpu, mask)
5157 instance->reply_map[cpu] = queue;
5162 for_each_possible_cpu(cpu)
5163 instance->reply_map[cpu] = cpu % instance->msix_vectors;
5167 * megasas_init_fw - Initializes the FW
5168 * @instance: Adapter soft state
5170 * This is the main function for initializing firmware
5173 static int megasas_init_fw(struct megasas_instance *instance)
5176 u32 max_sectors_2, tmp_sectors, msix_enable;
5177 u32 scratch_pad_2, scratch_pad_3, scratch_pad_4;
5178 resource_size_t base_addr;
5179 struct megasas_register_set __iomem *reg_set;
5180 struct megasas_ctrl_info *ctrl_info = NULL;
5181 unsigned long bar_list;
5182 int i, j, loop, fw_msix_count = 0;
5183 struct IOV_111 *iovPtr;
5184 struct fusion_context *fusion;
5186 fusion = instance->ctrl_context;
5188 /* Find first memory bar */
5189 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
5190 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
5191 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
5193 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
5197 base_addr = pci_resource_start(instance->pdev, instance->bar);
5198 instance->reg_set = ioremap_nocache(base_addr, 8192);
5200 if (!instance->reg_set) {
5201 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
5205 reg_set = instance->reg_set;
5207 if (instance->adapter_type != MFI_SERIES)
5208 instance->instancet = &megasas_instance_template_fusion;
5210 switch (instance->pdev->device) {
5211 case PCI_DEVICE_ID_LSI_SAS1078R:
5212 case PCI_DEVICE_ID_LSI_SAS1078DE:
5213 instance->instancet = &megasas_instance_template_ppc;
5215 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5216 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5217 instance->instancet = &megasas_instance_template_gen2;
5219 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5220 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5221 instance->instancet = &megasas_instance_template_skinny;
5223 case PCI_DEVICE_ID_LSI_SAS1064R:
5224 case PCI_DEVICE_ID_DELL_PERC5:
5226 instance->instancet = &megasas_instance_template_xscale;
5227 instance->pd_list_not_supported = 1;
5232 if (megasas_transition_to_ready(instance, 0)) {
5233 atomic_set(&instance->fw_reset_no_pci_access, 1);
5234 instance->instancet->adp_reset
5235 (instance, instance->reg_set);
5236 atomic_set(&instance->fw_reset_no_pci_access, 0);
5237 dev_info(&instance->pdev->dev,
5238 "FW restarted successfully from %s!\n",
5241 /*waitting for about 30 second before retry*/
5244 if (megasas_transition_to_ready(instance, 0))
5245 goto fail_ready_state;
5248 if (instance->adapter_type == VENTURA_SERIES) {
5250 readl(&instance->reg_set->outbound_scratch_pad_3);
5251 instance->max_raid_mapsize = ((scratch_pad_3 >>
5252 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5253 MR_MAX_RAID_MAP_SIZE_MASK);
5256 /* Check if MSI-X is supported while in ready state */
5257 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5259 if (msix_enable && !msix_disable) {
5260 int irq_flags = PCI_IRQ_MSIX;
5262 scratch_pad_2 = readl
5263 (&instance->reg_set->outbound_scratch_pad_2);
5264 /* Check max MSI-X vectors */
5266 if (instance->adapter_type == THUNDERBOLT_SERIES) {
5267 /* Thunderbolt Series*/
5268 instance->msix_vectors = (scratch_pad_2
5269 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5270 fw_msix_count = instance->msix_vectors;
5271 } else { /* Invader series supports more than 8 MSI-x vectors*/
5272 instance->msix_vectors = ((scratch_pad_2
5273 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5274 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
5275 if (instance->msix_vectors > 16)
5276 instance->msix_combined = true;
5279 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5281 fw_msix_count = instance->msix_vectors;
5282 /* Save 1-15 reply post index address to local memory
5283 * Index 0 is already saved from reg offset
5284 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5286 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5287 instance->reply_post_host_index_addr[loop] =
5289 ((u8 __iomem *)instance->reg_set +
5290 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5295 instance->msix_vectors = min(msix_vectors,
5296 instance->msix_vectors);
5297 } else /* MFI adapters */
5298 instance->msix_vectors = 1;
5299 /* Don't bother allocating more MSI-X vectors than cpus */
5300 instance->msix_vectors = min(instance->msix_vectors,
5301 (unsigned int)num_online_cpus());
5302 if (smp_affinity_enable)
5303 irq_flags |= PCI_IRQ_AFFINITY;
5304 i = pci_alloc_irq_vectors(instance->pdev, 1,
5305 instance->msix_vectors, irq_flags);
5307 instance->msix_vectors = i;
5309 instance->msix_vectors = 0;
5312 * MSI-X host index 0 is common for all adapter.
5313 * It is used for all MPT based Adapters.
5315 if (instance->msix_combined) {
5316 instance->reply_post_host_index_addr[0] =
5317 (u32 *)((u8 *)instance->reg_set +
5318 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5320 instance->reply_post_host_index_addr[0] =
5321 (u32 *)((u8 *)instance->reg_set +
5322 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5325 if (!instance->msix_vectors) {
5326 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5328 goto fail_init_adapter;
5331 megasas_setup_reply_map(instance);
5333 dev_info(&instance->pdev->dev,
5334 "firmware supports msix\t: (%d)", fw_msix_count);
5335 dev_info(&instance->pdev->dev,
5336 "current msix/online cpus\t: (%d/%d)\n",
5337 instance->msix_vectors, (unsigned int)num_online_cpus());
5338 dev_info(&instance->pdev->dev,
5339 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
5341 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5342 (unsigned long)instance);
5344 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5346 if (instance->ctrl_info == NULL)
5347 goto fail_init_adapter;
5350 * Below are default value for legacy Firmware.
5351 * non-fusion based controllers
5353 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5354 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5355 /* Get operational params, sge flags, send init cmd to controller */
5356 if (instance->instancet->init_adapter(instance))
5357 goto fail_init_adapter;
5359 if (instance->adapter_type == VENTURA_SERIES) {
5361 readl(&instance->reg_set->outbound_scratch_pad_4);
5362 if ((scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK) >=
5363 MR_DEFAULT_NVME_PAGE_SHIFT)
5364 instance->nvme_page_size =
5365 (1 << (scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK));
5367 dev_info(&instance->pdev->dev,
5368 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5371 if (instance->msix_vectors ?
5372 megasas_setup_irqs_msix(instance, 1) :
5373 megasas_setup_irqs_ioapic(instance))
5374 goto fail_init_adapter;
5376 instance->instancet->enable_intr(instance);
5378 dev_info(&instance->pdev->dev, "INIT adapter done\n");
5380 megasas_setup_jbod_map(instance);
5383 * the following function will get the PD LIST.
5385 memset(instance->pd_list, 0,
5386 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5387 if (megasas_get_pd_list(instance) < 0) {
5388 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5389 goto fail_get_ld_pd_list;
5392 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5394 /* stream detection initialization */
5395 if (instance->adapter_type == VENTURA_SERIES) {
5396 fusion->stream_detect_by_ld =
5397 kzalloc(sizeof(struct LD_STREAM_DETECT *)
5398 * MAX_LOGICAL_DRIVES_EXT,
5400 if (!fusion->stream_detect_by_ld) {
5401 dev_err(&instance->pdev->dev,
5402 "unable to allocate stream detection for pool of LDs\n");
5403 goto fail_get_ld_pd_list;
5405 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5406 fusion->stream_detect_by_ld[i] =
5407 kmalloc(sizeof(struct LD_STREAM_DETECT),
5409 if (!fusion->stream_detect_by_ld[i]) {
5410 dev_err(&instance->pdev->dev,
5411 "unable to allocate stream detect by LD\n ");
5412 for (j = 0; j < i; ++j)
5413 kfree(fusion->stream_detect_by_ld[j]);
5414 kfree(fusion->stream_detect_by_ld);
5415 fusion->stream_detect_by_ld = NULL;
5416 goto fail_get_ld_pd_list;
5418 fusion->stream_detect_by_ld[i]->mru_bit_map
5423 if (megasas_ld_list_query(instance,
5424 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5425 goto fail_get_ld_pd_list;
5428 * Compute the max allowed sectors per IO: The controller info has two
5429 * limits on max sectors. Driver should use the minimum of these two.
5431 * 1 << stripe_sz_ops.min = max sectors per strip
5433 * Note that older firmwares ( < FW ver 30) didn't report information
5434 * to calculate max_sectors_1. So the number ended up as zero always.
5437 ctrl_info = instance->ctrl_info;
5439 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5440 le16_to_cpu(ctrl_info->max_strips_per_io);
5441 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
5443 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
5445 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5446 instance->passive = ctrl_info->cluster.passive;
5447 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
5448 instance->UnevenSpanSupport =
5449 ctrl_info->adapterOperations2.supportUnevenSpans;
5450 if (instance->UnevenSpanSupport) {
5451 struct fusion_context *fusion = instance->ctrl_context;
5452 if (MR_ValidateMapInfo(instance))
5453 fusion->fast_path_io = 1;
5455 fusion->fast_path_io = 0;
5458 if (ctrl_info->host_interface.SRIOV) {
5459 instance->requestorId = ctrl_info->iov.requestorId;
5460 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5461 if (!ctrl_info->adapterOperations2.activePassive)
5462 instance->PlasmaFW111 = 1;
5464 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5465 instance->PlasmaFW111 ? "1.11" : "new");
5467 if (instance->PlasmaFW111) {
5468 iovPtr = (struct IOV_111 *)
5469 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5470 instance->requestorId = iovPtr->requestorId;
5473 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5474 instance->requestorId);
5477 instance->crash_dump_fw_support =
5478 ctrl_info->adapterOperations3.supportCrashDump;
5479 instance->crash_dump_drv_support =
5480 (instance->crash_dump_fw_support &&
5481 instance->crash_dump_buf);
5482 if (instance->crash_dump_drv_support)
5483 megasas_set_crash_dump_params(instance,
5484 MR_CRASH_BUF_TURN_OFF);
5487 if (instance->crash_dump_buf)
5488 pci_free_consistent(instance->pdev,
5490 instance->crash_dump_buf,
5491 instance->crash_dump_h);
5492 instance->crash_dump_buf = NULL;
5496 dev_info(&instance->pdev->dev,
5497 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5498 le16_to_cpu(ctrl_info->pci.vendor_id),
5499 le16_to_cpu(ctrl_info->pci.device_id),
5500 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5501 le16_to_cpu(ctrl_info->pci.sub_device_id));
5502 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5503 instance->UnevenSpanSupport ? "yes" : "no");
5504 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5505 instance->crash_dump_drv_support ? "yes" : "no");
5506 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5507 instance->use_seqnum_jbod_fp ? "yes" : "no");
5510 instance->max_sectors_per_req = instance->max_num_sge *
5511 SGE_BUFFER_SIZE / 512;
5512 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5513 instance->max_sectors_per_req = tmp_sectors;
5515 /* Check for valid throttlequeuedepth module parameter */
5516 if (throttlequeuedepth &&
5517 throttlequeuedepth <= instance->max_scsi_cmds)
5518 instance->throttlequeuedepth = throttlequeuedepth;
5520 instance->throttlequeuedepth =
5521 MEGASAS_THROTTLE_QUEUE_DEPTH;
5523 if ((resetwaittime < 1) ||
5524 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
5525 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5527 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5528 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
5530 /* Launch SR-IOV heartbeat timer */
5531 if (instance->requestorId) {
5532 if (!megasas_sriov_start_heartbeat(instance, 1))
5533 megasas_start_timer(instance,
5534 &instance->sriov_heartbeat_timer,
5535 megasas_sriov_heartbeat_handler,
5536 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5538 instance->skip_heartbeat_timer_del = 1;
5543 fail_get_ld_pd_list:
5544 instance->instancet->disable_intr(instance);
5545 megasas_destroy_irqs(instance);
5547 if (instance->msix_vectors)
5548 pci_free_irq_vectors(instance->pdev);
5549 instance->msix_vectors = 0;
5551 kfree(instance->ctrl_info);
5552 instance->ctrl_info = NULL;
5553 iounmap(instance->reg_set);
5556 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
5558 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5559 __func__, __LINE__);
5564 * megasas_release_mfi - Reverses the FW initialization
5565 * @instance: Adapter soft state
5567 static void megasas_release_mfi(struct megasas_instance *instance)
5569 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
5571 if (instance->reply_queue)
5572 pci_free_consistent(instance->pdev, reply_q_sz,
5573 instance->reply_queue, instance->reply_queue_h);
5575 megasas_free_cmds(instance);
5577 iounmap(instance->reg_set);
5579 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
5583 * megasas_get_seq_num - Gets latest event sequence numbers
5584 * @instance: Adapter soft state
5585 * @eli: FW event log sequence numbers information
5587 * FW maintains a log of all events in a non-volatile area. Upper layers would
5588 * usually find out the latest sequence number of the events, the seq number at
5589 * the boot etc. They would "read" all the events below the latest seq number
5590 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5591 * number), they would subsribe to AEN (asynchronous event notification) and
5592 * wait for the events to happen.
5595 megasas_get_seq_num(struct megasas_instance *instance,
5596 struct megasas_evt_log_info *eli)
5598 struct megasas_cmd *cmd;
5599 struct megasas_dcmd_frame *dcmd;
5600 struct megasas_evt_log_info *el_info;
5601 dma_addr_t el_info_h = 0;
5603 cmd = megasas_get_cmd(instance);
5609 dcmd = &cmd->frame->dcmd;
5610 el_info = pci_alloc_consistent(instance->pdev,
5611 sizeof(struct megasas_evt_log_info),
5615 megasas_return_cmd(instance, cmd);
5619 memset(el_info, 0, sizeof(*el_info));
5620 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5622 dcmd->cmd = MFI_CMD_DCMD;
5623 dcmd->cmd_status = 0x0;
5624 dcmd->sge_count = 1;
5625 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5628 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5629 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5630 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5631 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5633 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5636 * Copy the data back into callers buffer
5638 eli->newest_seq_num = el_info->newest_seq_num;
5639 eli->oldest_seq_num = el_info->oldest_seq_num;
5640 eli->clear_seq_num = el_info->clear_seq_num;
5641 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5642 eli->boot_seq_num = el_info->boot_seq_num;
5644 dev_err(&instance->pdev->dev, "DCMD failed "
5645 "from %s\n", __func__);
5647 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5648 el_info, el_info_h);
5650 megasas_return_cmd(instance, cmd);
5656 * megasas_register_aen - Registers for asynchronous event notification
5657 * @instance: Adapter soft state
5658 * @seq_num: The starting sequence number
5659 * @class_locale: Class of the event
5661 * This function subscribes for AEN for events beyond the @seq_num. It requests
5662 * to be notified if and only if the event is of type @class_locale
5665 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5666 u32 class_locale_word)
5669 struct megasas_cmd *cmd;
5670 struct megasas_dcmd_frame *dcmd;
5671 union megasas_evt_class_locale curr_aen;
5672 union megasas_evt_class_locale prev_aen;
5675 * If there an AEN pending already (aen_cmd), check if the
5676 * class_locale of that pending AEN is inclusive of the new
5677 * AEN request we currently have. If it is, then we don't have
5678 * to do anything. In other words, whichever events the current
5679 * AEN request is subscribing to, have already been subscribed
5682 * If the old_cmd is _not_ inclusive, then we have to abort
5683 * that command, form a class_locale that is superset of both
5684 * old and current and re-issue to the FW
5687 curr_aen.word = class_locale_word;
5689 if (instance->aen_cmd) {
5692 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
5694 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
5695 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
5696 dev_info(&instance->pdev->dev,
5697 "%s %d out of range class %d send by application\n",
5698 __func__, __LINE__, curr_aen.members.class);
5703 * A class whose enum value is smaller is inclusive of all
5704 * higher values. If a PROGRESS (= -1) was previously
5705 * registered, then a new registration requests for higher
5706 * classes need not be sent to FW. They are automatically
5709 * Locale numbers don't have such hierarchy. They are bitmap
5712 if ((prev_aen.members.class <= curr_aen.members.class) &&
5713 !((prev_aen.members.locale & curr_aen.members.locale) ^
5714 curr_aen.members.locale)) {
5716 * Previously issued event registration includes
5717 * current request. Nothing to do.
5721 curr_aen.members.locale |= prev_aen.members.locale;
5723 if (prev_aen.members.class < curr_aen.members.class)
5724 curr_aen.members.class = prev_aen.members.class;
5726 instance->aen_cmd->abort_aen = 1;
5727 ret_val = megasas_issue_blocked_abort_cmd(instance,
5732 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
5733 "previous AEN command\n");
5739 cmd = megasas_get_cmd(instance);
5744 dcmd = &cmd->frame->dcmd;
5746 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5749 * Prepare DCMD for aen registration
5751 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5753 dcmd->cmd = MFI_CMD_DCMD;
5754 dcmd->cmd_status = 0x0;
5755 dcmd->sge_count = 1;
5756 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5759 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5760 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5761 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
5762 instance->last_seq_num = seq_num;
5763 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5764 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5765 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
5767 if (instance->aen_cmd != NULL) {
5768 megasas_return_cmd(instance, cmd);
5773 * Store reference to the cmd used to register for AEN. When an
5774 * application wants us to register for AEN, we have to abort this
5775 * cmd and re-register with a new EVENT LOCALE supplied by that app
5777 instance->aen_cmd = cmd;
5780 * Issue the aen registration frame
5782 instance->instancet->issue_dcmd(instance, cmd);
5787 /* megasas_get_target_prop - Send DCMD with below details to firmware.
5789 * This DCMD will fetch few properties of LD/system PD defined
5790 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
5792 * DCMD send by drivers whenever new target is added to the OS.
5794 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
5795 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
5796 * 0 = system PD, 1 = LD.
5797 * dcmd.mbox.s[1] - TargetID for LD/system PD.
5798 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
5800 * @instance: Adapter soft state
5801 * @sdev: OS provided scsi device
5803 * Returns 0 on success non-zero on failure.
5806 megasas_get_target_prop(struct megasas_instance *instance,
5807 struct scsi_device *sdev)
5810 struct megasas_cmd *cmd;
5811 struct megasas_dcmd_frame *dcmd;
5812 u16 targetId = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL) +
5815 cmd = megasas_get_cmd(instance);
5818 dev_err(&instance->pdev->dev,
5819 "Failed to get cmd %s\n", __func__);
5823 dcmd = &cmd->frame->dcmd;
5825 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
5826 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5827 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
5829 dcmd->mbox.s[1] = cpu_to_le16(targetId);
5830 dcmd->cmd = MFI_CMD_DCMD;
5831 dcmd->cmd_status = 0xFF;
5832 dcmd->sge_count = 1;
5833 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5836 dcmd->data_xfer_len =
5837 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5838 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
5839 dcmd->sgl.sge32[0].phys_addr =
5840 cpu_to_le32(instance->tgt_prop_h);
5841 dcmd->sgl.sge32[0].length =
5842 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5844 if ((instance->adapter_type != MFI_SERIES) &&
5845 !instance->mask_interrupts)
5846 ret = megasas_issue_blocked_cmd(instance,
5847 cmd, MFI_IO_TIMEOUT_SECS);
5849 ret = megasas_issue_polled(instance, cmd);
5853 switch (dcmd_timeout_ocr_possible(instance)) {
5855 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5856 megasas_reset_fusion(instance->host,
5857 MFI_IO_TIMEOUT_OCR);
5860 megaraid_sas_kill_hba(instance);
5862 case IGNORE_TIMEOUT:
5863 dev_info(&instance->pdev->dev,
5864 "Ignore DCMD timeout: %s %d\n",
5865 __func__, __LINE__);
5871 megasas_return_cmd(instance, cmd);
5873 if (ret != DCMD_SUCCESS)
5874 dev_err(&instance->pdev->dev,
5875 "return from %s %d return value %d\n",
5876 __func__, __LINE__, ret);
5882 * megasas_start_aen - Subscribes to AEN during driver load time
5883 * @instance: Adapter soft state
5885 static int megasas_start_aen(struct megasas_instance *instance)
5887 struct megasas_evt_log_info eli;
5888 union megasas_evt_class_locale class_locale;
5891 * Get the latest sequence number from FW
5893 memset(&eli, 0, sizeof(eli));
5895 if (megasas_get_seq_num(instance, &eli))
5899 * Register AEN with FW for latest sequence number plus 1
5901 class_locale.members.reserved = 0;
5902 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5903 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5905 return megasas_register_aen(instance,
5906 le32_to_cpu(eli.newest_seq_num) + 1,
5911 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5912 * @instance: Adapter soft state
5914 static int megasas_io_attach(struct megasas_instance *instance)
5916 struct Scsi_Host *host = instance->host;
5919 * Export parameters required by SCSI mid-layer
5921 host->unique_id = instance->unique_id;
5922 host->can_queue = instance->max_scsi_cmds;
5923 host->this_id = instance->init_id;
5924 host->sg_tablesize = instance->max_num_sge;
5926 if (instance->fw_support_ieee)
5927 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5930 * Check if the module parameter value for max_sectors can be used
5932 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5933 instance->max_sectors_per_req = max_sectors;
5936 if (((instance->pdev->device ==
5937 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5938 (instance->pdev->device ==
5939 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5940 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5941 instance->max_sectors_per_req = max_sectors;
5943 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
5944 "and <= %d (or < 1MB for GEN2 controller)\n",
5945 instance->max_sectors_per_req);
5950 host->max_sectors = instance->max_sectors_per_req;
5951 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
5952 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5953 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5954 host->max_lun = MEGASAS_MAX_LUN;
5955 host->max_cmd_len = 16;
5958 * Notify the mid-layer about the new controller
5960 if (scsi_add_host(host, &instance->pdev->dev)) {
5961 dev_err(&instance->pdev->dev,
5962 "Failed to add host from %s %d\n",
5963 __func__, __LINE__);
5971 megasas_set_dma_mask(struct pci_dev *pdev)
5974 * All our controllers are capable of performing 64-bit DMA
5977 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
5979 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5980 goto fail_set_dma_mask;
5983 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5984 goto fail_set_dma_mask;
5987 * Ensure that all data structures are allocated in 32-bit
5990 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5991 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5992 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5993 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5994 dev_info(&pdev->dev, "set 32bit DMA mask"
5995 "and 32 bit consistent mask\n");
5997 goto fail_set_dma_mask;
6007 * megasas_set_adapter_type - Set adapter type.
6008 * Supported controllers can be divided in
6009 * 4 categories- enum MR_ADAPTER_TYPE {
6011 * THUNDERBOLT_SERIES = 2,
6012 * INVADER_SERIES = 3,
6013 * VENTURA_SERIES = 4,
6015 * @instance: Adapter soft state
6018 static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6020 switch (instance->pdev->device) {
6021 case PCI_DEVICE_ID_LSI_VENTURA:
6022 case PCI_DEVICE_ID_LSI_HARPOON:
6023 case PCI_DEVICE_ID_LSI_TOMCAT:
6024 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6025 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6026 instance->adapter_type = VENTURA_SERIES;
6028 case PCI_DEVICE_ID_LSI_FUSION:
6029 case PCI_DEVICE_ID_LSI_PLASMA:
6030 instance->adapter_type = THUNDERBOLT_SERIES;
6032 case PCI_DEVICE_ID_LSI_INVADER:
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:
6037 case PCI_DEVICE_ID_LSI_FURY:
6038 instance->adapter_type = INVADER_SERIES;
6040 default: /* For all other supported controllers */
6041 instance->adapter_type = MFI_SERIES;
6046 static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6048 instance->producer = pci_alloc_consistent(instance->pdev, sizeof(u32),
6049 &instance->producer_h);
6050 instance->consumer = pci_alloc_consistent(instance->pdev, sizeof(u32),
6051 &instance->consumer_h);
6053 if (!instance->producer || !instance->consumer) {
6054 dev_err(&instance->pdev->dev,
6055 "Failed to allocate memory for producer, consumer\n");
6059 *instance->producer = 0;
6060 *instance->consumer = 0;
6065 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
6066 * structures which are not common across MFI
6067 * adapters and fusion adapters.
6068 * For MFI based adapters, allocate producer and
6069 * consumer buffers. For fusion adapters, allocate
6070 * memory for fusion context.
6071 * @instance: Adapter soft state
6072 * return: 0 for SUCCESS
6074 static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6076 instance->reply_map = kzalloc(sizeof(unsigned int) * nr_cpu_ids,
6078 if (!instance->reply_map)
6081 switch (instance->adapter_type) {
6083 if (megasas_alloc_mfi_ctrl_mem(instance))
6086 case VENTURA_SERIES:
6087 case THUNDERBOLT_SERIES:
6088 case INVADER_SERIES:
6089 if (megasas_alloc_fusion_context(instance))
6096 kfree(instance->reply_map);
6097 instance->reply_map = NULL;
6102 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
6103 * producer, consumer buffers for MFI adapters
6105 * @instance - Adapter soft instance
6108 static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6110 kfree(instance->reply_map);
6111 if (instance->adapter_type == MFI_SERIES) {
6112 if (instance->producer)
6113 pci_free_consistent(instance->pdev, sizeof(u32),
6115 instance->producer_h);
6116 if (instance->consumer)
6117 pci_free_consistent(instance->pdev, sizeof(u32),
6119 instance->consumer_h);
6121 megasas_free_fusion_context(instance);
6126 * megasas_probe_one - PCI hotplug entry point
6127 * @pdev: PCI device structure
6128 * @id: PCI ids of supported hotplugged adapter
6130 static int megasas_probe_one(struct pci_dev *pdev,
6131 const struct pci_device_id *id)
6134 struct Scsi_Host *host;
6135 struct megasas_instance *instance;
6138 /* Reset MSI-X in the kdump kernel */
6139 if (reset_devices) {
6140 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
6142 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
6144 if (control & PCI_MSIX_FLAGS_ENABLE) {
6145 dev_info(&pdev->dev, "resetting MSI-X\n");
6146 pci_write_config_word(pdev,
6147 pos + PCI_MSIX_FLAGS,
6149 ~PCI_MSIX_FLAGS_ENABLE);
6155 * PCI prepping: enable device set bus mastering and dma mask
6157 rval = pci_enable_device_mem(pdev);
6163 pci_set_master(pdev);
6165 if (megasas_set_dma_mask(pdev))
6166 goto fail_set_dma_mask;
6168 host = scsi_host_alloc(&megasas_template,
6169 sizeof(struct megasas_instance));
6172 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
6173 goto fail_alloc_instance;
6176 instance = (struct megasas_instance *)host->hostdata;
6177 memset(instance, 0, sizeof(*instance));
6178 atomic_set(&instance->fw_reset_no_pci_access, 0);
6179 instance->pdev = pdev;
6181 megasas_set_adapter_type(instance);
6183 if (megasas_alloc_ctrl_mem(instance))
6184 goto fail_alloc_dma_buf;
6186 /* Crash dump feature related initialisation*/
6187 instance->drv_buf_index = 0;
6188 instance->drv_buf_alloc = 0;
6189 instance->crash_dump_fw_support = 0;
6190 instance->crash_dump_app_support = 0;
6191 instance->fw_crash_state = UNAVAILABLE;
6192 spin_lock_init(&instance->crashdump_lock);
6193 instance->crash_dump_buf = NULL;
6195 megasas_poll_wait_aen = 0;
6196 instance->flag_ieee = 0;
6197 instance->ev = NULL;
6198 instance->issuepend_done = 1;
6199 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
6200 instance->is_imr = 0;
6202 instance->evt_detail = pci_alloc_consistent(pdev,
6204 megasas_evt_detail),
6205 &instance->evt_detail_h);
6207 if (!instance->evt_detail) {
6208 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
6209 "event detail structure\n");
6210 goto fail_alloc_dma_buf;
6213 if (!reset_devices) {
6214 instance->system_info_buf = pci_zalloc_consistent(pdev,
6215 sizeof(struct MR_DRV_SYSTEM_INFO),
6216 &instance->system_info_h);
6217 if (!instance->system_info_buf)
6218 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
6220 instance->pd_info = pci_alloc_consistent(pdev,
6221 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
6223 if (!instance->pd_info)
6224 dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
6226 instance->tgt_prop = pci_alloc_consistent(pdev,
6227 sizeof(struct MR_TARGET_PROPERTIES), &instance->tgt_prop_h);
6229 if (!instance->tgt_prop)
6230 dev_err(&instance->pdev->dev, "Failed to alloc mem for tgt_prop\n");
6232 instance->crash_dump_buf = pci_alloc_consistent(pdev,
6234 &instance->crash_dump_h);
6235 if (!instance->crash_dump_buf)
6236 dev_err(&pdev->dev, "Can't allocate Firmware "
6237 "crash dump DMA buffer\n");
6241 * Initialize locks and queues
6243 INIT_LIST_HEAD(&instance->cmd_pool);
6244 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
6246 atomic_set(&instance->fw_outstanding,0);
6248 init_waitqueue_head(&instance->int_cmd_wait_q);
6249 init_waitqueue_head(&instance->abort_cmd_wait_q);
6251 spin_lock_init(&instance->mfi_pool_lock);
6252 spin_lock_init(&instance->hba_lock);
6253 spin_lock_init(&instance->stream_lock);
6254 spin_lock_init(&instance->completion_lock);
6256 mutex_init(&instance->reset_mutex);
6257 mutex_init(&instance->hba_mutex);
6260 * Initialize PCI related and misc parameters
6262 instance->host = host;
6263 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
6264 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
6265 instance->ctrl_info = NULL;
6268 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
6269 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
6270 instance->flag_ieee = 1;
6272 megasas_dbg_lvl = 0;
6274 instance->unload = 1;
6275 instance->last_time = 0;
6276 instance->disableOnlineCtrlReset = 1;
6277 instance->UnevenSpanSupport = 0;
6279 if (instance->adapter_type != MFI_SERIES) {
6280 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
6281 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
6283 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
6286 * Initialize MFI Firmware
6288 if (megasas_init_fw(instance))
6291 if (instance->requestorId) {
6292 if (instance->PlasmaFW111) {
6293 instance->vf_affiliation_111 =
6294 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
6295 &instance->vf_affiliation_111_h);
6296 if (!instance->vf_affiliation_111)
6297 dev_warn(&pdev->dev, "Can't allocate "
6298 "memory for VF affiliation buffer\n");
6300 instance->vf_affiliation =
6301 pci_alloc_consistent(pdev,
6302 (MAX_LOGICAL_DRIVES + 1) *
6303 sizeof(struct MR_LD_VF_AFFILIATION),
6304 &instance->vf_affiliation_h);
6305 if (!instance->vf_affiliation)
6306 dev_warn(&pdev->dev, "Can't allocate "
6307 "memory for VF affiliation buffer\n");
6312 * Store instance in PCI softstate
6314 pci_set_drvdata(pdev, instance);
6317 * Add this controller to megasas_mgmt_info structure so that it
6318 * can be exported to management applications
6320 megasas_mgmt_info.count++;
6321 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
6322 megasas_mgmt_info.max_index++;
6325 * Register with SCSI mid-layer
6327 if (megasas_io_attach(instance))
6328 goto fail_io_attach;
6330 instance->unload = 0;
6332 * Trigger SCSI to scan our drives
6334 scsi_scan_host(host);
6337 * Initiate AEN (Asynchronous Event Notification)
6339 if (megasas_start_aen(instance)) {
6340 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
6341 goto fail_start_aen;
6344 /* Get current SR-IOV LD/VF affiliation */
6345 if (instance->requestorId)
6346 megasas_get_ld_vf_affiliation(instance, 1);
6352 megasas_mgmt_info.count--;
6353 megasas_mgmt_info.max_index--;
6354 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
6356 instance->instancet->disable_intr(instance);
6357 megasas_destroy_irqs(instance);
6359 if (instance->adapter_type != MFI_SERIES)
6360 megasas_release_fusion(instance);
6362 megasas_release_mfi(instance);
6363 if (instance->msix_vectors)
6364 pci_free_irq_vectors(instance->pdev);
6367 if (instance->evt_detail)
6368 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6369 instance->evt_detail,
6370 instance->evt_detail_h);
6372 if (instance->pd_info)
6373 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6375 instance->pd_info_h);
6376 if (instance->tgt_prop)
6377 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6379 instance->tgt_prop_h);
6380 megasas_free_ctrl_mem(instance);
6381 scsi_host_put(host);
6382 fail_alloc_instance:
6384 pci_disable_device(pdev);
6390 * megasas_flush_cache - Requests FW to flush all its caches
6391 * @instance: Adapter soft state
6393 static void megasas_flush_cache(struct megasas_instance *instance)
6395 struct megasas_cmd *cmd;
6396 struct megasas_dcmd_frame *dcmd;
6398 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6401 cmd = megasas_get_cmd(instance);
6406 dcmd = &cmd->frame->dcmd;
6408 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6410 dcmd->cmd = MFI_CMD_DCMD;
6411 dcmd->cmd_status = 0x0;
6412 dcmd->sge_count = 0;
6413 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
6416 dcmd->data_xfer_len = 0;
6417 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
6418 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6420 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6422 dev_err(&instance->pdev->dev,
6423 "return from %s %d\n", __func__, __LINE__);
6427 megasas_return_cmd(instance, cmd);
6431 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6432 * @instance: Adapter soft state
6433 * @opcode: Shutdown/Hibernate
6435 static void megasas_shutdown_controller(struct megasas_instance *instance,
6438 struct megasas_cmd *cmd;
6439 struct megasas_dcmd_frame *dcmd;
6441 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6444 cmd = megasas_get_cmd(instance);
6449 if (instance->aen_cmd)
6450 megasas_issue_blocked_abort_cmd(instance,
6451 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
6452 if (instance->map_update_cmd)
6453 megasas_issue_blocked_abort_cmd(instance,
6454 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
6455 if (instance->jbod_seq_cmd)
6456 megasas_issue_blocked_abort_cmd(instance,
6457 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
6459 dcmd = &cmd->frame->dcmd;
6461 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6463 dcmd->cmd = MFI_CMD_DCMD;
6464 dcmd->cmd_status = 0x0;
6465 dcmd->sge_count = 0;
6466 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
6469 dcmd->data_xfer_len = 0;
6470 dcmd->opcode = cpu_to_le32(opcode);
6472 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6474 dev_err(&instance->pdev->dev,
6475 "return from %s %d\n", __func__, __LINE__);
6479 megasas_return_cmd(instance, cmd);
6484 * megasas_suspend - driver suspend entry point
6485 * @pdev: PCI device structure
6486 * @state: PCI power state to suspend routine
6489 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6491 struct Scsi_Host *host;
6492 struct megasas_instance *instance;
6494 instance = pci_get_drvdata(pdev);
6495 host = instance->host;
6496 instance->unload = 1;
6498 /* Shutdown SR-IOV heartbeat timer */
6499 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6500 del_timer_sync(&instance->sriov_heartbeat_timer);
6502 megasas_flush_cache(instance);
6503 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
6505 /* cancel the delayed work if this work still in queue */
6506 if (instance->ev != NULL) {
6507 struct megasas_aen_event *ev = instance->ev;
6508 cancel_delayed_work_sync(&ev->hotplug_work);
6509 instance->ev = NULL;
6512 tasklet_kill(&instance->isr_tasklet);
6514 pci_set_drvdata(instance->pdev, instance);
6515 instance->instancet->disable_intr(instance);
6517 megasas_destroy_irqs(instance);
6519 if (instance->msix_vectors)
6520 pci_free_irq_vectors(instance->pdev);
6522 pci_save_state(pdev);
6523 pci_disable_device(pdev);
6525 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6531 * megasas_resume- driver resume entry point
6532 * @pdev: PCI device structure
6535 megasas_resume(struct pci_dev *pdev)
6538 struct Scsi_Host *host;
6539 struct megasas_instance *instance;
6540 int irq_flags = PCI_IRQ_LEGACY;
6542 instance = pci_get_drvdata(pdev);
6543 host = instance->host;
6544 pci_set_power_state(pdev, PCI_D0);
6545 pci_enable_wake(pdev, PCI_D0, 0);
6546 pci_restore_state(pdev);
6549 * PCI prepping: enable device set bus mastering and dma mask
6551 rval = pci_enable_device_mem(pdev);
6554 dev_err(&pdev->dev, "Enable device failed\n");
6558 pci_set_master(pdev);
6560 if (megasas_set_dma_mask(pdev))
6561 goto fail_set_dma_mask;
6564 * Initialize MFI Firmware
6567 atomic_set(&instance->fw_outstanding, 0);
6570 * We expect the FW state to be READY
6572 if (megasas_transition_to_ready(instance, 0))
6573 goto fail_ready_state;
6575 /* Now re-enable MSI-X */
6576 if (instance->msix_vectors) {
6577 irq_flags = PCI_IRQ_MSIX;
6578 if (smp_affinity_enable)
6579 irq_flags |= PCI_IRQ_AFFINITY;
6581 rval = pci_alloc_irq_vectors(instance->pdev, 1,
6582 instance->msix_vectors ?
6583 instance->msix_vectors : 1, irq_flags);
6585 goto fail_reenable_msix;
6587 megasas_setup_reply_map(instance);
6589 if (instance->adapter_type != MFI_SERIES) {
6590 megasas_reset_reply_desc(instance);
6591 if (megasas_ioc_init_fusion(instance)) {
6592 megasas_free_cmds(instance);
6593 megasas_free_cmds_fusion(instance);
6596 if (!megasas_get_map_info(instance))
6597 megasas_sync_map_info(instance);
6599 *instance->producer = 0;
6600 *instance->consumer = 0;
6601 if (megasas_issue_init_mfi(instance))
6605 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
6608 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6609 (unsigned long)instance);
6611 if (instance->msix_vectors ?
6612 megasas_setup_irqs_msix(instance, 0) :
6613 megasas_setup_irqs_ioapic(instance))
6616 /* Re-launch SR-IOV heartbeat timer */
6617 if (instance->requestorId) {
6618 if (!megasas_sriov_start_heartbeat(instance, 0))
6619 megasas_start_timer(instance,
6620 &instance->sriov_heartbeat_timer,
6621 megasas_sriov_heartbeat_handler,
6622 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
6624 instance->skip_heartbeat_timer_del = 1;
6629 instance->instancet->enable_intr(instance);
6630 megasas_setup_jbod_map(instance);
6631 instance->unload = 0;
6634 * Initiate AEN (Asynchronous Event Notification)
6636 if (megasas_start_aen(instance))
6637 dev_err(&instance->pdev->dev, "Start AEN failed\n");
6642 if (instance->evt_detail)
6643 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6644 instance->evt_detail,
6645 instance->evt_detail_h);
6647 if (instance->pd_info)
6648 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6650 instance->pd_info_h);
6651 if (instance->tgt_prop)
6652 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6654 instance->tgt_prop_h);
6656 megasas_free_ctrl_mem(instance);
6657 scsi_host_put(host);
6663 pci_disable_device(pdev);
6668 #define megasas_suspend NULL
6669 #define megasas_resume NULL
6673 megasas_wait_for_adapter_operational(struct megasas_instance *instance)
6675 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
6678 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6681 for (i = 0; i < wait_time; i++) {
6682 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
6685 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
6686 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
6691 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6692 dev_info(&instance->pdev->dev, "%s timed out while waiting for HBA to recover.\n",
6701 * megasas_detach_one - PCI hot"un"plug entry point
6702 * @pdev: PCI device structure
6704 static void megasas_detach_one(struct pci_dev *pdev)
6707 struct Scsi_Host *host;
6708 struct megasas_instance *instance;
6709 struct fusion_context *fusion;
6712 instance = pci_get_drvdata(pdev);
6713 host = instance->host;
6714 fusion = instance->ctrl_context;
6716 /* Shutdown SR-IOV heartbeat timer */
6717 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6718 del_timer_sync(&instance->sriov_heartbeat_timer);
6720 if (instance->fw_crash_state != UNAVAILABLE)
6721 megasas_free_host_crash_buffer(instance);
6722 scsi_remove_host(instance->host);
6723 instance->unload = 1;
6725 if (megasas_wait_for_adapter_operational(instance))
6726 goto skip_firing_dcmds;
6728 megasas_flush_cache(instance);
6729 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
6732 /* cancel the delayed work if this work still in queue*/
6733 if (instance->ev != NULL) {
6734 struct megasas_aen_event *ev = instance->ev;
6735 cancel_delayed_work_sync(&ev->hotplug_work);
6736 instance->ev = NULL;
6739 /* cancel all wait events */
6740 wake_up_all(&instance->int_cmd_wait_q);
6742 tasklet_kill(&instance->isr_tasklet);
6745 * Take the instance off the instance array. Note that we will not
6746 * decrement the max_index. We let this array be sparse array
6748 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6749 if (megasas_mgmt_info.instance[i] == instance) {
6750 megasas_mgmt_info.count--;
6751 megasas_mgmt_info.instance[i] = NULL;
6757 instance->instancet->disable_intr(instance);
6759 megasas_destroy_irqs(instance);
6761 if (instance->msix_vectors)
6762 pci_free_irq_vectors(instance->pdev);
6764 if (instance->adapter_type == VENTURA_SERIES) {
6765 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
6766 kfree(fusion->stream_detect_by_ld[i]);
6767 kfree(fusion->stream_detect_by_ld);
6768 fusion->stream_detect_by_ld = NULL;
6772 if (instance->adapter_type != MFI_SERIES) {
6773 megasas_release_fusion(instance);
6774 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6775 (sizeof(struct MR_PD_CFG_SEQ) *
6776 (MAX_PHYSICAL_DEVICES - 1));
6777 for (i = 0; i < 2 ; i++) {
6778 if (fusion->ld_map[i])
6779 dma_free_coherent(&instance->pdev->dev,
6782 fusion->ld_map_phys[i]);
6783 if (fusion->ld_drv_map[i]) {
6784 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
6785 vfree(fusion->ld_drv_map[i]);
6787 free_pages((ulong)fusion->ld_drv_map[i],
6788 fusion->drv_map_pages);
6791 if (fusion->pd_seq_sync[i])
6792 dma_free_coherent(&instance->pdev->dev,
6794 fusion->pd_seq_sync[i],
6795 fusion->pd_seq_phys[i]);
6798 megasas_release_mfi(instance);
6801 kfree(instance->ctrl_info);
6803 if (instance->evt_detail)
6804 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6805 instance->evt_detail, instance->evt_detail_h);
6806 if (instance->pd_info)
6807 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6809 instance->pd_info_h);
6810 if (instance->tgt_prop)
6811 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6813 instance->tgt_prop_h);
6814 if (instance->vf_affiliation)
6815 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6816 sizeof(struct MR_LD_VF_AFFILIATION),
6817 instance->vf_affiliation,
6818 instance->vf_affiliation_h);
6820 if (instance->vf_affiliation_111)
6821 pci_free_consistent(pdev,
6822 sizeof(struct MR_LD_VF_AFFILIATION_111),
6823 instance->vf_affiliation_111,
6824 instance->vf_affiliation_111_h);
6826 if (instance->hb_host_mem)
6827 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6828 instance->hb_host_mem,
6829 instance->hb_host_mem_h);
6831 if (instance->crash_dump_buf)
6832 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6833 instance->crash_dump_buf, instance->crash_dump_h);
6835 if (instance->system_info_buf)
6836 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6837 instance->system_info_buf, instance->system_info_h);
6839 megasas_free_ctrl_mem(instance);
6841 scsi_host_put(host);
6843 pci_disable_device(pdev);
6847 * megasas_shutdown - Shutdown entry point
6848 * @device: Generic device structure
6850 static void megasas_shutdown(struct pci_dev *pdev)
6852 struct megasas_instance *instance = pci_get_drvdata(pdev);
6854 instance->unload = 1;
6856 if (megasas_wait_for_adapter_operational(instance))
6857 goto skip_firing_dcmds;
6859 megasas_flush_cache(instance);
6860 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
6863 instance->instancet->disable_intr(instance);
6864 megasas_destroy_irqs(instance);
6866 if (instance->msix_vectors)
6867 pci_free_irq_vectors(instance->pdev);
6871 * megasas_mgmt_open - char node "open" entry point
6873 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6876 * Allow only those users with admin rights
6878 if (!capable(CAP_SYS_ADMIN))
6885 * megasas_mgmt_fasync - Async notifier registration from applications
6887 * This function adds the calling process to a driver global queue. When an
6888 * event occurs, SIGIO will be sent to all processes in this queue.
6890 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6894 mutex_lock(&megasas_async_queue_mutex);
6896 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6898 mutex_unlock(&megasas_async_queue_mutex);
6901 /* For sanity check when we get ioctl */
6902 filep->private_data = filep;
6906 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6912 * megasas_mgmt_poll - char node "poll" entry point
6914 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6917 unsigned long flags;
6919 poll_wait(file, &megasas_poll_wait, wait);
6920 spin_lock_irqsave(&poll_aen_lock, flags);
6921 if (megasas_poll_wait_aen)
6922 mask = (POLLIN | POLLRDNORM);
6925 megasas_poll_wait_aen = 0;
6926 spin_unlock_irqrestore(&poll_aen_lock, flags);
6931 * megasas_set_crash_dump_params_ioctl:
6932 * Send CRASH_DUMP_MODE DCMD to all controllers
6933 * @cmd: MFI command frame
6936 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
6938 struct megasas_instance *local_instance;
6942 crash_support = cmd->frame->dcmd.mbox.w[0];
6944 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6945 local_instance = megasas_mgmt_info.instance[i];
6946 if (local_instance && local_instance->crash_dump_drv_support) {
6947 if ((atomic_read(&local_instance->adprecovery) ==
6948 MEGASAS_HBA_OPERATIONAL) &&
6949 !megasas_set_crash_dump_params(local_instance,
6951 local_instance->crash_dump_app_support =
6953 dev_info(&local_instance->pdev->dev,
6954 "Application firmware crash "
6955 "dump mode set success\n");
6958 dev_info(&local_instance->pdev->dev,
6959 "Application firmware crash "
6960 "dump mode set failed\n");
6969 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6970 * @instance: Adapter soft state
6971 * @argp: User's ioctl packet
6974 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6975 struct megasas_iocpacket __user * user_ioc,
6976 struct megasas_iocpacket *ioc)
6978 struct megasas_sge32 *kern_sge32;
6979 struct megasas_cmd *cmd;
6980 void *kbuff_arr[MAX_IOCTL_SGE];
6981 dma_addr_t buf_handle = 0;
6984 dma_addr_t sense_handle;
6985 unsigned long *sense_ptr;
6988 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6990 if (ioc->sge_count > MAX_IOCTL_SGE) {
6991 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
6992 ioc->sge_count, MAX_IOCTL_SGE);
6996 cmd = megasas_get_cmd(instance);
6998 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
7003 * User's IOCTL packet has 2 frames (maximum). Copy those two
7004 * frames into our cmd's frames. cmd->frame's context will get
7005 * overwritten when we copy from user's frames. So set that value
7008 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
7009 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
7010 cmd->frame->hdr.pad_0 = 0;
7011 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
7013 MFI_FRAME_SENSE64));
7014 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
7016 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
7017 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
7018 megasas_return_cmd(instance, cmd);
7023 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
7024 error = megasas_set_crash_dump_params_ioctl(cmd);
7025 megasas_return_cmd(instance, cmd);
7030 * The management interface between applications and the fw uses
7031 * MFI frames. E.g, RAID configuration changes, LD property changes
7032 * etc are accomplishes through different kinds of MFI frames. The
7033 * driver needs to care only about substituting user buffers with
7034 * kernel buffers in SGLs. The location of SGL is embedded in the
7035 * struct iocpacket itself.
7037 kern_sge32 = (struct megasas_sge32 *)
7038 ((unsigned long)cmd->frame + ioc->sgl_off);
7041 * For each user buffer, create a mirror buffer and copy in
7043 for (i = 0; i < ioc->sge_count; i++) {
7044 if (!ioc->sgl[i].iov_len)
7047 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
7048 ioc->sgl[i].iov_len,
7049 &buf_handle, GFP_KERNEL);
7050 if (!kbuff_arr[i]) {
7051 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
7052 "kernel SGL buffer for IOCTL\n");
7058 * We don't change the dma_coherent_mask, so
7059 * pci_alloc_consistent only returns 32bit addresses
7061 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
7062 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7065 * We created a kernel buffer corresponding to the
7066 * user buffer. Now copy in from the user buffer
7068 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
7069 (u32) (ioc->sgl[i].iov_len))) {
7075 if (ioc->sense_len) {
7076 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
7077 &sense_handle, GFP_KERNEL);
7084 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
7085 *sense_ptr = cpu_to_le32(sense_handle);
7089 * Set the sync_cmd flag so that the ISR knows not to complete this
7090 * cmd to the SCSI mid-layer
7093 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
7095 dev_err(&instance->pdev->dev,
7096 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
7097 __func__, __LINE__, opcode, cmd->cmd_status_drv);
7103 if (instance->unload == 1) {
7104 dev_info(&instance->pdev->dev, "Driver unload is in progress "
7105 "don't submit data to application\n");
7109 * copy out the kernel buffers to user buffers
7111 for (i = 0; i < ioc->sge_count; i++) {
7112 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7113 ioc->sgl[i].iov_len)) {
7120 * copy out the sense
7122 if (ioc->sense_len) {
7124 * sense_ptr points to the location that has the user
7125 * sense buffer address
7127 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7130 if (copy_to_user((void __user *)((unsigned long)
7131 get_unaligned((unsigned long *)sense_ptr)),
7132 sense, ioc->sense_len)) {
7133 dev_err(&instance->pdev->dev, "Failed to copy out to user "
7141 * copy the status codes returned by the fw
7143 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7144 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
7145 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
7151 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
7152 sense, sense_handle);
7155 for (i = 0; i < ioc->sge_count; i++) {
7157 dma_free_coherent(&instance->pdev->dev,
7158 le32_to_cpu(kern_sge32[i].length),
7160 le32_to_cpu(kern_sge32[i].phys_addr));
7161 kbuff_arr[i] = NULL;
7165 megasas_return_cmd(instance, cmd);
7169 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7171 struct megasas_iocpacket __user *user_ioc =
7172 (struct megasas_iocpacket __user *)arg;
7173 struct megasas_iocpacket *ioc;
7174 struct megasas_instance *instance;
7177 unsigned long flags;
7178 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
7180 ioc = memdup_user(user_ioc, sizeof(*ioc));
7182 return PTR_ERR(ioc);
7184 instance = megasas_lookup_instance(ioc->host_no);
7190 /* Adjust ioctl wait time for VF mode */
7191 if (instance->requestorId)
7192 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7194 /* Block ioctls in VF mode */
7195 if (instance->requestorId && !allow_vf_ioctls) {
7200 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
7201 dev_err(&instance->pdev->dev, "Controller in crit error\n");
7206 if (instance->unload == 1) {
7211 if (down_interruptible(&instance->ioctl_sem)) {
7212 error = -ERESTARTSYS;
7216 for (i = 0; i < wait_time; i++) {
7218 spin_lock_irqsave(&instance->hba_lock, flags);
7219 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
7220 spin_unlock_irqrestore(&instance->hba_lock, flags);
7223 spin_unlock_irqrestore(&instance->hba_lock, flags);
7225 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
7226 dev_notice(&instance->pdev->dev, "waiting"
7227 "for controller reset to finish\n");
7233 spin_lock_irqsave(&instance->hba_lock, flags);
7234 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
7235 spin_unlock_irqrestore(&instance->hba_lock, flags);
7237 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
7241 spin_unlock_irqrestore(&instance->hba_lock, flags);
7243 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
7245 up(&instance->ioctl_sem);
7252 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7254 struct megasas_instance *instance;
7255 struct megasas_aen aen;
7258 unsigned long flags;
7259 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
7261 if (file->private_data != file) {
7262 printk(KERN_DEBUG "megasas: fasync_helper was not "
7267 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7270 instance = megasas_lookup_instance(aen.host_no);
7275 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
7279 if (instance->unload == 1) {
7283 for (i = 0; i < wait_time; i++) {
7285 spin_lock_irqsave(&instance->hba_lock, flags);
7286 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
7287 spin_unlock_irqrestore(&instance->hba_lock,
7292 spin_unlock_irqrestore(&instance->hba_lock, flags);
7294 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
7295 dev_notice(&instance->pdev->dev, "waiting for"
7296 "controller reset to finish\n");
7302 spin_lock_irqsave(&instance->hba_lock, flags);
7303 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
7304 spin_unlock_irqrestore(&instance->hba_lock, flags);
7305 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
7308 spin_unlock_irqrestore(&instance->hba_lock, flags);
7310 mutex_lock(&instance->reset_mutex);
7311 error = megasas_register_aen(instance, aen.seq_num,
7312 aen.class_locale_word);
7313 mutex_unlock(&instance->reset_mutex);
7318 * megasas_mgmt_ioctl - char node ioctl entry point
7321 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7324 case MEGASAS_IOC_FIRMWARE:
7325 return megasas_mgmt_ioctl_fw(file, arg);
7327 case MEGASAS_IOC_GET_AEN:
7328 return megasas_mgmt_ioctl_aen(file, arg);
7334 #ifdef CONFIG_COMPAT
7335 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
7337 struct compat_megasas_iocpacket __user *cioc =
7338 (struct compat_megasas_iocpacket __user *)arg;
7339 struct megasas_iocpacket __user *ioc =
7340 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
7344 u32 local_sense_off;
7345 u32 local_sense_len;
7348 if (clear_user(ioc, sizeof(*ioc)))
7351 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
7352 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
7353 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
7354 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
7355 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
7356 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7360 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7361 * sense_len is not null, so prepare the 64bit value under
7362 * the same condition.
7364 if (get_user(local_sense_off, &ioc->sense_off) ||
7365 get_user(local_sense_len, &ioc->sense_len) ||
7366 get_user(user_sense_off, &cioc->sense_off))
7369 if (local_sense_off != user_sense_off)
7372 if (local_sense_len) {
7373 void __user **sense_ioc_ptr =
7374 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
7375 compat_uptr_t *sense_cioc_ptr =
7376 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
7377 if (get_user(ptr, sense_cioc_ptr) ||
7378 put_user(compat_ptr(ptr), sense_ioc_ptr))
7382 for (i = 0; i < MAX_IOCTL_SGE; i++) {
7383 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
7384 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
7385 copy_in_user(&ioc->sgl[i].iov_len,
7386 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
7390 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
7392 if (copy_in_user(&cioc->frame.hdr.cmd_status,
7393 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
7394 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
7401 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
7405 case MEGASAS_IOC_FIRMWARE32:
7406 return megasas_mgmt_compat_ioctl_fw(file, arg);
7407 case MEGASAS_IOC_GET_AEN:
7408 return megasas_mgmt_ioctl_aen(file, arg);
7416 * File operations structure for management interface
7418 static const struct file_operations megasas_mgmt_fops = {
7419 .owner = THIS_MODULE,
7420 .open = megasas_mgmt_open,
7421 .fasync = megasas_mgmt_fasync,
7422 .unlocked_ioctl = megasas_mgmt_ioctl,
7423 .poll = megasas_mgmt_poll,
7424 #ifdef CONFIG_COMPAT
7425 .compat_ioctl = megasas_mgmt_compat_ioctl,
7427 .llseek = noop_llseek,
7431 * PCI hotplug support registration structure
7433 static struct pci_driver megasas_pci_driver = {
7435 .name = "megaraid_sas",
7436 .id_table = megasas_pci_table,
7437 .probe = megasas_probe_one,
7438 .remove = megasas_detach_one,
7439 .suspend = megasas_suspend,
7440 .resume = megasas_resume,
7441 .shutdown = megasas_shutdown,
7445 * Sysfs driver attributes
7447 static ssize_t version_show(struct device_driver *dd, char *buf)
7449 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
7452 static DRIVER_ATTR_RO(version);
7454 static ssize_t release_date_show(struct device_driver *dd, char *buf)
7456 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
7459 static DRIVER_ATTR_RO(release_date);
7461 static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
7463 return sprintf(buf, "%u\n", support_poll_for_event);
7465 static DRIVER_ATTR_RO(support_poll_for_event);
7467 static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
7469 return sprintf(buf, "%u\n", support_device_change);
7471 static DRIVER_ATTR_RO(support_device_change);
7473 static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
7475 return sprintf(buf, "%u\n", megasas_dbg_lvl);
7478 static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
7483 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
7484 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7489 static DRIVER_ATTR_RW(dbg_lvl);
7491 static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
7493 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
7494 scsi_remove_device(sdev);
7495 scsi_device_put(sdev);
7499 megasas_aen_polling(struct work_struct *work)
7501 struct megasas_aen_event *ev =
7502 container_of(work, struct megasas_aen_event, hotplug_work.work);
7503 struct megasas_instance *instance = ev->instance;
7504 union megasas_evt_class_locale class_locale;
7505 struct Scsi_Host *host;
7506 struct scsi_device *sdev1;
7509 int i, j, doscan = 0;
7510 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
7512 u8 dcmd_ret = DCMD_SUCCESS;
7515 printk(KERN_ERR "invalid instance!\n");
7520 /* Adjust event workqueue thread wait time for VF mode */
7521 if (instance->requestorId)
7522 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7524 /* Don't run the event workqueue thread if OCR is running */
7525 mutex_lock(&instance->reset_mutex);
7527 instance->ev = NULL;
7528 host = instance->host;
7529 if (instance->evt_detail) {
7530 megasas_decode_evt(instance);
7532 switch (le32_to_cpu(instance->evt_detail->code)) {
7534 case MR_EVT_PD_INSERTED:
7535 case MR_EVT_PD_REMOVED:
7536 dcmd_ret = megasas_get_pd_list(instance);
7537 if (dcmd_ret == DCMD_SUCCESS)
7538 doscan = SCAN_PD_CHANNEL;
7541 case MR_EVT_LD_OFFLINE:
7542 case MR_EVT_CFG_CLEARED:
7543 case MR_EVT_LD_DELETED:
7544 case MR_EVT_LD_CREATED:
7545 if (!instance->requestorId ||
7546 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7547 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7549 if (dcmd_ret == DCMD_SUCCESS)
7550 doscan = SCAN_VD_CHANNEL;
7554 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7555 case MR_EVT_FOREIGN_CFG_IMPORTED:
7556 case MR_EVT_LD_STATE_CHANGE:
7557 dcmd_ret = megasas_get_pd_list(instance);
7559 if (dcmd_ret != DCMD_SUCCESS)
7562 if (!instance->requestorId ||
7563 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7564 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7566 if (dcmd_ret != DCMD_SUCCESS)
7569 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7570 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7571 instance->host->host_no);
7574 case MR_EVT_CTRL_PROP_CHANGED:
7575 dcmd_ret = megasas_get_ctrl_info(instance);
7582 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7583 mutex_unlock(&instance->reset_mutex);
7588 mutex_unlock(&instance->reset_mutex);
7590 if (doscan & SCAN_PD_CHANNEL) {
7591 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7592 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7593 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
7594 sdev1 = scsi_device_lookup(host, i, j, 0);
7595 if (instance->pd_list[pd_index].driveState ==
7596 MR_PD_STATE_SYSTEM) {
7598 scsi_add_device(host, i, j, 0);
7600 scsi_device_put(sdev1);
7603 megasas_remove_scsi_device(sdev1);
7609 if (doscan & SCAN_VD_CHANNEL) {
7610 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7611 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7612 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7613 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7614 if (instance->ld_ids[ld_index] != 0xff) {
7616 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7618 scsi_device_put(sdev1);
7621 megasas_remove_scsi_device(sdev1);
7627 if (dcmd_ret == DCMD_SUCCESS)
7628 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7630 seq_num = instance->last_seq_num;
7632 /* Register AEN with FW for latest sequence number plus 1 */
7633 class_locale.members.reserved = 0;
7634 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7635 class_locale.members.class = MR_EVT_CLASS_DEBUG;
7637 if (instance->aen_cmd != NULL) {
7642 mutex_lock(&instance->reset_mutex);
7643 error = megasas_register_aen(instance, seq_num,
7646 dev_err(&instance->pdev->dev,
7647 "register aen failed error %x\n", error);
7649 mutex_unlock(&instance->reset_mutex);
7654 * megasas_init - Driver load entry point
7656 static int __init megasas_init(void)
7661 * Booted in kdump kernel, minimize memory footprints by
7662 * disabling few features
7664 if (reset_devices) {
7667 dual_qdepth_disable = 1;
7671 * Announce driver version and other information
7673 pr_info("megasas: %s\n", MEGASAS_VERSION);
7675 spin_lock_init(&poll_aen_lock);
7677 support_poll_for_event = 2;
7678 support_device_change = 1;
7680 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7683 * Register character device node
7685 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7688 printk(KERN_DEBUG "megasas: failed to open device node\n");
7692 megasas_mgmt_majorno = rval;
7695 * Register ourselves as PCI hotplug module
7697 rval = pci_register_driver(&megasas_pci_driver);
7700 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
7704 rval = driver_create_file(&megasas_pci_driver.driver,
7705 &driver_attr_version);
7707 goto err_dcf_attr_ver;
7709 rval = driver_create_file(&megasas_pci_driver.driver,
7710 &driver_attr_release_date);
7712 goto err_dcf_rel_date;
7714 rval = driver_create_file(&megasas_pci_driver.driver,
7715 &driver_attr_support_poll_for_event);
7717 goto err_dcf_support_poll_for_event;
7719 rval = driver_create_file(&megasas_pci_driver.driver,
7720 &driver_attr_dbg_lvl);
7722 goto err_dcf_dbg_lvl;
7723 rval = driver_create_file(&megasas_pci_driver.driver,
7724 &driver_attr_support_device_change);
7726 goto err_dcf_support_device_change;
7730 err_dcf_support_device_change:
7731 driver_remove_file(&megasas_pci_driver.driver,
7732 &driver_attr_dbg_lvl);
7734 driver_remove_file(&megasas_pci_driver.driver,
7735 &driver_attr_support_poll_for_event);
7736 err_dcf_support_poll_for_event:
7737 driver_remove_file(&megasas_pci_driver.driver,
7738 &driver_attr_release_date);
7740 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7742 pci_unregister_driver(&megasas_pci_driver);
7744 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7749 * megasas_exit - Driver unload entry point
7751 static void __exit megasas_exit(void)
7753 driver_remove_file(&megasas_pci_driver.driver,
7754 &driver_attr_dbg_lvl);
7755 driver_remove_file(&megasas_pci_driver.driver,
7756 &driver_attr_support_poll_for_event);
7757 driver_remove_file(&megasas_pci_driver.driver,
7758 &driver_attr_support_device_change);
7759 driver_remove_file(&megasas_pci_driver.driver,
7760 &driver_attr_release_date);
7761 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7763 pci_unregister_driver(&megasas_pci_driver);
7764 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7767 module_init(megasas_init);
7768 module_exit(megasas_exit);