2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2003-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Authors: Avago Technologies
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
28 * Send feedback to: megaraidlinux.pdl@avagotech.com
30 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/list.h>
38 #include <linux/moduleparam.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/uio.h>
44 #include <linux/slab.h>
45 #include <asm/uaccess.h>
47 #include <linux/compat.h>
48 #include <linux/blkdev.h>
49 #include <linux/mutex.h>
50 #include <linux/poll.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_device.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_tcq.h>
57 #include "megaraid_sas_fusion.h"
58 #include "megaraid_sas.h"
61 * Number of sectors per IO command
62 * Will be set in megasas_init_mfi if user does not provide
64 static unsigned int max_sectors;
65 module_param_named(max_sectors, max_sectors, int, 0);
66 MODULE_PARM_DESC(max_sectors,
67 "Maximum number of sectors per IO command");
69 static int msix_disable;
70 module_param(msix_disable, int, S_IRUGO);
71 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73 static unsigned int msix_vectors;
74 module_param(msix_vectors, int, S_IRUGO);
75 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77 static int allow_vf_ioctls;
78 module_param(allow_vf_ioctls, int, S_IRUGO);
79 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
81 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
82 module_param(throttlequeuedepth, int, S_IRUGO);
83 MODULE_PARM_DESC(throttlequeuedepth,
84 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
86 unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
87 module_param(resetwaittime, int, S_IRUGO);
88 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
89 "before resetting adapter. Default: 180");
91 int smp_affinity_enable = 1;
92 module_param(smp_affinity_enable, int, S_IRUGO);
93 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
96 module_param(rdpq_enable, int, S_IRUGO);
97 MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
99 unsigned int dual_qdepth_disable;
100 module_param(dual_qdepth_disable, int, S_IRUGO);
101 MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
103 unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
104 module_param(scmd_timeout, int, S_IRUGO);
105 MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
107 MODULE_LICENSE("GPL");
108 MODULE_VERSION(MEGASAS_VERSION);
109 MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
110 MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
112 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
113 static int megasas_get_pd_list(struct megasas_instance *instance);
114 static int megasas_ld_list_query(struct megasas_instance *instance,
116 static int megasas_issue_init_mfi(struct megasas_instance *instance);
117 static int megasas_register_aen(struct megasas_instance *instance,
118 u32 seq_num, u32 class_locale_word);
120 megasas_get_pd_info(struct megasas_instance *instance, u16 device_id);
122 * PCI ID table for all supported controllers
124 static struct pci_device_id megasas_pci_table[] = {
126 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
128 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
141 /* xscale IOP, vega */
142 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
144 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
155 /* Intruder 24 port*/
156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
157 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
161 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
163 static int megasas_mgmt_majorno;
164 struct megasas_mgmt_info megasas_mgmt_info;
165 static struct fasync_struct *megasas_async_queue;
166 static DEFINE_MUTEX(megasas_async_queue_mutex);
168 static int megasas_poll_wait_aen;
169 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
170 static u32 support_poll_for_event;
172 static u32 support_device_change;
174 /* define lock for aen poll */
175 spinlock_t poll_aen_lock;
178 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
181 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
183 megasas_adp_reset_gen2(struct megasas_instance *instance,
184 struct megasas_register_set __iomem *reg_set);
185 static irqreturn_t megasas_isr(int irq, void *devp);
187 megasas_init_adapter_mfi(struct megasas_instance *instance);
189 megasas_build_and_issue_cmd(struct megasas_instance *instance,
190 struct scsi_cmnd *scmd);
191 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
193 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
195 void megasas_fusion_ocr_wq(struct work_struct *work);
196 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
200 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
202 instance->instancet->fire_cmd(instance,
203 cmd->frame_phys_addr, 0, instance->reg_set);
208 * megasas_get_cmd - Get a command from the free pool
209 * @instance: Adapter soft state
211 * Returns a free command from the pool
213 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
217 struct megasas_cmd *cmd = NULL;
219 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
221 if (!list_empty(&instance->cmd_pool)) {
222 cmd = list_entry((&instance->cmd_pool)->next,
223 struct megasas_cmd, list);
224 list_del_init(&cmd->list);
226 dev_err(&instance->pdev->dev, "Command pool empty!\n");
229 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
234 * megasas_return_cmd - Return a cmd to free command pool
235 * @instance: Adapter soft state
236 * @cmd: Command packet to be returned to free command pool
239 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
243 struct megasas_cmd_fusion *cmd_fusion;
244 struct fusion_context *fusion = instance->ctrl_context;
246 /* This flag is used only for fusion adapter.
247 * Wait for Interrupt for Polled mode DCMD
249 if (cmd->flags & DRV_DCMD_POLLED_MODE)
252 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
255 blk_tags = instance->max_scsi_cmds + cmd->index;
256 cmd_fusion = fusion->cmd_list[blk_tags];
257 megasas_return_cmd_fusion(instance, cmd_fusion);
260 cmd->frame_count = 0;
262 if (!fusion && reset_devices)
263 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
264 list_add(&cmd->list, (&instance->cmd_pool)->next);
266 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
271 format_timestamp(uint32_t timestamp)
273 static char buffer[32];
275 if ((timestamp & 0xff000000) == 0xff000000)
276 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
279 snprintf(buffer, sizeof(buffer), "%us", timestamp);
284 format_class(int8_t class)
286 static char buffer[6];
289 case MFI_EVT_CLASS_DEBUG:
291 case MFI_EVT_CLASS_PROGRESS:
293 case MFI_EVT_CLASS_INFO:
295 case MFI_EVT_CLASS_WARNING:
297 case MFI_EVT_CLASS_CRITICAL:
299 case MFI_EVT_CLASS_FATAL:
301 case MFI_EVT_CLASS_DEAD:
304 snprintf(buffer, sizeof(buffer), "%d", class);
310 * megasas_decode_evt: Decode FW AEN event and print critical event
312 * @instance: Adapter soft state
315 megasas_decode_evt(struct megasas_instance *instance)
317 struct megasas_evt_detail *evt_detail = instance->evt_detail;
318 union megasas_evt_class_locale class_locale;
319 class_locale.word = le32_to_cpu(evt_detail->cl.word);
321 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
322 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
323 le32_to_cpu(evt_detail->seq_num),
324 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
325 (class_locale.members.locale),
326 format_class(class_locale.members.class),
327 evt_detail->description);
331 * The following functions are defined for xscale
332 * (deviceid : 1064R, PERC5) controllers
336 * megasas_enable_intr_xscale - Enables interrupts
337 * @regs: MFI register set
340 megasas_enable_intr_xscale(struct megasas_instance *instance)
342 struct megasas_register_set __iomem *regs;
344 regs = instance->reg_set;
345 writel(0, &(regs)->outbound_intr_mask);
347 /* Dummy readl to force pci flush */
348 readl(®s->outbound_intr_mask);
352 * megasas_disable_intr_xscale -Disables interrupt
353 * @regs: MFI register set
356 megasas_disable_intr_xscale(struct megasas_instance *instance)
358 struct megasas_register_set __iomem *regs;
361 regs = instance->reg_set;
362 writel(mask, ®s->outbound_intr_mask);
363 /* Dummy readl to force pci flush */
364 readl(®s->outbound_intr_mask);
368 * megasas_read_fw_status_reg_xscale - returns the current FW status value
369 * @regs: MFI register set
372 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
374 return readl(&(regs)->outbound_msg_0);
377 * megasas_clear_interrupt_xscale - Check & clear interrupt
378 * @regs: MFI register set
381 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
387 * Check if it is our interrupt
389 status = readl(®s->outbound_intr_status);
391 if (status & MFI_OB_INTR_STATUS_MASK)
392 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
393 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
394 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
397 * Clear the interrupt by writing back the same value
400 writel(status, ®s->outbound_intr_status);
402 /* Dummy readl to force pci flush */
403 readl(®s->outbound_intr_status);
409 * megasas_fire_cmd_xscale - Sends command to the FW
410 * @frame_phys_addr : Physical address of cmd
411 * @frame_count : Number of frames for the command
412 * @regs : MFI register set
415 megasas_fire_cmd_xscale(struct megasas_instance *instance,
416 dma_addr_t frame_phys_addr,
418 struct megasas_register_set __iomem *regs)
422 spin_lock_irqsave(&instance->hba_lock, flags);
423 writel((frame_phys_addr >> 3)|(frame_count),
424 &(regs)->inbound_queue_port);
425 spin_unlock_irqrestore(&instance->hba_lock, flags);
429 * megasas_adp_reset_xscale - For controller reset
430 * @regs: MFI register set
433 megasas_adp_reset_xscale(struct megasas_instance *instance,
434 struct megasas_register_set __iomem *regs)
439 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
441 for (i = 0; i < 3; i++)
442 msleep(1000); /* sleep for 3 secs */
444 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
445 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
447 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
449 pci_write_config_dword(instance->pdev,
450 MFI_1068_PCSR_OFFSET, pcidata);
452 for (i = 0; i < 2; i++)
453 msleep(1000); /* need to wait 2 secs again */
456 pci_read_config_dword(instance->pdev,
457 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
458 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
459 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
460 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
462 pci_write_config_dword(instance->pdev,
463 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
470 * megasas_check_reset_xscale - For controller reset check
471 * @regs: MFI register set
474 megasas_check_reset_xscale(struct megasas_instance *instance,
475 struct megasas_register_set __iomem *regs)
477 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
478 (le32_to_cpu(*instance->consumer) ==
479 MEGASAS_ADPRESET_INPROG_SIGN))
484 static struct megasas_instance_template megasas_instance_template_xscale = {
486 .fire_cmd = megasas_fire_cmd_xscale,
487 .enable_intr = megasas_enable_intr_xscale,
488 .disable_intr = megasas_disable_intr_xscale,
489 .clear_intr = megasas_clear_intr_xscale,
490 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
491 .adp_reset = megasas_adp_reset_xscale,
492 .check_reset = megasas_check_reset_xscale,
493 .service_isr = megasas_isr,
494 .tasklet = megasas_complete_cmd_dpc,
495 .init_adapter = megasas_init_adapter_mfi,
496 .build_and_issue_cmd = megasas_build_and_issue_cmd,
497 .issue_dcmd = megasas_issue_dcmd,
501 * This is the end of set of functions & definitions specific
502 * to xscale (deviceid : 1064R, PERC5) controllers
506 * The following functions are defined for ppc (deviceid : 0x60)
511 * megasas_enable_intr_ppc - Enables interrupts
512 * @regs: MFI register set
515 megasas_enable_intr_ppc(struct megasas_instance *instance)
517 struct megasas_register_set __iomem *regs;
519 regs = instance->reg_set;
520 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
522 writel(~0x80000000, &(regs)->outbound_intr_mask);
524 /* Dummy readl to force pci flush */
525 readl(®s->outbound_intr_mask);
529 * megasas_disable_intr_ppc - Disable interrupt
530 * @regs: MFI register set
533 megasas_disable_intr_ppc(struct megasas_instance *instance)
535 struct megasas_register_set __iomem *regs;
536 u32 mask = 0xFFFFFFFF;
538 regs = instance->reg_set;
539 writel(mask, ®s->outbound_intr_mask);
540 /* Dummy readl to force pci flush */
541 readl(®s->outbound_intr_mask);
545 * megasas_read_fw_status_reg_ppc - returns the current FW status value
546 * @regs: MFI register set
549 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
551 return readl(&(regs)->outbound_scratch_pad);
555 * megasas_clear_interrupt_ppc - Check & clear interrupt
556 * @regs: MFI register set
559 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
561 u32 status, mfiStatus = 0;
564 * Check if it is our interrupt
566 status = readl(®s->outbound_intr_status);
568 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
569 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
571 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
572 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
575 * Clear the interrupt by writing back the same value
577 writel(status, ®s->outbound_doorbell_clear);
579 /* Dummy readl to force pci flush */
580 readl(®s->outbound_doorbell_clear);
586 * megasas_fire_cmd_ppc - Sends command to the FW
587 * @frame_phys_addr : Physical address of cmd
588 * @frame_count : Number of frames for the command
589 * @regs : MFI register set
592 megasas_fire_cmd_ppc(struct megasas_instance *instance,
593 dma_addr_t frame_phys_addr,
595 struct megasas_register_set __iomem *regs)
599 spin_lock_irqsave(&instance->hba_lock, flags);
600 writel((frame_phys_addr | (frame_count<<1))|1,
601 &(regs)->inbound_queue_port);
602 spin_unlock_irqrestore(&instance->hba_lock, flags);
606 * megasas_check_reset_ppc - For controller reset check
607 * @regs: MFI register set
610 megasas_check_reset_ppc(struct megasas_instance *instance,
611 struct megasas_register_set __iomem *regs)
613 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
619 static struct megasas_instance_template megasas_instance_template_ppc = {
621 .fire_cmd = megasas_fire_cmd_ppc,
622 .enable_intr = megasas_enable_intr_ppc,
623 .disable_intr = megasas_disable_intr_ppc,
624 .clear_intr = megasas_clear_intr_ppc,
625 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
626 .adp_reset = megasas_adp_reset_xscale,
627 .check_reset = megasas_check_reset_ppc,
628 .service_isr = megasas_isr,
629 .tasklet = megasas_complete_cmd_dpc,
630 .init_adapter = megasas_init_adapter_mfi,
631 .build_and_issue_cmd = megasas_build_and_issue_cmd,
632 .issue_dcmd = megasas_issue_dcmd,
636 * megasas_enable_intr_skinny - Enables interrupts
637 * @regs: MFI register set
640 megasas_enable_intr_skinny(struct megasas_instance *instance)
642 struct megasas_register_set __iomem *regs;
644 regs = instance->reg_set;
645 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
647 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
649 /* Dummy readl to force pci flush */
650 readl(®s->outbound_intr_mask);
654 * megasas_disable_intr_skinny - Disables interrupt
655 * @regs: MFI register set
658 megasas_disable_intr_skinny(struct megasas_instance *instance)
660 struct megasas_register_set __iomem *regs;
661 u32 mask = 0xFFFFFFFF;
663 regs = instance->reg_set;
664 writel(mask, ®s->outbound_intr_mask);
665 /* Dummy readl to force pci flush */
666 readl(®s->outbound_intr_mask);
670 * megasas_read_fw_status_reg_skinny - returns the current FW status value
671 * @regs: MFI register set
674 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
676 return readl(&(regs)->outbound_scratch_pad);
680 * megasas_clear_interrupt_skinny - Check & clear interrupt
681 * @regs: MFI register set
684 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
690 * Check if it is our interrupt
692 status = readl(®s->outbound_intr_status);
694 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
699 * Check if it is our interrupt
701 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
703 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
705 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
708 * Clear the interrupt by writing back the same value
710 writel(status, ®s->outbound_intr_status);
713 * dummy read to flush PCI
715 readl(®s->outbound_intr_status);
721 * megasas_fire_cmd_skinny - Sends command to the FW
722 * @frame_phys_addr : Physical address of cmd
723 * @frame_count : Number of frames for the command
724 * @regs : MFI register set
727 megasas_fire_cmd_skinny(struct megasas_instance *instance,
728 dma_addr_t frame_phys_addr,
730 struct megasas_register_set __iomem *regs)
734 spin_lock_irqsave(&instance->hba_lock, flags);
735 writel(upper_32_bits(frame_phys_addr),
736 &(regs)->inbound_high_queue_port);
737 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
738 &(regs)->inbound_low_queue_port);
740 spin_unlock_irqrestore(&instance->hba_lock, flags);
744 * megasas_check_reset_skinny - For controller reset check
745 * @regs: MFI register set
748 megasas_check_reset_skinny(struct megasas_instance *instance,
749 struct megasas_register_set __iomem *regs)
751 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
757 static struct megasas_instance_template megasas_instance_template_skinny = {
759 .fire_cmd = megasas_fire_cmd_skinny,
760 .enable_intr = megasas_enable_intr_skinny,
761 .disable_intr = megasas_disable_intr_skinny,
762 .clear_intr = megasas_clear_intr_skinny,
763 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
764 .adp_reset = megasas_adp_reset_gen2,
765 .check_reset = megasas_check_reset_skinny,
766 .service_isr = megasas_isr,
767 .tasklet = megasas_complete_cmd_dpc,
768 .init_adapter = megasas_init_adapter_mfi,
769 .build_and_issue_cmd = megasas_build_and_issue_cmd,
770 .issue_dcmd = megasas_issue_dcmd,
775 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
780 * megasas_enable_intr_gen2 - Enables interrupts
781 * @regs: MFI register set
784 megasas_enable_intr_gen2(struct megasas_instance *instance)
786 struct megasas_register_set __iomem *regs;
788 regs = instance->reg_set;
789 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
791 /* write ~0x00000005 (4 & 1) to the intr mask*/
792 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
794 /* Dummy readl to force pci flush */
795 readl(®s->outbound_intr_mask);
799 * megasas_disable_intr_gen2 - Disables interrupt
800 * @regs: MFI register set
803 megasas_disable_intr_gen2(struct megasas_instance *instance)
805 struct megasas_register_set __iomem *regs;
806 u32 mask = 0xFFFFFFFF;
808 regs = instance->reg_set;
809 writel(mask, ®s->outbound_intr_mask);
810 /* Dummy readl to force pci flush */
811 readl(®s->outbound_intr_mask);
815 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
816 * @regs: MFI register set
819 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
821 return readl(&(regs)->outbound_scratch_pad);
825 * megasas_clear_interrupt_gen2 - Check & clear interrupt
826 * @regs: MFI register set
829 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
835 * Check if it is our interrupt
837 status = readl(®s->outbound_intr_status);
839 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
840 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
842 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
843 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
847 * Clear the interrupt by writing back the same value
850 writel(status, ®s->outbound_doorbell_clear);
852 /* Dummy readl to force pci flush */
853 readl(®s->outbound_intr_status);
858 * megasas_fire_cmd_gen2 - Sends command to the FW
859 * @frame_phys_addr : Physical address of cmd
860 * @frame_count : Number of frames for the command
861 * @regs : MFI register set
864 megasas_fire_cmd_gen2(struct megasas_instance *instance,
865 dma_addr_t frame_phys_addr,
867 struct megasas_register_set __iomem *regs)
871 spin_lock_irqsave(&instance->hba_lock, flags);
872 writel((frame_phys_addr | (frame_count<<1))|1,
873 &(regs)->inbound_queue_port);
874 spin_unlock_irqrestore(&instance->hba_lock, flags);
878 * megasas_adp_reset_gen2 - For controller reset
879 * @regs: MFI register set
882 megasas_adp_reset_gen2(struct megasas_instance *instance,
883 struct megasas_register_set __iomem *reg_set)
887 u32 __iomem *seq_offset = ®_set->seq_offset;
888 u32 __iomem *hostdiag_offset = ®_set->host_diag;
890 if (instance->instancet == &megasas_instance_template_skinny) {
891 seq_offset = ®_set->fusion_seq_offset;
892 hostdiag_offset = ®_set->fusion_host_diag;
895 writel(0, seq_offset);
896 writel(4, seq_offset);
897 writel(0xb, seq_offset);
898 writel(2, seq_offset);
899 writel(7, seq_offset);
900 writel(0xd, seq_offset);
904 HostDiag = (u32)readl(hostdiag_offset);
906 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
908 HostDiag = (u32)readl(hostdiag_offset);
909 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
917 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
919 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
923 HostDiag = (u32)readl(hostdiag_offset);
924 while (HostDiag & DIAG_RESET_ADAPTER) {
926 HostDiag = (u32)readl(hostdiag_offset);
927 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
938 * megasas_check_reset_gen2 - For controller reset check
939 * @regs: MFI register set
942 megasas_check_reset_gen2(struct megasas_instance *instance,
943 struct megasas_register_set __iomem *regs)
945 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
951 static struct megasas_instance_template megasas_instance_template_gen2 = {
953 .fire_cmd = megasas_fire_cmd_gen2,
954 .enable_intr = megasas_enable_intr_gen2,
955 .disable_intr = megasas_disable_intr_gen2,
956 .clear_intr = megasas_clear_intr_gen2,
957 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
958 .adp_reset = megasas_adp_reset_gen2,
959 .check_reset = megasas_check_reset_gen2,
960 .service_isr = megasas_isr,
961 .tasklet = megasas_complete_cmd_dpc,
962 .init_adapter = megasas_init_adapter_mfi,
963 .build_and_issue_cmd = megasas_build_and_issue_cmd,
964 .issue_dcmd = megasas_issue_dcmd,
968 * This is the end of set of functions & definitions
969 * specific to gen2 (deviceid : 0x78, 0x79) controllers
973 * Template added for TB (Fusion)
975 extern struct megasas_instance_template megasas_instance_template_fusion;
978 * megasas_issue_polled - Issues a polling command
979 * @instance: Adapter soft state
980 * @cmd: Command packet to be issued
982 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
985 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
987 struct megasas_header *frame_hdr = &cmd->frame->hdr;
989 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
990 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
992 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
993 (instance->instancet->issue_dcmd(instance, cmd))) {
994 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
996 return DCMD_NOT_FIRED;
999 return wait_and_poll(instance, cmd, instance->requestorId ?
1000 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1004 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1005 * @instance: Adapter soft state
1006 * @cmd: Command to be issued
1007 * @timeout: Timeout in seconds
1009 * This function waits on an event for the command to be returned from ISR.
1010 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1011 * Used to issue ioctl commands.
1014 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1015 struct megasas_cmd *cmd, int timeout)
1018 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1020 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
1021 (instance->instancet->issue_dcmd(instance, cmd))) {
1022 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1023 __func__, __LINE__);
1024 return DCMD_NOT_FIRED;
1028 ret = wait_event_timeout(instance->int_cmd_wait_q,
1029 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1031 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1032 __func__, __LINE__);
1033 return DCMD_TIMEOUT;
1036 wait_event(instance->int_cmd_wait_q,
1037 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1039 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1040 DCMD_SUCCESS : DCMD_FAILED;
1044 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1045 * @instance: Adapter soft state
1046 * @cmd_to_abort: Previously issued cmd to be aborted
1047 * @timeout: Timeout in seconds
1049 * MFI firmware can abort previously issued AEN comamnd (automatic event
1050 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1051 * cmd and waits for return status.
1052 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1055 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1056 struct megasas_cmd *cmd_to_abort, int timeout)
1058 struct megasas_cmd *cmd;
1059 struct megasas_abort_frame *abort_fr;
1062 cmd = megasas_get_cmd(instance);
1067 abort_fr = &cmd->frame->abort;
1070 * Prepare and issue the abort frame
1072 abort_fr->cmd = MFI_CMD_ABORT;
1073 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1074 abort_fr->flags = cpu_to_le16(0);
1075 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1076 abort_fr->abort_mfi_phys_addr_lo =
1077 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1078 abort_fr->abort_mfi_phys_addr_hi =
1079 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1082 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1084 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
1085 (instance->instancet->issue_dcmd(instance, cmd))) {
1086 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1087 __func__, __LINE__);
1088 return DCMD_NOT_FIRED;
1092 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1093 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1095 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1096 __func__, __LINE__);
1097 return DCMD_TIMEOUT;
1100 wait_event(instance->abort_cmd_wait_q,
1101 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1105 megasas_return_cmd(instance, cmd);
1106 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1107 DCMD_SUCCESS : DCMD_FAILED;
1111 * megasas_make_sgl32 - Prepares 32-bit SGL
1112 * @instance: Adapter soft state
1113 * @scp: SCSI command from the mid-layer
1114 * @mfi_sgl: SGL to be filled in
1116 * If successful, this function returns the number of SG elements. Otherwise,
1120 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1121 union megasas_sgl *mfi_sgl)
1125 struct scatterlist *os_sgl;
1127 sge_count = scsi_dma_map(scp);
1128 BUG_ON(sge_count < 0);
1131 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1132 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1133 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1140 * megasas_make_sgl64 - Prepares 64-bit SGL
1141 * @instance: Adapter soft state
1142 * @scp: SCSI command from the mid-layer
1143 * @mfi_sgl: SGL to be filled in
1145 * If successful, this function returns the number of SG elements. Otherwise,
1149 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1150 union megasas_sgl *mfi_sgl)
1154 struct scatterlist *os_sgl;
1156 sge_count = scsi_dma_map(scp);
1157 BUG_ON(sge_count < 0);
1160 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1161 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1162 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1169 * megasas_make_sgl_skinny - Prepares IEEE SGL
1170 * @instance: Adapter soft state
1171 * @scp: SCSI command from the mid-layer
1172 * @mfi_sgl: SGL to be filled in
1174 * If successful, this function returns the number of SG elements. Otherwise,
1178 megasas_make_sgl_skinny(struct megasas_instance *instance,
1179 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1183 struct scatterlist *os_sgl;
1185 sge_count = scsi_dma_map(scp);
1188 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1189 mfi_sgl->sge_skinny[i].length =
1190 cpu_to_le32(sg_dma_len(os_sgl));
1191 mfi_sgl->sge_skinny[i].phys_addr =
1192 cpu_to_le64(sg_dma_address(os_sgl));
1193 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1200 * megasas_get_frame_count - Computes the number of frames
1201 * @frame_type : type of frame- io or pthru frame
1202 * @sge_count : number of sg elements
1204 * Returns the number of frames required for numnber of sge's (sge_count)
1207 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1208 u8 sge_count, u8 frame_type)
1213 u32 frame_count = 0;
1215 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1216 sizeof(struct megasas_sge32);
1218 if (instance->flag_ieee) {
1219 sge_sz = sizeof(struct megasas_sge_skinny);
1223 * Main frame can contain 2 SGEs for 64-bit SGLs and
1224 * 3 SGEs for 32-bit SGLs for ldio &
1225 * 1 SGEs for 64-bit SGLs and
1226 * 2 SGEs for 32-bit SGLs for pthru frame
1228 if (unlikely(frame_type == PTHRU_FRAME)) {
1229 if (instance->flag_ieee == 1) {
1230 num_cnt = sge_count - 1;
1231 } else if (IS_DMA64)
1232 num_cnt = sge_count - 1;
1234 num_cnt = sge_count - 2;
1236 if (instance->flag_ieee == 1) {
1237 num_cnt = sge_count - 1;
1238 } else if (IS_DMA64)
1239 num_cnt = sge_count - 2;
1241 num_cnt = sge_count - 3;
1245 sge_bytes = sge_sz * num_cnt;
1247 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1248 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1253 if (frame_count > 7)
1259 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1260 * @instance: Adapter soft state
1261 * @scp: SCSI command
1262 * @cmd: Command to be prepared in
1264 * This function prepares CDB commands. These are typcially pass-through
1265 * commands to the devices.
1268 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1269 struct megasas_cmd *cmd)
1274 struct megasas_pthru_frame *pthru;
1276 is_logical = MEGASAS_IS_LOGICAL(scp);
1277 device_id = MEGASAS_DEV_INDEX(scp);
1278 pthru = (struct megasas_pthru_frame *)cmd->frame;
1280 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1281 flags = MFI_FRAME_DIR_WRITE;
1282 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1283 flags = MFI_FRAME_DIR_READ;
1284 else if (scp->sc_data_direction == PCI_DMA_NONE)
1285 flags = MFI_FRAME_DIR_NONE;
1287 if (instance->flag_ieee == 1) {
1288 flags |= MFI_FRAME_IEEE;
1292 * Prepare the DCDB frame
1294 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1295 pthru->cmd_status = 0x0;
1296 pthru->scsi_status = 0x0;
1297 pthru->target_id = device_id;
1298 pthru->lun = scp->device->lun;
1299 pthru->cdb_len = scp->cmd_len;
1302 pthru->flags = cpu_to_le16(flags);
1303 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1305 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1308 * If the command is for the tape device, set the
1309 * pthru timeout to the os layer timeout value.
1311 if (scp->device->type == TYPE_TAPE) {
1312 if ((scp->request->timeout / HZ) > 0xFFFF)
1313 pthru->timeout = cpu_to_le16(0xFFFF);
1315 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1321 if (instance->flag_ieee == 1) {
1322 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1323 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1325 } else if (IS_DMA64) {
1326 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1327 pthru->sge_count = megasas_make_sgl64(instance, scp,
1330 pthru->sge_count = megasas_make_sgl32(instance, scp,
1333 if (pthru->sge_count > instance->max_num_sge) {
1334 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1340 * Sense info specific
1342 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1343 pthru->sense_buf_phys_addr_hi =
1344 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1345 pthru->sense_buf_phys_addr_lo =
1346 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1349 * Compute the total number of frames this command consumes. FW uses
1350 * this number to pull sufficient number of frames from host memory.
1352 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1355 return cmd->frame_count;
1359 * megasas_build_ldio - Prepares IOs to logical devices
1360 * @instance: Adapter soft state
1361 * @scp: SCSI command
1362 * @cmd: Command to be prepared
1364 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1367 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1368 struct megasas_cmd *cmd)
1371 u8 sc = scp->cmnd[0];
1373 struct megasas_io_frame *ldio;
1375 device_id = MEGASAS_DEV_INDEX(scp);
1376 ldio = (struct megasas_io_frame *)cmd->frame;
1378 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1379 flags = MFI_FRAME_DIR_WRITE;
1380 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1381 flags = MFI_FRAME_DIR_READ;
1383 if (instance->flag_ieee == 1) {
1384 flags |= MFI_FRAME_IEEE;
1388 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1390 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1391 ldio->cmd_status = 0x0;
1392 ldio->scsi_status = 0x0;
1393 ldio->target_id = device_id;
1395 ldio->reserved_0 = 0;
1397 ldio->flags = cpu_to_le16(flags);
1398 ldio->start_lba_hi = 0;
1399 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1402 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1404 if (scp->cmd_len == 6) {
1405 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1406 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1407 ((u32) scp->cmnd[2] << 8) |
1408 (u32) scp->cmnd[3]);
1410 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1414 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1416 else if (scp->cmd_len == 10) {
1417 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1418 ((u32) scp->cmnd[7] << 8));
1419 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1420 ((u32) scp->cmnd[3] << 16) |
1421 ((u32) scp->cmnd[4] << 8) |
1422 (u32) scp->cmnd[5]);
1426 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1428 else if (scp->cmd_len == 12) {
1429 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1430 ((u32) scp->cmnd[7] << 16) |
1431 ((u32) scp->cmnd[8] << 8) |
1432 (u32) scp->cmnd[9]);
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 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1443 else if (scp->cmd_len == 16) {
1444 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1445 ((u32) scp->cmnd[11] << 16) |
1446 ((u32) scp->cmnd[12] << 8) |
1447 (u32) scp->cmnd[13]);
1449 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1450 ((u32) scp->cmnd[7] << 16) |
1451 ((u32) scp->cmnd[8] << 8) |
1452 (u32) scp->cmnd[9]);
1454 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1455 ((u32) scp->cmnd[3] << 16) |
1456 ((u32) scp->cmnd[4] << 8) |
1457 (u32) scp->cmnd[5]);
1464 if (instance->flag_ieee) {
1465 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1466 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1468 } else if (IS_DMA64) {
1469 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1470 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1472 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1474 if (ldio->sge_count > instance->max_num_sge) {
1475 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1481 * Sense info specific
1483 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1484 ldio->sense_buf_phys_addr_hi = 0;
1485 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1488 * Compute the total number of frames this command consumes. FW uses
1489 * this number to pull sufficient number of frames from host memory.
1491 cmd->frame_count = megasas_get_frame_count(instance,
1492 ldio->sge_count, IO_FRAME);
1494 return cmd->frame_count;
1498 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1499 * and whether it's RW or non RW
1500 * @scmd: SCSI command
1503 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1507 switch (cmd->cmnd[0]) {
1516 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1517 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1520 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1521 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1527 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1529 * @instance: Adapter soft state
1532 megasas_dump_pending_frames(struct megasas_instance *instance)
1534 struct megasas_cmd *cmd;
1536 union megasas_sgl *mfi_sgl;
1537 struct megasas_io_frame *ldio;
1538 struct megasas_pthru_frame *pthru;
1540 u32 max_cmd = instance->max_fw_cmds;
1542 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1543 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1545 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1547 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1549 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1550 for (i = 0; i < max_cmd; i++) {
1551 cmd = instance->cmd_list[i];
1554 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1555 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1556 ldio = (struct megasas_io_frame *)cmd->frame;
1557 mfi_sgl = &ldio->sgl;
1558 sgcount = ldio->sge_count;
1559 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1560 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1561 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1562 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1563 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1565 pthru = (struct megasas_pthru_frame *) cmd->frame;
1566 mfi_sgl = &pthru->sgl;
1567 sgcount = pthru->sge_count;
1568 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1569 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1570 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1571 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1572 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1574 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1575 for (n = 0; n < sgcount; n++) {
1577 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1578 le32_to_cpu(mfi_sgl->sge64[n].length),
1579 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1581 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1582 le32_to_cpu(mfi_sgl->sge32[n].length),
1583 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1587 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1588 for (i = 0; i < max_cmd; i++) {
1590 cmd = instance->cmd_list[i];
1592 if (cmd->sync_cmd == 1)
1593 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1595 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1599 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1600 struct scsi_cmnd *scmd)
1602 struct megasas_cmd *cmd;
1605 cmd = megasas_get_cmd(instance);
1607 return SCSI_MLQUEUE_HOST_BUSY;
1610 * Logical drive command
1612 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1613 frame_count = megasas_build_ldio(instance, scmd, cmd);
1615 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1618 goto out_return_cmd;
1621 scmd->SCp.ptr = (char *)cmd;
1624 * Issue the command to the FW
1626 atomic_inc(&instance->fw_outstanding);
1628 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1629 cmd->frame_count-1, instance->reg_set);
1633 megasas_return_cmd(instance, cmd);
1634 return SCSI_MLQUEUE_HOST_BUSY;
1639 * megasas_queue_command - Queue entry point
1640 * @scmd: SCSI command to be queued
1641 * @done: Callback entry point
1644 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1646 struct megasas_instance *instance;
1647 struct MR_PRIV_DEVICE *mr_device_priv_data;
1649 instance = (struct megasas_instance *)
1650 scmd->device->host->hostdata;
1652 if (instance->unload == 1) {
1653 scmd->result = DID_NO_CONNECT << 16;
1654 scmd->scsi_done(scmd);
1658 if (instance->issuepend_done == 0)
1659 return SCSI_MLQUEUE_HOST_BUSY;
1662 /* Check for an mpio path and adjust behavior */
1663 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1664 if (megasas_check_mpio_paths(instance, scmd) ==
1665 (DID_RESET << 16)) {
1666 return SCSI_MLQUEUE_HOST_BUSY;
1668 scmd->result = DID_NO_CONNECT << 16;
1669 scmd->scsi_done(scmd);
1674 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1675 scmd->result = DID_NO_CONNECT << 16;
1676 scmd->scsi_done(scmd);
1680 mr_device_priv_data = scmd->device->hostdata;
1681 if (!mr_device_priv_data) {
1682 scmd->result = DID_NO_CONNECT << 16;
1683 scmd->scsi_done(scmd);
1687 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1688 return SCSI_MLQUEUE_HOST_BUSY;
1690 if (mr_device_priv_data->tm_busy)
1691 return SCSI_MLQUEUE_DEVICE_BUSY;
1696 if (MEGASAS_IS_LOGICAL(scmd) &&
1697 (scmd->device->id >= instance->fw_supported_vd_count ||
1698 scmd->device->lun)) {
1699 scmd->result = DID_BAD_TARGET << 16;
1703 switch (scmd->cmnd[0]) {
1704 case SYNCHRONIZE_CACHE:
1706 * FW takes care of flush cache on its own
1707 * No need to send it down
1709 scmd->result = DID_OK << 16;
1715 return instance->instancet->build_and_issue_cmd(instance, scmd);
1718 scmd->scsi_done(scmd);
1722 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1726 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1728 if ((megasas_mgmt_info.instance[i]) &&
1729 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1730 return megasas_mgmt_info.instance[i];
1737 * megasas_update_sdev_properties - Update sdev structure based on controller's FW capabilities
1739 * @sdev: OS provided scsi device
1743 void megasas_update_sdev_properties(struct scsi_device *sdev)
1747 struct megasas_instance *instance;
1748 struct fusion_context *fusion;
1749 struct MR_PRIV_DEVICE *mr_device_priv_data;
1750 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1751 struct MR_LD_RAID *raid;
1752 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1754 instance = megasas_lookup_instance(sdev->host->host_no);
1755 fusion = instance->ctrl_context;
1756 mr_device_priv_data = sdev->hostdata;
1761 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1762 instance->use_seqnum_jbod_fp) {
1763 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1765 pd_sync = (void *)fusion->pd_seq_sync
1766 [(instance->pd_seq_map_id - 1) & 1];
1767 mr_device_priv_data->is_tm_capable =
1768 pd_sync->seq[pd_index].capability.tmCapable;
1770 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1772 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1773 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1774 raid = MR_LdRaidGet(ld, local_map_ptr);
1776 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1777 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1778 mr_device_priv_data->is_tm_capable =
1779 raid->capability.tmCapable;
1783 static void megasas_set_device_queue_depth(struct scsi_device *sdev)
1786 int ret = DCMD_FAILED;
1787 struct megasas_instance *instance;
1789 instance = megasas_lookup_instance(sdev->host->host_no);
1791 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1792 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
1794 if (instance->pd_info) {
1795 mutex_lock(&instance->hba_mutex);
1796 ret = megasas_get_pd_info(instance, pd_index);
1797 mutex_unlock(&instance->hba_mutex);
1800 if (ret != DCMD_SUCCESS)
1803 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1805 switch (instance->pd_list[pd_index].interface) {
1807 scsi_change_queue_depth(sdev, MEGASAS_SAS_QD);
1811 scsi_change_queue_depth(sdev, MEGASAS_SATA_QD);
1815 scsi_change_queue_depth(sdev, MEGASAS_DEFAULT_PD_QD);
1822 static int megasas_slave_configure(struct scsi_device *sdev)
1825 struct megasas_instance *instance;
1827 instance = megasas_lookup_instance(sdev->host->host_no);
1828 if (instance->pd_list_not_supported) {
1829 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1830 sdev->type == TYPE_DISK) {
1831 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1833 if (instance->pd_list[pd_index].driveState !=
1838 megasas_set_device_queue_depth(sdev);
1839 megasas_update_sdev_properties(sdev);
1842 * The RAID firmware may require extended timeouts.
1844 blk_queue_rq_timeout(sdev->request_queue,
1850 static int megasas_slave_alloc(struct scsi_device *sdev)
1853 struct megasas_instance *instance ;
1854 struct MR_PRIV_DEVICE *mr_device_priv_data;
1856 instance = megasas_lookup_instance(sdev->host->host_no);
1857 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1859 * Open the OS scan to the SYSTEM PD
1862 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1864 if ((instance->pd_list_not_supported ||
1865 instance->pd_list[pd_index].driveState ==
1866 MR_PD_STATE_SYSTEM)) {
1873 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
1875 if (!mr_device_priv_data)
1877 sdev->hostdata = mr_device_priv_data;
1881 static void megasas_slave_destroy(struct scsi_device *sdev)
1883 kfree(sdev->hostdata);
1884 sdev->hostdata = NULL;
1888 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1890 * @instance: Adapter soft state
1893 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
1896 struct megasas_cmd *cmd_mfi;
1897 struct megasas_cmd_fusion *cmd_fusion;
1898 struct fusion_context *fusion = instance->ctrl_context;
1900 /* Find all outstanding ioctls */
1902 for (i = 0; i < instance->max_fw_cmds; i++) {
1903 cmd_fusion = fusion->cmd_list[i];
1904 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1905 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1906 if (cmd_mfi->sync_cmd &&
1907 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1908 megasas_complete_cmd(instance,
1913 for (i = 0; i < instance->max_fw_cmds; i++) {
1914 cmd_mfi = instance->cmd_list[i];
1915 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1917 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1923 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1925 /* Set critical error to block I/O & ioctls in case caller didn't */
1926 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
1927 /* Wait 1 second to ensure IO or ioctls in build have posted */
1929 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1930 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1931 (instance->ctrl_context)) {
1932 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1934 readl(&instance->reg_set->doorbell);
1935 if (instance->requestorId && instance->peerIsPresent)
1936 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
1938 writel(MFI_STOP_ADP,
1939 &instance->reg_set->inbound_doorbell);
1941 /* Complete outstanding ioctls when adapter is killed */
1942 megasas_complete_outstanding_ioctls(instance);
1946 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1947 * restored to max value
1948 * @instance: Adapter soft state
1952 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1954 unsigned long flags;
1956 if (instance->flag & MEGASAS_FW_BUSY
1957 && time_after(jiffies, instance->last_time + 5 * HZ)
1958 && atomic_read(&instance->fw_outstanding) <
1959 instance->throttlequeuedepth + 1) {
1961 spin_lock_irqsave(instance->host->host_lock, flags);
1962 instance->flag &= ~MEGASAS_FW_BUSY;
1964 instance->host->can_queue = instance->cur_can_queue;
1965 spin_unlock_irqrestore(instance->host->host_lock, flags);
1970 * megasas_complete_cmd_dpc - Returns FW's controller structure
1971 * @instance_addr: Address of adapter soft state
1973 * Tasklet to complete cmds
1975 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1980 struct megasas_cmd *cmd;
1981 struct megasas_instance *instance =
1982 (struct megasas_instance *)instance_addr;
1983 unsigned long flags;
1985 /* If we have already declared adapter dead, donot complete cmds */
1986 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
1989 spin_lock_irqsave(&instance->completion_lock, flags);
1991 producer = le32_to_cpu(*instance->producer);
1992 consumer = le32_to_cpu(*instance->consumer);
1994 while (consumer != producer) {
1995 context = le32_to_cpu(instance->reply_queue[consumer]);
1996 if (context >= instance->max_fw_cmds) {
1997 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2002 cmd = instance->cmd_list[context];
2004 megasas_complete_cmd(instance, cmd, DID_OK);
2007 if (consumer == (instance->max_fw_cmds + 1)) {
2012 *instance->consumer = cpu_to_le32(producer);
2014 spin_unlock_irqrestore(&instance->completion_lock, flags);
2017 * Check if we can restore can_queue
2019 megasas_check_and_restore_queue_depth(instance);
2023 * megasas_start_timer - Initializes a timer object
2024 * @instance: Adapter soft state
2025 * @timer: timer object to be initialized
2026 * @fn: timer function
2027 * @interval: time interval between timer function call
2030 void megasas_start_timer(struct megasas_instance *instance,
2031 struct timer_list *timer,
2032 void *fn, unsigned long interval)
2035 timer->expires = jiffies + interval;
2036 timer->data = (unsigned long)instance;
2037 timer->function = fn;
2042 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2045 process_fw_state_change_wq(struct work_struct *work);
2047 void megasas_do_ocr(struct megasas_instance *instance)
2049 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2050 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2051 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2052 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2054 instance->instancet->disable_intr(instance);
2055 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2056 instance->issuepend_done = 0;
2058 atomic_set(&instance->fw_outstanding, 0);
2059 megasas_internal_reset_defer_cmds(instance);
2060 process_fw_state_change_wq(&instance->work_init);
2063 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2066 struct megasas_cmd *cmd;
2067 struct megasas_dcmd_frame *dcmd;
2068 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2069 dma_addr_t new_affiliation_111_h;
2073 cmd = megasas_get_cmd(instance);
2076 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2077 "Failed to get cmd for scsi%d\n",
2078 instance->host->host_no);
2082 dcmd = &cmd->frame->dcmd;
2084 if (!instance->vf_affiliation_111) {
2085 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2086 "affiliation for scsi%d\n", instance->host->host_no);
2087 megasas_return_cmd(instance, cmd);
2092 memset(instance->vf_affiliation_111, 0,
2093 sizeof(struct MR_LD_VF_AFFILIATION_111));
2095 new_affiliation_111 =
2096 pci_alloc_consistent(instance->pdev,
2097 sizeof(struct MR_LD_VF_AFFILIATION_111),
2098 &new_affiliation_111_h);
2099 if (!new_affiliation_111) {
2100 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2101 "memory for new affiliation for scsi%d\n",
2102 instance->host->host_no);
2103 megasas_return_cmd(instance, cmd);
2106 memset(new_affiliation_111, 0,
2107 sizeof(struct MR_LD_VF_AFFILIATION_111));
2110 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2112 dcmd->cmd = MFI_CMD_DCMD;
2113 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2114 dcmd->sge_count = 1;
2115 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2118 dcmd->data_xfer_len =
2119 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2120 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2123 dcmd->sgl.sge32[0].phys_addr =
2124 cpu_to_le32(instance->vf_affiliation_111_h);
2126 dcmd->sgl.sge32[0].phys_addr =
2127 cpu_to_le32(new_affiliation_111_h);
2129 dcmd->sgl.sge32[0].length = cpu_to_le32(
2130 sizeof(struct MR_LD_VF_AFFILIATION_111));
2132 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2133 "scsi%d\n", instance->host->host_no);
2135 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2136 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2137 " failed with status 0x%x for scsi%d\n",
2138 dcmd->cmd_status, instance->host->host_no);
2139 retval = 1; /* Do a scan if we couldn't get affiliation */
2144 thisVf = new_affiliation_111->thisVf;
2145 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2146 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2147 new_affiliation_111->map[ld].policy[thisVf]) {
2148 dev_warn(&instance->pdev->dev, "SR-IOV: "
2149 "Got new LD/VF affiliation for scsi%d\n",
2150 instance->host->host_no);
2151 memcpy(instance->vf_affiliation_111,
2152 new_affiliation_111,
2153 sizeof(struct MR_LD_VF_AFFILIATION_111));
2159 if (new_affiliation_111) {
2160 pci_free_consistent(instance->pdev,
2161 sizeof(struct MR_LD_VF_AFFILIATION_111),
2162 new_affiliation_111,
2163 new_affiliation_111_h);
2166 megasas_return_cmd(instance, cmd);
2171 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2174 struct megasas_cmd *cmd;
2175 struct megasas_dcmd_frame *dcmd;
2176 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2177 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2178 dma_addr_t new_affiliation_h;
2179 int i, j, retval = 0, found = 0, doscan = 0;
2182 cmd = megasas_get_cmd(instance);
2185 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2186 "Failed to get cmd for scsi%d\n",
2187 instance->host->host_no);
2191 dcmd = &cmd->frame->dcmd;
2193 if (!instance->vf_affiliation) {
2194 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2195 "affiliation for scsi%d\n", instance->host->host_no);
2196 megasas_return_cmd(instance, cmd);
2201 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2202 sizeof(struct MR_LD_VF_AFFILIATION));
2205 pci_alloc_consistent(instance->pdev,
2206 (MAX_LOGICAL_DRIVES + 1) *
2207 sizeof(struct MR_LD_VF_AFFILIATION),
2208 &new_affiliation_h);
2209 if (!new_affiliation) {
2210 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2211 "memory for new affiliation for scsi%d\n",
2212 instance->host->host_no);
2213 megasas_return_cmd(instance, cmd);
2216 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2217 sizeof(struct MR_LD_VF_AFFILIATION));
2220 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2222 dcmd->cmd = MFI_CMD_DCMD;
2223 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2224 dcmd->sge_count = 1;
2225 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2228 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2229 sizeof(struct MR_LD_VF_AFFILIATION));
2230 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2233 dcmd->sgl.sge32[0].phys_addr =
2234 cpu_to_le32(instance->vf_affiliation_h);
2236 dcmd->sgl.sge32[0].phys_addr =
2237 cpu_to_le32(new_affiliation_h);
2239 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2240 sizeof(struct MR_LD_VF_AFFILIATION));
2242 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2243 "scsi%d\n", instance->host->host_no);
2246 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2247 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2248 " failed with status 0x%x for scsi%d\n",
2249 dcmd->cmd_status, instance->host->host_no);
2250 retval = 1; /* Do a scan if we couldn't get affiliation */
2255 if (!new_affiliation->ldCount) {
2256 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2257 "affiliation for passive path for scsi%d\n",
2258 instance->host->host_no);
2262 newmap = new_affiliation->map;
2263 savedmap = instance->vf_affiliation->map;
2264 thisVf = new_affiliation->thisVf;
2265 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2267 for (j = 0; j < instance->vf_affiliation->ldCount;
2269 if (newmap->ref.targetId ==
2270 savedmap->ref.targetId) {
2272 if (newmap->policy[thisVf] !=
2273 savedmap->policy[thisVf]) {
2278 savedmap = (struct MR_LD_VF_MAP *)
2279 ((unsigned char *)savedmap +
2282 if (!found && newmap->policy[thisVf] !=
2283 MR_LD_ACCESS_HIDDEN) {
2287 newmap = (struct MR_LD_VF_MAP *)
2288 ((unsigned char *)newmap + newmap->size);
2291 newmap = new_affiliation->map;
2292 savedmap = instance->vf_affiliation->map;
2294 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2296 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2297 if (savedmap->ref.targetId ==
2298 newmap->ref.targetId) {
2300 if (savedmap->policy[thisVf] !=
2301 newmap->policy[thisVf]) {
2306 newmap = (struct MR_LD_VF_MAP *)
2307 ((unsigned char *)newmap +
2310 if (!found && savedmap->policy[thisVf] !=
2311 MR_LD_ACCESS_HIDDEN) {
2315 savedmap = (struct MR_LD_VF_MAP *)
2316 ((unsigned char *)savedmap +
2322 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2323 "affiliation for scsi%d\n", instance->host->host_no);
2324 memcpy(instance->vf_affiliation, new_affiliation,
2325 new_affiliation->size);
2329 if (new_affiliation)
2330 pci_free_consistent(instance->pdev,
2331 (MAX_LOGICAL_DRIVES + 1) *
2332 sizeof(struct MR_LD_VF_AFFILIATION),
2333 new_affiliation, new_affiliation_h);
2334 megasas_return_cmd(instance, cmd);
2339 /* This function will get the current SR-IOV LD/VF affiliation */
2340 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2345 if (instance->PlasmaFW111)
2346 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2348 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2352 /* This function will tell FW to start the SR-IOV heartbeat */
2353 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2356 struct megasas_cmd *cmd;
2357 struct megasas_dcmd_frame *dcmd;
2360 cmd = megasas_get_cmd(instance);
2363 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2364 "Failed to get cmd for scsi%d\n",
2365 instance->host->host_no);
2369 dcmd = &cmd->frame->dcmd;
2372 instance->hb_host_mem =
2373 pci_zalloc_consistent(instance->pdev,
2374 sizeof(struct MR_CTRL_HB_HOST_MEM),
2375 &instance->hb_host_mem_h);
2376 if (!instance->hb_host_mem) {
2377 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2378 " memory for heartbeat host memory for scsi%d\n",
2379 instance->host->host_no);
2385 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2387 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2388 dcmd->cmd = MFI_CMD_DCMD;
2389 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2390 dcmd->sge_count = 1;
2391 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2394 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2395 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2396 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2397 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2399 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2400 instance->host->host_no);
2402 if (instance->ctrl_context && !instance->mask_interrupts)
2403 retval = megasas_issue_blocked_cmd(instance, cmd,
2404 MEGASAS_ROUTINE_WAIT_TIME_VF);
2406 retval = megasas_issue_polled(instance, cmd);
2409 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2410 "_MEM_ALLOC DCMD %s for scsi%d\n",
2411 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2412 "timed out" : "failed", instance->host->host_no);
2417 megasas_return_cmd(instance, cmd);
2422 /* Handler for SR-IOV heartbeat */
2423 void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2425 struct megasas_instance *instance =
2426 (struct megasas_instance *)instance_addr;
2428 if (instance->hb_host_mem->HB.fwCounter !=
2429 instance->hb_host_mem->HB.driverCounter) {
2430 instance->hb_host_mem->HB.driverCounter =
2431 instance->hb_host_mem->HB.fwCounter;
2432 mod_timer(&instance->sriov_heartbeat_timer,
2433 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2435 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2436 "completed for scsi%d\n", instance->host->host_no);
2437 schedule_work(&instance->work_init);
2442 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2443 * @instance: Adapter soft state
2445 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2446 * complete all its outstanding commands. Returns error if one or more IOs
2447 * are pending after this time period. It also marks the controller dead.
2449 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2451 int i, sl, outstanding;
2453 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2454 unsigned long flags;
2455 struct list_head clist_local;
2456 struct megasas_cmd *reset_cmd;
2459 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2460 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2461 __func__, __LINE__);
2465 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2467 INIT_LIST_HEAD(&clist_local);
2468 spin_lock_irqsave(&instance->hba_lock, flags);
2469 list_splice_init(&instance->internal_reset_pending_q,
2471 spin_unlock_irqrestore(&instance->hba_lock, flags);
2473 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2474 for (i = 0; i < wait_time; i++) {
2476 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2480 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2481 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2482 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2487 while (!list_empty(&clist_local)) {
2488 reset_cmd = list_entry((&clist_local)->next,
2489 struct megasas_cmd, list);
2490 list_del_init(&reset_cmd->list);
2491 if (reset_cmd->scmd) {
2492 reset_cmd->scmd->result = DID_RESET << 16;
2493 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2494 reset_index, reset_cmd,
2495 reset_cmd->scmd->cmnd[0]);
2497 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2498 megasas_return_cmd(instance, reset_cmd);
2499 } else if (reset_cmd->sync_cmd) {
2500 dev_notice(&instance->pdev->dev, "%p synch cmds"
2504 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2505 instance->instancet->fire_cmd(instance,
2506 reset_cmd->frame_phys_addr,
2507 0, instance->reg_set);
2509 dev_notice(&instance->pdev->dev, "%p unexpected"
2519 for (i = 0; i < resetwaittime; i++) {
2520 outstanding = atomic_read(&instance->fw_outstanding);
2525 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2526 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2527 "commands to complete\n",i,outstanding);
2529 * Call cmd completion routine. Cmd to be
2530 * be completed directly without depending on isr.
2532 megasas_complete_cmd_dpc((unsigned long)instance);
2539 outstanding = atomic_read(&instance->fw_outstanding);
2540 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2542 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2543 goto no_outstanding;
2545 if (instance->disableOnlineCtrlReset)
2546 goto kill_hba_and_failed;
2548 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2549 dev_info(&instance->pdev->dev,
2550 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2551 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2553 goto kill_hba_and_failed;
2554 megasas_do_ocr(instance);
2556 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2557 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2558 __func__, __LINE__);
2561 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2562 __func__, __LINE__);
2564 for (sl = 0; sl < 10; sl++)
2567 outstanding = atomic_read(&instance->fw_outstanding);
2569 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2570 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2571 goto no_outstanding;
2578 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2579 __func__, __LINE__);
2582 kill_hba_and_failed:
2584 /* Reset not supported, kill adapter */
2585 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2586 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2587 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2588 atomic_read(&instance->fw_outstanding));
2589 megasas_dump_pending_frames(instance);
2590 megaraid_sas_kill_hba(instance);
2596 * megasas_generic_reset - Generic reset routine
2597 * @scmd: Mid-layer SCSI command
2599 * This routine implements a generic reset handler for device, bus and host
2600 * reset requests. Device, bus and host specific reset handlers can use this
2601 * function after they do their specific tasks.
2603 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2606 struct megasas_instance *instance;
2608 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2610 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2611 scmd->cmnd[0], scmd->retries);
2613 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2614 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2618 ret_val = megasas_wait_for_outstanding(instance);
2619 if (ret_val == SUCCESS)
2620 dev_notice(&instance->pdev->dev, "reset successful\n");
2622 dev_err(&instance->pdev->dev, "failed to do reset\n");
2628 * megasas_reset_timer - quiesce the adapter if required
2631 * Sets the FW busy flag and reduces the host->can_queue if the
2632 * cmd has not been completed within the timeout period.
2635 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2637 struct megasas_instance *instance;
2638 unsigned long flags;
2640 if (time_after(jiffies, scmd->jiffies_at_alloc +
2641 (scmd_timeout * 2) * HZ)) {
2642 return BLK_EH_NOT_HANDLED;
2645 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2646 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2647 /* FW is busy, throttle IO */
2648 spin_lock_irqsave(instance->host->host_lock, flags);
2650 instance->host->can_queue = instance->throttlequeuedepth;
2651 instance->last_time = jiffies;
2652 instance->flag |= MEGASAS_FW_BUSY;
2654 spin_unlock_irqrestore(instance->host->host_lock, flags);
2656 return BLK_EH_RESET_TIMER;
2660 * megasas_reset_bus_host - Bus & host reset handler entry point
2662 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2665 struct megasas_instance *instance;
2667 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2670 * First wait for all commands to complete
2672 if (instance->ctrl_context)
2673 ret = megasas_reset_fusion(scmd->device->host, 1);
2675 ret = megasas_generic_reset(scmd);
2681 * megasas_task_abort - Issues task abort request to firmware
2682 * (supported only for fusion adapters)
2683 * @scmd: SCSI command pointer
2685 static int megasas_task_abort(struct scsi_cmnd *scmd)
2688 struct megasas_instance *instance;
2690 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2692 if (instance->ctrl_context)
2693 ret = megasas_task_abort_fusion(scmd);
2695 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2703 * megasas_reset_target: Issues target reset request to firmware
2704 * (supported only for fusion adapters)
2705 * @scmd: SCSI command pointer
2707 static int megasas_reset_target(struct scsi_cmnd *scmd)
2710 struct megasas_instance *instance;
2712 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2714 if (instance->ctrl_context)
2715 ret = megasas_reset_target_fusion(scmd);
2717 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2725 * megasas_bios_param - Returns disk geometry for a disk
2726 * @sdev: device handle
2727 * @bdev: block device
2728 * @capacity: drive capacity
2729 * @geom: geometry parameters
2732 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2733 sector_t capacity, int geom[])
2740 /* Default heads (64) & sectors (32) */
2744 tmp = heads * sectors;
2745 cylinders = capacity;
2747 sector_div(cylinders, tmp);
2750 * Handle extended translation size for logical drives > 1Gb
2753 if (capacity >= 0x200000) {
2756 tmp = heads*sectors;
2757 cylinders = capacity;
2758 sector_div(cylinders, tmp);
2763 geom[2] = cylinders;
2768 static void megasas_aen_polling(struct work_struct *work);
2771 * megasas_service_aen - Processes an event notification
2772 * @instance: Adapter soft state
2773 * @cmd: AEN command completed by the ISR
2775 * For AEN, driver sends a command down to FW that is held by the FW till an
2776 * event occurs. When an event of interest occurs, FW completes the command
2777 * that it was previously holding.
2779 * This routines sends SIGIO signal to processes that have registered with the
2783 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2785 unsigned long flags;
2788 * Don't signal app if it is just an aborted previously registered aen
2790 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2791 spin_lock_irqsave(&poll_aen_lock, flags);
2792 megasas_poll_wait_aen = 1;
2793 spin_unlock_irqrestore(&poll_aen_lock, flags);
2794 wake_up(&megasas_poll_wait);
2795 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2800 instance->aen_cmd = NULL;
2802 megasas_return_cmd(instance, cmd);
2804 if ((instance->unload == 0) &&
2805 ((instance->issuepend_done == 1))) {
2806 struct megasas_aen_event *ev;
2808 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2810 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
2812 ev->instance = instance;
2814 INIT_DELAYED_WORK(&ev->hotplug_work,
2815 megasas_aen_polling);
2816 schedule_delayed_work(&ev->hotplug_work, 0);
2822 megasas_fw_crash_buffer_store(struct device *cdev,
2823 struct device_attribute *attr, const char *buf, size_t count)
2825 struct Scsi_Host *shost = class_to_shost(cdev);
2826 struct megasas_instance *instance =
2827 (struct megasas_instance *) shost->hostdata;
2829 unsigned long flags;
2831 if (kstrtoint(buf, 0, &val) != 0)
2834 spin_lock_irqsave(&instance->crashdump_lock, flags);
2835 instance->fw_crash_buffer_offset = val;
2836 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2841 megasas_fw_crash_buffer_show(struct device *cdev,
2842 struct device_attribute *attr, char *buf)
2844 struct Scsi_Host *shost = class_to_shost(cdev);
2845 struct megasas_instance *instance =
2846 (struct megasas_instance *) shost->hostdata;
2848 unsigned long buff_addr;
2849 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2850 unsigned long src_addr;
2851 unsigned long flags;
2854 spin_lock_irqsave(&instance->crashdump_lock, flags);
2855 buff_offset = instance->fw_crash_buffer_offset;
2856 if (!instance->crash_dump_buf &&
2857 !((instance->fw_crash_state == AVAILABLE) ||
2858 (instance->fw_crash_state == COPYING))) {
2859 dev_err(&instance->pdev->dev,
2860 "Firmware crash dump is not available\n");
2861 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2865 buff_addr = (unsigned long) buf;
2867 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
2868 dev_err(&instance->pdev->dev,
2869 "Firmware crash dump offset is out of range\n");
2870 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2874 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2875 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2877 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2878 (buff_offset % dmachunk);
2879 memcpy(buf, (void *)src_addr, size);
2880 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2886 megasas_fw_crash_buffer_size_show(struct device *cdev,
2887 struct device_attribute *attr, char *buf)
2889 struct Scsi_Host *shost = class_to_shost(cdev);
2890 struct megasas_instance *instance =
2891 (struct megasas_instance *) shost->hostdata;
2893 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2894 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2898 megasas_fw_crash_state_store(struct device *cdev,
2899 struct device_attribute *attr, const char *buf, size_t count)
2901 struct Scsi_Host *shost = class_to_shost(cdev);
2902 struct megasas_instance *instance =
2903 (struct megasas_instance *) shost->hostdata;
2905 unsigned long flags;
2907 if (kstrtoint(buf, 0, &val) != 0)
2910 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2911 dev_err(&instance->pdev->dev, "application updates invalid "
2912 "firmware crash state\n");
2916 instance->fw_crash_state = val;
2918 if ((val == COPIED) || (val == COPY_ERROR)) {
2919 spin_lock_irqsave(&instance->crashdump_lock, flags);
2920 megasas_free_host_crash_buffer(instance);
2921 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2922 if (val == COPY_ERROR)
2923 dev_info(&instance->pdev->dev, "application failed to "
2924 "copy Firmware crash dump\n");
2926 dev_info(&instance->pdev->dev, "Firmware crash dump "
2927 "copied successfully\n");
2933 megasas_fw_crash_state_show(struct device *cdev,
2934 struct device_attribute *attr, char *buf)
2936 struct Scsi_Host *shost = class_to_shost(cdev);
2937 struct megasas_instance *instance =
2938 (struct megasas_instance *) shost->hostdata;
2940 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2944 megasas_page_size_show(struct device *cdev,
2945 struct device_attribute *attr, char *buf)
2947 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2951 megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
2954 struct Scsi_Host *shost = class_to_shost(cdev);
2955 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
2957 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
2960 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2961 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2962 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2963 megasas_fw_crash_buffer_size_show, NULL);
2964 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2965 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2966 static DEVICE_ATTR(page_size, S_IRUGO,
2967 megasas_page_size_show, NULL);
2968 static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
2969 megasas_ldio_outstanding_show, NULL);
2971 struct device_attribute *megaraid_host_attrs[] = {
2972 &dev_attr_fw_crash_buffer_size,
2973 &dev_attr_fw_crash_buffer,
2974 &dev_attr_fw_crash_state,
2975 &dev_attr_page_size,
2976 &dev_attr_ldio_outstanding,
2981 * Scsi host template for megaraid_sas driver
2983 static struct scsi_host_template megasas_template = {
2985 .module = THIS_MODULE,
2986 .name = "Avago SAS based MegaRAID driver",
2987 .proc_name = "megaraid_sas",
2988 .slave_configure = megasas_slave_configure,
2989 .slave_alloc = megasas_slave_alloc,
2990 .slave_destroy = megasas_slave_destroy,
2991 .queuecommand = megasas_queue_command,
2992 .eh_target_reset_handler = megasas_reset_target,
2993 .eh_abort_handler = megasas_task_abort,
2994 .eh_host_reset_handler = megasas_reset_bus_host,
2995 .eh_timed_out = megasas_reset_timer,
2996 .shost_attrs = megaraid_host_attrs,
2997 .bios_param = megasas_bios_param,
2998 .use_clustering = ENABLE_CLUSTERING,
2999 .change_queue_depth = scsi_change_queue_depth,
3004 * megasas_complete_int_cmd - Completes an internal command
3005 * @instance: Adapter soft state
3006 * @cmd: Command to be completed
3008 * The megasas_issue_blocked_cmd() function waits for a command to complete
3009 * after it issues a command. This function wakes up that waiting routine by
3010 * calling wake_up() on the wait queue.
3013 megasas_complete_int_cmd(struct megasas_instance *instance,
3014 struct megasas_cmd *cmd)
3016 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
3017 wake_up(&instance->int_cmd_wait_q);
3021 * megasas_complete_abort - Completes aborting a command
3022 * @instance: Adapter soft state
3023 * @cmd: Cmd that was issued to abort another cmd
3025 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3026 * after it issues an abort on a previously issued command. This function
3027 * wakes up all functions waiting on the same wait queue.
3030 megasas_complete_abort(struct megasas_instance *instance,
3031 struct megasas_cmd *cmd)
3033 if (cmd->sync_cmd) {
3035 cmd->cmd_status_drv = 0;
3036 wake_up(&instance->abort_cmd_wait_q);
3041 * megasas_complete_cmd - Completes a command
3042 * @instance: Adapter soft state
3043 * @cmd: Command to be completed
3044 * @alt_status: If non-zero, use this value as status to
3045 * SCSI mid-layer instead of the value returned
3046 * by the FW. This should be used if caller wants
3047 * an alternate status (as in the case of aborted
3051 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3055 struct megasas_header *hdr = &cmd->frame->hdr;
3056 unsigned long flags;
3057 struct fusion_context *fusion = instance->ctrl_context;
3060 /* flag for the retry reset */
3061 cmd->retry_for_fw_reset = 0;
3064 cmd->scmd->SCp.ptr = NULL;
3067 case MFI_CMD_INVALID:
3068 /* Some older 1068 controller FW may keep a pended
3069 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3070 when booting the kdump kernel. Ignore this command to
3071 prevent a kernel panic on shutdown of the kdump kernel. */
3072 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3074 dev_warn(&instance->pdev->dev, "If you have a controller "
3075 "other than PERC5, please upgrade your firmware\n");
3077 case MFI_CMD_PD_SCSI_IO:
3078 case MFI_CMD_LD_SCSI_IO:
3081 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3082 * issued either through an IO path or an IOCTL path. If it
3083 * was via IOCTL, we will send it to internal completion.
3085 if (cmd->sync_cmd) {
3087 megasas_complete_int_cmd(instance, cmd);
3091 case MFI_CMD_LD_READ:
3092 case MFI_CMD_LD_WRITE:
3095 cmd->scmd->result = alt_status << 16;
3101 atomic_dec(&instance->fw_outstanding);
3103 scsi_dma_unmap(cmd->scmd);
3104 cmd->scmd->scsi_done(cmd->scmd);
3105 megasas_return_cmd(instance, cmd);
3110 switch (hdr->cmd_status) {
3113 cmd->scmd->result = DID_OK << 16;
3116 case MFI_STAT_SCSI_IO_FAILED:
3117 case MFI_STAT_LD_INIT_IN_PROGRESS:
3119 (DID_ERROR << 16) | hdr->scsi_status;
3122 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3124 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3126 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3127 memset(cmd->scmd->sense_buffer, 0,
3128 SCSI_SENSE_BUFFERSIZE);
3129 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3132 cmd->scmd->result |= DRIVER_SENSE << 24;
3137 case MFI_STAT_LD_OFFLINE:
3138 case MFI_STAT_DEVICE_NOT_FOUND:
3139 cmd->scmd->result = DID_BAD_TARGET << 16;
3143 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3145 cmd->scmd->result = DID_ERROR << 16;
3149 atomic_dec(&instance->fw_outstanding);
3151 scsi_dma_unmap(cmd->scmd);
3152 cmd->scmd->scsi_done(cmd->scmd);
3153 megasas_return_cmd(instance, cmd);
3160 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3161 /* Check for LD map update */
3162 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3163 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3164 fusion->fast_path_io = 0;
3165 spin_lock_irqsave(instance->host->host_lock, flags);
3166 instance->map_update_cmd = NULL;
3167 if (cmd->frame->hdr.cmd_status != 0) {
3168 if (cmd->frame->hdr.cmd_status !=
3170 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3171 cmd->frame->hdr.cmd_status);
3173 megasas_return_cmd(instance, cmd);
3174 spin_unlock_irqrestore(
3175 instance->host->host_lock,
3181 megasas_return_cmd(instance, cmd);
3184 * Set fast path IO to ZERO.
3185 * Validate Map will set proper value.
3186 * Meanwhile all IOs will go as LD IO.
3188 if (MR_ValidateMapInfo(instance))
3189 fusion->fast_path_io = 1;
3191 fusion->fast_path_io = 0;
3192 megasas_sync_map_info(instance);
3193 spin_unlock_irqrestore(instance->host->host_lock,
3197 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3198 opcode == MR_DCMD_CTRL_EVENT_GET) {
3199 spin_lock_irqsave(&poll_aen_lock, flags);
3200 megasas_poll_wait_aen = 0;
3201 spin_unlock_irqrestore(&poll_aen_lock, flags);
3204 /* FW has an updated PD sequence */
3205 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3206 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3208 spin_lock_irqsave(instance->host->host_lock, flags);
3209 status = cmd->frame->hdr.cmd_status;
3210 instance->jbod_seq_cmd = NULL;
3211 megasas_return_cmd(instance, cmd);
3213 if (status == MFI_STAT_OK) {
3214 instance->pd_seq_map_id++;
3215 /* Re-register a pd sync seq num cmd */
3216 if (megasas_sync_pd_seq_num(instance, true))
3217 instance->use_seqnum_jbod_fp = false;
3219 instance->use_seqnum_jbod_fp = false;
3221 spin_unlock_irqrestore(instance->host->host_lock, flags);
3226 * See if got an event notification
3228 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3229 megasas_service_aen(instance, cmd);
3231 megasas_complete_int_cmd(instance, cmd);
3237 * Cmd issued to abort another cmd returned
3239 megasas_complete_abort(instance, cmd);
3243 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3250 * megasas_issue_pending_cmds_again - issue all pending cmds
3251 * in FW again because of the fw reset
3252 * @instance: Adapter soft state
3255 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3257 struct megasas_cmd *cmd;
3258 struct list_head clist_local;
3259 union megasas_evt_class_locale class_locale;
3260 unsigned long flags;
3263 INIT_LIST_HEAD(&clist_local);
3264 spin_lock_irqsave(&instance->hba_lock, flags);
3265 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3266 spin_unlock_irqrestore(&instance->hba_lock, flags);
3268 while (!list_empty(&clist_local)) {
3269 cmd = list_entry((&clist_local)->next,
3270 struct megasas_cmd, list);
3271 list_del_init(&cmd->list);
3273 if (cmd->sync_cmd || cmd->scmd) {
3274 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3275 "detected to be pending while HBA reset\n",
3276 cmd, cmd->scmd, cmd->sync_cmd);
3278 cmd->retry_for_fw_reset++;
3280 if (cmd->retry_for_fw_reset == 3) {
3281 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3282 "was tried multiple times during reset."
3283 "Shutting down the HBA\n",
3284 cmd, cmd->scmd, cmd->sync_cmd);
3285 instance->instancet->disable_intr(instance);
3286 atomic_set(&instance->fw_reset_no_pci_access, 1);
3287 megaraid_sas_kill_hba(instance);
3292 if (cmd->sync_cmd == 1) {
3294 dev_notice(&instance->pdev->dev, "unexpected"
3295 "cmd attached to internal command!\n");
3297 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3298 "on the internal reset queue,"
3299 "issue it again.\n", cmd);
3300 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3301 instance->instancet->fire_cmd(instance,
3302 cmd->frame_phys_addr,
3303 0, instance->reg_set);
3304 } else if (cmd->scmd) {
3305 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3306 "detected on the internal queue, issue again.\n",
3307 cmd, cmd->scmd->cmnd[0]);
3309 atomic_inc(&instance->fw_outstanding);
3310 instance->instancet->fire_cmd(instance,
3311 cmd->frame_phys_addr,
3312 cmd->frame_count-1, instance->reg_set);
3314 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3315 "internal reset defer list while re-issue!!\n",
3320 if (instance->aen_cmd) {
3321 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3322 megasas_return_cmd(instance, instance->aen_cmd);
3324 instance->aen_cmd = NULL;
3328 * Initiate AEN (Asynchronous Event Notification)
3330 seq_num = instance->last_seq_num;
3331 class_locale.members.reserved = 0;
3332 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3333 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3335 megasas_register_aen(instance, seq_num, class_locale.word);
3339 * Move the internal reset pending commands to a deferred queue.
3341 * We move the commands pending at internal reset time to a
3342 * pending queue. This queue would be flushed after successful
3343 * completion of the internal reset sequence. if the internal reset
3344 * did not complete in time, the kernel reset handler would flush
3348 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3350 struct megasas_cmd *cmd;
3352 u32 max_cmd = instance->max_fw_cmds;
3354 unsigned long flags;
3357 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3358 for (i = 0; i < max_cmd; i++) {
3359 cmd = instance->cmd_list[i];
3360 if (cmd->sync_cmd == 1 || cmd->scmd) {
3361 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3362 "on the defer queue as internal\n",
3363 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3365 if (!list_empty(&cmd->list)) {
3366 dev_notice(&instance->pdev->dev, "ERROR while"
3367 " moving this cmd:%p, %d %p, it was"
3368 "discovered on some list?\n",
3369 cmd, cmd->sync_cmd, cmd->scmd);
3371 list_del_init(&cmd->list);
3374 list_add_tail(&cmd->list,
3375 &instance->internal_reset_pending_q);
3378 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3383 process_fw_state_change_wq(struct work_struct *work)
3385 struct megasas_instance *instance =
3386 container_of(work, struct megasas_instance, work_init);
3388 unsigned long flags;
3390 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3391 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3392 atomic_read(&instance->adprecovery));
3396 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3397 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3398 "state, restarting it...\n");
3400 instance->instancet->disable_intr(instance);
3401 atomic_set(&instance->fw_outstanding, 0);
3403 atomic_set(&instance->fw_reset_no_pci_access, 1);
3404 instance->instancet->adp_reset(instance, instance->reg_set);
3405 atomic_set(&instance->fw_reset_no_pci_access, 0);
3407 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3408 "initiating next stage...\n");
3410 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3411 "state 2 starting...\n");
3413 /* waiting for about 20 second before start the second init */
3414 for (wait = 0; wait < 30; wait++) {
3418 if (megasas_transition_to_ready(instance, 1)) {
3419 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3421 atomic_set(&instance->fw_reset_no_pci_access, 1);
3422 megaraid_sas_kill_hba(instance);
3426 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3427 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3428 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3430 *instance->consumer = *instance->producer;
3432 *instance->consumer = 0;
3433 *instance->producer = 0;
3436 megasas_issue_init_mfi(instance);
3438 spin_lock_irqsave(&instance->hba_lock, flags);
3439 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3440 spin_unlock_irqrestore(&instance->hba_lock, flags);
3441 instance->instancet->enable_intr(instance);
3443 megasas_issue_pending_cmds_again(instance);
3444 instance->issuepend_done = 1;
3449 * megasas_deplete_reply_queue - Processes all completed commands
3450 * @instance: Adapter soft state
3451 * @alt_status: Alternate status to be returned to
3452 * SCSI mid-layer instead of the status
3453 * returned by the FW
3454 * Note: this must be called with hba lock held
3457 megasas_deplete_reply_queue(struct megasas_instance *instance,
3463 if ((mfiStatus = instance->instancet->check_reset(instance,
3464 instance->reg_set)) == 1) {
3468 if ((mfiStatus = instance->instancet->clear_intr(
3471 /* Hardware may not set outbound_intr_status in MSI-X mode */
3472 if (!instance->msix_vectors)
3476 instance->mfiStatus = mfiStatus;
3478 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3479 fw_state = instance->instancet->read_fw_status_reg(
3480 instance->reg_set) & MFI_STATE_MASK;
3482 if (fw_state != MFI_STATE_FAULT) {
3483 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3487 if ((fw_state == MFI_STATE_FAULT) &&
3488 (instance->disableOnlineCtrlReset == 0)) {
3489 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3491 if ((instance->pdev->device ==
3492 PCI_DEVICE_ID_LSI_SAS1064R) ||
3493 (instance->pdev->device ==
3494 PCI_DEVICE_ID_DELL_PERC5) ||
3495 (instance->pdev->device ==
3496 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3498 *instance->consumer =
3499 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3503 instance->instancet->disable_intr(instance);
3504 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
3505 instance->issuepend_done = 0;
3507 atomic_set(&instance->fw_outstanding, 0);
3508 megasas_internal_reset_defer_cmds(instance);
3510 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3511 fw_state, atomic_read(&instance->adprecovery));
3513 schedule_work(&instance->work_init);
3517 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3518 fw_state, instance->disableOnlineCtrlReset);
3522 tasklet_schedule(&instance->isr_tasklet);
3526 * megasas_isr - isr entry point
3528 static irqreturn_t megasas_isr(int irq, void *devp)
3530 struct megasas_irq_context *irq_context = devp;
3531 struct megasas_instance *instance = irq_context->instance;
3532 unsigned long flags;
3535 if (atomic_read(&instance->fw_reset_no_pci_access))
3538 spin_lock_irqsave(&instance->hba_lock, flags);
3539 rc = megasas_deplete_reply_queue(instance, DID_OK);
3540 spin_unlock_irqrestore(&instance->hba_lock, flags);
3546 * megasas_transition_to_ready - Move the FW to READY state
3547 * @instance: Adapter soft state
3549 * During the initialization, FW passes can potentially be in any one of
3550 * several possible states. If the FW in operational, waiting-for-handshake
3551 * states, driver must take steps to bring it to ready state. Otherwise, it
3552 * has to wait for the ready state.
3555 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3561 u32 abs_state, curr_abs_state;
3563 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3564 fw_state = abs_state & MFI_STATE_MASK;
3566 if (fw_state != MFI_STATE_READY)
3567 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3570 while (fw_state != MFI_STATE_READY) {
3574 case MFI_STATE_FAULT:
3575 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3577 max_wait = MEGASAS_RESET_WAIT_TIME;
3578 cur_state = MFI_STATE_FAULT;
3583 case MFI_STATE_WAIT_HANDSHAKE:
3585 * Set the CLR bit in inbound doorbell
3587 if ((instance->pdev->device ==
3588 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3589 (instance->pdev->device ==
3590 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3591 (instance->ctrl_context))
3593 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3594 &instance->reg_set->doorbell);
3597 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3598 &instance->reg_set->inbound_doorbell);
3600 max_wait = MEGASAS_RESET_WAIT_TIME;
3601 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3604 case MFI_STATE_BOOT_MESSAGE_PENDING:
3605 if ((instance->pdev->device ==
3606 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3607 (instance->pdev->device ==
3608 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3609 (instance->ctrl_context))
3610 writel(MFI_INIT_HOTPLUG,
3611 &instance->reg_set->doorbell);
3613 writel(MFI_INIT_HOTPLUG,
3614 &instance->reg_set->inbound_doorbell);
3616 max_wait = MEGASAS_RESET_WAIT_TIME;
3617 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3620 case MFI_STATE_OPERATIONAL:
3622 * Bring it to READY state; assuming max wait 10 secs
3624 instance->instancet->disable_intr(instance);
3625 if ((instance->pdev->device ==
3626 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3627 (instance->pdev->device ==
3628 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3629 (instance->ctrl_context)) {
3630 writel(MFI_RESET_FLAGS,
3631 &instance->reg_set->doorbell);
3633 if (instance->ctrl_context) {
3634 for (i = 0; i < (10 * 1000); i += 20) {
3645 writel(MFI_RESET_FLAGS,
3646 &instance->reg_set->inbound_doorbell);
3648 max_wait = MEGASAS_RESET_WAIT_TIME;
3649 cur_state = MFI_STATE_OPERATIONAL;
3652 case MFI_STATE_UNDEFINED:
3654 * This state should not last for more than 2 seconds
3656 max_wait = MEGASAS_RESET_WAIT_TIME;
3657 cur_state = MFI_STATE_UNDEFINED;
3660 case MFI_STATE_BB_INIT:
3661 max_wait = MEGASAS_RESET_WAIT_TIME;
3662 cur_state = MFI_STATE_BB_INIT;
3665 case MFI_STATE_FW_INIT:
3666 max_wait = MEGASAS_RESET_WAIT_TIME;
3667 cur_state = MFI_STATE_FW_INIT;
3670 case MFI_STATE_FW_INIT_2:
3671 max_wait = MEGASAS_RESET_WAIT_TIME;
3672 cur_state = MFI_STATE_FW_INIT_2;
3675 case MFI_STATE_DEVICE_SCAN:
3676 max_wait = MEGASAS_RESET_WAIT_TIME;
3677 cur_state = MFI_STATE_DEVICE_SCAN;
3680 case MFI_STATE_FLUSH_CACHE:
3681 max_wait = MEGASAS_RESET_WAIT_TIME;
3682 cur_state = MFI_STATE_FLUSH_CACHE;
3686 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3692 * The cur_state should not last for more than max_wait secs
3694 for (i = 0; i < (max_wait * 1000); i++) {
3695 curr_abs_state = instance->instancet->
3696 read_fw_status_reg(instance->reg_set);
3698 if (abs_state == curr_abs_state) {
3705 * Return error if fw_state hasn't changed after max_wait
3707 if (curr_abs_state == abs_state) {
3708 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3709 "in %d secs\n", fw_state, max_wait);
3713 abs_state = curr_abs_state;
3714 fw_state = curr_abs_state & MFI_STATE_MASK;
3716 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3722 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3723 * @instance: Adapter soft state
3725 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3728 u32 max_cmd = instance->max_mfi_cmds;
3729 struct megasas_cmd *cmd;
3731 if (!instance->frame_dma_pool)
3735 * Return all frames to pool
3737 for (i = 0; i < max_cmd; i++) {
3739 cmd = instance->cmd_list[i];
3742 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3743 cmd->frame_phys_addr);
3746 pci_pool_free(instance->sense_dma_pool, cmd->sense,
3747 cmd->sense_phys_addr);
3751 * Now destroy the pool itself
3753 pci_pool_destroy(instance->frame_dma_pool);
3754 pci_pool_destroy(instance->sense_dma_pool);
3756 instance->frame_dma_pool = NULL;
3757 instance->sense_dma_pool = NULL;
3761 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3762 * @instance: Adapter soft state
3764 * Each command packet has an embedded DMA memory buffer that is used for
3765 * filling MFI frame and the SG list that immediately follows the frame. This
3766 * function creates those DMA memory buffers for each command packet by using
3767 * PCI pool facility.
3769 static int megasas_create_frame_pool(struct megasas_instance *instance)
3776 struct megasas_cmd *cmd;
3778 max_cmd = instance->max_mfi_cmds;
3781 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3782 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3784 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3785 sizeof(struct megasas_sge32);
3787 if (instance->flag_ieee)
3788 sge_sz = sizeof(struct megasas_sge_skinny);
3791 * For MFI controllers.
3793 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3794 * Total 960 byte (15 MFI frame of 64 byte)
3796 * Fusion adapter require only 3 extra frame.
3797 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3798 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3799 * Total 192 byte (3 MFI frame of 64 byte)
3801 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
3802 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3804 * Use DMA pool facility provided by PCI layer
3806 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
3807 instance->pdev, total_sz, 256, 0);
3809 if (!instance->frame_dma_pool) {
3810 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
3814 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3815 instance->pdev, 128, 4, 0);
3817 if (!instance->sense_dma_pool) {
3818 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
3820 pci_pool_destroy(instance->frame_dma_pool);
3821 instance->frame_dma_pool = NULL;
3827 * Allocate and attach a frame to each of the commands in cmd_list.
3828 * By making cmd->index as the context instead of the &cmd, we can
3829 * always use 32bit context regardless of the architecture
3831 for (i = 0; i < max_cmd; i++) {
3833 cmd = instance->cmd_list[i];
3835 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3836 GFP_KERNEL, &cmd->frame_phys_addr);
3838 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3839 GFP_KERNEL, &cmd->sense_phys_addr);
3842 * megasas_teardown_frame_pool() takes care of freeing
3843 * whatever has been allocated
3845 if (!cmd->frame || !cmd->sense) {
3846 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
3847 megasas_teardown_frame_pool(instance);
3851 memset(cmd->frame, 0, total_sz);
3852 cmd->frame->io.context = cpu_to_le32(cmd->index);
3853 cmd->frame->io.pad_0 = 0;
3854 if (!instance->ctrl_context && reset_devices)
3855 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
3862 * megasas_free_cmds - Free all the cmds in the free cmd pool
3863 * @instance: Adapter soft state
3865 void megasas_free_cmds(struct megasas_instance *instance)
3869 /* First free the MFI frame pool */
3870 megasas_teardown_frame_pool(instance);
3872 /* Free all the commands in the cmd_list */
3873 for (i = 0; i < instance->max_mfi_cmds; i++)
3875 kfree(instance->cmd_list[i]);
3877 /* Free the cmd_list buffer itself */
3878 kfree(instance->cmd_list);
3879 instance->cmd_list = NULL;
3881 INIT_LIST_HEAD(&instance->cmd_pool);
3885 * megasas_alloc_cmds - Allocates the command packets
3886 * @instance: Adapter soft state
3888 * Each command that is issued to the FW, whether IO commands from the OS or
3889 * internal commands like IOCTLs, are wrapped in local data structure called
3890 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3893 * Each frame has a 32-bit field called context (tag). This context is used
3894 * to get back the megasas_cmd from the frame when a frame gets completed in
3895 * the ISR. Typically the address of the megasas_cmd itself would be used as
3896 * the context. But we wanted to keep the differences between 32 and 64 bit
3897 * systems to the mininum. We always use 32 bit integers for the context. In
3898 * this driver, the 32 bit values are the indices into an array cmd_list.
3899 * This array is used only to look up the megasas_cmd given the context. The
3900 * free commands themselves are maintained in a linked list called cmd_pool.
3902 int megasas_alloc_cmds(struct megasas_instance *instance)
3907 struct megasas_cmd *cmd;
3908 struct fusion_context *fusion;
3910 fusion = instance->ctrl_context;
3911 max_cmd = instance->max_mfi_cmds;
3914 * instance->cmd_list is an array of struct megasas_cmd pointers.
3915 * Allocate the dynamic array first and then allocate individual
3918 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3920 if (!instance->cmd_list) {
3921 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
3925 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3927 for (i = 0; i < max_cmd; i++) {
3928 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3931 if (!instance->cmd_list[i]) {
3933 for (j = 0; j < i; j++)
3934 kfree(instance->cmd_list[j]);
3936 kfree(instance->cmd_list);
3937 instance->cmd_list = NULL;
3943 for (i = 0; i < max_cmd; i++) {
3944 cmd = instance->cmd_list[i];
3945 memset(cmd, 0, sizeof(struct megasas_cmd));
3948 cmd->instance = instance;
3950 list_add_tail(&cmd->list, &instance->cmd_pool);
3954 * Create a frame pool and assign one frame to each cmd
3956 if (megasas_create_frame_pool(instance)) {
3957 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
3958 megasas_free_cmds(instance);
3965 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
3966 * @instance: Adapter soft state
3968 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
3969 * or FW is not under OCR.
3972 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
3974 if (!instance->ctrl_context)
3975 return KILL_ADAPTER;
3976 else if (instance->unload ||
3977 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
3978 return IGNORE_TIMEOUT;
3980 return INITIATE_OCR;
3984 megasas_get_pd_info(struct megasas_instance *instance, u16 device_id)
3987 struct megasas_cmd *cmd;
3988 struct megasas_dcmd_frame *dcmd;
3990 cmd = megasas_get_cmd(instance);
3993 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
3997 dcmd = &cmd->frame->dcmd;
3999 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4000 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4002 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4003 dcmd->cmd = MFI_CMD_DCMD;
4004 dcmd->cmd_status = 0xFF;
4005 dcmd->sge_count = 1;
4006 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4009 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4010 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4011 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
4012 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
4014 if (instance->ctrl_context && !instance->mask_interrupts)
4015 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4017 ret = megasas_issue_polled(instance, cmd);
4021 instance->pd_list[device_id].interface =
4022 instance->pd_info->state.ddf.pdType.intf;
4027 switch (dcmd_timeout_ocr_possible(instance)) {
4029 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4030 megasas_reset_fusion(instance->host,
4031 MFI_IO_TIMEOUT_OCR);
4034 megaraid_sas_kill_hba(instance);
4036 case IGNORE_TIMEOUT:
4037 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4038 __func__, __LINE__);
4045 if (ret != DCMD_TIMEOUT)
4046 megasas_return_cmd(instance, cmd);
4051 * megasas_get_pd_list_info - Returns FW's pd_list structure
4052 * @instance: Adapter soft state
4053 * @pd_list: pd_list structure
4055 * Issues an internal command (DCMD) to get the FW's controller PD
4056 * list structure. This information is mainly used to find out SYSTEM
4057 * supported by the FW.
4060 megasas_get_pd_list(struct megasas_instance *instance)
4062 int ret = 0, pd_index = 0;
4063 struct megasas_cmd *cmd;
4064 struct megasas_dcmd_frame *dcmd;
4065 struct MR_PD_LIST *ci;
4066 struct MR_PD_ADDRESS *pd_addr;
4067 dma_addr_t ci_h = 0;
4069 if (instance->pd_list_not_supported) {
4070 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4071 "not supported by firmware\n");
4075 cmd = megasas_get_cmd(instance);
4078 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4082 dcmd = &cmd->frame->dcmd;
4084 ci = pci_alloc_consistent(instance->pdev,
4085 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
4088 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
4089 megasas_return_cmd(instance, cmd);
4093 memset(ci, 0, sizeof(*ci));
4094 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4096 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4097 dcmd->mbox.b[1] = 0;
4098 dcmd->cmd = MFI_CMD_DCMD;
4099 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4100 dcmd->sge_count = 1;
4101 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4104 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4105 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4106 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4107 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4109 if (instance->ctrl_context && !instance->mask_interrupts)
4110 ret = megasas_issue_blocked_cmd(instance, cmd,
4111 MFI_IO_TIMEOUT_SECS);
4113 ret = megasas_issue_polled(instance, cmd);
4117 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4118 "failed/not supported by firmware\n");
4120 if (instance->ctrl_context)
4121 megaraid_sas_kill_hba(instance);
4123 instance->pd_list_not_supported = 1;
4127 switch (dcmd_timeout_ocr_possible(instance)) {
4129 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4131 * DCMD failed from AEN path.
4132 * AEN path already hold reset_mutex to avoid PCI access
4133 * while OCR is in progress.
4135 mutex_unlock(&instance->reset_mutex);
4136 megasas_reset_fusion(instance->host,
4137 MFI_IO_TIMEOUT_OCR);
4138 mutex_lock(&instance->reset_mutex);
4141 megaraid_sas_kill_hba(instance);
4143 case IGNORE_TIMEOUT:
4144 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4145 __func__, __LINE__);
4154 if ((le32_to_cpu(ci->count) >
4155 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4158 memset(instance->local_pd_list, 0,
4159 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4161 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4162 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
4163 le16_to_cpu(pd_addr->deviceId);
4164 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
4165 pd_addr->scsiDevType;
4166 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
4171 memcpy(instance->pd_list, instance->local_pd_list,
4172 sizeof(instance->pd_list));
4177 pci_free_consistent(instance->pdev,
4178 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4181 if (ret != DCMD_TIMEOUT)
4182 megasas_return_cmd(instance, cmd);
4188 * megasas_get_ld_list_info - Returns FW's ld_list structure
4189 * @instance: Adapter soft state
4190 * @ld_list: ld_list structure
4192 * Issues an internal command (DCMD) to get the FW's controller PD
4193 * list structure. This information is mainly used to find out SYSTEM
4194 * supported by the FW.
4197 megasas_get_ld_list(struct megasas_instance *instance)
4199 int ret = 0, ld_index = 0, ids = 0;
4200 struct megasas_cmd *cmd;
4201 struct megasas_dcmd_frame *dcmd;
4202 struct MR_LD_LIST *ci;
4203 dma_addr_t ci_h = 0;
4206 cmd = megasas_get_cmd(instance);
4209 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4213 dcmd = &cmd->frame->dcmd;
4215 ci = pci_alloc_consistent(instance->pdev,
4216 sizeof(struct MR_LD_LIST),
4220 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
4221 megasas_return_cmd(instance, cmd);
4225 memset(ci, 0, sizeof(*ci));
4226 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4228 if (instance->supportmax256vd)
4229 dcmd->mbox.b[0] = 1;
4230 dcmd->cmd = MFI_CMD_DCMD;
4231 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4232 dcmd->sge_count = 1;
4233 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4235 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4236 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4237 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4238 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
4241 if (instance->ctrl_context && !instance->mask_interrupts)
4242 ret = megasas_issue_blocked_cmd(instance, cmd,
4243 MFI_IO_TIMEOUT_SECS);
4245 ret = megasas_issue_polled(instance, cmd);
4247 ld_count = le32_to_cpu(ci->ldCount);
4251 megaraid_sas_kill_hba(instance);
4255 switch (dcmd_timeout_ocr_possible(instance)) {
4257 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4259 * DCMD failed from AEN path.
4260 * AEN path already hold reset_mutex to avoid PCI access
4261 * while OCR is in progress.
4263 mutex_unlock(&instance->reset_mutex);
4264 megasas_reset_fusion(instance->host,
4265 MFI_IO_TIMEOUT_OCR);
4266 mutex_lock(&instance->reset_mutex);
4269 megaraid_sas_kill_hba(instance);
4271 case IGNORE_TIMEOUT:
4272 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4273 __func__, __LINE__);
4280 if (ld_count > instance->fw_supported_vd_count)
4283 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4285 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4286 if (ci->ldList[ld_index].state != 0) {
4287 ids = ci->ldList[ld_index].ref.targetId;
4288 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4295 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
4297 if (ret != DCMD_TIMEOUT)
4298 megasas_return_cmd(instance, cmd);
4304 * megasas_ld_list_query - Returns FW's ld_list structure
4305 * @instance: Adapter soft state
4306 * @ld_list: ld_list structure
4308 * Issues an internal command (DCMD) to get the FW's controller PD
4309 * list structure. This information is mainly used to find out SYSTEM
4310 * supported by the FW.
4313 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4315 int ret = 0, ld_index = 0, ids = 0;
4316 struct megasas_cmd *cmd;
4317 struct megasas_dcmd_frame *dcmd;
4318 struct MR_LD_TARGETID_LIST *ci;
4319 dma_addr_t ci_h = 0;
4322 cmd = megasas_get_cmd(instance);
4325 dev_warn(&instance->pdev->dev,
4326 "megasas_ld_list_query: Failed to get cmd\n");
4330 dcmd = &cmd->frame->dcmd;
4332 ci = pci_alloc_consistent(instance->pdev,
4333 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4336 dev_warn(&instance->pdev->dev,
4337 "Failed to alloc mem for ld_list_query\n");
4338 megasas_return_cmd(instance, cmd);
4342 memset(ci, 0, sizeof(*ci));
4343 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4345 dcmd->mbox.b[0] = query_type;
4346 if (instance->supportmax256vd)
4347 dcmd->mbox.b[2] = 1;
4349 dcmd->cmd = MFI_CMD_DCMD;
4350 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4351 dcmd->sge_count = 1;
4352 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4354 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4355 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4356 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4357 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4360 if (instance->ctrl_context && !instance->mask_interrupts)
4361 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4363 ret = megasas_issue_polled(instance, cmd);
4367 dev_info(&instance->pdev->dev,
4368 "DCMD not supported by firmware - %s %d\n",
4369 __func__, __LINE__);
4370 ret = megasas_get_ld_list(instance);
4373 switch (dcmd_timeout_ocr_possible(instance)) {
4375 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4377 * DCMD failed from AEN path.
4378 * AEN path already hold reset_mutex to avoid PCI access
4379 * while OCR is in progress.
4381 mutex_unlock(&instance->reset_mutex);
4382 megasas_reset_fusion(instance->host,
4383 MFI_IO_TIMEOUT_OCR);
4384 mutex_lock(&instance->reset_mutex);
4387 megaraid_sas_kill_hba(instance);
4389 case IGNORE_TIMEOUT:
4390 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4391 __func__, __LINE__);
4397 tgtid_count = le32_to_cpu(ci->count);
4399 if ((tgtid_count > (instance->fw_supported_vd_count)))
4402 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4403 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4404 ids = ci->targetId[ld_index];
4405 instance->ld_ids[ids] = ci->targetId[ld_index];
4411 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4414 if (ret != DCMD_TIMEOUT)
4415 megasas_return_cmd(instance, cmd);
4421 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4422 * instance : Controller's instance
4424 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4426 struct fusion_context *fusion;
4430 fusion = instance->ctrl_context;
4431 /* For MFI based controllers return dummy success */
4435 instance->supportmax256vd =
4436 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4437 /* Below is additional check to address future FW enhancement */
4438 if (instance->ctrl_info->max_lds > 64)
4439 instance->supportmax256vd = 1;
4441 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4442 * MEGASAS_MAX_DEV_PER_CHANNEL;
4443 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4444 * MEGASAS_MAX_DEV_PER_CHANNEL;
4445 if (instance->supportmax256vd) {
4446 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4447 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4449 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4450 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4453 dev_info(&instance->pdev->dev,
4454 "firmware type\t: %s\n",
4455 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4456 "Legacy(64 VD) firmware");
4458 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4459 (sizeof(struct MR_LD_SPAN_MAP) *
4460 (instance->fw_supported_vd_count - 1));
4461 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4462 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
4463 (sizeof(struct MR_LD_SPAN_MAP) *
4464 (instance->drv_supported_vd_count - 1));
4466 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4469 if (instance->supportmax256vd)
4470 fusion->current_map_sz = new_map_sz;
4472 fusion->current_map_sz = old_map_sz;
4476 * megasas_get_controller_info - Returns FW's controller structure
4477 * @instance: Adapter soft state
4479 * Issues an internal command (DCMD) to get the FW's controller structure.
4480 * This information is mainly used to find out the maximum IO transfer per
4481 * command supported by the FW.
4484 megasas_get_ctrl_info(struct megasas_instance *instance)
4487 struct megasas_cmd *cmd;
4488 struct megasas_dcmd_frame *dcmd;
4489 struct megasas_ctrl_info *ci;
4490 struct megasas_ctrl_info *ctrl_info;
4491 dma_addr_t ci_h = 0;
4493 ctrl_info = instance->ctrl_info;
4495 cmd = megasas_get_cmd(instance);
4498 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4502 dcmd = &cmd->frame->dcmd;
4504 ci = pci_alloc_consistent(instance->pdev,
4505 sizeof(struct megasas_ctrl_info), &ci_h);
4508 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
4509 megasas_return_cmd(instance, cmd);
4513 memset(ci, 0, sizeof(*ci));
4514 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4516 dcmd->cmd = MFI_CMD_DCMD;
4517 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4518 dcmd->sge_count = 1;
4519 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4522 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4523 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4524 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4525 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4526 dcmd->mbox.b[0] = 1;
4528 if (instance->ctrl_context && !instance->mask_interrupts)
4529 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4531 ret = megasas_issue_polled(instance, cmd);
4535 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
4536 /* Save required controller information in
4537 * CPU endianness format.
4539 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4540 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4541 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4543 /* Update the latest Ext VD info.
4544 * From Init path, store current firmware details.
4545 * From OCR path, detect any firmware properties changes.
4546 * in case of Firmware upgrade without system reboot.
4548 megasas_update_ext_vd_details(instance);
4549 instance->use_seqnum_jbod_fp =
4550 ctrl_info->adapterOperations3.useSeqNumJbodFP;
4552 /*Check whether controller is iMR or MR */
4553 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4554 dev_info(&instance->pdev->dev,
4555 "controller type\t: %s(%dMB)\n",
4556 instance->is_imr ? "iMR" : "MR",
4557 le16_to_cpu(ctrl_info->memory_size));
4559 instance->disableOnlineCtrlReset =
4560 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4561 instance->secure_jbod_support =
4562 ctrl_info->adapterOperations3.supportSecurityonJBOD;
4563 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4564 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
4565 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4566 instance->secure_jbod_support ? "Yes" : "No");
4570 switch (dcmd_timeout_ocr_possible(instance)) {
4572 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4573 megasas_reset_fusion(instance->host,
4574 MFI_IO_TIMEOUT_OCR);
4577 megaraid_sas_kill_hba(instance);
4579 case IGNORE_TIMEOUT:
4580 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4581 __func__, __LINE__);
4585 megaraid_sas_kill_hba(instance);
4590 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4593 megasas_return_cmd(instance, cmd);
4600 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4603 * @instance: Adapter soft state
4604 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4605 MR_CRASH_BUF_TURN_OFF = 0
4606 MR_CRASH_BUF_TURN_ON = 1
4607 * @return 0 on success non-zero on failure.
4608 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4609 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4610 * that driver supports crash dump feature. This DCMD will be sent only if
4611 * crash dump feature is supported by the FW.
4614 int megasas_set_crash_dump_params(struct megasas_instance *instance,
4618 struct megasas_cmd *cmd;
4619 struct megasas_dcmd_frame *dcmd;
4621 cmd = megasas_get_cmd(instance);
4624 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4629 dcmd = &cmd->frame->dcmd;
4631 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4632 dcmd->mbox.b[0] = crash_buf_state;
4633 dcmd->cmd = MFI_CMD_DCMD;
4634 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4635 dcmd->sge_count = 1;
4636 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4639 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4640 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4641 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4642 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4644 if (instance->ctrl_context && !instance->mask_interrupts)
4645 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4647 ret = megasas_issue_polled(instance, cmd);
4649 if (ret == DCMD_TIMEOUT) {
4650 switch (dcmd_timeout_ocr_possible(instance)) {
4652 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4653 megasas_reset_fusion(instance->host,
4654 MFI_IO_TIMEOUT_OCR);
4657 megaraid_sas_kill_hba(instance);
4659 case IGNORE_TIMEOUT:
4660 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4661 __func__, __LINE__);
4665 megasas_return_cmd(instance, cmd);
4671 * megasas_issue_init_mfi - Initializes the FW
4672 * @instance: Adapter soft state
4674 * Issues the INIT MFI cmd
4677 megasas_issue_init_mfi(struct megasas_instance *instance)
4680 struct megasas_cmd *cmd;
4681 struct megasas_init_frame *init_frame;
4682 struct megasas_init_queue_info *initq_info;
4683 dma_addr_t init_frame_h;
4684 dma_addr_t initq_info_h;
4687 * Prepare a init frame. Note the init frame points to queue info
4688 * structure. Each frame has SGL allocated after first 64 bytes. For
4689 * this frame - since we don't need any SGL - we use SGL's space as
4690 * queue info structure
4692 * We will not get a NULL command below. We just created the pool.
4694 cmd = megasas_get_cmd(instance);
4696 init_frame = (struct megasas_init_frame *)cmd->frame;
4697 initq_info = (struct megasas_init_queue_info *)
4698 ((unsigned long)init_frame + 64);
4700 init_frame_h = cmd->frame_phys_addr;
4701 initq_info_h = init_frame_h + 64;
4703 context = init_frame->context;
4704 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4705 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4706 init_frame->context = context;
4708 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4709 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
4711 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4712 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
4714 init_frame->cmd = MFI_CMD_INIT;
4715 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
4716 init_frame->queue_info_new_phys_addr_lo =
4717 cpu_to_le32(lower_32_bits(initq_info_h));
4718 init_frame->queue_info_new_phys_addr_hi =
4719 cpu_to_le32(upper_32_bits(initq_info_h));
4721 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
4724 * disable the intr before firing the init frame to FW
4726 instance->instancet->disable_intr(instance);
4729 * Issue the init frame in polled mode
4732 if (megasas_issue_polled(instance, cmd)) {
4733 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
4734 megasas_return_cmd(instance, cmd);
4738 megasas_return_cmd(instance, cmd);
4747 megasas_init_adapter_mfi(struct megasas_instance *instance)
4749 struct megasas_register_set __iomem *reg_set;
4753 reg_set = instance->reg_set;
4756 * Get various operational parameters from status register
4758 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
4760 * Reduce the max supported cmds by 1. This is to ensure that the
4761 * reply_q_sz (1 more than the max cmd that driver may send)
4762 * does not exceed max cmds that the FW can support
4764 instance->max_fw_cmds = instance->max_fw_cmds-1;
4765 instance->max_mfi_cmds = instance->max_fw_cmds;
4766 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
4769 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4770 * are reserved for IOCTL + driver's internal DCMDs.
4772 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4773 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4774 instance->max_scsi_cmds = (instance->max_fw_cmds -
4775 MEGASAS_SKINNY_INT_CMDS);
4776 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4778 instance->max_scsi_cmds = (instance->max_fw_cmds -
4780 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4783 instance->cur_can_queue = instance->max_scsi_cmds;
4785 * Create a pool of commands
4787 if (megasas_alloc_cmds(instance))
4788 goto fail_alloc_cmds;
4791 * Allocate memory for reply queue. Length of reply queue should
4792 * be _one_ more than the maximum commands handled by the firmware.
4794 * Note: When FW completes commands, it places corresponding contex
4795 * values in this circular reply queue. This circular queue is a fairly
4796 * typical producer-consumer queue. FW is the producer (of completed
4797 * commands) and the driver is the consumer.
4799 context_sz = sizeof(u32);
4800 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4802 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4804 &instance->reply_queue_h);
4806 if (!instance->reply_queue) {
4807 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
4808 goto fail_reply_queue;
4811 if (megasas_issue_init_mfi(instance))
4814 if (megasas_get_ctrl_info(instance)) {
4815 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4816 "Fail from %s %d\n", instance->unique_id,
4817 __func__, __LINE__);
4821 instance->fw_support_ieee = 0;
4822 instance->fw_support_ieee =
4823 (instance->instancet->read_fw_status_reg(reg_set) &
4826 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
4827 instance->fw_support_ieee);
4829 if (instance->fw_support_ieee)
4830 instance->flag_ieee = 1;
4836 pci_free_consistent(instance->pdev, reply_q_sz,
4837 instance->reply_queue, instance->reply_queue_h);
4839 megasas_free_cmds(instance);
4846 * megasas_setup_irqs_msix - register legacy interrupts.
4847 * @instance: Adapter soft state
4849 * Do not enable interrupt, only setup ISRs.
4851 * Return 0 on success.
4854 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4856 struct pci_dev *pdev;
4858 pdev = instance->pdev;
4859 instance->irq_context[0].instance = instance;
4860 instance->irq_context[0].MSIxIndex = 0;
4861 if (request_irq(pdev->irq, instance->instancet->service_isr,
4862 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4863 dev_err(&instance->pdev->dev,
4864 "Failed to register IRQ from %s %d\n",
4865 __func__, __LINE__);
4872 * megasas_setup_irqs_msix - register MSI-x interrupts.
4873 * @instance: Adapter soft state
4874 * @is_probe: Driver probe check
4876 * Do not enable interrupt, only setup ISRs.
4878 * Return 0 on success.
4881 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4884 struct pci_dev *pdev;
4886 pdev = instance->pdev;
4889 cpu = cpumask_first(cpu_online_mask);
4890 for (i = 0; i < instance->msix_vectors; i++) {
4891 instance->irq_context[i].instance = instance;
4892 instance->irq_context[i].MSIxIndex = i;
4893 if (request_irq(instance->msixentry[i].vector,
4894 instance->instancet->service_isr, 0, "megasas",
4895 &instance->irq_context[i])) {
4896 dev_err(&instance->pdev->dev,
4897 "Failed to register IRQ for vector %d.\n", i);
4898 for (j = 0; j < i; j++) {
4899 if (smp_affinity_enable)
4900 irq_set_affinity_hint(
4901 instance->msixentry[j].vector, NULL);
4902 free_irq(instance->msixentry[j].vector,
4903 &instance->irq_context[j]);
4905 /* Retry irq register for IO_APIC*/
4906 instance->msix_vectors = 0;
4908 return megasas_setup_irqs_ioapic(instance);
4912 if (smp_affinity_enable) {
4913 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4915 dev_err(&instance->pdev->dev,
4916 "Failed to set affinity hint"
4917 " for cpu %d\n", cpu);
4918 cpu = cpumask_next(cpu, cpu_online_mask);
4925 * megasas_destroy_irqs- unregister interrupts.
4926 * @instance: Adapter soft state
4930 megasas_destroy_irqs(struct megasas_instance *instance) {
4934 if (instance->msix_vectors)
4935 for (i = 0; i < instance->msix_vectors; i++) {
4936 if (smp_affinity_enable)
4937 irq_set_affinity_hint(
4938 instance->msixentry[i].vector, NULL);
4939 free_irq(instance->msixentry[i].vector,
4940 &instance->irq_context[i]);
4943 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4947 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
4948 * @instance: Adapter soft state
4949 * @is_probe: Driver probe check
4951 * Return 0 on success.
4954 megasas_setup_jbod_map(struct megasas_instance *instance)
4957 struct fusion_context *fusion = instance->ctrl_context;
4960 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4961 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4963 if (reset_devices || !fusion ||
4964 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4965 dev_info(&instance->pdev->dev,
4966 "Jbod map is not supported %s %d\n",
4967 __func__, __LINE__);
4968 instance->use_seqnum_jbod_fp = false;
4972 if (fusion->pd_seq_sync[0])
4975 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4976 fusion->pd_seq_sync[i] = dma_alloc_coherent
4977 (&instance->pdev->dev, pd_seq_map_sz,
4978 &fusion->pd_seq_phys[i], GFP_KERNEL);
4979 if (!fusion->pd_seq_sync[i]) {
4980 dev_err(&instance->pdev->dev,
4981 "Failed to allocate memory from %s %d\n",
4982 __func__, __LINE__);
4984 dma_free_coherent(&instance->pdev->dev,
4985 pd_seq_map_sz, fusion->pd_seq_sync[0],
4986 fusion->pd_seq_phys[0]);
4987 fusion->pd_seq_sync[0] = NULL;
4989 instance->use_seqnum_jbod_fp = false;
4995 if (!megasas_sync_pd_seq_num(instance, false) &&
4996 !megasas_sync_pd_seq_num(instance, true))
4997 instance->use_seqnum_jbod_fp = true;
4999 instance->use_seqnum_jbod_fp = false;
5003 * megasas_init_fw - Initializes the FW
5004 * @instance: Adapter soft state
5006 * This is the main function for initializing firmware
5009 static int megasas_init_fw(struct megasas_instance *instance)
5013 u32 tmp_sectors, msix_enable, scratch_pad_2;
5014 resource_size_t base_addr;
5015 struct megasas_register_set __iomem *reg_set;
5016 struct megasas_ctrl_info *ctrl_info = NULL;
5017 unsigned long bar_list;
5018 int i, loop, fw_msix_count = 0;
5019 struct IOV_111 *iovPtr;
5020 struct fusion_context *fusion;
5022 fusion = instance->ctrl_context;
5024 /* Find first memory bar */
5025 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
5026 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
5027 if (pci_request_selected_regions(instance->pdev, instance->bar,
5029 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
5033 base_addr = pci_resource_start(instance->pdev, instance->bar);
5034 instance->reg_set = ioremap_nocache(base_addr, 8192);
5036 if (!instance->reg_set) {
5037 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
5041 reg_set = instance->reg_set;
5043 switch (instance->pdev->device) {
5044 case PCI_DEVICE_ID_LSI_FUSION:
5045 case PCI_DEVICE_ID_LSI_PLASMA:
5046 case PCI_DEVICE_ID_LSI_INVADER:
5047 case PCI_DEVICE_ID_LSI_FURY:
5048 case PCI_DEVICE_ID_LSI_INTRUDER:
5049 case PCI_DEVICE_ID_LSI_INTRUDER_24:
5050 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5051 case PCI_DEVICE_ID_LSI_CUTLASS_53:
5052 instance->instancet = &megasas_instance_template_fusion;
5054 case PCI_DEVICE_ID_LSI_SAS1078R:
5055 case PCI_DEVICE_ID_LSI_SAS1078DE:
5056 instance->instancet = &megasas_instance_template_ppc;
5058 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5059 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5060 instance->instancet = &megasas_instance_template_gen2;
5062 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5063 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5064 instance->instancet = &megasas_instance_template_skinny;
5066 case PCI_DEVICE_ID_LSI_SAS1064R:
5067 case PCI_DEVICE_ID_DELL_PERC5:
5069 instance->instancet = &megasas_instance_template_xscale;
5073 if (megasas_transition_to_ready(instance, 0)) {
5074 atomic_set(&instance->fw_reset_no_pci_access, 1);
5075 instance->instancet->adp_reset
5076 (instance, instance->reg_set);
5077 atomic_set(&instance->fw_reset_no_pci_access, 0);
5078 dev_info(&instance->pdev->dev,
5079 "FW restarted successfully from %s!\n",
5082 /*waitting for about 30 second before retry*/
5085 if (megasas_transition_to_ready(instance, 0))
5086 goto fail_ready_state;
5090 * MSI-X host index 0 is common for all adapter.
5091 * It is used for all MPT based Adapters.
5093 instance->reply_post_host_index_addr[0] =
5094 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
5095 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5097 /* Check if MSI-X is supported while in ready state */
5098 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5100 if (msix_enable && !msix_disable) {
5101 scratch_pad_2 = readl
5102 (&instance->reg_set->outbound_scratch_pad_2);
5103 /* Check max MSI-X vectors */
5105 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
5106 instance->msix_vectors = (scratch_pad_2
5107 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5108 fw_msix_count = instance->msix_vectors;
5109 } else { /* Invader series supports more than 8 MSI-x vectors*/
5110 instance->msix_vectors = ((scratch_pad_2
5111 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5112 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
5114 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5116 fw_msix_count = instance->msix_vectors;
5117 /* Save 1-15 reply post index address to local memory
5118 * Index 0 is already saved from reg offset
5119 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5121 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5122 instance->reply_post_host_index_addr[loop] =
5124 ((u8 __iomem *)instance->reg_set +
5125 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5130 instance->msix_vectors = min(msix_vectors,
5131 instance->msix_vectors);
5132 } else /* MFI adapters */
5133 instance->msix_vectors = 1;
5134 /* Don't bother allocating more MSI-X vectors than cpus */
5135 instance->msix_vectors = min(instance->msix_vectors,
5136 (unsigned int)num_online_cpus());
5137 for (i = 0; i < instance->msix_vectors; i++)
5138 instance->msixentry[i].entry = i;
5139 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
5140 1, instance->msix_vectors);
5142 instance->msix_vectors = i;
5144 instance->msix_vectors = 0;
5147 dev_info(&instance->pdev->dev,
5148 "firmware supports msix\t: (%d)", fw_msix_count);
5149 dev_info(&instance->pdev->dev,
5150 "current msix/online cpus\t: (%d/%d)\n",
5151 instance->msix_vectors, (unsigned int)num_online_cpus());
5152 dev_info(&instance->pdev->dev,
5153 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
5155 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5156 (unsigned long)instance);
5158 if (instance->msix_vectors ?
5159 megasas_setup_irqs_msix(instance, 1) :
5160 megasas_setup_irqs_ioapic(instance))
5161 goto fail_setup_irqs;
5163 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5165 if (instance->ctrl_info == NULL)
5166 goto fail_init_adapter;
5169 * Below are default value for legacy Firmware.
5170 * non-fusion based controllers
5172 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5173 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5174 /* Get operational params, sge flags, send init cmd to controller */
5175 if (instance->instancet->init_adapter(instance))
5176 goto fail_init_adapter;
5179 instance->instancet->enable_intr(instance);
5181 dev_info(&instance->pdev->dev, "INIT adapter done\n");
5183 megasas_setup_jbod_map(instance);
5186 * the following function will get the PD LIST.
5188 memset(instance->pd_list, 0,
5189 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5190 if (megasas_get_pd_list(instance) < 0) {
5191 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5192 goto fail_get_pd_list;
5195 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5196 if (megasas_ld_list_query(instance,
5197 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5198 megasas_get_ld_list(instance);
5201 * Compute the max allowed sectors per IO: The controller info has two
5202 * limits on max sectors. Driver should use the minimum of these two.
5204 * 1 << stripe_sz_ops.min = max sectors per strip
5206 * Note that older firmwares ( < FW ver 30) didn't report information
5207 * to calculate max_sectors_1. So the number ended up as zero always.
5210 ctrl_info = instance->ctrl_info;
5212 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5213 le16_to_cpu(ctrl_info->max_strips_per_io);
5214 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
5216 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
5218 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5219 instance->passive = ctrl_info->cluster.passive;
5220 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
5221 instance->UnevenSpanSupport =
5222 ctrl_info->adapterOperations2.supportUnevenSpans;
5223 if (instance->UnevenSpanSupport) {
5224 struct fusion_context *fusion = instance->ctrl_context;
5225 if (MR_ValidateMapInfo(instance))
5226 fusion->fast_path_io = 1;
5228 fusion->fast_path_io = 0;
5231 if (ctrl_info->host_interface.SRIOV) {
5232 instance->requestorId = ctrl_info->iov.requestorId;
5233 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5234 if (!ctrl_info->adapterOperations2.activePassive)
5235 instance->PlasmaFW111 = 1;
5237 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5238 instance->PlasmaFW111 ? "1.11" : "new");
5240 if (instance->PlasmaFW111) {
5241 iovPtr = (struct IOV_111 *)
5242 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5243 instance->requestorId = iovPtr->requestorId;
5246 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5247 instance->requestorId);
5250 instance->crash_dump_fw_support =
5251 ctrl_info->adapterOperations3.supportCrashDump;
5252 instance->crash_dump_drv_support =
5253 (instance->crash_dump_fw_support &&
5254 instance->crash_dump_buf);
5255 if (instance->crash_dump_drv_support)
5256 megasas_set_crash_dump_params(instance,
5257 MR_CRASH_BUF_TURN_OFF);
5260 if (instance->crash_dump_buf)
5261 pci_free_consistent(instance->pdev,
5263 instance->crash_dump_buf,
5264 instance->crash_dump_h);
5265 instance->crash_dump_buf = NULL;
5269 dev_info(&instance->pdev->dev,
5270 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5271 le16_to_cpu(ctrl_info->pci.vendor_id),
5272 le16_to_cpu(ctrl_info->pci.device_id),
5273 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5274 le16_to_cpu(ctrl_info->pci.sub_device_id));
5275 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5276 instance->UnevenSpanSupport ? "yes" : "no");
5277 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5278 instance->crash_dump_drv_support ? "yes" : "no");
5279 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5280 instance->use_seqnum_jbod_fp ? "yes" : "no");
5283 instance->max_sectors_per_req = instance->max_num_sge *
5284 SGE_BUFFER_SIZE / 512;
5285 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5286 instance->max_sectors_per_req = tmp_sectors;
5288 /* Check for valid throttlequeuedepth module parameter */
5289 if (throttlequeuedepth &&
5290 throttlequeuedepth <= instance->max_scsi_cmds)
5291 instance->throttlequeuedepth = throttlequeuedepth;
5293 instance->throttlequeuedepth =
5294 MEGASAS_THROTTLE_QUEUE_DEPTH;
5296 if (resetwaittime > MEGASAS_RESET_WAIT_TIME)
5297 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5299 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5300 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
5302 /* Launch SR-IOV heartbeat timer */
5303 if (instance->requestorId) {
5304 if (!megasas_sriov_start_heartbeat(instance, 1))
5305 megasas_start_timer(instance,
5306 &instance->sriov_heartbeat_timer,
5307 megasas_sriov_heartbeat_handler,
5308 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5310 instance->skip_heartbeat_timer_del = 1;
5316 instance->instancet->disable_intr(instance);
5318 megasas_destroy_irqs(instance);
5320 if (instance->msix_vectors)
5321 pci_disable_msix(instance->pdev);
5322 instance->msix_vectors = 0;
5324 kfree(instance->ctrl_info);
5325 instance->ctrl_info = NULL;
5326 iounmap(instance->reg_set);
5329 pci_release_selected_regions(instance->pdev, instance->bar);
5335 * megasas_release_mfi - Reverses the FW initialization
5336 * @instance: Adapter soft state
5338 static void megasas_release_mfi(struct megasas_instance *instance)
5340 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
5342 if (instance->reply_queue)
5343 pci_free_consistent(instance->pdev, reply_q_sz,
5344 instance->reply_queue, instance->reply_queue_h);
5346 megasas_free_cmds(instance);
5348 iounmap(instance->reg_set);
5350 pci_release_selected_regions(instance->pdev, instance->bar);
5354 * megasas_get_seq_num - Gets latest event sequence numbers
5355 * @instance: Adapter soft state
5356 * @eli: FW event log sequence numbers information
5358 * FW maintains a log of all events in a non-volatile area. Upper layers would
5359 * usually find out the latest sequence number of the events, the seq number at
5360 * the boot etc. They would "read" all the events below the latest seq number
5361 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5362 * number), they would subsribe to AEN (asynchronous event notification) and
5363 * wait for the events to happen.
5366 megasas_get_seq_num(struct megasas_instance *instance,
5367 struct megasas_evt_log_info *eli)
5369 struct megasas_cmd *cmd;
5370 struct megasas_dcmd_frame *dcmd;
5371 struct megasas_evt_log_info *el_info;
5372 dma_addr_t el_info_h = 0;
5374 cmd = megasas_get_cmd(instance);
5380 dcmd = &cmd->frame->dcmd;
5381 el_info = pci_alloc_consistent(instance->pdev,
5382 sizeof(struct megasas_evt_log_info),
5386 megasas_return_cmd(instance, cmd);
5390 memset(el_info, 0, sizeof(*el_info));
5391 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5393 dcmd->cmd = MFI_CMD_DCMD;
5394 dcmd->cmd_status = 0x0;
5395 dcmd->sge_count = 1;
5396 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5399 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5400 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5401 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5402 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5404 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5407 * Copy the data back into callers buffer
5409 eli->newest_seq_num = el_info->newest_seq_num;
5410 eli->oldest_seq_num = el_info->oldest_seq_num;
5411 eli->clear_seq_num = el_info->clear_seq_num;
5412 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5413 eli->boot_seq_num = el_info->boot_seq_num;
5415 dev_err(&instance->pdev->dev, "DCMD failed "
5416 "from %s\n", __func__);
5418 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5419 el_info, el_info_h);
5421 megasas_return_cmd(instance, cmd);
5427 * megasas_register_aen - Registers for asynchronous event notification
5428 * @instance: Adapter soft state
5429 * @seq_num: The starting sequence number
5430 * @class_locale: Class of the event
5432 * This function subscribes for AEN for events beyond the @seq_num. It requests
5433 * to be notified if and only if the event is of type @class_locale
5436 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5437 u32 class_locale_word)
5440 struct megasas_cmd *cmd;
5441 struct megasas_dcmd_frame *dcmd;
5442 union megasas_evt_class_locale curr_aen;
5443 union megasas_evt_class_locale prev_aen;
5446 * If there an AEN pending already (aen_cmd), check if the
5447 * class_locale of that pending AEN is inclusive of the new
5448 * AEN request we currently have. If it is, then we don't have
5449 * to do anything. In other words, whichever events the current
5450 * AEN request is subscribing to, have already been subscribed
5453 * If the old_cmd is _not_ inclusive, then we have to abort
5454 * that command, form a class_locale that is superset of both
5455 * old and current and re-issue to the FW
5458 curr_aen.word = class_locale_word;
5460 if (instance->aen_cmd) {
5463 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
5466 * A class whose enum value is smaller is inclusive of all
5467 * higher values. If a PROGRESS (= -1) was previously
5468 * registered, then a new registration requests for higher
5469 * classes need not be sent to FW. They are automatically
5472 * Locale numbers don't have such hierarchy. They are bitmap
5475 if ((prev_aen.members.class <= curr_aen.members.class) &&
5476 !((prev_aen.members.locale & curr_aen.members.locale) ^
5477 curr_aen.members.locale)) {
5479 * Previously issued event registration includes
5480 * current request. Nothing to do.
5484 curr_aen.members.locale |= prev_aen.members.locale;
5486 if (prev_aen.members.class < curr_aen.members.class)
5487 curr_aen.members.class = prev_aen.members.class;
5489 instance->aen_cmd->abort_aen = 1;
5490 ret_val = megasas_issue_blocked_abort_cmd(instance,
5495 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
5496 "previous AEN command\n");
5502 cmd = megasas_get_cmd(instance);
5507 dcmd = &cmd->frame->dcmd;
5509 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5512 * Prepare DCMD for aen registration
5514 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5516 dcmd->cmd = MFI_CMD_DCMD;
5517 dcmd->cmd_status = 0x0;
5518 dcmd->sge_count = 1;
5519 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5522 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5523 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5524 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
5525 instance->last_seq_num = seq_num;
5526 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5527 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5528 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
5530 if (instance->aen_cmd != NULL) {
5531 megasas_return_cmd(instance, cmd);
5536 * Store reference to the cmd used to register for AEN. When an
5537 * application wants us to register for AEN, we have to abort this
5538 * cmd and re-register with a new EVENT LOCALE supplied by that app
5540 instance->aen_cmd = cmd;
5543 * Issue the aen registration frame
5545 instance->instancet->issue_dcmd(instance, cmd);
5551 * megasas_start_aen - Subscribes to AEN during driver load time
5552 * @instance: Adapter soft state
5554 static int megasas_start_aen(struct megasas_instance *instance)
5556 struct megasas_evt_log_info eli;
5557 union megasas_evt_class_locale class_locale;
5560 * Get the latest sequence number from FW
5562 memset(&eli, 0, sizeof(eli));
5564 if (megasas_get_seq_num(instance, &eli))
5568 * Register AEN with FW for latest sequence number plus 1
5570 class_locale.members.reserved = 0;
5571 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5572 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5574 return megasas_register_aen(instance,
5575 le32_to_cpu(eli.newest_seq_num) + 1,
5580 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5581 * @instance: Adapter soft state
5583 static int megasas_io_attach(struct megasas_instance *instance)
5585 struct Scsi_Host *host = instance->host;
5588 * Export parameters required by SCSI mid-layer
5590 host->irq = instance->pdev->irq;
5591 host->unique_id = instance->unique_id;
5592 host->can_queue = instance->max_scsi_cmds;
5593 host->this_id = instance->init_id;
5594 host->sg_tablesize = instance->max_num_sge;
5596 if (instance->fw_support_ieee)
5597 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5600 * Check if the module parameter value for max_sectors can be used
5602 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5603 instance->max_sectors_per_req = max_sectors;
5606 if (((instance->pdev->device ==
5607 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5608 (instance->pdev->device ==
5609 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5610 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5611 instance->max_sectors_per_req = max_sectors;
5613 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
5614 "and <= %d (or < 1MB for GEN2 controller)\n",
5615 instance->max_sectors_per_req);
5620 host->max_sectors = instance->max_sectors_per_req;
5621 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
5622 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5623 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5624 host->max_lun = MEGASAS_MAX_LUN;
5625 host->max_cmd_len = 16;
5628 * Notify the mid-layer about the new controller
5630 if (scsi_add_host(host, &instance->pdev->dev)) {
5631 dev_err(&instance->pdev->dev,
5632 "Failed to add host from %s %d\n",
5633 __func__, __LINE__);
5641 megasas_set_dma_mask(struct pci_dev *pdev)
5644 * All our controllers are capable of performing 64-bit DMA
5647 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
5649 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5650 goto fail_set_dma_mask;
5653 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5654 goto fail_set_dma_mask;
5657 * Ensure that all data structures are allocated in 32-bit
5660 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5661 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5662 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5663 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5664 dev_info(&pdev->dev, "set 32bit DMA mask"
5665 "and 32 bit consistent mask\n");
5667 goto fail_set_dma_mask;
5677 * megasas_probe_one - PCI hotplug entry point
5678 * @pdev: PCI device structure
5679 * @id: PCI ids of supported hotplugged adapter
5681 static int megasas_probe_one(struct pci_dev *pdev,
5682 const struct pci_device_id *id)
5685 struct Scsi_Host *host;
5686 struct megasas_instance *instance;
5688 struct fusion_context *fusion = NULL;
5690 /* Reset MSI-X in the kdump kernel */
5691 if (reset_devices) {
5692 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5694 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
5696 if (control & PCI_MSIX_FLAGS_ENABLE) {
5697 dev_info(&pdev->dev, "resetting MSI-X\n");
5698 pci_write_config_word(pdev,
5699 pos + PCI_MSIX_FLAGS,
5701 ~PCI_MSIX_FLAGS_ENABLE);
5707 * PCI prepping: enable device set bus mastering and dma mask
5709 rval = pci_enable_device_mem(pdev);
5715 pci_set_master(pdev);
5717 if (megasas_set_dma_mask(pdev))
5718 goto fail_set_dma_mask;
5720 host = scsi_host_alloc(&megasas_template,
5721 sizeof(struct megasas_instance));
5724 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
5725 goto fail_alloc_instance;
5728 instance = (struct megasas_instance *)host->hostdata;
5729 memset(instance, 0, sizeof(*instance));
5730 atomic_set(&instance->fw_reset_no_pci_access, 0);
5731 instance->pdev = pdev;
5733 switch (instance->pdev->device) {
5734 case PCI_DEVICE_ID_LSI_FUSION:
5735 case PCI_DEVICE_ID_LSI_PLASMA:
5736 case PCI_DEVICE_ID_LSI_INVADER:
5737 case PCI_DEVICE_ID_LSI_FURY:
5738 case PCI_DEVICE_ID_LSI_INTRUDER:
5739 case PCI_DEVICE_ID_LSI_INTRUDER_24:
5740 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5741 case PCI_DEVICE_ID_LSI_CUTLASS_53:
5743 instance->ctrl_context_pages =
5744 get_order(sizeof(struct fusion_context));
5745 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5746 instance->ctrl_context_pages);
5747 if (!instance->ctrl_context) {
5748 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
5749 "memory for Fusion context info\n");
5750 goto fail_alloc_dma_buf;
5752 fusion = instance->ctrl_context;
5754 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
5755 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5756 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5757 fusion->adapter_type = THUNDERBOLT_SERIES;
5759 fusion->adapter_type = INVADER_SERIES;
5762 default: /* For all other supported controllers */
5764 instance->producer =
5765 pci_alloc_consistent(pdev, sizeof(u32),
5766 &instance->producer_h);
5767 instance->consumer =
5768 pci_alloc_consistent(pdev, sizeof(u32),
5769 &instance->consumer_h);
5771 if (!instance->producer || !instance->consumer) {
5772 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
5773 "memory for producer, consumer\n");
5774 goto fail_alloc_dma_buf;
5777 *instance->producer = 0;
5778 *instance->consumer = 0;
5782 /* Crash dump feature related initialisation*/
5783 instance->drv_buf_index = 0;
5784 instance->drv_buf_alloc = 0;
5785 instance->crash_dump_fw_support = 0;
5786 instance->crash_dump_app_support = 0;
5787 instance->fw_crash_state = UNAVAILABLE;
5788 spin_lock_init(&instance->crashdump_lock);
5789 instance->crash_dump_buf = NULL;
5791 megasas_poll_wait_aen = 0;
5792 instance->flag_ieee = 0;
5793 instance->ev = NULL;
5794 instance->issuepend_done = 1;
5795 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
5796 instance->is_imr = 0;
5798 instance->evt_detail = pci_alloc_consistent(pdev,
5800 megasas_evt_detail),
5801 &instance->evt_detail_h);
5803 if (!instance->evt_detail) {
5804 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
5805 "event detail structure\n");
5806 goto fail_alloc_dma_buf;
5809 if (!reset_devices) {
5810 instance->system_info_buf = pci_zalloc_consistent(pdev,
5811 sizeof(struct MR_DRV_SYSTEM_INFO),
5812 &instance->system_info_h);
5813 if (!instance->system_info_buf)
5814 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5816 instance->pd_info = pci_alloc_consistent(pdev,
5817 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
5819 if (!instance->pd_info)
5820 dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
5822 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5824 &instance->crash_dump_h);
5825 if (!instance->crash_dump_buf)
5826 dev_err(&pdev->dev, "Can't allocate Firmware "
5827 "crash dump DMA buffer\n");
5831 * Initialize locks and queues
5833 INIT_LIST_HEAD(&instance->cmd_pool);
5834 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
5836 atomic_set(&instance->fw_outstanding,0);
5838 init_waitqueue_head(&instance->int_cmd_wait_q);
5839 init_waitqueue_head(&instance->abort_cmd_wait_q);
5841 spin_lock_init(&instance->mfi_pool_lock);
5842 spin_lock_init(&instance->hba_lock);
5843 spin_lock_init(&instance->completion_lock);
5845 mutex_init(&instance->reset_mutex);
5846 mutex_init(&instance->hba_mutex);
5849 * Initialize PCI related and misc parameters
5851 instance->host = host;
5852 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5853 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
5854 instance->ctrl_info = NULL;
5857 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5858 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
5859 instance->flag_ieee = 1;
5861 megasas_dbg_lvl = 0;
5863 instance->unload = 1;
5864 instance->last_time = 0;
5865 instance->disableOnlineCtrlReset = 1;
5866 instance->UnevenSpanSupport = 0;
5868 if (instance->ctrl_context) {
5869 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
5870 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5872 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
5875 * Initialize MFI Firmware
5877 if (megasas_init_fw(instance))
5880 if (instance->requestorId) {
5881 if (instance->PlasmaFW111) {
5882 instance->vf_affiliation_111 =
5883 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5884 &instance->vf_affiliation_111_h);
5885 if (!instance->vf_affiliation_111)
5886 dev_warn(&pdev->dev, "Can't allocate "
5887 "memory for VF affiliation buffer\n");
5889 instance->vf_affiliation =
5890 pci_alloc_consistent(pdev,
5891 (MAX_LOGICAL_DRIVES + 1) *
5892 sizeof(struct MR_LD_VF_AFFILIATION),
5893 &instance->vf_affiliation_h);
5894 if (!instance->vf_affiliation)
5895 dev_warn(&pdev->dev, "Can't allocate "
5896 "memory for VF affiliation buffer\n");
5901 * Store instance in PCI softstate
5903 pci_set_drvdata(pdev, instance);
5906 * Add this controller to megasas_mgmt_info structure so that it
5907 * can be exported to management applications
5909 megasas_mgmt_info.count++;
5910 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5911 megasas_mgmt_info.max_index++;
5914 * Register with SCSI mid-layer
5916 if (megasas_io_attach(instance))
5917 goto fail_io_attach;
5919 instance->unload = 0;
5921 * Trigger SCSI to scan our drives
5923 scsi_scan_host(host);
5926 * Initiate AEN (Asynchronous Event Notification)
5928 if (megasas_start_aen(instance)) {
5929 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
5930 goto fail_start_aen;
5933 /* Get current SR-IOV LD/VF affiliation */
5934 if (instance->requestorId)
5935 megasas_get_ld_vf_affiliation(instance, 1);
5941 megasas_mgmt_info.count--;
5942 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5943 megasas_mgmt_info.max_index--;
5945 instance->instancet->disable_intr(instance);
5946 megasas_destroy_irqs(instance);
5948 if (instance->ctrl_context)
5949 megasas_release_fusion(instance);
5951 megasas_release_mfi(instance);
5952 if (instance->msix_vectors)
5953 pci_disable_msix(instance->pdev);
5956 if (instance->evt_detail)
5957 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5958 instance->evt_detail,
5959 instance->evt_detail_h);
5961 if (instance->pd_info)
5962 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
5964 instance->pd_info_h);
5965 if (instance->producer)
5966 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5967 instance->producer_h);
5968 if (instance->consumer)
5969 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5970 instance->consumer_h);
5971 scsi_host_put(host);
5973 fail_alloc_instance:
5975 pci_disable_device(pdev);
5981 * megasas_flush_cache - Requests FW to flush all its caches
5982 * @instance: Adapter soft state
5984 static void megasas_flush_cache(struct megasas_instance *instance)
5986 struct megasas_cmd *cmd;
5987 struct megasas_dcmd_frame *dcmd;
5989 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
5992 cmd = megasas_get_cmd(instance);
5997 dcmd = &cmd->frame->dcmd;
5999 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6001 dcmd->cmd = MFI_CMD_DCMD;
6002 dcmd->cmd_status = 0x0;
6003 dcmd->sge_count = 0;
6004 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
6007 dcmd->data_xfer_len = 0;
6008 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
6009 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6011 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6013 dev_err(&instance->pdev->dev,
6014 "return from %s %d\n", __func__, __LINE__);
6018 megasas_return_cmd(instance, cmd);
6022 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6023 * @instance: Adapter soft state
6024 * @opcode: Shutdown/Hibernate
6026 static void megasas_shutdown_controller(struct megasas_instance *instance,
6029 struct megasas_cmd *cmd;
6030 struct megasas_dcmd_frame *dcmd;
6032 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6035 cmd = megasas_get_cmd(instance);
6040 if (instance->aen_cmd)
6041 megasas_issue_blocked_abort_cmd(instance,
6042 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
6043 if (instance->map_update_cmd)
6044 megasas_issue_blocked_abort_cmd(instance,
6045 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
6046 if (instance->jbod_seq_cmd)
6047 megasas_issue_blocked_abort_cmd(instance,
6048 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
6050 dcmd = &cmd->frame->dcmd;
6052 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6054 dcmd->cmd = MFI_CMD_DCMD;
6055 dcmd->cmd_status = 0x0;
6056 dcmd->sge_count = 0;
6057 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
6060 dcmd->data_xfer_len = 0;
6061 dcmd->opcode = cpu_to_le32(opcode);
6063 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6065 dev_err(&instance->pdev->dev,
6066 "return from %s %d\n", __func__, __LINE__);
6070 megasas_return_cmd(instance, cmd);
6075 * megasas_suspend - driver suspend entry point
6076 * @pdev: PCI device structure
6077 * @state: PCI power state to suspend routine
6080 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6082 struct Scsi_Host *host;
6083 struct megasas_instance *instance;
6085 instance = pci_get_drvdata(pdev);
6086 host = instance->host;
6087 instance->unload = 1;
6089 /* Shutdown SR-IOV heartbeat timer */
6090 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6091 del_timer_sync(&instance->sriov_heartbeat_timer);
6093 megasas_flush_cache(instance);
6094 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
6096 /* cancel the delayed work if this work still in queue */
6097 if (instance->ev != NULL) {
6098 struct megasas_aen_event *ev = instance->ev;
6099 cancel_delayed_work_sync(&ev->hotplug_work);
6100 instance->ev = NULL;
6103 tasklet_kill(&instance->isr_tasklet);
6105 pci_set_drvdata(instance->pdev, instance);
6106 instance->instancet->disable_intr(instance);
6108 megasas_destroy_irqs(instance);
6110 if (instance->msix_vectors)
6111 pci_disable_msix(instance->pdev);
6113 pci_save_state(pdev);
6114 pci_disable_device(pdev);
6116 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6122 * megasas_resume- driver resume entry point
6123 * @pdev: PCI device structure
6126 megasas_resume(struct pci_dev *pdev)
6129 struct Scsi_Host *host;
6130 struct megasas_instance *instance;
6132 instance = pci_get_drvdata(pdev);
6133 host = instance->host;
6134 pci_set_power_state(pdev, PCI_D0);
6135 pci_enable_wake(pdev, PCI_D0, 0);
6136 pci_restore_state(pdev);
6139 * PCI prepping: enable device set bus mastering and dma mask
6141 rval = pci_enable_device_mem(pdev);
6144 dev_err(&pdev->dev, "Enable device failed\n");
6148 pci_set_master(pdev);
6150 if (megasas_set_dma_mask(pdev))
6151 goto fail_set_dma_mask;
6154 * Initialize MFI Firmware
6157 atomic_set(&instance->fw_outstanding, 0);
6160 * We expect the FW state to be READY
6162 if (megasas_transition_to_ready(instance, 0))
6163 goto fail_ready_state;
6165 /* Now re-enable MSI-X */
6166 if (instance->msix_vectors &&
6167 pci_enable_msix_exact(instance->pdev, instance->msixentry,
6168 instance->msix_vectors))
6169 goto fail_reenable_msix;
6171 if (instance->ctrl_context) {
6172 megasas_reset_reply_desc(instance);
6173 if (megasas_ioc_init_fusion(instance)) {
6174 megasas_free_cmds(instance);
6175 megasas_free_cmds_fusion(instance);
6178 if (!megasas_get_map_info(instance))
6179 megasas_sync_map_info(instance);
6181 *instance->producer = 0;
6182 *instance->consumer = 0;
6183 if (megasas_issue_init_mfi(instance))
6187 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6188 (unsigned long)instance);
6190 if (instance->msix_vectors ?
6191 megasas_setup_irqs_msix(instance, 0) :
6192 megasas_setup_irqs_ioapic(instance))
6195 /* Re-launch SR-IOV heartbeat timer */
6196 if (instance->requestorId) {
6197 if (!megasas_sriov_start_heartbeat(instance, 0))
6198 megasas_start_timer(instance,
6199 &instance->sriov_heartbeat_timer,
6200 megasas_sriov_heartbeat_handler,
6201 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
6203 instance->skip_heartbeat_timer_del = 1;
6208 instance->instancet->enable_intr(instance);
6209 megasas_setup_jbod_map(instance);
6210 instance->unload = 0;
6213 * Initiate AEN (Asynchronous Event Notification)
6215 if (megasas_start_aen(instance))
6216 dev_err(&instance->pdev->dev, "Start AEN failed\n");
6221 if (instance->evt_detail)
6222 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6223 instance->evt_detail,
6224 instance->evt_detail_h);
6226 if (instance->pd_info)
6227 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6229 instance->pd_info_h);
6230 if (instance->producer)
6231 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6232 instance->producer_h);
6233 if (instance->consumer)
6234 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6235 instance->consumer_h);
6236 scsi_host_put(host);
6242 pci_disable_device(pdev);
6247 #define megasas_suspend NULL
6248 #define megasas_resume NULL
6252 * megasas_detach_one - PCI hot"un"plug entry point
6253 * @pdev: PCI device structure
6255 static void megasas_detach_one(struct pci_dev *pdev)
6258 struct Scsi_Host *host;
6259 struct megasas_instance *instance;
6260 struct fusion_context *fusion;
6263 instance = pci_get_drvdata(pdev);
6264 instance->unload = 1;
6265 host = instance->host;
6266 fusion = instance->ctrl_context;
6268 /* Shutdown SR-IOV heartbeat timer */
6269 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6270 del_timer_sync(&instance->sriov_heartbeat_timer);
6272 if (instance->fw_crash_state != UNAVAILABLE)
6273 megasas_free_host_crash_buffer(instance);
6274 scsi_remove_host(instance->host);
6275 megasas_flush_cache(instance);
6276 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
6278 /* cancel the delayed work if this work still in queue*/
6279 if (instance->ev != NULL) {
6280 struct megasas_aen_event *ev = instance->ev;
6281 cancel_delayed_work_sync(&ev->hotplug_work);
6282 instance->ev = NULL;
6285 /* cancel all wait events */
6286 wake_up_all(&instance->int_cmd_wait_q);
6288 tasklet_kill(&instance->isr_tasklet);
6291 * Take the instance off the instance array. Note that we will not
6292 * decrement the max_index. We let this array be sparse array
6294 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6295 if (megasas_mgmt_info.instance[i] == instance) {
6296 megasas_mgmt_info.count--;
6297 megasas_mgmt_info.instance[i] = NULL;
6303 instance->instancet->disable_intr(instance);
6305 megasas_destroy_irqs(instance);
6307 if (instance->msix_vectors)
6308 pci_disable_msix(instance->pdev);
6310 if (instance->ctrl_context) {
6311 megasas_release_fusion(instance);
6312 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6313 (sizeof(struct MR_PD_CFG_SEQ) *
6314 (MAX_PHYSICAL_DEVICES - 1));
6315 for (i = 0; i < 2 ; i++) {
6316 if (fusion->ld_map[i])
6317 dma_free_coherent(&instance->pdev->dev,
6320 fusion->ld_map_phys[i]);
6321 if (fusion->ld_drv_map[i])
6322 free_pages((ulong)fusion->ld_drv_map[i],
6323 fusion->drv_map_pages);
6324 if (fusion->pd_seq_sync[i])
6325 dma_free_coherent(&instance->pdev->dev,
6327 fusion->pd_seq_sync[i],
6328 fusion->pd_seq_phys[i]);
6330 free_pages((ulong)instance->ctrl_context,
6331 instance->ctrl_context_pages);
6333 megasas_release_mfi(instance);
6334 pci_free_consistent(pdev, sizeof(u32),
6336 instance->producer_h);
6337 pci_free_consistent(pdev, sizeof(u32),
6339 instance->consumer_h);
6342 kfree(instance->ctrl_info);
6344 if (instance->evt_detail)
6345 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6346 instance->evt_detail, instance->evt_detail_h);
6348 if (instance->pd_info)
6349 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6351 instance->pd_info_h);
6352 if (instance->vf_affiliation)
6353 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6354 sizeof(struct MR_LD_VF_AFFILIATION),
6355 instance->vf_affiliation,
6356 instance->vf_affiliation_h);
6358 if (instance->vf_affiliation_111)
6359 pci_free_consistent(pdev,
6360 sizeof(struct MR_LD_VF_AFFILIATION_111),
6361 instance->vf_affiliation_111,
6362 instance->vf_affiliation_111_h);
6364 if (instance->hb_host_mem)
6365 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6366 instance->hb_host_mem,
6367 instance->hb_host_mem_h);
6369 if (instance->crash_dump_buf)
6370 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6371 instance->crash_dump_buf, instance->crash_dump_h);
6373 if (instance->system_info_buf)
6374 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6375 instance->system_info_buf, instance->system_info_h);
6377 scsi_host_put(host);
6379 pci_disable_device(pdev);
6383 * megasas_shutdown - Shutdown entry point
6384 * @device: Generic device structure
6386 static void megasas_shutdown(struct pci_dev *pdev)
6388 struct megasas_instance *instance = pci_get_drvdata(pdev);
6390 instance->unload = 1;
6391 megasas_flush_cache(instance);
6392 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
6393 instance->instancet->disable_intr(instance);
6394 megasas_destroy_irqs(instance);
6396 if (instance->msix_vectors)
6397 pci_disable_msix(instance->pdev);
6401 * megasas_mgmt_open - char node "open" entry point
6403 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6406 * Allow only those users with admin rights
6408 if (!capable(CAP_SYS_ADMIN))
6415 * megasas_mgmt_fasync - Async notifier registration from applications
6417 * This function adds the calling process to a driver global queue. When an
6418 * event occurs, SIGIO will be sent to all processes in this queue.
6420 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6424 mutex_lock(&megasas_async_queue_mutex);
6426 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6428 mutex_unlock(&megasas_async_queue_mutex);
6431 /* For sanity check when we get ioctl */
6432 filep->private_data = filep;
6436 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6442 * megasas_mgmt_poll - char node "poll" entry point
6444 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6447 unsigned long flags;
6449 poll_wait(file, &megasas_poll_wait, wait);
6450 spin_lock_irqsave(&poll_aen_lock, flags);
6451 if (megasas_poll_wait_aen)
6452 mask = (POLLIN | POLLRDNORM);
6455 megasas_poll_wait_aen = 0;
6456 spin_unlock_irqrestore(&poll_aen_lock, flags);
6461 * megasas_set_crash_dump_params_ioctl:
6462 * Send CRASH_DUMP_MODE DCMD to all controllers
6463 * @cmd: MFI command frame
6466 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
6468 struct megasas_instance *local_instance;
6472 crash_support = cmd->frame->dcmd.mbox.w[0];
6474 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6475 local_instance = megasas_mgmt_info.instance[i];
6476 if (local_instance && local_instance->crash_dump_drv_support) {
6477 if ((atomic_read(&local_instance->adprecovery) ==
6478 MEGASAS_HBA_OPERATIONAL) &&
6479 !megasas_set_crash_dump_params(local_instance,
6481 local_instance->crash_dump_app_support =
6483 dev_info(&local_instance->pdev->dev,
6484 "Application firmware crash "
6485 "dump mode set success\n");
6488 dev_info(&local_instance->pdev->dev,
6489 "Application firmware crash "
6490 "dump mode set failed\n");
6499 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6500 * @instance: Adapter soft state
6501 * @argp: User's ioctl packet
6504 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6505 struct megasas_iocpacket __user * user_ioc,
6506 struct megasas_iocpacket *ioc)
6508 struct megasas_sge32 *kern_sge32;
6509 struct megasas_cmd *cmd;
6510 void *kbuff_arr[MAX_IOCTL_SGE];
6511 dma_addr_t buf_handle = 0;
6514 dma_addr_t sense_handle;
6515 unsigned long *sense_ptr;
6517 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6519 if (ioc->sge_count > MAX_IOCTL_SGE) {
6520 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
6521 ioc->sge_count, MAX_IOCTL_SGE);
6525 cmd = megasas_get_cmd(instance);
6527 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
6532 * User's IOCTL packet has 2 frames (maximum). Copy those two
6533 * frames into our cmd's frames. cmd->frame's context will get
6534 * overwritten when we copy from user's frames. So set that value
6537 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
6538 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
6539 cmd->frame->hdr.pad_0 = 0;
6540 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6542 MFI_FRAME_SENSE64));
6544 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6545 error = megasas_set_crash_dump_params_ioctl(cmd);
6546 megasas_return_cmd(instance, cmd);
6551 * The management interface between applications and the fw uses
6552 * MFI frames. E.g, RAID configuration changes, LD property changes
6553 * etc are accomplishes through different kinds of MFI frames. The
6554 * driver needs to care only about substituting user buffers with
6555 * kernel buffers in SGLs. The location of SGL is embedded in the
6556 * struct iocpacket itself.
6558 kern_sge32 = (struct megasas_sge32 *)
6559 ((unsigned long)cmd->frame + ioc->sgl_off);
6562 * For each user buffer, create a mirror buffer and copy in
6564 for (i = 0; i < ioc->sge_count; i++) {
6565 if (!ioc->sgl[i].iov_len)
6568 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
6569 ioc->sgl[i].iov_len,
6570 &buf_handle, GFP_KERNEL);
6571 if (!kbuff_arr[i]) {
6572 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6573 "kernel SGL buffer for IOCTL\n");
6579 * We don't change the dma_coherent_mask, so
6580 * pci_alloc_consistent only returns 32bit addresses
6582 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6583 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
6586 * We created a kernel buffer corresponding to the
6587 * user buffer. Now copy in from the user buffer
6589 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6590 (u32) (ioc->sgl[i].iov_len))) {
6596 if (ioc->sense_len) {
6597 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6598 &sense_handle, GFP_KERNEL);
6605 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
6606 *sense_ptr = cpu_to_le32(sense_handle);
6610 * Set the sync_cmd flag so that the ISR knows not to complete this
6611 * cmd to the SCSI mid-layer
6614 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
6616 dev_err(&instance->pdev->dev,
6617 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
6618 __func__, __LINE__, cmd->frame->dcmd.opcode,
6619 cmd->cmd_status_drv);
6625 if (instance->unload == 1) {
6626 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6627 "don't submit data to application\n");
6631 * copy out the kernel buffers to user buffers
6633 for (i = 0; i < ioc->sge_count; i++) {
6634 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6635 ioc->sgl[i].iov_len)) {
6642 * copy out the sense
6644 if (ioc->sense_len) {
6646 * sense_ptr points to the location that has the user
6647 * sense buffer address
6649 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6652 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6653 sense, ioc->sense_len)) {
6654 dev_err(&instance->pdev->dev, "Failed to copy out to user "
6662 * copy the status codes returned by the fw
6664 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6665 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6666 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
6672 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
6673 sense, sense_handle);
6676 for (i = 0; i < ioc->sge_count; i++) {
6678 dma_free_coherent(&instance->pdev->dev,
6679 le32_to_cpu(kern_sge32[i].length),
6681 le32_to_cpu(kern_sge32[i].phys_addr));
6682 kbuff_arr[i] = NULL;
6686 megasas_return_cmd(instance, cmd);
6690 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6692 struct megasas_iocpacket __user *user_ioc =
6693 (struct megasas_iocpacket __user *)arg;
6694 struct megasas_iocpacket *ioc;
6695 struct megasas_instance *instance;
6698 unsigned long flags;
6699 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6701 ioc = memdup_user(user_ioc, sizeof(*ioc));
6703 return PTR_ERR(ioc);
6705 instance = megasas_lookup_instance(ioc->host_no);
6711 /* Adjust ioctl wait time for VF mode */
6712 if (instance->requestorId)
6713 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6715 /* Block ioctls in VF mode */
6716 if (instance->requestorId && !allow_vf_ioctls) {
6721 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
6722 dev_err(&instance->pdev->dev, "Controller in crit error\n");
6727 if (instance->unload == 1) {
6732 if (down_interruptible(&instance->ioctl_sem)) {
6733 error = -ERESTARTSYS;
6737 for (i = 0; i < wait_time; i++) {
6739 spin_lock_irqsave(&instance->hba_lock, flags);
6740 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
6741 spin_unlock_irqrestore(&instance->hba_lock, flags);
6744 spin_unlock_irqrestore(&instance->hba_lock, flags);
6746 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6747 dev_notice(&instance->pdev->dev, "waiting"
6748 "for controller reset to finish\n");
6754 spin_lock_irqsave(&instance->hba_lock, flags);
6755 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6756 spin_unlock_irqrestore(&instance->hba_lock, flags);
6758 dev_err(&instance->pdev->dev, "timed out while"
6759 "waiting for HBA to recover\n");
6763 spin_unlock_irqrestore(&instance->hba_lock, flags);
6765 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
6767 up(&instance->ioctl_sem);
6774 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6776 struct megasas_instance *instance;
6777 struct megasas_aen aen;
6780 unsigned long flags;
6781 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6783 if (file->private_data != file) {
6784 printk(KERN_DEBUG "megasas: fasync_helper was not "
6789 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6792 instance = megasas_lookup_instance(aen.host_no);
6797 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
6801 if (instance->unload == 1) {
6805 for (i = 0; i < wait_time; i++) {
6807 spin_lock_irqsave(&instance->hba_lock, flags);
6808 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
6809 spin_unlock_irqrestore(&instance->hba_lock,
6814 spin_unlock_irqrestore(&instance->hba_lock, flags);
6816 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6817 dev_notice(&instance->pdev->dev, "waiting for"
6818 "controller reset to finish\n");
6824 spin_lock_irqsave(&instance->hba_lock, flags);
6825 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6826 spin_unlock_irqrestore(&instance->hba_lock, flags);
6827 dev_err(&instance->pdev->dev, "timed out while waiting"
6828 "for HBA to recover\n");
6831 spin_unlock_irqrestore(&instance->hba_lock, flags);
6833 mutex_lock(&instance->reset_mutex);
6834 error = megasas_register_aen(instance, aen.seq_num,
6835 aen.class_locale_word);
6836 mutex_unlock(&instance->reset_mutex);
6841 * megasas_mgmt_ioctl - char node ioctl entry point
6844 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6847 case MEGASAS_IOC_FIRMWARE:
6848 return megasas_mgmt_ioctl_fw(file, arg);
6850 case MEGASAS_IOC_GET_AEN:
6851 return megasas_mgmt_ioctl_aen(file, arg);
6857 #ifdef CONFIG_COMPAT
6858 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6860 struct compat_megasas_iocpacket __user *cioc =
6861 (struct compat_megasas_iocpacket __user *)arg;
6862 struct megasas_iocpacket __user *ioc =
6863 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6867 u32 local_sense_off;
6868 u32 local_sense_len;
6871 if (clear_user(ioc, sizeof(*ioc)))
6874 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6875 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6876 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6877 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6878 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6879 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6883 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6884 * sense_len is not null, so prepare the 64bit value under
6885 * the same condition.
6887 if (get_user(local_sense_off, &ioc->sense_off) ||
6888 get_user(local_sense_len, &ioc->sense_len) ||
6889 get_user(user_sense_off, &cioc->sense_off))
6892 if (local_sense_len) {
6893 void __user **sense_ioc_ptr =
6894 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
6895 compat_uptr_t *sense_cioc_ptr =
6896 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
6897 if (get_user(ptr, sense_cioc_ptr) ||
6898 put_user(compat_ptr(ptr), sense_ioc_ptr))
6902 for (i = 0; i < MAX_IOCTL_SGE; i++) {
6903 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6904 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6905 copy_in_user(&ioc->sgl[i].iov_len,
6906 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6910 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6912 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6913 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6914 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6921 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6925 case MEGASAS_IOC_FIRMWARE32:
6926 return megasas_mgmt_compat_ioctl_fw(file, arg);
6927 case MEGASAS_IOC_GET_AEN:
6928 return megasas_mgmt_ioctl_aen(file, arg);
6936 * File operations structure for management interface
6938 static const struct file_operations megasas_mgmt_fops = {
6939 .owner = THIS_MODULE,
6940 .open = megasas_mgmt_open,
6941 .fasync = megasas_mgmt_fasync,
6942 .unlocked_ioctl = megasas_mgmt_ioctl,
6943 .poll = megasas_mgmt_poll,
6944 #ifdef CONFIG_COMPAT
6945 .compat_ioctl = megasas_mgmt_compat_ioctl,
6947 .llseek = noop_llseek,
6951 * PCI hotplug support registration structure
6953 static struct pci_driver megasas_pci_driver = {
6955 .name = "megaraid_sas",
6956 .id_table = megasas_pci_table,
6957 .probe = megasas_probe_one,
6958 .remove = megasas_detach_one,
6959 .suspend = megasas_suspend,
6960 .resume = megasas_resume,
6961 .shutdown = megasas_shutdown,
6965 * Sysfs driver attributes
6967 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6969 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6973 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6976 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6978 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6982 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6985 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6987 return sprintf(buf, "%u\n", support_poll_for_event);
6990 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6991 megasas_sysfs_show_support_poll_for_event, NULL);
6994 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6996 return sprintf(buf, "%u\n", support_device_change);
6999 static DRIVER_ATTR(support_device_change, S_IRUGO,
7000 megasas_sysfs_show_support_device_change, NULL);
7003 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
7005 return sprintf(buf, "%u\n", megasas_dbg_lvl);
7009 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
7013 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
7014 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7020 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
7021 megasas_sysfs_set_dbg_lvl);
7024 megasas_aen_polling(struct work_struct *work)
7026 struct megasas_aen_event *ev =
7027 container_of(work, struct megasas_aen_event, hotplug_work.work);
7028 struct megasas_instance *instance = ev->instance;
7029 union megasas_evt_class_locale class_locale;
7030 struct Scsi_Host *host;
7031 struct scsi_device *sdev1;
7034 int i, j, doscan = 0;
7035 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
7037 u8 dcmd_ret = DCMD_SUCCESS;
7040 printk(KERN_ERR "invalid instance!\n");
7045 /* Adjust event workqueue thread wait time for VF mode */
7046 if (instance->requestorId)
7047 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7049 /* Don't run the event workqueue thread if OCR is running */
7050 mutex_lock(&instance->reset_mutex);
7052 instance->ev = NULL;
7053 host = instance->host;
7054 if (instance->evt_detail) {
7055 megasas_decode_evt(instance);
7057 switch (le32_to_cpu(instance->evt_detail->code)) {
7059 case MR_EVT_PD_INSERTED:
7060 case MR_EVT_PD_REMOVED:
7061 dcmd_ret = megasas_get_pd_list(instance);
7062 if (dcmd_ret == DCMD_SUCCESS)
7063 doscan = SCAN_PD_CHANNEL;
7066 case MR_EVT_LD_OFFLINE:
7067 case MR_EVT_CFG_CLEARED:
7068 case MR_EVT_LD_DELETED:
7069 case MR_EVT_LD_CREATED:
7070 if (!instance->requestorId ||
7071 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7072 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7074 if (dcmd_ret == DCMD_SUCCESS)
7075 doscan = SCAN_VD_CHANNEL;
7079 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7080 case MR_EVT_FOREIGN_CFG_IMPORTED:
7081 case MR_EVT_LD_STATE_CHANGE:
7082 dcmd_ret = megasas_get_pd_list(instance);
7084 if (dcmd_ret != DCMD_SUCCESS)
7087 if (!instance->requestorId ||
7088 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7089 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7091 if (dcmd_ret != DCMD_SUCCESS)
7094 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7095 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7096 instance->host->host_no);
7099 case MR_EVT_CTRL_PROP_CHANGED:
7100 dcmd_ret = megasas_get_ctrl_info(instance);
7107 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7108 mutex_unlock(&instance->reset_mutex);
7113 mutex_unlock(&instance->reset_mutex);
7115 if (doscan & SCAN_PD_CHANNEL) {
7116 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7117 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7118 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
7119 sdev1 = scsi_device_lookup(host, i, j, 0);
7120 if (instance->pd_list[pd_index].driveState ==
7121 MR_PD_STATE_SYSTEM) {
7123 scsi_add_device(host, i, j, 0);
7125 scsi_device_put(sdev1);
7128 scsi_remove_device(sdev1);
7129 scsi_device_put(sdev1);
7136 if (doscan & SCAN_VD_CHANNEL) {
7137 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7138 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7139 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7140 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7141 if (instance->ld_ids[ld_index] != 0xff) {
7143 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7145 scsi_device_put(sdev1);
7148 scsi_remove_device(sdev1);
7149 scsi_device_put(sdev1);
7156 if (dcmd_ret == DCMD_SUCCESS)
7157 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7159 seq_num = instance->last_seq_num;
7161 /* Register AEN with FW for latest sequence number plus 1 */
7162 class_locale.members.reserved = 0;
7163 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7164 class_locale.members.class = MR_EVT_CLASS_DEBUG;
7166 if (instance->aen_cmd != NULL) {
7171 mutex_lock(&instance->reset_mutex);
7172 error = megasas_register_aen(instance, seq_num,
7175 dev_err(&instance->pdev->dev,
7176 "register aen failed error %x\n", error);
7178 mutex_unlock(&instance->reset_mutex);
7183 * megasas_init - Driver load entry point
7185 static int __init megasas_init(void)
7190 * Booted in kdump kernel, minimize memory footprints by
7191 * disabling few features
7193 if (reset_devices) {
7196 dual_qdepth_disable = 1;
7200 * Announce driver version and other information
7202 pr_info("megasas: %s\n", MEGASAS_VERSION);
7204 spin_lock_init(&poll_aen_lock);
7206 support_poll_for_event = 2;
7207 support_device_change = 1;
7209 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7212 * Register character device node
7214 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7217 printk(KERN_DEBUG "megasas: failed to open device node\n");
7221 megasas_mgmt_majorno = rval;
7224 * Register ourselves as PCI hotplug module
7226 rval = pci_register_driver(&megasas_pci_driver);
7229 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
7233 rval = driver_create_file(&megasas_pci_driver.driver,
7234 &driver_attr_version);
7236 goto err_dcf_attr_ver;
7238 rval = driver_create_file(&megasas_pci_driver.driver,
7239 &driver_attr_release_date);
7241 goto err_dcf_rel_date;
7243 rval = driver_create_file(&megasas_pci_driver.driver,
7244 &driver_attr_support_poll_for_event);
7246 goto err_dcf_support_poll_for_event;
7248 rval = driver_create_file(&megasas_pci_driver.driver,
7249 &driver_attr_dbg_lvl);
7251 goto err_dcf_dbg_lvl;
7252 rval = driver_create_file(&megasas_pci_driver.driver,
7253 &driver_attr_support_device_change);
7255 goto err_dcf_support_device_change;
7259 err_dcf_support_device_change:
7260 driver_remove_file(&megasas_pci_driver.driver,
7261 &driver_attr_dbg_lvl);
7263 driver_remove_file(&megasas_pci_driver.driver,
7264 &driver_attr_support_poll_for_event);
7265 err_dcf_support_poll_for_event:
7266 driver_remove_file(&megasas_pci_driver.driver,
7267 &driver_attr_release_date);
7269 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7271 pci_unregister_driver(&megasas_pci_driver);
7273 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7278 * megasas_exit - Driver unload entry point
7280 static void __exit megasas_exit(void)
7282 driver_remove_file(&megasas_pci_driver.driver,
7283 &driver_attr_dbg_lvl);
7284 driver_remove_file(&megasas_pci_driver.driver,
7285 &driver_attr_support_poll_for_event);
7286 driver_remove_file(&megasas_pci_driver.driver,
7287 &driver_attr_support_device_change);
7288 driver_remove_file(&megasas_pci_driver.driver,
7289 &driver_attr_release_date);
7290 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7292 pci_unregister_driver(&megasas_pci_driver);
7293 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7296 module_init(megasas_init);
7297 module_exit(megasas_exit);