2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2003-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Authors: Avago Technologies
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
28 * Send feedback to: megaraidlinux.pdl@avagotech.com
30 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/list.h>
38 #include <linux/moduleparam.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/uio.h>
44 #include <linux/slab.h>
45 #include <asm/uaccess.h>
47 #include <linux/compat.h>
48 #include <linux/blkdev.h>
49 #include <linux/mutex.h>
50 #include <linux/poll.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_device.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_tcq.h>
57 #include "megaraid_sas_fusion.h"
58 #include "megaraid_sas.h"
61 * Number of sectors per IO command
62 * Will be set in megasas_init_mfi if user does not provide
64 static unsigned int max_sectors;
65 module_param_named(max_sectors, max_sectors, int, 0);
66 MODULE_PARM_DESC(max_sectors,
67 "Maximum number of sectors per IO command");
69 static int msix_disable;
70 module_param(msix_disable, int, S_IRUGO);
71 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73 static unsigned int msix_vectors;
74 module_param(msix_vectors, int, S_IRUGO);
75 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77 static int allow_vf_ioctls;
78 module_param(allow_vf_ioctls, int, S_IRUGO);
79 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
81 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
82 module_param(throttlequeuedepth, int, S_IRUGO);
83 MODULE_PARM_DESC(throttlequeuedepth,
84 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
86 int resetwaittime = MEGASAS_RESET_WAIT_TIME;
87 module_param(resetwaittime, int, S_IRUGO);
88 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
89 "before resetting adapter. Default: 180");
91 int smp_affinity_enable = 1;
92 module_param(smp_affinity_enable, int, S_IRUGO);
93 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(MEGASAS_VERSION);
97 MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
98 MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
100 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
101 static int megasas_get_pd_list(struct megasas_instance *instance);
102 static int megasas_ld_list_query(struct megasas_instance *instance,
104 static int megasas_issue_init_mfi(struct megasas_instance *instance);
105 static int megasas_register_aen(struct megasas_instance *instance,
106 u32 seq_num, u32 class_locale_word);
108 * PCI ID table for all supported controllers
110 static struct pci_device_id megasas_pci_table[] = {
112 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
114 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
116 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
118 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
120 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
122 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
124 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
126 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
127 /* xscale IOP, vega */
128 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
141 /* Intruder 24 port*/
142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
143 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
147 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
149 static int megasas_mgmt_majorno;
150 struct megasas_mgmt_info megasas_mgmt_info;
151 static struct fasync_struct *megasas_async_queue;
152 static DEFINE_MUTEX(megasas_async_queue_mutex);
154 static int megasas_poll_wait_aen;
155 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
156 static u32 support_poll_for_event;
158 static u32 support_device_change;
160 /* define lock for aen poll */
161 spinlock_t poll_aen_lock;
164 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
167 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
169 megasas_adp_reset_gen2(struct megasas_instance *instance,
170 struct megasas_register_set __iomem *reg_set);
171 static irqreturn_t megasas_isr(int irq, void *devp);
173 megasas_init_adapter_mfi(struct megasas_instance *instance);
175 megasas_build_and_issue_cmd(struct megasas_instance *instance,
176 struct scsi_cmnd *scmd);
177 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
179 megasas_release_fusion(struct megasas_instance *instance);
181 megasas_ioc_init_fusion(struct megasas_instance *instance);
183 megasas_free_cmds_fusion(struct megasas_instance *instance);
185 megasas_get_map_info(struct megasas_instance *instance);
187 megasas_sync_map_info(struct megasas_instance *instance);
189 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
191 void megasas_reset_reply_desc(struct megasas_instance *instance);
192 int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout);
193 void megasas_fusion_ocr_wq(struct work_struct *work);
194 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
196 int megasas_check_mpio_paths(struct megasas_instance *instance,
197 struct scsi_cmnd *scmd);
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);
207 * megasas_get_cmd - Get a command from the free pool
208 * @instance: Adapter soft state
210 * Returns a free command from the pool
212 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
216 struct megasas_cmd *cmd = NULL;
218 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
220 if (!list_empty(&instance->cmd_pool)) {
221 cmd = list_entry((&instance->cmd_pool)->next,
222 struct megasas_cmd, list);
223 list_del_init(&cmd->list);
225 dev_err(&instance->pdev->dev, "Command pool empty!\n");
228 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
233 * megasas_return_cmd - Return a cmd to free command pool
234 * @instance: Adapter soft state
235 * @cmd: Command packet to be returned to free command pool
238 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
242 struct megasas_cmd_fusion *cmd_fusion;
243 struct fusion_context *fusion = instance->ctrl_context;
245 /* This flag is used only for fusion adapter.
246 * Wait for Interrupt for Polled mode DCMD
248 if (cmd->flags & DRV_DCMD_POLLED_MODE)
251 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
254 blk_tags = instance->max_scsi_cmds + cmd->index;
255 cmd_fusion = fusion->cmd_list[blk_tags];
256 megasas_return_cmd_fusion(instance, cmd_fusion);
259 cmd->frame_count = 0;
261 if (!fusion && reset_devices)
262 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
263 list_add(&cmd->list, (&instance->cmd_pool)->next);
265 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
270 format_timestamp(uint32_t timestamp)
272 static char buffer[32];
274 if ((timestamp & 0xff000000) == 0xff000000)
275 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
278 snprintf(buffer, sizeof(buffer), "%us", timestamp);
283 format_class(int8_t class)
285 static char buffer[6];
288 case MFI_EVT_CLASS_DEBUG:
290 case MFI_EVT_CLASS_PROGRESS:
292 case MFI_EVT_CLASS_INFO:
294 case MFI_EVT_CLASS_WARNING:
296 case MFI_EVT_CLASS_CRITICAL:
298 case MFI_EVT_CLASS_FATAL:
300 case MFI_EVT_CLASS_DEAD:
303 snprintf(buffer, sizeof(buffer), "%d", class);
309 * megasas_decode_evt: Decode FW AEN event and print critical event
311 * @instance: Adapter soft state
314 megasas_decode_evt(struct megasas_instance *instance)
316 struct megasas_evt_detail *evt_detail = instance->evt_detail;
317 union megasas_evt_class_locale class_locale;
318 class_locale.word = le32_to_cpu(evt_detail->cl.word);
320 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
321 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
322 le32_to_cpu(evt_detail->seq_num),
323 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
324 (class_locale.members.locale),
325 format_class(class_locale.members.class),
326 evt_detail->description);
330 * The following functions are defined for xscale
331 * (deviceid : 1064R, PERC5) controllers
335 * megasas_enable_intr_xscale - Enables interrupts
336 * @regs: MFI register set
339 megasas_enable_intr_xscale(struct megasas_instance *instance)
341 struct megasas_register_set __iomem *regs;
343 regs = instance->reg_set;
344 writel(0, &(regs)->outbound_intr_mask);
346 /* Dummy readl to force pci flush */
347 readl(®s->outbound_intr_mask);
351 * megasas_disable_intr_xscale -Disables interrupt
352 * @regs: MFI register set
355 megasas_disable_intr_xscale(struct megasas_instance *instance)
357 struct megasas_register_set __iomem *regs;
360 regs = instance->reg_set;
361 writel(mask, ®s->outbound_intr_mask);
362 /* Dummy readl to force pci flush */
363 readl(®s->outbound_intr_mask);
367 * megasas_read_fw_status_reg_xscale - returns the current FW status value
368 * @regs: MFI register set
371 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
373 return readl(&(regs)->outbound_msg_0);
376 * megasas_clear_interrupt_xscale - Check & clear interrupt
377 * @regs: MFI register set
380 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
386 * Check if it is our interrupt
388 status = readl(®s->outbound_intr_status);
390 if (status & MFI_OB_INTR_STATUS_MASK)
391 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
392 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
393 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
396 * Clear the interrupt by writing back the same value
399 writel(status, ®s->outbound_intr_status);
401 /* Dummy readl to force pci flush */
402 readl(®s->outbound_intr_status);
408 * megasas_fire_cmd_xscale - Sends command to the FW
409 * @frame_phys_addr : Physical address of cmd
410 * @frame_count : Number of frames for the command
411 * @regs : MFI register set
414 megasas_fire_cmd_xscale(struct megasas_instance *instance,
415 dma_addr_t frame_phys_addr,
417 struct megasas_register_set __iomem *regs)
421 spin_lock_irqsave(&instance->hba_lock, flags);
422 writel((frame_phys_addr >> 3)|(frame_count),
423 &(regs)->inbound_queue_port);
424 spin_unlock_irqrestore(&instance->hba_lock, flags);
428 * megasas_adp_reset_xscale - For controller reset
429 * @regs: MFI register set
432 megasas_adp_reset_xscale(struct megasas_instance *instance,
433 struct megasas_register_set __iomem *regs)
438 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
440 for (i = 0; i < 3; i++)
441 msleep(1000); /* sleep for 3 secs */
443 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
444 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
446 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
448 pci_write_config_dword(instance->pdev,
449 MFI_1068_PCSR_OFFSET, pcidata);
451 for (i = 0; i < 2; i++)
452 msleep(1000); /* need to wait 2 secs again */
455 pci_read_config_dword(instance->pdev,
456 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
457 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
458 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
459 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
461 pci_write_config_dword(instance->pdev,
462 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
469 * megasas_check_reset_xscale - For controller reset check
470 * @regs: MFI register set
473 megasas_check_reset_xscale(struct megasas_instance *instance,
474 struct megasas_register_set __iomem *regs)
476 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
477 (le32_to_cpu(*instance->consumer) ==
478 MEGASAS_ADPRESET_INPROG_SIGN))
483 static struct megasas_instance_template megasas_instance_template_xscale = {
485 .fire_cmd = megasas_fire_cmd_xscale,
486 .enable_intr = megasas_enable_intr_xscale,
487 .disable_intr = megasas_disable_intr_xscale,
488 .clear_intr = megasas_clear_intr_xscale,
489 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
490 .adp_reset = megasas_adp_reset_xscale,
491 .check_reset = megasas_check_reset_xscale,
492 .service_isr = megasas_isr,
493 .tasklet = megasas_complete_cmd_dpc,
494 .init_adapter = megasas_init_adapter_mfi,
495 .build_and_issue_cmd = megasas_build_and_issue_cmd,
496 .issue_dcmd = megasas_issue_dcmd,
500 * This is the end of set of functions & definitions specific
501 * to xscale (deviceid : 1064R, PERC5) controllers
505 * The following functions are defined for ppc (deviceid : 0x60)
510 * megasas_enable_intr_ppc - Enables interrupts
511 * @regs: MFI register set
514 megasas_enable_intr_ppc(struct megasas_instance *instance)
516 struct megasas_register_set __iomem *regs;
518 regs = instance->reg_set;
519 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
521 writel(~0x80000000, &(regs)->outbound_intr_mask);
523 /* Dummy readl to force pci flush */
524 readl(®s->outbound_intr_mask);
528 * megasas_disable_intr_ppc - Disable interrupt
529 * @regs: MFI register set
532 megasas_disable_intr_ppc(struct megasas_instance *instance)
534 struct megasas_register_set __iomem *regs;
535 u32 mask = 0xFFFFFFFF;
537 regs = instance->reg_set;
538 writel(mask, ®s->outbound_intr_mask);
539 /* Dummy readl to force pci flush */
540 readl(®s->outbound_intr_mask);
544 * megasas_read_fw_status_reg_ppc - returns the current FW status value
545 * @regs: MFI register set
548 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
550 return readl(&(regs)->outbound_scratch_pad);
554 * megasas_clear_interrupt_ppc - Check & clear interrupt
555 * @regs: MFI register set
558 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
560 u32 status, mfiStatus = 0;
563 * Check if it is our interrupt
565 status = readl(®s->outbound_intr_status);
567 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
568 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
570 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
571 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
574 * Clear the interrupt by writing back the same value
576 writel(status, ®s->outbound_doorbell_clear);
578 /* Dummy readl to force pci flush */
579 readl(®s->outbound_doorbell_clear);
585 * megasas_fire_cmd_ppc - Sends command to the FW
586 * @frame_phys_addr : Physical address of cmd
587 * @frame_count : Number of frames for the command
588 * @regs : MFI register set
591 megasas_fire_cmd_ppc(struct megasas_instance *instance,
592 dma_addr_t frame_phys_addr,
594 struct megasas_register_set __iomem *regs)
598 spin_lock_irqsave(&instance->hba_lock, flags);
599 writel((frame_phys_addr | (frame_count<<1))|1,
600 &(regs)->inbound_queue_port);
601 spin_unlock_irqrestore(&instance->hba_lock, flags);
605 * megasas_check_reset_ppc - For controller reset check
606 * @regs: MFI register set
609 megasas_check_reset_ppc(struct megasas_instance *instance,
610 struct megasas_register_set __iomem *regs)
612 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
618 static struct megasas_instance_template megasas_instance_template_ppc = {
620 .fire_cmd = megasas_fire_cmd_ppc,
621 .enable_intr = megasas_enable_intr_ppc,
622 .disable_intr = megasas_disable_intr_ppc,
623 .clear_intr = megasas_clear_intr_ppc,
624 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
625 .adp_reset = megasas_adp_reset_xscale,
626 .check_reset = megasas_check_reset_ppc,
627 .service_isr = megasas_isr,
628 .tasklet = megasas_complete_cmd_dpc,
629 .init_adapter = megasas_init_adapter_mfi,
630 .build_and_issue_cmd = megasas_build_and_issue_cmd,
631 .issue_dcmd = megasas_issue_dcmd,
635 * megasas_enable_intr_skinny - Enables interrupts
636 * @regs: MFI register set
639 megasas_enable_intr_skinny(struct megasas_instance *instance)
641 struct megasas_register_set __iomem *regs;
643 regs = instance->reg_set;
644 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
646 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
648 /* Dummy readl to force pci flush */
649 readl(®s->outbound_intr_mask);
653 * megasas_disable_intr_skinny - Disables interrupt
654 * @regs: MFI register set
657 megasas_disable_intr_skinny(struct megasas_instance *instance)
659 struct megasas_register_set __iomem *regs;
660 u32 mask = 0xFFFFFFFF;
662 regs = instance->reg_set;
663 writel(mask, ®s->outbound_intr_mask);
664 /* Dummy readl to force pci flush */
665 readl(®s->outbound_intr_mask);
669 * megasas_read_fw_status_reg_skinny - returns the current FW status value
670 * @regs: MFI register set
673 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
675 return readl(&(regs)->outbound_scratch_pad);
679 * megasas_clear_interrupt_skinny - Check & clear interrupt
680 * @regs: MFI register set
683 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
689 * Check if it is our interrupt
691 status = readl(®s->outbound_intr_status);
693 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
698 * Check if it is our interrupt
700 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
702 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
704 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
707 * Clear the interrupt by writing back the same value
709 writel(status, ®s->outbound_intr_status);
712 * dummy read to flush PCI
714 readl(®s->outbound_intr_status);
720 * megasas_fire_cmd_skinny - Sends command to the FW
721 * @frame_phys_addr : Physical address of cmd
722 * @frame_count : Number of frames for the command
723 * @regs : MFI register set
726 megasas_fire_cmd_skinny(struct megasas_instance *instance,
727 dma_addr_t frame_phys_addr,
729 struct megasas_register_set __iomem *regs)
733 spin_lock_irqsave(&instance->hba_lock, flags);
734 writel(upper_32_bits(frame_phys_addr),
735 &(regs)->inbound_high_queue_port);
736 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
737 &(regs)->inbound_low_queue_port);
739 spin_unlock_irqrestore(&instance->hba_lock, flags);
743 * megasas_check_reset_skinny - For controller reset check
744 * @regs: MFI register set
747 megasas_check_reset_skinny(struct megasas_instance *instance,
748 struct megasas_register_set __iomem *regs)
750 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
756 static struct megasas_instance_template megasas_instance_template_skinny = {
758 .fire_cmd = megasas_fire_cmd_skinny,
759 .enable_intr = megasas_enable_intr_skinny,
760 .disable_intr = megasas_disable_intr_skinny,
761 .clear_intr = megasas_clear_intr_skinny,
762 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
763 .adp_reset = megasas_adp_reset_gen2,
764 .check_reset = megasas_check_reset_skinny,
765 .service_isr = megasas_isr,
766 .tasklet = megasas_complete_cmd_dpc,
767 .init_adapter = megasas_init_adapter_mfi,
768 .build_and_issue_cmd = megasas_build_and_issue_cmd,
769 .issue_dcmd = megasas_issue_dcmd,
774 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
779 * megasas_enable_intr_gen2 - Enables interrupts
780 * @regs: MFI register set
783 megasas_enable_intr_gen2(struct megasas_instance *instance)
785 struct megasas_register_set __iomem *regs;
787 regs = instance->reg_set;
788 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
790 /* write ~0x00000005 (4 & 1) to the intr mask*/
791 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
793 /* Dummy readl to force pci flush */
794 readl(®s->outbound_intr_mask);
798 * megasas_disable_intr_gen2 - Disables interrupt
799 * @regs: MFI register set
802 megasas_disable_intr_gen2(struct megasas_instance *instance)
804 struct megasas_register_set __iomem *regs;
805 u32 mask = 0xFFFFFFFF;
807 regs = instance->reg_set;
808 writel(mask, ®s->outbound_intr_mask);
809 /* Dummy readl to force pci flush */
810 readl(®s->outbound_intr_mask);
814 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
815 * @regs: MFI register set
818 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
820 return readl(&(regs)->outbound_scratch_pad);
824 * megasas_clear_interrupt_gen2 - Check & clear interrupt
825 * @regs: MFI register set
828 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
834 * Check if it is our interrupt
836 status = readl(®s->outbound_intr_status);
838 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
839 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
841 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
842 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
846 * Clear the interrupt by writing back the same value
849 writel(status, ®s->outbound_doorbell_clear);
851 /* Dummy readl to force pci flush */
852 readl(®s->outbound_intr_status);
857 * megasas_fire_cmd_gen2 - Sends command to the FW
858 * @frame_phys_addr : Physical address of cmd
859 * @frame_count : Number of frames for the command
860 * @regs : MFI register set
863 megasas_fire_cmd_gen2(struct megasas_instance *instance,
864 dma_addr_t frame_phys_addr,
866 struct megasas_register_set __iomem *regs)
870 spin_lock_irqsave(&instance->hba_lock, flags);
871 writel((frame_phys_addr | (frame_count<<1))|1,
872 &(regs)->inbound_queue_port);
873 spin_unlock_irqrestore(&instance->hba_lock, flags);
877 * megasas_adp_reset_gen2 - For controller reset
878 * @regs: MFI register set
881 megasas_adp_reset_gen2(struct megasas_instance *instance,
882 struct megasas_register_set __iomem *reg_set)
886 u32 __iomem *seq_offset = ®_set->seq_offset;
887 u32 __iomem *hostdiag_offset = ®_set->host_diag;
889 if (instance->instancet == &megasas_instance_template_skinny) {
890 seq_offset = ®_set->fusion_seq_offset;
891 hostdiag_offset = ®_set->fusion_host_diag;
894 writel(0, seq_offset);
895 writel(4, seq_offset);
896 writel(0xb, seq_offset);
897 writel(2, seq_offset);
898 writel(7, seq_offset);
899 writel(0xd, seq_offset);
903 HostDiag = (u32)readl(hostdiag_offset);
905 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
907 HostDiag = (u32)readl(hostdiag_offset);
908 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
916 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
918 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
922 HostDiag = (u32)readl(hostdiag_offset);
923 while (HostDiag & DIAG_RESET_ADAPTER) {
925 HostDiag = (u32)readl(hostdiag_offset);
926 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
937 * megasas_check_reset_gen2 - For controller reset check
938 * @regs: MFI register set
941 megasas_check_reset_gen2(struct megasas_instance *instance,
942 struct megasas_register_set __iomem *regs)
944 if (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)
988 struct megasas_header *frame_hdr = &cmd->frame->hdr;
990 frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
991 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
994 * Issue the frame using inbound queue port
996 instance->instancet->issue_dcmd(instance, cmd);
999 * Wait for cmd_status to change
1001 if (instance->requestorId)
1002 seconds = MEGASAS_ROUTINE_WAIT_TIME_VF;
1004 seconds = MFI_POLL_TIMEOUT_SECS;
1005 return wait_and_poll(instance, cmd, seconds);
1009 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1010 * @instance: Adapter soft state
1011 * @cmd: Command to be issued
1012 * @timeout: Timeout in seconds
1014 * This function waits on an event for the command to be returned from ISR.
1015 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1016 * Used to issue ioctl commands.
1019 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1020 struct megasas_cmd *cmd, int timeout)
1024 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1026 instance->instancet->issue_dcmd(instance, cmd);
1028 ret = wait_event_timeout(instance->int_cmd_wait_q,
1029 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1033 wait_event(instance->int_cmd_wait_q,
1034 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1036 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1041 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1042 * @instance: Adapter soft state
1043 * @cmd_to_abort: Previously issued cmd to be aborted
1044 * @timeout: Timeout in seconds
1046 * MFI firmware can abort previously issued AEN comamnd (automatic event
1047 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1048 * cmd and waits for return status.
1049 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1052 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1053 struct megasas_cmd *cmd_to_abort, int timeout)
1055 struct megasas_cmd *cmd;
1056 struct megasas_abort_frame *abort_fr;
1059 cmd = megasas_get_cmd(instance);
1064 abort_fr = &cmd->frame->abort;
1067 * Prepare and issue the abort frame
1069 abort_fr->cmd = MFI_CMD_ABORT;
1070 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1071 abort_fr->flags = cpu_to_le16(0);
1072 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1073 abort_fr->abort_mfi_phys_addr_lo =
1074 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1075 abort_fr->abort_mfi_phys_addr_hi =
1076 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1079 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1081 instance->instancet->issue_dcmd(instance, cmd);
1084 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1085 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1087 dev_err(&instance->pdev->dev, "Command timedout"
1088 "from %s\n", __func__);
1092 wait_event(instance->abort_cmd_wait_q,
1093 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1097 megasas_return_cmd(instance, cmd);
1102 * megasas_make_sgl32 - Prepares 32-bit SGL
1103 * @instance: Adapter soft state
1104 * @scp: SCSI command from the mid-layer
1105 * @mfi_sgl: SGL to be filled in
1107 * If successful, this function returns the number of SG elements. Otherwise,
1111 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1112 union megasas_sgl *mfi_sgl)
1116 struct scatterlist *os_sgl;
1118 sge_count = scsi_dma_map(scp);
1119 BUG_ON(sge_count < 0);
1122 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1123 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1124 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1131 * megasas_make_sgl64 - Prepares 64-bit SGL
1132 * @instance: Adapter soft state
1133 * @scp: SCSI command from the mid-layer
1134 * @mfi_sgl: SGL to be filled in
1136 * If successful, this function returns the number of SG elements. Otherwise,
1140 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1141 union megasas_sgl *mfi_sgl)
1145 struct scatterlist *os_sgl;
1147 sge_count = scsi_dma_map(scp);
1148 BUG_ON(sge_count < 0);
1151 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1152 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1153 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1160 * megasas_make_sgl_skinny - Prepares IEEE SGL
1161 * @instance: Adapter soft state
1162 * @scp: SCSI command from the mid-layer
1163 * @mfi_sgl: SGL to be filled in
1165 * If successful, this function returns the number of SG elements. Otherwise,
1169 megasas_make_sgl_skinny(struct megasas_instance *instance,
1170 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1174 struct scatterlist *os_sgl;
1176 sge_count = scsi_dma_map(scp);
1179 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1180 mfi_sgl->sge_skinny[i].length =
1181 cpu_to_le32(sg_dma_len(os_sgl));
1182 mfi_sgl->sge_skinny[i].phys_addr =
1183 cpu_to_le64(sg_dma_address(os_sgl));
1184 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1191 * megasas_get_frame_count - Computes the number of frames
1192 * @frame_type : type of frame- io or pthru frame
1193 * @sge_count : number of sg elements
1195 * Returns the number of frames required for numnber of sge's (sge_count)
1198 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1199 u8 sge_count, u8 frame_type)
1204 u32 frame_count = 0;
1206 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1207 sizeof(struct megasas_sge32);
1209 if (instance->flag_ieee) {
1210 sge_sz = sizeof(struct megasas_sge_skinny);
1214 * Main frame can contain 2 SGEs for 64-bit SGLs and
1215 * 3 SGEs for 32-bit SGLs for ldio &
1216 * 1 SGEs for 64-bit SGLs and
1217 * 2 SGEs for 32-bit SGLs for pthru frame
1219 if (unlikely(frame_type == PTHRU_FRAME)) {
1220 if (instance->flag_ieee == 1) {
1221 num_cnt = sge_count - 1;
1222 } else if (IS_DMA64)
1223 num_cnt = sge_count - 1;
1225 num_cnt = sge_count - 2;
1227 if (instance->flag_ieee == 1) {
1228 num_cnt = sge_count - 1;
1229 } else if (IS_DMA64)
1230 num_cnt = sge_count - 2;
1232 num_cnt = sge_count - 3;
1236 sge_bytes = sge_sz * num_cnt;
1238 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1239 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1244 if (frame_count > 7)
1250 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1251 * @instance: Adapter soft state
1252 * @scp: SCSI command
1253 * @cmd: Command to be prepared in
1255 * This function prepares CDB commands. These are typcially pass-through
1256 * commands to the devices.
1259 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1260 struct megasas_cmd *cmd)
1265 struct megasas_pthru_frame *pthru;
1267 is_logical = MEGASAS_IS_LOGICAL(scp);
1268 device_id = MEGASAS_DEV_INDEX(scp);
1269 pthru = (struct megasas_pthru_frame *)cmd->frame;
1271 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1272 flags = MFI_FRAME_DIR_WRITE;
1273 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1274 flags = MFI_FRAME_DIR_READ;
1275 else if (scp->sc_data_direction == PCI_DMA_NONE)
1276 flags = MFI_FRAME_DIR_NONE;
1278 if (instance->flag_ieee == 1) {
1279 flags |= MFI_FRAME_IEEE;
1283 * Prepare the DCDB frame
1285 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1286 pthru->cmd_status = 0x0;
1287 pthru->scsi_status = 0x0;
1288 pthru->target_id = device_id;
1289 pthru->lun = scp->device->lun;
1290 pthru->cdb_len = scp->cmd_len;
1293 pthru->flags = cpu_to_le16(flags);
1294 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1296 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1299 * If the command is for the tape device, set the
1300 * pthru timeout to the os layer timeout value.
1302 if (scp->device->type == TYPE_TAPE) {
1303 if ((scp->request->timeout / HZ) > 0xFFFF)
1304 pthru->timeout = cpu_to_le16(0xFFFF);
1306 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1312 if (instance->flag_ieee == 1) {
1313 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1314 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1316 } else if (IS_DMA64) {
1317 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1318 pthru->sge_count = megasas_make_sgl64(instance, scp,
1321 pthru->sge_count = megasas_make_sgl32(instance, scp,
1324 if (pthru->sge_count > instance->max_num_sge) {
1325 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1331 * Sense info specific
1333 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1334 pthru->sense_buf_phys_addr_hi =
1335 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1336 pthru->sense_buf_phys_addr_lo =
1337 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1340 * Compute the total number of frames this command consumes. FW uses
1341 * this number to pull sufficient number of frames from host memory.
1343 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1346 return cmd->frame_count;
1350 * megasas_build_ldio - Prepares IOs to logical devices
1351 * @instance: Adapter soft state
1352 * @scp: SCSI command
1353 * @cmd: Command to be prepared
1355 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1358 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1359 struct megasas_cmd *cmd)
1362 u8 sc = scp->cmnd[0];
1364 struct megasas_io_frame *ldio;
1366 device_id = MEGASAS_DEV_INDEX(scp);
1367 ldio = (struct megasas_io_frame *)cmd->frame;
1369 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1370 flags = MFI_FRAME_DIR_WRITE;
1371 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1372 flags = MFI_FRAME_DIR_READ;
1374 if (instance->flag_ieee == 1) {
1375 flags |= MFI_FRAME_IEEE;
1379 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1381 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1382 ldio->cmd_status = 0x0;
1383 ldio->scsi_status = 0x0;
1384 ldio->target_id = device_id;
1386 ldio->reserved_0 = 0;
1388 ldio->flags = cpu_to_le16(flags);
1389 ldio->start_lba_hi = 0;
1390 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1393 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1395 if (scp->cmd_len == 6) {
1396 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1397 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1398 ((u32) scp->cmnd[2] << 8) |
1399 (u32) scp->cmnd[3]);
1401 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1405 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1407 else if (scp->cmd_len == 10) {
1408 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1409 ((u32) scp->cmnd[7] << 8));
1410 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1411 ((u32) scp->cmnd[3] << 16) |
1412 ((u32) scp->cmnd[4] << 8) |
1413 (u32) scp->cmnd[5]);
1417 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1419 else if (scp->cmd_len == 12) {
1420 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1421 ((u32) scp->cmnd[7] << 16) |
1422 ((u32) scp->cmnd[8] << 8) |
1423 (u32) scp->cmnd[9]);
1425 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1426 ((u32) scp->cmnd[3] << 16) |
1427 ((u32) scp->cmnd[4] << 8) |
1428 (u32) scp->cmnd[5]);
1432 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1434 else if (scp->cmd_len == 16) {
1435 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1436 ((u32) scp->cmnd[11] << 16) |
1437 ((u32) scp->cmnd[12] << 8) |
1438 (u32) scp->cmnd[13]);
1440 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1441 ((u32) scp->cmnd[7] << 16) |
1442 ((u32) scp->cmnd[8] << 8) |
1443 (u32) scp->cmnd[9]);
1445 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1446 ((u32) scp->cmnd[3] << 16) |
1447 ((u32) scp->cmnd[4] << 8) |
1448 (u32) scp->cmnd[5]);
1455 if (instance->flag_ieee) {
1456 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1457 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1459 } else if (IS_DMA64) {
1460 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1461 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1463 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1465 if (ldio->sge_count > instance->max_num_sge) {
1466 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1472 * Sense info specific
1474 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1475 ldio->sense_buf_phys_addr_hi = 0;
1476 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1479 * Compute the total number of frames this command consumes. FW uses
1480 * this number to pull sufficient number of frames from host memory.
1482 cmd->frame_count = megasas_get_frame_count(instance,
1483 ldio->sge_count, IO_FRAME);
1485 return cmd->frame_count;
1489 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1490 * and whether it's RW or non RW
1491 * @scmd: SCSI command
1494 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1498 switch (cmd->cmnd[0]) {
1507 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1508 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1511 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1512 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1518 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1520 * @instance: Adapter soft state
1523 megasas_dump_pending_frames(struct megasas_instance *instance)
1525 struct megasas_cmd *cmd;
1527 union megasas_sgl *mfi_sgl;
1528 struct megasas_io_frame *ldio;
1529 struct megasas_pthru_frame *pthru;
1531 u32 max_cmd = instance->max_fw_cmds;
1533 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1534 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1536 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1538 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1540 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1541 for (i = 0; i < max_cmd; i++) {
1542 cmd = instance->cmd_list[i];
1545 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1546 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1547 ldio = (struct megasas_io_frame *)cmd->frame;
1548 mfi_sgl = &ldio->sgl;
1549 sgcount = ldio->sge_count;
1550 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1551 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1552 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1553 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1554 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1556 pthru = (struct megasas_pthru_frame *) cmd->frame;
1557 mfi_sgl = &pthru->sgl;
1558 sgcount = pthru->sge_count;
1559 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1560 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1561 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1562 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1563 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1565 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1566 for (n = 0; n < sgcount; n++) {
1568 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1569 le32_to_cpu(mfi_sgl->sge64[n].length),
1570 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1572 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1573 le32_to_cpu(mfi_sgl->sge32[n].length),
1574 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1578 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1579 for (i = 0; i < max_cmd; i++) {
1581 cmd = instance->cmd_list[i];
1583 if (cmd->sync_cmd == 1)
1584 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1586 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1590 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1591 struct scsi_cmnd *scmd)
1593 struct megasas_cmd *cmd;
1596 cmd = megasas_get_cmd(instance);
1598 return SCSI_MLQUEUE_HOST_BUSY;
1601 * Logical drive command
1603 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1604 frame_count = megasas_build_ldio(instance, scmd, cmd);
1606 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1609 goto out_return_cmd;
1612 scmd->SCp.ptr = (char *)cmd;
1615 * Issue the command to the FW
1617 atomic_inc(&instance->fw_outstanding);
1619 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1620 cmd->frame_count-1, instance->reg_set);
1624 megasas_return_cmd(instance, cmd);
1630 * megasas_queue_command - Queue entry point
1631 * @scmd: SCSI command to be queued
1632 * @done: Callback entry point
1635 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1637 struct megasas_instance *instance;
1638 unsigned long flags;
1640 instance = (struct megasas_instance *)
1641 scmd->device->host->hostdata;
1643 if (instance->unload == 1) {
1644 scmd->result = DID_NO_CONNECT << 16;
1645 scmd->scsi_done(scmd);
1649 if (instance->issuepend_done == 0)
1650 return SCSI_MLQUEUE_HOST_BUSY;
1652 spin_lock_irqsave(&instance->hba_lock, flags);
1654 /* Check for an mpio path and adjust behavior */
1655 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1656 if (megasas_check_mpio_paths(instance, scmd) ==
1657 (DID_RESET << 16)) {
1658 spin_unlock_irqrestore(&instance->hba_lock, flags);
1659 return SCSI_MLQUEUE_HOST_BUSY;
1661 spin_unlock_irqrestore(&instance->hba_lock, flags);
1662 scmd->result = DID_NO_CONNECT << 16;
1663 scmd->scsi_done(scmd);
1668 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1669 spin_unlock_irqrestore(&instance->hba_lock, flags);
1670 scmd->result = DID_NO_CONNECT << 16;
1671 scmd->scsi_done(scmd);
1675 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1676 spin_unlock_irqrestore(&instance->hba_lock, flags);
1677 return SCSI_MLQUEUE_HOST_BUSY;
1680 spin_unlock_irqrestore(&instance->hba_lock, flags);
1684 if (MEGASAS_IS_LOGICAL(scmd) &&
1685 (scmd->device->id >= instance->fw_supported_vd_count ||
1686 scmd->device->lun)) {
1687 scmd->result = DID_BAD_TARGET << 16;
1692 * FW takes care of flush cache on its own for Virtual Disk.
1693 * No need to send it down for VD. For JBOD send SYNCHRONIZE_CACHE to FW.
1695 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) && MEGASAS_IS_LOGICAL(scmd)) {
1696 scmd->result = DID_OK << 16;
1700 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1701 dev_err(&instance->pdev->dev, "Err returned from build_and_issue_cmd\n");
1702 return SCSI_MLQUEUE_HOST_BUSY;
1708 scmd->scsi_done(scmd);
1712 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1716 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1718 if ((megasas_mgmt_info.instance[i]) &&
1719 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1720 return megasas_mgmt_info.instance[i];
1727 * megasas_set_dma_alignment - Set DMA alignment for PI enabled VD
1729 * @sdev: OS provided scsi device
1733 static void megasas_set_dma_alignment(struct scsi_device *sdev)
1736 struct megasas_instance *instance;
1737 struct fusion_context *fusion;
1738 struct MR_LD_RAID *raid;
1739 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1741 instance = megasas_lookup_instance(sdev->host->host_no);
1742 fusion = instance->ctrl_context;
1747 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS) {
1748 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1750 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1751 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1752 raid = MR_LdRaidGet(ld, local_map_ptr);
1754 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1755 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1759 static int megasas_slave_configure(struct scsi_device *sdev)
1762 struct megasas_instance *instance;
1764 instance = megasas_lookup_instance(sdev->host->host_no);
1765 if (instance->allow_fw_scan) {
1766 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1767 sdev->type == TYPE_DISK) {
1768 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1770 if (instance->pd_list[pd_index].driveState !=
1775 megasas_set_dma_alignment(sdev);
1777 * The RAID firmware may require extended timeouts.
1779 blk_queue_rq_timeout(sdev->request_queue,
1780 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1785 static int megasas_slave_alloc(struct scsi_device *sdev)
1788 struct megasas_instance *instance ;
1790 instance = megasas_lookup_instance(sdev->host->host_no);
1791 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1793 * Open the OS scan to the SYSTEM PD
1796 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1798 if ((instance->allow_fw_scan || instance->pd_list[pd_index].driveState ==
1799 MR_PD_STATE_SYSTEM)) {
1808 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1810 * @instance: Adapter soft state
1813 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
1816 struct megasas_cmd *cmd_mfi;
1817 struct megasas_cmd_fusion *cmd_fusion;
1818 struct fusion_context *fusion = instance->ctrl_context;
1820 /* Find all outstanding ioctls */
1822 for (i = 0; i < instance->max_fw_cmds; i++) {
1823 cmd_fusion = fusion->cmd_list[i];
1824 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1825 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1826 if (cmd_mfi->sync_cmd &&
1827 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
1828 cmd_mfi->frame->hdr.cmd_status =
1829 MFI_STAT_WRONG_STATE;
1830 megasas_complete_cmd(instance,
1836 for (i = 0; i < instance->max_fw_cmds; i++) {
1837 cmd_mfi = instance->cmd_list[i];
1838 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1840 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1846 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1848 /* Set critical error to block I/O & ioctls in case caller didn't */
1849 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1850 /* Wait 1 second to ensure IO or ioctls in build have posted */
1852 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1853 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1854 (instance->ctrl_context)) {
1855 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1857 readl(&instance->reg_set->doorbell);
1858 if (instance->mpio && instance->requestorId)
1859 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
1861 writel(MFI_STOP_ADP,
1862 &instance->reg_set->inbound_doorbell);
1864 /* Complete outstanding ioctls when adapter is killed */
1865 megasas_complete_outstanding_ioctls(instance);
1869 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1870 * restored to max value
1871 * @instance: Adapter soft state
1875 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1877 unsigned long flags;
1879 if (instance->flag & MEGASAS_FW_BUSY
1880 && time_after(jiffies, instance->last_time + 5 * HZ)
1881 && atomic_read(&instance->fw_outstanding) <
1882 instance->throttlequeuedepth + 1) {
1884 spin_lock_irqsave(instance->host->host_lock, flags);
1885 instance->flag &= ~MEGASAS_FW_BUSY;
1887 instance->host->can_queue = instance->max_scsi_cmds;
1888 spin_unlock_irqrestore(instance->host->host_lock, flags);
1893 * megasas_complete_cmd_dpc - Returns FW's controller structure
1894 * @instance_addr: Address of adapter soft state
1896 * Tasklet to complete cmds
1898 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1903 struct megasas_cmd *cmd;
1904 struct megasas_instance *instance =
1905 (struct megasas_instance *)instance_addr;
1906 unsigned long flags;
1908 /* If we have already declared adapter dead, donot complete cmds */
1909 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1912 spin_lock_irqsave(&instance->completion_lock, flags);
1914 producer = le32_to_cpu(*instance->producer);
1915 consumer = le32_to_cpu(*instance->consumer);
1917 while (consumer != producer) {
1918 context = le32_to_cpu(instance->reply_queue[consumer]);
1919 if (context >= instance->max_fw_cmds) {
1920 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
1925 cmd = instance->cmd_list[context];
1927 megasas_complete_cmd(instance, cmd, DID_OK);
1930 if (consumer == (instance->max_fw_cmds + 1)) {
1935 *instance->consumer = cpu_to_le32(producer);
1937 spin_unlock_irqrestore(&instance->completion_lock, flags);
1940 * Check if we can restore can_queue
1942 megasas_check_and_restore_queue_depth(instance);
1946 * megasas_start_timer - Initializes a timer object
1947 * @instance: Adapter soft state
1948 * @timer: timer object to be initialized
1949 * @fn: timer function
1950 * @interval: time interval between timer function call
1953 void megasas_start_timer(struct megasas_instance *instance,
1954 struct timer_list *timer,
1955 void *fn, unsigned long interval)
1958 timer->expires = jiffies + interval;
1959 timer->data = (unsigned long)instance;
1960 timer->function = fn;
1965 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1968 process_fw_state_change_wq(struct work_struct *work);
1970 void megasas_do_ocr(struct megasas_instance *instance)
1972 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1973 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1974 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1975 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
1977 instance->instancet->disable_intr(instance);
1978 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1979 instance->issuepend_done = 0;
1981 atomic_set(&instance->fw_outstanding, 0);
1982 megasas_internal_reset_defer_cmds(instance);
1983 process_fw_state_change_wq(&instance->work_init);
1986 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1989 struct megasas_cmd *cmd;
1990 struct megasas_dcmd_frame *dcmd;
1991 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
1992 dma_addr_t new_affiliation_111_h;
1996 cmd = megasas_get_cmd(instance);
1999 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2000 "Failed to get cmd for scsi%d\n",
2001 instance->host->host_no);
2005 dcmd = &cmd->frame->dcmd;
2007 if (!instance->vf_affiliation_111) {
2008 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2009 "affiliation for scsi%d\n", instance->host->host_no);
2010 megasas_return_cmd(instance, cmd);
2015 memset(instance->vf_affiliation_111, 0,
2016 sizeof(struct MR_LD_VF_AFFILIATION_111));
2018 new_affiliation_111 =
2019 pci_alloc_consistent(instance->pdev,
2020 sizeof(struct MR_LD_VF_AFFILIATION_111),
2021 &new_affiliation_111_h);
2022 if (!new_affiliation_111) {
2023 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2024 "memory for new affiliation for scsi%d\n",
2025 instance->host->host_no);
2026 megasas_return_cmd(instance, cmd);
2029 memset(new_affiliation_111, 0,
2030 sizeof(struct MR_LD_VF_AFFILIATION_111));
2033 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2035 dcmd->cmd = MFI_CMD_DCMD;
2036 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2037 dcmd->sge_count = 1;
2038 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2041 dcmd->data_xfer_len =
2042 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2043 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2046 dcmd->sgl.sge32[0].phys_addr =
2047 cpu_to_le32(instance->vf_affiliation_111_h);
2049 dcmd->sgl.sge32[0].phys_addr =
2050 cpu_to_le32(new_affiliation_111_h);
2052 dcmd->sgl.sge32[0].length = cpu_to_le32(
2053 sizeof(struct MR_LD_VF_AFFILIATION_111));
2055 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2056 "scsi%d\n", instance->host->host_no);
2058 megasas_issue_blocked_cmd(instance, cmd, 0);
2060 if (dcmd->cmd_status) {
2061 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2062 " failed with status 0x%x for scsi%d\n",
2063 dcmd->cmd_status, instance->host->host_no);
2064 retval = 1; /* Do a scan if we couldn't get affiliation */
2069 thisVf = new_affiliation_111->thisVf;
2070 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2071 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2072 new_affiliation_111->map[ld].policy[thisVf]) {
2073 dev_warn(&instance->pdev->dev, "SR-IOV: "
2074 "Got new LD/VF affiliation for scsi%d\n",
2075 instance->host->host_no);
2076 memcpy(instance->vf_affiliation_111,
2077 new_affiliation_111,
2078 sizeof(struct MR_LD_VF_AFFILIATION_111));
2084 if (new_affiliation_111) {
2085 pci_free_consistent(instance->pdev,
2086 sizeof(struct MR_LD_VF_AFFILIATION_111),
2087 new_affiliation_111,
2088 new_affiliation_111_h);
2091 megasas_return_cmd(instance, cmd);
2096 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2099 struct megasas_cmd *cmd;
2100 struct megasas_dcmd_frame *dcmd;
2101 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2102 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2103 dma_addr_t new_affiliation_h;
2104 int i, j, retval = 0, found = 0, doscan = 0;
2107 cmd = megasas_get_cmd(instance);
2110 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2111 "Failed to get cmd for scsi%d\n",
2112 instance->host->host_no);
2116 dcmd = &cmd->frame->dcmd;
2118 if (!instance->vf_affiliation) {
2119 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2120 "affiliation for scsi%d\n", instance->host->host_no);
2121 megasas_return_cmd(instance, cmd);
2126 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2127 sizeof(struct MR_LD_VF_AFFILIATION));
2130 pci_alloc_consistent(instance->pdev,
2131 (MAX_LOGICAL_DRIVES + 1) *
2132 sizeof(struct MR_LD_VF_AFFILIATION),
2133 &new_affiliation_h);
2134 if (!new_affiliation) {
2135 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2136 "memory for new affiliation for scsi%d\n",
2137 instance->host->host_no);
2138 megasas_return_cmd(instance, cmd);
2141 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2142 sizeof(struct MR_LD_VF_AFFILIATION));
2145 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2147 dcmd->cmd = MFI_CMD_DCMD;
2148 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2149 dcmd->sge_count = 1;
2150 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2153 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2154 sizeof(struct MR_LD_VF_AFFILIATION));
2155 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2158 dcmd->sgl.sge32[0].phys_addr =
2159 cpu_to_le32(instance->vf_affiliation_h);
2161 dcmd->sgl.sge32[0].phys_addr =
2162 cpu_to_le32(new_affiliation_h);
2164 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2165 sizeof(struct MR_LD_VF_AFFILIATION));
2167 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2168 "scsi%d\n", instance->host->host_no);
2170 megasas_issue_blocked_cmd(instance, cmd, 0);
2172 if (dcmd->cmd_status) {
2173 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2174 " failed with status 0x%x for scsi%d\n",
2175 dcmd->cmd_status, instance->host->host_no);
2176 retval = 1; /* Do a scan if we couldn't get affiliation */
2181 if (!new_affiliation->ldCount) {
2182 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2183 "affiliation for passive path for scsi%d\n",
2184 instance->host->host_no);
2188 newmap = new_affiliation->map;
2189 savedmap = instance->vf_affiliation->map;
2190 thisVf = new_affiliation->thisVf;
2191 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2193 for (j = 0; j < instance->vf_affiliation->ldCount;
2195 if (newmap->ref.targetId ==
2196 savedmap->ref.targetId) {
2198 if (newmap->policy[thisVf] !=
2199 savedmap->policy[thisVf]) {
2204 savedmap = (struct MR_LD_VF_MAP *)
2205 ((unsigned char *)savedmap +
2208 if (!found && newmap->policy[thisVf] !=
2209 MR_LD_ACCESS_HIDDEN) {
2213 newmap = (struct MR_LD_VF_MAP *)
2214 ((unsigned char *)newmap + newmap->size);
2217 newmap = new_affiliation->map;
2218 savedmap = instance->vf_affiliation->map;
2220 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2222 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2223 if (savedmap->ref.targetId ==
2224 newmap->ref.targetId) {
2226 if (savedmap->policy[thisVf] !=
2227 newmap->policy[thisVf]) {
2232 newmap = (struct MR_LD_VF_MAP *)
2233 ((unsigned char *)newmap +
2236 if (!found && savedmap->policy[thisVf] !=
2237 MR_LD_ACCESS_HIDDEN) {
2241 savedmap = (struct MR_LD_VF_MAP *)
2242 ((unsigned char *)savedmap +
2248 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2249 "affiliation for scsi%d\n", instance->host->host_no);
2250 memcpy(instance->vf_affiliation, new_affiliation,
2251 new_affiliation->size);
2255 if (new_affiliation)
2256 pci_free_consistent(instance->pdev,
2257 (MAX_LOGICAL_DRIVES + 1) *
2258 sizeof(struct MR_LD_VF_AFFILIATION),
2259 new_affiliation, new_affiliation_h);
2260 megasas_return_cmd(instance, cmd);
2265 /* This function will get the current SR-IOV LD/VF affiliation */
2266 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2271 if (instance->PlasmaFW111)
2272 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2274 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2278 /* This function will tell FW to start the SR-IOV heartbeat */
2279 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2282 struct megasas_cmd *cmd;
2283 struct megasas_dcmd_frame *dcmd;
2286 cmd = megasas_get_cmd(instance);
2289 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2290 "Failed to get cmd for scsi%d\n",
2291 instance->host->host_no);
2295 dcmd = &cmd->frame->dcmd;
2298 instance->hb_host_mem =
2299 pci_zalloc_consistent(instance->pdev,
2300 sizeof(struct MR_CTRL_HB_HOST_MEM),
2301 &instance->hb_host_mem_h);
2302 if (!instance->hb_host_mem) {
2303 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2304 " memory for heartbeat host memory for scsi%d\n",
2305 instance->host->host_no);
2311 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2313 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2314 dcmd->cmd = MFI_CMD_DCMD;
2315 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2316 dcmd->sge_count = 1;
2317 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2320 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2321 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2322 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2323 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2325 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2326 instance->host->host_no);
2328 if (instance->ctrl_context && !instance->mask_interrupts)
2329 retval = megasas_issue_blocked_cmd(instance, cmd,
2330 MEGASAS_ROUTINE_WAIT_TIME_VF);
2332 retval = megasas_issue_polled(instance, cmd);
2335 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2336 "_MEM_ALLOC DCMD %s for scsi%d\n",
2337 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2338 "timed out" : "failed", instance->host->host_no);
2343 megasas_return_cmd(instance, cmd);
2348 /* Handler for SR-IOV heartbeat */
2349 void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2351 struct megasas_instance *instance =
2352 (struct megasas_instance *)instance_addr;
2354 if (instance->hb_host_mem->HB.fwCounter !=
2355 instance->hb_host_mem->HB.driverCounter) {
2356 instance->hb_host_mem->HB.driverCounter =
2357 instance->hb_host_mem->HB.fwCounter;
2358 mod_timer(&instance->sriov_heartbeat_timer,
2359 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2361 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2362 "completed for scsi%d\n", instance->host->host_no);
2363 schedule_work(&instance->work_init);
2368 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2369 * @instance: Adapter soft state
2371 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2372 * complete all its outstanding commands. Returns error if one or more IOs
2373 * are pending after this time period. It also marks the controller dead.
2375 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2379 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2381 unsigned long flags;
2382 struct list_head clist_local;
2383 struct megasas_cmd *reset_cmd;
2385 u8 kill_adapter_flag;
2387 spin_lock_irqsave(&instance->hba_lock, flags);
2388 adprecovery = instance->adprecovery;
2389 spin_unlock_irqrestore(&instance->hba_lock, flags);
2391 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2393 INIT_LIST_HEAD(&clist_local);
2394 spin_lock_irqsave(&instance->hba_lock, flags);
2395 list_splice_init(&instance->internal_reset_pending_q,
2397 spin_unlock_irqrestore(&instance->hba_lock, flags);
2399 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2400 for (i = 0; i < wait_time; i++) {
2402 spin_lock_irqsave(&instance->hba_lock, flags);
2403 adprecovery = instance->adprecovery;
2404 spin_unlock_irqrestore(&instance->hba_lock, flags);
2405 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2409 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2410 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2411 spin_lock_irqsave(&instance->hba_lock, flags);
2412 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2413 spin_unlock_irqrestore(&instance->hba_lock, flags);
2418 while (!list_empty(&clist_local)) {
2419 reset_cmd = list_entry((&clist_local)->next,
2420 struct megasas_cmd, list);
2421 list_del_init(&reset_cmd->list);
2422 if (reset_cmd->scmd) {
2423 reset_cmd->scmd->result = DID_RESET << 16;
2424 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2425 reset_index, reset_cmd,
2426 reset_cmd->scmd->cmnd[0]);
2428 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2429 megasas_return_cmd(instance, reset_cmd);
2430 } else if (reset_cmd->sync_cmd) {
2431 dev_notice(&instance->pdev->dev, "%p synch cmds"
2435 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2436 instance->instancet->fire_cmd(instance,
2437 reset_cmd->frame_phys_addr,
2438 0, instance->reg_set);
2440 dev_notice(&instance->pdev->dev, "%p unexpected"
2450 for (i = 0; i < resetwaittime; i++) {
2451 int outstanding = atomic_read(&instance->fw_outstanding);
2456 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2457 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2458 "commands to complete\n",i,outstanding);
2460 * Call cmd completion routine. Cmd to be
2461 * be completed directly without depending on isr.
2463 megasas_complete_cmd_dpc((unsigned long)instance);
2470 kill_adapter_flag = 0;
2472 fw_state = instance->instancet->read_fw_status_reg(
2473 instance->reg_set) & MFI_STATE_MASK;
2474 if ((fw_state == MFI_STATE_FAULT) &&
2475 (instance->disableOnlineCtrlReset == 0)) {
2477 kill_adapter_flag = 2;
2480 megasas_do_ocr(instance);
2481 kill_adapter_flag = 1;
2483 /* wait for 1 secs to let FW finish the pending cmds */
2489 if (atomic_read(&instance->fw_outstanding) && !kill_adapter_flag) {
2490 if (instance->disableOnlineCtrlReset == 0) {
2491 megasas_do_ocr(instance);
2493 /* wait for 5 secs to let FW finish the pending cmds */
2494 for (i = 0; i < wait_time; i++) {
2496 atomic_read(&instance->fw_outstanding);
2504 if (atomic_read(&instance->fw_outstanding) ||
2505 (kill_adapter_flag == 2)) {
2506 dev_notice(&instance->pdev->dev, "pending cmds after reset\n");
2508 * Send signal to FW to stop processing any pending cmds.
2509 * The controller will be taken offline by the OS now.
2511 if ((instance->pdev->device ==
2512 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2513 (instance->pdev->device ==
2514 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2515 writel(MFI_STOP_ADP,
2516 &instance->reg_set->doorbell);
2518 writel(MFI_STOP_ADP,
2519 &instance->reg_set->inbound_doorbell);
2521 megasas_dump_pending_frames(instance);
2522 spin_lock_irqsave(&instance->hba_lock, flags);
2523 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2524 spin_unlock_irqrestore(&instance->hba_lock, flags);
2528 dev_notice(&instance->pdev->dev, "no pending cmds after reset\n");
2534 * megasas_generic_reset - Generic reset routine
2535 * @scmd: Mid-layer SCSI command
2537 * This routine implements a generic reset handler for device, bus and host
2538 * reset requests. Device, bus and host specific reset handlers can use this
2539 * function after they do their specific tasks.
2541 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2544 struct megasas_instance *instance;
2546 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2548 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2549 scmd->cmnd[0], scmd->retries);
2551 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2552 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2556 ret_val = megasas_wait_for_outstanding(instance);
2557 if (ret_val == SUCCESS)
2558 dev_notice(&instance->pdev->dev, "reset successful\n");
2560 dev_err(&instance->pdev->dev, "failed to do reset\n");
2566 * megasas_reset_timer - quiesce the adapter if required
2569 * Sets the FW busy flag and reduces the host->can_queue if the
2570 * cmd has not been completed within the timeout period.
2573 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2575 struct megasas_instance *instance;
2576 unsigned long flags;
2578 if (time_after(jiffies, scmd->jiffies_at_alloc +
2579 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
2580 return BLK_EH_NOT_HANDLED;
2583 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2584 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2585 /* FW is busy, throttle IO */
2586 spin_lock_irqsave(instance->host->host_lock, flags);
2588 instance->host->can_queue = instance->throttlequeuedepth;
2589 instance->last_time = jiffies;
2590 instance->flag |= MEGASAS_FW_BUSY;
2592 spin_unlock_irqrestore(instance->host->host_lock, flags);
2594 return BLK_EH_RESET_TIMER;
2598 * megasas_reset_device - Device reset handler entry point
2600 static int megasas_reset_device(struct scsi_cmnd *scmd)
2603 * First wait for all commands to complete
2605 return megasas_generic_reset(scmd);
2609 * megasas_reset_bus_host - Bus & host reset handler entry point
2611 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2614 struct megasas_instance *instance;
2616 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2619 * First wait for all commands to complete
2621 if (instance->ctrl_context)
2622 ret = megasas_reset_fusion(scmd->device->host, 1);
2624 ret = megasas_generic_reset(scmd);
2630 * megasas_bios_param - Returns disk geometry for a disk
2631 * @sdev: device handle
2632 * @bdev: block device
2633 * @capacity: drive capacity
2634 * @geom: geometry parameters
2637 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2638 sector_t capacity, int geom[])
2645 /* Default heads (64) & sectors (32) */
2649 tmp = heads * sectors;
2650 cylinders = capacity;
2652 sector_div(cylinders, tmp);
2655 * Handle extended translation size for logical drives > 1Gb
2658 if (capacity >= 0x200000) {
2661 tmp = heads*sectors;
2662 cylinders = capacity;
2663 sector_div(cylinders, tmp);
2668 geom[2] = cylinders;
2673 static void megasas_aen_polling(struct work_struct *work);
2676 * megasas_service_aen - Processes an event notification
2677 * @instance: Adapter soft state
2678 * @cmd: AEN command completed by the ISR
2680 * For AEN, driver sends a command down to FW that is held by the FW till an
2681 * event occurs. When an event of interest occurs, FW completes the command
2682 * that it was previously holding.
2684 * This routines sends SIGIO signal to processes that have registered with the
2688 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2690 unsigned long flags;
2693 * Don't signal app if it is just an aborted previously registered aen
2695 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2696 spin_lock_irqsave(&poll_aen_lock, flags);
2697 megasas_poll_wait_aen = 1;
2698 spin_unlock_irqrestore(&poll_aen_lock, flags);
2699 wake_up(&megasas_poll_wait);
2700 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2705 instance->aen_cmd = NULL;
2707 megasas_return_cmd(instance, cmd);
2709 if ((instance->unload == 0) &&
2710 ((instance->issuepend_done == 1))) {
2711 struct megasas_aen_event *ev;
2713 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2715 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
2717 ev->instance = instance;
2719 INIT_DELAYED_WORK(&ev->hotplug_work,
2720 megasas_aen_polling);
2721 schedule_delayed_work(&ev->hotplug_work, 0);
2727 megasas_fw_crash_buffer_store(struct device *cdev,
2728 struct device_attribute *attr, const char *buf, size_t count)
2730 struct Scsi_Host *shost = class_to_shost(cdev);
2731 struct megasas_instance *instance =
2732 (struct megasas_instance *) shost->hostdata;
2734 unsigned long flags;
2736 if (kstrtoint(buf, 0, &val) != 0)
2739 spin_lock_irqsave(&instance->crashdump_lock, flags);
2740 instance->fw_crash_buffer_offset = val;
2741 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2746 megasas_fw_crash_buffer_show(struct device *cdev,
2747 struct device_attribute *attr, char *buf)
2749 struct Scsi_Host *shost = class_to_shost(cdev);
2750 struct megasas_instance *instance =
2751 (struct megasas_instance *) shost->hostdata;
2753 unsigned long buff_addr;
2754 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2755 unsigned long chunk_left_bytes;
2756 unsigned long src_addr;
2757 unsigned long flags;
2760 spin_lock_irqsave(&instance->crashdump_lock, flags);
2761 buff_offset = instance->fw_crash_buffer_offset;
2762 if (!instance->crash_dump_buf &&
2763 !((instance->fw_crash_state == AVAILABLE) ||
2764 (instance->fw_crash_state == COPYING))) {
2765 dev_err(&instance->pdev->dev,
2766 "Firmware crash dump is not available\n");
2767 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2771 buff_addr = (unsigned long) buf;
2773 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
2774 dev_err(&instance->pdev->dev,
2775 "Firmware crash dump offset is out of range\n");
2776 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2780 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2781 chunk_left_bytes = dmachunk - (buff_offset % dmachunk);
2782 size = (size > chunk_left_bytes) ? chunk_left_bytes : size;
2783 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2785 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2786 (buff_offset % dmachunk);
2787 memcpy(buf, (void *)src_addr, size);
2788 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2794 megasas_fw_crash_buffer_size_show(struct device *cdev,
2795 struct device_attribute *attr, char *buf)
2797 struct Scsi_Host *shost = class_to_shost(cdev);
2798 struct megasas_instance *instance =
2799 (struct megasas_instance *) shost->hostdata;
2801 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2802 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2806 megasas_fw_crash_state_store(struct device *cdev,
2807 struct device_attribute *attr, const char *buf, size_t count)
2809 struct Scsi_Host *shost = class_to_shost(cdev);
2810 struct megasas_instance *instance =
2811 (struct megasas_instance *) shost->hostdata;
2813 unsigned long flags;
2815 if (kstrtoint(buf, 0, &val) != 0)
2818 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2819 dev_err(&instance->pdev->dev, "application updates invalid "
2820 "firmware crash state\n");
2824 instance->fw_crash_state = val;
2826 if ((val == COPIED) || (val == COPY_ERROR)) {
2827 spin_lock_irqsave(&instance->crashdump_lock, flags);
2828 megasas_free_host_crash_buffer(instance);
2829 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2830 if (val == COPY_ERROR)
2831 dev_info(&instance->pdev->dev, "application failed to "
2832 "copy Firmware crash dump\n");
2834 dev_info(&instance->pdev->dev, "Firmware crash dump "
2835 "copied successfully\n");
2841 megasas_fw_crash_state_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 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2852 megasas_page_size_show(struct device *cdev,
2853 struct device_attribute *attr, char *buf)
2855 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2858 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2859 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2860 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2861 megasas_fw_crash_buffer_size_show, NULL);
2862 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2863 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2864 static DEVICE_ATTR(page_size, S_IRUGO,
2865 megasas_page_size_show, NULL);
2867 struct device_attribute *megaraid_host_attrs[] = {
2868 &dev_attr_fw_crash_buffer_size,
2869 &dev_attr_fw_crash_buffer,
2870 &dev_attr_fw_crash_state,
2871 &dev_attr_page_size,
2876 * Scsi host template for megaraid_sas driver
2878 static struct scsi_host_template megasas_template = {
2880 .module = THIS_MODULE,
2881 .name = "Avago SAS based MegaRAID driver",
2882 .proc_name = "megaraid_sas",
2883 .slave_configure = megasas_slave_configure,
2884 .slave_alloc = megasas_slave_alloc,
2885 .queuecommand = megasas_queue_command,
2886 .eh_device_reset_handler = megasas_reset_device,
2887 .eh_bus_reset_handler = megasas_reset_bus_host,
2888 .eh_host_reset_handler = megasas_reset_bus_host,
2889 .eh_timed_out = megasas_reset_timer,
2890 .shost_attrs = megaraid_host_attrs,
2891 .bios_param = megasas_bios_param,
2892 .use_clustering = ENABLE_CLUSTERING,
2893 .change_queue_depth = scsi_change_queue_depth,
2898 * megasas_complete_int_cmd - Completes an internal command
2899 * @instance: Adapter soft state
2900 * @cmd: Command to be completed
2902 * The megasas_issue_blocked_cmd() function waits for a command to complete
2903 * after it issues a command. This function wakes up that waiting routine by
2904 * calling wake_up() on the wait queue.
2907 megasas_complete_int_cmd(struct megasas_instance *instance,
2908 struct megasas_cmd *cmd)
2910 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
2911 wake_up(&instance->int_cmd_wait_q);
2915 * megasas_complete_abort - Completes aborting a command
2916 * @instance: Adapter soft state
2917 * @cmd: Cmd that was issued to abort another cmd
2919 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2920 * after it issues an abort on a previously issued command. This function
2921 * wakes up all functions waiting on the same wait queue.
2924 megasas_complete_abort(struct megasas_instance *instance,
2925 struct megasas_cmd *cmd)
2927 if (cmd->sync_cmd) {
2929 cmd->cmd_status_drv = 0;
2930 wake_up(&instance->abort_cmd_wait_q);
2935 * megasas_complete_cmd - Completes a command
2936 * @instance: Adapter soft state
2937 * @cmd: Command to be completed
2938 * @alt_status: If non-zero, use this value as status to
2939 * SCSI mid-layer instead of the value returned
2940 * by the FW. This should be used if caller wants
2941 * an alternate status (as in the case of aborted
2945 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2949 struct megasas_header *hdr = &cmd->frame->hdr;
2950 unsigned long flags;
2951 struct fusion_context *fusion = instance->ctrl_context;
2954 /* flag for the retry reset */
2955 cmd->retry_for_fw_reset = 0;
2958 cmd->scmd->SCp.ptr = NULL;
2961 case MFI_CMD_INVALID:
2962 /* Some older 1068 controller FW may keep a pended
2963 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2964 when booting the kdump kernel. Ignore this command to
2965 prevent a kernel panic on shutdown of the kdump kernel. */
2966 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
2968 dev_warn(&instance->pdev->dev, "If you have a controller "
2969 "other than PERC5, please upgrade your firmware\n");
2971 case MFI_CMD_PD_SCSI_IO:
2972 case MFI_CMD_LD_SCSI_IO:
2975 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2976 * issued either through an IO path or an IOCTL path. If it
2977 * was via IOCTL, we will send it to internal completion.
2979 if (cmd->sync_cmd) {
2981 megasas_complete_int_cmd(instance, cmd);
2985 case MFI_CMD_LD_READ:
2986 case MFI_CMD_LD_WRITE:
2989 cmd->scmd->result = alt_status << 16;
2995 atomic_dec(&instance->fw_outstanding);
2997 scsi_dma_unmap(cmd->scmd);
2998 cmd->scmd->scsi_done(cmd->scmd);
2999 megasas_return_cmd(instance, cmd);
3004 switch (hdr->cmd_status) {
3007 cmd->scmd->result = DID_OK << 16;
3010 case MFI_STAT_SCSI_IO_FAILED:
3011 case MFI_STAT_LD_INIT_IN_PROGRESS:
3013 (DID_ERROR << 16) | hdr->scsi_status;
3016 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3018 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3020 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3021 memset(cmd->scmd->sense_buffer, 0,
3022 SCSI_SENSE_BUFFERSIZE);
3023 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3026 cmd->scmd->result |= DRIVER_SENSE << 24;
3031 case MFI_STAT_LD_OFFLINE:
3032 case MFI_STAT_DEVICE_NOT_FOUND:
3033 cmd->scmd->result = DID_BAD_TARGET << 16;
3037 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3039 cmd->scmd->result = DID_ERROR << 16;
3043 atomic_dec(&instance->fw_outstanding);
3045 scsi_dma_unmap(cmd->scmd);
3046 cmd->scmd->scsi_done(cmd->scmd);
3047 megasas_return_cmd(instance, cmd);
3054 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3055 /* Check for LD map update */
3056 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3057 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3058 fusion->fast_path_io = 0;
3059 spin_lock_irqsave(instance->host->host_lock, flags);
3060 instance->map_update_cmd = NULL;
3061 if (cmd->frame->hdr.cmd_status != 0) {
3062 if (cmd->frame->hdr.cmd_status !=
3064 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3065 cmd->frame->hdr.cmd_status);
3067 megasas_return_cmd(instance, cmd);
3068 spin_unlock_irqrestore(
3069 instance->host->host_lock,
3075 megasas_return_cmd(instance, cmd);
3078 * Set fast path IO to ZERO.
3079 * Validate Map will set proper value.
3080 * Meanwhile all IOs will go as LD IO.
3082 if (MR_ValidateMapInfo(instance))
3083 fusion->fast_path_io = 1;
3085 fusion->fast_path_io = 0;
3086 megasas_sync_map_info(instance);
3087 spin_unlock_irqrestore(instance->host->host_lock,
3091 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3092 opcode == MR_DCMD_CTRL_EVENT_GET) {
3093 spin_lock_irqsave(&poll_aen_lock, flags);
3094 megasas_poll_wait_aen = 0;
3095 spin_unlock_irqrestore(&poll_aen_lock, flags);
3098 /* FW has an updated PD sequence */
3099 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3100 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3102 spin_lock_irqsave(instance->host->host_lock, flags);
3103 status = cmd->frame->hdr.cmd_status;
3104 instance->jbod_seq_cmd = NULL;
3105 megasas_return_cmd(instance, cmd);
3107 if (status == MFI_STAT_OK) {
3108 instance->pd_seq_map_id++;
3109 /* Re-register a pd sync seq num cmd */
3110 if (megasas_sync_pd_seq_num(instance, true))
3111 instance->use_seqnum_jbod_fp = false;
3113 instance->use_seqnum_jbod_fp = false;
3115 spin_unlock_irqrestore(instance->host->host_lock, flags);
3120 * See if got an event notification
3122 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3123 megasas_service_aen(instance, cmd);
3125 megasas_complete_int_cmd(instance, cmd);
3131 * Cmd issued to abort another cmd returned
3133 megasas_complete_abort(instance, cmd);
3137 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3144 * megasas_issue_pending_cmds_again - issue all pending cmds
3145 * in FW again because of the fw reset
3146 * @instance: Adapter soft state
3149 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3151 struct megasas_cmd *cmd;
3152 struct list_head clist_local;
3153 union megasas_evt_class_locale class_locale;
3154 unsigned long flags;
3157 INIT_LIST_HEAD(&clist_local);
3158 spin_lock_irqsave(&instance->hba_lock, flags);
3159 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3160 spin_unlock_irqrestore(&instance->hba_lock, flags);
3162 while (!list_empty(&clist_local)) {
3163 cmd = list_entry((&clist_local)->next,
3164 struct megasas_cmd, list);
3165 list_del_init(&cmd->list);
3167 if (cmd->sync_cmd || cmd->scmd) {
3168 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3169 "detected to be pending while HBA reset\n",
3170 cmd, cmd->scmd, cmd->sync_cmd);
3172 cmd->retry_for_fw_reset++;
3174 if (cmd->retry_for_fw_reset == 3) {
3175 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3176 "was tried multiple times during reset."
3177 "Shutting down the HBA\n",
3178 cmd, cmd->scmd, cmd->sync_cmd);
3179 instance->instancet->disable_intr(instance);
3180 atomic_set(&instance->fw_reset_no_pci_access, 1);
3181 megaraid_sas_kill_hba(instance);
3186 if (cmd->sync_cmd == 1) {
3188 dev_notice(&instance->pdev->dev, "unexpected"
3189 "cmd attached to internal command!\n");
3191 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3192 "on the internal reset queue,"
3193 "issue it again.\n", cmd);
3194 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3195 instance->instancet->fire_cmd(instance,
3196 cmd->frame_phys_addr,
3197 0, instance->reg_set);
3198 } else if (cmd->scmd) {
3199 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3200 "detected on the internal queue, issue again.\n",
3201 cmd, cmd->scmd->cmnd[0]);
3203 atomic_inc(&instance->fw_outstanding);
3204 instance->instancet->fire_cmd(instance,
3205 cmd->frame_phys_addr,
3206 cmd->frame_count-1, instance->reg_set);
3208 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3209 "internal reset defer list while re-issue!!\n",
3214 if (instance->aen_cmd) {
3215 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3216 megasas_return_cmd(instance, instance->aen_cmd);
3218 instance->aen_cmd = NULL;
3222 * Initiate AEN (Asynchronous Event Notification)
3224 seq_num = instance->last_seq_num;
3225 class_locale.members.reserved = 0;
3226 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3227 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3229 megasas_register_aen(instance, seq_num, class_locale.word);
3233 * Move the internal reset pending commands to a deferred queue.
3235 * We move the commands pending at internal reset time to a
3236 * pending queue. This queue would be flushed after successful
3237 * completion of the internal reset sequence. if the internal reset
3238 * did not complete in time, the kernel reset handler would flush
3242 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3244 struct megasas_cmd *cmd;
3246 u32 max_cmd = instance->max_fw_cmds;
3248 unsigned long flags;
3251 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3252 for (i = 0; i < max_cmd; i++) {
3253 cmd = instance->cmd_list[i];
3254 if (cmd->sync_cmd == 1 || cmd->scmd) {
3255 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3256 "on the defer queue as internal\n",
3257 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3259 if (!list_empty(&cmd->list)) {
3260 dev_notice(&instance->pdev->dev, "ERROR while"
3261 " moving this cmd:%p, %d %p, it was"
3262 "discovered on some list?\n",
3263 cmd, cmd->sync_cmd, cmd->scmd);
3265 list_del_init(&cmd->list);
3268 list_add_tail(&cmd->list,
3269 &instance->internal_reset_pending_q);
3272 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3277 process_fw_state_change_wq(struct work_struct *work)
3279 struct megasas_instance *instance =
3280 container_of(work, struct megasas_instance, work_init);
3282 unsigned long flags;
3284 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
3285 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3286 instance->adprecovery);
3290 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
3291 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3292 "state, restarting it...\n");
3294 instance->instancet->disable_intr(instance);
3295 atomic_set(&instance->fw_outstanding, 0);
3297 atomic_set(&instance->fw_reset_no_pci_access, 1);
3298 instance->instancet->adp_reset(instance, instance->reg_set);
3299 atomic_set(&instance->fw_reset_no_pci_access, 0);
3301 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3302 "initiating next stage...\n");
3304 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3305 "state 2 starting...\n");
3307 /* waiting for about 20 second before start the second init */
3308 for (wait = 0; wait < 30; wait++) {
3312 if (megasas_transition_to_ready(instance, 1)) {
3313 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3315 atomic_set(&instance->fw_reset_no_pci_access, 1);
3316 megaraid_sas_kill_hba(instance);
3320 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3321 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3322 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3324 *instance->consumer = *instance->producer;
3326 *instance->consumer = 0;
3327 *instance->producer = 0;
3330 megasas_issue_init_mfi(instance);
3332 spin_lock_irqsave(&instance->hba_lock, flags);
3333 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3334 spin_unlock_irqrestore(&instance->hba_lock, flags);
3335 instance->instancet->enable_intr(instance);
3337 megasas_issue_pending_cmds_again(instance);
3338 instance->issuepend_done = 1;
3343 * megasas_deplete_reply_queue - Processes all completed commands
3344 * @instance: Adapter soft state
3345 * @alt_status: Alternate status to be returned to
3346 * SCSI mid-layer instead of the status
3347 * returned by the FW
3348 * Note: this must be called with hba lock held
3351 megasas_deplete_reply_queue(struct megasas_instance *instance,
3357 if ((mfiStatus = instance->instancet->check_reset(instance,
3358 instance->reg_set)) == 1) {
3362 if ((mfiStatus = instance->instancet->clear_intr(
3365 /* Hardware may not set outbound_intr_status in MSI-X mode */
3366 if (!instance->msix_vectors)
3370 instance->mfiStatus = mfiStatus;
3372 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3373 fw_state = instance->instancet->read_fw_status_reg(
3374 instance->reg_set) & MFI_STATE_MASK;
3376 if (fw_state != MFI_STATE_FAULT) {
3377 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3381 if ((fw_state == MFI_STATE_FAULT) &&
3382 (instance->disableOnlineCtrlReset == 0)) {
3383 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3385 if ((instance->pdev->device ==
3386 PCI_DEVICE_ID_LSI_SAS1064R) ||
3387 (instance->pdev->device ==
3388 PCI_DEVICE_ID_DELL_PERC5) ||
3389 (instance->pdev->device ==
3390 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3392 *instance->consumer =
3393 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3397 instance->instancet->disable_intr(instance);
3398 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
3399 instance->issuepend_done = 0;
3401 atomic_set(&instance->fw_outstanding, 0);
3402 megasas_internal_reset_defer_cmds(instance);
3404 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3405 fw_state, instance->adprecovery);
3407 schedule_work(&instance->work_init);
3411 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3412 fw_state, instance->disableOnlineCtrlReset);
3416 tasklet_schedule(&instance->isr_tasklet);
3420 * megasas_isr - isr entry point
3422 static irqreturn_t megasas_isr(int irq, void *devp)
3424 struct megasas_irq_context *irq_context = devp;
3425 struct megasas_instance *instance = irq_context->instance;
3426 unsigned long flags;
3429 if (atomic_read(&instance->fw_reset_no_pci_access))
3432 spin_lock_irqsave(&instance->hba_lock, flags);
3433 rc = megasas_deplete_reply_queue(instance, DID_OK);
3434 spin_unlock_irqrestore(&instance->hba_lock, flags);
3440 * megasas_transition_to_ready - Move the FW to READY state
3441 * @instance: Adapter soft state
3443 * During the initialization, FW passes can potentially be in any one of
3444 * several possible states. If the FW in operational, waiting-for-handshake
3445 * states, driver must take steps to bring it to ready state. Otherwise, it
3446 * has to wait for the ready state.
3449 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3455 u32 abs_state, curr_abs_state;
3457 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3458 fw_state = abs_state & MFI_STATE_MASK;
3460 if (fw_state != MFI_STATE_READY)
3461 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3464 while (fw_state != MFI_STATE_READY) {
3468 case MFI_STATE_FAULT:
3469 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3471 max_wait = MEGASAS_RESET_WAIT_TIME;
3472 cur_state = MFI_STATE_FAULT;
3477 case MFI_STATE_WAIT_HANDSHAKE:
3479 * Set the CLR bit in inbound doorbell
3481 if ((instance->pdev->device ==
3482 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3483 (instance->pdev->device ==
3484 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3485 (instance->ctrl_context))
3487 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3488 &instance->reg_set->doorbell);
3491 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3492 &instance->reg_set->inbound_doorbell);
3494 max_wait = MEGASAS_RESET_WAIT_TIME;
3495 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3498 case MFI_STATE_BOOT_MESSAGE_PENDING:
3499 if ((instance->pdev->device ==
3500 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3501 (instance->pdev->device ==
3502 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3503 (instance->ctrl_context))
3504 writel(MFI_INIT_HOTPLUG,
3505 &instance->reg_set->doorbell);
3507 writel(MFI_INIT_HOTPLUG,
3508 &instance->reg_set->inbound_doorbell);
3510 max_wait = MEGASAS_RESET_WAIT_TIME;
3511 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3514 case MFI_STATE_OPERATIONAL:
3516 * Bring it to READY state; assuming max wait 10 secs
3518 instance->instancet->disable_intr(instance);
3519 if ((instance->pdev->device ==
3520 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3521 (instance->pdev->device ==
3522 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3523 (instance->ctrl_context)) {
3524 writel(MFI_RESET_FLAGS,
3525 &instance->reg_set->doorbell);
3527 if (instance->ctrl_context) {
3528 for (i = 0; i < (10 * 1000); i += 20) {
3539 writel(MFI_RESET_FLAGS,
3540 &instance->reg_set->inbound_doorbell);
3542 max_wait = MEGASAS_RESET_WAIT_TIME;
3543 cur_state = MFI_STATE_OPERATIONAL;
3546 case MFI_STATE_UNDEFINED:
3548 * This state should not last for more than 2 seconds
3550 max_wait = MEGASAS_RESET_WAIT_TIME;
3551 cur_state = MFI_STATE_UNDEFINED;
3554 case MFI_STATE_BB_INIT:
3555 max_wait = MEGASAS_RESET_WAIT_TIME;
3556 cur_state = MFI_STATE_BB_INIT;
3559 case MFI_STATE_FW_INIT:
3560 max_wait = MEGASAS_RESET_WAIT_TIME;
3561 cur_state = MFI_STATE_FW_INIT;
3564 case MFI_STATE_FW_INIT_2:
3565 max_wait = MEGASAS_RESET_WAIT_TIME;
3566 cur_state = MFI_STATE_FW_INIT_2;
3569 case MFI_STATE_DEVICE_SCAN:
3570 max_wait = MEGASAS_RESET_WAIT_TIME;
3571 cur_state = MFI_STATE_DEVICE_SCAN;
3574 case MFI_STATE_FLUSH_CACHE:
3575 max_wait = MEGASAS_RESET_WAIT_TIME;
3576 cur_state = MFI_STATE_FLUSH_CACHE;
3580 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3586 * The cur_state should not last for more than max_wait secs
3588 for (i = 0; i < max_wait * 50; i++) {
3589 curr_abs_state = instance->instancet->
3590 read_fw_status_reg(instance->reg_set);
3592 if (abs_state == curr_abs_state) {
3599 * Return error if fw_state hasn't changed after max_wait
3601 if (curr_abs_state == abs_state) {
3602 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3603 "in %d secs\n", fw_state, max_wait);
3607 abs_state = curr_abs_state;
3608 fw_state = curr_abs_state & MFI_STATE_MASK;
3610 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3616 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3617 * @instance: Adapter soft state
3619 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3622 u32 max_cmd = instance->max_mfi_cmds;
3623 struct megasas_cmd *cmd;
3625 if (!instance->frame_dma_pool)
3629 * Return all frames to pool
3631 for (i = 0; i < max_cmd; i++) {
3633 cmd = instance->cmd_list[i];
3636 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3637 cmd->frame_phys_addr);
3640 pci_pool_free(instance->sense_dma_pool, cmd->sense,
3641 cmd->sense_phys_addr);
3645 * Now destroy the pool itself
3647 pci_pool_destroy(instance->frame_dma_pool);
3648 pci_pool_destroy(instance->sense_dma_pool);
3650 instance->frame_dma_pool = NULL;
3651 instance->sense_dma_pool = NULL;
3655 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3656 * @instance: Adapter soft state
3658 * Each command packet has an embedded DMA memory buffer that is used for
3659 * filling MFI frame and the SG list that immediately follows the frame. This
3660 * function creates those DMA memory buffers for each command packet by using
3661 * PCI pool facility.
3663 static int megasas_create_frame_pool(struct megasas_instance *instance)
3670 struct megasas_cmd *cmd;
3672 max_cmd = instance->max_mfi_cmds;
3675 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3676 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3678 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3679 sizeof(struct megasas_sge32);
3681 if (instance->flag_ieee)
3682 sge_sz = sizeof(struct megasas_sge_skinny);
3685 * For MFI controllers.
3687 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3688 * Total 960 byte (15 MFI frame of 64 byte)
3690 * Fusion adapter require only 3 extra frame.
3691 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3692 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3693 * Total 192 byte (3 MFI frame of 64 byte)
3695 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
3696 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3698 * Use DMA pool facility provided by PCI layer
3700 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
3701 instance->pdev, total_sz, 256, 0);
3703 if (!instance->frame_dma_pool) {
3704 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
3708 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3709 instance->pdev, 128, 4, 0);
3711 if (!instance->sense_dma_pool) {
3712 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
3714 pci_pool_destroy(instance->frame_dma_pool);
3715 instance->frame_dma_pool = NULL;
3721 * Allocate and attach a frame to each of the commands in cmd_list.
3722 * By making cmd->index as the context instead of the &cmd, we can
3723 * always use 32bit context regardless of the architecture
3725 for (i = 0; i < max_cmd; i++) {
3727 cmd = instance->cmd_list[i];
3729 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3730 GFP_KERNEL, &cmd->frame_phys_addr);
3732 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3733 GFP_KERNEL, &cmd->sense_phys_addr);
3736 * megasas_teardown_frame_pool() takes care of freeing
3737 * whatever has been allocated
3739 if (!cmd->frame || !cmd->sense) {
3740 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
3741 megasas_teardown_frame_pool(instance);
3745 memset(cmd->frame, 0, total_sz);
3746 cmd->frame->io.context = cpu_to_le32(cmd->index);
3747 cmd->frame->io.pad_0 = 0;
3748 if (!instance->ctrl_context && reset_devices)
3749 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
3756 * megasas_free_cmds - Free all the cmds in the free cmd pool
3757 * @instance: Adapter soft state
3759 void megasas_free_cmds(struct megasas_instance *instance)
3763 /* First free the MFI frame pool */
3764 megasas_teardown_frame_pool(instance);
3766 /* Free all the commands in the cmd_list */
3767 for (i = 0; i < instance->max_mfi_cmds; i++)
3769 kfree(instance->cmd_list[i]);
3771 /* Free the cmd_list buffer itself */
3772 kfree(instance->cmd_list);
3773 instance->cmd_list = NULL;
3775 INIT_LIST_HEAD(&instance->cmd_pool);
3779 * megasas_alloc_cmds - Allocates the command packets
3780 * @instance: Adapter soft state
3782 * Each command that is issued to the FW, whether IO commands from the OS or
3783 * internal commands like IOCTLs, are wrapped in local data structure called
3784 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3787 * Each frame has a 32-bit field called context (tag). This context is used
3788 * to get back the megasas_cmd from the frame when a frame gets completed in
3789 * the ISR. Typically the address of the megasas_cmd itself would be used as
3790 * the context. But we wanted to keep the differences between 32 and 64 bit
3791 * systems to the mininum. We always use 32 bit integers for the context. In
3792 * this driver, the 32 bit values are the indices into an array cmd_list.
3793 * This array is used only to look up the megasas_cmd given the context. The
3794 * free commands themselves are maintained in a linked list called cmd_pool.
3796 int megasas_alloc_cmds(struct megasas_instance *instance)
3801 struct megasas_cmd *cmd;
3802 struct fusion_context *fusion;
3804 fusion = instance->ctrl_context;
3805 max_cmd = instance->max_mfi_cmds;
3808 * instance->cmd_list is an array of struct megasas_cmd pointers.
3809 * Allocate the dynamic array first and then allocate individual
3812 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3814 if (!instance->cmd_list) {
3815 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
3819 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3821 for (i = 0; i < max_cmd; i++) {
3822 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3825 if (!instance->cmd_list[i]) {
3827 for (j = 0; j < i; j++)
3828 kfree(instance->cmd_list[j]);
3830 kfree(instance->cmd_list);
3831 instance->cmd_list = NULL;
3837 for (i = 0; i < max_cmd; i++) {
3838 cmd = instance->cmd_list[i];
3839 memset(cmd, 0, sizeof(struct megasas_cmd));
3842 cmd->instance = instance;
3844 list_add_tail(&cmd->list, &instance->cmd_pool);
3848 * Create a frame pool and assign one frame to each cmd
3850 if (megasas_create_frame_pool(instance)) {
3851 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
3852 megasas_free_cmds(instance);
3860 * megasas_get_pd_list_info - Returns FW's pd_list structure
3861 * @instance: Adapter soft state
3862 * @pd_list: pd_list structure
3864 * Issues an internal command (DCMD) to get the FW's controller PD
3865 * list structure. This information is mainly used to find out SYSTEM
3866 * supported by the FW.
3869 megasas_get_pd_list(struct megasas_instance *instance)
3871 int ret = 0, pd_index = 0;
3872 struct megasas_cmd *cmd;
3873 struct megasas_dcmd_frame *dcmd;
3874 struct MR_PD_LIST *ci;
3875 struct MR_PD_ADDRESS *pd_addr;
3876 dma_addr_t ci_h = 0;
3878 cmd = megasas_get_cmd(instance);
3881 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
3885 dcmd = &cmd->frame->dcmd;
3887 ci = pci_alloc_consistent(instance->pdev,
3888 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3891 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
3892 megasas_return_cmd(instance, cmd);
3896 memset(ci, 0, sizeof(*ci));
3897 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3899 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3900 dcmd->mbox.b[1] = 0;
3901 dcmd->cmd = MFI_CMD_DCMD;
3902 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3903 dcmd->sge_count = 1;
3904 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3907 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3908 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3909 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3910 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3912 if (instance->ctrl_context && !instance->mask_interrupts)
3913 ret = megasas_issue_blocked_cmd(instance, cmd,
3914 MEGASAS_BLOCKED_CMD_TIMEOUT);
3916 ret = megasas_issue_polled(instance, cmd);
3919 * the following function will get the instance PD LIST.
3925 (le32_to_cpu(ci->count) <
3926 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3928 memset(instance->local_pd_list, 0,
3929 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3931 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
3933 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
3934 le16_to_cpu(pd_addr->deviceId);
3935 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
3936 pd_addr->scsiDevType;
3937 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
3941 memcpy(instance->pd_list, instance->local_pd_list,
3942 sizeof(instance->pd_list));
3945 pci_free_consistent(instance->pdev,
3946 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3949 megasas_return_cmd(instance, cmd);
3955 * megasas_get_ld_list_info - Returns FW's ld_list structure
3956 * @instance: Adapter soft state
3957 * @ld_list: ld_list structure
3959 * Issues an internal command (DCMD) to get the FW's controller PD
3960 * list structure. This information is mainly used to find out SYSTEM
3961 * supported by the FW.
3964 megasas_get_ld_list(struct megasas_instance *instance)
3966 int ret = 0, ld_index = 0, ids = 0;
3967 struct megasas_cmd *cmd;
3968 struct megasas_dcmd_frame *dcmd;
3969 struct MR_LD_LIST *ci;
3970 dma_addr_t ci_h = 0;
3973 cmd = megasas_get_cmd(instance);
3976 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
3980 dcmd = &cmd->frame->dcmd;
3982 ci = pci_alloc_consistent(instance->pdev,
3983 sizeof(struct MR_LD_LIST),
3987 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
3988 megasas_return_cmd(instance, cmd);
3992 memset(ci, 0, sizeof(*ci));
3993 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3995 if (instance->supportmax256vd)
3996 dcmd->mbox.b[0] = 1;
3997 dcmd->cmd = MFI_CMD_DCMD;
3998 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3999 dcmd->sge_count = 1;
4000 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4002 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4003 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4004 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4005 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
4008 if (instance->ctrl_context && !instance->mask_interrupts)
4009 ret = megasas_issue_blocked_cmd(instance, cmd,
4010 MEGASAS_BLOCKED_CMD_TIMEOUT);
4012 ret = megasas_issue_polled(instance, cmd);
4015 ld_count = le32_to_cpu(ci->ldCount);
4017 /* the following function will get the instance PD LIST */
4019 if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
4020 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4022 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4023 if (ci->ldList[ld_index].state != 0) {
4024 ids = ci->ldList[ld_index].ref.targetId;
4025 instance->ld_ids[ids] =
4026 ci->ldList[ld_index].ref.targetId;
4031 pci_free_consistent(instance->pdev,
4032 sizeof(struct MR_LD_LIST),
4036 megasas_return_cmd(instance, cmd);
4041 * megasas_ld_list_query - Returns FW's ld_list structure
4042 * @instance: Adapter soft state
4043 * @ld_list: ld_list structure
4045 * Issues an internal command (DCMD) to get the FW's controller PD
4046 * list structure. This information is mainly used to find out SYSTEM
4047 * supported by the FW.
4050 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4052 int ret = 0, ld_index = 0, ids = 0;
4053 struct megasas_cmd *cmd;
4054 struct megasas_dcmd_frame *dcmd;
4055 struct MR_LD_TARGETID_LIST *ci;
4056 dma_addr_t ci_h = 0;
4059 cmd = megasas_get_cmd(instance);
4062 dev_warn(&instance->pdev->dev,
4063 "megasas_ld_list_query: Failed to get cmd\n");
4067 dcmd = &cmd->frame->dcmd;
4069 ci = pci_alloc_consistent(instance->pdev,
4070 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4073 dev_warn(&instance->pdev->dev,
4074 "Failed to alloc mem for ld_list_query\n");
4075 megasas_return_cmd(instance, cmd);
4079 memset(ci, 0, sizeof(*ci));
4080 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4082 dcmd->mbox.b[0] = query_type;
4083 if (instance->supportmax256vd)
4084 dcmd->mbox.b[2] = 1;
4086 dcmd->cmd = MFI_CMD_DCMD;
4087 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4088 dcmd->sge_count = 1;
4089 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4091 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4092 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4093 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4094 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4097 if (instance->ctrl_context && !instance->mask_interrupts)
4098 ret = megasas_issue_blocked_cmd(instance, cmd,
4099 MEGASAS_BLOCKED_CMD_TIMEOUT);
4101 ret = megasas_issue_polled(instance, cmd);
4103 tgtid_count = le32_to_cpu(ci->count);
4105 if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
4106 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4107 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4108 ids = ci->targetId[ld_index];
4109 instance->ld_ids[ids] = ci->targetId[ld_index];
4114 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4117 megasas_return_cmd(instance, cmd);
4123 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4124 * instance : Controller's instance
4126 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4128 struct fusion_context *fusion;
4132 fusion = instance->ctrl_context;
4133 /* For MFI based controllers return dummy success */
4137 instance->supportmax256vd =
4138 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4139 /* Below is additional check to address future FW enhancement */
4140 if (instance->ctrl_info->max_lds > 64)
4141 instance->supportmax256vd = 1;
4143 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4144 * MEGASAS_MAX_DEV_PER_CHANNEL;
4145 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4146 * MEGASAS_MAX_DEV_PER_CHANNEL;
4147 if (instance->supportmax256vd) {
4148 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4149 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4151 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4152 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4155 dev_info(&instance->pdev->dev,
4156 "firmware type\t: %s\n",
4157 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4158 "Legacy(64 VD) firmware");
4160 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4161 (sizeof(struct MR_LD_SPAN_MAP) *
4162 (instance->fw_supported_vd_count - 1));
4163 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4164 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
4165 (sizeof(struct MR_LD_SPAN_MAP) *
4166 (instance->drv_supported_vd_count - 1));
4168 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4171 if (instance->supportmax256vd)
4172 fusion->current_map_sz = new_map_sz;
4174 fusion->current_map_sz = old_map_sz;
4178 * megasas_get_controller_info - Returns FW's controller structure
4179 * @instance: Adapter soft state
4181 * Issues an internal command (DCMD) to get the FW's controller structure.
4182 * This information is mainly used to find out the maximum IO transfer per
4183 * command supported by the FW.
4186 megasas_get_ctrl_info(struct megasas_instance *instance)
4189 struct megasas_cmd *cmd;
4190 struct megasas_dcmd_frame *dcmd;
4191 struct megasas_ctrl_info *ci;
4192 struct megasas_ctrl_info *ctrl_info;
4193 dma_addr_t ci_h = 0;
4195 ctrl_info = instance->ctrl_info;
4197 cmd = megasas_get_cmd(instance);
4200 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4204 dcmd = &cmd->frame->dcmd;
4206 ci = pci_alloc_consistent(instance->pdev,
4207 sizeof(struct megasas_ctrl_info), &ci_h);
4210 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
4211 megasas_return_cmd(instance, cmd);
4215 memset(ci, 0, sizeof(*ci));
4216 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4218 dcmd->cmd = MFI_CMD_DCMD;
4219 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4220 dcmd->sge_count = 1;
4221 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4224 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4225 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4226 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4227 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4228 dcmd->mbox.b[0] = 1;
4230 if (instance->ctrl_context && !instance->mask_interrupts)
4231 ret = megasas_issue_blocked_cmd(instance, cmd,
4232 MEGASAS_BLOCKED_CMD_TIMEOUT);
4234 ret = megasas_issue_polled(instance, cmd);
4237 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
4238 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4239 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4240 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4241 megasas_update_ext_vd_details(instance);
4242 instance->use_seqnum_jbod_fp =
4243 ctrl_info->adapterOperations3.useSeqNumJbodFP;
4244 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4245 dev_info(&instance->pdev->dev,
4246 "controller type\t: %s(%dMB)\n",
4247 instance->is_imr ? "iMR" : "MR",
4248 le16_to_cpu(ctrl_info->memory_size));
4249 instance->disableOnlineCtrlReset =
4250 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4251 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4252 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
4253 instance->secure_jbod_support =
4254 ctrl_info->adapterOperations3.supportSecurityonJBOD;
4255 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4256 instance->secure_jbod_support ? "Yes" : "No");
4259 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4262 megasas_return_cmd(instance, cmd);
4267 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4270 * @instance: Adapter soft state
4271 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4272 MR_CRASH_BUF_TURN_OFF = 0
4273 MR_CRASH_BUF_TURN_ON = 1
4274 * @return 0 on success non-zero on failure.
4275 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4276 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4277 * that driver supports crash dump feature. This DCMD will be sent only if
4278 * crash dump feature is supported by the FW.
4281 int megasas_set_crash_dump_params(struct megasas_instance *instance,
4285 struct megasas_cmd *cmd;
4286 struct megasas_dcmd_frame *dcmd;
4288 cmd = megasas_get_cmd(instance);
4291 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4296 dcmd = &cmd->frame->dcmd;
4298 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4299 dcmd->mbox.b[0] = crash_buf_state;
4300 dcmd->cmd = MFI_CMD_DCMD;
4301 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4302 dcmd->sge_count = 1;
4303 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4306 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4307 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4308 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4309 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4311 if (instance->ctrl_context && !instance->mask_interrupts)
4312 ret = megasas_issue_blocked_cmd(instance, cmd,
4313 MEGASAS_BLOCKED_CMD_TIMEOUT);
4315 ret = megasas_issue_polled(instance, cmd);
4317 megasas_return_cmd(instance, cmd);
4322 * megasas_issue_init_mfi - Initializes the FW
4323 * @instance: Adapter soft state
4325 * Issues the INIT MFI cmd
4328 megasas_issue_init_mfi(struct megasas_instance *instance)
4331 struct megasas_cmd *cmd;
4332 struct megasas_init_frame *init_frame;
4333 struct megasas_init_queue_info *initq_info;
4334 dma_addr_t init_frame_h;
4335 dma_addr_t initq_info_h;
4338 * Prepare a init frame. Note the init frame points to queue info
4339 * structure. Each frame has SGL allocated after first 64 bytes. For
4340 * this frame - since we don't need any SGL - we use SGL's space as
4341 * queue info structure
4343 * We will not get a NULL command below. We just created the pool.
4345 cmd = megasas_get_cmd(instance);
4347 init_frame = (struct megasas_init_frame *)cmd->frame;
4348 initq_info = (struct megasas_init_queue_info *)
4349 ((unsigned long)init_frame + 64);
4351 init_frame_h = cmd->frame_phys_addr;
4352 initq_info_h = init_frame_h + 64;
4354 context = init_frame->context;
4355 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4356 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4357 init_frame->context = context;
4359 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4360 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
4362 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4363 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
4365 init_frame->cmd = MFI_CMD_INIT;
4366 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
4367 init_frame->queue_info_new_phys_addr_lo =
4368 cpu_to_le32(lower_32_bits(initq_info_h));
4369 init_frame->queue_info_new_phys_addr_hi =
4370 cpu_to_le32(upper_32_bits(initq_info_h));
4372 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
4375 * disable the intr before firing the init frame to FW
4377 instance->instancet->disable_intr(instance);
4380 * Issue the init frame in polled mode
4383 if (megasas_issue_polled(instance, cmd)) {
4384 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
4385 megasas_return_cmd(instance, cmd);
4389 megasas_return_cmd(instance, cmd);
4398 megasas_init_adapter_mfi(struct megasas_instance *instance)
4400 struct megasas_register_set __iomem *reg_set;
4404 reg_set = instance->reg_set;
4407 * Get various operational parameters from status register
4409 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
4411 * Reduce the max supported cmds by 1. This is to ensure that the
4412 * reply_q_sz (1 more than the max cmd that driver may send)
4413 * does not exceed max cmds that the FW can support
4415 instance->max_fw_cmds = instance->max_fw_cmds-1;
4416 instance->max_mfi_cmds = instance->max_fw_cmds;
4417 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
4420 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4421 * are reserved for IOCTL + driver's internal DCMDs.
4423 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4424 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4425 instance->max_scsi_cmds = (instance->max_fw_cmds -
4426 MEGASAS_SKINNY_INT_CMDS);
4427 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4429 instance->max_scsi_cmds = (instance->max_fw_cmds -
4431 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4435 * Create a pool of commands
4437 if (megasas_alloc_cmds(instance))
4438 goto fail_alloc_cmds;
4441 * Allocate memory for reply queue. Length of reply queue should
4442 * be _one_ more than the maximum commands handled by the firmware.
4444 * Note: When FW completes commands, it places corresponding contex
4445 * values in this circular reply queue. This circular queue is a fairly
4446 * typical producer-consumer queue. FW is the producer (of completed
4447 * commands) and the driver is the consumer.
4449 context_sz = sizeof(u32);
4450 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4452 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4454 &instance->reply_queue_h);
4456 if (!instance->reply_queue) {
4457 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
4458 goto fail_reply_queue;
4461 if (megasas_issue_init_mfi(instance))
4464 if (megasas_get_ctrl_info(instance)) {
4465 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4466 "Fail from %s %d\n", instance->unique_id,
4467 __func__, __LINE__);
4471 instance->fw_support_ieee = 0;
4472 instance->fw_support_ieee =
4473 (instance->instancet->read_fw_status_reg(reg_set) &
4476 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
4477 instance->fw_support_ieee);
4479 if (instance->fw_support_ieee)
4480 instance->flag_ieee = 1;
4486 pci_free_consistent(instance->pdev, reply_q_sz,
4487 instance->reply_queue, instance->reply_queue_h);
4489 megasas_free_cmds(instance);
4496 * megasas_setup_irqs_msix - register legacy interrupts.
4497 * @instance: Adapter soft state
4499 * Do not enable interrupt, only setup ISRs.
4501 * Return 0 on success.
4504 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4506 struct pci_dev *pdev;
4508 pdev = instance->pdev;
4509 instance->irq_context[0].instance = instance;
4510 instance->irq_context[0].MSIxIndex = 0;
4511 if (request_irq(pdev->irq, instance->instancet->service_isr,
4512 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4513 dev_err(&instance->pdev->dev,
4514 "Failed to register IRQ from %s %d\n",
4515 __func__, __LINE__);
4522 * megasas_setup_irqs_msix - register MSI-x interrupts.
4523 * @instance: Adapter soft state
4524 * @is_probe: Driver probe check
4526 * Do not enable interrupt, only setup ISRs.
4528 * Return 0 on success.
4531 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4534 struct pci_dev *pdev;
4536 pdev = instance->pdev;
4539 cpu = cpumask_first(cpu_online_mask);
4540 for (i = 0; i < instance->msix_vectors; i++) {
4541 instance->irq_context[i].instance = instance;
4542 instance->irq_context[i].MSIxIndex = i;
4543 if (request_irq(instance->msixentry[i].vector,
4544 instance->instancet->service_isr, 0, "megasas",
4545 &instance->irq_context[i])) {
4546 dev_err(&instance->pdev->dev,
4547 "Failed to register IRQ for vector %d.\n", i);
4548 for (j = 0; j < i; j++) {
4549 if (smp_affinity_enable)
4550 irq_set_affinity_hint(
4551 instance->msixentry[j].vector, NULL);
4552 free_irq(instance->msixentry[j].vector,
4553 &instance->irq_context[j]);
4555 /* Retry irq register for IO_APIC*/
4556 instance->msix_vectors = 0;
4558 return megasas_setup_irqs_ioapic(instance);
4562 if (smp_affinity_enable) {
4563 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4565 dev_err(&instance->pdev->dev,
4566 "Failed to set affinity hint"
4567 " for cpu %d\n", cpu);
4568 cpu = cpumask_next(cpu, cpu_online_mask);
4575 * megasas_destroy_irqs- unregister interrupts.
4576 * @instance: Adapter soft state
4580 megasas_destroy_irqs(struct megasas_instance *instance) {
4584 if (instance->msix_vectors)
4585 for (i = 0; i < instance->msix_vectors; i++) {
4586 if (smp_affinity_enable)
4587 irq_set_affinity_hint(
4588 instance->msixentry[i].vector, NULL);
4589 free_irq(instance->msixentry[i].vector,
4590 &instance->irq_context[i]);
4593 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4597 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
4598 * @instance: Adapter soft state
4599 * @is_probe: Driver probe check
4601 * Return 0 on success.
4604 megasas_setup_jbod_map(struct megasas_instance *instance)
4607 struct fusion_context *fusion = instance->ctrl_context;
4610 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4611 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4613 if (reset_devices || !fusion ||
4614 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4615 dev_info(&instance->pdev->dev,
4616 "Jbod map is not supported %s %d\n",
4617 __func__, __LINE__);
4618 instance->use_seqnum_jbod_fp = false;
4622 if (fusion->pd_seq_sync[0])
4625 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4626 fusion->pd_seq_sync[i] = dma_alloc_coherent
4627 (&instance->pdev->dev, pd_seq_map_sz,
4628 &fusion->pd_seq_phys[i], GFP_KERNEL);
4629 if (!fusion->pd_seq_sync[i]) {
4630 dev_err(&instance->pdev->dev,
4631 "Failed to allocate memory from %s %d\n",
4632 __func__, __LINE__);
4634 dma_free_coherent(&instance->pdev->dev,
4635 pd_seq_map_sz, fusion->pd_seq_sync[0],
4636 fusion->pd_seq_phys[0]);
4637 fusion->pd_seq_sync[0] = NULL;
4639 instance->use_seqnum_jbod_fp = false;
4645 if (!megasas_sync_pd_seq_num(instance, false) &&
4646 !megasas_sync_pd_seq_num(instance, true))
4647 instance->use_seqnum_jbod_fp = true;
4649 instance->use_seqnum_jbod_fp = false;
4653 * megasas_init_fw - Initializes the FW
4654 * @instance: Adapter soft state
4656 * This is the main function for initializing firmware
4659 static int megasas_init_fw(struct megasas_instance *instance)
4663 u32 tmp_sectors, msix_enable, scratch_pad_2;
4664 resource_size_t base_addr;
4665 struct megasas_register_set __iomem *reg_set;
4666 struct megasas_ctrl_info *ctrl_info = NULL;
4667 unsigned long bar_list;
4668 int i, loop, fw_msix_count = 0;
4669 struct IOV_111 *iovPtr;
4670 struct fusion_context *fusion;
4672 fusion = instance->ctrl_context;
4674 /* Find first memory bar */
4675 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4676 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
4677 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
4679 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
4683 base_addr = pci_resource_start(instance->pdev, instance->bar);
4684 instance->reg_set = ioremap_nocache(base_addr, 8192);
4686 if (!instance->reg_set) {
4687 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
4691 reg_set = instance->reg_set;
4693 switch (instance->pdev->device) {
4694 case PCI_DEVICE_ID_LSI_FUSION:
4695 case PCI_DEVICE_ID_LSI_PLASMA:
4696 case PCI_DEVICE_ID_LSI_INVADER:
4697 case PCI_DEVICE_ID_LSI_FURY:
4698 case PCI_DEVICE_ID_LSI_INTRUDER:
4699 case PCI_DEVICE_ID_LSI_INTRUDER_24:
4700 case PCI_DEVICE_ID_LSI_CUTLASS_52:
4701 case PCI_DEVICE_ID_LSI_CUTLASS_53:
4702 instance->instancet = &megasas_instance_template_fusion;
4704 case PCI_DEVICE_ID_LSI_SAS1078R:
4705 case PCI_DEVICE_ID_LSI_SAS1078DE:
4706 instance->instancet = &megasas_instance_template_ppc;
4708 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4709 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4710 instance->instancet = &megasas_instance_template_gen2;
4712 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4713 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4714 instance->instancet = &megasas_instance_template_skinny;
4716 case PCI_DEVICE_ID_LSI_SAS1064R:
4717 case PCI_DEVICE_ID_DELL_PERC5:
4719 instance->instancet = &megasas_instance_template_xscale;
4720 instance->allow_fw_scan = 1;
4724 if (megasas_transition_to_ready(instance, 0)) {
4725 atomic_set(&instance->fw_reset_no_pci_access, 1);
4726 instance->instancet->adp_reset
4727 (instance, instance->reg_set);
4728 atomic_set(&instance->fw_reset_no_pci_access, 0);
4729 dev_info(&instance->pdev->dev,
4730 "FW restarted successfully from %s!\n",
4733 /*waitting for about 30 second before retry*/
4736 if (megasas_transition_to_ready(instance, 0))
4737 goto fail_ready_state;
4741 * MSI-X host index 0 is common for all adapter.
4742 * It is used for all MPT based Adapters.
4744 instance->reply_post_host_index_addr[0] =
4745 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
4746 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4748 /* Check if MSI-X is supported while in ready state */
4749 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4751 if (msix_enable && !msix_disable) {
4752 scratch_pad_2 = readl
4753 (&instance->reg_set->outbound_scratch_pad_2);
4754 /* Check max MSI-X vectors */
4756 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
4757 instance->msix_vectors = (scratch_pad_2
4758 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4759 fw_msix_count = instance->msix_vectors;
4760 } else { /* Invader series supports more than 8 MSI-x vectors*/
4761 instance->msix_vectors = ((scratch_pad_2
4762 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4763 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4764 fw_msix_count = instance->msix_vectors;
4765 /* Save 1-15 reply post index address to local memory
4766 * Index 0 is already saved from reg offset
4767 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4769 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4770 instance->reply_post_host_index_addr[loop] =
4772 ((u8 __iomem *)instance->reg_set +
4773 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4778 instance->msix_vectors = min(msix_vectors,
4779 instance->msix_vectors);
4780 } else /* MFI adapters */
4781 instance->msix_vectors = 1;
4782 /* Don't bother allocating more MSI-X vectors than cpus */
4783 instance->msix_vectors = min(instance->msix_vectors,
4784 (unsigned int)num_online_cpus());
4785 for (i = 0; i < instance->msix_vectors; i++)
4786 instance->msixentry[i].entry = i;
4787 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4788 1, instance->msix_vectors);
4790 instance->msix_vectors = i;
4792 instance->msix_vectors = 0;
4795 dev_info(&instance->pdev->dev,
4796 "firmware supports msix\t: (%d)", fw_msix_count);
4797 dev_info(&instance->pdev->dev,
4798 "current msix/online cpus\t: (%d/%d)\n",
4799 instance->msix_vectors, (unsigned int)num_online_cpus());
4801 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4802 (unsigned long)instance);
4804 if (instance->msix_vectors ?
4805 megasas_setup_irqs_msix(instance, 1) :
4806 megasas_setup_irqs_ioapic(instance))
4807 goto fail_setup_irqs;
4809 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4811 if (instance->ctrl_info == NULL)
4812 goto fail_init_adapter;
4815 * Below are default value for legacy Firmware.
4816 * non-fusion based controllers
4818 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4819 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4820 /* Get operational params, sge flags, send init cmd to controller */
4821 if (instance->instancet->init_adapter(instance))
4822 goto fail_init_adapter;
4825 instance->instancet->enable_intr(instance);
4827 dev_err(&instance->pdev->dev, "INIT adapter done\n");
4829 megasas_setup_jbod_map(instance);
4832 * the following function will get the PD LIST.
4834 memset(instance->pd_list, 0,
4835 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
4836 if (megasas_get_pd_list(instance) < 0) {
4837 dev_err(&instance->pdev->dev, "failed to get PD list\n");
4838 goto fail_get_pd_list;
4841 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4842 if (megasas_ld_list_query(instance,
4843 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4844 megasas_get_ld_list(instance);
4847 * Compute the max allowed sectors per IO: The controller info has two
4848 * limits on max sectors. Driver should use the minimum of these two.
4850 * 1 << stripe_sz_ops.min = max sectors per strip
4852 * Note that older firmwares ( < FW ver 30) didn't report information
4853 * to calculate max_sectors_1. So the number ended up as zero always.
4856 ctrl_info = instance->ctrl_info;
4858 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4859 le16_to_cpu(ctrl_info->max_strips_per_io);
4860 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
4862 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
4864 instance->mpio = ctrl_info->adapterOperations2.mpio;
4865 instance->UnevenSpanSupport =
4866 ctrl_info->adapterOperations2.supportUnevenSpans;
4867 if (instance->UnevenSpanSupport) {
4868 struct fusion_context *fusion = instance->ctrl_context;
4869 if (MR_ValidateMapInfo(instance))
4870 fusion->fast_path_io = 1;
4872 fusion->fast_path_io = 0;
4875 if (ctrl_info->host_interface.SRIOV) {
4876 instance->requestorId = ctrl_info->iov.requestorId;
4877 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
4878 if (!ctrl_info->adapterOperations2.activePassive)
4879 instance->PlasmaFW111 = 1;
4881 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
4882 instance->PlasmaFW111 ? "1.11" : "new");
4884 if (instance->PlasmaFW111) {
4885 iovPtr = (struct IOV_111 *)
4886 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
4887 instance->requestorId = iovPtr->requestorId;
4890 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
4891 instance->requestorId);
4894 instance->crash_dump_fw_support =
4895 ctrl_info->adapterOperations3.supportCrashDump;
4896 instance->crash_dump_drv_support =
4897 (instance->crash_dump_fw_support &&
4898 instance->crash_dump_buf);
4899 if (instance->crash_dump_drv_support)
4900 megasas_set_crash_dump_params(instance,
4901 MR_CRASH_BUF_TURN_OFF);
4904 if (instance->crash_dump_buf)
4905 pci_free_consistent(instance->pdev,
4907 instance->crash_dump_buf,
4908 instance->crash_dump_h);
4909 instance->crash_dump_buf = NULL;
4913 dev_info(&instance->pdev->dev,
4914 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
4915 le16_to_cpu(ctrl_info->pci.vendor_id),
4916 le16_to_cpu(ctrl_info->pci.device_id),
4917 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
4918 le16_to_cpu(ctrl_info->pci.sub_device_id));
4919 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
4920 instance->UnevenSpanSupport ? "yes" : "no");
4921 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
4922 instance->crash_dump_drv_support ? "yes" : "no");
4923 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
4924 instance->use_seqnum_jbod_fp ? "yes" : "no");
4927 instance->max_sectors_per_req = instance->max_num_sge *
4928 SGE_BUFFER_SIZE / 512;
4929 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4930 instance->max_sectors_per_req = tmp_sectors;
4932 /* Check for valid throttlequeuedepth module parameter */
4933 if (throttlequeuedepth &&
4934 throttlequeuedepth <= instance->max_scsi_cmds)
4935 instance->throttlequeuedepth = throttlequeuedepth;
4937 instance->throttlequeuedepth =
4938 MEGASAS_THROTTLE_QUEUE_DEPTH;
4941 /* Launch SR-IOV heartbeat timer */
4942 if (instance->requestorId) {
4943 if (!megasas_sriov_start_heartbeat(instance, 1))
4944 megasas_start_timer(instance,
4945 &instance->sriov_heartbeat_timer,
4946 megasas_sriov_heartbeat_handler,
4947 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4949 instance->skip_heartbeat_timer_del = 1;
4955 instance->instancet->disable_intr(instance);
4957 megasas_destroy_irqs(instance);
4959 if (instance->msix_vectors)
4960 pci_disable_msix(instance->pdev);
4961 instance->msix_vectors = 0;
4963 kfree(instance->ctrl_info);
4964 instance->ctrl_info = NULL;
4965 iounmap(instance->reg_set);
4968 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
4974 * megasas_release_mfi - Reverses the FW initialization
4975 * @instance: Adapter soft state
4977 static void megasas_release_mfi(struct megasas_instance *instance)
4979 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
4981 if (instance->reply_queue)
4982 pci_free_consistent(instance->pdev, reply_q_sz,
4983 instance->reply_queue, instance->reply_queue_h);
4985 megasas_free_cmds(instance);
4987 iounmap(instance->reg_set);
4989 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
4993 * megasas_get_seq_num - Gets latest event sequence numbers
4994 * @instance: Adapter soft state
4995 * @eli: FW event log sequence numbers information
4997 * FW maintains a log of all events in a non-volatile area. Upper layers would
4998 * usually find out the latest sequence number of the events, the seq number at
4999 * the boot etc. They would "read" all the events below the latest seq number
5000 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5001 * number), they would subsribe to AEN (asynchronous event notification) and
5002 * wait for the events to happen.
5005 megasas_get_seq_num(struct megasas_instance *instance,
5006 struct megasas_evt_log_info *eli)
5008 struct megasas_cmd *cmd;
5009 struct megasas_dcmd_frame *dcmd;
5010 struct megasas_evt_log_info *el_info;
5011 dma_addr_t el_info_h = 0;
5013 cmd = megasas_get_cmd(instance);
5019 dcmd = &cmd->frame->dcmd;
5020 el_info = pci_alloc_consistent(instance->pdev,
5021 sizeof(struct megasas_evt_log_info),
5025 megasas_return_cmd(instance, cmd);
5029 memset(el_info, 0, sizeof(*el_info));
5030 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5032 dcmd->cmd = MFI_CMD_DCMD;
5033 dcmd->cmd_status = 0x0;
5034 dcmd->sge_count = 1;
5035 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5038 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5039 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5040 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5041 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5043 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5044 dev_err(&instance->pdev->dev, "Command timedout"
5045 "from %s\n", __func__);
5048 * Copy the data back into callers buffer
5050 eli->newest_seq_num = el_info->newest_seq_num;
5051 eli->oldest_seq_num = el_info->oldest_seq_num;
5052 eli->clear_seq_num = el_info->clear_seq_num;
5053 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5054 eli->boot_seq_num = el_info->boot_seq_num;
5057 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5058 el_info, el_info_h);
5060 megasas_return_cmd(instance, cmd);
5066 * megasas_register_aen - Registers for asynchronous event notification
5067 * @instance: Adapter soft state
5068 * @seq_num: The starting sequence number
5069 * @class_locale: Class of the event
5071 * This function subscribes for AEN for events beyond the @seq_num. It requests
5072 * to be notified if and only if the event is of type @class_locale
5075 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5076 u32 class_locale_word)
5079 struct megasas_cmd *cmd;
5080 struct megasas_dcmd_frame *dcmd;
5081 union megasas_evt_class_locale curr_aen;
5082 union megasas_evt_class_locale prev_aen;
5085 * If there an AEN pending already (aen_cmd), check if the
5086 * class_locale of that pending AEN is inclusive of the new
5087 * AEN request we currently have. If it is, then we don't have
5088 * to do anything. In other words, whichever events the current
5089 * AEN request is subscribing to, have already been subscribed
5092 * If the old_cmd is _not_ inclusive, then we have to abort
5093 * that command, form a class_locale that is superset of both
5094 * old and current and re-issue to the FW
5097 curr_aen.word = class_locale_word;
5099 if (instance->aen_cmd) {
5102 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
5104 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
5105 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
5106 dev_info(&instance->pdev->dev,
5107 "%s %d out of range class %d send by application\n",
5108 __func__, __LINE__, curr_aen.members.class);
5113 * A class whose enum value is smaller is inclusive of all
5114 * higher values. If a PROGRESS (= -1) was previously
5115 * registered, then a new registration requests for higher
5116 * classes need not be sent to FW. They are automatically
5119 * Locale numbers don't have such hierarchy. They are bitmap
5122 if ((prev_aen.members.class <= curr_aen.members.class) &&
5123 !((prev_aen.members.locale & curr_aen.members.locale) ^
5124 curr_aen.members.locale)) {
5126 * Previously issued event registration includes
5127 * current request. Nothing to do.
5131 curr_aen.members.locale |= prev_aen.members.locale;
5133 if (prev_aen.members.class < curr_aen.members.class)
5134 curr_aen.members.class = prev_aen.members.class;
5136 instance->aen_cmd->abort_aen = 1;
5137 ret_val = megasas_issue_blocked_abort_cmd(instance,
5142 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
5143 "previous AEN command\n");
5149 cmd = megasas_get_cmd(instance);
5154 dcmd = &cmd->frame->dcmd;
5156 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5159 * Prepare DCMD for aen registration
5161 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5163 dcmd->cmd = MFI_CMD_DCMD;
5164 dcmd->cmd_status = 0x0;
5165 dcmd->sge_count = 1;
5166 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5169 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5170 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5171 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
5172 instance->last_seq_num = seq_num;
5173 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5174 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5175 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
5177 if (instance->aen_cmd != NULL) {
5178 megasas_return_cmd(instance, cmd);
5183 * Store reference to the cmd used to register for AEN. When an
5184 * application wants us to register for AEN, we have to abort this
5185 * cmd and re-register with a new EVENT LOCALE supplied by that app
5187 instance->aen_cmd = cmd;
5190 * Issue the aen registration frame
5192 instance->instancet->issue_dcmd(instance, cmd);
5198 * megasas_start_aen - Subscribes to AEN during driver load time
5199 * @instance: Adapter soft state
5201 static int megasas_start_aen(struct megasas_instance *instance)
5203 struct megasas_evt_log_info eli;
5204 union megasas_evt_class_locale class_locale;
5207 * Get the latest sequence number from FW
5209 memset(&eli, 0, sizeof(eli));
5211 if (megasas_get_seq_num(instance, &eli))
5215 * Register AEN with FW for latest sequence number plus 1
5217 class_locale.members.reserved = 0;
5218 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5219 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5221 return megasas_register_aen(instance,
5222 le32_to_cpu(eli.newest_seq_num) + 1,
5227 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5228 * @instance: Adapter soft state
5230 static int megasas_io_attach(struct megasas_instance *instance)
5232 struct Scsi_Host *host = instance->host;
5235 * Export parameters required by SCSI mid-layer
5237 host->irq = instance->pdev->irq;
5238 host->unique_id = instance->unique_id;
5239 host->can_queue = instance->max_scsi_cmds;
5240 host->this_id = instance->init_id;
5241 host->sg_tablesize = instance->max_num_sge;
5243 if (instance->fw_support_ieee)
5244 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5247 * Check if the module parameter value for max_sectors can be used
5249 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5250 instance->max_sectors_per_req = max_sectors;
5253 if (((instance->pdev->device ==
5254 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5255 (instance->pdev->device ==
5256 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5257 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5258 instance->max_sectors_per_req = max_sectors;
5260 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
5261 "and <= %d (or < 1MB for GEN2 controller)\n",
5262 instance->max_sectors_per_req);
5267 host->max_sectors = instance->max_sectors_per_req;
5268 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
5269 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5270 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5271 host->max_lun = MEGASAS_MAX_LUN;
5272 host->max_cmd_len = 16;
5274 /* Fusion only supports host reset */
5275 if (instance->ctrl_context) {
5276 host->hostt->eh_device_reset_handler = NULL;
5277 host->hostt->eh_bus_reset_handler = NULL;
5281 * Notify the mid-layer about the new controller
5283 if (scsi_add_host(host, &instance->pdev->dev)) {
5284 dev_err(&instance->pdev->dev,
5285 "Failed to add host from %s %d\n",
5286 __func__, __LINE__);
5294 megasas_set_dma_mask(struct pci_dev *pdev)
5297 * All our controllers are capable of performing 64-bit DMA
5300 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
5302 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5303 goto fail_set_dma_mask;
5306 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5307 goto fail_set_dma_mask;
5310 * Ensure that all data structures are allocated in 32-bit
5313 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5314 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5315 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5316 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5317 dev_info(&pdev->dev, "set 32bit DMA mask"
5318 "and 32 bit consistent mask\n");
5320 goto fail_set_dma_mask;
5330 * megasas_probe_one - PCI hotplug entry point
5331 * @pdev: PCI device structure
5332 * @id: PCI ids of supported hotplugged adapter
5334 static int megasas_probe_one(struct pci_dev *pdev,
5335 const struct pci_device_id *id)
5338 struct Scsi_Host *host;
5339 struct megasas_instance *instance;
5341 struct fusion_context *fusion = NULL;
5343 /* Reset MSI-X in the kdump kernel */
5344 if (reset_devices) {
5345 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5347 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
5349 if (control & PCI_MSIX_FLAGS_ENABLE) {
5350 dev_info(&pdev->dev, "resetting MSI-X\n");
5351 pci_write_config_word(pdev,
5352 pos + PCI_MSIX_FLAGS,
5354 ~PCI_MSIX_FLAGS_ENABLE);
5360 * PCI prepping: enable device set bus mastering and dma mask
5362 rval = pci_enable_device_mem(pdev);
5368 pci_set_master(pdev);
5370 if (megasas_set_dma_mask(pdev))
5371 goto fail_set_dma_mask;
5373 host = scsi_host_alloc(&megasas_template,
5374 sizeof(struct megasas_instance));
5377 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
5378 goto fail_alloc_instance;
5381 instance = (struct megasas_instance *)host->hostdata;
5382 memset(instance, 0, sizeof(*instance));
5383 atomic_set(&instance->fw_reset_no_pci_access, 0);
5384 instance->pdev = pdev;
5386 switch (instance->pdev->device) {
5387 case PCI_DEVICE_ID_LSI_FUSION:
5388 case PCI_DEVICE_ID_LSI_PLASMA:
5389 case PCI_DEVICE_ID_LSI_INVADER:
5390 case PCI_DEVICE_ID_LSI_FURY:
5391 case PCI_DEVICE_ID_LSI_INTRUDER:
5392 case PCI_DEVICE_ID_LSI_INTRUDER_24:
5393 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5394 case PCI_DEVICE_ID_LSI_CUTLASS_53:
5396 instance->ctrl_context_pages =
5397 get_order(sizeof(struct fusion_context));
5398 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5399 instance->ctrl_context_pages);
5400 if (!instance->ctrl_context) {
5401 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
5402 "memory for Fusion context info\n");
5403 goto fail_alloc_dma_buf;
5405 fusion = instance->ctrl_context;
5407 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
5408 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5409 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5410 fusion->adapter_type = THUNDERBOLT_SERIES;
5412 fusion->adapter_type = INVADER_SERIES;
5415 default: /* For all other supported controllers */
5417 instance->producer =
5418 pci_alloc_consistent(pdev, sizeof(u32),
5419 &instance->producer_h);
5420 instance->consumer =
5421 pci_alloc_consistent(pdev, sizeof(u32),
5422 &instance->consumer_h);
5424 if (!instance->producer || !instance->consumer) {
5425 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate"
5426 "memory for producer, consumer\n");
5427 goto fail_alloc_dma_buf;
5430 *instance->producer = 0;
5431 *instance->consumer = 0;
5435 instance->system_info_buf = pci_zalloc_consistent(pdev,
5436 sizeof(struct MR_DRV_SYSTEM_INFO),
5437 &instance->system_info_h);
5439 if (!instance->system_info_buf)
5440 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5442 /* Crash dump feature related initialisation*/
5443 instance->drv_buf_index = 0;
5444 instance->drv_buf_alloc = 0;
5445 instance->crash_dump_fw_support = 0;
5446 instance->crash_dump_app_support = 0;
5447 instance->fw_crash_state = UNAVAILABLE;
5448 spin_lock_init(&instance->crashdump_lock);
5449 instance->crash_dump_buf = NULL;
5452 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5454 &instance->crash_dump_h);
5455 if (!instance->crash_dump_buf)
5456 dev_err(&pdev->dev, "Can't allocate Firmware "
5457 "crash dump DMA buffer\n");
5459 megasas_poll_wait_aen = 0;
5460 instance->flag_ieee = 0;
5461 instance->ev = NULL;
5462 instance->issuepend_done = 1;
5463 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
5464 instance->is_imr = 0;
5466 instance->evt_detail = pci_alloc_consistent(pdev,
5468 megasas_evt_detail),
5469 &instance->evt_detail_h);
5471 if (!instance->evt_detail) {
5472 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
5473 "event detail structure\n");
5474 goto fail_alloc_dma_buf;
5478 * Initialize locks and queues
5480 INIT_LIST_HEAD(&instance->cmd_pool);
5481 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
5483 atomic_set(&instance->fw_outstanding,0);
5485 init_waitqueue_head(&instance->int_cmd_wait_q);
5486 init_waitqueue_head(&instance->abort_cmd_wait_q);
5488 spin_lock_init(&instance->mfi_pool_lock);
5489 spin_lock_init(&instance->hba_lock);
5490 spin_lock_init(&instance->completion_lock);
5492 mutex_init(&instance->reset_mutex);
5495 * Initialize PCI related and misc parameters
5497 instance->host = host;
5498 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5499 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
5500 instance->ctrl_info = NULL;
5503 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5504 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
5505 instance->flag_ieee = 1;
5507 megasas_dbg_lvl = 0;
5509 instance->unload = 1;
5510 instance->last_time = 0;
5511 instance->disableOnlineCtrlReset = 1;
5512 instance->UnevenSpanSupport = 0;
5514 if (instance->ctrl_context) {
5515 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
5516 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5518 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
5521 * Initialize MFI Firmware
5523 if (megasas_init_fw(instance))
5526 if (instance->requestorId) {
5527 if (instance->PlasmaFW111) {
5528 instance->vf_affiliation_111 =
5529 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5530 &instance->vf_affiliation_111_h);
5531 if (!instance->vf_affiliation_111)
5532 dev_warn(&pdev->dev, "Can't allocate "
5533 "memory for VF affiliation buffer\n");
5535 instance->vf_affiliation =
5536 pci_alloc_consistent(pdev,
5537 (MAX_LOGICAL_DRIVES + 1) *
5538 sizeof(struct MR_LD_VF_AFFILIATION),
5539 &instance->vf_affiliation_h);
5540 if (!instance->vf_affiliation)
5541 dev_warn(&pdev->dev, "Can't allocate "
5542 "memory for VF affiliation buffer\n");
5547 * Store instance in PCI softstate
5549 pci_set_drvdata(pdev, instance);
5552 * Add this controller to megasas_mgmt_info structure so that it
5553 * can be exported to management applications
5555 megasas_mgmt_info.count++;
5556 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5557 megasas_mgmt_info.max_index++;
5560 * Register with SCSI mid-layer
5562 if (megasas_io_attach(instance))
5563 goto fail_io_attach;
5565 instance->unload = 0;
5567 * Trigger SCSI to scan our drives
5569 scsi_scan_host(host);
5572 * Initiate AEN (Asynchronous Event Notification)
5574 if (megasas_start_aen(instance)) {
5575 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
5576 goto fail_start_aen;
5579 /* Get current SR-IOV LD/VF affiliation */
5580 if (instance->requestorId)
5581 megasas_get_ld_vf_affiliation(instance, 1);
5587 megasas_mgmt_info.count--;
5588 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5589 megasas_mgmt_info.max_index--;
5591 instance->instancet->disable_intr(instance);
5592 megasas_destroy_irqs(instance);
5594 if (instance->ctrl_context)
5595 megasas_release_fusion(instance);
5597 megasas_release_mfi(instance);
5598 if (instance->msix_vectors)
5599 pci_disable_msix(instance->pdev);
5602 if (instance->evt_detail)
5603 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5604 instance->evt_detail,
5605 instance->evt_detail_h);
5607 if (instance->producer)
5608 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5609 instance->producer_h);
5610 if (instance->consumer)
5611 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5612 instance->consumer_h);
5613 scsi_host_put(host);
5615 fail_alloc_instance:
5617 pci_disable_device(pdev);
5623 * megasas_flush_cache - Requests FW to flush all its caches
5624 * @instance: Adapter soft state
5626 static void megasas_flush_cache(struct megasas_instance *instance)
5628 struct megasas_cmd *cmd;
5629 struct megasas_dcmd_frame *dcmd;
5631 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5634 cmd = megasas_get_cmd(instance);
5639 dcmd = &cmd->frame->dcmd;
5641 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5643 dcmd->cmd = MFI_CMD_DCMD;
5644 dcmd->cmd_status = 0x0;
5645 dcmd->sge_count = 0;
5646 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5649 dcmd->data_xfer_len = 0;
5650 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
5651 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5653 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5654 dev_err(&instance->pdev->dev, "Command timedout"
5655 " from %s\n", __func__);
5657 megasas_return_cmd(instance, cmd);
5661 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5662 * @instance: Adapter soft state
5663 * @opcode: Shutdown/Hibernate
5665 static void megasas_shutdown_controller(struct megasas_instance *instance,
5668 struct megasas_cmd *cmd;
5669 struct megasas_dcmd_frame *dcmd;
5671 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5674 cmd = megasas_get_cmd(instance);
5679 if (instance->aen_cmd)
5680 megasas_issue_blocked_abort_cmd(instance,
5681 instance->aen_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5682 if (instance->map_update_cmd)
5683 megasas_issue_blocked_abort_cmd(instance,
5684 instance->map_update_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5685 if (instance->jbod_seq_cmd)
5686 megasas_issue_blocked_abort_cmd(instance,
5687 instance->jbod_seq_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5689 dcmd = &cmd->frame->dcmd;
5691 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5693 dcmd->cmd = MFI_CMD_DCMD;
5694 dcmd->cmd_status = 0x0;
5695 dcmd->sge_count = 0;
5696 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5699 dcmd->data_xfer_len = 0;
5700 dcmd->opcode = cpu_to_le32(opcode);
5702 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5703 dev_err(&instance->pdev->dev, "Command timedout"
5704 "from %s\n", __func__);
5706 megasas_return_cmd(instance, cmd);
5711 * megasas_suspend - driver suspend entry point
5712 * @pdev: PCI device structure
5713 * @state: PCI power state to suspend routine
5716 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5718 struct Scsi_Host *host;
5719 struct megasas_instance *instance;
5721 instance = pci_get_drvdata(pdev);
5722 host = instance->host;
5723 instance->unload = 1;
5725 /* Shutdown SR-IOV heartbeat timer */
5726 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5727 del_timer_sync(&instance->sriov_heartbeat_timer);
5729 megasas_flush_cache(instance);
5730 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
5732 /* cancel the delayed work if this work still in queue */
5733 if (instance->ev != NULL) {
5734 struct megasas_aen_event *ev = instance->ev;
5735 cancel_delayed_work_sync(&ev->hotplug_work);
5736 instance->ev = NULL;
5739 tasklet_kill(&instance->isr_tasklet);
5741 pci_set_drvdata(instance->pdev, instance);
5742 instance->instancet->disable_intr(instance);
5744 megasas_destroy_irqs(instance);
5746 if (instance->msix_vectors)
5747 pci_disable_msix(instance->pdev);
5749 pci_save_state(pdev);
5750 pci_disable_device(pdev);
5752 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5758 * megasas_resume- driver resume entry point
5759 * @pdev: PCI device structure
5762 megasas_resume(struct pci_dev *pdev)
5765 struct Scsi_Host *host;
5766 struct megasas_instance *instance;
5768 instance = pci_get_drvdata(pdev);
5769 host = instance->host;
5770 pci_set_power_state(pdev, PCI_D0);
5771 pci_enable_wake(pdev, PCI_D0, 0);
5772 pci_restore_state(pdev);
5775 * PCI prepping: enable device set bus mastering and dma mask
5777 rval = pci_enable_device_mem(pdev);
5780 dev_err(&pdev->dev, "Enable device failed\n");
5784 pci_set_master(pdev);
5786 if (megasas_set_dma_mask(pdev))
5787 goto fail_set_dma_mask;
5790 * Initialize MFI Firmware
5793 atomic_set(&instance->fw_outstanding, 0);
5796 * We expect the FW state to be READY
5798 if (megasas_transition_to_ready(instance, 0))
5799 goto fail_ready_state;
5801 /* Now re-enable MSI-X */
5802 if (instance->msix_vectors &&
5803 pci_enable_msix_exact(instance->pdev, instance->msixentry,
5804 instance->msix_vectors))
5805 goto fail_reenable_msix;
5807 if (instance->ctrl_context) {
5808 megasas_reset_reply_desc(instance);
5809 if (megasas_ioc_init_fusion(instance)) {
5810 megasas_free_cmds(instance);
5811 megasas_free_cmds_fusion(instance);
5814 if (!megasas_get_map_info(instance))
5815 megasas_sync_map_info(instance);
5817 *instance->producer = 0;
5818 *instance->consumer = 0;
5819 if (megasas_issue_init_mfi(instance))
5823 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5824 (unsigned long)instance);
5826 if (instance->msix_vectors ?
5827 megasas_setup_irqs_msix(instance, 0) :
5828 megasas_setup_irqs_ioapic(instance))
5831 /* Re-launch SR-IOV heartbeat timer */
5832 if (instance->requestorId) {
5833 if (!megasas_sriov_start_heartbeat(instance, 0))
5834 megasas_start_timer(instance,
5835 &instance->sriov_heartbeat_timer,
5836 megasas_sriov_heartbeat_handler,
5837 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5839 instance->skip_heartbeat_timer_del = 1;
5844 instance->instancet->enable_intr(instance);
5845 megasas_setup_jbod_map(instance);
5846 instance->unload = 0;
5849 * Initiate AEN (Asynchronous Event Notification)
5851 if (megasas_start_aen(instance))
5852 dev_err(&instance->pdev->dev, "Start AEN failed\n");
5857 if (instance->evt_detail)
5858 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5859 instance->evt_detail,
5860 instance->evt_detail_h);
5862 if (instance->producer)
5863 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5864 instance->producer_h);
5865 if (instance->consumer)
5866 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5867 instance->consumer_h);
5868 scsi_host_put(host);
5874 pci_disable_device(pdev);
5879 #define megasas_suspend NULL
5880 #define megasas_resume NULL
5884 * megasas_detach_one - PCI hot"un"plug entry point
5885 * @pdev: PCI device structure
5887 static void megasas_detach_one(struct pci_dev *pdev)
5890 struct Scsi_Host *host;
5891 struct megasas_instance *instance;
5892 struct fusion_context *fusion;
5895 instance = pci_get_drvdata(pdev);
5896 instance->unload = 1;
5897 host = instance->host;
5898 fusion = instance->ctrl_context;
5900 /* Shutdown SR-IOV heartbeat timer */
5901 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5902 del_timer_sync(&instance->sriov_heartbeat_timer);
5904 if (instance->fw_crash_state != UNAVAILABLE)
5905 megasas_free_host_crash_buffer(instance);
5906 scsi_remove_host(instance->host);
5907 megasas_flush_cache(instance);
5908 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5910 /* cancel the delayed work if this work still in queue*/
5911 if (instance->ev != NULL) {
5912 struct megasas_aen_event *ev = instance->ev;
5913 cancel_delayed_work_sync(&ev->hotplug_work);
5914 instance->ev = NULL;
5917 /* cancel all wait events */
5918 wake_up_all(&instance->int_cmd_wait_q);
5920 tasklet_kill(&instance->isr_tasklet);
5923 * Take the instance off the instance array. Note that we will not
5924 * decrement the max_index. We let this array be sparse array
5926 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5927 if (megasas_mgmt_info.instance[i] == instance) {
5928 megasas_mgmt_info.count--;
5929 megasas_mgmt_info.instance[i] = NULL;
5935 instance->instancet->disable_intr(instance);
5937 megasas_destroy_irqs(instance);
5939 if (instance->msix_vectors)
5940 pci_disable_msix(instance->pdev);
5942 if (instance->ctrl_context) {
5943 megasas_release_fusion(instance);
5944 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5945 (sizeof(struct MR_PD_CFG_SEQ) *
5946 (MAX_PHYSICAL_DEVICES - 1));
5947 for (i = 0; i < 2 ; i++) {
5948 if (fusion->ld_map[i])
5949 dma_free_coherent(&instance->pdev->dev,
5952 fusion->ld_map_phys[i]);
5953 if (fusion->ld_drv_map[i])
5954 free_pages((ulong)fusion->ld_drv_map[i],
5955 fusion->drv_map_pages);
5956 if (fusion->pd_seq_sync[i])
5957 dma_free_coherent(&instance->pdev->dev,
5959 fusion->pd_seq_sync[i],
5960 fusion->pd_seq_phys[i]);
5962 free_pages((ulong)instance->ctrl_context,
5963 instance->ctrl_context_pages);
5965 megasas_release_mfi(instance);
5966 pci_free_consistent(pdev, sizeof(u32),
5968 instance->producer_h);
5969 pci_free_consistent(pdev, sizeof(u32),
5971 instance->consumer_h);
5974 kfree(instance->ctrl_info);
5976 if (instance->evt_detail)
5977 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5978 instance->evt_detail, instance->evt_detail_h);
5980 if (instance->vf_affiliation)
5981 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5982 sizeof(struct MR_LD_VF_AFFILIATION),
5983 instance->vf_affiliation,
5984 instance->vf_affiliation_h);
5986 if (instance->vf_affiliation_111)
5987 pci_free_consistent(pdev,
5988 sizeof(struct MR_LD_VF_AFFILIATION_111),
5989 instance->vf_affiliation_111,
5990 instance->vf_affiliation_111_h);
5992 if (instance->hb_host_mem)
5993 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5994 instance->hb_host_mem,
5995 instance->hb_host_mem_h);
5997 if (instance->crash_dump_buf)
5998 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5999 instance->crash_dump_buf, instance->crash_dump_h);
6001 if (instance->system_info_buf)
6002 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6003 instance->system_info_buf, instance->system_info_h);
6005 scsi_host_put(host);
6007 pci_disable_device(pdev);
6011 * megasas_shutdown - Shutdown entry point
6012 * @device: Generic device structure
6014 static void megasas_shutdown(struct pci_dev *pdev)
6016 struct megasas_instance *instance = pci_get_drvdata(pdev);
6018 instance->unload = 1;
6019 megasas_flush_cache(instance);
6020 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
6021 instance->instancet->disable_intr(instance);
6022 megasas_destroy_irqs(instance);
6024 if (instance->msix_vectors)
6025 pci_disable_msix(instance->pdev);
6029 * megasas_mgmt_open - char node "open" entry point
6031 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6034 * Allow only those users with admin rights
6036 if (!capable(CAP_SYS_ADMIN))
6043 * megasas_mgmt_fasync - Async notifier registration from applications
6045 * This function adds the calling process to a driver global queue. When an
6046 * event occurs, SIGIO will be sent to all processes in this queue.
6048 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6052 mutex_lock(&megasas_async_queue_mutex);
6054 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6056 mutex_unlock(&megasas_async_queue_mutex);
6059 /* For sanity check when we get ioctl */
6060 filep->private_data = filep;
6064 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6070 * megasas_mgmt_poll - char node "poll" entry point
6072 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6075 unsigned long flags;
6077 poll_wait(file, &megasas_poll_wait, wait);
6078 spin_lock_irqsave(&poll_aen_lock, flags);
6079 if (megasas_poll_wait_aen)
6080 mask = (POLLIN | POLLRDNORM);
6083 megasas_poll_wait_aen = 0;
6084 spin_unlock_irqrestore(&poll_aen_lock, flags);
6089 * megasas_set_crash_dump_params_ioctl:
6090 * Send CRASH_DUMP_MODE DCMD to all controllers
6091 * @cmd: MFI command frame
6094 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
6096 struct megasas_instance *local_instance;
6100 crash_support = cmd->frame->dcmd.mbox.w[0];
6102 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6103 local_instance = megasas_mgmt_info.instance[i];
6104 if (local_instance && local_instance->crash_dump_drv_support) {
6105 if ((local_instance->adprecovery ==
6106 MEGASAS_HBA_OPERATIONAL) &&
6107 !megasas_set_crash_dump_params(local_instance,
6109 local_instance->crash_dump_app_support =
6111 dev_info(&local_instance->pdev->dev,
6112 "Application firmware crash "
6113 "dump mode set success\n");
6116 dev_info(&local_instance->pdev->dev,
6117 "Application firmware crash "
6118 "dump mode set failed\n");
6127 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6128 * @instance: Adapter soft state
6129 * @argp: User's ioctl packet
6132 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6133 struct megasas_iocpacket __user * user_ioc,
6134 struct megasas_iocpacket *ioc)
6136 struct megasas_sge32 *kern_sge32;
6137 struct megasas_cmd *cmd;
6138 void *kbuff_arr[MAX_IOCTL_SGE];
6139 dma_addr_t buf_handle = 0;
6142 dma_addr_t sense_handle;
6143 unsigned long *sense_ptr;
6145 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6147 if (ioc->sge_count > MAX_IOCTL_SGE) {
6148 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
6149 ioc->sge_count, MAX_IOCTL_SGE);
6153 cmd = megasas_get_cmd(instance);
6155 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
6160 * User's IOCTL packet has 2 frames (maximum). Copy those two
6161 * frames into our cmd's frames. cmd->frame's context will get
6162 * overwritten when we copy from user's frames. So set that value
6165 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
6166 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
6167 cmd->frame->hdr.pad_0 = 0;
6168 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6170 MFI_FRAME_SENSE64));
6172 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6173 error = megasas_set_crash_dump_params_ioctl(cmd);
6174 megasas_return_cmd(instance, cmd);
6179 * The management interface between applications and the fw uses
6180 * MFI frames. E.g, RAID configuration changes, LD property changes
6181 * etc are accomplishes through different kinds of MFI frames. The
6182 * driver needs to care only about substituting user buffers with
6183 * kernel buffers in SGLs. The location of SGL is embedded in the
6184 * struct iocpacket itself.
6186 kern_sge32 = (struct megasas_sge32 *)
6187 ((unsigned long)cmd->frame + ioc->sgl_off);
6190 * For each user buffer, create a mirror buffer and copy in
6192 for (i = 0; i < ioc->sge_count; i++) {
6193 if (!ioc->sgl[i].iov_len)
6196 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
6197 ioc->sgl[i].iov_len,
6198 &buf_handle, GFP_KERNEL);
6199 if (!kbuff_arr[i]) {
6200 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6201 "kernel SGL buffer for IOCTL\n");
6207 * We don't change the dma_coherent_mask, so
6208 * pci_alloc_consistent only returns 32bit addresses
6210 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6211 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
6214 * We created a kernel buffer corresponding to the
6215 * user buffer. Now copy in from the user buffer
6217 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6218 (u32) (ioc->sgl[i].iov_len))) {
6224 if (ioc->sense_len) {
6225 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6226 &sense_handle, GFP_KERNEL);
6233 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
6234 *sense_ptr = cpu_to_le32(sense_handle);
6238 * Set the sync_cmd flag so that the ISR knows not to complete this
6239 * cmd to the SCSI mid-layer
6242 megasas_issue_blocked_cmd(instance, cmd, 0);
6245 if (instance->unload == 1) {
6246 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6247 "don't submit data to application\n");
6251 * copy out the kernel buffers to user buffers
6253 for (i = 0; i < ioc->sge_count; i++) {
6254 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6255 ioc->sgl[i].iov_len)) {
6262 * copy out the sense
6264 if (ioc->sense_len) {
6266 * sense_ptr points to the location that has the user
6267 * sense buffer address
6269 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6272 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6273 sense, ioc->sense_len)) {
6274 dev_err(&instance->pdev->dev, "Failed to copy out to user "
6282 * copy the status codes returned by the fw
6284 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6285 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6286 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
6292 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
6293 sense, sense_handle);
6296 for (i = 0; i < ioc->sge_count; i++) {
6298 dma_free_coherent(&instance->pdev->dev,
6299 le32_to_cpu(kern_sge32[i].length),
6301 le32_to_cpu(kern_sge32[i].phys_addr));
6302 kbuff_arr[i] = NULL;
6306 megasas_return_cmd(instance, cmd);
6310 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6312 struct megasas_iocpacket __user *user_ioc =
6313 (struct megasas_iocpacket __user *)arg;
6314 struct megasas_iocpacket *ioc;
6315 struct megasas_instance *instance;
6318 unsigned long flags;
6319 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6321 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6325 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6330 instance = megasas_lookup_instance(ioc->host_no);
6336 /* Adjust ioctl wait time for VF mode */
6337 if (instance->requestorId)
6338 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6340 /* Block ioctls in VF mode */
6341 if (instance->requestorId && !allow_vf_ioctls) {
6346 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6347 dev_err(&instance->pdev->dev, "Controller in crit error\n");
6352 if (instance->unload == 1) {
6357 if (down_interruptible(&instance->ioctl_sem)) {
6358 error = -ERESTARTSYS;
6362 for (i = 0; i < wait_time; i++) {
6364 spin_lock_irqsave(&instance->hba_lock, flags);
6365 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6366 spin_unlock_irqrestore(&instance->hba_lock, flags);
6369 spin_unlock_irqrestore(&instance->hba_lock, flags);
6371 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6372 dev_notice(&instance->pdev->dev, "waiting"
6373 "for controller reset to finish\n");
6379 spin_lock_irqsave(&instance->hba_lock, flags);
6380 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6381 spin_unlock_irqrestore(&instance->hba_lock, flags);
6383 dev_err(&instance->pdev->dev, "timed out while"
6384 "waiting for HBA to recover\n");
6388 spin_unlock_irqrestore(&instance->hba_lock, flags);
6390 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
6392 up(&instance->ioctl_sem);
6399 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6401 struct megasas_instance *instance;
6402 struct megasas_aen aen;
6405 unsigned long flags;
6406 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6408 if (file->private_data != file) {
6409 printk(KERN_DEBUG "megasas: fasync_helper was not "
6414 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6417 instance = megasas_lookup_instance(aen.host_no);
6422 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6426 if (instance->unload == 1) {
6430 for (i = 0; i < wait_time; i++) {
6432 spin_lock_irqsave(&instance->hba_lock, flags);
6433 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6434 spin_unlock_irqrestore(&instance->hba_lock,
6439 spin_unlock_irqrestore(&instance->hba_lock, flags);
6441 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6442 dev_notice(&instance->pdev->dev, "waiting for"
6443 "controller reset to finish\n");
6449 spin_lock_irqsave(&instance->hba_lock, flags);
6450 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6451 spin_unlock_irqrestore(&instance->hba_lock, flags);
6452 dev_err(&instance->pdev->dev, "timed out while waiting"
6453 "for HBA to recover\n");
6456 spin_unlock_irqrestore(&instance->hba_lock, flags);
6458 mutex_lock(&instance->reset_mutex);
6459 error = megasas_register_aen(instance, aen.seq_num,
6460 aen.class_locale_word);
6461 mutex_unlock(&instance->reset_mutex);
6466 * megasas_mgmt_ioctl - char node ioctl entry point
6469 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6472 case MEGASAS_IOC_FIRMWARE:
6473 return megasas_mgmt_ioctl_fw(file, arg);
6475 case MEGASAS_IOC_GET_AEN:
6476 return megasas_mgmt_ioctl_aen(file, arg);
6482 #ifdef CONFIG_COMPAT
6483 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6485 struct compat_megasas_iocpacket __user *cioc =
6486 (struct compat_megasas_iocpacket __user *)arg;
6487 struct megasas_iocpacket __user *ioc =
6488 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6492 u32 local_sense_off;
6493 u32 local_sense_len;
6496 if (clear_user(ioc, sizeof(*ioc)))
6499 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6500 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6501 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6502 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6503 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6504 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6508 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6509 * sense_len is not null, so prepare the 64bit value under
6510 * the same condition.
6512 if (get_user(local_sense_off, &ioc->sense_off) ||
6513 get_user(local_sense_len, &ioc->sense_len) ||
6514 get_user(user_sense_off, &cioc->sense_off))
6517 if (local_sense_off != user_sense_off)
6520 if (local_sense_len) {
6521 void __user **sense_ioc_ptr =
6522 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
6523 compat_uptr_t *sense_cioc_ptr =
6524 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
6525 if (get_user(ptr, sense_cioc_ptr) ||
6526 put_user(compat_ptr(ptr), sense_ioc_ptr))
6530 for (i = 0; i < MAX_IOCTL_SGE; i++) {
6531 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6532 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6533 copy_in_user(&ioc->sgl[i].iov_len,
6534 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6538 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6540 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6541 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6542 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6549 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6553 case MEGASAS_IOC_FIRMWARE32:
6554 return megasas_mgmt_compat_ioctl_fw(file, arg);
6555 case MEGASAS_IOC_GET_AEN:
6556 return megasas_mgmt_ioctl_aen(file, arg);
6564 * File operations structure for management interface
6566 static const struct file_operations megasas_mgmt_fops = {
6567 .owner = THIS_MODULE,
6568 .open = megasas_mgmt_open,
6569 .fasync = megasas_mgmt_fasync,
6570 .unlocked_ioctl = megasas_mgmt_ioctl,
6571 .poll = megasas_mgmt_poll,
6572 #ifdef CONFIG_COMPAT
6573 .compat_ioctl = megasas_mgmt_compat_ioctl,
6575 .llseek = noop_llseek,
6579 * PCI hotplug support registration structure
6581 static struct pci_driver megasas_pci_driver = {
6583 .name = "megaraid_sas",
6584 .id_table = megasas_pci_table,
6585 .probe = megasas_probe_one,
6586 .remove = megasas_detach_one,
6587 .suspend = megasas_suspend,
6588 .resume = megasas_resume,
6589 .shutdown = megasas_shutdown,
6593 * Sysfs driver attributes
6595 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6597 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6601 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6604 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6606 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6610 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6613 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6615 return sprintf(buf, "%u\n", support_poll_for_event);
6618 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6619 megasas_sysfs_show_support_poll_for_event, NULL);
6622 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6624 return sprintf(buf, "%u\n", support_device_change);
6627 static DRIVER_ATTR(support_device_change, S_IRUGO,
6628 megasas_sysfs_show_support_device_change, NULL);
6631 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6633 return sprintf(buf, "%u\n", megasas_dbg_lvl);
6637 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6641 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
6642 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6648 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
6649 megasas_sysfs_set_dbg_lvl);
6652 megasas_aen_polling(struct work_struct *work)
6654 struct megasas_aen_event *ev =
6655 container_of(work, struct megasas_aen_event, hotplug_work.work);
6656 struct megasas_instance *instance = ev->instance;
6657 union megasas_evt_class_locale class_locale;
6658 struct Scsi_Host *host;
6659 struct scsi_device *sdev1;
6662 int i, j, doscan = 0;
6663 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
6668 printk(KERN_ERR "invalid instance!\n");
6673 /* Adjust event workqueue thread wait time for VF mode */
6674 if (instance->requestorId)
6675 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6677 /* Don't run the event workqueue thread if OCR is running */
6678 mutex_lock(&instance->reset_mutex);
6680 instance->ev = NULL;
6681 host = instance->host;
6682 if (instance->evt_detail) {
6683 megasas_decode_evt(instance);
6685 switch (le32_to_cpu(instance->evt_detail->code)) {
6687 case MR_EVT_PD_INSERTED:
6688 case MR_EVT_PD_REMOVED:
6689 dcmd_ret = megasas_get_pd_list(instance);
6691 doscan = SCAN_PD_CHANNEL;
6694 case MR_EVT_LD_OFFLINE:
6695 case MR_EVT_CFG_CLEARED:
6696 case MR_EVT_LD_DELETED:
6697 case MR_EVT_LD_CREATED:
6698 if (!instance->requestorId ||
6699 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
6700 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
6703 doscan = SCAN_VD_CHANNEL;
6707 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
6708 case MR_EVT_FOREIGN_CFG_IMPORTED:
6709 case MR_EVT_LD_STATE_CHANGE:
6710 dcmd_ret = megasas_get_pd_list(instance);
6715 if (!instance->requestorId ||
6716 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
6717 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
6722 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
6723 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
6724 instance->host->host_no);
6727 case MR_EVT_CTRL_PROP_CHANGED:
6728 dcmd_ret = megasas_get_ctrl_info(instance);
6735 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
6736 mutex_unlock(&instance->reset_mutex);
6741 mutex_unlock(&instance->reset_mutex);
6743 if (doscan & SCAN_PD_CHANNEL) {
6744 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6745 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6746 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
6747 sdev1 = scsi_device_lookup(host, i, j, 0);
6748 if (instance->pd_list[pd_index].driveState ==
6749 MR_PD_STATE_SYSTEM) {
6751 scsi_add_device(host, i, j, 0);
6753 scsi_device_put(sdev1);
6756 scsi_remove_device(sdev1);
6757 scsi_device_put(sdev1);
6764 if (doscan & SCAN_VD_CHANNEL) {
6765 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6766 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6767 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6768 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6769 if (instance->ld_ids[ld_index] != 0xff) {
6771 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6773 scsi_device_put(sdev1);
6776 scsi_remove_device(sdev1);
6777 scsi_device_put(sdev1);
6785 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
6787 seq_num = instance->last_seq_num;
6789 /* Register AEN with FW for latest sequence number plus 1 */
6790 class_locale.members.reserved = 0;
6791 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6792 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6794 if (instance->aen_cmd != NULL) {
6799 mutex_lock(&instance->reset_mutex);
6800 error = megasas_register_aen(instance, seq_num,
6803 dev_err(&instance->pdev->dev,
6804 "register aen failed error %x\n", error);
6806 mutex_unlock(&instance->reset_mutex);
6811 * megasas_init - Driver load entry point
6813 static int __init megasas_init(void)
6818 * Announce driver version and other information
6820 pr_info("megasas: %s\n", MEGASAS_VERSION);
6822 spin_lock_init(&poll_aen_lock);
6824 support_poll_for_event = 2;
6825 support_device_change = 1;
6827 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
6830 * Register character device node
6832 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
6835 printk(KERN_DEBUG "megasas: failed to open device node\n");
6839 megasas_mgmt_majorno = rval;
6842 * Register ourselves as PCI hotplug module
6844 rval = pci_register_driver(&megasas_pci_driver);
6847 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
6851 rval = driver_create_file(&megasas_pci_driver.driver,
6852 &driver_attr_version);
6854 goto err_dcf_attr_ver;
6856 rval = driver_create_file(&megasas_pci_driver.driver,
6857 &driver_attr_release_date);
6859 goto err_dcf_rel_date;
6861 rval = driver_create_file(&megasas_pci_driver.driver,
6862 &driver_attr_support_poll_for_event);
6864 goto err_dcf_support_poll_for_event;
6866 rval = driver_create_file(&megasas_pci_driver.driver,
6867 &driver_attr_dbg_lvl);
6869 goto err_dcf_dbg_lvl;
6870 rval = driver_create_file(&megasas_pci_driver.driver,
6871 &driver_attr_support_device_change);
6873 goto err_dcf_support_device_change;
6877 err_dcf_support_device_change:
6878 driver_remove_file(&megasas_pci_driver.driver,
6879 &driver_attr_dbg_lvl);
6881 driver_remove_file(&megasas_pci_driver.driver,
6882 &driver_attr_support_poll_for_event);
6883 err_dcf_support_poll_for_event:
6884 driver_remove_file(&megasas_pci_driver.driver,
6885 &driver_attr_release_date);
6887 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6889 pci_unregister_driver(&megasas_pci_driver);
6891 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6896 * megasas_exit - Driver unload entry point
6898 static void __exit megasas_exit(void)
6900 driver_remove_file(&megasas_pci_driver.driver,
6901 &driver_attr_dbg_lvl);
6902 driver_remove_file(&megasas_pci_driver.driver,
6903 &driver_attr_support_poll_for_event);
6904 driver_remove_file(&megasas_pci_driver.driver,
6905 &driver_attr_support_device_change);
6906 driver_remove_file(&megasas_pci_driver.driver,
6907 &driver_attr_release_date);
6908 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6910 pci_unregister_driver(&megasas_pci_driver);
6911 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6914 module_init(megasas_init);
6915 module_exit(megasas_exit);