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);
738 spin_unlock_irqrestore(&instance->hba_lock, flags);
742 * megasas_check_reset_skinny - For controller reset check
743 * @regs: MFI register set
746 megasas_check_reset_skinny(struct megasas_instance *instance,
747 struct megasas_register_set __iomem *regs)
749 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
755 static struct megasas_instance_template megasas_instance_template_skinny = {
757 .fire_cmd = megasas_fire_cmd_skinny,
758 .enable_intr = megasas_enable_intr_skinny,
759 .disable_intr = megasas_disable_intr_skinny,
760 .clear_intr = megasas_clear_intr_skinny,
761 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
762 .adp_reset = megasas_adp_reset_gen2,
763 .check_reset = megasas_check_reset_skinny,
764 .service_isr = megasas_isr,
765 .tasklet = megasas_complete_cmd_dpc,
766 .init_adapter = megasas_init_adapter_mfi,
767 .build_and_issue_cmd = megasas_build_and_issue_cmd,
768 .issue_dcmd = megasas_issue_dcmd,
773 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
778 * megasas_enable_intr_gen2 - Enables interrupts
779 * @regs: MFI register set
782 megasas_enable_intr_gen2(struct megasas_instance *instance)
784 struct megasas_register_set __iomem *regs;
786 regs = instance->reg_set;
787 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
789 /* write ~0x00000005 (4 & 1) to the intr mask*/
790 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
792 /* Dummy readl to force pci flush */
793 readl(®s->outbound_intr_mask);
797 * megasas_disable_intr_gen2 - Disables interrupt
798 * @regs: MFI register set
801 megasas_disable_intr_gen2(struct megasas_instance *instance)
803 struct megasas_register_set __iomem *regs;
804 u32 mask = 0xFFFFFFFF;
806 regs = instance->reg_set;
807 writel(mask, ®s->outbound_intr_mask);
808 /* Dummy readl to force pci flush */
809 readl(®s->outbound_intr_mask);
813 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
814 * @regs: MFI register set
817 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
819 return readl(&(regs)->outbound_scratch_pad);
823 * megasas_clear_interrupt_gen2 - Check & clear interrupt
824 * @regs: MFI register set
827 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
833 * Check if it is our interrupt
835 status = readl(®s->outbound_intr_status);
837 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
838 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
840 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
841 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
845 * Clear the interrupt by writing back the same value
848 writel(status, ®s->outbound_doorbell_clear);
850 /* Dummy readl to force pci flush */
851 readl(®s->outbound_intr_status);
856 * megasas_fire_cmd_gen2 - Sends command to the FW
857 * @frame_phys_addr : Physical address of cmd
858 * @frame_count : Number of frames for the command
859 * @regs : MFI register set
862 megasas_fire_cmd_gen2(struct megasas_instance *instance,
863 dma_addr_t frame_phys_addr,
865 struct megasas_register_set __iomem *regs)
869 spin_lock_irqsave(&instance->hba_lock, flags);
870 writel((frame_phys_addr | (frame_count<<1))|1,
871 &(regs)->inbound_queue_port);
872 spin_unlock_irqrestore(&instance->hba_lock, flags);
876 * megasas_adp_reset_gen2 - For controller reset
877 * @regs: MFI register set
880 megasas_adp_reset_gen2(struct megasas_instance *instance,
881 struct megasas_register_set __iomem *reg_set)
885 u32 __iomem *seq_offset = ®_set->seq_offset;
886 u32 __iomem *hostdiag_offset = ®_set->host_diag;
888 if (instance->instancet == &megasas_instance_template_skinny) {
889 seq_offset = ®_set->fusion_seq_offset;
890 hostdiag_offset = ®_set->fusion_host_diag;
893 writel(0, seq_offset);
894 writel(4, seq_offset);
895 writel(0xb, seq_offset);
896 writel(2, seq_offset);
897 writel(7, seq_offset);
898 writel(0xd, seq_offset);
902 HostDiag = (u32)readl(hostdiag_offset);
904 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
906 HostDiag = (u32)readl(hostdiag_offset);
907 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
915 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
917 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
921 HostDiag = (u32)readl(hostdiag_offset);
922 while (HostDiag & DIAG_RESET_ADAPTER) {
924 HostDiag = (u32)readl(hostdiag_offset);
925 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
936 * megasas_check_reset_gen2 - For controller reset check
937 * @regs: MFI register set
940 megasas_check_reset_gen2(struct megasas_instance *instance,
941 struct megasas_register_set __iomem *regs)
943 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
950 static struct megasas_instance_template megasas_instance_template_gen2 = {
952 .fire_cmd = megasas_fire_cmd_gen2,
953 .enable_intr = megasas_enable_intr_gen2,
954 .disable_intr = megasas_disable_intr_gen2,
955 .clear_intr = megasas_clear_intr_gen2,
956 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
957 .adp_reset = megasas_adp_reset_gen2,
958 .check_reset = megasas_check_reset_gen2,
959 .service_isr = megasas_isr,
960 .tasklet = megasas_complete_cmd_dpc,
961 .init_adapter = megasas_init_adapter_mfi,
962 .build_and_issue_cmd = megasas_build_and_issue_cmd,
963 .issue_dcmd = megasas_issue_dcmd,
967 * This is the end of set of functions & definitions
968 * specific to gen2 (deviceid : 0x78, 0x79) controllers
972 * Template added for TB (Fusion)
974 extern struct megasas_instance_template megasas_instance_template_fusion;
977 * megasas_issue_polled - Issues a polling command
978 * @instance: Adapter soft state
979 * @cmd: Command packet to be issued
981 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
984 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
987 struct megasas_header *frame_hdr = &cmd->frame->hdr;
989 frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
990 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
993 * Issue the frame using inbound queue port
995 instance->instancet->issue_dcmd(instance, cmd);
998 * Wait for cmd_status to change
1000 if (instance->requestorId)
1001 seconds = MEGASAS_ROUTINE_WAIT_TIME_VF;
1003 seconds = MFI_POLL_TIMEOUT_SECS;
1004 return wait_and_poll(instance, cmd, seconds);
1008 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1009 * @instance: Adapter soft state
1010 * @cmd: Command to be issued
1011 * @timeout: Timeout in seconds
1013 * This function waits on an event for the command to be returned from ISR.
1014 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1015 * Used to issue ioctl commands.
1018 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1019 struct megasas_cmd *cmd, int timeout)
1023 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1025 instance->instancet->issue_dcmd(instance, cmd);
1027 ret = wait_event_timeout(instance->int_cmd_wait_q,
1028 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1032 wait_event(instance->int_cmd_wait_q,
1033 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1035 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1040 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1041 * @instance: Adapter soft state
1042 * @cmd_to_abort: Previously issued cmd to be aborted
1043 * @timeout: Timeout in seconds
1045 * MFI firmware can abort previously issued AEN comamnd (automatic event
1046 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1047 * cmd and waits for return status.
1048 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1051 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1052 struct megasas_cmd *cmd_to_abort, int timeout)
1054 struct megasas_cmd *cmd;
1055 struct megasas_abort_frame *abort_fr;
1058 cmd = megasas_get_cmd(instance);
1063 abort_fr = &cmd->frame->abort;
1066 * Prepare and issue the abort frame
1068 abort_fr->cmd = MFI_CMD_ABORT;
1069 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1070 abort_fr->flags = cpu_to_le16(0);
1071 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1072 abort_fr->abort_mfi_phys_addr_lo =
1073 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1074 abort_fr->abort_mfi_phys_addr_hi =
1075 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1078 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1080 instance->instancet->issue_dcmd(instance, cmd);
1083 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1084 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1086 dev_err(&instance->pdev->dev, "Command timedout"
1087 "from %s\n", __func__);
1091 wait_event(instance->abort_cmd_wait_q,
1092 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1096 megasas_return_cmd(instance, cmd);
1101 * megasas_make_sgl32 - Prepares 32-bit SGL
1102 * @instance: Adapter soft state
1103 * @scp: SCSI command from the mid-layer
1104 * @mfi_sgl: SGL to be filled in
1106 * If successful, this function returns the number of SG elements. Otherwise,
1110 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1111 union megasas_sgl *mfi_sgl)
1115 struct scatterlist *os_sgl;
1117 sge_count = scsi_dma_map(scp);
1118 BUG_ON(sge_count < 0);
1121 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1122 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1123 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1130 * megasas_make_sgl64 - Prepares 64-bit SGL
1131 * @instance: Adapter soft state
1132 * @scp: SCSI command from the mid-layer
1133 * @mfi_sgl: SGL to be filled in
1135 * If successful, this function returns the number of SG elements. Otherwise,
1139 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1140 union megasas_sgl *mfi_sgl)
1144 struct scatterlist *os_sgl;
1146 sge_count = scsi_dma_map(scp);
1147 BUG_ON(sge_count < 0);
1150 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1151 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1152 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1159 * megasas_make_sgl_skinny - Prepares IEEE SGL
1160 * @instance: Adapter soft state
1161 * @scp: SCSI command from the mid-layer
1162 * @mfi_sgl: SGL to be filled in
1164 * If successful, this function returns the number of SG elements. Otherwise,
1168 megasas_make_sgl_skinny(struct megasas_instance *instance,
1169 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1173 struct scatterlist *os_sgl;
1175 sge_count = scsi_dma_map(scp);
1178 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1179 mfi_sgl->sge_skinny[i].length =
1180 cpu_to_le32(sg_dma_len(os_sgl));
1181 mfi_sgl->sge_skinny[i].phys_addr =
1182 cpu_to_le64(sg_dma_address(os_sgl));
1183 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1190 * megasas_get_frame_count - Computes the number of frames
1191 * @frame_type : type of frame- io or pthru frame
1192 * @sge_count : number of sg elements
1194 * Returns the number of frames required for numnber of sge's (sge_count)
1197 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1198 u8 sge_count, u8 frame_type)
1203 u32 frame_count = 0;
1205 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1206 sizeof(struct megasas_sge32);
1208 if (instance->flag_ieee) {
1209 sge_sz = sizeof(struct megasas_sge_skinny);
1213 * Main frame can contain 2 SGEs for 64-bit SGLs and
1214 * 3 SGEs for 32-bit SGLs for ldio &
1215 * 1 SGEs for 64-bit SGLs and
1216 * 2 SGEs for 32-bit SGLs for pthru frame
1218 if (unlikely(frame_type == PTHRU_FRAME)) {
1219 if (instance->flag_ieee == 1) {
1220 num_cnt = sge_count - 1;
1221 } else if (IS_DMA64)
1222 num_cnt = sge_count - 1;
1224 num_cnt = sge_count - 2;
1226 if (instance->flag_ieee == 1) {
1227 num_cnt = sge_count - 1;
1228 } else if (IS_DMA64)
1229 num_cnt = sge_count - 2;
1231 num_cnt = sge_count - 3;
1235 sge_bytes = sge_sz * num_cnt;
1237 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1238 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1243 if (frame_count > 7)
1249 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1250 * @instance: Adapter soft state
1251 * @scp: SCSI command
1252 * @cmd: Command to be prepared in
1254 * This function prepares CDB commands. These are typcially pass-through
1255 * commands to the devices.
1258 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1259 struct megasas_cmd *cmd)
1264 struct megasas_pthru_frame *pthru;
1266 is_logical = MEGASAS_IS_LOGICAL(scp);
1267 device_id = MEGASAS_DEV_INDEX(scp);
1268 pthru = (struct megasas_pthru_frame *)cmd->frame;
1270 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1271 flags = MFI_FRAME_DIR_WRITE;
1272 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1273 flags = MFI_FRAME_DIR_READ;
1274 else if (scp->sc_data_direction == PCI_DMA_NONE)
1275 flags = MFI_FRAME_DIR_NONE;
1277 if (instance->flag_ieee == 1) {
1278 flags |= MFI_FRAME_IEEE;
1282 * Prepare the DCDB frame
1284 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1285 pthru->cmd_status = 0x0;
1286 pthru->scsi_status = 0x0;
1287 pthru->target_id = device_id;
1288 pthru->lun = scp->device->lun;
1289 pthru->cdb_len = scp->cmd_len;
1292 pthru->flags = cpu_to_le16(flags);
1293 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1295 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1298 * If the command is for the tape device, set the
1299 * pthru timeout to the os layer timeout value.
1301 if (scp->device->type == TYPE_TAPE) {
1302 if ((scp->request->timeout / HZ) > 0xFFFF)
1303 pthru->timeout = cpu_to_le16(0xFFFF);
1305 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1311 if (instance->flag_ieee == 1) {
1312 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1313 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1315 } else if (IS_DMA64) {
1316 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1317 pthru->sge_count = megasas_make_sgl64(instance, scp,
1320 pthru->sge_count = megasas_make_sgl32(instance, scp,
1323 if (pthru->sge_count > instance->max_num_sge) {
1324 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1330 * Sense info specific
1332 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1333 pthru->sense_buf_phys_addr_hi =
1334 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1335 pthru->sense_buf_phys_addr_lo =
1336 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1339 * Compute the total number of frames this command consumes. FW uses
1340 * this number to pull sufficient number of frames from host memory.
1342 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1345 return cmd->frame_count;
1349 * megasas_build_ldio - Prepares IOs to logical devices
1350 * @instance: Adapter soft state
1351 * @scp: SCSI command
1352 * @cmd: Command to be prepared
1354 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1357 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1358 struct megasas_cmd *cmd)
1361 u8 sc = scp->cmnd[0];
1363 struct megasas_io_frame *ldio;
1365 device_id = MEGASAS_DEV_INDEX(scp);
1366 ldio = (struct megasas_io_frame *)cmd->frame;
1368 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1369 flags = MFI_FRAME_DIR_WRITE;
1370 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1371 flags = MFI_FRAME_DIR_READ;
1373 if (instance->flag_ieee == 1) {
1374 flags |= MFI_FRAME_IEEE;
1378 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1380 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1381 ldio->cmd_status = 0x0;
1382 ldio->scsi_status = 0x0;
1383 ldio->target_id = device_id;
1385 ldio->reserved_0 = 0;
1387 ldio->flags = cpu_to_le16(flags);
1388 ldio->start_lba_hi = 0;
1389 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1392 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1394 if (scp->cmd_len == 6) {
1395 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1396 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1397 ((u32) scp->cmnd[2] << 8) |
1398 (u32) scp->cmnd[3]);
1400 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1404 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1406 else if (scp->cmd_len == 10) {
1407 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1408 ((u32) scp->cmnd[7] << 8));
1409 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1410 ((u32) scp->cmnd[3] << 16) |
1411 ((u32) scp->cmnd[4] << 8) |
1412 (u32) scp->cmnd[5]);
1416 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1418 else if (scp->cmd_len == 12) {
1419 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1420 ((u32) scp->cmnd[7] << 16) |
1421 ((u32) scp->cmnd[8] << 8) |
1422 (u32) scp->cmnd[9]);
1424 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1425 ((u32) scp->cmnd[3] << 16) |
1426 ((u32) scp->cmnd[4] << 8) |
1427 (u32) scp->cmnd[5]);
1431 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1433 else if (scp->cmd_len == 16) {
1434 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1435 ((u32) scp->cmnd[11] << 16) |
1436 ((u32) scp->cmnd[12] << 8) |
1437 (u32) scp->cmnd[13]);
1439 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1440 ((u32) scp->cmnd[7] << 16) |
1441 ((u32) scp->cmnd[8] << 8) |
1442 (u32) scp->cmnd[9]);
1444 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1445 ((u32) scp->cmnd[3] << 16) |
1446 ((u32) scp->cmnd[4] << 8) |
1447 (u32) scp->cmnd[5]);
1454 if (instance->flag_ieee) {
1455 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1456 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1458 } else if (IS_DMA64) {
1459 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1460 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1462 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1464 if (ldio->sge_count > instance->max_num_sge) {
1465 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1471 * Sense info specific
1473 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1474 ldio->sense_buf_phys_addr_hi = 0;
1475 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1478 * Compute the total number of frames this command consumes. FW uses
1479 * this number to pull sufficient number of frames from host memory.
1481 cmd->frame_count = megasas_get_frame_count(instance,
1482 ldio->sge_count, IO_FRAME);
1484 return cmd->frame_count;
1488 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1489 * and whether it's RW or non RW
1490 * @scmd: SCSI command
1493 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1497 switch (cmd->cmnd[0]) {
1506 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1507 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1510 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1511 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1517 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1519 * @instance: Adapter soft state
1522 megasas_dump_pending_frames(struct megasas_instance *instance)
1524 struct megasas_cmd *cmd;
1526 union megasas_sgl *mfi_sgl;
1527 struct megasas_io_frame *ldio;
1528 struct megasas_pthru_frame *pthru;
1530 u32 max_cmd = instance->max_fw_cmds;
1532 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1533 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1535 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1537 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1539 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1540 for (i = 0; i < max_cmd; i++) {
1541 cmd = instance->cmd_list[i];
1544 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1545 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1546 ldio = (struct megasas_io_frame *)cmd->frame;
1547 mfi_sgl = &ldio->sgl;
1548 sgcount = ldio->sge_count;
1549 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1550 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1551 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1552 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1553 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1555 pthru = (struct megasas_pthru_frame *) cmd->frame;
1556 mfi_sgl = &pthru->sgl;
1557 sgcount = pthru->sge_count;
1558 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1559 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1560 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1561 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1562 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1564 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1565 for (n = 0; n < sgcount; n++) {
1567 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1568 le32_to_cpu(mfi_sgl->sge64[n].length),
1569 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1571 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1572 le32_to_cpu(mfi_sgl->sge32[n].length),
1573 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1577 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1578 for (i = 0; i < max_cmd; i++) {
1580 cmd = instance->cmd_list[i];
1582 if (cmd->sync_cmd == 1)
1583 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1585 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1589 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1590 struct scsi_cmnd *scmd)
1592 struct megasas_cmd *cmd;
1595 cmd = megasas_get_cmd(instance);
1597 return SCSI_MLQUEUE_HOST_BUSY;
1600 * Logical drive command
1602 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1603 frame_count = megasas_build_ldio(instance, scmd, cmd);
1605 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1608 goto out_return_cmd;
1611 scmd->SCp.ptr = (char *)cmd;
1614 * Issue the command to the FW
1616 atomic_inc(&instance->fw_outstanding);
1618 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1619 cmd->frame_count-1, instance->reg_set);
1623 megasas_return_cmd(instance, cmd);
1629 * megasas_queue_command - Queue entry point
1630 * @scmd: SCSI command to be queued
1631 * @done: Callback entry point
1634 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1636 struct megasas_instance *instance;
1637 unsigned long flags;
1639 instance = (struct megasas_instance *)
1640 scmd->device->host->hostdata;
1642 if (instance->unload == 1) {
1643 scmd->result = DID_NO_CONNECT << 16;
1644 scmd->scsi_done(scmd);
1648 if (instance->issuepend_done == 0)
1649 return SCSI_MLQUEUE_HOST_BUSY;
1651 spin_lock_irqsave(&instance->hba_lock, flags);
1653 /* Check for an mpio path and adjust behavior */
1654 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1655 if (megasas_check_mpio_paths(instance, scmd) ==
1656 (DID_RESET << 16)) {
1657 spin_unlock_irqrestore(&instance->hba_lock, flags);
1658 return SCSI_MLQUEUE_HOST_BUSY;
1660 spin_unlock_irqrestore(&instance->hba_lock, flags);
1661 scmd->result = DID_NO_CONNECT << 16;
1662 scmd->scsi_done(scmd);
1667 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1668 spin_unlock_irqrestore(&instance->hba_lock, flags);
1669 scmd->result = DID_NO_CONNECT << 16;
1670 scmd->scsi_done(scmd);
1674 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1675 spin_unlock_irqrestore(&instance->hba_lock, flags);
1676 return SCSI_MLQUEUE_HOST_BUSY;
1679 spin_unlock_irqrestore(&instance->hba_lock, flags);
1683 if (MEGASAS_IS_LOGICAL(scmd) &&
1684 (scmd->device->id >= instance->fw_supported_vd_count ||
1685 scmd->device->lun)) {
1686 scmd->result = DID_BAD_TARGET << 16;
1690 switch (scmd->cmnd[0]) {
1691 case SYNCHRONIZE_CACHE:
1693 * FW takes care of flush cache on its own
1694 * No need to send it down
1696 scmd->result = DID_OK << 16;
1702 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1703 dev_err(&instance->pdev->dev, "Err returned from build_and_issue_cmd\n");
1704 return SCSI_MLQUEUE_HOST_BUSY;
1710 scmd->scsi_done(scmd);
1714 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1718 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1720 if ((megasas_mgmt_info.instance[i]) &&
1721 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1722 return megasas_mgmt_info.instance[i];
1728 static int megasas_slave_configure(struct scsi_device *sdev)
1731 * The RAID firmware may require extended timeouts.
1733 blk_queue_rq_timeout(sdev->request_queue,
1734 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1739 static int megasas_slave_alloc(struct scsi_device *sdev)
1742 struct megasas_instance *instance ;
1744 instance = megasas_lookup_instance(sdev->host->host_no);
1745 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1747 * Open the OS scan to the SYSTEM PD
1750 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1752 if ((instance->pd_list[pd_index].driveState ==
1753 MR_PD_STATE_SYSTEM) ||
1754 (instance->pd_list[pd_index].driveType != TYPE_DISK)) {
1763 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1765 * @instance: Adapter soft state
1768 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
1771 struct megasas_cmd *cmd_mfi;
1772 struct megasas_cmd_fusion *cmd_fusion;
1773 struct fusion_context *fusion = instance->ctrl_context;
1775 /* Find all outstanding ioctls */
1777 for (i = 0; i < instance->max_fw_cmds; i++) {
1778 cmd_fusion = fusion->cmd_list[i];
1779 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1780 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1781 if (cmd_mfi->sync_cmd &&
1782 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1783 megasas_complete_cmd(instance,
1788 for (i = 0; i < instance->max_fw_cmds; i++) {
1789 cmd_mfi = instance->cmd_list[i];
1790 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1792 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1798 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1800 /* Set critical error to block I/O & ioctls in case caller didn't */
1801 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1802 /* Wait 1 second to ensure IO or ioctls in build have posted */
1804 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1805 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1806 (instance->ctrl_context)) {
1807 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1809 readl(&instance->reg_set->doorbell);
1810 if (instance->mpio && instance->requestorId)
1811 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
1813 writel(MFI_STOP_ADP,
1814 &instance->reg_set->inbound_doorbell);
1816 /* Complete outstanding ioctls when adapter is killed */
1817 megasas_complete_outstanding_ioctls(instance);
1821 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1822 * restored to max value
1823 * @instance: Adapter soft state
1827 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1829 unsigned long flags;
1831 if (instance->flag & MEGASAS_FW_BUSY
1832 && time_after(jiffies, instance->last_time + 5 * HZ)
1833 && atomic_read(&instance->fw_outstanding) <
1834 instance->throttlequeuedepth + 1) {
1836 spin_lock_irqsave(instance->host->host_lock, flags);
1837 instance->flag &= ~MEGASAS_FW_BUSY;
1839 instance->host->can_queue = instance->max_scsi_cmds;
1840 spin_unlock_irqrestore(instance->host->host_lock, flags);
1845 * megasas_complete_cmd_dpc - Returns FW's controller structure
1846 * @instance_addr: Address of adapter soft state
1848 * Tasklet to complete cmds
1850 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1855 struct megasas_cmd *cmd;
1856 struct megasas_instance *instance =
1857 (struct megasas_instance *)instance_addr;
1858 unsigned long flags;
1860 /* If we have already declared adapter dead, donot complete cmds */
1861 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1864 spin_lock_irqsave(&instance->completion_lock, flags);
1866 producer = le32_to_cpu(*instance->producer);
1867 consumer = le32_to_cpu(*instance->consumer);
1869 while (consumer != producer) {
1870 context = le32_to_cpu(instance->reply_queue[consumer]);
1871 if (context >= instance->max_fw_cmds) {
1872 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
1877 cmd = instance->cmd_list[context];
1879 megasas_complete_cmd(instance, cmd, DID_OK);
1882 if (consumer == (instance->max_fw_cmds + 1)) {
1887 *instance->consumer = cpu_to_le32(producer);
1889 spin_unlock_irqrestore(&instance->completion_lock, flags);
1892 * Check if we can restore can_queue
1894 megasas_check_and_restore_queue_depth(instance);
1898 * megasas_start_timer - Initializes a timer object
1899 * @instance: Adapter soft state
1900 * @timer: timer object to be initialized
1901 * @fn: timer function
1902 * @interval: time interval between timer function call
1905 void megasas_start_timer(struct megasas_instance *instance,
1906 struct timer_list *timer,
1907 void *fn, unsigned long interval)
1910 timer->expires = jiffies + interval;
1911 timer->data = (unsigned long)instance;
1912 timer->function = fn;
1917 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1920 process_fw_state_change_wq(struct work_struct *work);
1922 void megasas_do_ocr(struct megasas_instance *instance)
1924 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1925 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1926 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1927 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
1929 instance->instancet->disable_intr(instance);
1930 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1931 instance->issuepend_done = 0;
1933 atomic_set(&instance->fw_outstanding, 0);
1934 megasas_internal_reset_defer_cmds(instance);
1935 process_fw_state_change_wq(&instance->work_init);
1938 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1941 struct megasas_cmd *cmd;
1942 struct megasas_dcmd_frame *dcmd;
1943 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
1944 dma_addr_t new_affiliation_111_h;
1948 cmd = megasas_get_cmd(instance);
1951 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
1952 "Failed to get cmd for scsi%d\n",
1953 instance->host->host_no);
1957 dcmd = &cmd->frame->dcmd;
1959 if (!instance->vf_affiliation_111) {
1960 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
1961 "affiliation for scsi%d\n", instance->host->host_no);
1962 megasas_return_cmd(instance, cmd);
1967 memset(instance->vf_affiliation_111, 0,
1968 sizeof(struct MR_LD_VF_AFFILIATION_111));
1970 new_affiliation_111 =
1971 pci_alloc_consistent(instance->pdev,
1972 sizeof(struct MR_LD_VF_AFFILIATION_111),
1973 &new_affiliation_111_h);
1974 if (!new_affiliation_111) {
1975 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
1976 "memory for new affiliation for scsi%d\n",
1977 instance->host->host_no);
1978 megasas_return_cmd(instance, cmd);
1981 memset(new_affiliation_111, 0,
1982 sizeof(struct MR_LD_VF_AFFILIATION_111));
1985 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1987 dcmd->cmd = MFI_CMD_DCMD;
1988 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
1989 dcmd->sge_count = 1;
1990 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
1993 dcmd->data_xfer_len =
1994 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
1995 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
1998 dcmd->sgl.sge32[0].phys_addr =
1999 cpu_to_le32(instance->vf_affiliation_111_h);
2001 dcmd->sgl.sge32[0].phys_addr =
2002 cpu_to_le32(new_affiliation_111_h);
2004 dcmd->sgl.sge32[0].length = cpu_to_le32(
2005 sizeof(struct MR_LD_VF_AFFILIATION_111));
2007 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2008 "scsi%d\n", instance->host->host_no);
2010 megasas_issue_blocked_cmd(instance, cmd, 0);
2012 if (dcmd->cmd_status) {
2013 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2014 " failed with status 0x%x for scsi%d\n",
2015 dcmd->cmd_status, instance->host->host_no);
2016 retval = 1; /* Do a scan if we couldn't get affiliation */
2021 thisVf = new_affiliation_111->thisVf;
2022 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2023 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2024 new_affiliation_111->map[ld].policy[thisVf]) {
2025 dev_warn(&instance->pdev->dev, "SR-IOV: "
2026 "Got new LD/VF affiliation for scsi%d\n",
2027 instance->host->host_no);
2028 memcpy(instance->vf_affiliation_111,
2029 new_affiliation_111,
2030 sizeof(struct MR_LD_VF_AFFILIATION_111));
2036 if (new_affiliation_111) {
2037 pci_free_consistent(instance->pdev,
2038 sizeof(struct MR_LD_VF_AFFILIATION_111),
2039 new_affiliation_111,
2040 new_affiliation_111_h);
2043 megasas_return_cmd(instance, cmd);
2048 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2051 struct megasas_cmd *cmd;
2052 struct megasas_dcmd_frame *dcmd;
2053 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2054 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2055 dma_addr_t new_affiliation_h;
2056 int i, j, retval = 0, found = 0, doscan = 0;
2059 cmd = megasas_get_cmd(instance);
2062 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2063 "Failed to get cmd for scsi%d\n",
2064 instance->host->host_no);
2068 dcmd = &cmd->frame->dcmd;
2070 if (!instance->vf_affiliation) {
2071 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2072 "affiliation for scsi%d\n", instance->host->host_no);
2073 megasas_return_cmd(instance, cmd);
2078 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2079 sizeof(struct MR_LD_VF_AFFILIATION));
2082 pci_alloc_consistent(instance->pdev,
2083 (MAX_LOGICAL_DRIVES + 1) *
2084 sizeof(struct MR_LD_VF_AFFILIATION),
2085 &new_affiliation_h);
2086 if (!new_affiliation) {
2087 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2088 "memory for new affiliation for scsi%d\n",
2089 instance->host->host_no);
2090 megasas_return_cmd(instance, cmd);
2093 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2094 sizeof(struct MR_LD_VF_AFFILIATION));
2097 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2099 dcmd->cmd = MFI_CMD_DCMD;
2100 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2101 dcmd->sge_count = 1;
2102 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2105 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2106 sizeof(struct MR_LD_VF_AFFILIATION));
2107 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2110 dcmd->sgl.sge32[0].phys_addr =
2111 cpu_to_le32(instance->vf_affiliation_h);
2113 dcmd->sgl.sge32[0].phys_addr =
2114 cpu_to_le32(new_affiliation_h);
2116 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2117 sizeof(struct MR_LD_VF_AFFILIATION));
2119 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2120 "scsi%d\n", instance->host->host_no);
2122 megasas_issue_blocked_cmd(instance, cmd, 0);
2124 if (dcmd->cmd_status) {
2125 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2126 " failed with status 0x%x for scsi%d\n",
2127 dcmd->cmd_status, instance->host->host_no);
2128 retval = 1; /* Do a scan if we couldn't get affiliation */
2133 if (!new_affiliation->ldCount) {
2134 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2135 "affiliation for passive path for scsi%d\n",
2136 instance->host->host_no);
2140 newmap = new_affiliation->map;
2141 savedmap = instance->vf_affiliation->map;
2142 thisVf = new_affiliation->thisVf;
2143 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2145 for (j = 0; j < instance->vf_affiliation->ldCount;
2147 if (newmap->ref.targetId ==
2148 savedmap->ref.targetId) {
2150 if (newmap->policy[thisVf] !=
2151 savedmap->policy[thisVf]) {
2156 savedmap = (struct MR_LD_VF_MAP *)
2157 ((unsigned char *)savedmap +
2160 if (!found && newmap->policy[thisVf] !=
2161 MR_LD_ACCESS_HIDDEN) {
2165 newmap = (struct MR_LD_VF_MAP *)
2166 ((unsigned char *)newmap + newmap->size);
2169 newmap = new_affiliation->map;
2170 savedmap = instance->vf_affiliation->map;
2172 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2174 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2175 if (savedmap->ref.targetId ==
2176 newmap->ref.targetId) {
2178 if (savedmap->policy[thisVf] !=
2179 newmap->policy[thisVf]) {
2184 newmap = (struct MR_LD_VF_MAP *)
2185 ((unsigned char *)newmap +
2188 if (!found && savedmap->policy[thisVf] !=
2189 MR_LD_ACCESS_HIDDEN) {
2193 savedmap = (struct MR_LD_VF_MAP *)
2194 ((unsigned char *)savedmap +
2200 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2201 "affiliation for scsi%d\n", instance->host->host_no);
2202 memcpy(instance->vf_affiliation, new_affiliation,
2203 new_affiliation->size);
2207 if (new_affiliation)
2208 pci_free_consistent(instance->pdev,
2209 (MAX_LOGICAL_DRIVES + 1) *
2210 sizeof(struct MR_LD_VF_AFFILIATION),
2211 new_affiliation, new_affiliation_h);
2212 megasas_return_cmd(instance, cmd);
2217 /* This function will get the current SR-IOV LD/VF affiliation */
2218 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2223 if (instance->PlasmaFW111)
2224 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2226 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2230 /* This function will tell FW to start the SR-IOV heartbeat */
2231 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2234 struct megasas_cmd *cmd;
2235 struct megasas_dcmd_frame *dcmd;
2238 cmd = megasas_get_cmd(instance);
2241 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2242 "Failed to get cmd for scsi%d\n",
2243 instance->host->host_no);
2247 dcmd = &cmd->frame->dcmd;
2250 instance->hb_host_mem =
2251 pci_zalloc_consistent(instance->pdev,
2252 sizeof(struct MR_CTRL_HB_HOST_MEM),
2253 &instance->hb_host_mem_h);
2254 if (!instance->hb_host_mem) {
2255 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2256 " memory for heartbeat host memory for scsi%d\n",
2257 instance->host->host_no);
2263 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2265 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2266 dcmd->cmd = MFI_CMD_DCMD;
2267 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2268 dcmd->sge_count = 1;
2269 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2272 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2273 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2274 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2275 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2277 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2278 instance->host->host_no);
2280 if (instance->ctrl_context && !instance->mask_interrupts)
2281 retval = megasas_issue_blocked_cmd(instance, cmd,
2282 MEGASAS_ROUTINE_WAIT_TIME_VF);
2284 retval = megasas_issue_polled(instance, cmd);
2287 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2288 "_MEM_ALLOC DCMD %s for scsi%d\n",
2289 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2290 "timed out" : "failed", instance->host->host_no);
2295 megasas_return_cmd(instance, cmd);
2300 /* Handler for SR-IOV heartbeat */
2301 void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2303 struct megasas_instance *instance =
2304 (struct megasas_instance *)instance_addr;
2306 if (instance->hb_host_mem->HB.fwCounter !=
2307 instance->hb_host_mem->HB.driverCounter) {
2308 instance->hb_host_mem->HB.driverCounter =
2309 instance->hb_host_mem->HB.fwCounter;
2310 mod_timer(&instance->sriov_heartbeat_timer,
2311 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2313 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2314 "completed for scsi%d\n", instance->host->host_no);
2315 schedule_work(&instance->work_init);
2320 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2321 * @instance: Adapter soft state
2323 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2324 * complete all its outstanding commands. Returns error if one or more IOs
2325 * are pending after this time period. It also marks the controller dead.
2327 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2331 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2333 unsigned long flags;
2334 struct list_head clist_local;
2335 struct megasas_cmd *reset_cmd;
2337 u8 kill_adapter_flag;
2339 spin_lock_irqsave(&instance->hba_lock, flags);
2340 adprecovery = instance->adprecovery;
2341 spin_unlock_irqrestore(&instance->hba_lock, flags);
2343 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2345 INIT_LIST_HEAD(&clist_local);
2346 spin_lock_irqsave(&instance->hba_lock, flags);
2347 list_splice_init(&instance->internal_reset_pending_q,
2349 spin_unlock_irqrestore(&instance->hba_lock, flags);
2351 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2352 for (i = 0; i < wait_time; i++) {
2354 spin_lock_irqsave(&instance->hba_lock, flags);
2355 adprecovery = instance->adprecovery;
2356 spin_unlock_irqrestore(&instance->hba_lock, flags);
2357 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2361 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2362 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2363 spin_lock_irqsave(&instance->hba_lock, flags);
2364 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2365 spin_unlock_irqrestore(&instance->hba_lock, flags);
2370 while (!list_empty(&clist_local)) {
2371 reset_cmd = list_entry((&clist_local)->next,
2372 struct megasas_cmd, list);
2373 list_del_init(&reset_cmd->list);
2374 if (reset_cmd->scmd) {
2375 reset_cmd->scmd->result = DID_RESET << 16;
2376 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2377 reset_index, reset_cmd,
2378 reset_cmd->scmd->cmnd[0]);
2380 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2381 megasas_return_cmd(instance, reset_cmd);
2382 } else if (reset_cmd->sync_cmd) {
2383 dev_notice(&instance->pdev->dev, "%p synch cmds"
2387 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2388 instance->instancet->fire_cmd(instance,
2389 reset_cmd->frame_phys_addr,
2390 0, instance->reg_set);
2392 dev_notice(&instance->pdev->dev, "%p unexpected"
2402 for (i = 0; i < resetwaittime; i++) {
2403 int outstanding = atomic_read(&instance->fw_outstanding);
2408 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2409 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2410 "commands to complete\n",i,outstanding);
2412 * Call cmd completion routine. Cmd to be
2413 * be completed directly without depending on isr.
2415 megasas_complete_cmd_dpc((unsigned long)instance);
2422 kill_adapter_flag = 0;
2424 fw_state = instance->instancet->read_fw_status_reg(
2425 instance->reg_set) & MFI_STATE_MASK;
2426 if ((fw_state == MFI_STATE_FAULT) &&
2427 (instance->disableOnlineCtrlReset == 0)) {
2429 kill_adapter_flag = 2;
2432 megasas_do_ocr(instance);
2433 kill_adapter_flag = 1;
2435 /* wait for 1 secs to let FW finish the pending cmds */
2441 if (atomic_read(&instance->fw_outstanding) && !kill_adapter_flag) {
2442 if (instance->disableOnlineCtrlReset == 0) {
2443 megasas_do_ocr(instance);
2445 /* wait for 5 secs to let FW finish the pending cmds */
2446 for (i = 0; i < wait_time; i++) {
2448 atomic_read(&instance->fw_outstanding);
2456 if (atomic_read(&instance->fw_outstanding) ||
2457 (kill_adapter_flag == 2)) {
2458 dev_notice(&instance->pdev->dev, "pending cmds after reset\n");
2460 * Send signal to FW to stop processing any pending cmds.
2461 * The controller will be taken offline by the OS now.
2463 if ((instance->pdev->device ==
2464 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2465 (instance->pdev->device ==
2466 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2467 writel(MFI_STOP_ADP,
2468 &instance->reg_set->doorbell);
2470 writel(MFI_STOP_ADP,
2471 &instance->reg_set->inbound_doorbell);
2473 megasas_dump_pending_frames(instance);
2474 spin_lock_irqsave(&instance->hba_lock, flags);
2475 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2476 spin_unlock_irqrestore(&instance->hba_lock, flags);
2480 dev_notice(&instance->pdev->dev, "no pending cmds after reset\n");
2486 * megasas_generic_reset - Generic reset routine
2487 * @scmd: Mid-layer SCSI command
2489 * This routine implements a generic reset handler for device, bus and host
2490 * reset requests. Device, bus and host specific reset handlers can use this
2491 * function after they do their specific tasks.
2493 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2496 struct megasas_instance *instance;
2498 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2500 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2501 scmd->cmnd[0], scmd->retries);
2503 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2504 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2508 ret_val = megasas_wait_for_outstanding(instance);
2509 if (ret_val == SUCCESS)
2510 dev_notice(&instance->pdev->dev, "reset successful\n");
2512 dev_err(&instance->pdev->dev, "failed to do reset\n");
2518 * megasas_reset_timer - quiesce the adapter if required
2521 * Sets the FW busy flag and reduces the host->can_queue if the
2522 * cmd has not been completed within the timeout period.
2525 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2527 struct megasas_instance *instance;
2528 unsigned long flags;
2530 if (time_after(jiffies, scmd->jiffies_at_alloc +
2531 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
2532 return BLK_EH_NOT_HANDLED;
2535 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2536 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2537 /* FW is busy, throttle IO */
2538 spin_lock_irqsave(instance->host->host_lock, flags);
2540 instance->host->can_queue = instance->throttlequeuedepth;
2541 instance->last_time = jiffies;
2542 instance->flag |= MEGASAS_FW_BUSY;
2544 spin_unlock_irqrestore(instance->host->host_lock, flags);
2546 return BLK_EH_RESET_TIMER;
2550 * megasas_reset_device - Device reset handler entry point
2552 static int megasas_reset_device(struct scsi_cmnd *scmd)
2555 * First wait for all commands to complete
2557 return megasas_generic_reset(scmd);
2561 * megasas_reset_bus_host - Bus & host reset handler entry point
2563 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2566 struct megasas_instance *instance;
2568 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2571 * First wait for all commands to complete
2573 if (instance->ctrl_context)
2574 ret = megasas_reset_fusion(scmd->device->host, 1);
2576 ret = megasas_generic_reset(scmd);
2582 * megasas_bios_param - Returns disk geometry for a disk
2583 * @sdev: device handle
2584 * @bdev: block device
2585 * @capacity: drive capacity
2586 * @geom: geometry parameters
2589 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2590 sector_t capacity, int geom[])
2597 /* Default heads (64) & sectors (32) */
2601 tmp = heads * sectors;
2602 cylinders = capacity;
2604 sector_div(cylinders, tmp);
2607 * Handle extended translation size for logical drives > 1Gb
2610 if (capacity >= 0x200000) {
2613 tmp = heads*sectors;
2614 cylinders = capacity;
2615 sector_div(cylinders, tmp);
2620 geom[2] = cylinders;
2625 static void megasas_aen_polling(struct work_struct *work);
2628 * megasas_service_aen - Processes an event notification
2629 * @instance: Adapter soft state
2630 * @cmd: AEN command completed by the ISR
2632 * For AEN, driver sends a command down to FW that is held by the FW till an
2633 * event occurs. When an event of interest occurs, FW completes the command
2634 * that it was previously holding.
2636 * This routines sends SIGIO signal to processes that have registered with the
2640 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2642 unsigned long flags;
2645 * Don't signal app if it is just an aborted previously registered aen
2647 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2648 spin_lock_irqsave(&poll_aen_lock, flags);
2649 megasas_poll_wait_aen = 1;
2650 spin_unlock_irqrestore(&poll_aen_lock, flags);
2651 wake_up(&megasas_poll_wait);
2652 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2657 instance->aen_cmd = NULL;
2659 megasas_return_cmd(instance, cmd);
2661 if ((instance->unload == 0) &&
2662 ((instance->issuepend_done == 1))) {
2663 struct megasas_aen_event *ev;
2665 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2667 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
2669 ev->instance = instance;
2671 INIT_DELAYED_WORK(&ev->hotplug_work,
2672 megasas_aen_polling);
2673 schedule_delayed_work(&ev->hotplug_work, 0);
2679 megasas_fw_crash_buffer_store(struct device *cdev,
2680 struct device_attribute *attr, const char *buf, size_t count)
2682 struct Scsi_Host *shost = class_to_shost(cdev);
2683 struct megasas_instance *instance =
2684 (struct megasas_instance *) shost->hostdata;
2686 unsigned long flags;
2688 if (kstrtoint(buf, 0, &val) != 0)
2691 spin_lock_irqsave(&instance->crashdump_lock, flags);
2692 instance->fw_crash_buffer_offset = val;
2693 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2698 megasas_fw_crash_buffer_show(struct device *cdev,
2699 struct device_attribute *attr, char *buf)
2701 struct Scsi_Host *shost = class_to_shost(cdev);
2702 struct megasas_instance *instance =
2703 (struct megasas_instance *) shost->hostdata;
2705 unsigned long buff_addr;
2706 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2707 unsigned long src_addr;
2708 unsigned long flags;
2711 spin_lock_irqsave(&instance->crashdump_lock, flags);
2712 buff_offset = instance->fw_crash_buffer_offset;
2713 if (!instance->crash_dump_buf &&
2714 !((instance->fw_crash_state == AVAILABLE) ||
2715 (instance->fw_crash_state == COPYING))) {
2716 dev_err(&instance->pdev->dev,
2717 "Firmware crash dump is not available\n");
2718 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2722 buff_addr = (unsigned long) buf;
2724 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
2725 dev_err(&instance->pdev->dev,
2726 "Firmware crash dump offset is out of range\n");
2727 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2731 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2732 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2734 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2735 (buff_offset % dmachunk);
2736 memcpy(buf, (void *)src_addr, size);
2737 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2743 megasas_fw_crash_buffer_size_show(struct device *cdev,
2744 struct device_attribute *attr, char *buf)
2746 struct Scsi_Host *shost = class_to_shost(cdev);
2747 struct megasas_instance *instance =
2748 (struct megasas_instance *) shost->hostdata;
2750 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2751 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2755 megasas_fw_crash_state_store(struct device *cdev,
2756 struct device_attribute *attr, const char *buf, size_t count)
2758 struct Scsi_Host *shost = class_to_shost(cdev);
2759 struct megasas_instance *instance =
2760 (struct megasas_instance *) shost->hostdata;
2762 unsigned long flags;
2764 if (kstrtoint(buf, 0, &val) != 0)
2767 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2768 dev_err(&instance->pdev->dev, "application updates invalid "
2769 "firmware crash state\n");
2773 instance->fw_crash_state = val;
2775 if ((val == COPIED) || (val == COPY_ERROR)) {
2776 spin_lock_irqsave(&instance->crashdump_lock, flags);
2777 megasas_free_host_crash_buffer(instance);
2778 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2779 if (val == COPY_ERROR)
2780 dev_info(&instance->pdev->dev, "application failed to "
2781 "copy Firmware crash dump\n");
2783 dev_info(&instance->pdev->dev, "Firmware crash dump "
2784 "copied successfully\n");
2790 megasas_fw_crash_state_show(struct device *cdev,
2791 struct device_attribute *attr, char *buf)
2793 struct Scsi_Host *shost = class_to_shost(cdev);
2794 struct megasas_instance *instance =
2795 (struct megasas_instance *) shost->hostdata;
2797 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2801 megasas_page_size_show(struct device *cdev,
2802 struct device_attribute *attr, char *buf)
2804 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2807 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2808 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2809 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2810 megasas_fw_crash_buffer_size_show, NULL);
2811 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2812 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2813 static DEVICE_ATTR(page_size, S_IRUGO,
2814 megasas_page_size_show, NULL);
2816 struct device_attribute *megaraid_host_attrs[] = {
2817 &dev_attr_fw_crash_buffer_size,
2818 &dev_attr_fw_crash_buffer,
2819 &dev_attr_fw_crash_state,
2820 &dev_attr_page_size,
2825 * Scsi host template for megaraid_sas driver
2827 static struct scsi_host_template megasas_template = {
2829 .module = THIS_MODULE,
2830 .name = "Avago SAS based MegaRAID driver",
2831 .proc_name = "megaraid_sas",
2832 .slave_configure = megasas_slave_configure,
2833 .slave_alloc = megasas_slave_alloc,
2834 .queuecommand = megasas_queue_command,
2835 .eh_device_reset_handler = megasas_reset_device,
2836 .eh_bus_reset_handler = megasas_reset_bus_host,
2837 .eh_host_reset_handler = megasas_reset_bus_host,
2838 .eh_timed_out = megasas_reset_timer,
2839 .shost_attrs = megaraid_host_attrs,
2840 .bios_param = megasas_bios_param,
2841 .use_clustering = ENABLE_CLUSTERING,
2842 .change_queue_depth = scsi_change_queue_depth,
2847 * megasas_complete_int_cmd - Completes an internal command
2848 * @instance: Adapter soft state
2849 * @cmd: Command to be completed
2851 * The megasas_issue_blocked_cmd() function waits for a command to complete
2852 * after it issues a command. This function wakes up that waiting routine by
2853 * calling wake_up() on the wait queue.
2856 megasas_complete_int_cmd(struct megasas_instance *instance,
2857 struct megasas_cmd *cmd)
2859 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
2860 wake_up(&instance->int_cmd_wait_q);
2864 * megasas_complete_abort - Completes aborting a command
2865 * @instance: Adapter soft state
2866 * @cmd: Cmd that was issued to abort another cmd
2868 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2869 * after it issues an abort on a previously issued command. This function
2870 * wakes up all functions waiting on the same wait queue.
2873 megasas_complete_abort(struct megasas_instance *instance,
2874 struct megasas_cmd *cmd)
2876 if (cmd->sync_cmd) {
2878 cmd->cmd_status_drv = 0;
2879 wake_up(&instance->abort_cmd_wait_q);
2884 * megasas_complete_cmd - Completes a command
2885 * @instance: Adapter soft state
2886 * @cmd: Command to be completed
2887 * @alt_status: If non-zero, use this value as status to
2888 * SCSI mid-layer instead of the value returned
2889 * by the FW. This should be used if caller wants
2890 * an alternate status (as in the case of aborted
2894 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2898 struct megasas_header *hdr = &cmd->frame->hdr;
2899 unsigned long flags;
2900 struct fusion_context *fusion = instance->ctrl_context;
2903 /* flag for the retry reset */
2904 cmd->retry_for_fw_reset = 0;
2907 cmd->scmd->SCp.ptr = NULL;
2910 case MFI_CMD_INVALID:
2911 /* Some older 1068 controller FW may keep a pended
2912 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2913 when booting the kdump kernel. Ignore this command to
2914 prevent a kernel panic on shutdown of the kdump kernel. */
2915 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
2917 dev_warn(&instance->pdev->dev, "If you have a controller "
2918 "other than PERC5, please upgrade your firmware\n");
2920 case MFI_CMD_PD_SCSI_IO:
2921 case MFI_CMD_LD_SCSI_IO:
2924 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2925 * issued either through an IO path or an IOCTL path. If it
2926 * was via IOCTL, we will send it to internal completion.
2928 if (cmd->sync_cmd) {
2930 megasas_complete_int_cmd(instance, cmd);
2934 case MFI_CMD_LD_READ:
2935 case MFI_CMD_LD_WRITE:
2938 cmd->scmd->result = alt_status << 16;
2944 atomic_dec(&instance->fw_outstanding);
2946 scsi_dma_unmap(cmd->scmd);
2947 cmd->scmd->scsi_done(cmd->scmd);
2948 megasas_return_cmd(instance, cmd);
2953 switch (hdr->cmd_status) {
2956 cmd->scmd->result = DID_OK << 16;
2959 case MFI_STAT_SCSI_IO_FAILED:
2960 case MFI_STAT_LD_INIT_IN_PROGRESS:
2962 (DID_ERROR << 16) | hdr->scsi_status;
2965 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2967 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2969 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2970 memset(cmd->scmd->sense_buffer, 0,
2971 SCSI_SENSE_BUFFERSIZE);
2972 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2975 cmd->scmd->result |= DRIVER_SENSE << 24;
2980 case MFI_STAT_LD_OFFLINE:
2981 case MFI_STAT_DEVICE_NOT_FOUND:
2982 cmd->scmd->result = DID_BAD_TARGET << 16;
2986 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
2988 cmd->scmd->result = DID_ERROR << 16;
2992 atomic_dec(&instance->fw_outstanding);
2994 scsi_dma_unmap(cmd->scmd);
2995 cmd->scmd->scsi_done(cmd->scmd);
2996 megasas_return_cmd(instance, cmd);
3003 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3004 /* Check for LD map update */
3005 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3006 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3007 fusion->fast_path_io = 0;
3008 spin_lock_irqsave(instance->host->host_lock, flags);
3009 instance->map_update_cmd = NULL;
3010 if (cmd->frame->hdr.cmd_status != 0) {
3011 if (cmd->frame->hdr.cmd_status !=
3013 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3014 cmd->frame->hdr.cmd_status);
3016 megasas_return_cmd(instance, cmd);
3017 spin_unlock_irqrestore(
3018 instance->host->host_lock,
3024 megasas_return_cmd(instance, cmd);
3027 * Set fast path IO to ZERO.
3028 * Validate Map will set proper value.
3029 * Meanwhile all IOs will go as LD IO.
3031 if (MR_ValidateMapInfo(instance))
3032 fusion->fast_path_io = 1;
3034 fusion->fast_path_io = 0;
3035 megasas_sync_map_info(instance);
3036 spin_unlock_irqrestore(instance->host->host_lock,
3040 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3041 opcode == MR_DCMD_CTRL_EVENT_GET) {
3042 spin_lock_irqsave(&poll_aen_lock, flags);
3043 megasas_poll_wait_aen = 0;
3044 spin_unlock_irqrestore(&poll_aen_lock, flags);
3047 /* FW has an updated PD sequence */
3048 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3049 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3051 spin_lock_irqsave(instance->host->host_lock, flags);
3052 status = cmd->frame->hdr.cmd_status;
3053 instance->jbod_seq_cmd = NULL;
3054 megasas_return_cmd(instance, cmd);
3056 if (status == MFI_STAT_OK) {
3057 instance->pd_seq_map_id++;
3058 /* Re-register a pd sync seq num cmd */
3059 if (megasas_sync_pd_seq_num(instance, true))
3060 instance->use_seqnum_jbod_fp = false;
3062 instance->use_seqnum_jbod_fp = false;
3064 spin_unlock_irqrestore(instance->host->host_lock, flags);
3069 * See if got an event notification
3071 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3072 megasas_service_aen(instance, cmd);
3074 megasas_complete_int_cmd(instance, cmd);
3080 * Cmd issued to abort another cmd returned
3082 megasas_complete_abort(instance, cmd);
3086 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3093 * megasas_issue_pending_cmds_again - issue all pending cmds
3094 * in FW again because of the fw reset
3095 * @instance: Adapter soft state
3098 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3100 struct megasas_cmd *cmd;
3101 struct list_head clist_local;
3102 union megasas_evt_class_locale class_locale;
3103 unsigned long flags;
3106 INIT_LIST_HEAD(&clist_local);
3107 spin_lock_irqsave(&instance->hba_lock, flags);
3108 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3109 spin_unlock_irqrestore(&instance->hba_lock, flags);
3111 while (!list_empty(&clist_local)) {
3112 cmd = list_entry((&clist_local)->next,
3113 struct megasas_cmd, list);
3114 list_del_init(&cmd->list);
3116 if (cmd->sync_cmd || cmd->scmd) {
3117 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3118 "detected to be pending while HBA reset\n",
3119 cmd, cmd->scmd, cmd->sync_cmd);
3121 cmd->retry_for_fw_reset++;
3123 if (cmd->retry_for_fw_reset == 3) {
3124 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3125 "was tried multiple times during reset."
3126 "Shutting down the HBA\n",
3127 cmd, cmd->scmd, cmd->sync_cmd);
3128 instance->instancet->disable_intr(instance);
3129 atomic_set(&instance->fw_reset_no_pci_access, 1);
3130 megaraid_sas_kill_hba(instance);
3135 if (cmd->sync_cmd == 1) {
3137 dev_notice(&instance->pdev->dev, "unexpected"
3138 "cmd attached to internal command!\n");
3140 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3141 "on the internal reset queue,"
3142 "issue it again.\n", cmd);
3143 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3144 instance->instancet->fire_cmd(instance,
3145 cmd->frame_phys_addr,
3146 0, instance->reg_set);
3147 } else if (cmd->scmd) {
3148 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3149 "detected on the internal queue, issue again.\n",
3150 cmd, cmd->scmd->cmnd[0]);
3152 atomic_inc(&instance->fw_outstanding);
3153 instance->instancet->fire_cmd(instance,
3154 cmd->frame_phys_addr,
3155 cmd->frame_count-1, instance->reg_set);
3157 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3158 "internal reset defer list while re-issue!!\n",
3163 if (instance->aen_cmd) {
3164 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3165 megasas_return_cmd(instance, instance->aen_cmd);
3167 instance->aen_cmd = NULL;
3171 * Initiate AEN (Asynchronous Event Notification)
3173 seq_num = instance->last_seq_num;
3174 class_locale.members.reserved = 0;
3175 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3176 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3178 megasas_register_aen(instance, seq_num, class_locale.word);
3182 * Move the internal reset pending commands to a deferred queue.
3184 * We move the commands pending at internal reset time to a
3185 * pending queue. This queue would be flushed after successful
3186 * completion of the internal reset sequence. if the internal reset
3187 * did not complete in time, the kernel reset handler would flush
3191 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3193 struct megasas_cmd *cmd;
3195 u32 max_cmd = instance->max_fw_cmds;
3197 unsigned long flags;
3200 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3201 for (i = 0; i < max_cmd; i++) {
3202 cmd = instance->cmd_list[i];
3203 if (cmd->sync_cmd == 1 || cmd->scmd) {
3204 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3205 "on the defer queue as internal\n",
3206 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3208 if (!list_empty(&cmd->list)) {
3209 dev_notice(&instance->pdev->dev, "ERROR while"
3210 " moving this cmd:%p, %d %p, it was"
3211 "discovered on some list?\n",
3212 cmd, cmd->sync_cmd, cmd->scmd);
3214 list_del_init(&cmd->list);
3217 list_add_tail(&cmd->list,
3218 &instance->internal_reset_pending_q);
3221 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3226 process_fw_state_change_wq(struct work_struct *work)
3228 struct megasas_instance *instance =
3229 container_of(work, struct megasas_instance, work_init);
3231 unsigned long flags;
3233 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
3234 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3235 instance->adprecovery);
3239 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
3240 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3241 "state, restarting it...\n");
3243 instance->instancet->disable_intr(instance);
3244 atomic_set(&instance->fw_outstanding, 0);
3246 atomic_set(&instance->fw_reset_no_pci_access, 1);
3247 instance->instancet->adp_reset(instance, instance->reg_set);
3248 atomic_set(&instance->fw_reset_no_pci_access, 0);
3250 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3251 "initiating next stage...\n");
3253 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3254 "state 2 starting...\n");
3256 /* waiting for about 20 second before start the second init */
3257 for (wait = 0; wait < 30; wait++) {
3261 if (megasas_transition_to_ready(instance, 1)) {
3262 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3264 atomic_set(&instance->fw_reset_no_pci_access, 1);
3265 megaraid_sas_kill_hba(instance);
3269 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3270 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3271 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3273 *instance->consumer = *instance->producer;
3275 *instance->consumer = 0;
3276 *instance->producer = 0;
3279 megasas_issue_init_mfi(instance);
3281 spin_lock_irqsave(&instance->hba_lock, flags);
3282 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3283 spin_unlock_irqrestore(&instance->hba_lock, flags);
3284 instance->instancet->enable_intr(instance);
3286 megasas_issue_pending_cmds_again(instance);
3287 instance->issuepend_done = 1;
3292 * megasas_deplete_reply_queue - Processes all completed commands
3293 * @instance: Adapter soft state
3294 * @alt_status: Alternate status to be returned to
3295 * SCSI mid-layer instead of the status
3296 * returned by the FW
3297 * Note: this must be called with hba lock held
3300 megasas_deplete_reply_queue(struct megasas_instance *instance,
3306 if ((mfiStatus = instance->instancet->check_reset(instance,
3307 instance->reg_set)) == 1) {
3311 if ((mfiStatus = instance->instancet->clear_intr(
3314 /* Hardware may not set outbound_intr_status in MSI-X mode */
3315 if (!instance->msix_vectors)
3319 instance->mfiStatus = mfiStatus;
3321 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3322 fw_state = instance->instancet->read_fw_status_reg(
3323 instance->reg_set) & MFI_STATE_MASK;
3325 if (fw_state != MFI_STATE_FAULT) {
3326 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3330 if ((fw_state == MFI_STATE_FAULT) &&
3331 (instance->disableOnlineCtrlReset == 0)) {
3332 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3334 if ((instance->pdev->device ==
3335 PCI_DEVICE_ID_LSI_SAS1064R) ||
3336 (instance->pdev->device ==
3337 PCI_DEVICE_ID_DELL_PERC5) ||
3338 (instance->pdev->device ==
3339 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3341 *instance->consumer =
3342 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3346 instance->instancet->disable_intr(instance);
3347 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
3348 instance->issuepend_done = 0;
3350 atomic_set(&instance->fw_outstanding, 0);
3351 megasas_internal_reset_defer_cmds(instance);
3353 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3354 fw_state, instance->adprecovery);
3356 schedule_work(&instance->work_init);
3360 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3361 fw_state, instance->disableOnlineCtrlReset);
3365 tasklet_schedule(&instance->isr_tasklet);
3369 * megasas_isr - isr entry point
3371 static irqreturn_t megasas_isr(int irq, void *devp)
3373 struct megasas_irq_context *irq_context = devp;
3374 struct megasas_instance *instance = irq_context->instance;
3375 unsigned long flags;
3378 if (atomic_read(&instance->fw_reset_no_pci_access))
3381 spin_lock_irqsave(&instance->hba_lock, flags);
3382 rc = megasas_deplete_reply_queue(instance, DID_OK);
3383 spin_unlock_irqrestore(&instance->hba_lock, flags);
3389 * megasas_transition_to_ready - Move the FW to READY state
3390 * @instance: Adapter soft state
3392 * During the initialization, FW passes can potentially be in any one of
3393 * several possible states. If the FW in operational, waiting-for-handshake
3394 * states, driver must take steps to bring it to ready state. Otherwise, it
3395 * has to wait for the ready state.
3398 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3404 u32 abs_state, curr_abs_state;
3406 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3407 fw_state = abs_state & MFI_STATE_MASK;
3409 if (fw_state != MFI_STATE_READY)
3410 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3413 while (fw_state != MFI_STATE_READY) {
3417 case MFI_STATE_FAULT:
3418 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3420 max_wait = MEGASAS_RESET_WAIT_TIME;
3421 cur_state = MFI_STATE_FAULT;
3426 case MFI_STATE_WAIT_HANDSHAKE:
3428 * Set the CLR bit in inbound doorbell
3430 if ((instance->pdev->device ==
3431 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3432 (instance->pdev->device ==
3433 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3434 (instance->ctrl_context))
3436 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3437 &instance->reg_set->doorbell);
3440 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3441 &instance->reg_set->inbound_doorbell);
3443 max_wait = MEGASAS_RESET_WAIT_TIME;
3444 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3447 case MFI_STATE_BOOT_MESSAGE_PENDING:
3448 if ((instance->pdev->device ==
3449 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3450 (instance->pdev->device ==
3451 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3452 (instance->ctrl_context))
3453 writel(MFI_INIT_HOTPLUG,
3454 &instance->reg_set->doorbell);
3456 writel(MFI_INIT_HOTPLUG,
3457 &instance->reg_set->inbound_doorbell);
3459 max_wait = MEGASAS_RESET_WAIT_TIME;
3460 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3463 case MFI_STATE_OPERATIONAL:
3465 * Bring it to READY state; assuming max wait 10 secs
3467 instance->instancet->disable_intr(instance);
3468 if ((instance->pdev->device ==
3469 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3470 (instance->pdev->device ==
3471 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3472 (instance->ctrl_context)) {
3473 writel(MFI_RESET_FLAGS,
3474 &instance->reg_set->doorbell);
3476 if (instance->ctrl_context) {
3477 for (i = 0; i < (10 * 1000); i += 20) {
3488 writel(MFI_RESET_FLAGS,
3489 &instance->reg_set->inbound_doorbell);
3491 max_wait = MEGASAS_RESET_WAIT_TIME;
3492 cur_state = MFI_STATE_OPERATIONAL;
3495 case MFI_STATE_UNDEFINED:
3497 * This state should not last for more than 2 seconds
3499 max_wait = MEGASAS_RESET_WAIT_TIME;
3500 cur_state = MFI_STATE_UNDEFINED;
3503 case MFI_STATE_BB_INIT:
3504 max_wait = MEGASAS_RESET_WAIT_TIME;
3505 cur_state = MFI_STATE_BB_INIT;
3508 case MFI_STATE_FW_INIT:
3509 max_wait = MEGASAS_RESET_WAIT_TIME;
3510 cur_state = MFI_STATE_FW_INIT;
3513 case MFI_STATE_FW_INIT_2:
3514 max_wait = MEGASAS_RESET_WAIT_TIME;
3515 cur_state = MFI_STATE_FW_INIT_2;
3518 case MFI_STATE_DEVICE_SCAN:
3519 max_wait = MEGASAS_RESET_WAIT_TIME;
3520 cur_state = MFI_STATE_DEVICE_SCAN;
3523 case MFI_STATE_FLUSH_CACHE:
3524 max_wait = MEGASAS_RESET_WAIT_TIME;
3525 cur_state = MFI_STATE_FLUSH_CACHE;
3529 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3535 * The cur_state should not last for more than max_wait secs
3537 for (i = 0; i < (max_wait * 1000); i++) {
3538 curr_abs_state = instance->instancet->
3539 read_fw_status_reg(instance->reg_set);
3541 if (abs_state == curr_abs_state) {
3548 * Return error if fw_state hasn't changed after max_wait
3550 if (curr_abs_state == abs_state) {
3551 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3552 "in %d secs\n", fw_state, max_wait);
3556 abs_state = curr_abs_state;
3557 fw_state = curr_abs_state & MFI_STATE_MASK;
3559 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3565 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3566 * @instance: Adapter soft state
3568 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3571 u32 max_cmd = instance->max_mfi_cmds;
3572 struct megasas_cmd *cmd;
3574 if (!instance->frame_dma_pool)
3578 * Return all frames to pool
3580 for (i = 0; i < max_cmd; i++) {
3582 cmd = instance->cmd_list[i];
3585 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3586 cmd->frame_phys_addr);
3589 pci_pool_free(instance->sense_dma_pool, cmd->sense,
3590 cmd->sense_phys_addr);
3594 * Now destroy the pool itself
3596 pci_pool_destroy(instance->frame_dma_pool);
3597 pci_pool_destroy(instance->sense_dma_pool);
3599 instance->frame_dma_pool = NULL;
3600 instance->sense_dma_pool = NULL;
3604 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3605 * @instance: Adapter soft state
3607 * Each command packet has an embedded DMA memory buffer that is used for
3608 * filling MFI frame and the SG list that immediately follows the frame. This
3609 * function creates those DMA memory buffers for each command packet by using
3610 * PCI pool facility.
3612 static int megasas_create_frame_pool(struct megasas_instance *instance)
3619 struct megasas_cmd *cmd;
3621 max_cmd = instance->max_mfi_cmds;
3624 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3625 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3627 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3628 sizeof(struct megasas_sge32);
3630 if (instance->flag_ieee)
3631 sge_sz = sizeof(struct megasas_sge_skinny);
3634 * For MFI controllers.
3636 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3637 * Total 960 byte (15 MFI frame of 64 byte)
3639 * Fusion adapter require only 3 extra frame.
3640 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3641 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3642 * Total 192 byte (3 MFI frame of 64 byte)
3644 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
3645 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3647 * Use DMA pool facility provided by PCI layer
3649 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
3650 instance->pdev, total_sz, 256, 0);
3652 if (!instance->frame_dma_pool) {
3653 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
3657 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3658 instance->pdev, 128, 4, 0);
3660 if (!instance->sense_dma_pool) {
3661 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
3663 pci_pool_destroy(instance->frame_dma_pool);
3664 instance->frame_dma_pool = NULL;
3670 * Allocate and attach a frame to each of the commands in cmd_list.
3671 * By making cmd->index as the context instead of the &cmd, we can
3672 * always use 32bit context regardless of the architecture
3674 for (i = 0; i < max_cmd; i++) {
3676 cmd = instance->cmd_list[i];
3678 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3679 GFP_KERNEL, &cmd->frame_phys_addr);
3681 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3682 GFP_KERNEL, &cmd->sense_phys_addr);
3685 * megasas_teardown_frame_pool() takes care of freeing
3686 * whatever has been allocated
3688 if (!cmd->frame || !cmd->sense) {
3689 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
3690 megasas_teardown_frame_pool(instance);
3694 memset(cmd->frame, 0, total_sz);
3695 cmd->frame->io.context = cpu_to_le32(cmd->index);
3696 cmd->frame->io.pad_0 = 0;
3697 if (!instance->ctrl_context && reset_devices)
3698 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
3705 * megasas_free_cmds - Free all the cmds in the free cmd pool
3706 * @instance: Adapter soft state
3708 void megasas_free_cmds(struct megasas_instance *instance)
3712 /* First free the MFI frame pool */
3713 megasas_teardown_frame_pool(instance);
3715 /* Free all the commands in the cmd_list */
3716 for (i = 0; i < instance->max_mfi_cmds; i++)
3718 kfree(instance->cmd_list[i]);
3720 /* Free the cmd_list buffer itself */
3721 kfree(instance->cmd_list);
3722 instance->cmd_list = NULL;
3724 INIT_LIST_HEAD(&instance->cmd_pool);
3728 * megasas_alloc_cmds - Allocates the command packets
3729 * @instance: Adapter soft state
3731 * Each command that is issued to the FW, whether IO commands from the OS or
3732 * internal commands like IOCTLs, are wrapped in local data structure called
3733 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3736 * Each frame has a 32-bit field called context (tag). This context is used
3737 * to get back the megasas_cmd from the frame when a frame gets completed in
3738 * the ISR. Typically the address of the megasas_cmd itself would be used as
3739 * the context. But we wanted to keep the differences between 32 and 64 bit
3740 * systems to the mininum. We always use 32 bit integers for the context. In
3741 * this driver, the 32 bit values are the indices into an array cmd_list.
3742 * This array is used only to look up the megasas_cmd given the context. The
3743 * free commands themselves are maintained in a linked list called cmd_pool.
3745 int megasas_alloc_cmds(struct megasas_instance *instance)
3750 struct megasas_cmd *cmd;
3751 struct fusion_context *fusion;
3753 fusion = instance->ctrl_context;
3754 max_cmd = instance->max_mfi_cmds;
3757 * instance->cmd_list is an array of struct megasas_cmd pointers.
3758 * Allocate the dynamic array first and then allocate individual
3761 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3763 if (!instance->cmd_list) {
3764 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
3768 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3770 for (i = 0; i < max_cmd; i++) {
3771 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3774 if (!instance->cmd_list[i]) {
3776 for (j = 0; j < i; j++)
3777 kfree(instance->cmd_list[j]);
3779 kfree(instance->cmd_list);
3780 instance->cmd_list = NULL;
3786 for (i = 0; i < max_cmd; i++) {
3787 cmd = instance->cmd_list[i];
3788 memset(cmd, 0, sizeof(struct megasas_cmd));
3791 cmd->instance = instance;
3793 list_add_tail(&cmd->list, &instance->cmd_pool);
3797 * Create a frame pool and assign one frame to each cmd
3799 if (megasas_create_frame_pool(instance)) {
3800 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
3801 megasas_free_cmds(instance);
3808 * megasas_get_pd_list_info - Returns FW's pd_list structure
3809 * @instance: Adapter soft state
3810 * @pd_list: pd_list structure
3812 * Issues an internal command (DCMD) to get the FW's controller PD
3813 * list structure. This information is mainly used to find out SYSTEM
3814 * supported by the FW.
3817 megasas_get_pd_list(struct megasas_instance *instance)
3819 int ret = 0, pd_index = 0;
3820 struct megasas_cmd *cmd;
3821 struct megasas_dcmd_frame *dcmd;
3822 struct MR_PD_LIST *ci;
3823 struct MR_PD_ADDRESS *pd_addr;
3824 dma_addr_t ci_h = 0;
3826 cmd = megasas_get_cmd(instance);
3829 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
3833 dcmd = &cmd->frame->dcmd;
3835 ci = pci_alloc_consistent(instance->pdev,
3836 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3839 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
3840 megasas_return_cmd(instance, cmd);
3844 memset(ci, 0, sizeof(*ci));
3845 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3847 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3848 dcmd->mbox.b[1] = 0;
3849 dcmd->cmd = MFI_CMD_DCMD;
3850 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3851 dcmd->sge_count = 1;
3852 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3855 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3856 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3857 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3858 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3860 if (instance->ctrl_context && !instance->mask_interrupts)
3861 ret = megasas_issue_blocked_cmd(instance, cmd,
3862 MEGASAS_BLOCKED_CMD_TIMEOUT);
3864 ret = megasas_issue_polled(instance, cmd);
3867 * the following function will get the instance PD LIST.
3873 (le32_to_cpu(ci->count) <
3874 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3876 memset(instance->local_pd_list, 0,
3877 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3879 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
3881 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
3882 le16_to_cpu(pd_addr->deviceId);
3883 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
3884 pd_addr->scsiDevType;
3885 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
3889 memcpy(instance->pd_list, instance->local_pd_list,
3890 sizeof(instance->pd_list));
3893 pci_free_consistent(instance->pdev,
3894 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3897 megasas_return_cmd(instance, cmd);
3903 * megasas_get_ld_list_info - Returns FW's ld_list structure
3904 * @instance: Adapter soft state
3905 * @ld_list: ld_list structure
3907 * Issues an internal command (DCMD) to get the FW's controller PD
3908 * list structure. This information is mainly used to find out SYSTEM
3909 * supported by the FW.
3912 megasas_get_ld_list(struct megasas_instance *instance)
3914 int ret = 0, ld_index = 0, ids = 0;
3915 struct megasas_cmd *cmd;
3916 struct megasas_dcmd_frame *dcmd;
3917 struct MR_LD_LIST *ci;
3918 dma_addr_t ci_h = 0;
3921 cmd = megasas_get_cmd(instance);
3924 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
3928 dcmd = &cmd->frame->dcmd;
3930 ci = pci_alloc_consistent(instance->pdev,
3931 sizeof(struct MR_LD_LIST),
3935 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
3936 megasas_return_cmd(instance, cmd);
3940 memset(ci, 0, sizeof(*ci));
3941 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3943 if (instance->supportmax256vd)
3944 dcmd->mbox.b[0] = 1;
3945 dcmd->cmd = MFI_CMD_DCMD;
3946 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3947 dcmd->sge_count = 1;
3948 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3950 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3951 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
3952 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3953 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
3956 if (instance->ctrl_context && !instance->mask_interrupts)
3957 ret = megasas_issue_blocked_cmd(instance, cmd,
3958 MEGASAS_BLOCKED_CMD_TIMEOUT);
3960 ret = megasas_issue_polled(instance, cmd);
3963 ld_count = le32_to_cpu(ci->ldCount);
3965 /* the following function will get the instance PD LIST */
3967 if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
3968 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
3970 for (ld_index = 0; ld_index < ld_count; ld_index++) {
3971 if (ci->ldList[ld_index].state != 0) {
3972 ids = ci->ldList[ld_index].ref.targetId;
3973 instance->ld_ids[ids] =
3974 ci->ldList[ld_index].ref.targetId;
3979 pci_free_consistent(instance->pdev,
3980 sizeof(struct MR_LD_LIST),
3984 megasas_return_cmd(instance, cmd);
3989 * megasas_ld_list_query - Returns FW's ld_list structure
3990 * @instance: Adapter soft state
3991 * @ld_list: ld_list structure
3993 * Issues an internal command (DCMD) to get the FW's controller PD
3994 * list structure. This information is mainly used to find out SYSTEM
3995 * supported by the FW.
3998 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4000 int ret = 0, ld_index = 0, ids = 0;
4001 struct megasas_cmd *cmd;
4002 struct megasas_dcmd_frame *dcmd;
4003 struct MR_LD_TARGETID_LIST *ci;
4004 dma_addr_t ci_h = 0;
4007 cmd = megasas_get_cmd(instance);
4010 dev_warn(&instance->pdev->dev,
4011 "megasas_ld_list_query: Failed to get cmd\n");
4015 dcmd = &cmd->frame->dcmd;
4017 ci = pci_alloc_consistent(instance->pdev,
4018 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4021 dev_warn(&instance->pdev->dev,
4022 "Failed to alloc mem for ld_list_query\n");
4023 megasas_return_cmd(instance, cmd);
4027 memset(ci, 0, sizeof(*ci));
4028 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4030 dcmd->mbox.b[0] = query_type;
4031 if (instance->supportmax256vd)
4032 dcmd->mbox.b[2] = 1;
4034 dcmd->cmd = MFI_CMD_DCMD;
4035 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4036 dcmd->sge_count = 1;
4037 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4039 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4040 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4041 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4042 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4045 if (instance->ctrl_context && !instance->mask_interrupts)
4046 ret = megasas_issue_blocked_cmd(instance, cmd,
4047 MEGASAS_BLOCKED_CMD_TIMEOUT);
4049 ret = megasas_issue_polled(instance, cmd);
4051 tgtid_count = le32_to_cpu(ci->count);
4053 if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
4054 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4055 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4056 ids = ci->targetId[ld_index];
4057 instance->ld_ids[ids] = ci->targetId[ld_index];
4062 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4065 megasas_return_cmd(instance, cmd);
4071 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4072 * instance : Controller's instance
4074 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4076 struct fusion_context *fusion;
4080 fusion = instance->ctrl_context;
4081 /* For MFI based controllers return dummy success */
4085 instance->supportmax256vd =
4086 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4087 /* Below is additional check to address future FW enhancement */
4088 if (instance->ctrl_info->max_lds > 64)
4089 instance->supportmax256vd = 1;
4091 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4092 * MEGASAS_MAX_DEV_PER_CHANNEL;
4093 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4094 * MEGASAS_MAX_DEV_PER_CHANNEL;
4095 if (instance->supportmax256vd) {
4096 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4097 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4099 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4100 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4103 dev_info(&instance->pdev->dev,
4104 "firmware type\t: %s\n",
4105 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4106 "Legacy(64 VD) firmware");
4108 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4109 (sizeof(struct MR_LD_SPAN_MAP) *
4110 (instance->fw_supported_vd_count - 1));
4111 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4112 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
4113 (sizeof(struct MR_LD_SPAN_MAP) *
4114 (instance->drv_supported_vd_count - 1));
4116 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4119 if (instance->supportmax256vd)
4120 fusion->current_map_sz = new_map_sz;
4122 fusion->current_map_sz = old_map_sz;
4126 * megasas_get_controller_info - Returns FW's controller structure
4127 * @instance: Adapter soft state
4129 * Issues an internal command (DCMD) to get the FW's controller structure.
4130 * This information is mainly used to find out the maximum IO transfer per
4131 * command supported by the FW.
4134 megasas_get_ctrl_info(struct megasas_instance *instance)
4137 struct megasas_cmd *cmd;
4138 struct megasas_dcmd_frame *dcmd;
4139 struct megasas_ctrl_info *ci;
4140 struct megasas_ctrl_info *ctrl_info;
4141 dma_addr_t ci_h = 0;
4143 ctrl_info = instance->ctrl_info;
4145 cmd = megasas_get_cmd(instance);
4148 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4152 dcmd = &cmd->frame->dcmd;
4154 ci = pci_alloc_consistent(instance->pdev,
4155 sizeof(struct megasas_ctrl_info), &ci_h);
4158 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
4159 megasas_return_cmd(instance, cmd);
4163 memset(ci, 0, sizeof(*ci));
4164 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4166 dcmd->cmd = MFI_CMD_DCMD;
4167 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4168 dcmd->sge_count = 1;
4169 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4172 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4173 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4174 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4175 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4176 dcmd->mbox.b[0] = 1;
4178 if (instance->ctrl_context && !instance->mask_interrupts)
4179 ret = megasas_issue_blocked_cmd(instance, cmd,
4180 MEGASAS_BLOCKED_CMD_TIMEOUT);
4182 ret = megasas_issue_polled(instance, cmd);
4185 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
4186 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4187 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4188 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4189 megasas_update_ext_vd_details(instance);
4190 instance->use_seqnum_jbod_fp =
4191 ctrl_info->adapterOperations3.useSeqNumJbodFP;
4192 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4193 dev_info(&instance->pdev->dev,
4194 "controller type\t: %s(%dMB)\n",
4195 instance->is_imr ? "iMR" : "MR",
4196 le16_to_cpu(ctrl_info->memory_size));
4197 instance->disableOnlineCtrlReset =
4198 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4199 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4200 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
4203 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4206 megasas_return_cmd(instance, cmd);
4211 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4214 * @instance: Adapter soft state
4215 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4216 MR_CRASH_BUF_TURN_OFF = 0
4217 MR_CRASH_BUF_TURN_ON = 1
4218 * @return 0 on success non-zero on failure.
4219 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4220 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4221 * that driver supports crash dump feature. This DCMD will be sent only if
4222 * crash dump feature is supported by the FW.
4225 int megasas_set_crash_dump_params(struct megasas_instance *instance,
4229 struct megasas_cmd *cmd;
4230 struct megasas_dcmd_frame *dcmd;
4232 cmd = megasas_get_cmd(instance);
4235 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4240 dcmd = &cmd->frame->dcmd;
4242 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4243 dcmd->mbox.b[0] = crash_buf_state;
4244 dcmd->cmd = MFI_CMD_DCMD;
4245 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4246 dcmd->sge_count = 1;
4247 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4250 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4251 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4252 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4253 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4255 if (instance->ctrl_context && !instance->mask_interrupts)
4256 ret = megasas_issue_blocked_cmd(instance, cmd,
4257 MEGASAS_BLOCKED_CMD_TIMEOUT);
4259 ret = megasas_issue_polled(instance, cmd);
4261 megasas_return_cmd(instance, cmd);
4266 * megasas_issue_init_mfi - Initializes the FW
4267 * @instance: Adapter soft state
4269 * Issues the INIT MFI cmd
4272 megasas_issue_init_mfi(struct megasas_instance *instance)
4275 struct megasas_cmd *cmd;
4276 struct megasas_init_frame *init_frame;
4277 struct megasas_init_queue_info *initq_info;
4278 dma_addr_t init_frame_h;
4279 dma_addr_t initq_info_h;
4282 * Prepare a init frame. Note the init frame points to queue info
4283 * structure. Each frame has SGL allocated after first 64 bytes. For
4284 * this frame - since we don't need any SGL - we use SGL's space as
4285 * queue info structure
4287 * We will not get a NULL command below. We just created the pool.
4289 cmd = megasas_get_cmd(instance);
4291 init_frame = (struct megasas_init_frame *)cmd->frame;
4292 initq_info = (struct megasas_init_queue_info *)
4293 ((unsigned long)init_frame + 64);
4295 init_frame_h = cmd->frame_phys_addr;
4296 initq_info_h = init_frame_h + 64;
4298 context = init_frame->context;
4299 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4300 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4301 init_frame->context = context;
4303 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4304 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
4306 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4307 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
4309 init_frame->cmd = MFI_CMD_INIT;
4310 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
4311 init_frame->queue_info_new_phys_addr_lo =
4312 cpu_to_le32(lower_32_bits(initq_info_h));
4313 init_frame->queue_info_new_phys_addr_hi =
4314 cpu_to_le32(upper_32_bits(initq_info_h));
4316 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
4319 * disable the intr before firing the init frame to FW
4321 instance->instancet->disable_intr(instance);
4324 * Issue the init frame in polled mode
4327 if (megasas_issue_polled(instance, cmd)) {
4328 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
4329 megasas_return_cmd(instance, cmd);
4333 megasas_return_cmd(instance, cmd);
4342 megasas_init_adapter_mfi(struct megasas_instance *instance)
4344 struct megasas_register_set __iomem *reg_set;
4348 reg_set = instance->reg_set;
4351 * Get various operational parameters from status register
4353 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
4355 * Reduce the max supported cmds by 1. This is to ensure that the
4356 * reply_q_sz (1 more than the max cmd that driver may send)
4357 * does not exceed max cmds that the FW can support
4359 instance->max_fw_cmds = instance->max_fw_cmds-1;
4360 instance->max_mfi_cmds = instance->max_fw_cmds;
4361 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
4364 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4365 * are reserved for IOCTL + driver's internal DCMDs.
4367 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4368 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4369 instance->max_scsi_cmds = (instance->max_fw_cmds -
4370 MEGASAS_SKINNY_INT_CMDS);
4371 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4373 instance->max_scsi_cmds = (instance->max_fw_cmds -
4375 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4379 * Create a pool of commands
4381 if (megasas_alloc_cmds(instance))
4382 goto fail_alloc_cmds;
4385 * Allocate memory for reply queue. Length of reply queue should
4386 * be _one_ more than the maximum commands handled by the firmware.
4388 * Note: When FW completes commands, it places corresponding contex
4389 * values in this circular reply queue. This circular queue is a fairly
4390 * typical producer-consumer queue. FW is the producer (of completed
4391 * commands) and the driver is the consumer.
4393 context_sz = sizeof(u32);
4394 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4396 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4398 &instance->reply_queue_h);
4400 if (!instance->reply_queue) {
4401 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
4402 goto fail_reply_queue;
4405 if (megasas_issue_init_mfi(instance))
4408 if (megasas_get_ctrl_info(instance)) {
4409 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4410 "Fail from %s %d\n", instance->unique_id,
4411 __func__, __LINE__);
4415 instance->fw_support_ieee = 0;
4416 instance->fw_support_ieee =
4417 (instance->instancet->read_fw_status_reg(reg_set) &
4420 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
4421 instance->fw_support_ieee);
4423 if (instance->fw_support_ieee)
4424 instance->flag_ieee = 1;
4430 pci_free_consistent(instance->pdev, reply_q_sz,
4431 instance->reply_queue, instance->reply_queue_h);
4433 megasas_free_cmds(instance);
4440 * megasas_setup_irqs_msix - register legacy interrupts.
4441 * @instance: Adapter soft state
4443 * Do not enable interrupt, only setup ISRs.
4445 * Return 0 on success.
4448 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4450 struct pci_dev *pdev;
4452 pdev = instance->pdev;
4453 instance->irq_context[0].instance = instance;
4454 instance->irq_context[0].MSIxIndex = 0;
4455 if (request_irq(pdev->irq, instance->instancet->service_isr,
4456 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4457 dev_err(&instance->pdev->dev,
4458 "Failed to register IRQ from %s %d\n",
4459 __func__, __LINE__);
4466 * megasas_setup_irqs_msix - register MSI-x interrupts.
4467 * @instance: Adapter soft state
4468 * @is_probe: Driver probe check
4470 * Do not enable interrupt, only setup ISRs.
4472 * Return 0 on success.
4475 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4478 struct pci_dev *pdev;
4480 pdev = instance->pdev;
4483 cpu = cpumask_first(cpu_online_mask);
4484 for (i = 0; i < instance->msix_vectors; i++) {
4485 instance->irq_context[i].instance = instance;
4486 instance->irq_context[i].MSIxIndex = i;
4487 if (request_irq(instance->msixentry[i].vector,
4488 instance->instancet->service_isr, 0, "megasas",
4489 &instance->irq_context[i])) {
4490 dev_err(&instance->pdev->dev,
4491 "Failed to register IRQ for vector %d.\n", i);
4492 for (j = 0; j < i; j++) {
4493 if (smp_affinity_enable)
4494 irq_set_affinity_hint(
4495 instance->msixentry[j].vector, NULL);
4496 free_irq(instance->msixentry[j].vector,
4497 &instance->irq_context[j]);
4499 /* Retry irq register for IO_APIC*/
4500 instance->msix_vectors = 0;
4502 return megasas_setup_irqs_ioapic(instance);
4506 if (smp_affinity_enable) {
4507 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4509 dev_err(&instance->pdev->dev,
4510 "Failed to set affinity hint"
4511 " for cpu %d\n", cpu);
4512 cpu = cpumask_next(cpu, cpu_online_mask);
4519 * megasas_destroy_irqs- unregister interrupts.
4520 * @instance: Adapter soft state
4524 megasas_destroy_irqs(struct megasas_instance *instance) {
4528 if (instance->msix_vectors)
4529 for (i = 0; i < instance->msix_vectors; i++) {
4530 if (smp_affinity_enable)
4531 irq_set_affinity_hint(
4532 instance->msixentry[i].vector, NULL);
4533 free_irq(instance->msixentry[i].vector,
4534 &instance->irq_context[i]);
4537 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4541 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
4542 * @instance: Adapter soft state
4543 * @is_probe: Driver probe check
4545 * Return 0 on success.
4548 megasas_setup_jbod_map(struct megasas_instance *instance)
4551 struct fusion_context *fusion = instance->ctrl_context;
4554 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4555 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4557 if (reset_devices || !fusion ||
4558 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4559 dev_info(&instance->pdev->dev,
4560 "Jbod map is not supported %s %d\n",
4561 __func__, __LINE__);
4562 instance->use_seqnum_jbod_fp = false;
4566 if (fusion->pd_seq_sync[0])
4569 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4570 fusion->pd_seq_sync[i] = dma_alloc_coherent
4571 (&instance->pdev->dev, pd_seq_map_sz,
4572 &fusion->pd_seq_phys[i], GFP_KERNEL);
4573 if (!fusion->pd_seq_sync[i]) {
4574 dev_err(&instance->pdev->dev,
4575 "Failed to allocate memory from %s %d\n",
4576 __func__, __LINE__);
4578 dma_free_coherent(&instance->pdev->dev,
4579 pd_seq_map_sz, fusion->pd_seq_sync[0],
4580 fusion->pd_seq_phys[0]);
4581 fusion->pd_seq_sync[0] = NULL;
4583 instance->use_seqnum_jbod_fp = false;
4589 if (!megasas_sync_pd_seq_num(instance, false) &&
4590 !megasas_sync_pd_seq_num(instance, true))
4591 instance->use_seqnum_jbod_fp = true;
4593 instance->use_seqnum_jbod_fp = false;
4597 * megasas_init_fw - Initializes the FW
4598 * @instance: Adapter soft state
4600 * This is the main function for initializing firmware
4603 static int megasas_init_fw(struct megasas_instance *instance)
4607 u32 tmp_sectors, msix_enable, scratch_pad_2;
4608 resource_size_t base_addr;
4609 struct megasas_register_set __iomem *reg_set;
4610 struct megasas_ctrl_info *ctrl_info = NULL;
4611 unsigned long bar_list;
4612 int i, loop, fw_msix_count = 0;
4613 struct IOV_111 *iovPtr;
4614 struct fusion_context *fusion;
4616 fusion = instance->ctrl_context;
4618 /* Find first memory bar */
4619 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4620 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
4621 if (pci_request_selected_regions(instance->pdev, instance->bar,
4623 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
4627 base_addr = pci_resource_start(instance->pdev, instance->bar);
4628 instance->reg_set = ioremap_nocache(base_addr, 8192);
4630 if (!instance->reg_set) {
4631 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
4635 reg_set = instance->reg_set;
4637 switch (instance->pdev->device) {
4638 case PCI_DEVICE_ID_LSI_FUSION:
4639 case PCI_DEVICE_ID_LSI_PLASMA:
4640 case PCI_DEVICE_ID_LSI_INVADER:
4641 case PCI_DEVICE_ID_LSI_FURY:
4642 case PCI_DEVICE_ID_LSI_INTRUDER:
4643 case PCI_DEVICE_ID_LSI_INTRUDER_24:
4644 case PCI_DEVICE_ID_LSI_CUTLASS_52:
4645 case PCI_DEVICE_ID_LSI_CUTLASS_53:
4646 instance->instancet = &megasas_instance_template_fusion;
4648 case PCI_DEVICE_ID_LSI_SAS1078R:
4649 case PCI_DEVICE_ID_LSI_SAS1078DE:
4650 instance->instancet = &megasas_instance_template_ppc;
4652 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4653 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4654 instance->instancet = &megasas_instance_template_gen2;
4656 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4657 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4658 instance->instancet = &megasas_instance_template_skinny;
4660 case PCI_DEVICE_ID_LSI_SAS1064R:
4661 case PCI_DEVICE_ID_DELL_PERC5:
4663 instance->instancet = &megasas_instance_template_xscale;
4667 if (megasas_transition_to_ready(instance, 0)) {
4668 atomic_set(&instance->fw_reset_no_pci_access, 1);
4669 instance->instancet->adp_reset
4670 (instance, instance->reg_set);
4671 atomic_set(&instance->fw_reset_no_pci_access, 0);
4672 dev_info(&instance->pdev->dev,
4673 "FW restarted successfully from %s!\n",
4676 /*waitting for about 30 second before retry*/
4679 if (megasas_transition_to_ready(instance, 0))
4680 goto fail_ready_state;
4684 * MSI-X host index 0 is common for all adapter.
4685 * It is used for all MPT based Adapters.
4687 instance->reply_post_host_index_addr[0] =
4688 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
4689 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4691 /* Check if MSI-X is supported while in ready state */
4692 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4694 if (msix_enable && !msix_disable) {
4695 scratch_pad_2 = readl
4696 (&instance->reg_set->outbound_scratch_pad_2);
4697 /* Check max MSI-X vectors */
4699 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
4700 instance->msix_vectors = (scratch_pad_2
4701 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4702 fw_msix_count = instance->msix_vectors;
4703 } else { /* Invader series supports more than 8 MSI-x vectors*/
4704 instance->msix_vectors = ((scratch_pad_2
4705 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4706 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4707 fw_msix_count = instance->msix_vectors;
4708 /* Save 1-15 reply post index address to local memory
4709 * Index 0 is already saved from reg offset
4710 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4712 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4713 instance->reply_post_host_index_addr[loop] =
4715 ((u8 __iomem *)instance->reg_set +
4716 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4721 instance->msix_vectors = min(msix_vectors,
4722 instance->msix_vectors);
4723 } else /* MFI adapters */
4724 instance->msix_vectors = 1;
4725 /* Don't bother allocating more MSI-X vectors than cpus */
4726 instance->msix_vectors = min(instance->msix_vectors,
4727 (unsigned int)num_online_cpus());
4728 for (i = 0; i < instance->msix_vectors; i++)
4729 instance->msixentry[i].entry = i;
4730 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4731 1, instance->msix_vectors);
4733 instance->msix_vectors = i;
4735 instance->msix_vectors = 0;
4738 dev_info(&instance->pdev->dev,
4739 "firmware supports msix\t: (%d)", fw_msix_count);
4740 dev_info(&instance->pdev->dev,
4741 "current msix/online cpus\t: (%d/%d)\n",
4742 instance->msix_vectors, (unsigned int)num_online_cpus());
4744 if (instance->msix_vectors ?
4745 megasas_setup_irqs_msix(instance, 1) :
4746 megasas_setup_irqs_ioapic(instance))
4747 goto fail_setup_irqs;
4749 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4751 if (instance->ctrl_info == NULL)
4752 goto fail_init_adapter;
4755 * Below are default value for legacy Firmware.
4756 * non-fusion based controllers
4758 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4759 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4760 /* Get operational params, sge flags, send init cmd to controller */
4761 if (instance->instancet->init_adapter(instance))
4762 goto fail_init_adapter;
4764 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4765 (unsigned long)instance);
4767 instance->instancet->enable_intr(instance);
4769 dev_err(&instance->pdev->dev, "INIT adapter done\n");
4771 megasas_setup_jbod_map(instance);
4774 * the following function will get the PD LIST.
4776 memset(instance->pd_list, 0,
4777 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
4778 if (megasas_get_pd_list(instance) < 0) {
4779 dev_err(&instance->pdev->dev, "failed to get PD list\n");
4780 goto fail_get_pd_list;
4783 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4784 if (megasas_ld_list_query(instance,
4785 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4786 megasas_get_ld_list(instance);
4789 * Compute the max allowed sectors per IO: The controller info has two
4790 * limits on max sectors. Driver should use the minimum of these two.
4792 * 1 << stripe_sz_ops.min = max sectors per strip
4794 * Note that older firmwares ( < FW ver 30) didn't report information
4795 * to calculate max_sectors_1. So the number ended up as zero always.
4798 ctrl_info = instance->ctrl_info;
4800 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4801 le16_to_cpu(ctrl_info->max_strips_per_io);
4802 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
4804 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
4806 instance->mpio = ctrl_info->adapterOperations2.mpio;
4807 instance->UnevenSpanSupport =
4808 ctrl_info->adapterOperations2.supportUnevenSpans;
4809 if (instance->UnevenSpanSupport) {
4810 struct fusion_context *fusion = instance->ctrl_context;
4811 if (MR_ValidateMapInfo(instance))
4812 fusion->fast_path_io = 1;
4814 fusion->fast_path_io = 0;
4817 if (ctrl_info->host_interface.SRIOV) {
4818 instance->requestorId = ctrl_info->iov.requestorId;
4819 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
4820 if (!ctrl_info->adapterOperations2.activePassive)
4821 instance->PlasmaFW111 = 1;
4823 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
4824 instance->PlasmaFW111 ? "1.11" : "new");
4826 if (instance->PlasmaFW111) {
4827 iovPtr = (struct IOV_111 *)
4828 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
4829 instance->requestorId = iovPtr->requestorId;
4832 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
4833 instance->requestorId);
4836 instance->crash_dump_fw_support =
4837 ctrl_info->adapterOperations3.supportCrashDump;
4838 instance->crash_dump_drv_support =
4839 (instance->crash_dump_fw_support &&
4840 instance->crash_dump_buf);
4841 if (instance->crash_dump_drv_support)
4842 megasas_set_crash_dump_params(instance,
4843 MR_CRASH_BUF_TURN_OFF);
4846 if (instance->crash_dump_buf)
4847 pci_free_consistent(instance->pdev,
4849 instance->crash_dump_buf,
4850 instance->crash_dump_h);
4851 instance->crash_dump_buf = NULL;
4854 instance->secure_jbod_support =
4855 ctrl_info->adapterOperations3.supportSecurityonJBOD;
4857 dev_info(&instance->pdev->dev,
4858 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
4859 le16_to_cpu(ctrl_info->pci.vendor_id),
4860 le16_to_cpu(ctrl_info->pci.device_id),
4861 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
4862 le16_to_cpu(ctrl_info->pci.sub_device_id));
4863 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
4864 instance->UnevenSpanSupport ? "yes" : "no");
4865 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
4866 instance->crash_dump_drv_support ? "yes" : "no");
4867 dev_info(&instance->pdev->dev, "secure jbod : %s\n",
4868 instance->secure_jbod_support ? "yes" : "no");
4869 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
4870 instance->use_seqnum_jbod_fp ? "yes" : "no");
4873 instance->max_sectors_per_req = instance->max_num_sge *
4874 SGE_BUFFER_SIZE / 512;
4875 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4876 instance->max_sectors_per_req = tmp_sectors;
4878 /* Check for valid throttlequeuedepth module parameter */
4879 if (throttlequeuedepth &&
4880 throttlequeuedepth <= instance->max_scsi_cmds)
4881 instance->throttlequeuedepth = throttlequeuedepth;
4883 instance->throttlequeuedepth =
4884 MEGASAS_THROTTLE_QUEUE_DEPTH;
4887 /* Launch SR-IOV heartbeat timer */
4888 if (instance->requestorId) {
4889 if (!megasas_sriov_start_heartbeat(instance, 1))
4890 megasas_start_timer(instance,
4891 &instance->sriov_heartbeat_timer,
4892 megasas_sriov_heartbeat_handler,
4893 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4895 instance->skip_heartbeat_timer_del = 1;
4901 instance->instancet->disable_intr(instance);
4903 megasas_destroy_irqs(instance);
4905 if (instance->msix_vectors)
4906 pci_disable_msix(instance->pdev);
4907 instance->msix_vectors = 0;
4909 kfree(instance->ctrl_info);
4910 instance->ctrl_info = NULL;
4911 iounmap(instance->reg_set);
4914 pci_release_selected_regions(instance->pdev, instance->bar);
4920 * megasas_release_mfi - Reverses the FW initialization
4921 * @instance: Adapter soft state
4923 static void megasas_release_mfi(struct megasas_instance *instance)
4925 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
4927 if (instance->reply_queue)
4928 pci_free_consistent(instance->pdev, reply_q_sz,
4929 instance->reply_queue, instance->reply_queue_h);
4931 megasas_free_cmds(instance);
4933 iounmap(instance->reg_set);
4935 pci_release_selected_regions(instance->pdev, instance->bar);
4939 * megasas_get_seq_num - Gets latest event sequence numbers
4940 * @instance: Adapter soft state
4941 * @eli: FW event log sequence numbers information
4943 * FW maintains a log of all events in a non-volatile area. Upper layers would
4944 * usually find out the latest sequence number of the events, the seq number at
4945 * the boot etc. They would "read" all the events below the latest seq number
4946 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4947 * number), they would subsribe to AEN (asynchronous event notification) and
4948 * wait for the events to happen.
4951 megasas_get_seq_num(struct megasas_instance *instance,
4952 struct megasas_evt_log_info *eli)
4954 struct megasas_cmd *cmd;
4955 struct megasas_dcmd_frame *dcmd;
4956 struct megasas_evt_log_info *el_info;
4957 dma_addr_t el_info_h = 0;
4959 cmd = megasas_get_cmd(instance);
4965 dcmd = &cmd->frame->dcmd;
4966 el_info = pci_alloc_consistent(instance->pdev,
4967 sizeof(struct megasas_evt_log_info),
4971 megasas_return_cmd(instance, cmd);
4975 memset(el_info, 0, sizeof(*el_info));
4976 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4978 dcmd->cmd = MFI_CMD_DCMD;
4979 dcmd->cmd_status = 0x0;
4980 dcmd->sge_count = 1;
4981 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4984 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4985 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
4986 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
4987 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4989 if (megasas_issue_blocked_cmd(instance, cmd, 30))
4990 dev_err(&instance->pdev->dev, "Command timedout"
4991 "from %s\n", __func__);
4994 * Copy the data back into callers buffer
4996 eli->newest_seq_num = el_info->newest_seq_num;
4997 eli->oldest_seq_num = el_info->oldest_seq_num;
4998 eli->clear_seq_num = el_info->clear_seq_num;
4999 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5000 eli->boot_seq_num = el_info->boot_seq_num;
5003 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5004 el_info, el_info_h);
5006 megasas_return_cmd(instance, cmd);
5012 * megasas_register_aen - Registers for asynchronous event notification
5013 * @instance: Adapter soft state
5014 * @seq_num: The starting sequence number
5015 * @class_locale: Class of the event
5017 * This function subscribes for AEN for events beyond the @seq_num. It requests
5018 * to be notified if and only if the event is of type @class_locale
5021 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5022 u32 class_locale_word)
5025 struct megasas_cmd *cmd;
5026 struct megasas_dcmd_frame *dcmd;
5027 union megasas_evt_class_locale curr_aen;
5028 union megasas_evt_class_locale prev_aen;
5031 * If there an AEN pending already (aen_cmd), check if the
5032 * class_locale of that pending AEN is inclusive of the new
5033 * AEN request we currently have. If it is, then we don't have
5034 * to do anything. In other words, whichever events the current
5035 * AEN request is subscribing to, have already been subscribed
5038 * If the old_cmd is _not_ inclusive, then we have to abort
5039 * that command, form a class_locale that is superset of both
5040 * old and current and re-issue to the FW
5043 curr_aen.word = class_locale_word;
5045 if (instance->aen_cmd) {
5048 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
5051 * A class whose enum value is smaller is inclusive of all
5052 * higher values. If a PROGRESS (= -1) was previously
5053 * registered, then a new registration requests for higher
5054 * classes need not be sent to FW. They are automatically
5057 * Locale numbers don't have such hierarchy. They are bitmap
5060 if ((prev_aen.members.class <= curr_aen.members.class) &&
5061 !((prev_aen.members.locale & curr_aen.members.locale) ^
5062 curr_aen.members.locale)) {
5064 * Previously issued event registration includes
5065 * current request. Nothing to do.
5069 curr_aen.members.locale |= prev_aen.members.locale;
5071 if (prev_aen.members.class < curr_aen.members.class)
5072 curr_aen.members.class = prev_aen.members.class;
5074 instance->aen_cmd->abort_aen = 1;
5075 ret_val = megasas_issue_blocked_abort_cmd(instance,
5080 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
5081 "previous AEN command\n");
5087 cmd = megasas_get_cmd(instance);
5092 dcmd = &cmd->frame->dcmd;
5094 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5097 * Prepare DCMD for aen registration
5099 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5101 dcmd->cmd = MFI_CMD_DCMD;
5102 dcmd->cmd_status = 0x0;
5103 dcmd->sge_count = 1;
5104 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5107 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5108 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5109 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
5110 instance->last_seq_num = seq_num;
5111 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5112 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5113 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
5115 if (instance->aen_cmd != NULL) {
5116 megasas_return_cmd(instance, cmd);
5121 * Store reference to the cmd used to register for AEN. When an
5122 * application wants us to register for AEN, we have to abort this
5123 * cmd and re-register with a new EVENT LOCALE supplied by that app
5125 instance->aen_cmd = cmd;
5128 * Issue the aen registration frame
5130 instance->instancet->issue_dcmd(instance, cmd);
5136 * megasas_start_aen - Subscribes to AEN during driver load time
5137 * @instance: Adapter soft state
5139 static int megasas_start_aen(struct megasas_instance *instance)
5141 struct megasas_evt_log_info eli;
5142 union megasas_evt_class_locale class_locale;
5145 * Get the latest sequence number from FW
5147 memset(&eli, 0, sizeof(eli));
5149 if (megasas_get_seq_num(instance, &eli))
5153 * Register AEN with FW for latest sequence number plus 1
5155 class_locale.members.reserved = 0;
5156 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5157 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5159 return megasas_register_aen(instance,
5160 le32_to_cpu(eli.newest_seq_num) + 1,
5165 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5166 * @instance: Adapter soft state
5168 static int megasas_io_attach(struct megasas_instance *instance)
5170 struct Scsi_Host *host = instance->host;
5174 * Export parameters required by SCSI mid-layer
5176 host->irq = instance->pdev->irq;
5177 host->unique_id = instance->unique_id;
5178 host->can_queue = instance->max_scsi_cmds;
5179 host->this_id = instance->init_id;
5180 host->sg_tablesize = instance->max_num_sge;
5182 if (instance->fw_support_ieee)
5183 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5186 * Check if the module parameter value for max_sectors can be used
5188 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5189 instance->max_sectors_per_req = max_sectors;
5192 if (((instance->pdev->device ==
5193 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5194 (instance->pdev->device ==
5195 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5196 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5197 instance->max_sectors_per_req = max_sectors;
5199 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
5200 "and <= %d (or < 1MB for GEN2 controller)\n",
5201 instance->max_sectors_per_req);
5206 host->max_sectors = instance->max_sectors_per_req;
5207 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
5208 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5209 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5210 host->max_lun = MEGASAS_MAX_LUN;
5211 host->max_cmd_len = 16;
5213 /* Fusion only supports host reset */
5214 if (instance->ctrl_context) {
5215 host->hostt->eh_device_reset_handler = NULL;
5216 host->hostt->eh_bus_reset_handler = NULL;
5218 error = scsi_init_shared_tag_map(host, host->can_queue);
5220 dev_err(&instance->pdev->dev,
5221 "Failed to shared tag from %s %d\n",
5222 __func__, __LINE__);
5227 * Notify the mid-layer about the new controller
5229 if (scsi_add_host(host, &instance->pdev->dev)) {
5230 dev_err(&instance->pdev->dev,
5231 "Failed to add host from %s %d\n",
5232 __func__, __LINE__);
5240 megasas_set_dma_mask(struct pci_dev *pdev)
5243 * All our controllers are capable of performing 64-bit DMA
5246 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
5248 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5249 goto fail_set_dma_mask;
5252 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5253 goto fail_set_dma_mask;
5256 * Ensure that all data structures are allocated in 32-bit
5259 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5260 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5261 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5262 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5263 dev_info(&pdev->dev, "set 32bit DMA mask"
5264 "and 32 bit consistent mask\n");
5266 goto fail_set_dma_mask;
5276 * megasas_probe_one - PCI hotplug entry point
5277 * @pdev: PCI device structure
5278 * @id: PCI ids of supported hotplugged adapter
5280 static int megasas_probe_one(struct pci_dev *pdev,
5281 const struct pci_device_id *id)
5284 struct Scsi_Host *host;
5285 struct megasas_instance *instance;
5287 struct fusion_context *fusion = NULL;
5289 /* Reset MSI-X in the kdump kernel */
5290 if (reset_devices) {
5291 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5293 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
5295 if (control & PCI_MSIX_FLAGS_ENABLE) {
5296 dev_info(&pdev->dev, "resetting MSI-X\n");
5297 pci_write_config_word(pdev,
5298 pos + PCI_MSIX_FLAGS,
5300 ~PCI_MSIX_FLAGS_ENABLE);
5306 * PCI prepping: enable device set bus mastering and dma mask
5308 rval = pci_enable_device_mem(pdev);
5314 pci_set_master(pdev);
5316 if (megasas_set_dma_mask(pdev))
5317 goto fail_set_dma_mask;
5319 host = scsi_host_alloc(&megasas_template,
5320 sizeof(struct megasas_instance));
5323 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
5324 goto fail_alloc_instance;
5327 instance = (struct megasas_instance *)host->hostdata;
5328 memset(instance, 0, sizeof(*instance));
5329 atomic_set(&instance->fw_reset_no_pci_access, 0);
5330 instance->pdev = pdev;
5332 switch (instance->pdev->device) {
5333 case PCI_DEVICE_ID_LSI_FUSION:
5334 case PCI_DEVICE_ID_LSI_PLASMA:
5335 case PCI_DEVICE_ID_LSI_INVADER:
5336 case PCI_DEVICE_ID_LSI_FURY:
5337 case PCI_DEVICE_ID_LSI_INTRUDER:
5338 case PCI_DEVICE_ID_LSI_INTRUDER_24:
5339 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5340 case PCI_DEVICE_ID_LSI_CUTLASS_53:
5342 instance->ctrl_context_pages =
5343 get_order(sizeof(struct fusion_context));
5344 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5345 instance->ctrl_context_pages);
5346 if (!instance->ctrl_context) {
5347 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
5348 "memory for Fusion context info\n");
5349 goto fail_alloc_dma_buf;
5351 fusion = instance->ctrl_context;
5353 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
5354 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5355 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5356 fusion->adapter_type = THUNDERBOLT_SERIES;
5358 fusion->adapter_type = INVADER_SERIES;
5361 default: /* For all other supported controllers */
5363 instance->producer =
5364 pci_alloc_consistent(pdev, sizeof(u32),
5365 &instance->producer_h);
5366 instance->consumer =
5367 pci_alloc_consistent(pdev, sizeof(u32),
5368 &instance->consumer_h);
5370 if (!instance->producer || !instance->consumer) {
5371 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate"
5372 "memory for producer, consumer\n");
5373 goto fail_alloc_dma_buf;
5376 *instance->producer = 0;
5377 *instance->consumer = 0;
5381 instance->system_info_buf = pci_zalloc_consistent(pdev,
5382 sizeof(struct MR_DRV_SYSTEM_INFO),
5383 &instance->system_info_h);
5385 if (!instance->system_info_buf)
5386 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5388 /* Crash dump feature related initialisation*/
5389 instance->drv_buf_index = 0;
5390 instance->drv_buf_alloc = 0;
5391 instance->crash_dump_fw_support = 0;
5392 instance->crash_dump_app_support = 0;
5393 instance->fw_crash_state = UNAVAILABLE;
5394 spin_lock_init(&instance->crashdump_lock);
5395 instance->crash_dump_buf = NULL;
5398 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5400 &instance->crash_dump_h);
5401 if (!instance->crash_dump_buf)
5402 dev_err(&pdev->dev, "Can't allocate Firmware "
5403 "crash dump DMA buffer\n");
5405 megasas_poll_wait_aen = 0;
5406 instance->flag_ieee = 0;
5407 instance->ev = NULL;
5408 instance->issuepend_done = 1;
5409 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
5410 instance->is_imr = 0;
5412 instance->evt_detail = pci_alloc_consistent(pdev,
5414 megasas_evt_detail),
5415 &instance->evt_detail_h);
5417 if (!instance->evt_detail) {
5418 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
5419 "event detail structure\n");
5420 goto fail_alloc_dma_buf;
5424 * Initialize locks and queues
5426 INIT_LIST_HEAD(&instance->cmd_pool);
5427 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
5429 atomic_set(&instance->fw_outstanding,0);
5431 init_waitqueue_head(&instance->int_cmd_wait_q);
5432 init_waitqueue_head(&instance->abort_cmd_wait_q);
5434 spin_lock_init(&instance->mfi_pool_lock);
5435 spin_lock_init(&instance->hba_lock);
5436 spin_lock_init(&instance->completion_lock);
5438 mutex_init(&instance->aen_mutex);
5439 mutex_init(&instance->reset_mutex);
5442 * Initialize PCI related and misc parameters
5444 instance->host = host;
5445 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5446 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
5447 instance->ctrl_info = NULL;
5450 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5451 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
5452 instance->flag_ieee = 1;
5454 megasas_dbg_lvl = 0;
5456 instance->unload = 1;
5457 instance->last_time = 0;
5458 instance->disableOnlineCtrlReset = 1;
5459 instance->UnevenSpanSupport = 0;
5461 if (instance->ctrl_context) {
5462 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
5463 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5465 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
5468 * Initialize MFI Firmware
5470 if (megasas_init_fw(instance))
5473 if (instance->requestorId) {
5474 if (instance->PlasmaFW111) {
5475 instance->vf_affiliation_111 =
5476 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5477 &instance->vf_affiliation_111_h);
5478 if (!instance->vf_affiliation_111)
5479 dev_warn(&pdev->dev, "Can't allocate "
5480 "memory for VF affiliation buffer\n");
5482 instance->vf_affiliation =
5483 pci_alloc_consistent(pdev,
5484 (MAX_LOGICAL_DRIVES + 1) *
5485 sizeof(struct MR_LD_VF_AFFILIATION),
5486 &instance->vf_affiliation_h);
5487 if (!instance->vf_affiliation)
5488 dev_warn(&pdev->dev, "Can't allocate "
5489 "memory for VF affiliation buffer\n");
5494 * Store instance in PCI softstate
5496 pci_set_drvdata(pdev, instance);
5499 * Add this controller to megasas_mgmt_info structure so that it
5500 * can be exported to management applications
5502 megasas_mgmt_info.count++;
5503 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5504 megasas_mgmt_info.max_index++;
5507 * Register with SCSI mid-layer
5509 if (megasas_io_attach(instance))
5510 goto fail_io_attach;
5512 instance->unload = 0;
5514 * Trigger SCSI to scan our drives
5516 scsi_scan_host(host);
5519 * Initiate AEN (Asynchronous Event Notification)
5521 if (megasas_start_aen(instance)) {
5522 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
5523 goto fail_start_aen;
5526 /* Get current SR-IOV LD/VF affiliation */
5527 if (instance->requestorId)
5528 megasas_get_ld_vf_affiliation(instance, 1);
5534 megasas_mgmt_info.count--;
5535 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5536 megasas_mgmt_info.max_index--;
5538 instance->instancet->disable_intr(instance);
5539 megasas_destroy_irqs(instance);
5541 if (instance->ctrl_context)
5542 megasas_release_fusion(instance);
5544 megasas_release_mfi(instance);
5545 if (instance->msix_vectors)
5546 pci_disable_msix(instance->pdev);
5549 if (instance->evt_detail)
5550 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5551 instance->evt_detail,
5552 instance->evt_detail_h);
5554 if (instance->producer)
5555 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5556 instance->producer_h);
5557 if (instance->consumer)
5558 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5559 instance->consumer_h);
5560 scsi_host_put(host);
5562 fail_alloc_instance:
5564 pci_disable_device(pdev);
5570 * megasas_flush_cache - Requests FW to flush all its caches
5571 * @instance: Adapter soft state
5573 static void megasas_flush_cache(struct megasas_instance *instance)
5575 struct megasas_cmd *cmd;
5576 struct megasas_dcmd_frame *dcmd;
5578 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5581 cmd = megasas_get_cmd(instance);
5586 dcmd = &cmd->frame->dcmd;
5588 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5590 dcmd->cmd = MFI_CMD_DCMD;
5591 dcmd->cmd_status = 0x0;
5592 dcmd->sge_count = 0;
5593 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5596 dcmd->data_xfer_len = 0;
5597 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
5598 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5600 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5601 dev_err(&instance->pdev->dev, "Command timedout"
5602 " from %s\n", __func__);
5604 megasas_return_cmd(instance, cmd);
5608 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5609 * @instance: Adapter soft state
5610 * @opcode: Shutdown/Hibernate
5612 static void megasas_shutdown_controller(struct megasas_instance *instance,
5615 struct megasas_cmd *cmd;
5616 struct megasas_dcmd_frame *dcmd;
5618 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5621 cmd = megasas_get_cmd(instance);
5626 if (instance->aen_cmd)
5627 megasas_issue_blocked_abort_cmd(instance,
5628 instance->aen_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5629 if (instance->map_update_cmd)
5630 megasas_issue_blocked_abort_cmd(instance,
5631 instance->map_update_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5632 if (instance->jbod_seq_cmd)
5633 megasas_issue_blocked_abort_cmd(instance,
5634 instance->jbod_seq_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5636 dcmd = &cmd->frame->dcmd;
5638 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5640 dcmd->cmd = MFI_CMD_DCMD;
5641 dcmd->cmd_status = 0x0;
5642 dcmd->sge_count = 0;
5643 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5646 dcmd->data_xfer_len = 0;
5647 dcmd->opcode = cpu_to_le32(opcode);
5649 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5650 dev_err(&instance->pdev->dev, "Command timedout"
5651 "from %s\n", __func__);
5653 megasas_return_cmd(instance, cmd);
5658 * megasas_suspend - driver suspend entry point
5659 * @pdev: PCI device structure
5660 * @state: PCI power state to suspend routine
5663 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5665 struct Scsi_Host *host;
5666 struct megasas_instance *instance;
5668 instance = pci_get_drvdata(pdev);
5669 host = instance->host;
5670 instance->unload = 1;
5672 /* Shutdown SR-IOV heartbeat timer */
5673 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5674 del_timer_sync(&instance->sriov_heartbeat_timer);
5676 megasas_flush_cache(instance);
5677 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
5679 /* cancel the delayed work if this work still in queue */
5680 if (instance->ev != NULL) {
5681 struct megasas_aen_event *ev = instance->ev;
5682 cancel_delayed_work_sync(&ev->hotplug_work);
5683 instance->ev = NULL;
5686 tasklet_kill(&instance->isr_tasklet);
5688 pci_set_drvdata(instance->pdev, instance);
5689 instance->instancet->disable_intr(instance);
5691 megasas_destroy_irqs(instance);
5693 if (instance->msix_vectors)
5694 pci_disable_msix(instance->pdev);
5696 pci_save_state(pdev);
5697 pci_disable_device(pdev);
5699 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5705 * megasas_resume- driver resume entry point
5706 * @pdev: PCI device structure
5709 megasas_resume(struct pci_dev *pdev)
5712 struct Scsi_Host *host;
5713 struct megasas_instance *instance;
5715 instance = pci_get_drvdata(pdev);
5716 host = instance->host;
5717 pci_set_power_state(pdev, PCI_D0);
5718 pci_enable_wake(pdev, PCI_D0, 0);
5719 pci_restore_state(pdev);
5722 * PCI prepping: enable device set bus mastering and dma mask
5724 rval = pci_enable_device_mem(pdev);
5727 dev_err(&pdev->dev, "Enable device failed\n");
5731 pci_set_master(pdev);
5733 if (megasas_set_dma_mask(pdev))
5734 goto fail_set_dma_mask;
5737 * Initialize MFI Firmware
5740 atomic_set(&instance->fw_outstanding, 0);
5743 * We expect the FW state to be READY
5745 if (megasas_transition_to_ready(instance, 0))
5746 goto fail_ready_state;
5748 /* Now re-enable MSI-X */
5749 if (instance->msix_vectors &&
5750 pci_enable_msix_exact(instance->pdev, instance->msixentry,
5751 instance->msix_vectors))
5752 goto fail_reenable_msix;
5754 if (instance->ctrl_context) {
5755 megasas_reset_reply_desc(instance);
5756 if (megasas_ioc_init_fusion(instance)) {
5757 megasas_free_cmds(instance);
5758 megasas_free_cmds_fusion(instance);
5761 if (!megasas_get_map_info(instance))
5762 megasas_sync_map_info(instance);
5764 *instance->producer = 0;
5765 *instance->consumer = 0;
5766 if (megasas_issue_init_mfi(instance))
5770 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5771 (unsigned long)instance);
5773 if (instance->msix_vectors ?
5774 megasas_setup_irqs_msix(instance, 0) :
5775 megasas_setup_irqs_ioapic(instance))
5778 /* Re-launch SR-IOV heartbeat timer */
5779 if (instance->requestorId) {
5780 if (!megasas_sriov_start_heartbeat(instance, 0))
5781 megasas_start_timer(instance,
5782 &instance->sriov_heartbeat_timer,
5783 megasas_sriov_heartbeat_handler,
5784 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5786 instance->skip_heartbeat_timer_del = 1;
5791 instance->instancet->enable_intr(instance);
5792 megasas_setup_jbod_map(instance);
5793 instance->unload = 0;
5796 * Initiate AEN (Asynchronous Event Notification)
5798 if (megasas_start_aen(instance))
5799 dev_err(&instance->pdev->dev, "Start AEN failed\n");
5804 if (instance->evt_detail)
5805 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5806 instance->evt_detail,
5807 instance->evt_detail_h);
5809 if (instance->producer)
5810 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5811 instance->producer_h);
5812 if (instance->consumer)
5813 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5814 instance->consumer_h);
5815 scsi_host_put(host);
5821 pci_disable_device(pdev);
5826 #define megasas_suspend NULL
5827 #define megasas_resume NULL
5831 * megasas_detach_one - PCI hot"un"plug entry point
5832 * @pdev: PCI device structure
5834 static void megasas_detach_one(struct pci_dev *pdev)
5837 struct Scsi_Host *host;
5838 struct megasas_instance *instance;
5839 struct fusion_context *fusion;
5842 instance = pci_get_drvdata(pdev);
5843 instance->unload = 1;
5844 host = instance->host;
5845 fusion = instance->ctrl_context;
5847 /* Shutdown SR-IOV heartbeat timer */
5848 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5849 del_timer_sync(&instance->sriov_heartbeat_timer);
5851 if (instance->fw_crash_state != UNAVAILABLE)
5852 megasas_free_host_crash_buffer(instance);
5853 scsi_remove_host(instance->host);
5854 megasas_flush_cache(instance);
5855 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5857 /* cancel the delayed work if this work still in queue*/
5858 if (instance->ev != NULL) {
5859 struct megasas_aen_event *ev = instance->ev;
5860 cancel_delayed_work_sync(&ev->hotplug_work);
5861 instance->ev = NULL;
5864 /* cancel all wait events */
5865 wake_up_all(&instance->int_cmd_wait_q);
5867 tasklet_kill(&instance->isr_tasklet);
5870 * Take the instance off the instance array. Note that we will not
5871 * decrement the max_index. We let this array be sparse array
5873 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5874 if (megasas_mgmt_info.instance[i] == instance) {
5875 megasas_mgmt_info.count--;
5876 megasas_mgmt_info.instance[i] = NULL;
5882 instance->instancet->disable_intr(instance);
5884 megasas_destroy_irqs(instance);
5886 if (instance->msix_vectors)
5887 pci_disable_msix(instance->pdev);
5889 if (instance->ctrl_context) {
5890 megasas_release_fusion(instance);
5891 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5892 (sizeof(struct MR_PD_CFG_SEQ) *
5893 (MAX_PHYSICAL_DEVICES - 1));
5894 for (i = 0; i < 2 ; i++) {
5895 if (fusion->ld_map[i])
5896 dma_free_coherent(&instance->pdev->dev,
5899 fusion->ld_map_phys[i]);
5900 if (fusion->ld_drv_map[i])
5901 free_pages((ulong)fusion->ld_drv_map[i],
5902 fusion->drv_map_pages);
5903 if (fusion->pd_seq_sync)
5904 dma_free_coherent(&instance->pdev->dev,
5906 fusion->pd_seq_sync[i],
5907 fusion->pd_seq_phys[i]);
5909 free_pages((ulong)instance->ctrl_context,
5910 instance->ctrl_context_pages);
5912 megasas_release_mfi(instance);
5913 pci_free_consistent(pdev, sizeof(u32),
5915 instance->producer_h);
5916 pci_free_consistent(pdev, sizeof(u32),
5918 instance->consumer_h);
5921 kfree(instance->ctrl_info);
5923 if (instance->evt_detail)
5924 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5925 instance->evt_detail, instance->evt_detail_h);
5927 if (instance->vf_affiliation)
5928 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5929 sizeof(struct MR_LD_VF_AFFILIATION),
5930 instance->vf_affiliation,
5931 instance->vf_affiliation_h);
5933 if (instance->vf_affiliation_111)
5934 pci_free_consistent(pdev,
5935 sizeof(struct MR_LD_VF_AFFILIATION_111),
5936 instance->vf_affiliation_111,
5937 instance->vf_affiliation_111_h);
5939 if (instance->hb_host_mem)
5940 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5941 instance->hb_host_mem,
5942 instance->hb_host_mem_h);
5944 if (instance->crash_dump_buf)
5945 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5946 instance->crash_dump_buf, instance->crash_dump_h);
5948 if (instance->system_info_buf)
5949 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
5950 instance->system_info_buf, instance->system_info_h);
5952 scsi_host_put(host);
5954 pci_disable_device(pdev);
5958 * megasas_shutdown - Shutdown entry point
5959 * @device: Generic device structure
5961 static void megasas_shutdown(struct pci_dev *pdev)
5963 struct megasas_instance *instance = pci_get_drvdata(pdev);
5965 instance->unload = 1;
5966 megasas_flush_cache(instance);
5967 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5968 instance->instancet->disable_intr(instance);
5969 megasas_destroy_irqs(instance);
5971 if (instance->msix_vectors)
5972 pci_disable_msix(instance->pdev);
5976 * megasas_mgmt_open - char node "open" entry point
5978 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
5981 * Allow only those users with admin rights
5983 if (!capable(CAP_SYS_ADMIN))
5990 * megasas_mgmt_fasync - Async notifier registration from applications
5992 * This function adds the calling process to a driver global queue. When an
5993 * event occurs, SIGIO will be sent to all processes in this queue.
5995 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
5999 mutex_lock(&megasas_async_queue_mutex);
6001 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6003 mutex_unlock(&megasas_async_queue_mutex);
6006 /* For sanity check when we get ioctl */
6007 filep->private_data = filep;
6011 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6017 * megasas_mgmt_poll - char node "poll" entry point
6019 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6022 unsigned long flags;
6024 poll_wait(file, &megasas_poll_wait, wait);
6025 spin_lock_irqsave(&poll_aen_lock, flags);
6026 if (megasas_poll_wait_aen)
6027 mask = (POLLIN | POLLRDNORM);
6030 megasas_poll_wait_aen = 0;
6031 spin_unlock_irqrestore(&poll_aen_lock, flags);
6036 * megasas_set_crash_dump_params_ioctl:
6037 * Send CRASH_DUMP_MODE DCMD to all controllers
6038 * @cmd: MFI command frame
6041 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
6043 struct megasas_instance *local_instance;
6047 crash_support = cmd->frame->dcmd.mbox.w[0];
6049 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6050 local_instance = megasas_mgmt_info.instance[i];
6051 if (local_instance && local_instance->crash_dump_drv_support) {
6052 if ((local_instance->adprecovery ==
6053 MEGASAS_HBA_OPERATIONAL) &&
6054 !megasas_set_crash_dump_params(local_instance,
6056 local_instance->crash_dump_app_support =
6058 dev_info(&local_instance->pdev->dev,
6059 "Application firmware crash "
6060 "dump mode set success\n");
6063 dev_info(&local_instance->pdev->dev,
6064 "Application firmware crash "
6065 "dump mode set failed\n");
6074 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6075 * @instance: Adapter soft state
6076 * @argp: User's ioctl packet
6079 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6080 struct megasas_iocpacket __user * user_ioc,
6081 struct megasas_iocpacket *ioc)
6083 struct megasas_sge32 *kern_sge32;
6084 struct megasas_cmd *cmd;
6085 void *kbuff_arr[MAX_IOCTL_SGE];
6086 dma_addr_t buf_handle = 0;
6089 dma_addr_t sense_handle;
6090 unsigned long *sense_ptr;
6092 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6094 if (ioc->sge_count > MAX_IOCTL_SGE) {
6095 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
6096 ioc->sge_count, MAX_IOCTL_SGE);
6100 cmd = megasas_get_cmd(instance);
6102 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
6107 * User's IOCTL packet has 2 frames (maximum). Copy those two
6108 * frames into our cmd's frames. cmd->frame's context will get
6109 * overwritten when we copy from user's frames. So set that value
6112 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
6113 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
6114 cmd->frame->hdr.pad_0 = 0;
6115 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6117 MFI_FRAME_SENSE64));
6119 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6120 error = megasas_set_crash_dump_params_ioctl(cmd);
6121 megasas_return_cmd(instance, cmd);
6126 * The management interface between applications and the fw uses
6127 * MFI frames. E.g, RAID configuration changes, LD property changes
6128 * etc are accomplishes through different kinds of MFI frames. The
6129 * driver needs to care only about substituting user buffers with
6130 * kernel buffers in SGLs. The location of SGL is embedded in the
6131 * struct iocpacket itself.
6133 kern_sge32 = (struct megasas_sge32 *)
6134 ((unsigned long)cmd->frame + ioc->sgl_off);
6137 * For each user buffer, create a mirror buffer and copy in
6139 for (i = 0; i < ioc->sge_count; i++) {
6140 if (!ioc->sgl[i].iov_len)
6143 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
6144 ioc->sgl[i].iov_len,
6145 &buf_handle, GFP_KERNEL);
6146 if (!kbuff_arr[i]) {
6147 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6148 "kernel SGL buffer for IOCTL\n");
6154 * We don't change the dma_coherent_mask, so
6155 * pci_alloc_consistent only returns 32bit addresses
6157 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6158 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
6161 * We created a kernel buffer corresponding to the
6162 * user buffer. Now copy in from the user buffer
6164 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6165 (u32) (ioc->sgl[i].iov_len))) {
6171 if (ioc->sense_len) {
6172 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6173 &sense_handle, GFP_KERNEL);
6180 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
6181 *sense_ptr = cpu_to_le32(sense_handle);
6185 * Set the sync_cmd flag so that the ISR knows not to complete this
6186 * cmd to the SCSI mid-layer
6189 megasas_issue_blocked_cmd(instance, cmd, 0);
6192 if (instance->unload == 1) {
6193 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6194 "don't submit data to application\n");
6198 * copy out the kernel buffers to user buffers
6200 for (i = 0; i < ioc->sge_count; i++) {
6201 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6202 ioc->sgl[i].iov_len)) {
6209 * copy out the sense
6211 if (ioc->sense_len) {
6213 * sense_ptr points to the location that has the user
6214 * sense buffer address
6216 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6219 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6220 sense, ioc->sense_len)) {
6221 dev_err(&instance->pdev->dev, "Failed to copy out to user "
6229 * copy the status codes returned by the fw
6231 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6232 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6233 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
6239 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
6240 sense, sense_handle);
6243 for (i = 0; i < ioc->sge_count; i++) {
6245 dma_free_coherent(&instance->pdev->dev,
6246 le32_to_cpu(kern_sge32[i].length),
6248 le32_to_cpu(kern_sge32[i].phys_addr));
6249 kbuff_arr[i] = NULL;
6252 megasas_return_cmd(instance, cmd);
6256 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6258 struct megasas_iocpacket __user *user_ioc =
6259 (struct megasas_iocpacket __user *)arg;
6260 struct megasas_iocpacket *ioc;
6261 struct megasas_instance *instance;
6264 unsigned long flags;
6265 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6267 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6271 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6276 instance = megasas_lookup_instance(ioc->host_no);
6282 /* Adjust ioctl wait time for VF mode */
6283 if (instance->requestorId)
6284 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6286 /* Block ioctls in VF mode */
6287 if (instance->requestorId && !allow_vf_ioctls) {
6292 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6293 dev_err(&instance->pdev->dev, "Controller in crit error\n");
6298 if (instance->unload == 1) {
6303 if (down_interruptible(&instance->ioctl_sem)) {
6304 error = -ERESTARTSYS;
6308 for (i = 0; i < wait_time; i++) {
6310 spin_lock_irqsave(&instance->hba_lock, flags);
6311 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6312 spin_unlock_irqrestore(&instance->hba_lock, flags);
6315 spin_unlock_irqrestore(&instance->hba_lock, flags);
6317 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6318 dev_notice(&instance->pdev->dev, "waiting"
6319 "for controller reset to finish\n");
6325 spin_lock_irqsave(&instance->hba_lock, flags);
6326 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6327 spin_unlock_irqrestore(&instance->hba_lock, flags);
6329 dev_err(&instance->pdev->dev, "timed out while"
6330 "waiting for HBA to recover\n");
6334 spin_unlock_irqrestore(&instance->hba_lock, flags);
6336 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
6338 up(&instance->ioctl_sem);
6345 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6347 struct megasas_instance *instance;
6348 struct megasas_aen aen;
6351 unsigned long flags;
6352 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6354 if (file->private_data != file) {
6355 printk(KERN_DEBUG "megasas: fasync_helper was not "
6360 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6363 instance = megasas_lookup_instance(aen.host_no);
6368 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6372 if (instance->unload == 1) {
6376 for (i = 0; i < wait_time; i++) {
6378 spin_lock_irqsave(&instance->hba_lock, flags);
6379 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6380 spin_unlock_irqrestore(&instance->hba_lock,
6385 spin_unlock_irqrestore(&instance->hba_lock, flags);
6387 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6388 dev_notice(&instance->pdev->dev, "waiting for"
6389 "controller reset to finish\n");
6395 spin_lock_irqsave(&instance->hba_lock, flags);
6396 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6397 spin_unlock_irqrestore(&instance->hba_lock, flags);
6398 dev_err(&instance->pdev->dev, "timed out while waiting"
6399 "for HBA to recover\n");
6402 spin_unlock_irqrestore(&instance->hba_lock, flags);
6404 mutex_lock(&instance->aen_mutex);
6405 error = megasas_register_aen(instance, aen.seq_num,
6406 aen.class_locale_word);
6407 mutex_unlock(&instance->aen_mutex);
6412 * megasas_mgmt_ioctl - char node ioctl entry point
6415 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6418 case MEGASAS_IOC_FIRMWARE:
6419 return megasas_mgmt_ioctl_fw(file, arg);
6421 case MEGASAS_IOC_GET_AEN:
6422 return megasas_mgmt_ioctl_aen(file, arg);
6428 #ifdef CONFIG_COMPAT
6429 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6431 struct compat_megasas_iocpacket __user *cioc =
6432 (struct compat_megasas_iocpacket __user *)arg;
6433 struct megasas_iocpacket __user *ioc =
6434 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6439 if (clear_user(ioc, sizeof(*ioc)))
6442 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6443 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6444 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6445 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6446 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6447 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6451 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6452 * sense_len is not null, so prepare the 64bit value under
6453 * the same condition.
6455 if (ioc->sense_len) {
6456 void __user **sense_ioc_ptr =
6457 (void __user **)(ioc->frame.raw + ioc->sense_off);
6458 compat_uptr_t *sense_cioc_ptr =
6459 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
6460 if (get_user(ptr, sense_cioc_ptr) ||
6461 put_user(compat_ptr(ptr), sense_ioc_ptr))
6465 for (i = 0; i < MAX_IOCTL_SGE; i++) {
6466 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6467 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6468 copy_in_user(&ioc->sgl[i].iov_len,
6469 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6473 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6475 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6476 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6477 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6484 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6488 case MEGASAS_IOC_FIRMWARE32:
6489 return megasas_mgmt_compat_ioctl_fw(file, arg);
6490 case MEGASAS_IOC_GET_AEN:
6491 return megasas_mgmt_ioctl_aen(file, arg);
6499 * File operations structure for management interface
6501 static const struct file_operations megasas_mgmt_fops = {
6502 .owner = THIS_MODULE,
6503 .open = megasas_mgmt_open,
6504 .fasync = megasas_mgmt_fasync,
6505 .unlocked_ioctl = megasas_mgmt_ioctl,
6506 .poll = megasas_mgmt_poll,
6507 #ifdef CONFIG_COMPAT
6508 .compat_ioctl = megasas_mgmt_compat_ioctl,
6510 .llseek = noop_llseek,
6514 * PCI hotplug support registration structure
6516 static struct pci_driver megasas_pci_driver = {
6518 .name = "megaraid_sas",
6519 .id_table = megasas_pci_table,
6520 .probe = megasas_probe_one,
6521 .remove = megasas_detach_one,
6522 .suspend = megasas_suspend,
6523 .resume = megasas_resume,
6524 .shutdown = megasas_shutdown,
6528 * Sysfs driver attributes
6530 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6532 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6536 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6539 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6541 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6545 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6548 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6550 return sprintf(buf, "%u\n", support_poll_for_event);
6553 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6554 megasas_sysfs_show_support_poll_for_event, NULL);
6557 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6559 return sprintf(buf, "%u\n", support_device_change);
6562 static DRIVER_ATTR(support_device_change, S_IRUGO,
6563 megasas_sysfs_show_support_device_change, NULL);
6566 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6568 return sprintf(buf, "%u\n", megasas_dbg_lvl);
6572 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6576 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
6577 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6583 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
6584 megasas_sysfs_set_dbg_lvl);
6587 megasas_aen_polling(struct work_struct *work)
6589 struct megasas_aen_event *ev =
6590 container_of(work, struct megasas_aen_event, hotplug_work.work);
6591 struct megasas_instance *instance = ev->instance;
6592 union megasas_evt_class_locale class_locale;
6593 struct Scsi_Host *host;
6594 struct scsi_device *sdev1;
6597 int i, j, doscan = 0;
6598 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
6602 printk(KERN_ERR "invalid instance!\n");
6607 /* Adjust event workqueue thread wait time for VF mode */
6608 if (instance->requestorId)
6609 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6611 /* Don't run the event workqueue thread if OCR is running */
6612 for (i = 0; i < wait_time; i++) {
6613 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL)
6615 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6616 dev_notice(&instance->pdev->dev, "%s waiting for "
6617 "controller reset to finish for scsi%d\n",
6618 __func__, instance->host->host_no);
6623 instance->ev = NULL;
6624 host = instance->host;
6625 if (instance->evt_detail) {
6626 megasas_decode_evt(instance);
6628 switch (le32_to_cpu(instance->evt_detail->code)) {
6629 case MR_EVT_PD_INSERTED:
6630 if (megasas_get_pd_list(instance) == 0) {
6631 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6633 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6637 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6639 sdev1 = scsi_device_lookup(host, i, j, 0);
6641 if (instance->pd_list[pd_index].driveState
6642 == MR_PD_STATE_SYSTEM) {
6644 scsi_add_device(host, i, j, 0);
6647 scsi_device_put(sdev1);
6655 case MR_EVT_PD_REMOVED:
6656 if (megasas_get_pd_list(instance) == 0) {
6657 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6659 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6663 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6665 sdev1 = scsi_device_lookup(host, i, j, 0);
6667 if (instance->pd_list[pd_index].driveState
6668 == MR_PD_STATE_SYSTEM) {
6670 scsi_device_put(sdev1);
6673 scsi_remove_device(sdev1);
6674 scsi_device_put(sdev1);
6683 case MR_EVT_LD_OFFLINE:
6684 case MR_EVT_CFG_CLEARED:
6685 case MR_EVT_LD_DELETED:
6686 if (!instance->requestorId ||
6687 megasas_get_ld_vf_affiliation(instance, 0)) {
6688 if (megasas_ld_list_query(instance,
6689 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6690 megasas_get_ld_list(instance);
6691 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6693 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6697 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6699 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6701 if (instance->ld_ids[ld_index]
6704 scsi_device_put(sdev1);
6707 scsi_remove_device(sdev1);
6708 scsi_device_put(sdev1);
6716 case MR_EVT_LD_CREATED:
6717 if (!instance->requestorId ||
6718 megasas_get_ld_vf_affiliation(instance, 0)) {
6719 if (megasas_ld_list_query(instance,
6720 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6721 megasas_get_ld_list(instance);
6722 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6724 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6727 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6729 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6731 if (instance->ld_ids[ld_index]
6734 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6737 scsi_device_put(sdev1);
6743 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
6744 case MR_EVT_FOREIGN_CFG_IMPORTED:
6745 case MR_EVT_LD_STATE_CHANGE:
6748 case MR_EVT_CTRL_PROP_CHANGED:
6749 megasas_get_ctrl_info(instance);
6756 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
6762 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
6763 instance->host->host_no);
6764 if (megasas_get_pd_list(instance) == 0) {
6765 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6766 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6767 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
6768 sdev1 = scsi_device_lookup(host, i, j, 0);
6769 if (instance->pd_list[pd_index].driveState ==
6770 MR_PD_STATE_SYSTEM) {
6772 scsi_add_device(host, i, j, 0);
6775 scsi_device_put(sdev1);
6778 scsi_remove_device(sdev1);
6779 scsi_device_put(sdev1);
6786 if (!instance->requestorId ||
6787 megasas_get_ld_vf_affiliation(instance, 0)) {
6788 if (megasas_ld_list_query(instance,
6789 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6790 megasas_get_ld_list(instance);
6791 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6792 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL;
6795 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6797 sdev1 = scsi_device_lookup(host,
6798 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6799 if (instance->ld_ids[ld_index]
6802 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6804 scsi_device_put(sdev1);
6807 scsi_remove_device(sdev1);
6808 scsi_device_put(sdev1);
6816 if (instance->aen_cmd != NULL) {
6821 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
6823 /* Register AEN with FW for latest sequence number plus 1 */
6824 class_locale.members.reserved = 0;
6825 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6826 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6827 mutex_lock(&instance->aen_mutex);
6828 error = megasas_register_aen(instance, seq_num,
6830 mutex_unlock(&instance->aen_mutex);
6833 dev_err(&instance->pdev->dev, "register aen failed error %x\n", error);
6839 * megasas_init - Driver load entry point
6841 static int __init megasas_init(void)
6846 * Announce driver version and other information
6848 pr_info("megasas: %s\n", MEGASAS_VERSION);
6850 spin_lock_init(&poll_aen_lock);
6852 support_poll_for_event = 2;
6853 support_device_change = 1;
6855 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
6858 * Register character device node
6860 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
6863 printk(KERN_DEBUG "megasas: failed to open device node\n");
6867 megasas_mgmt_majorno = rval;
6870 * Register ourselves as PCI hotplug module
6872 rval = pci_register_driver(&megasas_pci_driver);
6875 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
6879 rval = driver_create_file(&megasas_pci_driver.driver,
6880 &driver_attr_version);
6882 goto err_dcf_attr_ver;
6884 rval = driver_create_file(&megasas_pci_driver.driver,
6885 &driver_attr_release_date);
6887 goto err_dcf_rel_date;
6889 rval = driver_create_file(&megasas_pci_driver.driver,
6890 &driver_attr_support_poll_for_event);
6892 goto err_dcf_support_poll_for_event;
6894 rval = driver_create_file(&megasas_pci_driver.driver,
6895 &driver_attr_dbg_lvl);
6897 goto err_dcf_dbg_lvl;
6898 rval = driver_create_file(&megasas_pci_driver.driver,
6899 &driver_attr_support_device_change);
6901 goto err_dcf_support_device_change;
6905 err_dcf_support_device_change:
6906 driver_remove_file(&megasas_pci_driver.driver,
6907 &driver_attr_dbg_lvl);
6909 driver_remove_file(&megasas_pci_driver.driver,
6910 &driver_attr_support_poll_for_event);
6911 err_dcf_support_poll_for_event:
6912 driver_remove_file(&megasas_pci_driver.driver,
6913 &driver_attr_release_date);
6915 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6917 pci_unregister_driver(&megasas_pci_driver);
6919 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6924 * megasas_exit - Driver unload entry point
6926 static void __exit megasas_exit(void)
6928 driver_remove_file(&megasas_pci_driver.driver,
6929 &driver_attr_dbg_lvl);
6930 driver_remove_file(&megasas_pci_driver.driver,
6931 &driver_attr_support_poll_for_event);
6932 driver_remove_file(&megasas_pci_driver.driver,
6933 &driver_attr_support_device_change);
6934 driver_remove_file(&megasas_pci_driver.driver,
6935 &driver_attr_release_date);
6936 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6938 pci_unregister_driver(&megasas_pci_driver);
6939 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6942 module_init(megasas_init);
6943 module_exit(megasas_exit);